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