]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
* python/python.c (_initialize_python): Define new function
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
197e01b6 1/* Ada language support routines for GDB, the GNU debugger. Copyright (C)
10a2c479 2
ae6a3a4c
TJB
3 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008,
4 2009 Free Software Foundation, Inc.
14f9c5c9 5
a9762ec7 6 This file is part of GDB.
14f9c5c9 7
a9762ec7
JB
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
14f9c5c9 12
a9762ec7
JB
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
14f9c5c9 17
a9762ec7
JB
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 20
96d887e8 21
4c4b4cd2 22#include "defs.h"
14f9c5c9 23#include <stdio.h>
0c30c098 24#include "gdb_string.h"
14f9c5c9
AS
25#include <ctype.h>
26#include <stdarg.h>
27#include "demangle.h"
4c4b4cd2
PH
28#include "gdb_regex.h"
29#include "frame.h"
14f9c5c9
AS
30#include "symtab.h"
31#include "gdbtypes.h"
32#include "gdbcmd.h"
33#include "expression.h"
34#include "parser-defs.h"
35#include "language.h"
36#include "c-lang.h"
37#include "inferior.h"
38#include "symfile.h"
39#include "objfiles.h"
40#include "breakpoint.h"
41#include "gdbcore.h"
4c4b4cd2
PH
42#include "hashtab.h"
43#include "gdb_obstack.h"
14f9c5c9 44#include "ada-lang.h"
4c4b4cd2
PH
45#include "completer.h"
46#include "gdb_stat.h"
47#ifdef UI_OUT
14f9c5c9 48#include "ui-out.h"
4c4b4cd2 49#endif
fe898f56 50#include "block.h"
04714b91 51#include "infcall.h"
de4f826b 52#include "dictionary.h"
60250e8b 53#include "exceptions.h"
f7f9143b
JB
54#include "annotate.h"
55#include "valprint.h"
9bbc9174 56#include "source.h"
0259addd 57#include "observer.h"
2ba95b9b 58#include "vec.h"
692465f1 59#include "stack.h"
14f9c5c9 60
ccefe4c4
TT
61#include "psymtab.h"
62
4c4b4cd2
PH
63/* Define whether or not the C operator '/' truncates towards zero for
64 differently signed operands (truncation direction is undefined in C).
65 Copied from valarith.c. */
66
67#ifndef TRUNCATION_TOWARDS_ZERO
68#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
69#endif
70
50810684 71static void modify_general_field (struct type *, char *, LONGEST, int, int);
14f9c5c9 72
d2e4a39e 73static struct type *desc_base_type (struct type *);
14f9c5c9 74
d2e4a39e 75static struct type *desc_bounds_type (struct type *);
14f9c5c9 76
d2e4a39e 77static struct value *desc_bounds (struct value *);
14f9c5c9 78
d2e4a39e 79static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 80
d2e4a39e 81static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 82
556bdfd4 83static struct type *desc_data_target_type (struct type *);
14f9c5c9 84
d2e4a39e 85static struct value *desc_data (struct value *);
14f9c5c9 86
d2e4a39e 87static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 88
d2e4a39e 89static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 90
d2e4a39e 91static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 92
d2e4a39e 93static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 94
d2e4a39e 95static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 96
d2e4a39e 97static struct type *desc_index_type (struct type *, int);
14f9c5c9 98
d2e4a39e 99static int desc_arity (struct type *);
14f9c5c9 100
d2e4a39e 101static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 102
d2e4a39e 103static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 104
4a399546
UW
105static struct value *ensure_lval (struct value *,
106 struct gdbarch *, CORE_ADDR *);
14f9c5c9 107
d2e4a39e 108static struct value *make_array_descriptor (struct type *, struct value *,
4a399546 109 struct gdbarch *, CORE_ADDR *);
14f9c5c9 110
4c4b4cd2 111static void ada_add_block_symbols (struct obstack *,
76a01679 112 struct block *, const char *,
2570f2b7 113 domain_enum, struct objfile *, int);
14f9c5c9 114
4c4b4cd2 115static int is_nonfunction (struct ada_symbol_info *, int);
14f9c5c9 116
76a01679 117static void add_defn_to_vec (struct obstack *, struct symbol *,
2570f2b7 118 struct block *);
14f9c5c9 119
4c4b4cd2
PH
120static int num_defns_collected (struct obstack *);
121
122static struct ada_symbol_info *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,
4c4b4cd2 128 struct symbol *, 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 *,
150 struct block *);
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
4c4b4cd2
PH
218static struct value *ada_search_struct_field (char *, struct value *, int,
219 struct type *);
220
221static struct value *ada_value_primitive_field (struct value *, int, int,
222 struct type *);
223
76a01679 224static int find_struct_field (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
4c4b4cd2
PH
230static int ada_resolve_function (struct ada_symbol_info *, int,
231 struct value **, int, const char *,
232 struct type *);
233
234static struct value *ada_coerce_to_simple_array (struct value *);
235
236static int ada_is_direct_array_type (struct type *);
237
72d5681a
PH
238static void ada_language_arch_info (struct gdbarch *,
239 struct language_arch_info *);
714e53ab
PH
240
241static void check_size (const struct type *);
52ce6436
PH
242
243static struct value *ada_index_struct_field (int, struct value *, int,
244 struct type *);
245
246static struct value *assign_aggregate (struct value *, struct value *,
247 struct expression *, int *, enum noside);
248
249static void aggregate_assign_from_choices (struct value *, struct value *,
250 struct expression *,
251 int *, LONGEST *, int *,
252 int, LONGEST, LONGEST);
253
254static void aggregate_assign_positional (struct value *, struct value *,
255 struct expression *,
256 int *, LONGEST *, int *, int,
257 LONGEST, LONGEST);
258
259
260static void aggregate_assign_others (struct value *, struct value *,
261 struct expression *,
262 int *, LONGEST *, int, LONGEST, LONGEST);
263
264
265static void add_component_interval (LONGEST, LONGEST, LONGEST *, int *, int);
266
267
268static struct value *ada_evaluate_subexp (struct type *, struct expression *,
269 int *, enum noside);
270
271static void ada_forward_operator_length (struct expression *, int, int *,
272 int *);
4c4b4cd2
PH
273\f
274
76a01679 275
4c4b4cd2 276/* Maximum-sized dynamic type. */
14f9c5c9
AS
277static unsigned int varsize_limit;
278
4c4b4cd2
PH
279/* FIXME: brobecker/2003-09-17: No longer a const because it is
280 returned by a function that does not return a const char *. */
281static char *ada_completer_word_break_characters =
282#ifdef VMS
283 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
284#else
14f9c5c9 285 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 286#endif
14f9c5c9 287
4c4b4cd2 288/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 289static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 290 = "__gnat_ada_main_program_name";
14f9c5c9 291
4c4b4cd2
PH
292/* Limit on the number of warnings to raise per expression evaluation. */
293static int warning_limit = 2;
294
295/* Number of warning messages issued; reset to 0 by cleanups after
296 expression evaluation. */
297static int warnings_issued = 0;
298
299static const char *known_runtime_file_name_patterns[] = {
300 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
301};
302
303static const char *known_auxiliary_function_name_patterns[] = {
304 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
305};
306
307/* Space for allocating results of ada_lookup_symbol_list. */
308static struct obstack symbol_list_obstack;
309
e802dbe0
JB
310 /* Inferior-specific data. */
311
312/* Per-inferior data for this module. */
313
314struct ada_inferior_data
315{
316 /* The ada__tags__type_specific_data type, which is used when decoding
317 tagged types. With older versions of GNAT, this type was directly
318 accessible through a component ("tsd") in the object tag. But this
319 is no longer the case, so we cache it for each inferior. */
320 struct type *tsd_type;
321};
322
323/* Our key to this module's inferior data. */
324static const struct inferior_data *ada_inferior_data;
325
326/* A cleanup routine for our inferior data. */
327static void
328ada_inferior_data_cleanup (struct inferior *inf, void *arg)
329{
330 struct ada_inferior_data *data;
331
332 data = inferior_data (inf, ada_inferior_data);
333 if (data != NULL)
334 xfree (data);
335}
336
337/* Return our inferior data for the given inferior (INF).
338
339 This function always returns a valid pointer to an allocated
340 ada_inferior_data structure. If INF's inferior data has not
341 been previously set, this functions creates a new one with all
342 fields set to zero, sets INF's inferior to it, and then returns
343 a pointer to that newly allocated ada_inferior_data. */
344
345static struct ada_inferior_data *
346get_ada_inferior_data (struct inferior *inf)
347{
348 struct ada_inferior_data *data;
349
350 data = inferior_data (inf, ada_inferior_data);
351 if (data == NULL)
352 {
353 data = XZALLOC (struct ada_inferior_data);
354 set_inferior_data (inf, ada_inferior_data, data);
355 }
356
357 return data;
358}
359
360/* Perform all necessary cleanups regarding our module's inferior data
361 that is required after the inferior INF just exited. */
362
363static void
364ada_inferior_exit (struct inferior *inf)
365{
366 ada_inferior_data_cleanup (inf, NULL);
367 set_inferior_data (inf, ada_inferior_data, NULL);
368}
369
4c4b4cd2
PH
370 /* Utilities */
371
41d27058
JB
372/* Given DECODED_NAME a string holding a symbol name in its
373 decoded form (ie using the Ada dotted notation), returns
374 its unqualified name. */
375
376static const char *
377ada_unqualified_name (const char *decoded_name)
378{
379 const char *result = strrchr (decoded_name, '.');
380
381 if (result != NULL)
382 result++; /* Skip the dot... */
383 else
384 result = decoded_name;
385
386 return result;
387}
388
389/* Return a string starting with '<', followed by STR, and '>'.
390 The result is good until the next call. */
391
392static char *
393add_angle_brackets (const char *str)
394{
395 static char *result = NULL;
396
397 xfree (result);
88c15c34 398 result = xstrprintf ("<%s>", str);
41d27058
JB
399 return result;
400}
96d887e8 401
4c4b4cd2
PH
402static char *
403ada_get_gdb_completer_word_break_characters (void)
404{
405 return ada_completer_word_break_characters;
406}
407
e79af960
JB
408/* Print an array element index using the Ada syntax. */
409
410static void
411ada_print_array_index (struct value *index_value, struct ui_file *stream,
79a45b7d 412 const struct value_print_options *options)
e79af960 413{
79a45b7d 414 LA_VALUE_PRINT (index_value, stream, options);
e79af960
JB
415 fprintf_filtered (stream, " => ");
416}
417
f27cf670 418/* Assuming VECT points to an array of *SIZE objects of size
14f9c5c9 419 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
f27cf670 420 updating *SIZE as necessary and returning the (new) array. */
14f9c5c9 421
f27cf670
AS
422void *
423grow_vect (void *vect, size_t *size, size_t min_size, int element_size)
14f9c5c9 424{
d2e4a39e
AS
425 if (*size < min_size)
426 {
427 *size *= 2;
428 if (*size < min_size)
4c4b4cd2 429 *size = min_size;
f27cf670 430 vect = xrealloc (vect, *size * element_size);
d2e4a39e 431 }
f27cf670 432 return vect;
14f9c5c9
AS
433}
434
435/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 436 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
437
438static int
ebf56fd3 439field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
440{
441 int len = strlen (target);
5b4ee69b 442
d2e4a39e 443 return
4c4b4cd2
PH
444 (strncmp (field_name, target, len) == 0
445 && (field_name[len] == '\0'
446 || (strncmp (field_name + len, "___", 3) == 0
76a01679
JB
447 && strcmp (field_name + strlen (field_name) - 6,
448 "___XVN") != 0)));
14f9c5c9
AS
449}
450
451
872c8b51
JB
452/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
453 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
454 and return its index. This function also handles fields whose name
455 have ___ suffixes because the compiler sometimes alters their name
456 by adding such a suffix to represent fields with certain constraints.
457 If the field could not be found, return a negative number if
458 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
459
460int
461ada_get_field_index (const struct type *type, const char *field_name,
462 int maybe_missing)
463{
464 int fieldno;
872c8b51
JB
465 struct type *struct_type = check_typedef ((struct type *) type);
466
467 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); fieldno++)
468 if (field_name_match (TYPE_FIELD_NAME (struct_type, fieldno), field_name))
4c4b4cd2
PH
469 return fieldno;
470
471 if (!maybe_missing)
323e0a4a 472 error (_("Unable to find field %s in struct %s. Aborting"),
872c8b51 473 field_name, TYPE_NAME (struct_type));
4c4b4cd2
PH
474
475 return -1;
476}
477
478/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
479
480int
d2e4a39e 481ada_name_prefix_len (const char *name)
14f9c5c9
AS
482{
483 if (name == NULL)
484 return 0;
d2e4a39e 485 else
14f9c5c9 486 {
d2e4a39e 487 const char *p = strstr (name, "___");
5b4ee69b 488
14f9c5c9 489 if (p == NULL)
4c4b4cd2 490 return strlen (name);
14f9c5c9 491 else
4c4b4cd2 492 return p - name;
14f9c5c9
AS
493 }
494}
495
4c4b4cd2
PH
496/* Return non-zero if SUFFIX is a suffix of STR.
497 Return zero if STR is null. */
498
14f9c5c9 499static int
d2e4a39e 500is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
501{
502 int len1, len2;
5b4ee69b 503
14f9c5c9
AS
504 if (str == NULL)
505 return 0;
506 len1 = strlen (str);
507 len2 = strlen (suffix);
4c4b4cd2 508 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
509}
510
4c4b4cd2
PH
511/* The contents of value VAL, treated as a value of type TYPE. The
512 result is an lval in memory if VAL is. */
14f9c5c9 513
d2e4a39e 514static struct value *
4c4b4cd2 515coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 516{
61ee279c 517 type = ada_check_typedef (type);
df407dfe 518 if (value_type (val) == type)
4c4b4cd2 519 return val;
d2e4a39e 520 else
14f9c5c9 521 {
4c4b4cd2
PH
522 struct value *result;
523
524 /* Make sure that the object size is not unreasonable before
525 trying to allocate some memory for it. */
714e53ab 526 check_size (type);
4c4b4cd2
PH
527
528 result = allocate_value (type);
74bcbdf3 529 set_value_component_location (result, val);
9bbda503
AC
530 set_value_bitsize (result, value_bitsize (val));
531 set_value_bitpos (result, value_bitpos (val));
42ae5230 532 set_value_address (result, value_address (val));
d69fe07e 533 if (value_lazy (val)
df407dfe 534 || TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
dfa52d88 535 set_value_lazy (result, 1);
d2e4a39e 536 else
0fd88904 537 memcpy (value_contents_raw (result), value_contents (val),
4c4b4cd2 538 TYPE_LENGTH (type));
14f9c5c9
AS
539 return result;
540 }
541}
542
fc1a4b47
AC
543static const gdb_byte *
544cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
545{
546 if (valaddr == NULL)
547 return NULL;
548 else
549 return valaddr + offset;
550}
551
552static CORE_ADDR
ebf56fd3 553cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
554{
555 if (address == 0)
556 return 0;
d2e4a39e 557 else
14f9c5c9
AS
558 return address + offset;
559}
560
4c4b4cd2
PH
561/* Issue a warning (as for the definition of warning in utils.c, but
562 with exactly one argument rather than ...), unless the limit on the
563 number of warnings has passed during the evaluation of the current
564 expression. */
a2249542 565
77109804
AC
566/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
567 provided by "complaint". */
a0b31db1 568static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 569
14f9c5c9 570static void
a2249542 571lim_warning (const char *format, ...)
14f9c5c9 572{
a2249542 573 va_list args;
a2249542 574
5b4ee69b 575 va_start (args, format);
4c4b4cd2
PH
576 warnings_issued += 1;
577 if (warnings_issued <= warning_limit)
a2249542
MK
578 vwarning (format, args);
579
580 va_end (args);
4c4b4cd2
PH
581}
582
714e53ab
PH
583/* Issue an error if the size of an object of type T is unreasonable,
584 i.e. if it would be a bad idea to allocate a value of this type in
585 GDB. */
586
587static void
588check_size (const struct type *type)
589{
590 if (TYPE_LENGTH (type) > varsize_limit)
323e0a4a 591 error (_("object size is larger than varsize-limit"));
714e53ab
PH
592}
593
c3e5cd34 594/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 595static LONGEST
c3e5cd34 596max_of_size (int size)
4c4b4cd2 597{
76a01679 598 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 599
76a01679 600 return top_bit | (top_bit - 1);
4c4b4cd2
PH
601}
602
c3e5cd34 603/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 604static LONGEST
c3e5cd34 605min_of_size (int size)
4c4b4cd2 606{
c3e5cd34 607 return -max_of_size (size) - 1;
4c4b4cd2
PH
608}
609
c3e5cd34 610/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 611static ULONGEST
c3e5cd34 612umax_of_size (int size)
4c4b4cd2 613{
76a01679 614 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 615
76a01679 616 return top_bit | (top_bit - 1);
4c4b4cd2
PH
617}
618
c3e5cd34
PH
619/* Maximum value of integral type T, as a signed quantity. */
620static LONGEST
621max_of_type (struct type *t)
4c4b4cd2 622{
c3e5cd34
PH
623 if (TYPE_UNSIGNED (t))
624 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
625 else
626 return max_of_size (TYPE_LENGTH (t));
627}
628
629/* Minimum value of integral type T, as a signed quantity. */
630static LONGEST
631min_of_type (struct type *t)
632{
633 if (TYPE_UNSIGNED (t))
634 return 0;
635 else
636 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
637}
638
639/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
640LONGEST
641ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 642{
76a01679 643 switch (TYPE_CODE (type))
4c4b4cd2
PH
644 {
645 case TYPE_CODE_RANGE:
690cc4eb 646 return TYPE_HIGH_BOUND (type);
4c4b4cd2 647 case TYPE_CODE_ENUM:
690cc4eb
PH
648 return TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
649 case TYPE_CODE_BOOL:
650 return 1;
651 case TYPE_CODE_CHAR:
76a01679 652 case TYPE_CODE_INT:
690cc4eb 653 return max_of_type (type);
4c4b4cd2 654 default:
43bbcdc2 655 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
656 }
657}
658
659/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
660LONGEST
661ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 662{
76a01679 663 switch (TYPE_CODE (type))
4c4b4cd2
PH
664 {
665 case TYPE_CODE_RANGE:
690cc4eb 666 return TYPE_LOW_BOUND (type);
4c4b4cd2 667 case TYPE_CODE_ENUM:
690cc4eb
PH
668 return TYPE_FIELD_BITPOS (type, 0);
669 case TYPE_CODE_BOOL:
670 return 0;
671 case TYPE_CODE_CHAR:
76a01679 672 case TYPE_CODE_INT:
690cc4eb 673 return min_of_type (type);
4c4b4cd2 674 default:
43bbcdc2 675 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
676 }
677}
678
679/* The identity on non-range types. For range types, the underlying
76a01679 680 non-range scalar type. */
4c4b4cd2
PH
681
682static struct type *
683base_type (struct type *type)
684{
685 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
686 {
76a01679
JB
687 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
688 return type;
4c4b4cd2
PH
689 type = TYPE_TARGET_TYPE (type);
690 }
691 return type;
14f9c5c9 692}
4c4b4cd2 693\f
76a01679 694
4c4b4cd2 695 /* Language Selection */
14f9c5c9
AS
696
697/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 698 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 699
14f9c5c9 700enum language
ccefe4c4 701ada_update_initial_language (enum language lang)
14f9c5c9 702{
d2e4a39e 703 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
4c4b4cd2
PH
704 (struct objfile *) NULL) != NULL)
705 return language_ada;
14f9c5c9
AS
706
707 return lang;
708}
96d887e8
PH
709
710/* If the main procedure is written in Ada, then return its name.
711 The result is good until the next call. Return NULL if the main
712 procedure doesn't appear to be in Ada. */
713
714char *
715ada_main_name (void)
716{
717 struct minimal_symbol *msym;
f9bc20b9 718 static char *main_program_name = NULL;
6c038f32 719
96d887e8
PH
720 /* For Ada, the name of the main procedure is stored in a specific
721 string constant, generated by the binder. Look for that symbol,
722 extract its address, and then read that string. If we didn't find
723 that string, then most probably the main procedure is not written
724 in Ada. */
725 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
726
727 if (msym != NULL)
728 {
f9bc20b9
JB
729 CORE_ADDR main_program_name_addr;
730 int err_code;
731
96d887e8
PH
732 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
733 if (main_program_name_addr == 0)
323e0a4a 734 error (_("Invalid address for Ada main program name."));
96d887e8 735
f9bc20b9
JB
736 xfree (main_program_name);
737 target_read_string (main_program_name_addr, &main_program_name,
738 1024, &err_code);
739
740 if (err_code != 0)
741 return NULL;
96d887e8
PH
742 return main_program_name;
743 }
744
745 /* The main procedure doesn't seem to be in Ada. */
746 return NULL;
747}
14f9c5c9 748\f
4c4b4cd2 749 /* Symbols */
d2e4a39e 750
4c4b4cd2
PH
751/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
752 of NULLs. */
14f9c5c9 753
d2e4a39e
AS
754const struct ada_opname_map ada_opname_table[] = {
755 {"Oadd", "\"+\"", BINOP_ADD},
756 {"Osubtract", "\"-\"", BINOP_SUB},
757 {"Omultiply", "\"*\"", BINOP_MUL},
758 {"Odivide", "\"/\"", BINOP_DIV},
759 {"Omod", "\"mod\"", BINOP_MOD},
760 {"Orem", "\"rem\"", BINOP_REM},
761 {"Oexpon", "\"**\"", BINOP_EXP},
762 {"Olt", "\"<\"", BINOP_LESS},
763 {"Ole", "\"<=\"", BINOP_LEQ},
764 {"Ogt", "\">\"", BINOP_GTR},
765 {"Oge", "\">=\"", BINOP_GEQ},
766 {"Oeq", "\"=\"", BINOP_EQUAL},
767 {"One", "\"/=\"", BINOP_NOTEQUAL},
768 {"Oand", "\"and\"", BINOP_BITWISE_AND},
769 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
770 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
771 {"Oconcat", "\"&\"", BINOP_CONCAT},
772 {"Oabs", "\"abs\"", UNOP_ABS},
773 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
774 {"Oadd", "\"+\"", UNOP_PLUS},
775 {"Osubtract", "\"-\"", UNOP_NEG},
776 {NULL, NULL}
14f9c5c9
AS
777};
778
4c4b4cd2
PH
779/* The "encoded" form of DECODED, according to GNAT conventions.
780 The result is valid until the next call to ada_encode. */
781
14f9c5c9 782char *
4c4b4cd2 783ada_encode (const char *decoded)
14f9c5c9 784{
4c4b4cd2
PH
785 static char *encoding_buffer = NULL;
786 static size_t encoding_buffer_size = 0;
d2e4a39e 787 const char *p;
14f9c5c9 788 int k;
d2e4a39e 789
4c4b4cd2 790 if (decoded == NULL)
14f9c5c9
AS
791 return NULL;
792
4c4b4cd2
PH
793 GROW_VECT (encoding_buffer, encoding_buffer_size,
794 2 * strlen (decoded) + 10);
14f9c5c9
AS
795
796 k = 0;
4c4b4cd2 797 for (p = decoded; *p != '\0'; p += 1)
14f9c5c9 798 {
cdc7bb92 799 if (*p == '.')
4c4b4cd2
PH
800 {
801 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
802 k += 2;
803 }
14f9c5c9 804 else if (*p == '"')
4c4b4cd2
PH
805 {
806 const struct ada_opname_map *mapping;
807
808 for (mapping = ada_opname_table;
1265e4aa
JB
809 mapping->encoded != NULL
810 && strncmp (mapping->decoded, p,
811 strlen (mapping->decoded)) != 0; mapping += 1)
4c4b4cd2
PH
812 ;
813 if (mapping->encoded == NULL)
323e0a4a 814 error (_("invalid Ada operator name: %s"), p);
4c4b4cd2
PH
815 strcpy (encoding_buffer + k, mapping->encoded);
816 k += strlen (mapping->encoded);
817 break;
818 }
d2e4a39e 819 else
4c4b4cd2
PH
820 {
821 encoding_buffer[k] = *p;
822 k += 1;
823 }
14f9c5c9
AS
824 }
825
4c4b4cd2
PH
826 encoding_buffer[k] = '\0';
827 return encoding_buffer;
14f9c5c9
AS
828}
829
830/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
831 quotes, unfolded, but with the quotes stripped away. Result good
832 to next call. */
833
d2e4a39e
AS
834char *
835ada_fold_name (const char *name)
14f9c5c9 836{
d2e4a39e 837 static char *fold_buffer = NULL;
14f9c5c9
AS
838 static size_t fold_buffer_size = 0;
839
840 int len = strlen (name);
d2e4a39e 841 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
14f9c5c9
AS
842
843 if (name[0] == '\'')
844 {
d2e4a39e
AS
845 strncpy (fold_buffer, name + 1, len - 2);
846 fold_buffer[len - 2] = '\000';
14f9c5c9
AS
847 }
848 else
849 {
850 int i;
5b4ee69b 851
14f9c5c9 852 for (i = 0; i <= len; i += 1)
4c4b4cd2 853 fold_buffer[i] = tolower (name[i]);
14f9c5c9
AS
854 }
855
856 return fold_buffer;
857}
858
529cad9c
PH
859/* Return nonzero if C is either a digit or a lowercase alphabet character. */
860
861static int
862is_lower_alphanum (const char c)
863{
864 return (isdigit (c) || (isalpha (c) && islower (c)));
865}
866
29480c32
JB
867/* Remove either of these suffixes:
868 . .{DIGIT}+
869 . ${DIGIT}+
870 . ___{DIGIT}+
871 . __{DIGIT}+.
872 These are suffixes introduced by the compiler for entities such as
873 nested subprogram for instance, in order to avoid name clashes.
874 They do not serve any purpose for the debugger. */
875
876static void
877ada_remove_trailing_digits (const char *encoded, int *len)
878{
879 if (*len > 1 && isdigit (encoded[*len - 1]))
880 {
881 int i = *len - 2;
5b4ee69b 882
29480c32
JB
883 while (i > 0 && isdigit (encoded[i]))
884 i--;
885 if (i >= 0 && encoded[i] == '.')
886 *len = i;
887 else if (i >= 0 && encoded[i] == '$')
888 *len = i;
889 else if (i >= 2 && strncmp (encoded + i - 2, "___", 3) == 0)
890 *len = i - 2;
891 else if (i >= 1 && strncmp (encoded + i - 1, "__", 2) == 0)
892 *len = i - 1;
893 }
894}
895
896/* Remove the suffix introduced by the compiler for protected object
897 subprograms. */
898
899static void
900ada_remove_po_subprogram_suffix (const char *encoded, int *len)
901{
902 /* Remove trailing N. */
903
904 /* Protected entry subprograms are broken into two
905 separate subprograms: The first one is unprotected, and has
906 a 'N' suffix; the second is the protected version, and has
907 the 'P' suffix. The second calls the first one after handling
908 the protection. Since the P subprograms are internally generated,
909 we leave these names undecoded, giving the user a clue that this
910 entity is internal. */
911
912 if (*len > 1
913 && encoded[*len - 1] == 'N'
914 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
915 *len = *len - 1;
916}
917
69fadcdf
JB
918/* Remove trailing X[bn]* suffixes (indicating names in package bodies). */
919
920static void
921ada_remove_Xbn_suffix (const char *encoded, int *len)
922{
923 int i = *len - 1;
924
925 while (i > 0 && (encoded[i] == 'b' || encoded[i] == 'n'))
926 i--;
927
928 if (encoded[i] != 'X')
929 return;
930
931 if (i == 0)
932 return;
933
934 if (isalnum (encoded[i-1]))
935 *len = i;
936}
937
29480c32
JB
938/* If ENCODED follows the GNAT entity encoding conventions, then return
939 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
940 replaced by ENCODED.
14f9c5c9 941
4c4b4cd2 942 The resulting string is valid until the next call of ada_decode.
29480c32 943 If the string is unchanged by decoding, the original string pointer
4c4b4cd2
PH
944 is returned. */
945
946const char *
947ada_decode (const char *encoded)
14f9c5c9
AS
948{
949 int i, j;
950 int len0;
d2e4a39e 951 const char *p;
4c4b4cd2 952 char *decoded;
14f9c5c9 953 int at_start_name;
4c4b4cd2
PH
954 static char *decoding_buffer = NULL;
955 static size_t decoding_buffer_size = 0;
d2e4a39e 956
29480c32
JB
957 /* The name of the Ada main procedure starts with "_ada_".
958 This prefix is not part of the decoded name, so skip this part
959 if we see this prefix. */
4c4b4cd2
PH
960 if (strncmp (encoded, "_ada_", 5) == 0)
961 encoded += 5;
14f9c5c9 962
29480c32
JB
963 /* If the name starts with '_', then it is not a properly encoded
964 name, so do not attempt to decode it. Similarly, if the name
965 starts with '<', the name should not be decoded. */
4c4b4cd2 966 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
967 goto Suppress;
968
4c4b4cd2 969 len0 = strlen (encoded);
4c4b4cd2 970
29480c32
JB
971 ada_remove_trailing_digits (encoded, &len0);
972 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 973
4c4b4cd2
PH
974 /* Remove the ___X.* suffix if present. Do not forget to verify that
975 the suffix is located before the current "end" of ENCODED. We want
976 to avoid re-matching parts of ENCODED that have previously been
977 marked as discarded (by decrementing LEN0). */
978 p = strstr (encoded, "___");
979 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
980 {
981 if (p[3] == 'X')
4c4b4cd2 982 len0 = p - encoded;
14f9c5c9 983 else
4c4b4cd2 984 goto Suppress;
14f9c5c9 985 }
4c4b4cd2 986
29480c32
JB
987 /* Remove any trailing TKB suffix. It tells us that this symbol
988 is for the body of a task, but that information does not actually
989 appear in the decoded name. */
990
4c4b4cd2 991 if (len0 > 3 && strncmp (encoded + len0 - 3, "TKB", 3) == 0)
14f9c5c9 992 len0 -= 3;
76a01679 993
a10967fa
JB
994 /* Remove any trailing TB suffix. The TB suffix is slightly different
995 from the TKB suffix because it is used for non-anonymous task
996 bodies. */
997
998 if (len0 > 2 && strncmp (encoded + len0 - 2, "TB", 2) == 0)
999 len0 -= 2;
1000
29480c32
JB
1001 /* Remove trailing "B" suffixes. */
1002 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1003
4c4b4cd2 1004 if (len0 > 1 && strncmp (encoded + len0 - 1, "B", 1) == 0)
14f9c5c9
AS
1005 len0 -= 1;
1006
4c4b4cd2 1007 /* Make decoded big enough for possible expansion by operator name. */
29480c32 1008
4c4b4cd2
PH
1009 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
1010 decoded = decoding_buffer;
14f9c5c9 1011
29480c32
JB
1012 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1013
4c4b4cd2 1014 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1015 {
4c4b4cd2
PH
1016 i = len0 - 2;
1017 while ((i >= 0 && isdigit (encoded[i]))
1018 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1019 i -= 1;
1020 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
1021 len0 = i - 1;
1022 else if (encoded[i] == '$')
1023 len0 = i;
d2e4a39e 1024 }
14f9c5c9 1025
29480c32
JB
1026 /* The first few characters that are not alphabetic are not part
1027 of any encoding we use, so we can copy them over verbatim. */
1028
4c4b4cd2
PH
1029 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1030 decoded[j] = encoded[i];
14f9c5c9
AS
1031
1032 at_start_name = 1;
1033 while (i < len0)
1034 {
29480c32 1035 /* Is this a symbol function? */
4c4b4cd2
PH
1036 if (at_start_name && encoded[i] == 'O')
1037 {
1038 int k;
5b4ee69b 1039
4c4b4cd2
PH
1040 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1041 {
1042 int op_len = strlen (ada_opname_table[k].encoded);
06d5cf63
JB
1043 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1044 op_len - 1) == 0)
1045 && !isalnum (encoded[i + op_len]))
4c4b4cd2
PH
1046 {
1047 strcpy (decoded + j, ada_opname_table[k].decoded);
1048 at_start_name = 0;
1049 i += op_len;
1050 j += strlen (ada_opname_table[k].decoded);
1051 break;
1052 }
1053 }
1054 if (ada_opname_table[k].encoded != NULL)
1055 continue;
1056 }
14f9c5c9
AS
1057 at_start_name = 0;
1058
529cad9c
PH
1059 /* Replace "TK__" with "__", which will eventually be translated
1060 into "." (just below). */
1061
4c4b4cd2
PH
1062 if (i < len0 - 4 && strncmp (encoded + i, "TK__", 4) == 0)
1063 i += 2;
529cad9c 1064
29480c32
JB
1065 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1066 be translated into "." (just below). These are internal names
1067 generated for anonymous blocks inside which our symbol is nested. */
1068
1069 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
1070 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1071 && isdigit (encoded [i+4]))
1072 {
1073 int k = i + 5;
1074
1075 while (k < len0 && isdigit (encoded[k]))
1076 k++; /* Skip any extra digit. */
1077
1078 /* Double-check that the "__B_{DIGITS}+" sequence we found
1079 is indeed followed by "__". */
1080 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1081 i = k;
1082 }
1083
529cad9c
PH
1084 /* Remove _E{DIGITS}+[sb] */
1085
1086 /* Just as for protected object subprograms, there are 2 categories
1087 of subprograms created by the compiler for each entry. The first
1088 one implements the actual entry code, and has a suffix following
1089 the convention above; the second one implements the barrier and
1090 uses the same convention as above, except that the 'E' is replaced
1091 by a 'B'.
1092
1093 Just as above, we do not decode the name of barrier functions
1094 to give the user a clue that the code he is debugging has been
1095 internally generated. */
1096
1097 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
1098 && isdigit (encoded[i+2]))
1099 {
1100 int k = i + 3;
1101
1102 while (k < len0 && isdigit (encoded[k]))
1103 k++;
1104
1105 if (k < len0
1106 && (encoded[k] == 'b' || encoded[k] == 's'))
1107 {
1108 k++;
1109 /* Just as an extra precaution, make sure that if this
1110 suffix is followed by anything else, it is a '_'.
1111 Otherwise, we matched this sequence by accident. */
1112 if (k == len0
1113 || (k < len0 && encoded[k] == '_'))
1114 i = k;
1115 }
1116 }
1117
1118 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1119 the GNAT front-end in protected object subprograms. */
1120
1121 if (i < len0 + 3
1122 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1123 {
1124 /* Backtrack a bit up until we reach either the begining of
1125 the encoded name, or "__". Make sure that we only find
1126 digits or lowercase characters. */
1127 const char *ptr = encoded + i - 1;
1128
1129 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1130 ptr--;
1131 if (ptr < encoded
1132 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1133 i++;
1134 }
1135
4c4b4cd2
PH
1136 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
1137 {
29480c32
JB
1138 /* This is a X[bn]* sequence not separated from the previous
1139 part of the name with a non-alpha-numeric character (in other
1140 words, immediately following an alpha-numeric character), then
1141 verify that it is placed at the end of the encoded name. If
1142 not, then the encoding is not valid and we should abort the
1143 decoding. Otherwise, just skip it, it is used in body-nested
1144 package names. */
4c4b4cd2
PH
1145 do
1146 i += 1;
1147 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1148 if (i < len0)
1149 goto Suppress;
1150 }
cdc7bb92 1151 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
4c4b4cd2 1152 {
29480c32 1153 /* Replace '__' by '.'. */
4c4b4cd2
PH
1154 decoded[j] = '.';
1155 at_start_name = 1;
1156 i += 2;
1157 j += 1;
1158 }
14f9c5c9 1159 else
4c4b4cd2 1160 {
29480c32
JB
1161 /* It's a character part of the decoded name, so just copy it
1162 over. */
4c4b4cd2
PH
1163 decoded[j] = encoded[i];
1164 i += 1;
1165 j += 1;
1166 }
14f9c5c9 1167 }
4c4b4cd2 1168 decoded[j] = '\000';
14f9c5c9 1169
29480c32
JB
1170 /* Decoded names should never contain any uppercase character.
1171 Double-check this, and abort the decoding if we find one. */
1172
4c4b4cd2
PH
1173 for (i = 0; decoded[i] != '\0'; i += 1)
1174 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1175 goto Suppress;
1176
4c4b4cd2
PH
1177 if (strcmp (decoded, encoded) == 0)
1178 return encoded;
1179 else
1180 return decoded;
14f9c5c9
AS
1181
1182Suppress:
4c4b4cd2
PH
1183 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
1184 decoded = decoding_buffer;
1185 if (encoded[0] == '<')
1186 strcpy (decoded, encoded);
14f9c5c9 1187 else
88c15c34 1188 xsnprintf (decoded, decoding_buffer_size, "<%s>", encoded);
4c4b4cd2
PH
1189 return decoded;
1190
1191}
1192
1193/* Table for keeping permanent unique copies of decoded names. Once
1194 allocated, names in this table are never released. While this is a
1195 storage leak, it should not be significant unless there are massive
1196 changes in the set of decoded names in successive versions of a
1197 symbol table loaded during a single session. */
1198static struct htab *decoded_names_store;
1199
1200/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1201 in the language-specific part of GSYMBOL, if it has not been
1202 previously computed. Tries to save the decoded name in the same
1203 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1204 in any case, the decoded symbol has a lifetime at least that of
1205 GSYMBOL).
1206 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1207 const, but nevertheless modified to a semantically equivalent form
1208 when a decoded name is cached in it.
76a01679 1209*/
4c4b4cd2 1210
76a01679
JB
1211char *
1212ada_decode_symbol (const struct general_symbol_info *gsymbol)
4c4b4cd2 1213{
76a01679 1214 char **resultp =
afa16725 1215 (char **) &gsymbol->language_specific.mangled_lang.demangled_name;
5b4ee69b 1216
4c4b4cd2
PH
1217 if (*resultp == NULL)
1218 {
1219 const char *decoded = ada_decode (gsymbol->name);
5b4ee69b 1220
714835d5 1221 if (gsymbol->obj_section != NULL)
76a01679 1222 {
714835d5 1223 struct objfile *objf = gsymbol->obj_section->objfile;
5b4ee69b 1224
714835d5
UW
1225 *resultp = obsavestring (decoded, strlen (decoded),
1226 &objf->objfile_obstack);
76a01679 1227 }
4c4b4cd2 1228 /* Sometimes, we can't find a corresponding objfile, in which
76a01679
JB
1229 case, we put the result on the heap. Since we only decode
1230 when needed, we hope this usually does not cause a
1231 significant memory leak (FIXME). */
4c4b4cd2 1232 if (*resultp == NULL)
76a01679
JB
1233 {
1234 char **slot = (char **) htab_find_slot (decoded_names_store,
1235 decoded, INSERT);
5b4ee69b 1236
76a01679
JB
1237 if (*slot == NULL)
1238 *slot = xstrdup (decoded);
1239 *resultp = *slot;
1240 }
4c4b4cd2 1241 }
14f9c5c9 1242
4c4b4cd2
PH
1243 return *resultp;
1244}
76a01679 1245
2c0b251b 1246static char *
76a01679 1247ada_la_decode (const char *encoded, int options)
4c4b4cd2
PH
1248{
1249 return xstrdup (ada_decode (encoded));
14f9c5c9
AS
1250}
1251
1252/* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
4c4b4cd2
PH
1253 suffixes that encode debugging information or leading _ada_ on
1254 SYM_NAME (see is_name_suffix commentary for the debugging
1255 information that is ignored). If WILD, then NAME need only match a
1256 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1257 either argument is NULL. */
14f9c5c9 1258
2c0b251b 1259static int
d2e4a39e 1260ada_match_name (const char *sym_name, const char *name, int wild)
14f9c5c9
AS
1261{
1262 if (sym_name == NULL || name == NULL)
1263 return 0;
1264 else if (wild)
73589123 1265 return wild_match (sym_name, name) == 0;
d2e4a39e
AS
1266 else
1267 {
1268 int len_name = strlen (name);
5b4ee69b 1269
4c4b4cd2
PH
1270 return (strncmp (sym_name, name, len_name) == 0
1271 && is_name_suffix (sym_name + len_name))
1272 || (strncmp (sym_name, "_ada_", 5) == 0
1273 && strncmp (sym_name + 5, name, len_name) == 0
1274 && is_name_suffix (sym_name + len_name + 5));
d2e4a39e 1275 }
14f9c5c9 1276}
14f9c5c9 1277\f
d2e4a39e 1278
4c4b4cd2 1279 /* Arrays */
14f9c5c9 1280
28c85d6c
JB
1281/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1282 generated by the GNAT compiler to describe the index type used
1283 for each dimension of an array, check whether it follows the latest
1284 known encoding. If not, fix it up to conform to the latest encoding.
1285 Otherwise, do nothing. This function also does nothing if
1286 INDEX_DESC_TYPE is NULL.
1287
1288 The GNAT encoding used to describle the array index type evolved a bit.
1289 Initially, the information would be provided through the name of each
1290 field of the structure type only, while the type of these fields was
1291 described as unspecified and irrelevant. The debugger was then expected
1292 to perform a global type lookup using the name of that field in order
1293 to get access to the full index type description. Because these global
1294 lookups can be very expensive, the encoding was later enhanced to make
1295 the global lookup unnecessary by defining the field type as being
1296 the full index type description.
1297
1298 The purpose of this routine is to allow us to support older versions
1299 of the compiler by detecting the use of the older encoding, and by
1300 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1301 we essentially replace each field's meaningless type by the associated
1302 index subtype). */
1303
1304void
1305ada_fixup_array_indexes_type (struct type *index_desc_type)
1306{
1307 int i;
1308
1309 if (index_desc_type == NULL)
1310 return;
1311 gdb_assert (TYPE_NFIELDS (index_desc_type) > 0);
1312
1313 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1314 to check one field only, no need to check them all). If not, return
1315 now.
1316
1317 If our INDEX_DESC_TYPE was generated using the older encoding,
1318 the field type should be a meaningless integer type whose name
1319 is not equal to the field name. */
1320 if (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)) != NULL
1321 && strcmp (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)),
1322 TYPE_FIELD_NAME (index_desc_type, 0)) == 0)
1323 return;
1324
1325 /* Fixup each field of INDEX_DESC_TYPE. */
1326 for (i = 0; i < TYPE_NFIELDS (index_desc_type); i++)
1327 {
1328 char *name = TYPE_FIELD_NAME (index_desc_type, i);
1329 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1330
1331 if (raw_type)
1332 TYPE_FIELD_TYPE (index_desc_type, i) = raw_type;
1333 }
1334}
1335
4c4b4cd2 1336/* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
14f9c5c9 1337
d2e4a39e
AS
1338static char *bound_name[] = {
1339 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
14f9c5c9
AS
1340 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1341};
1342
1343/* Maximum number of array dimensions we are prepared to handle. */
1344
4c4b4cd2 1345#define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
14f9c5c9 1346
4c4b4cd2 1347/* Like modify_field, but allows bitpos > wordlength. */
14f9c5c9
AS
1348
1349static void
50810684
UW
1350modify_general_field (struct type *type, char *addr,
1351 LONGEST fieldval, int bitpos, int bitsize)
14f9c5c9 1352{
50810684 1353 modify_field (type, addr + bitpos / 8, fieldval, bitpos % 8, bitsize);
14f9c5c9
AS
1354}
1355
1356
4c4b4cd2
PH
1357/* The desc_* routines return primitive portions of array descriptors
1358 (fat pointers). */
14f9c5c9
AS
1359
1360/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1361 level of indirection, if needed. */
1362
d2e4a39e
AS
1363static struct type *
1364desc_base_type (struct type *type)
14f9c5c9
AS
1365{
1366 if (type == NULL)
1367 return NULL;
61ee279c 1368 type = ada_check_typedef (type);
1265e4aa
JB
1369 if (type != NULL
1370 && (TYPE_CODE (type) == TYPE_CODE_PTR
1371 || TYPE_CODE (type) == TYPE_CODE_REF))
61ee279c 1372 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1373 else
1374 return type;
1375}
1376
4c4b4cd2
PH
1377/* True iff TYPE indicates a "thin" array pointer type. */
1378
14f9c5c9 1379static int
d2e4a39e 1380is_thin_pntr (struct type *type)
14f9c5c9 1381{
d2e4a39e 1382 return
14f9c5c9
AS
1383 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1384 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1385}
1386
4c4b4cd2
PH
1387/* The descriptor type for thin pointer type TYPE. */
1388
d2e4a39e
AS
1389static struct type *
1390thin_descriptor_type (struct type *type)
14f9c5c9 1391{
d2e4a39e 1392 struct type *base_type = desc_base_type (type);
5b4ee69b 1393
14f9c5c9
AS
1394 if (base_type == NULL)
1395 return NULL;
1396 if (is_suffix (ada_type_name (base_type), "___XVE"))
1397 return base_type;
d2e4a39e 1398 else
14f9c5c9 1399 {
d2e4a39e 1400 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1401
14f9c5c9 1402 if (alt_type == NULL)
4c4b4cd2 1403 return base_type;
14f9c5c9 1404 else
4c4b4cd2 1405 return alt_type;
14f9c5c9
AS
1406 }
1407}
1408
4c4b4cd2
PH
1409/* A pointer to the array data for thin-pointer value VAL. */
1410
d2e4a39e
AS
1411static struct value *
1412thin_data_pntr (struct value *val)
14f9c5c9 1413{
df407dfe 1414 struct type *type = value_type (val);
556bdfd4 1415 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1416
556bdfd4
UW
1417 data_type = lookup_pointer_type (data_type);
1418
14f9c5c9 1419 if (TYPE_CODE (type) == TYPE_CODE_PTR)
556bdfd4 1420 return value_cast (data_type, value_copy (val));
d2e4a39e 1421 else
42ae5230 1422 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1423}
1424
4c4b4cd2
PH
1425/* True iff TYPE indicates a "thick" array pointer type. */
1426
14f9c5c9 1427static int
d2e4a39e 1428is_thick_pntr (struct type *type)
14f9c5c9
AS
1429{
1430 type = desc_base_type (type);
1431 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2 1432 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1433}
1434
4c4b4cd2
PH
1435/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1436 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1437
d2e4a39e
AS
1438static struct type *
1439desc_bounds_type (struct type *type)
14f9c5c9 1440{
d2e4a39e 1441 struct type *r;
14f9c5c9
AS
1442
1443 type = desc_base_type (type);
1444
1445 if (type == NULL)
1446 return NULL;
1447 else if (is_thin_pntr (type))
1448 {
1449 type = thin_descriptor_type (type);
1450 if (type == NULL)
4c4b4cd2 1451 return NULL;
14f9c5c9
AS
1452 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1453 if (r != NULL)
61ee279c 1454 return ada_check_typedef (r);
14f9c5c9
AS
1455 }
1456 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1457 {
1458 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1459 if (r != NULL)
61ee279c 1460 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1461 }
1462 return NULL;
1463}
1464
1465/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1466 one, a pointer to its bounds data. Otherwise NULL. */
1467
d2e4a39e
AS
1468static struct value *
1469desc_bounds (struct value *arr)
14f9c5c9 1470{
df407dfe 1471 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1472
d2e4a39e 1473 if (is_thin_pntr (type))
14f9c5c9 1474 {
d2e4a39e 1475 struct type *bounds_type =
4c4b4cd2 1476 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1477 LONGEST addr;
1478
4cdfadb1 1479 if (bounds_type == NULL)
323e0a4a 1480 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1481
1482 /* NOTE: The following calculation is not really kosher, but
d2e4a39e 1483 since desc_type is an XVE-encoded type (and shouldn't be),
4c4b4cd2 1484 the correct calculation is a real pain. FIXME (and fix GCC). */
14f9c5c9 1485 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4c4b4cd2 1486 addr = value_as_long (arr);
d2e4a39e 1487 else
42ae5230 1488 addr = value_address (arr);
14f9c5c9 1489
d2e4a39e 1490 return
4c4b4cd2
PH
1491 value_from_longest (lookup_pointer_type (bounds_type),
1492 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1493 }
1494
1495 else if (is_thick_pntr (type))
05e522ef
JB
1496 {
1497 struct value *p_bounds = value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
1498 _("Bad GNAT array descriptor"));
1499 struct type *p_bounds_type = value_type (p_bounds);
1500
1501 if (p_bounds_type
1502 && TYPE_CODE (p_bounds_type) == TYPE_CODE_PTR)
1503 {
1504 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1505
1506 if (TYPE_STUB (target_type))
1507 p_bounds = value_cast (lookup_pointer_type
1508 (ada_check_typedef (target_type)),
1509 p_bounds);
1510 }
1511 else
1512 error (_("Bad GNAT array descriptor"));
1513
1514 return p_bounds;
1515 }
14f9c5c9
AS
1516 else
1517 return NULL;
1518}
1519
4c4b4cd2
PH
1520/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1521 position of the field containing the address of the bounds data. */
1522
14f9c5c9 1523static int
d2e4a39e 1524fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1525{
1526 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1527}
1528
1529/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1530 size of the field containing the address of the bounds data. */
1531
14f9c5c9 1532static int
d2e4a39e 1533fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1534{
1535 type = desc_base_type (type);
1536
d2e4a39e 1537 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1538 return TYPE_FIELD_BITSIZE (type, 1);
1539 else
61ee279c 1540 return 8 * TYPE_LENGTH (ada_check_typedef (TYPE_FIELD_TYPE (type, 1)));
14f9c5c9
AS
1541}
1542
4c4b4cd2 1543/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1544 pointer to one, the type of its array data (a array-with-no-bounds type);
1545 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1546 data. */
4c4b4cd2 1547
d2e4a39e 1548static struct type *
556bdfd4 1549desc_data_target_type (struct type *type)
14f9c5c9
AS
1550{
1551 type = desc_base_type (type);
1552
4c4b4cd2 1553 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1554 if (is_thin_pntr (type))
556bdfd4 1555 return desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1));
14f9c5c9 1556 else if (is_thick_pntr (type))
556bdfd4
UW
1557 {
1558 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1559
1560 if (data_type
1561 && TYPE_CODE (ada_check_typedef (data_type)) == TYPE_CODE_PTR)
05e522ef 1562 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1563 }
1564
1565 return NULL;
14f9c5c9
AS
1566}
1567
1568/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1569 its array data. */
4c4b4cd2 1570
d2e4a39e
AS
1571static struct value *
1572desc_data (struct value *arr)
14f9c5c9 1573{
df407dfe 1574 struct type *type = value_type (arr);
5b4ee69b 1575
14f9c5c9
AS
1576 if (is_thin_pntr (type))
1577 return thin_data_pntr (arr);
1578 else if (is_thick_pntr (type))
d2e4a39e 1579 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
323e0a4a 1580 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1581 else
1582 return NULL;
1583}
1584
1585
1586/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1587 position of the field containing the address of the data. */
1588
14f9c5c9 1589static int
d2e4a39e 1590fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1591{
1592 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1593}
1594
1595/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1596 size of the field containing the address of the data. */
1597
14f9c5c9 1598static int
d2e4a39e 1599fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1600{
1601 type = desc_base_type (type);
1602
1603 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1604 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1605 else
14f9c5c9
AS
1606 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1607}
1608
4c4b4cd2 1609/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1610 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1611 bound, if WHICH is 1. The first bound is I=1. */
1612
d2e4a39e
AS
1613static struct value *
1614desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1615{
d2e4a39e 1616 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
323e0a4a 1617 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1618}
1619
1620/* If BOUNDS is an array-bounds structure type, return the bit position
1621 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1622 bound, if WHICH is 1. The first bound is I=1. */
1623
14f9c5c9 1624static int
d2e4a39e 1625desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1626{
d2e4a39e 1627 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1628}
1629
1630/* If BOUNDS is an array-bounds structure type, return the bit field size
1631 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1632 bound, if WHICH is 1. The first bound is I=1. */
1633
76a01679 1634static int
d2e4a39e 1635desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1636{
1637 type = desc_base_type (type);
1638
d2e4a39e
AS
1639 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1640 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1641 else
1642 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
14f9c5c9
AS
1643}
1644
1645/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1646 Ith bound (numbering from 1). Otherwise, NULL. */
1647
d2e4a39e
AS
1648static struct type *
1649desc_index_type (struct type *type, int i)
14f9c5c9
AS
1650{
1651 type = desc_base_type (type);
1652
1653 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
d2e4a39e
AS
1654 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1655 else
14f9c5c9
AS
1656 return NULL;
1657}
1658
4c4b4cd2
PH
1659/* The number of index positions in the array-bounds type TYPE.
1660 Return 0 if TYPE is NULL. */
1661
14f9c5c9 1662static int
d2e4a39e 1663desc_arity (struct type *type)
14f9c5c9
AS
1664{
1665 type = desc_base_type (type);
1666
1667 if (type != NULL)
1668 return TYPE_NFIELDS (type) / 2;
1669 return 0;
1670}
1671
4c4b4cd2
PH
1672/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1673 an array descriptor type (representing an unconstrained array
1674 type). */
1675
76a01679
JB
1676static int
1677ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1678{
1679 if (type == NULL)
1680 return 0;
61ee279c 1681 type = ada_check_typedef (type);
4c4b4cd2 1682 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
76a01679 1683 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1684}
1685
52ce6436
PH
1686/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
1687 * to one. */
1688
2c0b251b 1689static int
52ce6436
PH
1690ada_is_array_type (struct type *type)
1691{
1692 while (type != NULL
1693 && (TYPE_CODE (type) == TYPE_CODE_PTR
1694 || TYPE_CODE (type) == TYPE_CODE_REF))
1695 type = TYPE_TARGET_TYPE (type);
1696 return ada_is_direct_array_type (type);
1697}
1698
4c4b4cd2 1699/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1700
14f9c5c9 1701int
4c4b4cd2 1702ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1703{
1704 if (type == NULL)
1705 return 0;
61ee279c 1706 type = ada_check_typedef (type);
14f9c5c9 1707 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
4c4b4cd2
PH
1708 || (TYPE_CODE (type) == TYPE_CODE_PTR
1709 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
14f9c5c9
AS
1710}
1711
4c4b4cd2
PH
1712/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1713
14f9c5c9 1714int
4c4b4cd2 1715ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1716{
556bdfd4 1717 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
1718
1719 if (type == NULL)
1720 return 0;
61ee279c 1721 type = ada_check_typedef (type);
556bdfd4
UW
1722 return (data_type != NULL
1723 && TYPE_CODE (data_type) == TYPE_CODE_ARRAY
1724 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
1725}
1726
1727/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1728 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1729 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1730 is still needed. */
1731
14f9c5c9 1732int
ebf56fd3 1733ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1734{
d2e4a39e 1735 return
14f9c5c9
AS
1736 type != NULL
1737 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1738 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
4c4b4cd2
PH
1739 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1740 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1741}
1742
1743
4c4b4cd2 1744/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1745 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1746 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1747 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1748 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1749 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1750 a descriptor. */
d2e4a39e
AS
1751struct type *
1752ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 1753{
ad82864c
JB
1754 if (ada_is_constrained_packed_array_type (value_type (arr)))
1755 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 1756
df407dfe
AC
1757 if (!ada_is_array_descriptor_type (value_type (arr)))
1758 return value_type (arr);
d2e4a39e
AS
1759
1760 if (!bounds)
ad82864c
JB
1761 {
1762 struct type *array_type =
1763 ada_check_typedef (desc_data_target_type (value_type (arr)));
1764
1765 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1766 TYPE_FIELD_BITSIZE (array_type, 0) =
1767 decode_packed_array_bitsize (value_type (arr));
1768
1769 return array_type;
1770 }
14f9c5c9
AS
1771 else
1772 {
d2e4a39e 1773 struct type *elt_type;
14f9c5c9 1774 int arity;
d2e4a39e 1775 struct value *descriptor;
14f9c5c9 1776
df407dfe
AC
1777 elt_type = ada_array_element_type (value_type (arr), -1);
1778 arity = ada_array_arity (value_type (arr));
14f9c5c9 1779
d2e4a39e 1780 if (elt_type == NULL || arity == 0)
df407dfe 1781 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
1782
1783 descriptor = desc_bounds (arr);
d2e4a39e 1784 if (value_as_long (descriptor) == 0)
4c4b4cd2 1785 return NULL;
d2e4a39e 1786 while (arity > 0)
4c4b4cd2 1787 {
e9bb382b
UW
1788 struct type *range_type = alloc_type_copy (value_type (arr));
1789 struct type *array_type = alloc_type_copy (value_type (arr));
4c4b4cd2
PH
1790 struct value *low = desc_one_bound (descriptor, arity, 0);
1791 struct value *high = desc_one_bound (descriptor, arity, 1);
4c4b4cd2 1792
5b4ee69b 1793 arity -= 1;
df407dfe 1794 create_range_type (range_type, value_type (low),
529cad9c
PH
1795 longest_to_int (value_as_long (low)),
1796 longest_to_int (value_as_long (high)));
4c4b4cd2 1797 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
1798
1799 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1800 TYPE_FIELD_BITSIZE (elt_type, 0) =
1801 decode_packed_array_bitsize (value_type (arr));
4c4b4cd2 1802 }
14f9c5c9
AS
1803
1804 return lookup_pointer_type (elt_type);
1805 }
1806}
1807
1808/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
1809 Otherwise, returns either a standard GDB array with bounds set
1810 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1811 GDB array. Returns NULL if ARR is a null fat pointer. */
1812
d2e4a39e
AS
1813struct value *
1814ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 1815{
df407dfe 1816 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1817 {
d2e4a39e 1818 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 1819
14f9c5c9 1820 if (arrType == NULL)
4c4b4cd2 1821 return NULL;
14f9c5c9
AS
1822 return value_cast (arrType, value_copy (desc_data (arr)));
1823 }
ad82864c
JB
1824 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1825 return decode_constrained_packed_array (arr);
14f9c5c9
AS
1826 else
1827 return arr;
1828}
1829
1830/* If ARR does not represent an array, returns ARR unchanged.
1831 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
1832 be ARR itself if it already is in the proper form). */
1833
1834static struct value *
d2e4a39e 1835ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 1836{
df407dfe 1837 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1838 {
d2e4a39e 1839 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 1840
14f9c5c9 1841 if (arrVal == NULL)
323e0a4a 1842 error (_("Bounds unavailable for null array pointer."));
529cad9c 1843 check_size (TYPE_TARGET_TYPE (value_type (arrVal)));
14f9c5c9
AS
1844 return value_ind (arrVal);
1845 }
ad82864c
JB
1846 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1847 return decode_constrained_packed_array (arr);
d2e4a39e 1848 else
14f9c5c9
AS
1849 return arr;
1850}
1851
1852/* If TYPE represents a GNAT array type, return it translated to an
1853 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
1854 packing). For other types, is the identity. */
1855
d2e4a39e
AS
1856struct type *
1857ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1858{
ad82864c
JB
1859 if (ada_is_constrained_packed_array_type (type))
1860 return decode_constrained_packed_array_type (type);
17280b9f
UW
1861
1862 if (ada_is_array_descriptor_type (type))
556bdfd4 1863 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
1864
1865 return type;
14f9c5c9
AS
1866}
1867
4c4b4cd2
PH
1868/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1869
ad82864c
JB
1870static int
1871ada_is_packed_array_type (struct type *type)
14f9c5c9
AS
1872{
1873 if (type == NULL)
1874 return 0;
4c4b4cd2 1875 type = desc_base_type (type);
61ee279c 1876 type = ada_check_typedef (type);
d2e4a39e 1877 return
14f9c5c9
AS
1878 ada_type_name (type) != NULL
1879 && strstr (ada_type_name (type), "___XP") != NULL;
1880}
1881
ad82864c
JB
1882/* Non-zero iff TYPE represents a standard GNAT constrained
1883 packed-array type. */
1884
1885int
1886ada_is_constrained_packed_array_type (struct type *type)
1887{
1888 return ada_is_packed_array_type (type)
1889 && !ada_is_array_descriptor_type (type);
1890}
1891
1892/* Non-zero iff TYPE represents an array descriptor for a
1893 unconstrained packed-array type. */
1894
1895static int
1896ada_is_unconstrained_packed_array_type (struct type *type)
1897{
1898 return ada_is_packed_array_type (type)
1899 && ada_is_array_descriptor_type (type);
1900}
1901
1902/* Given that TYPE encodes a packed array type (constrained or unconstrained),
1903 return the size of its elements in bits. */
1904
1905static long
1906decode_packed_array_bitsize (struct type *type)
1907{
1908 char *raw_name = ada_type_name (ada_check_typedef (type));
1909 char *tail;
1910 long bits;
1911
1912 if (!raw_name)
1913 raw_name = ada_type_name (desc_base_type (type));
1914
1915 if (!raw_name)
1916 return 0;
1917
1918 tail = strstr (raw_name, "___XP");
1919
1920 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1921 {
1922 lim_warning
1923 (_("could not understand bit size information on packed array"));
1924 return 0;
1925 }
1926
1927 return bits;
1928}
1929
14f9c5c9
AS
1930/* Given that TYPE is a standard GDB array type with all bounds filled
1931 in, and that the element size of its ultimate scalar constituents
1932 (that is, either its elements, or, if it is an array of arrays, its
1933 elements' elements, etc.) is *ELT_BITS, return an identical type,
1934 but with the bit sizes of its elements (and those of any
1935 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2
PH
1936 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1937 in bits. */
1938
d2e4a39e 1939static struct type *
ad82864c 1940constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 1941{
d2e4a39e
AS
1942 struct type *new_elt_type;
1943 struct type *new_type;
14f9c5c9
AS
1944 LONGEST low_bound, high_bound;
1945
61ee279c 1946 type = ada_check_typedef (type);
14f9c5c9
AS
1947 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1948 return type;
1949
e9bb382b 1950 new_type = alloc_type_copy (type);
ad82864c
JB
1951 new_elt_type =
1952 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
1953 elt_bits);
262452ec 1954 create_array_type (new_type, new_elt_type, TYPE_INDEX_TYPE (type));
14f9c5c9
AS
1955 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1956 TYPE_NAME (new_type) = ada_type_name (type);
1957
262452ec 1958 if (get_discrete_bounds (TYPE_INDEX_TYPE (type),
4c4b4cd2 1959 &low_bound, &high_bound) < 0)
14f9c5c9
AS
1960 low_bound = high_bound = 0;
1961 if (high_bound < low_bound)
1962 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 1963 else
14f9c5c9
AS
1964 {
1965 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 1966 TYPE_LENGTH (new_type) =
4c4b4cd2 1967 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
1968 }
1969
876cecd0 1970 TYPE_FIXED_INSTANCE (new_type) = 1;
14f9c5c9
AS
1971 return new_type;
1972}
1973
ad82864c
JB
1974/* The array type encoded by TYPE, where
1975 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 1976
d2e4a39e 1977static struct type *
ad82864c 1978decode_constrained_packed_array_type (struct type *type)
d2e4a39e 1979{
727e3d2e
JB
1980 char *raw_name = ada_type_name (ada_check_typedef (type));
1981 char *name;
1982 char *tail;
d2e4a39e 1983 struct type *shadow_type;
14f9c5c9 1984 long bits;
14f9c5c9 1985
727e3d2e
JB
1986 if (!raw_name)
1987 raw_name = ada_type_name (desc_base_type (type));
1988
1989 if (!raw_name)
1990 return NULL;
1991
1992 name = (char *) alloca (strlen (raw_name) + 1);
1993 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
1994 type = desc_base_type (type);
1995
14f9c5c9
AS
1996 memcpy (name, raw_name, tail - raw_name);
1997 name[tail - raw_name] = '\000';
1998
b4ba55a1
JB
1999 shadow_type = ada_find_parallel_type_with_name (type, name);
2000
2001 if (shadow_type == NULL)
14f9c5c9 2002 {
323e0a4a 2003 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2004 return NULL;
2005 }
cb249c71 2006 CHECK_TYPEDEF (shadow_type);
14f9c5c9
AS
2007
2008 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
2009 {
323e0a4a 2010 lim_warning (_("could not understand bounds information on packed array"));
14f9c5c9
AS
2011 return NULL;
2012 }
d2e4a39e 2013
ad82864c
JB
2014 bits = decode_packed_array_bitsize (type);
2015 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2016}
2017
ad82864c
JB
2018/* Given that ARR is a struct value *indicating a GNAT constrained packed
2019 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2020 standard GDB array type except that the BITSIZEs of the array
2021 target types are set to the number of bits in each element, and the
4c4b4cd2 2022 type length is set appropriately. */
14f9c5c9 2023
d2e4a39e 2024static struct value *
ad82864c 2025decode_constrained_packed_array (struct value *arr)
14f9c5c9 2026{
4c4b4cd2 2027 struct type *type;
14f9c5c9 2028
4c4b4cd2 2029 arr = ada_coerce_ref (arr);
284614f0
JB
2030
2031 /* If our value is a pointer, then dererence it. Make sure that
2032 this operation does not cause the target type to be fixed, as
2033 this would indirectly cause this array to be decoded. The rest
2034 of the routine assumes that the array hasn't been decoded yet,
2035 so we use the basic "value_ind" routine to perform the dereferencing,
2036 as opposed to using "ada_value_ind". */
df407dfe 2037 if (TYPE_CODE (value_type (arr)) == TYPE_CODE_PTR)
284614f0 2038 arr = value_ind (arr);
4c4b4cd2 2039
ad82864c 2040 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2041 if (type == NULL)
2042 {
323e0a4a 2043 error (_("can't unpack array"));
14f9c5c9
AS
2044 return NULL;
2045 }
61ee279c 2046
50810684 2047 if (gdbarch_bits_big_endian (get_type_arch (value_type (arr)))
32c9a795 2048 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2049 {
2050 /* This is a (right-justified) modular type representing a packed
2051 array with no wrapper. In order to interpret the value through
2052 the (left-justified) packed array type we just built, we must
2053 first left-justify it. */
2054 int bit_size, bit_pos;
2055 ULONGEST mod;
2056
df407dfe 2057 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2058 bit_size = 0;
2059 while (mod > 0)
2060 {
2061 bit_size += 1;
2062 mod >>= 1;
2063 }
df407dfe 2064 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2065 arr = ada_value_primitive_packed_val (arr, NULL,
2066 bit_pos / HOST_CHAR_BIT,
2067 bit_pos % HOST_CHAR_BIT,
2068 bit_size,
2069 type);
2070 }
2071
4c4b4cd2 2072 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2073}
2074
2075
2076/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2077 given in IND. ARR must be a simple array. */
14f9c5c9 2078
d2e4a39e
AS
2079static struct value *
2080value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2081{
2082 int i;
2083 int bits, elt_off, bit_off;
2084 long elt_total_bit_offset;
d2e4a39e
AS
2085 struct type *elt_type;
2086 struct value *v;
14f9c5c9
AS
2087
2088 bits = 0;
2089 elt_total_bit_offset = 0;
df407dfe 2090 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2091 for (i = 0; i < arity; i += 1)
14f9c5c9 2092 {
d2e4a39e 2093 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
4c4b4cd2
PH
2094 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2095 error
323e0a4a 2096 (_("attempt to do packed indexing of something other than a packed array"));
14f9c5c9 2097 else
4c4b4cd2
PH
2098 {
2099 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
2100 LONGEST lowerbound, upperbound;
2101 LONGEST idx;
2102
2103 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2104 {
323e0a4a 2105 lim_warning (_("don't know bounds of array"));
4c4b4cd2
PH
2106 lowerbound = upperbound = 0;
2107 }
2108
3cb382c9 2109 idx = pos_atr (ind[i]);
4c4b4cd2 2110 if (idx < lowerbound || idx > upperbound)
323e0a4a 2111 lim_warning (_("packed array index %ld out of bounds"), (long) idx);
4c4b4cd2
PH
2112 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2113 elt_total_bit_offset += (idx - lowerbound) * bits;
61ee279c 2114 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
4c4b4cd2 2115 }
14f9c5c9
AS
2116 }
2117 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2118 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2119
2120 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
4c4b4cd2 2121 bits, elt_type);
14f9c5c9
AS
2122 return v;
2123}
2124
4c4b4cd2 2125/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2126
2127static int
d2e4a39e 2128has_negatives (struct type *type)
14f9c5c9 2129{
d2e4a39e
AS
2130 switch (TYPE_CODE (type))
2131 {
2132 default:
2133 return 0;
2134 case TYPE_CODE_INT:
2135 return !TYPE_UNSIGNED (type);
2136 case TYPE_CODE_RANGE:
2137 return TYPE_LOW_BOUND (type) < 0;
2138 }
14f9c5c9 2139}
d2e4a39e 2140
14f9c5c9
AS
2141
2142/* Create a new value of type TYPE from the contents of OBJ starting
2143 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2144 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
4c4b4cd2
PH
2145 assigning through the result will set the field fetched from.
2146 VALADDR is ignored unless OBJ is NULL, in which case,
2147 VALADDR+OFFSET must address the start of storage containing the
2148 packed value. The value returned in this case is never an lval.
2149 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
14f9c5c9 2150
d2e4a39e 2151struct value *
fc1a4b47 2152ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
a2bd3dcd 2153 long offset, int bit_offset, int bit_size,
4c4b4cd2 2154 struct type *type)
14f9c5c9 2155{
d2e4a39e 2156 struct value *v;
4c4b4cd2
PH
2157 int src, /* Index into the source area */
2158 targ, /* Index into the target area */
2159 srcBitsLeft, /* Number of source bits left to move */
2160 nsrc, ntarg, /* Number of source and target bytes */
2161 unusedLS, /* Number of bits in next significant
2162 byte of source that are unused */
2163 accumSize; /* Number of meaningful bits in accum */
2164 unsigned char *bytes; /* First byte containing data to unpack */
d2e4a39e 2165 unsigned char *unpacked;
4c4b4cd2 2166 unsigned long accum; /* Staging area for bits being transferred */
14f9c5c9
AS
2167 unsigned char sign;
2168 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
4c4b4cd2
PH
2169 /* Transmit bytes from least to most significant; delta is the direction
2170 the indices move. */
50810684 2171 int delta = gdbarch_bits_big_endian (get_type_arch (type)) ? -1 : 1;
14f9c5c9 2172
61ee279c 2173 type = ada_check_typedef (type);
14f9c5c9
AS
2174
2175 if (obj == NULL)
2176 {
2177 v = allocate_value (type);
d2e4a39e 2178 bytes = (unsigned char *) (valaddr + offset);
14f9c5c9 2179 }
9214ee5f 2180 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
14f9c5c9
AS
2181 {
2182 v = value_at (type,
42ae5230 2183 value_address (obj) + offset);
d2e4a39e 2184 bytes = (unsigned char *) alloca (len);
42ae5230 2185 read_memory (value_address (v), bytes, len);
14f9c5c9 2186 }
d2e4a39e 2187 else
14f9c5c9
AS
2188 {
2189 v = allocate_value (type);
0fd88904 2190 bytes = (unsigned char *) value_contents (obj) + offset;
14f9c5c9 2191 }
d2e4a39e
AS
2192
2193 if (obj != NULL)
14f9c5c9 2194 {
42ae5230 2195 CORE_ADDR new_addr;
5b4ee69b 2196
74bcbdf3 2197 set_value_component_location (v, obj);
42ae5230 2198 new_addr = value_address (obj) + offset;
9bbda503
AC
2199 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2200 set_value_bitsize (v, bit_size);
df407dfe 2201 if (value_bitpos (v) >= HOST_CHAR_BIT)
4c4b4cd2 2202 {
42ae5230 2203 ++new_addr;
9bbda503 2204 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
4c4b4cd2 2205 }
42ae5230 2206 set_value_address (v, new_addr);
14f9c5c9
AS
2207 }
2208 else
9bbda503 2209 set_value_bitsize (v, bit_size);
0fd88904 2210 unpacked = (unsigned char *) value_contents (v);
14f9c5c9
AS
2211
2212 srcBitsLeft = bit_size;
2213 nsrc = len;
2214 ntarg = TYPE_LENGTH (type);
2215 sign = 0;
2216 if (bit_size == 0)
2217 {
2218 memset (unpacked, 0, TYPE_LENGTH (type));
2219 return v;
2220 }
50810684 2221 else if (gdbarch_bits_big_endian (get_type_arch (type)))
14f9c5c9 2222 {
d2e4a39e 2223 src = len - 1;
1265e4aa
JB
2224 if (has_negatives (type)
2225 && ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
4c4b4cd2 2226 sign = ~0;
d2e4a39e
AS
2227
2228 unusedLS =
4c4b4cd2
PH
2229 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2230 % HOST_CHAR_BIT;
14f9c5c9
AS
2231
2232 switch (TYPE_CODE (type))
4c4b4cd2
PH
2233 {
2234 case TYPE_CODE_ARRAY:
2235 case TYPE_CODE_UNION:
2236 case TYPE_CODE_STRUCT:
2237 /* Non-scalar values must be aligned at a byte boundary... */
2238 accumSize =
2239 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2240 /* ... And are placed at the beginning (most-significant) bytes
2241 of the target. */
529cad9c 2242 targ = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
0056e4d5 2243 ntarg = targ + 1;
4c4b4cd2
PH
2244 break;
2245 default:
2246 accumSize = 0;
2247 targ = TYPE_LENGTH (type) - 1;
2248 break;
2249 }
14f9c5c9 2250 }
d2e4a39e 2251 else
14f9c5c9
AS
2252 {
2253 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2254
2255 src = targ = 0;
2256 unusedLS = bit_offset;
2257 accumSize = 0;
2258
d2e4a39e 2259 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
4c4b4cd2 2260 sign = ~0;
14f9c5c9 2261 }
d2e4a39e 2262
14f9c5c9
AS
2263 accum = 0;
2264 while (nsrc > 0)
2265 {
2266 /* Mask for removing bits of the next source byte that are not
4c4b4cd2 2267 part of the value. */
d2e4a39e 2268 unsigned int unusedMSMask =
4c4b4cd2
PH
2269 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2270 1;
2271 /* Sign-extend bits for this byte. */
14f9c5c9 2272 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2273
d2e4a39e 2274 accum |=
4c4b4cd2 2275 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2276 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2277 if (accumSize >= HOST_CHAR_BIT)
4c4b4cd2
PH
2278 {
2279 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2280 accumSize -= HOST_CHAR_BIT;
2281 accum >>= HOST_CHAR_BIT;
2282 ntarg -= 1;
2283 targ += delta;
2284 }
14f9c5c9
AS
2285 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2286 unusedLS = 0;
2287 nsrc -= 1;
2288 src += delta;
2289 }
2290 while (ntarg > 0)
2291 {
2292 accum |= sign << accumSize;
2293 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2294 accumSize -= HOST_CHAR_BIT;
2295 accum >>= HOST_CHAR_BIT;
2296 ntarg -= 1;
2297 targ += delta;
2298 }
2299
2300 return v;
2301}
d2e4a39e 2302
14f9c5c9
AS
2303/* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
2304 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
4c4b4cd2 2305 not overlap. */
14f9c5c9 2306static void
fc1a4b47 2307move_bits (gdb_byte *target, int targ_offset, const gdb_byte *source,
50810684 2308 int src_offset, int n, int bits_big_endian_p)
14f9c5c9
AS
2309{
2310 unsigned int accum, mask;
2311 int accum_bits, chunk_size;
2312
2313 target += targ_offset / HOST_CHAR_BIT;
2314 targ_offset %= HOST_CHAR_BIT;
2315 source += src_offset / HOST_CHAR_BIT;
2316 src_offset %= HOST_CHAR_BIT;
50810684 2317 if (bits_big_endian_p)
14f9c5c9
AS
2318 {
2319 accum = (unsigned char) *source;
2320 source += 1;
2321 accum_bits = HOST_CHAR_BIT - src_offset;
2322
d2e4a39e 2323 while (n > 0)
4c4b4cd2
PH
2324 {
2325 int unused_right;
5b4ee69b 2326
4c4b4cd2
PH
2327 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
2328 accum_bits += HOST_CHAR_BIT;
2329 source += 1;
2330 chunk_size = HOST_CHAR_BIT - targ_offset;
2331 if (chunk_size > n)
2332 chunk_size = n;
2333 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
2334 mask = ((1 << chunk_size) - 1) << unused_right;
2335 *target =
2336 (*target & ~mask)
2337 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
2338 n -= chunk_size;
2339 accum_bits -= chunk_size;
2340 target += 1;
2341 targ_offset = 0;
2342 }
14f9c5c9
AS
2343 }
2344 else
2345 {
2346 accum = (unsigned char) *source >> src_offset;
2347 source += 1;
2348 accum_bits = HOST_CHAR_BIT - src_offset;
2349
d2e4a39e 2350 while (n > 0)
4c4b4cd2
PH
2351 {
2352 accum = accum + ((unsigned char) *source << accum_bits);
2353 accum_bits += HOST_CHAR_BIT;
2354 source += 1;
2355 chunk_size = HOST_CHAR_BIT - targ_offset;
2356 if (chunk_size > n)
2357 chunk_size = n;
2358 mask = ((1 << chunk_size) - 1) << targ_offset;
2359 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2360 n -= chunk_size;
2361 accum_bits -= chunk_size;
2362 accum >>= chunk_size;
2363 target += 1;
2364 targ_offset = 0;
2365 }
14f9c5c9
AS
2366 }
2367}
2368
14f9c5c9
AS
2369/* Store the contents of FROMVAL into the location of TOVAL.
2370 Return a new value with the location of TOVAL and contents of
2371 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2372 floating-point or non-scalar types. */
14f9c5c9 2373
d2e4a39e
AS
2374static struct value *
2375ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2376{
df407dfe
AC
2377 struct type *type = value_type (toval);
2378 int bits = value_bitsize (toval);
14f9c5c9 2379
52ce6436
PH
2380 toval = ada_coerce_ref (toval);
2381 fromval = ada_coerce_ref (fromval);
2382
2383 if (ada_is_direct_array_type (value_type (toval)))
2384 toval = ada_coerce_to_simple_array (toval);
2385 if (ada_is_direct_array_type (value_type (fromval)))
2386 fromval = ada_coerce_to_simple_array (fromval);
2387
88e3b34b 2388 if (!deprecated_value_modifiable (toval))
323e0a4a 2389 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2390
d2e4a39e 2391 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2392 && bits > 0
d2e4a39e 2393 && (TYPE_CODE (type) == TYPE_CODE_FLT
4c4b4cd2 2394 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
14f9c5c9 2395 {
df407dfe
AC
2396 int len = (value_bitpos (toval)
2397 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2398 int from_size;
d2e4a39e
AS
2399 char *buffer = (char *) alloca (len);
2400 struct value *val;
42ae5230 2401 CORE_ADDR to_addr = value_address (toval);
14f9c5c9
AS
2402
2403 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4c4b4cd2 2404 fromval = value_cast (type, fromval);
14f9c5c9 2405
52ce6436 2406 read_memory (to_addr, buffer, len);
aced2898
PH
2407 from_size = value_bitsize (fromval);
2408 if (from_size == 0)
2409 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
50810684 2410 if (gdbarch_bits_big_endian (get_type_arch (type)))
df407dfe 2411 move_bits (buffer, value_bitpos (toval),
50810684 2412 value_contents (fromval), from_size - bits, bits, 1);
14f9c5c9 2413 else
50810684
UW
2414 move_bits (buffer, value_bitpos (toval),
2415 value_contents (fromval), 0, bits, 0);
52ce6436 2416 write_memory (to_addr, buffer, len);
8cebebb9
PP
2417 observer_notify_memory_changed (to_addr, len, buffer);
2418
14f9c5c9 2419 val = value_copy (toval);
0fd88904 2420 memcpy (value_contents_raw (val), value_contents (fromval),
4c4b4cd2 2421 TYPE_LENGTH (type));
04624583 2422 deprecated_set_value_type (val, type);
d2e4a39e 2423
14f9c5c9
AS
2424 return val;
2425 }
2426
2427 return value_assign (toval, fromval);
2428}
2429
2430
52ce6436
PH
2431/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2432 * CONTAINER, assign the contents of VAL to COMPONENTS's place in
2433 * CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2434 * COMPONENT, and not the inferior's memory. The current contents
2435 * of COMPONENT are ignored. */
2436static void
2437value_assign_to_component (struct value *container, struct value *component,
2438 struct value *val)
2439{
2440 LONGEST offset_in_container =
42ae5230 2441 (LONGEST) (value_address (component) - value_address (container));
52ce6436
PH
2442 int bit_offset_in_container =
2443 value_bitpos (component) - value_bitpos (container);
2444 int bits;
2445
2446 val = value_cast (value_type (component), val);
2447
2448 if (value_bitsize (component) == 0)
2449 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2450 else
2451 bits = value_bitsize (component);
2452
50810684 2453 if (gdbarch_bits_big_endian (get_type_arch (value_type (container))))
52ce6436
PH
2454 move_bits (value_contents_writeable (container) + offset_in_container,
2455 value_bitpos (container) + bit_offset_in_container,
2456 value_contents (val),
2457 TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits,
50810684 2458 bits, 1);
52ce6436
PH
2459 else
2460 move_bits (value_contents_writeable (container) + offset_in_container,
2461 value_bitpos (container) + bit_offset_in_container,
50810684 2462 value_contents (val), 0, bits, 0);
52ce6436
PH
2463}
2464
4c4b4cd2
PH
2465/* The value of the element of array ARR at the ARITY indices given in IND.
2466 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2467 thereto. */
2468
d2e4a39e
AS
2469struct value *
2470ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2471{
2472 int k;
d2e4a39e
AS
2473 struct value *elt;
2474 struct type *elt_type;
14f9c5c9
AS
2475
2476 elt = ada_coerce_to_simple_array (arr);
2477
df407dfe 2478 elt_type = ada_check_typedef (value_type (elt));
d2e4a39e 2479 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
14f9c5c9
AS
2480 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2481 return value_subscript_packed (elt, arity, ind);
2482
2483 for (k = 0; k < arity; k += 1)
2484 {
2485 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
323e0a4a 2486 error (_("too many subscripts (%d expected)"), k);
2497b498 2487 elt = value_subscript (elt, pos_atr (ind[k]));
14f9c5c9
AS
2488 }
2489 return elt;
2490}
2491
2492/* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
2493 value of the element of *ARR at the ARITY indices given in
4c4b4cd2 2494 IND. Does not read the entire array into memory. */
14f9c5c9 2495
2c0b251b 2496static struct value *
d2e4a39e 2497ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
4c4b4cd2 2498 struct value **ind)
14f9c5c9
AS
2499{
2500 int k;
2501
2502 for (k = 0; k < arity; k += 1)
2503 {
2504 LONGEST lwb, upb;
14f9c5c9
AS
2505
2506 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
323e0a4a 2507 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 2508 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4c4b4cd2 2509 value_copy (arr));
14f9c5c9 2510 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
2497b498 2511 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
14f9c5c9
AS
2512 type = TYPE_TARGET_TYPE (type);
2513 }
2514
2515 return value_ind (arr);
2516}
2517
0b5d8877 2518/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
f5938064
JG
2519 actual type of ARRAY_PTR is ignored), returns the Ada slice of HIGH-LOW+1
2520 elements starting at index LOW. The lower bound of this array is LOW, as
2521 per Ada rules. */
0b5d8877 2522static struct value *
f5938064
JG
2523ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
2524 int low, int high)
0b5d8877 2525{
6c038f32 2526 CORE_ADDR base = value_as_address (array_ptr)
43bbcdc2 2527 + ((low - ada_discrete_type_low_bound (TYPE_INDEX_TYPE (type)))
0b5d8877 2528 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
6c038f32
PH
2529 struct type *index_type =
2530 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)),
0b5d8877 2531 low, high);
6c038f32 2532 struct type *slice_type =
0b5d8877 2533 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
5b4ee69b 2534
f5938064 2535 return value_at_lazy (slice_type, base);
0b5d8877
PH
2536}
2537
2538
2539static struct value *
2540ada_value_slice (struct value *array, int low, int high)
2541{
df407dfe 2542 struct type *type = value_type (array);
6c038f32 2543 struct type *index_type =
0b5d8877 2544 create_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
6c038f32 2545 struct type *slice_type =
0b5d8877 2546 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
5b4ee69b 2547
6c038f32 2548 return value_cast (slice_type, value_slice (array, low, high - low + 1));
0b5d8877
PH
2549}
2550
14f9c5c9
AS
2551/* If type is a record type in the form of a standard GNAT array
2552 descriptor, returns the number of dimensions for type. If arr is a
2553 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2554 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2555
2556int
d2e4a39e 2557ada_array_arity (struct type *type)
14f9c5c9
AS
2558{
2559 int arity;
2560
2561 if (type == NULL)
2562 return 0;
2563
2564 type = desc_base_type (type);
2565
2566 arity = 0;
d2e4a39e 2567 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9 2568 return desc_arity (desc_bounds_type (type));
d2e4a39e
AS
2569 else
2570 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9 2571 {
4c4b4cd2 2572 arity += 1;
61ee279c 2573 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2574 }
d2e4a39e 2575
14f9c5c9
AS
2576 return arity;
2577}
2578
2579/* If TYPE is a record type in the form of a standard GNAT array
2580 descriptor or a simple array type, returns the element type for
2581 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2582 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2583
d2e4a39e
AS
2584struct type *
2585ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2586{
2587 type = desc_base_type (type);
2588
d2e4a39e 2589 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9
AS
2590 {
2591 int k;
d2e4a39e 2592 struct type *p_array_type;
14f9c5c9 2593
556bdfd4 2594 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
2595
2596 k = ada_array_arity (type);
2597 if (k == 0)
4c4b4cd2 2598 return NULL;
d2e4a39e 2599
4c4b4cd2 2600 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2601 if (nindices >= 0 && k > nindices)
4c4b4cd2 2602 k = nindices;
d2e4a39e 2603 while (k > 0 && p_array_type != NULL)
4c4b4cd2 2604 {
61ee279c 2605 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
4c4b4cd2
PH
2606 k -= 1;
2607 }
14f9c5c9
AS
2608 return p_array_type;
2609 }
2610 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2611 {
2612 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
4c4b4cd2
PH
2613 {
2614 type = TYPE_TARGET_TYPE (type);
2615 nindices -= 1;
2616 }
14f9c5c9
AS
2617 return type;
2618 }
2619
2620 return NULL;
2621}
2622
4c4b4cd2 2623/* The type of nth index in arrays of given type (n numbering from 1).
dd19d49e
UW
2624 Does not examine memory. Throws an error if N is invalid or TYPE
2625 is not an array type. NAME is the name of the Ada attribute being
2626 evaluated ('range, 'first, 'last, or 'length); it is used in building
2627 the error message. */
14f9c5c9 2628
1eea4ebd
UW
2629static struct type *
2630ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 2631{
4c4b4cd2
PH
2632 struct type *result_type;
2633
14f9c5c9
AS
2634 type = desc_base_type (type);
2635
1eea4ebd
UW
2636 if (n < 0 || n > ada_array_arity (type))
2637 error (_("invalid dimension number to '%s"), name);
14f9c5c9 2638
4c4b4cd2 2639 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2640 {
2641 int i;
2642
2643 for (i = 1; i < n; i += 1)
4c4b4cd2 2644 type = TYPE_TARGET_TYPE (type);
262452ec 2645 result_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type));
4c4b4cd2
PH
2646 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2647 has a target type of TYPE_CODE_UNDEF. We compensate here, but
76a01679 2648 perhaps stabsread.c would make more sense. */
1eea4ebd
UW
2649 if (result_type && TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2650 result_type = NULL;
14f9c5c9 2651 }
d2e4a39e 2652 else
1eea4ebd
UW
2653 {
2654 result_type = desc_index_type (desc_bounds_type (type), n);
2655 if (result_type == NULL)
2656 error (_("attempt to take bound of something that is not an array"));
2657 }
2658
2659 return result_type;
14f9c5c9
AS
2660}
2661
2662/* Given that arr is an array type, returns the lower bound of the
2663 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 2664 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
2665 array-descriptor type. It works for other arrays with bounds supplied
2666 by run-time quantities other than discriminants. */
14f9c5c9 2667
abb68b3e 2668static LONGEST
1eea4ebd 2669ada_array_bound_from_type (struct type * arr_type, int n, int which)
14f9c5c9 2670{
1ce677a4 2671 struct type *type, *elt_type, *index_type_desc, *index_type;
1ce677a4 2672 int i;
262452ec
JK
2673
2674 gdb_assert (which == 0 || which == 1);
14f9c5c9 2675
ad82864c
JB
2676 if (ada_is_constrained_packed_array_type (arr_type))
2677 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 2678
4c4b4cd2 2679 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 2680 return (LONGEST) - which;
14f9c5c9
AS
2681
2682 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
2683 type = TYPE_TARGET_TYPE (arr_type);
2684 else
2685 type = arr_type;
2686
1ce677a4
UW
2687 elt_type = type;
2688 for (i = n; i > 1; i--)
2689 elt_type = TYPE_TARGET_TYPE (type);
2690
14f9c5c9 2691 index_type_desc = ada_find_parallel_type (type, "___XA");
28c85d6c 2692 ada_fixup_array_indexes_type (index_type_desc);
262452ec 2693 if (index_type_desc != NULL)
28c85d6c
JB
2694 index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, n - 1),
2695 NULL);
262452ec 2696 else
1ce677a4 2697 index_type = TYPE_INDEX_TYPE (elt_type);
262452ec 2698
43bbcdc2
PH
2699 return
2700 (LONGEST) (which == 0
2701 ? ada_discrete_type_low_bound (index_type)
2702 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
2703}
2704
2705/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
2706 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2707 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 2708 supplied by run-time quantities other than discriminants. */
14f9c5c9 2709
1eea4ebd 2710static LONGEST
4dc81987 2711ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 2712{
df407dfe 2713 struct type *arr_type = value_type (arr);
14f9c5c9 2714
ad82864c
JB
2715 if (ada_is_constrained_packed_array_type (arr_type))
2716 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 2717 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 2718 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 2719 else
1eea4ebd 2720 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
2721}
2722
2723/* Given that arr is an array value, returns the length of the
2724 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
2725 supplied by run-time quantities other than discriminants.
2726 Does not work for arrays indexed by enumeration types with representation
2727 clauses at the moment. */
14f9c5c9 2728
1eea4ebd 2729static LONGEST
d2e4a39e 2730ada_array_length (struct value *arr, int n)
14f9c5c9 2731{
df407dfe 2732 struct type *arr_type = ada_check_typedef (value_type (arr));
14f9c5c9 2733
ad82864c
JB
2734 if (ada_is_constrained_packed_array_type (arr_type))
2735 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 2736
4c4b4cd2 2737 if (ada_is_simple_array_type (arr_type))
1eea4ebd
UW
2738 return (ada_array_bound_from_type (arr_type, n, 1)
2739 - ada_array_bound_from_type (arr_type, n, 0) + 1);
14f9c5c9 2740 else
1eea4ebd
UW
2741 return (value_as_long (desc_one_bound (desc_bounds (arr), n, 1))
2742 - value_as_long (desc_one_bound (desc_bounds (arr), n, 0)) + 1);
4c4b4cd2
PH
2743}
2744
2745/* An empty array whose type is that of ARR_TYPE (an array type),
2746 with bounds LOW to LOW-1. */
2747
2748static struct value *
2749empty_array (struct type *arr_type, int low)
2750{
6c038f32 2751 struct type *index_type =
0b5d8877
PH
2752 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type)),
2753 low, low - 1);
2754 struct type *elt_type = ada_array_element_type (arr_type, 1);
5b4ee69b 2755
0b5d8877 2756 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 2757}
14f9c5c9 2758\f
d2e4a39e 2759
4c4b4cd2 2760 /* Name resolution */
14f9c5c9 2761
4c4b4cd2
PH
2762/* The "decoded" name for the user-definable Ada operator corresponding
2763 to OP. */
14f9c5c9 2764
d2e4a39e 2765static const char *
4c4b4cd2 2766ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
2767{
2768 int i;
2769
4c4b4cd2 2770 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
2771 {
2772 if (ada_opname_table[i].op == op)
4c4b4cd2 2773 return ada_opname_table[i].decoded;
14f9c5c9 2774 }
323e0a4a 2775 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
2776}
2777
2778
4c4b4cd2
PH
2779/* Same as evaluate_type (*EXP), but resolves ambiguous symbol
2780 references (marked by OP_VAR_VALUE nodes in which the symbol has an
2781 undefined namespace) and converts operators that are
2782 user-defined into appropriate function calls. If CONTEXT_TYPE is
14f9c5c9
AS
2783 non-null, it provides a preferred result type [at the moment, only
2784 type void has any effect---causing procedures to be preferred over
2785 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
4c4b4cd2 2786 return type is preferred. May change (expand) *EXP. */
14f9c5c9 2787
4c4b4cd2
PH
2788static void
2789resolve (struct expression **expp, int void_context_p)
14f9c5c9 2790{
30b15541
UW
2791 struct type *context_type = NULL;
2792 int pc = 0;
2793
2794 if (void_context_p)
2795 context_type = builtin_type ((*expp)->gdbarch)->builtin_void;
2796
2797 resolve_subexp (expp, &pc, 1, context_type);
14f9c5c9
AS
2798}
2799
4c4b4cd2
PH
2800/* Resolve the operator of the subexpression beginning at
2801 position *POS of *EXPP. "Resolving" consists of replacing
2802 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
2803 with their resolutions, replacing built-in operators with
2804 function calls to user-defined operators, where appropriate, and,
2805 when DEPROCEDURE_P is non-zero, converting function-valued variables
2806 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
2807 are as in ada_resolve, above. */
14f9c5c9 2808
d2e4a39e 2809static struct value *
4c4b4cd2 2810resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
76a01679 2811 struct type *context_type)
14f9c5c9
AS
2812{
2813 int pc = *pos;
2814 int i;
4c4b4cd2 2815 struct expression *exp; /* Convenience: == *expp. */
14f9c5c9 2816 enum exp_opcode op = (*expp)->elts[pc].opcode;
4c4b4cd2
PH
2817 struct value **argvec; /* Vector of operand types (alloca'ed). */
2818 int nargs; /* Number of operands. */
52ce6436 2819 int oplen;
14f9c5c9
AS
2820
2821 argvec = NULL;
2822 nargs = 0;
2823 exp = *expp;
2824
52ce6436
PH
2825 /* Pass one: resolve operands, saving their types and updating *pos,
2826 if needed. */
14f9c5c9
AS
2827 switch (op)
2828 {
4c4b4cd2
PH
2829 case OP_FUNCALL:
2830 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679
JB
2831 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
2832 *pos += 7;
4c4b4cd2
PH
2833 else
2834 {
2835 *pos += 3;
2836 resolve_subexp (expp, pos, 0, NULL);
2837 }
2838 nargs = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9
AS
2839 break;
2840
14f9c5c9 2841 case UNOP_ADDR:
4c4b4cd2
PH
2842 *pos += 1;
2843 resolve_subexp (expp, pos, 0, NULL);
2844 break;
2845
52ce6436
PH
2846 case UNOP_QUAL:
2847 *pos += 3;
17466c1a 2848 resolve_subexp (expp, pos, 1, check_typedef (exp->elts[pc + 1].type));
4c4b4cd2
PH
2849 break;
2850
52ce6436 2851 case OP_ATR_MODULUS:
4c4b4cd2
PH
2852 case OP_ATR_SIZE:
2853 case OP_ATR_TAG:
4c4b4cd2
PH
2854 case OP_ATR_FIRST:
2855 case OP_ATR_LAST:
2856 case OP_ATR_LENGTH:
2857 case OP_ATR_POS:
2858 case OP_ATR_VAL:
4c4b4cd2
PH
2859 case OP_ATR_MIN:
2860 case OP_ATR_MAX:
52ce6436
PH
2861 case TERNOP_IN_RANGE:
2862 case BINOP_IN_BOUNDS:
2863 case UNOP_IN_RANGE:
2864 case OP_AGGREGATE:
2865 case OP_OTHERS:
2866 case OP_CHOICES:
2867 case OP_POSITIONAL:
2868 case OP_DISCRETE_RANGE:
2869 case OP_NAME:
2870 ada_forward_operator_length (exp, pc, &oplen, &nargs);
2871 *pos += oplen;
14f9c5c9
AS
2872 break;
2873
2874 case BINOP_ASSIGN:
2875 {
4c4b4cd2
PH
2876 struct value *arg1;
2877
2878 *pos += 1;
2879 arg1 = resolve_subexp (expp, pos, 0, NULL);
2880 if (arg1 == NULL)
2881 resolve_subexp (expp, pos, 1, NULL);
2882 else
df407dfe 2883 resolve_subexp (expp, pos, 1, value_type (arg1));
4c4b4cd2 2884 break;
14f9c5c9
AS
2885 }
2886
4c4b4cd2 2887 case UNOP_CAST:
4c4b4cd2
PH
2888 *pos += 3;
2889 nargs = 1;
2890 break;
14f9c5c9 2891
4c4b4cd2
PH
2892 case BINOP_ADD:
2893 case BINOP_SUB:
2894 case BINOP_MUL:
2895 case BINOP_DIV:
2896 case BINOP_REM:
2897 case BINOP_MOD:
2898 case BINOP_EXP:
2899 case BINOP_CONCAT:
2900 case BINOP_LOGICAL_AND:
2901 case BINOP_LOGICAL_OR:
2902 case BINOP_BITWISE_AND:
2903 case BINOP_BITWISE_IOR:
2904 case BINOP_BITWISE_XOR:
14f9c5c9 2905
4c4b4cd2
PH
2906 case BINOP_EQUAL:
2907 case BINOP_NOTEQUAL:
2908 case BINOP_LESS:
2909 case BINOP_GTR:
2910 case BINOP_LEQ:
2911 case BINOP_GEQ:
14f9c5c9 2912
4c4b4cd2
PH
2913 case BINOP_REPEAT:
2914 case BINOP_SUBSCRIPT:
2915 case BINOP_COMMA:
40c8aaa9
JB
2916 *pos += 1;
2917 nargs = 2;
2918 break;
14f9c5c9 2919
4c4b4cd2
PH
2920 case UNOP_NEG:
2921 case UNOP_PLUS:
2922 case UNOP_LOGICAL_NOT:
2923 case UNOP_ABS:
2924 case UNOP_IND:
2925 *pos += 1;
2926 nargs = 1;
2927 break;
14f9c5c9 2928
4c4b4cd2
PH
2929 case OP_LONG:
2930 case OP_DOUBLE:
2931 case OP_VAR_VALUE:
2932 *pos += 4;
2933 break;
14f9c5c9 2934
4c4b4cd2
PH
2935 case OP_TYPE:
2936 case OP_BOOL:
2937 case OP_LAST:
4c4b4cd2
PH
2938 case OP_INTERNALVAR:
2939 *pos += 3;
2940 break;
14f9c5c9 2941
4c4b4cd2
PH
2942 case UNOP_MEMVAL:
2943 *pos += 3;
2944 nargs = 1;
2945 break;
2946
67f3407f
DJ
2947 case OP_REGISTER:
2948 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2949 break;
2950
4c4b4cd2
PH
2951 case STRUCTOP_STRUCT:
2952 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2953 nargs = 1;
2954 break;
2955
4c4b4cd2 2956 case TERNOP_SLICE:
4c4b4cd2
PH
2957 *pos += 1;
2958 nargs = 3;
2959 break;
2960
52ce6436 2961 case OP_STRING:
14f9c5c9 2962 break;
4c4b4cd2
PH
2963
2964 default:
323e0a4a 2965 error (_("Unexpected operator during name resolution"));
14f9c5c9
AS
2966 }
2967
76a01679 2968 argvec = (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
4c4b4cd2
PH
2969 for (i = 0; i < nargs; i += 1)
2970 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
2971 argvec[i] = NULL;
2972 exp = *expp;
2973
2974 /* Pass two: perform any resolution on principal operator. */
14f9c5c9
AS
2975 switch (op)
2976 {
2977 default:
2978 break;
2979
14f9c5c9 2980 case OP_VAR_VALUE:
4c4b4cd2 2981 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
2982 {
2983 struct ada_symbol_info *candidates;
2984 int n_candidates;
2985
2986 n_candidates =
2987 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2988 (exp->elts[pc + 2].symbol),
2989 exp->elts[pc + 1].block, VAR_DOMAIN,
2990 &candidates);
2991
2992 if (n_candidates > 1)
2993 {
2994 /* Types tend to get re-introduced locally, so if there
2995 are any local symbols that are not types, first filter
2996 out all types. */
2997 int j;
2998 for (j = 0; j < n_candidates; j += 1)
2999 switch (SYMBOL_CLASS (candidates[j].sym))
3000 {
3001 case LOC_REGISTER:
3002 case LOC_ARG:
3003 case LOC_REF_ARG:
76a01679
JB
3004 case LOC_REGPARM_ADDR:
3005 case LOC_LOCAL:
76a01679 3006 case LOC_COMPUTED:
76a01679
JB
3007 goto FoundNonType;
3008 default:
3009 break;
3010 }
3011 FoundNonType:
3012 if (j < n_candidates)
3013 {
3014 j = 0;
3015 while (j < n_candidates)
3016 {
3017 if (SYMBOL_CLASS (candidates[j].sym) == LOC_TYPEDEF)
3018 {
3019 candidates[j] = candidates[n_candidates - 1];
3020 n_candidates -= 1;
3021 }
3022 else
3023 j += 1;
3024 }
3025 }
3026 }
3027
3028 if (n_candidates == 0)
323e0a4a 3029 error (_("No definition found for %s"),
76a01679
JB
3030 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3031 else if (n_candidates == 1)
3032 i = 0;
3033 else if (deprocedure_p
3034 && !is_nonfunction (candidates, n_candidates))
3035 {
06d5cf63
JB
3036 i = ada_resolve_function
3037 (candidates, n_candidates, NULL, 0,
3038 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
3039 context_type);
76a01679 3040 if (i < 0)
323e0a4a 3041 error (_("Could not find a match for %s"),
76a01679
JB
3042 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3043 }
3044 else
3045 {
323e0a4a 3046 printf_filtered (_("Multiple matches for %s\n"),
76a01679
JB
3047 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3048 user_select_syms (candidates, n_candidates, 1);
3049 i = 0;
3050 }
3051
3052 exp->elts[pc + 1].block = candidates[i].block;
3053 exp->elts[pc + 2].symbol = candidates[i].sym;
1265e4aa
JB
3054 if (innermost_block == NULL
3055 || contained_in (candidates[i].block, innermost_block))
76a01679
JB
3056 innermost_block = candidates[i].block;
3057 }
3058
3059 if (deprocedure_p
3060 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
3061 == TYPE_CODE_FUNC))
3062 {
3063 replace_operator_with_call (expp, pc, 0, 0,
3064 exp->elts[pc + 2].symbol,
3065 exp->elts[pc + 1].block);
3066 exp = *expp;
3067 }
14f9c5c9
AS
3068 break;
3069
3070 case OP_FUNCALL:
3071 {
4c4b4cd2 3072 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679 3073 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
4c4b4cd2
PH
3074 {
3075 struct ada_symbol_info *candidates;
3076 int n_candidates;
3077
3078 n_candidates =
76a01679
JB
3079 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3080 (exp->elts[pc + 5].symbol),
3081 exp->elts[pc + 4].block, VAR_DOMAIN,
3082 &candidates);
4c4b4cd2
PH
3083 if (n_candidates == 1)
3084 i = 0;
3085 else
3086 {
06d5cf63
JB
3087 i = ada_resolve_function
3088 (candidates, n_candidates,
3089 argvec, nargs,
3090 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
3091 context_type);
4c4b4cd2 3092 if (i < 0)
323e0a4a 3093 error (_("Could not find a match for %s"),
4c4b4cd2
PH
3094 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
3095 }
3096
3097 exp->elts[pc + 4].block = candidates[i].block;
3098 exp->elts[pc + 5].symbol = candidates[i].sym;
1265e4aa
JB
3099 if (innermost_block == NULL
3100 || contained_in (candidates[i].block, innermost_block))
4c4b4cd2
PH
3101 innermost_block = candidates[i].block;
3102 }
14f9c5c9
AS
3103 }
3104 break;
3105 case BINOP_ADD:
3106 case BINOP_SUB:
3107 case BINOP_MUL:
3108 case BINOP_DIV:
3109 case BINOP_REM:
3110 case BINOP_MOD:
3111 case BINOP_CONCAT:
3112 case BINOP_BITWISE_AND:
3113 case BINOP_BITWISE_IOR:
3114 case BINOP_BITWISE_XOR:
3115 case BINOP_EQUAL:
3116 case BINOP_NOTEQUAL:
3117 case BINOP_LESS:
3118 case BINOP_GTR:
3119 case BINOP_LEQ:
3120 case BINOP_GEQ:
3121 case BINOP_EXP:
3122 case UNOP_NEG:
3123 case UNOP_PLUS:
3124 case UNOP_LOGICAL_NOT:
3125 case UNOP_ABS:
3126 if (possible_user_operator_p (op, argvec))
4c4b4cd2
PH
3127 {
3128 struct ada_symbol_info *candidates;
3129 int n_candidates;
3130
3131 n_candidates =
3132 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
3133 (struct block *) NULL, VAR_DOMAIN,
3134 &candidates);
3135 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
76a01679 3136 ada_decoded_op_name (op), NULL);
4c4b4cd2
PH
3137 if (i < 0)
3138 break;
3139
76a01679
JB
3140 replace_operator_with_call (expp, pc, nargs, 1,
3141 candidates[i].sym, candidates[i].block);
4c4b4cd2
PH
3142 exp = *expp;
3143 }
14f9c5c9 3144 break;
4c4b4cd2
PH
3145
3146 case OP_TYPE:
b3dbf008 3147 case OP_REGISTER:
4c4b4cd2 3148 return NULL;
14f9c5c9
AS
3149 }
3150
3151 *pos = pc;
3152 return evaluate_subexp_type (exp, pos);
3153}
3154
3155/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
4c4b4cd2 3156 MAY_DEREF is non-zero, the formal may be a pointer and the actual
5b3d5b7d 3157 a non-pointer. */
14f9c5c9 3158/* The term "match" here is rather loose. The match is heuristic and
5b3d5b7d 3159 liberal. */
14f9c5c9
AS
3160
3161static int
4dc81987 3162ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9 3163{
61ee279c
PH
3164 ftype = ada_check_typedef (ftype);
3165 atype = ada_check_typedef (atype);
14f9c5c9
AS
3166
3167 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
3168 ftype = TYPE_TARGET_TYPE (ftype);
3169 if (TYPE_CODE (atype) == TYPE_CODE_REF)
3170 atype = TYPE_TARGET_TYPE (atype);
3171
d2e4a39e 3172 switch (TYPE_CODE (ftype))
14f9c5c9
AS
3173 {
3174 default:
5b3d5b7d 3175 return TYPE_CODE (ftype) == TYPE_CODE (atype);
14f9c5c9
AS
3176 case TYPE_CODE_PTR:
3177 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
4c4b4cd2
PH
3178 return ada_type_match (TYPE_TARGET_TYPE (ftype),
3179 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e 3180 else
1265e4aa
JB
3181 return (may_deref
3182 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
14f9c5c9
AS
3183 case TYPE_CODE_INT:
3184 case TYPE_CODE_ENUM:
3185 case TYPE_CODE_RANGE:
3186 switch (TYPE_CODE (atype))
4c4b4cd2
PH
3187 {
3188 case TYPE_CODE_INT:
3189 case TYPE_CODE_ENUM:
3190 case TYPE_CODE_RANGE:
3191 return 1;
3192 default:
3193 return 0;
3194 }
14f9c5c9
AS
3195
3196 case TYPE_CODE_ARRAY:
d2e4a39e 3197 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
4c4b4cd2 3198 || ada_is_array_descriptor_type (atype));
14f9c5c9
AS
3199
3200 case TYPE_CODE_STRUCT:
4c4b4cd2
PH
3201 if (ada_is_array_descriptor_type (ftype))
3202 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3203 || ada_is_array_descriptor_type (atype));
14f9c5c9 3204 else
4c4b4cd2
PH
3205 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
3206 && !ada_is_array_descriptor_type (atype));
14f9c5c9
AS
3207
3208 case TYPE_CODE_UNION:
3209 case TYPE_CODE_FLT:
3210 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
3211 }
3212}
3213
3214/* Return non-zero if the formals of FUNC "sufficiently match" the
3215 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3216 may also be an enumeral, in which case it is treated as a 0-
4c4b4cd2 3217 argument function. */
14f9c5c9
AS
3218
3219static int
d2e4a39e 3220ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
14f9c5c9
AS
3221{
3222 int i;
d2e4a39e 3223 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 3224
1265e4aa
JB
3225 if (SYMBOL_CLASS (func) == LOC_CONST
3226 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
14f9c5c9
AS
3227 return (n_actuals == 0);
3228 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
3229 return 0;
3230
3231 if (TYPE_NFIELDS (func_type) != n_actuals)
3232 return 0;
3233
3234 for (i = 0; i < n_actuals; i += 1)
3235 {
4c4b4cd2 3236 if (actuals[i] == NULL)
76a01679
JB
3237 return 0;
3238 else
3239 {
5b4ee69b
MS
3240 struct type *ftype = ada_check_typedef (TYPE_FIELD_TYPE (func_type,
3241 i));
df407dfe 3242 struct type *atype = ada_check_typedef (value_type (actuals[i]));
4c4b4cd2 3243
76a01679
JB
3244 if (!ada_type_match (ftype, atype, 1))
3245 return 0;
3246 }
14f9c5c9
AS
3247 }
3248 return 1;
3249}
3250
3251/* False iff function type FUNC_TYPE definitely does not produce a value
3252 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3253 FUNC_TYPE is not a valid function type with a non-null return type
3254 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
3255
3256static int
d2e4a39e 3257return_match (struct type *func_type, struct type *context_type)
14f9c5c9 3258{
d2e4a39e 3259 struct type *return_type;
14f9c5c9
AS
3260
3261 if (func_type == NULL)
3262 return 1;
3263
4c4b4cd2
PH
3264 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
3265 return_type = base_type (TYPE_TARGET_TYPE (func_type));
3266 else
3267 return_type = base_type (func_type);
14f9c5c9
AS
3268 if (return_type == NULL)
3269 return 1;
3270
4c4b4cd2 3271 context_type = base_type (context_type);
14f9c5c9
AS
3272
3273 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
3274 return context_type == NULL || return_type == context_type;
3275 else if (context_type == NULL)
3276 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
3277 else
3278 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
3279}
3280
3281
4c4b4cd2 3282/* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
14f9c5c9 3283 function (if any) that matches the types of the NARGS arguments in
4c4b4cd2
PH
3284 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3285 that returns that type, then eliminate matches that don't. If
3286 CONTEXT_TYPE is void and there is at least one match that does not
3287 return void, eliminate all matches that do.
3288
14f9c5c9
AS
3289 Asks the user if there is more than one match remaining. Returns -1
3290 if there is no such symbol or none is selected. NAME is used
4c4b4cd2
PH
3291 solely for messages. May re-arrange and modify SYMS in
3292 the process; the index returned is for the modified vector. */
14f9c5c9 3293
4c4b4cd2
PH
3294static int
3295ada_resolve_function (struct ada_symbol_info syms[],
3296 int nsyms, struct value **args, int nargs,
3297 const char *name, struct type *context_type)
14f9c5c9 3298{
30b15541 3299 int fallback;
14f9c5c9 3300 int k;
4c4b4cd2 3301 int m; /* Number of hits */
14f9c5c9 3302
d2e4a39e 3303 m = 0;
30b15541
UW
3304 /* In the first pass of the loop, we only accept functions matching
3305 context_type. If none are found, we add a second pass of the loop
3306 where every function is accepted. */
3307 for (fallback = 0; m == 0 && fallback < 2; fallback++)
14f9c5c9
AS
3308 {
3309 for (k = 0; k < nsyms; k += 1)
4c4b4cd2 3310 {
61ee279c 3311 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].sym));
4c4b4cd2
PH
3312
3313 if (ada_args_match (syms[k].sym, args, nargs)
30b15541 3314 && (fallback || return_match (type, context_type)))
4c4b4cd2
PH
3315 {
3316 syms[m] = syms[k];
3317 m += 1;
3318 }
3319 }
14f9c5c9
AS
3320 }
3321
3322 if (m == 0)
3323 return -1;
3324 else if (m > 1)
3325 {
323e0a4a 3326 printf_filtered (_("Multiple matches for %s\n"), name);
4c4b4cd2 3327 user_select_syms (syms, m, 1);
14f9c5c9
AS
3328 return 0;
3329 }
3330 return 0;
3331}
3332
4c4b4cd2
PH
3333/* Returns true (non-zero) iff decoded name N0 should appear before N1
3334 in a listing of choices during disambiguation (see sort_choices, below).
3335 The idea is that overloadings of a subprogram name from the
3336 same package should sort in their source order. We settle for ordering
3337 such symbols by their trailing number (__N or $N). */
3338
14f9c5c9 3339static int
4c4b4cd2 3340encoded_ordered_before (char *N0, char *N1)
14f9c5c9
AS
3341{
3342 if (N1 == NULL)
3343 return 0;
3344 else if (N0 == NULL)
3345 return 1;
3346 else
3347 {
3348 int k0, k1;
5b4ee69b 3349
d2e4a39e 3350 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
4c4b4cd2 3351 ;
d2e4a39e 3352 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
4c4b4cd2 3353 ;
d2e4a39e 3354 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
4c4b4cd2
PH
3355 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3356 {
3357 int n0, n1;
5b4ee69b 3358
4c4b4cd2
PH
3359 n0 = k0;
3360 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3361 n0 -= 1;
3362 n1 = k1;
3363 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3364 n1 -= 1;
3365 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3366 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3367 }
14f9c5c9
AS
3368 return (strcmp (N0, N1) < 0);
3369 }
3370}
d2e4a39e 3371
4c4b4cd2
PH
3372/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3373 encoded names. */
3374
d2e4a39e 3375static void
4c4b4cd2 3376sort_choices (struct ada_symbol_info syms[], int nsyms)
14f9c5c9 3377{
4c4b4cd2 3378 int i;
5b4ee69b 3379
d2e4a39e 3380 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3381 {
4c4b4cd2 3382 struct ada_symbol_info sym = syms[i];
14f9c5c9
AS
3383 int j;
3384
d2e4a39e 3385 for (j = i - 1; j >= 0; j -= 1)
4c4b4cd2
PH
3386 {
3387 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].sym),
3388 SYMBOL_LINKAGE_NAME (sym.sym)))
3389 break;
3390 syms[j + 1] = syms[j];
3391 }
d2e4a39e 3392 syms[j + 1] = sym;
14f9c5c9
AS
3393 }
3394}
3395
4c4b4cd2
PH
3396/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3397 by asking the user (if necessary), returning the number selected,
3398 and setting the first elements of SYMS items. Error if no symbols
3399 selected. */
14f9c5c9
AS
3400
3401/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
4c4b4cd2 3402 to be re-integrated one of these days. */
14f9c5c9
AS
3403
3404int
4c4b4cd2 3405user_select_syms (struct ada_symbol_info *syms, int nsyms, int max_results)
14f9c5c9
AS
3406{
3407 int i;
d2e4a39e 3408 int *chosen = (int *) alloca (sizeof (int) * nsyms);
14f9c5c9
AS
3409 int n_chosen;
3410 int first_choice = (max_results == 1) ? 1 : 2;
717d2f5a 3411 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9
AS
3412
3413 if (max_results < 1)
323e0a4a 3414 error (_("Request to select 0 symbols!"));
14f9c5c9
AS
3415 if (nsyms <= 1)
3416 return nsyms;
3417
717d2f5a
JB
3418 if (select_mode == multiple_symbols_cancel)
3419 error (_("\
3420canceled because the command is ambiguous\n\
3421See set/show multiple-symbol."));
3422
3423 /* If select_mode is "all", then return all possible symbols.
3424 Only do that if more than one symbol can be selected, of course.
3425 Otherwise, display the menu as usual. */
3426 if (select_mode == multiple_symbols_all && max_results > 1)
3427 return nsyms;
3428
323e0a4a 3429 printf_unfiltered (_("[0] cancel\n"));
14f9c5c9 3430 if (max_results > 1)
323e0a4a 3431 printf_unfiltered (_("[1] all\n"));
14f9c5c9 3432
4c4b4cd2 3433 sort_choices (syms, nsyms);
14f9c5c9
AS
3434
3435 for (i = 0; i < nsyms; i += 1)
3436 {
4c4b4cd2
PH
3437 if (syms[i].sym == NULL)
3438 continue;
3439
3440 if (SYMBOL_CLASS (syms[i].sym) == LOC_BLOCK)
3441 {
76a01679
JB
3442 struct symtab_and_line sal =
3443 find_function_start_sal (syms[i].sym, 1);
5b4ee69b 3444
323e0a4a
AC
3445 if (sal.symtab == NULL)
3446 printf_unfiltered (_("[%d] %s at <no source file available>:%d\n"),
3447 i + first_choice,
3448 SYMBOL_PRINT_NAME (syms[i].sym),
3449 sal.line);
3450 else
3451 printf_unfiltered (_("[%d] %s at %s:%d\n"), i + first_choice,
3452 SYMBOL_PRINT_NAME (syms[i].sym),
3453 sal.symtab->filename, sal.line);
4c4b4cd2
PH
3454 continue;
3455 }
d2e4a39e 3456 else
4c4b4cd2
PH
3457 {
3458 int is_enumeral =
3459 (SYMBOL_CLASS (syms[i].sym) == LOC_CONST
3460 && SYMBOL_TYPE (syms[i].sym) != NULL
3461 && TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) == TYPE_CODE_ENUM);
6f38eac8 3462 struct symtab *symtab = syms[i].sym->symtab;
4c4b4cd2
PH
3463
3464 if (SYMBOL_LINE (syms[i].sym) != 0 && symtab != NULL)
323e0a4a 3465 printf_unfiltered (_("[%d] %s at %s:%d\n"),
4c4b4cd2
PH
3466 i + first_choice,
3467 SYMBOL_PRINT_NAME (syms[i].sym),
3468 symtab->filename, SYMBOL_LINE (syms[i].sym));
76a01679
JB
3469 else if (is_enumeral
3470 && TYPE_NAME (SYMBOL_TYPE (syms[i].sym)) != NULL)
4c4b4cd2 3471 {
a3f17187 3472 printf_unfiltered (("[%d] "), i + first_choice);
76a01679
JB
3473 ada_print_type (SYMBOL_TYPE (syms[i].sym), NULL,
3474 gdb_stdout, -1, 0);
323e0a4a 3475 printf_unfiltered (_("'(%s) (enumeral)\n"),
4c4b4cd2
PH
3476 SYMBOL_PRINT_NAME (syms[i].sym));
3477 }
3478 else if (symtab != NULL)
3479 printf_unfiltered (is_enumeral
323e0a4a
AC
3480 ? _("[%d] %s in %s (enumeral)\n")
3481 : _("[%d] %s at %s:?\n"),
4c4b4cd2
PH
3482 i + first_choice,
3483 SYMBOL_PRINT_NAME (syms[i].sym),
3484 symtab->filename);
3485 else
3486 printf_unfiltered (is_enumeral
323e0a4a
AC
3487 ? _("[%d] %s (enumeral)\n")
3488 : _("[%d] %s at ?\n"),
4c4b4cd2
PH
3489 i + first_choice,
3490 SYMBOL_PRINT_NAME (syms[i].sym));
3491 }
14f9c5c9 3492 }
d2e4a39e 3493
14f9c5c9 3494 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
4c4b4cd2 3495 "overload-choice");
14f9c5c9
AS
3496
3497 for (i = 0; i < n_chosen; i += 1)
4c4b4cd2 3498 syms[i] = syms[chosen[i]];
14f9c5c9
AS
3499
3500 return n_chosen;
3501}
3502
3503/* Read and validate a set of numeric choices from the user in the
4c4b4cd2 3504 range 0 .. N_CHOICES-1. Place the results in increasing
14f9c5c9
AS
3505 order in CHOICES[0 .. N-1], and return N.
3506
3507 The user types choices as a sequence of numbers on one line
3508 separated by blanks, encoding them as follows:
3509
4c4b4cd2 3510 + A choice of 0 means to cancel the selection, throwing an error.
14f9c5c9
AS
3511 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3512 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3513
4c4b4cd2 3514 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9
AS
3515
3516 ANNOTATION_SUFFIX, if present, is used to annotate the input
4c4b4cd2 3517 prompts (for use with the -f switch). */
14f9c5c9
AS
3518
3519int
d2e4a39e 3520get_selections (int *choices, int n_choices, int max_results,
4c4b4cd2 3521 int is_all_choice, char *annotation_suffix)
14f9c5c9 3522{
d2e4a39e 3523 char *args;
0bcd0149 3524 char *prompt;
14f9c5c9
AS
3525 int n_chosen;
3526 int first_choice = is_all_choice ? 2 : 1;
d2e4a39e 3527
14f9c5c9
AS
3528 prompt = getenv ("PS2");
3529 if (prompt == NULL)
0bcd0149 3530 prompt = "> ";
14f9c5c9 3531
0bcd0149 3532 args = command_line_input (prompt, 0, annotation_suffix);
d2e4a39e 3533
14f9c5c9 3534 if (args == NULL)
323e0a4a 3535 error_no_arg (_("one or more choice numbers"));
14f9c5c9
AS
3536
3537 n_chosen = 0;
76a01679 3538
4c4b4cd2
PH
3539 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3540 order, as given in args. Choices are validated. */
14f9c5c9
AS
3541 while (1)
3542 {
d2e4a39e 3543 char *args2;
14f9c5c9
AS
3544 int choice, j;
3545
3546 while (isspace (*args))
4c4b4cd2 3547 args += 1;
14f9c5c9 3548 if (*args == '\0' && n_chosen == 0)
323e0a4a 3549 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3550 else if (*args == '\0')
4c4b4cd2 3551 break;
14f9c5c9
AS
3552
3553 choice = strtol (args, &args2, 10);
d2e4a39e 3554 if (args == args2 || choice < 0
4c4b4cd2 3555 || choice > n_choices + first_choice - 1)
323e0a4a 3556 error (_("Argument must be choice number"));
14f9c5c9
AS
3557 args = args2;
3558
d2e4a39e 3559 if (choice == 0)
323e0a4a 3560 error (_("cancelled"));
14f9c5c9
AS
3561
3562 if (choice < first_choice)
4c4b4cd2
PH
3563 {
3564 n_chosen = n_choices;
3565 for (j = 0; j < n_choices; j += 1)
3566 choices[j] = j;
3567 break;
3568 }
14f9c5c9
AS
3569 choice -= first_choice;
3570
d2e4a39e 3571 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
4c4b4cd2
PH
3572 {
3573 }
14f9c5c9
AS
3574
3575 if (j < 0 || choice != choices[j])
4c4b4cd2
PH
3576 {
3577 int k;
5b4ee69b 3578
4c4b4cd2
PH
3579 for (k = n_chosen - 1; k > j; k -= 1)
3580 choices[k + 1] = choices[k];
3581 choices[j + 1] = choice;
3582 n_chosen += 1;
3583 }
14f9c5c9
AS
3584 }
3585
3586 if (n_chosen > max_results)
323e0a4a 3587 error (_("Select no more than %d of the above"), max_results);
d2e4a39e 3588
14f9c5c9
AS
3589 return n_chosen;
3590}
3591
4c4b4cd2
PH
3592/* Replace the operator of length OPLEN at position PC in *EXPP with a call
3593 on the function identified by SYM and BLOCK, and taking NARGS
3594 arguments. Update *EXPP as needed to hold more space. */
14f9c5c9
AS
3595
3596static void
d2e4a39e 3597replace_operator_with_call (struct expression **expp, int pc, int nargs,
4c4b4cd2
PH
3598 int oplen, struct symbol *sym,
3599 struct block *block)
14f9c5c9
AS
3600{
3601 /* A new expression, with 6 more elements (3 for funcall, 4 for function
4c4b4cd2 3602 symbol, -oplen for operator being replaced). */
d2e4a39e 3603 struct expression *newexp = (struct expression *)
14f9c5c9 3604 xmalloc (sizeof (struct expression)
4c4b4cd2 3605 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
d2e4a39e 3606 struct expression *exp = *expp;
14f9c5c9
AS
3607
3608 newexp->nelts = exp->nelts + 7 - oplen;
3609 newexp->language_defn = exp->language_defn;
3610 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
d2e4a39e 3611 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
4c4b4cd2 3612 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
14f9c5c9
AS
3613
3614 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
3615 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
3616
3617 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
3618 newexp->elts[pc + 4].block = block;
3619 newexp->elts[pc + 5].symbol = sym;
3620
3621 *expp = newexp;
aacb1f0a 3622 xfree (exp);
d2e4a39e 3623}
14f9c5c9
AS
3624
3625/* Type-class predicates */
3626
4c4b4cd2
PH
3627/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3628 or FLOAT). */
14f9c5c9
AS
3629
3630static int
d2e4a39e 3631numeric_type_p (struct type *type)
14f9c5c9
AS
3632{
3633 if (type == NULL)
3634 return 0;
d2e4a39e
AS
3635 else
3636 {
3637 switch (TYPE_CODE (type))
4c4b4cd2
PH
3638 {
3639 case TYPE_CODE_INT:
3640 case TYPE_CODE_FLT:
3641 return 1;
3642 case TYPE_CODE_RANGE:
3643 return (type == TYPE_TARGET_TYPE (type)
3644 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3645 default:
3646 return 0;
3647 }
d2e4a39e 3648 }
14f9c5c9
AS
3649}
3650
4c4b4cd2 3651/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
3652
3653static int
d2e4a39e 3654integer_type_p (struct type *type)
14f9c5c9
AS
3655{
3656 if (type == NULL)
3657 return 0;
d2e4a39e
AS
3658 else
3659 {
3660 switch (TYPE_CODE (type))
4c4b4cd2
PH
3661 {
3662 case TYPE_CODE_INT:
3663 return 1;
3664 case TYPE_CODE_RANGE:
3665 return (type == TYPE_TARGET_TYPE (type)
3666 || integer_type_p (TYPE_TARGET_TYPE (type)));
3667 default:
3668 return 0;
3669 }
d2e4a39e 3670 }
14f9c5c9
AS
3671}
3672
4c4b4cd2 3673/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
3674
3675static int
d2e4a39e 3676scalar_type_p (struct type *type)
14f9c5c9
AS
3677{
3678 if (type == NULL)
3679 return 0;
d2e4a39e
AS
3680 else
3681 {
3682 switch (TYPE_CODE (type))
4c4b4cd2
PH
3683 {
3684 case TYPE_CODE_INT:
3685 case TYPE_CODE_RANGE:
3686 case TYPE_CODE_ENUM:
3687 case TYPE_CODE_FLT:
3688 return 1;
3689 default:
3690 return 0;
3691 }
d2e4a39e 3692 }
14f9c5c9
AS
3693}
3694
4c4b4cd2 3695/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
3696
3697static int
d2e4a39e 3698discrete_type_p (struct type *type)
14f9c5c9
AS
3699{
3700 if (type == NULL)
3701 return 0;
d2e4a39e
AS
3702 else
3703 {
3704 switch (TYPE_CODE (type))
4c4b4cd2
PH
3705 {
3706 case TYPE_CODE_INT:
3707 case TYPE_CODE_RANGE:
3708 case TYPE_CODE_ENUM:
872f0337 3709 case TYPE_CODE_BOOL:
4c4b4cd2
PH
3710 return 1;
3711 default:
3712 return 0;
3713 }
d2e4a39e 3714 }
14f9c5c9
AS
3715}
3716
4c4b4cd2
PH
3717/* Returns non-zero if OP with operands in the vector ARGS could be
3718 a user-defined function. Errs on the side of pre-defined operators
3719 (i.e., result 0). */
14f9c5c9
AS
3720
3721static int
d2e4a39e 3722possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 3723{
76a01679 3724 struct type *type0 =
df407dfe 3725 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 3726 struct type *type1 =
df407dfe 3727 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 3728
4c4b4cd2
PH
3729 if (type0 == NULL)
3730 return 0;
3731
14f9c5c9
AS
3732 switch (op)
3733 {
3734 default:
3735 return 0;
3736
3737 case BINOP_ADD:
3738 case BINOP_SUB:
3739 case BINOP_MUL:
3740 case BINOP_DIV:
d2e4a39e 3741 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
3742
3743 case BINOP_REM:
3744 case BINOP_MOD:
3745 case BINOP_BITWISE_AND:
3746 case BINOP_BITWISE_IOR:
3747 case BINOP_BITWISE_XOR:
d2e4a39e 3748 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3749
3750 case BINOP_EQUAL:
3751 case BINOP_NOTEQUAL:
3752 case BINOP_LESS:
3753 case BINOP_GTR:
3754 case BINOP_LEQ:
3755 case BINOP_GEQ:
d2e4a39e 3756 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
3757
3758 case BINOP_CONCAT:
ee90b9ab 3759 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
3760
3761 case BINOP_EXP:
d2e4a39e 3762 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3763
3764 case UNOP_NEG:
3765 case UNOP_PLUS:
3766 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
3767 case UNOP_ABS:
3768 return (!numeric_type_p (type0));
14f9c5c9
AS
3769
3770 }
3771}
3772\f
4c4b4cd2 3773 /* Renaming */
14f9c5c9 3774
aeb5907d
JB
3775/* NOTES:
3776
3777 1. In the following, we assume that a renaming type's name may
3778 have an ___XD suffix. It would be nice if this went away at some
3779 point.
3780 2. We handle both the (old) purely type-based representation of
3781 renamings and the (new) variable-based encoding. At some point,
3782 it is devoutly to be hoped that the former goes away
3783 (FIXME: hilfinger-2007-07-09).
3784 3. Subprogram renamings are not implemented, although the XRS
3785 suffix is recognized (FIXME: hilfinger-2007-07-09). */
3786
3787/* If SYM encodes a renaming,
3788
3789 <renaming> renames <renamed entity>,
3790
3791 sets *LEN to the length of the renamed entity's name,
3792 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
3793 the string describing the subcomponent selected from the renamed
3794 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
3795 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
3796 are undefined). Otherwise, returns a value indicating the category
3797 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
3798 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
3799 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
3800 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
3801 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
3802 may be NULL, in which case they are not assigned.
3803
3804 [Currently, however, GCC does not generate subprogram renamings.] */
3805
3806enum ada_renaming_category
3807ada_parse_renaming (struct symbol *sym,
3808 const char **renamed_entity, int *len,
3809 const char **renaming_expr)
3810{
3811 enum ada_renaming_category kind;
3812 const char *info;
3813 const char *suffix;
3814
3815 if (sym == NULL)
3816 return ADA_NOT_RENAMING;
3817 switch (SYMBOL_CLASS (sym))
14f9c5c9 3818 {
aeb5907d
JB
3819 default:
3820 return ADA_NOT_RENAMING;
3821 case LOC_TYPEDEF:
3822 return parse_old_style_renaming (SYMBOL_TYPE (sym),
3823 renamed_entity, len, renaming_expr);
3824 case LOC_LOCAL:
3825 case LOC_STATIC:
3826 case LOC_COMPUTED:
3827 case LOC_OPTIMIZED_OUT:
3828 info = strstr (SYMBOL_LINKAGE_NAME (sym), "___XR");
3829 if (info == NULL)
3830 return ADA_NOT_RENAMING;
3831 switch (info[5])
3832 {
3833 case '_':
3834 kind = ADA_OBJECT_RENAMING;
3835 info += 6;
3836 break;
3837 case 'E':
3838 kind = ADA_EXCEPTION_RENAMING;
3839 info += 7;
3840 break;
3841 case 'P':
3842 kind = ADA_PACKAGE_RENAMING;
3843 info += 7;
3844 break;
3845 case 'S':
3846 kind = ADA_SUBPROGRAM_RENAMING;
3847 info += 7;
3848 break;
3849 default:
3850 return ADA_NOT_RENAMING;
3851 }
14f9c5c9 3852 }
4c4b4cd2 3853
aeb5907d
JB
3854 if (renamed_entity != NULL)
3855 *renamed_entity = info;
3856 suffix = strstr (info, "___XE");
3857 if (suffix == NULL || suffix == info)
3858 return ADA_NOT_RENAMING;
3859 if (len != NULL)
3860 *len = strlen (info) - strlen (suffix);
3861 suffix += 5;
3862 if (renaming_expr != NULL)
3863 *renaming_expr = suffix;
3864 return kind;
3865}
3866
3867/* Assuming TYPE encodes a renaming according to the old encoding in
3868 exp_dbug.ads, returns details of that renaming in *RENAMED_ENTITY,
3869 *LEN, and *RENAMING_EXPR, as for ada_parse_renaming, above. Returns
3870 ADA_NOT_RENAMING otherwise. */
3871static enum ada_renaming_category
3872parse_old_style_renaming (struct type *type,
3873 const char **renamed_entity, int *len,
3874 const char **renaming_expr)
3875{
3876 enum ada_renaming_category kind;
3877 const char *name;
3878 const char *info;
3879 const char *suffix;
14f9c5c9 3880
aeb5907d
JB
3881 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
3882 || TYPE_NFIELDS (type) != 1)
3883 return ADA_NOT_RENAMING;
14f9c5c9 3884
aeb5907d
JB
3885 name = type_name_no_tag (type);
3886 if (name == NULL)
3887 return ADA_NOT_RENAMING;
3888
3889 name = strstr (name, "___XR");
3890 if (name == NULL)
3891 return ADA_NOT_RENAMING;
3892 switch (name[5])
3893 {
3894 case '\0':
3895 case '_':
3896 kind = ADA_OBJECT_RENAMING;
3897 break;
3898 case 'E':
3899 kind = ADA_EXCEPTION_RENAMING;
3900 break;
3901 case 'P':
3902 kind = ADA_PACKAGE_RENAMING;
3903 break;
3904 case 'S':
3905 kind = ADA_SUBPROGRAM_RENAMING;
3906 break;
3907 default:
3908 return ADA_NOT_RENAMING;
3909 }
14f9c5c9 3910
aeb5907d
JB
3911 info = TYPE_FIELD_NAME (type, 0);
3912 if (info == NULL)
3913 return ADA_NOT_RENAMING;
3914 if (renamed_entity != NULL)
3915 *renamed_entity = info;
3916 suffix = strstr (info, "___XE");
3917 if (renaming_expr != NULL)
3918 *renaming_expr = suffix + 5;
3919 if (suffix == NULL || suffix == info)
3920 return ADA_NOT_RENAMING;
3921 if (len != NULL)
3922 *len = suffix - info;
3923 return kind;
3924}
52ce6436 3925
14f9c5c9 3926\f
d2e4a39e 3927
4c4b4cd2 3928 /* Evaluation: Function Calls */
14f9c5c9 3929
4c4b4cd2
PH
3930/* Return an lvalue containing the value VAL. This is the identity on
3931 lvalues, and otherwise has the side-effect of pushing a copy of VAL
3932 on the stack, using and updating *SP as the stack pointer, and
42ae5230 3933 returning an lvalue whose value_address points to the copy. */
14f9c5c9 3934
d2e4a39e 3935static struct value *
4a399546 3936ensure_lval (struct value *val, struct gdbarch *gdbarch, CORE_ADDR *sp)
14f9c5c9 3937{
c3e5cd34
PH
3938 if (! VALUE_LVAL (val))
3939 {
df407dfe 3940 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
c3e5cd34
PH
3941
3942 /* The following is taken from the structure-return code in
3943 call_function_by_hand. FIXME: Therefore, some refactoring seems
3944 indicated. */
4a399546 3945 if (gdbarch_inner_than (gdbarch, 1, 2))
c3e5cd34 3946 {
42ae5230 3947 /* Stack grows downward. Align SP and value_address (val) after
c3e5cd34
PH
3948 reserving sufficient space. */
3949 *sp -= len;
4a399546
UW
3950 if (gdbarch_frame_align_p (gdbarch))
3951 *sp = gdbarch_frame_align (gdbarch, *sp);
42ae5230 3952 set_value_address (val, *sp);
c3e5cd34
PH
3953 }
3954 else
3955 {
3956 /* Stack grows upward. Align the frame, allocate space, and
3957 then again, re-align the frame. */
4a399546
UW
3958 if (gdbarch_frame_align_p (gdbarch))
3959 *sp = gdbarch_frame_align (gdbarch, *sp);
42ae5230 3960 set_value_address (val, *sp);
c3e5cd34 3961 *sp += len;
4a399546
UW
3962 if (gdbarch_frame_align_p (gdbarch))
3963 *sp = gdbarch_frame_align (gdbarch, *sp);
c3e5cd34 3964 }
a84a8a0d 3965 VALUE_LVAL (val) = lval_memory;
14f9c5c9 3966
12b795ad 3967 write_memory (value_address (val), value_contents (val), len);
c3e5cd34 3968 }
14f9c5c9
AS
3969
3970 return val;
3971}
3972
3973/* Return the value ACTUAL, converted to be an appropriate value for a
3974 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3975 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 3976 values not residing in memory, updating it as needed. */
14f9c5c9 3977
a93c0eb6
JB
3978struct value *
3979ada_convert_actual (struct value *actual, struct type *formal_type0,
4a399546 3980 struct gdbarch *gdbarch, CORE_ADDR *sp)
14f9c5c9 3981{
df407dfe 3982 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 3983 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e
AS
3984 struct type *formal_target =
3985 TYPE_CODE (formal_type) == TYPE_CODE_PTR
61ee279c 3986 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e
AS
3987 struct type *actual_target =
3988 TYPE_CODE (actual_type) == TYPE_CODE_PTR
61ee279c 3989 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 3990
4c4b4cd2 3991 if (ada_is_array_descriptor_type (formal_target)
14f9c5c9 3992 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
4a399546 3993 return make_array_descriptor (formal_type, actual, gdbarch, sp);
a84a8a0d
JB
3994 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR
3995 || TYPE_CODE (formal_type) == TYPE_CODE_REF)
14f9c5c9 3996 {
a84a8a0d 3997 struct value *result;
5b4ee69b 3998
14f9c5c9 3999 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4c4b4cd2 4000 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4001 result = desc_data (actual);
14f9c5c9 4002 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4c4b4cd2
PH
4003 {
4004 if (VALUE_LVAL (actual) != lval_memory)
4005 {
4006 struct value *val;
5b4ee69b 4007
df407dfe 4008 actual_type = ada_check_typedef (value_type (actual));
4c4b4cd2 4009 val = allocate_value (actual_type);
990a07ab 4010 memcpy ((char *) value_contents_raw (val),
0fd88904 4011 (char *) value_contents (actual),
4c4b4cd2 4012 TYPE_LENGTH (actual_type));
4a399546 4013 actual = ensure_lval (val, gdbarch, sp);
4c4b4cd2 4014 }
a84a8a0d 4015 result = value_addr (actual);
4c4b4cd2 4016 }
a84a8a0d
JB
4017 else
4018 return actual;
4019 return value_cast_pointers (formal_type, result);
14f9c5c9
AS
4020 }
4021 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
4022 return ada_value_ind (actual);
4023
4024 return actual;
4025}
4026
438c98a1
JB
4027/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4028 type TYPE. This is usually an inefficient no-op except on some targets
4029 (such as AVR) where the representation of a pointer and an address
4030 differs. */
4031
4032static CORE_ADDR
4033value_pointer (struct value *value, struct type *type)
4034{
4035 struct gdbarch *gdbarch = get_type_arch (type);
4036 unsigned len = TYPE_LENGTH (type);
4037 gdb_byte *buf = alloca (len);
4038 CORE_ADDR addr;
4039
4040 addr = value_address (value);
4041 gdbarch_address_to_pointer (gdbarch, type, buf, addr);
4042 addr = extract_unsigned_integer (buf, len, gdbarch_byte_order (gdbarch));
4043 return addr;
4044}
4045
14f9c5c9 4046
4c4b4cd2
PH
4047/* Push a descriptor of type TYPE for array value ARR on the stack at
4048 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4049 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4050 to-descriptor type rather than a descriptor type), a struct value *
4051 representing a pointer to this descriptor. */
14f9c5c9 4052
d2e4a39e 4053static struct value *
4a399546
UW
4054make_array_descriptor (struct type *type, struct value *arr,
4055 struct gdbarch *gdbarch, CORE_ADDR *sp)
14f9c5c9 4056{
d2e4a39e
AS
4057 struct type *bounds_type = desc_bounds_type (type);
4058 struct type *desc_type = desc_base_type (type);
4059 struct value *descriptor = allocate_value (desc_type);
4060 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4061 int i;
d2e4a39e 4062
df407dfe 4063 for (i = ada_array_arity (ada_check_typedef (value_type (arr))); i > 0; i -= 1)
14f9c5c9 4064 {
50810684
UW
4065 modify_general_field (value_type (bounds),
4066 value_contents_writeable (bounds),
1eea4ebd 4067 ada_array_bound (arr, i, 0),
4c4b4cd2
PH
4068 desc_bound_bitpos (bounds_type, i, 0),
4069 desc_bound_bitsize (bounds_type, i, 0));
50810684
UW
4070 modify_general_field (value_type (bounds),
4071 value_contents_writeable (bounds),
1eea4ebd 4072 ada_array_bound (arr, i, 1),
4c4b4cd2
PH
4073 desc_bound_bitpos (bounds_type, i, 1),
4074 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4075 }
d2e4a39e 4076
4a399546 4077 bounds = ensure_lval (bounds, gdbarch, sp);
d2e4a39e 4078
50810684
UW
4079 modify_general_field (value_type (descriptor),
4080 value_contents_writeable (descriptor),
438c98a1
JB
4081 value_pointer (ensure_lval (arr, gdbarch, sp),
4082 TYPE_FIELD_TYPE (desc_type, 0)),
76a01679
JB
4083 fat_pntr_data_bitpos (desc_type),
4084 fat_pntr_data_bitsize (desc_type));
4c4b4cd2 4085
50810684
UW
4086 modify_general_field (value_type (descriptor),
4087 value_contents_writeable (descriptor),
438c98a1
JB
4088 value_pointer (bounds,
4089 TYPE_FIELD_TYPE (desc_type, 1)),
4c4b4cd2
PH
4090 fat_pntr_bounds_bitpos (desc_type),
4091 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4092
4a399546 4093 descriptor = ensure_lval (descriptor, gdbarch, sp);
14f9c5c9
AS
4094
4095 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4096 return value_addr (descriptor);
4097 else
4098 return descriptor;
4099}
14f9c5c9 4100\f
963a6417
PH
4101/* Dummy definitions for an experimental caching module that is not
4102 * used in the public sources. */
96d887e8 4103
96d887e8
PH
4104static int
4105lookup_cached_symbol (const char *name, domain_enum namespace,
2570f2b7 4106 struct symbol **sym, struct block **block)
96d887e8
PH
4107{
4108 return 0;
4109}
4110
4111static void
4112cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
2570f2b7 4113 struct block *block)
96d887e8
PH
4114{
4115}
4c4b4cd2
PH
4116\f
4117 /* Symbol Lookup */
4118
4119/* Return the result of a standard (literal, C-like) lookup of NAME in
4120 given DOMAIN, visible from lexical block BLOCK. */
4121
4122static struct symbol *
4123standard_lookup (const char *name, const struct block *block,
4124 domain_enum domain)
4125{
4126 struct symbol *sym;
4c4b4cd2 4127
2570f2b7 4128 if (lookup_cached_symbol (name, domain, &sym, NULL))
4c4b4cd2 4129 return sym;
2570f2b7
UW
4130 sym = lookup_symbol_in_language (name, block, domain, language_c, 0);
4131 cache_symbol (name, domain, sym, block_found);
4c4b4cd2
PH
4132 return sym;
4133}
4134
4135
4136/* Non-zero iff there is at least one non-function/non-enumeral symbol
4137 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
4138 since they contend in overloading in the same way. */
4139static int
4140is_nonfunction (struct ada_symbol_info syms[], int n)
4141{
4142 int i;
4143
4144 for (i = 0; i < n; i += 1)
4145 if (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_FUNC
4146 && (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_ENUM
4147 || SYMBOL_CLASS (syms[i].sym) != LOC_CONST))
14f9c5c9
AS
4148 return 1;
4149
4150 return 0;
4151}
4152
4153/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4154 struct types. Otherwise, they may not. */
14f9c5c9
AS
4155
4156static int
d2e4a39e 4157equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4158{
d2e4a39e 4159 if (type0 == type1)
14f9c5c9 4160 return 1;
d2e4a39e 4161 if (type0 == NULL || type1 == NULL
14f9c5c9
AS
4162 || TYPE_CODE (type0) != TYPE_CODE (type1))
4163 return 0;
d2e4a39e 4164 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
14f9c5c9
AS
4165 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
4166 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4167 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4168 return 1;
d2e4a39e 4169
14f9c5c9
AS
4170 return 0;
4171}
4172
4173/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4174 no more defined than that of SYM1. */
14f9c5c9
AS
4175
4176static int
d2e4a39e 4177lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4178{
4179 if (sym0 == sym1)
4180 return 1;
176620f1 4181 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
4182 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4183 return 0;
4184
d2e4a39e 4185 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
4186 {
4187 case LOC_UNDEF:
4188 return 1;
4189 case LOC_TYPEDEF:
4190 {
4c4b4cd2
PH
4191 struct type *type0 = SYMBOL_TYPE (sym0);
4192 struct type *type1 = SYMBOL_TYPE (sym1);
4193 char *name0 = SYMBOL_LINKAGE_NAME (sym0);
4194 char *name1 = SYMBOL_LINKAGE_NAME (sym1);
4195 int len0 = strlen (name0);
5b4ee69b 4196
4c4b4cd2
PH
4197 return
4198 TYPE_CODE (type0) == TYPE_CODE (type1)
4199 && (equiv_types (type0, type1)
4200 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4201 && strncmp (name1 + len0, "___XV", 5) == 0));
14f9c5c9
AS
4202 }
4203 case LOC_CONST:
4204 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4c4b4cd2 4205 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
d2e4a39e
AS
4206 default:
4207 return 0;
14f9c5c9
AS
4208 }
4209}
4210
4c4b4cd2
PH
4211/* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct ada_symbol_info
4212 records in OBSTACKP. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4213
4214static void
76a01679
JB
4215add_defn_to_vec (struct obstack *obstackp,
4216 struct symbol *sym,
2570f2b7 4217 struct block *block)
14f9c5c9
AS
4218{
4219 int i;
4c4b4cd2 4220 struct ada_symbol_info *prevDefns = defns_collected (obstackp, 0);
14f9c5c9 4221
529cad9c
PH
4222 /* Do not try to complete stub types, as the debugger is probably
4223 already scanning all symbols matching a certain name at the
4224 time when this function is called. Trying to replace the stub
4225 type by its associated full type will cause us to restart a scan
4226 which may lead to an infinite recursion. Instead, the client
4227 collecting the matching symbols will end up collecting several
4228 matches, with at least one of them complete. It can then filter
4229 out the stub ones if needed. */
4230
4c4b4cd2
PH
4231 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
4232 {
4233 if (lesseq_defined_than (sym, prevDefns[i].sym))
4234 return;
4235 else if (lesseq_defined_than (prevDefns[i].sym, sym))
4236 {
4237 prevDefns[i].sym = sym;
4238 prevDefns[i].block = block;
4c4b4cd2 4239 return;
76a01679 4240 }
4c4b4cd2
PH
4241 }
4242
4243 {
4244 struct ada_symbol_info info;
4245
4246 info.sym = sym;
4247 info.block = block;
4c4b4cd2
PH
4248 obstack_grow (obstackp, &info, sizeof (struct ada_symbol_info));
4249 }
4250}
4251
4252/* Number of ada_symbol_info structures currently collected in
4253 current vector in *OBSTACKP. */
4254
76a01679
JB
4255static int
4256num_defns_collected (struct obstack *obstackp)
4c4b4cd2
PH
4257{
4258 return obstack_object_size (obstackp) / sizeof (struct ada_symbol_info);
4259}
4260
4261/* Vector of ada_symbol_info structures currently collected in current
4262 vector in *OBSTACKP. If FINISH, close off the vector and return
4263 its final address. */
4264
76a01679 4265static struct ada_symbol_info *
4c4b4cd2
PH
4266defns_collected (struct obstack *obstackp, int finish)
4267{
4268 if (finish)
4269 return obstack_finish (obstackp);
4270 else
4271 return (struct ada_symbol_info *) obstack_base (obstackp);
4272}
4273
96d887e8
PH
4274/* Return a minimal symbol matching NAME according to Ada decoding
4275 rules. Returns NULL if there is no such minimal symbol. Names
4276 prefixed with "standard__" are handled specially: "standard__" is
4277 first stripped off, and only static and global symbols are searched. */
4c4b4cd2 4278
96d887e8
PH
4279struct minimal_symbol *
4280ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4281{
4c4b4cd2 4282 struct objfile *objfile;
96d887e8
PH
4283 struct minimal_symbol *msymbol;
4284 int wild_match;
4c4b4cd2 4285
96d887e8 4286 if (strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
4c4b4cd2 4287 {
96d887e8 4288 name += sizeof ("standard__") - 1;
4c4b4cd2 4289 wild_match = 0;
4c4b4cd2
PH
4290 }
4291 else
96d887e8 4292 wild_match = (strstr (name, "__") == NULL);
4c4b4cd2 4293
96d887e8
PH
4294 ALL_MSYMBOLS (objfile, msymbol)
4295 {
4296 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match)
4297 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4298 return msymbol;
4299 }
4c4b4cd2 4300
96d887e8
PH
4301 return NULL;
4302}
4c4b4cd2 4303
96d887e8
PH
4304/* For all subprograms that statically enclose the subprogram of the
4305 selected frame, add symbols matching identifier NAME in DOMAIN
4306 and their blocks to the list of data in OBSTACKP, as for
4307 ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
4308 wildcard prefix. */
4c4b4cd2 4309
96d887e8
PH
4310static void
4311add_symbols_from_enclosing_procs (struct obstack *obstackp,
76a01679 4312 const char *name, domain_enum namespace,
96d887e8
PH
4313 int wild_match)
4314{
96d887e8 4315}
14f9c5c9 4316
96d887e8
PH
4317/* True if TYPE is definitely an artificial type supplied to a symbol
4318 for which no debugging information was given in the symbol file. */
14f9c5c9 4319
96d887e8
PH
4320static int
4321is_nondebugging_type (struct type *type)
4322{
4323 char *name = ada_type_name (type);
5b4ee69b 4324
96d887e8
PH
4325 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4326}
4c4b4cd2 4327
96d887e8
PH
4328/* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4329 duplicate other symbols in the list (The only case I know of where
4330 this happens is when object files containing stabs-in-ecoff are
4331 linked with files containing ordinary ecoff debugging symbols (or no
4332 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4333 Returns the number of items in the modified list. */
4c4b4cd2 4334
96d887e8
PH
4335static int
4336remove_extra_symbols (struct ada_symbol_info *syms, int nsyms)
4337{
4338 int i, j;
4c4b4cd2 4339
96d887e8
PH
4340 i = 0;
4341 while (i < nsyms)
4342 {
339c13b6
JB
4343 int remove = 0;
4344
4345 /* If two symbols have the same name and one of them is a stub type,
4346 the get rid of the stub. */
4347
4348 if (TYPE_STUB (SYMBOL_TYPE (syms[i].sym))
4349 && SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL)
4350 {
4351 for (j = 0; j < nsyms; j++)
4352 {
4353 if (j != i
4354 && !TYPE_STUB (SYMBOL_TYPE (syms[j].sym))
4355 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4356 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
4357 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0)
4358 remove = 1;
4359 }
4360 }
4361
4362 /* Two symbols with the same name, same class and same address
4363 should be identical. */
4364
4365 else if (SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL
96d887e8
PH
4366 && SYMBOL_CLASS (syms[i].sym) == LOC_STATIC
4367 && is_nondebugging_type (SYMBOL_TYPE (syms[i].sym)))
4368 {
4369 for (j = 0; j < nsyms; j += 1)
4370 {
4371 if (i != j
4372 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4373 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
76a01679 4374 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0
96d887e8
PH
4375 && SYMBOL_CLASS (syms[i].sym) == SYMBOL_CLASS (syms[j].sym)
4376 && SYMBOL_VALUE_ADDRESS (syms[i].sym)
4377 == SYMBOL_VALUE_ADDRESS (syms[j].sym))
339c13b6 4378 remove = 1;
4c4b4cd2 4379 }
4c4b4cd2 4380 }
339c13b6
JB
4381
4382 if (remove)
4383 {
4384 for (j = i + 1; j < nsyms; j += 1)
4385 syms[j - 1] = syms[j];
4386 nsyms -= 1;
4387 }
4388
96d887e8 4389 i += 1;
14f9c5c9 4390 }
96d887e8 4391 return nsyms;
14f9c5c9
AS
4392}
4393
96d887e8
PH
4394/* Given a type that corresponds to a renaming entity, use the type name
4395 to extract the scope (package name or function name, fully qualified,
4396 and following the GNAT encoding convention) where this renaming has been
4397 defined. The string returned needs to be deallocated after use. */
4c4b4cd2 4398
96d887e8
PH
4399static char *
4400xget_renaming_scope (struct type *renaming_type)
14f9c5c9 4401{
96d887e8
PH
4402 /* The renaming types adhere to the following convention:
4403 <scope>__<rename>___<XR extension>.
4404 So, to extract the scope, we search for the "___XR" extension,
4405 and then backtrack until we find the first "__". */
76a01679 4406
96d887e8
PH
4407 const char *name = type_name_no_tag (renaming_type);
4408 char *suffix = strstr (name, "___XR");
4409 char *last;
4410 int scope_len;
4411 char *scope;
14f9c5c9 4412
96d887e8
PH
4413 /* Now, backtrack a bit until we find the first "__". Start looking
4414 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 4415
96d887e8
PH
4416 for (last = suffix - 3; last > name; last--)
4417 if (last[0] == '_' && last[1] == '_')
4418 break;
76a01679 4419
96d887e8 4420 /* Make a copy of scope and return it. */
14f9c5c9 4421
96d887e8
PH
4422 scope_len = last - name;
4423 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
14f9c5c9 4424
96d887e8
PH
4425 strncpy (scope, name, scope_len);
4426 scope[scope_len] = '\0';
4c4b4cd2 4427
96d887e8 4428 return scope;
4c4b4cd2
PH
4429}
4430
96d887e8 4431/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 4432
96d887e8
PH
4433static int
4434is_package_name (const char *name)
4c4b4cd2 4435{
96d887e8
PH
4436 /* Here, We take advantage of the fact that no symbols are generated
4437 for packages, while symbols are generated for each function.
4438 So the condition for NAME represent a package becomes equivalent
4439 to NAME not existing in our list of symbols. There is only one
4440 small complication with library-level functions (see below). */
4c4b4cd2 4441
96d887e8 4442 char *fun_name;
76a01679 4443
96d887e8
PH
4444 /* If it is a function that has not been defined at library level,
4445 then we should be able to look it up in the symbols. */
4446 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4447 return 0;
14f9c5c9 4448
96d887e8
PH
4449 /* Library-level function names start with "_ada_". See if function
4450 "_ada_" followed by NAME can be found. */
14f9c5c9 4451
96d887e8 4452 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 4453 functions names cannot contain "__" in them. */
96d887e8
PH
4454 if (strstr (name, "__") != NULL)
4455 return 0;
4c4b4cd2 4456
b435e160 4457 fun_name = xstrprintf ("_ada_%s", name);
14f9c5c9 4458
96d887e8
PH
4459 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
4460}
14f9c5c9 4461
96d887e8 4462/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 4463 not visible from FUNCTION_NAME. */
14f9c5c9 4464
96d887e8 4465static int
aeb5907d 4466old_renaming_is_invisible (const struct symbol *sym, char *function_name)
96d887e8 4467{
aeb5907d
JB
4468 char *scope;
4469
4470 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
4471 return 0;
4472
4473 scope = xget_renaming_scope (SYMBOL_TYPE (sym));
d2e4a39e 4474
96d887e8 4475 make_cleanup (xfree, scope);
14f9c5c9 4476
96d887e8
PH
4477 /* If the rename has been defined in a package, then it is visible. */
4478 if (is_package_name (scope))
aeb5907d 4479 return 0;
14f9c5c9 4480
96d887e8
PH
4481 /* Check that the rename is in the current function scope by checking
4482 that its name starts with SCOPE. */
76a01679 4483
96d887e8
PH
4484 /* If the function name starts with "_ada_", it means that it is
4485 a library-level function. Strip this prefix before doing the
4486 comparison, as the encoding for the renaming does not contain
4487 this prefix. */
4488 if (strncmp (function_name, "_ada_", 5) == 0)
4489 function_name += 5;
f26caa11 4490
aeb5907d 4491 return (strncmp (function_name, scope, strlen (scope)) != 0);
f26caa11
PH
4492}
4493
aeb5907d
JB
4494/* Remove entries from SYMS that corresponds to a renaming entity that
4495 is not visible from the function associated with CURRENT_BLOCK or
4496 that is superfluous due to the presence of more specific renaming
4497 information. Places surviving symbols in the initial entries of
4498 SYMS and returns the number of surviving symbols.
96d887e8
PH
4499
4500 Rationale:
aeb5907d
JB
4501 First, in cases where an object renaming is implemented as a
4502 reference variable, GNAT may produce both the actual reference
4503 variable and the renaming encoding. In this case, we discard the
4504 latter.
4505
4506 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
4507 entity. Unfortunately, STABS currently does not support the definition
4508 of types that are local to a given lexical block, so all renamings types
4509 are emitted at library level. As a consequence, if an application
4510 contains two renaming entities using the same name, and a user tries to
4511 print the value of one of these entities, the result of the ada symbol
4512 lookup will also contain the wrong renaming type.
f26caa11 4513
96d887e8
PH
4514 This function partially covers for this limitation by attempting to
4515 remove from the SYMS list renaming symbols that should be visible
4516 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4517 method with the current information available. The implementation
4518 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4519
4520 - When the user tries to print a rename in a function while there
4521 is another rename entity defined in a package: Normally, the
4522 rename in the function has precedence over the rename in the
4523 package, so the latter should be removed from the list. This is
4524 currently not the case.
4525
4526 - This function will incorrectly remove valid renames if
4527 the CURRENT_BLOCK corresponds to a function which symbol name
4528 has been changed by an "Export" pragma. As a consequence,
4529 the user will be unable to print such rename entities. */
4c4b4cd2 4530
14f9c5c9 4531static int
aeb5907d
JB
4532remove_irrelevant_renamings (struct ada_symbol_info *syms,
4533 int nsyms, const struct block *current_block)
4c4b4cd2
PH
4534{
4535 struct symbol *current_function;
4536 char *current_function_name;
4537 int i;
aeb5907d
JB
4538 int is_new_style_renaming;
4539
4540 /* If there is both a renaming foo___XR... encoded as a variable and
4541 a simple variable foo in the same block, discard the latter.
4542 First, zero out such symbols, then compress. */
4543 is_new_style_renaming = 0;
4544 for (i = 0; i < nsyms; i += 1)
4545 {
4546 struct symbol *sym = syms[i].sym;
4547 struct block *block = syms[i].block;
4548 const char *name;
4549 const char *suffix;
4550
4551 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4552 continue;
4553 name = SYMBOL_LINKAGE_NAME (sym);
4554 suffix = strstr (name, "___XR");
4555
4556 if (suffix != NULL)
4557 {
4558 int name_len = suffix - name;
4559 int j;
5b4ee69b 4560
aeb5907d
JB
4561 is_new_style_renaming = 1;
4562 for (j = 0; j < nsyms; j += 1)
4563 if (i != j && syms[j].sym != NULL
4564 && strncmp (name, SYMBOL_LINKAGE_NAME (syms[j].sym),
4565 name_len) == 0
4566 && block == syms[j].block)
4567 syms[j].sym = NULL;
4568 }
4569 }
4570 if (is_new_style_renaming)
4571 {
4572 int j, k;
4573
4574 for (j = k = 0; j < nsyms; j += 1)
4575 if (syms[j].sym != NULL)
4576 {
4577 syms[k] = syms[j];
4578 k += 1;
4579 }
4580 return k;
4581 }
4c4b4cd2
PH
4582
4583 /* Extract the function name associated to CURRENT_BLOCK.
4584 Abort if unable to do so. */
76a01679 4585
4c4b4cd2
PH
4586 if (current_block == NULL)
4587 return nsyms;
76a01679 4588
7f0df278 4589 current_function = block_linkage_function (current_block);
4c4b4cd2
PH
4590 if (current_function == NULL)
4591 return nsyms;
4592
4593 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
4594 if (current_function_name == NULL)
4595 return nsyms;
4596
4597 /* Check each of the symbols, and remove it from the list if it is
4598 a type corresponding to a renaming that is out of the scope of
4599 the current block. */
4600
4601 i = 0;
4602 while (i < nsyms)
4603 {
aeb5907d
JB
4604 if (ada_parse_renaming (syms[i].sym, NULL, NULL, NULL)
4605 == ADA_OBJECT_RENAMING
4606 && old_renaming_is_invisible (syms[i].sym, current_function_name))
4c4b4cd2
PH
4607 {
4608 int j;
5b4ee69b 4609
aeb5907d 4610 for (j = i + 1; j < nsyms; j += 1)
76a01679 4611 syms[j - 1] = syms[j];
4c4b4cd2
PH
4612 nsyms -= 1;
4613 }
4614 else
4615 i += 1;
4616 }
4617
4618 return nsyms;
4619}
4620
339c13b6
JB
4621/* Add to OBSTACKP all symbols from BLOCK (and its super-blocks)
4622 whose name and domain match NAME and DOMAIN respectively.
4623 If no match was found, then extend the search to "enclosing"
4624 routines (in other words, if we're inside a nested function,
4625 search the symbols defined inside the enclosing functions).
4626
4627 Note: This function assumes that OBSTACKP has 0 (zero) element in it. */
4628
4629static void
4630ada_add_local_symbols (struct obstack *obstackp, const char *name,
4631 struct block *block, domain_enum domain,
4632 int wild_match)
4633{
4634 int block_depth = 0;
4635
4636 while (block != NULL)
4637 {
4638 block_depth += 1;
4639 ada_add_block_symbols (obstackp, block, name, domain, NULL, wild_match);
4640
4641 /* If we found a non-function match, assume that's the one. */
4642 if (is_nonfunction (defns_collected (obstackp, 0),
4643 num_defns_collected (obstackp)))
4644 return;
4645
4646 block = BLOCK_SUPERBLOCK (block);
4647 }
4648
4649 /* If no luck so far, try to find NAME as a local symbol in some lexically
4650 enclosing subprogram. */
4651 if (num_defns_collected (obstackp) == 0 && block_depth > 2)
4652 add_symbols_from_enclosing_procs (obstackp, name, domain, wild_match);
4653}
4654
ccefe4c4
TT
4655/* An object of this type is used as the user_data argument when
4656 calling the map_ada_symtabs method. */
4657
4658struct ada_psym_data
4659{
4660 struct obstack *obstackp;
4661 const char *name;
4662 domain_enum domain;
4663 int global;
4664 int wild_match;
4665};
4666
4667/* Callback function for map_ada_symtabs. */
4668
4669static void
4670ada_add_psyms (struct objfile *objfile, struct symtab *s, void *user_data)
4671{
4672 struct ada_psym_data *data = user_data;
4673 const int block_kind = data->global ? GLOBAL_BLOCK : STATIC_BLOCK;
5b4ee69b 4674
ccefe4c4
TT
4675 ada_add_block_symbols (data->obstackp,
4676 BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), block_kind),
4677 data->name, data->domain, objfile, data->wild_match);
4678}
4679
339c13b6
JB
4680/* Add to OBSTACKP all non-local symbols whose name and domain match
4681 NAME and DOMAIN respectively. The search is performed on GLOBAL_BLOCK
4682 symbols if GLOBAL is non-zero, or on STATIC_BLOCK symbols otherwise. */
4683
4684static void
4685ada_add_non_local_symbols (struct obstack *obstackp, const char *name,
4686 domain_enum domain, int global,
ccefe4c4 4687 int is_wild_match)
339c13b6
JB
4688{
4689 struct objfile *objfile;
ccefe4c4 4690 struct ada_psym_data data;
339c13b6 4691
ccefe4c4
TT
4692 data.obstackp = obstackp;
4693 data.name = name;
4694 data.domain = domain;
4695 data.global = global;
4696 data.wild_match = is_wild_match;
339c13b6 4697
ccefe4c4
TT
4698 ALL_OBJFILES (objfile)
4699 {
4700 if (objfile->sf)
4701 objfile->sf->qf->map_ada_symtabs (objfile, wild_match, is_name_suffix,
4702 ada_add_psyms, name,
4703 global, domain,
4704 is_wild_match, &data);
339c13b6
JB
4705 }
4706}
4707
4c4b4cd2
PH
4708/* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing
4709 scope and in global scopes, returning the number of matches. Sets
6c9353d3 4710 *RESULTS to point to a vector of (SYM,BLOCK) tuples,
4c4b4cd2
PH
4711 indicating the symbols found and the blocks and symbol tables (if
4712 any) in which they were found. This vector are transient---good only to
4713 the next call of ada_lookup_symbol_list. Any non-function/non-enumeral
4714 symbol match within the nest of blocks whose innermost member is BLOCK0,
4715 is the one match returned (no other matches in that or
4716 enclosing blocks is returned). If there are any matches in or
4717 surrounding BLOCK0, then these alone are returned. Otherwise, the
4718 search extends to global and file-scope (static) symbol tables.
4719 Names prefixed with "standard__" are handled specially: "standard__"
4720 is first stripped off, and only static and global symbols are searched. */
14f9c5c9
AS
4721
4722int
4c4b4cd2 4723ada_lookup_symbol_list (const char *name0, const struct block *block0,
76a01679
JB
4724 domain_enum namespace,
4725 struct ada_symbol_info **results)
14f9c5c9
AS
4726{
4727 struct symbol *sym;
14f9c5c9 4728 struct block *block;
4c4b4cd2 4729 const char *name;
4c4b4cd2 4730 int wild_match;
14f9c5c9 4731 int cacheIfUnique;
4c4b4cd2 4732 int ndefns;
14f9c5c9 4733
4c4b4cd2
PH
4734 obstack_free (&symbol_list_obstack, NULL);
4735 obstack_init (&symbol_list_obstack);
14f9c5c9 4736
14f9c5c9
AS
4737 cacheIfUnique = 0;
4738
4739 /* Search specified block and its superiors. */
4740
4c4b4cd2
PH
4741 wild_match = (strstr (name0, "__") == NULL);
4742 name = name0;
76a01679
JB
4743 block = (struct block *) block0; /* FIXME: No cast ought to be
4744 needed, but adding const will
4745 have a cascade effect. */
339c13b6
JB
4746
4747 /* Special case: If the user specifies a symbol name inside package
4748 Standard, do a non-wild matching of the symbol name without
4749 the "standard__" prefix. This was primarily introduced in order
4750 to allow the user to specifically access the standard exceptions
4751 using, for instance, Standard.Constraint_Error when Constraint_Error
4752 is ambiguous (due to the user defining its own Constraint_Error
4753 entity inside its program). */
4c4b4cd2
PH
4754 if (strncmp (name0, "standard__", sizeof ("standard__") - 1) == 0)
4755 {
4756 wild_match = 0;
4757 block = NULL;
4758 name = name0 + sizeof ("standard__") - 1;
4759 }
4760
339c13b6 4761 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 4762
339c13b6
JB
4763 ada_add_local_symbols (&symbol_list_obstack, name, block, namespace,
4764 wild_match);
4c4b4cd2 4765 if (num_defns_collected (&symbol_list_obstack) > 0)
14f9c5c9 4766 goto done;
d2e4a39e 4767
339c13b6
JB
4768 /* No non-global symbols found. Check our cache to see if we have
4769 already performed this search before. If we have, then return
4770 the same result. */
4771
14f9c5c9 4772 cacheIfUnique = 1;
2570f2b7 4773 if (lookup_cached_symbol (name0, namespace, &sym, &block))
4c4b4cd2
PH
4774 {
4775 if (sym != NULL)
2570f2b7 4776 add_defn_to_vec (&symbol_list_obstack, sym, block);
4c4b4cd2
PH
4777 goto done;
4778 }
14f9c5c9 4779
339c13b6
JB
4780 /* Search symbols from all global blocks. */
4781
4782 ada_add_non_local_symbols (&symbol_list_obstack, name, namespace, 1,
4783 wild_match);
d2e4a39e 4784
4c4b4cd2 4785 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 4786 (not strictly correct, but perhaps better than an error). */
d2e4a39e 4787
4c4b4cd2 4788 if (num_defns_collected (&symbol_list_obstack) == 0)
339c13b6
JB
4789 ada_add_non_local_symbols (&symbol_list_obstack, name, namespace, 0,
4790 wild_match);
14f9c5c9 4791
4c4b4cd2
PH
4792done:
4793 ndefns = num_defns_collected (&symbol_list_obstack);
4794 *results = defns_collected (&symbol_list_obstack, 1);
4795
4796 ndefns = remove_extra_symbols (*results, ndefns);
4797
d2e4a39e 4798 if (ndefns == 0)
2570f2b7 4799 cache_symbol (name0, namespace, NULL, NULL);
14f9c5c9 4800
4c4b4cd2 4801 if (ndefns == 1 && cacheIfUnique)
2570f2b7 4802 cache_symbol (name0, namespace, (*results)[0].sym, (*results)[0].block);
14f9c5c9 4803
aeb5907d 4804 ndefns = remove_irrelevant_renamings (*results, ndefns, block0);
14f9c5c9 4805
14f9c5c9
AS
4806 return ndefns;
4807}
4808
d2e4a39e 4809struct symbol *
aeb5907d 4810ada_lookup_encoded_symbol (const char *name, const struct block *block0,
21b556f4 4811 domain_enum namespace, struct block **block_found)
14f9c5c9 4812{
4c4b4cd2 4813 struct ada_symbol_info *candidates;
14f9c5c9
AS
4814 int n_candidates;
4815
aeb5907d 4816 n_candidates = ada_lookup_symbol_list (name, block0, namespace, &candidates);
14f9c5c9
AS
4817
4818 if (n_candidates == 0)
4819 return NULL;
4c4b4cd2 4820
aeb5907d
JB
4821 if (block_found != NULL)
4822 *block_found = candidates[0].block;
4c4b4cd2 4823
21b556f4 4824 return fixup_symbol_section (candidates[0].sym, NULL);
aeb5907d
JB
4825}
4826
4827/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
4828 scope and in global scopes, or NULL if none. NAME is folded and
4829 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
4830 choosing the first symbol if there are multiple choices.
4831 *IS_A_FIELD_OF_THIS is set to 0 and *SYMTAB is set to the symbol
4832 table in which the symbol was found (in both cases, these
4833 assignments occur only if the pointers are non-null). */
4834struct symbol *
4835ada_lookup_symbol (const char *name, const struct block *block0,
21b556f4 4836 domain_enum namespace, int *is_a_field_of_this)
aeb5907d
JB
4837{
4838 if (is_a_field_of_this != NULL)
4839 *is_a_field_of_this = 0;
4840
4841 return
4842 ada_lookup_encoded_symbol (ada_encode (ada_fold_name (name)),
21b556f4 4843 block0, namespace, NULL);
4c4b4cd2 4844}
14f9c5c9 4845
4c4b4cd2
PH
4846static struct symbol *
4847ada_lookup_symbol_nonlocal (const char *name,
76a01679 4848 const struct block *block,
21b556f4 4849 const domain_enum domain)
4c4b4cd2 4850{
94af9270 4851 return ada_lookup_symbol (name, block_static_block (block), domain, NULL);
14f9c5c9
AS
4852}
4853
4854
4c4b4cd2
PH
4855/* True iff STR is a possible encoded suffix of a normal Ada name
4856 that is to be ignored for matching purposes. Suffixes of parallel
4857 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 4858 are given by any of the regular expressions:
4c4b4cd2 4859
babe1480
JB
4860 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
4861 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
4862 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 4863 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
4864
4865 Also, any leading "__[0-9]+" sequence is skipped before the suffix
4866 match is performed. This sequence is used to differentiate homonyms,
4867 is an optional part of a valid name suffix. */
4c4b4cd2 4868
14f9c5c9 4869static int
d2e4a39e 4870is_name_suffix (const char *str)
14f9c5c9
AS
4871{
4872 int k;
4c4b4cd2
PH
4873 const char *matching;
4874 const int len = strlen (str);
4875
babe1480
JB
4876 /* Skip optional leading __[0-9]+. */
4877
4c4b4cd2
PH
4878 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
4879 {
babe1480
JB
4880 str += 3;
4881 while (isdigit (str[0]))
4882 str += 1;
4c4b4cd2 4883 }
babe1480
JB
4884
4885 /* [.$][0-9]+ */
4c4b4cd2 4886
babe1480 4887 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 4888 {
babe1480 4889 matching = str + 1;
4c4b4cd2
PH
4890 while (isdigit (matching[0]))
4891 matching += 1;
4892 if (matching[0] == '\0')
4893 return 1;
4894 }
4895
4896 /* ___[0-9]+ */
babe1480 4897
4c4b4cd2
PH
4898 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
4899 {
4900 matching = str + 3;
4901 while (isdigit (matching[0]))
4902 matching += 1;
4903 if (matching[0] == '\0')
4904 return 1;
4905 }
4906
529cad9c
PH
4907#if 0
4908 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
4909 with a N at the end. Unfortunately, the compiler uses the same
4910 convention for other internal types it creates. So treating
4911 all entity names that end with an "N" as a name suffix causes
4912 some regressions. For instance, consider the case of an enumerated
4913 type. To support the 'Image attribute, it creates an array whose
4914 name ends with N.
4915 Having a single character like this as a suffix carrying some
4916 information is a bit risky. Perhaps we should change the encoding
4917 to be something like "_N" instead. In the meantime, do not do
4918 the following check. */
4919 /* Protected Object Subprograms */
4920 if (len == 1 && str [0] == 'N')
4921 return 1;
4922#endif
4923
4924 /* _E[0-9]+[bs]$ */
4925 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
4926 {
4927 matching = str + 3;
4928 while (isdigit (matching[0]))
4929 matching += 1;
4930 if ((matching[0] == 'b' || matching[0] == 's')
4931 && matching [1] == '\0')
4932 return 1;
4933 }
4934
4c4b4cd2
PH
4935 /* ??? We should not modify STR directly, as we are doing below. This
4936 is fine in this case, but may become problematic later if we find
4937 that this alternative did not work, and want to try matching
4938 another one from the begining of STR. Since we modified it, we
4939 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
4940 if (str[0] == 'X')
4941 {
4942 str += 1;
d2e4a39e 4943 while (str[0] != '_' && str[0] != '\0')
4c4b4cd2
PH
4944 {
4945 if (str[0] != 'n' && str[0] != 'b')
4946 return 0;
4947 str += 1;
4948 }
14f9c5c9 4949 }
babe1480 4950
14f9c5c9
AS
4951 if (str[0] == '\000')
4952 return 1;
babe1480 4953
d2e4a39e 4954 if (str[0] == '_')
14f9c5c9
AS
4955 {
4956 if (str[1] != '_' || str[2] == '\000')
4c4b4cd2 4957 return 0;
d2e4a39e 4958 if (str[2] == '_')
4c4b4cd2 4959 {
61ee279c
PH
4960 if (strcmp (str + 3, "JM") == 0)
4961 return 1;
4962 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
4963 the LJM suffix in favor of the JM one. But we will
4964 still accept LJM as a valid suffix for a reasonable
4965 amount of time, just to allow ourselves to debug programs
4966 compiled using an older version of GNAT. */
4c4b4cd2
PH
4967 if (strcmp (str + 3, "LJM") == 0)
4968 return 1;
4969 if (str[3] != 'X')
4970 return 0;
1265e4aa
JB
4971 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
4972 || str[4] == 'U' || str[4] == 'P')
4c4b4cd2
PH
4973 return 1;
4974 if (str[4] == 'R' && str[5] != 'T')
4975 return 1;
4976 return 0;
4977 }
4978 if (!isdigit (str[2]))
4979 return 0;
4980 for (k = 3; str[k] != '\0'; k += 1)
4981 if (!isdigit (str[k]) && str[k] != '_')
4982 return 0;
14f9c5c9
AS
4983 return 1;
4984 }
4c4b4cd2 4985 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 4986 {
4c4b4cd2
PH
4987 for (k = 2; str[k] != '\0'; k += 1)
4988 if (!isdigit (str[k]) && str[k] != '_')
4989 return 0;
14f9c5c9
AS
4990 return 1;
4991 }
4992 return 0;
4993}
d2e4a39e 4994
aeb5907d
JB
4995/* Return non-zero if the string starting at NAME and ending before
4996 NAME_END contains no capital letters. */
529cad9c
PH
4997
4998static int
4999is_valid_name_for_wild_match (const char *name0)
5000{
5001 const char *decoded_name = ada_decode (name0);
5002 int i;
5003
5823c3ef
JB
5004 /* If the decoded name starts with an angle bracket, it means that
5005 NAME0 does not follow the GNAT encoding format. It should then
5006 not be allowed as a possible wild match. */
5007 if (decoded_name[0] == '<')
5008 return 0;
5009
529cad9c
PH
5010 for (i=0; decoded_name[i] != '\0'; i++)
5011 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5012 return 0;
5013
5014 return 1;
5015}
5016
73589123
PH
5017/* Advance *NAMEP to next occurrence of TARGET0 in the string NAME0
5018 that could start a simple name. Assumes that *NAMEP points into
5019 the string beginning at NAME0. */
4c4b4cd2 5020
14f9c5c9 5021static int
73589123 5022advance_wild_match (const char **namep, const char *name0, int target0)
14f9c5c9 5023{
73589123 5024 const char *name = *namep;
5b4ee69b 5025
5823c3ef 5026 while (1)
14f9c5c9 5027 {
73589123
PH
5028 int t0, t1, t2;
5029
5030 t0 = *name;
5031 if (t0 == '_')
5032 {
5033 t1 = name[1];
5034 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5035 {
5036 name += 1;
5037 if (name == name0 + 5 && strncmp (name0, "_ada", 4) == 0)
5038 break;
5039 else
5040 name += 1;
5041 }
5042 else if (t1 == '_' &&
5043 (((t2 = name[2]) >= 'a' && t2 <= 'z') || t2 == target0))
5044 {
5045 name += 2;
5046 break;
5047 }
5048 else
5049 return 0;
5050 }
5051 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5052 name += 1;
5053 else
5823c3ef 5054 return 0;
73589123
PH
5055 }
5056
5057 *namep = name;
5058 return 1;
5059}
5060
5061/* Return 0 iff NAME encodes a name of the form prefix.PATN. Ignores any
5062 informational suffixes of NAME (i.e., for which is_name_suffix is
5063 true). Assumes that PATN is a lower-cased Ada simple name. */
5064
5065static int
5066wild_match (const char *name, const char *patn)
5067{
5068 const char *p, *n;
5069 const char *name0 = name;
5070
5071 while (1)
5072 {
5073 const char *match = name;
5074
5075 if (*name == *patn)
5076 {
5077 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
5078 if (*p != *name)
5079 break;
5080 if (*p == '\0' && is_name_suffix (name))
5081 return match != name0 && !is_valid_name_for_wild_match (name0);
5082
5083 if (name[-1] == '_')
5084 name -= 1;
5085 }
5086 if (!advance_wild_match (&name, name0, *patn))
5087 return 1;
96d887e8 5088 }
96d887e8
PH
5089}
5090
96d887e8
PH
5091/* Add symbols from BLOCK matching identifier NAME in DOMAIN to
5092 vector *defn_symbols, updating the list of symbols in OBSTACKP
5093 (if necessary). If WILD, treat as NAME with a wildcard prefix.
5094 OBJFILE is the section containing BLOCK.
5095 SYMTAB is recorded with each symbol added. */
5096
5097static void
5098ada_add_block_symbols (struct obstack *obstackp,
76a01679 5099 struct block *block, const char *name,
96d887e8 5100 domain_enum domain, struct objfile *objfile,
2570f2b7 5101 int wild)
96d887e8
PH
5102{
5103 struct dict_iterator iter;
5104 int name_len = strlen (name);
5105 /* A matching argument symbol, if any. */
5106 struct symbol *arg_sym;
5107 /* Set true when we find a matching non-argument symbol. */
5108 int found_sym;
5109 struct symbol *sym;
5110
5111 arg_sym = NULL;
5112 found_sym = 0;
5113 if (wild)
5114 {
5115 struct symbol *sym;
5b4ee69b 5116
96d887e8 5117 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 5118 {
5eeb2539
AR
5119 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5120 SYMBOL_DOMAIN (sym), domain)
73589123 5121 && wild_match (SYMBOL_LINKAGE_NAME (sym), name) == 0)
76a01679 5122 {
2a2d4dc3
AS
5123 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
5124 continue;
5125 else if (SYMBOL_IS_ARGUMENT (sym))
5126 arg_sym = sym;
5127 else
5128 {
76a01679
JB
5129 found_sym = 1;
5130 add_defn_to_vec (obstackp,
5131 fixup_symbol_section (sym, objfile),
2570f2b7 5132 block);
76a01679
JB
5133 }
5134 }
5135 }
96d887e8
PH
5136 }
5137 else
5138 {
5139 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 5140 {
5eeb2539
AR
5141 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5142 SYMBOL_DOMAIN (sym), domain))
76a01679
JB
5143 {
5144 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym), name_len);
5b4ee69b 5145
76a01679
JB
5146 if (cmp == 0
5147 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len))
5148 {
2a2d4dc3
AS
5149 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5150 {
5151 if (SYMBOL_IS_ARGUMENT (sym))
5152 arg_sym = sym;
5153 else
5154 {
5155 found_sym = 1;
5156 add_defn_to_vec (obstackp,
5157 fixup_symbol_section (sym, objfile),
5158 block);
5159 }
5160 }
76a01679
JB
5161 }
5162 }
5163 }
96d887e8
PH
5164 }
5165
5166 if (!found_sym && arg_sym != NULL)
5167 {
76a01679
JB
5168 add_defn_to_vec (obstackp,
5169 fixup_symbol_section (arg_sym, objfile),
2570f2b7 5170 block);
96d887e8
PH
5171 }
5172
5173 if (!wild)
5174 {
5175 arg_sym = NULL;
5176 found_sym = 0;
5177
5178 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 5179 {
5eeb2539
AR
5180 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5181 SYMBOL_DOMAIN (sym), domain))
76a01679
JB
5182 {
5183 int cmp;
5184
5185 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
5186 if (cmp == 0)
5187 {
5188 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (sym), 5);
5189 if (cmp == 0)
5190 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
5191 name_len);
5192 }
5193
5194 if (cmp == 0
5195 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
5196 {
2a2d4dc3
AS
5197 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5198 {
5199 if (SYMBOL_IS_ARGUMENT (sym))
5200 arg_sym = sym;
5201 else
5202 {
5203 found_sym = 1;
5204 add_defn_to_vec (obstackp,
5205 fixup_symbol_section (sym, objfile),
5206 block);
5207 }
5208 }
76a01679
JB
5209 }
5210 }
76a01679 5211 }
96d887e8
PH
5212
5213 /* NOTE: This really shouldn't be needed for _ada_ symbols.
5214 They aren't parameters, right? */
5215 if (!found_sym && arg_sym != NULL)
5216 {
5217 add_defn_to_vec (obstackp,
76a01679 5218 fixup_symbol_section (arg_sym, objfile),
2570f2b7 5219 block);
96d887e8
PH
5220 }
5221 }
5222}
5223\f
41d27058
JB
5224
5225 /* Symbol Completion */
5226
5227/* If SYM_NAME is a completion candidate for TEXT, return this symbol
5228 name in a form that's appropriate for the completion. The result
5229 does not need to be deallocated, but is only good until the next call.
5230
5231 TEXT_LEN is equal to the length of TEXT.
5232 Perform a wild match if WILD_MATCH is set.
5233 ENCODED should be set if TEXT represents the start of a symbol name
5234 in its encoded form. */
5235
5236static const char *
5237symbol_completion_match (const char *sym_name,
5238 const char *text, int text_len,
5239 int wild_match, int encoded)
5240{
41d27058
JB
5241 const int verbatim_match = (text[0] == '<');
5242 int match = 0;
5243
5244 if (verbatim_match)
5245 {
5246 /* Strip the leading angle bracket. */
5247 text = text + 1;
5248 text_len--;
5249 }
5250
5251 /* First, test against the fully qualified name of the symbol. */
5252
5253 if (strncmp (sym_name, text, text_len) == 0)
5254 match = 1;
5255
5256 if (match && !encoded)
5257 {
5258 /* One needed check before declaring a positive match is to verify
5259 that iff we are doing a verbatim match, the decoded version
5260 of the symbol name starts with '<'. Otherwise, this symbol name
5261 is not a suitable completion. */
5262 const char *sym_name_copy = sym_name;
5263 int has_angle_bracket;
5264
5265 sym_name = ada_decode (sym_name);
5266 has_angle_bracket = (sym_name[0] == '<');
5267 match = (has_angle_bracket == verbatim_match);
5268 sym_name = sym_name_copy;
5269 }
5270
5271 if (match && !verbatim_match)
5272 {
5273 /* When doing non-verbatim match, another check that needs to
5274 be done is to verify that the potentially matching symbol name
5275 does not include capital letters, because the ada-mode would
5276 not be able to understand these symbol names without the
5277 angle bracket notation. */
5278 const char *tmp;
5279
5280 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
5281 if (*tmp != '\0')
5282 match = 0;
5283 }
5284
5285 /* Second: Try wild matching... */
5286
5287 if (!match && wild_match)
5288 {
5289 /* Since we are doing wild matching, this means that TEXT
5290 may represent an unqualified symbol name. We therefore must
5291 also compare TEXT against the unqualified name of the symbol. */
5292 sym_name = ada_unqualified_name (ada_decode (sym_name));
5293
5294 if (strncmp (sym_name, text, text_len) == 0)
5295 match = 1;
5296 }
5297
5298 /* Finally: If we found a mach, prepare the result to return. */
5299
5300 if (!match)
5301 return NULL;
5302
5303 if (verbatim_match)
5304 sym_name = add_angle_brackets (sym_name);
5305
5306 if (!encoded)
5307 sym_name = ada_decode (sym_name);
5308
5309 return sym_name;
5310}
5311
2ba95b9b
JB
5312DEF_VEC_P (char_ptr);
5313
41d27058
JB
5314/* A companion function to ada_make_symbol_completion_list().
5315 Check if SYM_NAME represents a symbol which name would be suitable
5316 to complete TEXT (TEXT_LEN is the length of TEXT), in which case
5317 it is appended at the end of the given string vector SV.
5318
5319 ORIG_TEXT is the string original string from the user command
5320 that needs to be completed. WORD is the entire command on which
5321 completion should be performed. These two parameters are used to
5322 determine which part of the symbol name should be added to the
5323 completion vector.
5324 if WILD_MATCH is set, then wild matching is performed.
5325 ENCODED should be set if TEXT represents a symbol name in its
5326 encoded formed (in which case the completion should also be
5327 encoded). */
5328
5329static void
d6565258 5330symbol_completion_add (VEC(char_ptr) **sv,
41d27058
JB
5331 const char *sym_name,
5332 const char *text, int text_len,
5333 const char *orig_text, const char *word,
5334 int wild_match, int encoded)
5335{
5336 const char *match = symbol_completion_match (sym_name, text, text_len,
5337 wild_match, encoded);
5338 char *completion;
5339
5340 if (match == NULL)
5341 return;
5342
5343 /* We found a match, so add the appropriate completion to the given
5344 string vector. */
5345
5346 if (word == orig_text)
5347 {
5348 completion = xmalloc (strlen (match) + 5);
5349 strcpy (completion, match);
5350 }
5351 else if (word > orig_text)
5352 {
5353 /* Return some portion of sym_name. */
5354 completion = xmalloc (strlen (match) + 5);
5355 strcpy (completion, match + (word - orig_text));
5356 }
5357 else
5358 {
5359 /* Return some of ORIG_TEXT plus sym_name. */
5360 completion = xmalloc (strlen (match) + (orig_text - word) + 5);
5361 strncpy (completion, word, orig_text - word);
5362 completion[orig_text - word] = '\0';
5363 strcat (completion, match);
5364 }
5365
d6565258 5366 VEC_safe_push (char_ptr, *sv, completion);
41d27058
JB
5367}
5368
ccefe4c4
TT
5369/* An object of this type is passed as the user_data argument to the
5370 map_partial_symbol_names method. */
5371struct add_partial_datum
5372{
5373 VEC(char_ptr) **completions;
5374 char *text;
5375 int text_len;
5376 char *text0;
5377 char *word;
5378 int wild_match;
5379 int encoded;
5380};
5381
5382/* A callback for map_partial_symbol_names. */
5383static void
5384ada_add_partial_symbol_completions (const char *name, void *user_data)
5385{
5386 struct add_partial_datum *data = user_data;
5b4ee69b 5387
ccefe4c4
TT
5388 symbol_completion_add (data->completions, name,
5389 data->text, data->text_len, data->text0, data->word,
5390 data->wild_match, data->encoded);
5391}
5392
41d27058
JB
5393/* Return a list of possible symbol names completing TEXT0. The list
5394 is NULL terminated. WORD is the entire command on which completion
5395 is made. */
5396
5397static char **
5398ada_make_symbol_completion_list (char *text0, char *word)
5399{
5400 char *text;
5401 int text_len;
5402 int wild_match;
5403 int encoded;
2ba95b9b 5404 VEC(char_ptr) *completions = VEC_alloc (char_ptr, 128);
41d27058
JB
5405 struct symbol *sym;
5406 struct symtab *s;
41d27058
JB
5407 struct minimal_symbol *msymbol;
5408 struct objfile *objfile;
5409 struct block *b, *surrounding_static_block = 0;
5410 int i;
5411 struct dict_iterator iter;
5412
5413 if (text0[0] == '<')
5414 {
5415 text = xstrdup (text0);
5416 make_cleanup (xfree, text);
5417 text_len = strlen (text);
5418 wild_match = 0;
5419 encoded = 1;
5420 }
5421 else
5422 {
5423 text = xstrdup (ada_encode (text0));
5424 make_cleanup (xfree, text);
5425 text_len = strlen (text);
5426 for (i = 0; i < text_len; i++)
5427 text[i] = tolower (text[i]);
5428
5429 encoded = (strstr (text0, "__") != NULL);
5430 /* If the name contains a ".", then the user is entering a fully
5431 qualified entity name, and the match must not be done in wild
5432 mode. Similarly, if the user wants to complete what looks like
5433 an encoded name, the match must not be done in wild mode. */
5434 wild_match = (strchr (text0, '.') == NULL && !encoded);
5435 }
5436
5437 /* First, look at the partial symtab symbols. */
41d27058 5438 {
ccefe4c4
TT
5439 struct add_partial_datum data;
5440
5441 data.completions = &completions;
5442 data.text = text;
5443 data.text_len = text_len;
5444 data.text0 = text0;
5445 data.word = word;
5446 data.wild_match = wild_match;
5447 data.encoded = encoded;
5448 map_partial_symbol_names (ada_add_partial_symbol_completions, &data);
41d27058
JB
5449 }
5450
5451 /* At this point scan through the misc symbol vectors and add each
5452 symbol you find to the list. Eventually we want to ignore
5453 anything that isn't a text symbol (everything else will be
5454 handled by the psymtab code above). */
5455
5456 ALL_MSYMBOLS (objfile, msymbol)
5457 {
5458 QUIT;
d6565258 5459 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (msymbol),
41d27058
JB
5460 text, text_len, text0, word, wild_match, encoded);
5461 }
5462
5463 /* Search upwards from currently selected frame (so that we can
5464 complete on local vars. */
5465
5466 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
5467 {
5468 if (!BLOCK_SUPERBLOCK (b))
5469 surrounding_static_block = b; /* For elmin of dups */
5470
5471 ALL_BLOCK_SYMBOLS (b, iter, sym)
5472 {
d6565258 5473 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058
JB
5474 text, text_len, text0, word,
5475 wild_match, encoded);
5476 }
5477 }
5478
5479 /* Go through the symtabs and check the externs and statics for
5480 symbols which match. */
5481
5482 ALL_SYMTABS (objfile, s)
5483 {
5484 QUIT;
5485 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
5486 ALL_BLOCK_SYMBOLS (b, iter, sym)
5487 {
d6565258 5488 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058
JB
5489 text, text_len, text0, word,
5490 wild_match, encoded);
5491 }
5492 }
5493
5494 ALL_SYMTABS (objfile, s)
5495 {
5496 QUIT;
5497 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
5498 /* Don't do this block twice. */
5499 if (b == surrounding_static_block)
5500 continue;
5501 ALL_BLOCK_SYMBOLS (b, iter, sym)
5502 {
d6565258 5503 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
41d27058
JB
5504 text, text_len, text0, word,
5505 wild_match, encoded);
5506 }
5507 }
5508
5509 /* Append the closing NULL entry. */
2ba95b9b 5510 VEC_safe_push (char_ptr, completions, NULL);
41d27058 5511
2ba95b9b
JB
5512 /* Make a copy of the COMPLETIONS VEC before we free it, and then
5513 return the copy. It's unfortunate that we have to make a copy
5514 of an array that we're about to destroy, but there is nothing much
5515 we can do about it. Fortunately, it's typically not a very large
5516 array. */
5517 {
5518 const size_t completions_size =
5519 VEC_length (char_ptr, completions) * sizeof (char *);
5520 char **result = malloc (completions_size);
5521
5522 memcpy (result, VEC_address (char_ptr, completions), completions_size);
5523
5524 VEC_free (char_ptr, completions);
5525 return result;
5526 }
41d27058
JB
5527}
5528
963a6417 5529 /* Field Access */
96d887e8 5530
73fb9985
JB
5531/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
5532 for tagged types. */
5533
5534static int
5535ada_is_dispatch_table_ptr_type (struct type *type)
5536{
5537 char *name;
5538
5539 if (TYPE_CODE (type) != TYPE_CODE_PTR)
5540 return 0;
5541
5542 name = TYPE_NAME (TYPE_TARGET_TYPE (type));
5543 if (name == NULL)
5544 return 0;
5545
5546 return (strcmp (name, "ada__tags__dispatch_table") == 0);
5547}
5548
963a6417
PH
5549/* True if field number FIELD_NUM in struct or union type TYPE is supposed
5550 to be invisible to users. */
96d887e8 5551
963a6417
PH
5552int
5553ada_is_ignored_field (struct type *type, int field_num)
96d887e8 5554{
963a6417
PH
5555 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
5556 return 1;
73fb9985
JB
5557
5558 /* Check the name of that field. */
5559 {
5560 const char *name = TYPE_FIELD_NAME (type, field_num);
5561
5562 /* Anonymous field names should not be printed.
5563 brobecker/2007-02-20: I don't think this can actually happen
5564 but we don't want to print the value of annonymous fields anyway. */
5565 if (name == NULL)
5566 return 1;
5567
5568 /* A field named "_parent" is internally generated by GNAT for
5569 tagged types, and should not be printed either. */
5570 if (name[0] == '_' && strncmp (name, "_parent", 7) != 0)
5571 return 1;
5572 }
5573
5574 /* If this is the dispatch table of a tagged type, then ignore. */
5575 if (ada_is_tagged_type (type, 1)
5576 && ada_is_dispatch_table_ptr_type (TYPE_FIELD_TYPE (type, field_num)))
5577 return 1;
5578
5579 /* Not a special field, so it should not be ignored. */
5580 return 0;
963a6417 5581}
96d887e8 5582
963a6417
PH
5583/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
5584 pointer or reference type whose ultimate target has a tag field. */
96d887e8 5585
963a6417
PH
5586int
5587ada_is_tagged_type (struct type *type, int refok)
5588{
5589 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
5590}
96d887e8 5591
963a6417 5592/* True iff TYPE represents the type of X'Tag */
96d887e8 5593
963a6417
PH
5594int
5595ada_is_tag_type (struct type *type)
5596{
5597 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
5598 return 0;
5599 else
96d887e8 5600 {
963a6417 5601 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 5602
963a6417
PH
5603 return (name != NULL
5604 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 5605 }
96d887e8
PH
5606}
5607
963a6417 5608/* The type of the tag on VAL. */
76a01679 5609
963a6417
PH
5610struct type *
5611ada_tag_type (struct value *val)
96d887e8 5612{
df407dfe 5613 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0, NULL);
963a6417 5614}
96d887e8 5615
963a6417 5616/* The value of the tag on VAL. */
96d887e8 5617
963a6417
PH
5618struct value *
5619ada_value_tag (struct value *val)
5620{
03ee6b2e 5621 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
5622}
5623
963a6417
PH
5624/* The value of the tag on the object of type TYPE whose contents are
5625 saved at VALADDR, if it is non-null, or is at memory address
5626 ADDRESS. */
96d887e8 5627
963a6417 5628static struct value *
10a2c479 5629value_tag_from_contents_and_address (struct type *type,
fc1a4b47 5630 const gdb_byte *valaddr,
963a6417 5631 CORE_ADDR address)
96d887e8 5632{
b5385fc0 5633 int tag_byte_offset;
963a6417 5634 struct type *tag_type;
5b4ee69b 5635
963a6417 5636 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
52ce6436 5637 NULL, NULL, NULL))
96d887e8 5638 {
fc1a4b47 5639 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
5640 ? NULL
5641 : valaddr + tag_byte_offset);
963a6417 5642 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 5643
963a6417 5644 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 5645 }
963a6417
PH
5646 return NULL;
5647}
96d887e8 5648
963a6417
PH
5649static struct type *
5650type_from_tag (struct value *tag)
5651{
5652 const char *type_name = ada_tag_name (tag);
5b4ee69b 5653
963a6417
PH
5654 if (type_name != NULL)
5655 return ada_find_any_type (ada_encode (type_name));
5656 return NULL;
5657}
96d887e8 5658
963a6417
PH
5659struct tag_args
5660{
5661 struct value *tag;
5662 char *name;
5663};
4c4b4cd2 5664
529cad9c
PH
5665
5666static int ada_tag_name_1 (void *);
5667static int ada_tag_name_2 (struct tag_args *);
5668
4c4b4cd2
PH
5669/* Wrapper function used by ada_tag_name. Given a struct tag_args*
5670 value ARGS, sets ARGS->name to the tag name of ARGS->tag.
5671 The value stored in ARGS->name is valid until the next call to
5672 ada_tag_name_1. */
5673
5674static int
5675ada_tag_name_1 (void *args0)
5676{
5677 struct tag_args *args = (struct tag_args *) args0;
5678 static char name[1024];
76a01679 5679 char *p;
4c4b4cd2 5680 struct value *val;
5b4ee69b 5681
4c4b4cd2 5682 args->name = NULL;
03ee6b2e 5683 val = ada_value_struct_elt (args->tag, "tsd", 1);
529cad9c
PH
5684 if (val == NULL)
5685 return ada_tag_name_2 (args);
03ee6b2e 5686 val = ada_value_struct_elt (val, "expanded_name", 1);
529cad9c
PH
5687 if (val == NULL)
5688 return 0;
5689 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5690 for (p = name; *p != '\0'; p += 1)
5691 if (isalpha (*p))
5692 *p = tolower (*p);
5693 args->name = name;
5694 return 0;
5695}
5696
e802dbe0
JB
5697/* Return the "ada__tags__type_specific_data" type. */
5698
5699static struct type *
5700ada_get_tsd_type (struct inferior *inf)
5701{
5702 struct ada_inferior_data *data = get_ada_inferior_data (inf);
5703
5704 if (data->tsd_type == 0)
5705 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
5706 return data->tsd_type;
5707}
5708
529cad9c
PH
5709/* Utility function for ada_tag_name_1 that tries the second
5710 representation for the dispatch table (in which there is no
5711 explicit 'tsd' field in the referent of the tag pointer, and instead
5712 the tsd pointer is stored just before the dispatch table. */
5713
5714static int
5715ada_tag_name_2 (struct tag_args *args)
5716{
5717 struct type *info_type;
5718 static char name[1024];
5719 char *p;
5720 struct value *val, *valp;
5721
5722 args->name = NULL;
e802dbe0 5723 info_type = ada_get_tsd_type (current_inferior());
529cad9c
PH
5724 if (info_type == NULL)
5725 return 0;
5726 info_type = lookup_pointer_type (lookup_pointer_type (info_type));
5727 valp = value_cast (info_type, args->tag);
5728 if (valp == NULL)
5729 return 0;
2497b498 5730 val = value_ind (value_ptradd (valp, -1));
4c4b4cd2
PH
5731 if (val == NULL)
5732 return 0;
03ee6b2e 5733 val = ada_value_struct_elt (val, "expanded_name", 1);
4c4b4cd2
PH
5734 if (val == NULL)
5735 return 0;
5736 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5737 for (p = name; *p != '\0'; p += 1)
5738 if (isalpha (*p))
5739 *p = tolower (*p);
5740 args->name = name;
5741 return 0;
5742}
5743
5744/* The type name of the dynamic type denoted by the 'tag value TAG, as
e802dbe0 5745 a C string. */
4c4b4cd2
PH
5746
5747const char *
5748ada_tag_name (struct value *tag)
5749{
5750 struct tag_args args;
5b4ee69b 5751
df407dfe 5752 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 5753 return NULL;
76a01679 5754 args.tag = tag;
4c4b4cd2
PH
5755 args.name = NULL;
5756 catch_errors (ada_tag_name_1, &args, NULL, RETURN_MASK_ALL);
5757 return args.name;
5758}
5759
5760/* The parent type of TYPE, or NULL if none. */
14f9c5c9 5761
d2e4a39e 5762struct type *
ebf56fd3 5763ada_parent_type (struct type *type)
14f9c5c9
AS
5764{
5765 int i;
5766
61ee279c 5767 type = ada_check_typedef (type);
14f9c5c9
AS
5768
5769 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5770 return NULL;
5771
5772 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5773 if (ada_is_parent_field (type, i))
0c1f74cf
JB
5774 {
5775 struct type *parent_type = TYPE_FIELD_TYPE (type, i);
5776
5777 /* If the _parent field is a pointer, then dereference it. */
5778 if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
5779 parent_type = TYPE_TARGET_TYPE (parent_type);
5780 /* If there is a parallel XVS type, get the actual base type. */
5781 parent_type = ada_get_base_type (parent_type);
5782
5783 return ada_check_typedef (parent_type);
5784 }
14f9c5c9
AS
5785
5786 return NULL;
5787}
5788
4c4b4cd2
PH
5789/* True iff field number FIELD_NUM of structure type TYPE contains the
5790 parent-type (inherited) fields of a derived type. Assumes TYPE is
5791 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
5792
5793int
ebf56fd3 5794ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 5795{
61ee279c 5796 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
5b4ee69b 5797
4c4b4cd2
PH
5798 return (name != NULL
5799 && (strncmp (name, "PARENT", 6) == 0
5800 || strncmp (name, "_parent", 7) == 0));
14f9c5c9
AS
5801}
5802
4c4b4cd2 5803/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 5804 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 5805 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 5806 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 5807 structures. */
14f9c5c9
AS
5808
5809int
ebf56fd3 5810ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 5811{
d2e4a39e 5812 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 5813
d2e4a39e 5814 return (name != NULL
4c4b4cd2
PH
5815 && (strncmp (name, "PARENT", 6) == 0
5816 || strcmp (name, "REP") == 0
5817 || strncmp (name, "_parent", 7) == 0
5818 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
5819}
5820
4c4b4cd2
PH
5821/* True iff field number FIELD_NUM of structure or union type TYPE
5822 is a variant wrapper. Assumes TYPE is a structure type with at least
5823 FIELD_NUM+1 fields. */
14f9c5c9
AS
5824
5825int
ebf56fd3 5826ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 5827{
d2e4a39e 5828 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
5b4ee69b 5829
14f9c5c9 5830 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
4c4b4cd2 5831 || (is_dynamic_field (type, field_num)
c3e5cd34
PH
5832 && (TYPE_CODE (TYPE_TARGET_TYPE (field_type))
5833 == TYPE_CODE_UNION)));
14f9c5c9
AS
5834}
5835
5836/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 5837 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
5838 returns the type of the controlling discriminant for the variant.
5839 May return NULL if the type could not be found. */
14f9c5c9 5840
d2e4a39e 5841struct type *
ebf56fd3 5842ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 5843{
d2e4a39e 5844 char *name = ada_variant_discrim_name (var_type);
5b4ee69b 5845
7c964f07 5846 return ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
14f9c5c9
AS
5847}
5848
4c4b4cd2 5849/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 5850 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 5851 represents a 'when others' clause; otherwise 0. */
14f9c5c9
AS
5852
5853int
ebf56fd3 5854ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 5855{
d2e4a39e 5856 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 5857
14f9c5c9
AS
5858 return (name != NULL && name[0] == 'O');
5859}
5860
5861/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
5862 returns the name of the discriminant controlling the variant.
5863 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 5864
d2e4a39e 5865char *
ebf56fd3 5866ada_variant_discrim_name (struct type *type0)
14f9c5c9 5867{
d2e4a39e 5868 static char *result = NULL;
14f9c5c9 5869 static size_t result_len = 0;
d2e4a39e
AS
5870 struct type *type;
5871 const char *name;
5872 const char *discrim_end;
5873 const char *discrim_start;
14f9c5c9
AS
5874
5875 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
5876 type = TYPE_TARGET_TYPE (type0);
5877 else
5878 type = type0;
5879
5880 name = ada_type_name (type);
5881
5882 if (name == NULL || name[0] == '\000')
5883 return "";
5884
5885 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
5886 discrim_end -= 1)
5887 {
4c4b4cd2
PH
5888 if (strncmp (discrim_end, "___XVN", 6) == 0)
5889 break;
14f9c5c9
AS
5890 }
5891 if (discrim_end == name)
5892 return "";
5893
d2e4a39e 5894 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
5895 discrim_start -= 1)
5896 {
d2e4a39e 5897 if (discrim_start == name + 1)
4c4b4cd2 5898 return "";
76a01679 5899 if ((discrim_start > name + 3
4c4b4cd2
PH
5900 && strncmp (discrim_start - 3, "___", 3) == 0)
5901 || discrim_start[-1] == '.')
5902 break;
14f9c5c9
AS
5903 }
5904
5905 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
5906 strncpy (result, discrim_start, discrim_end - discrim_start);
d2e4a39e 5907 result[discrim_end - discrim_start] = '\0';
14f9c5c9
AS
5908 return result;
5909}
5910
4c4b4cd2
PH
5911/* Scan STR for a subtype-encoded number, beginning at position K.
5912 Put the position of the character just past the number scanned in
5913 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
5914 Return 1 if there was a valid number at the given position, and 0
5915 otherwise. A "subtype-encoded" number consists of the absolute value
5916 in decimal, followed by the letter 'm' to indicate a negative number.
5917 Assumes 0m does not occur. */
14f9c5c9
AS
5918
5919int
d2e4a39e 5920ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
5921{
5922 ULONGEST RU;
5923
d2e4a39e 5924 if (!isdigit (str[k]))
14f9c5c9
AS
5925 return 0;
5926
4c4b4cd2 5927 /* Do it the hard way so as not to make any assumption about
14f9c5c9 5928 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 5929 LONGEST. */
14f9c5c9
AS
5930 RU = 0;
5931 while (isdigit (str[k]))
5932 {
d2e4a39e 5933 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
5934 k += 1;
5935 }
5936
d2e4a39e 5937 if (str[k] == 'm')
14f9c5c9
AS
5938 {
5939 if (R != NULL)
4c4b4cd2 5940 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
5941 k += 1;
5942 }
5943 else if (R != NULL)
5944 *R = (LONGEST) RU;
5945
4c4b4cd2 5946 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
5947 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
5948 number representable as a LONGEST (although either would probably work
5949 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 5950 above is always equivalent to the negative of RU. */
14f9c5c9
AS
5951
5952 if (new_k != NULL)
5953 *new_k = k;
5954 return 1;
5955}
5956
4c4b4cd2
PH
5957/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
5958 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
5959 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 5960
d2e4a39e 5961int
ebf56fd3 5962ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 5963{
d2e4a39e 5964 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
5965 int p;
5966
5967 p = 0;
5968 while (1)
5969 {
d2e4a39e 5970 switch (name[p])
4c4b4cd2
PH
5971 {
5972 case '\0':
5973 return 0;
5974 case 'S':
5975 {
5976 LONGEST W;
5b4ee69b 5977
4c4b4cd2
PH
5978 if (!ada_scan_number (name, p + 1, &W, &p))
5979 return 0;
5980 if (val == W)
5981 return 1;
5982 break;
5983 }
5984 case 'R':
5985 {
5986 LONGEST L, U;
5b4ee69b 5987
4c4b4cd2
PH
5988 if (!ada_scan_number (name, p + 1, &L, &p)
5989 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
5990 return 0;
5991 if (val >= L && val <= U)
5992 return 1;
5993 break;
5994 }
5995 case 'O':
5996 return 1;
5997 default:
5998 return 0;
5999 }
6000 }
6001}
6002
6003/* FIXME: Lots of redundancy below. Try to consolidate. */
6004
6005/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6006 ARG_TYPE, extract and return the value of one of its (non-static)
6007 fields. FIELDNO says which field. Differs from value_primitive_field
6008 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6009
4c4b4cd2 6010static struct value *
d2e4a39e 6011ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
4c4b4cd2 6012 struct type *arg_type)
14f9c5c9 6013{
14f9c5c9
AS
6014 struct type *type;
6015
61ee279c 6016 arg_type = ada_check_typedef (arg_type);
14f9c5c9
AS
6017 type = TYPE_FIELD_TYPE (arg_type, fieldno);
6018
4c4b4cd2 6019 /* Handle packed fields. */
14f9c5c9
AS
6020
6021 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
6022 {
6023 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6024 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6025
0fd88904 6026 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
4c4b4cd2
PH
6027 offset + bit_pos / 8,
6028 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6029 }
6030 else
6031 return value_primitive_field (arg1, offset, fieldno, arg_type);
6032}
6033
52ce6436
PH
6034/* Find field with name NAME in object of type TYPE. If found,
6035 set the following for each argument that is non-null:
6036 - *FIELD_TYPE_P to the field's type;
6037 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6038 an object of that type;
6039 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6040 - *BIT_SIZE_P to its size in bits if the field is packed, and
6041 0 otherwise;
6042 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6043 fields up to but not including the desired field, or by the total
6044 number of fields if not found. A NULL value of NAME never
6045 matches; the function just counts visible fields in this case.
6046
6047 Returns 1 if found, 0 otherwise. */
6048
4c4b4cd2 6049static int
76a01679
JB
6050find_struct_field (char *name, struct type *type, int offset,
6051 struct type **field_type_p,
52ce6436
PH
6052 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
6053 int *index_p)
4c4b4cd2
PH
6054{
6055 int i;
6056
61ee279c 6057 type = ada_check_typedef (type);
76a01679 6058
52ce6436
PH
6059 if (field_type_p != NULL)
6060 *field_type_p = NULL;
6061 if (byte_offset_p != NULL)
d5d6fca5 6062 *byte_offset_p = 0;
52ce6436
PH
6063 if (bit_offset_p != NULL)
6064 *bit_offset_p = 0;
6065 if (bit_size_p != NULL)
6066 *bit_size_p = 0;
6067
6068 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
4c4b4cd2
PH
6069 {
6070 int bit_pos = TYPE_FIELD_BITPOS (type, i);
6071 int fld_offset = offset + bit_pos / 8;
6072 char *t_field_name = TYPE_FIELD_NAME (type, i);
76a01679 6073
4c4b4cd2
PH
6074 if (t_field_name == NULL)
6075 continue;
6076
52ce6436 6077 else if (name != NULL && field_name_match (t_field_name, name))
76a01679
JB
6078 {
6079 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 6080
52ce6436
PH
6081 if (field_type_p != NULL)
6082 *field_type_p = TYPE_FIELD_TYPE (type, i);
6083 if (byte_offset_p != NULL)
6084 *byte_offset_p = fld_offset;
6085 if (bit_offset_p != NULL)
6086 *bit_offset_p = bit_pos % 8;
6087 if (bit_size_p != NULL)
6088 *bit_size_p = bit_size;
76a01679
JB
6089 return 1;
6090 }
4c4b4cd2
PH
6091 else if (ada_is_wrapper_field (type, i))
6092 {
52ce6436
PH
6093 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
6094 field_type_p, byte_offset_p, bit_offset_p,
6095 bit_size_p, index_p))
76a01679
JB
6096 return 1;
6097 }
4c4b4cd2
PH
6098 else if (ada_is_variant_part (type, i))
6099 {
52ce6436
PH
6100 /* PNH: Wait. Do we ever execute this section, or is ARG always of
6101 fixed type?? */
4c4b4cd2 6102 int j;
52ce6436
PH
6103 struct type *field_type
6104 = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2 6105
52ce6436 6106 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
4c4b4cd2 6107 {
76a01679
JB
6108 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
6109 fld_offset
6110 + TYPE_FIELD_BITPOS (field_type, j) / 8,
6111 field_type_p, byte_offset_p,
52ce6436 6112 bit_offset_p, bit_size_p, index_p))
76a01679 6113 return 1;
4c4b4cd2
PH
6114 }
6115 }
52ce6436
PH
6116 else if (index_p != NULL)
6117 *index_p += 1;
4c4b4cd2
PH
6118 }
6119 return 0;
6120}
6121
52ce6436 6122/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 6123
52ce6436
PH
6124static int
6125num_visible_fields (struct type *type)
6126{
6127 int n;
5b4ee69b 6128
52ce6436
PH
6129 n = 0;
6130 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
6131 return n;
6132}
14f9c5c9 6133
4c4b4cd2 6134/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
6135 and search in it assuming it has (class) type TYPE.
6136 If found, return value, else return NULL.
6137
4c4b4cd2 6138 Searches recursively through wrapper fields (e.g., '_parent'). */
14f9c5c9 6139
4c4b4cd2 6140static struct value *
d2e4a39e 6141ada_search_struct_field (char *name, struct value *arg, int offset,
4c4b4cd2 6142 struct type *type)
14f9c5c9
AS
6143{
6144 int i;
14f9c5c9 6145
5b4ee69b 6146 type = ada_check_typedef (type);
52ce6436 6147 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
14f9c5c9
AS
6148 {
6149 char *t_field_name = TYPE_FIELD_NAME (type, i);
6150
6151 if (t_field_name == NULL)
4c4b4cd2 6152 continue;
14f9c5c9
AS
6153
6154 else if (field_name_match (t_field_name, name))
4c4b4cd2 6155 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
6156
6157 else if (ada_is_wrapper_field (type, i))
4c4b4cd2 6158 {
06d5cf63
JB
6159 struct value *v = /* Do not let indent join lines here. */
6160 ada_search_struct_field (name, arg,
6161 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6162 TYPE_FIELD_TYPE (type, i));
5b4ee69b 6163
4c4b4cd2
PH
6164 if (v != NULL)
6165 return v;
6166 }
14f9c5c9
AS
6167
6168 else if (ada_is_variant_part (type, i))
4c4b4cd2 6169 {
52ce6436 6170 /* PNH: Do we ever get here? See find_struct_field. */
4c4b4cd2 6171 int j;
5b4ee69b
MS
6172 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6173 i));
4c4b4cd2
PH
6174 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
6175
52ce6436 6176 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
4c4b4cd2 6177 {
06d5cf63
JB
6178 struct value *v = ada_search_struct_field /* Force line break. */
6179 (name, arg,
6180 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
6181 TYPE_FIELD_TYPE (field_type, j));
5b4ee69b 6182
4c4b4cd2
PH
6183 if (v != NULL)
6184 return v;
6185 }
6186 }
14f9c5c9
AS
6187 }
6188 return NULL;
6189}
d2e4a39e 6190
52ce6436
PH
6191static struct value *ada_index_struct_field_1 (int *, struct value *,
6192 int, struct type *);
6193
6194
6195/* Return field #INDEX in ARG, where the index is that returned by
6196 * find_struct_field through its INDEX_P argument. Adjust the address
6197 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
6198 * If found, return value, else return NULL. */
6199
6200static struct value *
6201ada_index_struct_field (int index, struct value *arg, int offset,
6202 struct type *type)
6203{
6204 return ada_index_struct_field_1 (&index, arg, offset, type);
6205}
6206
6207
6208/* Auxiliary function for ada_index_struct_field. Like
6209 * ada_index_struct_field, but takes index from *INDEX_P and modifies
6210 * *INDEX_P. */
6211
6212static struct value *
6213ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
6214 struct type *type)
6215{
6216 int i;
6217 type = ada_check_typedef (type);
6218
6219 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6220 {
6221 if (TYPE_FIELD_NAME (type, i) == NULL)
6222 continue;
6223 else if (ada_is_wrapper_field (type, i))
6224 {
6225 struct value *v = /* Do not let indent join lines here. */
6226 ada_index_struct_field_1 (index_p, arg,
6227 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6228 TYPE_FIELD_TYPE (type, i));
5b4ee69b 6229
52ce6436
PH
6230 if (v != NULL)
6231 return v;
6232 }
6233
6234 else if (ada_is_variant_part (type, i))
6235 {
6236 /* PNH: Do we ever get here? See ada_search_struct_field,
6237 find_struct_field. */
6238 error (_("Cannot assign this kind of variant record"));
6239 }
6240 else if (*index_p == 0)
6241 return ada_value_primitive_field (arg, offset, i, type);
6242 else
6243 *index_p -= 1;
6244 }
6245 return NULL;
6246}
6247
4c4b4cd2
PH
6248/* Given ARG, a value of type (pointer or reference to a)*
6249 structure/union, extract the component named NAME from the ultimate
6250 target structure/union and return it as a value with its
f5938064 6251 appropriate type.
14f9c5c9 6252
4c4b4cd2
PH
6253 The routine searches for NAME among all members of the structure itself
6254 and (recursively) among all members of any wrapper members
14f9c5c9
AS
6255 (e.g., '_parent').
6256
03ee6b2e
PH
6257 If NO_ERR, then simply return NULL in case of error, rather than
6258 calling error. */
14f9c5c9 6259
d2e4a39e 6260struct value *
03ee6b2e 6261ada_value_struct_elt (struct value *arg, char *name, int no_err)
14f9c5c9 6262{
4c4b4cd2 6263 struct type *t, *t1;
d2e4a39e 6264 struct value *v;
14f9c5c9 6265
4c4b4cd2 6266 v = NULL;
df407dfe 6267 t1 = t = ada_check_typedef (value_type (arg));
4c4b4cd2
PH
6268 if (TYPE_CODE (t) == TYPE_CODE_REF)
6269 {
6270 t1 = TYPE_TARGET_TYPE (t);
6271 if (t1 == NULL)
03ee6b2e 6272 goto BadValue;
61ee279c 6273 t1 = ada_check_typedef (t1);
4c4b4cd2 6274 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679 6275 {
994b9211 6276 arg = coerce_ref (arg);
76a01679
JB
6277 t = t1;
6278 }
4c4b4cd2 6279 }
14f9c5c9 6280
4c4b4cd2
PH
6281 while (TYPE_CODE (t) == TYPE_CODE_PTR)
6282 {
6283 t1 = TYPE_TARGET_TYPE (t);
6284 if (t1 == NULL)
03ee6b2e 6285 goto BadValue;
61ee279c 6286 t1 = ada_check_typedef (t1);
4c4b4cd2 6287 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679
JB
6288 {
6289 arg = value_ind (arg);
6290 t = t1;
6291 }
4c4b4cd2 6292 else
76a01679 6293 break;
4c4b4cd2 6294 }
14f9c5c9 6295
4c4b4cd2 6296 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
03ee6b2e 6297 goto BadValue;
14f9c5c9 6298
4c4b4cd2
PH
6299 if (t1 == t)
6300 v = ada_search_struct_field (name, arg, 0, t);
6301 else
6302 {
6303 int bit_offset, bit_size, byte_offset;
6304 struct type *field_type;
6305 CORE_ADDR address;
6306
76a01679
JB
6307 if (TYPE_CODE (t) == TYPE_CODE_PTR)
6308 address = value_as_address (arg);
4c4b4cd2 6309 else
0fd88904 6310 address = unpack_pointer (t, value_contents (arg));
14f9c5c9 6311
1ed6ede0 6312 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL, 1);
76a01679
JB
6313 if (find_struct_field (name, t1, 0,
6314 &field_type, &byte_offset, &bit_offset,
52ce6436 6315 &bit_size, NULL))
76a01679
JB
6316 {
6317 if (bit_size != 0)
6318 {
714e53ab
PH
6319 if (TYPE_CODE (t) == TYPE_CODE_REF)
6320 arg = ada_coerce_ref (arg);
6321 else
6322 arg = ada_value_ind (arg);
76a01679
JB
6323 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
6324 bit_offset, bit_size,
6325 field_type);
6326 }
6327 else
f5938064 6328 v = value_at_lazy (field_type, address + byte_offset);
76a01679
JB
6329 }
6330 }
6331
03ee6b2e
PH
6332 if (v != NULL || no_err)
6333 return v;
6334 else
323e0a4a 6335 error (_("There is no member named %s."), name);
14f9c5c9 6336
03ee6b2e
PH
6337 BadValue:
6338 if (no_err)
6339 return NULL;
6340 else
6341 error (_("Attempt to extract a component of a value that is not a record."));
14f9c5c9
AS
6342}
6343
6344/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
6345 If DISPP is non-null, add its byte displacement from the beginning of a
6346 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
6347 work for packed fields).
6348
6349 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 6350 followed by "___".
14f9c5c9 6351
4c4b4cd2
PH
6352 TYPE can be either a struct or union. If REFOK, TYPE may also
6353 be a (pointer or reference)+ to a struct or union, and the
6354 ultimate target type will be searched.
14f9c5c9
AS
6355
6356 Looks recursively into variant clauses and parent types.
6357
4c4b4cd2
PH
6358 If NOERR is nonzero, return NULL if NAME is not suitably defined or
6359 TYPE is not a type of the right kind. */
14f9c5c9 6360
4c4b4cd2 6361static struct type *
76a01679
JB
6362ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
6363 int noerr, int *dispp)
14f9c5c9
AS
6364{
6365 int i;
6366
6367 if (name == NULL)
6368 goto BadName;
6369
76a01679 6370 if (refok && type != NULL)
4c4b4cd2
PH
6371 while (1)
6372 {
61ee279c 6373 type = ada_check_typedef (type);
76a01679
JB
6374 if (TYPE_CODE (type) != TYPE_CODE_PTR
6375 && TYPE_CODE (type) != TYPE_CODE_REF)
6376 break;
6377 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 6378 }
14f9c5c9 6379
76a01679 6380 if (type == NULL
1265e4aa
JB
6381 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
6382 && TYPE_CODE (type) != TYPE_CODE_UNION))
14f9c5c9 6383 {
4c4b4cd2 6384 if (noerr)
76a01679 6385 return NULL;
4c4b4cd2 6386 else
76a01679
JB
6387 {
6388 target_terminal_ours ();
6389 gdb_flush (gdb_stdout);
323e0a4a
AC
6390 if (type == NULL)
6391 error (_("Type (null) is not a structure or union type"));
6392 else
6393 {
6394 /* XXX: type_sprint */
6395 fprintf_unfiltered (gdb_stderr, _("Type "));
6396 type_print (type, "", gdb_stderr, -1);
6397 error (_(" is not a structure or union type"));
6398 }
76a01679 6399 }
14f9c5c9
AS
6400 }
6401
6402 type = to_static_fixed_type (type);
6403
6404 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6405 {
6406 char *t_field_name = TYPE_FIELD_NAME (type, i);
6407 struct type *t;
6408 int disp;
d2e4a39e 6409
14f9c5c9 6410 if (t_field_name == NULL)
4c4b4cd2 6411 continue;
14f9c5c9
AS
6412
6413 else if (field_name_match (t_field_name, name))
4c4b4cd2
PH
6414 {
6415 if (dispp != NULL)
6416 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
61ee279c 6417 return ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2 6418 }
14f9c5c9
AS
6419
6420 else if (ada_is_wrapper_field (type, i))
4c4b4cd2
PH
6421 {
6422 disp = 0;
6423 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
6424 0, 1, &disp);
6425 if (t != NULL)
6426 {
6427 if (dispp != NULL)
6428 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6429 return t;
6430 }
6431 }
14f9c5c9
AS
6432
6433 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
6434 {
6435 int j;
5b4ee69b
MS
6436 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6437 i));
4c4b4cd2
PH
6438
6439 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
6440 {
b1f33ddd
JB
6441 /* FIXME pnh 2008/01/26: We check for a field that is
6442 NOT wrapped in a struct, since the compiler sometimes
6443 generates these for unchecked variant types. Revisit
6444 if the compiler changes this practice. */
6445 char *v_field_name = TYPE_FIELD_NAME (field_type, j);
4c4b4cd2 6446 disp = 0;
b1f33ddd
JB
6447 if (v_field_name != NULL
6448 && field_name_match (v_field_name, name))
6449 t = ada_check_typedef (TYPE_FIELD_TYPE (field_type, j));
6450 else
6451 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type, j),
6452 name, 0, 1, &disp);
6453
4c4b4cd2
PH
6454 if (t != NULL)
6455 {
6456 if (dispp != NULL)
6457 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6458 return t;
6459 }
6460 }
6461 }
14f9c5c9
AS
6462
6463 }
6464
6465BadName:
d2e4a39e 6466 if (!noerr)
14f9c5c9
AS
6467 {
6468 target_terminal_ours ();
6469 gdb_flush (gdb_stdout);
323e0a4a
AC
6470 if (name == NULL)
6471 {
6472 /* XXX: type_sprint */
6473 fprintf_unfiltered (gdb_stderr, _("Type "));
6474 type_print (type, "", gdb_stderr, -1);
6475 error (_(" has no component named <null>"));
6476 }
6477 else
6478 {
6479 /* XXX: type_sprint */
6480 fprintf_unfiltered (gdb_stderr, _("Type "));
6481 type_print (type, "", gdb_stderr, -1);
6482 error (_(" has no component named %s"), name);
6483 }
14f9c5c9
AS
6484 }
6485
6486 return NULL;
6487}
6488
b1f33ddd
JB
6489/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6490 within a value of type OUTER_TYPE, return true iff VAR_TYPE
6491 represents an unchecked union (that is, the variant part of a
6492 record that is named in an Unchecked_Union pragma). */
6493
6494static int
6495is_unchecked_variant (struct type *var_type, struct type *outer_type)
6496{
6497 char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 6498
b1f33ddd
JB
6499 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1, NULL)
6500 == NULL);
6501}
6502
6503
14f9c5c9
AS
6504/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6505 within a value of type OUTER_TYPE that is stored in GDB at
4c4b4cd2
PH
6506 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
6507 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 6508
d2e4a39e 6509int
ebf56fd3 6510ada_which_variant_applies (struct type *var_type, struct type *outer_type,
fc1a4b47 6511 const gdb_byte *outer_valaddr)
14f9c5c9
AS
6512{
6513 int others_clause;
6514 int i;
d2e4a39e 6515 char *discrim_name = ada_variant_discrim_name (var_type);
0c281816
JB
6516 struct value *outer;
6517 struct value *discrim;
14f9c5c9
AS
6518 LONGEST discrim_val;
6519
0c281816
JB
6520 outer = value_from_contents_and_address (outer_type, outer_valaddr, 0);
6521 discrim = ada_value_struct_elt (outer, discrim_name, 1);
6522 if (discrim == NULL)
14f9c5c9 6523 return -1;
0c281816 6524 discrim_val = value_as_long (discrim);
14f9c5c9
AS
6525
6526 others_clause = -1;
6527 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
6528 {
6529 if (ada_is_others_clause (var_type, i))
4c4b4cd2 6530 others_clause = i;
14f9c5c9 6531 else if (ada_in_variant (discrim_val, var_type, i))
4c4b4cd2 6532 return i;
14f9c5c9
AS
6533 }
6534
6535 return others_clause;
6536}
d2e4a39e 6537\f
14f9c5c9
AS
6538
6539
4c4b4cd2 6540 /* Dynamic-Sized Records */
14f9c5c9
AS
6541
6542/* Strategy: The type ostensibly attached to a value with dynamic size
6543 (i.e., a size that is not statically recorded in the debugging
6544 data) does not accurately reflect the size or layout of the value.
6545 Our strategy is to convert these values to values with accurate,
4c4b4cd2 6546 conventional types that are constructed on the fly. */
14f9c5c9
AS
6547
6548/* There is a subtle and tricky problem here. In general, we cannot
6549 determine the size of dynamic records without its data. However,
6550 the 'struct value' data structure, which GDB uses to represent
6551 quantities in the inferior process (the target), requires the size
6552 of the type at the time of its allocation in order to reserve space
6553 for GDB's internal copy of the data. That's why the
6554 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 6555 rather than struct value*s.
14f9c5c9
AS
6556
6557 However, GDB's internal history variables ($1, $2, etc.) are
6558 struct value*s containing internal copies of the data that are not, in
6559 general, the same as the data at their corresponding addresses in
6560 the target. Fortunately, the types we give to these values are all
6561 conventional, fixed-size types (as per the strategy described
6562 above), so that we don't usually have to perform the
6563 'to_fixed_xxx_type' conversions to look at their values.
6564 Unfortunately, there is one exception: if one of the internal
6565 history variables is an array whose elements are unconstrained
6566 records, then we will need to create distinct fixed types for each
6567 element selected. */
6568
6569/* The upshot of all of this is that many routines take a (type, host
6570 address, target address) triple as arguments to represent a value.
6571 The host address, if non-null, is supposed to contain an internal
6572 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 6573 target at the target address. */
14f9c5c9
AS
6574
6575/* Assuming that VAL0 represents a pointer value, the result of
6576 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 6577 dynamic-sized types. */
14f9c5c9 6578
d2e4a39e
AS
6579struct value *
6580ada_value_ind (struct value *val0)
14f9c5c9 6581{
d2e4a39e 6582 struct value *val = unwrap_value (value_ind (val0));
5b4ee69b 6583
4c4b4cd2 6584 return ada_to_fixed_value (val);
14f9c5c9
AS
6585}
6586
6587/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
6588 qualifiers on VAL0. */
6589
d2e4a39e
AS
6590static struct value *
6591ada_coerce_ref (struct value *val0)
6592{
df407dfe 6593 if (TYPE_CODE (value_type (val0)) == TYPE_CODE_REF)
d2e4a39e
AS
6594 {
6595 struct value *val = val0;
5b4ee69b 6596
994b9211 6597 val = coerce_ref (val);
d2e4a39e 6598 val = unwrap_value (val);
4c4b4cd2 6599 return ada_to_fixed_value (val);
d2e4a39e
AS
6600 }
6601 else
14f9c5c9
AS
6602 return val0;
6603}
6604
6605/* Return OFF rounded upward if necessary to a multiple of
4c4b4cd2 6606 ALIGNMENT (a power of 2). */
14f9c5c9
AS
6607
6608static unsigned int
ebf56fd3 6609align_value (unsigned int off, unsigned int alignment)
14f9c5c9
AS
6610{
6611 return (off + alignment - 1) & ~(alignment - 1);
6612}
6613
4c4b4cd2 6614/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
6615
6616static unsigned int
ebf56fd3 6617field_alignment (struct type *type, int f)
14f9c5c9 6618{
d2e4a39e 6619 const char *name = TYPE_FIELD_NAME (type, f);
64a1bf19 6620 int len;
14f9c5c9
AS
6621 int align_offset;
6622
64a1bf19
JB
6623 /* The field name should never be null, unless the debugging information
6624 is somehow malformed. In this case, we assume the field does not
6625 require any alignment. */
6626 if (name == NULL)
6627 return 1;
6628
6629 len = strlen (name);
6630
4c4b4cd2
PH
6631 if (!isdigit (name[len - 1]))
6632 return 1;
14f9c5c9 6633
d2e4a39e 6634 if (isdigit (name[len - 2]))
14f9c5c9
AS
6635 align_offset = len - 2;
6636 else
6637 align_offset = len - 1;
6638
4c4b4cd2 6639 if (align_offset < 7 || strncmp ("___XV", name + align_offset - 6, 5) != 0)
14f9c5c9
AS
6640 return TARGET_CHAR_BIT;
6641
4c4b4cd2
PH
6642 return atoi (name + align_offset) * TARGET_CHAR_BIT;
6643}
6644
6645/* Find a symbol named NAME. Ignores ambiguity. */
6646
6647struct symbol *
6648ada_find_any_symbol (const char *name)
6649{
6650 struct symbol *sym;
6651
6652 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
6653 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
6654 return sym;
6655
6656 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
6657 return sym;
14f9c5c9
AS
6658}
6659
dddfab26
UW
6660/* Find a type named NAME. Ignores ambiguity. This routine will look
6661 solely for types defined by debug info, it will not search the GDB
6662 primitive types. */
4c4b4cd2 6663
d2e4a39e 6664struct type *
ebf56fd3 6665ada_find_any_type (const char *name)
14f9c5c9 6666{
4c4b4cd2 6667 struct symbol *sym = ada_find_any_symbol (name);
14f9c5c9 6668
14f9c5c9 6669 if (sym != NULL)
dddfab26 6670 return SYMBOL_TYPE (sym);
14f9c5c9 6671
dddfab26 6672 return NULL;
14f9c5c9
AS
6673}
6674
aeb5907d
JB
6675/* Given NAME and an associated BLOCK, search all symbols for
6676 NAME suffixed with "___XR", which is the ``renaming'' symbol
4c4b4cd2
PH
6677 associated to NAME. Return this symbol if found, return
6678 NULL otherwise. */
6679
6680struct symbol *
6681ada_find_renaming_symbol (const char *name, struct block *block)
aeb5907d
JB
6682{
6683 struct symbol *sym;
6684
6685 sym = find_old_style_renaming_symbol (name, block);
6686
6687 if (sym != NULL)
6688 return sym;
6689
6690 /* Not right yet. FIXME pnh 7/20/2007. */
6691 sym = ada_find_any_symbol (name);
6692 if (sym != NULL && strstr (SYMBOL_LINKAGE_NAME (sym), "___XR") != NULL)
6693 return sym;
6694 else
6695 return NULL;
6696}
6697
6698static struct symbol *
6699find_old_style_renaming_symbol (const char *name, struct block *block)
4c4b4cd2 6700{
7f0df278 6701 const struct symbol *function_sym = block_linkage_function (block);
4c4b4cd2
PH
6702 char *rename;
6703
6704 if (function_sym != NULL)
6705 {
6706 /* If the symbol is defined inside a function, NAME is not fully
6707 qualified. This means we need to prepend the function name
6708 as well as adding the ``___XR'' suffix to build the name of
6709 the associated renaming symbol. */
6710 char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
529cad9c
PH
6711 /* Function names sometimes contain suffixes used
6712 for instance to qualify nested subprograms. When building
6713 the XR type name, we need to make sure that this suffix is
6714 not included. So do not include any suffix in the function
6715 name length below. */
69fadcdf 6716 int function_name_len = ada_name_prefix_len (function_name);
76a01679
JB
6717 const int rename_len = function_name_len + 2 /* "__" */
6718 + strlen (name) + 6 /* "___XR\0" */ ;
4c4b4cd2 6719
529cad9c 6720 /* Strip the suffix if necessary. */
69fadcdf
JB
6721 ada_remove_trailing_digits (function_name, &function_name_len);
6722 ada_remove_po_subprogram_suffix (function_name, &function_name_len);
6723 ada_remove_Xbn_suffix (function_name, &function_name_len);
529cad9c 6724
4c4b4cd2
PH
6725 /* Library-level functions are a special case, as GNAT adds
6726 a ``_ada_'' prefix to the function name to avoid namespace
aeb5907d 6727 pollution. However, the renaming symbols themselves do not
4c4b4cd2
PH
6728 have this prefix, so we need to skip this prefix if present. */
6729 if (function_name_len > 5 /* "_ada_" */
6730 && strstr (function_name, "_ada_") == function_name)
69fadcdf
JB
6731 {
6732 function_name += 5;
6733 function_name_len -= 5;
6734 }
4c4b4cd2
PH
6735
6736 rename = (char *) alloca (rename_len * sizeof (char));
69fadcdf
JB
6737 strncpy (rename, function_name, function_name_len);
6738 xsnprintf (rename + function_name_len, rename_len - function_name_len,
6739 "__%s___XR", name);
4c4b4cd2
PH
6740 }
6741 else
6742 {
6743 const int rename_len = strlen (name) + 6;
5b4ee69b 6744
4c4b4cd2 6745 rename = (char *) alloca (rename_len * sizeof (char));
88c15c34 6746 xsnprintf (rename, rename_len * sizeof (char), "%s___XR", name);
4c4b4cd2
PH
6747 }
6748
6749 return ada_find_any_symbol (rename);
6750}
6751
14f9c5c9 6752/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 6753 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 6754 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
6755 otherwise return 0. */
6756
14f9c5c9 6757int
d2e4a39e 6758ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
6759{
6760 if (type1 == NULL)
6761 return 1;
6762 else if (type0 == NULL)
6763 return 0;
6764 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
6765 return 1;
6766 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
6767 return 0;
4c4b4cd2
PH
6768 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
6769 return 1;
ad82864c 6770 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 6771 return 1;
4c4b4cd2
PH
6772 else if (ada_is_array_descriptor_type (type0)
6773 && !ada_is_array_descriptor_type (type1))
14f9c5c9 6774 return 1;
aeb5907d
JB
6775 else
6776 {
6777 const char *type0_name = type_name_no_tag (type0);
6778 const char *type1_name = type_name_no_tag (type1);
6779
6780 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
6781 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
6782 return 1;
6783 }
14f9c5c9
AS
6784 return 0;
6785}
6786
6787/* The name of TYPE, which is either its TYPE_NAME, or, if that is
4c4b4cd2
PH
6788 null, its TYPE_TAG_NAME. Null if TYPE is null. */
6789
d2e4a39e
AS
6790char *
6791ada_type_name (struct type *type)
14f9c5c9 6792{
d2e4a39e 6793 if (type == NULL)
14f9c5c9
AS
6794 return NULL;
6795 else if (TYPE_NAME (type) != NULL)
6796 return TYPE_NAME (type);
6797 else
6798 return TYPE_TAG_NAME (type);
6799}
6800
b4ba55a1
JB
6801/* Search the list of "descriptive" types associated to TYPE for a type
6802 whose name is NAME. */
6803
6804static struct type *
6805find_parallel_type_by_descriptive_type (struct type *type, const char *name)
6806{
6807 struct type *result;
6808
6809 /* If there no descriptive-type info, then there is no parallel type
6810 to be found. */
6811 if (!HAVE_GNAT_AUX_INFO (type))
6812 return NULL;
6813
6814 result = TYPE_DESCRIPTIVE_TYPE (type);
6815 while (result != NULL)
6816 {
6817 char *result_name = ada_type_name (result);
6818
6819 if (result_name == NULL)
6820 {
6821 warning (_("unexpected null name on descriptive type"));
6822 return NULL;
6823 }
6824
6825 /* If the names match, stop. */
6826 if (strcmp (result_name, name) == 0)
6827 break;
6828
6829 /* Otherwise, look at the next item on the list, if any. */
6830 if (HAVE_GNAT_AUX_INFO (result))
6831 result = TYPE_DESCRIPTIVE_TYPE (result);
6832 else
6833 result = NULL;
6834 }
6835
6836 /* If we didn't find a match, see whether this is a packed array. With
6837 older compilers, the descriptive type information is either absent or
6838 irrelevant when it comes to packed arrays so the above lookup fails.
6839 Fall back to using a parallel lookup by name in this case. */
12ab9e09 6840 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
6841 return ada_find_any_type (name);
6842
6843 return result;
6844}
6845
6846/* Find a parallel type to TYPE with the specified NAME, using the
6847 descriptive type taken from the debugging information, if available,
6848 and otherwise using the (slower) name-based method. */
6849
6850static struct type *
6851ada_find_parallel_type_with_name (struct type *type, const char *name)
6852{
6853 struct type *result = NULL;
6854
6855 if (HAVE_GNAT_AUX_INFO (type))
6856 result = find_parallel_type_by_descriptive_type (type, name);
6857 else
6858 result = ada_find_any_type (name);
6859
6860 return result;
6861}
6862
6863/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 6864 SUFFIX to the name of TYPE. */
14f9c5c9 6865
d2e4a39e 6866struct type *
ebf56fd3 6867ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 6868{
b4ba55a1 6869 char *name, *typename = ada_type_name (type);
14f9c5c9 6870 int len;
d2e4a39e 6871
14f9c5c9
AS
6872 if (typename == NULL)
6873 return NULL;
6874
6875 len = strlen (typename);
6876
b4ba55a1 6877 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9
AS
6878
6879 strcpy (name, typename);
6880 strcpy (name + len, suffix);
6881
b4ba55a1 6882 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
6883}
6884
14f9c5c9 6885/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 6886 type describing its fields. Otherwise, return NULL. */
14f9c5c9 6887
d2e4a39e
AS
6888static struct type *
6889dynamic_template_type (struct type *type)
14f9c5c9 6890{
61ee279c 6891 type = ada_check_typedef (type);
14f9c5c9
AS
6892
6893 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
d2e4a39e 6894 || ada_type_name (type) == NULL)
14f9c5c9 6895 return NULL;
d2e4a39e 6896 else
14f9c5c9
AS
6897 {
6898 int len = strlen (ada_type_name (type));
5b4ee69b 6899
4c4b4cd2
PH
6900 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
6901 return type;
14f9c5c9 6902 else
4c4b4cd2 6903 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
6904 }
6905}
6906
6907/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 6908 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 6909
d2e4a39e
AS
6910static int
6911is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
6912{
6913 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
5b4ee69b 6914
d2e4a39e 6915 return name != NULL
14f9c5c9
AS
6916 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
6917 && strstr (name, "___XVL") != NULL;
6918}
6919
4c4b4cd2
PH
6920/* The index of the variant field of TYPE, or -1 if TYPE does not
6921 represent a variant record type. */
14f9c5c9 6922
d2e4a39e 6923static int
4c4b4cd2 6924variant_field_index (struct type *type)
14f9c5c9
AS
6925{
6926 int f;
6927
4c4b4cd2
PH
6928 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
6929 return -1;
6930
6931 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
6932 {
6933 if (ada_is_variant_part (type, f))
6934 return f;
6935 }
6936 return -1;
14f9c5c9
AS
6937}
6938
4c4b4cd2
PH
6939/* A record type with no fields. */
6940
d2e4a39e 6941static struct type *
e9bb382b 6942empty_record (struct type *template)
14f9c5c9 6943{
e9bb382b 6944 struct type *type = alloc_type_copy (template);
5b4ee69b 6945
14f9c5c9
AS
6946 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6947 TYPE_NFIELDS (type) = 0;
6948 TYPE_FIELDS (type) = NULL;
b1f33ddd 6949 INIT_CPLUS_SPECIFIC (type);
14f9c5c9
AS
6950 TYPE_NAME (type) = "<empty>";
6951 TYPE_TAG_NAME (type) = NULL;
14f9c5c9
AS
6952 TYPE_LENGTH (type) = 0;
6953 return type;
6954}
6955
6956/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
6957 the value of type TYPE at VALADDR or ADDRESS (see comments at
6958 the beginning of this section) VAL according to GNAT conventions.
6959 DVAL0 should describe the (portion of a) record that contains any
df407dfe 6960 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
6961 an outer-level type (i.e., as opposed to a branch of a variant.) A
6962 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 6963 of the variant.
14f9c5c9 6964
4c4b4cd2
PH
6965 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
6966 length are not statically known are discarded. As a consequence,
6967 VALADDR, ADDRESS and DVAL0 are ignored.
6968
6969 NOTE: Limitations: For now, we assume that dynamic fields and
6970 variants occupy whole numbers of bytes. However, they need not be
6971 byte-aligned. */
6972
6973struct type *
10a2c479 6974ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 6975 const gdb_byte *valaddr,
4c4b4cd2
PH
6976 CORE_ADDR address, struct value *dval0,
6977 int keep_dynamic_fields)
14f9c5c9 6978{
d2e4a39e
AS
6979 struct value *mark = value_mark ();
6980 struct value *dval;
6981 struct type *rtype;
14f9c5c9 6982 int nfields, bit_len;
4c4b4cd2 6983 int variant_field;
14f9c5c9 6984 long off;
4c4b4cd2 6985 int fld_bit_len, bit_incr;
14f9c5c9
AS
6986 int f;
6987
4c4b4cd2
PH
6988 /* Compute the number of fields in this record type that are going
6989 to be processed: unless keep_dynamic_fields, this includes only
6990 fields whose position and length are static will be processed. */
6991 if (keep_dynamic_fields)
6992 nfields = TYPE_NFIELDS (type);
6993 else
6994 {
6995 nfields = 0;
76a01679 6996 while (nfields < TYPE_NFIELDS (type)
4c4b4cd2
PH
6997 && !ada_is_variant_part (type, nfields)
6998 && !is_dynamic_field (type, nfields))
6999 nfields++;
7000 }
7001
e9bb382b 7002 rtype = alloc_type_copy (type);
14f9c5c9
AS
7003 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7004 INIT_CPLUS_SPECIFIC (rtype);
7005 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e 7006 TYPE_FIELDS (rtype) = (struct field *)
14f9c5c9
AS
7007 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7008 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
7009 TYPE_NAME (rtype) = ada_type_name (type);
7010 TYPE_TAG_NAME (rtype) = NULL;
876cecd0 7011 TYPE_FIXED_INSTANCE (rtype) = 1;
14f9c5c9 7012
d2e4a39e
AS
7013 off = 0;
7014 bit_len = 0;
4c4b4cd2
PH
7015 variant_field = -1;
7016
14f9c5c9
AS
7017 for (f = 0; f < nfields; f += 1)
7018 {
6c038f32
PH
7019 off = align_value (off, field_alignment (type, f))
7020 + TYPE_FIELD_BITPOS (type, f);
14f9c5c9 7021 TYPE_FIELD_BITPOS (rtype, f) = off;
d2e4a39e 7022 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7023
d2e4a39e 7024 if (ada_is_variant_part (type, f))
4c4b4cd2
PH
7025 {
7026 variant_field = f;
7027 fld_bit_len = bit_incr = 0;
7028 }
14f9c5c9 7029 else if (is_dynamic_field (type, f))
4c4b4cd2 7030 {
284614f0
JB
7031 const gdb_byte *field_valaddr = valaddr;
7032 CORE_ADDR field_address = address;
7033 struct type *field_type =
7034 TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f));
7035
4c4b4cd2 7036 if (dval0 == NULL)
b5304971
JG
7037 {
7038 /* rtype's length is computed based on the run-time
7039 value of discriminants. If the discriminants are not
7040 initialized, the type size may be completely bogus and
7041 GDB may fail to allocate a value for it. So check the
7042 size first before creating the value. */
7043 check_size (rtype);
7044 dval = value_from_contents_and_address (rtype, valaddr, address);
7045 }
4c4b4cd2
PH
7046 else
7047 dval = dval0;
7048
284614f0
JB
7049 /* If the type referenced by this field is an aligner type, we need
7050 to unwrap that aligner type, because its size might not be set.
7051 Keeping the aligner type would cause us to compute the wrong
7052 size for this field, impacting the offset of the all the fields
7053 that follow this one. */
7054 if (ada_is_aligner_type (field_type))
7055 {
7056 long field_offset = TYPE_FIELD_BITPOS (field_type, f);
7057
7058 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7059 field_address = cond_offset_target (field_address, field_offset);
7060 field_type = ada_aligned_type (field_type);
7061 }
7062
7063 field_valaddr = cond_offset_host (field_valaddr,
7064 off / TARGET_CHAR_BIT);
7065 field_address = cond_offset_target (field_address,
7066 off / TARGET_CHAR_BIT);
7067
7068 /* Get the fixed type of the field. Note that, in this case,
7069 we do not want to get the real type out of the tag: if
7070 the current field is the parent part of a tagged record,
7071 we will get the tag of the object. Clearly wrong: the real
7072 type of the parent is not the real type of the child. We
7073 would end up in an infinite loop. */
7074 field_type = ada_get_base_type (field_type);
7075 field_type = ada_to_fixed_type (field_type, field_valaddr,
7076 field_address, dval, 0);
7077
7078 TYPE_FIELD_TYPE (rtype, f) = field_type;
4c4b4cd2
PH
7079 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7080 bit_incr = fld_bit_len =
7081 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
7082 }
14f9c5c9 7083 else
4c4b4cd2 7084 {
9f0dec2d
JB
7085 struct type *field_type = TYPE_FIELD_TYPE (type, f);
7086
7087 TYPE_FIELD_TYPE (rtype, f) = field_type;
4c4b4cd2
PH
7088 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7089 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7090 bit_incr = fld_bit_len =
7091 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7092 else
7093 bit_incr = fld_bit_len =
9f0dec2d 7094 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
4c4b4cd2 7095 }
14f9c5c9 7096 if (off + fld_bit_len > bit_len)
4c4b4cd2 7097 bit_len = off + fld_bit_len;
14f9c5c9 7098 off += bit_incr;
4c4b4cd2
PH
7099 TYPE_LENGTH (rtype) =
7100 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7101 }
4c4b4cd2
PH
7102
7103 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7104 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7105 the record. This can happen in the presence of representation
7106 clauses. */
7107 if (variant_field >= 0)
7108 {
7109 struct type *branch_type;
7110
7111 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7112
7113 if (dval0 == NULL)
7114 dval = value_from_contents_and_address (rtype, valaddr, address);
7115 else
7116 dval = dval0;
7117
7118 branch_type =
7119 to_fixed_variant_branch_type
7120 (TYPE_FIELD_TYPE (type, variant_field),
7121 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7122 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
7123 if (branch_type == NULL)
7124 {
7125 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
7126 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7127 TYPE_NFIELDS (rtype) -= 1;
7128 }
7129 else
7130 {
7131 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7132 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7133 fld_bit_len =
7134 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
7135 TARGET_CHAR_BIT;
7136 if (off + fld_bit_len > bit_len)
7137 bit_len = off + fld_bit_len;
7138 TYPE_LENGTH (rtype) =
7139 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7140 }
7141 }
7142
714e53ab
PH
7143 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7144 should contain the alignment of that record, which should be a strictly
7145 positive value. If null or negative, then something is wrong, most
7146 probably in the debug info. In that case, we don't round up the size
7147 of the resulting type. If this record is not part of another structure,
7148 the current RTYPE length might be good enough for our purposes. */
7149 if (TYPE_LENGTH (type) <= 0)
7150 {
323e0a4a
AC
7151 if (TYPE_NAME (rtype))
7152 warning (_("Invalid type size for `%s' detected: %d."),
7153 TYPE_NAME (rtype), TYPE_LENGTH (type));
7154 else
7155 warning (_("Invalid type size for <unnamed> detected: %d."),
7156 TYPE_LENGTH (type));
714e53ab
PH
7157 }
7158 else
7159 {
7160 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype),
7161 TYPE_LENGTH (type));
7162 }
14f9c5c9
AS
7163
7164 value_free_to_mark (mark);
d2e4a39e 7165 if (TYPE_LENGTH (rtype) > varsize_limit)
323e0a4a 7166 error (_("record type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
7167 return rtype;
7168}
7169
4c4b4cd2
PH
7170/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7171 of 1. */
14f9c5c9 7172
d2e4a39e 7173static struct type *
fc1a4b47 7174template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
4c4b4cd2
PH
7175 CORE_ADDR address, struct value *dval0)
7176{
7177 return ada_template_to_fixed_record_type_1 (type, valaddr,
7178 address, dval0, 1);
7179}
7180
7181/* An ordinary record type in which ___XVL-convention fields and
7182 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7183 static approximations, containing all possible fields. Uses
7184 no runtime values. Useless for use in values, but that's OK,
7185 since the results are used only for type determinations. Works on both
7186 structs and unions. Representation note: to save space, we memorize
7187 the result of this function in the TYPE_TARGET_TYPE of the
7188 template type. */
7189
7190static struct type *
7191template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
7192{
7193 struct type *type;
7194 int nfields;
7195 int f;
7196
4c4b4cd2
PH
7197 if (TYPE_TARGET_TYPE (type0) != NULL)
7198 return TYPE_TARGET_TYPE (type0);
7199
7200 nfields = TYPE_NFIELDS (type0);
7201 type = type0;
14f9c5c9
AS
7202
7203 for (f = 0; f < nfields; f += 1)
7204 {
61ee279c 7205 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type0, f));
4c4b4cd2 7206 struct type *new_type;
14f9c5c9 7207
4c4b4cd2
PH
7208 if (is_dynamic_field (type0, f))
7209 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
14f9c5c9 7210 else
f192137b 7211 new_type = static_unwrap_type (field_type);
4c4b4cd2
PH
7212 if (type == type0 && new_type != field_type)
7213 {
e9bb382b 7214 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
4c4b4cd2
PH
7215 TYPE_CODE (type) = TYPE_CODE (type0);
7216 INIT_CPLUS_SPECIFIC (type);
7217 TYPE_NFIELDS (type) = nfields;
7218 TYPE_FIELDS (type) = (struct field *)
7219 TYPE_ALLOC (type, nfields * sizeof (struct field));
7220 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
7221 sizeof (struct field) * nfields);
7222 TYPE_NAME (type) = ada_type_name (type0);
7223 TYPE_TAG_NAME (type) = NULL;
876cecd0 7224 TYPE_FIXED_INSTANCE (type) = 1;
4c4b4cd2
PH
7225 TYPE_LENGTH (type) = 0;
7226 }
7227 TYPE_FIELD_TYPE (type, f) = new_type;
7228 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
14f9c5c9 7229 }
14f9c5c9
AS
7230 return type;
7231}
7232
4c4b4cd2 7233/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
7234 whose address in memory is ADDRESS, returns a revision of TYPE,
7235 which should be a non-dynamic-sized record, in which the variant
7236 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
7237 for discriminant values in DVAL0, which can be NULL if the record
7238 contains the necessary discriminant values. */
7239
d2e4a39e 7240static struct type *
fc1a4b47 7241to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
4c4b4cd2 7242 CORE_ADDR address, struct value *dval0)
14f9c5c9 7243{
d2e4a39e 7244 struct value *mark = value_mark ();
4c4b4cd2 7245 struct value *dval;
d2e4a39e 7246 struct type *rtype;
14f9c5c9
AS
7247 struct type *branch_type;
7248 int nfields = TYPE_NFIELDS (type);
4c4b4cd2 7249 int variant_field = variant_field_index (type);
14f9c5c9 7250
4c4b4cd2 7251 if (variant_field == -1)
14f9c5c9
AS
7252 return type;
7253
4c4b4cd2
PH
7254 if (dval0 == NULL)
7255 dval = value_from_contents_and_address (type, valaddr, address);
7256 else
7257 dval = dval0;
7258
e9bb382b 7259 rtype = alloc_type_copy (type);
14f9c5c9 7260 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
4c4b4cd2
PH
7261 INIT_CPLUS_SPECIFIC (rtype);
7262 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e
AS
7263 TYPE_FIELDS (rtype) =
7264 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7265 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
4c4b4cd2 7266 sizeof (struct field) * nfields);
14f9c5c9
AS
7267 TYPE_NAME (rtype) = ada_type_name (type);
7268 TYPE_TAG_NAME (rtype) = NULL;
876cecd0 7269 TYPE_FIXED_INSTANCE (rtype) = 1;
14f9c5c9
AS
7270 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
7271
4c4b4cd2
PH
7272 branch_type = to_fixed_variant_branch_type
7273 (TYPE_FIELD_TYPE (type, variant_field),
d2e4a39e 7274 cond_offset_host (valaddr,
4c4b4cd2
PH
7275 TYPE_FIELD_BITPOS (type, variant_field)
7276 / TARGET_CHAR_BIT),
d2e4a39e 7277 cond_offset_target (address,
4c4b4cd2
PH
7278 TYPE_FIELD_BITPOS (type, variant_field)
7279 / TARGET_CHAR_BIT), dval);
d2e4a39e 7280 if (branch_type == NULL)
14f9c5c9 7281 {
4c4b4cd2 7282 int f;
5b4ee69b 7283
4c4b4cd2
PH
7284 for (f = variant_field + 1; f < nfields; f += 1)
7285 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
14f9c5c9 7286 TYPE_NFIELDS (rtype) -= 1;
14f9c5c9
AS
7287 }
7288 else
7289 {
4c4b4cd2
PH
7290 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7291 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7292 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 7293 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 7294 }
4c4b4cd2 7295 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
d2e4a39e 7296
4c4b4cd2 7297 value_free_to_mark (mark);
14f9c5c9
AS
7298 return rtype;
7299}
7300
7301/* An ordinary record type (with fixed-length fields) that describes
7302 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
7303 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
7304 should be in DVAL, a record value; it may be NULL if the object
7305 at ADDR itself contains any necessary discriminant values.
7306 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
7307 values from the record are needed. Except in the case that DVAL,
7308 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
7309 unchecked) is replaced by a particular branch of the variant.
7310
7311 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
7312 is questionable and may be removed. It can arise during the
7313 processing of an unconstrained-array-of-record type where all the
7314 variant branches have exactly the same size. This is because in
7315 such cases, the compiler does not bother to use the XVS convention
7316 when encoding the record. I am currently dubious of this
7317 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 7318
d2e4a39e 7319static struct type *
fc1a4b47 7320to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
4c4b4cd2 7321 CORE_ADDR address, struct value *dval)
14f9c5c9 7322{
d2e4a39e 7323 struct type *templ_type;
14f9c5c9 7324
876cecd0 7325 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2
PH
7326 return type0;
7327
d2e4a39e 7328 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
7329
7330 if (templ_type != NULL)
7331 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
7332 else if (variant_field_index (type0) >= 0)
7333 {
7334 if (dval == NULL && valaddr == NULL && address == 0)
7335 return type0;
7336 return to_record_with_fixed_variant_part (type0, valaddr, address,
7337 dval);
7338 }
14f9c5c9
AS
7339 else
7340 {
876cecd0 7341 TYPE_FIXED_INSTANCE (type0) = 1;
14f9c5c9
AS
7342 return type0;
7343 }
7344
7345}
7346
7347/* An ordinary record type (with fixed-length fields) that describes
7348 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
7349 union type. Any necessary discriminants' values should be in DVAL,
7350 a record value. That is, this routine selects the appropriate
7351 branch of the union at ADDR according to the discriminant value
b1f33ddd
JB
7352 indicated in the union's type name. Returns VAR_TYPE0 itself if
7353 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 7354
d2e4a39e 7355static struct type *
fc1a4b47 7356to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
4c4b4cd2 7357 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
7358{
7359 int which;
d2e4a39e
AS
7360 struct type *templ_type;
7361 struct type *var_type;
14f9c5c9
AS
7362
7363 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
7364 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 7365 else
14f9c5c9
AS
7366 var_type = var_type0;
7367
7368 templ_type = ada_find_parallel_type (var_type, "___XVU");
7369
7370 if (templ_type != NULL)
7371 var_type = templ_type;
7372
b1f33ddd
JB
7373 if (is_unchecked_variant (var_type, value_type (dval)))
7374 return var_type0;
d2e4a39e
AS
7375 which =
7376 ada_which_variant_applies (var_type,
0fd88904 7377 value_type (dval), value_contents (dval));
14f9c5c9
AS
7378
7379 if (which < 0)
e9bb382b 7380 return empty_record (var_type);
14f9c5c9 7381 else if (is_dynamic_field (var_type, which))
4c4b4cd2 7382 return to_fixed_record_type
d2e4a39e
AS
7383 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
7384 valaddr, address, dval);
4c4b4cd2 7385 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
d2e4a39e
AS
7386 return
7387 to_fixed_record_type
7388 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
14f9c5c9
AS
7389 else
7390 return TYPE_FIELD_TYPE (var_type, which);
7391}
7392
7393/* Assuming that TYPE0 is an array type describing the type of a value
7394 at ADDR, and that DVAL describes a record containing any
7395 discriminants used in TYPE0, returns a type for the value that
7396 contains no dynamic components (that is, no components whose sizes
7397 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
7398 true, gives an error message if the resulting type's size is over
4c4b4cd2 7399 varsize_limit. */
14f9c5c9 7400
d2e4a39e
AS
7401static struct type *
7402to_fixed_array_type (struct type *type0, struct value *dval,
4c4b4cd2 7403 int ignore_too_big)
14f9c5c9 7404{
d2e4a39e
AS
7405 struct type *index_type_desc;
7406 struct type *result;
ad82864c 7407 int constrained_packed_array_p;
14f9c5c9 7408
284614f0 7409 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2 7410 return type0;
14f9c5c9 7411
ad82864c
JB
7412 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
7413 if (constrained_packed_array_p)
7414 type0 = decode_constrained_packed_array_type (type0);
284614f0 7415
14f9c5c9 7416 index_type_desc = ada_find_parallel_type (type0, "___XA");
28c85d6c 7417 ada_fixup_array_indexes_type (index_type_desc);
14f9c5c9
AS
7418 if (index_type_desc == NULL)
7419 {
61ee279c 7420 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 7421
14f9c5c9 7422 /* NOTE: elt_type---the fixed version of elt_type0---should never
4c4b4cd2
PH
7423 depend on the contents of the array in properly constructed
7424 debugging data. */
529cad9c
PH
7425 /* Create a fixed version of the array element type.
7426 We're not providing the address of an element here,
e1d5a0d2 7427 and thus the actual object value cannot be inspected to do
529cad9c
PH
7428 the conversion. This should not be a problem, since arrays of
7429 unconstrained objects are not allowed. In particular, all
7430 the elements of an array of a tagged type should all be of
7431 the same type specified in the debugging info. No need to
7432 consult the object tag. */
1ed6ede0 7433 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 7434
284614f0
JB
7435 /* Make sure we always create a new array type when dealing with
7436 packed array types, since we're going to fix-up the array
7437 type length and element bitsize a little further down. */
ad82864c 7438 if (elt_type0 == elt_type && !constrained_packed_array_p)
4c4b4cd2 7439 result = type0;
14f9c5c9 7440 else
e9bb382b 7441 result = create_array_type (alloc_type_copy (type0),
4c4b4cd2 7442 elt_type, TYPE_INDEX_TYPE (type0));
14f9c5c9
AS
7443 }
7444 else
7445 {
7446 int i;
7447 struct type *elt_type0;
7448
7449 elt_type0 = type0;
7450 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
4c4b4cd2 7451 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
7452
7453 /* NOTE: result---the fixed version of elt_type0---should never
4c4b4cd2
PH
7454 depend on the contents of the array in properly constructed
7455 debugging data. */
529cad9c
PH
7456 /* Create a fixed version of the array element type.
7457 We're not providing the address of an element here,
e1d5a0d2 7458 and thus the actual object value cannot be inspected to do
529cad9c
PH
7459 the conversion. This should not be a problem, since arrays of
7460 unconstrained objects are not allowed. In particular, all
7461 the elements of an array of a tagged type should all be of
7462 the same type specified in the debugging info. No need to
7463 consult the object tag. */
1ed6ede0
JB
7464 result =
7465 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
7466
7467 elt_type0 = type0;
14f9c5c9 7468 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
4c4b4cd2
PH
7469 {
7470 struct type *range_type =
28c85d6c 7471 to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, i), dval);
5b4ee69b 7472
e9bb382b 7473 result = create_array_type (alloc_type_copy (elt_type0),
4c4b4cd2 7474 result, range_type);
1ce677a4 7475 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
4c4b4cd2 7476 }
d2e4a39e 7477 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
323e0a4a 7478 error (_("array type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
7479 }
7480
ad82864c 7481 if (constrained_packed_array_p)
284614f0
JB
7482 {
7483 /* So far, the resulting type has been created as if the original
7484 type was a regular (non-packed) array type. As a result, the
7485 bitsize of the array elements needs to be set again, and the array
7486 length needs to be recomputed based on that bitsize. */
7487 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
7488 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
7489
7490 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
7491 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
7492 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
7493 TYPE_LENGTH (result)++;
7494 }
7495
876cecd0 7496 TYPE_FIXED_INSTANCE (result) = 1;
14f9c5c9 7497 return result;
d2e4a39e 7498}
14f9c5c9
AS
7499
7500
7501/* A standard type (containing no dynamically sized components)
7502 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
7503 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 7504 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
7505 ADDRESS or in VALADDR contains these discriminants.
7506
1ed6ede0
JB
7507 If CHECK_TAG is not null, in the case of tagged types, this function
7508 attempts to locate the object's tag and use it to compute the actual
7509 type. However, when ADDRESS is null, we cannot use it to determine the
7510 location of the tag, and therefore compute the tagged type's actual type.
7511 So we return the tagged type without consulting the tag. */
529cad9c 7512
f192137b
JB
7513static struct type *
7514ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
1ed6ede0 7515 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 7516{
61ee279c 7517 type = ada_check_typedef (type);
d2e4a39e
AS
7518 switch (TYPE_CODE (type))
7519 {
7520 default:
14f9c5c9 7521 return type;
d2e4a39e 7522 case TYPE_CODE_STRUCT:
4c4b4cd2 7523 {
76a01679 7524 struct type *static_type = to_static_fixed_type (type);
1ed6ede0
JB
7525 struct type *fixed_record_type =
7526 to_fixed_record_type (type, valaddr, address, NULL);
5b4ee69b 7527
529cad9c
PH
7528 /* If STATIC_TYPE is a tagged type and we know the object's address,
7529 then we can determine its tag, and compute the object's actual
1ed6ede0
JB
7530 type from there. Note that we have to use the fixed record
7531 type (the parent part of the record may have dynamic fields
7532 and the way the location of _tag is expressed may depend on
7533 them). */
529cad9c 7534
1ed6ede0 7535 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
76a01679
JB
7536 {
7537 struct type *real_type =
1ed6ede0
JB
7538 type_from_tag (value_tag_from_contents_and_address
7539 (fixed_record_type,
7540 valaddr,
7541 address));
5b4ee69b 7542
76a01679 7543 if (real_type != NULL)
1ed6ede0 7544 return to_fixed_record_type (real_type, valaddr, address, NULL);
76a01679 7545 }
4af88198
JB
7546
7547 /* Check to see if there is a parallel ___XVZ variable.
7548 If there is, then it provides the actual size of our type. */
7549 else if (ada_type_name (fixed_record_type) != NULL)
7550 {
7551 char *name = ada_type_name (fixed_record_type);
7552 char *xvz_name = alloca (strlen (name) + 7 /* "___XVZ\0" */);
7553 int xvz_found = 0;
7554 LONGEST size;
7555
88c15c34 7556 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
4af88198
JB
7557 size = get_int_var_value (xvz_name, &xvz_found);
7558 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
7559 {
7560 fixed_record_type = copy_type (fixed_record_type);
7561 TYPE_LENGTH (fixed_record_type) = size;
7562
7563 /* The FIXED_RECORD_TYPE may have be a stub. We have
7564 observed this when the debugging info is STABS, and
7565 apparently it is something that is hard to fix.
7566
7567 In practice, we don't need the actual type definition
7568 at all, because the presence of the XVZ variable allows us
7569 to assume that there must be a XVS type as well, which we
7570 should be able to use later, when we need the actual type
7571 definition.
7572
7573 In the meantime, pretend that the "fixed" type we are
7574 returning is NOT a stub, because this can cause trouble
7575 when using this type to create new types targeting it.
7576 Indeed, the associated creation routines often check
7577 whether the target type is a stub and will try to replace
7578 it, thus using a type with the wrong size. This, in turn,
7579 might cause the new type to have the wrong size too.
7580 Consider the case of an array, for instance, where the size
7581 of the array is computed from the number of elements in
7582 our array multiplied by the size of its element. */
7583 TYPE_STUB (fixed_record_type) = 0;
7584 }
7585 }
1ed6ede0 7586 return fixed_record_type;
4c4b4cd2 7587 }
d2e4a39e 7588 case TYPE_CODE_ARRAY:
4c4b4cd2 7589 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
7590 case TYPE_CODE_UNION:
7591 if (dval == NULL)
4c4b4cd2 7592 return type;
d2e4a39e 7593 else
4c4b4cd2 7594 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 7595 }
14f9c5c9
AS
7596}
7597
f192137b
JB
7598/* The same as ada_to_fixed_type_1, except that it preserves the type
7599 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
7600 ada_to_fixed_type_1 would return the type referenced by TYPE. */
7601
7602struct type *
7603ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
7604 CORE_ADDR address, struct value *dval, int check_tag)
7605
7606{
7607 struct type *fixed_type =
7608 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
7609
7610 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
7611 && TYPE_TARGET_TYPE (type) == fixed_type)
7612 return type;
7613
7614 return fixed_type;
7615}
7616
14f9c5c9 7617/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 7618 TYPE0, but based on no runtime data. */
14f9c5c9 7619
d2e4a39e
AS
7620static struct type *
7621to_static_fixed_type (struct type *type0)
14f9c5c9 7622{
d2e4a39e 7623 struct type *type;
14f9c5c9
AS
7624
7625 if (type0 == NULL)
7626 return NULL;
7627
876cecd0 7628 if (TYPE_FIXED_INSTANCE (type0))
4c4b4cd2
PH
7629 return type0;
7630
61ee279c 7631 type0 = ada_check_typedef (type0);
d2e4a39e 7632
14f9c5c9
AS
7633 switch (TYPE_CODE (type0))
7634 {
7635 default:
7636 return type0;
7637 case TYPE_CODE_STRUCT:
7638 type = dynamic_template_type (type0);
d2e4a39e 7639 if (type != NULL)
4c4b4cd2
PH
7640 return template_to_static_fixed_type (type);
7641 else
7642 return template_to_static_fixed_type (type0);
14f9c5c9
AS
7643 case TYPE_CODE_UNION:
7644 type = ada_find_parallel_type (type0, "___XVU");
7645 if (type != NULL)
4c4b4cd2
PH
7646 return template_to_static_fixed_type (type);
7647 else
7648 return template_to_static_fixed_type (type0);
14f9c5c9
AS
7649 }
7650}
7651
4c4b4cd2
PH
7652/* A static approximation of TYPE with all type wrappers removed. */
7653
d2e4a39e
AS
7654static struct type *
7655static_unwrap_type (struct type *type)
14f9c5c9
AS
7656{
7657 if (ada_is_aligner_type (type))
7658 {
61ee279c 7659 struct type *type1 = TYPE_FIELD_TYPE (ada_check_typedef (type), 0);
14f9c5c9 7660 if (ada_type_name (type1) == NULL)
4c4b4cd2 7661 TYPE_NAME (type1) = ada_type_name (type);
14f9c5c9
AS
7662
7663 return static_unwrap_type (type1);
7664 }
d2e4a39e 7665 else
14f9c5c9 7666 {
d2e4a39e 7667 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 7668
d2e4a39e 7669 if (raw_real_type == type)
4c4b4cd2 7670 return type;
14f9c5c9 7671 else
4c4b4cd2 7672 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
7673 }
7674}
7675
7676/* In some cases, incomplete and private types require
4c4b4cd2 7677 cross-references that are not resolved as records (for example,
14f9c5c9
AS
7678 type Foo;
7679 type FooP is access Foo;
7680 V: FooP;
7681 type Foo is array ...;
4c4b4cd2 7682 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
7683 cross-references to such types, we instead substitute for FooP a
7684 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 7685 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
7686
7687/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
7688 exists, otherwise TYPE. */
7689
d2e4a39e 7690struct type *
61ee279c 7691ada_check_typedef (struct type *type)
14f9c5c9 7692{
727e3d2e
JB
7693 if (type == NULL)
7694 return NULL;
7695
14f9c5c9
AS
7696 CHECK_TYPEDEF (type);
7697 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
529cad9c 7698 || !TYPE_STUB (type)
14f9c5c9
AS
7699 || TYPE_TAG_NAME (type) == NULL)
7700 return type;
d2e4a39e 7701 else
14f9c5c9 7702 {
d2e4a39e
AS
7703 char *name = TYPE_TAG_NAME (type);
7704 struct type *type1 = ada_find_any_type (name);
5b4ee69b 7705
05e522ef
JB
7706 if (type1 == NULL)
7707 return type;
7708
7709 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
7710 stubs pointing to arrays, as we don't create symbols for array
7711 types, only for the typedef-to-array types). This is why
7712 we process TYPE1 with ada_check_typedef before returning
7713 the result. */
7714 return ada_check_typedef (type1);
14f9c5c9
AS
7715 }
7716}
7717
7718/* A value representing the data at VALADDR/ADDRESS as described by
7719 type TYPE0, but with a standard (static-sized) type that correctly
7720 describes it. If VAL0 is not NULL and TYPE0 already is a standard
7721 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 7722 creation of struct values]. */
14f9c5c9 7723
4c4b4cd2
PH
7724static struct value *
7725ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
7726 struct value *val0)
14f9c5c9 7727{
1ed6ede0 7728 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 7729
14f9c5c9
AS
7730 if (type == type0 && val0 != NULL)
7731 return val0;
d2e4a39e 7732 else
4c4b4cd2
PH
7733 return value_from_contents_and_address (type, 0, address);
7734}
7735
7736/* A value representing VAL, but with a standard (static-sized) type
7737 that correctly describes it. Does not necessarily create a new
7738 value. */
7739
0c3acc09 7740struct value *
4c4b4cd2
PH
7741ada_to_fixed_value (struct value *val)
7742{
df407dfe 7743 return ada_to_fixed_value_create (value_type (val),
42ae5230 7744 value_address (val),
4c4b4cd2 7745 val);
14f9c5c9 7746}
d2e4a39e 7747\f
14f9c5c9 7748
14f9c5c9
AS
7749/* Attributes */
7750
4c4b4cd2
PH
7751/* Table mapping attribute numbers to names.
7752 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 7753
d2e4a39e 7754static const char *attribute_names[] = {
14f9c5c9
AS
7755 "<?>",
7756
d2e4a39e 7757 "first",
14f9c5c9
AS
7758 "last",
7759 "length",
7760 "image",
14f9c5c9
AS
7761 "max",
7762 "min",
4c4b4cd2
PH
7763 "modulus",
7764 "pos",
7765 "size",
7766 "tag",
14f9c5c9 7767 "val",
14f9c5c9
AS
7768 0
7769};
7770
d2e4a39e 7771const char *
4c4b4cd2 7772ada_attribute_name (enum exp_opcode n)
14f9c5c9 7773{
4c4b4cd2
PH
7774 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
7775 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
7776 else
7777 return attribute_names[0];
7778}
7779
4c4b4cd2 7780/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 7781
4c4b4cd2
PH
7782static LONGEST
7783pos_atr (struct value *arg)
14f9c5c9 7784{
24209737
PH
7785 struct value *val = coerce_ref (arg);
7786 struct type *type = value_type (val);
14f9c5c9 7787
d2e4a39e 7788 if (!discrete_type_p (type))
323e0a4a 7789 error (_("'POS only defined on discrete types"));
14f9c5c9
AS
7790
7791 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7792 {
7793 int i;
24209737 7794 LONGEST v = value_as_long (val);
14f9c5c9 7795
d2e4a39e 7796 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
4c4b4cd2
PH
7797 {
7798 if (v == TYPE_FIELD_BITPOS (type, i))
7799 return i;
7800 }
323e0a4a 7801 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9
AS
7802 }
7803 else
24209737 7804 return value_as_long (val);
4c4b4cd2
PH
7805}
7806
7807static struct value *
3cb382c9 7808value_pos_atr (struct type *type, struct value *arg)
4c4b4cd2 7809{
3cb382c9 7810 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
7811}
7812
4c4b4cd2 7813/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 7814
d2e4a39e
AS
7815static struct value *
7816value_val_atr (struct type *type, struct value *arg)
14f9c5c9 7817{
d2e4a39e 7818 if (!discrete_type_p (type))
323e0a4a 7819 error (_("'VAL only defined on discrete types"));
df407dfe 7820 if (!integer_type_p (value_type (arg)))
323e0a4a 7821 error (_("'VAL requires integral argument"));
14f9c5c9
AS
7822
7823 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7824 {
7825 long pos = value_as_long (arg);
5b4ee69b 7826
14f9c5c9 7827 if (pos < 0 || pos >= TYPE_NFIELDS (type))
323e0a4a 7828 error (_("argument to 'VAL out of range"));
d2e4a39e 7829 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
14f9c5c9
AS
7830 }
7831 else
7832 return value_from_longest (type, value_as_long (arg));
7833}
14f9c5c9 7834\f
d2e4a39e 7835
4c4b4cd2 7836 /* Evaluation */
14f9c5c9 7837
4c4b4cd2
PH
7838/* True if TYPE appears to be an Ada character type.
7839 [At the moment, this is true only for Character and Wide_Character;
7840 It is a heuristic test that could stand improvement]. */
14f9c5c9 7841
d2e4a39e
AS
7842int
7843ada_is_character_type (struct type *type)
14f9c5c9 7844{
7b9f71f2
JB
7845 const char *name;
7846
7847 /* If the type code says it's a character, then assume it really is,
7848 and don't check any further. */
7849 if (TYPE_CODE (type) == TYPE_CODE_CHAR)
7850 return 1;
7851
7852 /* Otherwise, assume it's a character type iff it is a discrete type
7853 with a known character type name. */
7854 name = ada_type_name (type);
7855 return (name != NULL
7856 && (TYPE_CODE (type) == TYPE_CODE_INT
7857 || TYPE_CODE (type) == TYPE_CODE_RANGE)
7858 && (strcmp (name, "character") == 0
7859 || strcmp (name, "wide_character") == 0
5a517ebd 7860 || strcmp (name, "wide_wide_character") == 0
7b9f71f2 7861 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
7862}
7863
4c4b4cd2 7864/* True if TYPE appears to be an Ada string type. */
14f9c5c9
AS
7865
7866int
ebf56fd3 7867ada_is_string_type (struct type *type)
14f9c5c9 7868{
61ee279c 7869 type = ada_check_typedef (type);
d2e4a39e 7870 if (type != NULL
14f9c5c9 7871 && TYPE_CODE (type) != TYPE_CODE_PTR
76a01679
JB
7872 && (ada_is_simple_array_type (type)
7873 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
7874 && ada_array_arity (type) == 1)
7875 {
7876 struct type *elttype = ada_array_element_type (type, 1);
7877
7878 return ada_is_character_type (elttype);
7879 }
d2e4a39e 7880 else
14f9c5c9
AS
7881 return 0;
7882}
7883
5bf03f13
JB
7884/* The compiler sometimes provides a parallel XVS type for a given
7885 PAD type. Normally, it is safe to follow the PAD type directly,
7886 but older versions of the compiler have a bug that causes the offset
7887 of its "F" field to be wrong. Following that field in that case
7888 would lead to incorrect results, but this can be worked around
7889 by ignoring the PAD type and using the associated XVS type instead.
7890
7891 Set to True if the debugger should trust the contents of PAD types.
7892 Otherwise, ignore the PAD type if there is a parallel XVS type. */
7893static int trust_pad_over_xvs = 1;
14f9c5c9
AS
7894
7895/* True if TYPE is a struct type introduced by the compiler to force the
7896 alignment of a value. Such types have a single field with a
4c4b4cd2 7897 distinctive name. */
14f9c5c9
AS
7898
7899int
ebf56fd3 7900ada_is_aligner_type (struct type *type)
14f9c5c9 7901{
61ee279c 7902 type = ada_check_typedef (type);
714e53ab 7903
5bf03f13 7904 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
7905 return 0;
7906
14f9c5c9 7907 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2
PH
7908 && TYPE_NFIELDS (type) == 1
7909 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
14f9c5c9
AS
7910}
7911
7912/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 7913 the parallel type. */
14f9c5c9 7914
d2e4a39e
AS
7915struct type *
7916ada_get_base_type (struct type *raw_type)
14f9c5c9 7917{
d2e4a39e
AS
7918 struct type *real_type_namer;
7919 struct type *raw_real_type;
14f9c5c9
AS
7920
7921 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
7922 return raw_type;
7923
284614f0
JB
7924 if (ada_is_aligner_type (raw_type))
7925 /* The encoding specifies that we should always use the aligner type.
7926 So, even if this aligner type has an associated XVS type, we should
7927 simply ignore it.
7928
7929 According to the compiler gurus, an XVS type parallel to an aligner
7930 type may exist because of a stabs limitation. In stabs, aligner
7931 types are empty because the field has a variable-sized type, and
7932 thus cannot actually be used as an aligner type. As a result,
7933 we need the associated parallel XVS type to decode the type.
7934 Since the policy in the compiler is to not change the internal
7935 representation based on the debugging info format, we sometimes
7936 end up having a redundant XVS type parallel to the aligner type. */
7937 return raw_type;
7938
14f9c5c9 7939 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 7940 if (real_type_namer == NULL
14f9c5c9
AS
7941 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
7942 || TYPE_NFIELDS (real_type_namer) != 1)
7943 return raw_type;
7944
f80d3ff2
JB
7945 if (TYPE_CODE (TYPE_FIELD_TYPE (real_type_namer, 0)) != TYPE_CODE_REF)
7946 {
7947 /* This is an older encoding form where the base type needs to be
7948 looked up by name. We prefer the newer enconding because it is
7949 more efficient. */
7950 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
7951 if (raw_real_type == NULL)
7952 return raw_type;
7953 else
7954 return raw_real_type;
7955 }
7956
7957 /* The field in our XVS type is a reference to the base type. */
7958 return TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (real_type_namer, 0));
d2e4a39e 7959}
14f9c5c9 7960
4c4b4cd2 7961/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 7962
d2e4a39e
AS
7963struct type *
7964ada_aligned_type (struct type *type)
14f9c5c9
AS
7965{
7966 if (ada_is_aligner_type (type))
7967 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
7968 else
7969 return ada_get_base_type (type);
7970}
7971
7972
7973/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 7974 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 7975
fc1a4b47
AC
7976const gdb_byte *
7977ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 7978{
d2e4a39e 7979 if (ada_is_aligner_type (type))
14f9c5c9 7980 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2
PH
7981 valaddr +
7982 TYPE_FIELD_BITPOS (type,
7983 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
7984 else
7985 return valaddr;
7986}
7987
4c4b4cd2
PH
7988
7989
14f9c5c9 7990/* The printed representation of an enumeration literal with encoded
4c4b4cd2 7991 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
7992const char *
7993ada_enum_name (const char *name)
14f9c5c9 7994{
4c4b4cd2
PH
7995 static char *result;
7996 static size_t result_len = 0;
d2e4a39e 7997 char *tmp;
14f9c5c9 7998
4c4b4cd2
PH
7999 /* First, unqualify the enumeration name:
8000 1. Search for the last '.' character. If we find one, then skip
76a01679
JB
8001 all the preceeding characters, the unqualified name starts
8002 right after that dot.
4c4b4cd2 8003 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
8004 translates dots into "__". Search forward for double underscores,
8005 but stop searching when we hit an overloading suffix, which is
8006 of the form "__" followed by digits. */
4c4b4cd2 8007
c3e5cd34
PH
8008 tmp = strrchr (name, '.');
8009 if (tmp != NULL)
4c4b4cd2
PH
8010 name = tmp + 1;
8011 else
14f9c5c9 8012 {
4c4b4cd2
PH
8013 while ((tmp = strstr (name, "__")) != NULL)
8014 {
8015 if (isdigit (tmp[2]))
8016 break;
8017 else
8018 name = tmp + 2;
8019 }
14f9c5c9
AS
8020 }
8021
8022 if (name[0] == 'Q')
8023 {
14f9c5c9 8024 int v;
5b4ee69b 8025
14f9c5c9 8026 if (name[1] == 'U' || name[1] == 'W')
4c4b4cd2
PH
8027 {
8028 if (sscanf (name + 2, "%x", &v) != 1)
8029 return name;
8030 }
14f9c5c9 8031 else
4c4b4cd2 8032 return name;
14f9c5c9 8033
4c4b4cd2 8034 GROW_VECT (result, result_len, 16);
14f9c5c9 8035 if (isascii (v) && isprint (v))
88c15c34 8036 xsnprintf (result, result_len, "'%c'", v);
14f9c5c9 8037 else if (name[1] == 'U')
88c15c34 8038 xsnprintf (result, result_len, "[\"%02x\"]", v);
14f9c5c9 8039 else
88c15c34 8040 xsnprintf (result, result_len, "[\"%04x\"]", v);
14f9c5c9
AS
8041
8042 return result;
8043 }
d2e4a39e 8044 else
4c4b4cd2 8045 {
c3e5cd34
PH
8046 tmp = strstr (name, "__");
8047 if (tmp == NULL)
8048 tmp = strstr (name, "$");
8049 if (tmp != NULL)
4c4b4cd2
PH
8050 {
8051 GROW_VECT (result, result_len, tmp - name + 1);
8052 strncpy (result, name, tmp - name);
8053 result[tmp - name] = '\0';
8054 return result;
8055 }
8056
8057 return name;
8058 }
14f9c5c9
AS
8059}
8060
14f9c5c9
AS
8061/* Evaluate the subexpression of EXP starting at *POS as for
8062 evaluate_type, updating *POS to point just past the evaluated
4c4b4cd2 8063 expression. */
14f9c5c9 8064
d2e4a39e
AS
8065static struct value *
8066evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 8067{
4b27a620 8068 return evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
14f9c5c9
AS
8069}
8070
8071/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 8072 value it wraps. */
14f9c5c9 8073
d2e4a39e
AS
8074static struct value *
8075unwrap_value (struct value *val)
14f9c5c9 8076{
df407dfe 8077 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 8078
14f9c5c9
AS
8079 if (ada_is_aligner_type (type))
8080 {
de4d072f 8081 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 8082 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 8083
14f9c5c9 8084 if (ada_type_name (val_type) == NULL)
4c4b4cd2 8085 TYPE_NAME (val_type) = ada_type_name (type);
14f9c5c9
AS
8086
8087 return unwrap_value (v);
8088 }
d2e4a39e 8089 else
14f9c5c9 8090 {
d2e4a39e 8091 struct type *raw_real_type =
61ee279c 8092 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 8093
5bf03f13
JB
8094 /* If there is no parallel XVS or XVE type, then the value is
8095 already unwrapped. Return it without further modification. */
8096 if ((type == raw_real_type)
8097 && ada_find_parallel_type (type, "___XVE") == NULL)
8098 return val;
14f9c5c9 8099
d2e4a39e 8100 return
4c4b4cd2
PH
8101 coerce_unspec_val_to_type
8102 (val, ada_to_fixed_type (raw_real_type, 0,
42ae5230 8103 value_address (val),
1ed6ede0 8104 NULL, 1));
14f9c5c9
AS
8105 }
8106}
d2e4a39e
AS
8107
8108static struct value *
8109cast_to_fixed (struct type *type, struct value *arg)
14f9c5c9
AS
8110{
8111 LONGEST val;
8112
df407dfe 8113 if (type == value_type (arg))
14f9c5c9 8114 return arg;
df407dfe 8115 else if (ada_is_fixed_point_type (value_type (arg)))
d2e4a39e 8116 val = ada_float_to_fixed (type,
df407dfe 8117 ada_fixed_to_float (value_type (arg),
4c4b4cd2 8118 value_as_long (arg)));
d2e4a39e 8119 else
14f9c5c9 8120 {
a53b7a21 8121 DOUBLEST argd = value_as_double (arg);
5b4ee69b 8122
14f9c5c9
AS
8123 val = ada_float_to_fixed (type, argd);
8124 }
8125
8126 return value_from_longest (type, val);
8127}
8128
d2e4a39e 8129static struct value *
a53b7a21 8130cast_from_fixed (struct type *type, struct value *arg)
14f9c5c9 8131{
df407dfe 8132 DOUBLEST val = ada_fixed_to_float (value_type (arg),
4c4b4cd2 8133 value_as_long (arg));
5b4ee69b 8134
a53b7a21 8135 return value_from_double (type, val);
14f9c5c9
AS
8136}
8137
4c4b4cd2
PH
8138/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
8139 return the converted value. */
8140
d2e4a39e
AS
8141static struct value *
8142coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 8143{
df407dfe 8144 struct type *type2 = value_type (val);
5b4ee69b 8145
14f9c5c9
AS
8146 if (type == type2)
8147 return val;
8148
61ee279c
PH
8149 type2 = ada_check_typedef (type2);
8150 type = ada_check_typedef (type);
14f9c5c9 8151
d2e4a39e
AS
8152 if (TYPE_CODE (type2) == TYPE_CODE_PTR
8153 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
8154 {
8155 val = ada_value_ind (val);
df407dfe 8156 type2 = value_type (val);
14f9c5c9
AS
8157 }
8158
d2e4a39e 8159 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
14f9c5c9
AS
8160 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8161 {
8162 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
4c4b4cd2
PH
8163 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
8164 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
323e0a4a 8165 error (_("Incompatible types in assignment"));
04624583 8166 deprecated_set_value_type (val, type);
14f9c5c9 8167 }
d2e4a39e 8168 return val;
14f9c5c9
AS
8169}
8170
4c4b4cd2
PH
8171static struct value *
8172ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8173{
8174 struct value *val;
8175 struct type *type1, *type2;
8176 LONGEST v, v1, v2;
8177
994b9211
AC
8178 arg1 = coerce_ref (arg1);
8179 arg2 = coerce_ref (arg2);
df407dfe
AC
8180 type1 = base_type (ada_check_typedef (value_type (arg1)));
8181 type2 = base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 8182
76a01679
JB
8183 if (TYPE_CODE (type1) != TYPE_CODE_INT
8184 || TYPE_CODE (type2) != TYPE_CODE_INT)
4c4b4cd2
PH
8185 return value_binop (arg1, arg2, op);
8186
76a01679 8187 switch (op)
4c4b4cd2
PH
8188 {
8189 case BINOP_MOD:
8190 case BINOP_DIV:
8191 case BINOP_REM:
8192 break;
8193 default:
8194 return value_binop (arg1, arg2, op);
8195 }
8196
8197 v2 = value_as_long (arg2);
8198 if (v2 == 0)
323e0a4a 8199 error (_("second operand of %s must not be zero."), op_string (op));
4c4b4cd2
PH
8200
8201 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
8202 return value_binop (arg1, arg2, op);
8203
8204 v1 = value_as_long (arg1);
8205 switch (op)
8206 {
8207 case BINOP_DIV:
8208 v = v1 / v2;
76a01679
JB
8209 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
8210 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
8211 break;
8212 case BINOP_REM:
8213 v = v1 % v2;
76a01679
JB
8214 if (v * v1 < 0)
8215 v -= v2;
4c4b4cd2
PH
8216 break;
8217 default:
8218 /* Should not reach this point. */
8219 v = 0;
8220 }
8221
8222 val = allocate_value (type1);
990a07ab 8223 store_unsigned_integer (value_contents_raw (val),
e17a4113
UW
8224 TYPE_LENGTH (value_type (val)),
8225 gdbarch_byte_order (get_type_arch (type1)), v);
4c4b4cd2
PH
8226 return val;
8227}
8228
8229static int
8230ada_value_equal (struct value *arg1, struct value *arg2)
8231{
df407dfe
AC
8232 if (ada_is_direct_array_type (value_type (arg1))
8233 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 8234 {
f58b38bf
JB
8235 /* Automatically dereference any array reference before
8236 we attempt to perform the comparison. */
8237 arg1 = ada_coerce_ref (arg1);
8238 arg2 = ada_coerce_ref (arg2);
8239
4c4b4cd2
PH
8240 arg1 = ada_coerce_to_simple_array (arg1);
8241 arg2 = ada_coerce_to_simple_array (arg2);
df407dfe
AC
8242 if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
8243 || TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
323e0a4a 8244 error (_("Attempt to compare array with non-array"));
4c4b4cd2 8245 /* FIXME: The following works only for types whose
76a01679
JB
8246 representations use all bits (no padding or undefined bits)
8247 and do not have user-defined equality. */
8248 return
df407dfe 8249 TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
0fd88904 8250 && memcmp (value_contents (arg1), value_contents (arg2),
df407dfe 8251 TYPE_LENGTH (value_type (arg1))) == 0;
4c4b4cd2
PH
8252 }
8253 return value_equal (arg1, arg2);
8254}
8255
52ce6436
PH
8256/* Total number of component associations in the aggregate starting at
8257 index PC in EXP. Assumes that index PC is the start of an
8258 OP_AGGREGATE. */
8259
8260static int
8261num_component_specs (struct expression *exp, int pc)
8262{
8263 int n, m, i;
5b4ee69b 8264
52ce6436
PH
8265 m = exp->elts[pc + 1].longconst;
8266 pc += 3;
8267 n = 0;
8268 for (i = 0; i < m; i += 1)
8269 {
8270 switch (exp->elts[pc].opcode)
8271 {
8272 default:
8273 n += 1;
8274 break;
8275 case OP_CHOICES:
8276 n += exp->elts[pc + 1].longconst;
8277 break;
8278 }
8279 ada_evaluate_subexp (NULL, exp, &pc, EVAL_SKIP);
8280 }
8281 return n;
8282}
8283
8284/* Assign the result of evaluating EXP starting at *POS to the INDEXth
8285 component of LHS (a simple array or a record), updating *POS past
8286 the expression, assuming that LHS is contained in CONTAINER. Does
8287 not modify the inferior's memory, nor does it modify LHS (unless
8288 LHS == CONTAINER). */
8289
8290static void
8291assign_component (struct value *container, struct value *lhs, LONGEST index,
8292 struct expression *exp, int *pos)
8293{
8294 struct value *mark = value_mark ();
8295 struct value *elt;
5b4ee69b 8296
52ce6436
PH
8297 if (TYPE_CODE (value_type (lhs)) == TYPE_CODE_ARRAY)
8298 {
22601c15
UW
8299 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
8300 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 8301
52ce6436
PH
8302 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
8303 }
8304 else
8305 {
8306 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
8307 elt = ada_to_fixed_value (unwrap_value (elt));
8308 }
8309
8310 if (exp->elts[*pos].opcode == OP_AGGREGATE)
8311 assign_aggregate (container, elt, exp, pos, EVAL_NORMAL);
8312 else
8313 value_assign_to_component (container, elt,
8314 ada_evaluate_subexp (NULL, exp, pos,
8315 EVAL_NORMAL));
8316
8317 value_free_to_mark (mark);
8318}
8319
8320/* Assuming that LHS represents an lvalue having a record or array
8321 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
8322 of that aggregate's value to LHS, advancing *POS past the
8323 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
8324 lvalue containing LHS (possibly LHS itself). Does not modify
8325 the inferior's memory, nor does it modify the contents of
8326 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
8327
8328static struct value *
8329assign_aggregate (struct value *container,
8330 struct value *lhs, struct expression *exp,
8331 int *pos, enum noside noside)
8332{
8333 struct type *lhs_type;
8334 int n = exp->elts[*pos+1].longconst;
8335 LONGEST low_index, high_index;
8336 int num_specs;
8337 LONGEST *indices;
8338 int max_indices, num_indices;
8339 int is_array_aggregate;
8340 int i;
52ce6436
PH
8341
8342 *pos += 3;
8343 if (noside != EVAL_NORMAL)
8344 {
8345 int i;
5b4ee69b 8346
52ce6436
PH
8347 for (i = 0; i < n; i += 1)
8348 ada_evaluate_subexp (NULL, exp, pos, noside);
8349 return container;
8350 }
8351
8352 container = ada_coerce_ref (container);
8353 if (ada_is_direct_array_type (value_type (container)))
8354 container = ada_coerce_to_simple_array (container);
8355 lhs = ada_coerce_ref (lhs);
8356 if (!deprecated_value_modifiable (lhs))
8357 error (_("Left operand of assignment is not a modifiable lvalue."));
8358
8359 lhs_type = value_type (lhs);
8360 if (ada_is_direct_array_type (lhs_type))
8361 {
8362 lhs = ada_coerce_to_simple_array (lhs);
8363 lhs_type = value_type (lhs);
8364 low_index = TYPE_ARRAY_LOWER_BOUND_VALUE (lhs_type);
8365 high_index = TYPE_ARRAY_UPPER_BOUND_VALUE (lhs_type);
8366 is_array_aggregate = 1;
8367 }
8368 else if (TYPE_CODE (lhs_type) == TYPE_CODE_STRUCT)
8369 {
8370 low_index = 0;
8371 high_index = num_visible_fields (lhs_type) - 1;
8372 is_array_aggregate = 0;
8373 }
8374 else
8375 error (_("Left-hand side must be array or record."));
8376
8377 num_specs = num_component_specs (exp, *pos - 3);
8378 max_indices = 4 * num_specs + 4;
8379 indices = alloca (max_indices * sizeof (indices[0]));
8380 indices[0] = indices[1] = low_index - 1;
8381 indices[2] = indices[3] = high_index + 1;
8382 num_indices = 4;
8383
8384 for (i = 0; i < n; i += 1)
8385 {
8386 switch (exp->elts[*pos].opcode)
8387 {
8388 case OP_CHOICES:
8389 aggregate_assign_from_choices (container, lhs, exp, pos, indices,
8390 &num_indices, max_indices,
8391 low_index, high_index);
8392 break;
8393 case OP_POSITIONAL:
8394 aggregate_assign_positional (container, lhs, exp, pos, indices,
8395 &num_indices, max_indices,
8396 low_index, high_index);
8397 break;
8398 case OP_OTHERS:
8399 if (i != n-1)
8400 error (_("Misplaced 'others' clause"));
8401 aggregate_assign_others (container, lhs, exp, pos, indices,
8402 num_indices, low_index, high_index);
8403 break;
8404 default:
8405 error (_("Internal error: bad aggregate clause"));
8406 }
8407 }
8408
8409 return container;
8410}
8411
8412/* Assign into the component of LHS indexed by the OP_POSITIONAL
8413 construct at *POS, updating *POS past the construct, given that
8414 the positions are relative to lower bound LOW, where HIGH is the
8415 upper bound. Record the position in INDICES[0 .. MAX_INDICES-1]
8416 updating *NUM_INDICES as needed. CONTAINER is as for
8417 assign_aggregate. */
8418static void
8419aggregate_assign_positional (struct value *container,
8420 struct value *lhs, struct expression *exp,
8421 int *pos, LONGEST *indices, int *num_indices,
8422 int max_indices, LONGEST low, LONGEST high)
8423{
8424 LONGEST ind = longest_to_int (exp->elts[*pos + 1].longconst) + low;
8425
8426 if (ind - 1 == high)
e1d5a0d2 8427 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
8428 if (ind <= high)
8429 {
8430 add_component_interval (ind, ind, indices, num_indices, max_indices);
8431 *pos += 3;
8432 assign_component (container, lhs, ind, exp, pos);
8433 }
8434 else
8435 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8436}
8437
8438/* Assign into the components of LHS indexed by the OP_CHOICES
8439 construct at *POS, updating *POS past the construct, given that
8440 the allowable indices are LOW..HIGH. Record the indices assigned
8441 to in INDICES[0 .. MAX_INDICES-1], updating *NUM_INDICES as
8442 needed. CONTAINER is as for assign_aggregate. */
8443static void
8444aggregate_assign_from_choices (struct value *container,
8445 struct value *lhs, struct expression *exp,
8446 int *pos, LONGEST *indices, int *num_indices,
8447 int max_indices, LONGEST low, LONGEST high)
8448{
8449 int j;
8450 int n_choices = longest_to_int (exp->elts[*pos+1].longconst);
8451 int choice_pos, expr_pc;
8452 int is_array = ada_is_direct_array_type (value_type (lhs));
8453
8454 choice_pos = *pos += 3;
8455
8456 for (j = 0; j < n_choices; j += 1)
8457 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8458 expr_pc = *pos;
8459 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8460
8461 for (j = 0; j < n_choices; j += 1)
8462 {
8463 LONGEST lower, upper;
8464 enum exp_opcode op = exp->elts[choice_pos].opcode;
5b4ee69b 8465
52ce6436
PH
8466 if (op == OP_DISCRETE_RANGE)
8467 {
8468 choice_pos += 1;
8469 lower = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8470 EVAL_NORMAL));
8471 upper = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8472 EVAL_NORMAL));
8473 }
8474 else if (is_array)
8475 {
8476 lower = value_as_long (ada_evaluate_subexp (NULL, exp, &choice_pos,
8477 EVAL_NORMAL));
8478 upper = lower;
8479 }
8480 else
8481 {
8482 int ind;
8483 char *name;
5b4ee69b 8484
52ce6436
PH
8485 switch (op)
8486 {
8487 case OP_NAME:
8488 name = &exp->elts[choice_pos + 2].string;
8489 break;
8490 case OP_VAR_VALUE:
8491 name = SYMBOL_NATURAL_NAME (exp->elts[choice_pos + 2].symbol);
8492 break;
8493 default:
8494 error (_("Invalid record component association."));
8495 }
8496 ada_evaluate_subexp (NULL, exp, &choice_pos, EVAL_SKIP);
8497 ind = 0;
8498 if (! find_struct_field (name, value_type (lhs), 0,
8499 NULL, NULL, NULL, NULL, &ind))
8500 error (_("Unknown component name: %s."), name);
8501 lower = upper = ind;
8502 }
8503
8504 if (lower <= upper && (lower < low || upper > high))
8505 error (_("Index in component association out of bounds."));
8506
8507 add_component_interval (lower, upper, indices, num_indices,
8508 max_indices);
8509 while (lower <= upper)
8510 {
8511 int pos1;
5b4ee69b 8512
52ce6436
PH
8513 pos1 = expr_pc;
8514 assign_component (container, lhs, lower, exp, &pos1);
8515 lower += 1;
8516 }
8517 }
8518}
8519
8520/* Assign the value of the expression in the OP_OTHERS construct in
8521 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
8522 have not been previously assigned. The index intervals already assigned
8523 are in INDICES[0 .. NUM_INDICES-1]. Updates *POS to after the
8524 OP_OTHERS clause. CONTAINER is as for assign_aggregate*/
8525static void
8526aggregate_assign_others (struct value *container,
8527 struct value *lhs, struct expression *exp,
8528 int *pos, LONGEST *indices, int num_indices,
8529 LONGEST low, LONGEST high)
8530{
8531 int i;
8532 int expr_pc = *pos+1;
8533
8534 for (i = 0; i < num_indices - 2; i += 2)
8535 {
8536 LONGEST ind;
5b4ee69b 8537
52ce6436
PH
8538 for (ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
8539 {
8540 int pos;
5b4ee69b 8541
52ce6436
PH
8542 pos = expr_pc;
8543 assign_component (container, lhs, ind, exp, &pos);
8544 }
8545 }
8546 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8547}
8548
8549/* Add the interval [LOW .. HIGH] to the sorted set of intervals
8550 [ INDICES[0] .. INDICES[1] ],..., [ INDICES[*SIZE-2] .. INDICES[*SIZE-1] ],
8551 modifying *SIZE as needed. It is an error if *SIZE exceeds
8552 MAX_SIZE. The resulting intervals do not overlap. */
8553static void
8554add_component_interval (LONGEST low, LONGEST high,
8555 LONGEST* indices, int *size, int max_size)
8556{
8557 int i, j;
5b4ee69b 8558
52ce6436
PH
8559 for (i = 0; i < *size; i += 2) {
8560 if (high >= indices[i] && low <= indices[i + 1])
8561 {
8562 int kh;
5b4ee69b 8563
52ce6436
PH
8564 for (kh = i + 2; kh < *size; kh += 2)
8565 if (high < indices[kh])
8566 break;
8567 if (low < indices[i])
8568 indices[i] = low;
8569 indices[i + 1] = indices[kh - 1];
8570 if (high > indices[i + 1])
8571 indices[i + 1] = high;
8572 memcpy (indices + i + 2, indices + kh, *size - kh);
8573 *size -= kh - i - 2;
8574 return;
8575 }
8576 else if (high < indices[i])
8577 break;
8578 }
8579
8580 if (*size == max_size)
8581 error (_("Internal error: miscounted aggregate components."));
8582 *size += 2;
8583 for (j = *size-1; j >= i+2; j -= 1)
8584 indices[j] = indices[j - 2];
8585 indices[i] = low;
8586 indices[i + 1] = high;
8587}
8588
6e48bd2c
JB
8589/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
8590 is different. */
8591
8592static struct value *
8593ada_value_cast (struct type *type, struct value *arg2, enum noside noside)
8594{
8595 if (type == ada_check_typedef (value_type (arg2)))
8596 return arg2;
8597
8598 if (ada_is_fixed_point_type (type))
8599 return (cast_to_fixed (type, arg2));
8600
8601 if (ada_is_fixed_point_type (value_type (arg2)))
a53b7a21 8602 return cast_from_fixed (type, arg2);
6e48bd2c
JB
8603
8604 return value_cast (type, arg2);
8605}
8606
284614f0
JB
8607/* Evaluating Ada expressions, and printing their result.
8608 ------------------------------------------------------
8609
21649b50
JB
8610 1. Introduction:
8611 ----------------
8612
284614f0
JB
8613 We usually evaluate an Ada expression in order to print its value.
8614 We also evaluate an expression in order to print its type, which
8615 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
8616 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
8617 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
8618 the evaluation compared to the EVAL_NORMAL, but is otherwise very
8619 similar.
8620
8621 Evaluating expressions is a little more complicated for Ada entities
8622 than it is for entities in languages such as C. The main reason for
8623 this is that Ada provides types whose definition might be dynamic.
8624 One example of such types is variant records. Or another example
8625 would be an array whose bounds can only be known at run time.
8626
8627 The following description is a general guide as to what should be
8628 done (and what should NOT be done) in order to evaluate an expression
8629 involving such types, and when. This does not cover how the semantic
8630 information is encoded by GNAT as this is covered separatly. For the
8631 document used as the reference for the GNAT encoding, see exp_dbug.ads
8632 in the GNAT sources.
8633
8634 Ideally, we should embed each part of this description next to its
8635 associated code. Unfortunately, the amount of code is so vast right
8636 now that it's hard to see whether the code handling a particular
8637 situation might be duplicated or not. One day, when the code is
8638 cleaned up, this guide might become redundant with the comments
8639 inserted in the code, and we might want to remove it.
8640
21649b50
JB
8641 2. ``Fixing'' an Entity, the Simple Case:
8642 -----------------------------------------
8643
284614f0
JB
8644 When evaluating Ada expressions, the tricky issue is that they may
8645 reference entities whose type contents and size are not statically
8646 known. Consider for instance a variant record:
8647
8648 type Rec (Empty : Boolean := True) is record
8649 case Empty is
8650 when True => null;
8651 when False => Value : Integer;
8652 end case;
8653 end record;
8654 Yes : Rec := (Empty => False, Value => 1);
8655 No : Rec := (empty => True);
8656
8657 The size and contents of that record depends on the value of the
8658 descriminant (Rec.Empty). At this point, neither the debugging
8659 information nor the associated type structure in GDB are able to
8660 express such dynamic types. So what the debugger does is to create
8661 "fixed" versions of the type that applies to the specific object.
8662 We also informally refer to this opperation as "fixing" an object,
8663 which means creating its associated fixed type.
8664
8665 Example: when printing the value of variable "Yes" above, its fixed
8666 type would look like this:
8667
8668 type Rec is record
8669 Empty : Boolean;
8670 Value : Integer;
8671 end record;
8672
8673 On the other hand, if we printed the value of "No", its fixed type
8674 would become:
8675
8676 type Rec is record
8677 Empty : Boolean;
8678 end record;
8679
8680 Things become a little more complicated when trying to fix an entity
8681 with a dynamic type that directly contains another dynamic type,
8682 such as an array of variant records, for instance. There are
8683 two possible cases: Arrays, and records.
8684
21649b50
JB
8685 3. ``Fixing'' Arrays:
8686 ---------------------
8687
8688 The type structure in GDB describes an array in terms of its bounds,
8689 and the type of its elements. By design, all elements in the array
8690 have the same type and we cannot represent an array of variant elements
8691 using the current type structure in GDB. When fixing an array,
8692 we cannot fix the array element, as we would potentially need one
8693 fixed type per element of the array. As a result, the best we can do
8694 when fixing an array is to produce an array whose bounds and size
8695 are correct (allowing us to read it from memory), but without having
8696 touched its element type. Fixing each element will be done later,
8697 when (if) necessary.
8698
8699 Arrays are a little simpler to handle than records, because the same
8700 amount of memory is allocated for each element of the array, even if
1b536f04 8701 the amount of space actually used by each element differs from element
21649b50 8702 to element. Consider for instance the following array of type Rec:
284614f0
JB
8703
8704 type Rec_Array is array (1 .. 2) of Rec;
8705
1b536f04
JB
8706 The actual amount of memory occupied by each element might be different
8707 from element to element, depending on the value of their discriminant.
21649b50 8708 But the amount of space reserved for each element in the array remains
1b536f04 8709 fixed regardless. So we simply need to compute that size using
21649b50
JB
8710 the debugging information available, from which we can then determine
8711 the array size (we multiply the number of elements of the array by
8712 the size of each element).
8713
8714 The simplest case is when we have an array of a constrained element
8715 type. For instance, consider the following type declarations:
8716
8717 type Bounded_String (Max_Size : Integer) is
8718 Length : Integer;
8719 Buffer : String (1 .. Max_Size);
8720 end record;
8721 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
8722
8723 In this case, the compiler describes the array as an array of
8724 variable-size elements (identified by its XVS suffix) for which
8725 the size can be read in the parallel XVZ variable.
8726
8727 In the case of an array of an unconstrained element type, the compiler
8728 wraps the array element inside a private PAD type. This type should not
8729 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
8730 that we also use the adjective "aligner" in our code to designate
8731 these wrapper types.
8732
1b536f04 8733 In some cases, the size allocated for each element is statically
21649b50
JB
8734 known. In that case, the PAD type already has the correct size,
8735 and the array element should remain unfixed.
8736
8737 But there are cases when this size is not statically known.
8738 For instance, assuming that "Five" is an integer variable:
284614f0
JB
8739
8740 type Dynamic is array (1 .. Five) of Integer;
8741 type Wrapper (Has_Length : Boolean := False) is record
8742 Data : Dynamic;
8743 case Has_Length is
8744 when True => Length : Integer;
8745 when False => null;
8746 end case;
8747 end record;
8748 type Wrapper_Array is array (1 .. 2) of Wrapper;
8749
8750 Hello : Wrapper_Array := (others => (Has_Length => True,
8751 Data => (others => 17),
8752 Length => 1));
8753
8754
8755 The debugging info would describe variable Hello as being an
8756 array of a PAD type. The size of that PAD type is not statically
8757 known, but can be determined using a parallel XVZ variable.
8758 In that case, a copy of the PAD type with the correct size should
8759 be used for the fixed array.
8760
21649b50
JB
8761 3. ``Fixing'' record type objects:
8762 ----------------------------------
8763
8764 Things are slightly different from arrays in the case of dynamic
284614f0
JB
8765 record types. In this case, in order to compute the associated
8766 fixed type, we need to determine the size and offset of each of
8767 its components. This, in turn, requires us to compute the fixed
8768 type of each of these components.
8769
8770 Consider for instance the example:
8771
8772 type Bounded_String (Max_Size : Natural) is record
8773 Str : String (1 .. Max_Size);
8774 Length : Natural;
8775 end record;
8776 My_String : Bounded_String (Max_Size => 10);
8777
8778 In that case, the position of field "Length" depends on the size
8779 of field Str, which itself depends on the value of the Max_Size
21649b50 8780 discriminant. In order to fix the type of variable My_String,
284614f0
JB
8781 we need to fix the type of field Str. Therefore, fixing a variant
8782 record requires us to fix each of its components.
8783
8784 However, if a component does not have a dynamic size, the component
8785 should not be fixed. In particular, fields that use a PAD type
8786 should not fixed. Here is an example where this might happen
8787 (assuming type Rec above):
8788
8789 type Container (Big : Boolean) is record
8790 First : Rec;
8791 After : Integer;
8792 case Big is
8793 when True => Another : Integer;
8794 when False => null;
8795 end case;
8796 end record;
8797 My_Container : Container := (Big => False,
8798 First => (Empty => True),
8799 After => 42);
8800
8801 In that example, the compiler creates a PAD type for component First,
8802 whose size is constant, and then positions the component After just
8803 right after it. The offset of component After is therefore constant
8804 in this case.
8805
8806 The debugger computes the position of each field based on an algorithm
8807 that uses, among other things, the actual position and size of the field
21649b50
JB
8808 preceding it. Let's now imagine that the user is trying to print
8809 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
8810 end up computing the offset of field After based on the size of the
8811 fixed version of field First. And since in our example First has
8812 only one actual field, the size of the fixed type is actually smaller
8813 than the amount of space allocated to that field, and thus we would
8814 compute the wrong offset of field After.
8815
21649b50
JB
8816 To make things more complicated, we need to watch out for dynamic
8817 components of variant records (identified by the ___XVL suffix in
8818 the component name). Even if the target type is a PAD type, the size
8819 of that type might not be statically known. So the PAD type needs
8820 to be unwrapped and the resulting type needs to be fixed. Otherwise,
8821 we might end up with the wrong size for our component. This can be
8822 observed with the following type declarations:
284614f0
JB
8823
8824 type Octal is new Integer range 0 .. 7;
8825 type Octal_Array is array (Positive range <>) of Octal;
8826 pragma Pack (Octal_Array);
8827
8828 type Octal_Buffer (Size : Positive) is record
8829 Buffer : Octal_Array (1 .. Size);
8830 Length : Integer;
8831 end record;
8832
8833 In that case, Buffer is a PAD type whose size is unset and needs
8834 to be computed by fixing the unwrapped type.
8835
21649b50
JB
8836 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
8837 ----------------------------------------------------------
8838
8839 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
8840 thus far, be actually fixed?
8841
8842 The answer is: Only when referencing that element. For instance
8843 when selecting one component of a record, this specific component
8844 should be fixed at that point in time. Or when printing the value
8845 of a record, each component should be fixed before its value gets
8846 printed. Similarly for arrays, the element of the array should be
8847 fixed when printing each element of the array, or when extracting
8848 one element out of that array. On the other hand, fixing should
8849 not be performed on the elements when taking a slice of an array!
8850
8851 Note that one of the side-effects of miscomputing the offset and
8852 size of each field is that we end up also miscomputing the size
8853 of the containing type. This can have adverse results when computing
8854 the value of an entity. GDB fetches the value of an entity based
8855 on the size of its type, and thus a wrong size causes GDB to fetch
8856 the wrong amount of memory. In the case where the computed size is
8857 too small, GDB fetches too little data to print the value of our
8858 entiry. Results in this case as unpredicatble, as we usually read
8859 past the buffer containing the data =:-o. */
8860
8861/* Implement the evaluate_exp routine in the exp_descriptor structure
8862 for the Ada language. */
8863
52ce6436 8864static struct value *
ebf56fd3 8865ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
4c4b4cd2 8866 int *pos, enum noside noside)
14f9c5c9
AS
8867{
8868 enum exp_opcode op;
b5385fc0 8869 int tem;
14f9c5c9
AS
8870 int pc;
8871 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
8872 struct type *type;
52ce6436 8873 int nargs, oplen;
d2e4a39e 8874 struct value **argvec;
14f9c5c9 8875
d2e4a39e
AS
8876 pc = *pos;
8877 *pos += 1;
14f9c5c9
AS
8878 op = exp->elts[pc].opcode;
8879
d2e4a39e 8880 switch (op)
14f9c5c9
AS
8881 {
8882 default:
8883 *pos -= 1;
6e48bd2c
JB
8884 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
8885 arg1 = unwrap_value (arg1);
8886
8887 /* If evaluating an OP_DOUBLE and an EXPECT_TYPE was provided,
8888 then we need to perform the conversion manually, because
8889 evaluate_subexp_standard doesn't do it. This conversion is
8890 necessary in Ada because the different kinds of float/fixed
8891 types in Ada have different representations.
8892
8893 Similarly, we need to perform the conversion from OP_LONG
8894 ourselves. */
8895 if ((op == OP_DOUBLE || op == OP_LONG) && expect_type != NULL)
8896 arg1 = ada_value_cast (expect_type, arg1, noside);
8897
8898 return arg1;
4c4b4cd2
PH
8899
8900 case OP_STRING:
8901 {
76a01679 8902 struct value *result;
5b4ee69b 8903
76a01679
JB
8904 *pos -= 1;
8905 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
8906 /* The result type will have code OP_STRING, bashed there from
8907 OP_ARRAY. Bash it back. */
df407dfe
AC
8908 if (TYPE_CODE (value_type (result)) == TYPE_CODE_STRING)
8909 TYPE_CODE (value_type (result)) = TYPE_CODE_ARRAY;
76a01679 8910 return result;
4c4b4cd2 8911 }
14f9c5c9
AS
8912
8913 case UNOP_CAST:
8914 (*pos) += 2;
8915 type = exp->elts[pc + 1].type;
8916 arg1 = evaluate_subexp (type, exp, pos, noside);
8917 if (noside == EVAL_SKIP)
4c4b4cd2 8918 goto nosideret;
6e48bd2c 8919 arg1 = ada_value_cast (type, arg1, noside);
14f9c5c9
AS
8920 return arg1;
8921
4c4b4cd2
PH
8922 case UNOP_QUAL:
8923 (*pos) += 2;
8924 type = exp->elts[pc + 1].type;
8925 return ada_evaluate_subexp (type, exp, pos, noside);
8926
14f9c5c9
AS
8927 case BINOP_ASSIGN:
8928 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
52ce6436
PH
8929 if (exp->elts[*pos].opcode == OP_AGGREGATE)
8930 {
8931 arg1 = assign_aggregate (arg1, arg1, exp, pos, noside);
8932 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
8933 return arg1;
8934 return ada_value_assign (arg1, arg1);
8935 }
003f3813
JB
8936 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
8937 except if the lhs of our assignment is a convenience variable.
8938 In the case of assigning to a convenience variable, the lhs
8939 should be exactly the result of the evaluation of the rhs. */
8940 type = value_type (arg1);
8941 if (VALUE_LVAL (arg1) == lval_internalvar)
8942 type = NULL;
8943 arg2 = evaluate_subexp (type, exp, pos, noside);
14f9c5c9 8944 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 8945 return arg1;
df407dfe
AC
8946 if (ada_is_fixed_point_type (value_type (arg1)))
8947 arg2 = cast_to_fixed (value_type (arg1), arg2);
8948 else if (ada_is_fixed_point_type (value_type (arg2)))
76a01679 8949 error
323e0a4a 8950 (_("Fixed-point values must be assigned to fixed-point variables"));
d2e4a39e 8951 else
df407dfe 8952 arg2 = coerce_for_assign (value_type (arg1), arg2);
4c4b4cd2 8953 return ada_value_assign (arg1, arg2);
14f9c5c9
AS
8954
8955 case BINOP_ADD:
8956 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8957 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8958 if (noside == EVAL_SKIP)
4c4b4cd2 8959 goto nosideret;
2ac8a782
JB
8960 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
8961 return (value_from_longest
8962 (value_type (arg1),
8963 value_as_long (arg1) + value_as_long (arg2)));
df407dfe
AC
8964 if ((ada_is_fixed_point_type (value_type (arg1))
8965 || ada_is_fixed_point_type (value_type (arg2)))
8966 && value_type (arg1) != value_type (arg2))
323e0a4a 8967 error (_("Operands of fixed-point addition must have the same type"));
b7789565
JB
8968 /* Do the addition, and cast the result to the type of the first
8969 argument. We cannot cast the result to a reference type, so if
8970 ARG1 is a reference type, find its underlying type. */
8971 type = value_type (arg1);
8972 while (TYPE_CODE (type) == TYPE_CODE_REF)
8973 type = TYPE_TARGET_TYPE (type);
f44316fa 8974 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
89eef114 8975 return value_cast (type, value_binop (arg1, arg2, BINOP_ADD));
14f9c5c9
AS
8976
8977 case BINOP_SUB:
8978 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8979 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8980 if (noside == EVAL_SKIP)
4c4b4cd2 8981 goto nosideret;
2ac8a782
JB
8982 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
8983 return (value_from_longest
8984 (value_type (arg1),
8985 value_as_long (arg1) - value_as_long (arg2)));
df407dfe
AC
8986 if ((ada_is_fixed_point_type (value_type (arg1))
8987 || ada_is_fixed_point_type (value_type (arg2)))
8988 && value_type (arg1) != value_type (arg2))
323e0a4a 8989 error (_("Operands of fixed-point subtraction must have the same type"));
b7789565
JB
8990 /* Do the substraction, and cast the result to the type of the first
8991 argument. We cannot cast the result to a reference type, so if
8992 ARG1 is a reference type, find its underlying type. */
8993 type = value_type (arg1);
8994 while (TYPE_CODE (type) == TYPE_CODE_REF)
8995 type = TYPE_TARGET_TYPE (type);
f44316fa 8996 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
89eef114 8997 return value_cast (type, value_binop (arg1, arg2, BINOP_SUB));
14f9c5c9
AS
8998
8999 case BINOP_MUL:
9000 case BINOP_DIV:
e1578042
JB
9001 case BINOP_REM:
9002 case BINOP_MOD:
14f9c5c9
AS
9003 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9004 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9005 if (noside == EVAL_SKIP)
4c4b4cd2 9006 goto nosideret;
e1578042 9007 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9c2be529
JB
9008 {
9009 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9010 return value_zero (value_type (arg1), not_lval);
9011 }
14f9c5c9 9012 else
4c4b4cd2 9013 {
a53b7a21 9014 type = builtin_type (exp->gdbarch)->builtin_double;
df407dfe 9015 if (ada_is_fixed_point_type (value_type (arg1)))
a53b7a21 9016 arg1 = cast_from_fixed (type, arg1);
df407dfe 9017 if (ada_is_fixed_point_type (value_type (arg2)))
a53b7a21 9018 arg2 = cast_from_fixed (type, arg2);
f44316fa 9019 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
4c4b4cd2
PH
9020 return ada_value_binop (arg1, arg2, op);
9021 }
9022
4c4b4cd2
PH
9023 case BINOP_EQUAL:
9024 case BINOP_NOTEQUAL:
14f9c5c9 9025 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
df407dfe 9026 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
14f9c5c9 9027 if (noside == EVAL_SKIP)
76a01679 9028 goto nosideret;
4c4b4cd2 9029 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9030 tem = 0;
4c4b4cd2 9031 else
f44316fa
UW
9032 {
9033 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9034 tem = ada_value_equal (arg1, arg2);
9035 }
4c4b4cd2 9036 if (op == BINOP_NOTEQUAL)
76a01679 9037 tem = !tem;
fbb06eb1
UW
9038 type = language_bool_type (exp->language_defn, exp->gdbarch);
9039 return value_from_longest (type, (LONGEST) tem);
4c4b4cd2
PH
9040
9041 case UNOP_NEG:
9042 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9043 if (noside == EVAL_SKIP)
9044 goto nosideret;
df407dfe
AC
9045 else if (ada_is_fixed_point_type (value_type (arg1)))
9046 return value_cast (value_type (arg1), value_neg (arg1));
14f9c5c9 9047 else
f44316fa
UW
9048 {
9049 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9050 return value_neg (arg1);
9051 }
4c4b4cd2 9052
2330c6c6
JB
9053 case BINOP_LOGICAL_AND:
9054 case BINOP_LOGICAL_OR:
9055 case UNOP_LOGICAL_NOT:
000d5124
JB
9056 {
9057 struct value *val;
9058
9059 *pos -= 1;
9060 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
fbb06eb1
UW
9061 type = language_bool_type (exp->language_defn, exp->gdbarch);
9062 return value_cast (type, val);
000d5124 9063 }
2330c6c6
JB
9064
9065 case BINOP_BITWISE_AND:
9066 case BINOP_BITWISE_IOR:
9067 case BINOP_BITWISE_XOR:
000d5124
JB
9068 {
9069 struct value *val;
9070
9071 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
9072 *pos = pc;
9073 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
9074
9075 return value_cast (value_type (arg1), val);
9076 }
2330c6c6 9077
14f9c5c9
AS
9078 case OP_VAR_VALUE:
9079 *pos -= 1;
6799def4 9080
14f9c5c9 9081 if (noside == EVAL_SKIP)
4c4b4cd2
PH
9082 {
9083 *pos += 4;
9084 goto nosideret;
9085 }
9086 else if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
9087 /* Only encountered when an unresolved symbol occurs in a
9088 context other than a function call, in which case, it is
52ce6436 9089 invalid. */
323e0a4a 9090 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2 9091 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
14f9c5c9 9092 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 9093 {
0c1f74cf 9094 type = static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
31dbc1c5
JB
9095 /* Check to see if this is a tagged type. We also need to handle
9096 the case where the type is a reference to a tagged type, but
9097 we have to be careful to exclude pointers to tagged types.
9098 The latter should be shown as usual (as a pointer), whereas
9099 a reference should mostly be transparent to the user. */
9100 if (ada_is_tagged_type (type, 0)
9101 || (TYPE_CODE(type) == TYPE_CODE_REF
9102 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
0c1f74cf
JB
9103 {
9104 /* Tagged types are a little special in the fact that the real
9105 type is dynamic and can only be determined by inspecting the
9106 object's tag. This means that we need to get the object's
9107 value first (EVAL_NORMAL) and then extract the actual object
9108 type from its tag.
9109
9110 Note that we cannot skip the final step where we extract
9111 the object type from its tag, because the EVAL_NORMAL phase
9112 results in dynamic components being resolved into fixed ones.
9113 This can cause problems when trying to print the type
9114 description of tagged types whose parent has a dynamic size:
9115 We use the type name of the "_parent" component in order
9116 to print the name of the ancestor type in the type description.
9117 If that component had a dynamic size, the resolution into
9118 a fixed type would result in the loss of that type name,
9119 thus preventing us from printing the name of the ancestor
9120 type in the type description. */
b79819ba
JB
9121 struct type *actual_type;
9122
0c1f74cf 9123 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
b79819ba
JB
9124 actual_type = type_from_tag (ada_value_tag (arg1));
9125 if (actual_type == NULL)
9126 /* If, for some reason, we were unable to determine
9127 the actual type from the tag, then use the static
9128 approximation that we just computed as a fallback.
9129 This can happen if the debugging information is
9130 incomplete, for instance. */
9131 actual_type = type;
9132
9133 return value_zero (actual_type, not_lval);
0c1f74cf
JB
9134 }
9135
4c4b4cd2
PH
9136 *pos += 4;
9137 return value_zero
9138 (to_static_fixed_type
9139 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
9140 not_lval);
9141 }
d2e4a39e 9142 else
4c4b4cd2 9143 {
284614f0
JB
9144 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
9145 arg1 = unwrap_value (arg1);
4c4b4cd2
PH
9146 return ada_to_fixed_value (arg1);
9147 }
9148
9149 case OP_FUNCALL:
9150 (*pos) += 2;
9151
9152 /* Allocate arg vector, including space for the function to be
9153 called in argvec[0] and a terminating NULL. */
9154 nargs = longest_to_int (exp->elts[pc + 1].longconst);
9155 argvec =
9156 (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
9157
9158 if (exp->elts[*pos].opcode == OP_VAR_VALUE
76a01679 9159 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
323e0a4a 9160 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2
PH
9161 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
9162 else
9163 {
9164 for (tem = 0; tem <= nargs; tem += 1)
9165 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9166 argvec[tem] = 0;
9167
9168 if (noside == EVAL_SKIP)
9169 goto nosideret;
9170 }
9171
ad82864c
JB
9172 if (ada_is_constrained_packed_array_type
9173 (desc_base_type (value_type (argvec[0]))))
4c4b4cd2 9174 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
284614f0
JB
9175 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
9176 && TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
9177 /* This is a packed array that has already been fixed, and
9178 therefore already coerced to a simple array. Nothing further
9179 to do. */
9180 ;
df407dfe
AC
9181 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_REF
9182 || (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
76a01679 9183 && VALUE_LVAL (argvec[0]) == lval_memory))
4c4b4cd2
PH
9184 argvec[0] = value_addr (argvec[0]);
9185
df407dfe 9186 type = ada_check_typedef (value_type (argvec[0]));
4c4b4cd2
PH
9187 if (TYPE_CODE (type) == TYPE_CODE_PTR)
9188 {
61ee279c 9189 switch (TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type))))
4c4b4cd2
PH
9190 {
9191 case TYPE_CODE_FUNC:
61ee279c 9192 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
9193 break;
9194 case TYPE_CODE_ARRAY:
9195 break;
9196 case TYPE_CODE_STRUCT:
9197 if (noside != EVAL_AVOID_SIDE_EFFECTS)
9198 argvec[0] = ada_value_ind (argvec[0]);
61ee279c 9199 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
9200 break;
9201 default:
323e0a4a 9202 error (_("cannot subscript or call something of type `%s'"),
df407dfe 9203 ada_type_name (value_type (argvec[0])));
4c4b4cd2
PH
9204 break;
9205 }
9206 }
9207
9208 switch (TYPE_CODE (type))
9209 {
9210 case TYPE_CODE_FUNC:
9211 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9212 return allocate_value (TYPE_TARGET_TYPE (type));
9213 return call_function_by_hand (argvec[0], nargs, argvec + 1);
9214 case TYPE_CODE_STRUCT:
9215 {
9216 int arity;
9217
4c4b4cd2
PH
9218 arity = ada_array_arity (type);
9219 type = ada_array_element_type (type, nargs);
9220 if (type == NULL)
323e0a4a 9221 error (_("cannot subscript or call a record"));
4c4b4cd2 9222 if (arity != nargs)
323e0a4a 9223 error (_("wrong number of subscripts; expecting %d"), arity);
4c4b4cd2 9224 if (noside == EVAL_AVOID_SIDE_EFFECTS)
0a07e705 9225 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
9226 return
9227 unwrap_value (ada_value_subscript
9228 (argvec[0], nargs, argvec + 1));
9229 }
9230 case TYPE_CODE_ARRAY:
9231 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9232 {
9233 type = ada_array_element_type (type, nargs);
9234 if (type == NULL)
323e0a4a 9235 error (_("element type of array unknown"));
4c4b4cd2 9236 else
0a07e705 9237 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
9238 }
9239 return
9240 unwrap_value (ada_value_subscript
9241 (ada_coerce_to_simple_array (argvec[0]),
9242 nargs, argvec + 1));
9243 case TYPE_CODE_PTR: /* Pointer to array */
9244 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
9245 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9246 {
9247 type = ada_array_element_type (type, nargs);
9248 if (type == NULL)
323e0a4a 9249 error (_("element type of array unknown"));
4c4b4cd2 9250 else
0a07e705 9251 return value_zero (ada_aligned_type (type), lval_memory);
4c4b4cd2
PH
9252 }
9253 return
9254 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
9255 nargs, argvec + 1));
9256
9257 default:
e1d5a0d2
PH
9258 error (_("Attempt to index or call something other than an "
9259 "array or function"));
4c4b4cd2
PH
9260 }
9261
9262 case TERNOP_SLICE:
9263 {
9264 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9265 struct value *low_bound_val =
9266 evaluate_subexp (NULL_TYPE, exp, pos, noside);
714e53ab
PH
9267 struct value *high_bound_val =
9268 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9269 LONGEST low_bound;
9270 LONGEST high_bound;
5b4ee69b 9271
994b9211
AC
9272 low_bound_val = coerce_ref (low_bound_val);
9273 high_bound_val = coerce_ref (high_bound_val);
714e53ab
PH
9274 low_bound = pos_atr (low_bound_val);
9275 high_bound = pos_atr (high_bound_val);
963a6417 9276
4c4b4cd2
PH
9277 if (noside == EVAL_SKIP)
9278 goto nosideret;
9279
4c4b4cd2
PH
9280 /* If this is a reference to an aligner type, then remove all
9281 the aligners. */
df407dfe
AC
9282 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9283 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
9284 TYPE_TARGET_TYPE (value_type (array)) =
9285 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
4c4b4cd2 9286
ad82864c 9287 if (ada_is_constrained_packed_array_type (value_type (array)))
323e0a4a 9288 error (_("cannot slice a packed array"));
4c4b4cd2
PH
9289
9290 /* If this is a reference to an array or an array lvalue,
9291 convert to a pointer. */
df407dfe
AC
9292 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9293 || (TYPE_CODE (value_type (array)) == TYPE_CODE_ARRAY
4c4b4cd2
PH
9294 && VALUE_LVAL (array) == lval_memory))
9295 array = value_addr (array);
9296
1265e4aa 9297 if (noside == EVAL_AVOID_SIDE_EFFECTS
61ee279c 9298 && ada_is_array_descriptor_type (ada_check_typedef
df407dfe 9299 (value_type (array))))
0b5d8877 9300 return empty_array (ada_type_of_array (array, 0), low_bound);
4c4b4cd2
PH
9301
9302 array = ada_coerce_to_simple_array_ptr (array);
9303
714e53ab
PH
9304 /* If we have more than one level of pointer indirection,
9305 dereference the value until we get only one level. */
df407dfe
AC
9306 while (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR
9307 && (TYPE_CODE (TYPE_TARGET_TYPE (value_type (array)))
714e53ab
PH
9308 == TYPE_CODE_PTR))
9309 array = value_ind (array);
9310
9311 /* Make sure we really do have an array type before going further,
9312 to avoid a SEGV when trying to get the index type or the target
9313 type later down the road if the debug info generated by
9314 the compiler is incorrect or incomplete. */
df407dfe 9315 if (!ada_is_simple_array_type (value_type (array)))
323e0a4a 9316 error (_("cannot take slice of non-array"));
714e53ab 9317
df407dfe 9318 if (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR)
4c4b4cd2 9319 {
0b5d8877 9320 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 9321 return empty_array (TYPE_TARGET_TYPE (value_type (array)),
4c4b4cd2
PH
9322 low_bound);
9323 else
9324 {
9325 struct type *arr_type0 =
df407dfe 9326 to_fixed_array_type (TYPE_TARGET_TYPE (value_type (array)),
4c4b4cd2 9327 NULL, 1);
5b4ee69b 9328
f5938064
JG
9329 return ada_value_slice_from_ptr (array, arr_type0,
9330 longest_to_int (low_bound),
9331 longest_to_int (high_bound));
4c4b4cd2
PH
9332 }
9333 }
9334 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9335 return array;
9336 else if (high_bound < low_bound)
df407dfe 9337 return empty_array (value_type (array), low_bound);
4c4b4cd2 9338 else
529cad9c
PH
9339 return ada_value_slice (array, longest_to_int (low_bound),
9340 longest_to_int (high_bound));
4c4b4cd2 9341 }
14f9c5c9 9342
4c4b4cd2
PH
9343 case UNOP_IN_RANGE:
9344 (*pos) += 2;
9345 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8008e265 9346 type = check_typedef (exp->elts[pc + 1].type);
14f9c5c9 9347
14f9c5c9 9348 if (noside == EVAL_SKIP)
4c4b4cd2 9349 goto nosideret;
14f9c5c9 9350
4c4b4cd2
PH
9351 switch (TYPE_CODE (type))
9352 {
9353 default:
e1d5a0d2
PH
9354 lim_warning (_("Membership test incompletely implemented; "
9355 "always returns true"));
fbb06eb1
UW
9356 type = language_bool_type (exp->language_defn, exp->gdbarch);
9357 return value_from_longest (type, (LONGEST) 1);
4c4b4cd2
PH
9358
9359 case TYPE_CODE_RANGE:
030b4912
UW
9360 arg2 = value_from_longest (type, TYPE_LOW_BOUND (type));
9361 arg3 = value_from_longest (type, TYPE_HIGH_BOUND (type));
f44316fa
UW
9362 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9363 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1
UW
9364 type = language_bool_type (exp->language_defn, exp->gdbarch);
9365 return
9366 value_from_longest (type,
4c4b4cd2
PH
9367 (value_less (arg1, arg3)
9368 || value_equal (arg1, arg3))
9369 && (value_less (arg2, arg1)
9370 || value_equal (arg2, arg1)));
9371 }
9372
9373 case BINOP_IN_BOUNDS:
14f9c5c9 9374 (*pos) += 2;
4c4b4cd2
PH
9375 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9376 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 9377
4c4b4cd2
PH
9378 if (noside == EVAL_SKIP)
9379 goto nosideret;
14f9c5c9 9380
4c4b4cd2 9381 if (noside == EVAL_AVOID_SIDE_EFFECTS)
fbb06eb1
UW
9382 {
9383 type = language_bool_type (exp->language_defn, exp->gdbarch);
9384 return value_zero (type, not_lval);
9385 }
14f9c5c9 9386
4c4b4cd2 9387 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 9388
1eea4ebd
UW
9389 type = ada_index_type (value_type (arg2), tem, "range");
9390 if (!type)
9391 type = value_type (arg1);
14f9c5c9 9392
1eea4ebd
UW
9393 arg3 = value_from_longest (type, ada_array_bound (arg2, tem, 1));
9394 arg2 = value_from_longest (type, ada_array_bound (arg2, tem, 0));
d2e4a39e 9395
f44316fa
UW
9396 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9397 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1 9398 type = language_bool_type (exp->language_defn, exp->gdbarch);
4c4b4cd2 9399 return
fbb06eb1 9400 value_from_longest (type,
4c4b4cd2
PH
9401 (value_less (arg1, arg3)
9402 || value_equal (arg1, arg3))
9403 && (value_less (arg2, arg1)
9404 || value_equal (arg2, arg1)));
9405
9406 case TERNOP_IN_RANGE:
9407 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9408 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9409 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9410
9411 if (noside == EVAL_SKIP)
9412 goto nosideret;
9413
f44316fa
UW
9414 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9415 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
fbb06eb1 9416 type = language_bool_type (exp->language_defn, exp->gdbarch);
4c4b4cd2 9417 return
fbb06eb1 9418 value_from_longest (type,
4c4b4cd2
PH
9419 (value_less (arg1, arg3)
9420 || value_equal (arg1, arg3))
9421 && (value_less (arg2, arg1)
9422 || value_equal (arg2, arg1)));
9423
9424 case OP_ATR_FIRST:
9425 case OP_ATR_LAST:
9426 case OP_ATR_LENGTH:
9427 {
76a01679 9428 struct type *type_arg;
5b4ee69b 9429
76a01679
JB
9430 if (exp->elts[*pos].opcode == OP_TYPE)
9431 {
9432 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9433 arg1 = NULL;
5bc23cb3 9434 type_arg = check_typedef (exp->elts[pc + 2].type);
76a01679
JB
9435 }
9436 else
9437 {
9438 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9439 type_arg = NULL;
9440 }
9441
9442 if (exp->elts[*pos].opcode != OP_LONG)
323e0a4a 9443 error (_("Invalid operand to '%s"), ada_attribute_name (op));
76a01679
JB
9444 tem = longest_to_int (exp->elts[*pos + 2].longconst);
9445 *pos += 4;
9446
9447 if (noside == EVAL_SKIP)
9448 goto nosideret;
9449
9450 if (type_arg == NULL)
9451 {
9452 arg1 = ada_coerce_ref (arg1);
9453
ad82864c 9454 if (ada_is_constrained_packed_array_type (value_type (arg1)))
76a01679
JB
9455 arg1 = ada_coerce_to_simple_array (arg1);
9456
1eea4ebd
UW
9457 type = ada_index_type (value_type (arg1), tem,
9458 ada_attribute_name (op));
9459 if (type == NULL)
9460 type = builtin_type (exp->gdbarch)->builtin_int;
76a01679
JB
9461
9462 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1eea4ebd 9463 return allocate_value (type);
76a01679
JB
9464
9465 switch (op)
9466 {
9467 default: /* Should never happen. */
323e0a4a 9468 error (_("unexpected attribute encountered"));
76a01679 9469 case OP_ATR_FIRST:
1eea4ebd
UW
9470 return value_from_longest
9471 (type, ada_array_bound (arg1, tem, 0));
76a01679 9472 case OP_ATR_LAST:
1eea4ebd
UW
9473 return value_from_longest
9474 (type, ada_array_bound (arg1, tem, 1));
76a01679 9475 case OP_ATR_LENGTH:
1eea4ebd
UW
9476 return value_from_longest
9477 (type, ada_array_length (arg1, tem));
76a01679
JB
9478 }
9479 }
9480 else if (discrete_type_p (type_arg))
9481 {
9482 struct type *range_type;
9483 char *name = ada_type_name (type_arg);
5b4ee69b 9484
76a01679
JB
9485 range_type = NULL;
9486 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
28c85d6c 9487 range_type = to_fixed_range_type (type_arg, NULL);
76a01679
JB
9488 if (range_type == NULL)
9489 range_type = type_arg;
9490 switch (op)
9491 {
9492 default:
323e0a4a 9493 error (_("unexpected attribute encountered"));
76a01679 9494 case OP_ATR_FIRST:
690cc4eb 9495 return value_from_longest
43bbcdc2 9496 (range_type, ada_discrete_type_low_bound (range_type));
76a01679 9497 case OP_ATR_LAST:
690cc4eb 9498 return value_from_longest
43bbcdc2 9499 (range_type, ada_discrete_type_high_bound (range_type));
76a01679 9500 case OP_ATR_LENGTH:
323e0a4a 9501 error (_("the 'length attribute applies only to array types"));
76a01679
JB
9502 }
9503 }
9504 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
323e0a4a 9505 error (_("unimplemented type attribute"));
76a01679
JB
9506 else
9507 {
9508 LONGEST low, high;
9509
ad82864c
JB
9510 if (ada_is_constrained_packed_array_type (type_arg))
9511 type_arg = decode_constrained_packed_array_type (type_arg);
76a01679 9512
1eea4ebd 9513 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
76a01679 9514 if (type == NULL)
1eea4ebd
UW
9515 type = builtin_type (exp->gdbarch)->builtin_int;
9516
76a01679
JB
9517 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9518 return allocate_value (type);
9519
9520 switch (op)
9521 {
9522 default:
323e0a4a 9523 error (_("unexpected attribute encountered"));
76a01679 9524 case OP_ATR_FIRST:
1eea4ebd 9525 low = ada_array_bound_from_type (type_arg, tem, 0);
76a01679
JB
9526 return value_from_longest (type, low);
9527 case OP_ATR_LAST:
1eea4ebd 9528 high = ada_array_bound_from_type (type_arg, tem, 1);
76a01679
JB
9529 return value_from_longest (type, high);
9530 case OP_ATR_LENGTH:
1eea4ebd
UW
9531 low = ada_array_bound_from_type (type_arg, tem, 0);
9532 high = ada_array_bound_from_type (type_arg, tem, 1);
76a01679
JB
9533 return value_from_longest (type, high - low + 1);
9534 }
9535 }
14f9c5c9
AS
9536 }
9537
4c4b4cd2
PH
9538 case OP_ATR_TAG:
9539 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9540 if (noside == EVAL_SKIP)
76a01679 9541 goto nosideret;
4c4b4cd2
PH
9542
9543 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9544 return value_zero (ada_tag_type (arg1), not_lval);
4c4b4cd2
PH
9545
9546 return ada_value_tag (arg1);
9547
9548 case OP_ATR_MIN:
9549 case OP_ATR_MAX:
9550 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
9551 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9552 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9553 if (noside == EVAL_SKIP)
76a01679 9554 goto nosideret;
d2e4a39e 9555 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 9556 return value_zero (value_type (arg1), not_lval);
14f9c5c9 9557 else
f44316fa
UW
9558 {
9559 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9560 return value_binop (arg1, arg2,
9561 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
9562 }
14f9c5c9 9563
4c4b4cd2
PH
9564 case OP_ATR_MODULUS:
9565 {
31dedfee 9566 struct type *type_arg = check_typedef (exp->elts[pc + 2].type);
4c4b4cd2 9567
5b4ee69b 9568 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
76a01679
JB
9569 if (noside == EVAL_SKIP)
9570 goto nosideret;
4c4b4cd2 9571
76a01679 9572 if (!ada_is_modular_type (type_arg))
323e0a4a 9573 error (_("'modulus must be applied to modular type"));
4c4b4cd2 9574
76a01679
JB
9575 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
9576 ada_modulus (type_arg));
4c4b4cd2
PH
9577 }
9578
9579
9580 case OP_ATR_POS:
9581 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
9582 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9583 if (noside == EVAL_SKIP)
76a01679 9584 goto nosideret;
3cb382c9
UW
9585 type = builtin_type (exp->gdbarch)->builtin_int;
9586 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9587 return value_zero (type, not_lval);
14f9c5c9 9588 else
3cb382c9 9589 return value_pos_atr (type, arg1);
14f9c5c9 9590
4c4b4cd2
PH
9591 case OP_ATR_SIZE:
9592 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8c1c099f
JB
9593 type = value_type (arg1);
9594
9595 /* If the argument is a reference, then dereference its type, since
9596 the user is really asking for the size of the actual object,
9597 not the size of the pointer. */
9598 if (TYPE_CODE (type) == TYPE_CODE_REF)
9599 type = TYPE_TARGET_TYPE (type);
9600
4c4b4cd2 9601 if (noside == EVAL_SKIP)
76a01679 9602 goto nosideret;
4c4b4cd2 9603 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
22601c15 9604 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
4c4b4cd2 9605 else
22601c15 9606 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
8c1c099f 9607 TARGET_CHAR_BIT * TYPE_LENGTH (type));
4c4b4cd2
PH
9608
9609 case OP_ATR_VAL:
9610 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9 9611 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4c4b4cd2 9612 type = exp->elts[pc + 2].type;
14f9c5c9 9613 if (noside == EVAL_SKIP)
76a01679 9614 goto nosideret;
4c4b4cd2 9615 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9616 return value_zero (type, not_lval);
4c4b4cd2 9617 else
76a01679 9618 return value_val_atr (type, arg1);
4c4b4cd2
PH
9619
9620 case BINOP_EXP:
9621 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9622 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9623 if (noside == EVAL_SKIP)
9624 goto nosideret;
9625 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 9626 return value_zero (value_type (arg1), not_lval);
4c4b4cd2 9627 else
f44316fa
UW
9628 {
9629 /* For integer exponentiation operations,
9630 only promote the first argument. */
9631 if (is_integral_type (value_type (arg2)))
9632 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9633 else
9634 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9635
9636 return value_binop (arg1, arg2, op);
9637 }
4c4b4cd2
PH
9638
9639 case UNOP_PLUS:
9640 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9641 if (noside == EVAL_SKIP)
9642 goto nosideret;
9643 else
9644 return arg1;
9645
9646 case UNOP_ABS:
9647 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9648 if (noside == EVAL_SKIP)
9649 goto nosideret;
f44316fa 9650 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
df407dfe 9651 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
4c4b4cd2 9652 return value_neg (arg1);
14f9c5c9 9653 else
4c4b4cd2 9654 return arg1;
14f9c5c9
AS
9655
9656 case UNOP_IND:
6b0d7253 9657 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 9658 if (noside == EVAL_SKIP)
4c4b4cd2 9659 goto nosideret;
df407dfe 9660 type = ada_check_typedef (value_type (arg1));
14f9c5c9 9661 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
9662 {
9663 if (ada_is_array_descriptor_type (type))
9664 /* GDB allows dereferencing GNAT array descriptors. */
9665 {
9666 struct type *arrType = ada_type_of_array (arg1, 0);
5b4ee69b 9667
4c4b4cd2 9668 if (arrType == NULL)
323e0a4a 9669 error (_("Attempt to dereference null array pointer."));
00a4c844 9670 return value_at_lazy (arrType, 0);
4c4b4cd2
PH
9671 }
9672 else if (TYPE_CODE (type) == TYPE_CODE_PTR
9673 || TYPE_CODE (type) == TYPE_CODE_REF
9674 /* In C you can dereference an array to get the 1st elt. */
9675 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
714e53ab
PH
9676 {
9677 type = to_static_fixed_type
9678 (ada_aligned_type
9679 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
9680 check_size (type);
9681 return value_zero (type, lval_memory);
9682 }
4c4b4cd2 9683 else if (TYPE_CODE (type) == TYPE_CODE_INT)
6b0d7253
JB
9684 {
9685 /* GDB allows dereferencing an int. */
9686 if (expect_type == NULL)
9687 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
9688 lval_memory);
9689 else
9690 {
9691 expect_type =
9692 to_static_fixed_type (ada_aligned_type (expect_type));
9693 return value_zero (expect_type, lval_memory);
9694 }
9695 }
4c4b4cd2 9696 else
323e0a4a 9697 error (_("Attempt to take contents of a non-pointer value."));
4c4b4cd2 9698 }
76a01679 9699 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
df407dfe 9700 type = ada_check_typedef (value_type (arg1));
d2e4a39e 9701
96967637
JB
9702 if (TYPE_CODE (type) == TYPE_CODE_INT)
9703 /* GDB allows dereferencing an int. If we were given
9704 the expect_type, then use that as the target type.
9705 Otherwise, assume that the target type is an int. */
9706 {
9707 if (expect_type != NULL)
9708 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
9709 arg1));
9710 else
9711 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
9712 (CORE_ADDR) value_as_address (arg1));
9713 }
6b0d7253 9714
4c4b4cd2
PH
9715 if (ada_is_array_descriptor_type (type))
9716 /* GDB allows dereferencing GNAT array descriptors. */
9717 return ada_coerce_to_simple_array (arg1);
14f9c5c9 9718 else
4c4b4cd2 9719 return ada_value_ind (arg1);
14f9c5c9
AS
9720
9721 case STRUCTOP_STRUCT:
9722 tem = longest_to_int (exp->elts[pc + 1].longconst);
9723 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
9724 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9725 if (noside == EVAL_SKIP)
4c4b4cd2 9726 goto nosideret;
14f9c5c9 9727 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9728 {
df407dfe 9729 struct type *type1 = value_type (arg1);
5b4ee69b 9730
76a01679
JB
9731 if (ada_is_tagged_type (type1, 1))
9732 {
9733 type = ada_lookup_struct_elt_type (type1,
9734 &exp->elts[pc + 2].string,
9735 1, 1, NULL);
9736 if (type == NULL)
9737 /* In this case, we assume that the field COULD exist
9738 in some extension of the type. Return an object of
9739 "type" void, which will match any formal
9740 (see ada_type_match). */
30b15541
UW
9741 return value_zero (builtin_type (exp->gdbarch)->builtin_void,
9742 lval_memory);
76a01679
JB
9743 }
9744 else
9745 type =
9746 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
9747 0, NULL);
9748
9749 return value_zero (ada_aligned_type (type), lval_memory);
9750 }
14f9c5c9 9751 else
284614f0
JB
9752 arg1 = ada_value_struct_elt (arg1, &exp->elts[pc + 2].string, 0);
9753 arg1 = unwrap_value (arg1);
9754 return ada_to_fixed_value (arg1);
9755
14f9c5c9 9756 case OP_TYPE:
4c4b4cd2
PH
9757 /* The value is not supposed to be used. This is here to make it
9758 easier to accommodate expressions that contain types. */
14f9c5c9
AS
9759 (*pos) += 2;
9760 if (noside == EVAL_SKIP)
4c4b4cd2 9761 goto nosideret;
14f9c5c9 9762 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
a6cfbe68 9763 return allocate_value (exp->elts[pc + 1].type);
14f9c5c9 9764 else
323e0a4a 9765 error (_("Attempt to use a type name as an expression"));
52ce6436
PH
9766
9767 case OP_AGGREGATE:
9768 case OP_CHOICES:
9769 case OP_OTHERS:
9770 case OP_DISCRETE_RANGE:
9771 case OP_POSITIONAL:
9772 case OP_NAME:
9773 if (noside == EVAL_NORMAL)
9774 switch (op)
9775 {
9776 case OP_NAME:
9777 error (_("Undefined name, ambiguous name, or renaming used in "
e1d5a0d2 9778 "component association: %s."), &exp->elts[pc+2].string);
52ce6436
PH
9779 case OP_AGGREGATE:
9780 error (_("Aggregates only allowed on the right of an assignment"));
9781 default:
e1d5a0d2 9782 internal_error (__FILE__, __LINE__, _("aggregate apparently mangled"));
52ce6436
PH
9783 }
9784
9785 ada_forward_operator_length (exp, pc, &oplen, &nargs);
9786 *pos += oplen - 1;
9787 for (tem = 0; tem < nargs; tem += 1)
9788 ada_evaluate_subexp (NULL, exp, pos, noside);
9789 goto nosideret;
14f9c5c9
AS
9790 }
9791
9792nosideret:
22601c15 9793 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
14f9c5c9 9794}
14f9c5c9 9795\f
d2e4a39e 9796
4c4b4cd2 9797 /* Fixed point */
14f9c5c9
AS
9798
9799/* If TYPE encodes an Ada fixed-point type, return the suffix of the
9800 type name that encodes the 'small and 'delta information.
4c4b4cd2 9801 Otherwise, return NULL. */
14f9c5c9 9802
d2e4a39e 9803static const char *
ebf56fd3 9804fixed_type_info (struct type *type)
14f9c5c9 9805{
d2e4a39e 9806 const char *name = ada_type_name (type);
14f9c5c9
AS
9807 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
9808
d2e4a39e
AS
9809 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
9810 {
14f9c5c9 9811 const char *tail = strstr (name, "___XF_");
5b4ee69b 9812
14f9c5c9 9813 if (tail == NULL)
4c4b4cd2 9814 return NULL;
d2e4a39e 9815 else
4c4b4cd2 9816 return tail + 5;
14f9c5c9
AS
9817 }
9818 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
9819 return fixed_type_info (TYPE_TARGET_TYPE (type));
9820 else
9821 return NULL;
9822}
9823
4c4b4cd2 9824/* Returns non-zero iff TYPE represents an Ada fixed-point type. */
14f9c5c9
AS
9825
9826int
ebf56fd3 9827ada_is_fixed_point_type (struct type *type)
14f9c5c9
AS
9828{
9829 return fixed_type_info (type) != NULL;
9830}
9831
4c4b4cd2
PH
9832/* Return non-zero iff TYPE represents a System.Address type. */
9833
9834int
9835ada_is_system_address_type (struct type *type)
9836{
9837 return (TYPE_NAME (type)
9838 && strcmp (TYPE_NAME (type), "system__address") == 0);
9839}
9840
14f9c5c9
AS
9841/* Assuming that TYPE is the representation of an Ada fixed-point
9842 type, return its delta, or -1 if the type is malformed and the
4c4b4cd2 9843 delta cannot be determined. */
14f9c5c9
AS
9844
9845DOUBLEST
ebf56fd3 9846ada_delta (struct type *type)
14f9c5c9
AS
9847{
9848 const char *encoding = fixed_type_info (type);
facc390f 9849 DOUBLEST num, den;
14f9c5c9 9850
facc390f
JB
9851 /* Strictly speaking, num and den are encoded as integer. However,
9852 they may not fit into a long, and they will have to be converted
9853 to DOUBLEST anyway. So scan them as DOUBLEST. */
9854 if (sscanf (encoding, "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
9855 &num, &den) < 2)
14f9c5c9 9856 return -1.0;
d2e4a39e 9857 else
facc390f 9858 return num / den;
14f9c5c9
AS
9859}
9860
9861/* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
4c4b4cd2 9862 factor ('SMALL value) associated with the type. */
14f9c5c9
AS
9863
9864static DOUBLEST
ebf56fd3 9865scaling_factor (struct type *type)
14f9c5c9
AS
9866{
9867 const char *encoding = fixed_type_info (type);
facc390f 9868 DOUBLEST num0, den0, num1, den1;
14f9c5c9 9869 int n;
d2e4a39e 9870
facc390f
JB
9871 /* Strictly speaking, num's and den's are encoded as integer. However,
9872 they may not fit into a long, and they will have to be converted
9873 to DOUBLEST anyway. So scan them as DOUBLEST. */
9874 n = sscanf (encoding,
9875 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT
9876 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
9877 &num0, &den0, &num1, &den1);
14f9c5c9
AS
9878
9879 if (n < 2)
9880 return 1.0;
9881 else if (n == 4)
facc390f 9882 return num1 / den1;
d2e4a39e 9883 else
facc390f 9884 return num0 / den0;
14f9c5c9
AS
9885}
9886
9887
9888/* Assuming that X is the representation of a value of fixed-point
4c4b4cd2 9889 type TYPE, return its floating-point equivalent. */
14f9c5c9
AS
9890
9891DOUBLEST
ebf56fd3 9892ada_fixed_to_float (struct type *type, LONGEST x)
14f9c5c9 9893{
d2e4a39e 9894 return (DOUBLEST) x *scaling_factor (type);
14f9c5c9
AS
9895}
9896
4c4b4cd2
PH
9897/* The representation of a fixed-point value of type TYPE
9898 corresponding to the value X. */
14f9c5c9
AS
9899
9900LONGEST
ebf56fd3 9901ada_float_to_fixed (struct type *type, DOUBLEST x)
14f9c5c9
AS
9902{
9903 return (LONGEST) (x / scaling_factor (type) + 0.5);
9904}
9905
14f9c5c9 9906\f
d2e4a39e 9907
4c4b4cd2 9908 /* Range types */
14f9c5c9
AS
9909
9910/* Scan STR beginning at position K for a discriminant name, and
9911 return the value of that discriminant field of DVAL in *PX. If
9912 PNEW_K is not null, put the position of the character beyond the
9913 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 9914 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
9915
9916static int
07d8f827 9917scan_discrim_bound (char *str, int k, struct value *dval, LONGEST * px,
76a01679 9918 int *pnew_k)
14f9c5c9
AS
9919{
9920 static char *bound_buffer = NULL;
9921 static size_t bound_buffer_len = 0;
9922 char *bound;
9923 char *pend;
d2e4a39e 9924 struct value *bound_val;
14f9c5c9
AS
9925
9926 if (dval == NULL || str == NULL || str[k] == '\0')
9927 return 0;
9928
d2e4a39e 9929 pend = strstr (str + k, "__");
14f9c5c9
AS
9930 if (pend == NULL)
9931 {
d2e4a39e 9932 bound = str + k;
14f9c5c9
AS
9933 k += strlen (bound);
9934 }
d2e4a39e 9935 else
14f9c5c9 9936 {
d2e4a39e 9937 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
14f9c5c9 9938 bound = bound_buffer;
d2e4a39e
AS
9939 strncpy (bound_buffer, str + k, pend - (str + k));
9940 bound[pend - (str + k)] = '\0';
9941 k = pend - str;
14f9c5c9 9942 }
d2e4a39e 9943
df407dfe 9944 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
9945 if (bound_val == NULL)
9946 return 0;
9947
9948 *px = value_as_long (bound_val);
9949 if (pnew_k != NULL)
9950 *pnew_k = k;
9951 return 1;
9952}
9953
9954/* Value of variable named NAME in the current environment. If
9955 no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
9956 otherwise causes an error with message ERR_MSG. */
9957
d2e4a39e
AS
9958static struct value *
9959get_var_value (char *name, char *err_msg)
14f9c5c9 9960{
4c4b4cd2 9961 struct ada_symbol_info *syms;
14f9c5c9
AS
9962 int nsyms;
9963
4c4b4cd2
PH
9964 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
9965 &syms);
14f9c5c9
AS
9966
9967 if (nsyms != 1)
9968 {
9969 if (err_msg == NULL)
4c4b4cd2 9970 return 0;
14f9c5c9 9971 else
8a3fe4f8 9972 error (("%s"), err_msg);
14f9c5c9
AS
9973 }
9974
4c4b4cd2 9975 return value_of_variable (syms[0].sym, syms[0].block);
14f9c5c9 9976}
d2e4a39e 9977
14f9c5c9 9978/* Value of integer variable named NAME in the current environment. If
4c4b4cd2
PH
9979 no such variable found, returns 0, and sets *FLAG to 0. If
9980 successful, sets *FLAG to 1. */
9981
14f9c5c9 9982LONGEST
4c4b4cd2 9983get_int_var_value (char *name, int *flag)
14f9c5c9 9984{
4c4b4cd2 9985 struct value *var_val = get_var_value (name, 0);
d2e4a39e 9986
14f9c5c9
AS
9987 if (var_val == 0)
9988 {
9989 if (flag != NULL)
4c4b4cd2 9990 *flag = 0;
14f9c5c9
AS
9991 return 0;
9992 }
9993 else
9994 {
9995 if (flag != NULL)
4c4b4cd2 9996 *flag = 1;
14f9c5c9
AS
9997 return value_as_long (var_val);
9998 }
9999}
d2e4a39e 10000
14f9c5c9
AS
10001
10002/* Return a range type whose base type is that of the range type named
10003 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 10004 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
10005 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
10006 corresponding range type from debug information; fall back to using it
10007 if symbol lookup fails. If a new type must be created, allocate it
10008 like ORIG_TYPE was. The bounds information, in general, is encoded
10009 in NAME, the base type given in the named range type. */
14f9c5c9 10010
d2e4a39e 10011static struct type *
28c85d6c 10012to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 10013{
28c85d6c 10014 char *name;
14f9c5c9 10015 struct type *base_type;
d2e4a39e 10016 char *subtype_info;
14f9c5c9 10017
28c85d6c
JB
10018 gdb_assert (raw_type != NULL);
10019 gdb_assert (TYPE_NAME (raw_type) != NULL);
dddfab26 10020
1ce677a4 10021 if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
14f9c5c9
AS
10022 base_type = TYPE_TARGET_TYPE (raw_type);
10023 else
10024 base_type = raw_type;
10025
28c85d6c 10026 name = TYPE_NAME (raw_type);
14f9c5c9
AS
10027 subtype_info = strstr (name, "___XD");
10028 if (subtype_info == NULL)
690cc4eb 10029 {
43bbcdc2
PH
10030 LONGEST L = ada_discrete_type_low_bound (raw_type);
10031 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 10032
690cc4eb
PH
10033 if (L < INT_MIN || U > INT_MAX)
10034 return raw_type;
10035 else
28c85d6c 10036 return create_range_type (alloc_type_copy (raw_type), raw_type,
43bbcdc2
PH
10037 ada_discrete_type_low_bound (raw_type),
10038 ada_discrete_type_high_bound (raw_type));
690cc4eb 10039 }
14f9c5c9
AS
10040 else
10041 {
10042 static char *name_buf = NULL;
10043 static size_t name_len = 0;
10044 int prefix_len = subtype_info - name;
10045 LONGEST L, U;
10046 struct type *type;
10047 char *bounds_str;
10048 int n;
10049
10050 GROW_VECT (name_buf, name_len, prefix_len + 5);
10051 strncpy (name_buf, name, prefix_len);
10052 name_buf[prefix_len] = '\0';
10053
10054 subtype_info += 5;
10055 bounds_str = strchr (subtype_info, '_');
10056 n = 1;
10057
d2e4a39e 10058 if (*subtype_info == 'L')
4c4b4cd2
PH
10059 {
10060 if (!ada_scan_number (bounds_str, n, &L, &n)
10061 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
10062 return raw_type;
10063 if (bounds_str[n] == '_')
10064 n += 2;
10065 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
10066 n += 1;
10067 subtype_info += 1;
10068 }
d2e4a39e 10069 else
4c4b4cd2
PH
10070 {
10071 int ok;
5b4ee69b 10072
4c4b4cd2
PH
10073 strcpy (name_buf + prefix_len, "___L");
10074 L = get_int_var_value (name_buf, &ok);
10075 if (!ok)
10076 {
323e0a4a 10077 lim_warning (_("Unknown lower bound, using 1."));
4c4b4cd2
PH
10078 L = 1;
10079 }
10080 }
14f9c5c9 10081
d2e4a39e 10082 if (*subtype_info == 'U')
4c4b4cd2
PH
10083 {
10084 if (!ada_scan_number (bounds_str, n, &U, &n)
10085 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
10086 return raw_type;
10087 }
d2e4a39e 10088 else
4c4b4cd2
PH
10089 {
10090 int ok;
5b4ee69b 10091
4c4b4cd2
PH
10092 strcpy (name_buf + prefix_len, "___U");
10093 U = get_int_var_value (name_buf, &ok);
10094 if (!ok)
10095 {
323e0a4a 10096 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
4c4b4cd2
PH
10097 U = L;
10098 }
10099 }
14f9c5c9 10100
28c85d6c 10101 type = create_range_type (alloc_type_copy (raw_type), base_type, L, U);
d2e4a39e 10102 TYPE_NAME (type) = name;
14f9c5c9
AS
10103 return type;
10104 }
10105}
10106
4c4b4cd2
PH
10107/* True iff NAME is the name of a range type. */
10108
14f9c5c9 10109int
d2e4a39e 10110ada_is_range_type_name (const char *name)
14f9c5c9
AS
10111{
10112 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 10113}
14f9c5c9 10114\f
d2e4a39e 10115
4c4b4cd2
PH
10116 /* Modular types */
10117
10118/* True iff TYPE is an Ada modular type. */
14f9c5c9 10119
14f9c5c9 10120int
d2e4a39e 10121ada_is_modular_type (struct type *type)
14f9c5c9 10122{
4c4b4cd2 10123 struct type *subranged_type = base_type (type);
14f9c5c9
AS
10124
10125 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
690cc4eb 10126 && TYPE_CODE (subranged_type) == TYPE_CODE_INT
4c4b4cd2 10127 && TYPE_UNSIGNED (subranged_type));
14f9c5c9
AS
10128}
10129
0056e4d5
JB
10130/* Try to determine the lower and upper bounds of the given modular type
10131 using the type name only. Return non-zero and set L and U as the lower
10132 and upper bounds (respectively) if successful. */
10133
10134int
10135ada_modulus_from_name (struct type *type, ULONGEST *modulus)
10136{
10137 char *name = ada_type_name (type);
10138 char *suffix;
10139 int k;
10140 LONGEST U;
10141
10142 if (name == NULL)
10143 return 0;
10144
10145 /* Discrete type bounds are encoded using an __XD suffix. In our case,
10146 we are looking for static bounds, which means an __XDLU suffix.
10147 Moreover, we know that the lower bound of modular types is always
10148 zero, so the actual suffix should start with "__XDLU_0__", and
10149 then be followed by the upper bound value. */
10150 suffix = strstr (name, "__XDLU_0__");
10151 if (suffix == NULL)
10152 return 0;
10153 k = 10;
10154 if (!ada_scan_number (suffix, k, &U, NULL))
10155 return 0;
10156
10157 *modulus = (ULONGEST) U + 1;
10158 return 1;
10159}
10160
4c4b4cd2
PH
10161/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
10162
61ee279c 10163ULONGEST
0056e4d5 10164ada_modulus (struct type *type)
14f9c5c9 10165{
43bbcdc2 10166 return (ULONGEST) TYPE_HIGH_BOUND (type) + 1;
14f9c5c9 10167}
d2e4a39e 10168\f
f7f9143b
JB
10169
10170/* Ada exception catchpoint support:
10171 ---------------------------------
10172
10173 We support 3 kinds of exception catchpoints:
10174 . catchpoints on Ada exceptions
10175 . catchpoints on unhandled Ada exceptions
10176 . catchpoints on failed assertions
10177
10178 Exceptions raised during failed assertions, or unhandled exceptions
10179 could perfectly be caught with the general catchpoint on Ada exceptions.
10180 However, we can easily differentiate these two special cases, and having
10181 the option to distinguish these two cases from the rest can be useful
10182 to zero-in on certain situations.
10183
10184 Exception catchpoints are a specialized form of breakpoint,
10185 since they rely on inserting breakpoints inside known routines
10186 of the GNAT runtime. The implementation therefore uses a standard
10187 breakpoint structure of the BP_BREAKPOINT type, but with its own set
10188 of breakpoint_ops.
10189
0259addd
JB
10190 Support in the runtime for exception catchpoints have been changed
10191 a few times already, and these changes affect the implementation
10192 of these catchpoints. In order to be able to support several
10193 variants of the runtime, we use a sniffer that will determine
10194 the runtime variant used by the program being debugged.
10195
f7f9143b
JB
10196 At this time, we do not support the use of conditions on Ada exception
10197 catchpoints. The COND and COND_STRING fields are therefore set
10198 to NULL (most of the time, see below).
10199
10200 Conditions where EXP_STRING, COND, and COND_STRING are used:
10201
10202 When a user specifies the name of a specific exception in the case
10203 of catchpoints on Ada exceptions, we store the name of that exception
10204 in the EXP_STRING. We then translate this request into an actual
10205 condition stored in COND_STRING, and then parse it into an expression
10206 stored in COND. */
10207
10208/* The different types of catchpoints that we introduced for catching
10209 Ada exceptions. */
10210
10211enum exception_catchpoint_kind
10212{
10213 ex_catch_exception,
10214 ex_catch_exception_unhandled,
10215 ex_catch_assert
10216};
10217
3d0b0fa3
JB
10218/* Ada's standard exceptions. */
10219
10220static char *standard_exc[] = {
10221 "constraint_error",
10222 "program_error",
10223 "storage_error",
10224 "tasking_error"
10225};
10226
0259addd
JB
10227typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
10228
10229/* A structure that describes how to support exception catchpoints
10230 for a given executable. */
10231
10232struct exception_support_info
10233{
10234 /* The name of the symbol to break on in order to insert
10235 a catchpoint on exceptions. */
10236 const char *catch_exception_sym;
10237
10238 /* The name of the symbol to break on in order to insert
10239 a catchpoint on unhandled exceptions. */
10240 const char *catch_exception_unhandled_sym;
10241
10242 /* The name of the symbol to break on in order to insert
10243 a catchpoint on failed assertions. */
10244 const char *catch_assert_sym;
10245
10246 /* Assuming that the inferior just triggered an unhandled exception
10247 catchpoint, this function is responsible for returning the address
10248 in inferior memory where the name of that exception is stored.
10249 Return zero if the address could not be computed. */
10250 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
10251};
10252
10253static CORE_ADDR ada_unhandled_exception_name_addr (void);
10254static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
10255
10256/* The following exception support info structure describes how to
10257 implement exception catchpoints with the latest version of the
10258 Ada runtime (as of 2007-03-06). */
10259
10260static const struct exception_support_info default_exception_support_info =
10261{
10262 "__gnat_debug_raise_exception", /* catch_exception_sym */
10263 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10264 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
10265 ada_unhandled_exception_name_addr
10266};
10267
10268/* The following exception support info structure describes how to
10269 implement exception catchpoints with a slightly older version
10270 of the Ada runtime. */
10271
10272static const struct exception_support_info exception_support_info_fallback =
10273{
10274 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
10275 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10276 "system__assertions__raise_assert_failure", /* catch_assert_sym */
10277 ada_unhandled_exception_name_addr_from_raise
10278};
10279
10280/* For each executable, we sniff which exception info structure to use
10281 and cache it in the following global variable. */
10282
10283static const struct exception_support_info *exception_info = NULL;
10284
10285/* Inspect the Ada runtime and determine which exception info structure
10286 should be used to provide support for exception catchpoints.
10287
10288 This function will always set exception_info, or raise an error. */
10289
10290static void
10291ada_exception_support_info_sniffer (void)
10292{
10293 struct symbol *sym;
10294
10295 /* If the exception info is already known, then no need to recompute it. */
10296 if (exception_info != NULL)
10297 return;
10298
10299 /* Check the latest (default) exception support info. */
10300 sym = standard_lookup (default_exception_support_info.catch_exception_sym,
10301 NULL, VAR_DOMAIN);
10302 if (sym != NULL)
10303 {
10304 exception_info = &default_exception_support_info;
10305 return;
10306 }
10307
10308 /* Try our fallback exception suport info. */
10309 sym = standard_lookup (exception_support_info_fallback.catch_exception_sym,
10310 NULL, VAR_DOMAIN);
10311 if (sym != NULL)
10312 {
10313 exception_info = &exception_support_info_fallback;
10314 return;
10315 }
10316
10317 /* Sometimes, it is normal for us to not be able to find the routine
10318 we are looking for. This happens when the program is linked with
10319 the shared version of the GNAT runtime, and the program has not been
10320 started yet. Inform the user of these two possible causes if
10321 applicable. */
10322
ccefe4c4 10323 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
10324 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
10325
10326 /* If the symbol does not exist, then check that the program is
10327 already started, to make sure that shared libraries have been
10328 loaded. If it is not started, this may mean that the symbol is
10329 in a shared library. */
10330
10331 if (ptid_get_pid (inferior_ptid) == 0)
10332 error (_("Unable to insert catchpoint. Try to start the program first."));
10333
10334 /* At this point, we know that we are debugging an Ada program and
10335 that the inferior has been started, but we still are not able to
10336 find the run-time symbols. That can mean that we are in
10337 configurable run time mode, or that a-except as been optimized
10338 out by the linker... In any case, at this point it is not worth
10339 supporting this feature. */
10340
10341 error (_("Cannot insert catchpoints in this configuration."));
10342}
10343
10344/* An observer of "executable_changed" events.
10345 Its role is to clear certain cached values that need to be recomputed
10346 each time a new executable is loaded by GDB. */
10347
10348static void
781b42b0 10349ada_executable_changed_observer (void)
0259addd
JB
10350{
10351 /* If the executable changed, then it is possible that the Ada runtime
10352 is different. So we need to invalidate the exception support info
10353 cache. */
10354 exception_info = NULL;
10355}
10356
f7f9143b
JB
10357/* True iff FRAME is very likely to be that of a function that is
10358 part of the runtime system. This is all very heuristic, but is
10359 intended to be used as advice as to what frames are uninteresting
10360 to most users. */
10361
10362static int
10363is_known_support_routine (struct frame_info *frame)
10364{
4ed6b5be 10365 struct symtab_and_line sal;
f7f9143b 10366 char *func_name;
692465f1 10367 enum language func_lang;
f7f9143b 10368 int i;
f7f9143b 10369
4ed6b5be
JB
10370 /* If this code does not have any debugging information (no symtab),
10371 This cannot be any user code. */
f7f9143b 10372
4ed6b5be 10373 find_frame_sal (frame, &sal);
f7f9143b
JB
10374 if (sal.symtab == NULL)
10375 return 1;
10376
4ed6b5be
JB
10377 /* If there is a symtab, but the associated source file cannot be
10378 located, then assume this is not user code: Selecting a frame
10379 for which we cannot display the code would not be very helpful
10380 for the user. This should also take care of case such as VxWorks
10381 where the kernel has some debugging info provided for a few units. */
f7f9143b 10382
9bbc9174 10383 if (symtab_to_fullname (sal.symtab) == NULL)
f7f9143b
JB
10384 return 1;
10385
4ed6b5be
JB
10386 /* Check the unit filename againt the Ada runtime file naming.
10387 We also check the name of the objfile against the name of some
10388 known system libraries that sometimes come with debugging info
10389 too. */
10390
f7f9143b
JB
10391 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
10392 {
10393 re_comp (known_runtime_file_name_patterns[i]);
10394 if (re_exec (sal.symtab->filename))
10395 return 1;
4ed6b5be
JB
10396 if (sal.symtab->objfile != NULL
10397 && re_exec (sal.symtab->objfile->name))
10398 return 1;
f7f9143b
JB
10399 }
10400
4ed6b5be 10401 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 10402
e9e07ba6 10403 find_frame_funname (frame, &func_name, &func_lang, NULL);
f7f9143b
JB
10404 if (func_name == NULL)
10405 return 1;
10406
10407 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
10408 {
10409 re_comp (known_auxiliary_function_name_patterns[i]);
10410 if (re_exec (func_name))
10411 return 1;
10412 }
10413
10414 return 0;
10415}
10416
10417/* Find the first frame that contains debugging information and that is not
10418 part of the Ada run-time, starting from FI and moving upward. */
10419
0ef643c8 10420void
f7f9143b
JB
10421ada_find_printable_frame (struct frame_info *fi)
10422{
10423 for (; fi != NULL; fi = get_prev_frame (fi))
10424 {
10425 if (!is_known_support_routine (fi))
10426 {
10427 select_frame (fi);
10428 break;
10429 }
10430 }
10431
10432}
10433
10434/* Assuming that the inferior just triggered an unhandled exception
10435 catchpoint, return the address in inferior memory where the name
10436 of the exception is stored.
10437
10438 Return zero if the address could not be computed. */
10439
10440static CORE_ADDR
10441ada_unhandled_exception_name_addr (void)
0259addd
JB
10442{
10443 return parse_and_eval_address ("e.full_name");
10444}
10445
10446/* Same as ada_unhandled_exception_name_addr, except that this function
10447 should be used when the inferior uses an older version of the runtime,
10448 where the exception name needs to be extracted from a specific frame
10449 several frames up in the callstack. */
10450
10451static CORE_ADDR
10452ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
10453{
10454 int frame_level;
10455 struct frame_info *fi;
10456
10457 /* To determine the name of this exception, we need to select
10458 the frame corresponding to RAISE_SYM_NAME. This frame is
10459 at least 3 levels up, so we simply skip the first 3 frames
10460 without checking the name of their associated function. */
10461 fi = get_current_frame ();
10462 for (frame_level = 0; frame_level < 3; frame_level += 1)
10463 if (fi != NULL)
10464 fi = get_prev_frame (fi);
10465
10466 while (fi != NULL)
10467 {
692465f1
JB
10468 char *func_name;
10469 enum language func_lang;
10470
e9e07ba6 10471 find_frame_funname (fi, &func_name, &func_lang, NULL);
f7f9143b 10472 if (func_name != NULL
0259addd 10473 && strcmp (func_name, exception_info->catch_exception_sym) == 0)
f7f9143b
JB
10474 break; /* We found the frame we were looking for... */
10475 fi = get_prev_frame (fi);
10476 }
10477
10478 if (fi == NULL)
10479 return 0;
10480
10481 select_frame (fi);
10482 return parse_and_eval_address ("id.full_name");
10483}
10484
10485/* Assuming the inferior just triggered an Ada exception catchpoint
10486 (of any type), return the address in inferior memory where the name
10487 of the exception is stored, if applicable.
10488
10489 Return zero if the address could not be computed, or if not relevant. */
10490
10491static CORE_ADDR
10492ada_exception_name_addr_1 (enum exception_catchpoint_kind ex,
10493 struct breakpoint *b)
10494{
10495 switch (ex)
10496 {
10497 case ex_catch_exception:
10498 return (parse_and_eval_address ("e.full_name"));
10499 break;
10500
10501 case ex_catch_exception_unhandled:
0259addd 10502 return exception_info->unhandled_exception_name_addr ();
f7f9143b
JB
10503 break;
10504
10505 case ex_catch_assert:
10506 return 0; /* Exception name is not relevant in this case. */
10507 break;
10508
10509 default:
10510 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10511 break;
10512 }
10513
10514 return 0; /* Should never be reached. */
10515}
10516
10517/* Same as ada_exception_name_addr_1, except that it intercepts and contains
10518 any error that ada_exception_name_addr_1 might cause to be thrown.
10519 When an error is intercepted, a warning with the error message is printed,
10520 and zero is returned. */
10521
10522static CORE_ADDR
10523ada_exception_name_addr (enum exception_catchpoint_kind ex,
10524 struct breakpoint *b)
10525{
10526 struct gdb_exception e;
10527 CORE_ADDR result = 0;
10528
10529 TRY_CATCH (e, RETURN_MASK_ERROR)
10530 {
10531 result = ada_exception_name_addr_1 (ex, b);
10532 }
10533
10534 if (e.reason < 0)
10535 {
10536 warning (_("failed to get exception name: %s"), e.message);
10537 return 0;
10538 }
10539
10540 return result;
10541}
10542
10543/* Implement the PRINT_IT method in the breakpoint_ops structure
10544 for all exception catchpoint kinds. */
10545
10546static enum print_stop_action
10547print_it_exception (enum exception_catchpoint_kind ex, struct breakpoint *b)
10548{
10549 const CORE_ADDR addr = ada_exception_name_addr (ex, b);
10550 char exception_name[256];
10551
10552 if (addr != 0)
10553 {
10554 read_memory (addr, exception_name, sizeof (exception_name) - 1);
10555 exception_name [sizeof (exception_name) - 1] = '\0';
10556 }
10557
10558 ada_find_printable_frame (get_current_frame ());
10559
10560 annotate_catchpoint (b->number);
10561 switch (ex)
10562 {
10563 case ex_catch_exception:
10564 if (addr != 0)
10565 printf_filtered (_("\nCatchpoint %d, %s at "),
10566 b->number, exception_name);
10567 else
10568 printf_filtered (_("\nCatchpoint %d, exception at "), b->number);
10569 break;
10570 case ex_catch_exception_unhandled:
10571 if (addr != 0)
10572 printf_filtered (_("\nCatchpoint %d, unhandled %s at "),
10573 b->number, exception_name);
10574 else
10575 printf_filtered (_("\nCatchpoint %d, unhandled exception at "),
10576 b->number);
10577 break;
10578 case ex_catch_assert:
10579 printf_filtered (_("\nCatchpoint %d, failed assertion at "),
10580 b->number);
10581 break;
10582 }
10583
10584 return PRINT_SRC_AND_LOC;
10585}
10586
10587/* Implement the PRINT_ONE method in the breakpoint_ops structure
10588 for all exception catchpoint kinds. */
10589
10590static void
10591print_one_exception (enum exception_catchpoint_kind ex,
a6d9a66e 10592 struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 10593{
79a45b7d
TT
10594 struct value_print_options opts;
10595
10596 get_user_print_options (&opts);
10597 if (opts.addressprint)
f7f9143b
JB
10598 {
10599 annotate_field (4);
5af949e3 10600 ui_out_field_core_addr (uiout, "addr", b->loc->gdbarch, b->loc->address);
f7f9143b
JB
10601 }
10602
10603 annotate_field (5);
a6d9a66e 10604 *last_loc = b->loc;
f7f9143b
JB
10605 switch (ex)
10606 {
10607 case ex_catch_exception:
10608 if (b->exp_string != NULL)
10609 {
10610 char *msg = xstrprintf (_("`%s' Ada exception"), b->exp_string);
10611
10612 ui_out_field_string (uiout, "what", msg);
10613 xfree (msg);
10614 }
10615 else
10616 ui_out_field_string (uiout, "what", "all Ada exceptions");
10617
10618 break;
10619
10620 case ex_catch_exception_unhandled:
10621 ui_out_field_string (uiout, "what", "unhandled Ada exceptions");
10622 break;
10623
10624 case ex_catch_assert:
10625 ui_out_field_string (uiout, "what", "failed Ada assertions");
10626 break;
10627
10628 default:
10629 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10630 break;
10631 }
10632}
10633
10634/* Implement the PRINT_MENTION method in the breakpoint_ops structure
10635 for all exception catchpoint kinds. */
10636
10637static void
10638print_mention_exception (enum exception_catchpoint_kind ex,
10639 struct breakpoint *b)
10640{
10641 switch (ex)
10642 {
10643 case ex_catch_exception:
10644 if (b->exp_string != NULL)
10645 printf_filtered (_("Catchpoint %d: `%s' Ada exception"),
10646 b->number, b->exp_string);
10647 else
10648 printf_filtered (_("Catchpoint %d: all Ada exceptions"), b->number);
10649
10650 break;
10651
10652 case ex_catch_exception_unhandled:
10653 printf_filtered (_("Catchpoint %d: unhandled Ada exceptions"),
10654 b->number);
10655 break;
10656
10657 case ex_catch_assert:
10658 printf_filtered (_("Catchpoint %d: failed Ada assertions"), b->number);
10659 break;
10660
10661 default:
10662 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10663 break;
10664 }
10665}
10666
6149aea9
PA
10667/* Implement the PRINT_RECREATE method in the breakpoint_ops structure
10668 for all exception catchpoint kinds. */
10669
10670static void
10671print_recreate_exception (enum exception_catchpoint_kind ex,
10672 struct breakpoint *b, struct ui_file *fp)
10673{
10674 switch (ex)
10675 {
10676 case ex_catch_exception:
10677 fprintf_filtered (fp, "catch exception");
10678 if (b->exp_string != NULL)
10679 fprintf_filtered (fp, " %s", b->exp_string);
10680 break;
10681
10682 case ex_catch_exception_unhandled:
78076abc 10683 fprintf_filtered (fp, "catch exception unhandled");
6149aea9
PA
10684 break;
10685
10686 case ex_catch_assert:
10687 fprintf_filtered (fp, "catch assert");
10688 break;
10689
10690 default:
10691 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10692 }
10693}
10694
f7f9143b
JB
10695/* Virtual table for "catch exception" breakpoints. */
10696
10697static enum print_stop_action
10698print_it_catch_exception (struct breakpoint *b)
10699{
10700 return print_it_exception (ex_catch_exception, b);
10701}
10702
10703static void
a6d9a66e 10704print_one_catch_exception (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 10705{
a6d9a66e 10706 print_one_exception (ex_catch_exception, b, last_loc);
f7f9143b
JB
10707}
10708
10709static void
10710print_mention_catch_exception (struct breakpoint *b)
10711{
10712 print_mention_exception (ex_catch_exception, b);
10713}
10714
6149aea9
PA
10715static void
10716print_recreate_catch_exception (struct breakpoint *b, struct ui_file *fp)
10717{
10718 print_recreate_exception (ex_catch_exception, b, fp);
10719}
10720
f7f9143b
JB
10721static struct breakpoint_ops catch_exception_breakpoint_ops =
10722{
ce78b96d
JB
10723 NULL, /* insert */
10724 NULL, /* remove */
10725 NULL, /* breakpoint_hit */
f7f9143b
JB
10726 print_it_catch_exception,
10727 print_one_catch_exception,
6149aea9
PA
10728 print_mention_catch_exception,
10729 print_recreate_catch_exception
f7f9143b
JB
10730};
10731
10732/* Virtual table for "catch exception unhandled" breakpoints. */
10733
10734static enum print_stop_action
10735print_it_catch_exception_unhandled (struct breakpoint *b)
10736{
10737 return print_it_exception (ex_catch_exception_unhandled, b);
10738}
10739
10740static void
a6d9a66e
UW
10741print_one_catch_exception_unhandled (struct breakpoint *b,
10742 struct bp_location **last_loc)
f7f9143b 10743{
a6d9a66e 10744 print_one_exception (ex_catch_exception_unhandled, b, last_loc);
f7f9143b
JB
10745}
10746
10747static void
10748print_mention_catch_exception_unhandled (struct breakpoint *b)
10749{
10750 print_mention_exception (ex_catch_exception_unhandled, b);
10751}
10752
6149aea9
PA
10753static void
10754print_recreate_catch_exception_unhandled (struct breakpoint *b,
10755 struct ui_file *fp)
10756{
10757 print_recreate_exception (ex_catch_exception_unhandled, b, fp);
10758}
10759
f7f9143b 10760static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops = {
ce78b96d
JB
10761 NULL, /* insert */
10762 NULL, /* remove */
10763 NULL, /* breakpoint_hit */
f7f9143b
JB
10764 print_it_catch_exception_unhandled,
10765 print_one_catch_exception_unhandled,
6149aea9
PA
10766 print_mention_catch_exception_unhandled,
10767 print_recreate_catch_exception_unhandled
f7f9143b
JB
10768};
10769
10770/* Virtual table for "catch assert" breakpoints. */
10771
10772static enum print_stop_action
10773print_it_catch_assert (struct breakpoint *b)
10774{
10775 return print_it_exception (ex_catch_assert, b);
10776}
10777
10778static void
a6d9a66e 10779print_one_catch_assert (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 10780{
a6d9a66e 10781 print_one_exception (ex_catch_assert, b, last_loc);
f7f9143b
JB
10782}
10783
10784static void
10785print_mention_catch_assert (struct breakpoint *b)
10786{
10787 print_mention_exception (ex_catch_assert, b);
10788}
10789
6149aea9
PA
10790static void
10791print_recreate_catch_assert (struct breakpoint *b, struct ui_file *fp)
10792{
10793 print_recreate_exception (ex_catch_assert, b, fp);
10794}
10795
f7f9143b 10796static struct breakpoint_ops catch_assert_breakpoint_ops = {
ce78b96d
JB
10797 NULL, /* insert */
10798 NULL, /* remove */
10799 NULL, /* breakpoint_hit */
f7f9143b
JB
10800 print_it_catch_assert,
10801 print_one_catch_assert,
6149aea9
PA
10802 print_mention_catch_assert,
10803 print_recreate_catch_assert
f7f9143b
JB
10804};
10805
10806/* Return non-zero if B is an Ada exception catchpoint. */
10807
10808int
10809ada_exception_catchpoint_p (struct breakpoint *b)
10810{
10811 return (b->ops == &catch_exception_breakpoint_ops
10812 || b->ops == &catch_exception_unhandled_breakpoint_ops
10813 || b->ops == &catch_assert_breakpoint_ops);
10814}
10815
f7f9143b
JB
10816/* Return a newly allocated copy of the first space-separated token
10817 in ARGSP, and then adjust ARGSP to point immediately after that
10818 token.
10819
10820 Return NULL if ARGPS does not contain any more tokens. */
10821
10822static char *
10823ada_get_next_arg (char **argsp)
10824{
10825 char *args = *argsp;
10826 char *end;
10827 char *result;
10828
10829 /* Skip any leading white space. */
10830
10831 while (isspace (*args))
10832 args++;
10833
10834 if (args[0] == '\0')
10835 return NULL; /* No more arguments. */
10836
10837 /* Find the end of the current argument. */
10838
10839 end = args;
10840 while (*end != '\0' && !isspace (*end))
10841 end++;
10842
10843 /* Adjust ARGSP to point to the start of the next argument. */
10844
10845 *argsp = end;
10846
10847 /* Make a copy of the current argument and return it. */
10848
10849 result = xmalloc (end - args + 1);
10850 strncpy (result, args, end - args);
10851 result[end - args] = '\0';
10852
10853 return result;
10854}
10855
10856/* Split the arguments specified in a "catch exception" command.
10857 Set EX to the appropriate catchpoint type.
10858 Set EXP_STRING to the name of the specific exception if
10859 specified by the user. */
10860
10861static void
10862catch_ada_exception_command_split (char *args,
10863 enum exception_catchpoint_kind *ex,
10864 char **exp_string)
10865{
10866 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
10867 char *exception_name;
10868
10869 exception_name = ada_get_next_arg (&args);
10870 make_cleanup (xfree, exception_name);
10871
10872 /* Check that we do not have any more arguments. Anything else
10873 is unexpected. */
10874
10875 while (isspace (*args))
10876 args++;
10877
10878 if (args[0] != '\0')
10879 error (_("Junk at end of expression"));
10880
10881 discard_cleanups (old_chain);
10882
10883 if (exception_name == NULL)
10884 {
10885 /* Catch all exceptions. */
10886 *ex = ex_catch_exception;
10887 *exp_string = NULL;
10888 }
10889 else if (strcmp (exception_name, "unhandled") == 0)
10890 {
10891 /* Catch unhandled exceptions. */
10892 *ex = ex_catch_exception_unhandled;
10893 *exp_string = NULL;
10894 }
10895 else
10896 {
10897 /* Catch a specific exception. */
10898 *ex = ex_catch_exception;
10899 *exp_string = exception_name;
10900 }
10901}
10902
10903/* Return the name of the symbol on which we should break in order to
10904 implement a catchpoint of the EX kind. */
10905
10906static const char *
10907ada_exception_sym_name (enum exception_catchpoint_kind ex)
10908{
0259addd
JB
10909 gdb_assert (exception_info != NULL);
10910
f7f9143b
JB
10911 switch (ex)
10912 {
10913 case ex_catch_exception:
0259addd 10914 return (exception_info->catch_exception_sym);
f7f9143b
JB
10915 break;
10916 case ex_catch_exception_unhandled:
0259addd 10917 return (exception_info->catch_exception_unhandled_sym);
f7f9143b
JB
10918 break;
10919 case ex_catch_assert:
0259addd 10920 return (exception_info->catch_assert_sym);
f7f9143b
JB
10921 break;
10922 default:
10923 internal_error (__FILE__, __LINE__,
10924 _("unexpected catchpoint kind (%d)"), ex);
10925 }
10926}
10927
10928/* Return the breakpoint ops "virtual table" used for catchpoints
10929 of the EX kind. */
10930
10931static struct breakpoint_ops *
4b9eee8c 10932ada_exception_breakpoint_ops (enum exception_catchpoint_kind ex)
f7f9143b
JB
10933{
10934 switch (ex)
10935 {
10936 case ex_catch_exception:
10937 return (&catch_exception_breakpoint_ops);
10938 break;
10939 case ex_catch_exception_unhandled:
10940 return (&catch_exception_unhandled_breakpoint_ops);
10941 break;
10942 case ex_catch_assert:
10943 return (&catch_assert_breakpoint_ops);
10944 break;
10945 default:
10946 internal_error (__FILE__, __LINE__,
10947 _("unexpected catchpoint kind (%d)"), ex);
10948 }
10949}
10950
10951/* Return the condition that will be used to match the current exception
10952 being raised with the exception that the user wants to catch. This
10953 assumes that this condition is used when the inferior just triggered
10954 an exception catchpoint.
10955
10956 The string returned is a newly allocated string that needs to be
10957 deallocated later. */
10958
10959static char *
10960ada_exception_catchpoint_cond_string (const char *exp_string)
10961{
3d0b0fa3
JB
10962 int i;
10963
10964 /* The standard exceptions are a special case. They are defined in
10965 runtime units that have been compiled without debugging info; if
10966 EXP_STRING is the not-fully-qualified name of a standard
10967 exception (e.g. "constraint_error") then, during the evaluation
10968 of the condition expression, the symbol lookup on this name would
10969 *not* return this standard exception. The catchpoint condition
10970 may then be set only on user-defined exceptions which have the
10971 same not-fully-qualified name (e.g. my_package.constraint_error).
10972
10973 To avoid this unexcepted behavior, these standard exceptions are
10974 systematically prefixed by "standard". This means that "catch
10975 exception constraint_error" is rewritten into "catch exception
10976 standard.constraint_error".
10977
10978 If an exception named contraint_error is defined in another package of
10979 the inferior program, then the only way to specify this exception as a
10980 breakpoint condition is to use its fully-qualified named:
10981 e.g. my_package.constraint_error. */
10982
10983 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
10984 {
10985 if (strcmp (standard_exc [i], exp_string) == 0)
10986 {
10987 return xstrprintf ("long_integer (e) = long_integer (&standard.%s)",
10988 exp_string);
10989 }
10990 }
f7f9143b
JB
10991 return xstrprintf ("long_integer (e) = long_integer (&%s)", exp_string);
10992}
10993
10994/* Return the expression corresponding to COND_STRING evaluated at SAL. */
10995
10996static struct expression *
10997ada_parse_catchpoint_condition (char *cond_string,
10998 struct symtab_and_line sal)
10999{
11000 return (parse_exp_1 (&cond_string, block_for_pc (sal.pc), 0));
11001}
11002
11003/* Return the symtab_and_line that should be used to insert an exception
11004 catchpoint of the TYPE kind.
11005
11006 EX_STRING should contain the name of a specific exception
11007 that the catchpoint should catch, or NULL otherwise.
11008
11009 The idea behind all the remaining parameters is that their names match
11010 the name of certain fields in the breakpoint structure that are used to
11011 handle exception catchpoints. This function returns the value to which
11012 these fields should be set, depending on the type of catchpoint we need
11013 to create.
11014
11015 If COND and COND_STRING are both non-NULL, any value they might
11016 hold will be free'ed, and then replaced by newly allocated ones.
11017 These parameters are left untouched otherwise. */
11018
11019static struct symtab_and_line
11020ada_exception_sal (enum exception_catchpoint_kind ex, char *exp_string,
11021 char **addr_string, char **cond_string,
11022 struct expression **cond, struct breakpoint_ops **ops)
11023{
11024 const char *sym_name;
11025 struct symbol *sym;
11026 struct symtab_and_line sal;
11027
0259addd
JB
11028 /* First, find out which exception support info to use. */
11029 ada_exception_support_info_sniffer ();
11030
11031 /* Then lookup the function on which we will break in order to catch
f7f9143b
JB
11032 the Ada exceptions requested by the user. */
11033
11034 sym_name = ada_exception_sym_name (ex);
11035 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
11036
11037 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11038 that should be compiled with debugging information. As a result, we
11039 expect to find that symbol in the symtabs. If we don't find it, then
11040 the target most likely does not support Ada exceptions, or we cannot
11041 insert exception breakpoints yet, because the GNAT runtime hasn't been
11042 loaded yet. */
11043
11044 /* brobecker/2006-12-26: It is conceivable that the runtime was compiled
11045 in such a way that no debugging information is produced for the symbol
11046 we are looking for. In this case, we could search the minimal symbols
11047 as a fall-back mechanism. This would still be operating in degraded
11048 mode, however, as we would still be missing the debugging information
11049 that is needed in order to extract the name of the exception being
11050 raised (this name is printed in the catchpoint message, and is also
11051 used when trying to catch a specific exception). We do not handle
11052 this case for now. */
11053
11054 if (sym == NULL)
0259addd 11055 error (_("Unable to break on '%s' in this configuration."), sym_name);
f7f9143b
JB
11056
11057 /* Make sure that the symbol we found corresponds to a function. */
11058 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11059 error (_("Symbol \"%s\" is not a function (class = %d)"),
11060 sym_name, SYMBOL_CLASS (sym));
11061
11062 sal = find_function_start_sal (sym, 1);
11063
11064 /* Set ADDR_STRING. */
11065
11066 *addr_string = xstrdup (sym_name);
11067
11068 /* Set the COND and COND_STRING (if not NULL). */
11069
11070 if (cond_string != NULL && cond != NULL)
11071 {
11072 if (*cond_string != NULL)
11073 {
11074 xfree (*cond_string);
11075 *cond_string = NULL;
11076 }
11077 if (*cond != NULL)
11078 {
11079 xfree (*cond);
11080 *cond = NULL;
11081 }
11082 if (exp_string != NULL)
11083 {
11084 *cond_string = ada_exception_catchpoint_cond_string (exp_string);
11085 *cond = ada_parse_catchpoint_condition (*cond_string, sal);
11086 }
11087 }
11088
11089 /* Set OPS. */
4b9eee8c 11090 *ops = ada_exception_breakpoint_ops (ex);
f7f9143b
JB
11091
11092 return sal;
11093}
11094
11095/* Parse the arguments (ARGS) of the "catch exception" command.
11096
11097 Set TYPE to the appropriate exception catchpoint type.
11098 If the user asked the catchpoint to catch only a specific
11099 exception, then save the exception name in ADDR_STRING.
11100
11101 See ada_exception_sal for a description of all the remaining
11102 function arguments of this function. */
11103
11104struct symtab_and_line
11105ada_decode_exception_location (char *args, char **addr_string,
11106 char **exp_string, char **cond_string,
11107 struct expression **cond,
11108 struct breakpoint_ops **ops)
11109{
11110 enum exception_catchpoint_kind ex;
11111
11112 catch_ada_exception_command_split (args, &ex, exp_string);
11113 return ada_exception_sal (ex, *exp_string, addr_string, cond_string,
11114 cond, ops);
11115}
11116
11117struct symtab_and_line
11118ada_decode_assert_location (char *args, char **addr_string,
11119 struct breakpoint_ops **ops)
11120{
11121 /* Check that no argument where provided at the end of the command. */
11122
11123 if (args != NULL)
11124 {
11125 while (isspace (*args))
11126 args++;
11127 if (*args != '\0')
11128 error (_("Junk at end of arguments."));
11129 }
11130
11131 return ada_exception_sal (ex_catch_assert, NULL, addr_string, NULL, NULL,
11132 ops);
11133}
11134
4c4b4cd2
PH
11135 /* Operators */
11136/* Information about operators given special treatment in functions
11137 below. */
11138/* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
11139
11140#define ADA_OPERATORS \
11141 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
11142 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
11143 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
11144 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
11145 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
11146 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
11147 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
11148 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
11149 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
11150 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
11151 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
11152 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
11153 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
11154 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
11155 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
52ce6436
PH
11156 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
11157 OP_DEFN (OP_OTHERS, 1, 1, 0) \
11158 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
11159 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
4c4b4cd2
PH
11160
11161static void
554794dc
SDJ
11162ada_operator_length (const struct expression *exp, int pc, int *oplenp,
11163 int *argsp)
4c4b4cd2
PH
11164{
11165 switch (exp->elts[pc - 1].opcode)
11166 {
76a01679 11167 default:
4c4b4cd2
PH
11168 operator_length_standard (exp, pc, oplenp, argsp);
11169 break;
11170
11171#define OP_DEFN(op, len, args, binop) \
11172 case op: *oplenp = len; *argsp = args; break;
11173 ADA_OPERATORS;
11174#undef OP_DEFN
52ce6436
PH
11175
11176 case OP_AGGREGATE:
11177 *oplenp = 3;
11178 *argsp = longest_to_int (exp->elts[pc - 2].longconst);
11179 break;
11180
11181 case OP_CHOICES:
11182 *oplenp = 3;
11183 *argsp = longest_to_int (exp->elts[pc - 2].longconst) + 1;
11184 break;
4c4b4cd2
PH
11185 }
11186}
11187
c0201579
JK
11188/* Implementation of the exp_descriptor method operator_check. */
11189
11190static int
11191ada_operator_check (struct expression *exp, int pos,
11192 int (*objfile_func) (struct objfile *objfile, void *data),
11193 void *data)
11194{
11195 const union exp_element *const elts = exp->elts;
11196 struct type *type = NULL;
11197
11198 switch (elts[pos].opcode)
11199 {
11200 case UNOP_IN_RANGE:
11201 case UNOP_QUAL:
11202 type = elts[pos + 1].type;
11203 break;
11204
11205 default:
11206 return operator_check_standard (exp, pos, objfile_func, data);
11207 }
11208
11209 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
11210
11211 if (type && TYPE_OBJFILE (type)
11212 && (*objfile_func) (TYPE_OBJFILE (type), data))
11213 return 1;
11214
11215 return 0;
11216}
11217
4c4b4cd2
PH
11218static char *
11219ada_op_name (enum exp_opcode opcode)
11220{
11221 switch (opcode)
11222 {
76a01679 11223 default:
4c4b4cd2 11224 return op_name_standard (opcode);
52ce6436 11225
4c4b4cd2
PH
11226#define OP_DEFN(op, len, args, binop) case op: return #op;
11227 ADA_OPERATORS;
11228#undef OP_DEFN
52ce6436
PH
11229
11230 case OP_AGGREGATE:
11231 return "OP_AGGREGATE";
11232 case OP_CHOICES:
11233 return "OP_CHOICES";
11234 case OP_NAME:
11235 return "OP_NAME";
4c4b4cd2
PH
11236 }
11237}
11238
11239/* As for operator_length, but assumes PC is pointing at the first
11240 element of the operator, and gives meaningful results only for the
52ce6436 11241 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
4c4b4cd2
PH
11242
11243static void
76a01679
JB
11244ada_forward_operator_length (struct expression *exp, int pc,
11245 int *oplenp, int *argsp)
4c4b4cd2 11246{
76a01679 11247 switch (exp->elts[pc].opcode)
4c4b4cd2
PH
11248 {
11249 default:
11250 *oplenp = *argsp = 0;
11251 break;
52ce6436 11252
4c4b4cd2
PH
11253#define OP_DEFN(op, len, args, binop) \
11254 case op: *oplenp = len; *argsp = args; break;
11255 ADA_OPERATORS;
11256#undef OP_DEFN
52ce6436
PH
11257
11258 case OP_AGGREGATE:
11259 *oplenp = 3;
11260 *argsp = longest_to_int (exp->elts[pc + 1].longconst);
11261 break;
11262
11263 case OP_CHOICES:
11264 *oplenp = 3;
11265 *argsp = longest_to_int (exp->elts[pc + 1].longconst) + 1;
11266 break;
11267
11268 case OP_STRING:
11269 case OP_NAME:
11270 {
11271 int len = longest_to_int (exp->elts[pc + 1].longconst);
5b4ee69b 11272
52ce6436
PH
11273 *oplenp = 4 + BYTES_TO_EXP_ELEM (len + 1);
11274 *argsp = 0;
11275 break;
11276 }
4c4b4cd2
PH
11277 }
11278}
11279
11280static int
11281ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
11282{
11283 enum exp_opcode op = exp->elts[elt].opcode;
11284 int oplen, nargs;
11285 int pc = elt;
11286 int i;
76a01679 11287
4c4b4cd2
PH
11288 ada_forward_operator_length (exp, elt, &oplen, &nargs);
11289
76a01679 11290 switch (op)
4c4b4cd2 11291 {
76a01679 11292 /* Ada attributes ('Foo). */
4c4b4cd2
PH
11293 case OP_ATR_FIRST:
11294 case OP_ATR_LAST:
11295 case OP_ATR_LENGTH:
11296 case OP_ATR_IMAGE:
11297 case OP_ATR_MAX:
11298 case OP_ATR_MIN:
11299 case OP_ATR_MODULUS:
11300 case OP_ATR_POS:
11301 case OP_ATR_SIZE:
11302 case OP_ATR_TAG:
11303 case OP_ATR_VAL:
11304 break;
11305
11306 case UNOP_IN_RANGE:
11307 case UNOP_QUAL:
323e0a4a
AC
11308 /* XXX: gdb_sprint_host_address, type_sprint */
11309 fprintf_filtered (stream, _("Type @"));
4c4b4cd2
PH
11310 gdb_print_host_address (exp->elts[pc + 1].type, stream);
11311 fprintf_filtered (stream, " (");
11312 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
11313 fprintf_filtered (stream, ")");
11314 break;
11315 case BINOP_IN_BOUNDS:
52ce6436
PH
11316 fprintf_filtered (stream, " (%d)",
11317 longest_to_int (exp->elts[pc + 2].longconst));
4c4b4cd2
PH
11318 break;
11319 case TERNOP_IN_RANGE:
11320 break;
11321
52ce6436
PH
11322 case OP_AGGREGATE:
11323 case OP_OTHERS:
11324 case OP_DISCRETE_RANGE:
11325 case OP_POSITIONAL:
11326 case OP_CHOICES:
11327 break;
11328
11329 case OP_NAME:
11330 case OP_STRING:
11331 {
11332 char *name = &exp->elts[elt + 2].string;
11333 int len = longest_to_int (exp->elts[elt + 1].longconst);
5b4ee69b 11334
52ce6436
PH
11335 fprintf_filtered (stream, "Text: `%.*s'", len, name);
11336 break;
11337 }
11338
4c4b4cd2
PH
11339 default:
11340 return dump_subexp_body_standard (exp, stream, elt);
11341 }
11342
11343 elt += oplen;
11344 for (i = 0; i < nargs; i += 1)
11345 elt = dump_subexp (exp, stream, elt);
11346
11347 return elt;
11348}
11349
11350/* The Ada extension of print_subexp (q.v.). */
11351
76a01679
JB
11352static void
11353ada_print_subexp (struct expression *exp, int *pos,
11354 struct ui_file *stream, enum precedence prec)
4c4b4cd2 11355{
52ce6436 11356 int oplen, nargs, i;
4c4b4cd2
PH
11357 int pc = *pos;
11358 enum exp_opcode op = exp->elts[pc].opcode;
11359
11360 ada_forward_operator_length (exp, pc, &oplen, &nargs);
11361
52ce6436 11362 *pos += oplen;
4c4b4cd2
PH
11363 switch (op)
11364 {
11365 default:
52ce6436 11366 *pos -= oplen;
4c4b4cd2
PH
11367 print_subexp_standard (exp, pos, stream, prec);
11368 return;
11369
11370 case OP_VAR_VALUE:
4c4b4cd2
PH
11371 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
11372 return;
11373
11374 case BINOP_IN_BOUNDS:
323e0a4a 11375 /* XXX: sprint_subexp */
4c4b4cd2 11376 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 11377 fputs_filtered (" in ", stream);
4c4b4cd2 11378 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 11379 fputs_filtered ("'range", stream);
4c4b4cd2 11380 if (exp->elts[pc + 1].longconst > 1)
76a01679
JB
11381 fprintf_filtered (stream, "(%ld)",
11382 (long) exp->elts[pc + 1].longconst);
4c4b4cd2
PH
11383 return;
11384
11385 case TERNOP_IN_RANGE:
4c4b4cd2 11386 if (prec >= PREC_EQUAL)
76a01679 11387 fputs_filtered ("(", stream);
323e0a4a 11388 /* XXX: sprint_subexp */
4c4b4cd2 11389 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 11390 fputs_filtered (" in ", stream);
4c4b4cd2
PH
11391 print_subexp (exp, pos, stream, PREC_EQUAL);
11392 fputs_filtered (" .. ", stream);
11393 print_subexp (exp, pos, stream, PREC_EQUAL);
11394 if (prec >= PREC_EQUAL)
76a01679
JB
11395 fputs_filtered (")", stream);
11396 return;
4c4b4cd2
PH
11397
11398 case OP_ATR_FIRST:
11399 case OP_ATR_LAST:
11400 case OP_ATR_LENGTH:
11401 case OP_ATR_IMAGE:
11402 case OP_ATR_MAX:
11403 case OP_ATR_MIN:
11404 case OP_ATR_MODULUS:
11405 case OP_ATR_POS:
11406 case OP_ATR_SIZE:
11407 case OP_ATR_TAG:
11408 case OP_ATR_VAL:
4c4b4cd2 11409 if (exp->elts[*pos].opcode == OP_TYPE)
76a01679
JB
11410 {
11411 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
11412 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0);
11413 *pos += 3;
11414 }
4c4b4cd2 11415 else
76a01679 11416 print_subexp (exp, pos, stream, PREC_SUFFIX);
4c4b4cd2
PH
11417 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
11418 if (nargs > 1)
76a01679
JB
11419 {
11420 int tem;
5b4ee69b 11421
76a01679
JB
11422 for (tem = 1; tem < nargs; tem += 1)
11423 {
11424 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
11425 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
11426 }
11427 fputs_filtered (")", stream);
11428 }
4c4b4cd2 11429 return;
14f9c5c9 11430
4c4b4cd2 11431 case UNOP_QUAL:
4c4b4cd2
PH
11432 type_print (exp->elts[pc + 1].type, "", stream, 0);
11433 fputs_filtered ("'(", stream);
11434 print_subexp (exp, pos, stream, PREC_PREFIX);
11435 fputs_filtered (")", stream);
11436 return;
14f9c5c9 11437
4c4b4cd2 11438 case UNOP_IN_RANGE:
323e0a4a 11439 /* XXX: sprint_subexp */
4c4b4cd2 11440 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 11441 fputs_filtered (" in ", stream);
4c4b4cd2
PH
11442 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0);
11443 return;
52ce6436
PH
11444
11445 case OP_DISCRETE_RANGE:
11446 print_subexp (exp, pos, stream, PREC_SUFFIX);
11447 fputs_filtered ("..", stream);
11448 print_subexp (exp, pos, stream, PREC_SUFFIX);
11449 return;
11450
11451 case OP_OTHERS:
11452 fputs_filtered ("others => ", stream);
11453 print_subexp (exp, pos, stream, PREC_SUFFIX);
11454 return;
11455
11456 case OP_CHOICES:
11457 for (i = 0; i < nargs-1; i += 1)
11458 {
11459 if (i > 0)
11460 fputs_filtered ("|", stream);
11461 print_subexp (exp, pos, stream, PREC_SUFFIX);
11462 }
11463 fputs_filtered (" => ", stream);
11464 print_subexp (exp, pos, stream, PREC_SUFFIX);
11465 return;
11466
11467 case OP_POSITIONAL:
11468 print_subexp (exp, pos, stream, PREC_SUFFIX);
11469 return;
11470
11471 case OP_AGGREGATE:
11472 fputs_filtered ("(", stream);
11473 for (i = 0; i < nargs; i += 1)
11474 {
11475 if (i > 0)
11476 fputs_filtered (", ", stream);
11477 print_subexp (exp, pos, stream, PREC_SUFFIX);
11478 }
11479 fputs_filtered (")", stream);
11480 return;
4c4b4cd2
PH
11481 }
11482}
14f9c5c9
AS
11483
11484/* Table mapping opcodes into strings for printing operators
11485 and precedences of the operators. */
11486
d2e4a39e
AS
11487static const struct op_print ada_op_print_tab[] = {
11488 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
11489 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
11490 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
11491 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
11492 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
11493 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
11494 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
11495 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
11496 {"<=", BINOP_LEQ, PREC_ORDER, 0},
11497 {">=", BINOP_GEQ, PREC_ORDER, 0},
11498 {">", BINOP_GTR, PREC_ORDER, 0},
11499 {"<", BINOP_LESS, PREC_ORDER, 0},
11500 {">>", BINOP_RSH, PREC_SHIFT, 0},
11501 {"<<", BINOP_LSH, PREC_SHIFT, 0},
11502 {"+", BINOP_ADD, PREC_ADD, 0},
11503 {"-", BINOP_SUB, PREC_ADD, 0},
11504 {"&", BINOP_CONCAT, PREC_ADD, 0},
11505 {"*", BINOP_MUL, PREC_MUL, 0},
11506 {"/", BINOP_DIV, PREC_MUL, 0},
11507 {"rem", BINOP_REM, PREC_MUL, 0},
11508 {"mod", BINOP_MOD, PREC_MUL, 0},
11509 {"**", BINOP_EXP, PREC_REPEAT, 0},
11510 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
11511 {"-", UNOP_NEG, PREC_PREFIX, 0},
11512 {"+", UNOP_PLUS, PREC_PREFIX, 0},
11513 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
11514 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
11515 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
4c4b4cd2
PH
11516 {".all", UNOP_IND, PREC_SUFFIX, 1},
11517 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
11518 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
d2e4a39e 11519 {NULL, 0, 0, 0}
14f9c5c9
AS
11520};
11521\f
72d5681a
PH
11522enum ada_primitive_types {
11523 ada_primitive_type_int,
11524 ada_primitive_type_long,
11525 ada_primitive_type_short,
11526 ada_primitive_type_char,
11527 ada_primitive_type_float,
11528 ada_primitive_type_double,
11529 ada_primitive_type_void,
11530 ada_primitive_type_long_long,
11531 ada_primitive_type_long_double,
11532 ada_primitive_type_natural,
11533 ada_primitive_type_positive,
11534 ada_primitive_type_system_address,
11535 nr_ada_primitive_types
11536};
6c038f32
PH
11537
11538static void
d4a9a881 11539ada_language_arch_info (struct gdbarch *gdbarch,
72d5681a
PH
11540 struct language_arch_info *lai)
11541{
d4a9a881 11542 const struct builtin_type *builtin = builtin_type (gdbarch);
5b4ee69b 11543
72d5681a 11544 lai->primitive_type_vector
d4a9a881 11545 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_ada_primitive_types + 1,
72d5681a 11546 struct type *);
e9bb382b
UW
11547
11548 lai->primitive_type_vector [ada_primitive_type_int]
11549 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11550 0, "integer");
11551 lai->primitive_type_vector [ada_primitive_type_long]
11552 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
11553 0, "long_integer");
11554 lai->primitive_type_vector [ada_primitive_type_short]
11555 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
11556 0, "short_integer");
11557 lai->string_char_type
11558 = lai->primitive_type_vector [ada_primitive_type_char]
11559 = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
11560 lai->primitive_type_vector [ada_primitive_type_float]
11561 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
11562 "float", NULL);
11563 lai->primitive_type_vector [ada_primitive_type_double]
11564 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11565 "long_float", NULL);
11566 lai->primitive_type_vector [ada_primitive_type_long_long]
11567 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
11568 0, "long_long_integer");
11569 lai->primitive_type_vector [ada_primitive_type_long_double]
11570 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11571 "long_long_float", NULL);
11572 lai->primitive_type_vector [ada_primitive_type_natural]
11573 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11574 0, "natural");
11575 lai->primitive_type_vector [ada_primitive_type_positive]
11576 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11577 0, "positive");
11578 lai->primitive_type_vector [ada_primitive_type_void]
11579 = builtin->builtin_void;
11580
11581 lai->primitive_type_vector [ada_primitive_type_system_address]
11582 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"));
72d5681a
PH
11583 TYPE_NAME (lai->primitive_type_vector [ada_primitive_type_system_address])
11584 = "system__address";
fbb06eb1 11585
47e729a8 11586 lai->bool_type_symbol = NULL;
fbb06eb1 11587 lai->bool_type_default = builtin->builtin_bool;
6c038f32 11588}
6c038f32
PH
11589\f
11590 /* Language vector */
11591
11592/* Not really used, but needed in the ada_language_defn. */
11593
11594static void
6c7a06a3 11595emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
6c038f32 11596{
6c7a06a3 11597 ada_emit_char (c, type, stream, quoter, 1);
6c038f32
PH
11598}
11599
11600static int
11601parse (void)
11602{
11603 warnings_issued = 0;
11604 return ada_parse ();
11605}
11606
11607static const struct exp_descriptor ada_exp_descriptor = {
11608 ada_print_subexp,
11609 ada_operator_length,
c0201579 11610 ada_operator_check,
6c038f32
PH
11611 ada_op_name,
11612 ada_dump_subexp_body,
11613 ada_evaluate_subexp
11614};
11615
11616const struct language_defn ada_language_defn = {
11617 "ada", /* Language name */
11618 language_ada,
6c038f32
PH
11619 range_check_off,
11620 type_check_off,
11621 case_sensitive_on, /* Yes, Ada is case-insensitive, but
11622 that's not quite what this means. */
6c038f32 11623 array_row_major,
9a044a89 11624 macro_expansion_no,
6c038f32
PH
11625 &ada_exp_descriptor,
11626 parse,
11627 ada_error,
11628 resolve,
11629 ada_printchar, /* Print a character constant */
11630 ada_printstr, /* Function to print string constant */
11631 emit_char, /* Function to print single char (not used) */
6c038f32 11632 ada_print_type, /* Print a type using appropriate syntax */
be942545 11633 ada_print_typedef, /* Print a typedef using appropriate syntax */
6c038f32
PH
11634 ada_val_print, /* Print a value using appropriate syntax */
11635 ada_value_print, /* Print a top-level value */
11636 NULL, /* Language specific skip_trampoline */
2b2d9e11 11637 NULL, /* name_of_this */
6c038f32
PH
11638 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
11639 basic_lookup_transparent_type, /* lookup_transparent_type */
11640 ada_la_decode, /* Language specific symbol demangler */
11641 NULL, /* Language specific class_name_from_physname */
11642 ada_op_print_tab, /* expression operators for printing */
11643 0, /* c-style arrays */
11644 1, /* String lower bound */
6c038f32 11645 ada_get_gdb_completer_word_break_characters,
41d27058 11646 ada_make_symbol_completion_list,
72d5681a 11647 ada_language_arch_info,
e79af960 11648 ada_print_array_index,
41f1b697 11649 default_pass_by_reference,
ae6a3a4c 11650 c_get_string,
6c038f32
PH
11651 LANG_MAGIC
11652};
11653
2c0b251b
PA
11654/* Provide a prototype to silence -Wmissing-prototypes. */
11655extern initialize_file_ftype _initialize_ada_language;
11656
5bf03f13
JB
11657/* Command-list for the "set/show ada" prefix command. */
11658static struct cmd_list_element *set_ada_list;
11659static struct cmd_list_element *show_ada_list;
11660
11661/* Implement the "set ada" prefix command. */
11662
11663static void
11664set_ada_command (char *arg, int from_tty)
11665{
11666 printf_unfiltered (_(\
11667"\"set ada\" must be followed by the name of a setting.\n"));
11668 help_list (set_ada_list, "set ada ", -1, gdb_stdout);
11669}
11670
11671/* Implement the "show ada" prefix command. */
11672
11673static void
11674show_ada_command (char *args, int from_tty)
11675{
11676 cmd_show_list (show_ada_list, from_tty, "");
11677}
11678
d2e4a39e 11679void
6c038f32 11680_initialize_ada_language (void)
14f9c5c9 11681{
6c038f32
PH
11682 add_language (&ada_language_defn);
11683
5bf03f13
JB
11684 add_prefix_cmd ("ada", no_class, set_ada_command,
11685 _("Prefix command for changing Ada-specfic settings"),
11686 &set_ada_list, "set ada ", 0, &setlist);
11687
11688 add_prefix_cmd ("ada", no_class, show_ada_command,
11689 _("Generic command for showing Ada-specific settings."),
11690 &show_ada_list, "show ada ", 0, &showlist);
11691
11692 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
11693 &trust_pad_over_xvs, _("\
11694Enable or disable an optimization trusting PAD types over XVS types"), _("\
11695Show whether an optimization trusting PAD types over XVS types is activated"),
11696 _("\
11697This is related to the encoding used by the GNAT compiler. The debugger\n\
11698should normally trust the contents of PAD types, but certain older versions\n\
11699of GNAT have a bug that sometimes causes the information in the PAD type\n\
11700to be incorrect. Turning this setting \"off\" allows the debugger to\n\
11701work around this bug. It is always safe to turn this option \"off\", but\n\
11702this incurs a slight performance penalty, so it is recommended to NOT change\n\
11703this option to \"off\" unless necessary."),
11704 NULL, NULL, &set_ada_list, &show_ada_list);
11705
6c038f32 11706 varsize_limit = 65536;
6c038f32
PH
11707
11708 obstack_init (&symbol_list_obstack);
11709
11710 decoded_names_store = htab_create_alloc
11711 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
11712 NULL, xcalloc, xfree);
6b69afc4
JB
11713
11714 observer_attach_executable_changed (ada_executable_changed_observer);
e802dbe0
JB
11715
11716 /* Setup per-inferior data. */
11717 observer_attach_inferior_exit (ada_inferior_exit);
11718 ada_inferior_data
11719 = register_inferior_data_with_cleanup (ada_inferior_data_cleanup);
14f9c5c9 11720}