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