]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
[Ada] split data unpacking code out of ada_value_primitive_packed_val.
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
32d0add0 3 Copyright (C) 1992-2015 Free Software Foundation, Inc.
14f9c5c9 4
a9762ec7 5 This file is part of GDB.
14f9c5c9 6
a9762ec7
JB
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
14f9c5c9 11
a9762ec7
JB
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
14f9c5c9 16
a9762ec7
JB
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 19
96d887e8 20
4c4b4cd2 21#include "defs.h"
14f9c5c9 22#include <ctype.h>
14f9c5c9 23#include "demangle.h"
4c4b4cd2
PH
24#include "gdb_regex.h"
25#include "frame.h"
14f9c5c9
AS
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "gdbcmd.h"
29#include "expression.h"
30#include "parser-defs.h"
31#include "language.h"
a53b64ea 32#include "varobj.h"
14f9c5c9
AS
33#include "c-lang.h"
34#include "inferior.h"
35#include "symfile.h"
36#include "objfiles.h"
37#include "breakpoint.h"
38#include "gdbcore.h"
4c4b4cd2
PH
39#include "hashtab.h"
40#include "gdb_obstack.h"
14f9c5c9 41#include "ada-lang.h"
4c4b4cd2 42#include "completer.h"
53ce3c39 43#include <sys/stat.h>
14f9c5c9 44#include "ui-out.h"
fe898f56 45#include "block.h"
04714b91 46#include "infcall.h"
de4f826b 47#include "dictionary.h"
f7f9143b
JB
48#include "annotate.h"
49#include "valprint.h"
9bbc9174 50#include "source.h"
0259addd 51#include "observer.h"
2ba95b9b 52#include "vec.h"
692465f1 53#include "stack.h"
fa864999 54#include "gdb_vecs.h"
79d43c61 55#include "typeprint.h"
22cee43f 56#include "namespace.h"
14f9c5c9 57
ccefe4c4 58#include "psymtab.h"
40bc484c 59#include "value.h"
956a9fb9 60#include "mi/mi-common.h"
9ac4176b 61#include "arch-utils.h"
0fcd72ba 62#include "cli/cli-utils.h"
ccefe4c4 63
4c4b4cd2 64/* Define whether or not the C operator '/' truncates towards zero for
0963b4bd 65 differently signed operands (truncation direction is undefined in C).
4c4b4cd2
PH
66 Copied from valarith.c. */
67
68#ifndef TRUNCATION_TOWARDS_ZERO
69#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
70#endif
71
d2e4a39e 72static struct type *desc_base_type (struct type *);
14f9c5c9 73
d2e4a39e 74static struct type *desc_bounds_type (struct type *);
14f9c5c9 75
d2e4a39e 76static struct value *desc_bounds (struct value *);
14f9c5c9 77
d2e4a39e 78static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 79
d2e4a39e 80static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 81
556bdfd4 82static struct type *desc_data_target_type (struct type *);
14f9c5c9 83
d2e4a39e 84static struct value *desc_data (struct value *);
14f9c5c9 85
d2e4a39e 86static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 87
d2e4a39e 88static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 89
d2e4a39e 90static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 91
d2e4a39e 92static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 93
d2e4a39e 94static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 95
d2e4a39e 96static struct type *desc_index_type (struct type *, int);
14f9c5c9 97
d2e4a39e 98static int desc_arity (struct type *);
14f9c5c9 99
d2e4a39e 100static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 101
d2e4a39e 102static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 103
40658b94
PH
104static int full_match (const char *, const char *);
105
40bc484c 106static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 107
4c4b4cd2 108static void ada_add_block_symbols (struct obstack *,
f0c5f9b2 109 const struct block *, const char *,
2570f2b7 110 domain_enum, struct objfile *, int);
14f9c5c9 111
22cee43f
PMR
112static void ada_add_all_symbols (struct obstack *, const struct block *,
113 const char *, domain_enum, int, int *);
114
d12307c1 115static int is_nonfunction (struct block_symbol *, int);
14f9c5c9 116
76a01679 117static void add_defn_to_vec (struct obstack *, struct symbol *,
f0c5f9b2 118 const struct block *);
14f9c5c9 119
4c4b4cd2
PH
120static int num_defns_collected (struct obstack *);
121
d12307c1 122static struct block_symbol *defns_collected (struct obstack *, int);
14f9c5c9 123
4c4b4cd2 124static struct value *resolve_subexp (struct expression **, int *, int,
76a01679 125 struct type *);
14f9c5c9 126
d2e4a39e 127static void replace_operator_with_call (struct expression **, int, int, int,
270140bd 128 struct symbol *, const struct block *);
14f9c5c9 129
d2e4a39e 130static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 131
4c4b4cd2
PH
132static char *ada_op_name (enum exp_opcode);
133
134static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 135
d2e4a39e 136static int numeric_type_p (struct type *);
14f9c5c9 137
d2e4a39e 138static int integer_type_p (struct type *);
14f9c5c9 139
d2e4a39e 140static int scalar_type_p (struct type *);
14f9c5c9 141
d2e4a39e 142static int discrete_type_p (struct type *);
14f9c5c9 143
aeb5907d
JB
144static enum ada_renaming_category parse_old_style_renaming (struct type *,
145 const char **,
146 int *,
147 const char **);
148
149static struct symbol *find_old_style_renaming_symbol (const char *,
270140bd 150 const struct block *);
aeb5907d 151
4c4b4cd2 152static struct type *ada_lookup_struct_elt_type (struct type *, char *,
76a01679 153 int, int, int *);
4c4b4cd2 154
d2e4a39e 155static struct value *evaluate_subexp_type (struct expression *, int *);
14f9c5c9 156
b4ba55a1
JB
157static struct type *ada_find_parallel_type_with_name (struct type *,
158 const char *);
159
d2e4a39e 160static int is_dynamic_field (struct type *, int);
14f9c5c9 161
10a2c479 162static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 163 const gdb_byte *,
4c4b4cd2
PH
164 CORE_ADDR, struct value *);
165
166static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 167
28c85d6c 168static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 169
d2e4a39e 170static struct type *to_static_fixed_type (struct type *);
f192137b 171static struct type *static_unwrap_type (struct type *type);
14f9c5c9 172
d2e4a39e 173static struct value *unwrap_value (struct value *);
14f9c5c9 174
ad82864c 175static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 176
ad82864c 177static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 178
ad82864c
JB
179static long decode_packed_array_bitsize (struct type *);
180
181static struct value *decode_constrained_packed_array (struct value *);
182
183static int ada_is_packed_array_type (struct type *);
184
185static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 186
d2e4a39e 187static struct value *value_subscript_packed (struct value *, int,
4c4b4cd2 188 struct value **);
14f9c5c9 189
50810684 190static void move_bits (gdb_byte *, int, const gdb_byte *, int, int, int);
52ce6436 191
4c4b4cd2
PH
192static struct value *coerce_unspec_val_to_type (struct value *,
193 struct type *);
14f9c5c9 194
d2e4a39e 195static struct value *get_var_value (char *, char *);
14f9c5c9 196
d2e4a39e 197static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 198
d2e4a39e 199static int equiv_types (struct type *, struct type *);
14f9c5c9 200
d2e4a39e 201static int is_name_suffix (const char *);
14f9c5c9 202
73589123
PH
203static int advance_wild_match (const char **, const char *, int);
204
205static int wild_match (const char *, const char *);
14f9c5c9 206
d2e4a39e 207static struct value *ada_coerce_ref (struct value *);
14f9c5c9 208
4c4b4cd2
PH
209static LONGEST pos_atr (struct value *);
210
3cb382c9 211static struct value *value_pos_atr (struct type *, struct value *);
14f9c5c9 212
d2e4a39e 213static struct value *value_val_atr (struct type *, struct value *);
14f9c5c9 214
4c4b4cd2
PH
215static struct symbol *standard_lookup (const char *, const struct block *,
216 domain_enum);
14f9c5c9 217
108d56a4 218static struct value *ada_search_struct_field (const char *, struct value *, int,
4c4b4cd2
PH
219 struct type *);
220
221static struct value *ada_value_primitive_field (struct value *, int, int,
222 struct type *);
223
0d5cff50 224static int find_struct_field (const char *, struct type *, int,
52ce6436 225 struct type **, int *, int *, int *, int *);
4c4b4cd2
PH
226
227static struct value *ada_to_fixed_value_create (struct type *, CORE_ADDR,
228 struct value *);
229
d12307c1 230static int ada_resolve_function (struct block_symbol *, int,
4c4b4cd2
PH
231 struct value **, int, const char *,
232 struct type *);
233
4c4b4cd2
PH
234static int ada_is_direct_array_type (struct type *);
235
72d5681a
PH
236static void ada_language_arch_info (struct gdbarch *,
237 struct language_arch_info *);
714e53ab 238
52ce6436
PH
239static struct value *ada_index_struct_field (int, struct value *, int,
240 struct type *);
241
242static struct value *assign_aggregate (struct value *, struct value *,
0963b4bd
MS
243 struct expression *,
244 int *, enum noside);
52ce6436
PH
245
246static void aggregate_assign_from_choices (struct value *, struct value *,
247 struct expression *,
248 int *, LONGEST *, int *,
249 int, LONGEST, LONGEST);
250
251static void aggregate_assign_positional (struct value *, struct value *,
252 struct expression *,
253 int *, LONGEST *, int *, int,
254 LONGEST, LONGEST);
255
256
257static void aggregate_assign_others (struct value *, struct value *,
258 struct expression *,
259 int *, LONGEST *, int, LONGEST, LONGEST);
260
261
262static void add_component_interval (LONGEST, LONGEST, LONGEST *, int *, int);
263
264
265static struct value *ada_evaluate_subexp (struct type *, struct expression *,
266 int *, enum noside);
267
268static void ada_forward_operator_length (struct expression *, int, int *,
269 int *);
852dff6c
JB
270
271static struct type *ada_find_any_type (const char *name);
4c4b4cd2
PH
272\f
273
ee01b665
JB
274/* The result of a symbol lookup to be stored in our symbol cache. */
275
276struct cache_entry
277{
278 /* The name used to perform the lookup. */
279 const char *name;
280 /* The namespace used during the lookup. */
fe978cb0 281 domain_enum domain;
ee01b665
JB
282 /* The symbol returned by the lookup, or NULL if no matching symbol
283 was found. */
284 struct symbol *sym;
285 /* The block where the symbol was found, or NULL if no matching
286 symbol was found. */
287 const struct block *block;
288 /* A pointer to the next entry with the same hash. */
289 struct cache_entry *next;
290};
291
292/* The Ada symbol cache, used to store the result of Ada-mode symbol
293 lookups in the course of executing the user's commands.
294
295 The cache is implemented using a simple, fixed-sized hash.
296 The size is fixed on the grounds that there are not likely to be
297 all that many symbols looked up during any given session, regardless
298 of the size of the symbol table. If we decide to go to a resizable
299 table, let's just use the stuff from libiberty instead. */
300
301#define HASH_SIZE 1009
302
303struct ada_symbol_cache
304{
305 /* An obstack used to store the entries in our cache. */
306 struct obstack cache_space;
307
308 /* The root of the hash table used to implement our symbol cache. */
309 struct cache_entry *root[HASH_SIZE];
310};
311
312static void ada_free_symbol_cache (struct ada_symbol_cache *sym_cache);
76a01679 313
4c4b4cd2 314/* Maximum-sized dynamic type. */
14f9c5c9
AS
315static unsigned int varsize_limit;
316
4c4b4cd2
PH
317/* FIXME: brobecker/2003-09-17: No longer a const because it is
318 returned by a function that does not return a const char *. */
319static char *ada_completer_word_break_characters =
320#ifdef VMS
321 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
322#else
14f9c5c9 323 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 324#endif
14f9c5c9 325
4c4b4cd2 326/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 327static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 328 = "__gnat_ada_main_program_name";
14f9c5c9 329
4c4b4cd2
PH
330/* Limit on the number of warnings to raise per expression evaluation. */
331static int warning_limit = 2;
332
333/* Number of warning messages issued; reset to 0 by cleanups after
334 expression evaluation. */
335static int warnings_issued = 0;
336
337static const char *known_runtime_file_name_patterns[] = {
338 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
339};
340
341static const char *known_auxiliary_function_name_patterns[] = {
342 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
343};
344
345/* Space for allocating results of ada_lookup_symbol_list. */
346static struct obstack symbol_list_obstack;
347
c6044dd1
JB
348/* Maintenance-related settings for this module. */
349
350static struct cmd_list_element *maint_set_ada_cmdlist;
351static struct cmd_list_element *maint_show_ada_cmdlist;
352
353/* Implement the "maintenance set ada" (prefix) command. */
354
355static void
356maint_set_ada_cmd (char *args, int from_tty)
357{
635c7e8a
TT
358 help_list (maint_set_ada_cmdlist, "maintenance set ada ", all_commands,
359 gdb_stdout);
c6044dd1
JB
360}
361
362/* Implement the "maintenance show ada" (prefix) command. */
363
364static void
365maint_show_ada_cmd (char *args, int from_tty)
366{
367 cmd_show_list (maint_show_ada_cmdlist, from_tty, "");
368}
369
370/* The "maintenance ada set/show ignore-descriptive-type" value. */
371
372static int ada_ignore_descriptive_types_p = 0;
373
e802dbe0
JB
374 /* Inferior-specific data. */
375
376/* Per-inferior data for this module. */
377
378struct ada_inferior_data
379{
380 /* The ada__tags__type_specific_data type, which is used when decoding
381 tagged types. With older versions of GNAT, this type was directly
382 accessible through a component ("tsd") in the object tag. But this
383 is no longer the case, so we cache it for each inferior. */
384 struct type *tsd_type;
3eecfa55
JB
385
386 /* The exception_support_info data. This data is used to determine
387 how to implement support for Ada exception catchpoints in a given
388 inferior. */
389 const struct exception_support_info *exception_info;
e802dbe0
JB
390};
391
392/* Our key to this module's inferior data. */
393static const struct inferior_data *ada_inferior_data;
394
395/* A cleanup routine for our inferior data. */
396static void
397ada_inferior_data_cleanup (struct inferior *inf, void *arg)
398{
399 struct ada_inferior_data *data;
400
9a3c8263 401 data = (struct ada_inferior_data *) inferior_data (inf, ada_inferior_data);
e802dbe0
JB
402 if (data != NULL)
403 xfree (data);
404}
405
406/* Return our inferior data for the given inferior (INF).
407
408 This function always returns a valid pointer to an allocated
409 ada_inferior_data structure. If INF's inferior data has not
410 been previously set, this functions creates a new one with all
411 fields set to zero, sets INF's inferior to it, and then returns
412 a pointer to that newly allocated ada_inferior_data. */
413
414static struct ada_inferior_data *
415get_ada_inferior_data (struct inferior *inf)
416{
417 struct ada_inferior_data *data;
418
9a3c8263 419 data = (struct ada_inferior_data *) inferior_data (inf, ada_inferior_data);
e802dbe0
JB
420 if (data == NULL)
421 {
41bf6aca 422 data = XCNEW (struct ada_inferior_data);
e802dbe0
JB
423 set_inferior_data (inf, ada_inferior_data, data);
424 }
425
426 return data;
427}
428
429/* Perform all necessary cleanups regarding our module's inferior data
430 that is required after the inferior INF just exited. */
431
432static void
433ada_inferior_exit (struct inferior *inf)
434{
435 ada_inferior_data_cleanup (inf, NULL);
436 set_inferior_data (inf, ada_inferior_data, NULL);
437}
438
ee01b665
JB
439
440 /* program-space-specific data. */
441
442/* This module's per-program-space data. */
443struct ada_pspace_data
444{
445 /* The Ada symbol cache. */
446 struct ada_symbol_cache *sym_cache;
447};
448
449/* Key to our per-program-space data. */
450static const struct program_space_data *ada_pspace_data_handle;
451
452/* Return this module's data for the given program space (PSPACE).
453 If not is found, add a zero'ed one now.
454
455 This function always returns a valid object. */
456
457static struct ada_pspace_data *
458get_ada_pspace_data (struct program_space *pspace)
459{
460 struct ada_pspace_data *data;
461
9a3c8263
SM
462 data = ((struct ada_pspace_data *)
463 program_space_data (pspace, ada_pspace_data_handle));
ee01b665
JB
464 if (data == NULL)
465 {
466 data = XCNEW (struct ada_pspace_data);
467 set_program_space_data (pspace, ada_pspace_data_handle, data);
468 }
469
470 return data;
471}
472
473/* The cleanup callback for this module's per-program-space data. */
474
475static void
476ada_pspace_data_cleanup (struct program_space *pspace, void *data)
477{
9a3c8263 478 struct ada_pspace_data *pspace_data = (struct ada_pspace_data *) data;
ee01b665
JB
479
480 if (pspace_data->sym_cache != NULL)
481 ada_free_symbol_cache (pspace_data->sym_cache);
482 xfree (pspace_data);
483}
484
4c4b4cd2
PH
485 /* Utilities */
486
720d1a40 487/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 488 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
489
490 Normally, we really expect a typedef type to only have 1 typedef layer.
491 In other words, we really expect the target type of a typedef type to be
492 a non-typedef type. This is particularly true for Ada units, because
493 the language does not have a typedef vs not-typedef distinction.
494 In that respect, the Ada compiler has been trying to eliminate as many
495 typedef definitions in the debugging information, since they generally
496 do not bring any extra information (we still use typedef under certain
497 circumstances related mostly to the GNAT encoding).
498
499 Unfortunately, we have seen situations where the debugging information
500 generated by the compiler leads to such multiple typedef layers. For
501 instance, consider the following example with stabs:
502
503 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
504 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
505
506 This is an error in the debugging information which causes type
507 pck__float_array___XUP to be defined twice, and the second time,
508 it is defined as a typedef of a typedef.
509
510 This is on the fringe of legality as far as debugging information is
511 concerned, and certainly unexpected. But it is easy to handle these
512 situations correctly, so we can afford to be lenient in this case. */
513
514static struct type *
515ada_typedef_target_type (struct type *type)
516{
517 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
518 type = TYPE_TARGET_TYPE (type);
519 return type;
520}
521
41d27058
JB
522/* Given DECODED_NAME a string holding a symbol name in its
523 decoded form (ie using the Ada dotted notation), returns
524 its unqualified name. */
525
526static const char *
527ada_unqualified_name (const char *decoded_name)
528{
2b0f535a
JB
529 const char *result;
530
531 /* If the decoded name starts with '<', it means that the encoded
532 name does not follow standard naming conventions, and thus that
533 it is not your typical Ada symbol name. Trying to unqualify it
534 is therefore pointless and possibly erroneous. */
535 if (decoded_name[0] == '<')
536 return decoded_name;
537
538 result = strrchr (decoded_name, '.');
41d27058
JB
539 if (result != NULL)
540 result++; /* Skip the dot... */
541 else
542 result = decoded_name;
543
544 return result;
545}
546
547/* Return a string starting with '<', followed by STR, and '>'.
548 The result is good until the next call. */
549
550static char *
551add_angle_brackets (const char *str)
552{
553 static char *result = NULL;
554
555 xfree (result);
88c15c34 556 result = xstrprintf ("<%s>", str);
41d27058
JB
557 return result;
558}
96d887e8 559
4c4b4cd2
PH
560static char *
561ada_get_gdb_completer_word_break_characters (void)
562{
563 return ada_completer_word_break_characters;
564}
565
e79af960
JB
566/* Print an array element index using the Ada syntax. */
567
568static void
569ada_print_array_index (struct value *index_value, struct ui_file *stream,
79a45b7d 570 const struct value_print_options *options)
e79af960 571{
79a45b7d 572 LA_VALUE_PRINT (index_value, stream, options);
e79af960
JB
573 fprintf_filtered (stream, " => ");
574}
575
f27cf670 576/* Assuming VECT points to an array of *SIZE objects of size
14f9c5c9 577 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
f27cf670 578 updating *SIZE as necessary and returning the (new) array. */
14f9c5c9 579
f27cf670
AS
580void *
581grow_vect (void *vect, size_t *size, size_t min_size, int element_size)
14f9c5c9 582{
d2e4a39e
AS
583 if (*size < min_size)
584 {
585 *size *= 2;
586 if (*size < min_size)
4c4b4cd2 587 *size = min_size;
f27cf670 588 vect = xrealloc (vect, *size * element_size);
d2e4a39e 589 }
f27cf670 590 return vect;
14f9c5c9
AS
591}
592
593/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 594 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
595
596static int
ebf56fd3 597field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
598{
599 int len = strlen (target);
5b4ee69b 600
d2e4a39e 601 return
4c4b4cd2
PH
602 (strncmp (field_name, target, len) == 0
603 && (field_name[len] == '\0'
61012eef 604 || (startswith (field_name + len, "___")
76a01679
JB
605 && strcmp (field_name + strlen (field_name) - 6,
606 "___XVN") != 0)));
14f9c5c9
AS
607}
608
609
872c8b51
JB
610/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
611 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
612 and return its index. This function also handles fields whose name
613 have ___ suffixes because the compiler sometimes alters their name
614 by adding such a suffix to represent fields with certain constraints.
615 If the field could not be found, return a negative number if
616 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
617
618int
619ada_get_field_index (const struct type *type, const char *field_name,
620 int maybe_missing)
621{
622 int fieldno;
872c8b51
JB
623 struct type *struct_type = check_typedef ((struct type *) type);
624
625 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); fieldno++)
626 if (field_name_match (TYPE_FIELD_NAME (struct_type, fieldno), field_name))
4c4b4cd2
PH
627 return fieldno;
628
629 if (!maybe_missing)
323e0a4a 630 error (_("Unable to find field %s in struct %s. Aborting"),
872c8b51 631 field_name, TYPE_NAME (struct_type));
4c4b4cd2
PH
632
633 return -1;
634}
635
636/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
637
638int
d2e4a39e 639ada_name_prefix_len (const char *name)
14f9c5c9
AS
640{
641 if (name == NULL)
642 return 0;
d2e4a39e 643 else
14f9c5c9 644 {
d2e4a39e 645 const char *p = strstr (name, "___");
5b4ee69b 646
14f9c5c9 647 if (p == NULL)
4c4b4cd2 648 return strlen (name);
14f9c5c9 649 else
4c4b4cd2 650 return p - name;
14f9c5c9
AS
651 }
652}
653
4c4b4cd2
PH
654/* Return non-zero if SUFFIX is a suffix of STR.
655 Return zero if STR is null. */
656
14f9c5c9 657static int
d2e4a39e 658is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
659{
660 int len1, len2;
5b4ee69b 661
14f9c5c9
AS
662 if (str == NULL)
663 return 0;
664 len1 = strlen (str);
665 len2 = strlen (suffix);
4c4b4cd2 666 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
667}
668
4c4b4cd2
PH
669/* The contents of value VAL, treated as a value of type TYPE. The
670 result is an lval in memory if VAL is. */
14f9c5c9 671
d2e4a39e 672static struct value *
4c4b4cd2 673coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 674{
61ee279c 675 type = ada_check_typedef (type);
df407dfe 676 if (value_type (val) == type)
4c4b4cd2 677 return val;
d2e4a39e 678 else
14f9c5c9 679 {
4c4b4cd2
PH
680 struct value *result;
681
682 /* Make sure that the object size is not unreasonable before
683 trying to allocate some memory for it. */
c1b5a1a6 684 ada_ensure_varsize_limit (type);
4c4b4cd2 685
41e8491f
JK
686 if (value_lazy (val)
687 || TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
688 result = allocate_value_lazy (type);
689 else
690 {
691 result = allocate_value (type);
9a0dc9e3 692 value_contents_copy_raw (result, 0, val, 0, TYPE_LENGTH (type));
41e8491f 693 }
74bcbdf3 694 set_value_component_location (result, val);
9bbda503
AC
695 set_value_bitsize (result, value_bitsize (val));
696 set_value_bitpos (result, value_bitpos (val));
42ae5230 697 set_value_address (result, value_address (val));
14f9c5c9
AS
698 return result;
699 }
700}
701
fc1a4b47
AC
702static const gdb_byte *
703cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
704{
705 if (valaddr == NULL)
706 return NULL;
707 else
708 return valaddr + offset;
709}
710
711static CORE_ADDR
ebf56fd3 712cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
713{
714 if (address == 0)
715 return 0;
d2e4a39e 716 else
14f9c5c9
AS
717 return address + offset;
718}
719
4c4b4cd2
PH
720/* Issue a warning (as for the definition of warning in utils.c, but
721 with exactly one argument rather than ...), unless the limit on the
722 number of warnings has passed during the evaluation of the current
723 expression. */
a2249542 724
77109804
AC
725/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
726 provided by "complaint". */
a0b31db1 727static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 728
14f9c5c9 729static void
a2249542 730lim_warning (const char *format, ...)
14f9c5c9 731{
a2249542 732 va_list args;
a2249542 733
5b4ee69b 734 va_start (args, format);
4c4b4cd2
PH
735 warnings_issued += 1;
736 if (warnings_issued <= warning_limit)
a2249542
MK
737 vwarning (format, args);
738
739 va_end (args);
4c4b4cd2
PH
740}
741
714e53ab
PH
742/* Issue an error if the size of an object of type T is unreasonable,
743 i.e. if it would be a bad idea to allocate a value of this type in
744 GDB. */
745
c1b5a1a6
JB
746void
747ada_ensure_varsize_limit (const struct type *type)
714e53ab
PH
748{
749 if (TYPE_LENGTH (type) > varsize_limit)
323e0a4a 750 error (_("object size is larger than varsize-limit"));
714e53ab
PH
751}
752
0963b4bd 753/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 754static LONGEST
c3e5cd34 755max_of_size (int size)
4c4b4cd2 756{
76a01679 757 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 758
76a01679 759 return top_bit | (top_bit - 1);
4c4b4cd2
PH
760}
761
0963b4bd 762/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 763static LONGEST
c3e5cd34 764min_of_size (int size)
4c4b4cd2 765{
c3e5cd34 766 return -max_of_size (size) - 1;
4c4b4cd2
PH
767}
768
0963b4bd 769/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 770static ULONGEST
c3e5cd34 771umax_of_size (int size)
4c4b4cd2 772{
76a01679 773 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 774
76a01679 775 return top_bit | (top_bit - 1);
4c4b4cd2
PH
776}
777
0963b4bd 778/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
779static LONGEST
780max_of_type (struct type *t)
4c4b4cd2 781{
c3e5cd34
PH
782 if (TYPE_UNSIGNED (t))
783 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
784 else
785 return max_of_size (TYPE_LENGTH (t));
786}
787
0963b4bd 788/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
789static LONGEST
790min_of_type (struct type *t)
791{
792 if (TYPE_UNSIGNED (t))
793 return 0;
794 else
795 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
796}
797
798/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
799LONGEST
800ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 801{
c3345124 802 type = resolve_dynamic_type (type, NULL, 0);
76a01679 803 switch (TYPE_CODE (type))
4c4b4cd2
PH
804 {
805 case TYPE_CODE_RANGE:
690cc4eb 806 return TYPE_HIGH_BOUND (type);
4c4b4cd2 807 case TYPE_CODE_ENUM:
14e75d8e 808 return TYPE_FIELD_ENUMVAL (type, TYPE_NFIELDS (type) - 1);
690cc4eb
PH
809 case TYPE_CODE_BOOL:
810 return 1;
811 case TYPE_CODE_CHAR:
76a01679 812 case TYPE_CODE_INT:
690cc4eb 813 return max_of_type (type);
4c4b4cd2 814 default:
43bbcdc2 815 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
816 }
817}
818
14e75d8e 819/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
820LONGEST
821ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 822{
c3345124 823 type = resolve_dynamic_type (type, NULL, 0);
76a01679 824 switch (TYPE_CODE (type))
4c4b4cd2
PH
825 {
826 case TYPE_CODE_RANGE:
690cc4eb 827 return TYPE_LOW_BOUND (type);
4c4b4cd2 828 case TYPE_CODE_ENUM:
14e75d8e 829 return TYPE_FIELD_ENUMVAL (type, 0);
690cc4eb
PH
830 case TYPE_CODE_BOOL:
831 return 0;
832 case TYPE_CODE_CHAR:
76a01679 833 case TYPE_CODE_INT:
690cc4eb 834 return min_of_type (type);
4c4b4cd2 835 default:
43bbcdc2 836 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
837 }
838}
839
840/* The identity on non-range types. For range types, the underlying
76a01679 841 non-range scalar type. */
4c4b4cd2
PH
842
843static struct type *
18af8284 844get_base_type (struct type *type)
4c4b4cd2
PH
845{
846 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
847 {
76a01679
JB
848 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
849 return type;
4c4b4cd2
PH
850 type = TYPE_TARGET_TYPE (type);
851 }
852 return type;
14f9c5c9 853}
41246937
JB
854
855/* Return a decoded version of the given VALUE. This means returning
856 a value whose type is obtained by applying all the GNAT-specific
857 encondings, making the resulting type a static but standard description
858 of the initial type. */
859
860struct value *
861ada_get_decoded_value (struct value *value)
862{
863 struct type *type = ada_check_typedef (value_type (value));
864
865 if (ada_is_array_descriptor_type (type)
866 || (ada_is_constrained_packed_array_type (type)
867 && TYPE_CODE (type) != TYPE_CODE_PTR))
868 {
869 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
870 value = ada_coerce_to_simple_array_ptr (value);
871 else
872 value = ada_coerce_to_simple_array (value);
873 }
874 else
875 value = ada_to_fixed_value (value);
876
877 return value;
878}
879
880/* Same as ada_get_decoded_value, but with the given TYPE.
881 Because there is no associated actual value for this type,
882 the resulting type might be a best-effort approximation in
883 the case of dynamic types. */
884
885struct type *
886ada_get_decoded_type (struct type *type)
887{
888 type = to_static_fixed_type (type);
889 if (ada_is_constrained_packed_array_type (type))
890 type = ada_coerce_to_simple_array_type (type);
891 return type;
892}
893
4c4b4cd2 894\f
76a01679 895
4c4b4cd2 896 /* Language Selection */
14f9c5c9
AS
897
898/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 899 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 900
14f9c5c9 901enum language
ccefe4c4 902ada_update_initial_language (enum language lang)
14f9c5c9 903{
d2e4a39e 904 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
3b7344d5 905 (struct objfile *) NULL).minsym != NULL)
4c4b4cd2 906 return language_ada;
14f9c5c9
AS
907
908 return lang;
909}
96d887e8
PH
910
911/* If the main procedure is written in Ada, then return its name.
912 The result is good until the next call. Return NULL if the main
913 procedure doesn't appear to be in Ada. */
914
915char *
916ada_main_name (void)
917{
3b7344d5 918 struct bound_minimal_symbol msym;
f9bc20b9 919 static char *main_program_name = NULL;
6c038f32 920
96d887e8
PH
921 /* For Ada, the name of the main procedure is stored in a specific
922 string constant, generated by the binder. Look for that symbol,
923 extract its address, and then read that string. If we didn't find
924 that string, then most probably the main procedure is not written
925 in Ada. */
926 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
927
3b7344d5 928 if (msym.minsym != NULL)
96d887e8 929 {
f9bc20b9
JB
930 CORE_ADDR main_program_name_addr;
931 int err_code;
932
77e371c0 933 main_program_name_addr = BMSYMBOL_VALUE_ADDRESS (msym);
96d887e8 934 if (main_program_name_addr == 0)
323e0a4a 935 error (_("Invalid address for Ada main program name."));
96d887e8 936
f9bc20b9
JB
937 xfree (main_program_name);
938 target_read_string (main_program_name_addr, &main_program_name,
939 1024, &err_code);
940
941 if (err_code != 0)
942 return NULL;
96d887e8
PH
943 return main_program_name;
944 }
945
946 /* The main procedure doesn't seem to be in Ada. */
947 return NULL;
948}
14f9c5c9 949\f
4c4b4cd2 950 /* Symbols */
d2e4a39e 951
4c4b4cd2
PH
952/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
953 of NULLs. */
14f9c5c9 954
d2e4a39e
AS
955const struct ada_opname_map ada_opname_table[] = {
956 {"Oadd", "\"+\"", BINOP_ADD},
957 {"Osubtract", "\"-\"", BINOP_SUB},
958 {"Omultiply", "\"*\"", BINOP_MUL},
959 {"Odivide", "\"/\"", BINOP_DIV},
960 {"Omod", "\"mod\"", BINOP_MOD},
961 {"Orem", "\"rem\"", BINOP_REM},
962 {"Oexpon", "\"**\"", BINOP_EXP},
963 {"Olt", "\"<\"", BINOP_LESS},
964 {"Ole", "\"<=\"", BINOP_LEQ},
965 {"Ogt", "\">\"", BINOP_GTR},
966 {"Oge", "\">=\"", BINOP_GEQ},
967 {"Oeq", "\"=\"", BINOP_EQUAL},
968 {"One", "\"/=\"", BINOP_NOTEQUAL},
969 {"Oand", "\"and\"", BINOP_BITWISE_AND},
970 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
971 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
972 {"Oconcat", "\"&\"", BINOP_CONCAT},
973 {"Oabs", "\"abs\"", UNOP_ABS},
974 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
975 {"Oadd", "\"+\"", UNOP_PLUS},
976 {"Osubtract", "\"-\"", UNOP_NEG},
977 {NULL, NULL}
14f9c5c9
AS
978};
979
4c4b4cd2
PH
980/* The "encoded" form of DECODED, according to GNAT conventions.
981 The result is valid until the next call to ada_encode. */
982
14f9c5c9 983char *
4c4b4cd2 984ada_encode (const char *decoded)
14f9c5c9 985{
4c4b4cd2
PH
986 static char *encoding_buffer = NULL;
987 static size_t encoding_buffer_size = 0;
d2e4a39e 988 const char *p;
14f9c5c9 989 int k;
d2e4a39e 990
4c4b4cd2 991 if (decoded == NULL)
14f9c5c9
AS
992 return NULL;
993
4c4b4cd2
PH
994 GROW_VECT (encoding_buffer, encoding_buffer_size,
995 2 * strlen (decoded) + 10);
14f9c5c9
AS
996
997 k = 0;
4c4b4cd2 998 for (p = decoded; *p != '\0'; p += 1)
14f9c5c9 999 {
cdc7bb92 1000 if (*p == '.')
4c4b4cd2
PH
1001 {
1002 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
1003 k += 2;
1004 }
14f9c5c9 1005 else if (*p == '"')
4c4b4cd2
PH
1006 {
1007 const struct ada_opname_map *mapping;
1008
1009 for (mapping = ada_opname_table;
1265e4aa 1010 mapping->encoded != NULL
61012eef 1011 && !startswith (p, mapping->decoded); mapping += 1)
4c4b4cd2
PH
1012 ;
1013 if (mapping->encoded == NULL)
323e0a4a 1014 error (_("invalid Ada operator name: %s"), p);
4c4b4cd2
PH
1015 strcpy (encoding_buffer + k, mapping->encoded);
1016 k += strlen (mapping->encoded);
1017 break;
1018 }
d2e4a39e 1019 else
4c4b4cd2
PH
1020 {
1021 encoding_buffer[k] = *p;
1022 k += 1;
1023 }
14f9c5c9
AS
1024 }
1025
4c4b4cd2
PH
1026 encoding_buffer[k] = '\0';
1027 return encoding_buffer;
14f9c5c9
AS
1028}
1029
1030/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
1031 quotes, unfolded, but with the quotes stripped away. Result good
1032 to next call. */
1033
d2e4a39e
AS
1034char *
1035ada_fold_name (const char *name)
14f9c5c9 1036{
d2e4a39e 1037 static char *fold_buffer = NULL;
14f9c5c9
AS
1038 static size_t fold_buffer_size = 0;
1039
1040 int len = strlen (name);
d2e4a39e 1041 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
14f9c5c9
AS
1042
1043 if (name[0] == '\'')
1044 {
d2e4a39e
AS
1045 strncpy (fold_buffer, name + 1, len - 2);
1046 fold_buffer[len - 2] = '\000';
14f9c5c9
AS
1047 }
1048 else
1049 {
1050 int i;
5b4ee69b 1051
14f9c5c9 1052 for (i = 0; i <= len; i += 1)
4c4b4cd2 1053 fold_buffer[i] = tolower (name[i]);
14f9c5c9
AS
1054 }
1055
1056 return fold_buffer;
1057}
1058
529cad9c
PH
1059/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1060
1061static int
1062is_lower_alphanum (const char c)
1063{
1064 return (isdigit (c) || (isalpha (c) && islower (c)));
1065}
1066
c90092fe
JB
1067/* ENCODED is the linkage name of a symbol and LEN contains its length.
1068 This function saves in LEN the length of that same symbol name but
1069 without either of these suffixes:
29480c32
JB
1070 . .{DIGIT}+
1071 . ${DIGIT}+
1072 . ___{DIGIT}+
1073 . __{DIGIT}+.
c90092fe 1074
29480c32
JB
1075 These are suffixes introduced by the compiler for entities such as
1076 nested subprogram for instance, in order to avoid name clashes.
1077 They do not serve any purpose for the debugger. */
1078
1079static void
1080ada_remove_trailing_digits (const char *encoded, int *len)
1081{
1082 if (*len > 1 && isdigit (encoded[*len - 1]))
1083 {
1084 int i = *len - 2;
5b4ee69b 1085
29480c32
JB
1086 while (i > 0 && isdigit (encoded[i]))
1087 i--;
1088 if (i >= 0 && encoded[i] == '.')
1089 *len = i;
1090 else if (i >= 0 && encoded[i] == '$')
1091 *len = i;
61012eef 1092 else if (i >= 2 && startswith (encoded + i - 2, "___"))
29480c32 1093 *len = i - 2;
61012eef 1094 else if (i >= 1 && startswith (encoded + i - 1, "__"))
29480c32
JB
1095 *len = i - 1;
1096 }
1097}
1098
1099/* Remove the suffix introduced by the compiler for protected object
1100 subprograms. */
1101
1102static void
1103ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1104{
1105 /* Remove trailing N. */
1106
1107 /* Protected entry subprograms are broken into two
1108 separate subprograms: The first one is unprotected, and has
1109 a 'N' suffix; the second is the protected version, and has
0963b4bd 1110 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1111 the protection. Since the P subprograms are internally generated,
1112 we leave these names undecoded, giving the user a clue that this
1113 entity is internal. */
1114
1115 if (*len > 1
1116 && encoded[*len - 1] == 'N'
1117 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1118 *len = *len - 1;
1119}
1120
69fadcdf
JB
1121/* Remove trailing X[bn]* suffixes (indicating names in package bodies). */
1122
1123static void
1124ada_remove_Xbn_suffix (const char *encoded, int *len)
1125{
1126 int i = *len - 1;
1127
1128 while (i > 0 && (encoded[i] == 'b' || encoded[i] == 'n'))
1129 i--;
1130
1131 if (encoded[i] != 'X')
1132 return;
1133
1134 if (i == 0)
1135 return;
1136
1137 if (isalnum (encoded[i-1]))
1138 *len = i;
1139}
1140
29480c32
JB
1141/* If ENCODED follows the GNAT entity encoding conventions, then return
1142 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
1143 replaced by ENCODED.
14f9c5c9 1144
4c4b4cd2 1145 The resulting string is valid until the next call of ada_decode.
29480c32 1146 If the string is unchanged by decoding, the original string pointer
4c4b4cd2
PH
1147 is returned. */
1148
1149const char *
1150ada_decode (const char *encoded)
14f9c5c9
AS
1151{
1152 int i, j;
1153 int len0;
d2e4a39e 1154 const char *p;
4c4b4cd2 1155 char *decoded;
14f9c5c9 1156 int at_start_name;
4c4b4cd2
PH
1157 static char *decoding_buffer = NULL;
1158 static size_t decoding_buffer_size = 0;
d2e4a39e 1159
29480c32
JB
1160 /* The name of the Ada main procedure starts with "_ada_".
1161 This prefix is not part of the decoded name, so skip this part
1162 if we see this prefix. */
61012eef 1163 if (startswith (encoded, "_ada_"))
4c4b4cd2 1164 encoded += 5;
14f9c5c9 1165
29480c32
JB
1166 /* If the name starts with '_', then it is not a properly encoded
1167 name, so do not attempt to decode it. Similarly, if the name
1168 starts with '<', the name should not be decoded. */
4c4b4cd2 1169 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1170 goto Suppress;
1171
4c4b4cd2 1172 len0 = strlen (encoded);
4c4b4cd2 1173
29480c32
JB
1174 ada_remove_trailing_digits (encoded, &len0);
1175 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1176
4c4b4cd2
PH
1177 /* Remove the ___X.* suffix if present. Do not forget to verify that
1178 the suffix is located before the current "end" of ENCODED. We want
1179 to avoid re-matching parts of ENCODED that have previously been
1180 marked as discarded (by decrementing LEN0). */
1181 p = strstr (encoded, "___");
1182 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1183 {
1184 if (p[3] == 'X')
4c4b4cd2 1185 len0 = p - encoded;
14f9c5c9 1186 else
4c4b4cd2 1187 goto Suppress;
14f9c5c9 1188 }
4c4b4cd2 1189
29480c32
JB
1190 /* Remove any trailing TKB suffix. It tells us that this symbol
1191 is for the body of a task, but that information does not actually
1192 appear in the decoded name. */
1193
61012eef 1194 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1195 len0 -= 3;
76a01679 1196
a10967fa
JB
1197 /* Remove any trailing TB suffix. The TB suffix is slightly different
1198 from the TKB suffix because it is used for non-anonymous task
1199 bodies. */
1200
61012eef 1201 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1202 len0 -= 2;
1203
29480c32
JB
1204 /* Remove trailing "B" suffixes. */
1205 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1206
61012eef 1207 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1208 len0 -= 1;
1209
4c4b4cd2 1210 /* Make decoded big enough for possible expansion by operator name. */
29480c32 1211
4c4b4cd2
PH
1212 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
1213 decoded = decoding_buffer;
14f9c5c9 1214
29480c32
JB
1215 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1216
4c4b4cd2 1217 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1218 {
4c4b4cd2
PH
1219 i = len0 - 2;
1220 while ((i >= 0 && isdigit (encoded[i]))
1221 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1222 i -= 1;
1223 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
1224 len0 = i - 1;
1225 else if (encoded[i] == '$')
1226 len0 = i;
d2e4a39e 1227 }
14f9c5c9 1228
29480c32
JB
1229 /* The first few characters that are not alphabetic are not part
1230 of any encoding we use, so we can copy them over verbatim. */
1231
4c4b4cd2
PH
1232 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1233 decoded[j] = encoded[i];
14f9c5c9
AS
1234
1235 at_start_name = 1;
1236 while (i < len0)
1237 {
29480c32 1238 /* Is this a symbol function? */
4c4b4cd2
PH
1239 if (at_start_name && encoded[i] == 'O')
1240 {
1241 int k;
5b4ee69b 1242
4c4b4cd2
PH
1243 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1244 {
1245 int op_len = strlen (ada_opname_table[k].encoded);
06d5cf63
JB
1246 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1247 op_len - 1) == 0)
1248 && !isalnum (encoded[i + op_len]))
4c4b4cd2
PH
1249 {
1250 strcpy (decoded + j, ada_opname_table[k].decoded);
1251 at_start_name = 0;
1252 i += op_len;
1253 j += strlen (ada_opname_table[k].decoded);
1254 break;
1255 }
1256 }
1257 if (ada_opname_table[k].encoded != NULL)
1258 continue;
1259 }
14f9c5c9
AS
1260 at_start_name = 0;
1261
529cad9c
PH
1262 /* Replace "TK__" with "__", which will eventually be translated
1263 into "." (just below). */
1264
61012eef 1265 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
4c4b4cd2 1266 i += 2;
529cad9c 1267
29480c32
JB
1268 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1269 be translated into "." (just below). These are internal names
1270 generated for anonymous blocks inside which our symbol is nested. */
1271
1272 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
1273 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1274 && isdigit (encoded [i+4]))
1275 {
1276 int k = i + 5;
1277
1278 while (k < len0 && isdigit (encoded[k]))
1279 k++; /* Skip any extra digit. */
1280
1281 /* Double-check that the "__B_{DIGITS}+" sequence we found
1282 is indeed followed by "__". */
1283 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1284 i = k;
1285 }
1286
529cad9c
PH
1287 /* Remove _E{DIGITS}+[sb] */
1288
1289 /* Just as for protected object subprograms, there are 2 categories
0963b4bd 1290 of subprograms created by the compiler for each entry. The first
529cad9c
PH
1291 one implements the actual entry code, and has a suffix following
1292 the convention above; the second one implements the barrier and
1293 uses the same convention as above, except that the 'E' is replaced
1294 by a 'B'.
1295
1296 Just as above, we do not decode the name of barrier functions
1297 to give the user a clue that the code he is debugging has been
1298 internally generated. */
1299
1300 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
1301 && isdigit (encoded[i+2]))
1302 {
1303 int k = i + 3;
1304
1305 while (k < len0 && isdigit (encoded[k]))
1306 k++;
1307
1308 if (k < len0
1309 && (encoded[k] == 'b' || encoded[k] == 's'))
1310 {
1311 k++;
1312 /* Just as an extra precaution, make sure that if this
1313 suffix is followed by anything else, it is a '_'.
1314 Otherwise, we matched this sequence by accident. */
1315 if (k == len0
1316 || (k < len0 && encoded[k] == '_'))
1317 i = k;
1318 }
1319 }
1320
1321 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1322 the GNAT front-end in protected object subprograms. */
1323
1324 if (i < len0 + 3
1325 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1326 {
1327 /* Backtrack a bit up until we reach either the begining of
1328 the encoded name, or "__". Make sure that we only find
1329 digits or lowercase characters. */
1330 const char *ptr = encoded + i - 1;
1331
1332 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1333 ptr--;
1334 if (ptr < encoded
1335 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1336 i++;
1337 }
1338
4c4b4cd2
PH
1339 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
1340 {
29480c32
JB
1341 /* This is a X[bn]* sequence not separated from the previous
1342 part of the name with a non-alpha-numeric character (in other
1343 words, immediately following an alpha-numeric character), then
1344 verify that it is placed at the end of the encoded name. If
1345 not, then the encoding is not valid and we should abort the
1346 decoding. Otherwise, just skip it, it is used in body-nested
1347 package names. */
4c4b4cd2
PH
1348 do
1349 i += 1;
1350 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1351 if (i < len0)
1352 goto Suppress;
1353 }
cdc7bb92 1354 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
4c4b4cd2 1355 {
29480c32 1356 /* Replace '__' by '.'. */
4c4b4cd2
PH
1357 decoded[j] = '.';
1358 at_start_name = 1;
1359 i += 2;
1360 j += 1;
1361 }
14f9c5c9 1362 else
4c4b4cd2 1363 {
29480c32
JB
1364 /* It's a character part of the decoded name, so just copy it
1365 over. */
4c4b4cd2
PH
1366 decoded[j] = encoded[i];
1367 i += 1;
1368 j += 1;
1369 }
14f9c5c9 1370 }
4c4b4cd2 1371 decoded[j] = '\000';
14f9c5c9 1372
29480c32
JB
1373 /* Decoded names should never contain any uppercase character.
1374 Double-check this, and abort the decoding if we find one. */
1375
4c4b4cd2
PH
1376 for (i = 0; decoded[i] != '\0'; i += 1)
1377 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1378 goto Suppress;
1379
4c4b4cd2
PH
1380 if (strcmp (decoded, encoded) == 0)
1381 return encoded;
1382 else
1383 return decoded;
14f9c5c9
AS
1384
1385Suppress:
4c4b4cd2
PH
1386 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
1387 decoded = decoding_buffer;
1388 if (encoded[0] == '<')
1389 strcpy (decoded, encoded);
14f9c5c9 1390 else
88c15c34 1391 xsnprintf (decoded, decoding_buffer_size, "<%s>", encoded);
4c4b4cd2
PH
1392 return decoded;
1393
1394}
1395
1396/* Table for keeping permanent unique copies of decoded names. Once
1397 allocated, names in this table are never released. While this is a
1398 storage leak, it should not be significant unless there are massive
1399 changes in the set of decoded names in successive versions of a
1400 symbol table loaded during a single session. */
1401static struct htab *decoded_names_store;
1402
1403/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1404 in the language-specific part of GSYMBOL, if it has not been
1405 previously computed. Tries to save the decoded name in the same
1406 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1407 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1408 GSYMBOL).
4c4b4cd2
PH
1409 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1410 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1411 when a decoded name is cached in it. */
4c4b4cd2 1412
45e6c716 1413const char *
f85f34ed 1414ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1415{
f85f34ed
TT
1416 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1417 const char **resultp =
615b3f62 1418 &gsymbol->language_specific.demangled_name;
5b4ee69b 1419
f85f34ed 1420 if (!gsymbol->ada_mangled)
4c4b4cd2
PH
1421 {
1422 const char *decoded = ada_decode (gsymbol->name);
f85f34ed 1423 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1424
f85f34ed 1425 gsymbol->ada_mangled = 1;
5b4ee69b 1426
f85f34ed 1427 if (obstack != NULL)
224c3ddb
SM
1428 *resultp
1429 = (const char *) obstack_copy0 (obstack, decoded, strlen (decoded));
f85f34ed 1430 else
76a01679 1431 {
f85f34ed
TT
1432 /* Sometimes, we can't find a corresponding objfile, in
1433 which case, we put the result on the heap. Since we only
1434 decode when needed, we hope this usually does not cause a
1435 significant memory leak (FIXME). */
1436
76a01679
JB
1437 char **slot = (char **) htab_find_slot (decoded_names_store,
1438 decoded, INSERT);
5b4ee69b 1439
76a01679
JB
1440 if (*slot == NULL)
1441 *slot = xstrdup (decoded);
1442 *resultp = *slot;
1443 }
4c4b4cd2 1444 }
14f9c5c9 1445
4c4b4cd2
PH
1446 return *resultp;
1447}
76a01679 1448
2c0b251b 1449static char *
76a01679 1450ada_la_decode (const char *encoded, int options)
4c4b4cd2
PH
1451{
1452 return xstrdup (ada_decode (encoded));
14f9c5c9
AS
1453}
1454
1455/* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
4c4b4cd2
PH
1456 suffixes that encode debugging information or leading _ada_ on
1457 SYM_NAME (see is_name_suffix commentary for the debugging
1458 information that is ignored). If WILD, then NAME need only match a
1459 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1460 either argument is NULL. */
14f9c5c9 1461
2c0b251b 1462static int
40658b94 1463match_name (const char *sym_name, const char *name, int wild)
14f9c5c9
AS
1464{
1465 if (sym_name == NULL || name == NULL)
1466 return 0;
1467 else if (wild)
73589123 1468 return wild_match (sym_name, name) == 0;
d2e4a39e
AS
1469 else
1470 {
1471 int len_name = strlen (name);
5b4ee69b 1472
4c4b4cd2
PH
1473 return (strncmp (sym_name, name, len_name) == 0
1474 && is_name_suffix (sym_name + len_name))
61012eef 1475 || (startswith (sym_name, "_ada_")
4c4b4cd2
PH
1476 && strncmp (sym_name + 5, name, len_name) == 0
1477 && is_name_suffix (sym_name + len_name + 5));
d2e4a39e 1478 }
14f9c5c9 1479}
14f9c5c9 1480\f
d2e4a39e 1481
4c4b4cd2 1482 /* Arrays */
14f9c5c9 1483
28c85d6c
JB
1484/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1485 generated by the GNAT compiler to describe the index type used
1486 for each dimension of an array, check whether it follows the latest
1487 known encoding. If not, fix it up to conform to the latest encoding.
1488 Otherwise, do nothing. This function also does nothing if
1489 INDEX_DESC_TYPE is NULL.
1490
1491 The GNAT encoding used to describle the array index type evolved a bit.
1492 Initially, the information would be provided through the name of each
1493 field of the structure type only, while the type of these fields was
1494 described as unspecified and irrelevant. The debugger was then expected
1495 to perform a global type lookup using the name of that field in order
1496 to get access to the full index type description. Because these global
1497 lookups can be very expensive, the encoding was later enhanced to make
1498 the global lookup unnecessary by defining the field type as being
1499 the full index type description.
1500
1501 The purpose of this routine is to allow us to support older versions
1502 of the compiler by detecting the use of the older encoding, and by
1503 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1504 we essentially replace each field's meaningless type by the associated
1505 index subtype). */
1506
1507void
1508ada_fixup_array_indexes_type (struct type *index_desc_type)
1509{
1510 int i;
1511
1512 if (index_desc_type == NULL)
1513 return;
1514 gdb_assert (TYPE_NFIELDS (index_desc_type) > 0);
1515
1516 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1517 to check one field only, no need to check them all). If not, return
1518 now.
1519
1520 If our INDEX_DESC_TYPE was generated using the older encoding,
1521 the field type should be a meaningless integer type whose name
1522 is not equal to the field name. */
1523 if (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)) != NULL
1524 && strcmp (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)),
1525 TYPE_FIELD_NAME (index_desc_type, 0)) == 0)
1526 return;
1527
1528 /* Fixup each field of INDEX_DESC_TYPE. */
1529 for (i = 0; i < TYPE_NFIELDS (index_desc_type); i++)
1530 {
0d5cff50 1531 const char *name = TYPE_FIELD_NAME (index_desc_type, i);
28c85d6c
JB
1532 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1533
1534 if (raw_type)
1535 TYPE_FIELD_TYPE (index_desc_type, i) = raw_type;
1536 }
1537}
1538
4c4b4cd2 1539/* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
14f9c5c9 1540
d2e4a39e
AS
1541static char *bound_name[] = {
1542 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
14f9c5c9
AS
1543 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1544};
1545
1546/* Maximum number of array dimensions we are prepared to handle. */
1547
4c4b4cd2 1548#define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
14f9c5c9 1549
14f9c5c9 1550
4c4b4cd2
PH
1551/* The desc_* routines return primitive portions of array descriptors
1552 (fat pointers). */
14f9c5c9
AS
1553
1554/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1555 level of indirection, if needed. */
1556
d2e4a39e
AS
1557static struct type *
1558desc_base_type (struct type *type)
14f9c5c9
AS
1559{
1560 if (type == NULL)
1561 return NULL;
61ee279c 1562 type = ada_check_typedef (type);
720d1a40
JB
1563 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1564 type = ada_typedef_target_type (type);
1565
1265e4aa
JB
1566 if (type != NULL
1567 && (TYPE_CODE (type) == TYPE_CODE_PTR
1568 || TYPE_CODE (type) == TYPE_CODE_REF))
61ee279c 1569 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1570 else
1571 return type;
1572}
1573
4c4b4cd2
PH
1574/* True iff TYPE indicates a "thin" array pointer type. */
1575
14f9c5c9 1576static int
d2e4a39e 1577is_thin_pntr (struct type *type)
14f9c5c9 1578{
d2e4a39e 1579 return
14f9c5c9
AS
1580 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1581 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1582}
1583
4c4b4cd2
PH
1584/* The descriptor type for thin pointer type TYPE. */
1585
d2e4a39e
AS
1586static struct type *
1587thin_descriptor_type (struct type *type)
14f9c5c9 1588{
d2e4a39e 1589 struct type *base_type = desc_base_type (type);
5b4ee69b 1590
14f9c5c9
AS
1591 if (base_type == NULL)
1592 return NULL;
1593 if (is_suffix (ada_type_name (base_type), "___XVE"))
1594 return base_type;
d2e4a39e 1595 else
14f9c5c9 1596 {
d2e4a39e 1597 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1598
14f9c5c9 1599 if (alt_type == NULL)
4c4b4cd2 1600 return base_type;
14f9c5c9 1601 else
4c4b4cd2 1602 return alt_type;
14f9c5c9
AS
1603 }
1604}
1605
4c4b4cd2
PH
1606/* A pointer to the array data for thin-pointer value VAL. */
1607
d2e4a39e
AS
1608static struct value *
1609thin_data_pntr (struct value *val)
14f9c5c9 1610{
828292f2 1611 struct type *type = ada_check_typedef (value_type (val));
556bdfd4 1612 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1613
556bdfd4
UW
1614 data_type = lookup_pointer_type (data_type);
1615
14f9c5c9 1616 if (TYPE_CODE (type) == TYPE_CODE_PTR)
556bdfd4 1617 return value_cast (data_type, value_copy (val));
d2e4a39e 1618 else
42ae5230 1619 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1620}
1621
4c4b4cd2
PH
1622/* True iff TYPE indicates a "thick" array pointer type. */
1623
14f9c5c9 1624static int
d2e4a39e 1625is_thick_pntr (struct type *type)
14f9c5c9
AS
1626{
1627 type = desc_base_type (type);
1628 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2 1629 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1630}
1631
4c4b4cd2
PH
1632/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1633 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1634
d2e4a39e
AS
1635static struct type *
1636desc_bounds_type (struct type *type)
14f9c5c9 1637{
d2e4a39e 1638 struct type *r;
14f9c5c9
AS
1639
1640 type = desc_base_type (type);
1641
1642 if (type == NULL)
1643 return NULL;
1644 else if (is_thin_pntr (type))
1645 {
1646 type = thin_descriptor_type (type);
1647 if (type == NULL)
4c4b4cd2 1648 return NULL;
14f9c5c9
AS
1649 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1650 if (r != NULL)
61ee279c 1651 return ada_check_typedef (r);
14f9c5c9
AS
1652 }
1653 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1654 {
1655 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1656 if (r != NULL)
61ee279c 1657 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1658 }
1659 return NULL;
1660}
1661
1662/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1663 one, a pointer to its bounds data. Otherwise NULL. */
1664
d2e4a39e
AS
1665static struct value *
1666desc_bounds (struct value *arr)
14f9c5c9 1667{
df407dfe 1668 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1669
d2e4a39e 1670 if (is_thin_pntr (type))
14f9c5c9 1671 {
d2e4a39e 1672 struct type *bounds_type =
4c4b4cd2 1673 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1674 LONGEST addr;
1675
4cdfadb1 1676 if (bounds_type == NULL)
323e0a4a 1677 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1678
1679 /* NOTE: The following calculation is not really kosher, but
d2e4a39e 1680 since desc_type is an XVE-encoded type (and shouldn't be),
4c4b4cd2 1681 the correct calculation is a real pain. FIXME (and fix GCC). */
14f9c5c9 1682 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4c4b4cd2 1683 addr = value_as_long (arr);
d2e4a39e 1684 else
42ae5230 1685 addr = value_address (arr);
14f9c5c9 1686
d2e4a39e 1687 return
4c4b4cd2
PH
1688 value_from_longest (lookup_pointer_type (bounds_type),
1689 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1690 }
1691
1692 else if (is_thick_pntr (type))
05e522ef
JB
1693 {
1694 struct value *p_bounds = value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
1695 _("Bad GNAT array descriptor"));
1696 struct type *p_bounds_type = value_type (p_bounds);
1697
1698 if (p_bounds_type
1699 && TYPE_CODE (p_bounds_type) == TYPE_CODE_PTR)
1700 {
1701 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1702
1703 if (TYPE_STUB (target_type))
1704 p_bounds = value_cast (lookup_pointer_type
1705 (ada_check_typedef (target_type)),
1706 p_bounds);
1707 }
1708 else
1709 error (_("Bad GNAT array descriptor"));
1710
1711 return p_bounds;
1712 }
14f9c5c9
AS
1713 else
1714 return NULL;
1715}
1716
4c4b4cd2
PH
1717/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1718 position of the field containing the address of the bounds data. */
1719
14f9c5c9 1720static int
d2e4a39e 1721fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1722{
1723 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1724}
1725
1726/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1727 size of the field containing the address of the bounds data. */
1728
14f9c5c9 1729static int
d2e4a39e 1730fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1731{
1732 type = desc_base_type (type);
1733
d2e4a39e 1734 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1735 return TYPE_FIELD_BITSIZE (type, 1);
1736 else
61ee279c 1737 return 8 * TYPE_LENGTH (ada_check_typedef (TYPE_FIELD_TYPE (type, 1)));
14f9c5c9
AS
1738}
1739
4c4b4cd2 1740/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1741 pointer to one, the type of its array data (a array-with-no-bounds type);
1742 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1743 data. */
4c4b4cd2 1744
d2e4a39e 1745static struct type *
556bdfd4 1746desc_data_target_type (struct type *type)
14f9c5c9
AS
1747{
1748 type = desc_base_type (type);
1749
4c4b4cd2 1750 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1751 if (is_thin_pntr (type))
556bdfd4 1752 return desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1));
14f9c5c9 1753 else if (is_thick_pntr (type))
556bdfd4
UW
1754 {
1755 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1756
1757 if (data_type
1758 && TYPE_CODE (ada_check_typedef (data_type)) == TYPE_CODE_PTR)
05e522ef 1759 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1760 }
1761
1762 return NULL;
14f9c5c9
AS
1763}
1764
1765/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1766 its array data. */
4c4b4cd2 1767
d2e4a39e
AS
1768static struct value *
1769desc_data (struct value *arr)
14f9c5c9 1770{
df407dfe 1771 struct type *type = value_type (arr);
5b4ee69b 1772
14f9c5c9
AS
1773 if (is_thin_pntr (type))
1774 return thin_data_pntr (arr);
1775 else if (is_thick_pntr (type))
d2e4a39e 1776 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
323e0a4a 1777 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1778 else
1779 return NULL;
1780}
1781
1782
1783/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1784 position of the field containing the address of the data. */
1785
14f9c5c9 1786static int
d2e4a39e 1787fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1788{
1789 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1790}
1791
1792/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1793 size of the field containing the address of the data. */
1794
14f9c5c9 1795static int
d2e4a39e 1796fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1797{
1798 type = desc_base_type (type);
1799
1800 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1801 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1802 else
14f9c5c9
AS
1803 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1804}
1805
4c4b4cd2 1806/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1807 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1808 bound, if WHICH is 1. The first bound is I=1. */
1809
d2e4a39e
AS
1810static struct value *
1811desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1812{
d2e4a39e 1813 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
323e0a4a 1814 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1815}
1816
1817/* If BOUNDS is an array-bounds structure type, return the bit position
1818 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1819 bound, if WHICH is 1. The first bound is I=1. */
1820
14f9c5c9 1821static int
d2e4a39e 1822desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1823{
d2e4a39e 1824 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1825}
1826
1827/* If BOUNDS is an array-bounds structure type, return the bit field size
1828 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1829 bound, if WHICH is 1. The first bound is I=1. */
1830
76a01679 1831static int
d2e4a39e 1832desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1833{
1834 type = desc_base_type (type);
1835
d2e4a39e
AS
1836 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1837 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1838 else
1839 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
14f9c5c9
AS
1840}
1841
1842/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1843 Ith bound (numbering from 1). Otherwise, NULL. */
1844
d2e4a39e
AS
1845static struct type *
1846desc_index_type (struct type *type, int i)
14f9c5c9
AS
1847{
1848 type = desc_base_type (type);
1849
1850 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
d2e4a39e
AS
1851 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1852 else
14f9c5c9
AS
1853 return NULL;
1854}
1855
4c4b4cd2
PH
1856/* The number of index positions in the array-bounds type TYPE.
1857 Return 0 if TYPE is NULL. */
1858
14f9c5c9 1859static int
d2e4a39e 1860desc_arity (struct type *type)
14f9c5c9
AS
1861{
1862 type = desc_base_type (type);
1863
1864 if (type != NULL)
1865 return TYPE_NFIELDS (type) / 2;
1866 return 0;
1867}
1868
4c4b4cd2
PH
1869/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1870 an array descriptor type (representing an unconstrained array
1871 type). */
1872
76a01679
JB
1873static int
1874ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1875{
1876 if (type == NULL)
1877 return 0;
61ee279c 1878 type = ada_check_typedef (type);
4c4b4cd2 1879 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
76a01679 1880 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1881}
1882
52ce6436 1883/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 1884 * to one. */
52ce6436 1885
2c0b251b 1886static int
52ce6436
PH
1887ada_is_array_type (struct type *type)
1888{
1889 while (type != NULL
1890 && (TYPE_CODE (type) == TYPE_CODE_PTR
1891 || TYPE_CODE (type) == TYPE_CODE_REF))
1892 type = TYPE_TARGET_TYPE (type);
1893 return ada_is_direct_array_type (type);
1894}
1895
4c4b4cd2 1896/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1897
14f9c5c9 1898int
4c4b4cd2 1899ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1900{
1901 if (type == NULL)
1902 return 0;
61ee279c 1903 type = ada_check_typedef (type);
14f9c5c9 1904 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
4c4b4cd2 1905 || (TYPE_CODE (type) == TYPE_CODE_PTR
b0dd7688
JB
1906 && TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type)))
1907 == TYPE_CODE_ARRAY));
14f9c5c9
AS
1908}
1909
4c4b4cd2
PH
1910/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1911
14f9c5c9 1912int
4c4b4cd2 1913ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1914{
556bdfd4 1915 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
1916
1917 if (type == NULL)
1918 return 0;
61ee279c 1919 type = ada_check_typedef (type);
556bdfd4
UW
1920 return (data_type != NULL
1921 && TYPE_CODE (data_type) == TYPE_CODE_ARRAY
1922 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
1923}
1924
1925/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1926 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1927 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1928 is still needed. */
1929
14f9c5c9 1930int
ebf56fd3 1931ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1932{
d2e4a39e 1933 return
14f9c5c9
AS
1934 type != NULL
1935 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1936 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
4c4b4cd2
PH
1937 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1938 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1939}
1940
1941
4c4b4cd2 1942/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1943 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1944 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1945 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1946 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1947 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1948 a descriptor. */
d2e4a39e
AS
1949struct type *
1950ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 1951{
ad82864c
JB
1952 if (ada_is_constrained_packed_array_type (value_type (arr)))
1953 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 1954
df407dfe
AC
1955 if (!ada_is_array_descriptor_type (value_type (arr)))
1956 return value_type (arr);
d2e4a39e
AS
1957
1958 if (!bounds)
ad82864c
JB
1959 {
1960 struct type *array_type =
1961 ada_check_typedef (desc_data_target_type (value_type (arr)));
1962
1963 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1964 TYPE_FIELD_BITSIZE (array_type, 0) =
1965 decode_packed_array_bitsize (value_type (arr));
1966
1967 return array_type;
1968 }
14f9c5c9
AS
1969 else
1970 {
d2e4a39e 1971 struct type *elt_type;
14f9c5c9 1972 int arity;
d2e4a39e 1973 struct value *descriptor;
14f9c5c9 1974
df407dfe
AC
1975 elt_type = ada_array_element_type (value_type (arr), -1);
1976 arity = ada_array_arity (value_type (arr));
14f9c5c9 1977
d2e4a39e 1978 if (elt_type == NULL || arity == 0)
df407dfe 1979 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
1980
1981 descriptor = desc_bounds (arr);
d2e4a39e 1982 if (value_as_long (descriptor) == 0)
4c4b4cd2 1983 return NULL;
d2e4a39e 1984 while (arity > 0)
4c4b4cd2 1985 {
e9bb382b
UW
1986 struct type *range_type = alloc_type_copy (value_type (arr));
1987 struct type *array_type = alloc_type_copy (value_type (arr));
4c4b4cd2
PH
1988 struct value *low = desc_one_bound (descriptor, arity, 0);
1989 struct value *high = desc_one_bound (descriptor, arity, 1);
4c4b4cd2 1990
5b4ee69b 1991 arity -= 1;
0c9c3474
SA
1992 create_static_range_type (range_type, value_type (low),
1993 longest_to_int (value_as_long (low)),
1994 longest_to_int (value_as_long (high)));
4c4b4cd2 1995 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
1996
1997 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
e67ad678
JB
1998 {
1999 /* We need to store the element packed bitsize, as well as
2000 recompute the array size, because it was previously
2001 computed based on the unpacked element size. */
2002 LONGEST lo = value_as_long (low);
2003 LONGEST hi = value_as_long (high);
2004
2005 TYPE_FIELD_BITSIZE (elt_type, 0) =
2006 decode_packed_array_bitsize (value_type (arr));
2007 /* If the array has no element, then the size is already
2008 zero, and does not need to be recomputed. */
2009 if (lo < hi)
2010 {
2011 int array_bitsize =
2012 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
2013
2014 TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
2015 }
2016 }
4c4b4cd2 2017 }
14f9c5c9
AS
2018
2019 return lookup_pointer_type (elt_type);
2020 }
2021}
2022
2023/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2024 Otherwise, returns either a standard GDB array with bounds set
2025 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2026 GDB array. Returns NULL if ARR is a null fat pointer. */
2027
d2e4a39e
AS
2028struct value *
2029ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2030{
df407dfe 2031 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2032 {
d2e4a39e 2033 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2034
14f9c5c9 2035 if (arrType == NULL)
4c4b4cd2 2036 return NULL;
14f9c5c9
AS
2037 return value_cast (arrType, value_copy (desc_data (arr)));
2038 }
ad82864c
JB
2039 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2040 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2041 else
2042 return arr;
2043}
2044
2045/* If ARR does not represent an array, returns ARR unchanged.
2046 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2047 be ARR itself if it already is in the proper form). */
2048
720d1a40 2049struct value *
d2e4a39e 2050ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2051{
df407dfe 2052 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2053 {
d2e4a39e 2054 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2055
14f9c5c9 2056 if (arrVal == NULL)
323e0a4a 2057 error (_("Bounds unavailable for null array pointer."));
c1b5a1a6 2058 ada_ensure_varsize_limit (TYPE_TARGET_TYPE (value_type (arrVal)));
14f9c5c9
AS
2059 return value_ind (arrVal);
2060 }
ad82864c
JB
2061 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2062 return decode_constrained_packed_array (arr);
d2e4a39e 2063 else
14f9c5c9
AS
2064 return arr;
2065}
2066
2067/* If TYPE represents a GNAT array type, return it translated to an
2068 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2069 packing). For other types, is the identity. */
2070
d2e4a39e
AS
2071struct type *
2072ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2073{
ad82864c
JB
2074 if (ada_is_constrained_packed_array_type (type))
2075 return decode_constrained_packed_array_type (type);
17280b9f
UW
2076
2077 if (ada_is_array_descriptor_type (type))
556bdfd4 2078 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2079
2080 return type;
14f9c5c9
AS
2081}
2082
4c4b4cd2
PH
2083/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2084
ad82864c
JB
2085static int
2086ada_is_packed_array_type (struct type *type)
14f9c5c9
AS
2087{
2088 if (type == NULL)
2089 return 0;
4c4b4cd2 2090 type = desc_base_type (type);
61ee279c 2091 type = ada_check_typedef (type);
d2e4a39e 2092 return
14f9c5c9
AS
2093 ada_type_name (type) != NULL
2094 && strstr (ada_type_name (type), "___XP") != NULL;
2095}
2096
ad82864c
JB
2097/* Non-zero iff TYPE represents a standard GNAT constrained
2098 packed-array type. */
2099
2100int
2101ada_is_constrained_packed_array_type (struct type *type)
2102{
2103 return ada_is_packed_array_type (type)
2104 && !ada_is_array_descriptor_type (type);
2105}
2106
2107/* Non-zero iff TYPE represents an array descriptor for a
2108 unconstrained packed-array type. */
2109
2110static int
2111ada_is_unconstrained_packed_array_type (struct type *type)
2112{
2113 return ada_is_packed_array_type (type)
2114 && ada_is_array_descriptor_type (type);
2115}
2116
2117/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2118 return the size of its elements in bits. */
2119
2120static long
2121decode_packed_array_bitsize (struct type *type)
2122{
0d5cff50
DE
2123 const char *raw_name;
2124 const char *tail;
ad82864c
JB
2125 long bits;
2126
720d1a40
JB
2127 /* Access to arrays implemented as fat pointers are encoded as a typedef
2128 of the fat pointer type. We need the name of the fat pointer type
2129 to do the decoding, so strip the typedef layer. */
2130 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2131 type = ada_typedef_target_type (type);
2132
2133 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2134 if (!raw_name)
2135 raw_name = ada_type_name (desc_base_type (type));
2136
2137 if (!raw_name)
2138 return 0;
2139
2140 tail = strstr (raw_name, "___XP");
720d1a40 2141 gdb_assert (tail != NULL);
ad82864c
JB
2142
2143 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2144 {
2145 lim_warning
2146 (_("could not understand bit size information on packed array"));
2147 return 0;
2148 }
2149
2150 return bits;
2151}
2152
14f9c5c9
AS
2153/* Given that TYPE is a standard GDB array type with all bounds filled
2154 in, and that the element size of its ultimate scalar constituents
2155 (that is, either its elements, or, if it is an array of arrays, its
2156 elements' elements, etc.) is *ELT_BITS, return an identical type,
2157 but with the bit sizes of its elements (and those of any
2158 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2159 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2160 in bits.
2161
2162 Note that, for arrays whose index type has an XA encoding where
2163 a bound references a record discriminant, getting that discriminant,
2164 and therefore the actual value of that bound, is not possible
2165 because none of the given parameters gives us access to the record.
2166 This function assumes that it is OK in the context where it is being
2167 used to return an array whose bounds are still dynamic and where
2168 the length is arbitrary. */
4c4b4cd2 2169
d2e4a39e 2170static struct type *
ad82864c 2171constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2172{
d2e4a39e
AS
2173 struct type *new_elt_type;
2174 struct type *new_type;
99b1c762
JB
2175 struct type *index_type_desc;
2176 struct type *index_type;
14f9c5c9
AS
2177 LONGEST low_bound, high_bound;
2178
61ee279c 2179 type = ada_check_typedef (type);
14f9c5c9
AS
2180 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
2181 return type;
2182
99b1c762
JB
2183 index_type_desc = ada_find_parallel_type (type, "___XA");
2184 if (index_type_desc)
2185 index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, 0),
2186 NULL);
2187 else
2188 index_type = TYPE_INDEX_TYPE (type);
2189
e9bb382b 2190 new_type = alloc_type_copy (type);
ad82864c
JB
2191 new_elt_type =
2192 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
2193 elt_bits);
99b1c762 2194 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9
AS
2195 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
2196 TYPE_NAME (new_type) = ada_type_name (type);
2197
4a46959e
JB
2198 if ((TYPE_CODE (check_typedef (index_type)) == TYPE_CODE_RANGE
2199 && is_dynamic_type (check_typedef (index_type)))
2200 || get_discrete_bounds (index_type, &low_bound, &high_bound) < 0)
14f9c5c9
AS
2201 low_bound = high_bound = 0;
2202 if (high_bound < low_bound)
2203 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 2204 else
14f9c5c9
AS
2205 {
2206 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 2207 TYPE_LENGTH (new_type) =
4c4b4cd2 2208 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
2209 }
2210
876cecd0 2211 TYPE_FIXED_INSTANCE (new_type) = 1;
14f9c5c9
AS
2212 return new_type;
2213}
2214
ad82864c
JB
2215/* The array type encoded by TYPE, where
2216 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2217
d2e4a39e 2218static struct type *
ad82864c 2219decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2220{
0d5cff50 2221 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2222 char *name;
0d5cff50 2223 const char *tail;
d2e4a39e 2224 struct type *shadow_type;
14f9c5c9 2225 long bits;
14f9c5c9 2226
727e3d2e
JB
2227 if (!raw_name)
2228 raw_name = ada_type_name (desc_base_type (type));
2229
2230 if (!raw_name)
2231 return NULL;
2232
2233 name = (char *) alloca (strlen (raw_name) + 1);
2234 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2235 type = desc_base_type (type);
2236
14f9c5c9
AS
2237 memcpy (name, raw_name, tail - raw_name);
2238 name[tail - raw_name] = '\000';
2239
b4ba55a1
JB
2240 shadow_type = ada_find_parallel_type_with_name (type, name);
2241
2242 if (shadow_type == NULL)
14f9c5c9 2243 {
323e0a4a 2244 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2245 return NULL;
2246 }
f168693b 2247 shadow_type = check_typedef (shadow_type);
14f9c5c9
AS
2248
2249 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
2250 {
0963b4bd
MS
2251 lim_warning (_("could not understand bounds "
2252 "information on packed array"));
14f9c5c9
AS
2253 return NULL;
2254 }
d2e4a39e 2255
ad82864c
JB
2256 bits = decode_packed_array_bitsize (type);
2257 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2258}
2259
ad82864c
JB
2260/* Given that ARR is a struct value *indicating a GNAT constrained packed
2261 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2262 standard GDB array type except that the BITSIZEs of the array
2263 target types are set to the number of bits in each element, and the
4c4b4cd2 2264 type length is set appropriately. */
14f9c5c9 2265
d2e4a39e 2266static struct value *
ad82864c 2267decode_constrained_packed_array (struct value *arr)
14f9c5c9 2268{
4c4b4cd2 2269 struct type *type;
14f9c5c9 2270
11aa919a
PMR
2271 /* If our value is a pointer, then dereference it. Likewise if
2272 the value is a reference. Make sure that this operation does not
2273 cause the target type to be fixed, as this would indirectly cause
2274 this array to be decoded. The rest of the routine assumes that
2275 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2276 and "value_ind" routines to perform the dereferencing, as opposed
2277 to using "ada_coerce_ref" or "ada_value_ind". */
2278 arr = coerce_ref (arr);
828292f2 2279 if (TYPE_CODE (ada_check_typedef (value_type (arr))) == TYPE_CODE_PTR)
284614f0 2280 arr = value_ind (arr);
4c4b4cd2 2281
ad82864c 2282 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2283 if (type == NULL)
2284 {
323e0a4a 2285 error (_("can't unpack array"));
14f9c5c9
AS
2286 return NULL;
2287 }
61ee279c 2288
50810684 2289 if (gdbarch_bits_big_endian (get_type_arch (value_type (arr)))
32c9a795 2290 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2291 {
2292 /* This is a (right-justified) modular type representing a packed
2293 array with no wrapper. In order to interpret the value through
2294 the (left-justified) packed array type we just built, we must
2295 first left-justify it. */
2296 int bit_size, bit_pos;
2297 ULONGEST mod;
2298
df407dfe 2299 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2300 bit_size = 0;
2301 while (mod > 0)
2302 {
2303 bit_size += 1;
2304 mod >>= 1;
2305 }
df407dfe 2306 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2307 arr = ada_value_primitive_packed_val (arr, NULL,
2308 bit_pos / HOST_CHAR_BIT,
2309 bit_pos % HOST_CHAR_BIT,
2310 bit_size,
2311 type);
2312 }
2313
4c4b4cd2 2314 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2315}
2316
2317
2318/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2319 given in IND. ARR must be a simple array. */
14f9c5c9 2320
d2e4a39e
AS
2321static struct value *
2322value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2323{
2324 int i;
2325 int bits, elt_off, bit_off;
2326 long elt_total_bit_offset;
d2e4a39e
AS
2327 struct type *elt_type;
2328 struct value *v;
14f9c5c9
AS
2329
2330 bits = 0;
2331 elt_total_bit_offset = 0;
df407dfe 2332 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2333 for (i = 0; i < arity; i += 1)
14f9c5c9 2334 {
d2e4a39e 2335 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
4c4b4cd2
PH
2336 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2337 error
0963b4bd
MS
2338 (_("attempt to do packed indexing of "
2339 "something other than a packed array"));
14f9c5c9 2340 else
4c4b4cd2
PH
2341 {
2342 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
2343 LONGEST lowerbound, upperbound;
2344 LONGEST idx;
2345
2346 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2347 {
323e0a4a 2348 lim_warning (_("don't know bounds of array"));
4c4b4cd2
PH
2349 lowerbound = upperbound = 0;
2350 }
2351
3cb382c9 2352 idx = pos_atr (ind[i]);
4c4b4cd2 2353 if (idx < lowerbound || idx > upperbound)
0963b4bd
MS
2354 lim_warning (_("packed array index %ld out of bounds"),
2355 (long) idx);
4c4b4cd2
PH
2356 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2357 elt_total_bit_offset += (idx - lowerbound) * bits;
61ee279c 2358 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
4c4b4cd2 2359 }
14f9c5c9
AS
2360 }
2361 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2362 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2363
2364 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
4c4b4cd2 2365 bits, elt_type);
14f9c5c9
AS
2366 return v;
2367}
2368
4c4b4cd2 2369/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2370
2371static int
d2e4a39e 2372has_negatives (struct type *type)
14f9c5c9 2373{
d2e4a39e
AS
2374 switch (TYPE_CODE (type))
2375 {
2376 default:
2377 return 0;
2378 case TYPE_CODE_INT:
2379 return !TYPE_UNSIGNED (type);
2380 case TYPE_CODE_RANGE:
2381 return TYPE_LOW_BOUND (type) < 0;
2382 }
14f9c5c9 2383}
d2e4a39e 2384
f93fca70
JB
2385/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
2386 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
2387 the unpacked buffer.
14f9c5c9 2388
f93fca70
JB
2389 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2390 zero otherwise.
14f9c5c9 2391
f93fca70 2392 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2393
f93fca70
JB
2394 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2395
2396static void
2397ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2398 gdb_byte *unpacked, int unpacked_len,
2399 int is_big_endian, int is_signed_type,
2400 int is_scalar)
2401{
a1c95e6b
JB
2402 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2403 int src_idx; /* Index into the source area */
2404 int src_bytes_left; /* Number of source bytes left to process. */
2405 int srcBitsLeft; /* Number of source bits left to move */
2406 int unusedLS; /* Number of bits in next significant
2407 byte of source that are unused */
2408
a1c95e6b
JB
2409 int unpacked_idx; /* Index into the unpacked buffer */
2410 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2411
4c4b4cd2 2412 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2413 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2414 unsigned char sign;
a1c95e6b 2415
4c4b4cd2
PH
2416 /* Transmit bytes from least to most significant; delta is the direction
2417 the indices move. */
f93fca70 2418 int delta = is_big_endian ? -1 : 1;
14f9c5c9
AS
2419
2420 srcBitsLeft = bit_size;
086ca51f 2421 src_bytes_left = src_len;
f93fca70 2422 unpacked_bytes_left = unpacked_len;
14f9c5c9 2423 sign = 0;
f93fca70
JB
2424
2425 if (is_big_endian)
14f9c5c9 2426 {
086ca51f 2427 src_idx = src_len - 1;
f93fca70
JB
2428 if (is_signed_type
2429 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
4c4b4cd2 2430 sign = ~0;
d2e4a39e
AS
2431
2432 unusedLS =
4c4b4cd2
PH
2433 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2434 % HOST_CHAR_BIT;
14f9c5c9 2435
f93fca70
JB
2436 if (is_scalar)
2437 {
2438 accumSize = 0;
2439 unpacked_idx = unpacked_len - 1;
2440 }
2441 else
2442 {
4c4b4cd2
PH
2443 /* Non-scalar values must be aligned at a byte boundary... */
2444 accumSize =
2445 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2446 /* ... And are placed at the beginning (most-significant) bytes
2447 of the target. */
086ca51f
JB
2448 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2449 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2450 }
14f9c5c9 2451 }
d2e4a39e 2452 else
14f9c5c9
AS
2453 {
2454 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2455
086ca51f 2456 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2457 unusedLS = bit_offset;
2458 accumSize = 0;
2459
f93fca70 2460 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
4c4b4cd2 2461 sign = ~0;
14f9c5c9 2462 }
d2e4a39e 2463
14f9c5c9 2464 accum = 0;
086ca51f 2465 while (src_bytes_left > 0)
14f9c5c9
AS
2466 {
2467 /* Mask for removing bits of the next source byte that are not
4c4b4cd2 2468 part of the value. */
d2e4a39e 2469 unsigned int unusedMSMask =
4c4b4cd2
PH
2470 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2471 1;
2472 /* Sign-extend bits for this byte. */
14f9c5c9 2473 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2474
d2e4a39e 2475 accum |=
086ca51f 2476 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2477 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2478 if (accumSize >= HOST_CHAR_BIT)
4c4b4cd2 2479 {
086ca51f 2480 unpacked[unpacked_idx] = accum & ~(~0L << HOST_CHAR_BIT);
4c4b4cd2
PH
2481 accumSize -= HOST_CHAR_BIT;
2482 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2483 unpacked_bytes_left -= 1;
2484 unpacked_idx += delta;
4c4b4cd2 2485 }
14f9c5c9
AS
2486 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2487 unusedLS = 0;
086ca51f
JB
2488 src_bytes_left -= 1;
2489 src_idx += delta;
14f9c5c9 2490 }
086ca51f 2491 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2492 {
2493 accum |= sign << accumSize;
086ca51f 2494 unpacked[unpacked_idx] = accum & ~(~0L << HOST_CHAR_BIT);
14f9c5c9 2495 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2496 if (accumSize < 0)
2497 accumSize = 0;
14f9c5c9 2498 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2499 unpacked_bytes_left -= 1;
2500 unpacked_idx += delta;
14f9c5c9 2501 }
f93fca70
JB
2502}
2503
2504/* Create a new value of type TYPE from the contents of OBJ starting
2505 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2506 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2507 assigning through the result will set the field fetched from.
2508 VALADDR is ignored unless OBJ is NULL, in which case,
2509 VALADDR+OFFSET must address the start of storage containing the
2510 packed value. The value returned in this case is never an lval.
2511 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2512
2513struct value *
2514ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2515 long offset, int bit_offset, int bit_size,
2516 struct type *type)
2517{
2518 struct value *v;
2519 gdb_byte *src; /* First byte containing data to unpack */
2520 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2521 gdb_byte *unpacked;
2522 int is_scalar;
2523
2524 type = ada_check_typedef (type);
2525
2526 if (obj == NULL)
2527 {
2528 v = allocate_value (type);
2529 src = (gdb_byte *) valaddr + offset;
2530 }
2531 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2532 {
2533 v = value_at (type, value_address (obj) + offset);
2534 type = value_type (v);
2535 if (TYPE_LENGTH (type) * HOST_CHAR_BIT < bit_size)
2536 {
2537 /* This can happen in the case of an array of dynamic objects,
2538 where the size of each element changes from element to element.
2539 In that case, we're initially given the array stride, but
2540 after resolving the element type, we find that its size is
2541 less than this stride. In that case, adjust bit_size to
2542 match TYPE's length, and recompute LEN accordingly. */
2543 bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
2544 src_len = TYPE_LENGTH (type) + (bit_offset + HOST_CHAR_BIT - 1) / 8;
2545 }
2546 src = alloca (src_len);
2547 read_memory (value_address (v), src, src_len);
2548 }
2549 else
2550 {
2551 v = allocate_value (type);
2552 src = (gdb_byte *) value_contents (obj) + offset;
2553 }
2554
2555 if (obj != NULL)
2556 {
2557 long new_offset = offset;
2558
2559 set_value_component_location (v, obj);
2560 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2561 set_value_bitsize (v, bit_size);
2562 if (value_bitpos (v) >= HOST_CHAR_BIT)
2563 {
2564 ++new_offset;
2565 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2566 }
2567 set_value_offset (v, new_offset);
2568
2569 /* Also set the parent value. This is needed when trying to
2570 assign a new value (in inferior memory). */
2571 set_value_parent (v, obj);
2572 }
2573 else
2574 set_value_bitsize (v, bit_size);
2575 unpacked = (gdb_byte *) value_contents (v);
2576
2577 if (bit_size == 0)
2578 {
2579 memset (unpacked, 0, TYPE_LENGTH (type));
2580 return v;
2581 }
2582
2583 switch (TYPE_CODE (type))
2584 {
2585 case TYPE_CODE_ARRAY:
2586 case TYPE_CODE_UNION:
2587 case TYPE_CODE_STRUCT:
2588 is_scalar = 0;
2589 break;
2590 default:
2591 is_scalar = 1;
2592 break;
2593 }
2594
2595 ada_unpack_from_contents (src, bit_offset, bit_size,
2596 unpacked, TYPE_LENGTH (type),
2597 gdbarch_bits_big_endian (get_type_arch (type)),
2598 has_negatives (type), is_scalar);
14f9c5c9 2599
2478d075
JB
2600 if (is_dynamic_type (value_type (v)))
2601 v = value_from_contents_and_address (value_type (v), value_contents (v),
2602 0);
14f9c5c9
AS
2603 return v;
2604}
d2e4a39e 2605
14f9c5c9
AS
2606/* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
2607 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
4c4b4cd2 2608 not overlap. */
14f9c5c9 2609static void
fc1a4b47 2610move_bits (gdb_byte *target, int targ_offset, const gdb_byte *source,
50810684 2611 int src_offset, int n, int bits_big_endian_p)
14f9c5c9
AS
2612{
2613 unsigned int accum, mask;
2614 int accum_bits, chunk_size;
2615
2616 target += targ_offset / HOST_CHAR_BIT;
2617 targ_offset %= HOST_CHAR_BIT;
2618 source += src_offset / HOST_CHAR_BIT;
2619 src_offset %= HOST_CHAR_BIT;
50810684 2620 if (bits_big_endian_p)
14f9c5c9
AS
2621 {
2622 accum = (unsigned char) *source;
2623 source += 1;
2624 accum_bits = HOST_CHAR_BIT - src_offset;
2625
d2e4a39e 2626 while (n > 0)
4c4b4cd2
PH
2627 {
2628 int unused_right;
5b4ee69b 2629
4c4b4cd2
PH
2630 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
2631 accum_bits += HOST_CHAR_BIT;
2632 source += 1;
2633 chunk_size = HOST_CHAR_BIT - targ_offset;
2634 if (chunk_size > n)
2635 chunk_size = n;
2636 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
2637 mask = ((1 << chunk_size) - 1) << unused_right;
2638 *target =
2639 (*target & ~mask)
2640 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
2641 n -= chunk_size;
2642 accum_bits -= chunk_size;
2643 target += 1;
2644 targ_offset = 0;
2645 }
14f9c5c9
AS
2646 }
2647 else
2648 {
2649 accum = (unsigned char) *source >> src_offset;
2650 source += 1;
2651 accum_bits = HOST_CHAR_BIT - src_offset;
2652
d2e4a39e 2653 while (n > 0)
4c4b4cd2
PH
2654 {
2655 accum = accum + ((unsigned char) *source << accum_bits);
2656 accum_bits += HOST_CHAR_BIT;
2657 source += 1;
2658 chunk_size = HOST_CHAR_BIT - targ_offset;
2659 if (chunk_size > n)
2660 chunk_size = n;
2661 mask = ((1 << chunk_size) - 1) << targ_offset;
2662 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2663 n -= chunk_size;
2664 accum_bits -= chunk_size;
2665 accum >>= chunk_size;
2666 target += 1;
2667 targ_offset = 0;
2668 }
14f9c5c9
AS
2669 }
2670}
2671
14f9c5c9
AS
2672/* Store the contents of FROMVAL into the location of TOVAL.
2673 Return a new value with the location of TOVAL and contents of
2674 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2675 floating-point or non-scalar types. */
14f9c5c9 2676
d2e4a39e
AS
2677static struct value *
2678ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2679{
df407dfe
AC
2680 struct type *type = value_type (toval);
2681 int bits = value_bitsize (toval);
14f9c5c9 2682
52ce6436
PH
2683 toval = ada_coerce_ref (toval);
2684 fromval = ada_coerce_ref (fromval);
2685
2686 if (ada_is_direct_array_type (value_type (toval)))
2687 toval = ada_coerce_to_simple_array (toval);
2688 if (ada_is_direct_array_type (value_type (fromval)))
2689 fromval = ada_coerce_to_simple_array (fromval);
2690
88e3b34b 2691 if (!deprecated_value_modifiable (toval))
323e0a4a 2692 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2693
d2e4a39e 2694 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2695 && bits > 0
d2e4a39e 2696 && (TYPE_CODE (type) == TYPE_CODE_FLT
4c4b4cd2 2697 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
14f9c5c9 2698 {
df407dfe
AC
2699 int len = (value_bitpos (toval)
2700 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2701 int from_size;
224c3ddb 2702 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2703 struct value *val;
42ae5230 2704 CORE_ADDR to_addr = value_address (toval);
14f9c5c9
AS
2705
2706 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4c4b4cd2 2707 fromval = value_cast (type, fromval);
14f9c5c9 2708
52ce6436 2709 read_memory (to_addr, buffer, len);
aced2898
PH
2710 from_size = value_bitsize (fromval);
2711 if (from_size == 0)
2712 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
50810684 2713 if (gdbarch_bits_big_endian (get_type_arch (type)))
df407dfe 2714 move_bits (buffer, value_bitpos (toval),
50810684 2715 value_contents (fromval), from_size - bits, bits, 1);
14f9c5c9 2716 else
50810684
UW
2717 move_bits (buffer, value_bitpos (toval),
2718 value_contents (fromval), 0, bits, 0);
972daa01 2719 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2720
14f9c5c9 2721 val = value_copy (toval);
0fd88904 2722 memcpy (value_contents_raw (val), value_contents (fromval),
4c4b4cd2 2723 TYPE_LENGTH (type));
04624583 2724 deprecated_set_value_type (val, type);
d2e4a39e 2725
14f9c5c9
AS
2726 return val;
2727 }
2728
2729 return value_assign (toval, fromval);
2730}
2731
2732
7c512744
JB
2733/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2734 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2735 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2736 COMPONENT, and not the inferior's memory. The current contents
2737 of COMPONENT are ignored.
2738
2739 Although not part of the initial design, this function also works
2740 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2741 had a null address, and COMPONENT had an address which is equal to
2742 its offset inside CONTAINER. */
2743
52ce6436
PH
2744static void
2745value_assign_to_component (struct value *container, struct value *component,
2746 struct value *val)
2747{
2748 LONGEST offset_in_container =
42ae5230 2749 (LONGEST) (value_address (component) - value_address (container));
7c512744 2750 int bit_offset_in_container =
52ce6436
PH
2751 value_bitpos (component) - value_bitpos (container);
2752 int bits;
7c512744 2753
52ce6436
PH
2754 val = value_cast (value_type (component), val);
2755
2756 if (value_bitsize (component) == 0)
2757 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2758 else
2759 bits = value_bitsize (component);
2760
50810684 2761 if (gdbarch_bits_big_endian (get_type_arch (value_type (container))))
7c512744 2762 move_bits (value_contents_writeable (container) + offset_in_container,
52ce6436
PH
2763 value_bitpos (container) + bit_offset_in_container,
2764 value_contents (val),
2765 TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits,
50810684 2766 bits, 1);
52ce6436 2767 else
7c512744 2768 move_bits (value_contents_writeable (container) + offset_in_container,
52ce6436 2769 value_bitpos (container) + bit_offset_in_container,
50810684 2770 value_contents (val), 0, bits, 0);
7c512744
JB
2771}
2772
4c4b4cd2
PH
2773/* The value of the element of array ARR at the ARITY indices given in IND.
2774 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2775 thereto. */
2776
d2e4a39e
AS
2777struct value *
2778ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2779{
2780 int k;
d2e4a39e
AS
2781 struct value *elt;
2782 struct type *elt_type;
14f9c5c9
AS
2783
2784 elt = ada_coerce_to_simple_array (arr);
2785
df407dfe 2786 elt_type = ada_check_typedef (value_type (elt));
d2e4a39e 2787 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
14f9c5c9
AS
2788 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2789 return value_subscript_packed (elt, arity, ind);
2790
2791 for (k = 0; k < arity; k += 1)
2792 {
2793 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
323e0a4a 2794 error (_("too many subscripts (%d expected)"), k);
2497b498 2795 elt = value_subscript (elt, pos_atr (ind[k]));
14f9c5c9
AS
2796 }
2797 return elt;
2798}
2799
deede10c
JB
2800/* Assuming ARR is a pointer to a GDB array, the value of the element
2801 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
2802 Does not read the entire array into memory.
2803
2804 Note: Unlike what one would expect, this function is used instead of
2805 ada_value_subscript for basically all non-packed array types. The reason
2806 for this is that a side effect of doing our own pointer arithmetics instead
2807 of relying on value_subscript is that there is no implicit typedef peeling.
2808 This is important for arrays of array accesses, where it allows us to
2809 preserve the fact that the array's element is an array access, where the
2810 access part os encoded in a typedef layer. */
14f9c5c9 2811
2c0b251b 2812static struct value *
deede10c 2813ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2814{
2815 int k;
919e6dbe 2816 struct value *array_ind = ada_value_ind (arr);
deede10c 2817 struct type *type
919e6dbe
PMR
2818 = check_typedef (value_enclosing_type (array_ind));
2819
2820 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
2821 && TYPE_FIELD_BITSIZE (type, 0) > 0)
2822 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
2823
2824 for (k = 0; k < arity; k += 1)
2825 {
2826 LONGEST lwb, upb;
aa715135 2827 struct value *lwb_value;
14f9c5c9
AS
2828
2829 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
323e0a4a 2830 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 2831 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4c4b4cd2 2832 value_copy (arr));
14f9c5c9 2833 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
aa715135
JG
2834 lwb_value = value_from_longest (value_type(ind[k]), lwb);
2835 arr = value_ptradd (arr, pos_atr (ind[k]) - pos_atr (lwb_value));
14f9c5c9
AS
2836 type = TYPE_TARGET_TYPE (type);
2837 }
2838
2839 return value_ind (arr);
2840}
2841
0b5d8877 2842/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
2843 actual type of ARRAY_PTR is ignored), returns the Ada slice of
2844 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
2845 this array is LOW, as per Ada rules. */
0b5d8877 2846static struct value *
f5938064
JG
2847ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
2848 int low, int high)
0b5d8877 2849{
b0dd7688 2850 struct type *type0 = ada_check_typedef (type);
aa715135 2851 struct type *base_index_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type0));
0c9c3474 2852 struct type *index_type
aa715135 2853 = create_static_range_type (NULL, base_index_type, low, high);
6c038f32 2854 struct type *slice_type =
b0dd7688 2855 create_array_type (NULL, TYPE_TARGET_TYPE (type0), index_type);
aa715135
JG
2856 int base_low = ada_discrete_type_low_bound (TYPE_INDEX_TYPE (type0));
2857 LONGEST base_low_pos, low_pos;
2858 CORE_ADDR base;
2859
2860 if (!discrete_position (base_index_type, low, &low_pos)
2861 || !discrete_position (base_index_type, base_low, &base_low_pos))
2862 {
2863 warning (_("unable to get positions in slice, use bounds instead"));
2864 low_pos = low;
2865 base_low_pos = base_low;
2866 }
5b4ee69b 2867
aa715135
JG
2868 base = value_as_address (array_ptr)
2869 + ((low_pos - base_low_pos)
2870 * TYPE_LENGTH (TYPE_TARGET_TYPE (type0)));
f5938064 2871 return value_at_lazy (slice_type, base);
0b5d8877
PH
2872}
2873
2874
2875static struct value *
2876ada_value_slice (struct value *array, int low, int high)
2877{
b0dd7688 2878 struct type *type = ada_check_typedef (value_type (array));
aa715135 2879 struct type *base_index_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type));
0c9c3474
SA
2880 struct type *index_type
2881 = create_static_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
6c038f32 2882 struct type *slice_type =
0b5d8877 2883 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
aa715135 2884 LONGEST low_pos, high_pos;
5b4ee69b 2885
aa715135
JG
2886 if (!discrete_position (base_index_type, low, &low_pos)
2887 || !discrete_position (base_index_type, high, &high_pos))
2888 {
2889 warning (_("unable to get positions in slice, use bounds instead"));
2890 low_pos = low;
2891 high_pos = high;
2892 }
2893
2894 return value_cast (slice_type,
2895 value_slice (array, low, high_pos - low_pos + 1));
0b5d8877
PH
2896}
2897
14f9c5c9
AS
2898/* If type is a record type in the form of a standard GNAT array
2899 descriptor, returns the number of dimensions for type. If arr is a
2900 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2901 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2902
2903int
d2e4a39e 2904ada_array_arity (struct type *type)
14f9c5c9
AS
2905{
2906 int arity;
2907
2908 if (type == NULL)
2909 return 0;
2910
2911 type = desc_base_type (type);
2912
2913 arity = 0;
d2e4a39e 2914 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9 2915 return desc_arity (desc_bounds_type (type));
d2e4a39e
AS
2916 else
2917 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9 2918 {
4c4b4cd2 2919 arity += 1;
61ee279c 2920 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2921 }
d2e4a39e 2922
14f9c5c9
AS
2923 return arity;
2924}
2925
2926/* If TYPE is a record type in the form of a standard GNAT array
2927 descriptor or a simple array type, returns the element type for
2928 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2929 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2930
d2e4a39e
AS
2931struct type *
2932ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2933{
2934 type = desc_base_type (type);
2935
d2e4a39e 2936 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9
AS
2937 {
2938 int k;
d2e4a39e 2939 struct type *p_array_type;
14f9c5c9 2940
556bdfd4 2941 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
2942
2943 k = ada_array_arity (type);
2944 if (k == 0)
4c4b4cd2 2945 return NULL;
d2e4a39e 2946
4c4b4cd2 2947 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2948 if (nindices >= 0 && k > nindices)
4c4b4cd2 2949 k = nindices;
d2e4a39e 2950 while (k > 0 && p_array_type != NULL)
4c4b4cd2 2951 {
61ee279c 2952 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
4c4b4cd2
PH
2953 k -= 1;
2954 }
14f9c5c9
AS
2955 return p_array_type;
2956 }
2957 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2958 {
2959 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
4c4b4cd2
PH
2960 {
2961 type = TYPE_TARGET_TYPE (type);
2962 nindices -= 1;
2963 }
14f9c5c9
AS
2964 return type;
2965 }
2966
2967 return NULL;
2968}
2969
4c4b4cd2 2970/* The type of nth index in arrays of given type (n numbering from 1).
dd19d49e
UW
2971 Does not examine memory. Throws an error if N is invalid or TYPE
2972 is not an array type. NAME is the name of the Ada attribute being
2973 evaluated ('range, 'first, 'last, or 'length); it is used in building
2974 the error message. */
14f9c5c9 2975
1eea4ebd
UW
2976static struct type *
2977ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 2978{
4c4b4cd2
PH
2979 struct type *result_type;
2980
14f9c5c9
AS
2981 type = desc_base_type (type);
2982
1eea4ebd
UW
2983 if (n < 0 || n > ada_array_arity (type))
2984 error (_("invalid dimension number to '%s"), name);
14f9c5c9 2985
4c4b4cd2 2986 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2987 {
2988 int i;
2989
2990 for (i = 1; i < n; i += 1)
4c4b4cd2 2991 type = TYPE_TARGET_TYPE (type);
262452ec 2992 result_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type));
4c4b4cd2
PH
2993 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2994 has a target type of TYPE_CODE_UNDEF. We compensate here, but
76a01679 2995 perhaps stabsread.c would make more sense. */
1eea4ebd
UW
2996 if (result_type && TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2997 result_type = NULL;
14f9c5c9 2998 }
d2e4a39e 2999 else
1eea4ebd
UW
3000 {
3001 result_type = desc_index_type (desc_bounds_type (type), n);
3002 if (result_type == NULL)
3003 error (_("attempt to take bound of something that is not an array"));
3004 }
3005
3006 return result_type;
14f9c5c9
AS
3007}
3008
3009/* Given that arr is an array type, returns the lower bound of the
3010 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3011 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3012 array-descriptor type. It works for other arrays with bounds supplied
3013 by run-time quantities other than discriminants. */
14f9c5c9 3014
abb68b3e 3015static LONGEST
fb5e3d5c 3016ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3017{
8a48ac95 3018 struct type *type, *index_type_desc, *index_type;
1ce677a4 3019 int i;
262452ec
JK
3020
3021 gdb_assert (which == 0 || which == 1);
14f9c5c9 3022
ad82864c
JB
3023 if (ada_is_constrained_packed_array_type (arr_type))
3024 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3025
4c4b4cd2 3026 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 3027 return (LONGEST) - which;
14f9c5c9
AS
3028
3029 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
3030 type = TYPE_TARGET_TYPE (arr_type);
3031 else
3032 type = arr_type;
3033
bafffb51
JB
3034 if (TYPE_FIXED_INSTANCE (type))
3035 {
3036 /* The array has already been fixed, so we do not need to
3037 check the parallel ___XA type again. That encoding has
3038 already been applied, so ignore it now. */
3039 index_type_desc = NULL;
3040 }
3041 else
3042 {
3043 index_type_desc = ada_find_parallel_type (type, "___XA");
3044 ada_fixup_array_indexes_type (index_type_desc);
3045 }
3046
262452ec 3047 if (index_type_desc != NULL)
28c85d6c
JB
3048 index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, n - 1),
3049 NULL);
262452ec 3050 else
8a48ac95
JB
3051 {
3052 struct type *elt_type = check_typedef (type);
3053
3054 for (i = 1; i < n; i++)
3055 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
3056
3057 index_type = TYPE_INDEX_TYPE (elt_type);
3058 }
262452ec 3059
43bbcdc2
PH
3060 return
3061 (LONGEST) (which == 0
3062 ? ada_discrete_type_low_bound (index_type)
3063 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3064}
3065
3066/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3067 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3068 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3069 supplied by run-time quantities other than discriminants. */
14f9c5c9 3070
1eea4ebd 3071static LONGEST
4dc81987 3072ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3073{
eb479039
JB
3074 struct type *arr_type;
3075
3076 if (TYPE_CODE (check_typedef (value_type (arr))) == TYPE_CODE_PTR)
3077 arr = value_ind (arr);
3078 arr_type = value_enclosing_type (arr);
14f9c5c9 3079
ad82864c
JB
3080 if (ada_is_constrained_packed_array_type (arr_type))
3081 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3082 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3083 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3084 else
1eea4ebd 3085 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3086}
3087
3088/* Given that arr is an array value, returns the length of the
3089 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3090 supplied by run-time quantities other than discriminants.
3091 Does not work for arrays indexed by enumeration types with representation
3092 clauses at the moment. */
14f9c5c9 3093
1eea4ebd 3094static LONGEST
d2e4a39e 3095ada_array_length (struct value *arr, int n)
14f9c5c9 3096{
aa715135
JG
3097 struct type *arr_type, *index_type;
3098 int low, high;
eb479039
JB
3099
3100 if (TYPE_CODE (check_typedef (value_type (arr))) == TYPE_CODE_PTR)
3101 arr = value_ind (arr);
3102 arr_type = value_enclosing_type (arr);
14f9c5c9 3103
ad82864c
JB
3104 if (ada_is_constrained_packed_array_type (arr_type))
3105 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3106
4c4b4cd2 3107 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3108 {
3109 low = ada_array_bound_from_type (arr_type, n, 0);
3110 high = ada_array_bound_from_type (arr_type, n, 1);
3111 }
14f9c5c9 3112 else
aa715135
JG
3113 {
3114 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3115 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3116 }
3117
f168693b 3118 arr_type = check_typedef (arr_type);
aa715135
JG
3119 index_type = TYPE_INDEX_TYPE (arr_type);
3120 if (index_type != NULL)
3121 {
3122 struct type *base_type;
3123 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
3124 base_type = TYPE_TARGET_TYPE (index_type);
3125 else
3126 base_type = index_type;
3127
3128 low = pos_atr (value_from_longest (base_type, low));
3129 high = pos_atr (value_from_longest (base_type, high));
3130 }
3131 return high - low + 1;
4c4b4cd2
PH
3132}
3133
3134/* An empty array whose type is that of ARR_TYPE (an array type),
3135 with bounds LOW to LOW-1. */
3136
3137static struct value *
3138empty_array (struct type *arr_type, int low)
3139{
b0dd7688 3140 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3141 struct type *index_type
3142 = create_static_range_type
3143 (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type0)), low, low - 1);
b0dd7688 3144 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3145
0b5d8877 3146 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3147}
14f9c5c9 3148\f
d2e4a39e 3149
4c4b4cd2 3150 /* Name resolution */
14f9c5c9 3151
4c4b4cd2
PH
3152/* The "decoded" name for the user-definable Ada operator corresponding
3153 to OP. */
14f9c5c9 3154
d2e4a39e 3155static const char *
4c4b4cd2 3156ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3157{
3158 int i;
3159
4c4b4cd2 3160 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3161 {
3162 if (ada_opname_table[i].op == op)
4c4b4cd2 3163 return ada_opname_table[i].decoded;
14f9c5c9 3164 }
323e0a4a 3165 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3166}
3167
3168
4c4b4cd2
PH
3169/* Same as evaluate_type (*EXP), but resolves ambiguous symbol
3170 references (marked by OP_VAR_VALUE nodes in which the symbol has an
3171 undefined namespace) and converts operators that are
3172 user-defined into appropriate function calls. If CONTEXT_TYPE is
14f9c5c9
AS
3173 non-null, it provides a preferred result type [at the moment, only
3174 type void has any effect---causing procedures to be preferred over
3175 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
4c4b4cd2 3176 return type is preferred. May change (expand) *EXP. */
14f9c5c9 3177
4c4b4cd2
PH
3178static void
3179resolve (struct expression **expp, int void_context_p)
14f9c5c9 3180{
30b15541
UW
3181 struct type *context_type = NULL;
3182 int pc = 0;
3183
3184 if (void_context_p)
3185 context_type = builtin_type ((*expp)->gdbarch)->builtin_void;
3186
3187 resolve_subexp (expp, &pc, 1, context_type);
14f9c5c9
AS
3188}
3189
4c4b4cd2
PH
3190/* Resolve the operator of the subexpression beginning at
3191 position *POS of *EXPP. "Resolving" consists of replacing
3192 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
3193 with their resolutions, replacing built-in operators with
3194 function calls to user-defined operators, where appropriate, and,
3195 when DEPROCEDURE_P is non-zero, converting function-valued variables
3196 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
3197 are as in ada_resolve, above. */
14f9c5c9 3198
d2e4a39e 3199static struct value *
4c4b4cd2 3200resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
76a01679 3201 struct type *context_type)
14f9c5c9
AS
3202{
3203 int pc = *pos;
3204 int i;
4c4b4cd2 3205 struct expression *exp; /* Convenience: == *expp. */
14f9c5c9 3206 enum exp_opcode op = (*expp)->elts[pc].opcode;
4c4b4cd2
PH
3207 struct value **argvec; /* Vector of operand types (alloca'ed). */
3208 int nargs; /* Number of operands. */
52ce6436 3209 int oplen;
14f9c5c9
AS
3210
3211 argvec = NULL;
3212 nargs = 0;
3213 exp = *expp;
3214
52ce6436
PH
3215 /* Pass one: resolve operands, saving their types and updating *pos,
3216 if needed. */
14f9c5c9
AS
3217 switch (op)
3218 {
4c4b4cd2
PH
3219 case OP_FUNCALL:
3220 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679
JB
3221 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
3222 *pos += 7;
4c4b4cd2
PH
3223 else
3224 {
3225 *pos += 3;
3226 resolve_subexp (expp, pos, 0, NULL);
3227 }
3228 nargs = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9
AS
3229 break;
3230
14f9c5c9 3231 case UNOP_ADDR:
4c4b4cd2
PH
3232 *pos += 1;
3233 resolve_subexp (expp, pos, 0, NULL);
3234 break;
3235
52ce6436
PH
3236 case UNOP_QUAL:
3237 *pos += 3;
17466c1a 3238 resolve_subexp (expp, pos, 1, check_typedef (exp->elts[pc + 1].type));
4c4b4cd2
PH
3239 break;
3240
52ce6436 3241 case OP_ATR_MODULUS:
4c4b4cd2
PH
3242 case OP_ATR_SIZE:
3243 case OP_ATR_TAG:
4c4b4cd2
PH
3244 case OP_ATR_FIRST:
3245 case OP_ATR_LAST:
3246 case OP_ATR_LENGTH:
3247 case OP_ATR_POS:
3248 case OP_ATR_VAL:
4c4b4cd2
PH
3249 case OP_ATR_MIN:
3250 case OP_ATR_MAX:
52ce6436
PH
3251 case TERNOP_IN_RANGE:
3252 case BINOP_IN_BOUNDS:
3253 case UNOP_IN_RANGE:
3254 case OP_AGGREGATE:
3255 case OP_OTHERS:
3256 case OP_CHOICES:
3257 case OP_POSITIONAL:
3258 case OP_DISCRETE_RANGE:
3259 case OP_NAME:
3260 ada_forward_operator_length (exp, pc, &oplen, &nargs);
3261 *pos += oplen;
14f9c5c9
AS
3262 break;
3263
3264 case BINOP_ASSIGN:
3265 {
4c4b4cd2
PH
3266 struct value *arg1;
3267
3268 *pos += 1;
3269 arg1 = resolve_subexp (expp, pos, 0, NULL);
3270 if (arg1 == NULL)
3271 resolve_subexp (expp, pos, 1, NULL);
3272 else
df407dfe 3273 resolve_subexp (expp, pos, 1, value_type (arg1));
4c4b4cd2 3274 break;
14f9c5c9
AS
3275 }
3276
4c4b4cd2 3277 case UNOP_CAST:
4c4b4cd2
PH
3278 *pos += 3;
3279 nargs = 1;
3280 break;
14f9c5c9 3281
4c4b4cd2
PH
3282 case BINOP_ADD:
3283 case BINOP_SUB:
3284 case BINOP_MUL:
3285 case BINOP_DIV:
3286 case BINOP_REM:
3287 case BINOP_MOD:
3288 case BINOP_EXP:
3289 case BINOP_CONCAT:
3290 case BINOP_LOGICAL_AND:
3291 case BINOP_LOGICAL_OR:
3292 case BINOP_BITWISE_AND:
3293 case BINOP_BITWISE_IOR:
3294 case BINOP_BITWISE_XOR:
14f9c5c9 3295
4c4b4cd2
PH
3296 case BINOP_EQUAL:
3297 case BINOP_NOTEQUAL:
3298 case BINOP_LESS:
3299 case BINOP_GTR:
3300 case BINOP_LEQ:
3301 case BINOP_GEQ:
14f9c5c9 3302
4c4b4cd2
PH
3303 case BINOP_REPEAT:
3304 case BINOP_SUBSCRIPT:
3305 case BINOP_COMMA:
40c8aaa9
JB
3306 *pos += 1;
3307 nargs = 2;
3308 break;
14f9c5c9 3309
4c4b4cd2
PH
3310 case UNOP_NEG:
3311 case UNOP_PLUS:
3312 case UNOP_LOGICAL_NOT:
3313 case UNOP_ABS:
3314 case UNOP_IND:
3315 *pos += 1;
3316 nargs = 1;
3317 break;
14f9c5c9 3318
4c4b4cd2
PH
3319 case OP_LONG:
3320 case OP_DOUBLE:
3321 case OP_VAR_VALUE:
3322 *pos += 4;
3323 break;
14f9c5c9 3324
4c4b4cd2
PH
3325 case OP_TYPE:
3326 case OP_BOOL:
3327 case OP_LAST:
4c4b4cd2
PH
3328 case OP_INTERNALVAR:
3329 *pos += 3;
3330 break;
14f9c5c9 3331
4c4b4cd2
PH
3332 case UNOP_MEMVAL:
3333 *pos += 3;
3334 nargs = 1;
3335 break;
3336
67f3407f
DJ
3337 case OP_REGISTER:
3338 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
3339 break;
3340
4c4b4cd2
PH
3341 case STRUCTOP_STRUCT:
3342 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
3343 nargs = 1;
3344 break;
3345
4c4b4cd2 3346 case TERNOP_SLICE:
4c4b4cd2
PH
3347 *pos += 1;
3348 nargs = 3;
3349 break;
3350
52ce6436 3351 case OP_STRING:
14f9c5c9 3352 break;
4c4b4cd2
PH
3353
3354 default:
323e0a4a 3355 error (_("Unexpected operator during name resolution"));
14f9c5c9
AS
3356 }
3357
8d749320 3358 argvec = XALLOCAVEC (struct value *, nargs + 1);
4c4b4cd2
PH
3359 for (i = 0; i < nargs; i += 1)
3360 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
3361 argvec[i] = NULL;
3362 exp = *expp;
3363
3364 /* Pass two: perform any resolution on principal operator. */
14f9c5c9
AS
3365 switch (op)
3366 {
3367 default:
3368 break;
3369
14f9c5c9 3370 case OP_VAR_VALUE:
4c4b4cd2 3371 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679 3372 {
d12307c1 3373 struct block_symbol *candidates;
76a01679
JB
3374 int n_candidates;
3375
3376 n_candidates =
3377 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3378 (exp->elts[pc + 2].symbol),
3379 exp->elts[pc + 1].block, VAR_DOMAIN,
4eeaa230 3380 &candidates);
76a01679
JB
3381
3382 if (n_candidates > 1)
3383 {
3384 /* Types tend to get re-introduced locally, so if there
3385 are any local symbols that are not types, first filter
3386 out all types. */
3387 int j;
3388 for (j = 0; j < n_candidates; j += 1)
d12307c1 3389 switch (SYMBOL_CLASS (candidates[j].symbol))
76a01679
JB
3390 {
3391 case LOC_REGISTER:
3392 case LOC_ARG:
3393 case LOC_REF_ARG:
76a01679
JB
3394 case LOC_REGPARM_ADDR:
3395 case LOC_LOCAL:
76a01679 3396 case LOC_COMPUTED:
76a01679
JB
3397 goto FoundNonType;
3398 default:
3399 break;
3400 }
3401 FoundNonType:
3402 if (j < n_candidates)
3403 {
3404 j = 0;
3405 while (j < n_candidates)
3406 {
d12307c1 3407 if (SYMBOL_CLASS (candidates[j].symbol) == LOC_TYPEDEF)
76a01679
JB
3408 {
3409 candidates[j] = candidates[n_candidates - 1];
3410 n_candidates -= 1;
3411 }
3412 else
3413 j += 1;
3414 }
3415 }
3416 }
3417
3418 if (n_candidates == 0)
323e0a4a 3419 error (_("No definition found for %s"),
76a01679
JB
3420 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3421 else if (n_candidates == 1)
3422 i = 0;
3423 else if (deprocedure_p
3424 && !is_nonfunction (candidates, n_candidates))
3425 {
06d5cf63
JB
3426 i = ada_resolve_function
3427 (candidates, n_candidates, NULL, 0,
3428 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
3429 context_type);
76a01679 3430 if (i < 0)
323e0a4a 3431 error (_("Could not find a match for %s"),
76a01679
JB
3432 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3433 }
3434 else
3435 {
323e0a4a 3436 printf_filtered (_("Multiple matches for %s\n"),
76a01679
JB
3437 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3438 user_select_syms (candidates, n_candidates, 1);
3439 i = 0;
3440 }
3441
3442 exp->elts[pc + 1].block = candidates[i].block;
d12307c1 3443 exp->elts[pc + 2].symbol = candidates[i].symbol;
1265e4aa
JB
3444 if (innermost_block == NULL
3445 || contained_in (candidates[i].block, innermost_block))
76a01679
JB
3446 innermost_block = candidates[i].block;
3447 }
3448
3449 if (deprocedure_p
3450 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
3451 == TYPE_CODE_FUNC))
3452 {
3453 replace_operator_with_call (expp, pc, 0, 0,
3454 exp->elts[pc + 2].symbol,
3455 exp->elts[pc + 1].block);
3456 exp = *expp;
3457 }
14f9c5c9
AS
3458 break;
3459
3460 case OP_FUNCALL:
3461 {
4c4b4cd2 3462 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679 3463 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
4c4b4cd2 3464 {
d12307c1 3465 struct block_symbol *candidates;
4c4b4cd2
PH
3466 int n_candidates;
3467
3468 n_candidates =
76a01679
JB
3469 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3470 (exp->elts[pc + 5].symbol),
3471 exp->elts[pc + 4].block, VAR_DOMAIN,
4eeaa230 3472 &candidates);
4c4b4cd2
PH
3473 if (n_candidates == 1)
3474 i = 0;
3475 else
3476 {
06d5cf63
JB
3477 i = ada_resolve_function
3478 (candidates, n_candidates,
3479 argvec, nargs,
3480 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
3481 context_type);
4c4b4cd2 3482 if (i < 0)
323e0a4a 3483 error (_("Could not find a match for %s"),
4c4b4cd2
PH
3484 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
3485 }
3486
3487 exp->elts[pc + 4].block = candidates[i].block;
d12307c1 3488 exp->elts[pc + 5].symbol = candidates[i].symbol;
1265e4aa
JB
3489 if (innermost_block == NULL
3490 || contained_in (candidates[i].block, innermost_block))
4c4b4cd2
PH
3491 innermost_block = candidates[i].block;
3492 }
14f9c5c9
AS
3493 }
3494 break;
3495 case BINOP_ADD:
3496 case BINOP_SUB:
3497 case BINOP_MUL:
3498 case BINOP_DIV:
3499 case BINOP_REM:
3500 case BINOP_MOD:
3501 case BINOP_CONCAT:
3502 case BINOP_BITWISE_AND:
3503 case BINOP_BITWISE_IOR:
3504 case BINOP_BITWISE_XOR:
3505 case BINOP_EQUAL:
3506 case BINOP_NOTEQUAL:
3507 case BINOP_LESS:
3508 case BINOP_GTR:
3509 case BINOP_LEQ:
3510 case BINOP_GEQ:
3511 case BINOP_EXP:
3512 case UNOP_NEG:
3513 case UNOP_PLUS:
3514 case UNOP_LOGICAL_NOT:
3515 case UNOP_ABS:
3516 if (possible_user_operator_p (op, argvec))
4c4b4cd2 3517 {
d12307c1 3518 struct block_symbol *candidates;
4c4b4cd2
PH
3519 int n_candidates;
3520
3521 n_candidates =
3522 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
3523 (struct block *) NULL, VAR_DOMAIN,
4eeaa230 3524 &candidates);
4c4b4cd2 3525 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
76a01679 3526 ada_decoded_op_name (op), NULL);
4c4b4cd2
PH
3527 if (i < 0)
3528 break;
3529
d12307c1
PMR
3530 replace_operator_with_call (expp, pc, nargs, 1,
3531 candidates[i].symbol,
3532 candidates[i].block);
4c4b4cd2
PH
3533 exp = *expp;
3534 }
14f9c5c9 3535 break;
4c4b4cd2
PH
3536
3537 case OP_TYPE:
b3dbf008 3538 case OP_REGISTER:
4c4b4cd2 3539 return NULL;
14f9c5c9
AS
3540 }
3541
3542 *pos = pc;
3543 return evaluate_subexp_type (exp, pos);
3544}
3545
3546/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
4c4b4cd2 3547 MAY_DEREF is non-zero, the formal may be a pointer and the actual
5b3d5b7d 3548 a non-pointer. */
14f9c5c9 3549/* The term "match" here is rather loose. The match is heuristic and
5b3d5b7d 3550 liberal. */
14f9c5c9
AS
3551
3552static int
4dc81987 3553ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9 3554{
61ee279c
PH
3555 ftype = ada_check_typedef (ftype);
3556 atype = ada_check_typedef (atype);
14f9c5c9
AS
3557
3558 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
3559 ftype = TYPE_TARGET_TYPE (ftype);
3560 if (TYPE_CODE (atype) == TYPE_CODE_REF)
3561 atype = TYPE_TARGET_TYPE (atype);
3562
d2e4a39e 3563 switch (TYPE_CODE (ftype))
14f9c5c9
AS
3564 {
3565 default:
5b3d5b7d 3566 return TYPE_CODE (ftype) == TYPE_CODE (atype);
14f9c5c9
AS
3567 case TYPE_CODE_PTR:
3568 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
4c4b4cd2
PH
3569 return ada_type_match (TYPE_TARGET_TYPE (ftype),
3570 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e 3571 else
1265e4aa
JB
3572 return (may_deref
3573 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
14f9c5c9
AS
3574 case TYPE_CODE_INT:
3575 case TYPE_CODE_ENUM:
3576 case TYPE_CODE_RANGE:
3577 switch (TYPE_CODE (atype))
4c4b4cd2
PH
3578 {
3579 case TYPE_CODE_INT:
3580 case TYPE_CODE_ENUM:
3581 case TYPE_CODE_RANGE:
3582 return 1;
3583 default:
3584 return 0;
3585 }
14f9c5c9
AS
3586
3587 case TYPE_CODE_ARRAY:
d2e4a39e 3588 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
4c4b4cd2 3589 || ada_is_array_descriptor_type (atype));
14f9c5c9
AS
3590
3591 case TYPE_CODE_STRUCT:
4c4b4cd2
PH
3592 if (ada_is_array_descriptor_type (ftype))
3593 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3594 || ada_is_array_descriptor_type (atype));
14f9c5c9 3595 else
4c4b4cd2
PH
3596 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
3597 && !ada_is_array_descriptor_type (atype));
14f9c5c9
AS
3598
3599 case TYPE_CODE_UNION:
3600 case TYPE_CODE_FLT:
3601 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
3602 }
3603}
3604
3605/* Return non-zero if the formals of FUNC "sufficiently match" the
3606 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3607 may also be an enumeral, in which case it is treated as a 0-
4c4b4cd2 3608 argument function. */
14f9c5c9
AS
3609
3610static int
d2e4a39e 3611ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
14f9c5c9
AS
3612{
3613 int i;
d2e4a39e 3614 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 3615
1265e4aa
JB
3616 if (SYMBOL_CLASS (func) == LOC_CONST
3617 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
14f9c5c9
AS
3618 return (n_actuals == 0);
3619 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
3620 return 0;
3621
3622 if (TYPE_NFIELDS (func_type) != n_actuals)
3623 return 0;
3624
3625 for (i = 0; i < n_actuals; i += 1)
3626 {
4c4b4cd2 3627 if (actuals[i] == NULL)
76a01679
JB
3628 return 0;
3629 else
3630 {
5b4ee69b
MS
3631 struct type *ftype = ada_check_typedef (TYPE_FIELD_TYPE (func_type,
3632 i));
df407dfe 3633 struct type *atype = ada_check_typedef (value_type (actuals[i]));
4c4b4cd2 3634
76a01679
JB
3635 if (!ada_type_match (ftype, atype, 1))
3636 return 0;
3637 }
14f9c5c9
AS
3638 }
3639 return 1;
3640}
3641
3642/* False iff function type FUNC_TYPE definitely does not produce a value
3643 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3644 FUNC_TYPE is not a valid function type with a non-null return type
3645 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
3646
3647static int
d2e4a39e 3648return_match (struct type *func_type, struct type *context_type)
14f9c5c9 3649{
d2e4a39e 3650 struct type *return_type;
14f9c5c9
AS
3651
3652 if (func_type == NULL)
3653 return 1;
3654
4c4b4cd2 3655 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
18af8284 3656 return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
4c4b4cd2 3657 else
18af8284 3658 return_type = get_base_type (func_type);
14f9c5c9
AS
3659 if (return_type == NULL)
3660 return 1;
3661
18af8284 3662 context_type = get_base_type (context_type);
14f9c5c9
AS
3663
3664 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
3665 return context_type == NULL || return_type == context_type;
3666 else if (context_type == NULL)
3667 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
3668 else
3669 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
3670}
3671
3672
4c4b4cd2 3673/* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
14f9c5c9 3674 function (if any) that matches the types of the NARGS arguments in
4c4b4cd2
PH
3675 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3676 that returns that type, then eliminate matches that don't. If
3677 CONTEXT_TYPE is void and there is at least one match that does not
3678 return void, eliminate all matches that do.
3679
14f9c5c9
AS
3680 Asks the user if there is more than one match remaining. Returns -1
3681 if there is no such symbol or none is selected. NAME is used
4c4b4cd2
PH
3682 solely for messages. May re-arrange and modify SYMS in
3683 the process; the index returned is for the modified vector. */
14f9c5c9 3684
4c4b4cd2 3685static int
d12307c1 3686ada_resolve_function (struct block_symbol syms[],
4c4b4cd2
PH
3687 int nsyms, struct value **args, int nargs,
3688 const char *name, struct type *context_type)
14f9c5c9 3689{
30b15541 3690 int fallback;
14f9c5c9 3691 int k;
4c4b4cd2 3692 int m; /* Number of hits */
14f9c5c9 3693
d2e4a39e 3694 m = 0;
30b15541
UW
3695 /* In the first pass of the loop, we only accept functions matching
3696 context_type. If none are found, we add a second pass of the loop
3697 where every function is accepted. */
3698 for (fallback = 0; m == 0 && fallback < 2; fallback++)
14f9c5c9
AS
3699 {
3700 for (k = 0; k < nsyms; k += 1)
4c4b4cd2 3701 {
d12307c1 3702 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].symbol));
4c4b4cd2 3703
d12307c1 3704 if (ada_args_match (syms[k].symbol, args, nargs)
30b15541 3705 && (fallback || return_match (type, context_type)))
4c4b4cd2
PH
3706 {
3707 syms[m] = syms[k];
3708 m += 1;
3709 }
3710 }
14f9c5c9
AS
3711 }
3712
dc5c8746
PMR
3713 /* If we got multiple matches, ask the user which one to use. Don't do this
3714 interactive thing during completion, though, as the purpose of the
3715 completion is providing a list of all possible matches. Prompting the
3716 user to filter it down would be completely unexpected in this case. */
14f9c5c9
AS
3717 if (m == 0)
3718 return -1;
dc5c8746 3719 else if (m > 1 && !parse_completion)
14f9c5c9 3720 {
323e0a4a 3721 printf_filtered (_("Multiple matches for %s\n"), name);
4c4b4cd2 3722 user_select_syms (syms, m, 1);
14f9c5c9
AS
3723 return 0;
3724 }
3725 return 0;
3726}
3727
4c4b4cd2
PH
3728/* Returns true (non-zero) iff decoded name N0 should appear before N1
3729 in a listing of choices during disambiguation (see sort_choices, below).
3730 The idea is that overloadings of a subprogram name from the
3731 same package should sort in their source order. We settle for ordering
3732 such symbols by their trailing number (__N or $N). */
3733
14f9c5c9 3734static int
0d5cff50 3735encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9
AS
3736{
3737 if (N1 == NULL)
3738 return 0;
3739 else if (N0 == NULL)
3740 return 1;
3741 else
3742 {
3743 int k0, k1;
5b4ee69b 3744
d2e4a39e 3745 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
4c4b4cd2 3746 ;
d2e4a39e 3747 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
4c4b4cd2 3748 ;
d2e4a39e 3749 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
4c4b4cd2
PH
3750 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3751 {
3752 int n0, n1;
5b4ee69b 3753
4c4b4cd2
PH
3754 n0 = k0;
3755 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3756 n0 -= 1;
3757 n1 = k1;
3758 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3759 n1 -= 1;
3760 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3761 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3762 }
14f9c5c9
AS
3763 return (strcmp (N0, N1) < 0);
3764 }
3765}
d2e4a39e 3766
4c4b4cd2
PH
3767/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3768 encoded names. */
3769
d2e4a39e 3770static void
d12307c1 3771sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3772{
4c4b4cd2 3773 int i;
5b4ee69b 3774
d2e4a39e 3775 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3776 {
d12307c1 3777 struct block_symbol sym = syms[i];
14f9c5c9
AS
3778 int j;
3779
d2e4a39e 3780 for (j = i - 1; j >= 0; j -= 1)
4c4b4cd2 3781 {
d12307c1
PMR
3782 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].symbol),
3783 SYMBOL_LINKAGE_NAME (sym.symbol)))
4c4b4cd2
PH
3784 break;
3785 syms[j + 1] = syms[j];
3786 }
d2e4a39e 3787 syms[j + 1] = sym;
14f9c5c9
AS
3788 }
3789}
3790
4c4b4cd2
PH
3791/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3792 by asking the user (if necessary), returning the number selected,
3793 and setting the first elements of SYMS items. Error if no symbols
3794 selected. */
14f9c5c9
AS
3795
3796/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
4c4b4cd2 3797 to be re-integrated one of these days. */
14f9c5c9
AS
3798
3799int
d12307c1 3800user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9
AS
3801{
3802 int i;
8d749320 3803 int *chosen = XALLOCAVEC (int , nsyms);
14f9c5c9
AS
3804 int n_chosen;
3805 int first_choice = (max_results == 1) ? 1 : 2;
717d2f5a 3806 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9
AS
3807
3808 if (max_results < 1)
323e0a4a 3809 error (_("Request to select 0 symbols!"));
14f9c5c9
AS
3810 if (nsyms <= 1)
3811 return nsyms;
3812
717d2f5a
JB
3813 if (select_mode == multiple_symbols_cancel)
3814 error (_("\
3815canceled because the command is ambiguous\n\
3816See set/show multiple-symbol."));
3817
3818 /* If select_mode is "all", then return all possible symbols.
3819 Only do that if more than one symbol can be selected, of course.
3820 Otherwise, display the menu as usual. */
3821 if (select_mode == multiple_symbols_all && max_results > 1)
3822 return nsyms;
3823
323e0a4a 3824 printf_unfiltered (_("[0] cancel\n"));
14f9c5c9 3825 if (max_results > 1)
323e0a4a 3826 printf_unfiltered (_("[1] all\n"));
14f9c5c9 3827
4c4b4cd2 3828 sort_choices (syms, nsyms);
14f9c5c9
AS
3829
3830 for (i = 0; i < nsyms; i += 1)
3831 {
d12307c1 3832 if (syms[i].symbol == NULL)
4c4b4cd2
PH
3833 continue;
3834
d12307c1 3835 if (SYMBOL_CLASS (syms[i].symbol) == LOC_BLOCK)
4c4b4cd2 3836 {
76a01679 3837 struct symtab_and_line sal =
d12307c1 3838 find_function_start_sal (syms[i].symbol, 1);
5b4ee69b 3839
323e0a4a
AC
3840 if (sal.symtab == NULL)
3841 printf_unfiltered (_("[%d] %s at <no source file available>:%d\n"),
3842 i + first_choice,
d12307c1 3843 SYMBOL_PRINT_NAME (syms[i].symbol),
323e0a4a
AC
3844 sal.line);
3845 else
3846 printf_unfiltered (_("[%d] %s at %s:%d\n"), i + first_choice,
d12307c1 3847 SYMBOL_PRINT_NAME (syms[i].symbol),
05cba821
JK
3848 symtab_to_filename_for_display (sal.symtab),
3849 sal.line);
4c4b4cd2
PH
3850 continue;
3851 }
d2e4a39e 3852 else
4c4b4cd2
PH
3853 {
3854 int is_enumeral =
d12307c1
PMR
3855 (SYMBOL_CLASS (syms[i].symbol) == LOC_CONST
3856 && SYMBOL_TYPE (syms[i].symbol) != NULL
3857 && TYPE_CODE (SYMBOL_TYPE (syms[i].symbol)) == TYPE_CODE_ENUM);
1994afbf
DE
3858 struct symtab *symtab = NULL;
3859
d12307c1
PMR
3860 if (SYMBOL_OBJFILE_OWNED (syms[i].symbol))
3861 symtab = symbol_symtab (syms[i].symbol);
4c4b4cd2 3862
d12307c1 3863 if (SYMBOL_LINE (syms[i].symbol) != 0 && symtab != NULL)
323e0a4a 3864 printf_unfiltered (_("[%d] %s at %s:%d\n"),
4c4b4cd2 3865 i + first_choice,
d12307c1 3866 SYMBOL_PRINT_NAME (syms[i].symbol),
05cba821 3867 symtab_to_filename_for_display (symtab),
d12307c1 3868 SYMBOL_LINE (syms[i].symbol));
76a01679 3869 else if (is_enumeral
d12307c1 3870 && TYPE_NAME (SYMBOL_TYPE (syms[i].symbol)) != NULL)
4c4b4cd2 3871 {
a3f17187 3872 printf_unfiltered (("[%d] "), i + first_choice);
d12307c1 3873 ada_print_type (SYMBOL_TYPE (syms[i].symbol), NULL,
79d43c61 3874 gdb_stdout, -1, 0, &type_print_raw_options);
323e0a4a 3875 printf_unfiltered (_("'(%s) (enumeral)\n"),
d12307c1 3876 SYMBOL_PRINT_NAME (syms[i].symbol));
4c4b4cd2
PH
3877 }
3878 else if (symtab != NULL)
3879 printf_unfiltered (is_enumeral
323e0a4a
AC
3880 ? _("[%d] %s in %s (enumeral)\n")
3881 : _("[%d] %s at %s:?\n"),
4c4b4cd2 3882 i + first_choice,
d12307c1 3883 SYMBOL_PRINT_NAME (syms[i].symbol),
05cba821 3884 symtab_to_filename_for_display (symtab));
4c4b4cd2
PH
3885 else
3886 printf_unfiltered (is_enumeral
323e0a4a
AC
3887 ? _("[%d] %s (enumeral)\n")
3888 : _("[%d] %s at ?\n"),
4c4b4cd2 3889 i + first_choice,
d12307c1 3890 SYMBOL_PRINT_NAME (syms[i].symbol));
4c4b4cd2 3891 }
14f9c5c9 3892 }
d2e4a39e 3893
14f9c5c9 3894 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
4c4b4cd2 3895 "overload-choice");
14f9c5c9
AS
3896
3897 for (i = 0; i < n_chosen; i += 1)
4c4b4cd2 3898 syms[i] = syms[chosen[i]];
14f9c5c9
AS
3899
3900 return n_chosen;
3901}
3902
3903/* Read and validate a set of numeric choices from the user in the
4c4b4cd2 3904 range 0 .. N_CHOICES-1. Place the results in increasing
14f9c5c9
AS
3905 order in CHOICES[0 .. N-1], and return N.
3906
3907 The user types choices as a sequence of numbers on one line
3908 separated by blanks, encoding them as follows:
3909
4c4b4cd2 3910 + A choice of 0 means to cancel the selection, throwing an error.
14f9c5c9
AS
3911 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3912 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3913
4c4b4cd2 3914 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9
AS
3915
3916 ANNOTATION_SUFFIX, if present, is used to annotate the input
4c4b4cd2 3917 prompts (for use with the -f switch). */
14f9c5c9
AS
3918
3919int
d2e4a39e 3920get_selections (int *choices, int n_choices, int max_results,
4c4b4cd2 3921 int is_all_choice, char *annotation_suffix)
14f9c5c9 3922{
d2e4a39e 3923 char *args;
0bcd0149 3924 char *prompt;
14f9c5c9
AS
3925 int n_chosen;
3926 int first_choice = is_all_choice ? 2 : 1;
d2e4a39e 3927
14f9c5c9
AS
3928 prompt = getenv ("PS2");
3929 if (prompt == NULL)
0bcd0149 3930 prompt = "> ";
14f9c5c9 3931
0bcd0149 3932 args = command_line_input (prompt, 0, annotation_suffix);
d2e4a39e 3933
14f9c5c9 3934 if (args == NULL)
323e0a4a 3935 error_no_arg (_("one or more choice numbers"));
14f9c5c9
AS
3936
3937 n_chosen = 0;
76a01679 3938
4c4b4cd2
PH
3939 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3940 order, as given in args. Choices are validated. */
14f9c5c9
AS
3941 while (1)
3942 {
d2e4a39e 3943 char *args2;
14f9c5c9
AS
3944 int choice, j;
3945
0fcd72ba 3946 args = skip_spaces (args);
14f9c5c9 3947 if (*args == '\0' && n_chosen == 0)
323e0a4a 3948 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3949 else if (*args == '\0')
4c4b4cd2 3950 break;
14f9c5c9
AS
3951
3952 choice = strtol (args, &args2, 10);
d2e4a39e 3953 if (args == args2 || choice < 0
4c4b4cd2 3954 || choice > n_choices + first_choice - 1)
323e0a4a 3955 error (_("Argument must be choice number"));
14f9c5c9
AS
3956 args = args2;
3957
d2e4a39e 3958 if (choice == 0)
323e0a4a 3959 error (_("cancelled"));
14f9c5c9
AS
3960
3961 if (choice < first_choice)
4c4b4cd2
PH
3962 {
3963 n_chosen = n_choices;
3964 for (j = 0; j < n_choices; j += 1)
3965 choices[j] = j;
3966 break;
3967 }
14f9c5c9
AS
3968 choice -= first_choice;
3969
d2e4a39e 3970 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
4c4b4cd2
PH
3971 {
3972 }
14f9c5c9
AS
3973
3974 if (j < 0 || choice != choices[j])
4c4b4cd2
PH
3975 {
3976 int k;
5b4ee69b 3977
4c4b4cd2
PH
3978 for (k = n_chosen - 1; k > j; k -= 1)
3979 choices[k + 1] = choices[k];
3980 choices[j + 1] = choice;
3981 n_chosen += 1;
3982 }
14f9c5c9
AS
3983 }
3984
3985 if (n_chosen > max_results)
323e0a4a 3986 error (_("Select no more than %d of the above"), max_results);
d2e4a39e 3987
14f9c5c9
AS
3988 return n_chosen;
3989}
3990
4c4b4cd2
PH
3991/* Replace the operator of length OPLEN at position PC in *EXPP with a call
3992 on the function identified by SYM and BLOCK, and taking NARGS
3993 arguments. Update *EXPP as needed to hold more space. */
14f9c5c9
AS
3994
3995static void
d2e4a39e 3996replace_operator_with_call (struct expression **expp, int pc, int nargs,
4c4b4cd2 3997 int oplen, struct symbol *sym,
270140bd 3998 const struct block *block)
14f9c5c9
AS
3999{
4000 /* A new expression, with 6 more elements (3 for funcall, 4 for function
4c4b4cd2 4001 symbol, -oplen for operator being replaced). */
d2e4a39e 4002 struct expression *newexp = (struct expression *)
8c1a34e7 4003 xzalloc (sizeof (struct expression)
4c4b4cd2 4004 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
d2e4a39e 4005 struct expression *exp = *expp;
14f9c5c9
AS
4006
4007 newexp->nelts = exp->nelts + 7 - oplen;
4008 newexp->language_defn = exp->language_defn;
3489610d 4009 newexp->gdbarch = exp->gdbarch;
14f9c5c9 4010 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
d2e4a39e 4011 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
4c4b4cd2 4012 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
14f9c5c9
AS
4013
4014 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
4015 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
4016
4017 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
4018 newexp->elts[pc + 4].block = block;
4019 newexp->elts[pc + 5].symbol = sym;
4020
4021 *expp = newexp;
aacb1f0a 4022 xfree (exp);
d2e4a39e 4023}
14f9c5c9
AS
4024
4025/* Type-class predicates */
4026
4c4b4cd2
PH
4027/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4028 or FLOAT). */
14f9c5c9
AS
4029
4030static int
d2e4a39e 4031numeric_type_p (struct type *type)
14f9c5c9
AS
4032{
4033 if (type == NULL)
4034 return 0;
d2e4a39e
AS
4035 else
4036 {
4037 switch (TYPE_CODE (type))
4c4b4cd2
PH
4038 {
4039 case TYPE_CODE_INT:
4040 case TYPE_CODE_FLT:
4041 return 1;
4042 case TYPE_CODE_RANGE:
4043 return (type == TYPE_TARGET_TYPE (type)
4044 || numeric_type_p (TYPE_TARGET_TYPE (type)));
4045 default:
4046 return 0;
4047 }
d2e4a39e 4048 }
14f9c5c9
AS
4049}
4050
4c4b4cd2 4051/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4052
4053static int
d2e4a39e 4054integer_type_p (struct type *type)
14f9c5c9
AS
4055{
4056 if (type == NULL)
4057 return 0;
d2e4a39e
AS
4058 else
4059 {
4060 switch (TYPE_CODE (type))
4c4b4cd2
PH
4061 {
4062 case TYPE_CODE_INT:
4063 return 1;
4064 case TYPE_CODE_RANGE:
4065 return (type == TYPE_TARGET_TYPE (type)
4066 || integer_type_p (TYPE_TARGET_TYPE (type)));
4067 default:
4068 return 0;
4069 }
d2e4a39e 4070 }
14f9c5c9
AS
4071}
4072
4c4b4cd2 4073/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4074
4075static int
d2e4a39e 4076scalar_type_p (struct type *type)
14f9c5c9
AS
4077{
4078 if (type == NULL)
4079 return 0;
d2e4a39e
AS
4080 else
4081 {
4082 switch (TYPE_CODE (type))
4c4b4cd2
PH
4083 {
4084 case TYPE_CODE_INT:
4085 case TYPE_CODE_RANGE:
4086 case TYPE_CODE_ENUM:
4087 case TYPE_CODE_FLT:
4088 return 1;
4089 default:
4090 return 0;
4091 }
d2e4a39e 4092 }
14f9c5c9
AS
4093}
4094
4c4b4cd2 4095/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
4096
4097static int
d2e4a39e 4098discrete_type_p (struct type *type)
14f9c5c9
AS
4099{
4100 if (type == NULL)
4101 return 0;
d2e4a39e
AS
4102 else
4103 {
4104 switch (TYPE_CODE (type))
4c4b4cd2
PH
4105 {
4106 case TYPE_CODE_INT:
4107 case TYPE_CODE_RANGE:
4108 case TYPE_CODE_ENUM:
872f0337 4109 case TYPE_CODE_BOOL:
4c4b4cd2
PH
4110 return 1;
4111 default:
4112 return 0;
4113 }
d2e4a39e 4114 }
14f9c5c9
AS
4115}
4116
4c4b4cd2
PH
4117/* Returns non-zero if OP with operands in the vector ARGS could be
4118 a user-defined function. Errs on the side of pre-defined operators
4119 (i.e., result 0). */
14f9c5c9
AS
4120
4121static int
d2e4a39e 4122possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4123{
76a01679 4124 struct type *type0 =
df407dfe 4125 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 4126 struct type *type1 =
df407dfe 4127 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 4128
4c4b4cd2
PH
4129 if (type0 == NULL)
4130 return 0;
4131
14f9c5c9
AS
4132 switch (op)
4133 {
4134 default:
4135 return 0;
4136
4137 case BINOP_ADD:
4138 case BINOP_SUB:
4139 case BINOP_MUL:
4140 case BINOP_DIV:
d2e4a39e 4141 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4142
4143 case BINOP_REM:
4144 case BINOP_MOD:
4145 case BINOP_BITWISE_AND:
4146 case BINOP_BITWISE_IOR:
4147 case BINOP_BITWISE_XOR:
d2e4a39e 4148 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4149
4150 case BINOP_EQUAL:
4151 case BINOP_NOTEQUAL:
4152 case BINOP_LESS:
4153 case BINOP_GTR:
4154 case BINOP_LEQ:
4155 case BINOP_GEQ:
d2e4a39e 4156 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4157
4158 case BINOP_CONCAT:
ee90b9ab 4159 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4160
4161 case BINOP_EXP:
d2e4a39e 4162 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4163
4164 case UNOP_NEG:
4165 case UNOP_PLUS:
4166 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4167 case UNOP_ABS:
4168 return (!numeric_type_p (type0));
14f9c5c9
AS
4169
4170 }
4171}
4172\f
4c4b4cd2 4173 /* Renaming */
14f9c5c9 4174
aeb5907d
JB
4175/* NOTES:
4176
4177 1. In the following, we assume that a renaming type's name may
4178 have an ___XD suffix. It would be nice if this went away at some
4179 point.
4180 2. We handle both the (old) purely type-based representation of
4181 renamings and the (new) variable-based encoding. At some point,
4182 it is devoutly to be hoped that the former goes away
4183 (FIXME: hilfinger-2007-07-09).
4184 3. Subprogram renamings are not implemented, although the XRS
4185 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4186
4187/* If SYM encodes a renaming,
4188
4189 <renaming> renames <renamed entity>,
4190
4191 sets *LEN to the length of the renamed entity's name,
4192 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4193 the string describing the subcomponent selected from the renamed
0963b4bd 4194 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4195 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4196 are undefined). Otherwise, returns a value indicating the category
4197 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4198 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4199 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4200 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4201 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4202 may be NULL, in which case they are not assigned.
4203
4204 [Currently, however, GCC does not generate subprogram renamings.] */
4205
4206enum ada_renaming_category
4207ada_parse_renaming (struct symbol *sym,
4208 const char **renamed_entity, int *len,
4209 const char **renaming_expr)
4210{
4211 enum ada_renaming_category kind;
4212 const char *info;
4213 const char *suffix;
4214
4215 if (sym == NULL)
4216 return ADA_NOT_RENAMING;
4217 switch (SYMBOL_CLASS (sym))
14f9c5c9 4218 {
aeb5907d
JB
4219 default:
4220 return ADA_NOT_RENAMING;
4221 case LOC_TYPEDEF:
4222 return parse_old_style_renaming (SYMBOL_TYPE (sym),
4223 renamed_entity, len, renaming_expr);
4224 case LOC_LOCAL:
4225 case LOC_STATIC:
4226 case LOC_COMPUTED:
4227 case LOC_OPTIMIZED_OUT:
4228 info = strstr (SYMBOL_LINKAGE_NAME (sym), "___XR");
4229 if (info == NULL)
4230 return ADA_NOT_RENAMING;
4231 switch (info[5])
4232 {
4233 case '_':
4234 kind = ADA_OBJECT_RENAMING;
4235 info += 6;
4236 break;
4237 case 'E':
4238 kind = ADA_EXCEPTION_RENAMING;
4239 info += 7;
4240 break;
4241 case 'P':
4242 kind = ADA_PACKAGE_RENAMING;
4243 info += 7;
4244 break;
4245 case 'S':
4246 kind = ADA_SUBPROGRAM_RENAMING;
4247 info += 7;
4248 break;
4249 default:
4250 return ADA_NOT_RENAMING;
4251 }
14f9c5c9 4252 }
4c4b4cd2 4253
aeb5907d
JB
4254 if (renamed_entity != NULL)
4255 *renamed_entity = info;
4256 suffix = strstr (info, "___XE");
4257 if (suffix == NULL || suffix == info)
4258 return ADA_NOT_RENAMING;
4259 if (len != NULL)
4260 *len = strlen (info) - strlen (suffix);
4261 suffix += 5;
4262 if (renaming_expr != NULL)
4263 *renaming_expr = suffix;
4264 return kind;
4265}
4266
4267/* Assuming TYPE encodes a renaming according to the old encoding in
4268 exp_dbug.ads, returns details of that renaming in *RENAMED_ENTITY,
4269 *LEN, and *RENAMING_EXPR, as for ada_parse_renaming, above. Returns
4270 ADA_NOT_RENAMING otherwise. */
4271static enum ada_renaming_category
4272parse_old_style_renaming (struct type *type,
4273 const char **renamed_entity, int *len,
4274 const char **renaming_expr)
4275{
4276 enum ada_renaming_category kind;
4277 const char *name;
4278 const char *info;
4279 const char *suffix;
14f9c5c9 4280
aeb5907d
JB
4281 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
4282 || TYPE_NFIELDS (type) != 1)
4283 return ADA_NOT_RENAMING;
14f9c5c9 4284
aeb5907d
JB
4285 name = type_name_no_tag (type);
4286 if (name == NULL)
4287 return ADA_NOT_RENAMING;
4288
4289 name = strstr (name, "___XR");
4290 if (name == NULL)
4291 return ADA_NOT_RENAMING;
4292 switch (name[5])
4293 {
4294 case '\0':
4295 case '_':
4296 kind = ADA_OBJECT_RENAMING;
4297 break;
4298 case 'E':
4299 kind = ADA_EXCEPTION_RENAMING;
4300 break;
4301 case 'P':
4302 kind = ADA_PACKAGE_RENAMING;
4303 break;
4304 case 'S':
4305 kind = ADA_SUBPROGRAM_RENAMING;
4306 break;
4307 default:
4308 return ADA_NOT_RENAMING;
4309 }
14f9c5c9 4310
aeb5907d
JB
4311 info = TYPE_FIELD_NAME (type, 0);
4312 if (info == NULL)
4313 return ADA_NOT_RENAMING;
4314 if (renamed_entity != NULL)
4315 *renamed_entity = info;
4316 suffix = strstr (info, "___XE");
4317 if (renaming_expr != NULL)
4318 *renaming_expr = suffix + 5;
4319 if (suffix == NULL || suffix == info)
4320 return ADA_NOT_RENAMING;
4321 if (len != NULL)
4322 *len = suffix - info;
4323 return kind;
a5ee536b
JB
4324}
4325
4326/* Compute the value of the given RENAMING_SYM, which is expected to
4327 be a symbol encoding a renaming expression. BLOCK is the block
4328 used to evaluate the renaming. */
52ce6436 4329
a5ee536b
JB
4330static struct value *
4331ada_read_renaming_var_value (struct symbol *renaming_sym,
3977b71f 4332 const struct block *block)
a5ee536b 4333{
bbc13ae3 4334 const char *sym_name;
a5ee536b
JB
4335 struct expression *expr;
4336 struct value *value;
4337 struct cleanup *old_chain = NULL;
4338
bbc13ae3 4339 sym_name = SYMBOL_LINKAGE_NAME (renaming_sym);
1bb9788d 4340 expr = parse_exp_1 (&sym_name, 0, block, 0);
bbc13ae3 4341 old_chain = make_cleanup (free_current_contents, &expr);
a5ee536b
JB
4342 value = evaluate_expression (expr);
4343
4344 do_cleanups (old_chain);
4345 return value;
4346}
14f9c5c9 4347\f
d2e4a39e 4348
4c4b4cd2 4349 /* Evaluation: Function Calls */
14f9c5c9 4350
4c4b4cd2 4351/* Return an lvalue containing the value VAL. This is the identity on
40bc484c
JB
4352 lvalues, and otherwise has the side-effect of allocating memory
4353 in the inferior where a copy of the value contents is copied. */
14f9c5c9 4354
d2e4a39e 4355static struct value *
40bc484c 4356ensure_lval (struct value *val)
14f9c5c9 4357{
40bc484c
JB
4358 if (VALUE_LVAL (val) == not_lval
4359 || VALUE_LVAL (val) == lval_internalvar)
c3e5cd34 4360 {
df407dfe 4361 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
40bc484c
JB
4362 const CORE_ADDR addr =
4363 value_as_long (value_allocate_space_in_inferior (len));
c3e5cd34 4364
40bc484c 4365 set_value_address (val, addr);
a84a8a0d 4366 VALUE_LVAL (val) = lval_memory;
40bc484c 4367 write_memory (addr, value_contents (val), len);
c3e5cd34 4368 }
14f9c5c9
AS
4369
4370 return val;
4371}
4372
4373/* Return the value ACTUAL, converted to be an appropriate value for a
4374 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4375 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4376 values not residing in memory, updating it as needed. */
14f9c5c9 4377
a93c0eb6 4378struct value *
40bc484c 4379ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4380{
df407dfe 4381 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 4382 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e
AS
4383 struct type *formal_target =
4384 TYPE_CODE (formal_type) == TYPE_CODE_PTR
61ee279c 4385 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e
AS
4386 struct type *actual_target =
4387 TYPE_CODE (actual_type) == TYPE_CODE_PTR
61ee279c 4388 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 4389
4c4b4cd2 4390 if (ada_is_array_descriptor_type (formal_target)
14f9c5c9 4391 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
40bc484c 4392 return make_array_descriptor (formal_type, actual);
a84a8a0d
JB
4393 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR
4394 || TYPE_CODE (formal_type) == TYPE_CODE_REF)
14f9c5c9 4395 {
a84a8a0d 4396 struct value *result;
5b4ee69b 4397
14f9c5c9 4398 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4c4b4cd2 4399 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4400 result = desc_data (actual);
14f9c5c9 4401 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4c4b4cd2
PH
4402 {
4403 if (VALUE_LVAL (actual) != lval_memory)
4404 {
4405 struct value *val;
5b4ee69b 4406
df407dfe 4407 actual_type = ada_check_typedef (value_type (actual));
4c4b4cd2 4408 val = allocate_value (actual_type);
990a07ab 4409 memcpy ((char *) value_contents_raw (val),
0fd88904 4410 (char *) value_contents (actual),
4c4b4cd2 4411 TYPE_LENGTH (actual_type));
40bc484c 4412 actual = ensure_lval (val);
4c4b4cd2 4413 }
a84a8a0d 4414 result = value_addr (actual);
4c4b4cd2 4415 }
a84a8a0d
JB
4416 else
4417 return actual;
b1af9e97 4418 return value_cast_pointers (formal_type, result, 0);
14f9c5c9
AS
4419 }
4420 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
4421 return ada_value_ind (actual);
8344af1e
JB
4422 else if (ada_is_aligner_type (formal_type))
4423 {
4424 /* We need to turn this parameter into an aligner type
4425 as well. */
4426 struct value *aligner = allocate_value (formal_type);
4427 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4428
4429 value_assign_to_component (aligner, component, actual);
4430 return aligner;
4431 }
14f9c5c9
AS
4432
4433 return actual;
4434}
4435
438c98a1
JB
4436/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4437 type TYPE. This is usually an inefficient no-op except on some targets
4438 (such as AVR) where the representation of a pointer and an address
4439 differs. */
4440
4441static CORE_ADDR
4442value_pointer (struct value *value, struct type *type)
4443{
4444 struct gdbarch *gdbarch = get_type_arch (type);
4445 unsigned len = TYPE_LENGTH (type);
224c3ddb 4446 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4447 CORE_ADDR addr;
4448
4449 addr = value_address (value);
4450 gdbarch_address_to_pointer (gdbarch, type, buf, addr);
4451 addr = extract_unsigned_integer (buf, len, gdbarch_byte_order (gdbarch));
4452 return addr;
4453}
4454
14f9c5c9 4455
4c4b4cd2
PH
4456/* Push a descriptor of type TYPE for array value ARR on the stack at
4457 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4458 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4459 to-descriptor type rather than a descriptor type), a struct value *
4460 representing a pointer to this descriptor. */
14f9c5c9 4461
d2e4a39e 4462static struct value *
40bc484c 4463make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4464{
d2e4a39e
AS
4465 struct type *bounds_type = desc_bounds_type (type);
4466 struct type *desc_type = desc_base_type (type);
4467 struct value *descriptor = allocate_value (desc_type);
4468 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4469 int i;
d2e4a39e 4470
0963b4bd
MS
4471 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4472 i > 0; i -= 1)
14f9c5c9 4473 {
19f220c3
JK
4474 modify_field (value_type (bounds), value_contents_writeable (bounds),
4475 ada_array_bound (arr, i, 0),
4476 desc_bound_bitpos (bounds_type, i, 0),
4477 desc_bound_bitsize (bounds_type, i, 0));
4478 modify_field (value_type (bounds), value_contents_writeable (bounds),
4479 ada_array_bound (arr, i, 1),
4480 desc_bound_bitpos (bounds_type, i, 1),
4481 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4482 }
d2e4a39e 4483
40bc484c 4484 bounds = ensure_lval (bounds);
d2e4a39e 4485
19f220c3
JK
4486 modify_field (value_type (descriptor),
4487 value_contents_writeable (descriptor),
4488 value_pointer (ensure_lval (arr),
4489 TYPE_FIELD_TYPE (desc_type, 0)),
4490 fat_pntr_data_bitpos (desc_type),
4491 fat_pntr_data_bitsize (desc_type));
4492
4493 modify_field (value_type (descriptor),
4494 value_contents_writeable (descriptor),
4495 value_pointer (bounds,
4496 TYPE_FIELD_TYPE (desc_type, 1)),
4497 fat_pntr_bounds_bitpos (desc_type),
4498 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4499
40bc484c 4500 descriptor = ensure_lval (descriptor);
14f9c5c9
AS
4501
4502 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4503 return value_addr (descriptor);
4504 else
4505 return descriptor;
4506}
14f9c5c9 4507\f
3d9434b5
JB
4508 /* Symbol Cache Module */
4509
3d9434b5 4510/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4511 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4512 on the type of entity being printed, the cache can make it as much
4513 as an order of magnitude faster than without it.
4514
4515 The descriptive type DWARF extension has significantly reduced
4516 the need for this cache, at least when DWARF is being used. However,
4517 even in this case, some expensive name-based symbol searches are still
4518 sometimes necessary - to find an XVZ variable, mostly. */
4519
ee01b665 4520/* Initialize the contents of SYM_CACHE. */
3d9434b5 4521
ee01b665
JB
4522static void
4523ada_init_symbol_cache (struct ada_symbol_cache *sym_cache)
4524{
4525 obstack_init (&sym_cache->cache_space);
4526 memset (sym_cache->root, '\000', sizeof (sym_cache->root));
4527}
3d9434b5 4528
ee01b665
JB
4529/* Free the memory used by SYM_CACHE. */
4530
4531static void
4532ada_free_symbol_cache (struct ada_symbol_cache *sym_cache)
3d9434b5 4533{
ee01b665
JB
4534 obstack_free (&sym_cache->cache_space, NULL);
4535 xfree (sym_cache);
4536}
3d9434b5 4537
ee01b665
JB
4538/* Return the symbol cache associated to the given program space PSPACE.
4539 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4540
ee01b665
JB
4541static struct ada_symbol_cache *
4542ada_get_symbol_cache (struct program_space *pspace)
4543{
4544 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4545
66c168ae 4546 if (pspace_data->sym_cache == NULL)
ee01b665 4547 {
66c168ae
JB
4548 pspace_data->sym_cache = XCNEW (struct ada_symbol_cache);
4549 ada_init_symbol_cache (pspace_data->sym_cache);
ee01b665
JB
4550 }
4551
66c168ae 4552 return pspace_data->sym_cache;
ee01b665 4553}
3d9434b5
JB
4554
4555/* Clear all entries from the symbol cache. */
4556
4557static void
4558ada_clear_symbol_cache (void)
4559{
ee01b665
JB
4560 struct ada_symbol_cache *sym_cache
4561 = ada_get_symbol_cache (current_program_space);
4562
4563 obstack_free (&sym_cache->cache_space, NULL);
4564 ada_init_symbol_cache (sym_cache);
3d9434b5
JB
4565}
4566
fe978cb0 4567/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4568 Return it if found, or NULL otherwise. */
4569
4570static struct cache_entry **
fe978cb0 4571find_entry (const char *name, domain_enum domain)
3d9434b5 4572{
ee01b665
JB
4573 struct ada_symbol_cache *sym_cache
4574 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4575 int h = msymbol_hash (name) % HASH_SIZE;
4576 struct cache_entry **e;
4577
ee01b665 4578 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4579 {
fe978cb0 4580 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
3d9434b5
JB
4581 return e;
4582 }
4583 return NULL;
4584}
4585
fe978cb0 4586/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4587 Return 1 if found, 0 otherwise.
4588
4589 If an entry was found and SYM is not NULL, set *SYM to the entry's
4590 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4591
96d887e8 4592static int
fe978cb0 4593lookup_cached_symbol (const char *name, domain_enum domain,
f0c5f9b2 4594 struct symbol **sym, const struct block **block)
96d887e8 4595{
fe978cb0 4596 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4597
4598 if (e == NULL)
4599 return 0;
4600 if (sym != NULL)
4601 *sym = (*e)->sym;
4602 if (block != NULL)
4603 *block = (*e)->block;
4604 return 1;
96d887e8
PH
4605}
4606
3d9434b5 4607/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4608 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4609
96d887e8 4610static void
fe978cb0 4611cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
270140bd 4612 const struct block *block)
96d887e8 4613{
ee01b665
JB
4614 struct ada_symbol_cache *sym_cache
4615 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4616 int h;
4617 char *copy;
4618 struct cache_entry *e;
4619
1994afbf
DE
4620 /* Symbols for builtin types don't have a block.
4621 For now don't cache such symbols. */
4622 if (sym != NULL && !SYMBOL_OBJFILE_OWNED (sym))
4623 return;
4624
3d9434b5
JB
4625 /* If the symbol is a local symbol, then do not cache it, as a search
4626 for that symbol depends on the context. To determine whether
4627 the symbol is local or not, we check the block where we found it
4628 against the global and static blocks of its associated symtab. */
4629 if (sym
08be3fe3 4630 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4631 GLOBAL_BLOCK) != block
08be3fe3 4632 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4633 STATIC_BLOCK) != block)
3d9434b5
JB
4634 return;
4635
4636 h = msymbol_hash (name) % HASH_SIZE;
ee01b665
JB
4637 e = (struct cache_entry *) obstack_alloc (&sym_cache->cache_space,
4638 sizeof (*e));
4639 e->next = sym_cache->root[h];
4640 sym_cache->root[h] = e;
224c3ddb
SM
4641 e->name = copy
4642 = (char *) obstack_alloc (&sym_cache->cache_space, strlen (name) + 1);
3d9434b5
JB
4643 strcpy (copy, name);
4644 e->sym = sym;
fe978cb0 4645 e->domain = domain;
3d9434b5 4646 e->block = block;
96d887e8 4647}
4c4b4cd2
PH
4648\f
4649 /* Symbol Lookup */
4650
c0431670
JB
4651/* Return nonzero if wild matching should be used when searching for
4652 all symbols matching LOOKUP_NAME.
4653
4654 LOOKUP_NAME is expected to be a symbol name after transformation
4655 for Ada lookups (see ada_name_for_lookup). */
4656
4657static int
4658should_use_wild_match (const char *lookup_name)
4659{
4660 return (strstr (lookup_name, "__") == NULL);
4661}
4662
4c4b4cd2
PH
4663/* Return the result of a standard (literal, C-like) lookup of NAME in
4664 given DOMAIN, visible from lexical block BLOCK. */
4665
4666static struct symbol *
4667standard_lookup (const char *name, const struct block *block,
4668 domain_enum domain)
4669{
acbd605d 4670 /* Initialize it just to avoid a GCC false warning. */
d12307c1 4671 struct block_symbol sym = {NULL, NULL};
4c4b4cd2 4672
d12307c1
PMR
4673 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4674 return sym.symbol;
2570f2b7 4675 sym = lookup_symbol_in_language (name, block, domain, language_c, 0);
d12307c1
PMR
4676 cache_symbol (name, domain, sym.symbol, sym.block);
4677 return sym.symbol;
4c4b4cd2
PH
4678}
4679
4680
4681/* Non-zero iff there is at least one non-function/non-enumeral symbol
4682 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
4683 since they contend in overloading in the same way. */
4684static int
d12307c1 4685is_nonfunction (struct block_symbol syms[], int n)
4c4b4cd2
PH
4686{
4687 int i;
4688
4689 for (i = 0; i < n; i += 1)
d12307c1
PMR
4690 if (TYPE_CODE (SYMBOL_TYPE (syms[i].symbol)) != TYPE_CODE_FUNC
4691 && (TYPE_CODE (SYMBOL_TYPE (syms[i].symbol)) != TYPE_CODE_ENUM
4692 || SYMBOL_CLASS (syms[i].symbol) != LOC_CONST))
14f9c5c9
AS
4693 return 1;
4694
4695 return 0;
4696}
4697
4698/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4699 struct types. Otherwise, they may not. */
14f9c5c9
AS
4700
4701static int
d2e4a39e 4702equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4703{
d2e4a39e 4704 if (type0 == type1)
14f9c5c9 4705 return 1;
d2e4a39e 4706 if (type0 == NULL || type1 == NULL
14f9c5c9
AS
4707 || TYPE_CODE (type0) != TYPE_CODE (type1))
4708 return 0;
d2e4a39e 4709 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
14f9c5c9
AS
4710 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
4711 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4712 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4713 return 1;
d2e4a39e 4714
14f9c5c9
AS
4715 return 0;
4716}
4717
4718/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4719 no more defined than that of SYM1. */
14f9c5c9
AS
4720
4721static int
d2e4a39e 4722lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4723{
4724 if (sym0 == sym1)
4725 return 1;
176620f1 4726 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
4727 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4728 return 0;
4729
d2e4a39e 4730 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
4731 {
4732 case LOC_UNDEF:
4733 return 1;
4734 case LOC_TYPEDEF:
4735 {
4c4b4cd2
PH
4736 struct type *type0 = SYMBOL_TYPE (sym0);
4737 struct type *type1 = SYMBOL_TYPE (sym1);
0d5cff50
DE
4738 const char *name0 = SYMBOL_LINKAGE_NAME (sym0);
4739 const char *name1 = SYMBOL_LINKAGE_NAME (sym1);
4c4b4cd2 4740 int len0 = strlen (name0);
5b4ee69b 4741
4c4b4cd2
PH
4742 return
4743 TYPE_CODE (type0) == TYPE_CODE (type1)
4744 && (equiv_types (type0, type1)
4745 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
61012eef 4746 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4747 }
4748 case LOC_CONST:
4749 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4c4b4cd2 4750 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
d2e4a39e
AS
4751 default:
4752 return 0;
14f9c5c9
AS
4753 }
4754}
4755
d12307c1 4756/* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct block_symbol
4c4b4cd2 4757 records in OBSTACKP. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4758
4759static void
76a01679
JB
4760add_defn_to_vec (struct obstack *obstackp,
4761 struct symbol *sym,
f0c5f9b2 4762 const struct block *block)
14f9c5c9
AS
4763{
4764 int i;
d12307c1 4765 struct block_symbol *prevDefns = defns_collected (obstackp, 0);
14f9c5c9 4766
529cad9c
PH
4767 /* Do not try to complete stub types, as the debugger is probably
4768 already scanning all symbols matching a certain name at the
4769 time when this function is called. Trying to replace the stub
4770 type by its associated full type will cause us to restart a scan
4771 which may lead to an infinite recursion. Instead, the client
4772 collecting the matching symbols will end up collecting several
4773 matches, with at least one of them complete. It can then filter
4774 out the stub ones if needed. */
4775
4c4b4cd2
PH
4776 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
4777 {
d12307c1 4778 if (lesseq_defined_than (sym, prevDefns[i].symbol))
4c4b4cd2 4779 return;
d12307c1 4780 else if (lesseq_defined_than (prevDefns[i].symbol, sym))
4c4b4cd2 4781 {
d12307c1 4782 prevDefns[i].symbol = sym;
4c4b4cd2 4783 prevDefns[i].block = block;
4c4b4cd2 4784 return;
76a01679 4785 }
4c4b4cd2
PH
4786 }
4787
4788 {
d12307c1 4789 struct block_symbol info;
4c4b4cd2 4790
d12307c1 4791 info.symbol = sym;
4c4b4cd2 4792 info.block = block;
d12307c1 4793 obstack_grow (obstackp, &info, sizeof (struct block_symbol));
4c4b4cd2
PH
4794 }
4795}
4796
d12307c1
PMR
4797/* Number of block_symbol structures currently collected in current vector in
4798 OBSTACKP. */
4c4b4cd2 4799
76a01679
JB
4800static int
4801num_defns_collected (struct obstack *obstackp)
4c4b4cd2 4802{
d12307c1 4803 return obstack_object_size (obstackp) / sizeof (struct block_symbol);
4c4b4cd2
PH
4804}
4805
d12307c1
PMR
4806/* Vector of block_symbol structures currently collected in current vector in
4807 OBSTACKP. If FINISH, close off the vector and return its final address. */
4c4b4cd2 4808
d12307c1 4809static struct block_symbol *
4c4b4cd2
PH
4810defns_collected (struct obstack *obstackp, int finish)
4811{
4812 if (finish)
224c3ddb 4813 return (struct block_symbol *) obstack_finish (obstackp);
4c4b4cd2 4814 else
d12307c1 4815 return (struct block_symbol *) obstack_base (obstackp);
4c4b4cd2
PH
4816}
4817
7c7b6655
TT
4818/* Return a bound minimal symbol matching NAME according to Ada
4819 decoding rules. Returns an invalid symbol if there is no such
4820 minimal symbol. Names prefixed with "standard__" are handled
4821 specially: "standard__" is first stripped off, and only static and
4822 global symbols are searched. */
4c4b4cd2 4823
7c7b6655 4824struct bound_minimal_symbol
96d887e8 4825ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4826{
7c7b6655 4827 struct bound_minimal_symbol result;
4c4b4cd2 4828 struct objfile *objfile;
96d887e8 4829 struct minimal_symbol *msymbol;
dc4024cd 4830 const int wild_match_p = should_use_wild_match (name);
4c4b4cd2 4831
7c7b6655
TT
4832 memset (&result, 0, sizeof (result));
4833
c0431670
JB
4834 /* Special case: If the user specifies a symbol name inside package
4835 Standard, do a non-wild matching of the symbol name without
4836 the "standard__" prefix. This was primarily introduced in order
4837 to allow the user to specifically access the standard exceptions
4838 using, for instance, Standard.Constraint_Error when Constraint_Error
4839 is ambiguous (due to the user defining its own Constraint_Error
4840 entity inside its program). */
61012eef 4841 if (startswith (name, "standard__"))
c0431670 4842 name += sizeof ("standard__") - 1;
4c4b4cd2 4843
96d887e8
PH
4844 ALL_MSYMBOLS (objfile, msymbol)
4845 {
efd66ac6 4846 if (match_name (MSYMBOL_LINKAGE_NAME (msymbol), name, wild_match_p)
96d887e8 4847 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
7c7b6655
TT
4848 {
4849 result.minsym = msymbol;
4850 result.objfile = objfile;
4851 break;
4852 }
96d887e8 4853 }
4c4b4cd2 4854
7c7b6655 4855 return result;
96d887e8 4856}
4c4b4cd2 4857
96d887e8
PH
4858/* For all subprograms that statically enclose the subprogram of the
4859 selected frame, add symbols matching identifier NAME in DOMAIN
4860 and their blocks to the list of data in OBSTACKP, as for
48b78332
JB
4861 ada_add_block_symbols (q.v.). If WILD_MATCH_P, treat as NAME
4862 with a wildcard prefix. */
4c4b4cd2 4863
96d887e8
PH
4864static void
4865add_symbols_from_enclosing_procs (struct obstack *obstackp,
fe978cb0 4866 const char *name, domain_enum domain,
48b78332 4867 int wild_match_p)
96d887e8 4868{
96d887e8 4869}
14f9c5c9 4870
96d887e8
PH
4871/* True if TYPE is definitely an artificial type supplied to a symbol
4872 for which no debugging information was given in the symbol file. */
14f9c5c9 4873
96d887e8
PH
4874static int
4875is_nondebugging_type (struct type *type)
4876{
0d5cff50 4877 const char *name = ada_type_name (type);
5b4ee69b 4878
96d887e8
PH
4879 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4880}
4c4b4cd2 4881
8f17729f
JB
4882/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4883 that are deemed "identical" for practical purposes.
4884
4885 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4886 types and that their number of enumerals is identical (in other
4887 words, TYPE_NFIELDS (type1) == TYPE_NFIELDS (type2)). */
4888
4889static int
4890ada_identical_enum_types_p (struct type *type1, struct type *type2)
4891{
4892 int i;
4893
4894 /* The heuristic we use here is fairly conservative. We consider
4895 that 2 enumerate types are identical if they have the same
4896 number of enumerals and that all enumerals have the same
4897 underlying value and name. */
4898
4899 /* All enums in the type should have an identical underlying value. */
4900 for (i = 0; i < TYPE_NFIELDS (type1); i++)
14e75d8e 4901 if (TYPE_FIELD_ENUMVAL (type1, i) != TYPE_FIELD_ENUMVAL (type2, i))
8f17729f
JB
4902 return 0;
4903
4904 /* All enumerals should also have the same name (modulo any numerical
4905 suffix). */
4906 for (i = 0; i < TYPE_NFIELDS (type1); i++)
4907 {
0d5cff50
DE
4908 const char *name_1 = TYPE_FIELD_NAME (type1, i);
4909 const char *name_2 = TYPE_FIELD_NAME (type2, i);
8f17729f
JB
4910 int len_1 = strlen (name_1);
4911 int len_2 = strlen (name_2);
4912
4913 ada_remove_trailing_digits (TYPE_FIELD_NAME (type1, i), &len_1);
4914 ada_remove_trailing_digits (TYPE_FIELD_NAME (type2, i), &len_2);
4915 if (len_1 != len_2
4916 || strncmp (TYPE_FIELD_NAME (type1, i),
4917 TYPE_FIELD_NAME (type2, i),
4918 len_1) != 0)
4919 return 0;
4920 }
4921
4922 return 1;
4923}
4924
4925/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4926 that are deemed "identical" for practical purposes. Sometimes,
4927 enumerals are not strictly identical, but their types are so similar
4928 that they can be considered identical.
4929
4930 For instance, consider the following code:
4931
4932 type Color is (Black, Red, Green, Blue, White);
4933 type RGB_Color is new Color range Red .. Blue;
4934
4935 Type RGB_Color is a subrange of an implicit type which is a copy
4936 of type Color. If we call that implicit type RGB_ColorB ("B" is
4937 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4938 As a result, when an expression references any of the enumeral
4939 by name (Eg. "print green"), the expression is technically
4940 ambiguous and the user should be asked to disambiguate. But
4941 doing so would only hinder the user, since it wouldn't matter
4942 what choice he makes, the outcome would always be the same.
4943 So, for practical purposes, we consider them as the same. */
4944
4945static int
d12307c1 4946symbols_are_identical_enums (struct block_symbol *syms, int nsyms)
8f17729f
JB
4947{
4948 int i;
4949
4950 /* Before performing a thorough comparison check of each type,
4951 we perform a series of inexpensive checks. We expect that these
4952 checks will quickly fail in the vast majority of cases, and thus
4953 help prevent the unnecessary use of a more expensive comparison.
4954 Said comparison also expects us to make some of these checks
4955 (see ada_identical_enum_types_p). */
4956
4957 /* Quick check: All symbols should have an enum type. */
4958 for (i = 0; i < nsyms; i++)
d12307c1 4959 if (TYPE_CODE (SYMBOL_TYPE (syms[i].symbol)) != TYPE_CODE_ENUM)
8f17729f
JB
4960 return 0;
4961
4962 /* Quick check: They should all have the same value. */
4963 for (i = 1; i < nsyms; i++)
d12307c1 4964 if (SYMBOL_VALUE (syms[i].symbol) != SYMBOL_VALUE (syms[0].symbol))
8f17729f
JB
4965 return 0;
4966
4967 /* Quick check: They should all have the same number of enumerals. */
4968 for (i = 1; i < nsyms; i++)
d12307c1
PMR
4969 if (TYPE_NFIELDS (SYMBOL_TYPE (syms[i].symbol))
4970 != TYPE_NFIELDS (SYMBOL_TYPE (syms[0].symbol)))
8f17729f
JB
4971 return 0;
4972
4973 /* All the sanity checks passed, so we might have a set of
4974 identical enumeration types. Perform a more complete
4975 comparison of the type of each symbol. */
4976 for (i = 1; i < nsyms; i++)
d12307c1
PMR
4977 if (!ada_identical_enum_types_p (SYMBOL_TYPE (syms[i].symbol),
4978 SYMBOL_TYPE (syms[0].symbol)))
8f17729f
JB
4979 return 0;
4980
4981 return 1;
4982}
4983
96d887e8
PH
4984/* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4985 duplicate other symbols in the list (The only case I know of where
4986 this happens is when object files containing stabs-in-ecoff are
4987 linked with files containing ordinary ecoff debugging symbols (or no
4988 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4989 Returns the number of items in the modified list. */
4c4b4cd2 4990
96d887e8 4991static int
d12307c1 4992remove_extra_symbols (struct block_symbol *syms, int nsyms)
96d887e8
PH
4993{
4994 int i, j;
4c4b4cd2 4995
8f17729f
JB
4996 /* We should never be called with less than 2 symbols, as there
4997 cannot be any extra symbol in that case. But it's easy to
4998 handle, since we have nothing to do in that case. */
4999 if (nsyms < 2)
5000 return nsyms;
5001
96d887e8
PH
5002 i = 0;
5003 while (i < nsyms)
5004 {
a35ddb44 5005 int remove_p = 0;
339c13b6
JB
5006
5007 /* If two symbols have the same name and one of them is a stub type,
5008 the get rid of the stub. */
5009
d12307c1
PMR
5010 if (TYPE_STUB (SYMBOL_TYPE (syms[i].symbol))
5011 && SYMBOL_LINKAGE_NAME (syms[i].symbol) != NULL)
339c13b6
JB
5012 {
5013 for (j = 0; j < nsyms; j++)
5014 {
5015 if (j != i
d12307c1
PMR
5016 && !TYPE_STUB (SYMBOL_TYPE (syms[j].symbol))
5017 && SYMBOL_LINKAGE_NAME (syms[j].symbol) != NULL
5018 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].symbol),
5019 SYMBOL_LINKAGE_NAME (syms[j].symbol)) == 0)
a35ddb44 5020 remove_p = 1;
339c13b6
JB
5021 }
5022 }
5023
5024 /* Two symbols with the same name, same class and same address
5025 should be identical. */
5026
d12307c1
PMR
5027 else if (SYMBOL_LINKAGE_NAME (syms[i].symbol) != NULL
5028 && SYMBOL_CLASS (syms[i].symbol) == LOC_STATIC
5029 && is_nondebugging_type (SYMBOL_TYPE (syms[i].symbol)))
96d887e8
PH
5030 {
5031 for (j = 0; j < nsyms; j += 1)
5032 {
5033 if (i != j
d12307c1
PMR
5034 && SYMBOL_LINKAGE_NAME (syms[j].symbol) != NULL
5035 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].symbol),
5036 SYMBOL_LINKAGE_NAME (syms[j].symbol)) == 0
5037 && SYMBOL_CLASS (syms[i].symbol)
5038 == SYMBOL_CLASS (syms[j].symbol)
5039 && SYMBOL_VALUE_ADDRESS (syms[i].symbol)
5040 == SYMBOL_VALUE_ADDRESS (syms[j].symbol))
a35ddb44 5041 remove_p = 1;
4c4b4cd2 5042 }
4c4b4cd2 5043 }
339c13b6 5044
a35ddb44 5045 if (remove_p)
339c13b6
JB
5046 {
5047 for (j = i + 1; j < nsyms; j += 1)
5048 syms[j - 1] = syms[j];
5049 nsyms -= 1;
5050 }
5051
96d887e8 5052 i += 1;
14f9c5c9 5053 }
8f17729f
JB
5054
5055 /* If all the remaining symbols are identical enumerals, then
5056 just keep the first one and discard the rest.
5057
5058 Unlike what we did previously, we do not discard any entry
5059 unless they are ALL identical. This is because the symbol
5060 comparison is not a strict comparison, but rather a practical
5061 comparison. If all symbols are considered identical, then
5062 we can just go ahead and use the first one and discard the rest.
5063 But if we cannot reduce the list to a single element, we have
5064 to ask the user to disambiguate anyways. And if we have to
5065 present a multiple-choice menu, it's less confusing if the list
5066 isn't missing some choices that were identical and yet distinct. */
5067 if (symbols_are_identical_enums (syms, nsyms))
5068 nsyms = 1;
5069
96d887e8 5070 return nsyms;
14f9c5c9
AS
5071}
5072
96d887e8
PH
5073/* Given a type that corresponds to a renaming entity, use the type name
5074 to extract the scope (package name or function name, fully qualified,
5075 and following the GNAT encoding convention) where this renaming has been
5076 defined. The string returned needs to be deallocated after use. */
4c4b4cd2 5077
96d887e8
PH
5078static char *
5079xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5080{
96d887e8 5081 /* The renaming types adhere to the following convention:
0963b4bd 5082 <scope>__<rename>___<XR extension>.
96d887e8
PH
5083 So, to extract the scope, we search for the "___XR" extension,
5084 and then backtrack until we find the first "__". */
76a01679 5085
96d887e8 5086 const char *name = type_name_no_tag (renaming_type);
108d56a4
SM
5087 const char *suffix = strstr (name, "___XR");
5088 const char *last;
96d887e8
PH
5089 int scope_len;
5090 char *scope;
14f9c5c9 5091
96d887e8
PH
5092 /* Now, backtrack a bit until we find the first "__". Start looking
5093 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5094
96d887e8
PH
5095 for (last = suffix - 3; last > name; last--)
5096 if (last[0] == '_' && last[1] == '_')
5097 break;
76a01679 5098
96d887e8 5099 /* Make a copy of scope and return it. */
14f9c5c9 5100
96d887e8
PH
5101 scope_len = last - name;
5102 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
14f9c5c9 5103
96d887e8
PH
5104 strncpy (scope, name, scope_len);
5105 scope[scope_len] = '\0';
4c4b4cd2 5106
96d887e8 5107 return scope;
4c4b4cd2
PH
5108}
5109
96d887e8 5110/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5111
96d887e8
PH
5112static int
5113is_package_name (const char *name)
4c4b4cd2 5114{
96d887e8
PH
5115 /* Here, We take advantage of the fact that no symbols are generated
5116 for packages, while symbols are generated for each function.
5117 So the condition for NAME represent a package becomes equivalent
5118 to NAME not existing in our list of symbols. There is only one
5119 small complication with library-level functions (see below). */
4c4b4cd2 5120
96d887e8 5121 char *fun_name;
76a01679 5122
96d887e8
PH
5123 /* If it is a function that has not been defined at library level,
5124 then we should be able to look it up in the symbols. */
5125 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5126 return 0;
14f9c5c9 5127
96d887e8
PH
5128 /* Library-level function names start with "_ada_". See if function
5129 "_ada_" followed by NAME can be found. */
14f9c5c9 5130
96d887e8 5131 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5132 functions names cannot contain "__" in them. */
96d887e8
PH
5133 if (strstr (name, "__") != NULL)
5134 return 0;
4c4b4cd2 5135
b435e160 5136 fun_name = xstrprintf ("_ada_%s", name);
14f9c5c9 5137
96d887e8
PH
5138 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
5139}
14f9c5c9 5140
96d887e8 5141/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5142 not visible from FUNCTION_NAME. */
14f9c5c9 5143
96d887e8 5144static int
0d5cff50 5145old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5146{
aeb5907d 5147 char *scope;
1509e573 5148 struct cleanup *old_chain;
aeb5907d
JB
5149
5150 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
5151 return 0;
5152
5153 scope = xget_renaming_scope (SYMBOL_TYPE (sym));
1509e573 5154 old_chain = make_cleanup (xfree, scope);
14f9c5c9 5155
96d887e8
PH
5156 /* If the rename has been defined in a package, then it is visible. */
5157 if (is_package_name (scope))
1509e573
JB
5158 {
5159 do_cleanups (old_chain);
5160 return 0;
5161 }
14f9c5c9 5162
96d887e8
PH
5163 /* Check that the rename is in the current function scope by checking
5164 that its name starts with SCOPE. */
76a01679 5165
96d887e8
PH
5166 /* If the function name starts with "_ada_", it means that it is
5167 a library-level function. Strip this prefix before doing the
5168 comparison, as the encoding for the renaming does not contain
5169 this prefix. */
61012eef 5170 if (startswith (function_name, "_ada_"))
96d887e8 5171 function_name += 5;
f26caa11 5172
1509e573 5173 {
61012eef 5174 int is_invisible = !startswith (function_name, scope);
1509e573
JB
5175
5176 do_cleanups (old_chain);
5177 return is_invisible;
5178 }
f26caa11
PH
5179}
5180
aeb5907d
JB
5181/* Remove entries from SYMS that corresponds to a renaming entity that
5182 is not visible from the function associated with CURRENT_BLOCK or
5183 that is superfluous due to the presence of more specific renaming
5184 information. Places surviving symbols in the initial entries of
5185 SYMS and returns the number of surviving symbols.
96d887e8
PH
5186
5187 Rationale:
aeb5907d
JB
5188 First, in cases where an object renaming is implemented as a
5189 reference variable, GNAT may produce both the actual reference
5190 variable and the renaming encoding. In this case, we discard the
5191 latter.
5192
5193 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5194 entity. Unfortunately, STABS currently does not support the definition
5195 of types that are local to a given lexical block, so all renamings types
5196 are emitted at library level. As a consequence, if an application
5197 contains two renaming entities using the same name, and a user tries to
5198 print the value of one of these entities, the result of the ada symbol
5199 lookup will also contain the wrong renaming type.
f26caa11 5200
96d887e8
PH
5201 This function partially covers for this limitation by attempting to
5202 remove from the SYMS list renaming symbols that should be visible
5203 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5204 method with the current information available. The implementation
5205 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5206
5207 - When the user tries to print a rename in a function while there
5208 is another rename entity defined in a package: Normally, the
5209 rename in the function has precedence over the rename in the
5210 package, so the latter should be removed from the list. This is
5211 currently not the case.
5212
5213 - This function will incorrectly remove valid renames if
5214 the CURRENT_BLOCK corresponds to a function which symbol name
5215 has been changed by an "Export" pragma. As a consequence,
5216 the user will be unable to print such rename entities. */
4c4b4cd2 5217
14f9c5c9 5218static int
d12307c1 5219remove_irrelevant_renamings (struct block_symbol *syms,
aeb5907d 5220 int nsyms, const struct block *current_block)
4c4b4cd2
PH
5221{
5222 struct symbol *current_function;
0d5cff50 5223 const char *current_function_name;
4c4b4cd2 5224 int i;
aeb5907d
JB
5225 int is_new_style_renaming;
5226
5227 /* If there is both a renaming foo___XR... encoded as a variable and
5228 a simple variable foo in the same block, discard the latter.
0963b4bd 5229 First, zero out such symbols, then compress. */
aeb5907d
JB
5230 is_new_style_renaming = 0;
5231 for (i = 0; i < nsyms; i += 1)
5232 {
d12307c1 5233 struct symbol *sym = syms[i].symbol;
270140bd 5234 const struct block *block = syms[i].block;
aeb5907d
JB
5235 const char *name;
5236 const char *suffix;
5237
5238 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
5239 continue;
5240 name = SYMBOL_LINKAGE_NAME (sym);
5241 suffix = strstr (name, "___XR");
5242
5243 if (suffix != NULL)
5244 {
5245 int name_len = suffix - name;
5246 int j;
5b4ee69b 5247
aeb5907d
JB
5248 is_new_style_renaming = 1;
5249 for (j = 0; j < nsyms; j += 1)
d12307c1
PMR
5250 if (i != j && syms[j].symbol != NULL
5251 && strncmp (name, SYMBOL_LINKAGE_NAME (syms[j].symbol),
aeb5907d
JB
5252 name_len) == 0
5253 && block == syms[j].block)
d12307c1 5254 syms[j].symbol = NULL;
aeb5907d
JB
5255 }
5256 }
5257 if (is_new_style_renaming)
5258 {
5259 int j, k;
5260
5261 for (j = k = 0; j < nsyms; j += 1)
d12307c1 5262 if (syms[j].symbol != NULL)
aeb5907d
JB
5263 {
5264 syms[k] = syms[j];
5265 k += 1;
5266 }
5267 return k;
5268 }
4c4b4cd2
PH
5269
5270 /* Extract the function name associated to CURRENT_BLOCK.
5271 Abort if unable to do so. */
76a01679 5272
4c4b4cd2
PH
5273 if (current_block == NULL)
5274 return nsyms;
76a01679 5275
7f0df278 5276 current_function = block_linkage_function (current_block);
4c4b4cd2
PH
5277 if (current_function == NULL)
5278 return nsyms;
5279
5280 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
5281 if (current_function_name == NULL)
5282 return nsyms;
5283
5284 /* Check each of the symbols, and remove it from the list if it is
5285 a type corresponding to a renaming that is out of the scope of
5286 the current block. */
5287
5288 i = 0;
5289 while (i < nsyms)
5290 {
d12307c1 5291 if (ada_parse_renaming (syms[i].symbol, NULL, NULL, NULL)
aeb5907d 5292 == ADA_OBJECT_RENAMING
d12307c1 5293 && old_renaming_is_invisible (syms[i].symbol, current_function_name))
4c4b4cd2
PH
5294 {
5295 int j;
5b4ee69b 5296
aeb5907d 5297 for (j = i + 1; j < nsyms; j += 1)
76a01679 5298 syms[j - 1] = syms[j];
4c4b4cd2
PH
5299 nsyms -= 1;
5300 }
5301 else
5302 i += 1;
5303 }
5304
5305 return nsyms;
5306}
5307
339c13b6
JB
5308/* Add to OBSTACKP all symbols from BLOCK (and its super-blocks)
5309 whose name and domain match NAME and DOMAIN respectively.
5310 If no match was found, then extend the search to "enclosing"
5311 routines (in other words, if we're inside a nested function,
5312 search the symbols defined inside the enclosing functions).
d0a8ab18
JB
5313 If WILD_MATCH_P is nonzero, perform the naming matching in
5314 "wild" mode (see function "wild_match" for more info).
339c13b6
JB
5315
5316 Note: This function assumes that OBSTACKP has 0 (zero) element in it. */
5317
5318static void
5319ada_add_local_symbols (struct obstack *obstackp, const char *name,
f0c5f9b2 5320 const struct block *block, domain_enum domain,
d0a8ab18 5321 int wild_match_p)
339c13b6
JB
5322{
5323 int block_depth = 0;
5324
5325 while (block != NULL)
5326 {
5327 block_depth += 1;
d0a8ab18
JB
5328 ada_add_block_symbols (obstackp, block, name, domain, NULL,
5329 wild_match_p);
339c13b6
JB
5330
5331 /* If we found a non-function match, assume that's the one. */
5332 if (is_nonfunction (defns_collected (obstackp, 0),
5333 num_defns_collected (obstackp)))
5334 return;
5335
5336 block = BLOCK_SUPERBLOCK (block);
5337 }
5338
5339 /* If no luck so far, try to find NAME as a local symbol in some lexically
5340 enclosing subprogram. */
5341 if (num_defns_collected (obstackp) == 0 && block_depth > 2)
d0a8ab18 5342 add_symbols_from_enclosing_procs (obstackp, name, domain, wild_match_p);
339c13b6
JB
5343}
5344
ccefe4c4 5345/* An object of this type is used as the user_data argument when
40658b94 5346 calling the map_matching_symbols method. */
ccefe4c4 5347
40658b94 5348struct match_data
ccefe4c4 5349{
40658b94 5350 struct objfile *objfile;
ccefe4c4 5351 struct obstack *obstackp;
40658b94
PH
5352 struct symbol *arg_sym;
5353 int found_sym;
ccefe4c4
TT
5354};
5355
22cee43f 5356/* A callback for add_nonlocal_symbols that adds SYM, found in BLOCK,
40658b94
PH
5357 to a list of symbols. DATA0 is a pointer to a struct match_data *
5358 containing the obstack that collects the symbol list, the file that SYM
5359 must come from, a flag indicating whether a non-argument symbol has
5360 been found in the current block, and the last argument symbol
5361 passed in SYM within the current block (if any). When SYM is null,
5362 marking the end of a block, the argument symbol is added if no
5363 other has been found. */
ccefe4c4 5364
40658b94
PH
5365static int
5366aux_add_nonlocal_symbols (struct block *block, struct symbol *sym, void *data0)
ccefe4c4 5367{
40658b94
PH
5368 struct match_data *data = (struct match_data *) data0;
5369
5370 if (sym == NULL)
5371 {
5372 if (!data->found_sym && data->arg_sym != NULL)
5373 add_defn_to_vec (data->obstackp,
5374 fixup_symbol_section (data->arg_sym, data->objfile),
5375 block);
5376 data->found_sym = 0;
5377 data->arg_sym = NULL;
5378 }
5379 else
5380 {
5381 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
5382 return 0;
5383 else if (SYMBOL_IS_ARGUMENT (sym))
5384 data->arg_sym = sym;
5385 else
5386 {
5387 data->found_sym = 1;
5388 add_defn_to_vec (data->obstackp,
5389 fixup_symbol_section (sym, data->objfile),
5390 block);
5391 }
5392 }
5393 return 0;
5394}
5395
22cee43f
PMR
5396/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are targetted
5397 by renamings matching NAME in BLOCK. Add these symbols to OBSTACKP. If
5398 WILD_MATCH_P is nonzero, perform the naming matching in "wild" mode (see
5399 function "wild_match" for more information). Return whether we found such
5400 symbols. */
5401
5402static int
5403ada_add_block_renamings (struct obstack *obstackp,
5404 const struct block *block,
5405 const char *name,
5406 domain_enum domain,
5407 int wild_match_p)
5408{
5409 struct using_direct *renaming;
5410 int defns_mark = num_defns_collected (obstackp);
5411
5412 for (renaming = block_using (block);
5413 renaming != NULL;
5414 renaming = renaming->next)
5415 {
5416 const char *r_name;
5417 int name_match;
5418
5419 /* Avoid infinite recursions: skip this renaming if we are actually
5420 already traversing it.
5421
5422 Currently, symbol lookup in Ada don't use the namespace machinery from
5423 C++/Fortran support: skip namespace imports that use them. */
5424 if (renaming->searched
5425 || (renaming->import_src != NULL
5426 && renaming->import_src[0] != '\0')
5427 || (renaming->import_dest != NULL
5428 && renaming->import_dest[0] != '\0'))
5429 continue;
5430 renaming->searched = 1;
5431
5432 /* TODO: here, we perform another name-based symbol lookup, which can
5433 pull its own multiple overloads. In theory, we should be able to do
5434 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5435 not a simple name. But in order to do this, we would need to enhance
5436 the DWARF reader to associate a symbol to this renaming, instead of a
5437 name. So, for now, we do something simpler: re-use the C++/Fortran
5438 namespace machinery. */
5439 r_name = (renaming->alias != NULL
5440 ? renaming->alias
5441 : renaming->declaration);
5442 name_match
5443 = wild_match_p ? wild_match (r_name, name) : strcmp (r_name, name);
5444 if (name_match == 0)
5445 ada_add_all_symbols (obstackp, block, renaming->declaration, domain,
5446 1, NULL);
5447 renaming->searched = 0;
5448 }
5449 return num_defns_collected (obstackp) != defns_mark;
5450}
5451
db230ce3
JB
5452/* Implements compare_names, but only applying the comparision using
5453 the given CASING. */
5b4ee69b 5454
40658b94 5455static int
db230ce3
JB
5456compare_names_with_case (const char *string1, const char *string2,
5457 enum case_sensitivity casing)
40658b94
PH
5458{
5459 while (*string1 != '\0' && *string2 != '\0')
5460 {
db230ce3
JB
5461 char c1, c2;
5462
40658b94
PH
5463 if (isspace (*string1) || isspace (*string2))
5464 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5465
5466 if (casing == case_sensitive_off)
5467 {
5468 c1 = tolower (*string1);
5469 c2 = tolower (*string2);
5470 }
5471 else
5472 {
5473 c1 = *string1;
5474 c2 = *string2;
5475 }
5476 if (c1 != c2)
40658b94 5477 break;
db230ce3 5478
40658b94
PH
5479 string1 += 1;
5480 string2 += 1;
5481 }
db230ce3 5482
40658b94
PH
5483 switch (*string1)
5484 {
5485 case '(':
5486 return strcmp_iw_ordered (string1, string2);
5487 case '_':
5488 if (*string2 == '\0')
5489 {
052874e8 5490 if (is_name_suffix (string1))
40658b94
PH
5491 return 0;
5492 else
1a1d5513 5493 return 1;
40658b94 5494 }
dbb8534f 5495 /* FALLTHROUGH */
40658b94
PH
5496 default:
5497 if (*string2 == '(')
5498 return strcmp_iw_ordered (string1, string2);
5499 else
db230ce3
JB
5500 {
5501 if (casing == case_sensitive_off)
5502 return tolower (*string1) - tolower (*string2);
5503 else
5504 return *string1 - *string2;
5505 }
40658b94 5506 }
ccefe4c4
TT
5507}
5508
db230ce3
JB
5509/* Compare STRING1 to STRING2, with results as for strcmp.
5510 Compatible with strcmp_iw_ordered in that...
5511
5512 strcmp_iw_ordered (STRING1, STRING2) <= 0
5513
5514 ... implies...
5515
5516 compare_names (STRING1, STRING2) <= 0
5517
5518 (they may differ as to what symbols compare equal). */
5519
5520static int
5521compare_names (const char *string1, const char *string2)
5522{
5523 int result;
5524
5525 /* Similar to what strcmp_iw_ordered does, we need to perform
5526 a case-insensitive comparison first, and only resort to
5527 a second, case-sensitive, comparison if the first one was
5528 not sufficient to differentiate the two strings. */
5529
5530 result = compare_names_with_case (string1, string2, case_sensitive_off);
5531 if (result == 0)
5532 result = compare_names_with_case (string1, string2, case_sensitive_on);
5533
5534 return result;
5535}
5536
339c13b6
JB
5537/* Add to OBSTACKP all non-local symbols whose name and domain match
5538 NAME and DOMAIN respectively. The search is performed on GLOBAL_BLOCK
5539 symbols if GLOBAL is non-zero, or on STATIC_BLOCK symbols otherwise. */
5540
5541static void
40658b94
PH
5542add_nonlocal_symbols (struct obstack *obstackp, const char *name,
5543 domain_enum domain, int global,
5544 int is_wild_match)
339c13b6
JB
5545{
5546 struct objfile *objfile;
22cee43f 5547 struct compunit_symtab *cu;
40658b94 5548 struct match_data data;
339c13b6 5549
6475f2fe 5550 memset (&data, 0, sizeof data);
ccefe4c4 5551 data.obstackp = obstackp;
339c13b6 5552
ccefe4c4 5553 ALL_OBJFILES (objfile)
40658b94
PH
5554 {
5555 data.objfile = objfile;
5556
5557 if (is_wild_match)
4186eb54
KS
5558 objfile->sf->qf->map_matching_symbols (objfile, name, domain, global,
5559 aux_add_nonlocal_symbols, &data,
5560 wild_match, NULL);
40658b94 5561 else
4186eb54
KS
5562 objfile->sf->qf->map_matching_symbols (objfile, name, domain, global,
5563 aux_add_nonlocal_symbols, &data,
5564 full_match, compare_names);
22cee43f
PMR
5565
5566 ALL_OBJFILE_COMPUNITS (objfile, cu)
5567 {
5568 const struct block *global_block
5569 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cu), GLOBAL_BLOCK);
5570
5571 if (ada_add_block_renamings (obstackp, global_block , name, domain,
5572 is_wild_match))
5573 data.found_sym = 1;
5574 }
40658b94
PH
5575 }
5576
5577 if (num_defns_collected (obstackp) == 0 && global && !is_wild_match)
5578 {
5579 ALL_OBJFILES (objfile)
5580 {
224c3ddb 5581 char *name1 = (char *) alloca (strlen (name) + sizeof ("_ada_"));
40658b94
PH
5582 strcpy (name1, "_ada_");
5583 strcpy (name1 + sizeof ("_ada_") - 1, name);
5584 data.objfile = objfile;
ade7ed9e
DE
5585 objfile->sf->qf->map_matching_symbols (objfile, name1, domain,
5586 global,
0963b4bd
MS
5587 aux_add_nonlocal_symbols,
5588 &data,
40658b94
PH
5589 full_match, compare_names);
5590 }
5591 }
339c13b6
JB
5592}
5593
22cee43f 5594/* Find symbols in DOMAIN matching NAME, in BLOCK and, if FULL_SEARCH is
4eeaa230 5595 non-zero, enclosing scope and in global scopes, returning the number of
22cee43f 5596 matches. Add these to OBSTACKP.
4eeaa230 5597
22cee43f
PMR
5598 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5599 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5600 is the one match returned (no other matches in that or
d9680e73 5601 enclosing blocks is returned). If there are any matches in or
22cee43f 5602 surrounding BLOCK, then these alone are returned.
4eeaa230 5603
9f88c959 5604 Names prefixed with "standard__" are handled specially: "standard__"
22cee43f 5605 is first stripped off, and only static and global symbols are searched.
14f9c5c9 5606
22cee43f
PMR
5607 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5608 to lookup global symbols. */
5609
5610static void
5611ada_add_all_symbols (struct obstack *obstackp,
5612 const struct block *block,
5613 const char *name,
5614 domain_enum domain,
5615 int full_search,
5616 int *made_global_lookup_p)
14f9c5c9
AS
5617{
5618 struct symbol *sym;
22cee43f 5619 const int wild_match_p = should_use_wild_match (name);
14f9c5c9 5620
22cee43f
PMR
5621 if (made_global_lookup_p)
5622 *made_global_lookup_p = 0;
339c13b6
JB
5623
5624 /* Special case: If the user specifies a symbol name inside package
5625 Standard, do a non-wild matching of the symbol name without
5626 the "standard__" prefix. This was primarily introduced in order
5627 to allow the user to specifically access the standard exceptions
5628 using, for instance, Standard.Constraint_Error when Constraint_Error
5629 is ambiguous (due to the user defining its own Constraint_Error
5630 entity inside its program). */
22cee43f 5631 if (startswith (name, "standard__"))
4c4b4cd2 5632 {
4c4b4cd2 5633 block = NULL;
22cee43f 5634 name = name + sizeof ("standard__") - 1;
4c4b4cd2
PH
5635 }
5636
339c13b6 5637 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5638
4eeaa230
DE
5639 if (block != NULL)
5640 {
5641 if (full_search)
22cee43f 5642 ada_add_local_symbols (obstackp, name, block, domain, wild_match_p);
4eeaa230
DE
5643 else
5644 {
5645 /* In the !full_search case we're are being called by
5646 ada_iterate_over_symbols, and we don't want to search
5647 superblocks. */
22cee43f
PMR
5648 ada_add_block_symbols (obstackp, block, name, domain, NULL,
5649 wild_match_p);
4eeaa230 5650 }
22cee43f
PMR
5651 if (num_defns_collected (obstackp) > 0 || !full_search)
5652 return;
4eeaa230 5653 }
d2e4a39e 5654
339c13b6
JB
5655 /* No non-global symbols found. Check our cache to see if we have
5656 already performed this search before. If we have, then return
5657 the same result. */
5658
22cee43f 5659 if (lookup_cached_symbol (name, domain, &sym, &block))
4c4b4cd2
PH
5660 {
5661 if (sym != NULL)
22cee43f
PMR
5662 add_defn_to_vec (obstackp, sym, block);
5663 return;
4c4b4cd2 5664 }
14f9c5c9 5665
22cee43f
PMR
5666 if (made_global_lookup_p)
5667 *made_global_lookup_p = 1;
b1eedac9 5668
339c13b6
JB
5669 /* Search symbols from all global blocks. */
5670
22cee43f 5671 add_nonlocal_symbols (obstackp, name, domain, 1, wild_match_p);
d2e4a39e 5672
4c4b4cd2 5673 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5674 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5675
22cee43f
PMR
5676 if (num_defns_collected (obstackp) == 0)
5677 add_nonlocal_symbols (obstackp, name, domain, 0, wild_match_p);
5678}
5679
5680/* Find symbols in DOMAIN matching NAME, in BLOCK and, if full_search is
5681 non-zero, enclosing scope and in global scopes, returning the number of
5682 matches.
5683 Sets *RESULTS to point to a vector of (SYM,BLOCK) tuples,
5684 indicating the symbols found and the blocks and symbol tables (if
5685 any) in which they were found. This vector is transient---good only to
5686 the next call of ada_lookup_symbol_list.
5687
5688 When full_search is non-zero, any non-function/non-enumeral
5689 symbol match within the nest of blocks whose innermost member is BLOCK,
5690 is the one match returned (no other matches in that or
5691 enclosing blocks is returned). If there are any matches in or
5692 surrounding BLOCK, then these alone are returned.
5693
5694 Names prefixed with "standard__" are handled specially: "standard__"
5695 is first stripped off, and only static and global symbols are searched. */
5696
5697static int
5698ada_lookup_symbol_list_worker (const char *name, const struct block *block,
5699 domain_enum domain,
5700 struct block_symbol **results,
5701 int full_search)
5702{
5703 const int wild_match_p = should_use_wild_match (name);
5704 int syms_from_global_search;
5705 int ndefns;
5706
5707 obstack_free (&symbol_list_obstack, NULL);
5708 obstack_init (&symbol_list_obstack);
5709 ada_add_all_symbols (&symbol_list_obstack, block, name, domain,
5710 full_search, &syms_from_global_search);
14f9c5c9 5711
4c4b4cd2
PH
5712 ndefns = num_defns_collected (&symbol_list_obstack);
5713 *results = defns_collected (&symbol_list_obstack, 1);
5714
5715 ndefns = remove_extra_symbols (*results, ndefns);
5716
b1eedac9 5717 if (ndefns == 0 && full_search && syms_from_global_search)
22cee43f 5718 cache_symbol (name, domain, NULL, NULL);
14f9c5c9 5719
b1eedac9 5720 if (ndefns == 1 && full_search && syms_from_global_search)
22cee43f 5721 cache_symbol (name, domain, (*results)[0].symbol, (*results)[0].block);
14f9c5c9 5722
22cee43f 5723 ndefns = remove_irrelevant_renamings (*results, ndefns, block);
14f9c5c9
AS
5724 return ndefns;
5725}
5726
4eeaa230
DE
5727/* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing scope and
5728 in global scopes, returning the number of matches, and setting *RESULTS
5729 to a vector of (SYM,BLOCK) tuples.
5730 See ada_lookup_symbol_list_worker for further details. */
5731
5732int
5733ada_lookup_symbol_list (const char *name0, const struct block *block0,
d12307c1 5734 domain_enum domain, struct block_symbol **results)
4eeaa230
DE
5735{
5736 return ada_lookup_symbol_list_worker (name0, block0, domain, results, 1);
5737}
5738
5739/* Implementation of the la_iterate_over_symbols method. */
5740
5741static void
5742ada_iterate_over_symbols (const struct block *block,
5743 const char *name, domain_enum domain,
5744 symbol_found_callback_ftype *callback,
5745 void *data)
5746{
5747 int ndefs, i;
d12307c1 5748 struct block_symbol *results;
4eeaa230
DE
5749
5750 ndefs = ada_lookup_symbol_list_worker (name, block, domain, &results, 0);
5751 for (i = 0; i < ndefs; ++i)
5752 {
d12307c1 5753 if (! (*callback) (results[i].symbol, data))
4eeaa230
DE
5754 break;
5755 }
5756}
5757
f8eba3c6
TT
5758/* If NAME is the name of an entity, return a string that should
5759 be used to look that entity up in Ada units. This string should
5760 be deallocated after use using xfree.
5761
5762 NAME can have any form that the "break" or "print" commands might
5763 recognize. In other words, it does not have to be the "natural"
5764 name, or the "encoded" name. */
5765
5766char *
5767ada_name_for_lookup (const char *name)
5768{
5769 char *canon;
5770 int nlen = strlen (name);
5771
5772 if (name[0] == '<' && name[nlen - 1] == '>')
5773 {
224c3ddb 5774 canon = (char *) xmalloc (nlen - 1);
f8eba3c6
TT
5775 memcpy (canon, name + 1, nlen - 2);
5776 canon[nlen - 2] = '\0';
5777 }
5778 else
5779 canon = xstrdup (ada_encode (ada_fold_name (name)));
5780 return canon;
5781}
5782
4e5c77fe
JB
5783/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5784 to 1, but choosing the first symbol found if there are multiple
5785 choices.
5786
5e2336be
JB
5787 The result is stored in *INFO, which must be non-NULL.
5788 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5789
5790void
5791ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5792 domain_enum domain,
d12307c1 5793 struct block_symbol *info)
14f9c5c9 5794{
d12307c1 5795 struct block_symbol *candidates;
14f9c5c9
AS
5796 int n_candidates;
5797
5e2336be 5798 gdb_assert (info != NULL);
d12307c1 5799 memset (info, 0, sizeof (struct block_symbol));
4e5c77fe 5800
fe978cb0 5801 n_candidates = ada_lookup_symbol_list (name, block, domain, &candidates);
14f9c5c9 5802 if (n_candidates == 0)
4e5c77fe 5803 return;
4c4b4cd2 5804
5e2336be 5805 *info = candidates[0];
d12307c1 5806 info->symbol = fixup_symbol_section (info->symbol, NULL);
4e5c77fe 5807}
aeb5907d
JB
5808
5809/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5810 scope and in global scopes, or NULL if none. NAME is folded and
5811 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
0963b4bd 5812 choosing the first symbol if there are multiple choices.
4e5c77fe
JB
5813 If IS_A_FIELD_OF_THIS is not NULL, it is set to zero. */
5814
d12307c1 5815struct block_symbol
aeb5907d 5816ada_lookup_symbol (const char *name, const struct block *block0,
fe978cb0 5817 domain_enum domain, int *is_a_field_of_this)
aeb5907d 5818{
d12307c1 5819 struct block_symbol info;
4e5c77fe 5820
aeb5907d
JB
5821 if (is_a_field_of_this != NULL)
5822 *is_a_field_of_this = 0;
5823
4e5c77fe 5824 ada_lookup_encoded_symbol (ada_encode (ada_fold_name (name)),
fe978cb0 5825 block0, domain, &info);
d12307c1 5826 return info;
4c4b4cd2 5827}
14f9c5c9 5828
d12307c1 5829static struct block_symbol
f606139a
DE
5830ada_lookup_symbol_nonlocal (const struct language_defn *langdef,
5831 const char *name,
76a01679 5832 const struct block *block,
21b556f4 5833 const domain_enum domain)
4c4b4cd2 5834{
d12307c1 5835 struct block_symbol sym;
04dccad0
JB
5836
5837 sym = ada_lookup_symbol (name, block_static_block (block), domain, NULL);
d12307c1 5838 if (sym.symbol != NULL)
04dccad0
JB
5839 return sym;
5840
5841 /* If we haven't found a match at this point, try the primitive
5842 types. In other languages, this search is performed before
5843 searching for global symbols in order to short-circuit that
5844 global-symbol search if it happens that the name corresponds
5845 to a primitive type. But we cannot do the same in Ada, because
5846 it is perfectly legitimate for a program to declare a type which
5847 has the same name as a standard type. If looking up a type in
5848 that situation, we have traditionally ignored the primitive type
5849 in favor of user-defined types. This is why, unlike most other
5850 languages, we search the primitive types this late and only after
5851 having searched the global symbols without success. */
5852
5853 if (domain == VAR_DOMAIN)
5854 {
5855 struct gdbarch *gdbarch;
5856
5857 if (block == NULL)
5858 gdbarch = target_gdbarch ();
5859 else
5860 gdbarch = block_gdbarch (block);
d12307c1
PMR
5861 sym.symbol = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
5862 if (sym.symbol != NULL)
04dccad0
JB
5863 return sym;
5864 }
5865
d12307c1 5866 return (struct block_symbol) {NULL, NULL};
14f9c5c9
AS
5867}
5868
5869
4c4b4cd2
PH
5870/* True iff STR is a possible encoded suffix of a normal Ada name
5871 that is to be ignored for matching purposes. Suffixes of parallel
5872 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5873 are given by any of the regular expressions:
4c4b4cd2 5874
babe1480
JB
5875 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5876 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5877 TKB [subprogram suffix for task bodies]
babe1480 5878 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5879 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5880
5881 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5882 match is performed. This sequence is used to differentiate homonyms,
5883 is an optional part of a valid name suffix. */
4c4b4cd2 5884
14f9c5c9 5885static int
d2e4a39e 5886is_name_suffix (const char *str)
14f9c5c9
AS
5887{
5888 int k;
4c4b4cd2
PH
5889 const char *matching;
5890 const int len = strlen (str);
5891
babe1480
JB
5892 /* Skip optional leading __[0-9]+. */
5893
4c4b4cd2
PH
5894 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5895 {
babe1480
JB
5896 str += 3;
5897 while (isdigit (str[0]))
5898 str += 1;
4c4b4cd2 5899 }
babe1480
JB
5900
5901 /* [.$][0-9]+ */
4c4b4cd2 5902
babe1480 5903 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5904 {
babe1480 5905 matching = str + 1;
4c4b4cd2
PH
5906 while (isdigit (matching[0]))
5907 matching += 1;
5908 if (matching[0] == '\0')
5909 return 1;
5910 }
5911
5912 /* ___[0-9]+ */
babe1480 5913
4c4b4cd2
PH
5914 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5915 {
5916 matching = str + 3;
5917 while (isdigit (matching[0]))
5918 matching += 1;
5919 if (matching[0] == '\0')
5920 return 1;
5921 }
5922
9ac7f98e
JB
5923 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5924
5925 if (strcmp (str, "TKB") == 0)
5926 return 1;
5927
529cad9c
PH
5928#if 0
5929 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5930 with a N at the end. Unfortunately, the compiler uses the same
5931 convention for other internal types it creates. So treating
529cad9c 5932 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5933 some regressions. For instance, consider the case of an enumerated
5934 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5935 name ends with N.
5936 Having a single character like this as a suffix carrying some
0963b4bd 5937 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5938 to be something like "_N" instead. In the meantime, do not do
5939 the following check. */
5940 /* Protected Object Subprograms */
5941 if (len == 1 && str [0] == 'N')
5942 return 1;
5943#endif
5944
5945 /* _E[0-9]+[bs]$ */
5946 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5947 {
5948 matching = str + 3;
5949 while (isdigit (matching[0]))
5950 matching += 1;
5951 if ((matching[0] == 'b' || matching[0] == 's')
5952 && matching [1] == '\0')
5953 return 1;
5954 }
5955
4c4b4cd2
PH
5956 /* ??? We should not modify STR directly, as we are doing below. This
5957 is fine in this case, but may become problematic later if we find
5958 that this alternative did not work, and want to try matching
5959 another one from the begining of STR. Since we modified it, we
5960 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5961 if (str[0] == 'X')
5962 {
5963 str += 1;
d2e4a39e 5964 while (str[0] != '_' && str[0] != '\0')
4c4b4cd2
PH
5965 {
5966 if (str[0] != 'n' && str[0] != 'b')
5967 return 0;
5968 str += 1;
5969 }
14f9c5c9 5970 }
babe1480 5971
14f9c5c9
AS
5972 if (str[0] == '\000')
5973 return 1;
babe1480 5974
d2e4a39e 5975 if (str[0] == '_')
14f9c5c9
AS
5976 {
5977 if (str[1] != '_' || str[2] == '\000')
4c4b4cd2 5978 return 0;
d2e4a39e 5979 if (str[2] == '_')
4c4b4cd2 5980 {
61ee279c
PH
5981 if (strcmp (str + 3, "JM") == 0)
5982 return 1;
5983 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5984 the LJM suffix in favor of the JM one. But we will
5985 still accept LJM as a valid suffix for a reasonable
5986 amount of time, just to allow ourselves to debug programs
5987 compiled using an older version of GNAT. */
4c4b4cd2
PH
5988 if (strcmp (str + 3, "LJM") == 0)
5989 return 1;
5990 if (str[3] != 'X')
5991 return 0;
1265e4aa
JB
5992 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5993 || str[4] == 'U' || str[4] == 'P')
4c4b4cd2
PH
5994 return 1;
5995 if (str[4] == 'R' && str[5] != 'T')
5996 return 1;
5997 return 0;
5998 }
5999 if (!isdigit (str[2]))
6000 return 0;
6001 for (k = 3; str[k] != '\0'; k += 1)
6002 if (!isdigit (str[k]) && str[k] != '_')
6003 return 0;
14f9c5c9
AS
6004 return 1;
6005 }
4c4b4cd2 6006 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 6007 {
4c4b4cd2
PH
6008 for (k = 2; str[k] != '\0'; k += 1)
6009 if (!isdigit (str[k]) && str[k] != '_')
6010 return 0;
14f9c5c9
AS
6011 return 1;
6012 }
6013 return 0;
6014}
d2e4a39e 6015
aeb5907d
JB
6016/* Return non-zero if the string starting at NAME and ending before
6017 NAME_END contains no capital letters. */
529cad9c
PH
6018
6019static int
6020is_valid_name_for_wild_match (const char *name0)
6021{
6022 const char *decoded_name = ada_decode (name0);
6023 int i;
6024
5823c3ef
JB
6025 /* If the decoded name starts with an angle bracket, it means that
6026 NAME0 does not follow the GNAT encoding format. It should then
6027 not be allowed as a possible wild match. */
6028 if (decoded_name[0] == '<')
6029 return 0;
6030
529cad9c
PH
6031 for (i=0; decoded_name[i] != '\0'; i++)
6032 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
6033 return 0;
6034
6035 return 1;
6036}
6037
73589123
PH
6038/* Advance *NAMEP to next occurrence of TARGET0 in the string NAME0
6039 that could start a simple name. Assumes that *NAMEP points into
6040 the string beginning at NAME0. */
4c4b4cd2 6041
14f9c5c9 6042static int
73589123 6043advance_wild_match (const char **namep, const char *name0, int target0)
14f9c5c9 6044{
73589123 6045 const char *name = *namep;
5b4ee69b 6046
5823c3ef 6047 while (1)
14f9c5c9 6048 {
aa27d0b3 6049 int t0, t1;
73589123
PH
6050
6051 t0 = *name;
6052 if (t0 == '_')
6053 {
6054 t1 = name[1];
6055 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
6056 {
6057 name += 1;
61012eef 6058 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
6059 break;
6060 else
6061 name += 1;
6062 }
aa27d0b3
JB
6063 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
6064 || name[2] == target0))
73589123
PH
6065 {
6066 name += 2;
6067 break;
6068 }
6069 else
6070 return 0;
6071 }
6072 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
6073 name += 1;
6074 else
5823c3ef 6075 return 0;
73589123
PH
6076 }
6077
6078 *namep = name;
6079 return 1;
6080}
6081
6082/* Return 0 iff NAME encodes a name of the form prefix.PATN. Ignores any
6083 informational suffixes of NAME (i.e., for which is_name_suffix is
6084 true). Assumes that PATN is a lower-cased Ada simple name. */
6085
6086static int
6087wild_match (const char *name, const char *patn)
6088{
22e048c9 6089 const char *p;
73589123
PH
6090 const char *name0 = name;
6091
6092 while (1)
6093 {
6094 const char *match = name;
6095
6096 if (*name == *patn)
6097 {
6098 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6099 if (*p != *name)
6100 break;
6101 if (*p == '\0' && is_name_suffix (name))
6102 return match != name0 && !is_valid_name_for_wild_match (name0);
6103
6104 if (name[-1] == '_')
6105 name -= 1;
6106 }
6107 if (!advance_wild_match (&name, name0, *patn))
6108 return 1;
96d887e8 6109 }
96d887e8
PH
6110}
6111
40658b94
PH
6112/* Returns 0 iff symbol name SYM_NAME matches SEARCH_NAME, apart from
6113 informational suffix. */
6114
c4d840bd
PH
6115static int
6116full_match (const char *sym_name, const char *search_name)
6117{
40658b94 6118 return !match_name (sym_name, search_name, 0);
c4d840bd
PH
6119}
6120
6121
96d887e8
PH
6122/* Add symbols from BLOCK matching identifier NAME in DOMAIN to
6123 vector *defn_symbols, updating the list of symbols in OBSTACKP
0963b4bd 6124 (if necessary). If WILD, treat as NAME with a wildcard prefix.
4eeaa230 6125 OBJFILE is the section containing BLOCK. */
96d887e8
PH
6126
6127static void
6128ada_add_block_symbols (struct obstack *obstackp,
f0c5f9b2 6129 const struct block *block, const char *name,
96d887e8 6130 domain_enum domain, struct objfile *objfile,
2570f2b7 6131 int wild)
96d887e8 6132{
8157b174 6133 struct block_iterator iter;
96d887e8
PH
6134 int name_len = strlen (name);
6135 /* A matching argument symbol, if any. */
6136 struct symbol *arg_sym;
6137 /* Set true when we find a matching non-argument symbol. */
6138 int found_sym;
6139 struct symbol *sym;
6140
6141 arg_sym = NULL;
6142 found_sym = 0;
6143 if (wild)
6144 {
8157b174
TT
6145 for (sym = block_iter_match_first (block, name, wild_match, &iter);
6146 sym != NULL; sym = block_iter_match_next (name, wild_match, &iter))
76a01679 6147 {
4186eb54
KS
6148 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
6149 SYMBOL_DOMAIN (sym), domain)
73589123 6150 && wild_match (SYMBOL_LINKAGE_NAME (sym), name) == 0)
76a01679 6151 {
2a2d4dc3
AS
6152 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
6153 continue;
6154 else if (SYMBOL_IS_ARGUMENT (sym))
6155 arg_sym = sym;
6156 else
6157 {
76a01679
JB
6158 found_sym = 1;
6159 add_defn_to_vec (obstackp,
6160 fixup_symbol_section (sym, objfile),
2570f2b7 6161 block);
76a01679
JB
6162 }
6163 }
6164 }
96d887e8
PH
6165 }
6166 else
6167 {
8157b174
TT
6168 for (sym = block_iter_match_first (block, name, full_match, &iter);
6169 sym != NULL; sym = block_iter_match_next (name, full_match, &iter))
76a01679 6170 {
4186eb54
KS
6171 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
6172 SYMBOL_DOMAIN (sym), domain))
76a01679 6173 {
c4d840bd
PH
6174 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
6175 {
6176 if (SYMBOL_IS_ARGUMENT (sym))
6177 arg_sym = sym;
6178 else
2a2d4dc3 6179 {
c4d840bd
PH
6180 found_sym = 1;
6181 add_defn_to_vec (obstackp,
6182 fixup_symbol_section (sym, objfile),
6183 block);
2a2d4dc3 6184 }
c4d840bd 6185 }
76a01679
JB
6186 }
6187 }
96d887e8
PH
6188 }
6189
22cee43f
PMR
6190 /* Handle renamings. */
6191
6192 if (ada_add_block_renamings (obstackp, block, name, domain, wild))
6193 found_sym = 1;
6194
96d887e8
PH
6195 if (!found_sym && arg_sym != NULL)
6196 {
76a01679
JB
6197 add_defn_to_vec (obstackp,
6198 fixup_symbol_section (arg_sym, objfile),
2570f2b7 6199 block);
96d887e8
PH
6200 }
6201
6202 if (!wild)
6203 {
6204 arg_sym = NULL;
6205 found_sym = 0;
6206
6207 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 6208 {
4186eb54
KS
6209 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
6210 SYMBOL_DOMAIN (sym), domain))
76a01679
JB
6211 {
6212 int cmp;
6213
6214 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
6215 if (cmp == 0)
6216 {
61012eef 6217 cmp = !startswith (SYMBOL_LINKAGE_NAME (sym), "_ada_");
76a01679
JB
6218 if (cmp == 0)
6219 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
6220 name_len);
6221 }
6222
6223 if (cmp == 0
6224 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
6225 {
2a2d4dc3
AS
6226 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
6227 {
6228 if (SYMBOL_IS_ARGUMENT (sym))
6229 arg_sym = sym;
6230 else
6231 {
6232 found_sym = 1;
6233 add_defn_to_vec (obstackp,
6234 fixup_symbol_section (sym, objfile),
6235 block);
6236 }
6237 }
76a01679
JB
6238 }
6239 }
76a01679 6240 }
96d887e8
PH
6241
6242 /* NOTE: This really shouldn't be needed for _ada_ symbols.
6243 They aren't parameters, right? */
6244 if (!found_sym && arg_sym != NULL)
6245 {
6246 add_defn_to_vec (obstackp,
76a01679 6247 fixup_symbol_section (arg_sym, objfile),
2570f2b7 6248 block);
96d887e8
PH
6249 }
6250 }
6251}
6252\f
41d27058
JB
6253
6254 /* Symbol Completion */
6255
6256/* If SYM_NAME is a completion candidate for TEXT, return this symbol
6257 name in a form that's appropriate for the completion. The result
6258 does not need to be deallocated, but is only good until the next call.
6259
6260 TEXT_LEN is equal to the length of TEXT.
e701b3c0 6261 Perform a wild match if WILD_MATCH_P is set.
6ea35997 6262 ENCODED_P should be set if TEXT represents the start of a symbol name
41d27058
JB
6263 in its encoded form. */
6264
6265static const char *
6266symbol_completion_match (const char *sym_name,
6267 const char *text, int text_len,
6ea35997 6268 int wild_match_p, int encoded_p)
41d27058 6269{
41d27058
JB
6270 const int verbatim_match = (text[0] == '<');
6271 int match = 0;
6272
6273 if (verbatim_match)
6274 {
6275 /* Strip the leading angle bracket. */
6276 text = text + 1;
6277 text_len--;
6278 }
6279
6280 /* First, test against the fully qualified name of the symbol. */
6281
6282 if (strncmp (sym_name, text, text_len) == 0)
6283 match = 1;
6284
6ea35997 6285 if (match && !encoded_p)
41d27058
JB
6286 {
6287 /* One needed check before declaring a positive match is to verify
6288 that iff we are doing a verbatim match, the decoded version
6289 of the symbol name starts with '<'. Otherwise, this symbol name
6290 is not a suitable completion. */
6291 const char *sym_name_copy = sym_name;
6292 int has_angle_bracket;
6293
6294 sym_name = ada_decode (sym_name);
6295 has_angle_bracket = (sym_name[0] == '<');
6296 match = (has_angle_bracket == verbatim_match);
6297 sym_name = sym_name_copy;
6298 }
6299
6300 if (match && !verbatim_match)
6301 {
6302 /* When doing non-verbatim match, another check that needs to
6303 be done is to verify that the potentially matching symbol name
6304 does not include capital letters, because the ada-mode would
6305 not be able to understand these symbol names without the
6306 angle bracket notation. */
6307 const char *tmp;
6308
6309 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6310 if (*tmp != '\0')
6311 match = 0;
6312 }
6313
6314 /* Second: Try wild matching... */
6315
e701b3c0 6316 if (!match && wild_match_p)
41d27058
JB
6317 {
6318 /* Since we are doing wild matching, this means that TEXT
6319 may represent an unqualified symbol name. We therefore must
6320 also compare TEXT against the unqualified name of the symbol. */
6321 sym_name = ada_unqualified_name (ada_decode (sym_name));
6322
6323 if (strncmp (sym_name, text, text_len) == 0)
6324 match = 1;
6325 }
6326
6327 /* Finally: If we found a mach, prepare the result to return. */
6328
6329 if (!match)
6330 return NULL;
6331
6332 if (verbatim_match)
6333 sym_name = add_angle_brackets (sym_name);
6334
6ea35997 6335 if (!encoded_p)
41d27058
JB
6336 sym_name = ada_decode (sym_name);
6337
6338 return sym_name;
6339}
6340
6341/* A companion function to ada_make_symbol_completion_list().
6342 Check if SYM_NAME represents a symbol which name would be suitable
6343 to complete TEXT (TEXT_LEN is the length of TEXT), in which case
6344 it is appended at the end of the given string vector SV.
6345
6346 ORIG_TEXT is the string original string from the user command
6347 that needs to be completed. WORD is the entire command on which
6348 completion should be performed. These two parameters are used to
6349 determine which part of the symbol name should be added to the
6350 completion vector.
c0af1706 6351 if WILD_MATCH_P is set, then wild matching is performed.
cb8e9b97 6352 ENCODED_P should be set if TEXT represents a symbol name in its
41d27058
JB
6353 encoded formed (in which case the completion should also be
6354 encoded). */
6355
6356static void
d6565258 6357symbol_completion_add (VEC(char_ptr) **sv,
41d27058
JB
6358 const char *sym_name,
6359 const char *text, int text_len,
6360 const char *orig_text, const char *word,
cb8e9b97 6361 int wild_match_p, int encoded_p)
41d27058
JB
6362{
6363 const char *match = symbol_completion_match (sym_name, text, text_len,
cb8e9b97 6364 wild_match_p, encoded_p);
41d27058
JB
6365 char *completion;
6366
6367 if (match == NULL)
6368 return;
6369
6370 /* We found a match, so add the appropriate completion to the given
6371 string vector. */
6372
6373 if (word == orig_text)
6374 {
224c3ddb 6375 completion = (char *) xmalloc (strlen (match) + 5);
41d27058
JB
6376 strcpy (completion, match);
6377 }
6378 else if (word > orig_text)
6379 {
6380 /* Return some portion of sym_name. */
224c3ddb 6381 completion = (char *) xmalloc (strlen (match) + 5);
41d27058
JB
6382 strcpy (completion, match + (word - orig_text));
6383 }
6384 else
6385 {
6386 /* Return some of ORIG_TEXT plus sym_name. */
224c3ddb 6387 completion = (char *) xmalloc (strlen (match) + (orig_text - word) + 5);
41d27058
JB
6388 strncpy (completion, word, orig_text - word);
6389 completion[orig_text - word] = '\0';
6390 strcat (completion, match);
6391 }
6392
d6565258 6393 VEC_safe_push (char_ptr, *sv, completion);
41d27058
JB
6394}
6395
ccefe4c4 6396/* An object of this type is passed as the user_data argument to the
bb4142cf 6397 expand_symtabs_matching method. */
ccefe4c4
TT
6398struct add_partial_datum
6399{
6400 VEC(char_ptr) **completions;
6f937416 6401 const char *text;
ccefe4c4 6402 int text_len;
6f937416
PA
6403 const char *text0;
6404 const char *word;
ccefe4c4
TT
6405 int wild_match;
6406 int encoded;
6407};
6408
bb4142cf
DE
6409/* A callback for expand_symtabs_matching. */
6410
7b08b9eb 6411static int
bb4142cf 6412ada_complete_symbol_matcher (const char *name, void *user_data)
ccefe4c4 6413{
9a3c8263 6414 struct add_partial_datum *data = (struct add_partial_datum *) user_data;
7b08b9eb
JK
6415
6416 return symbol_completion_match (name, data->text, data->text_len,
6417 data->wild_match, data->encoded) != NULL;
ccefe4c4
TT
6418}
6419
49c4e619
TT
6420/* Return a list of possible symbol names completing TEXT0. WORD is
6421 the entire command on which completion is made. */
41d27058 6422
49c4e619 6423static VEC (char_ptr) *
6f937416
PA
6424ada_make_symbol_completion_list (const char *text0, const char *word,
6425 enum type_code code)
41d27058
JB
6426{
6427 char *text;
6428 int text_len;
b1ed564a
JB
6429 int wild_match_p;
6430 int encoded_p;
2ba95b9b 6431 VEC(char_ptr) *completions = VEC_alloc (char_ptr, 128);
41d27058 6432 struct symbol *sym;
43f3e411 6433 struct compunit_symtab *s;
41d27058
JB
6434 struct minimal_symbol *msymbol;
6435 struct objfile *objfile;
3977b71f 6436 const struct block *b, *surrounding_static_block = 0;
41d27058 6437 int i;
8157b174 6438 struct block_iterator iter;
b8fea896 6439 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
41d27058 6440
2f68a895
TT
6441 gdb_assert (code == TYPE_CODE_UNDEF);
6442
41d27058
JB
6443 if (text0[0] == '<')
6444 {
6445 text = xstrdup (text0);
6446 make_cleanup (xfree, text);
6447 text_len = strlen (text);
b1ed564a
JB
6448 wild_match_p = 0;
6449 encoded_p = 1;
41d27058
JB
6450 }
6451 else
6452 {
6453 text = xstrdup (ada_encode (text0));
6454 make_cleanup (xfree, text);
6455 text_len = strlen (text);
6456 for (i = 0; i < text_len; i++)
6457 text[i] = tolower (text[i]);
6458
b1ed564a 6459 encoded_p = (strstr (text0, "__") != NULL);
41d27058
JB
6460 /* If the name contains a ".", then the user is entering a fully
6461 qualified entity name, and the match must not be done in wild
6462 mode. Similarly, if the user wants to complete what looks like
6463 an encoded name, the match must not be done in wild mode. */
b1ed564a 6464 wild_match_p = (strchr (text0, '.') == NULL && !encoded_p);
41d27058
JB
6465 }
6466
6467 /* First, look at the partial symtab symbols. */
41d27058 6468 {
ccefe4c4
TT
6469 struct add_partial_datum data;
6470
6471 data.completions = &completions;
6472 data.text = text;
6473 data.text_len = text_len;
6474 data.text0 = text0;
6475 data.word = word;
b1ed564a
JB
6476 data.wild_match = wild_match_p;
6477 data.encoded = encoded_p;
276d885b
GB
6478 expand_symtabs_matching (NULL, ada_complete_symbol_matcher, NULL,
6479 ALL_DOMAIN, &data);
41d27058
JB
6480 }
6481
6482 /* At this point scan through the misc symbol vectors and add each
6483 symbol you find to the list. Eventually we want to ignore
6484 anything that isn't a text symbol (everything else will be
6485 handled by the psymtab code above). */
6486
6487 ALL_MSYMBOLS (objfile, msymbol)
6488 {
6489 QUIT;
efd66ac6 6490 symbol_completion_add (&completions, MSYMBOL_LINKAGE_NAME (msymbol),
b1ed564a
JB
6491 text, text_len, text0, word, wild_match_p,
6492 encoded_p);
41d27058
JB
6493 }
6494
6495 /* Search upwards from currently selected frame (so that we can
6496 complete on local vars. */
6497
6498 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
6499 {
6500 if (!BLOCK_SUPERBLOCK (b))
6501 surrounding_static_block = b; /* For elmin of dups */
6502
6503 ALL_BLOCK_SYMBOLS (b, iter, sym)
6504 {
d6565258 6505 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058 6506 text, text_len, text0, word,
b1ed564a 6507 wild_match_p, encoded_p);
41d27058
JB
6508 }
6509 }
6510
6511 /* Go through the symtabs and check the externs and statics for
43f3e411 6512 symbols which match. */
41d27058 6513
43f3e411 6514 ALL_COMPUNITS (objfile, s)
41d27058
JB
6515 {
6516 QUIT;
43f3e411 6517 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), GLOBAL_BLOCK);
41d27058
JB
6518 ALL_BLOCK_SYMBOLS (b, iter, sym)
6519 {
d6565258 6520 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058 6521 text, text_len, text0, word,
b1ed564a 6522 wild_match_p, encoded_p);
41d27058
JB
6523 }
6524 }
6525
43f3e411 6526 ALL_COMPUNITS (objfile, s)
41d27058
JB
6527 {
6528 QUIT;
43f3e411 6529 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), STATIC_BLOCK);
41d27058
JB
6530 /* Don't do this block twice. */
6531 if (b == surrounding_static_block)
6532 continue;
6533 ALL_BLOCK_SYMBOLS (b, iter, sym)
6534 {
d6565258 6535 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058 6536 text, text_len, text0, word,
b1ed564a 6537 wild_match_p, encoded_p);
41d27058
JB
6538 }
6539 }
6540
b8fea896 6541 do_cleanups (old_chain);
49c4e619 6542 return completions;
41d27058
JB
6543}
6544
963a6417 6545 /* Field Access */
96d887e8 6546
73fb9985
JB
6547/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6548 for tagged types. */
6549
6550static int
6551ada_is_dispatch_table_ptr_type (struct type *type)
6552{
0d5cff50 6553 const char *name;
73fb9985
JB
6554
6555 if (TYPE_CODE (type) != TYPE_CODE_PTR)
6556 return 0;
6557
6558 name = TYPE_NAME (TYPE_TARGET_TYPE (type));
6559 if (name == NULL)
6560 return 0;
6561
6562 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6563}
6564
ac4a2da4
JG
6565/* Return non-zero if TYPE is an interface tag. */
6566
6567static int
6568ada_is_interface_tag (struct type *type)
6569{
6570 const char *name = TYPE_NAME (type);
6571
6572 if (name == NULL)
6573 return 0;
6574
6575 return (strcmp (name, "ada__tags__interface_tag") == 0);
6576}
6577
963a6417
PH
6578/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6579 to be invisible to users. */
96d887e8 6580
963a6417
PH
6581int
6582ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6583{
963a6417
PH
6584 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
6585 return 1;
ffde82bf 6586
73fb9985
JB
6587 /* Check the name of that field. */
6588 {
6589 const char *name = TYPE_FIELD_NAME (type, field_num);
6590
6591 /* Anonymous field names should not be printed.
6592 brobecker/2007-02-20: I don't think this can actually happen
6593 but we don't want to print the value of annonymous fields anyway. */
6594 if (name == NULL)
6595 return 1;
6596
ffde82bf
JB
6597 /* Normally, fields whose name start with an underscore ("_")
6598 are fields that have been internally generated by the compiler,
6599 and thus should not be printed. The "_parent" field is special,
6600 however: This is a field internally generated by the compiler
6601 for tagged types, and it contains the components inherited from
6602 the parent type. This field should not be printed as is, but
6603 should not be ignored either. */
61012eef 6604 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985
JB
6605 return 1;
6606 }
6607
ac4a2da4
JG
6608 /* If this is the dispatch table of a tagged type or an interface tag,
6609 then ignore. */
73fb9985 6610 if (ada_is_tagged_type (type, 1)
ac4a2da4
JG
6611 && (ada_is_dispatch_table_ptr_type (TYPE_FIELD_TYPE (type, field_num))
6612 || ada_is_interface_tag (TYPE_FIELD_TYPE (type, field_num))))
73fb9985
JB
6613 return 1;
6614
6615 /* Not a special field, so it should not be ignored. */
6616 return 0;
963a6417 6617}
96d887e8 6618
963a6417 6619/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6620 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6621
963a6417
PH
6622int
6623ada_is_tagged_type (struct type *type, int refok)
6624{
6625 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
6626}
96d887e8 6627
963a6417 6628/* True iff TYPE represents the type of X'Tag */
96d887e8 6629
963a6417
PH
6630int
6631ada_is_tag_type (struct type *type)
6632{
460efde1
JB
6633 type = ada_check_typedef (type);
6634
963a6417
PH
6635 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
6636 return 0;
6637 else
96d887e8 6638 {
963a6417 6639 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 6640
963a6417
PH
6641 return (name != NULL
6642 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6643 }
96d887e8
PH
6644}
6645
963a6417 6646/* The type of the tag on VAL. */
76a01679 6647
963a6417
PH
6648struct type *
6649ada_tag_type (struct value *val)
96d887e8 6650{
df407dfe 6651 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0, NULL);
963a6417 6652}
96d887e8 6653
b50d69b5
JG
6654/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6655 retired at Ada 05). */
6656
6657static int
6658is_ada95_tag (struct value *tag)
6659{
6660 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6661}
6662
963a6417 6663/* The value of the tag on VAL. */
96d887e8 6664
963a6417
PH
6665struct value *
6666ada_value_tag (struct value *val)
6667{
03ee6b2e 6668 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6669}
6670
963a6417
PH
6671/* The value of the tag on the object of type TYPE whose contents are
6672 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6673 ADDRESS. */
96d887e8 6674
963a6417 6675static struct value *
10a2c479 6676value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6677 const gdb_byte *valaddr,
963a6417 6678 CORE_ADDR address)
96d887e8 6679{
b5385fc0 6680 int tag_byte_offset;
963a6417 6681 struct type *tag_type;
5b4ee69b 6682
963a6417 6683 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
52ce6436 6684 NULL, NULL, NULL))
96d887e8 6685 {
fc1a4b47 6686 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6687 ? NULL
6688 : valaddr + tag_byte_offset);
963a6417 6689 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6690
963a6417 6691 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6692 }
963a6417
PH
6693 return NULL;
6694}
96d887e8 6695
963a6417
PH
6696static struct type *
6697type_from_tag (struct value *tag)
6698{
6699 const char *type_name = ada_tag_name (tag);
5b4ee69b 6700
963a6417
PH
6701 if (type_name != NULL)
6702 return ada_find_any_type (ada_encode (type_name));
6703 return NULL;
6704}
96d887e8 6705
b50d69b5
JG
6706/* Given a value OBJ of a tagged type, return a value of this
6707 type at the base address of the object. The base address, as
6708 defined in Ada.Tags, it is the address of the primary tag of
6709 the object, and therefore where the field values of its full
6710 view can be fetched. */
6711
6712struct value *
6713ada_tag_value_at_base_address (struct value *obj)
6714{
b50d69b5
JG
6715 struct value *val;
6716 LONGEST offset_to_top = 0;
6717 struct type *ptr_type, *obj_type;
6718 struct value *tag;
6719 CORE_ADDR base_address;
6720
6721 obj_type = value_type (obj);
6722
6723 /* It is the responsability of the caller to deref pointers. */
6724
6725 if (TYPE_CODE (obj_type) == TYPE_CODE_PTR
6726 || TYPE_CODE (obj_type) == TYPE_CODE_REF)
6727 return obj;
6728
6729 tag = ada_value_tag (obj);
6730 if (!tag)
6731 return obj;
6732
6733 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6734
6735 if (is_ada95_tag (tag))
6736 return obj;
6737
6738 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
6739 ptr_type = lookup_pointer_type (ptr_type);
6740 val = value_cast (ptr_type, tag);
6741 if (!val)
6742 return obj;
6743
6744 /* It is perfectly possible that an exception be raised while
6745 trying to determine the base address, just like for the tag;
6746 see ada_tag_name for more details. We do not print the error
6747 message for the same reason. */
6748
492d29ea 6749 TRY
b50d69b5
JG
6750 {
6751 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6752 }
6753
492d29ea
PA
6754 CATCH (e, RETURN_MASK_ERROR)
6755 {
6756 return obj;
6757 }
6758 END_CATCH
b50d69b5
JG
6759
6760 /* If offset is null, nothing to do. */
6761
6762 if (offset_to_top == 0)
6763 return obj;
6764
6765 /* -1 is a special case in Ada.Tags; however, what should be done
6766 is not quite clear from the documentation. So do nothing for
6767 now. */
6768
6769 if (offset_to_top == -1)
6770 return obj;
6771
6772 base_address = value_address (obj) - offset_to_top;
6773 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6774
6775 /* Make sure that we have a proper tag at the new address.
6776 Otherwise, offset_to_top is bogus (which can happen when
6777 the object is not initialized yet). */
6778
6779 if (!tag)
6780 return obj;
6781
6782 obj_type = type_from_tag (tag);
6783
6784 if (!obj_type)
6785 return obj;
6786
6787 return value_from_contents_and_address (obj_type, NULL, base_address);
6788}
6789
1b611343
JB
6790/* Return the "ada__tags__type_specific_data" type. */
6791
6792static struct type *
6793ada_get_tsd_type (struct inferior *inf)
963a6417 6794{
1b611343 6795 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6796
1b611343
JB
6797 if (data->tsd_type == 0)
6798 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6799 return data->tsd_type;
6800}
529cad9c 6801
1b611343
JB
6802/* Return the TSD (type-specific data) associated to the given TAG.
6803 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6804
1b611343 6805 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6806
1b611343
JB
6807static struct value *
6808ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6809{
4c4b4cd2 6810 struct value *val;
1b611343 6811 struct type *type;
5b4ee69b 6812
1b611343
JB
6813 /* First option: The TSD is simply stored as a field of our TAG.
6814 Only older versions of GNAT would use this format, but we have
6815 to test it first, because there are no visible markers for
6816 the current approach except the absence of that field. */
529cad9c 6817
1b611343
JB
6818 val = ada_value_struct_elt (tag, "tsd", 1);
6819 if (val)
6820 return val;
e802dbe0 6821
1b611343
JB
6822 /* Try the second representation for the dispatch table (in which
6823 there is no explicit 'tsd' field in the referent of the tag pointer,
6824 and instead the tsd pointer is stored just before the dispatch
6825 table. */
e802dbe0 6826
1b611343
JB
6827 type = ada_get_tsd_type (current_inferior());
6828 if (type == NULL)
6829 return NULL;
6830 type = lookup_pointer_type (lookup_pointer_type (type));
6831 val = value_cast (type, tag);
6832 if (val == NULL)
6833 return NULL;
6834 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6835}
6836
1b611343
JB
6837/* Given the TSD of a tag (type-specific data), return a string
6838 containing the name of the associated type.
6839
6840 The returned value is good until the next call. May return NULL
6841 if we are unable to determine the tag name. */
6842
6843static char *
6844ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6845{
529cad9c
PH
6846 static char name[1024];
6847 char *p;
1b611343 6848 struct value *val;
529cad9c 6849
1b611343 6850 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6851 if (val == NULL)
1b611343 6852 return NULL;
4c4b4cd2
PH
6853 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
6854 for (p = name; *p != '\0'; p += 1)
6855 if (isalpha (*p))
6856 *p = tolower (*p);
1b611343 6857 return name;
4c4b4cd2
PH
6858}
6859
6860/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6861 a C string.
6862
6863 Return NULL if the TAG is not an Ada tag, or if we were unable to
6864 determine the name of that tag. The result is good until the next
6865 call. */
4c4b4cd2
PH
6866
6867const char *
6868ada_tag_name (struct value *tag)
6869{
1b611343 6870 char *name = NULL;
5b4ee69b 6871
df407dfe 6872 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 6873 return NULL;
1b611343
JB
6874
6875 /* It is perfectly possible that an exception be raised while trying
6876 to determine the TAG's name, even under normal circumstances:
6877 The associated variable may be uninitialized or corrupted, for
6878 instance. We do not let any exception propagate past this point.
6879 instead we return NULL.
6880
6881 We also do not print the error message either (which often is very
6882 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6883 the caller print a more meaningful message if necessary. */
492d29ea 6884 TRY
1b611343
JB
6885 {
6886 struct value *tsd = ada_get_tsd_from_tag (tag);
6887
6888 if (tsd != NULL)
6889 name = ada_tag_name_from_tsd (tsd);
6890 }
492d29ea
PA
6891 CATCH (e, RETURN_MASK_ERROR)
6892 {
6893 }
6894 END_CATCH
1b611343
JB
6895
6896 return name;
4c4b4cd2
PH
6897}
6898
6899/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6900
d2e4a39e 6901struct type *
ebf56fd3 6902ada_parent_type (struct type *type)
14f9c5c9
AS
6903{
6904 int i;
6905
61ee279c 6906 type = ada_check_typedef (type);
14f9c5c9
AS
6907
6908 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
6909 return NULL;
6910
6911 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6912 if (ada_is_parent_field (type, i))
0c1f74cf
JB
6913 {
6914 struct type *parent_type = TYPE_FIELD_TYPE (type, i);
6915
6916 /* If the _parent field is a pointer, then dereference it. */
6917 if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
6918 parent_type = TYPE_TARGET_TYPE (parent_type);
6919 /* If there is a parallel XVS type, get the actual base type. */
6920 parent_type = ada_get_base_type (parent_type);
6921
6922 return ada_check_typedef (parent_type);
6923 }
14f9c5c9
AS
6924
6925 return NULL;
6926}
6927
4c4b4cd2
PH
6928/* True iff field number FIELD_NUM of structure type TYPE contains the
6929 parent-type (inherited) fields of a derived type. Assumes TYPE is
6930 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6931
6932int
ebf56fd3 6933ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6934{
61ee279c 6935 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
5b4ee69b 6936
4c4b4cd2 6937 return (name != NULL
61012eef
GB
6938 && (startswith (name, "PARENT")
6939 || startswith (name, "_parent")));
14f9c5c9
AS
6940}
6941
4c4b4cd2 6942/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6943 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6944 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6945 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6946 structures. */
14f9c5c9
AS
6947
6948int
ebf56fd3 6949ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6950{
d2e4a39e 6951 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 6952
d2e4a39e 6953 return (name != NULL
61012eef 6954 && (startswith (name, "PARENT")
4c4b4cd2 6955 || strcmp (name, "REP") == 0
61012eef 6956 || startswith (name, "_parent")
4c4b4cd2 6957 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6958}
6959
4c4b4cd2
PH
6960/* True iff field number FIELD_NUM of structure or union type TYPE
6961 is a variant wrapper. Assumes TYPE is a structure type with at least
6962 FIELD_NUM+1 fields. */
14f9c5c9
AS
6963
6964int
ebf56fd3 6965ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6966{
d2e4a39e 6967 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
5b4ee69b 6968
14f9c5c9 6969 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
4c4b4cd2 6970 || (is_dynamic_field (type, field_num)
c3e5cd34
PH
6971 && (TYPE_CODE (TYPE_TARGET_TYPE (field_type))
6972 == TYPE_CODE_UNION)));
14f9c5c9
AS
6973}
6974
6975/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6976 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6977 returns the type of the controlling discriminant for the variant.
6978 May return NULL if the type could not be found. */
14f9c5c9 6979
d2e4a39e 6980struct type *
ebf56fd3 6981ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6982{
d2e4a39e 6983 char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6984
7c964f07 6985 return ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
14f9c5c9
AS
6986}
6987
4c4b4cd2 6988/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6989 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6990 represents a 'when others' clause; otherwise 0. */
14f9c5c9
AS
6991
6992int
ebf56fd3 6993ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6994{
d2e4a39e 6995 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 6996
14f9c5c9
AS
6997 return (name != NULL && name[0] == 'O');
6998}
6999
7000/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
7001 returns the name of the discriminant controlling the variant.
7002 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 7003
d2e4a39e 7004char *
ebf56fd3 7005ada_variant_discrim_name (struct type *type0)
14f9c5c9 7006{
d2e4a39e 7007 static char *result = NULL;
14f9c5c9 7008 static size_t result_len = 0;
d2e4a39e
AS
7009 struct type *type;
7010 const char *name;
7011 const char *discrim_end;
7012 const char *discrim_start;
14f9c5c9
AS
7013
7014 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
7015 type = TYPE_TARGET_TYPE (type0);
7016 else
7017 type = type0;
7018
7019 name = ada_type_name (type);
7020
7021 if (name == NULL || name[0] == '\000')
7022 return "";
7023
7024 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
7025 discrim_end -= 1)
7026 {
61012eef 7027 if (startswith (discrim_end, "___XVN"))
4c4b4cd2 7028 break;
14f9c5c9
AS
7029 }
7030 if (discrim_end == name)
7031 return "";
7032
d2e4a39e 7033 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
7034 discrim_start -= 1)
7035 {
d2e4a39e 7036 if (discrim_start == name + 1)
4c4b4cd2 7037 return "";
76a01679 7038 if ((discrim_start > name + 3
61012eef 7039 && startswith (discrim_start - 3, "___"))
4c4b4cd2
PH
7040 || discrim_start[-1] == '.')
7041 break;
14f9c5c9
AS
7042 }
7043
7044 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
7045 strncpy (result, discrim_start, discrim_end - discrim_start);
d2e4a39e 7046 result[discrim_end - discrim_start] = '\0';
14f9c5c9
AS
7047 return result;
7048}
7049
4c4b4cd2
PH
7050/* Scan STR for a subtype-encoded number, beginning at position K.
7051 Put the position of the character just past the number scanned in
7052 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
7053 Return 1 if there was a valid number at the given position, and 0
7054 otherwise. A "subtype-encoded" number consists of the absolute value
7055 in decimal, followed by the letter 'm' to indicate a negative number.
7056 Assumes 0m does not occur. */
14f9c5c9
AS
7057
7058int
d2e4a39e 7059ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
7060{
7061 ULONGEST RU;
7062
d2e4a39e 7063 if (!isdigit (str[k]))
14f9c5c9
AS
7064 return 0;
7065
4c4b4cd2 7066 /* Do it the hard way so as not to make any assumption about
14f9c5c9 7067 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 7068 LONGEST. */
14f9c5c9
AS
7069 RU = 0;
7070 while (isdigit (str[k]))
7071 {
d2e4a39e 7072 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
7073 k += 1;
7074 }
7075
d2e4a39e 7076 if (str[k] == 'm')
14f9c5c9
AS
7077 {
7078 if (R != NULL)
4c4b4cd2 7079 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
7080 k += 1;
7081 }
7082 else if (R != NULL)
7083 *R = (LONGEST) RU;
7084
4c4b4cd2 7085 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
7086 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
7087 number representable as a LONGEST (although either would probably work
7088 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 7089 above is always equivalent to the negative of RU. */
14f9c5c9
AS
7090
7091 if (new_k != NULL)
7092 *new_k = k;
7093 return 1;
7094}
7095
4c4b4cd2
PH
7096/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
7097 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
7098 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 7099
d2e4a39e 7100int
ebf56fd3 7101ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 7102{
d2e4a39e 7103 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
7104 int p;
7105
7106 p = 0;
7107 while (1)
7108 {
d2e4a39e 7109 switch (name[p])
4c4b4cd2
PH
7110 {
7111 case '\0':
7112 return 0;
7113 case 'S':
7114 {
7115 LONGEST W;
5b4ee69b 7116
4c4b4cd2
PH
7117 if (!ada_scan_number (name, p + 1, &W, &p))
7118 return 0;
7119 if (val == W)
7120 return 1;
7121 break;
7122 }
7123 case 'R':
7124 {
7125 LONGEST L, U;
5b4ee69b 7126
4c4b4cd2
PH
7127 if (!ada_scan_number (name, p + 1, &L, &p)
7128 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
7129 return 0;
7130 if (val >= L && val <= U)
7131 return 1;
7132 break;
7133 }
7134 case 'O':
7135 return 1;
7136 default:
7137 return 0;
7138 }
7139 }
7140}
7141
0963b4bd 7142/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
7143
7144/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
7145 ARG_TYPE, extract and return the value of one of its (non-static)
7146 fields. FIELDNO says which field. Differs from value_primitive_field
7147 only in that it can handle packed values of arbitrary type. */
14f9c5c9 7148
4c4b4cd2 7149static struct value *
d2e4a39e 7150ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
4c4b4cd2 7151 struct type *arg_type)
14f9c5c9 7152{
14f9c5c9
AS
7153 struct type *type;
7154
61ee279c 7155 arg_type = ada_check_typedef (arg_type);
14f9c5c9
AS
7156 type = TYPE_FIELD_TYPE (arg_type, fieldno);
7157
4c4b4cd2 7158 /* Handle packed fields. */
14f9c5c9
AS
7159
7160 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
7161 {
7162 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
7163 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 7164
0fd88904 7165 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
4c4b4cd2
PH
7166 offset + bit_pos / 8,
7167 bit_pos % 8, bit_size, type);
14f9c5c9
AS
7168 }
7169 else
7170 return value_primitive_field (arg1, offset, fieldno, arg_type);
7171}
7172
52ce6436
PH
7173/* Find field with name NAME in object of type TYPE. If found,
7174 set the following for each argument that is non-null:
7175 - *FIELD_TYPE_P to the field's type;
7176 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
7177 an object of that type;
7178 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
7179 - *BIT_SIZE_P to its size in bits if the field is packed, and
7180 0 otherwise;
7181 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
7182 fields up to but not including the desired field, or by the total
7183 number of fields if not found. A NULL value of NAME never
7184 matches; the function just counts visible fields in this case.
7185
0963b4bd 7186 Returns 1 if found, 0 otherwise. */
52ce6436 7187
4c4b4cd2 7188static int
0d5cff50 7189find_struct_field (const char *name, struct type *type, int offset,
76a01679 7190 struct type **field_type_p,
52ce6436
PH
7191 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
7192 int *index_p)
4c4b4cd2
PH
7193{
7194 int i;
7195
61ee279c 7196 type = ada_check_typedef (type);
76a01679 7197
52ce6436
PH
7198 if (field_type_p != NULL)
7199 *field_type_p = NULL;
7200 if (byte_offset_p != NULL)
d5d6fca5 7201 *byte_offset_p = 0;
52ce6436
PH
7202 if (bit_offset_p != NULL)
7203 *bit_offset_p = 0;
7204 if (bit_size_p != NULL)
7205 *bit_size_p = 0;
7206
7207 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
4c4b4cd2
PH
7208 {
7209 int bit_pos = TYPE_FIELD_BITPOS (type, i);
7210 int fld_offset = offset + bit_pos / 8;
0d5cff50 7211 const char *t_field_name = TYPE_FIELD_NAME (type, i);
76a01679 7212
4c4b4cd2
PH
7213 if (t_field_name == NULL)
7214 continue;
7215
52ce6436 7216 else if (name != NULL && field_name_match (t_field_name, name))
76a01679
JB
7217 {
7218 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 7219
52ce6436
PH
7220 if (field_type_p != NULL)
7221 *field_type_p = TYPE_FIELD_TYPE (type, i);
7222 if (byte_offset_p != NULL)
7223 *byte_offset_p = fld_offset;
7224 if (bit_offset_p != NULL)
7225 *bit_offset_p = bit_pos % 8;
7226 if (bit_size_p != NULL)
7227 *bit_size_p = bit_size;
76a01679
JB
7228 return 1;
7229 }
4c4b4cd2
PH
7230 else if (ada_is_wrapper_field (type, i))
7231 {
52ce6436
PH
7232 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
7233 field_type_p, byte_offset_p, bit_offset_p,
7234 bit_size_p, index_p))
76a01679
JB
7235 return 1;
7236 }
4c4b4cd2
PH
7237 else if (ada_is_variant_part (type, i))
7238 {
52ce6436
PH
7239 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7240 fixed type?? */
4c4b4cd2 7241 int j;
52ce6436
PH
7242 struct type *field_type
7243 = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2 7244
52ce6436 7245 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
4c4b4cd2 7246 {
76a01679
JB
7247 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
7248 fld_offset
7249 + TYPE_FIELD_BITPOS (field_type, j) / 8,
7250 field_type_p, byte_offset_p,
52ce6436 7251 bit_offset_p, bit_size_p, index_p))
76a01679 7252 return 1;
4c4b4cd2
PH
7253 }
7254 }
52ce6436
PH
7255 else if (index_p != NULL)
7256 *index_p += 1;
4c4b4cd2
PH
7257 }
7258 return 0;
7259}
7260
0963b4bd 7261/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7262
52ce6436
PH
7263static int
7264num_visible_fields (struct type *type)
7265{
7266 int n;
5b4ee69b 7267
52ce6436
PH
7268 n = 0;
7269 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7270 return n;
7271}
14f9c5c9 7272
4c4b4cd2 7273/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7274 and search in it assuming it has (class) type TYPE.
7275 If found, return value, else return NULL.
7276
4c4b4cd2 7277 Searches recursively through wrapper fields (e.g., '_parent'). */
14f9c5c9 7278
4c4b4cd2 7279static struct value *
108d56a4 7280ada_search_struct_field (const char *name, struct value *arg, int offset,
4c4b4cd2 7281 struct type *type)
14f9c5c9
AS
7282{
7283 int i;
14f9c5c9 7284
5b4ee69b 7285 type = ada_check_typedef (type);
52ce6436 7286 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
14f9c5c9 7287 {
0d5cff50 7288 const char *t_field_name = TYPE_FIELD_NAME (type, i);
14f9c5c9
AS
7289
7290 if (t_field_name == NULL)
4c4b4cd2 7291 continue;
14f9c5c9
AS
7292
7293 else if (field_name_match (t_field_name, name))
4c4b4cd2 7294 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7295
7296 else if (ada_is_wrapper_field (type, i))
4c4b4cd2 7297 {
0963b4bd 7298 struct value *v = /* Do not let indent join lines here. */
06d5cf63
JB
7299 ada_search_struct_field (name, arg,
7300 offset + TYPE_FIELD_BITPOS (type, i) / 8,
7301 TYPE_FIELD_TYPE (type, i));
5b4ee69b 7302
4c4b4cd2
PH
7303 if (v != NULL)
7304 return v;
7305 }
14f9c5c9
AS
7306
7307 else if (ada_is_variant_part (type, i))
4c4b4cd2 7308 {
0963b4bd 7309 /* PNH: Do we ever get here? See find_struct_field. */
4c4b4cd2 7310 int j;
5b4ee69b
MS
7311 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
7312 i));
4c4b4cd2
PH
7313 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
7314
52ce6436 7315 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
4c4b4cd2 7316 {
0963b4bd
MS
7317 struct value *v = ada_search_struct_field /* Force line
7318 break. */
06d5cf63
JB
7319 (name, arg,
7320 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
7321 TYPE_FIELD_TYPE (field_type, j));
5b4ee69b 7322
4c4b4cd2
PH
7323 if (v != NULL)
7324 return v;
7325 }
7326 }
14f9c5c9
AS
7327 }
7328 return NULL;
7329}
d2e4a39e 7330
52ce6436
PH
7331static struct value *ada_index_struct_field_1 (int *, struct value *,
7332 int, struct type *);
7333
7334
7335/* Return field #INDEX in ARG, where the index is that returned by
7336 * find_struct_field through its INDEX_P argument. Adjust the address
7337 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7338 * If found, return value, else return NULL. */
52ce6436
PH
7339
7340static struct value *
7341ada_index_struct_field (int index, struct value *arg, int offset,
7342 struct type *type)
7343{
7344 return ada_index_struct_field_1 (&index, arg, offset, type);
7345}
7346
7347
7348/* Auxiliary function for ada_index_struct_field. Like
7349 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7350 * *INDEX_P. */
52ce6436
PH
7351
7352static struct value *
7353ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7354 struct type *type)
7355{
7356 int i;
7357 type = ada_check_typedef (type);
7358
7359 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
7360 {
7361 if (TYPE_FIELD_NAME (type, i) == NULL)
7362 continue;
7363 else if (ada_is_wrapper_field (type, i))
7364 {
0963b4bd 7365 struct value *v = /* Do not let indent join lines here. */
52ce6436
PH
7366 ada_index_struct_field_1 (index_p, arg,
7367 offset + TYPE_FIELD_BITPOS (type, i) / 8,
7368 TYPE_FIELD_TYPE (type, i));
5b4ee69b 7369
52ce6436
PH
7370 if (v != NULL)
7371 return v;
7372 }
7373
7374 else if (ada_is_variant_part (type, i))
7375 {
7376 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7377 find_struct_field. */
52ce6436
PH
7378 error (_("Cannot assign this kind of variant record"));
7379 }
7380 else if (*index_p == 0)
7381 return ada_value_primitive_field (arg, offset, i, type);
7382 else
7383 *index_p -= 1;
7384 }
7385 return NULL;
7386}
7387
4c4b4cd2
PH
7388/* Given ARG, a value of type (pointer or reference to a)*
7389 structure/union, extract the component named NAME from the ultimate
7390 target structure/union and return it as a value with its
f5938064 7391 appropriate type.
14f9c5c9 7392
4c4b4cd2
PH
7393 The routine searches for NAME among all members of the structure itself
7394 and (recursively) among all members of any wrapper members
14f9c5c9
AS
7395 (e.g., '_parent').
7396
03ee6b2e
PH
7397 If NO_ERR, then simply return NULL in case of error, rather than
7398 calling error. */
14f9c5c9 7399
d2e4a39e 7400struct value *
03ee6b2e 7401ada_value_struct_elt (struct value *arg, char *name, int no_err)
14f9c5c9 7402{
4c4b4cd2 7403 struct type *t, *t1;
d2e4a39e 7404 struct value *v;
14f9c5c9 7405
4c4b4cd2 7406 v = NULL;
df407dfe 7407 t1 = t = ada_check_typedef (value_type (arg));
4c4b4cd2
PH
7408 if (TYPE_CODE (t) == TYPE_CODE_REF)
7409 {
7410 t1 = TYPE_TARGET_TYPE (t);
7411 if (t1 == NULL)
03ee6b2e 7412 goto BadValue;
61ee279c 7413 t1 = ada_check_typedef (t1);
4c4b4cd2 7414 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679 7415 {
994b9211 7416 arg = coerce_ref (arg);
76a01679
JB
7417 t = t1;
7418 }
4c4b4cd2 7419 }
14f9c5c9 7420
4c4b4cd2
PH
7421 while (TYPE_CODE (t) == TYPE_CODE_PTR)
7422 {
7423 t1 = TYPE_TARGET_TYPE (t);
7424 if (t1 == NULL)
03ee6b2e 7425 goto BadValue;
61ee279c 7426 t1 = ada_check_typedef (t1);
4c4b4cd2 7427 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679
JB
7428 {
7429 arg = value_ind (arg);
7430 t = t1;
7431 }
4c4b4cd2 7432 else
76a01679 7433 break;
4c4b4cd2 7434 }
14f9c5c9 7435
4c4b4cd2 7436 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
03ee6b2e 7437 goto BadValue;
14f9c5c9 7438
4c4b4cd2
PH
7439 if (t1 == t)
7440 v = ada_search_struct_field (name, arg, 0, t);
7441 else
7442 {
7443 int bit_offset, bit_size, byte_offset;
7444 struct type *field_type;
7445 CORE_ADDR address;
7446
76a01679 7447 if (TYPE_CODE (t) == TYPE_CODE_PTR)
b50d69b5 7448 address = value_address (ada_value_ind (arg));
4c4b4cd2 7449 else
b50d69b5 7450 address = value_address (ada_coerce_ref (arg));
14f9c5c9 7451
1ed6ede0 7452 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL, 1);
76a01679
JB
7453 if (find_struct_field (name, t1, 0,
7454 &field_type, &byte_offset, &bit_offset,
52ce6436 7455 &bit_size, NULL))
76a01679
JB
7456 {
7457 if (bit_size != 0)
7458 {
714e53ab
PH
7459 if (TYPE_CODE (t) == TYPE_CODE_REF)
7460 arg = ada_coerce_ref (arg);
7461 else
7462 arg = ada_value_ind (arg);
76a01679
JB
7463 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
7464 bit_offset, bit_size,
7465 field_type);
7466 }
7467 else
f5938064 7468 v = value_at_lazy (field_type, address + byte_offset);
76a01679
JB
7469 }
7470 }
7471
03ee6b2e
PH
7472 if (v != NULL || no_err)
7473 return v;
7474 else
323e0a4a 7475 error (_("There is no member named %s."), name);
14f9c5c9 7476
03ee6b2e
PH
7477 BadValue:
7478 if (no_err)
7479 return NULL;
7480 else
0963b4bd
MS
7481 error (_("Attempt to extract a component of "
7482 "a value that is not a record."));
14f9c5c9
AS
7483}
7484
7485/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7486 If DISPP is non-null, add its byte displacement from the beginning of a
7487 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7488 work for packed fields).
7489
7490 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7491 followed by "___".
14f9c5c9 7492
0963b4bd 7493 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7494 be a (pointer or reference)+ to a struct or union, and the
7495 ultimate target type will be searched.
14f9c5c9
AS
7496
7497 Looks recursively into variant clauses and parent types.
7498
4c4b4cd2
PH
7499 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7500 TYPE is not a type of the right kind. */
14f9c5c9 7501
4c4b4cd2 7502static struct type *
76a01679
JB
7503ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
7504 int noerr, int *dispp)
14f9c5c9
AS
7505{
7506 int i;
7507
7508 if (name == NULL)
7509 goto BadName;
7510
76a01679 7511 if (refok && type != NULL)
4c4b4cd2
PH
7512 while (1)
7513 {
61ee279c 7514 type = ada_check_typedef (type);
76a01679
JB
7515 if (TYPE_CODE (type) != TYPE_CODE_PTR
7516 && TYPE_CODE (type) != TYPE_CODE_REF)
7517 break;
7518 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 7519 }
14f9c5c9 7520
76a01679 7521 if (type == NULL
1265e4aa
JB
7522 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
7523 && TYPE_CODE (type) != TYPE_CODE_UNION))
14f9c5c9 7524 {
4c4b4cd2 7525 if (noerr)
76a01679 7526 return NULL;
4c4b4cd2 7527 else
76a01679
JB
7528 {
7529 target_terminal_ours ();
7530 gdb_flush (gdb_stdout);
323e0a4a
AC
7531 if (type == NULL)
7532 error (_("Type (null) is not a structure or union type"));
7533 else
7534 {
7535 /* XXX: type_sprint */
7536 fprintf_unfiltered (gdb_stderr, _("Type "));
7537 type_print (type, "", gdb_stderr, -1);
7538 error (_(" is not a structure or union type"));
7539 }
76a01679 7540 }
14f9c5c9
AS
7541 }
7542
7543 type = to_static_fixed_type (type);
7544
7545 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
7546 {
0d5cff50 7547 const char *t_field_name = TYPE_FIELD_NAME (type, i);
14f9c5c9
AS
7548 struct type *t;
7549 int disp;
d2e4a39e 7550
14f9c5c9 7551 if (t_field_name == NULL)
4c4b4cd2 7552 continue;
14f9c5c9
AS
7553
7554 else if (field_name_match (t_field_name, name))
4c4b4cd2
PH
7555 {
7556 if (dispp != NULL)
7557 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
460efde1 7558 return TYPE_FIELD_TYPE (type, i);
4c4b4cd2 7559 }
14f9c5c9
AS
7560
7561 else if (ada_is_wrapper_field (type, i))
4c4b4cd2
PH
7562 {
7563 disp = 0;
7564 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
7565 0, 1, &disp);
7566 if (t != NULL)
7567 {
7568 if (dispp != NULL)
7569 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
7570 return t;
7571 }
7572 }
14f9c5c9
AS
7573
7574 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
7575 {
7576 int j;
5b4ee69b
MS
7577 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
7578 i));
4c4b4cd2
PH
7579
7580 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
7581 {
b1f33ddd
JB
7582 /* FIXME pnh 2008/01/26: We check for a field that is
7583 NOT wrapped in a struct, since the compiler sometimes
7584 generates these for unchecked variant types. Revisit
0963b4bd 7585 if the compiler changes this practice. */
0d5cff50 7586 const char *v_field_name = TYPE_FIELD_NAME (field_type, j);
4c4b4cd2 7587 disp = 0;
b1f33ddd
JB
7588 if (v_field_name != NULL
7589 && field_name_match (v_field_name, name))
460efde1 7590 t = TYPE_FIELD_TYPE (field_type, j);
b1f33ddd 7591 else
0963b4bd
MS
7592 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type,
7593 j),
b1f33ddd
JB
7594 name, 0, 1, &disp);
7595
4c4b4cd2
PH
7596 if (t != NULL)
7597 {
7598 if (dispp != NULL)
7599 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
7600 return t;
7601 }
7602 }
7603 }
14f9c5c9
AS
7604
7605 }
7606
7607BadName:
d2e4a39e 7608 if (!noerr)
14f9c5c9
AS
7609 {
7610 target_terminal_ours ();
7611 gdb_flush (gdb_stdout);
323e0a4a
AC
7612 if (name == NULL)
7613 {
7614 /* XXX: type_sprint */
7615 fprintf_unfiltered (gdb_stderr, _("Type "));
7616 type_print (type, "", gdb_stderr, -1);
7617 error (_(" has no component named <null>"));
7618 }
7619 else
7620 {
7621 /* XXX: type_sprint */
7622 fprintf_unfiltered (gdb_stderr, _("Type "));
7623 type_print (type, "", gdb_stderr, -1);
7624 error (_(" has no component named %s"), name);
7625 }
14f9c5c9
AS
7626 }
7627
7628 return NULL;
7629}
7630
b1f33ddd
JB
7631/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7632 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7633 represents an unchecked union (that is, the variant part of a
0963b4bd 7634 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7635
7636static int
7637is_unchecked_variant (struct type *var_type, struct type *outer_type)
7638{
7639 char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7640
b1f33ddd
JB
7641 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1, NULL)
7642 == NULL);
7643}
7644
7645
14f9c5c9
AS
7646/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7647 within a value of type OUTER_TYPE that is stored in GDB at
4c4b4cd2
PH
7648 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
7649 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7650
d2e4a39e 7651int
ebf56fd3 7652ada_which_variant_applies (struct type *var_type, struct type *outer_type,
fc1a4b47 7653 const gdb_byte *outer_valaddr)
14f9c5c9
AS
7654{
7655 int others_clause;
7656 int i;
d2e4a39e 7657 char *discrim_name = ada_variant_discrim_name (var_type);
0c281816
JB
7658 struct value *outer;
7659 struct value *discrim;
14f9c5c9
AS
7660 LONGEST discrim_val;
7661
012370f6
TT
7662 /* Using plain value_from_contents_and_address here causes problems
7663 because we will end up trying to resolve a type that is currently
7664 being constructed. */
7665 outer = value_from_contents_and_address_unresolved (outer_type,
7666 outer_valaddr, 0);
0c281816
JB
7667 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7668 if (discrim == NULL)
14f9c5c9 7669 return -1;
0c281816 7670 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7671
7672 others_clause = -1;
7673 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
7674 {
7675 if (ada_is_others_clause (var_type, i))
4c4b4cd2 7676 others_clause = i;
14f9c5c9 7677 else if (ada_in_variant (discrim_val, var_type, i))
4c4b4cd2 7678 return i;
14f9c5c9
AS
7679 }
7680
7681 return others_clause;
7682}
d2e4a39e 7683\f
14f9c5c9
AS
7684
7685
4c4b4cd2 7686 /* Dynamic-Sized Records */
14f9c5c9
AS
7687
7688/* Strategy: The type ostensibly attached to a value with dynamic size
7689 (i.e., a size that is not statically recorded in the debugging
7690 data) does not accurately reflect the size or layout of the value.
7691 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7692 conventional types that are constructed on the fly. */
14f9c5c9
AS
7693
7694/* There is a subtle and tricky problem here. In general, we cannot
7695 determine the size of dynamic records without its data. However,
7696 the 'struct value' data structure, which GDB uses to represent
7697 quantities in the inferior process (the target), requires the size
7698 of the type at the time of its allocation in order to reserve space
7699 for GDB's internal copy of the data. That's why the
7700 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7701 rather than struct value*s.
14f9c5c9
AS
7702
7703 However, GDB's internal history variables ($1, $2, etc.) are
7704 struct value*s containing internal copies of the data that are not, in
7705 general, the same as the data at their corresponding addresses in
7706 the target. Fortunately, the types we give to these values are all
7707 conventional, fixed-size types (as per the strategy described
7708 above), so that we don't usually have to perform the
7709 'to_fixed_xxx_type' conversions to look at their values.
7710 Unfortunately, there is one exception: if one of the internal
7711 history variables is an array whose elements are unconstrained
7712 records, then we will need to create distinct fixed types for each
7713 element selected. */
7714
7715/* The upshot of all of this is that many routines take a (type, host
7716 address, target address) triple as arguments to represent a value.
7717 The host address, if non-null, is supposed to contain an internal
7718 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7719 target at the target address. */
14f9c5c9
AS
7720
7721/* Assuming that VAL0 represents a pointer value, the result of
7722 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7723 dynamic-sized types. */
14f9c5c9 7724
d2e4a39e
AS
7725struct value *
7726ada_value_ind (struct value *val0)
14f9c5c9 7727{
c48db5ca 7728 struct value *val = value_ind (val0);
5b4ee69b 7729
b50d69b5
JG
7730 if (ada_is_tagged_type (value_type (val), 0))
7731 val = ada_tag_value_at_base_address (val);
7732
4c4b4cd2 7733 return ada_to_fixed_value (val);
14f9c5c9
AS
7734}
7735
7736/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7737 qualifiers on VAL0. */
7738
d2e4a39e
AS
7739static struct value *
7740ada_coerce_ref (struct value *val0)
7741{
df407dfe 7742 if (TYPE_CODE (value_type (val0)) == TYPE_CODE_REF)
d2e4a39e
AS
7743 {
7744 struct value *val = val0;
5b4ee69b 7745
994b9211 7746 val = coerce_ref (val);
b50d69b5
JG
7747
7748 if (ada_is_tagged_type (value_type (val), 0))
7749 val = ada_tag_value_at_base_address (val);
7750
4c4b4cd2 7751 return ada_to_fixed_value (val);
d2e4a39e
AS
7752 }
7753 else
14f9c5c9
AS
7754 return val0;
7755}
7756
7757/* Return OFF rounded upward if necessary to a multiple of
4c4b4cd2 7758 ALIGNMENT (a power of 2). */
14f9c5c9
AS
7759
7760static unsigned int
ebf56fd3 7761align_value (unsigned int off, unsigned int alignment)
14f9c5c9
AS
7762{
7763 return (off + alignment - 1) & ~(alignment - 1);
7764}
7765
4c4b4cd2 7766/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7767
7768static unsigned int
ebf56fd3 7769field_alignment (struct type *type, int f)
14f9c5c9 7770{
d2e4a39e 7771 const char *name = TYPE_FIELD_NAME (type, f);
64a1bf19 7772 int len;
14f9c5c9
AS
7773 int align_offset;
7774
64a1bf19
JB
7775 /* The field name should never be null, unless the debugging information
7776 is somehow malformed. In this case, we assume the field does not
7777 require any alignment. */
7778 if (name == NULL)
7779 return 1;
7780
7781 len = strlen (name);
7782
4c4b4cd2
PH
7783 if (!isdigit (name[len - 1]))
7784 return 1;
14f9c5c9 7785
d2e4a39e 7786 if (isdigit (name[len - 2]))
14f9c5c9
AS
7787 align_offset = len - 2;
7788 else
7789 align_offset = len - 1;
7790
61012eef 7791 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7792 return TARGET_CHAR_BIT;
7793
4c4b4cd2
PH
7794 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7795}
7796
852dff6c 7797/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7798
852dff6c
JB
7799static struct symbol *
7800ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7801{
7802 struct symbol *sym;
7803
7804 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
4186eb54 7805 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4c4b4cd2
PH
7806 return sym;
7807
4186eb54
KS
7808 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7809 return sym;
14f9c5c9
AS
7810}
7811
dddfab26
UW
7812/* Find a type named NAME. Ignores ambiguity. This routine will look
7813 solely for types defined by debug info, it will not search the GDB
7814 primitive types. */
4c4b4cd2 7815
852dff6c 7816static struct type *
ebf56fd3 7817ada_find_any_type (const char *name)
14f9c5c9 7818{
852dff6c 7819 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7820
14f9c5c9 7821 if (sym != NULL)
dddfab26 7822 return SYMBOL_TYPE (sym);
14f9c5c9 7823
dddfab26 7824 return NULL;
14f9c5c9
AS
7825}
7826
739593e0
JB
7827/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7828 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7829 symbol, in which case it is returned. Otherwise, this looks for
7830 symbols whose name is that of NAME_SYM suffixed with "___XR".
7831 Return symbol if found, and NULL otherwise. */
4c4b4cd2
PH
7832
7833struct symbol *
270140bd 7834ada_find_renaming_symbol (struct symbol *name_sym, const struct block *block)
aeb5907d 7835{
739593e0 7836 const char *name = SYMBOL_LINKAGE_NAME (name_sym);
aeb5907d
JB
7837 struct symbol *sym;
7838
739593e0
JB
7839 if (strstr (name, "___XR") != NULL)
7840 return name_sym;
7841
aeb5907d
JB
7842 sym = find_old_style_renaming_symbol (name, block);
7843
7844 if (sym != NULL)
7845 return sym;
7846
0963b4bd 7847 /* Not right yet. FIXME pnh 7/20/2007. */
852dff6c 7848 sym = ada_find_any_type_symbol (name);
aeb5907d
JB
7849 if (sym != NULL && strstr (SYMBOL_LINKAGE_NAME (sym), "___XR") != NULL)
7850 return sym;
7851 else
7852 return NULL;
7853}
7854
7855static struct symbol *
270140bd 7856find_old_style_renaming_symbol (const char *name, const struct block *block)
4c4b4cd2 7857{
7f0df278 7858 const struct symbol *function_sym = block_linkage_function (block);
4c4b4cd2
PH
7859 char *rename;
7860
7861 if (function_sym != NULL)
7862 {
7863 /* If the symbol is defined inside a function, NAME is not fully
7864 qualified. This means we need to prepend the function name
7865 as well as adding the ``___XR'' suffix to build the name of
7866 the associated renaming symbol. */
0d5cff50 7867 const char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
529cad9c
PH
7868 /* Function names sometimes contain suffixes used
7869 for instance to qualify nested subprograms. When building
7870 the XR type name, we need to make sure that this suffix is
7871 not included. So do not include any suffix in the function
7872 name length below. */
69fadcdf 7873 int function_name_len = ada_name_prefix_len (function_name);
76a01679
JB
7874 const int rename_len = function_name_len + 2 /* "__" */
7875 + strlen (name) + 6 /* "___XR\0" */ ;
4c4b4cd2 7876
529cad9c 7877 /* Strip the suffix if necessary. */
69fadcdf
JB
7878 ada_remove_trailing_digits (function_name, &function_name_len);
7879 ada_remove_po_subprogram_suffix (function_name, &function_name_len);
7880 ada_remove_Xbn_suffix (function_name, &function_name_len);
529cad9c 7881
4c4b4cd2
PH
7882 /* Library-level functions are a special case, as GNAT adds
7883 a ``_ada_'' prefix to the function name to avoid namespace
aeb5907d 7884 pollution. However, the renaming symbols themselves do not
4c4b4cd2
PH
7885 have this prefix, so we need to skip this prefix if present. */
7886 if (function_name_len > 5 /* "_ada_" */
7887 && strstr (function_name, "_ada_") == function_name)
69fadcdf
JB
7888 {
7889 function_name += 5;
7890 function_name_len -= 5;
7891 }
4c4b4cd2
PH
7892
7893 rename = (char *) alloca (rename_len * sizeof (char));
69fadcdf
JB
7894 strncpy (rename, function_name, function_name_len);
7895 xsnprintf (rename + function_name_len, rename_len - function_name_len,
7896 "__%s___XR", name);
4c4b4cd2
PH
7897 }
7898 else
7899 {
7900 const int rename_len = strlen (name) + 6;
5b4ee69b 7901
4c4b4cd2 7902 rename = (char *) alloca (rename_len * sizeof (char));
88c15c34 7903 xsnprintf (rename, rename_len * sizeof (char), "%s___XR", name);
4c4b4cd2
PH
7904 }
7905
852dff6c 7906 return ada_find_any_type_symbol (rename);
4c4b4cd2
PH
7907}
7908
14f9c5c9 7909/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7910 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7911 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7912 otherwise return 0. */
7913
14f9c5c9 7914int
d2e4a39e 7915ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7916{
7917 if (type1 == NULL)
7918 return 1;
7919 else if (type0 == NULL)
7920 return 0;
7921 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
7922 return 1;
7923 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
7924 return 0;
4c4b4cd2
PH
7925 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
7926 return 1;
ad82864c 7927 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7928 return 1;
4c4b4cd2
PH
7929 else if (ada_is_array_descriptor_type (type0)
7930 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7931 return 1;
aeb5907d
JB
7932 else
7933 {
7934 const char *type0_name = type_name_no_tag (type0);
7935 const char *type1_name = type_name_no_tag (type1);
7936
7937 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7938 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7939 return 1;
7940 }
14f9c5c9
AS
7941 return 0;
7942}
7943
7944/* The name of TYPE, which is either its TYPE_NAME, or, if that is
4c4b4cd2
PH
7945 null, its TYPE_TAG_NAME. Null if TYPE is null. */
7946
0d5cff50 7947const char *
d2e4a39e 7948ada_type_name (struct type *type)
14f9c5c9 7949{
d2e4a39e 7950 if (type == NULL)
14f9c5c9
AS
7951 return NULL;
7952 else if (TYPE_NAME (type) != NULL)
7953 return TYPE_NAME (type);
7954 else
7955 return TYPE_TAG_NAME (type);
7956}
7957
b4ba55a1
JB
7958/* Search the list of "descriptive" types associated to TYPE for a type
7959 whose name is NAME. */
7960
7961static struct type *
7962find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7963{
931e5bc3 7964 struct type *result, *tmp;
b4ba55a1 7965
c6044dd1
JB
7966 if (ada_ignore_descriptive_types_p)
7967 return NULL;
7968
b4ba55a1
JB
7969 /* If there no descriptive-type info, then there is no parallel type
7970 to be found. */
7971 if (!HAVE_GNAT_AUX_INFO (type))
7972 return NULL;
7973
7974 result = TYPE_DESCRIPTIVE_TYPE (type);
7975 while (result != NULL)
7976 {
0d5cff50 7977 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7978
7979 if (result_name == NULL)
7980 {
7981 warning (_("unexpected null name on descriptive type"));
7982 return NULL;
7983 }
7984
7985 /* If the names match, stop. */
7986 if (strcmp (result_name, name) == 0)
7987 break;
7988
7989 /* Otherwise, look at the next item on the list, if any. */
7990 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7991 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7992 else
7993 tmp = NULL;
7994
7995 /* If not found either, try after having resolved the typedef. */
7996 if (tmp != NULL)
7997 result = tmp;
b4ba55a1 7998 else
931e5bc3 7999 {
f168693b 8000 result = check_typedef (result);
931e5bc3
JG
8001 if (HAVE_GNAT_AUX_INFO (result))
8002 result = TYPE_DESCRIPTIVE_TYPE (result);
8003 else
8004 result = NULL;
8005 }
b4ba55a1
JB
8006 }
8007
8008 /* If we didn't find a match, see whether this is a packed array. With
8009 older compilers, the descriptive type information is either absent or
8010 irrelevant when it comes to packed arrays so the above lookup fails.
8011 Fall back to using a parallel lookup by name in this case. */
12ab9e09 8012 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
8013 return ada_find_any_type (name);
8014
8015 return result;
8016}
8017
8018/* Find a parallel type to TYPE with the specified NAME, using the
8019 descriptive type taken from the debugging information, if available,
8020 and otherwise using the (slower) name-based method. */
8021
8022static struct type *
8023ada_find_parallel_type_with_name (struct type *type, const char *name)
8024{
8025 struct type *result = NULL;
8026
8027 if (HAVE_GNAT_AUX_INFO (type))
8028 result = find_parallel_type_by_descriptive_type (type, name);
8029 else
8030 result = ada_find_any_type (name);
8031
8032 return result;
8033}
8034
8035/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 8036 SUFFIX to the name of TYPE. */
14f9c5c9 8037
d2e4a39e 8038struct type *
ebf56fd3 8039ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 8040{
0d5cff50 8041 char *name;
fe978cb0 8042 const char *type_name = ada_type_name (type);
14f9c5c9 8043 int len;
d2e4a39e 8044
fe978cb0 8045 if (type_name == NULL)
14f9c5c9
AS
8046 return NULL;
8047
fe978cb0 8048 len = strlen (type_name);
14f9c5c9 8049
b4ba55a1 8050 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 8051
fe978cb0 8052 strcpy (name, type_name);
14f9c5c9
AS
8053 strcpy (name + len, suffix);
8054
b4ba55a1 8055 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
8056}
8057
14f9c5c9 8058/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 8059 type describing its fields. Otherwise, return NULL. */
14f9c5c9 8060
d2e4a39e
AS
8061static struct type *
8062dynamic_template_type (struct type *type)
14f9c5c9 8063{
61ee279c 8064 type = ada_check_typedef (type);
14f9c5c9
AS
8065
8066 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
d2e4a39e 8067 || ada_type_name (type) == NULL)
14f9c5c9 8068 return NULL;
d2e4a39e 8069 else
14f9c5c9
AS
8070 {
8071 int len = strlen (ada_type_name (type));
5b4ee69b 8072
4c4b4cd2
PH
8073 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
8074 return type;
14f9c5c9 8075 else
4c4b4cd2 8076 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
8077 }
8078}
8079
8080/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 8081 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 8082
d2e4a39e
AS
8083static int
8084is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
8085{
8086 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
5b4ee69b 8087
d2e4a39e 8088 return name != NULL
14f9c5c9
AS
8089 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
8090 && strstr (name, "___XVL") != NULL;
8091}
8092
4c4b4cd2
PH
8093/* The index of the variant field of TYPE, or -1 if TYPE does not
8094 represent a variant record type. */
14f9c5c9 8095
d2e4a39e 8096static int
4c4b4cd2 8097variant_field_index (struct type *type)
14f9c5c9
AS
8098{
8099 int f;
8100
4c4b4cd2
PH
8101 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
8102 return -1;
8103
8104 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
8105 {
8106 if (ada_is_variant_part (type, f))
8107 return f;
8108 }
8109 return -1;
14f9c5c9
AS
8110}
8111
4c4b4cd2
PH
8112/* A record type with no fields. */
8113
d2e4a39e 8114static struct type *
fe978cb0 8115empty_record (struct type *templ)
14f9c5c9 8116{
fe978cb0 8117 struct type *type = alloc_type_copy (templ);
5b4ee69b 8118
14f9c5c9
AS
8119 TYPE_CODE (type) = TYPE_CODE_STRUCT;
8120 TYPE_NFIELDS (type) = 0;
8121 TYPE_FIELDS (type) = NULL;
b1f33ddd 8122 INIT_CPLUS_SPECIFIC (type);
14f9c5c9
AS
8123 TYPE_NAME (type) = "<empty>";
8124 TYPE_TAG_NAME (type) = NULL;
14f9c5c9
AS
8125 TYPE_LENGTH (type) = 0;
8126 return type;
8127}
8128
8129/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
8130 the value of type TYPE at VALADDR or ADDRESS (see comments at
8131 the beginning of this section) VAL according to GNAT conventions.
8132 DVAL0 should describe the (portion of a) record that contains any
df407dfe 8133 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
8134 an outer-level type (i.e., as opposed to a branch of a variant.) A
8135 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 8136 of the variant.
14f9c5c9 8137
4c4b4cd2
PH
8138 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
8139 length are not statically known are discarded. As a consequence,
8140 VALADDR, ADDRESS and DVAL0 are ignored.
8141
8142 NOTE: Limitations: For now, we assume that dynamic fields and
8143 variants occupy whole numbers of bytes. However, they need not be
8144 byte-aligned. */
8145
8146struct type *
10a2c479 8147ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 8148 const gdb_byte *valaddr,
4c4b4cd2
PH
8149 CORE_ADDR address, struct value *dval0,
8150 int keep_dynamic_fields)
14f9c5c9 8151{
d2e4a39e
AS
8152 struct value *mark = value_mark ();
8153 struct value *dval;
8154 struct type *rtype;
14f9c5c9 8155 int nfields, bit_len;
4c4b4cd2 8156 int variant_field;
14f9c5c9 8157 long off;
d94e4f4f 8158 int fld_bit_len;
14f9c5c9
AS
8159 int f;
8160
4c4b4cd2
PH
8161 /* Compute the number of fields in this record type that are going
8162 to be processed: unless keep_dynamic_fields, this includes only
8163 fields whose position and length are static will be processed. */
8164 if (keep_dynamic_fields)
8165 nfields = TYPE_NFIELDS (type);
8166 else
8167 {
8168 nfields = 0;
76a01679 8169 while (nfields < TYPE_NFIELDS (type)
4c4b4cd2
PH
8170 && !ada_is_variant_part (type, nfields)
8171 && !is_dynamic_field (type, nfields))
8172 nfields++;
8173 }
8174
e9bb382b 8175 rtype = alloc_type_copy (type);
14f9c5c9
AS
8176 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
8177 INIT_CPLUS_SPECIFIC (rtype);
8178 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e 8179 TYPE_FIELDS (rtype) = (struct field *)
14f9c5c9
AS
8180 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
8181 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
8182 TYPE_NAME (rtype) = ada_type_name (type);
8183 TYPE_TAG_NAME (rtype) = NULL;
876cecd0 8184 TYPE_FIXED_INSTANCE (rtype) = 1;
14f9c5c9 8185
d2e4a39e
AS
8186 off = 0;
8187 bit_len = 0;
4c4b4cd2
PH
8188 variant_field = -1;
8189
14f9c5c9
AS
8190 for (f = 0; f < nfields; f += 1)
8191 {
6c038f32
PH
8192 off = align_value (off, field_alignment (type, f))
8193 + TYPE_FIELD_BITPOS (type, f);
945b3a32 8194 SET_FIELD_BITPOS (TYPE_FIELD (rtype, f), off);
d2e4a39e 8195 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 8196
d2e4a39e 8197 if (ada_is_variant_part (type, f))
4c4b4cd2
PH
8198 {
8199 variant_field = f;
d94e4f4f 8200 fld_bit_len = 0;
4c4b4cd2 8201 }
14f9c5c9 8202 else if (is_dynamic_field (type, f))
4c4b4cd2 8203 {
284614f0
JB
8204 const gdb_byte *field_valaddr = valaddr;
8205 CORE_ADDR field_address = address;
8206 struct type *field_type =
8207 TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f));
8208
4c4b4cd2 8209 if (dval0 == NULL)
b5304971
JG
8210 {
8211 /* rtype's length is computed based on the run-time
8212 value of discriminants. If the discriminants are not
8213 initialized, the type size may be completely bogus and
0963b4bd 8214 GDB may fail to allocate a value for it. So check the
b5304971 8215 size first before creating the value. */
c1b5a1a6 8216 ada_ensure_varsize_limit (rtype);
012370f6
TT
8217 /* Using plain value_from_contents_and_address here
8218 causes problems because we will end up trying to
8219 resolve a type that is currently being
8220 constructed. */
8221 dval = value_from_contents_and_address_unresolved (rtype,
8222 valaddr,
8223 address);
9f1f738a 8224 rtype = value_type (dval);
b5304971 8225 }
4c4b4cd2
PH
8226 else
8227 dval = dval0;
8228
284614f0
JB
8229 /* If the type referenced by this field is an aligner type, we need
8230 to unwrap that aligner type, because its size might not be set.
8231 Keeping the aligner type would cause us to compute the wrong
8232 size for this field, impacting the offset of the all the fields
8233 that follow this one. */
8234 if (ada_is_aligner_type (field_type))
8235 {
8236 long field_offset = TYPE_FIELD_BITPOS (field_type, f);
8237
8238 field_valaddr = cond_offset_host (field_valaddr, field_offset);
8239 field_address = cond_offset_target (field_address, field_offset);
8240 field_type = ada_aligned_type (field_type);
8241 }
8242
8243 field_valaddr = cond_offset_host (field_valaddr,
8244 off / TARGET_CHAR_BIT);
8245 field_address = cond_offset_target (field_address,
8246 off / TARGET_CHAR_BIT);
8247
8248 /* Get the fixed type of the field. Note that, in this case,
8249 we do not want to get the real type out of the tag: if
8250 the current field is the parent part of a tagged record,
8251 we will get the tag of the object. Clearly wrong: the real
8252 type of the parent is not the real type of the child. We
8253 would end up in an infinite loop. */
8254 field_type = ada_get_base_type (field_type);
8255 field_type = ada_to_fixed_type (field_type, field_valaddr,
8256 field_address, dval, 0);
27f2a97b
JB
8257 /* If the field size is already larger than the maximum
8258 object size, then the record itself will necessarily
8259 be larger than the maximum object size. We need to make
8260 this check now, because the size might be so ridiculously
8261 large (due to an uninitialized variable in the inferior)
8262 that it would cause an overflow when adding it to the
8263 record size. */
c1b5a1a6 8264 ada_ensure_varsize_limit (field_type);
284614f0
JB
8265
8266 TYPE_FIELD_TYPE (rtype, f) = field_type;
4c4b4cd2 8267 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
27f2a97b
JB
8268 /* The multiplication can potentially overflow. But because
8269 the field length has been size-checked just above, and
8270 assuming that the maximum size is a reasonable value,
8271 an overflow should not happen in practice. So rather than
8272 adding overflow recovery code to this already complex code,
8273 we just assume that it's not going to happen. */
d94e4f4f 8274 fld_bit_len =
4c4b4cd2
PH
8275 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
8276 }
14f9c5c9 8277 else
4c4b4cd2 8278 {
5ded5331
JB
8279 /* Note: If this field's type is a typedef, it is important
8280 to preserve the typedef layer.
8281
8282 Otherwise, we might be transforming a typedef to a fat
8283 pointer (encoding a pointer to an unconstrained array),
8284 into a basic fat pointer (encoding an unconstrained
8285 array). As both types are implemented using the same
8286 structure, the typedef is the only clue which allows us
8287 to distinguish between the two options. Stripping it
8288 would prevent us from printing this field appropriately. */
8289 TYPE_FIELD_TYPE (rtype, f) = TYPE_FIELD_TYPE (type, f);
4c4b4cd2
PH
8290 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
8291 if (TYPE_FIELD_BITSIZE (type, f) > 0)
d94e4f4f 8292 fld_bit_len =
4c4b4cd2
PH
8293 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
8294 else
5ded5331
JB
8295 {
8296 struct type *field_type = TYPE_FIELD_TYPE (type, f);
8297
8298 /* We need to be careful of typedefs when computing
8299 the length of our field. If this is a typedef,
8300 get the length of the target type, not the length
8301 of the typedef. */
8302 if (TYPE_CODE (field_type) == TYPE_CODE_TYPEDEF)
8303 field_type = ada_typedef_target_type (field_type);
8304
8305 fld_bit_len =
8306 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
8307 }
4c4b4cd2 8308 }
14f9c5c9 8309 if (off + fld_bit_len > bit_len)
4c4b4cd2 8310 bit_len = off + fld_bit_len;
d94e4f4f 8311 off += fld_bit_len;
4c4b4cd2
PH
8312 TYPE_LENGTH (rtype) =
8313 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 8314 }
4c4b4cd2
PH
8315
8316 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 8317 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
8318 the record. This can happen in the presence of representation
8319 clauses. */
8320 if (variant_field >= 0)
8321 {
8322 struct type *branch_type;
8323
8324 off = TYPE_FIELD_BITPOS (rtype, variant_field);
8325
8326 if (dval0 == NULL)
9f1f738a 8327 {
012370f6
TT
8328 /* Using plain value_from_contents_and_address here causes
8329 problems because we will end up trying to resolve a type
8330 that is currently being constructed. */
8331 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
8332 address);
9f1f738a
SA
8333 rtype = value_type (dval);
8334 }
4c4b4cd2
PH
8335 else
8336 dval = dval0;
8337
8338 branch_type =
8339 to_fixed_variant_branch_type
8340 (TYPE_FIELD_TYPE (type, variant_field),
8341 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
8342 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
8343 if (branch_type == NULL)
8344 {
8345 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
8346 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
8347 TYPE_NFIELDS (rtype) -= 1;
8348 }
8349 else
8350 {
8351 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
8352 TYPE_FIELD_NAME (rtype, variant_field) = "S";
8353 fld_bit_len =
8354 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
8355 TARGET_CHAR_BIT;
8356 if (off + fld_bit_len > bit_len)
8357 bit_len = off + fld_bit_len;
8358 TYPE_LENGTH (rtype) =
8359 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
8360 }
8361 }
8362
714e53ab
PH
8363 /* According to exp_dbug.ads, the size of TYPE for variable-size records
8364 should contain the alignment of that record, which should be a strictly
8365 positive value. If null or negative, then something is wrong, most
8366 probably in the debug info. In that case, we don't round up the size
0963b4bd 8367 of the resulting type. If this record is not part of another structure,
714e53ab
PH
8368 the current RTYPE length might be good enough for our purposes. */
8369 if (TYPE_LENGTH (type) <= 0)
8370 {
323e0a4a
AC
8371 if (TYPE_NAME (rtype))
8372 warning (_("Invalid type size for `%s' detected: %d."),
8373 TYPE_NAME (rtype), TYPE_LENGTH (type));
8374 else
8375 warning (_("Invalid type size for <unnamed> detected: %d."),
8376 TYPE_LENGTH (type));
714e53ab
PH
8377 }
8378 else
8379 {
8380 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype),
8381 TYPE_LENGTH (type));
8382 }
14f9c5c9
AS
8383
8384 value_free_to_mark (mark);
d2e4a39e 8385 if (TYPE_LENGTH (rtype) > varsize_limit)
323e0a4a 8386 error (_("record type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
8387 return rtype;
8388}
8389
4c4b4cd2
PH
8390/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8391 of 1. */
14f9c5c9 8392
d2e4a39e 8393static struct type *
fc1a4b47 8394template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
4c4b4cd2
PH
8395 CORE_ADDR address, struct value *dval0)
8396{
8397 return ada_template_to_fixed_record_type_1 (type, valaddr,
8398 address, dval0, 1);
8399}
8400
8401/* An ordinary record type in which ___XVL-convention fields and
8402 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8403 static approximations, containing all possible fields. Uses
8404 no runtime values. Useless for use in values, but that's OK,
8405 since the results are used only for type determinations. Works on both
8406 structs and unions. Representation note: to save space, we memorize
8407 the result of this function in the TYPE_TARGET_TYPE of the
8408 template type. */
8409
8410static struct type *
8411template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8412{
8413 struct type *type;
8414 int nfields;
8415 int f;
8416
9e195661
PMR
8417 /* No need no do anything if the input type is already fixed. */
8418 if (TYPE_FIXED_INSTANCE (type0))
8419 return type0;
8420
8421 /* Likewise if we already have computed the static approximation. */
4c4b4cd2
PH
8422 if (TYPE_TARGET_TYPE (type0) != NULL)
8423 return TYPE_TARGET_TYPE (type0);
8424
9e195661 8425 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8426 type = type0;
9e195661
PMR
8427 nfields = TYPE_NFIELDS (type0);
8428
8429 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8430 recompute all over next time. */
8431 TYPE_TARGET_TYPE (type0) = type;
14f9c5c9
AS
8432
8433 for (f = 0; f < nfields; f += 1)
8434 {
460efde1 8435 struct type *field_type = TYPE_FIELD_TYPE (type0, f);
4c4b4cd2 8436 struct type *new_type;
14f9c5c9 8437
4c4b4cd2 8438 if (is_dynamic_field (type0, f))
460efde1
JB
8439 {
8440 field_type = ada_check_typedef (field_type);
8441 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
8442 }
14f9c5c9 8443 else
f192137b 8444 new_type = static_unwrap_type (field_type);
9e195661
PMR
8445
8446 if (new_type != field_type)
8447 {
8448 /* Clone TYPE0 only the first time we get a new field type. */
8449 if (type == type0)
8450 {
8451 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
8452 TYPE_CODE (type) = TYPE_CODE (type0);
8453 INIT_CPLUS_SPECIFIC (type);
8454 TYPE_NFIELDS (type) = nfields;
8455 TYPE_FIELDS (type) = (struct field *)
8456 TYPE_ALLOC (type, nfields * sizeof (struct field));
8457 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
8458 sizeof (struct field) * nfields);
8459 TYPE_NAME (type) = ada_type_name (type0);
8460 TYPE_TAG_NAME (type) = NULL;
8461 TYPE_FIXED_INSTANCE (type) = 1;
8462 TYPE_LENGTH (type) = 0;
8463 }
8464 TYPE_FIELD_TYPE (type, f) = new_type;
8465 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
8466 }
14f9c5c9 8467 }
9e195661 8468
14f9c5c9
AS
8469 return type;
8470}
8471
4c4b4cd2 8472/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8473 whose address in memory is ADDRESS, returns a revision of TYPE,
8474 which should be a non-dynamic-sized record, in which the variant
8475 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8476 for discriminant values in DVAL0, which can be NULL if the record
8477 contains the necessary discriminant values. */
8478
d2e4a39e 8479static struct type *
fc1a4b47 8480to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
4c4b4cd2 8481 CORE_ADDR address, struct value *dval0)
14f9c5c9 8482{
d2e4a39e 8483 struct value *mark = value_mark ();
4c4b4cd2 8484 struct value *dval;
d2e4a39e 8485 struct type *rtype;
14f9c5c9
AS
8486 struct type *branch_type;
8487 int nfields = TYPE_NFIELDS (type);
4c4b4cd2 8488 int variant_field = variant_field_index (type);
14f9c5c9 8489
4c4b4cd2 8490 if (variant_field == -1)
14f9c5c9
AS
8491 return type;
8492
4c4b4cd2 8493 if (dval0 == NULL)
9f1f738a
SA
8494 {
8495 dval = value_from_contents_and_address (type, valaddr, address);
8496 type = value_type (dval);
8497 }
4c4b4cd2
PH
8498 else
8499 dval = dval0;
8500
e9bb382b 8501 rtype = alloc_type_copy (type);
14f9c5c9 8502 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
4c4b4cd2
PH
8503 INIT_CPLUS_SPECIFIC (rtype);
8504 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e
AS
8505 TYPE_FIELDS (rtype) =
8506 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
8507 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
4c4b4cd2 8508 sizeof (struct field) * nfields);
14f9c5c9
AS
8509 TYPE_NAME (rtype) = ada_type_name (type);
8510 TYPE_TAG_NAME (rtype) = NULL;
876cecd0 8511 TYPE_FIXED_INSTANCE (rtype) = 1;
14f9c5c9
AS
8512 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
8513
4c4b4cd2
PH
8514 branch_type = to_fixed_variant_branch_type
8515 (TYPE_FIELD_TYPE (type, variant_field),
d2e4a39e 8516 cond_offset_host (valaddr,
4c4b4cd2
PH
8517 TYPE_FIELD_BITPOS (type, variant_field)
8518 / TARGET_CHAR_BIT),
d2e4a39e 8519 cond_offset_target (address,
4c4b4cd2
PH
8520 TYPE_FIELD_BITPOS (type, variant_field)
8521 / TARGET_CHAR_BIT), dval);
d2e4a39e 8522 if (branch_type == NULL)
14f9c5c9 8523 {
4c4b4cd2 8524 int f;
5b4ee69b 8525
4c4b4cd2
PH
8526 for (f = variant_field + 1; f < nfields; f += 1)
8527 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
14f9c5c9 8528 TYPE_NFIELDS (rtype) -= 1;
14f9c5c9
AS
8529 }
8530 else
8531 {
4c4b4cd2
PH
8532 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
8533 TYPE_FIELD_NAME (rtype, variant_field) = "S";
8534 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 8535 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 8536 }
4c4b4cd2 8537 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
d2e4a39e 8538
4c4b4cd2 8539 value_free_to_mark (mark);
14f9c5c9
AS
8540 return rtype;
8541}
8542
8543/* An ordinary record type (with fixed-length fields) that describes
8544 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8545 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8546 should be in DVAL, a record value; it may be NULL if the object
8547 at ADDR itself contains any necessary discriminant values.
8548 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8549 values from the record are needed. Except in the case that DVAL,
8550 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8551 unchecked) is replaced by a particular branch of the variant.
8552
8553 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8554 is questionable and may be removed. It can arise during the
8555 processing of an unconstrained-array-of-record type where all the
8556 variant branches have exactly the same size. This is because in
8557 such cases, the compiler does not bother to use the XVS convention
8558 when encoding the record. I am currently dubious of this
8559 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8560
d2e4a39e 8561static struct type *
fc1a4b47 8562to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
4c4b4cd2 8563 CORE_ADDR address, struct value *dval)
14f9c5c9 8564{
d2e4a39e 8565 struct type *templ_type;
14f9c5c9 8566
876cecd0 8567 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2
PH
8568 return type0;
8569
d2e4a39e 8570 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8571
8572 if (templ_type != NULL)
8573 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8574 else if (variant_field_index (type0) >= 0)
8575 {
8576 if (dval == NULL && valaddr == NULL && address == 0)
8577 return type0;
8578 return to_record_with_fixed_variant_part (type0, valaddr, address,
8579 dval);
8580 }
14f9c5c9
AS
8581 else
8582 {
876cecd0 8583 TYPE_FIXED_INSTANCE (type0) = 1;
14f9c5c9
AS
8584 return type0;
8585 }
8586
8587}
8588
8589/* An ordinary record type (with fixed-length fields) that describes
8590 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8591 union type. Any necessary discriminants' values should be in DVAL,
8592 a record value. That is, this routine selects the appropriate
8593 branch of the union at ADDR according to the discriminant value
b1f33ddd 8594 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8595 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8596
d2e4a39e 8597static struct type *
fc1a4b47 8598to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
4c4b4cd2 8599 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8600{
8601 int which;
d2e4a39e
AS
8602 struct type *templ_type;
8603 struct type *var_type;
14f9c5c9
AS
8604
8605 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
8606 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 8607 else
14f9c5c9
AS
8608 var_type = var_type0;
8609
8610 templ_type = ada_find_parallel_type (var_type, "___XVU");
8611
8612 if (templ_type != NULL)
8613 var_type = templ_type;
8614
b1f33ddd
JB
8615 if (is_unchecked_variant (var_type, value_type (dval)))
8616 return var_type0;
d2e4a39e
AS
8617 which =
8618 ada_which_variant_applies (var_type,
0fd88904 8619 value_type (dval), value_contents (dval));
14f9c5c9
AS
8620
8621 if (which < 0)
e9bb382b 8622 return empty_record (var_type);
14f9c5c9 8623 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8624 return to_fixed_record_type
d2e4a39e
AS
8625 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
8626 valaddr, address, dval);
4c4b4cd2 8627 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
d2e4a39e
AS
8628 return
8629 to_fixed_record_type
8630 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
14f9c5c9
AS
8631 else
8632 return TYPE_FIELD_TYPE (var_type, which);
8633}
8634
8908fca5
JB
8635/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8636 ENCODING_TYPE, a type following the GNAT conventions for discrete
8637 type encodings, only carries redundant information. */
8638
8639static int
8640ada_is_redundant_range_encoding (struct type *range_type,
8641 struct type *encoding_type)
8642{
8643 struct type *fixed_range_type;
108d56a4 8644 const char *bounds_str;
8908fca5
JB
8645 int n;
8646 LONGEST lo, hi;
8647
8648 gdb_assert (TYPE_CODE (range_type) == TYPE_CODE_RANGE);
8649
005e2509
JB
8650 if (TYPE_CODE (get_base_type (range_type))
8651 != TYPE_CODE (get_base_type (encoding_type)))
8652 {
8653 /* The compiler probably used a simple base type to describe
8654 the range type instead of the range's actual base type,
8655 expecting us to get the real base type from the encoding
8656 anyway. In this situation, the encoding cannot be ignored
8657 as redundant. */
8658 return 0;
8659 }
8660
8908fca5
JB
8661 if (is_dynamic_type (range_type))
8662 return 0;
8663
8664 if (TYPE_NAME (encoding_type) == NULL)
8665 return 0;
8666
8667 bounds_str = strstr (TYPE_NAME (encoding_type), "___XDLU_");
8668 if (bounds_str == NULL)
8669 return 0;
8670
8671 n = 8; /* Skip "___XDLU_". */
8672 if (!ada_scan_number (bounds_str, n, &lo, &n))
8673 return 0;
8674 if (TYPE_LOW_BOUND (range_type) != lo)
8675 return 0;
8676
8677 n += 2; /* Skip the "__" separator between the two bounds. */
8678 if (!ada_scan_number (bounds_str, n, &hi, &n))
8679 return 0;
8680 if (TYPE_HIGH_BOUND (range_type) != hi)
8681 return 0;
8682
8683 return 1;
8684}
8685
8686/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8687 a type following the GNAT encoding for describing array type
8688 indices, only carries redundant information. */
8689
8690static int
8691ada_is_redundant_index_type_desc (struct type *array_type,
8692 struct type *desc_type)
8693{
8694 struct type *this_layer = check_typedef (array_type);
8695 int i;
8696
8697 for (i = 0; i < TYPE_NFIELDS (desc_type); i++)
8698 {
8699 if (!ada_is_redundant_range_encoding (TYPE_INDEX_TYPE (this_layer),
8700 TYPE_FIELD_TYPE (desc_type, i)))
8701 return 0;
8702 this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
8703 }
8704
8705 return 1;
8706}
8707
14f9c5c9
AS
8708/* Assuming that TYPE0 is an array type describing the type of a value
8709 at ADDR, and that DVAL describes a record containing any
8710 discriminants used in TYPE0, returns a type for the value that
8711 contains no dynamic components (that is, no components whose sizes
8712 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8713 true, gives an error message if the resulting type's size is over
4c4b4cd2 8714 varsize_limit. */
14f9c5c9 8715
d2e4a39e
AS
8716static struct type *
8717to_fixed_array_type (struct type *type0, struct value *dval,
4c4b4cd2 8718 int ignore_too_big)
14f9c5c9 8719{
d2e4a39e
AS
8720 struct type *index_type_desc;
8721 struct type *result;
ad82864c 8722 int constrained_packed_array_p;
931e5bc3 8723 static const char *xa_suffix = "___XA";
14f9c5c9 8724
b0dd7688 8725 type0 = ada_check_typedef (type0);
284614f0 8726 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2 8727 return type0;
14f9c5c9 8728
ad82864c
JB
8729 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8730 if (constrained_packed_array_p)
8731 type0 = decode_constrained_packed_array_type (type0);
284614f0 8732
931e5bc3
JG
8733 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8734
8735 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8736 encoding suffixed with 'P' may still be generated. If so,
8737 it should be used to find the XA type. */
8738
8739 if (index_type_desc == NULL)
8740 {
1da0522e 8741 const char *type_name = ada_type_name (type0);
931e5bc3 8742
1da0522e 8743 if (type_name != NULL)
931e5bc3 8744 {
1da0522e 8745 const int len = strlen (type_name);
931e5bc3
JG
8746 char *name = (char *) alloca (len + strlen (xa_suffix));
8747
1da0522e 8748 if (type_name[len - 1] == 'P')
931e5bc3 8749 {
1da0522e 8750 strcpy (name, type_name);
931e5bc3
JG
8751 strcpy (name + len - 1, xa_suffix);
8752 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8753 }
8754 }
8755 }
8756
28c85d6c 8757 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8758 if (index_type_desc != NULL
8759 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8760 {
8761 /* Ignore this ___XA parallel type, as it does not bring any
8762 useful information. This allows us to avoid creating fixed
8763 versions of the array's index types, which would be identical
8764 to the original ones. This, in turn, can also help avoid
8765 the creation of fixed versions of the array itself. */
8766 index_type_desc = NULL;
8767 }
8768
14f9c5c9
AS
8769 if (index_type_desc == NULL)
8770 {
61ee279c 8771 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 8772
14f9c5c9 8773 /* NOTE: elt_type---the fixed version of elt_type0---should never
4c4b4cd2
PH
8774 depend on the contents of the array in properly constructed
8775 debugging data. */
529cad9c
PH
8776 /* Create a fixed version of the array element type.
8777 We're not providing the address of an element here,
e1d5a0d2 8778 and thus the actual object value cannot be inspected to do
529cad9c
PH
8779 the conversion. This should not be a problem, since arrays of
8780 unconstrained objects are not allowed. In particular, all
8781 the elements of an array of a tagged type should all be of
8782 the same type specified in the debugging info. No need to
8783 consult the object tag. */
1ed6ede0 8784 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8785
284614f0
JB
8786 /* Make sure we always create a new array type when dealing with
8787 packed array types, since we're going to fix-up the array
8788 type length and element bitsize a little further down. */
ad82864c 8789 if (elt_type0 == elt_type && !constrained_packed_array_p)
4c4b4cd2 8790 result = type0;
14f9c5c9 8791 else
e9bb382b 8792 result = create_array_type (alloc_type_copy (type0),
4c4b4cd2 8793 elt_type, TYPE_INDEX_TYPE (type0));
14f9c5c9
AS
8794 }
8795 else
8796 {
8797 int i;
8798 struct type *elt_type0;
8799
8800 elt_type0 = type0;
8801 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
4c4b4cd2 8802 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8803
8804 /* NOTE: result---the fixed version of elt_type0---should never
4c4b4cd2
PH
8805 depend on the contents of the array in properly constructed
8806 debugging data. */
529cad9c
PH
8807 /* Create a fixed version of the array element type.
8808 We're not providing the address of an element here,
e1d5a0d2 8809 and thus the actual object value cannot be inspected to do
529cad9c
PH
8810 the conversion. This should not be a problem, since arrays of
8811 unconstrained objects are not allowed. In particular, all
8812 the elements of an array of a tagged type should all be of
8813 the same type specified in the debugging info. No need to
8814 consult the object tag. */
1ed6ede0
JB
8815 result =
8816 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8817
8818 elt_type0 = type0;
14f9c5c9 8819 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
4c4b4cd2
PH
8820 {
8821 struct type *range_type =
28c85d6c 8822 to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, i), dval);
5b4ee69b 8823
e9bb382b 8824 result = create_array_type (alloc_type_copy (elt_type0),
4c4b4cd2 8825 result, range_type);
1ce677a4 8826 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
4c4b4cd2 8827 }
d2e4a39e 8828 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
323e0a4a 8829 error (_("array type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
8830 }
8831
2e6fda7d
JB
8832 /* We want to preserve the type name. This can be useful when
8833 trying to get the type name of a value that has already been
8834 printed (for instance, if the user did "print VAR; whatis $". */
8835 TYPE_NAME (result) = TYPE_NAME (type0);
8836
ad82864c 8837 if (constrained_packed_array_p)
284614f0
JB
8838 {
8839 /* So far, the resulting type has been created as if the original
8840 type was a regular (non-packed) array type. As a result, the
8841 bitsize of the array elements needs to be set again, and the array
8842 length needs to be recomputed based on that bitsize. */
8843 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
8844 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8845
8846 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
8847 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
8848 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
8849 TYPE_LENGTH (result)++;
8850 }
8851
876cecd0 8852 TYPE_FIXED_INSTANCE (result) = 1;
14f9c5c9 8853 return result;
d2e4a39e 8854}
14f9c5c9
AS
8855
8856
8857/* A standard type (containing no dynamically sized components)
8858 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8859 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8860 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8861 ADDRESS or in VALADDR contains these discriminants.
8862
1ed6ede0
JB
8863 If CHECK_TAG is not null, in the case of tagged types, this function
8864 attempts to locate the object's tag and use it to compute the actual
8865 type. However, when ADDRESS is null, we cannot use it to determine the
8866 location of the tag, and therefore compute the tagged type's actual type.
8867 So we return the tagged type without consulting the tag. */
529cad9c 8868
f192137b
JB
8869static struct type *
8870ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
1ed6ede0 8871 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8872{
61ee279c 8873 type = ada_check_typedef (type);
d2e4a39e
AS
8874 switch (TYPE_CODE (type))
8875 {
8876 default:
14f9c5c9 8877 return type;
d2e4a39e 8878 case TYPE_CODE_STRUCT:
4c4b4cd2 8879 {
76a01679 8880 struct type *static_type = to_static_fixed_type (type);
1ed6ede0
JB
8881 struct type *fixed_record_type =
8882 to_fixed_record_type (type, valaddr, address, NULL);
5b4ee69b 8883
529cad9c
PH
8884 /* If STATIC_TYPE is a tagged type and we know the object's address,
8885 then we can determine its tag, and compute the object's actual
0963b4bd 8886 type from there. Note that we have to use the fixed record
1ed6ede0
JB
8887 type (the parent part of the record may have dynamic fields
8888 and the way the location of _tag is expressed may depend on
8889 them). */
529cad9c 8890
1ed6ede0 8891 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
76a01679 8892 {
b50d69b5
JG
8893 struct value *tag =
8894 value_tag_from_contents_and_address
8895 (fixed_record_type,
8896 valaddr,
8897 address);
8898 struct type *real_type = type_from_tag (tag);
8899 struct value *obj =
8900 value_from_contents_and_address (fixed_record_type,
8901 valaddr,
8902 address);
9f1f738a 8903 fixed_record_type = value_type (obj);
76a01679 8904 if (real_type != NULL)
b50d69b5
JG
8905 return to_fixed_record_type
8906 (real_type, NULL,
8907 value_address (ada_tag_value_at_base_address (obj)), NULL);
76a01679 8908 }
4af88198
JB
8909
8910 /* Check to see if there is a parallel ___XVZ variable.
8911 If there is, then it provides the actual size of our type. */
8912 else if (ada_type_name (fixed_record_type) != NULL)
8913 {
0d5cff50 8914 const char *name = ada_type_name (fixed_record_type);
224c3ddb
SM
8915 char *xvz_name
8916 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
4af88198
JB
8917 int xvz_found = 0;
8918 LONGEST size;
8919
88c15c34 8920 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
4af88198
JB
8921 size = get_int_var_value (xvz_name, &xvz_found);
8922 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
8923 {
8924 fixed_record_type = copy_type (fixed_record_type);
8925 TYPE_LENGTH (fixed_record_type) = size;
8926
8927 /* The FIXED_RECORD_TYPE may have be a stub. We have
8928 observed this when the debugging info is STABS, and
8929 apparently it is something that is hard to fix.
8930
8931 In practice, we don't need the actual type definition
8932 at all, because the presence of the XVZ variable allows us
8933 to assume that there must be a XVS type as well, which we
8934 should be able to use later, when we need the actual type
8935 definition.
8936
8937 In the meantime, pretend that the "fixed" type we are
8938 returning is NOT a stub, because this can cause trouble
8939 when using this type to create new types targeting it.
8940 Indeed, the associated creation routines often check
8941 whether the target type is a stub and will try to replace
0963b4bd 8942 it, thus using a type with the wrong size. This, in turn,
4af88198
JB
8943 might cause the new type to have the wrong size too.
8944 Consider the case of an array, for instance, where the size
8945 of the array is computed from the number of elements in
8946 our array multiplied by the size of its element. */
8947 TYPE_STUB (fixed_record_type) = 0;
8948 }
8949 }
1ed6ede0 8950 return fixed_record_type;
4c4b4cd2 8951 }
d2e4a39e 8952 case TYPE_CODE_ARRAY:
4c4b4cd2 8953 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8954 case TYPE_CODE_UNION:
8955 if (dval == NULL)
4c4b4cd2 8956 return type;
d2e4a39e 8957 else
4c4b4cd2 8958 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8959 }
14f9c5c9
AS
8960}
8961
f192137b
JB
8962/* The same as ada_to_fixed_type_1, except that it preserves the type
8963 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8964
8965 The typedef layer needs be preserved in order to differentiate between
8966 arrays and array pointers when both types are implemented using the same
8967 fat pointer. In the array pointer case, the pointer is encoded as
8968 a typedef of the pointer type. For instance, considering:
8969
8970 type String_Access is access String;
8971 S1 : String_Access := null;
8972
8973 To the debugger, S1 is defined as a typedef of type String. But
8974 to the user, it is a pointer. So if the user tries to print S1,
8975 we should not dereference the array, but print the array address
8976 instead.
8977
8978 If we didn't preserve the typedef layer, we would lose the fact that
8979 the type is to be presented as a pointer (needs de-reference before
8980 being printed). And we would also use the source-level type name. */
f192137b
JB
8981
8982struct type *
8983ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
8984 CORE_ADDR address, struct value *dval, int check_tag)
8985
8986{
8987 struct type *fixed_type =
8988 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8989
96dbd2c1
JB
8990 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8991 then preserve the typedef layer.
8992
8993 Implementation note: We can only check the main-type portion of
8994 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8995 from TYPE now returns a type that has the same instance flags
8996 as TYPE. For instance, if TYPE is a "typedef const", and its
8997 target type is a "struct", then the typedef elimination will return
8998 a "const" version of the target type. See check_typedef for more
8999 details about how the typedef layer elimination is done.
9000
9001 brobecker/2010-11-19: It seems to me that the only case where it is
9002 useful to preserve the typedef layer is when dealing with fat pointers.
9003 Perhaps, we could add a check for that and preserve the typedef layer
9004 only in that situation. But this seems unecessary so far, probably
9005 because we call check_typedef/ada_check_typedef pretty much everywhere.
9006 */
f192137b 9007 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
720d1a40 9008 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 9009 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
9010 return type;
9011
9012 return fixed_type;
9013}
9014
14f9c5c9 9015/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 9016 TYPE0, but based on no runtime data. */
14f9c5c9 9017
d2e4a39e
AS
9018static struct type *
9019to_static_fixed_type (struct type *type0)
14f9c5c9 9020{
d2e4a39e 9021 struct type *type;
14f9c5c9
AS
9022
9023 if (type0 == NULL)
9024 return NULL;
9025
876cecd0 9026 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2
PH
9027 return type0;
9028
61ee279c 9029 type0 = ada_check_typedef (type0);
d2e4a39e 9030
14f9c5c9
AS
9031 switch (TYPE_CODE (type0))
9032 {
9033 default:
9034 return type0;
9035 case TYPE_CODE_STRUCT:
9036 type = dynamic_template_type (type0);
d2e4a39e 9037 if (type != NULL)
4c4b4cd2
PH
9038 return template_to_static_fixed_type (type);
9039 else
9040 return template_to_static_fixed_type (type0);
14f9c5c9
AS
9041 case TYPE_CODE_UNION:
9042 type = ada_find_parallel_type (type0, "___XVU");
9043 if (type != NULL)
4c4b4cd2
PH
9044 return template_to_static_fixed_type (type);
9045 else
9046 return template_to_static_fixed_type (type0);
14f9c5c9
AS
9047 }
9048}
9049
4c4b4cd2
PH
9050/* A static approximation of TYPE with all type wrappers removed. */
9051
d2e4a39e
AS
9052static struct type *
9053static_unwrap_type (struct type *type)
14f9c5c9
AS
9054{
9055 if (ada_is_aligner_type (type))
9056 {
61ee279c 9057 struct type *type1 = TYPE_FIELD_TYPE (ada_check_typedef (type), 0);
14f9c5c9 9058 if (ada_type_name (type1) == NULL)
4c4b4cd2 9059 TYPE_NAME (type1) = ada_type_name (type);
14f9c5c9
AS
9060
9061 return static_unwrap_type (type1);
9062 }
d2e4a39e 9063 else
14f9c5c9 9064 {
d2e4a39e 9065 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 9066
d2e4a39e 9067 if (raw_real_type == type)
4c4b4cd2 9068 return type;
14f9c5c9 9069 else
4c4b4cd2 9070 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
9071 }
9072}
9073
9074/* In some cases, incomplete and private types require
4c4b4cd2 9075 cross-references that are not resolved as records (for example,
14f9c5c9
AS
9076 type Foo;
9077 type FooP is access Foo;
9078 V: FooP;
9079 type Foo is array ...;
4c4b4cd2 9080 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
9081 cross-references to such types, we instead substitute for FooP a
9082 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 9083 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
9084
9085/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
9086 exists, otherwise TYPE. */
9087
d2e4a39e 9088struct type *
61ee279c 9089ada_check_typedef (struct type *type)
14f9c5c9 9090{
727e3d2e
JB
9091 if (type == NULL)
9092 return NULL;
9093
720d1a40
JB
9094 /* If our type is a typedef type of a fat pointer, then we're done.
9095 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
9096 what allows us to distinguish between fat pointers that represent
9097 array types, and fat pointers that represent array access types
9098 (in both cases, the compiler implements them as fat pointers). */
9099 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
9100 && is_thick_pntr (ada_typedef_target_type (type)))
9101 return type;
9102
f168693b 9103 type = check_typedef (type);
14f9c5c9 9104 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
529cad9c 9105 || !TYPE_STUB (type)
14f9c5c9
AS
9106 || TYPE_TAG_NAME (type) == NULL)
9107 return type;
d2e4a39e 9108 else
14f9c5c9 9109 {
0d5cff50 9110 const char *name = TYPE_TAG_NAME (type);
d2e4a39e 9111 struct type *type1 = ada_find_any_type (name);
5b4ee69b 9112
05e522ef
JB
9113 if (type1 == NULL)
9114 return type;
9115
9116 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
9117 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
9118 types, only for the typedef-to-array types). If that's the case,
9119 strip the typedef layer. */
9120 if (TYPE_CODE (type1) == TYPE_CODE_TYPEDEF)
9121 type1 = ada_check_typedef (type1);
9122
9123 return type1;
14f9c5c9
AS
9124 }
9125}
9126
9127/* A value representing the data at VALADDR/ADDRESS as described by
9128 type TYPE0, but with a standard (static-sized) type that correctly
9129 describes it. If VAL0 is not NULL and TYPE0 already is a standard
9130 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 9131 creation of struct values]. */
14f9c5c9 9132
4c4b4cd2
PH
9133static struct value *
9134ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
9135 struct value *val0)
14f9c5c9 9136{
1ed6ede0 9137 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 9138
14f9c5c9
AS
9139 if (type == type0 && val0 != NULL)
9140 return val0;
d2e4a39e 9141 else
4c4b4cd2
PH
9142 return value_from_contents_and_address (type, 0, address);
9143}
9144
9145/* A value representing VAL, but with a standard (static-sized) type
9146 that correctly describes it. Does not necessarily create a new
9147 value. */
9148
0c3acc09 9149struct value *
4c4b4cd2
PH
9150ada_to_fixed_value (struct value *val)
9151{
c48db5ca
JB
9152 val = unwrap_value (val);
9153 val = ada_to_fixed_value_create (value_type (val),
9154 value_address (val),
9155 val);
9156 return val;
14f9c5c9 9157}
d2e4a39e 9158\f
14f9c5c9 9159
14f9c5c9
AS
9160/* Attributes */
9161
4c4b4cd2
PH
9162/* Table mapping attribute numbers to names.
9163 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 9164
d2e4a39e 9165static const char *attribute_names[] = {
14f9c5c9
AS
9166 "<?>",
9167
d2e4a39e 9168 "first",
14f9c5c9
AS
9169 "last",
9170 "length",
9171 "image",
14f9c5c9
AS
9172 "max",
9173 "min",
4c4b4cd2
PH
9174 "modulus",
9175 "pos",
9176 "size",
9177 "tag",
14f9c5c9 9178 "val",
14f9c5c9
AS
9179 0
9180};
9181
d2e4a39e 9182const char *
4c4b4cd2 9183ada_attribute_name (enum exp_opcode n)
14f9c5c9 9184{
4c4b4cd2
PH
9185 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
9186 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
9187 else
9188 return attribute_names[0];
9189}
9190
4c4b4cd2 9191/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 9192
4c4b4cd2
PH
9193static LONGEST
9194pos_atr (struct value *arg)
14f9c5c9 9195{
24209737
PH
9196 struct value *val = coerce_ref (arg);
9197 struct type *type = value_type (val);
aa715135 9198 LONGEST result;
14f9c5c9 9199
d2e4a39e 9200 if (!discrete_type_p (type))
323e0a4a 9201 error (_("'POS only defined on discrete types"));
14f9c5c9 9202
aa715135
JG
9203 if (!discrete_position (type, value_as_long (val), &result))
9204 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 9205
aa715135 9206 return result;
4c4b4cd2
PH
9207}
9208
9209static struct value *
3cb382c9 9210value_pos_atr (struct type *type, struct value *arg)
4c4b4cd2 9211{
3cb382c9 9212 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
9213}
9214
4c4b4cd2 9215/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 9216
d2e4a39e
AS
9217static struct value *
9218value_val_atr (struct type *type, struct value *arg)
14f9c5c9 9219{
d2e4a39e 9220 if (!discrete_type_p (type))
323e0a4a 9221 error (_("'VAL only defined on discrete types"));
df407dfe 9222 if (!integer_type_p (value_type (arg)))
323e0a4a 9223 error (_("'VAL requires integral argument"));
14f9c5c9
AS
9224
9225 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
9226 {
9227 long pos = value_as_long (arg);
5b4ee69b 9228
14f9c5c9 9229 if (pos < 0 || pos >= TYPE_NFIELDS (type))
323e0a4a 9230 error (_("argument to 'VAL out of range"));
14e75d8e 9231 return value_from_longest (type, TYPE_FIELD_ENUMVAL (type, pos));
14f9c5c9
AS
9232 }
9233 else
9234 return value_from_longest (type, value_as_long (arg));
9235}
14f9c5c9 9236\f
d2e4a39e 9237
4c4b4cd2 9238 /* Evaluation */
14f9c5c9 9239
4c4b4cd2
PH
9240/* True if TYPE appears to be an Ada character type.
9241 [At the moment, this is true only for Character and Wide_Character;
9242 It is a heuristic test that could stand improvement]. */
14f9c5c9 9243
d2e4a39e
AS
9244int
9245ada_is_character_type (struct type *type)
14f9c5c9 9246{
7b9f71f2
JB
9247 const char *name;
9248
9249 /* If the type code says it's a character, then assume it really is,
9250 and don't check any further. */
9251 if (TYPE_CODE (type) == TYPE_CODE_CHAR)
9252 return 1;
9253
9254 /* Otherwise, assume it's a character type iff it is a discrete type
9255 with a known character type name. */
9256 name = ada_type_name (type);
9257 return (name != NULL
9258 && (TYPE_CODE (type) == TYPE_CODE_INT
9259 || TYPE_CODE (type) == TYPE_CODE_RANGE)
9260 && (strcmp (name, "character") == 0
9261 || strcmp (name, "wide_character") == 0
5a517ebd 9262 || strcmp (name, "wide_wide_character") == 0
7b9f71f2 9263 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
9264}
9265
4c4b4cd2 9266/* True if TYPE appears to be an Ada string type. */
14f9c5c9
AS
9267
9268int
ebf56fd3 9269ada_is_string_type (struct type *type)
14f9c5c9 9270{
61ee279c 9271 type = ada_check_typedef (type);
d2e4a39e 9272 if (type != NULL
14f9c5c9 9273 && TYPE_CODE (type) != TYPE_CODE_PTR
76a01679
JB
9274 && (ada_is_simple_array_type (type)
9275 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
9276 && ada_array_arity (type) == 1)
9277 {
9278 struct type *elttype = ada_array_element_type (type, 1);
9279
9280 return ada_is_character_type (elttype);
9281 }
d2e4a39e 9282 else
14f9c5c9
AS
9283 return 0;
9284}
9285
5bf03f13
JB
9286/* The compiler sometimes provides a parallel XVS type for a given
9287 PAD type. Normally, it is safe to follow the PAD type directly,
9288 but older versions of the compiler have a bug that causes the offset
9289 of its "F" field to be wrong. Following that field in that case
9290 would lead to incorrect results, but this can be worked around
9291 by ignoring the PAD type and using the associated XVS type instead.
9292
9293 Set to True if the debugger should trust the contents of PAD types.
9294 Otherwise, ignore the PAD type if there is a parallel XVS type. */
9295static int trust_pad_over_xvs = 1;
14f9c5c9
AS
9296
9297/* True if TYPE is a struct type introduced by the compiler to force the
9298 alignment of a value. Such types have a single field with a
4c4b4cd2 9299 distinctive name. */
14f9c5c9
AS
9300
9301int
ebf56fd3 9302ada_is_aligner_type (struct type *type)
14f9c5c9 9303{
61ee279c 9304 type = ada_check_typedef (type);
714e53ab 9305
5bf03f13 9306 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
9307 return 0;
9308
14f9c5c9 9309 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2
PH
9310 && TYPE_NFIELDS (type) == 1
9311 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
14f9c5c9
AS
9312}
9313
9314/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 9315 the parallel type. */
14f9c5c9 9316
d2e4a39e
AS
9317struct type *
9318ada_get_base_type (struct type *raw_type)
14f9c5c9 9319{
d2e4a39e
AS
9320 struct type *real_type_namer;
9321 struct type *raw_real_type;
14f9c5c9
AS
9322
9323 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
9324 return raw_type;
9325
284614f0
JB
9326 if (ada_is_aligner_type (raw_type))
9327 /* The encoding specifies that we should always use the aligner type.
9328 So, even if this aligner type has an associated XVS type, we should
9329 simply ignore it.
9330
9331 According to the compiler gurus, an XVS type parallel to an aligner
9332 type may exist because of a stabs limitation. In stabs, aligner
9333 types are empty because the field has a variable-sized type, and
9334 thus cannot actually be used as an aligner type. As a result,
9335 we need the associated parallel XVS type to decode the type.
9336 Since the policy in the compiler is to not change the internal
9337 representation based on the debugging info format, we sometimes
9338 end up having a redundant XVS type parallel to the aligner type. */
9339 return raw_type;
9340
14f9c5c9 9341 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9342 if (real_type_namer == NULL
14f9c5c9
AS
9343 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
9344 || TYPE_NFIELDS (real_type_namer) != 1)
9345 return raw_type;
9346
f80d3ff2
JB
9347 if (TYPE_CODE (TYPE_FIELD_TYPE (real_type_namer, 0)) != TYPE_CODE_REF)
9348 {
9349 /* This is an older encoding form where the base type needs to be
9350 looked up by name. We prefer the newer enconding because it is
9351 more efficient. */
9352 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
9353 if (raw_real_type == NULL)
9354 return raw_type;
9355 else
9356 return raw_real_type;
9357 }
9358
9359 /* The field in our XVS type is a reference to the base type. */
9360 return TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (real_type_namer, 0));
d2e4a39e 9361}
14f9c5c9 9362
4c4b4cd2 9363/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9364
d2e4a39e
AS
9365struct type *
9366ada_aligned_type (struct type *type)
14f9c5c9
AS
9367{
9368 if (ada_is_aligner_type (type))
9369 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
9370 else
9371 return ada_get_base_type (type);
9372}
9373
9374
9375/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9376 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9377
fc1a4b47
AC
9378const gdb_byte *
9379ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9380{
d2e4a39e 9381 if (ada_is_aligner_type (type))
14f9c5c9 9382 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2
PH
9383 valaddr +
9384 TYPE_FIELD_BITPOS (type,
9385 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
9386 else
9387 return valaddr;
9388}
9389
4c4b4cd2
PH
9390
9391
14f9c5c9 9392/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9393 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9394const char *
9395ada_enum_name (const char *name)
14f9c5c9 9396{
4c4b4cd2
PH
9397 static char *result;
9398 static size_t result_len = 0;
d2e4a39e 9399 char *tmp;
14f9c5c9 9400
4c4b4cd2
PH
9401 /* First, unqualify the enumeration name:
9402 1. Search for the last '.' character. If we find one, then skip
177b42fe 9403 all the preceding characters, the unqualified name starts
76a01679 9404 right after that dot.
4c4b4cd2 9405 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9406 translates dots into "__". Search forward for double underscores,
9407 but stop searching when we hit an overloading suffix, which is
9408 of the form "__" followed by digits. */
4c4b4cd2 9409
c3e5cd34
PH
9410 tmp = strrchr (name, '.');
9411 if (tmp != NULL)
4c4b4cd2
PH
9412 name = tmp + 1;
9413 else
14f9c5c9 9414 {
4c4b4cd2
PH
9415 while ((tmp = strstr (name, "__")) != NULL)
9416 {
9417 if (isdigit (tmp[2]))
9418 break;
9419 else
9420 name = tmp + 2;
9421 }
14f9c5c9
AS
9422 }
9423
9424 if (name[0] == 'Q')
9425 {
14f9c5c9 9426 int v;
5b4ee69b 9427
14f9c5c9 9428 if (name[1] == 'U' || name[1] == 'W')
4c4b4cd2
PH
9429 {
9430 if (sscanf (name + 2, "%x", &v) != 1)
9431 return name;
9432 }
14f9c5c9 9433 else
4c4b4cd2 9434 return name;
14f9c5c9 9435
4c4b4cd2 9436 GROW_VECT (result, result_len, 16);
14f9c5c9 9437 if (isascii (v) && isprint (v))
88c15c34 9438 xsnprintf (result, result_len, "'%c'", v);
14f9c5c9 9439 else if (name[1] == 'U')
88c15c34 9440 xsnprintf (result, result_len, "[\"%02x\"]", v);
14f9c5c9 9441 else
88c15c34 9442 xsnprintf (result, result_len, "[\"%04x\"]", v);
14f9c5c9
AS
9443
9444 return result;
9445 }
d2e4a39e 9446 else
4c4b4cd2 9447 {
c3e5cd34
PH
9448 tmp = strstr (name, "__");
9449 if (tmp == NULL)
9450 tmp = strstr (name, "$");
9451 if (tmp != NULL)
4c4b4cd2
PH
9452 {
9453 GROW_VECT (result, result_len, tmp - name + 1);
9454 strncpy (result, name, tmp - name);
9455 result[tmp - name] = '\0';
9456 return result;
9457 }
9458
9459 return name;
9460 }
14f9c5c9
AS
9461}
9462
14f9c5c9
AS
9463/* Evaluate the subexpression of EXP starting at *POS as for
9464 evaluate_type, updating *POS to point just past the evaluated
4c4b4cd2 9465 expression. */
14f9c5c9 9466
d2e4a39e
AS
9467static struct value *
9468evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 9469{
4b27a620 9470 return evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
14f9c5c9
AS
9471}
9472
9473/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9474 value it wraps. */
14f9c5c9 9475
d2e4a39e
AS
9476static struct value *
9477unwrap_value (struct value *val)
14f9c5c9 9478{
df407dfe 9479 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 9480
14f9c5c9
AS
9481 if (ada_is_aligner_type (type))
9482 {
de4d072f 9483 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 9484 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 9485
14f9c5c9 9486 if (ada_type_name (val_type) == NULL)
4c4b4cd2 9487 TYPE_NAME (val_type) = ada_type_name (type);
14f9c5c9
AS
9488
9489 return unwrap_value (v);
9490 }
d2e4a39e 9491 else
14f9c5c9 9492 {
d2e4a39e 9493 struct type *raw_real_type =
61ee279c 9494 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 9495
5bf03f13
JB
9496 /* If there is no parallel XVS or XVE type, then the value is
9497 already unwrapped. Return it without further modification. */
9498 if ((type == raw_real_type)
9499 && ada_find_parallel_type (type, "___XVE") == NULL)
9500 return val;
14f9c5c9 9501
d2e4a39e 9502 return
4c4b4cd2
PH
9503 coerce_unspec_val_to_type
9504 (val, ada_to_fixed_type (raw_real_type, 0,
42ae5230 9505 value_address (val),
1ed6ede0 9506 NULL, 1));
14f9c5c9
AS
9507 }
9508}
d2e4a39e
AS
9509
9510static struct value *
9511cast_to_fixed (struct type *type, struct value *arg)
14f9c5c9
AS
9512{
9513 LONGEST val;
9514
df407dfe 9515 if (type == value_type (arg))
14f9c5c9 9516 return arg;
df407dfe 9517 else if (ada_is_fixed_point_type (value_type (arg)))
d2e4a39e 9518 val = ada_float_to_fixed (type,
df407dfe 9519 ada_fixed_to_float (value_type (arg),
4c4b4cd2 9520 value_as_long (arg)));
d2e4a39e 9521 else
14f9c5c9 9522 {
a53b7a21 9523 DOUBLEST argd = value_as_double (arg);
5b4ee69b 9524
14f9c5c9
AS
9525 val = ada_float_to_fixed (type, argd);
9526 }
9527
9528 return value_from_longest (type, val);
9529}
9530
d2e4a39e 9531static struct value *
a53b7a21 9532cast_from_fixed (struct type *type, struct value *arg)
14f9c5c9 9533{
df407dfe 9534 DOUBLEST val = ada_fixed_to_float (value_type (arg),
4c4b4cd2 9535 value_as_long (arg));
5b4ee69b 9536
a53b7a21 9537 return value_from_double (type, val);
14f9c5c9
AS
9538}
9539
d99dcf51
JB
9540/* Given two array types T1 and T2, return nonzero iff both arrays
9541 contain the same number of elements. */
9542
9543static int
9544ada_same_array_size_p (struct type *t1, struct type *t2)
9545{
9546 LONGEST lo1, hi1, lo2, hi2;
9547
9548 /* Get the array bounds in order to verify that the size of
9549 the two arrays match. */
9550 if (!get_array_bounds (t1, &lo1, &hi1)
9551 || !get_array_bounds (t2, &lo2, &hi2))
9552 error (_("unable to determine array bounds"));
9553
9554 /* To make things easier for size comparison, normalize a bit
9555 the case of empty arrays by making sure that the difference
9556 between upper bound and lower bound is always -1. */
9557 if (lo1 > hi1)
9558 hi1 = lo1 - 1;
9559 if (lo2 > hi2)
9560 hi2 = lo2 - 1;
9561
9562 return (hi1 - lo1 == hi2 - lo2);
9563}
9564
9565/* Assuming that VAL is an array of integrals, and TYPE represents
9566 an array with the same number of elements, but with wider integral
9567 elements, return an array "casted" to TYPE. In practice, this
9568 means that the returned array is built by casting each element
9569 of the original array into TYPE's (wider) element type. */
9570
9571static struct value *
9572ada_promote_array_of_integrals (struct type *type, struct value *val)
9573{
9574 struct type *elt_type = TYPE_TARGET_TYPE (type);
9575 LONGEST lo, hi;
9576 struct value *res;
9577 LONGEST i;
9578
9579 /* Verify that both val and type are arrays of scalars, and
9580 that the size of val's elements is smaller than the size
9581 of type's element. */
9582 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
9583 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
9584 gdb_assert (TYPE_CODE (value_type (val)) == TYPE_CODE_ARRAY);
9585 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
9586 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
9587 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
9588
9589 if (!get_array_bounds (type, &lo, &hi))
9590 error (_("unable to determine array bounds"));
9591
9592 res = allocate_value (type);
9593
9594 /* Promote each array element. */
9595 for (i = 0; i < hi - lo + 1; i++)
9596 {
9597 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
9598
9599 memcpy (value_contents_writeable (res) + (i * TYPE_LENGTH (elt_type)),
9600 value_contents_all (elt), TYPE_LENGTH (elt_type));
9601 }
9602
9603 return res;
9604}
9605
4c4b4cd2
PH
9606/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9607 return the converted value. */
9608
d2e4a39e
AS
9609static struct value *
9610coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9611{
df407dfe 9612 struct type *type2 = value_type (val);
5b4ee69b 9613
14f9c5c9
AS
9614 if (type == type2)
9615 return val;
9616
61ee279c
PH
9617 type2 = ada_check_typedef (type2);
9618 type = ada_check_typedef (type);
14f9c5c9 9619
d2e4a39e
AS
9620 if (TYPE_CODE (type2) == TYPE_CODE_PTR
9621 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
9622 {
9623 val = ada_value_ind (val);
df407dfe 9624 type2 = value_type (val);
14f9c5c9
AS
9625 }
9626
d2e4a39e 9627 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
14f9c5c9
AS
9628 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
9629 {
d99dcf51
JB
9630 if (!ada_same_array_size_p (type, type2))
9631 error (_("cannot assign arrays of different length"));
9632
9633 if (is_integral_type (TYPE_TARGET_TYPE (type))
9634 && is_integral_type (TYPE_TARGET_TYPE (type2))
9635 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
9636 < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9637 {
9638 /* Allow implicit promotion of the array elements to
9639 a wider type. */
9640 return ada_promote_array_of_integrals (type, val);
9641 }
9642
9643 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
9644 != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
323e0a4a 9645 error (_("Incompatible types in assignment"));
04624583 9646 deprecated_set_value_type (val, type);
14f9c5c9 9647 }
d2e4a39e 9648 return val;
14f9c5c9
AS
9649}
9650
4c4b4cd2
PH
9651static struct value *
9652ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9653{
9654 struct value *val;
9655 struct type *type1, *type2;
9656 LONGEST v, v1, v2;
9657
994b9211
AC
9658 arg1 = coerce_ref (arg1);
9659 arg2 = coerce_ref (arg2);
18af8284
JB
9660 type1 = get_base_type (ada_check_typedef (value_type (arg1)));
9661 type2 = get_base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 9662
76a01679
JB
9663 if (TYPE_CODE (type1) != TYPE_CODE_INT
9664 || TYPE_CODE (type2) != TYPE_CODE_INT)
4c4b4cd2
PH
9665 return value_binop (arg1, arg2, op);
9666
76a01679 9667 switch (op)
4c4b4cd2
PH
9668 {
9669 case BINOP_MOD:
9670 case BINOP_DIV:
9671 case BINOP_REM:
9672 break;
9673 default:
9674 return value_binop (arg1, arg2, op);
9675 }
9676
9677 v2 = value_as_long (arg2);
9678 if (v2 == 0)
323e0a4a 9679 error (_("second operand of %s must not be zero."), op_string (op));
4c4b4cd2
PH
9680
9681 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
9682 return value_binop (arg1, arg2, op);
9683
9684 v1 = value_as_long (arg1);
9685 switch (op)
9686 {
9687 case BINOP_DIV:
9688 v = v1 / v2;
76a01679
JB
9689 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
9690 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9691 break;
9692 case BINOP_REM:
9693 v = v1 % v2;
76a01679
JB
9694 if (v * v1 < 0)
9695 v -= v2;
4c4b4cd2
PH
9696 break;
9697 default:
9698 /* Should not reach this point. */
9699 v = 0;
9700 }
9701
9702 val = allocate_value (type1);
990a07ab 9703 store_unsigned_integer (value_contents_raw (val),
e17a4113
UW
9704 TYPE_LENGTH (value_type (val)),
9705 gdbarch_byte_order (get_type_arch (type1)), v);
4c4b4cd2
PH
9706 return val;
9707}
9708
9709static int
9710ada_value_equal (struct value *arg1, struct value *arg2)
9711{
df407dfe
AC
9712 if (ada_is_direct_array_type (value_type (arg1))
9713 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 9714 {
f58b38bf
JB
9715 /* Automatically dereference any array reference before
9716 we attempt to perform the comparison. */
9717 arg1 = ada_coerce_ref (arg1);
9718 arg2 = ada_coerce_ref (arg2);
9719
4c4b4cd2
PH
9720 arg1 = ada_coerce_to_simple_array (arg1);
9721 arg2 = ada_coerce_to_simple_array (arg2);
df407dfe
AC
9722 if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
9723 || TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
323e0a4a 9724 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9725 /* FIXME: The following works only for types whose
76a01679
JB
9726 representations use all bits (no padding or undefined bits)
9727 and do not have user-defined equality. */
9728 return
df407dfe 9729 TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
0fd88904 9730 && memcmp (value_contents (arg1), value_contents (arg2),
df407dfe 9731 TYPE_LENGTH (value_type (arg1))) == 0;
4c4b4cd2
PH
9732 }
9733 return value_equal (arg1, arg2);
9734}
9735
52ce6436
PH
9736/* Total number of component associations in the aggregate starting at
9737 index PC in EXP. Assumes that index PC is the start of an
0963b4bd 9738 OP_AGGREGATE. */
52ce6436
PH
9739
9740static int
9741num_component_specs (struct expression *exp, int pc)
9742{
9743 int n, m, i;
5b4ee69b 9744
52ce6436
PH
9745 m = exp->elts[pc + 1].longconst;
9746 pc += 3;
9747 n = 0;
9748 for (i = 0; i < m; i += 1)
9749 {
9750 switch (exp->elts[pc].opcode)
9751 {
9752 default:
9753 n += 1;
9754 break;
9755 case OP_CHOICES:
9756 n += exp->elts[pc + 1].longconst;
9757 break;
9758 }
9759 ada_evaluate_subexp (NULL, exp, &pc, EVAL_SKIP);
9760 }
9761 return n;
9762}
9763
9764/* Assign the result of evaluating EXP starting at *POS to the INDEXth
9765 component of LHS (a simple array or a record), updating *POS past
9766 the expression, assuming that LHS is contained in CONTAINER. Does
9767 not modify the inferior's memory, nor does it modify LHS (unless
9768 LHS == CONTAINER). */
9769
9770static void
9771assign_component (struct value *container, struct value *lhs, LONGEST index,
9772 struct expression *exp, int *pos)
9773{
9774 struct value *mark = value_mark ();
9775 struct value *elt;
5b4ee69b 9776
52ce6436
PH
9777 if (TYPE_CODE (value_type (lhs)) == TYPE_CODE_ARRAY)
9778 {
22601c15
UW
9779 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9780 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9781
52ce6436
PH
9782 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9783 }
9784 else
9785 {
9786 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
c48db5ca 9787 elt = ada_to_fixed_value (elt);
52ce6436
PH
9788 }
9789
9790 if (exp->elts[*pos].opcode == OP_AGGREGATE)
9791 assign_aggregate (container, elt, exp, pos, EVAL_NORMAL);
9792 else
9793 value_assign_to_component (container, elt,
9794 ada_evaluate_subexp (NULL, exp, pos,
9795 EVAL_NORMAL));
9796
9797 value_free_to_mark (mark);
9798}
9799
9800/* Assuming that LHS represents an lvalue having a record or array
9801 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
9802 of that aggregate's value to LHS, advancing *POS past the
9803 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
9804 lvalue containing LHS (possibly LHS itself). Does not modify
9805 the inferior's memory, nor does it modify the contents of
0963b4bd 9806 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
52ce6436
PH
9807
9808static struct value *
9809assign_aggregate (struct value *container,
9810 struct value *lhs, struct expression *exp,
9811 int *pos, enum noside noside)
9812{
9813 struct type *lhs_type;
9814 int n = exp->elts[*pos+1].longconst;
9815 LONGEST low_index, high_index;
9816 int num_specs;
9817 LONGEST *indices;
9818 int max_indices, num_indices;
52ce6436 9819 int i;
52ce6436
PH
9820
9821 *pos += 3;
9822 if (noside != EVAL_NORMAL)
9823 {
52ce6436
PH
9824 for (i = 0; i < n; i += 1)
9825 ada_evaluate_subexp (NULL, exp, pos, noside);
9826 return container;
9827 }
9828
9829 container = ada_coerce_ref (container);
9830 if (ada_is_direct_array_type (value_type (container)))
9831 container = ada_coerce_to_simple_array (container);
9832 lhs = ada_coerce_ref (lhs);
9833 if (!deprecated_value_modifiable (lhs))
9834 error (_("Left operand of assignment is not a modifiable lvalue."));
9835
9836 lhs_type = value_type (lhs);
9837 if (ada_is_direct_array_type (lhs_type))
9838 {
9839 lhs = ada_coerce_to_simple_array (lhs);
9840 lhs_type = value_type (lhs);
9841 low_index = TYPE_ARRAY_LOWER_BOUND_VALUE (lhs_type);
9842 high_index = TYPE_ARRAY_UPPER_BOUND_VALUE (lhs_type);
52ce6436
PH
9843 }
9844 else if (TYPE_CODE (lhs_type) == TYPE_CODE_STRUCT)
9845 {
9846 low_index = 0;
9847 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9848 }
9849 else
9850 error (_("Left-hand side must be array or record."));
9851
9852 num_specs = num_component_specs (exp, *pos - 3);
9853 max_indices = 4 * num_specs + 4;
8d749320 9854 indices = XALLOCAVEC (LONGEST, max_indices);
52ce6436
PH
9855 indices[0] = indices[1] = low_index - 1;
9856 indices[2] = indices[3] = high_index + 1;
9857 num_indices = 4;
9858
9859 for (i = 0; i < n; i += 1)
9860 {
9861 switch (exp->elts[*pos].opcode)
9862 {
1fbf5ada
JB
9863 case OP_CHOICES:
9864 aggregate_assign_from_choices (container, lhs, exp, pos, indices,
9865 &num_indices, max_indices,
9866 low_index, high_index);
9867 break;
9868 case OP_POSITIONAL:
9869 aggregate_assign_positional (container, lhs, exp, pos, indices,
52ce6436
PH
9870 &num_indices, max_indices,
9871 low_index, high_index);
1fbf5ada
JB
9872 break;
9873 case OP_OTHERS:
9874 if (i != n-1)
9875 error (_("Misplaced 'others' clause"));
9876 aggregate_assign_others (container, lhs, exp, pos, indices,
9877 num_indices, low_index, high_index);
9878 break;
9879 default:
9880 error (_("Internal error: bad aggregate clause"));
52ce6436
PH
9881 }
9882 }
9883
9884 return container;
9885}
9886
9887/* Assign into the component of LHS indexed by the OP_POSITIONAL
9888 construct at *POS, updating *POS past the construct, given that
9889 the positions are relative to lower bound LOW, where HIGH is the
9890 upper bound. Record the position in INDICES[0 .. MAX_INDICES-1]
9891 updating *NUM_INDICES as needed. CONTAINER is as for
0963b4bd 9892 assign_aggregate. */
52ce6436
PH
9893static void
9894aggregate_assign_positional (struct value *container,
9895 struct value *lhs, struct expression *exp,
9896 int *pos, LONGEST *indices, int *num_indices,
9897 int max_indices, LONGEST low, LONGEST high)
9898{
9899 LONGEST ind = longest_to_int (exp->elts[*pos + 1].longconst) + low;
9900
9901 if (ind - 1 == high)
e1d5a0d2 9902 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9903 if (ind <= high)
9904 {
9905 add_component_interval (ind, ind, indices, num_indices, max_indices);
9906 *pos += 3;
9907 assign_component (container, lhs, ind, exp, pos);
9908 }
9909 else
9910 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9911}
9912
9913/* Assign into the components of LHS indexed by the OP_CHOICES
9914 construct at *POS, updating *POS past the construct, given that
9915 the allowable indices are LOW..HIGH. Record the indices assigned
9916 to in INDICES[0 .. MAX_INDICES-1], updating *NUM_INDICES as
0963b4bd 9917 needed. CONTAINER is as for assign_aggregate. */
52ce6436
PH
9918static void
9919aggregate_assign_from_choices (struct value *container,
9920 struct value *lhs, struct expression *exp,
9921 int *pos, LONGEST *indices, int *num_indices,
9922 int max_indices, LONGEST low, LONGEST high)
9923{
9924 int j;
9925 int n_choices = longest_to_int (exp->elts[*pos+1].longconst);
9926 int choice_pos, expr_pc;
9927 int is_array = ada_is_direct_array_type (value_type (lhs));
9928
9929 choice_pos = *pos += 3;
9930
9931 for (j = 0; j < n_choices; j += 1)
9932 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9933 expr_pc = *pos;
9934 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9935
9936 for (j = 0; j < n_choices; j += 1)
9937 {
9938 LONGEST lower, upper;
9939 enum exp_opcode op = exp->elts[choice_pos].opcode;
5b4ee69b 9940
52ce6436
PH
9941 if (op == OP_DISCRETE_RANGE)
9942 {
9943 choice_pos += 1;
9944 lower = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
9945 EVAL_NORMAL));
9946 upper = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
9947 EVAL_NORMAL));
9948 }
9949 else if (is_array)
9950 {
9951 lower = value_as_long (ada_evaluate_subexp (NULL, exp, &choice_pos,
9952 EVAL_NORMAL));
9953 upper = lower;
9954 }
9955 else
9956 {
9957 int ind;
0d5cff50 9958 const char *name;
5b4ee69b 9959
52ce6436
PH
9960 switch (op)
9961 {
9962 case OP_NAME:
9963 name = &exp->elts[choice_pos + 2].string;
9964 break;
9965 case OP_VAR_VALUE:
9966 name = SYMBOL_NATURAL_NAME (exp->elts[choice_pos + 2].symbol);
9967 break;
9968 default:
9969 error (_("Invalid record component association."));
9970 }
9971 ada_evaluate_subexp (NULL, exp, &choice_pos, EVAL_SKIP);
9972 ind = 0;
9973 if (! find_struct_field (name, value_type (lhs), 0,
9974 NULL, NULL, NULL, NULL, &ind))
9975 error (_("Unknown component name: %s."), name);
9976 lower = upper = ind;
9977 }
9978
9979 if (lower <= upper && (lower < low || upper > high))
9980 error (_("Index in component association out of bounds."));
9981
9982 add_component_interval (lower, upper, indices, num_indices,
9983 max_indices);
9984 while (lower <= upper)
9985 {
9986 int pos1;
5b4ee69b 9987
52ce6436
PH
9988 pos1 = expr_pc;
9989 assign_component (container, lhs, lower, exp, &pos1);
9990 lower += 1;
9991 }
9992 }
9993}
9994
9995/* Assign the value of the expression in the OP_OTHERS construct in
9996 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9997 have not been previously assigned. The index intervals already assigned
9998 are in INDICES[0 .. NUM_INDICES-1]. Updates *POS to after the
0963b4bd 9999 OP_OTHERS clause. CONTAINER is as for assign_aggregate. */
52ce6436
PH
10000static void
10001aggregate_assign_others (struct value *container,
10002 struct value *lhs, struct expression *exp,
10003 int *pos, LONGEST *indices, int num_indices,
10004 LONGEST low, LONGEST high)
10005{
10006 int i;
5ce64950 10007 int expr_pc = *pos + 1;
52ce6436
PH
10008
10009 for (i = 0; i < num_indices - 2; i += 2)
10010 {
10011 LONGEST ind;
5b4ee69b 10012
52ce6436
PH
10013 for (ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
10014 {
5ce64950 10015 int localpos;
5b4ee69b 10016
5ce64950
MS
10017 localpos = expr_pc;
10018 assign_component (container, lhs, ind, exp, &localpos);
52ce6436
PH
10019 }
10020 }
10021 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
10022}
10023
10024/* Add the interval [LOW .. HIGH] to the sorted set of intervals
10025 [ INDICES[0] .. INDICES[1] ],..., [ INDICES[*SIZE-2] .. INDICES[*SIZE-1] ],
10026 modifying *SIZE as needed. It is an error if *SIZE exceeds
10027 MAX_SIZE. The resulting intervals do not overlap. */
10028static void
10029add_component_interval (LONGEST low, LONGEST high,
10030 LONGEST* indices, int *size, int max_size)
10031{
10032 int i, j;
5b4ee69b 10033
52ce6436
PH
10034 for (i = 0; i < *size; i += 2) {
10035 if (high >= indices[i] && low <= indices[i + 1])
10036 {
10037 int kh;
5b4ee69b 10038
52ce6436
PH
10039 for (kh = i + 2; kh < *size; kh += 2)
10040 if (high < indices[kh])
10041 break;
10042 if (low < indices[i])
10043 indices[i] = low;
10044 indices[i + 1] = indices[kh - 1];
10045 if (high > indices[i + 1])
10046 indices[i + 1] = high;
10047 memcpy (indices + i + 2, indices + kh, *size - kh);
10048 *size -= kh - i - 2;
10049 return;
10050 }
10051 else if (high < indices[i])
10052 break;
10053 }
10054
10055 if (*size == max_size)
10056 error (_("Internal error: miscounted aggregate components."));
10057 *size += 2;
10058 for (j = *size-1; j >= i+2; j -= 1)
10059 indices[j] = indices[j - 2];
10060 indices[i] = low;
10061 indices[i + 1] = high;
10062}
10063
6e48bd2c
JB
10064/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
10065 is different. */
10066
10067static struct value *
10068ada_value_cast (struct type *type, struct value *arg2, enum noside noside)
10069{
10070 if (type == ada_check_typedef (value_type (arg2)))
10071 return arg2;
10072
10073 if (ada_is_fixed_point_type (type))
10074 return (cast_to_fixed (type, arg2));
10075
10076 if (ada_is_fixed_point_type (value_type (arg2)))
a53b7a21 10077 return cast_from_fixed (type, arg2);
6e48bd2c
JB
10078
10079 return value_cast (type, arg2);
10080}
10081
284614f0
JB
10082/* Evaluating Ada expressions, and printing their result.
10083 ------------------------------------------------------
10084
21649b50
JB
10085 1. Introduction:
10086 ----------------
10087
284614f0
JB
10088 We usually evaluate an Ada expression in order to print its value.
10089 We also evaluate an expression in order to print its type, which
10090 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
10091 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
10092 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
10093 the evaluation compared to the EVAL_NORMAL, but is otherwise very
10094 similar.
10095
10096 Evaluating expressions is a little more complicated for Ada entities
10097 than it is for entities in languages such as C. The main reason for
10098 this is that Ada provides types whose definition might be dynamic.
10099 One example of such types is variant records. Or another example
10100 would be an array whose bounds can only be known at run time.
10101
10102 The following description is a general guide as to what should be
10103 done (and what should NOT be done) in order to evaluate an expression
10104 involving such types, and when. This does not cover how the semantic
10105 information is encoded by GNAT as this is covered separatly. For the
10106 document used as the reference for the GNAT encoding, see exp_dbug.ads
10107 in the GNAT sources.
10108
10109 Ideally, we should embed each part of this description next to its
10110 associated code. Unfortunately, the amount of code is so vast right
10111 now that it's hard to see whether the code handling a particular
10112 situation might be duplicated or not. One day, when the code is
10113 cleaned up, this guide might become redundant with the comments
10114 inserted in the code, and we might want to remove it.
10115
21649b50
JB
10116 2. ``Fixing'' an Entity, the Simple Case:
10117 -----------------------------------------
10118
284614f0
JB
10119 When evaluating Ada expressions, the tricky issue is that they may
10120 reference entities whose type contents and size are not statically
10121 known. Consider for instance a variant record:
10122
10123 type Rec (Empty : Boolean := True) is record
10124 case Empty is
10125 when True => null;
10126 when False => Value : Integer;
10127 end case;
10128 end record;
10129 Yes : Rec := (Empty => False, Value => 1);
10130 No : Rec := (empty => True);
10131
10132 The size and contents of that record depends on the value of the
10133 descriminant (Rec.Empty). At this point, neither the debugging
10134 information nor the associated type structure in GDB are able to
10135 express such dynamic types. So what the debugger does is to create
10136 "fixed" versions of the type that applies to the specific object.
10137 We also informally refer to this opperation as "fixing" an object,
10138 which means creating its associated fixed type.
10139
10140 Example: when printing the value of variable "Yes" above, its fixed
10141 type would look like this:
10142
10143 type Rec is record
10144 Empty : Boolean;
10145 Value : Integer;
10146 end record;
10147
10148 On the other hand, if we printed the value of "No", its fixed type
10149 would become:
10150
10151 type Rec is record
10152 Empty : Boolean;
10153 end record;
10154
10155 Things become a little more complicated when trying to fix an entity
10156 with a dynamic type that directly contains another dynamic type,
10157 such as an array of variant records, for instance. There are
10158 two possible cases: Arrays, and records.
10159
21649b50
JB
10160 3. ``Fixing'' Arrays:
10161 ---------------------
10162
10163 The type structure in GDB describes an array in terms of its bounds,
10164 and the type of its elements. By design, all elements in the array
10165 have the same type and we cannot represent an array of variant elements
10166 using the current type structure in GDB. When fixing an array,
10167 we cannot fix the array element, as we would potentially need one
10168 fixed type per element of the array. As a result, the best we can do
10169 when fixing an array is to produce an array whose bounds and size
10170 are correct (allowing us to read it from memory), but without having
10171 touched its element type. Fixing each element will be done later,
10172 when (if) necessary.
10173
10174 Arrays are a little simpler to handle than records, because the same
10175 amount of memory is allocated for each element of the array, even if
1b536f04 10176 the amount of space actually used by each element differs from element
21649b50 10177 to element. Consider for instance the following array of type Rec:
284614f0
JB
10178
10179 type Rec_Array is array (1 .. 2) of Rec;
10180
1b536f04
JB
10181 The actual amount of memory occupied by each element might be different
10182 from element to element, depending on the value of their discriminant.
21649b50 10183 But the amount of space reserved for each element in the array remains
1b536f04 10184 fixed regardless. So we simply need to compute that size using
21649b50
JB
10185 the debugging information available, from which we can then determine
10186 the array size (we multiply the number of elements of the array by
10187 the size of each element).
10188
10189 The simplest case is when we have an array of a constrained element
10190 type. For instance, consider the following type declarations:
10191
10192 type Bounded_String (Max_Size : Integer) is
10193 Length : Integer;
10194 Buffer : String (1 .. Max_Size);
10195 end record;
10196 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
10197
10198 In this case, the compiler describes the array as an array of
10199 variable-size elements (identified by its XVS suffix) for which
10200 the size can be read in the parallel XVZ variable.
10201
10202 In the case of an array of an unconstrained element type, the compiler
10203 wraps the array element inside a private PAD type. This type should not
10204 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
10205 that we also use the adjective "aligner" in our code to designate
10206 these wrapper types.
10207
1b536f04 10208 In some cases, the size allocated for each element is statically
21649b50
JB
10209 known. In that case, the PAD type already has the correct size,
10210 and the array element should remain unfixed.
10211
10212 But there are cases when this size is not statically known.
10213 For instance, assuming that "Five" is an integer variable:
284614f0
JB
10214
10215 type Dynamic is array (1 .. Five) of Integer;
10216 type Wrapper (Has_Length : Boolean := False) is record
10217 Data : Dynamic;
10218 case Has_Length is
10219 when True => Length : Integer;
10220 when False => null;
10221 end case;
10222 end record;
10223 type Wrapper_Array is array (1 .. 2) of Wrapper;
10224
10225 Hello : Wrapper_Array := (others => (Has_Length => True,
10226 Data => (others => 17),
10227 Length => 1));
10228
10229
10230 The debugging info would describe variable Hello as being an
10231 array of a PAD type. The size of that PAD type is not statically
10232 known, but can be determined using a parallel XVZ variable.
10233 In that case, a copy of the PAD type with the correct size should
10234 be used for the fixed array.
10235
21649b50
JB
10236 3. ``Fixing'' record type objects:
10237 ----------------------------------
10238
10239 Things are slightly different from arrays in the case of dynamic
284614f0
JB
10240 record types. In this case, in order to compute the associated
10241 fixed type, we need to determine the size and offset of each of
10242 its components. This, in turn, requires us to compute the fixed
10243 type of each of these components.
10244
10245 Consider for instance the example:
10246
10247 type Bounded_String (Max_Size : Natural) is record
10248 Str : String (1 .. Max_Size);
10249 Length : Natural;
10250 end record;
10251 My_String : Bounded_String (Max_Size => 10);
10252
10253 In that case, the position of field "Length" depends on the size
10254 of field Str, which itself depends on the value of the Max_Size
21649b50 10255 discriminant. In order to fix the type of variable My_String,
284614f0
JB
10256 we need to fix the type of field Str. Therefore, fixing a variant
10257 record requires us to fix each of its components.
10258
10259 However, if a component does not have a dynamic size, the component
10260 should not be fixed. In particular, fields that use a PAD type
10261 should not fixed. Here is an example where this might happen
10262 (assuming type Rec above):
10263
10264 type Container (Big : Boolean) is record
10265 First : Rec;
10266 After : Integer;
10267 case Big is
10268 when True => Another : Integer;
10269 when False => null;
10270 end case;
10271 end record;
10272 My_Container : Container := (Big => False,
10273 First => (Empty => True),
10274 After => 42);
10275
10276 In that example, the compiler creates a PAD type for component First,
10277 whose size is constant, and then positions the component After just
10278 right after it. The offset of component After is therefore constant
10279 in this case.
10280
10281 The debugger computes the position of each field based on an algorithm
10282 that uses, among other things, the actual position and size of the field
21649b50
JB
10283 preceding it. Let's now imagine that the user is trying to print
10284 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
10285 end up computing the offset of field After based on the size of the
10286 fixed version of field First. And since in our example First has
10287 only one actual field, the size of the fixed type is actually smaller
10288 than the amount of space allocated to that field, and thus we would
10289 compute the wrong offset of field After.
10290
21649b50
JB
10291 To make things more complicated, we need to watch out for dynamic
10292 components of variant records (identified by the ___XVL suffix in
10293 the component name). Even if the target type is a PAD type, the size
10294 of that type might not be statically known. So the PAD type needs
10295 to be unwrapped and the resulting type needs to be fixed. Otherwise,
10296 we might end up with the wrong size for our component. This can be
10297 observed with the following type declarations:
284614f0
JB
10298
10299 type Octal is new Integer range 0 .. 7;
10300 type Octal_Array is array (Positive range <>) of Octal;
10301 pragma Pack (Octal_Array);
10302
10303 type Octal_Buffer (Size : Positive) is record
10304 Buffer : Octal_Array (1 .. Size);
10305 Length : Integer;
10306 end record;
10307
10308 In that case, Buffer is a PAD type whose size is unset and needs
10309 to be computed by fixing the unwrapped type.
10310
21649b50
JB
10311 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
10312 ----------------------------------------------------------
10313
10314 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
10315 thus far, be actually fixed?
10316
10317 The answer is: Only when referencing that element. For instance
10318 when selecting one component of a record, this specific component
10319 should be fixed at that point in time. Or when printing the value
10320 of a record, each component should be fixed before its value gets
10321 printed. Similarly for arrays, the element of the array should be
10322 fixed when printing each element of the array, or when extracting
10323 one element out of that array. On the other hand, fixing should
10324 not be performed on the elements when taking a slice of an array!
10325
10326 Note that one of the side-effects of miscomputing the offset and
10327 size of each field is that we end up also miscomputing the size
10328 of the containing type. This can have adverse results when computing
10329 the value of an entity. GDB fetches the value of an entity based
10330 on the size of its type, and thus a wrong size causes GDB to fetch
10331 the wrong amount of memory. In the case where the computed size is
10332 too small, GDB fetches too little data to print the value of our
10333 entiry. Results in this case as unpredicatble, as we usually read
10334 past the buffer containing the data =:-o. */
10335
10336/* Implement the evaluate_exp routine in the exp_descriptor structure
10337 for the Ada language. */
10338
52ce6436 10339static struct value *
ebf56fd3 10340ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
4c4b4cd2 10341 int *pos, enum noside noside)
14f9c5c9
AS
10342{
10343 enum exp_opcode op;
b5385fc0 10344 int tem;
14f9c5c9 10345 int pc;
5ec18f2b 10346 int preeval_pos;
14f9c5c9
AS
10347 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
10348 struct type *type;
52ce6436 10349 int nargs, oplen;
d2e4a39e 10350 struct value **argvec;
14f9c5c9 10351
d2e4a39e
AS
10352 pc = *pos;
10353 *pos += 1;
14f9c5c9
AS
10354 op = exp->elts[pc].opcode;
10355
d2e4a39e 10356 switch (op)
14f9c5c9
AS
10357 {
10358 default:
10359 *pos -= 1;
6e48bd2c 10360 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
ca1f964d
JG
10361
10362 if (noside == EVAL_NORMAL)
10363 arg1 = unwrap_value (arg1);
6e48bd2c
JB
10364
10365 /* If evaluating an OP_DOUBLE and an EXPECT_TYPE was provided,
10366 then we need to perform the conversion manually, because
10367 evaluate_subexp_standard doesn't do it. This conversion is
10368 necessary in Ada because the different kinds of float/fixed
10369 types in Ada have different representations.
10370
10371 Similarly, we need to perform the conversion from OP_LONG
10372 ourselves. */
10373 if ((op == OP_DOUBLE || op == OP_LONG) && expect_type != NULL)
10374 arg1 = ada_value_cast (expect_type, arg1, noside);
10375
10376 return arg1;
4c4b4cd2
PH
10377
10378 case OP_STRING:
10379 {
76a01679 10380 struct value *result;
5b4ee69b 10381
76a01679
JB
10382 *pos -= 1;
10383 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
10384 /* The result type will have code OP_STRING, bashed there from
10385 OP_ARRAY. Bash it back. */
df407dfe
AC
10386 if (TYPE_CODE (value_type (result)) == TYPE_CODE_STRING)
10387 TYPE_CODE (value_type (result)) = TYPE_CODE_ARRAY;
76a01679 10388 return result;
4c4b4cd2 10389 }
14f9c5c9
AS
10390
10391 case UNOP_CAST:
10392 (*pos) += 2;
10393 type = exp->elts[pc + 1].type;
10394 arg1 = evaluate_subexp (type, exp, pos, noside);
10395 if (noside == EVAL_SKIP)
4c4b4cd2 10396 goto nosideret;
6e48bd2c 10397 arg1 = ada_value_cast (type, arg1, noside);
14f9c5c9
AS
10398 return arg1;
10399
4c4b4cd2
PH
10400 case UNOP_QUAL:
10401 (*pos) += 2;
10402 type = exp->elts[pc + 1].type;
10403 return ada_evaluate_subexp (type, exp, pos, noside);
10404
14f9c5c9
AS
10405 case BINOP_ASSIGN:
10406 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
52ce6436
PH
10407 if (exp->elts[*pos].opcode == OP_AGGREGATE)
10408 {
10409 arg1 = assign_aggregate (arg1, arg1, exp, pos, noside);
10410 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
10411 return arg1;
10412 return ada_value_assign (arg1, arg1);
10413 }
003f3813
JB
10414 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
10415 except if the lhs of our assignment is a convenience variable.
10416 In the case of assigning to a convenience variable, the lhs
10417 should be exactly the result of the evaluation of the rhs. */
10418 type = value_type (arg1);
10419 if (VALUE_LVAL (arg1) == lval_internalvar)
10420 type = NULL;
10421 arg2 = evaluate_subexp (type, exp, pos, noside);
14f9c5c9 10422 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 10423 return arg1;
df407dfe
AC
10424 if (ada_is_fixed_point_type (value_type (arg1)))
10425 arg2 = cast_to_fixed (value_type (arg1), arg2);
10426 else if (ada_is_fixed_point_type (value_type (arg2)))
76a01679 10427 error
323e0a4a 10428 (_("Fixed-point values must be assigned to fixed-point variables"));
d2e4a39e 10429 else
df407dfe 10430 arg2 = coerce_for_assign (value_type (arg1), arg2);
4c4b4cd2 10431 return ada_value_assign (arg1, arg2);
14f9c5c9
AS
10432
10433 case BINOP_ADD:
10434 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
10435 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
10436 if (noside == EVAL_SKIP)
4c4b4cd2 10437 goto nosideret;
2ac8a782
JB
10438 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
10439 return (value_from_longest
10440 (value_type (arg1),
10441 value_as_long (arg1) + value_as_long (arg2)));
c40cc657
JB
10442 if (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR)
10443 return (value_from_longest
10444 (value_type (arg2),
10445 value_as_long (arg1) + value_as_long (arg2)));
df407dfe
AC
10446 if ((ada_is_fixed_point_type (value_type (arg1))
10447 || ada_is_fixed_point_type (value_type (arg2)))
10448 && value_type (arg1) != value_type (arg2))
323e0a4a 10449 error (_("Operands of fixed-point addition must have the same type"));
b7789565
JB
10450 /* Do the addition, and cast the result to the type of the first
10451 argument. We cannot cast the result to a reference type, so if
10452 ARG1 is a reference type, find its underlying type. */
10453 type = value_type (arg1);
10454 while (TYPE_CODE (type) == TYPE_CODE_REF)
10455 type = TYPE_TARGET_TYPE (type);
f44316fa 10456 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
89eef114 10457 return value_cast (type, value_binop (arg1, arg2, BINOP_ADD));
14f9c5c9
AS
10458
10459 case BINOP_SUB:
10460 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
10461 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
10462 if (noside == EVAL_SKIP)
4c4b4cd2 10463 goto nosideret;
2ac8a782
JB
10464 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
10465 return (value_from_longest
10466 (value_type (arg1),
10467 value_as_long (arg1) - value_as_long (arg2)));
c40cc657
JB
10468 if (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR)
10469 return (value_from_longest
10470 (value_type (arg2),
10471 value_as_long (arg1) - value_as_long (arg2)));
df407dfe
AC
10472 if ((ada_is_fixed_point_type (value_type (arg1))
10473 || ada_is_fixed_point_type (value_type (arg2)))
10474 && value_type (arg1) != value_type (arg2))
0963b4bd
MS
10475 error (_("Operands of fixed-point subtraction "
10476 "must have the same type"));
b7789565
JB
10477 /* Do the substraction, and cast the result to the type of the first
10478 argument. We cannot cast the result to a reference type, so if
10479 ARG1 is a reference type, find its underlying type. */
10480 type = value_type (arg1);
10481 while (TYPE_CODE (type) == TYPE_CODE_REF)
10482 type = TYPE_TARGET_TYPE (type);
f44316fa 10483 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
89eef114 10484 return value_cast (type, value_binop (arg1, arg2, BINOP_SUB));
14f9c5c9
AS
10485
10486 case BINOP_MUL:
10487 case BINOP_DIV:
e1578042
JB
10488 case BINOP_REM:
10489 case BINOP_MOD:
14f9c5c9
AS
10490 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10491 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10492 if (noside == EVAL_SKIP)
4c4b4cd2 10493 goto nosideret;
e1578042 10494 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9c2be529
JB
10495 {
10496 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10497 return value_zero (value_type (arg1), not_lval);
10498 }
14f9c5c9 10499 else
4c4b4cd2 10500 {
a53b7a21 10501 type = builtin_type (exp->gdbarch)->builtin_double;
df407dfe 10502 if (ada_is_fixed_point_type (value_type (arg1)))
a53b7a21 10503 arg1 = cast_from_fixed (type, arg1);
df407dfe 10504 if (ada_is_fixed_point_type (value_type (arg2)))
a53b7a21 10505 arg2 = cast_from_fixed (type, arg2);
f44316fa 10506 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
4c4b4cd2
PH
10507 return ada_value_binop (arg1, arg2, op);
10508 }
10509
4c4b4cd2
PH
10510 case BINOP_EQUAL:
10511 case BINOP_NOTEQUAL:
14f9c5c9 10512 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
df407dfe 10513 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
14f9c5c9 10514 if (noside == EVAL_SKIP)
76a01679 10515 goto nosideret;
4c4b4cd2 10516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 10517 tem = 0;
4c4b4cd2 10518 else
f44316fa
UW
10519 {
10520 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10521 tem = ada_value_equal (arg1, arg2);
10522 }
4c4b4cd2 10523 if (op == BINOP_NOTEQUAL)
76a01679 10524 tem = !tem;
fbb06eb1
UW
10525 type = language_bool_type (exp->language_defn, exp->gdbarch);
10526 return value_from_longest (type, (LONGEST) tem);
4c4b4cd2
PH
10527
10528 case UNOP_NEG:
10529 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10530 if (noside == EVAL_SKIP)
10531 goto nosideret;
df407dfe
AC
10532 else if (ada_is_fixed_point_type (value_type (arg1)))
10533 return value_cast (value_type (arg1), value_neg (arg1));
14f9c5c9 10534 else
f44316fa
UW
10535 {
10536 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10537 return value_neg (arg1);
10538 }
4c4b4cd2 10539
2330c6c6
JB
10540 case BINOP_LOGICAL_AND:
10541 case BINOP_LOGICAL_OR:
10542 case UNOP_LOGICAL_NOT:
000d5124
JB
10543 {
10544 struct value *val;
10545
10546 *pos -= 1;
10547 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
fbb06eb1
UW
10548 type = language_bool_type (exp->language_defn, exp->gdbarch);
10549 return value_cast (type, val);
000d5124 10550 }
2330c6c6
JB
10551
10552 case BINOP_BITWISE_AND:
10553 case BINOP_BITWISE_IOR:
10554 case BINOP_BITWISE_XOR:
000d5124
JB
10555 {
10556 struct value *val;
10557
10558 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
10559 *pos = pc;
10560 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
10561
10562 return value_cast (value_type (arg1), val);
10563 }
2330c6c6 10564
14f9c5c9
AS
10565 case OP_VAR_VALUE:
10566 *pos -= 1;
6799def4 10567
14f9c5c9 10568 if (noside == EVAL_SKIP)
4c4b4cd2
PH
10569 {
10570 *pos += 4;
10571 goto nosideret;
10572 }
da5c522f
JB
10573
10574 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
10575 /* Only encountered when an unresolved symbol occurs in a
10576 context other than a function call, in which case, it is
52ce6436 10577 invalid. */
323e0a4a 10578 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2 10579 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
da5c522f
JB
10580
10581 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 10582 {
0c1f74cf 10583 type = static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
31dbc1c5
JB
10584 /* Check to see if this is a tagged type. We also need to handle
10585 the case where the type is a reference to a tagged type, but
10586 we have to be careful to exclude pointers to tagged types.
10587 The latter should be shown as usual (as a pointer), whereas
10588 a reference should mostly be transparent to the user. */
10589 if (ada_is_tagged_type (type, 0)
023db19c 10590 || (TYPE_CODE (type) == TYPE_CODE_REF
31dbc1c5 10591 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
0d72a7c3
JB
10592 {
10593 /* Tagged types are a little special in the fact that the real
10594 type is dynamic and can only be determined by inspecting the
10595 object's tag. This means that we need to get the object's
10596 value first (EVAL_NORMAL) and then extract the actual object
10597 type from its tag.
10598
10599 Note that we cannot skip the final step where we extract
10600 the object type from its tag, because the EVAL_NORMAL phase
10601 results in dynamic components being resolved into fixed ones.
10602 This can cause problems when trying to print the type
10603 description of tagged types whose parent has a dynamic size:
10604 We use the type name of the "_parent" component in order
10605 to print the name of the ancestor type in the type description.
10606 If that component had a dynamic size, the resolution into
10607 a fixed type would result in the loss of that type name,
10608 thus preventing us from printing the name of the ancestor
10609 type in the type description. */
10610 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
10611
10612 if (TYPE_CODE (type) != TYPE_CODE_REF)
10613 {
10614 struct type *actual_type;
10615
10616 actual_type = type_from_tag (ada_value_tag (arg1));
10617 if (actual_type == NULL)
10618 /* If, for some reason, we were unable to determine
10619 the actual type from the tag, then use the static
10620 approximation that we just computed as a fallback.
10621 This can happen if the debugging information is
10622 incomplete, for instance. */
10623 actual_type = type;
10624 return value_zero (actual_type, not_lval);
10625 }
10626 else
10627 {
10628 /* In the case of a ref, ada_coerce_ref takes care
10629 of determining the actual type. But the evaluation
10630 should return a ref as it should be valid to ask
10631 for its address; so rebuild a ref after coerce. */
10632 arg1 = ada_coerce_ref (arg1);
10633 return value_ref (arg1);
10634 }
10635 }
0c1f74cf 10636
84754697
JB
10637 /* Records and unions for which GNAT encodings have been
10638 generated need to be statically fixed as well.
10639 Otherwise, non-static fixing produces a type where
10640 all dynamic properties are removed, which prevents "ptype"
10641 from being able to completely describe the type.
10642 For instance, a case statement in a variant record would be
10643 replaced by the relevant components based on the actual
10644 value of the discriminants. */
10645 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
10646 && dynamic_template_type (type) != NULL)
10647 || (TYPE_CODE (type) == TYPE_CODE_UNION
10648 && ada_find_parallel_type (type, "___XVU") != NULL))
10649 {
10650 *pos += 4;
10651 return value_zero (to_static_fixed_type (type), not_lval);
10652 }
4c4b4cd2 10653 }
da5c522f
JB
10654
10655 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
10656 return ada_to_fixed_value (arg1);
4c4b4cd2
PH
10657
10658 case OP_FUNCALL:
10659 (*pos) += 2;
10660
10661 /* Allocate arg vector, including space for the function to be
10662 called in argvec[0] and a terminating NULL. */
10663 nargs = longest_to_int (exp->elts[pc + 1].longconst);
8d749320 10664 argvec = XALLOCAVEC (struct value *, nargs + 2);
4c4b4cd2
PH
10665
10666 if (exp->elts[*pos].opcode == OP_VAR_VALUE
76a01679 10667 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
323e0a4a 10668 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2
PH
10669 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
10670 else
10671 {
10672 for (tem = 0; tem <= nargs; tem += 1)
10673 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10674 argvec[tem] = 0;
10675
10676 if (noside == EVAL_SKIP)
10677 goto nosideret;
10678 }
10679
ad82864c
JB
10680 if (ada_is_constrained_packed_array_type
10681 (desc_base_type (value_type (argvec[0]))))
4c4b4cd2 10682 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
284614f0
JB
10683 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
10684 && TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
10685 /* This is a packed array that has already been fixed, and
10686 therefore already coerced to a simple array. Nothing further
10687 to do. */
10688 ;
e6c2c623
PMR
10689 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_REF)
10690 {
10691 /* Make sure we dereference references so that all the code below
10692 feels like it's really handling the referenced value. Wrapping
10693 types (for alignment) may be there, so make sure we strip them as
10694 well. */
10695 argvec[0] = ada_to_fixed_value (coerce_ref (argvec[0]));
10696 }
10697 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
10698 && VALUE_LVAL (argvec[0]) == lval_memory)
10699 argvec[0] = value_addr (argvec[0]);
4c4b4cd2 10700
df407dfe 10701 type = ada_check_typedef (value_type (argvec[0]));
720d1a40
JB
10702
10703 /* Ada allows us to implicitly dereference arrays when subscripting
8f465ea7
JB
10704 them. So, if this is an array typedef (encoding use for array
10705 access types encoded as fat pointers), strip it now. */
720d1a40
JB
10706 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
10707 type = ada_typedef_target_type (type);
10708
4c4b4cd2
PH
10709 if (TYPE_CODE (type) == TYPE_CODE_PTR)
10710 {
61ee279c 10711 switch (TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type))))
4c4b4cd2
PH
10712 {
10713 case TYPE_CODE_FUNC:
61ee279c 10714 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
10715 break;
10716 case TYPE_CODE_ARRAY:
10717 break;
10718 case TYPE_CODE_STRUCT:
10719 if (noside != EVAL_AVOID_SIDE_EFFECTS)
10720 argvec[0] = ada_value_ind (argvec[0]);
61ee279c 10721 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
10722 break;
10723 default:
323e0a4a 10724 error (_("cannot subscript or call something of type `%s'"),
df407dfe 10725 ada_type_name (value_type (argvec[0])));
4c4b4cd2
PH
10726 break;
10727 }
10728 }
10729
10730 switch (TYPE_CODE (type))
10731 {
10732 case TYPE_CODE_FUNC:
10733 if (noside == EVAL_AVOID_SIDE_EFFECTS)
c8ea1972
PH
10734 {
10735 struct type *rtype = TYPE_TARGET_TYPE (type);
10736
10737 if (TYPE_GNU_IFUNC (type))
10738 return allocate_value (TYPE_TARGET_TYPE (rtype));
10739 return allocate_value (rtype);
10740 }
4c4b4cd2 10741 return call_function_by_hand (argvec[0], nargs, argvec + 1);
c8ea1972
PH
10742 case TYPE_CODE_INTERNAL_FUNCTION:
10743 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10744 /* We don't know anything about what the internal
10745 function might return, but we have to return
10746 something. */
10747 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
10748 not_lval);
10749 else
10750 return call_internal_function (exp->gdbarch, exp->language_defn,
10751 argvec[0], nargs, argvec + 1);
10752
4c4b4cd2
PH
10753 case TYPE_CODE_STRUCT:
10754 {
10755 int arity;
10756
4c4b4cd2
PH
10757 arity = ada_array_arity (type);
10758 type = ada_array_element_type (type, nargs);
10759 if (type == NULL)
323e0a4a 10760 error (_("cannot subscript or call a record"));
4c4b4cd2 10761 if (arity != nargs)
323e0a4a 10762 error (_("wrong number of subscripts; expecting %d"), arity);
4c4b4cd2 10763 if (noside == EVAL_AVOID_SIDE_EFFECTS)
0a07e705 10764 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
10765 return
10766 unwrap_value (ada_value_subscript
10767 (argvec[0], nargs, argvec + 1));
10768 }
10769 case TYPE_CODE_ARRAY:
10770 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10771 {
10772 type = ada_array_element_type (type, nargs);
10773 if (type == NULL)
323e0a4a 10774 error (_("element type of array unknown"));
4c4b4cd2 10775 else
0a07e705 10776 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
10777 }
10778 return
10779 unwrap_value (ada_value_subscript
10780 (ada_coerce_to_simple_array (argvec[0]),
10781 nargs, argvec + 1));
10782 case TYPE_CODE_PTR: /* Pointer to array */
4c4b4cd2
PH
10783 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10784 {
deede10c 10785 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
4c4b4cd2
PH
10786 type = ada_array_element_type (type, nargs);
10787 if (type == NULL)
323e0a4a 10788 error (_("element type of array unknown"));
4c4b4cd2 10789 else
0a07e705 10790 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
10791 }
10792 return
deede10c
JB
10793 unwrap_value (ada_value_ptr_subscript (argvec[0],
10794 nargs, argvec + 1));
4c4b4cd2
PH
10795
10796 default:
e1d5a0d2
PH
10797 error (_("Attempt to index or call something other than an "
10798 "array or function"));
4c4b4cd2
PH
10799 }
10800
10801 case TERNOP_SLICE:
10802 {
10803 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10804 struct value *low_bound_val =
10805 evaluate_subexp (NULL_TYPE, exp, pos, noside);
714e53ab
PH
10806 struct value *high_bound_val =
10807 evaluate_subexp (NULL_TYPE, exp, pos, noside);
10808 LONGEST low_bound;
10809 LONGEST high_bound;
5b4ee69b 10810
994b9211
AC
10811 low_bound_val = coerce_ref (low_bound_val);
10812 high_bound_val = coerce_ref (high_bound_val);
aa715135
JG
10813 low_bound = value_as_long (low_bound_val);
10814 high_bound = value_as_long (high_bound_val);
963a6417 10815
4c4b4cd2
PH
10816 if (noside == EVAL_SKIP)
10817 goto nosideret;
10818
4c4b4cd2
PH
10819 /* If this is a reference to an aligner type, then remove all
10820 the aligners. */
df407dfe
AC
10821 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
10822 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
10823 TYPE_TARGET_TYPE (value_type (array)) =
10824 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
4c4b4cd2 10825
ad82864c 10826 if (ada_is_constrained_packed_array_type (value_type (array)))
323e0a4a 10827 error (_("cannot slice a packed array"));
4c4b4cd2
PH
10828
10829 /* If this is a reference to an array or an array lvalue,
10830 convert to a pointer. */
df407dfe
AC
10831 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
10832 || (TYPE_CODE (value_type (array)) == TYPE_CODE_ARRAY
4c4b4cd2
PH
10833 && VALUE_LVAL (array) == lval_memory))
10834 array = value_addr (array);
10835
1265e4aa 10836 if (noside == EVAL_AVOID_SIDE_EFFECTS
61ee279c 10837 && ada_is_array_descriptor_type (ada_check_typedef
df407dfe 10838 (value_type (array))))
0b5d8877 10839 return empty_array (ada_type_of_array (array, 0), low_bound);
4c4b4cd2
PH
10840
10841 array = ada_coerce_to_simple_array_ptr (array);
10842
714e53ab
PH
10843 /* If we have more than one level of pointer indirection,
10844 dereference the value until we get only one level. */
df407dfe
AC
10845 while (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR
10846 && (TYPE_CODE (TYPE_TARGET_TYPE (value_type (array)))
714e53ab
PH
10847 == TYPE_CODE_PTR))
10848 array = value_ind (array);
10849
10850 /* Make sure we really do have an array type before going further,
10851 to avoid a SEGV when trying to get the index type or the target
10852 type later down the road if the debug info generated by
10853 the compiler is incorrect or incomplete. */
df407dfe 10854 if (!ada_is_simple_array_type (value_type (array)))
323e0a4a 10855 error (_("cannot take slice of non-array"));
714e53ab 10856
828292f2
JB
10857 if (TYPE_CODE (ada_check_typedef (value_type (array)))
10858 == TYPE_CODE_PTR)
4c4b4cd2 10859 {
828292f2
JB
10860 struct type *type0 = ada_check_typedef (value_type (array));
10861
0b5d8877 10862 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
828292f2 10863 return empty_array (TYPE_TARGET_TYPE (type0), low_bound);
4c4b4cd2
PH
10864 else
10865 {
10866 struct type *arr_type0 =
828292f2 10867 to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
5b4ee69b 10868
f5938064
JG
10869 return ada_value_slice_from_ptr (array, arr_type0,
10870 longest_to_int (low_bound),
10871 longest_to_int (high_bound));
4c4b4cd2
PH
10872 }
10873 }
10874 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10875 return array;
10876 else if (high_bound < low_bound)
df407dfe 10877 return empty_array (value_type (array), low_bound);
4c4b4cd2 10878 else
529cad9c
PH
10879 return ada_value_slice (array, longest_to_int (low_bound),
10880 longest_to_int (high_bound));
4c4b4cd2 10881 }
14f9c5c9 10882
4c4b4cd2
PH
10883 case UNOP_IN_RANGE:
10884 (*pos) += 2;
10885 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8008e265 10886 type = check_typedef (exp->elts[pc + 1].type);
14f9c5c9 10887
14f9c5c9 10888 if (noside == EVAL_SKIP)
4c4b4cd2 10889 goto nosideret;
14f9c5c9 10890
4c4b4cd2
PH
10891 switch (TYPE_CODE (type))
10892 {
10893 default:
e1d5a0d2
PH
10894 lim_warning (_("Membership test incompletely implemented; "
10895 "always returns true"));
fbb06eb1
UW
10896 type = language_bool_type (exp->language_defn, exp->gdbarch);
10897 return value_from_longest (type, (LONGEST) 1);
4c4b4cd2
PH
10898
10899 case TYPE_CODE_RANGE:
030b4912
UW
10900 arg2 = value_from_longest (type, TYPE_LOW_BOUND (type));
10901 arg3 = value_from_longest (type, TYPE_HIGH_BOUND (type));
f44316fa
UW
10902 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10903 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1
UW
10904 type = language_bool_type (exp->language_defn, exp->gdbarch);
10905 return
10906 value_from_longest (type,
4c4b4cd2
PH
10907 (value_less (arg1, arg3)
10908 || value_equal (arg1, arg3))
10909 && (value_less (arg2, arg1)
10910 || value_equal (arg2, arg1)));
10911 }
10912
10913 case BINOP_IN_BOUNDS:
14f9c5c9 10914 (*pos) += 2;
4c4b4cd2
PH
10915 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10916 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 10917
4c4b4cd2
PH
10918 if (noside == EVAL_SKIP)
10919 goto nosideret;
14f9c5c9 10920
4c4b4cd2 10921 if (noside == EVAL_AVOID_SIDE_EFFECTS)
fbb06eb1
UW
10922 {
10923 type = language_bool_type (exp->language_defn, exp->gdbarch);
10924 return value_zero (type, not_lval);
10925 }
14f9c5c9 10926
4c4b4cd2 10927 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 10928
1eea4ebd
UW
10929 type = ada_index_type (value_type (arg2), tem, "range");
10930 if (!type)
10931 type = value_type (arg1);
14f9c5c9 10932
1eea4ebd
UW
10933 arg3 = value_from_longest (type, ada_array_bound (arg2, tem, 1));
10934 arg2 = value_from_longest (type, ada_array_bound (arg2, tem, 0));
d2e4a39e 10935
f44316fa
UW
10936 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10937 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1 10938 type = language_bool_type (exp->language_defn, exp->gdbarch);
4c4b4cd2 10939 return
fbb06eb1 10940 value_from_longest (type,
4c4b4cd2
PH
10941 (value_less (arg1, arg3)
10942 || value_equal (arg1, arg3))
10943 && (value_less (arg2, arg1)
10944 || value_equal (arg2, arg1)));
10945
10946 case TERNOP_IN_RANGE:
10947 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10948 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10949 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10950
10951 if (noside == EVAL_SKIP)
10952 goto nosideret;
10953
f44316fa
UW
10954 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10955 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1 10956 type = language_bool_type (exp->language_defn, exp->gdbarch);
4c4b4cd2 10957 return
fbb06eb1 10958 value_from_longest (type,
4c4b4cd2
PH
10959 (value_less (arg1, arg3)
10960 || value_equal (arg1, arg3))
10961 && (value_less (arg2, arg1)
10962 || value_equal (arg2, arg1)));
10963
10964 case OP_ATR_FIRST:
10965 case OP_ATR_LAST:
10966 case OP_ATR_LENGTH:
10967 {
76a01679 10968 struct type *type_arg;
5b4ee69b 10969
76a01679
JB
10970 if (exp->elts[*pos].opcode == OP_TYPE)
10971 {
10972 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
10973 arg1 = NULL;
5bc23cb3 10974 type_arg = check_typedef (exp->elts[pc + 2].type);
76a01679
JB
10975 }
10976 else
10977 {
10978 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
10979 type_arg = NULL;
10980 }
10981
10982 if (exp->elts[*pos].opcode != OP_LONG)
323e0a4a 10983 error (_("Invalid operand to '%s"), ada_attribute_name (op));
76a01679
JB
10984 tem = longest_to_int (exp->elts[*pos + 2].longconst);
10985 *pos += 4;
10986
10987 if (noside == EVAL_SKIP)
10988 goto nosideret;
10989
10990 if (type_arg == NULL)
10991 {
10992 arg1 = ada_coerce_ref (arg1);
10993
ad82864c 10994 if (ada_is_constrained_packed_array_type (value_type (arg1)))
76a01679
JB
10995 arg1 = ada_coerce_to_simple_array (arg1);
10996
aa4fb036 10997 if (op == OP_ATR_LENGTH)
1eea4ebd 10998 type = builtin_type (exp->gdbarch)->builtin_int;
aa4fb036
JB
10999 else
11000 {
11001 type = ada_index_type (value_type (arg1), tem,
11002 ada_attribute_name (op));
11003 if (type == NULL)
11004 type = builtin_type (exp->gdbarch)->builtin_int;
11005 }
76a01679
JB
11006
11007 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1eea4ebd 11008 return allocate_value (type);
76a01679
JB
11009
11010 switch (op)
11011 {
11012 default: /* Should never happen. */
323e0a4a 11013 error (_("unexpected attribute encountered"));
76a01679 11014 case OP_ATR_FIRST:
1eea4ebd
UW
11015 return value_from_longest
11016 (type, ada_array_bound (arg1, tem, 0));
76a01679 11017 case OP_ATR_LAST:
1eea4ebd
UW
11018 return value_from_longest
11019 (type, ada_array_bound (arg1, tem, 1));
76a01679 11020 case OP_ATR_LENGTH:
1eea4ebd
UW
11021 return value_from_longest
11022 (type, ada_array_length (arg1, tem));
76a01679
JB
11023 }
11024 }
11025 else if (discrete_type_p (type_arg))
11026 {
11027 struct type *range_type;
0d5cff50 11028 const char *name = ada_type_name (type_arg);
5b4ee69b 11029
76a01679
JB
11030 range_type = NULL;
11031 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
28c85d6c 11032 range_type = to_fixed_range_type (type_arg, NULL);
76a01679
JB
11033 if (range_type == NULL)
11034 range_type = type_arg;
11035 switch (op)
11036 {
11037 default:
323e0a4a 11038 error (_("unexpected attribute encountered"));
76a01679 11039 case OP_ATR_FIRST:
690cc4eb 11040 return value_from_longest
43bbcdc2 11041 (range_type, ada_discrete_type_low_bound (range_type));
76a01679 11042 case OP_ATR_LAST:
690cc4eb 11043 return value_from_longest
43bbcdc2 11044 (range_type, ada_discrete_type_high_bound (range_type));
76a01679 11045 case OP_ATR_LENGTH:
323e0a4a 11046 error (_("the 'length attribute applies only to array types"));
76a01679
JB
11047 }
11048 }
11049 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
323e0a4a 11050 error (_("unimplemented type attribute"));
76a01679
JB
11051 else
11052 {
11053 LONGEST low, high;
11054
ad82864c
JB
11055 if (ada_is_constrained_packed_array_type (type_arg))
11056 type_arg = decode_constrained_packed_array_type (type_arg);
76a01679 11057
aa4fb036 11058 if (op == OP_ATR_LENGTH)
1eea4ebd 11059 type = builtin_type (exp->gdbarch)->builtin_int;
aa4fb036
JB
11060 else
11061 {
11062 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
11063 if (type == NULL)
11064 type = builtin_type (exp->gdbarch)->builtin_int;
11065 }
1eea4ebd 11066
76a01679
JB
11067 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11068 return allocate_value (type);
11069
11070 switch (op)
11071 {
11072 default:
323e0a4a 11073 error (_("unexpected attribute encountered"));
76a01679 11074 case OP_ATR_FIRST:
1eea4ebd 11075 low = ada_array_bound_from_type (type_arg, tem, 0);
76a01679
JB
11076 return value_from_longest (type, low);
11077 case OP_ATR_LAST:
1eea4ebd 11078 high = ada_array_bound_from_type (type_arg, tem, 1);
76a01679
JB
11079 return value_from_longest (type, high);
11080 case OP_ATR_LENGTH:
1eea4ebd
UW
11081 low = ada_array_bound_from_type (type_arg, tem, 0);
11082 high = ada_array_bound_from_type (type_arg, tem, 1);
76a01679
JB
11083 return value_from_longest (type, high - low + 1);
11084 }
11085 }
14f9c5c9
AS
11086 }
11087
4c4b4cd2
PH
11088 case OP_ATR_TAG:
11089 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11090 if (noside == EVAL_SKIP)
76a01679 11091 goto nosideret;
4c4b4cd2
PH
11092
11093 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 11094 return value_zero (ada_tag_type (arg1), not_lval);
4c4b4cd2
PH
11095
11096 return ada_value_tag (arg1);
11097
11098 case OP_ATR_MIN:
11099 case OP_ATR_MAX:
11100 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
11101 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11102 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11103 if (noside == EVAL_SKIP)
76a01679 11104 goto nosideret;
d2e4a39e 11105 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 11106 return value_zero (value_type (arg1), not_lval);
14f9c5c9 11107 else
f44316fa
UW
11108 {
11109 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
11110 return value_binop (arg1, arg2,
11111 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
11112 }
14f9c5c9 11113
4c4b4cd2
PH
11114 case OP_ATR_MODULUS:
11115 {
31dedfee 11116 struct type *type_arg = check_typedef (exp->elts[pc + 2].type);
4c4b4cd2 11117
5b4ee69b 11118 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
76a01679
JB
11119 if (noside == EVAL_SKIP)
11120 goto nosideret;
4c4b4cd2 11121
76a01679 11122 if (!ada_is_modular_type (type_arg))
323e0a4a 11123 error (_("'modulus must be applied to modular type"));
4c4b4cd2 11124
76a01679
JB
11125 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
11126 ada_modulus (type_arg));
4c4b4cd2
PH
11127 }
11128
11129
11130 case OP_ATR_POS:
11131 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
11132 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11133 if (noside == EVAL_SKIP)
76a01679 11134 goto nosideret;
3cb382c9
UW
11135 type = builtin_type (exp->gdbarch)->builtin_int;
11136 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11137 return value_zero (type, not_lval);
14f9c5c9 11138 else
3cb382c9 11139 return value_pos_atr (type, arg1);
14f9c5c9 11140
4c4b4cd2
PH
11141 case OP_ATR_SIZE:
11142 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8c1c099f
JB
11143 type = value_type (arg1);
11144
11145 /* If the argument is a reference, then dereference its type, since
11146 the user is really asking for the size of the actual object,
11147 not the size of the pointer. */
11148 if (TYPE_CODE (type) == TYPE_CODE_REF)
11149 type = TYPE_TARGET_TYPE (type);
11150
4c4b4cd2 11151 if (noside == EVAL_SKIP)
76a01679 11152 goto nosideret;
4c4b4cd2 11153 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
22601c15 11154 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
4c4b4cd2 11155 else
22601c15 11156 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
8c1c099f 11157 TARGET_CHAR_BIT * TYPE_LENGTH (type));
4c4b4cd2
PH
11158
11159 case OP_ATR_VAL:
11160 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9 11161 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4c4b4cd2 11162 type = exp->elts[pc + 2].type;
14f9c5c9 11163 if (noside == EVAL_SKIP)
76a01679 11164 goto nosideret;
4c4b4cd2 11165 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 11166 return value_zero (type, not_lval);
4c4b4cd2 11167 else
76a01679 11168 return value_val_atr (type, arg1);
4c4b4cd2
PH
11169
11170 case BINOP_EXP:
11171 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11172 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11173 if (noside == EVAL_SKIP)
11174 goto nosideret;
11175 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 11176 return value_zero (value_type (arg1), not_lval);
4c4b4cd2 11177 else
f44316fa
UW
11178 {
11179 /* For integer exponentiation operations,
11180 only promote the first argument. */
11181 if (is_integral_type (value_type (arg2)))
11182 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
11183 else
11184 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
11185
11186 return value_binop (arg1, arg2, op);
11187 }
4c4b4cd2
PH
11188
11189 case UNOP_PLUS:
11190 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11191 if (noside == EVAL_SKIP)
11192 goto nosideret;
11193 else
11194 return arg1;
11195
11196 case UNOP_ABS:
11197 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11198 if (noside == EVAL_SKIP)
11199 goto nosideret;
f44316fa 11200 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
df407dfe 11201 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
4c4b4cd2 11202 return value_neg (arg1);
14f9c5c9 11203 else
4c4b4cd2 11204 return arg1;
14f9c5c9
AS
11205
11206 case UNOP_IND:
5ec18f2b 11207 preeval_pos = *pos;
6b0d7253 11208 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 11209 if (noside == EVAL_SKIP)
4c4b4cd2 11210 goto nosideret;
df407dfe 11211 type = ada_check_typedef (value_type (arg1));
14f9c5c9 11212 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
11213 {
11214 if (ada_is_array_descriptor_type (type))
11215 /* GDB allows dereferencing GNAT array descriptors. */
11216 {
11217 struct type *arrType = ada_type_of_array (arg1, 0);
5b4ee69b 11218
4c4b4cd2 11219 if (arrType == NULL)
323e0a4a 11220 error (_("Attempt to dereference null array pointer."));
00a4c844 11221 return value_at_lazy (arrType, 0);
4c4b4cd2
PH
11222 }
11223 else if (TYPE_CODE (type) == TYPE_CODE_PTR
11224 || TYPE_CODE (type) == TYPE_CODE_REF
11225 /* In C you can dereference an array to get the 1st elt. */
11226 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
714e53ab 11227 {
5ec18f2b
JG
11228 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11229 only be determined by inspecting the object's tag.
11230 This means that we need to evaluate completely the
11231 expression in order to get its type. */
11232
023db19c
JB
11233 if ((TYPE_CODE (type) == TYPE_CODE_REF
11234 || TYPE_CODE (type) == TYPE_CODE_PTR)
5ec18f2b
JG
11235 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
11236 {
11237 arg1 = evaluate_subexp (NULL_TYPE, exp, &preeval_pos,
11238 EVAL_NORMAL);
11239 type = value_type (ada_value_ind (arg1));
11240 }
11241 else
11242 {
11243 type = to_static_fixed_type
11244 (ada_aligned_type
11245 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
11246 }
c1b5a1a6 11247 ada_ensure_varsize_limit (type);
714e53ab
PH
11248 return value_zero (type, lval_memory);
11249 }
4c4b4cd2 11250 else if (TYPE_CODE (type) == TYPE_CODE_INT)
6b0d7253
JB
11251 {
11252 /* GDB allows dereferencing an int. */
11253 if (expect_type == NULL)
11254 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11255 lval_memory);
11256 else
11257 {
11258 expect_type =
11259 to_static_fixed_type (ada_aligned_type (expect_type));
11260 return value_zero (expect_type, lval_memory);
11261 }
11262 }
4c4b4cd2 11263 else
323e0a4a 11264 error (_("Attempt to take contents of a non-pointer value."));
4c4b4cd2 11265 }
0963b4bd 11266 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
df407dfe 11267 type = ada_check_typedef (value_type (arg1));
d2e4a39e 11268
96967637
JB
11269 if (TYPE_CODE (type) == TYPE_CODE_INT)
11270 /* GDB allows dereferencing an int. If we were given
11271 the expect_type, then use that as the target type.
11272 Otherwise, assume that the target type is an int. */
11273 {
11274 if (expect_type != NULL)
11275 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11276 arg1));
11277 else
11278 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11279 (CORE_ADDR) value_as_address (arg1));
11280 }
6b0d7253 11281
4c4b4cd2
PH
11282 if (ada_is_array_descriptor_type (type))
11283 /* GDB allows dereferencing GNAT array descriptors. */
11284 return ada_coerce_to_simple_array (arg1);
14f9c5c9 11285 else
4c4b4cd2 11286 return ada_value_ind (arg1);
14f9c5c9
AS
11287
11288 case STRUCTOP_STRUCT:
11289 tem = longest_to_int (exp->elts[pc + 1].longconst);
11290 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
5ec18f2b 11291 preeval_pos = *pos;
14f9c5c9
AS
11292 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
11293 if (noside == EVAL_SKIP)
4c4b4cd2 11294 goto nosideret;
14f9c5c9 11295 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 11296 {
df407dfe 11297 struct type *type1 = value_type (arg1);
5b4ee69b 11298
76a01679
JB
11299 if (ada_is_tagged_type (type1, 1))
11300 {
11301 type = ada_lookup_struct_elt_type (type1,
11302 &exp->elts[pc + 2].string,
11303 1, 1, NULL);
5ec18f2b
JG
11304
11305 /* If the field is not found, check if it exists in the
11306 extension of this object's type. This means that we
11307 need to evaluate completely the expression. */
11308
76a01679 11309 if (type == NULL)
5ec18f2b
JG
11310 {
11311 arg1 = evaluate_subexp (NULL_TYPE, exp, &preeval_pos,
11312 EVAL_NORMAL);
11313 arg1 = ada_value_struct_elt (arg1,
11314 &exp->elts[pc + 2].string,
11315 0);
11316 arg1 = unwrap_value (arg1);
11317 type = value_type (ada_to_fixed_value (arg1));
11318 }
76a01679
JB
11319 }
11320 else
11321 type =
11322 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
11323 0, NULL);
11324
11325 return value_zero (ada_aligned_type (type), lval_memory);
11326 }
14f9c5c9 11327 else
284614f0
JB
11328 arg1 = ada_value_struct_elt (arg1, &exp->elts[pc + 2].string, 0);
11329 arg1 = unwrap_value (arg1);
11330 return ada_to_fixed_value (arg1);
11331
14f9c5c9 11332 case OP_TYPE:
4c4b4cd2
PH
11333 /* The value is not supposed to be used. This is here to make it
11334 easier to accommodate expressions that contain types. */
14f9c5c9
AS
11335 (*pos) += 2;
11336 if (noside == EVAL_SKIP)
4c4b4cd2 11337 goto nosideret;
14f9c5c9 11338 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
a6cfbe68 11339 return allocate_value (exp->elts[pc + 1].type);
14f9c5c9 11340 else
323e0a4a 11341 error (_("Attempt to use a type name as an expression"));
52ce6436
PH
11342
11343 case OP_AGGREGATE:
11344 case OP_CHOICES:
11345 case OP_OTHERS:
11346 case OP_DISCRETE_RANGE:
11347 case OP_POSITIONAL:
11348 case OP_NAME:
11349 if (noside == EVAL_NORMAL)
11350 switch (op)
11351 {
11352 case OP_NAME:
11353 error (_("Undefined name, ambiguous name, or renaming used in "
e1d5a0d2 11354 "component association: %s."), &exp->elts[pc+2].string);
52ce6436
PH
11355 case OP_AGGREGATE:
11356 error (_("Aggregates only allowed on the right of an assignment"));
11357 default:
0963b4bd
MS
11358 internal_error (__FILE__, __LINE__,
11359 _("aggregate apparently mangled"));
52ce6436
PH
11360 }
11361
11362 ada_forward_operator_length (exp, pc, &oplen, &nargs);
11363 *pos += oplen - 1;
11364 for (tem = 0; tem < nargs; tem += 1)
11365 ada_evaluate_subexp (NULL, exp, pos, noside);
11366 goto nosideret;
14f9c5c9
AS
11367 }
11368
11369nosideret:
22601c15 11370 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
14f9c5c9 11371}
14f9c5c9 11372\f
d2e4a39e 11373
4c4b4cd2 11374 /* Fixed point */
14f9c5c9
AS
11375
11376/* If TYPE encodes an Ada fixed-point type, return the suffix of the
11377 type name that encodes the 'small and 'delta information.
4c4b4cd2 11378 Otherwise, return NULL. */
14f9c5c9 11379
d2e4a39e 11380static const char *
ebf56fd3 11381fixed_type_info (struct type *type)
14f9c5c9 11382{
d2e4a39e 11383 const char *name = ada_type_name (type);
14f9c5c9
AS
11384 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
11385
d2e4a39e
AS
11386 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
11387 {
14f9c5c9 11388 const char *tail = strstr (name, "___XF_");
5b4ee69b 11389
14f9c5c9 11390 if (tail == NULL)
4c4b4cd2 11391 return NULL;
d2e4a39e 11392 else
4c4b4cd2 11393 return tail + 5;
14f9c5c9
AS
11394 }
11395 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
11396 return fixed_type_info (TYPE_TARGET_TYPE (type));
11397 else
11398 return NULL;
11399}
11400
4c4b4cd2 11401/* Returns non-zero iff TYPE represents an Ada fixed-point type. */
14f9c5c9
AS
11402
11403int
ebf56fd3 11404ada_is_fixed_point_type (struct type *type)
14f9c5c9
AS
11405{
11406 return fixed_type_info (type) != NULL;
11407}
11408
4c4b4cd2
PH
11409/* Return non-zero iff TYPE represents a System.Address type. */
11410
11411int
11412ada_is_system_address_type (struct type *type)
11413{
11414 return (TYPE_NAME (type)
11415 && strcmp (TYPE_NAME (type), "system__address") == 0);
11416}
11417
14f9c5c9
AS
11418/* Assuming that TYPE is the representation of an Ada fixed-point
11419 type, return its delta, or -1 if the type is malformed and the
4c4b4cd2 11420 delta cannot be determined. */
14f9c5c9
AS
11421
11422DOUBLEST
ebf56fd3 11423ada_delta (struct type *type)
14f9c5c9
AS
11424{
11425 const char *encoding = fixed_type_info (type);
facc390f 11426 DOUBLEST num, den;
14f9c5c9 11427
facc390f
JB
11428 /* Strictly speaking, num and den are encoded as integer. However,
11429 they may not fit into a long, and they will have to be converted
11430 to DOUBLEST anyway. So scan them as DOUBLEST. */
11431 if (sscanf (encoding, "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
11432 &num, &den) < 2)
14f9c5c9 11433 return -1.0;
d2e4a39e 11434 else
facc390f 11435 return num / den;
14f9c5c9
AS
11436}
11437
11438/* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
4c4b4cd2 11439 factor ('SMALL value) associated with the type. */
14f9c5c9
AS
11440
11441static DOUBLEST
ebf56fd3 11442scaling_factor (struct type *type)
14f9c5c9
AS
11443{
11444 const char *encoding = fixed_type_info (type);
facc390f 11445 DOUBLEST num0, den0, num1, den1;
14f9c5c9 11446 int n;
d2e4a39e 11447
facc390f
JB
11448 /* Strictly speaking, num's and den's are encoded as integer. However,
11449 they may not fit into a long, and they will have to be converted
11450 to DOUBLEST anyway. So scan them as DOUBLEST. */
11451 n = sscanf (encoding,
11452 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT
11453 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
11454 &num0, &den0, &num1, &den1);
14f9c5c9
AS
11455
11456 if (n < 2)
11457 return 1.0;
11458 else if (n == 4)
facc390f 11459 return num1 / den1;
d2e4a39e 11460 else
facc390f 11461 return num0 / den0;
14f9c5c9
AS
11462}
11463
11464
11465/* Assuming that X is the representation of a value of fixed-point
4c4b4cd2 11466 type TYPE, return its floating-point equivalent. */
14f9c5c9
AS
11467
11468DOUBLEST
ebf56fd3 11469ada_fixed_to_float (struct type *type, LONGEST x)
14f9c5c9 11470{
d2e4a39e 11471 return (DOUBLEST) x *scaling_factor (type);
14f9c5c9
AS
11472}
11473
4c4b4cd2
PH
11474/* The representation of a fixed-point value of type TYPE
11475 corresponding to the value X. */
14f9c5c9
AS
11476
11477LONGEST
ebf56fd3 11478ada_float_to_fixed (struct type *type, DOUBLEST x)
14f9c5c9
AS
11479{
11480 return (LONGEST) (x / scaling_factor (type) + 0.5);
11481}
11482
14f9c5c9 11483\f
d2e4a39e 11484
4c4b4cd2 11485 /* Range types */
14f9c5c9
AS
11486
11487/* Scan STR beginning at position K for a discriminant name, and
11488 return the value of that discriminant field of DVAL in *PX. If
11489 PNEW_K is not null, put the position of the character beyond the
11490 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11491 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11492
11493static int
108d56a4 11494scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
76a01679 11495 int *pnew_k)
14f9c5c9
AS
11496{
11497 static char *bound_buffer = NULL;
11498 static size_t bound_buffer_len = 0;
5da1a4d3 11499 const char *pstart, *pend, *bound;
d2e4a39e 11500 struct value *bound_val;
14f9c5c9
AS
11501
11502 if (dval == NULL || str == NULL || str[k] == '\0')
11503 return 0;
11504
5da1a4d3
SM
11505 pstart = str + k;
11506 pend = strstr (pstart, "__");
14f9c5c9
AS
11507 if (pend == NULL)
11508 {
5da1a4d3 11509 bound = pstart;
14f9c5c9
AS
11510 k += strlen (bound);
11511 }
d2e4a39e 11512 else
14f9c5c9 11513 {
5da1a4d3
SM
11514 int len = pend - pstart;
11515
11516 /* Strip __ and beyond. */
11517 GROW_VECT (bound_buffer, bound_buffer_len, len + 1);
11518 strncpy (bound_buffer, pstart, len);
11519 bound_buffer[len] = '\0';
11520
14f9c5c9 11521 bound = bound_buffer;
d2e4a39e 11522 k = pend - str;
14f9c5c9 11523 }
d2e4a39e 11524
df407dfe 11525 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
11526 if (bound_val == NULL)
11527 return 0;
11528
11529 *px = value_as_long (bound_val);
11530 if (pnew_k != NULL)
11531 *pnew_k = k;
11532 return 1;
11533}
11534
11535/* Value of variable named NAME in the current environment. If
11536 no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11537 otherwise causes an error with message ERR_MSG. */
11538
d2e4a39e
AS
11539static struct value *
11540get_var_value (char *name, char *err_msg)
14f9c5c9 11541{
d12307c1 11542 struct block_symbol *syms;
14f9c5c9
AS
11543 int nsyms;
11544
4c4b4cd2 11545 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
4eeaa230 11546 &syms);
14f9c5c9
AS
11547
11548 if (nsyms != 1)
11549 {
11550 if (err_msg == NULL)
4c4b4cd2 11551 return 0;
14f9c5c9 11552 else
8a3fe4f8 11553 error (("%s"), err_msg);
14f9c5c9
AS
11554 }
11555
d12307c1 11556 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11557}
d2e4a39e 11558
14f9c5c9 11559/* Value of integer variable named NAME in the current environment. If
4c4b4cd2
PH
11560 no such variable found, returns 0, and sets *FLAG to 0. If
11561 successful, sets *FLAG to 1. */
11562
14f9c5c9 11563LONGEST
4c4b4cd2 11564get_int_var_value (char *name, int *flag)
14f9c5c9 11565{
4c4b4cd2 11566 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11567
14f9c5c9
AS
11568 if (var_val == 0)
11569 {
11570 if (flag != NULL)
4c4b4cd2 11571 *flag = 0;
14f9c5c9
AS
11572 return 0;
11573 }
11574 else
11575 {
11576 if (flag != NULL)
4c4b4cd2 11577 *flag = 1;
14f9c5c9
AS
11578 return value_as_long (var_val);
11579 }
11580}
d2e4a39e 11581
14f9c5c9
AS
11582
11583/* Return a range type whose base type is that of the range type named
11584 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11585 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11586 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11587 corresponding range type from debug information; fall back to using it
11588 if symbol lookup fails. If a new type must be created, allocate it
11589 like ORIG_TYPE was. The bounds information, in general, is encoded
11590 in NAME, the base type given in the named range type. */
14f9c5c9 11591
d2e4a39e 11592static struct type *
28c85d6c 11593to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11594{
0d5cff50 11595 const char *name;
14f9c5c9 11596 struct type *base_type;
108d56a4 11597 const char *subtype_info;
14f9c5c9 11598
28c85d6c
JB
11599 gdb_assert (raw_type != NULL);
11600 gdb_assert (TYPE_NAME (raw_type) != NULL);
dddfab26 11601
1ce677a4 11602 if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
14f9c5c9
AS
11603 base_type = TYPE_TARGET_TYPE (raw_type);
11604 else
11605 base_type = raw_type;
11606
28c85d6c 11607 name = TYPE_NAME (raw_type);
14f9c5c9
AS
11608 subtype_info = strstr (name, "___XD");
11609 if (subtype_info == NULL)
690cc4eb 11610 {
43bbcdc2
PH
11611 LONGEST L = ada_discrete_type_low_bound (raw_type);
11612 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11613
690cc4eb
PH
11614 if (L < INT_MIN || U > INT_MAX)
11615 return raw_type;
11616 else
0c9c3474
SA
11617 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
11618 L, U);
690cc4eb 11619 }
14f9c5c9
AS
11620 else
11621 {
11622 static char *name_buf = NULL;
11623 static size_t name_len = 0;
11624 int prefix_len = subtype_info - name;
11625 LONGEST L, U;
11626 struct type *type;
108d56a4 11627 const char *bounds_str;
14f9c5c9
AS
11628 int n;
11629
11630 GROW_VECT (name_buf, name_len, prefix_len + 5);
11631 strncpy (name_buf, name, prefix_len);
11632 name_buf[prefix_len] = '\0';
11633
11634 subtype_info += 5;
11635 bounds_str = strchr (subtype_info, '_');
11636 n = 1;
11637
d2e4a39e 11638 if (*subtype_info == 'L')
4c4b4cd2
PH
11639 {
11640 if (!ada_scan_number (bounds_str, n, &L, &n)
11641 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11642 return raw_type;
11643 if (bounds_str[n] == '_')
11644 n += 2;
0963b4bd 11645 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
4c4b4cd2
PH
11646 n += 1;
11647 subtype_info += 1;
11648 }
d2e4a39e 11649 else
4c4b4cd2
PH
11650 {
11651 int ok;
5b4ee69b 11652
4c4b4cd2
PH
11653 strcpy (name_buf + prefix_len, "___L");
11654 L = get_int_var_value (name_buf, &ok);
11655 if (!ok)
11656 {
323e0a4a 11657 lim_warning (_("Unknown lower bound, using 1."));
4c4b4cd2
PH
11658 L = 1;
11659 }
11660 }
14f9c5c9 11661
d2e4a39e 11662 if (*subtype_info == 'U')
4c4b4cd2
PH
11663 {
11664 if (!ada_scan_number (bounds_str, n, &U, &n)
11665 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11666 return raw_type;
11667 }
d2e4a39e 11668 else
4c4b4cd2
PH
11669 {
11670 int ok;
5b4ee69b 11671
4c4b4cd2
PH
11672 strcpy (name_buf + prefix_len, "___U");
11673 U = get_int_var_value (name_buf, &ok);
11674 if (!ok)
11675 {
323e0a4a 11676 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
4c4b4cd2
PH
11677 U = L;
11678 }
11679 }
14f9c5c9 11680
0c9c3474
SA
11681 type = create_static_range_type (alloc_type_copy (raw_type),
11682 base_type, L, U);
d2e4a39e 11683 TYPE_NAME (type) = name;
14f9c5c9
AS
11684 return type;
11685 }
11686}
11687
4c4b4cd2
PH
11688/* True iff NAME is the name of a range type. */
11689
14f9c5c9 11690int
d2e4a39e 11691ada_is_range_type_name (const char *name)
14f9c5c9
AS
11692{
11693 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11694}
14f9c5c9 11695\f
d2e4a39e 11696
4c4b4cd2
PH
11697 /* Modular types */
11698
11699/* True iff TYPE is an Ada modular type. */
14f9c5c9 11700
14f9c5c9 11701int
d2e4a39e 11702ada_is_modular_type (struct type *type)
14f9c5c9 11703{
18af8284 11704 struct type *subranged_type = get_base_type (type);
14f9c5c9
AS
11705
11706 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
690cc4eb 11707 && TYPE_CODE (subranged_type) == TYPE_CODE_INT
4c4b4cd2 11708 && TYPE_UNSIGNED (subranged_type));
14f9c5c9
AS
11709}
11710
4c4b4cd2
PH
11711/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11712
61ee279c 11713ULONGEST
0056e4d5 11714ada_modulus (struct type *type)
14f9c5c9 11715{
43bbcdc2 11716 return (ULONGEST) TYPE_HIGH_BOUND (type) + 1;
14f9c5c9 11717}
d2e4a39e 11718\f
f7f9143b
JB
11719
11720/* Ada exception catchpoint support:
11721 ---------------------------------
11722
11723 We support 3 kinds of exception catchpoints:
11724 . catchpoints on Ada exceptions
11725 . catchpoints on unhandled Ada exceptions
11726 . catchpoints on failed assertions
11727
11728 Exceptions raised during failed assertions, or unhandled exceptions
11729 could perfectly be caught with the general catchpoint on Ada exceptions.
11730 However, we can easily differentiate these two special cases, and having
11731 the option to distinguish these two cases from the rest can be useful
11732 to zero-in on certain situations.
11733
11734 Exception catchpoints are a specialized form of breakpoint,
11735 since they rely on inserting breakpoints inside known routines
11736 of the GNAT runtime. The implementation therefore uses a standard
11737 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11738 of breakpoint_ops.
11739
0259addd
JB
11740 Support in the runtime for exception catchpoints have been changed
11741 a few times already, and these changes affect the implementation
11742 of these catchpoints. In order to be able to support several
11743 variants of the runtime, we use a sniffer that will determine
28010a5d 11744 the runtime variant used by the program being debugged. */
f7f9143b 11745
82eacd52
JB
11746/* Ada's standard exceptions.
11747
11748 The Ada 83 standard also defined Numeric_Error. But there so many
11749 situations where it was unclear from the Ada 83 Reference Manual
11750 (RM) whether Constraint_Error or Numeric_Error should be raised,
11751 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11752 Interpretation saying that anytime the RM says that Numeric_Error
11753 should be raised, the implementation may raise Constraint_Error.
11754 Ada 95 went one step further and pretty much removed Numeric_Error
11755 from the list of standard exceptions (it made it a renaming of
11756 Constraint_Error, to help preserve compatibility when compiling
11757 an Ada83 compiler). As such, we do not include Numeric_Error from
11758 this list of standard exceptions. */
3d0b0fa3
JB
11759
11760static char *standard_exc[] = {
11761 "constraint_error",
11762 "program_error",
11763 "storage_error",
11764 "tasking_error"
11765};
11766
0259addd
JB
11767typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11768
11769/* A structure that describes how to support exception catchpoints
11770 for a given executable. */
11771
11772struct exception_support_info
11773{
11774 /* The name of the symbol to break on in order to insert
11775 a catchpoint on exceptions. */
11776 const char *catch_exception_sym;
11777
11778 /* The name of the symbol to break on in order to insert
11779 a catchpoint on unhandled exceptions. */
11780 const char *catch_exception_unhandled_sym;
11781
11782 /* The name of the symbol to break on in order to insert
11783 a catchpoint on failed assertions. */
11784 const char *catch_assert_sym;
11785
11786 /* Assuming that the inferior just triggered an unhandled exception
11787 catchpoint, this function is responsible for returning the address
11788 in inferior memory where the name of that exception is stored.
11789 Return zero if the address could not be computed. */
11790 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11791};
11792
11793static CORE_ADDR ada_unhandled_exception_name_addr (void);
11794static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11795
11796/* The following exception support info structure describes how to
11797 implement exception catchpoints with the latest version of the
11798 Ada runtime (as of 2007-03-06). */
11799
11800static const struct exception_support_info default_exception_support_info =
11801{
11802 "__gnat_debug_raise_exception", /* catch_exception_sym */
11803 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11804 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11805 ada_unhandled_exception_name_addr
11806};
11807
11808/* The following exception support info structure describes how to
11809 implement exception catchpoints with a slightly older version
11810 of the Ada runtime. */
11811
11812static const struct exception_support_info exception_support_info_fallback =
11813{
11814 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11815 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11816 "system__assertions__raise_assert_failure", /* catch_assert_sym */
11817 ada_unhandled_exception_name_addr_from_raise
11818};
11819
f17011e0
JB
11820/* Return nonzero if we can detect the exception support routines
11821 described in EINFO.
11822
11823 This function errors out if an abnormal situation is detected
11824 (for instance, if we find the exception support routines, but
11825 that support is found to be incomplete). */
11826
11827static int
11828ada_has_this_exception_support (const struct exception_support_info *einfo)
11829{
11830 struct symbol *sym;
11831
11832 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11833 that should be compiled with debugging information. As a result, we
11834 expect to find that symbol in the symtabs. */
11835
11836 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11837 if (sym == NULL)
a6af7abe
JB
11838 {
11839 /* Perhaps we did not find our symbol because the Ada runtime was
11840 compiled without debugging info, or simply stripped of it.
11841 It happens on some GNU/Linux distributions for instance, where
11842 users have to install a separate debug package in order to get
11843 the runtime's debugging info. In that situation, let the user
11844 know why we cannot insert an Ada exception catchpoint.
11845
11846 Note: Just for the purpose of inserting our Ada exception
11847 catchpoint, we could rely purely on the associated minimal symbol.
11848 But we would be operating in degraded mode anyway, since we are
11849 still lacking the debugging info needed later on to extract
11850 the name of the exception being raised (this name is printed in
11851 the catchpoint message, and is also used when trying to catch
11852 a specific exception). We do not handle this case for now. */
3b7344d5 11853 struct bound_minimal_symbol msym
1c8e84b0
JB
11854 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11855
3b7344d5 11856 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
a6af7abe
JB
11857 error (_("Your Ada runtime appears to be missing some debugging "
11858 "information.\nCannot insert Ada exception catchpoint "
11859 "in this configuration."));
11860
11861 return 0;
11862 }
f17011e0
JB
11863
11864 /* Make sure that the symbol we found corresponds to a function. */
11865
11866 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11867 error (_("Symbol \"%s\" is not a function (class = %d)"),
11868 SYMBOL_LINKAGE_NAME (sym), SYMBOL_CLASS (sym));
11869
11870 return 1;
11871}
11872
0259addd
JB
11873/* Inspect the Ada runtime and determine which exception info structure
11874 should be used to provide support for exception catchpoints.
11875
3eecfa55
JB
11876 This function will always set the per-inferior exception_info,
11877 or raise an error. */
0259addd
JB
11878
11879static void
11880ada_exception_support_info_sniffer (void)
11881{
3eecfa55 11882 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11883
11884 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11885 if (data->exception_info != NULL)
0259addd
JB
11886 return;
11887
11888 /* Check the latest (default) exception support info. */
f17011e0 11889 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11890 {
3eecfa55 11891 data->exception_info = &default_exception_support_info;
0259addd
JB
11892 return;
11893 }
11894
11895 /* Try our fallback exception suport info. */
f17011e0 11896 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11897 {
3eecfa55 11898 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11899 return;
11900 }
11901
11902 /* Sometimes, it is normal for us to not be able to find the routine
11903 we are looking for. This happens when the program is linked with
11904 the shared version of the GNAT runtime, and the program has not been
11905 started yet. Inform the user of these two possible causes if
11906 applicable. */
11907
ccefe4c4 11908 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11909 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11910
11911 /* If the symbol does not exist, then check that the program is
11912 already started, to make sure that shared libraries have been
11913 loaded. If it is not started, this may mean that the symbol is
11914 in a shared library. */
11915
11916 if (ptid_get_pid (inferior_ptid) == 0)
11917 error (_("Unable to insert catchpoint. Try to start the program first."));
11918
11919 /* At this point, we know that we are debugging an Ada program and
11920 that the inferior has been started, but we still are not able to
0963b4bd 11921 find the run-time symbols. That can mean that we are in
0259addd
JB
11922 configurable run time mode, or that a-except as been optimized
11923 out by the linker... In any case, at this point it is not worth
11924 supporting this feature. */
11925
7dda8cff 11926 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11927}
11928
f7f9143b
JB
11929/* True iff FRAME is very likely to be that of a function that is
11930 part of the runtime system. This is all very heuristic, but is
11931 intended to be used as advice as to what frames are uninteresting
11932 to most users. */
11933
11934static int
11935is_known_support_routine (struct frame_info *frame)
11936{
4ed6b5be 11937 struct symtab_and_line sal;
55b87a52 11938 char *func_name;
692465f1 11939 enum language func_lang;
f7f9143b 11940 int i;
f35a17b5 11941 const char *fullname;
f7f9143b 11942
4ed6b5be
JB
11943 /* If this code does not have any debugging information (no symtab),
11944 This cannot be any user code. */
f7f9143b 11945
4ed6b5be 11946 find_frame_sal (frame, &sal);
f7f9143b
JB
11947 if (sal.symtab == NULL)
11948 return 1;
11949
4ed6b5be
JB
11950 /* If there is a symtab, but the associated source file cannot be
11951 located, then assume this is not user code: Selecting a frame
11952 for which we cannot display the code would not be very helpful
11953 for the user. This should also take care of case such as VxWorks
11954 where the kernel has some debugging info provided for a few units. */
f7f9143b 11955
f35a17b5
JK
11956 fullname = symtab_to_fullname (sal.symtab);
11957 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11958 return 1;
11959
4ed6b5be
JB
11960 /* Check the unit filename againt the Ada runtime file naming.
11961 We also check the name of the objfile against the name of some
11962 known system libraries that sometimes come with debugging info
11963 too. */
11964
f7f9143b
JB
11965 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11966 {
11967 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11968 if (re_exec (lbasename (sal.symtab->filename)))
f7f9143b 11969 return 1;
eb822aa6
DE
11970 if (SYMTAB_OBJFILE (sal.symtab) != NULL
11971 && re_exec (objfile_name (SYMTAB_OBJFILE (sal.symtab))))
4ed6b5be 11972 return 1;
f7f9143b
JB
11973 }
11974
4ed6b5be 11975 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11976
e9e07ba6 11977 find_frame_funname (frame, &func_name, &func_lang, NULL);
f7f9143b
JB
11978 if (func_name == NULL)
11979 return 1;
11980
11981 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11982 {
11983 re_comp (known_auxiliary_function_name_patterns[i]);
11984 if (re_exec (func_name))
55b87a52
KS
11985 {
11986 xfree (func_name);
11987 return 1;
11988 }
f7f9143b
JB
11989 }
11990
55b87a52 11991 xfree (func_name);
f7f9143b
JB
11992 return 0;
11993}
11994
11995/* Find the first frame that contains debugging information and that is not
11996 part of the Ada run-time, starting from FI and moving upward. */
11997
0ef643c8 11998void
f7f9143b
JB
11999ada_find_printable_frame (struct frame_info *fi)
12000{
12001 for (; fi != NULL; fi = get_prev_frame (fi))
12002 {
12003 if (!is_known_support_routine (fi))
12004 {
12005 select_frame (fi);
12006 break;
12007 }
12008 }
12009
12010}
12011
12012/* Assuming that the inferior just triggered an unhandled exception
12013 catchpoint, return the address in inferior memory where the name
12014 of the exception is stored.
12015
12016 Return zero if the address could not be computed. */
12017
12018static CORE_ADDR
12019ada_unhandled_exception_name_addr (void)
0259addd
JB
12020{
12021 return parse_and_eval_address ("e.full_name");
12022}
12023
12024/* Same as ada_unhandled_exception_name_addr, except that this function
12025 should be used when the inferior uses an older version of the runtime,
12026 where the exception name needs to be extracted from a specific frame
12027 several frames up in the callstack. */
12028
12029static CORE_ADDR
12030ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
12031{
12032 int frame_level;
12033 struct frame_info *fi;
3eecfa55 12034 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
55b87a52 12035 struct cleanup *old_chain;
f7f9143b
JB
12036
12037 /* To determine the name of this exception, we need to select
12038 the frame corresponding to RAISE_SYM_NAME. This frame is
12039 at least 3 levels up, so we simply skip the first 3 frames
12040 without checking the name of their associated function. */
12041 fi = get_current_frame ();
12042 for (frame_level = 0; frame_level < 3; frame_level += 1)
12043 if (fi != NULL)
12044 fi = get_prev_frame (fi);
12045
55b87a52 12046 old_chain = make_cleanup (null_cleanup, NULL);
f7f9143b
JB
12047 while (fi != NULL)
12048 {
55b87a52 12049 char *func_name;
692465f1
JB
12050 enum language func_lang;
12051
e9e07ba6 12052 find_frame_funname (fi, &func_name, &func_lang, NULL);
55b87a52
KS
12053 if (func_name != NULL)
12054 {
12055 make_cleanup (xfree, func_name);
12056
12057 if (strcmp (func_name,
12058 data->exception_info->catch_exception_sym) == 0)
12059 break; /* We found the frame we were looking for... */
12060 fi = get_prev_frame (fi);
12061 }
f7f9143b 12062 }
55b87a52 12063 do_cleanups (old_chain);
f7f9143b
JB
12064
12065 if (fi == NULL)
12066 return 0;
12067
12068 select_frame (fi);
12069 return parse_and_eval_address ("id.full_name");
12070}
12071
12072/* Assuming the inferior just triggered an Ada exception catchpoint
12073 (of any type), return the address in inferior memory where the name
12074 of the exception is stored, if applicable.
12075
12076 Return zero if the address could not be computed, or if not relevant. */
12077
12078static CORE_ADDR
761269c8 12079ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex,
f7f9143b
JB
12080 struct breakpoint *b)
12081{
3eecfa55
JB
12082 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12083
f7f9143b
JB
12084 switch (ex)
12085 {
761269c8 12086 case ada_catch_exception:
f7f9143b
JB
12087 return (parse_and_eval_address ("e.full_name"));
12088 break;
12089
761269c8 12090 case ada_catch_exception_unhandled:
3eecfa55 12091 return data->exception_info->unhandled_exception_name_addr ();
f7f9143b
JB
12092 break;
12093
761269c8 12094 case ada_catch_assert:
f7f9143b
JB
12095 return 0; /* Exception name is not relevant in this case. */
12096 break;
12097
12098 default:
12099 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12100 break;
12101 }
12102
12103 return 0; /* Should never be reached. */
12104}
12105
12106/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12107 any error that ada_exception_name_addr_1 might cause to be thrown.
12108 When an error is intercepted, a warning with the error message is printed,
12109 and zero is returned. */
12110
12111static CORE_ADDR
761269c8 12112ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
f7f9143b
JB
12113 struct breakpoint *b)
12114{
f7f9143b
JB
12115 CORE_ADDR result = 0;
12116
492d29ea 12117 TRY
f7f9143b
JB
12118 {
12119 result = ada_exception_name_addr_1 (ex, b);
12120 }
12121
492d29ea 12122 CATCH (e, RETURN_MASK_ERROR)
f7f9143b
JB
12123 {
12124 warning (_("failed to get exception name: %s"), e.message);
12125 return 0;
12126 }
492d29ea 12127 END_CATCH
f7f9143b
JB
12128
12129 return result;
12130}
12131
28010a5d
PA
12132static char *ada_exception_catchpoint_cond_string (const char *excep_string);
12133
12134/* Ada catchpoints.
12135
12136 In the case of catchpoints on Ada exceptions, the catchpoint will
12137 stop the target on every exception the program throws. When a user
12138 specifies the name of a specific exception, we translate this
12139 request into a condition expression (in text form), and then parse
12140 it into an expression stored in each of the catchpoint's locations.
12141 We then use this condition to check whether the exception that was
12142 raised is the one the user is interested in. If not, then the
12143 target is resumed again. We store the name of the requested
12144 exception, in order to be able to re-set the condition expression
12145 when symbols change. */
12146
12147/* An instance of this type is used to represent an Ada catchpoint
12148 breakpoint location. It includes a "struct bp_location" as a kind
12149 of base class; users downcast to "struct bp_location *" when
12150 needed. */
12151
12152struct ada_catchpoint_location
12153{
12154 /* The base class. */
12155 struct bp_location base;
12156
12157 /* The condition that checks whether the exception that was raised
12158 is the specific exception the user specified on catchpoint
12159 creation. */
12160 struct expression *excep_cond_expr;
12161};
12162
12163/* Implement the DTOR method in the bp_location_ops structure for all
12164 Ada exception catchpoint kinds. */
12165
12166static void
12167ada_catchpoint_location_dtor (struct bp_location *bl)
12168{
12169 struct ada_catchpoint_location *al = (struct ada_catchpoint_location *) bl;
12170
12171 xfree (al->excep_cond_expr);
12172}
12173
12174/* The vtable to be used in Ada catchpoint locations. */
12175
12176static const struct bp_location_ops ada_catchpoint_location_ops =
12177{
12178 ada_catchpoint_location_dtor
12179};
12180
12181/* An instance of this type is used to represent an Ada catchpoint.
12182 It includes a "struct breakpoint" as a kind of base class; users
12183 downcast to "struct breakpoint *" when needed. */
12184
12185struct ada_catchpoint
12186{
12187 /* The base class. */
12188 struct breakpoint base;
12189
12190 /* The name of the specific exception the user specified. */
12191 char *excep_string;
12192};
12193
12194/* Parse the exception condition string in the context of each of the
12195 catchpoint's locations, and store them for later evaluation. */
12196
12197static void
12198create_excep_cond_exprs (struct ada_catchpoint *c)
12199{
12200 struct cleanup *old_chain;
12201 struct bp_location *bl;
12202 char *cond_string;
12203
12204 /* Nothing to do if there's no specific exception to catch. */
12205 if (c->excep_string == NULL)
12206 return;
12207
12208 /* Same if there are no locations... */
12209 if (c->base.loc == NULL)
12210 return;
12211
12212 /* Compute the condition expression in text form, from the specific
12213 expection we want to catch. */
12214 cond_string = ada_exception_catchpoint_cond_string (c->excep_string);
12215 old_chain = make_cleanup (xfree, cond_string);
12216
12217 /* Iterate over all the catchpoint's locations, and parse an
12218 expression for each. */
12219 for (bl = c->base.loc; bl != NULL; bl = bl->next)
12220 {
12221 struct ada_catchpoint_location *ada_loc
12222 = (struct ada_catchpoint_location *) bl;
12223 struct expression *exp = NULL;
12224
12225 if (!bl->shlib_disabled)
12226 {
bbc13ae3 12227 const char *s;
28010a5d
PA
12228
12229 s = cond_string;
492d29ea 12230 TRY
28010a5d 12231 {
1bb9788d
TT
12232 exp = parse_exp_1 (&s, bl->address,
12233 block_for_pc (bl->address), 0);
28010a5d 12234 }
492d29ea 12235 CATCH (e, RETURN_MASK_ERROR)
849f2b52
JB
12236 {
12237 warning (_("failed to reevaluate internal exception condition "
12238 "for catchpoint %d: %s"),
12239 c->base.number, e.message);
12240 /* There is a bug in GCC on sparc-solaris when building with
12241 optimization which causes EXP to change unexpectedly
12242 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56982).
12243 The problem should be fixed starting with GCC 4.9.
12244 In the meantime, work around it by forcing EXP back
12245 to NULL. */
12246 exp = NULL;
12247 }
492d29ea 12248 END_CATCH
28010a5d
PA
12249 }
12250
12251 ada_loc->excep_cond_expr = exp;
12252 }
12253
12254 do_cleanups (old_chain);
12255}
12256
12257/* Implement the DTOR method in the breakpoint_ops structure for all
12258 exception catchpoint kinds. */
12259
12260static void
761269c8 12261dtor_exception (enum ada_exception_catchpoint_kind ex, struct breakpoint *b)
28010a5d
PA
12262{
12263 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12264
12265 xfree (c->excep_string);
348d480f 12266
2060206e 12267 bkpt_breakpoint_ops.dtor (b);
28010a5d
PA
12268}
12269
12270/* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
12271 structure for all exception catchpoint kinds. */
12272
12273static struct bp_location *
761269c8 12274allocate_location_exception (enum ada_exception_catchpoint_kind ex,
28010a5d
PA
12275 struct breakpoint *self)
12276{
12277 struct ada_catchpoint_location *loc;
12278
12279 loc = XNEW (struct ada_catchpoint_location);
12280 init_bp_location (&loc->base, &ada_catchpoint_location_ops, self);
12281 loc->excep_cond_expr = NULL;
12282 return &loc->base;
12283}
12284
12285/* Implement the RE_SET method in the breakpoint_ops structure for all
12286 exception catchpoint kinds. */
12287
12288static void
761269c8 12289re_set_exception (enum ada_exception_catchpoint_kind ex, struct breakpoint *b)
28010a5d
PA
12290{
12291 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12292
12293 /* Call the base class's method. This updates the catchpoint's
12294 locations. */
2060206e 12295 bkpt_breakpoint_ops.re_set (b);
28010a5d
PA
12296
12297 /* Reparse the exception conditional expressions. One for each
12298 location. */
12299 create_excep_cond_exprs (c);
12300}
12301
12302/* Returns true if we should stop for this breakpoint hit. If the
12303 user specified a specific exception, we only want to cause a stop
12304 if the program thrown that exception. */
12305
12306static int
12307should_stop_exception (const struct bp_location *bl)
12308{
12309 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12310 const struct ada_catchpoint_location *ada_loc
12311 = (const struct ada_catchpoint_location *) bl;
28010a5d
PA
12312 int stop;
12313
12314 /* With no specific exception, should always stop. */
12315 if (c->excep_string == NULL)
12316 return 1;
12317
12318 if (ada_loc->excep_cond_expr == NULL)
12319 {
12320 /* We will have a NULL expression if back when we were creating
12321 the expressions, this location's had failed to parse. */
12322 return 1;
12323 }
12324
12325 stop = 1;
492d29ea 12326 TRY
28010a5d
PA
12327 {
12328 struct value *mark;
12329
12330 mark = value_mark ();
12331 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr));
12332 value_free_to_mark (mark);
12333 }
492d29ea
PA
12334 CATCH (ex, RETURN_MASK_ALL)
12335 {
12336 exception_fprintf (gdb_stderr, ex,
12337 _("Error in testing exception condition:\n"));
12338 }
12339 END_CATCH
12340
28010a5d
PA
12341 return stop;
12342}
12343
12344/* Implement the CHECK_STATUS method in the breakpoint_ops structure
12345 for all exception catchpoint kinds. */
12346
12347static void
761269c8 12348check_status_exception (enum ada_exception_catchpoint_kind ex, bpstat bs)
28010a5d
PA
12349{
12350 bs->stop = should_stop_exception (bs->bp_location_at);
12351}
12352
f7f9143b
JB
12353/* Implement the PRINT_IT method in the breakpoint_ops structure
12354 for all exception catchpoint kinds. */
12355
12356static enum print_stop_action
761269c8 12357print_it_exception (enum ada_exception_catchpoint_kind ex, bpstat bs)
f7f9143b 12358{
79a45e25 12359 struct ui_out *uiout = current_uiout;
348d480f
PA
12360 struct breakpoint *b = bs->breakpoint_at;
12361
956a9fb9 12362 annotate_catchpoint (b->number);
f7f9143b 12363
956a9fb9 12364 if (ui_out_is_mi_like_p (uiout))
f7f9143b 12365 {
956a9fb9
JB
12366 ui_out_field_string (uiout, "reason",
12367 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12368 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
f7f9143b
JB
12369 }
12370
00eb2c4a
JB
12371 ui_out_text (uiout,
12372 b->disposition == disp_del ? "\nTemporary catchpoint "
12373 : "\nCatchpoint ");
956a9fb9
JB
12374 ui_out_field_int (uiout, "bkptno", b->number);
12375 ui_out_text (uiout, ", ");
f7f9143b 12376
f7f9143b
JB
12377 switch (ex)
12378 {
761269c8
JB
12379 case ada_catch_exception:
12380 case ada_catch_exception_unhandled:
956a9fb9
JB
12381 {
12382 const CORE_ADDR addr = ada_exception_name_addr (ex, b);
12383 char exception_name[256];
12384
12385 if (addr != 0)
12386 {
c714b426
PA
12387 read_memory (addr, (gdb_byte *) exception_name,
12388 sizeof (exception_name) - 1);
956a9fb9
JB
12389 exception_name [sizeof (exception_name) - 1] = '\0';
12390 }
12391 else
12392 {
12393 /* For some reason, we were unable to read the exception
12394 name. This could happen if the Runtime was compiled
12395 without debugging info, for instance. In that case,
12396 just replace the exception name by the generic string
12397 "exception" - it will read as "an exception" in the
12398 notification we are about to print. */
967cff16 12399 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12400 }
12401 /* In the case of unhandled exception breakpoints, we print
12402 the exception name as "unhandled EXCEPTION_NAME", to make
12403 it clearer to the user which kind of catchpoint just got
12404 hit. We used ui_out_text to make sure that this extra
12405 info does not pollute the exception name in the MI case. */
761269c8 12406 if (ex == ada_catch_exception_unhandled)
956a9fb9
JB
12407 ui_out_text (uiout, "unhandled ");
12408 ui_out_field_string (uiout, "exception-name", exception_name);
12409 }
12410 break;
761269c8 12411 case ada_catch_assert:
956a9fb9
JB
12412 /* In this case, the name of the exception is not really
12413 important. Just print "failed assertion" to make it clearer
12414 that his program just hit an assertion-failure catchpoint.
12415 We used ui_out_text because this info does not belong in
12416 the MI output. */
12417 ui_out_text (uiout, "failed assertion");
12418 break;
f7f9143b 12419 }
956a9fb9
JB
12420 ui_out_text (uiout, " at ");
12421 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12422
12423 return PRINT_SRC_AND_LOC;
12424}
12425
12426/* Implement the PRINT_ONE method in the breakpoint_ops structure
12427 for all exception catchpoint kinds. */
12428
12429static void
761269c8 12430print_one_exception (enum ada_exception_catchpoint_kind ex,
a6d9a66e 12431 struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 12432{
79a45e25 12433 struct ui_out *uiout = current_uiout;
28010a5d 12434 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45b7d
TT
12435 struct value_print_options opts;
12436
12437 get_user_print_options (&opts);
12438 if (opts.addressprint)
f7f9143b
JB
12439 {
12440 annotate_field (4);
5af949e3 12441 ui_out_field_core_addr (uiout, "addr", b->loc->gdbarch, b->loc->address);
f7f9143b
JB
12442 }
12443
12444 annotate_field (5);
a6d9a66e 12445 *last_loc = b->loc;
f7f9143b
JB
12446 switch (ex)
12447 {
761269c8 12448 case ada_catch_exception:
28010a5d 12449 if (c->excep_string != NULL)
f7f9143b 12450 {
28010a5d
PA
12451 char *msg = xstrprintf (_("`%s' Ada exception"), c->excep_string);
12452
f7f9143b
JB
12453 ui_out_field_string (uiout, "what", msg);
12454 xfree (msg);
12455 }
12456 else
12457 ui_out_field_string (uiout, "what", "all Ada exceptions");
12458
12459 break;
12460
761269c8 12461 case ada_catch_exception_unhandled:
f7f9143b
JB
12462 ui_out_field_string (uiout, "what", "unhandled Ada exceptions");
12463 break;
12464
761269c8 12465 case ada_catch_assert:
f7f9143b
JB
12466 ui_out_field_string (uiout, "what", "failed Ada assertions");
12467 break;
12468
12469 default:
12470 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12471 break;
12472 }
12473}
12474
12475/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12476 for all exception catchpoint kinds. */
12477
12478static void
761269c8 12479print_mention_exception (enum ada_exception_catchpoint_kind ex,
f7f9143b
JB
12480 struct breakpoint *b)
12481{
28010a5d 12482 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45e25 12483 struct ui_out *uiout = current_uiout;
28010a5d 12484
00eb2c4a
JB
12485 ui_out_text (uiout, b->disposition == disp_del ? _("Temporary catchpoint ")
12486 : _("Catchpoint "));
12487 ui_out_field_int (uiout, "bkptno", b->number);
12488 ui_out_text (uiout, ": ");
12489
f7f9143b
JB
12490 switch (ex)
12491 {
761269c8 12492 case ada_catch_exception:
28010a5d 12493 if (c->excep_string != NULL)
00eb2c4a
JB
12494 {
12495 char *info = xstrprintf (_("`%s' Ada exception"), c->excep_string);
12496 struct cleanup *old_chain = make_cleanup (xfree, info);
12497
12498 ui_out_text (uiout, info);
12499 do_cleanups (old_chain);
12500 }
f7f9143b 12501 else
00eb2c4a 12502 ui_out_text (uiout, _("all Ada exceptions"));
f7f9143b
JB
12503 break;
12504
761269c8 12505 case ada_catch_exception_unhandled:
00eb2c4a 12506 ui_out_text (uiout, _("unhandled Ada exceptions"));
f7f9143b
JB
12507 break;
12508
761269c8 12509 case ada_catch_assert:
00eb2c4a 12510 ui_out_text (uiout, _("failed Ada assertions"));
f7f9143b
JB
12511 break;
12512
12513 default:
12514 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12515 break;
12516 }
12517}
12518
6149aea9
PA
12519/* Implement the PRINT_RECREATE method in the breakpoint_ops structure
12520 for all exception catchpoint kinds. */
12521
12522static void
761269c8 12523print_recreate_exception (enum ada_exception_catchpoint_kind ex,
6149aea9
PA
12524 struct breakpoint *b, struct ui_file *fp)
12525{
28010a5d
PA
12526 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12527
6149aea9
PA
12528 switch (ex)
12529 {
761269c8 12530 case ada_catch_exception:
6149aea9 12531 fprintf_filtered (fp, "catch exception");
28010a5d
PA
12532 if (c->excep_string != NULL)
12533 fprintf_filtered (fp, " %s", c->excep_string);
6149aea9
PA
12534 break;
12535
761269c8 12536 case ada_catch_exception_unhandled:
78076abc 12537 fprintf_filtered (fp, "catch exception unhandled");
6149aea9
PA
12538 break;
12539
761269c8 12540 case ada_catch_assert:
6149aea9
PA
12541 fprintf_filtered (fp, "catch assert");
12542 break;
12543
12544 default:
12545 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12546 }
d9b3f62e 12547 print_recreate_thread (b, fp);
6149aea9
PA
12548}
12549
f7f9143b
JB
12550/* Virtual table for "catch exception" breakpoints. */
12551
28010a5d
PA
12552static void
12553dtor_catch_exception (struct breakpoint *b)
12554{
761269c8 12555 dtor_exception (ada_catch_exception, b);
28010a5d
PA
12556}
12557
12558static struct bp_location *
12559allocate_location_catch_exception (struct breakpoint *self)
12560{
761269c8 12561 return allocate_location_exception (ada_catch_exception, self);
28010a5d
PA
12562}
12563
12564static void
12565re_set_catch_exception (struct breakpoint *b)
12566{
761269c8 12567 re_set_exception (ada_catch_exception, b);
28010a5d
PA
12568}
12569
12570static void
12571check_status_catch_exception (bpstat bs)
12572{
761269c8 12573 check_status_exception (ada_catch_exception, bs);
28010a5d
PA
12574}
12575
f7f9143b 12576static enum print_stop_action
348d480f 12577print_it_catch_exception (bpstat bs)
f7f9143b 12578{
761269c8 12579 return print_it_exception (ada_catch_exception, bs);
f7f9143b
JB
12580}
12581
12582static void
a6d9a66e 12583print_one_catch_exception (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 12584{
761269c8 12585 print_one_exception (ada_catch_exception, b, last_loc);
f7f9143b
JB
12586}
12587
12588static void
12589print_mention_catch_exception (struct breakpoint *b)
12590{
761269c8 12591 print_mention_exception (ada_catch_exception, b);
f7f9143b
JB
12592}
12593
6149aea9
PA
12594static void
12595print_recreate_catch_exception (struct breakpoint *b, struct ui_file *fp)
12596{
761269c8 12597 print_recreate_exception (ada_catch_exception, b, fp);
6149aea9
PA
12598}
12599
2060206e 12600static struct breakpoint_ops catch_exception_breakpoint_ops;
f7f9143b
JB
12601
12602/* Virtual table for "catch exception unhandled" breakpoints. */
12603
28010a5d
PA
12604static void
12605dtor_catch_exception_unhandled (struct breakpoint *b)
12606{
761269c8 12607 dtor_exception (ada_catch_exception_unhandled, b);
28010a5d
PA
12608}
12609
12610static struct bp_location *
12611allocate_location_catch_exception_unhandled (struct breakpoint *self)
12612{
761269c8 12613 return allocate_location_exception (ada_catch_exception_unhandled, self);
28010a5d
PA
12614}
12615
12616static void
12617re_set_catch_exception_unhandled (struct breakpoint *b)
12618{
761269c8 12619 re_set_exception (ada_catch_exception_unhandled, b);
28010a5d
PA
12620}
12621
12622static void
12623check_status_catch_exception_unhandled (bpstat bs)
12624{
761269c8 12625 check_status_exception (ada_catch_exception_unhandled, bs);
28010a5d
PA
12626}
12627
f7f9143b 12628static enum print_stop_action
348d480f 12629print_it_catch_exception_unhandled (bpstat bs)
f7f9143b 12630{
761269c8 12631 return print_it_exception (ada_catch_exception_unhandled, bs);
f7f9143b
JB
12632}
12633
12634static void
a6d9a66e
UW
12635print_one_catch_exception_unhandled (struct breakpoint *b,
12636 struct bp_location **last_loc)
f7f9143b 12637{
761269c8 12638 print_one_exception (ada_catch_exception_unhandled, b, last_loc);
f7f9143b
JB
12639}
12640
12641static void
12642print_mention_catch_exception_unhandled (struct breakpoint *b)
12643{
761269c8 12644 print_mention_exception (ada_catch_exception_unhandled, b);
f7f9143b
JB
12645}
12646
6149aea9
PA
12647static void
12648print_recreate_catch_exception_unhandled (struct breakpoint *b,
12649 struct ui_file *fp)
12650{
761269c8 12651 print_recreate_exception (ada_catch_exception_unhandled, b, fp);
6149aea9
PA
12652}
12653
2060206e 12654static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops;
f7f9143b
JB
12655
12656/* Virtual table for "catch assert" breakpoints. */
12657
28010a5d
PA
12658static void
12659dtor_catch_assert (struct breakpoint *b)
12660{
761269c8 12661 dtor_exception (ada_catch_assert, b);
28010a5d
PA
12662}
12663
12664static struct bp_location *
12665allocate_location_catch_assert (struct breakpoint *self)
12666{
761269c8 12667 return allocate_location_exception (ada_catch_assert, self);
28010a5d
PA
12668}
12669
12670static void
12671re_set_catch_assert (struct breakpoint *b)
12672{
761269c8 12673 re_set_exception (ada_catch_assert, b);
28010a5d
PA
12674}
12675
12676static void
12677check_status_catch_assert (bpstat bs)
12678{
761269c8 12679 check_status_exception (ada_catch_assert, bs);
28010a5d
PA
12680}
12681
f7f9143b 12682static enum print_stop_action
348d480f 12683print_it_catch_assert (bpstat bs)
f7f9143b 12684{
761269c8 12685 return print_it_exception (ada_catch_assert, bs);
f7f9143b
JB
12686}
12687
12688static void
a6d9a66e 12689print_one_catch_assert (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 12690{
761269c8 12691 print_one_exception (ada_catch_assert, b, last_loc);
f7f9143b
JB
12692}
12693
12694static void
12695print_mention_catch_assert (struct breakpoint *b)
12696{
761269c8 12697 print_mention_exception (ada_catch_assert, b);
f7f9143b
JB
12698}
12699
6149aea9
PA
12700static void
12701print_recreate_catch_assert (struct breakpoint *b, struct ui_file *fp)
12702{
761269c8 12703 print_recreate_exception (ada_catch_assert, b, fp);
6149aea9
PA
12704}
12705
2060206e 12706static struct breakpoint_ops catch_assert_breakpoint_ops;
f7f9143b 12707
f7f9143b
JB
12708/* Return a newly allocated copy of the first space-separated token
12709 in ARGSP, and then adjust ARGSP to point immediately after that
12710 token.
12711
12712 Return NULL if ARGPS does not contain any more tokens. */
12713
12714static char *
12715ada_get_next_arg (char **argsp)
12716{
12717 char *args = *argsp;
12718 char *end;
12719 char *result;
12720
0fcd72ba 12721 args = skip_spaces (args);
f7f9143b
JB
12722 if (args[0] == '\0')
12723 return NULL; /* No more arguments. */
12724
12725 /* Find the end of the current argument. */
12726
0fcd72ba 12727 end = skip_to_space (args);
f7f9143b
JB
12728
12729 /* Adjust ARGSP to point to the start of the next argument. */
12730
12731 *argsp = end;
12732
12733 /* Make a copy of the current argument and return it. */
12734
224c3ddb 12735 result = (char *) xmalloc (end - args + 1);
f7f9143b
JB
12736 strncpy (result, args, end - args);
12737 result[end - args] = '\0';
12738
12739 return result;
12740}
12741
12742/* Split the arguments specified in a "catch exception" command.
12743 Set EX to the appropriate catchpoint type.
28010a5d 12744 Set EXCEP_STRING to the name of the specific exception if
5845583d
JB
12745 specified by the user.
12746 If a condition is found at the end of the arguments, the condition
12747 expression is stored in COND_STRING (memory must be deallocated
12748 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12749
12750static void
12751catch_ada_exception_command_split (char *args,
761269c8 12752 enum ada_exception_catchpoint_kind *ex,
5845583d
JB
12753 char **excep_string,
12754 char **cond_string)
f7f9143b
JB
12755{
12756 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
12757 char *exception_name;
5845583d 12758 char *cond = NULL;
f7f9143b
JB
12759
12760 exception_name = ada_get_next_arg (&args);
5845583d
JB
12761 if (exception_name != NULL && strcmp (exception_name, "if") == 0)
12762 {
12763 /* This is not an exception name; this is the start of a condition
12764 expression for a catchpoint on all exceptions. So, "un-get"
12765 this token, and set exception_name to NULL. */
12766 xfree (exception_name);
12767 exception_name = NULL;
12768 args -= 2;
12769 }
f7f9143b
JB
12770 make_cleanup (xfree, exception_name);
12771
5845583d 12772 /* Check to see if we have a condition. */
f7f9143b 12773
0fcd72ba 12774 args = skip_spaces (args);
61012eef 12775 if (startswith (args, "if")
5845583d
JB
12776 && (isspace (args[2]) || args[2] == '\0'))
12777 {
12778 args += 2;
12779 args = skip_spaces (args);
12780
12781 if (args[0] == '\0')
12782 error (_("Condition missing after `if' keyword"));
12783 cond = xstrdup (args);
12784 make_cleanup (xfree, cond);
12785
12786 args += strlen (args);
12787 }
12788
12789 /* Check that we do not have any more arguments. Anything else
12790 is unexpected. */
f7f9143b
JB
12791
12792 if (args[0] != '\0')
12793 error (_("Junk at end of expression"));
12794
12795 discard_cleanups (old_chain);
12796
12797 if (exception_name == NULL)
12798 {
12799 /* Catch all exceptions. */
761269c8 12800 *ex = ada_catch_exception;
28010a5d 12801 *excep_string = NULL;
f7f9143b
JB
12802 }
12803 else if (strcmp (exception_name, "unhandled") == 0)
12804 {
12805 /* Catch unhandled exceptions. */
761269c8 12806 *ex = ada_catch_exception_unhandled;
28010a5d 12807 *excep_string = NULL;
f7f9143b
JB
12808 }
12809 else
12810 {
12811 /* Catch a specific exception. */
761269c8 12812 *ex = ada_catch_exception;
28010a5d 12813 *excep_string = exception_name;
f7f9143b 12814 }
5845583d 12815 *cond_string = cond;
f7f9143b
JB
12816}
12817
12818/* Return the name of the symbol on which we should break in order to
12819 implement a catchpoint of the EX kind. */
12820
12821static const char *
761269c8 12822ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12823{
3eecfa55
JB
12824 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12825
12826 gdb_assert (data->exception_info != NULL);
0259addd 12827
f7f9143b
JB
12828 switch (ex)
12829 {
761269c8 12830 case ada_catch_exception:
3eecfa55 12831 return (data->exception_info->catch_exception_sym);
f7f9143b 12832 break;
761269c8 12833 case ada_catch_exception_unhandled:
3eecfa55 12834 return (data->exception_info->catch_exception_unhandled_sym);
f7f9143b 12835 break;
761269c8 12836 case ada_catch_assert:
3eecfa55 12837 return (data->exception_info->catch_assert_sym);
f7f9143b
JB
12838 break;
12839 default:
12840 internal_error (__FILE__, __LINE__,
12841 _("unexpected catchpoint kind (%d)"), ex);
12842 }
12843}
12844
12845/* Return the breakpoint ops "virtual table" used for catchpoints
12846 of the EX kind. */
12847
c0a91b2b 12848static const struct breakpoint_ops *
761269c8 12849ada_exception_breakpoint_ops (enum ada_exception_catchpoint_kind ex)
f7f9143b
JB
12850{
12851 switch (ex)
12852 {
761269c8 12853 case ada_catch_exception:
f7f9143b
JB
12854 return (&catch_exception_breakpoint_ops);
12855 break;
761269c8 12856 case ada_catch_exception_unhandled:
f7f9143b
JB
12857 return (&catch_exception_unhandled_breakpoint_ops);
12858 break;
761269c8 12859 case ada_catch_assert:
f7f9143b
JB
12860 return (&catch_assert_breakpoint_ops);
12861 break;
12862 default:
12863 internal_error (__FILE__, __LINE__,
12864 _("unexpected catchpoint kind (%d)"), ex);
12865 }
12866}
12867
12868/* Return the condition that will be used to match the current exception
12869 being raised with the exception that the user wants to catch. This
12870 assumes that this condition is used when the inferior just triggered
12871 an exception catchpoint.
12872
12873 The string returned is a newly allocated string that needs to be
12874 deallocated later. */
12875
12876static char *
28010a5d 12877ada_exception_catchpoint_cond_string (const char *excep_string)
f7f9143b 12878{
3d0b0fa3
JB
12879 int i;
12880
0963b4bd 12881 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12882 runtime units that have been compiled without debugging info; if
28010a5d 12883 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12884 exception (e.g. "constraint_error") then, during the evaluation
12885 of the condition expression, the symbol lookup on this name would
0963b4bd 12886 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12887 may then be set only on user-defined exceptions which have the
12888 same not-fully-qualified name (e.g. my_package.constraint_error).
12889
12890 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12891 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12892 exception constraint_error" is rewritten into "catch exception
12893 standard.constraint_error".
12894
12895 If an exception named contraint_error is defined in another package of
12896 the inferior program, then the only way to specify this exception as a
12897 breakpoint condition is to use its fully-qualified named:
12898 e.g. my_package.constraint_error. */
12899
12900 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
12901 {
28010a5d 12902 if (strcmp (standard_exc [i], excep_string) == 0)
3d0b0fa3
JB
12903 {
12904 return xstrprintf ("long_integer (e) = long_integer (&standard.%s)",
28010a5d 12905 excep_string);
3d0b0fa3
JB
12906 }
12907 }
28010a5d 12908 return xstrprintf ("long_integer (e) = long_integer (&%s)", excep_string);
f7f9143b
JB
12909}
12910
12911/* Return the symtab_and_line that should be used to insert an exception
12912 catchpoint of the TYPE kind.
12913
28010a5d
PA
12914 EXCEP_STRING should contain the name of a specific exception that
12915 the catchpoint should catch, or NULL otherwise.
f7f9143b 12916
28010a5d
PA
12917 ADDR_STRING returns the name of the function where the real
12918 breakpoint that implements the catchpoints is set, depending on the
12919 type of catchpoint we need to create. */
f7f9143b
JB
12920
12921static struct symtab_and_line
761269c8 12922ada_exception_sal (enum ada_exception_catchpoint_kind ex, char *excep_string,
c0a91b2b 12923 char **addr_string, const struct breakpoint_ops **ops)
f7f9143b
JB
12924{
12925 const char *sym_name;
12926 struct symbol *sym;
f7f9143b 12927
0259addd
JB
12928 /* First, find out which exception support info to use. */
12929 ada_exception_support_info_sniffer ();
12930
12931 /* Then lookup the function on which we will break in order to catch
f7f9143b 12932 the Ada exceptions requested by the user. */
f7f9143b
JB
12933 sym_name = ada_exception_sym_name (ex);
12934 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12935
f17011e0
JB
12936 /* We can assume that SYM is not NULL at this stage. If the symbol
12937 did not exist, ada_exception_support_info_sniffer would have
12938 raised an exception.
f7f9143b 12939
f17011e0
JB
12940 Also, ada_exception_support_info_sniffer should have already
12941 verified that SYM is a function symbol. */
12942 gdb_assert (sym != NULL);
12943 gdb_assert (SYMBOL_CLASS (sym) == LOC_BLOCK);
f7f9143b
JB
12944
12945 /* Set ADDR_STRING. */
f7f9143b
JB
12946 *addr_string = xstrdup (sym_name);
12947
f7f9143b 12948 /* Set OPS. */
4b9eee8c 12949 *ops = ada_exception_breakpoint_ops (ex);
f7f9143b 12950
f17011e0 12951 return find_function_start_sal (sym, 1);
f7f9143b
JB
12952}
12953
b4a5b78b 12954/* Create an Ada exception catchpoint.
f7f9143b 12955
b4a5b78b 12956 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12957
2df4d1d5
JB
12958 If EXCEPT_STRING is NULL, this catchpoint is expected to trigger
12959 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
12960 of the exception to which this catchpoint applies. When not NULL,
12961 the string must be allocated on the heap, and its deallocation
12962 is no longer the responsibility of the caller.
12963
12964 COND_STRING, if not NULL, is the catchpoint condition. This string
12965 must be allocated on the heap, and its deallocation is no longer
12966 the responsibility of the caller.
f7f9143b 12967
b4a5b78b
JB
12968 TEMPFLAG, if nonzero, means that the underlying breakpoint
12969 should be temporary.
28010a5d 12970
b4a5b78b 12971 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12972
349774ef 12973void
28010a5d 12974create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12975 enum ada_exception_catchpoint_kind ex_kind,
28010a5d 12976 char *excep_string,
5845583d 12977 char *cond_string,
28010a5d 12978 int tempflag,
349774ef 12979 int disabled,
28010a5d
PA
12980 int from_tty)
12981{
12982 struct ada_catchpoint *c;
b4a5b78b
JB
12983 char *addr_string = NULL;
12984 const struct breakpoint_ops *ops = NULL;
12985 struct symtab_and_line sal
12986 = ada_exception_sal (ex_kind, excep_string, &addr_string, &ops);
28010a5d
PA
12987
12988 c = XNEW (struct ada_catchpoint);
12989 init_ada_exception_breakpoint (&c->base, gdbarch, sal, addr_string,
349774ef 12990 ops, tempflag, disabled, from_tty);
28010a5d
PA
12991 c->excep_string = excep_string;
12992 create_excep_cond_exprs (c);
5845583d
JB
12993 if (cond_string != NULL)
12994 set_breakpoint_condition (&c->base, cond_string, from_tty);
3ea46bff 12995 install_breakpoint (0, &c->base, 1);
f7f9143b
JB
12996}
12997
9ac4176b
PA
12998/* Implement the "catch exception" command. */
12999
13000static void
13001catch_ada_exception_command (char *arg, int from_tty,
13002 struct cmd_list_element *command)
13003{
13004 struct gdbarch *gdbarch = get_current_arch ();
13005 int tempflag;
761269c8 13006 enum ada_exception_catchpoint_kind ex_kind;
28010a5d 13007 char *excep_string = NULL;
5845583d 13008 char *cond_string = NULL;
9ac4176b
PA
13009
13010 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
13011
13012 if (!arg)
13013 arg = "";
b4a5b78b
JB
13014 catch_ada_exception_command_split (arg, &ex_kind, &excep_string,
13015 &cond_string);
13016 create_ada_exception_catchpoint (gdbarch, ex_kind,
13017 excep_string, cond_string,
349774ef
JB
13018 tempflag, 1 /* enabled */,
13019 from_tty);
9ac4176b
PA
13020}
13021
b4a5b78b 13022/* Split the arguments specified in a "catch assert" command.
5845583d 13023
b4a5b78b
JB
13024 ARGS contains the command's arguments (or the empty string if
13025 no arguments were passed).
5845583d
JB
13026
13027 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 13028 (the memory needs to be deallocated after use). */
5845583d 13029
b4a5b78b
JB
13030static void
13031catch_ada_assert_command_split (char *args, char **cond_string)
f7f9143b 13032{
5845583d 13033 args = skip_spaces (args);
f7f9143b 13034
5845583d 13035 /* Check whether a condition was provided. */
61012eef 13036 if (startswith (args, "if")
5845583d 13037 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 13038 {
5845583d 13039 args += 2;
0fcd72ba 13040 args = skip_spaces (args);
5845583d
JB
13041 if (args[0] == '\0')
13042 error (_("condition missing after `if' keyword"));
13043 *cond_string = xstrdup (args);
f7f9143b
JB
13044 }
13045
5845583d
JB
13046 /* Otherwise, there should be no other argument at the end of
13047 the command. */
13048 else if (args[0] != '\0')
13049 error (_("Junk at end of arguments."));
f7f9143b
JB
13050}
13051
9ac4176b
PA
13052/* Implement the "catch assert" command. */
13053
13054static void
13055catch_assert_command (char *arg, int from_tty,
13056 struct cmd_list_element *command)
13057{
13058 struct gdbarch *gdbarch = get_current_arch ();
13059 int tempflag;
5845583d 13060 char *cond_string = NULL;
9ac4176b
PA
13061
13062 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
13063
13064 if (!arg)
13065 arg = "";
b4a5b78b 13066 catch_ada_assert_command_split (arg, &cond_string);
761269c8 13067 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
b4a5b78b 13068 NULL, cond_string,
349774ef
JB
13069 tempflag, 1 /* enabled */,
13070 from_tty);
9ac4176b 13071}
778865d3
JB
13072
13073/* Return non-zero if the symbol SYM is an Ada exception object. */
13074
13075static int
13076ada_is_exception_sym (struct symbol *sym)
13077{
13078 const char *type_name = type_name_no_tag (SYMBOL_TYPE (sym));
13079
13080 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
13081 && SYMBOL_CLASS (sym) != LOC_BLOCK
13082 && SYMBOL_CLASS (sym) != LOC_CONST
13083 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
13084 && type_name != NULL && strcmp (type_name, "exception") == 0);
13085}
13086
13087/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
13088 Ada exception object. This matches all exceptions except the ones
13089 defined by the Ada language. */
13090
13091static int
13092ada_is_non_standard_exception_sym (struct symbol *sym)
13093{
13094 int i;
13095
13096 if (!ada_is_exception_sym (sym))
13097 return 0;
13098
13099 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
13100 if (strcmp (SYMBOL_LINKAGE_NAME (sym), standard_exc[i]) == 0)
13101 return 0; /* A standard exception. */
13102
13103 /* Numeric_Error is also a standard exception, so exclude it.
13104 See the STANDARD_EXC description for more details as to why
13105 this exception is not listed in that array. */
13106 if (strcmp (SYMBOL_LINKAGE_NAME (sym), "numeric_error") == 0)
13107 return 0;
13108
13109 return 1;
13110}
13111
13112/* A helper function for qsort, comparing two struct ada_exc_info
13113 objects.
13114
13115 The comparison is determined first by exception name, and then
13116 by exception address. */
13117
13118static int
13119compare_ada_exception_info (const void *a, const void *b)
13120{
13121 const struct ada_exc_info *exc_a = (struct ada_exc_info *) a;
13122 const struct ada_exc_info *exc_b = (struct ada_exc_info *) b;
13123 int result;
13124
13125 result = strcmp (exc_a->name, exc_b->name);
13126 if (result != 0)
13127 return result;
13128
13129 if (exc_a->addr < exc_b->addr)
13130 return -1;
13131 if (exc_a->addr > exc_b->addr)
13132 return 1;
13133
13134 return 0;
13135}
13136
13137/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
13138 routine, but keeping the first SKIP elements untouched.
13139
13140 All duplicates are also removed. */
13141
13142static void
13143sort_remove_dups_ada_exceptions_list (VEC(ada_exc_info) **exceptions,
13144 int skip)
13145{
13146 struct ada_exc_info *to_sort
13147 = VEC_address (ada_exc_info, *exceptions) + skip;
13148 int to_sort_len
13149 = VEC_length (ada_exc_info, *exceptions) - skip;
13150 int i, j;
13151
13152 qsort (to_sort, to_sort_len, sizeof (struct ada_exc_info),
13153 compare_ada_exception_info);
13154
13155 for (i = 1, j = 1; i < to_sort_len; i++)
13156 if (compare_ada_exception_info (&to_sort[i], &to_sort[j - 1]) != 0)
13157 to_sort[j++] = to_sort[i];
13158 to_sort_len = j;
13159 VEC_truncate(ada_exc_info, *exceptions, skip + to_sort_len);
13160}
13161
13162/* A function intended as the "name_matcher" callback in the struct
13163 quick_symbol_functions' expand_symtabs_matching method.
13164
13165 SEARCH_NAME is the symbol's search name.
13166
13167 If USER_DATA is not NULL, it is a pointer to a regext_t object
13168 used to match the symbol (by natural name). Otherwise, when USER_DATA
13169 is null, no filtering is performed, and all symbols are a positive
13170 match. */
13171
13172static int
13173ada_exc_search_name_matches (const char *search_name, void *user_data)
13174{
9a3c8263 13175 regex_t *preg = (regex_t *) user_data;
778865d3
JB
13176
13177 if (preg == NULL)
13178 return 1;
13179
13180 /* In Ada, the symbol "search name" is a linkage name, whereas
13181 the regular expression used to do the matching refers to
13182 the natural name. So match against the decoded name. */
13183 return (regexec (preg, ada_decode (search_name), 0, NULL, 0) == 0);
13184}
13185
13186/* Add all exceptions defined by the Ada standard whose name match
13187 a regular expression.
13188
13189 If PREG is not NULL, then this regexp_t object is used to
13190 perform the symbol name matching. Otherwise, no name-based
13191 filtering is performed.
13192
13193 EXCEPTIONS is a vector of exceptions to which matching exceptions
13194 gets pushed. */
13195
13196static void
13197ada_add_standard_exceptions (regex_t *preg, VEC(ada_exc_info) **exceptions)
13198{
13199 int i;
13200
13201 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
13202 {
13203 if (preg == NULL
13204 || regexec (preg, standard_exc[i], 0, NULL, 0) == 0)
13205 {
13206 struct bound_minimal_symbol msymbol
13207 = ada_lookup_simple_minsym (standard_exc[i]);
13208
13209 if (msymbol.minsym != NULL)
13210 {
13211 struct ada_exc_info info
77e371c0 13212 = {standard_exc[i], BMSYMBOL_VALUE_ADDRESS (msymbol)};
778865d3
JB
13213
13214 VEC_safe_push (ada_exc_info, *exceptions, &info);
13215 }
13216 }
13217 }
13218}
13219
13220/* Add all Ada exceptions defined locally and accessible from the given
13221 FRAME.
13222
13223 If PREG is not NULL, then this regexp_t object is used to
13224 perform the symbol name matching. Otherwise, no name-based
13225 filtering is performed.
13226
13227 EXCEPTIONS is a vector of exceptions to which matching exceptions
13228 gets pushed. */
13229
13230static void
13231ada_add_exceptions_from_frame (regex_t *preg, struct frame_info *frame,
13232 VEC(ada_exc_info) **exceptions)
13233{
3977b71f 13234 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
13235
13236 while (block != 0)
13237 {
13238 struct block_iterator iter;
13239 struct symbol *sym;
13240
13241 ALL_BLOCK_SYMBOLS (block, iter, sym)
13242 {
13243 switch (SYMBOL_CLASS (sym))
13244 {
13245 case LOC_TYPEDEF:
13246 case LOC_BLOCK:
13247 case LOC_CONST:
13248 break;
13249 default:
13250 if (ada_is_exception_sym (sym))
13251 {
13252 struct ada_exc_info info = {SYMBOL_PRINT_NAME (sym),
13253 SYMBOL_VALUE_ADDRESS (sym)};
13254
13255 VEC_safe_push (ada_exc_info, *exceptions, &info);
13256 }
13257 }
13258 }
13259 if (BLOCK_FUNCTION (block) != NULL)
13260 break;
13261 block = BLOCK_SUPERBLOCK (block);
13262 }
13263}
13264
13265/* Add all exceptions defined globally whose name name match
13266 a regular expression, excluding standard exceptions.
13267
13268 The reason we exclude standard exceptions is that they need
13269 to be handled separately: Standard exceptions are defined inside
13270 a runtime unit which is normally not compiled with debugging info,
13271 and thus usually do not show up in our symbol search. However,
13272 if the unit was in fact built with debugging info, we need to
13273 exclude them because they would duplicate the entry we found
13274 during the special loop that specifically searches for those
13275 standard exceptions.
13276
13277 If PREG is not NULL, then this regexp_t object is used to
13278 perform the symbol name matching. Otherwise, no name-based
13279 filtering is performed.
13280
13281 EXCEPTIONS is a vector of exceptions to which matching exceptions
13282 gets pushed. */
13283
13284static void
13285ada_add_global_exceptions (regex_t *preg, VEC(ada_exc_info) **exceptions)
13286{
13287 struct objfile *objfile;
43f3e411 13288 struct compunit_symtab *s;
778865d3 13289
276d885b 13290 expand_symtabs_matching (NULL, ada_exc_search_name_matches, NULL,
bb4142cf 13291 VARIABLES_DOMAIN, preg);
778865d3 13292
43f3e411 13293 ALL_COMPUNITS (objfile, s)
778865d3 13294 {
43f3e411 13295 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (s);
778865d3
JB
13296 int i;
13297
13298 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13299 {
13300 struct block *b = BLOCKVECTOR_BLOCK (bv, i);
13301 struct block_iterator iter;
13302 struct symbol *sym;
13303
13304 ALL_BLOCK_SYMBOLS (b, iter, sym)
13305 if (ada_is_non_standard_exception_sym (sym)
13306 && (preg == NULL
13307 || regexec (preg, SYMBOL_NATURAL_NAME (sym),
13308 0, NULL, 0) == 0))
13309 {
13310 struct ada_exc_info info
13311 = {SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE_ADDRESS (sym)};
13312
13313 VEC_safe_push (ada_exc_info, *exceptions, &info);
13314 }
13315 }
13316 }
13317}
13318
13319/* Implements ada_exceptions_list with the regular expression passed
13320 as a regex_t, rather than a string.
13321
13322 If not NULL, PREG is used to filter out exceptions whose names
13323 do not match. Otherwise, all exceptions are listed. */
13324
13325static VEC(ada_exc_info) *
13326ada_exceptions_list_1 (regex_t *preg)
13327{
13328 VEC(ada_exc_info) *result = NULL;
13329 struct cleanup *old_chain
13330 = make_cleanup (VEC_cleanup (ada_exc_info), &result);
13331 int prev_len;
13332
13333 /* First, list the known standard exceptions. These exceptions
13334 need to be handled separately, as they are usually defined in
13335 runtime units that have been compiled without debugging info. */
13336
13337 ada_add_standard_exceptions (preg, &result);
13338
13339 /* Next, find all exceptions whose scope is local and accessible
13340 from the currently selected frame. */
13341
13342 if (has_stack_frames ())
13343 {
13344 prev_len = VEC_length (ada_exc_info, result);
13345 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13346 &result);
13347 if (VEC_length (ada_exc_info, result) > prev_len)
13348 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13349 }
13350
13351 /* Add all exceptions whose scope is global. */
13352
13353 prev_len = VEC_length (ada_exc_info, result);
13354 ada_add_global_exceptions (preg, &result);
13355 if (VEC_length (ada_exc_info, result) > prev_len)
13356 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13357
13358 discard_cleanups (old_chain);
13359 return result;
13360}
13361
13362/* Return a vector of ada_exc_info.
13363
13364 If REGEXP is NULL, all exceptions are included in the result.
13365 Otherwise, it should contain a valid regular expression,
13366 and only the exceptions whose names match that regular expression
13367 are included in the result.
13368
13369 The exceptions are sorted in the following order:
13370 - Standard exceptions (defined by the Ada language), in
13371 alphabetical order;
13372 - Exceptions only visible from the current frame, in
13373 alphabetical order;
13374 - Exceptions whose scope is global, in alphabetical order. */
13375
13376VEC(ada_exc_info) *
13377ada_exceptions_list (const char *regexp)
13378{
13379 VEC(ada_exc_info) *result = NULL;
13380 struct cleanup *old_chain = NULL;
13381 regex_t reg;
13382
13383 if (regexp != NULL)
13384 old_chain = compile_rx_or_error (&reg, regexp,
13385 _("invalid regular expression"));
13386
13387 result = ada_exceptions_list_1 (regexp != NULL ? &reg : NULL);
13388
13389 if (old_chain != NULL)
13390 do_cleanups (old_chain);
13391 return result;
13392}
13393
13394/* Implement the "info exceptions" command. */
13395
13396static void
13397info_exceptions_command (char *regexp, int from_tty)
13398{
13399 VEC(ada_exc_info) *exceptions;
13400 struct cleanup *cleanup;
13401 struct gdbarch *gdbarch = get_current_arch ();
13402 int ix;
13403 struct ada_exc_info *info;
13404
13405 exceptions = ada_exceptions_list (regexp);
13406 cleanup = make_cleanup (VEC_cleanup (ada_exc_info), &exceptions);
13407
13408 if (regexp != NULL)
13409 printf_filtered
13410 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13411 else
13412 printf_filtered (_("All defined Ada exceptions:\n"));
13413
13414 for (ix = 0; VEC_iterate(ada_exc_info, exceptions, ix, info); ix++)
13415 printf_filtered ("%s: %s\n", info->name, paddress (gdbarch, info->addr));
13416
13417 do_cleanups (cleanup);
13418}
13419
4c4b4cd2
PH
13420 /* Operators */
13421/* Information about operators given special treatment in functions
13422 below. */
13423/* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
13424
13425#define ADA_OPERATORS \
13426 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
13427 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
13428 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
13429 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
13430 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
13431 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
13432 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
13433 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
13434 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
13435 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
13436 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
13437 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
13438 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
13439 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
13440 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
52ce6436
PH
13441 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
13442 OP_DEFN (OP_OTHERS, 1, 1, 0) \
13443 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
13444 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
4c4b4cd2
PH
13445
13446static void
554794dc
SDJ
13447ada_operator_length (const struct expression *exp, int pc, int *oplenp,
13448 int *argsp)
4c4b4cd2
PH
13449{
13450 switch (exp->elts[pc - 1].opcode)
13451 {
76a01679 13452 default:
4c4b4cd2
PH
13453 operator_length_standard (exp, pc, oplenp, argsp);
13454 break;
13455
13456#define OP_DEFN(op, len, args, binop) \
13457 case op: *oplenp = len; *argsp = args; break;
13458 ADA_OPERATORS;
13459#undef OP_DEFN
52ce6436
PH
13460
13461 case OP_AGGREGATE:
13462 *oplenp = 3;
13463 *argsp = longest_to_int (exp->elts[pc - 2].longconst);
13464 break;
13465
13466 case OP_CHOICES:
13467 *oplenp = 3;
13468 *argsp = longest_to_int (exp->elts[pc - 2].longconst) + 1;
13469 break;
4c4b4cd2
PH
13470 }
13471}
13472
c0201579
JK
13473/* Implementation of the exp_descriptor method operator_check. */
13474
13475static int
13476ada_operator_check (struct expression *exp, int pos,
13477 int (*objfile_func) (struct objfile *objfile, void *data),
13478 void *data)
13479{
13480 const union exp_element *const elts = exp->elts;
13481 struct type *type = NULL;
13482
13483 switch (elts[pos].opcode)
13484 {
13485 case UNOP_IN_RANGE:
13486 case UNOP_QUAL:
13487 type = elts[pos + 1].type;
13488 break;
13489
13490 default:
13491 return operator_check_standard (exp, pos, objfile_func, data);
13492 }
13493
13494 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
13495
13496 if (type && TYPE_OBJFILE (type)
13497 && (*objfile_func) (TYPE_OBJFILE (type), data))
13498 return 1;
13499
13500 return 0;
13501}
13502
4c4b4cd2
PH
13503static char *
13504ada_op_name (enum exp_opcode opcode)
13505{
13506 switch (opcode)
13507 {
76a01679 13508 default:
4c4b4cd2 13509 return op_name_standard (opcode);
52ce6436 13510
4c4b4cd2
PH
13511#define OP_DEFN(op, len, args, binop) case op: return #op;
13512 ADA_OPERATORS;
13513#undef OP_DEFN
52ce6436
PH
13514
13515 case OP_AGGREGATE:
13516 return "OP_AGGREGATE";
13517 case OP_CHOICES:
13518 return "OP_CHOICES";
13519 case OP_NAME:
13520 return "OP_NAME";
4c4b4cd2
PH
13521 }
13522}
13523
13524/* As for operator_length, but assumes PC is pointing at the first
13525 element of the operator, and gives meaningful results only for the
52ce6436 13526 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
4c4b4cd2
PH
13527
13528static void
76a01679
JB
13529ada_forward_operator_length (struct expression *exp, int pc,
13530 int *oplenp, int *argsp)
4c4b4cd2 13531{
76a01679 13532 switch (exp->elts[pc].opcode)
4c4b4cd2
PH
13533 {
13534 default:
13535 *oplenp = *argsp = 0;
13536 break;
52ce6436 13537
4c4b4cd2
PH
13538#define OP_DEFN(op, len, args, binop) \
13539 case op: *oplenp = len; *argsp = args; break;
13540 ADA_OPERATORS;
13541#undef OP_DEFN
52ce6436
PH
13542
13543 case OP_AGGREGATE:
13544 *oplenp = 3;
13545 *argsp = longest_to_int (exp->elts[pc + 1].longconst);
13546 break;
13547
13548 case OP_CHOICES:
13549 *oplenp = 3;
13550 *argsp = longest_to_int (exp->elts[pc + 1].longconst) + 1;
13551 break;
13552
13553 case OP_STRING:
13554 case OP_NAME:
13555 {
13556 int len = longest_to_int (exp->elts[pc + 1].longconst);
5b4ee69b 13557
52ce6436
PH
13558 *oplenp = 4 + BYTES_TO_EXP_ELEM (len + 1);
13559 *argsp = 0;
13560 break;
13561 }
4c4b4cd2
PH
13562 }
13563}
13564
13565static int
13566ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
13567{
13568 enum exp_opcode op = exp->elts[elt].opcode;
13569 int oplen, nargs;
13570 int pc = elt;
13571 int i;
76a01679 13572
4c4b4cd2
PH
13573 ada_forward_operator_length (exp, elt, &oplen, &nargs);
13574
76a01679 13575 switch (op)
4c4b4cd2 13576 {
76a01679 13577 /* Ada attributes ('Foo). */
4c4b4cd2
PH
13578 case OP_ATR_FIRST:
13579 case OP_ATR_LAST:
13580 case OP_ATR_LENGTH:
13581 case OP_ATR_IMAGE:
13582 case OP_ATR_MAX:
13583 case OP_ATR_MIN:
13584 case OP_ATR_MODULUS:
13585 case OP_ATR_POS:
13586 case OP_ATR_SIZE:
13587 case OP_ATR_TAG:
13588 case OP_ATR_VAL:
13589 break;
13590
13591 case UNOP_IN_RANGE:
13592 case UNOP_QUAL:
323e0a4a
AC
13593 /* XXX: gdb_sprint_host_address, type_sprint */
13594 fprintf_filtered (stream, _("Type @"));
4c4b4cd2
PH
13595 gdb_print_host_address (exp->elts[pc + 1].type, stream);
13596 fprintf_filtered (stream, " (");
13597 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
13598 fprintf_filtered (stream, ")");
13599 break;
13600 case BINOP_IN_BOUNDS:
52ce6436
PH
13601 fprintf_filtered (stream, " (%d)",
13602 longest_to_int (exp->elts[pc + 2].longconst));
4c4b4cd2
PH
13603 break;
13604 case TERNOP_IN_RANGE:
13605 break;
13606
52ce6436
PH
13607 case OP_AGGREGATE:
13608 case OP_OTHERS:
13609 case OP_DISCRETE_RANGE:
13610 case OP_POSITIONAL:
13611 case OP_CHOICES:
13612 break;
13613
13614 case OP_NAME:
13615 case OP_STRING:
13616 {
13617 char *name = &exp->elts[elt + 2].string;
13618 int len = longest_to_int (exp->elts[elt + 1].longconst);
5b4ee69b 13619
52ce6436
PH
13620 fprintf_filtered (stream, "Text: `%.*s'", len, name);
13621 break;
13622 }
13623
4c4b4cd2
PH
13624 default:
13625 return dump_subexp_body_standard (exp, stream, elt);
13626 }
13627
13628 elt += oplen;
13629 for (i = 0; i < nargs; i += 1)
13630 elt = dump_subexp (exp, stream, elt);
13631
13632 return elt;
13633}
13634
13635/* The Ada extension of print_subexp (q.v.). */
13636
76a01679
JB
13637static void
13638ada_print_subexp (struct expression *exp, int *pos,
13639 struct ui_file *stream, enum precedence prec)
4c4b4cd2 13640{
52ce6436 13641 int oplen, nargs, i;
4c4b4cd2
PH
13642 int pc = *pos;
13643 enum exp_opcode op = exp->elts[pc].opcode;
13644
13645 ada_forward_operator_length (exp, pc, &oplen, &nargs);
13646
52ce6436 13647 *pos += oplen;
4c4b4cd2
PH
13648 switch (op)
13649 {
13650 default:
52ce6436 13651 *pos -= oplen;
4c4b4cd2
PH
13652 print_subexp_standard (exp, pos, stream, prec);
13653 return;
13654
13655 case OP_VAR_VALUE:
4c4b4cd2
PH
13656 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
13657 return;
13658
13659 case BINOP_IN_BOUNDS:
323e0a4a 13660 /* XXX: sprint_subexp */
4c4b4cd2 13661 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13662 fputs_filtered (" in ", stream);
4c4b4cd2 13663 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13664 fputs_filtered ("'range", stream);
4c4b4cd2 13665 if (exp->elts[pc + 1].longconst > 1)
76a01679
JB
13666 fprintf_filtered (stream, "(%ld)",
13667 (long) exp->elts[pc + 1].longconst);
4c4b4cd2
PH
13668 return;
13669
13670 case TERNOP_IN_RANGE:
4c4b4cd2 13671 if (prec >= PREC_EQUAL)
76a01679 13672 fputs_filtered ("(", stream);
323e0a4a 13673 /* XXX: sprint_subexp */
4c4b4cd2 13674 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13675 fputs_filtered (" in ", stream);
4c4b4cd2
PH
13676 print_subexp (exp, pos, stream, PREC_EQUAL);
13677 fputs_filtered (" .. ", stream);
13678 print_subexp (exp, pos, stream, PREC_EQUAL);
13679 if (prec >= PREC_EQUAL)
76a01679
JB
13680 fputs_filtered (")", stream);
13681 return;
4c4b4cd2
PH
13682
13683 case OP_ATR_FIRST:
13684 case OP_ATR_LAST:
13685 case OP_ATR_LENGTH:
13686 case OP_ATR_IMAGE:
13687 case OP_ATR_MAX:
13688 case OP_ATR_MIN:
13689 case OP_ATR_MODULUS:
13690 case OP_ATR_POS:
13691 case OP_ATR_SIZE:
13692 case OP_ATR_TAG:
13693 case OP_ATR_VAL:
4c4b4cd2 13694 if (exp->elts[*pos].opcode == OP_TYPE)
76a01679
JB
13695 {
13696 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
79d43c61
TT
13697 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0,
13698 &type_print_raw_options);
76a01679
JB
13699 *pos += 3;
13700 }
4c4b4cd2 13701 else
76a01679 13702 print_subexp (exp, pos, stream, PREC_SUFFIX);
4c4b4cd2
PH
13703 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
13704 if (nargs > 1)
76a01679
JB
13705 {
13706 int tem;
5b4ee69b 13707
76a01679
JB
13708 for (tem = 1; tem < nargs; tem += 1)
13709 {
13710 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
13711 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
13712 }
13713 fputs_filtered (")", stream);
13714 }
4c4b4cd2 13715 return;
14f9c5c9 13716
4c4b4cd2 13717 case UNOP_QUAL:
4c4b4cd2
PH
13718 type_print (exp->elts[pc + 1].type, "", stream, 0);
13719 fputs_filtered ("'(", stream);
13720 print_subexp (exp, pos, stream, PREC_PREFIX);
13721 fputs_filtered (")", stream);
13722 return;
14f9c5c9 13723
4c4b4cd2 13724 case UNOP_IN_RANGE:
323e0a4a 13725 /* XXX: sprint_subexp */
4c4b4cd2 13726 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13727 fputs_filtered (" in ", stream);
79d43c61
TT
13728 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0,
13729 &type_print_raw_options);
4c4b4cd2 13730 return;
52ce6436
PH
13731
13732 case OP_DISCRETE_RANGE:
13733 print_subexp (exp, pos, stream, PREC_SUFFIX);
13734 fputs_filtered ("..", stream);
13735 print_subexp (exp, pos, stream, PREC_SUFFIX);
13736 return;
13737
13738 case OP_OTHERS:
13739 fputs_filtered ("others => ", stream);
13740 print_subexp (exp, pos, stream, PREC_SUFFIX);
13741 return;
13742
13743 case OP_CHOICES:
13744 for (i = 0; i < nargs-1; i += 1)
13745 {
13746 if (i > 0)
13747 fputs_filtered ("|", stream);
13748 print_subexp (exp, pos, stream, PREC_SUFFIX);
13749 }
13750 fputs_filtered (" => ", stream);
13751 print_subexp (exp, pos, stream, PREC_SUFFIX);
13752 return;
13753
13754 case OP_POSITIONAL:
13755 print_subexp (exp, pos, stream, PREC_SUFFIX);
13756 return;
13757
13758 case OP_AGGREGATE:
13759 fputs_filtered ("(", stream);
13760 for (i = 0; i < nargs; i += 1)
13761 {
13762 if (i > 0)
13763 fputs_filtered (", ", stream);
13764 print_subexp (exp, pos, stream, PREC_SUFFIX);
13765 }
13766 fputs_filtered (")", stream);
13767 return;
4c4b4cd2
PH
13768 }
13769}
14f9c5c9
AS
13770
13771/* Table mapping opcodes into strings for printing operators
13772 and precedences of the operators. */
13773
d2e4a39e
AS
13774static const struct op_print ada_op_print_tab[] = {
13775 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
13776 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
13777 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
13778 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
13779 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
13780 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
13781 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
13782 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
13783 {"<=", BINOP_LEQ, PREC_ORDER, 0},
13784 {">=", BINOP_GEQ, PREC_ORDER, 0},
13785 {">", BINOP_GTR, PREC_ORDER, 0},
13786 {"<", BINOP_LESS, PREC_ORDER, 0},
13787 {">>", BINOP_RSH, PREC_SHIFT, 0},
13788 {"<<", BINOP_LSH, PREC_SHIFT, 0},
13789 {"+", BINOP_ADD, PREC_ADD, 0},
13790 {"-", BINOP_SUB, PREC_ADD, 0},
13791 {"&", BINOP_CONCAT, PREC_ADD, 0},
13792 {"*", BINOP_MUL, PREC_MUL, 0},
13793 {"/", BINOP_DIV, PREC_MUL, 0},
13794 {"rem", BINOP_REM, PREC_MUL, 0},
13795 {"mod", BINOP_MOD, PREC_MUL, 0},
13796 {"**", BINOP_EXP, PREC_REPEAT, 0},
13797 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
13798 {"-", UNOP_NEG, PREC_PREFIX, 0},
13799 {"+", UNOP_PLUS, PREC_PREFIX, 0},
13800 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
13801 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
13802 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
4c4b4cd2
PH
13803 {".all", UNOP_IND, PREC_SUFFIX, 1},
13804 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
13805 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
f486487f 13806 {NULL, OP_NULL, PREC_SUFFIX, 0}
14f9c5c9
AS
13807};
13808\f
72d5681a
PH
13809enum ada_primitive_types {
13810 ada_primitive_type_int,
13811 ada_primitive_type_long,
13812 ada_primitive_type_short,
13813 ada_primitive_type_char,
13814 ada_primitive_type_float,
13815 ada_primitive_type_double,
13816 ada_primitive_type_void,
13817 ada_primitive_type_long_long,
13818 ada_primitive_type_long_double,
13819 ada_primitive_type_natural,
13820 ada_primitive_type_positive,
13821 ada_primitive_type_system_address,
13822 nr_ada_primitive_types
13823};
6c038f32
PH
13824
13825static void
d4a9a881 13826ada_language_arch_info (struct gdbarch *gdbarch,
72d5681a
PH
13827 struct language_arch_info *lai)
13828{
d4a9a881 13829 const struct builtin_type *builtin = builtin_type (gdbarch);
5b4ee69b 13830
72d5681a 13831 lai->primitive_type_vector
d4a9a881 13832 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_ada_primitive_types + 1,
72d5681a 13833 struct type *);
e9bb382b
UW
13834
13835 lai->primitive_type_vector [ada_primitive_type_int]
13836 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13837 0, "integer");
13838 lai->primitive_type_vector [ada_primitive_type_long]
13839 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13840 0, "long_integer");
13841 lai->primitive_type_vector [ada_primitive_type_short]
13842 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13843 0, "short_integer");
13844 lai->string_char_type
13845 = lai->primitive_type_vector [ada_primitive_type_char]
cd7c1778 13846 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
e9bb382b
UW
13847 lai->primitive_type_vector [ada_primitive_type_float]
13848 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
13849 "float", NULL);
13850 lai->primitive_type_vector [ada_primitive_type_double]
13851 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13852 "long_float", NULL);
13853 lai->primitive_type_vector [ada_primitive_type_long_long]
13854 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
13855 0, "long_long_integer");
13856 lai->primitive_type_vector [ada_primitive_type_long_double]
13857 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13858 "long_long_float", NULL);
13859 lai->primitive_type_vector [ada_primitive_type_natural]
13860 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13861 0, "natural");
13862 lai->primitive_type_vector [ada_primitive_type_positive]
13863 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13864 0, "positive");
13865 lai->primitive_type_vector [ada_primitive_type_void]
13866 = builtin->builtin_void;
13867
13868 lai->primitive_type_vector [ada_primitive_type_system_address]
13869 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"));
72d5681a
PH
13870 TYPE_NAME (lai->primitive_type_vector [ada_primitive_type_system_address])
13871 = "system__address";
fbb06eb1 13872
47e729a8 13873 lai->bool_type_symbol = NULL;
fbb06eb1 13874 lai->bool_type_default = builtin->builtin_bool;
6c038f32 13875}
6c038f32
PH
13876\f
13877 /* Language vector */
13878
13879/* Not really used, but needed in the ada_language_defn. */
13880
13881static void
6c7a06a3 13882emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
6c038f32 13883{
6c7a06a3 13884 ada_emit_char (c, type, stream, quoter, 1);
6c038f32
PH
13885}
13886
13887static int
410a0ff2 13888parse (struct parser_state *ps)
6c038f32
PH
13889{
13890 warnings_issued = 0;
410a0ff2 13891 return ada_parse (ps);
6c038f32
PH
13892}
13893
13894static const struct exp_descriptor ada_exp_descriptor = {
13895 ada_print_subexp,
13896 ada_operator_length,
c0201579 13897 ada_operator_check,
6c038f32
PH
13898 ada_op_name,
13899 ada_dump_subexp_body,
13900 ada_evaluate_subexp
13901};
13902
1a119f36 13903/* Implement the "la_get_symbol_name_cmp" language_defn method
74ccd7f5
JB
13904 for Ada. */
13905
1a119f36
JB
13906static symbol_name_cmp_ftype
13907ada_get_symbol_name_cmp (const char *lookup_name)
74ccd7f5
JB
13908{
13909 if (should_use_wild_match (lookup_name))
13910 return wild_match;
13911 else
13912 return compare_names;
13913}
13914
a5ee536b
JB
13915/* Implement the "la_read_var_value" language_defn method for Ada. */
13916
13917static struct value *
63e43d3a
PMR
13918ada_read_var_value (struct symbol *var, const struct block *var_block,
13919 struct frame_info *frame)
a5ee536b 13920{
3977b71f 13921 const struct block *frame_block = NULL;
a5ee536b
JB
13922 struct symbol *renaming_sym = NULL;
13923
13924 /* The only case where default_read_var_value is not sufficient
13925 is when VAR is a renaming... */
13926 if (frame)
13927 frame_block = get_frame_block (frame, NULL);
13928 if (frame_block)
13929 renaming_sym = ada_find_renaming_symbol (var, frame_block);
13930 if (renaming_sym != NULL)
13931 return ada_read_renaming_var_value (renaming_sym, frame_block);
13932
13933 /* This is a typical case where we expect the default_read_var_value
13934 function to work. */
63e43d3a 13935 return default_read_var_value (var, var_block, frame);
a5ee536b
JB
13936}
13937
6c038f32
PH
13938const struct language_defn ada_language_defn = {
13939 "ada", /* Language name */
6abde28f 13940 "Ada",
6c038f32 13941 language_ada,
6c038f32 13942 range_check_off,
6c038f32
PH
13943 case_sensitive_on, /* Yes, Ada is case-insensitive, but
13944 that's not quite what this means. */
6c038f32 13945 array_row_major,
9a044a89 13946 macro_expansion_no,
6c038f32
PH
13947 &ada_exp_descriptor,
13948 parse,
13949 ada_error,
13950 resolve,
13951 ada_printchar, /* Print a character constant */
13952 ada_printstr, /* Function to print string constant */
13953 emit_char, /* Function to print single char (not used) */
6c038f32 13954 ada_print_type, /* Print a type using appropriate syntax */
be942545 13955 ada_print_typedef, /* Print a typedef using appropriate syntax */
6c038f32
PH
13956 ada_val_print, /* Print a value using appropriate syntax */
13957 ada_value_print, /* Print a top-level value */
a5ee536b 13958 ada_read_var_value, /* la_read_var_value */
6c038f32 13959 NULL, /* Language specific skip_trampoline */
2b2d9e11 13960 NULL, /* name_of_this */
6c038f32
PH
13961 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
13962 basic_lookup_transparent_type, /* lookup_transparent_type */
13963 ada_la_decode, /* Language specific symbol demangler */
0963b4bd
MS
13964 NULL, /* Language specific
13965 class_name_from_physname */
6c038f32
PH
13966 ada_op_print_tab, /* expression operators for printing */
13967 0, /* c-style arrays */
13968 1, /* String lower bound */
6c038f32 13969 ada_get_gdb_completer_word_break_characters,
41d27058 13970 ada_make_symbol_completion_list,
72d5681a 13971 ada_language_arch_info,
e79af960 13972 ada_print_array_index,
41f1b697 13973 default_pass_by_reference,
ae6a3a4c 13974 c_get_string,
1a119f36 13975 ada_get_symbol_name_cmp, /* la_get_symbol_name_cmp */
f8eba3c6 13976 ada_iterate_over_symbols,
a53b64ea 13977 &ada_varobj_ops,
bb2ec1b3
TT
13978 NULL,
13979 NULL,
6c038f32
PH
13980 LANG_MAGIC
13981};
13982
2c0b251b
PA
13983/* Provide a prototype to silence -Wmissing-prototypes. */
13984extern initialize_file_ftype _initialize_ada_language;
13985
5bf03f13
JB
13986/* Command-list for the "set/show ada" prefix command. */
13987static struct cmd_list_element *set_ada_list;
13988static struct cmd_list_element *show_ada_list;
13989
13990/* Implement the "set ada" prefix command. */
13991
13992static void
13993set_ada_command (char *arg, int from_tty)
13994{
13995 printf_unfiltered (_(\
13996"\"set ada\" must be followed by the name of a setting.\n"));
635c7e8a 13997 help_list (set_ada_list, "set ada ", all_commands, gdb_stdout);
5bf03f13
JB
13998}
13999
14000/* Implement the "show ada" prefix command. */
14001
14002static void
14003show_ada_command (char *args, int from_tty)
14004{
14005 cmd_show_list (show_ada_list, from_tty, "");
14006}
14007
2060206e
PA
14008static void
14009initialize_ada_catchpoint_ops (void)
14010{
14011 struct breakpoint_ops *ops;
14012
14013 initialize_breakpoint_ops ();
14014
14015 ops = &catch_exception_breakpoint_ops;
14016 *ops = bkpt_breakpoint_ops;
14017 ops->dtor = dtor_catch_exception;
14018 ops->allocate_location = allocate_location_catch_exception;
14019 ops->re_set = re_set_catch_exception;
14020 ops->check_status = check_status_catch_exception;
14021 ops->print_it = print_it_catch_exception;
14022 ops->print_one = print_one_catch_exception;
14023 ops->print_mention = print_mention_catch_exception;
14024 ops->print_recreate = print_recreate_catch_exception;
14025
14026 ops = &catch_exception_unhandled_breakpoint_ops;
14027 *ops = bkpt_breakpoint_ops;
14028 ops->dtor = dtor_catch_exception_unhandled;
14029 ops->allocate_location = allocate_location_catch_exception_unhandled;
14030 ops->re_set = re_set_catch_exception_unhandled;
14031 ops->check_status = check_status_catch_exception_unhandled;
14032 ops->print_it = print_it_catch_exception_unhandled;
14033 ops->print_one = print_one_catch_exception_unhandled;
14034 ops->print_mention = print_mention_catch_exception_unhandled;
14035 ops->print_recreate = print_recreate_catch_exception_unhandled;
14036
14037 ops = &catch_assert_breakpoint_ops;
14038 *ops = bkpt_breakpoint_ops;
14039 ops->dtor = dtor_catch_assert;
14040 ops->allocate_location = allocate_location_catch_assert;
14041 ops->re_set = re_set_catch_assert;
14042 ops->check_status = check_status_catch_assert;
14043 ops->print_it = print_it_catch_assert;
14044 ops->print_one = print_one_catch_assert;
14045 ops->print_mention = print_mention_catch_assert;
14046 ops->print_recreate = print_recreate_catch_assert;
14047}
14048
3d9434b5
JB
14049/* This module's 'new_objfile' observer. */
14050
14051static void
14052ada_new_objfile_observer (struct objfile *objfile)
14053{
14054 ada_clear_symbol_cache ();
14055}
14056
14057/* This module's 'free_objfile' observer. */
14058
14059static void
14060ada_free_objfile_observer (struct objfile *objfile)
14061{
14062 ada_clear_symbol_cache ();
14063}
14064
d2e4a39e 14065void
6c038f32 14066_initialize_ada_language (void)
14f9c5c9 14067{
6c038f32
PH
14068 add_language (&ada_language_defn);
14069
2060206e
PA
14070 initialize_ada_catchpoint_ops ();
14071
5bf03f13
JB
14072 add_prefix_cmd ("ada", no_class, set_ada_command,
14073 _("Prefix command for changing Ada-specfic settings"),
14074 &set_ada_list, "set ada ", 0, &setlist);
14075
14076 add_prefix_cmd ("ada", no_class, show_ada_command,
14077 _("Generic command for showing Ada-specific settings."),
14078 &show_ada_list, "show ada ", 0, &showlist);
14079
14080 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
14081 &trust_pad_over_xvs, _("\
14082Enable or disable an optimization trusting PAD types over XVS types"), _("\
14083Show whether an optimization trusting PAD types over XVS types is activated"),
14084 _("\
14085This is related to the encoding used by the GNAT compiler. The debugger\n\
14086should normally trust the contents of PAD types, but certain older versions\n\
14087of GNAT have a bug that sometimes causes the information in the PAD type\n\
14088to be incorrect. Turning this setting \"off\" allows the debugger to\n\
14089work around this bug. It is always safe to turn this option \"off\", but\n\
14090this incurs a slight performance penalty, so it is recommended to NOT change\n\
14091this option to \"off\" unless necessary."),
14092 NULL, NULL, &set_ada_list, &show_ada_list);
14093
9ac4176b
PA
14094 add_catch_command ("exception", _("\
14095Catch Ada exceptions, when raised.\n\
14096With an argument, catch only exceptions with the given name."),
14097 catch_ada_exception_command,
14098 NULL,
14099 CATCH_PERMANENT,
14100 CATCH_TEMPORARY);
14101 add_catch_command ("assert", _("\
14102Catch failed Ada assertions, when raised.\n\
14103With an argument, catch only exceptions with the given name."),
14104 catch_assert_command,
14105 NULL,
14106 CATCH_PERMANENT,
14107 CATCH_TEMPORARY);
14108
6c038f32 14109 varsize_limit = 65536;
6c038f32 14110
778865d3
JB
14111 add_info ("exceptions", info_exceptions_command,
14112 _("\
14113List all Ada exception names.\n\
14114If a regular expression is passed as an argument, only those matching\n\
14115the regular expression are listed."));
14116
c6044dd1
JB
14117 add_prefix_cmd ("ada", class_maintenance, maint_set_ada_cmd,
14118 _("Set Ada maintenance-related variables."),
14119 &maint_set_ada_cmdlist, "maintenance set ada ",
14120 0/*allow-unknown*/, &maintenance_set_cmdlist);
14121
14122 add_prefix_cmd ("ada", class_maintenance, maint_show_ada_cmd,
14123 _("Show Ada maintenance-related variables"),
14124 &maint_show_ada_cmdlist, "maintenance show ada ",
14125 0/*allow-unknown*/, &maintenance_show_cmdlist);
14126
14127 add_setshow_boolean_cmd
14128 ("ignore-descriptive-types", class_maintenance,
14129 &ada_ignore_descriptive_types_p,
14130 _("Set whether descriptive types generated by GNAT should be ignored."),
14131 _("Show whether descriptive types generated by GNAT should be ignored."),
14132 _("\
14133When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14134DWARF attribute."),
14135 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14136
6c038f32
PH
14137 obstack_init (&symbol_list_obstack);
14138
14139 decoded_names_store = htab_create_alloc
14140 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
14141 NULL, xcalloc, xfree);
6b69afc4 14142
3d9434b5
JB
14143 /* The ada-lang observers. */
14144 observer_attach_new_objfile (ada_new_objfile_observer);
14145 observer_attach_free_objfile (ada_free_objfile_observer);
e802dbe0 14146 observer_attach_inferior_exit (ada_inferior_exit);
ee01b665
JB
14147
14148 /* Setup various context-specific data. */
e802dbe0 14149 ada_inferior_data
8e260fc0 14150 = register_inferior_data_with_cleanup (NULL, ada_inferior_data_cleanup);
ee01b665
JB
14151 ada_pspace_data_handle
14152 = register_program_space_data_with_cleanup (NULL, ada_pspace_data_cleanup);
14f9c5c9 14153}