* linux-nat.c (linux_nat_resume): Add more debugging messages. Do
[deliverable/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
14f9c5c9 1/* Ada language support routines for GDB, the GNU debugger. Copyright
10a2c479
AC
2
3 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003, 2004, 2005 Free
4 Software Foundation, Inc.
14f9c5c9
AS
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
96d887e8 22
4c4b4cd2 23#include "defs.h"
14f9c5c9 24#include <stdio.h>
0c30c098 25#include "gdb_string.h"
14f9c5c9
AS
26#include <ctype.h>
27#include <stdarg.h>
28#include "demangle.h"
4c4b4cd2
PH
29#include "gdb_regex.h"
30#include "frame.h"
14f9c5c9
AS
31#include "symtab.h"
32#include "gdbtypes.h"
33#include "gdbcmd.h"
34#include "expression.h"
35#include "parser-defs.h"
36#include "language.h"
37#include "c-lang.h"
38#include "inferior.h"
39#include "symfile.h"
40#include "objfiles.h"
41#include "breakpoint.h"
42#include "gdbcore.h"
4c4b4cd2
PH
43#include "hashtab.h"
44#include "gdb_obstack.h"
14f9c5c9 45#include "ada-lang.h"
4c4b4cd2
PH
46#include "completer.h"
47#include "gdb_stat.h"
48#ifdef UI_OUT
14f9c5c9 49#include "ui-out.h"
4c4b4cd2 50#endif
fe898f56 51#include "block.h"
04714b91 52#include "infcall.h"
de4f826b 53#include "dictionary.h"
60250e8b 54#include "exceptions.h"
14f9c5c9 55
4c4b4cd2
PH
56#ifndef ADA_RETAIN_DOTS
57#define ADA_RETAIN_DOTS 0
58#endif
59
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 68
4c4b4cd2 69static void extract_string (CORE_ADDR addr, char *buf);
14f9c5c9 70
d2e4a39e 71static struct type *ada_create_fundamental_type (struct objfile *, int);
14f9c5c9
AS
72
73static void modify_general_field (char *, LONGEST, int, int);
74
d2e4a39e 75static struct type *desc_base_type (struct type *);
14f9c5c9 76
d2e4a39e 77static struct type *desc_bounds_type (struct type *);
14f9c5c9 78
d2e4a39e 79static struct value *desc_bounds (struct value *);
14f9c5c9 80
d2e4a39e 81static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 82
d2e4a39e 83static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 84
d2e4a39e 85static struct type *desc_data_type (struct type *);
14f9c5c9 86
d2e4a39e 87static struct value *desc_data (struct value *);
14f9c5c9 88
d2e4a39e 89static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 90
d2e4a39e 91static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 92
d2e4a39e 93static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 94
d2e4a39e 95static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 96
d2e4a39e 97static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 98
d2e4a39e 99static struct type *desc_index_type (struct type *, int);
14f9c5c9 100
d2e4a39e 101static int desc_arity (struct type *);
14f9c5c9 102
d2e4a39e 103static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 104
d2e4a39e 105static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 106
4c4b4cd2 107static struct value *ensure_lval (struct value *, CORE_ADDR *);
14f9c5c9 108
d2e4a39e 109static struct value *convert_actual (struct value *, struct type *,
4c4b4cd2 110 CORE_ADDR *);
14f9c5c9 111
d2e4a39e 112static struct value *make_array_descriptor (struct type *, struct value *,
4c4b4cd2 113 CORE_ADDR *);
14f9c5c9 114
4c4b4cd2 115static void ada_add_block_symbols (struct obstack *,
76a01679 116 struct block *, const char *,
4c4b4cd2 117 domain_enum, struct objfile *,
76a01679 118 struct symtab *, int);
14f9c5c9 119
4c4b4cd2 120static int is_nonfunction (struct ada_symbol_info *, int);
14f9c5c9 121
76a01679
JB
122static void add_defn_to_vec (struct obstack *, struct symbol *,
123 struct block *, struct symtab *);
14f9c5c9 124
4c4b4cd2
PH
125static int num_defns_collected (struct obstack *);
126
127static struct ada_symbol_info *defns_collected (struct obstack *, int);
14f9c5c9 128
d2e4a39e 129static struct partial_symbol *ada_lookup_partial_symbol (struct partial_symtab
76a01679
JB
130 *, const char *, int,
131 domain_enum, int);
14f9c5c9 132
d2e4a39e 133static struct symtab *symtab_for_sym (struct symbol *);
14f9c5c9 134
4c4b4cd2 135static struct value *resolve_subexp (struct expression **, int *, int,
76a01679 136 struct type *);
14f9c5c9 137
d2e4a39e 138static void replace_operator_with_call (struct expression **, int, int, int,
4c4b4cd2 139 struct symbol *, struct block *);
14f9c5c9 140
d2e4a39e 141static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 142
4c4b4cd2
PH
143static char *ada_op_name (enum exp_opcode);
144
145static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 146
d2e4a39e 147static int numeric_type_p (struct type *);
14f9c5c9 148
d2e4a39e 149static int integer_type_p (struct type *);
14f9c5c9 150
d2e4a39e 151static int scalar_type_p (struct type *);
14f9c5c9 152
d2e4a39e 153static int discrete_type_p (struct type *);
14f9c5c9 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 (struct type *, struct expression *,
4c4b4cd2 159 int *, enum noside);
14f9c5c9 160
d2e4a39e 161static struct value *evaluate_subexp_type (struct expression *, int *);
14f9c5c9 162
d2e4a39e 163static int is_dynamic_field (struct type *, int);
14f9c5c9 164
10a2c479 165static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 166 const gdb_byte *,
4c4b4cd2
PH
167 CORE_ADDR, struct value *);
168
169static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 170
d2e4a39e 171static struct type *to_fixed_range_type (char *, struct value *,
4c4b4cd2 172 struct objfile *);
14f9c5c9 173
d2e4a39e 174static struct type *to_static_fixed_type (struct type *);
14f9c5c9 175
d2e4a39e 176static struct value *unwrap_value (struct value *);
14f9c5c9 177
d2e4a39e 178static struct type *packed_array_type (struct type *, long *);
14f9c5c9 179
d2e4a39e 180static struct type *decode_packed_array_type (struct type *);
14f9c5c9 181
d2e4a39e 182static struct value *decode_packed_array (struct value *);
14f9c5c9 183
d2e4a39e 184static struct value *value_subscript_packed (struct value *, int,
4c4b4cd2 185 struct value **);
14f9c5c9 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
d2e4a39e 204static struct value *value_pos_atr (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
JB
217static int find_struct_field (char *, struct type *, int,
218 struct type **, 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 *);
4c4b4cd2
PH
237\f
238
76a01679 239
4c4b4cd2 240/* Maximum-sized dynamic type. */
14f9c5c9
AS
241static unsigned int varsize_limit;
242
4c4b4cd2
PH
243/* FIXME: brobecker/2003-09-17: No longer a const because it is
244 returned by a function that does not return a const char *. */
245static char *ada_completer_word_break_characters =
246#ifdef VMS
247 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
248#else
14f9c5c9 249 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 250#endif
14f9c5c9 251
4c4b4cd2 252/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 253static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 254 = "__gnat_ada_main_program_name";
14f9c5c9 255
4c4b4cd2
PH
256/* The name of the runtime function called when an exception is raised. */
257static const char raise_sym_name[] = "__gnat_raise_nodefer_with_msg";
14f9c5c9 258
4c4b4cd2
PH
259/* The name of the runtime function called when an unhandled exception
260 is raised. */
261static const char raise_unhandled_sym_name[] = "__gnat_unhandled_exception";
262
263/* The name of the runtime function called when an assert failure is
264 raised. */
265static const char raise_assert_sym_name[] =
266 "system__assertions__raise_assert_failure";
267
268/* When GDB stops on an unhandled exception, GDB will go up the stack until
269 if finds a frame corresponding to this function, in order to extract the
270 name of the exception that has been raised from one of the parameters. */
271static const char process_raise_exception_name[] =
272 "ada__exceptions__process_raise_exception";
273
274/* A string that reflects the longest exception expression rewrite,
275 aside from the exception name. */
276static const char longest_exception_template[] =
277 "'__gnat_raise_nodefer_with_msg' if long_integer(e) = long_integer(&)";
278
279/* Limit on the number of warnings to raise per expression evaluation. */
280static int warning_limit = 2;
281
282/* Number of warning messages issued; reset to 0 by cleanups after
283 expression evaluation. */
284static int warnings_issued = 0;
285
286static const char *known_runtime_file_name_patterns[] = {
287 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
288};
289
290static const char *known_auxiliary_function_name_patterns[] = {
291 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
292};
293
294/* Space for allocating results of ada_lookup_symbol_list. */
295static struct obstack symbol_list_obstack;
296
297 /* Utilities */
298
96d887e8 299
4c4b4cd2
PH
300static char *
301ada_get_gdb_completer_word_break_characters (void)
302{
303 return ada_completer_word_break_characters;
304}
305
e79af960
JB
306/* Print an array element index using the Ada syntax. */
307
308static void
309ada_print_array_index (struct value *index_value, struct ui_file *stream,
310 int format, enum val_prettyprint pretty)
311{
312 LA_VALUE_PRINT (index_value, stream, format, pretty);
313 fprintf_filtered (stream, " => ");
314}
315
4c4b4cd2
PH
316/* Read the string located at ADDR from the inferior and store the
317 result into BUF. */
318
319static void
14f9c5c9
AS
320extract_string (CORE_ADDR addr, char *buf)
321{
d2e4a39e 322 int char_index = 0;
14f9c5c9 323
4c4b4cd2
PH
324 /* Loop, reading one byte at a time, until we reach the '\000'
325 end-of-string marker. */
d2e4a39e
AS
326 do
327 {
328 target_read_memory (addr + char_index * sizeof (char),
4c4b4cd2 329 buf + char_index * sizeof (char), sizeof (char));
d2e4a39e
AS
330 char_index++;
331 }
332 while (buf[char_index - 1] != '\000');
14f9c5c9
AS
333}
334
f27cf670 335/* Assuming VECT points to an array of *SIZE objects of size
14f9c5c9 336 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
f27cf670 337 updating *SIZE as necessary and returning the (new) array. */
14f9c5c9 338
f27cf670
AS
339void *
340grow_vect (void *vect, size_t *size, size_t min_size, int element_size)
14f9c5c9 341{
d2e4a39e
AS
342 if (*size < min_size)
343 {
344 *size *= 2;
345 if (*size < min_size)
4c4b4cd2 346 *size = min_size;
f27cf670 347 vect = xrealloc (vect, *size * element_size);
d2e4a39e 348 }
f27cf670 349 return vect;
14f9c5c9
AS
350}
351
352/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 353 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
354
355static int
ebf56fd3 356field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
357{
358 int len = strlen (target);
d2e4a39e 359 return
4c4b4cd2
PH
360 (strncmp (field_name, target, len) == 0
361 && (field_name[len] == '\0'
362 || (strncmp (field_name + len, "___", 3) == 0
76a01679
JB
363 && strcmp (field_name + strlen (field_name) - 6,
364 "___XVN") != 0)));
14f9c5c9
AS
365}
366
367
4c4b4cd2
PH
368/* Assuming TYPE is a TYPE_CODE_STRUCT, find the field whose name matches
369 FIELD_NAME, and return its index. This function also handles fields
370 whose name have ___ suffixes because the compiler sometimes alters
371 their name by adding such a suffix to represent fields with certain
372 constraints. If the field could not be found, return a negative
373 number if MAYBE_MISSING is set. Otherwise raise an error. */
374
375int
376ada_get_field_index (const struct type *type, const char *field_name,
377 int maybe_missing)
378{
379 int fieldno;
380 for (fieldno = 0; fieldno < TYPE_NFIELDS (type); fieldno++)
381 if (field_name_match (TYPE_FIELD_NAME (type, fieldno), field_name))
382 return fieldno;
383
384 if (!maybe_missing)
323e0a4a 385 error (_("Unable to find field %s in struct %s. Aborting"),
4c4b4cd2
PH
386 field_name, TYPE_NAME (type));
387
388 return -1;
389}
390
391/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
392
393int
d2e4a39e 394ada_name_prefix_len (const char *name)
14f9c5c9
AS
395{
396 if (name == NULL)
397 return 0;
d2e4a39e 398 else
14f9c5c9 399 {
d2e4a39e 400 const char *p = strstr (name, "___");
14f9c5c9 401 if (p == NULL)
4c4b4cd2 402 return strlen (name);
14f9c5c9 403 else
4c4b4cd2 404 return p - name;
14f9c5c9
AS
405 }
406}
407
4c4b4cd2
PH
408/* Return non-zero if SUFFIX is a suffix of STR.
409 Return zero if STR is null. */
410
14f9c5c9 411static int
d2e4a39e 412is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
413{
414 int len1, len2;
415 if (str == NULL)
416 return 0;
417 len1 = strlen (str);
418 len2 = strlen (suffix);
4c4b4cd2 419 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
420}
421
422/* Create a value of type TYPE whose contents come from VALADDR, if it
4c4b4cd2
PH
423 is non-null, and whose memory address (in the inferior) is
424 ADDRESS. */
425
d2e4a39e 426struct value *
10a2c479 427value_from_contents_and_address (struct type *type,
fc1a4b47 428 const gdb_byte *valaddr,
4c4b4cd2 429 CORE_ADDR address)
14f9c5c9 430{
d2e4a39e
AS
431 struct value *v = allocate_value (type);
432 if (valaddr == NULL)
dfa52d88 433 set_value_lazy (v, 1);
14f9c5c9 434 else
990a07ab 435 memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
14f9c5c9
AS
436 VALUE_ADDRESS (v) = address;
437 if (address != 0)
438 VALUE_LVAL (v) = lval_memory;
439 return v;
440}
441
4c4b4cd2
PH
442/* The contents of value VAL, treated as a value of type TYPE. The
443 result is an lval in memory if VAL is. */
14f9c5c9 444
d2e4a39e 445static struct value *
4c4b4cd2 446coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 447{
61ee279c 448 type = ada_check_typedef (type);
df407dfe 449 if (value_type (val) == type)
4c4b4cd2 450 return val;
d2e4a39e 451 else
14f9c5c9 452 {
4c4b4cd2
PH
453 struct value *result;
454
455 /* Make sure that the object size is not unreasonable before
456 trying to allocate some memory for it. */
714e53ab 457 check_size (type);
4c4b4cd2
PH
458
459 result = allocate_value (type);
460 VALUE_LVAL (result) = VALUE_LVAL (val);
9bbda503
AC
461 set_value_bitsize (result, value_bitsize (val));
462 set_value_bitpos (result, value_bitpos (val));
df407dfe 463 VALUE_ADDRESS (result) = VALUE_ADDRESS (val) + value_offset (val);
d69fe07e 464 if (value_lazy (val)
df407dfe 465 || TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
dfa52d88 466 set_value_lazy (result, 1);
d2e4a39e 467 else
0fd88904 468 memcpy (value_contents_raw (result), value_contents (val),
4c4b4cd2 469 TYPE_LENGTH (type));
14f9c5c9
AS
470 return result;
471 }
472}
473
fc1a4b47
AC
474static const gdb_byte *
475cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
476{
477 if (valaddr == NULL)
478 return NULL;
479 else
480 return valaddr + offset;
481}
482
483static CORE_ADDR
ebf56fd3 484cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
485{
486 if (address == 0)
487 return 0;
d2e4a39e 488 else
14f9c5c9
AS
489 return address + offset;
490}
491
4c4b4cd2
PH
492/* Issue a warning (as for the definition of warning in utils.c, but
493 with exactly one argument rather than ...), unless the limit on the
494 number of warnings has passed during the evaluation of the current
495 expression. */
a2249542 496
77109804
AC
497/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
498 provided by "complaint". */
499static void lim_warning (const char *format, ...) ATTR_FORMAT (printf, 1, 2);
500
14f9c5c9 501static void
a2249542 502lim_warning (const char *format, ...)
14f9c5c9 503{
a2249542
MK
504 va_list args;
505 va_start (args, format);
506
4c4b4cd2
PH
507 warnings_issued += 1;
508 if (warnings_issued <= warning_limit)
a2249542
MK
509 vwarning (format, args);
510
511 va_end (args);
4c4b4cd2
PH
512}
513
714e53ab
PH
514/* Issue an error if the size of an object of type T is unreasonable,
515 i.e. if it would be a bad idea to allocate a value of this type in
516 GDB. */
517
518static void
519check_size (const struct type *type)
520{
521 if (TYPE_LENGTH (type) > varsize_limit)
323e0a4a 522 error (_("object size is larger than varsize-limit"));
714e53ab
PH
523}
524
525
c3e5cd34
PH
526/* Note: would have used MAX_OF_TYPE and MIN_OF_TYPE macros from
527 gdbtypes.h, but some of the necessary definitions in that file
528 seem to have gone missing. */
529
530/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 531static LONGEST
c3e5cd34 532max_of_size (int size)
4c4b4cd2 533{
76a01679
JB
534 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
535 return top_bit | (top_bit - 1);
4c4b4cd2
PH
536}
537
c3e5cd34 538/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 539static LONGEST
c3e5cd34 540min_of_size (int size)
4c4b4cd2 541{
c3e5cd34 542 return -max_of_size (size) - 1;
4c4b4cd2
PH
543}
544
c3e5cd34 545/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 546static ULONGEST
c3e5cd34 547umax_of_size (int size)
4c4b4cd2 548{
76a01679
JB
549 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
550 return top_bit | (top_bit - 1);
4c4b4cd2
PH
551}
552
c3e5cd34
PH
553/* Maximum value of integral type T, as a signed quantity. */
554static LONGEST
555max_of_type (struct type *t)
4c4b4cd2 556{
c3e5cd34
PH
557 if (TYPE_UNSIGNED (t))
558 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
559 else
560 return max_of_size (TYPE_LENGTH (t));
561}
562
563/* Minimum value of integral type T, as a signed quantity. */
564static LONGEST
565min_of_type (struct type *t)
566{
567 if (TYPE_UNSIGNED (t))
568 return 0;
569 else
570 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
571}
572
573/* The largest value in the domain of TYPE, a discrete type, as an integer. */
574static struct value *
575discrete_type_high_bound (struct type *type)
576{
76a01679 577 switch (TYPE_CODE (type))
4c4b4cd2
PH
578 {
579 case TYPE_CODE_RANGE:
580 return value_from_longest (TYPE_TARGET_TYPE (type),
76a01679 581 TYPE_HIGH_BOUND (type));
4c4b4cd2 582 case TYPE_CODE_ENUM:
76a01679
JB
583 return
584 value_from_longest (type,
585 TYPE_FIELD_BITPOS (type,
586 TYPE_NFIELDS (type) - 1));
587 case TYPE_CODE_INT:
c3e5cd34 588 return value_from_longest (type, max_of_type (type));
4c4b4cd2 589 default:
323e0a4a 590 error (_("Unexpected type in discrete_type_high_bound."));
4c4b4cd2
PH
591 }
592}
593
594/* The largest value in the domain of TYPE, a discrete type, as an integer. */
595static struct value *
596discrete_type_low_bound (struct type *type)
597{
76a01679 598 switch (TYPE_CODE (type))
4c4b4cd2
PH
599 {
600 case TYPE_CODE_RANGE:
601 return value_from_longest (TYPE_TARGET_TYPE (type),
76a01679 602 TYPE_LOW_BOUND (type));
4c4b4cd2 603 case TYPE_CODE_ENUM:
76a01679
JB
604 return value_from_longest (type, TYPE_FIELD_BITPOS (type, 0));
605 case TYPE_CODE_INT:
c3e5cd34 606 return value_from_longest (type, min_of_type (type));
4c4b4cd2 607 default:
323e0a4a 608 error (_("Unexpected type in discrete_type_low_bound."));
4c4b4cd2
PH
609 }
610}
611
612/* The identity on non-range types. For range types, the underlying
76a01679 613 non-range scalar type. */
4c4b4cd2
PH
614
615static struct type *
616base_type (struct type *type)
617{
618 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
619 {
76a01679
JB
620 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
621 return type;
4c4b4cd2
PH
622 type = TYPE_TARGET_TYPE (type);
623 }
624 return type;
14f9c5c9 625}
4c4b4cd2 626\f
76a01679 627
4c4b4cd2 628 /* Language Selection */
14f9c5c9
AS
629
630/* If the main program is in Ada, return language_ada, otherwise return LANG
631 (the main program is in Ada iif the adainit symbol is found).
632
4c4b4cd2 633 MAIN_PST is not used. */
d2e4a39e 634
14f9c5c9 635enum language
d2e4a39e 636ada_update_initial_language (enum language lang,
4c4b4cd2 637 struct partial_symtab *main_pst)
14f9c5c9 638{
d2e4a39e 639 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
4c4b4cd2
PH
640 (struct objfile *) NULL) != NULL)
641 return language_ada;
14f9c5c9
AS
642
643 return lang;
644}
96d887e8
PH
645
646/* If the main procedure is written in Ada, then return its name.
647 The result is good until the next call. Return NULL if the main
648 procedure doesn't appear to be in Ada. */
649
650char *
651ada_main_name (void)
652{
653 struct minimal_symbol *msym;
654 CORE_ADDR main_program_name_addr;
655 static char main_program_name[1024];
6c038f32 656
96d887e8
PH
657 /* For Ada, the name of the main procedure is stored in a specific
658 string constant, generated by the binder. Look for that symbol,
659 extract its address, and then read that string. If we didn't find
660 that string, then most probably the main procedure is not written
661 in Ada. */
662 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
663
664 if (msym != NULL)
665 {
666 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
667 if (main_program_name_addr == 0)
323e0a4a 668 error (_("Invalid address for Ada main program name."));
96d887e8
PH
669
670 extract_string (main_program_name_addr, main_program_name);
671 return main_program_name;
672 }
673
674 /* The main procedure doesn't seem to be in Ada. */
675 return NULL;
676}
14f9c5c9 677\f
4c4b4cd2 678 /* Symbols */
d2e4a39e 679
4c4b4cd2
PH
680/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
681 of NULLs. */
14f9c5c9 682
d2e4a39e
AS
683const struct ada_opname_map ada_opname_table[] = {
684 {"Oadd", "\"+\"", BINOP_ADD},
685 {"Osubtract", "\"-\"", BINOP_SUB},
686 {"Omultiply", "\"*\"", BINOP_MUL},
687 {"Odivide", "\"/\"", BINOP_DIV},
688 {"Omod", "\"mod\"", BINOP_MOD},
689 {"Orem", "\"rem\"", BINOP_REM},
690 {"Oexpon", "\"**\"", BINOP_EXP},
691 {"Olt", "\"<\"", BINOP_LESS},
692 {"Ole", "\"<=\"", BINOP_LEQ},
693 {"Ogt", "\">\"", BINOP_GTR},
694 {"Oge", "\">=\"", BINOP_GEQ},
695 {"Oeq", "\"=\"", BINOP_EQUAL},
696 {"One", "\"/=\"", BINOP_NOTEQUAL},
697 {"Oand", "\"and\"", BINOP_BITWISE_AND},
698 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
699 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
700 {"Oconcat", "\"&\"", BINOP_CONCAT},
701 {"Oabs", "\"abs\"", UNOP_ABS},
702 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
703 {"Oadd", "\"+\"", UNOP_PLUS},
704 {"Osubtract", "\"-\"", UNOP_NEG},
705 {NULL, NULL}
14f9c5c9
AS
706};
707
4c4b4cd2
PH
708/* Return non-zero if STR should be suppressed in info listings. */
709
14f9c5c9 710static int
d2e4a39e 711is_suppressed_name (const char *str)
14f9c5c9 712{
4c4b4cd2 713 if (strncmp (str, "_ada_", 5) == 0)
14f9c5c9
AS
714 str += 5;
715 if (str[0] == '_' || str[0] == '\000')
716 return 1;
717 else
718 {
d2e4a39e
AS
719 const char *p;
720 const char *suffix = strstr (str, "___");
14f9c5c9 721 if (suffix != NULL && suffix[3] != 'X')
4c4b4cd2 722 return 1;
14f9c5c9 723 if (suffix == NULL)
4c4b4cd2 724 suffix = str + strlen (str);
d2e4a39e 725 for (p = suffix - 1; p != str; p -= 1)
4c4b4cd2
PH
726 if (isupper (*p))
727 {
728 int i;
729 if (p[0] == 'X' && p[-1] != '_')
730 goto OK;
731 if (*p != 'O')
732 return 1;
733 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
734 if (strncmp (ada_opname_table[i].encoded, p,
735 strlen (ada_opname_table[i].encoded)) == 0)
736 goto OK;
737 return 1;
738 OK:;
739 }
14f9c5c9
AS
740 return 0;
741 }
742}
743
4c4b4cd2
PH
744/* The "encoded" form of DECODED, according to GNAT conventions.
745 The result is valid until the next call to ada_encode. */
746
14f9c5c9 747char *
4c4b4cd2 748ada_encode (const char *decoded)
14f9c5c9 749{
4c4b4cd2
PH
750 static char *encoding_buffer = NULL;
751 static size_t encoding_buffer_size = 0;
d2e4a39e 752 const char *p;
14f9c5c9 753 int k;
d2e4a39e 754
4c4b4cd2 755 if (decoded == NULL)
14f9c5c9
AS
756 return NULL;
757
4c4b4cd2
PH
758 GROW_VECT (encoding_buffer, encoding_buffer_size,
759 2 * strlen (decoded) + 10);
14f9c5c9
AS
760
761 k = 0;
4c4b4cd2 762 for (p = decoded; *p != '\0'; p += 1)
14f9c5c9 763 {
4c4b4cd2
PH
764 if (!ADA_RETAIN_DOTS && *p == '.')
765 {
766 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
767 k += 2;
768 }
14f9c5c9 769 else if (*p == '"')
4c4b4cd2
PH
770 {
771 const struct ada_opname_map *mapping;
772
773 for (mapping = ada_opname_table;
1265e4aa
JB
774 mapping->encoded != NULL
775 && strncmp (mapping->decoded, p,
776 strlen (mapping->decoded)) != 0; mapping += 1)
4c4b4cd2
PH
777 ;
778 if (mapping->encoded == NULL)
323e0a4a 779 error (_("invalid Ada operator name: %s"), p);
4c4b4cd2
PH
780 strcpy (encoding_buffer + k, mapping->encoded);
781 k += strlen (mapping->encoded);
782 break;
783 }
d2e4a39e 784 else
4c4b4cd2
PH
785 {
786 encoding_buffer[k] = *p;
787 k += 1;
788 }
14f9c5c9
AS
789 }
790
4c4b4cd2
PH
791 encoding_buffer[k] = '\0';
792 return encoding_buffer;
14f9c5c9
AS
793}
794
795/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
796 quotes, unfolded, but with the quotes stripped away. Result good
797 to next call. */
798
d2e4a39e
AS
799char *
800ada_fold_name (const char *name)
14f9c5c9 801{
d2e4a39e 802 static char *fold_buffer = NULL;
14f9c5c9
AS
803 static size_t fold_buffer_size = 0;
804
805 int len = strlen (name);
d2e4a39e 806 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
14f9c5c9
AS
807
808 if (name[0] == '\'')
809 {
d2e4a39e
AS
810 strncpy (fold_buffer, name + 1, len - 2);
811 fold_buffer[len - 2] = '\000';
14f9c5c9
AS
812 }
813 else
814 {
815 int i;
816 for (i = 0; i <= len; i += 1)
4c4b4cd2 817 fold_buffer[i] = tolower (name[i]);
14f9c5c9
AS
818 }
819
820 return fold_buffer;
821}
822
4c4b4cd2
PH
823/* decode:
824 0. Discard trailing .{DIGIT}+ or trailing ___{DIGIT}+
825 These are suffixes introduced by GNAT5 to nested subprogram
826 names, and do not serve any purpose for the debugger.
827 1. Discard final __{DIGIT}+ or $({DIGIT}+(__{DIGIT}+)*)
14f9c5c9
AS
828 2. Convert other instances of embedded "__" to `.'.
829 3. Discard leading _ada_.
830 4. Convert operator names to the appropriate quoted symbols.
4c4b4cd2 831 5. Remove everything after first ___ if it is followed by
14f9c5c9
AS
832 'X'.
833 6. Replace TK__ with __, and a trailing B or TKB with nothing.
834 7. Put symbols that should be suppressed in <...> brackets.
835 8. Remove trailing X[bn]* suffix (indicating names in package bodies).
14f9c5c9 836
4c4b4cd2
PH
837 The resulting string is valid until the next call of ada_decode.
838 If the string is unchanged by demangling, the original string pointer
839 is returned. */
840
841const char *
842ada_decode (const char *encoded)
14f9c5c9
AS
843{
844 int i, j;
845 int len0;
d2e4a39e 846 const char *p;
4c4b4cd2 847 char *decoded;
14f9c5c9 848 int at_start_name;
4c4b4cd2
PH
849 static char *decoding_buffer = NULL;
850 static size_t decoding_buffer_size = 0;
d2e4a39e 851
4c4b4cd2
PH
852 if (strncmp (encoded, "_ada_", 5) == 0)
853 encoded += 5;
14f9c5c9 854
4c4b4cd2 855 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
856 goto Suppress;
857
4c4b4cd2
PH
858 /* Remove trailing .{DIGIT}+ or ___{DIGIT}+. */
859 len0 = strlen (encoded);
860 if (len0 > 1 && isdigit (encoded[len0 - 1]))
861 {
862 i = len0 - 2;
863 while (i > 0 && isdigit (encoded[i]))
864 i--;
865 if (i >= 0 && encoded[i] == '.')
866 len0 = i;
867 else if (i >= 2 && strncmp (encoded + i - 2, "___", 3) == 0)
868 len0 = i - 2;
869 }
870
871 /* Remove the ___X.* suffix if present. Do not forget to verify that
872 the suffix is located before the current "end" of ENCODED. We want
873 to avoid re-matching parts of ENCODED that have previously been
874 marked as discarded (by decrementing LEN0). */
875 p = strstr (encoded, "___");
876 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
877 {
878 if (p[3] == 'X')
4c4b4cd2 879 len0 = p - encoded;
14f9c5c9 880 else
4c4b4cd2 881 goto Suppress;
14f9c5c9 882 }
4c4b4cd2
PH
883
884 if (len0 > 3 && strncmp (encoded + len0 - 3, "TKB", 3) == 0)
14f9c5c9 885 len0 -= 3;
76a01679 886
4c4b4cd2 887 if (len0 > 1 && strncmp (encoded + len0 - 1, "B", 1) == 0)
14f9c5c9
AS
888 len0 -= 1;
889
4c4b4cd2
PH
890 /* Make decoded big enough for possible expansion by operator name. */
891 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
892 decoded = decoding_buffer;
14f9c5c9 893
4c4b4cd2 894 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 895 {
4c4b4cd2
PH
896 i = len0 - 2;
897 while ((i >= 0 && isdigit (encoded[i]))
898 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
899 i -= 1;
900 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
901 len0 = i - 1;
902 else if (encoded[i] == '$')
903 len0 = i;
d2e4a39e 904 }
14f9c5c9 905
4c4b4cd2
PH
906 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
907 decoded[j] = encoded[i];
14f9c5c9
AS
908
909 at_start_name = 1;
910 while (i < len0)
911 {
4c4b4cd2
PH
912 if (at_start_name && encoded[i] == 'O')
913 {
914 int k;
915 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
916 {
917 int op_len = strlen (ada_opname_table[k].encoded);
06d5cf63
JB
918 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
919 op_len - 1) == 0)
920 && !isalnum (encoded[i + op_len]))
4c4b4cd2
PH
921 {
922 strcpy (decoded + j, ada_opname_table[k].decoded);
923 at_start_name = 0;
924 i += op_len;
925 j += strlen (ada_opname_table[k].decoded);
926 break;
927 }
928 }
929 if (ada_opname_table[k].encoded != NULL)
930 continue;
931 }
14f9c5c9
AS
932 at_start_name = 0;
933
4c4b4cd2
PH
934 if (i < len0 - 4 && strncmp (encoded + i, "TK__", 4) == 0)
935 i += 2;
936 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
937 {
938 do
939 i += 1;
940 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
941 if (i < len0)
942 goto Suppress;
943 }
944 else if (!ADA_RETAIN_DOTS
945 && i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
946 {
947 decoded[j] = '.';
948 at_start_name = 1;
949 i += 2;
950 j += 1;
951 }
14f9c5c9 952 else
4c4b4cd2
PH
953 {
954 decoded[j] = encoded[i];
955 i += 1;
956 j += 1;
957 }
14f9c5c9 958 }
4c4b4cd2 959 decoded[j] = '\000';
14f9c5c9 960
4c4b4cd2
PH
961 for (i = 0; decoded[i] != '\0'; i += 1)
962 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
963 goto Suppress;
964
4c4b4cd2
PH
965 if (strcmp (decoded, encoded) == 0)
966 return encoded;
967 else
968 return decoded;
14f9c5c9
AS
969
970Suppress:
4c4b4cd2
PH
971 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
972 decoded = decoding_buffer;
973 if (encoded[0] == '<')
974 strcpy (decoded, encoded);
14f9c5c9 975 else
4c4b4cd2
PH
976 sprintf (decoded, "<%s>", encoded);
977 return decoded;
978
979}
980
981/* Table for keeping permanent unique copies of decoded names. Once
982 allocated, names in this table are never released. While this is a
983 storage leak, it should not be significant unless there are massive
984 changes in the set of decoded names in successive versions of a
985 symbol table loaded during a single session. */
986static struct htab *decoded_names_store;
987
988/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
989 in the language-specific part of GSYMBOL, if it has not been
990 previously computed. Tries to save the decoded name in the same
991 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
992 in any case, the decoded symbol has a lifetime at least that of
993 GSYMBOL).
994 The GSYMBOL parameter is "mutable" in the C++ sense: logically
995 const, but nevertheless modified to a semantically equivalent form
996 when a decoded name is cached in it.
76a01679 997*/
4c4b4cd2 998
76a01679
JB
999char *
1000ada_decode_symbol (const struct general_symbol_info *gsymbol)
4c4b4cd2 1001{
76a01679 1002 char **resultp =
4c4b4cd2
PH
1003 (char **) &gsymbol->language_specific.cplus_specific.demangled_name;
1004 if (*resultp == NULL)
1005 {
1006 const char *decoded = ada_decode (gsymbol->name);
1007 if (gsymbol->bfd_section != NULL)
76a01679
JB
1008 {
1009 bfd *obfd = gsymbol->bfd_section->owner;
1010 if (obfd != NULL)
1011 {
1012 struct objfile *objf;
1013 ALL_OBJFILES (objf)
1014 {
1015 if (obfd == objf->obfd)
1016 {
1017 *resultp = obsavestring (decoded, strlen (decoded),
1018 &objf->objfile_obstack);
1019 break;
1020 }
1021 }
1022 }
1023 }
4c4b4cd2 1024 /* Sometimes, we can't find a corresponding objfile, in which
76a01679
JB
1025 case, we put the result on the heap. Since we only decode
1026 when needed, we hope this usually does not cause a
1027 significant memory leak (FIXME). */
4c4b4cd2 1028 if (*resultp == NULL)
76a01679
JB
1029 {
1030 char **slot = (char **) htab_find_slot (decoded_names_store,
1031 decoded, INSERT);
1032 if (*slot == NULL)
1033 *slot = xstrdup (decoded);
1034 *resultp = *slot;
1035 }
4c4b4cd2 1036 }
14f9c5c9 1037
4c4b4cd2
PH
1038 return *resultp;
1039}
76a01679
JB
1040
1041char *
1042ada_la_decode (const char *encoded, int options)
4c4b4cd2
PH
1043{
1044 return xstrdup (ada_decode (encoded));
14f9c5c9
AS
1045}
1046
1047/* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
4c4b4cd2
PH
1048 suffixes that encode debugging information or leading _ada_ on
1049 SYM_NAME (see is_name_suffix commentary for the debugging
1050 information that is ignored). If WILD, then NAME need only match a
1051 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1052 either argument is NULL. */
14f9c5c9
AS
1053
1054int
d2e4a39e 1055ada_match_name (const char *sym_name, const char *name, int wild)
14f9c5c9
AS
1056{
1057 if (sym_name == NULL || name == NULL)
1058 return 0;
1059 else if (wild)
1060 return wild_match (name, strlen (name), sym_name);
d2e4a39e
AS
1061 else
1062 {
1063 int len_name = strlen (name);
4c4b4cd2
PH
1064 return (strncmp (sym_name, name, len_name) == 0
1065 && is_name_suffix (sym_name + len_name))
1066 || (strncmp (sym_name, "_ada_", 5) == 0
1067 && strncmp (sym_name + 5, name, len_name) == 0
1068 && is_name_suffix (sym_name + len_name + 5));
d2e4a39e 1069 }
14f9c5c9
AS
1070}
1071
4c4b4cd2
PH
1072/* True (non-zero) iff, in Ada mode, the symbol SYM should be
1073 suppressed in info listings. */
14f9c5c9
AS
1074
1075int
ebf56fd3 1076ada_suppress_symbol_printing (struct symbol *sym)
14f9c5c9 1077{
176620f1 1078 if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN)
14f9c5c9 1079 return 1;
d2e4a39e 1080 else
4c4b4cd2 1081 return is_suppressed_name (SYMBOL_LINKAGE_NAME (sym));
14f9c5c9 1082}
14f9c5c9 1083\f
d2e4a39e 1084
4c4b4cd2 1085 /* Arrays */
14f9c5c9 1086
4c4b4cd2 1087/* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
14f9c5c9 1088
d2e4a39e
AS
1089static char *bound_name[] = {
1090 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
14f9c5c9
AS
1091 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1092};
1093
1094/* Maximum number of array dimensions we are prepared to handle. */
1095
4c4b4cd2 1096#define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
14f9c5c9 1097
4c4b4cd2 1098/* Like modify_field, but allows bitpos > wordlength. */
14f9c5c9
AS
1099
1100static void
ebf56fd3 1101modify_general_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
14f9c5c9 1102{
4c4b4cd2 1103 modify_field (addr + bitpos / 8, fieldval, bitpos % 8, bitsize);
14f9c5c9
AS
1104}
1105
1106
4c4b4cd2
PH
1107/* The desc_* routines return primitive portions of array descriptors
1108 (fat pointers). */
14f9c5c9
AS
1109
1110/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1111 level of indirection, if needed. */
1112
d2e4a39e
AS
1113static struct type *
1114desc_base_type (struct type *type)
14f9c5c9
AS
1115{
1116 if (type == NULL)
1117 return NULL;
61ee279c 1118 type = ada_check_typedef (type);
1265e4aa
JB
1119 if (type != NULL
1120 && (TYPE_CODE (type) == TYPE_CODE_PTR
1121 || TYPE_CODE (type) == TYPE_CODE_REF))
61ee279c 1122 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1123 else
1124 return type;
1125}
1126
4c4b4cd2
PH
1127/* True iff TYPE indicates a "thin" array pointer type. */
1128
14f9c5c9 1129static int
d2e4a39e 1130is_thin_pntr (struct type *type)
14f9c5c9 1131{
d2e4a39e 1132 return
14f9c5c9
AS
1133 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1134 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1135}
1136
4c4b4cd2
PH
1137/* The descriptor type for thin pointer type TYPE. */
1138
d2e4a39e
AS
1139static struct type *
1140thin_descriptor_type (struct type *type)
14f9c5c9 1141{
d2e4a39e 1142 struct type *base_type = desc_base_type (type);
14f9c5c9
AS
1143 if (base_type == NULL)
1144 return NULL;
1145 if (is_suffix (ada_type_name (base_type), "___XVE"))
1146 return base_type;
d2e4a39e 1147 else
14f9c5c9 1148 {
d2e4a39e 1149 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
14f9c5c9 1150 if (alt_type == NULL)
4c4b4cd2 1151 return base_type;
14f9c5c9 1152 else
4c4b4cd2 1153 return alt_type;
14f9c5c9
AS
1154 }
1155}
1156
4c4b4cd2
PH
1157/* A pointer to the array data for thin-pointer value VAL. */
1158
d2e4a39e
AS
1159static struct value *
1160thin_data_pntr (struct value *val)
14f9c5c9 1161{
df407dfe 1162 struct type *type = value_type (val);
14f9c5c9 1163 if (TYPE_CODE (type) == TYPE_CODE_PTR)
d2e4a39e 1164 return value_cast (desc_data_type (thin_descriptor_type (type)),
4c4b4cd2 1165 value_copy (val));
d2e4a39e 1166 else
14f9c5c9 1167 return value_from_longest (desc_data_type (thin_descriptor_type (type)),
df407dfe 1168 VALUE_ADDRESS (val) + value_offset (val));
14f9c5c9
AS
1169}
1170
4c4b4cd2
PH
1171/* True iff TYPE indicates a "thick" array pointer type. */
1172
14f9c5c9 1173static int
d2e4a39e 1174is_thick_pntr (struct type *type)
14f9c5c9
AS
1175{
1176 type = desc_base_type (type);
1177 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2 1178 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1179}
1180
4c4b4cd2
PH
1181/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1182 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1183
d2e4a39e
AS
1184static struct type *
1185desc_bounds_type (struct type *type)
14f9c5c9 1186{
d2e4a39e 1187 struct type *r;
14f9c5c9
AS
1188
1189 type = desc_base_type (type);
1190
1191 if (type == NULL)
1192 return NULL;
1193 else if (is_thin_pntr (type))
1194 {
1195 type = thin_descriptor_type (type);
1196 if (type == NULL)
4c4b4cd2 1197 return NULL;
14f9c5c9
AS
1198 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1199 if (r != NULL)
61ee279c 1200 return ada_check_typedef (r);
14f9c5c9
AS
1201 }
1202 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1203 {
1204 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1205 if (r != NULL)
61ee279c 1206 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1207 }
1208 return NULL;
1209}
1210
1211/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1212 one, a pointer to its bounds data. Otherwise NULL. */
1213
d2e4a39e
AS
1214static struct value *
1215desc_bounds (struct value *arr)
14f9c5c9 1216{
df407dfe 1217 struct type *type = ada_check_typedef (value_type (arr));
d2e4a39e 1218 if (is_thin_pntr (type))
14f9c5c9 1219 {
d2e4a39e 1220 struct type *bounds_type =
4c4b4cd2 1221 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1222 LONGEST addr;
1223
1224 if (desc_bounds_type == NULL)
323e0a4a 1225 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1226
1227 /* NOTE: The following calculation is not really kosher, but
d2e4a39e 1228 since desc_type is an XVE-encoded type (and shouldn't be),
4c4b4cd2 1229 the correct calculation is a real pain. FIXME (and fix GCC). */
14f9c5c9 1230 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4c4b4cd2 1231 addr = value_as_long (arr);
d2e4a39e 1232 else
df407dfe 1233 addr = VALUE_ADDRESS (arr) + value_offset (arr);
14f9c5c9 1234
d2e4a39e 1235 return
4c4b4cd2
PH
1236 value_from_longest (lookup_pointer_type (bounds_type),
1237 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1238 }
1239
1240 else if (is_thick_pntr (type))
d2e4a39e 1241 return value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
323e0a4a 1242 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1243 else
1244 return NULL;
1245}
1246
4c4b4cd2
PH
1247/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1248 position of the field containing the address of the bounds data. */
1249
14f9c5c9 1250static int
d2e4a39e 1251fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1252{
1253 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1254}
1255
1256/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1257 size of the field containing the address of the bounds data. */
1258
14f9c5c9 1259static int
d2e4a39e 1260fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1261{
1262 type = desc_base_type (type);
1263
d2e4a39e 1264 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1265 return TYPE_FIELD_BITSIZE (type, 1);
1266 else
61ee279c 1267 return 8 * TYPE_LENGTH (ada_check_typedef (TYPE_FIELD_TYPE (type, 1)));
14f9c5c9
AS
1268}
1269
4c4b4cd2 1270/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
14f9c5c9 1271 pointer to one, the type of its array data (a
4c4b4cd2
PH
1272 pointer-to-array-with-no-bounds type); otherwise, NULL. Use
1273 ada_type_of_array to get an array type with bounds data. */
1274
d2e4a39e
AS
1275static struct type *
1276desc_data_type (struct type *type)
14f9c5c9
AS
1277{
1278 type = desc_base_type (type);
1279
4c4b4cd2 1280 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1281 if (is_thin_pntr (type))
d2e4a39e
AS
1282 return lookup_pointer_type
1283 (desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1)));
14f9c5c9
AS
1284 else if (is_thick_pntr (type))
1285 return lookup_struct_elt_type (type, "P_ARRAY", 1);
1286 else
1287 return NULL;
1288}
1289
1290/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1291 its array data. */
4c4b4cd2 1292
d2e4a39e
AS
1293static struct value *
1294desc_data (struct value *arr)
14f9c5c9 1295{
df407dfe 1296 struct type *type = value_type (arr);
14f9c5c9
AS
1297 if (is_thin_pntr (type))
1298 return thin_data_pntr (arr);
1299 else if (is_thick_pntr (type))
d2e4a39e 1300 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
323e0a4a 1301 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1302 else
1303 return NULL;
1304}
1305
1306
1307/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1308 position of the field containing the address of the data. */
1309
14f9c5c9 1310static int
d2e4a39e 1311fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1312{
1313 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1314}
1315
1316/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1317 size of the field containing the address of the data. */
1318
14f9c5c9 1319static int
d2e4a39e 1320fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1321{
1322 type = desc_base_type (type);
1323
1324 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1325 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1326 else
14f9c5c9
AS
1327 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1328}
1329
4c4b4cd2 1330/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1331 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1332 bound, if WHICH is 1. The first bound is I=1. */
1333
d2e4a39e
AS
1334static struct value *
1335desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1336{
d2e4a39e 1337 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
323e0a4a 1338 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1339}
1340
1341/* If BOUNDS is an array-bounds structure type, return the bit position
1342 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1343 bound, if WHICH is 1. The first bound is I=1. */
1344
14f9c5c9 1345static int
d2e4a39e 1346desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1347{
d2e4a39e 1348 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1349}
1350
1351/* If BOUNDS is an array-bounds structure type, return the bit field size
1352 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1353 bound, if WHICH is 1. The first bound is I=1. */
1354
76a01679 1355static int
d2e4a39e 1356desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1357{
1358 type = desc_base_type (type);
1359
d2e4a39e
AS
1360 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1361 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1362 else
1363 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
14f9c5c9
AS
1364}
1365
1366/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1367 Ith bound (numbering from 1). Otherwise, NULL. */
1368
d2e4a39e
AS
1369static struct type *
1370desc_index_type (struct type *type, int i)
14f9c5c9
AS
1371{
1372 type = desc_base_type (type);
1373
1374 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
d2e4a39e
AS
1375 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1376 else
14f9c5c9
AS
1377 return NULL;
1378}
1379
4c4b4cd2
PH
1380/* The number of index positions in the array-bounds type TYPE.
1381 Return 0 if TYPE is NULL. */
1382
14f9c5c9 1383static int
d2e4a39e 1384desc_arity (struct type *type)
14f9c5c9
AS
1385{
1386 type = desc_base_type (type);
1387
1388 if (type != NULL)
1389 return TYPE_NFIELDS (type) / 2;
1390 return 0;
1391}
1392
4c4b4cd2
PH
1393/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1394 an array descriptor type (representing an unconstrained array
1395 type). */
1396
76a01679
JB
1397static int
1398ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1399{
1400 if (type == NULL)
1401 return 0;
61ee279c 1402 type = ada_check_typedef (type);
4c4b4cd2 1403 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
76a01679 1404 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1405}
1406
1407/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1408
14f9c5c9 1409int
4c4b4cd2 1410ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1411{
1412 if (type == NULL)
1413 return 0;
61ee279c 1414 type = ada_check_typedef (type);
14f9c5c9 1415 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
4c4b4cd2
PH
1416 || (TYPE_CODE (type) == TYPE_CODE_PTR
1417 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
14f9c5c9
AS
1418}
1419
4c4b4cd2
PH
1420/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1421
14f9c5c9 1422int
4c4b4cd2 1423ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1424{
d2e4a39e 1425 struct type *data_type = desc_data_type (type);
14f9c5c9
AS
1426
1427 if (type == NULL)
1428 return 0;
61ee279c 1429 type = ada_check_typedef (type);
d2e4a39e 1430 return
14f9c5c9
AS
1431 data_type != NULL
1432 && ((TYPE_CODE (data_type) == TYPE_CODE_PTR
4c4b4cd2
PH
1433 && TYPE_TARGET_TYPE (data_type) != NULL
1434 && TYPE_CODE (TYPE_TARGET_TYPE (data_type)) == TYPE_CODE_ARRAY)
1265e4aa 1435 || TYPE_CODE (data_type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
1436 && desc_arity (desc_bounds_type (type)) > 0;
1437}
1438
1439/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1440 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1441 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1442 is still needed. */
1443
14f9c5c9 1444int
ebf56fd3 1445ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1446{
d2e4a39e 1447 return
14f9c5c9
AS
1448 type != NULL
1449 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1450 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
4c4b4cd2
PH
1451 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1452 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1453}
1454
1455
4c4b4cd2 1456/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1457 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1458 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1459 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1460 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1461 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1462 a descriptor. */
d2e4a39e
AS
1463struct type *
1464ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 1465{
df407dfe
AC
1466 if (ada_is_packed_array_type (value_type (arr)))
1467 return decode_packed_array_type (value_type (arr));
14f9c5c9 1468
df407dfe
AC
1469 if (!ada_is_array_descriptor_type (value_type (arr)))
1470 return value_type (arr);
d2e4a39e
AS
1471
1472 if (!bounds)
1473 return
df407dfe 1474 ada_check_typedef (TYPE_TARGET_TYPE (desc_data_type (value_type (arr))));
14f9c5c9
AS
1475 else
1476 {
d2e4a39e 1477 struct type *elt_type;
14f9c5c9 1478 int arity;
d2e4a39e 1479 struct value *descriptor;
df407dfe 1480 struct objfile *objf = TYPE_OBJFILE (value_type (arr));
14f9c5c9 1481
df407dfe
AC
1482 elt_type = ada_array_element_type (value_type (arr), -1);
1483 arity = ada_array_arity (value_type (arr));
14f9c5c9 1484
d2e4a39e 1485 if (elt_type == NULL || arity == 0)
df407dfe 1486 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
1487
1488 descriptor = desc_bounds (arr);
d2e4a39e 1489 if (value_as_long (descriptor) == 0)
4c4b4cd2 1490 return NULL;
d2e4a39e 1491 while (arity > 0)
4c4b4cd2
PH
1492 {
1493 struct type *range_type = alloc_type (objf);
1494 struct type *array_type = alloc_type (objf);
1495 struct value *low = desc_one_bound (descriptor, arity, 0);
1496 struct value *high = desc_one_bound (descriptor, arity, 1);
1497 arity -= 1;
1498
df407dfe 1499 create_range_type (range_type, value_type (low),
4c4b4cd2
PH
1500 (int) value_as_long (low),
1501 (int) value_as_long (high));
1502 elt_type = create_array_type (array_type, elt_type, range_type);
1503 }
14f9c5c9
AS
1504
1505 return lookup_pointer_type (elt_type);
1506 }
1507}
1508
1509/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
1510 Otherwise, returns either a standard GDB array with bounds set
1511 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1512 GDB array. Returns NULL if ARR is a null fat pointer. */
1513
d2e4a39e
AS
1514struct value *
1515ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 1516{
df407dfe 1517 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1518 {
d2e4a39e 1519 struct type *arrType = ada_type_of_array (arr, 1);
14f9c5c9 1520 if (arrType == NULL)
4c4b4cd2 1521 return NULL;
14f9c5c9
AS
1522 return value_cast (arrType, value_copy (desc_data (arr)));
1523 }
df407dfe 1524 else if (ada_is_packed_array_type (value_type (arr)))
14f9c5c9
AS
1525 return decode_packed_array (arr);
1526 else
1527 return arr;
1528}
1529
1530/* If ARR does not represent an array, returns ARR unchanged.
1531 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
1532 be ARR itself if it already is in the proper form). */
1533
1534static struct value *
d2e4a39e 1535ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 1536{
df407dfe 1537 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1538 {
d2e4a39e 1539 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
14f9c5c9 1540 if (arrVal == NULL)
323e0a4a 1541 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
1542 return value_ind (arrVal);
1543 }
df407dfe 1544 else if (ada_is_packed_array_type (value_type (arr)))
14f9c5c9 1545 return decode_packed_array (arr);
d2e4a39e 1546 else
14f9c5c9
AS
1547 return arr;
1548}
1549
1550/* If TYPE represents a GNAT array type, return it translated to an
1551 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
1552 packing). For other types, is the identity. */
1553
d2e4a39e
AS
1554struct type *
1555ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1556{
d2e4a39e
AS
1557 struct value *mark = value_mark ();
1558 struct value *dummy = value_from_longest (builtin_type_long, 0);
1559 struct type *result;
04624583 1560 deprecated_set_value_type (dummy, type);
14f9c5c9 1561 result = ada_type_of_array (dummy, 0);
4c4b4cd2 1562 value_free_to_mark (mark);
14f9c5c9
AS
1563 return result;
1564}
1565
4c4b4cd2
PH
1566/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1567
14f9c5c9 1568int
d2e4a39e 1569ada_is_packed_array_type (struct type *type)
14f9c5c9
AS
1570{
1571 if (type == NULL)
1572 return 0;
4c4b4cd2 1573 type = desc_base_type (type);
61ee279c 1574 type = ada_check_typedef (type);
d2e4a39e 1575 return
14f9c5c9
AS
1576 ada_type_name (type) != NULL
1577 && strstr (ada_type_name (type), "___XP") != NULL;
1578}
1579
1580/* Given that TYPE is a standard GDB array type with all bounds filled
1581 in, and that the element size of its ultimate scalar constituents
1582 (that is, either its elements, or, if it is an array of arrays, its
1583 elements' elements, etc.) is *ELT_BITS, return an identical type,
1584 but with the bit sizes of its elements (and those of any
1585 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2
PH
1586 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1587 in bits. */
1588
d2e4a39e
AS
1589static struct type *
1590packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 1591{
d2e4a39e
AS
1592 struct type *new_elt_type;
1593 struct type *new_type;
14f9c5c9
AS
1594 LONGEST low_bound, high_bound;
1595
61ee279c 1596 type = ada_check_typedef (type);
14f9c5c9
AS
1597 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1598 return type;
1599
1600 new_type = alloc_type (TYPE_OBJFILE (type));
61ee279c 1601 new_elt_type = packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
4c4b4cd2 1602 elt_bits);
14f9c5c9
AS
1603 create_array_type (new_type, new_elt_type, TYPE_FIELD_TYPE (type, 0));
1604 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1605 TYPE_NAME (new_type) = ada_type_name (type);
1606
d2e4a39e 1607 if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2 1608 &low_bound, &high_bound) < 0)
14f9c5c9
AS
1609 low_bound = high_bound = 0;
1610 if (high_bound < low_bound)
1611 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 1612 else
14f9c5c9
AS
1613 {
1614 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 1615 TYPE_LENGTH (new_type) =
4c4b4cd2 1616 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
1617 }
1618
4c4b4cd2 1619 TYPE_FLAGS (new_type) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
1620 return new_type;
1621}
1622
4c4b4cd2
PH
1623/* The array type encoded by TYPE, where ada_is_packed_array_type (TYPE). */
1624
d2e4a39e
AS
1625static struct type *
1626decode_packed_array_type (struct type *type)
1627{
4c4b4cd2 1628 struct symbol *sym;
d2e4a39e 1629 struct block **blocks;
61ee279c 1630 const char *raw_name = ada_type_name (ada_check_typedef (type));
d2e4a39e
AS
1631 char *name = (char *) alloca (strlen (raw_name) + 1);
1632 char *tail = strstr (raw_name, "___XP");
1633 struct type *shadow_type;
14f9c5c9
AS
1634 long bits;
1635 int i, n;
1636
4c4b4cd2
PH
1637 type = desc_base_type (type);
1638
14f9c5c9
AS
1639 memcpy (name, raw_name, tail - raw_name);
1640 name[tail - raw_name] = '\000';
1641
4c4b4cd2
PH
1642 sym = standard_lookup (name, get_selected_block (0), VAR_DOMAIN);
1643 if (sym == NULL || SYMBOL_TYPE (sym) == NULL)
14f9c5c9 1644 {
323e0a4a 1645 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
1646 return NULL;
1647 }
4c4b4cd2 1648 shadow_type = SYMBOL_TYPE (sym);
14f9c5c9
AS
1649
1650 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
1651 {
323e0a4a 1652 lim_warning (_("could not understand bounds information on packed array"));
14f9c5c9
AS
1653 return NULL;
1654 }
d2e4a39e 1655
14f9c5c9
AS
1656 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1657 {
4c4b4cd2 1658 lim_warning
323e0a4a 1659 (_("could not understand bit size information on packed array"));
14f9c5c9
AS
1660 return NULL;
1661 }
d2e4a39e 1662
14f9c5c9
AS
1663 return packed_array_type (shadow_type, &bits);
1664}
1665
4c4b4cd2 1666/* Given that ARR is a struct value *indicating a GNAT packed array,
14f9c5c9
AS
1667 returns a simple array that denotes that array. Its type is a
1668 standard GDB array type except that the BITSIZEs of the array
1669 target types are set to the number of bits in each element, and the
4c4b4cd2 1670 type length is set appropriately. */
14f9c5c9 1671
d2e4a39e
AS
1672static struct value *
1673decode_packed_array (struct value *arr)
14f9c5c9 1674{
4c4b4cd2 1675 struct type *type;
14f9c5c9 1676
4c4b4cd2 1677 arr = ada_coerce_ref (arr);
df407dfe 1678 if (TYPE_CODE (value_type (arr)) == TYPE_CODE_PTR)
4c4b4cd2
PH
1679 arr = ada_value_ind (arr);
1680
df407dfe 1681 type = decode_packed_array_type (value_type (arr));
14f9c5c9
AS
1682 if (type == NULL)
1683 {
323e0a4a 1684 error (_("can't unpack array"));
14f9c5c9
AS
1685 return NULL;
1686 }
61ee279c 1687
df407dfe 1688 if (BITS_BIG_ENDIAN && ada_is_modular_type (value_type (arr)))
61ee279c
PH
1689 {
1690 /* This is a (right-justified) modular type representing a packed
1691 array with no wrapper. In order to interpret the value through
1692 the (left-justified) packed array type we just built, we must
1693 first left-justify it. */
1694 int bit_size, bit_pos;
1695 ULONGEST mod;
1696
df407dfe 1697 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
1698 bit_size = 0;
1699 while (mod > 0)
1700 {
1701 bit_size += 1;
1702 mod >>= 1;
1703 }
df407dfe 1704 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
1705 arr = ada_value_primitive_packed_val (arr, NULL,
1706 bit_pos / HOST_CHAR_BIT,
1707 bit_pos % HOST_CHAR_BIT,
1708 bit_size,
1709 type);
1710 }
1711
4c4b4cd2 1712 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
1713}
1714
1715
1716/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 1717 given in IND. ARR must be a simple array. */
14f9c5c9 1718
d2e4a39e
AS
1719static struct value *
1720value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
1721{
1722 int i;
1723 int bits, elt_off, bit_off;
1724 long elt_total_bit_offset;
d2e4a39e
AS
1725 struct type *elt_type;
1726 struct value *v;
14f9c5c9
AS
1727
1728 bits = 0;
1729 elt_total_bit_offset = 0;
df407dfe 1730 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 1731 for (i = 0; i < arity; i += 1)
14f9c5c9 1732 {
d2e4a39e 1733 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
4c4b4cd2
PH
1734 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
1735 error
323e0a4a 1736 (_("attempt to do packed indexing of something other than a packed array"));
14f9c5c9 1737 else
4c4b4cd2
PH
1738 {
1739 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
1740 LONGEST lowerbound, upperbound;
1741 LONGEST idx;
1742
1743 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
1744 {
323e0a4a 1745 lim_warning (_("don't know bounds of array"));
4c4b4cd2
PH
1746 lowerbound = upperbound = 0;
1747 }
1748
1749 idx = value_as_long (value_pos_atr (ind[i]));
1750 if (idx < lowerbound || idx > upperbound)
323e0a4a 1751 lim_warning (_("packed array index %ld out of bounds"), (long) idx);
4c4b4cd2
PH
1752 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
1753 elt_total_bit_offset += (idx - lowerbound) * bits;
61ee279c 1754 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
4c4b4cd2 1755 }
14f9c5c9
AS
1756 }
1757 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
1758 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
1759
1760 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
4c4b4cd2 1761 bits, elt_type);
14f9c5c9
AS
1762 if (VALUE_LVAL (arr) == lval_internalvar)
1763 VALUE_LVAL (v) = lval_internalvar_component;
1764 else
1765 VALUE_LVAL (v) = VALUE_LVAL (arr);
1766 return v;
1767}
1768
4c4b4cd2 1769/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
1770
1771static int
d2e4a39e 1772has_negatives (struct type *type)
14f9c5c9 1773{
d2e4a39e
AS
1774 switch (TYPE_CODE (type))
1775 {
1776 default:
1777 return 0;
1778 case TYPE_CODE_INT:
1779 return !TYPE_UNSIGNED (type);
1780 case TYPE_CODE_RANGE:
1781 return TYPE_LOW_BOUND (type) < 0;
1782 }
14f9c5c9 1783}
d2e4a39e 1784
14f9c5c9
AS
1785
1786/* Create a new value of type TYPE from the contents of OBJ starting
1787 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
1788 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
4c4b4cd2
PH
1789 assigning through the result will set the field fetched from.
1790 VALADDR is ignored unless OBJ is NULL, in which case,
1791 VALADDR+OFFSET must address the start of storage containing the
1792 packed value. The value returned in this case is never an lval.
1793 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
14f9c5c9 1794
d2e4a39e 1795struct value *
fc1a4b47 1796ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
a2bd3dcd 1797 long offset, int bit_offset, int bit_size,
4c4b4cd2 1798 struct type *type)
14f9c5c9 1799{
d2e4a39e 1800 struct value *v;
4c4b4cd2
PH
1801 int src, /* Index into the source area */
1802 targ, /* Index into the target area */
1803 srcBitsLeft, /* Number of source bits left to move */
1804 nsrc, ntarg, /* Number of source and target bytes */
1805 unusedLS, /* Number of bits in next significant
1806 byte of source that are unused */
1807 accumSize; /* Number of meaningful bits in accum */
1808 unsigned char *bytes; /* First byte containing data to unpack */
d2e4a39e 1809 unsigned char *unpacked;
4c4b4cd2 1810 unsigned long accum; /* Staging area for bits being transferred */
14f9c5c9
AS
1811 unsigned char sign;
1812 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
4c4b4cd2
PH
1813 /* Transmit bytes from least to most significant; delta is the direction
1814 the indices move. */
14f9c5c9
AS
1815 int delta = BITS_BIG_ENDIAN ? -1 : 1;
1816
61ee279c 1817 type = ada_check_typedef (type);
14f9c5c9
AS
1818
1819 if (obj == NULL)
1820 {
1821 v = allocate_value (type);
d2e4a39e 1822 bytes = (unsigned char *) (valaddr + offset);
14f9c5c9 1823 }
d69fe07e 1824 else if (value_lazy (obj))
14f9c5c9
AS
1825 {
1826 v = value_at (type,
df407dfe 1827 VALUE_ADDRESS (obj) + value_offset (obj) + offset);
d2e4a39e 1828 bytes = (unsigned char *) alloca (len);
14f9c5c9
AS
1829 read_memory (VALUE_ADDRESS (v), bytes, len);
1830 }
d2e4a39e 1831 else
14f9c5c9
AS
1832 {
1833 v = allocate_value (type);
0fd88904 1834 bytes = (unsigned char *) value_contents (obj) + offset;
14f9c5c9 1835 }
d2e4a39e
AS
1836
1837 if (obj != NULL)
14f9c5c9
AS
1838 {
1839 VALUE_LVAL (v) = VALUE_LVAL (obj);
1840 if (VALUE_LVAL (obj) == lval_internalvar)
4c4b4cd2 1841 VALUE_LVAL (v) = lval_internalvar_component;
df407dfe 1842 VALUE_ADDRESS (v) = VALUE_ADDRESS (obj) + value_offset (obj) + offset;
9bbda503
AC
1843 set_value_bitpos (v, bit_offset + value_bitpos (obj));
1844 set_value_bitsize (v, bit_size);
df407dfe 1845 if (value_bitpos (v) >= HOST_CHAR_BIT)
4c4b4cd2
PH
1846 {
1847 VALUE_ADDRESS (v) += 1;
9bbda503 1848 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
4c4b4cd2 1849 }
14f9c5c9
AS
1850 }
1851 else
9bbda503 1852 set_value_bitsize (v, bit_size);
0fd88904 1853 unpacked = (unsigned char *) value_contents (v);
14f9c5c9
AS
1854
1855 srcBitsLeft = bit_size;
1856 nsrc = len;
1857 ntarg = TYPE_LENGTH (type);
1858 sign = 0;
1859 if (bit_size == 0)
1860 {
1861 memset (unpacked, 0, TYPE_LENGTH (type));
1862 return v;
1863 }
1864 else if (BITS_BIG_ENDIAN)
1865 {
d2e4a39e 1866 src = len - 1;
1265e4aa
JB
1867 if (has_negatives (type)
1868 && ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
4c4b4cd2 1869 sign = ~0;
d2e4a39e
AS
1870
1871 unusedLS =
4c4b4cd2
PH
1872 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
1873 % HOST_CHAR_BIT;
14f9c5c9
AS
1874
1875 switch (TYPE_CODE (type))
4c4b4cd2
PH
1876 {
1877 case TYPE_CODE_ARRAY:
1878 case TYPE_CODE_UNION:
1879 case TYPE_CODE_STRUCT:
1880 /* Non-scalar values must be aligned at a byte boundary... */
1881 accumSize =
1882 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
1883 /* ... And are placed at the beginning (most-significant) bytes
1884 of the target. */
1885 targ = src;
1886 break;
1887 default:
1888 accumSize = 0;
1889 targ = TYPE_LENGTH (type) - 1;
1890 break;
1891 }
14f9c5c9 1892 }
d2e4a39e 1893 else
14f9c5c9
AS
1894 {
1895 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
1896
1897 src = targ = 0;
1898 unusedLS = bit_offset;
1899 accumSize = 0;
1900
d2e4a39e 1901 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
4c4b4cd2 1902 sign = ~0;
14f9c5c9 1903 }
d2e4a39e 1904
14f9c5c9
AS
1905 accum = 0;
1906 while (nsrc > 0)
1907 {
1908 /* Mask for removing bits of the next source byte that are not
4c4b4cd2 1909 part of the value. */
d2e4a39e 1910 unsigned int unusedMSMask =
4c4b4cd2
PH
1911 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
1912 1;
1913 /* Sign-extend bits for this byte. */
14f9c5c9 1914 unsigned int signMask = sign & ~unusedMSMask;
d2e4a39e 1915 accum |=
4c4b4cd2 1916 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 1917 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 1918 if (accumSize >= HOST_CHAR_BIT)
4c4b4cd2
PH
1919 {
1920 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1921 accumSize -= HOST_CHAR_BIT;
1922 accum >>= HOST_CHAR_BIT;
1923 ntarg -= 1;
1924 targ += delta;
1925 }
14f9c5c9
AS
1926 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
1927 unusedLS = 0;
1928 nsrc -= 1;
1929 src += delta;
1930 }
1931 while (ntarg > 0)
1932 {
1933 accum |= sign << accumSize;
1934 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1935 accumSize -= HOST_CHAR_BIT;
1936 accum >>= HOST_CHAR_BIT;
1937 ntarg -= 1;
1938 targ += delta;
1939 }
1940
1941 return v;
1942}
d2e4a39e 1943
14f9c5c9
AS
1944/* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
1945 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
4c4b4cd2 1946 not overlap. */
14f9c5c9 1947static void
fc1a4b47 1948move_bits (gdb_byte *target, int targ_offset, const gdb_byte *source,
0fd88904 1949 int src_offset, int n)
14f9c5c9
AS
1950{
1951 unsigned int accum, mask;
1952 int accum_bits, chunk_size;
1953
1954 target += targ_offset / HOST_CHAR_BIT;
1955 targ_offset %= HOST_CHAR_BIT;
1956 source += src_offset / HOST_CHAR_BIT;
1957 src_offset %= HOST_CHAR_BIT;
d2e4a39e 1958 if (BITS_BIG_ENDIAN)
14f9c5c9
AS
1959 {
1960 accum = (unsigned char) *source;
1961 source += 1;
1962 accum_bits = HOST_CHAR_BIT - src_offset;
1963
d2e4a39e 1964 while (n > 0)
4c4b4cd2
PH
1965 {
1966 int unused_right;
1967 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
1968 accum_bits += HOST_CHAR_BIT;
1969 source += 1;
1970 chunk_size = HOST_CHAR_BIT - targ_offset;
1971 if (chunk_size > n)
1972 chunk_size = n;
1973 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
1974 mask = ((1 << chunk_size) - 1) << unused_right;
1975 *target =
1976 (*target & ~mask)
1977 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
1978 n -= chunk_size;
1979 accum_bits -= chunk_size;
1980 target += 1;
1981 targ_offset = 0;
1982 }
14f9c5c9
AS
1983 }
1984 else
1985 {
1986 accum = (unsigned char) *source >> src_offset;
1987 source += 1;
1988 accum_bits = HOST_CHAR_BIT - src_offset;
1989
d2e4a39e 1990 while (n > 0)
4c4b4cd2
PH
1991 {
1992 accum = accum + ((unsigned char) *source << accum_bits);
1993 accum_bits += HOST_CHAR_BIT;
1994 source += 1;
1995 chunk_size = HOST_CHAR_BIT - targ_offset;
1996 if (chunk_size > n)
1997 chunk_size = n;
1998 mask = ((1 << chunk_size) - 1) << targ_offset;
1999 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2000 n -= chunk_size;
2001 accum_bits -= chunk_size;
2002 accum >>= chunk_size;
2003 target += 1;
2004 targ_offset = 0;
2005 }
14f9c5c9
AS
2006 }
2007}
2008
2009
2010/* Store the contents of FROMVAL into the location of TOVAL.
2011 Return a new value with the location of TOVAL and contents of
2012 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2013 floating-point or non-scalar types. */
14f9c5c9 2014
d2e4a39e
AS
2015static struct value *
2016ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2017{
df407dfe
AC
2018 struct type *type = value_type (toval);
2019 int bits = value_bitsize (toval);
14f9c5c9 2020
88e3b34b 2021 if (!deprecated_value_modifiable (toval))
323e0a4a 2022 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2023
994b9211 2024 toval = coerce_ref (toval);
14f9c5c9 2025
d2e4a39e 2026 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2027 && bits > 0
d2e4a39e 2028 && (TYPE_CODE (type) == TYPE_CODE_FLT
4c4b4cd2 2029 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
14f9c5c9 2030 {
df407dfe
AC
2031 int len = (value_bitpos (toval)
2032 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
d2e4a39e
AS
2033 char *buffer = (char *) alloca (len);
2034 struct value *val;
14f9c5c9
AS
2035
2036 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4c4b4cd2 2037 fromval = value_cast (type, fromval);
14f9c5c9 2038
df407dfe 2039 read_memory (VALUE_ADDRESS (toval) + value_offset (toval), buffer, len);
14f9c5c9 2040 if (BITS_BIG_ENDIAN)
df407dfe 2041 move_bits (buffer, value_bitpos (toval),
0fd88904 2042 value_contents (fromval),
df407dfe 2043 TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT -
4c4b4cd2 2044 bits, bits);
14f9c5c9 2045 else
0fd88904 2046 move_bits (buffer, value_bitpos (toval), value_contents (fromval),
4c4b4cd2 2047 0, bits);
df407dfe 2048 write_memory (VALUE_ADDRESS (toval) + value_offset (toval), buffer,
4c4b4cd2 2049 len);
14f9c5c9
AS
2050
2051 val = value_copy (toval);
0fd88904 2052 memcpy (value_contents_raw (val), value_contents (fromval),
4c4b4cd2 2053 TYPE_LENGTH (type));
04624583 2054 deprecated_set_value_type (val, type);
d2e4a39e 2055
14f9c5c9
AS
2056 return val;
2057 }
2058
2059 return value_assign (toval, fromval);
2060}
2061
2062
4c4b4cd2
PH
2063/* The value of the element of array ARR at the ARITY indices given in IND.
2064 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2065 thereto. */
2066
d2e4a39e
AS
2067struct value *
2068ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2069{
2070 int k;
d2e4a39e
AS
2071 struct value *elt;
2072 struct type *elt_type;
14f9c5c9
AS
2073
2074 elt = ada_coerce_to_simple_array (arr);
2075
df407dfe 2076 elt_type = ada_check_typedef (value_type (elt));
d2e4a39e 2077 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
14f9c5c9
AS
2078 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2079 return value_subscript_packed (elt, arity, ind);
2080
2081 for (k = 0; k < arity; k += 1)
2082 {
2083 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
323e0a4a 2084 error (_("too many subscripts (%d expected)"), k);
14f9c5c9
AS
2085 elt = value_subscript (elt, value_pos_atr (ind[k]));
2086 }
2087 return elt;
2088}
2089
2090/* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
2091 value of the element of *ARR at the ARITY indices given in
4c4b4cd2 2092 IND. Does not read the entire array into memory. */
14f9c5c9 2093
d2e4a39e
AS
2094struct value *
2095ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
4c4b4cd2 2096 struct value **ind)
14f9c5c9
AS
2097{
2098 int k;
2099
2100 for (k = 0; k < arity; k += 1)
2101 {
2102 LONGEST lwb, upb;
d2e4a39e 2103 struct value *idx;
14f9c5c9
AS
2104
2105 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
323e0a4a 2106 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 2107 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4c4b4cd2 2108 value_copy (arr));
14f9c5c9 2109 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
4c4b4cd2
PH
2110 idx = value_pos_atr (ind[k]);
2111 if (lwb != 0)
2112 idx = value_sub (idx, value_from_longest (builtin_type_int, lwb));
14f9c5c9
AS
2113 arr = value_add (arr, idx);
2114 type = TYPE_TARGET_TYPE (type);
2115 }
2116
2117 return value_ind (arr);
2118}
2119
0b5d8877
PH
2120/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
2121 actual type of ARRAY_PTR is ignored), returns a reference to
2122 the Ada slice of HIGH-LOW+1 elements starting at index LOW. The lower
2123 bound of this array is LOW, as per Ada rules. */
2124static struct value *
6c038f32 2125ada_value_slice_ptr (struct value *array_ptr, struct type *type,
0b5d8877
PH
2126 int low, int high)
2127{
6c038f32 2128 CORE_ADDR base = value_as_address (array_ptr)
0b5d8877
PH
2129 + ((low - TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)))
2130 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
6c038f32
PH
2131 struct type *index_type =
2132 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)),
0b5d8877 2133 low, high);
6c038f32 2134 struct type *slice_type =
0b5d8877
PH
2135 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2136 return value_from_pointer (lookup_reference_type (slice_type), base);
2137}
2138
2139
2140static struct value *
2141ada_value_slice (struct value *array, int low, int high)
2142{
df407dfe 2143 struct type *type = value_type (array);
6c038f32 2144 struct type *index_type =
0b5d8877 2145 create_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
6c038f32 2146 struct type *slice_type =
0b5d8877 2147 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
6c038f32 2148 return value_cast (slice_type, value_slice (array, low, high - low + 1));
0b5d8877
PH
2149}
2150
14f9c5c9
AS
2151/* If type is a record type in the form of a standard GNAT array
2152 descriptor, returns the number of dimensions for type. If arr is a
2153 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2154 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2155
2156int
d2e4a39e 2157ada_array_arity (struct type *type)
14f9c5c9
AS
2158{
2159 int arity;
2160
2161 if (type == NULL)
2162 return 0;
2163
2164 type = desc_base_type (type);
2165
2166 arity = 0;
d2e4a39e 2167 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9 2168 return desc_arity (desc_bounds_type (type));
d2e4a39e
AS
2169 else
2170 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9 2171 {
4c4b4cd2 2172 arity += 1;
61ee279c 2173 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2174 }
d2e4a39e 2175
14f9c5c9
AS
2176 return arity;
2177}
2178
2179/* If TYPE is a record type in the form of a standard GNAT array
2180 descriptor or a simple array type, returns the element type for
2181 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2182 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2183
d2e4a39e
AS
2184struct type *
2185ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2186{
2187 type = desc_base_type (type);
2188
d2e4a39e 2189 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9
AS
2190 {
2191 int k;
d2e4a39e 2192 struct type *p_array_type;
14f9c5c9
AS
2193
2194 p_array_type = desc_data_type (type);
2195
2196 k = ada_array_arity (type);
2197 if (k == 0)
4c4b4cd2 2198 return NULL;
d2e4a39e 2199
4c4b4cd2 2200 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2201 if (nindices >= 0 && k > nindices)
4c4b4cd2 2202 k = nindices;
14f9c5c9 2203 p_array_type = TYPE_TARGET_TYPE (p_array_type);
d2e4a39e 2204 while (k > 0 && p_array_type != NULL)
4c4b4cd2 2205 {
61ee279c 2206 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
4c4b4cd2
PH
2207 k -= 1;
2208 }
14f9c5c9
AS
2209 return p_array_type;
2210 }
2211 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2212 {
2213 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
4c4b4cd2
PH
2214 {
2215 type = TYPE_TARGET_TYPE (type);
2216 nindices -= 1;
2217 }
14f9c5c9
AS
2218 return type;
2219 }
2220
2221 return NULL;
2222}
2223
4c4b4cd2
PH
2224/* The type of nth index in arrays of given type (n numbering from 1).
2225 Does not examine memory. */
14f9c5c9 2226
d2e4a39e
AS
2227struct type *
2228ada_index_type (struct type *type, int n)
14f9c5c9 2229{
4c4b4cd2
PH
2230 struct type *result_type;
2231
14f9c5c9
AS
2232 type = desc_base_type (type);
2233
2234 if (n > ada_array_arity (type))
2235 return NULL;
2236
4c4b4cd2 2237 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2238 {
2239 int i;
2240
2241 for (i = 1; i < n; i += 1)
4c4b4cd2
PH
2242 type = TYPE_TARGET_TYPE (type);
2243 result_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
2244 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2245 has a target type of TYPE_CODE_UNDEF. We compensate here, but
76a01679
JB
2246 perhaps stabsread.c would make more sense. */
2247 if (result_type == NULL || TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2248 result_type = builtin_type_int;
14f9c5c9 2249
4c4b4cd2 2250 return result_type;
14f9c5c9 2251 }
d2e4a39e 2252 else
14f9c5c9
AS
2253 return desc_index_type (desc_bounds_type (type), n);
2254}
2255
2256/* Given that arr is an array type, returns the lower bound of the
2257 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2
PH
2258 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
2259 array-descriptor type. If TYPEP is non-null, *TYPEP is set to the
2260 bounds type. It works for other arrays with bounds supplied by
2261 run-time quantities other than discriminants. */
14f9c5c9
AS
2262
2263LONGEST
d2e4a39e 2264ada_array_bound_from_type (struct type * arr_type, int n, int which,
4c4b4cd2 2265 struct type ** typep)
14f9c5c9 2266{
d2e4a39e
AS
2267 struct type *type;
2268 struct type *index_type_desc;
14f9c5c9
AS
2269
2270 if (ada_is_packed_array_type (arr_type))
2271 arr_type = decode_packed_array_type (arr_type);
2272
4c4b4cd2 2273 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
14f9c5c9
AS
2274 {
2275 if (typep != NULL)
4c4b4cd2 2276 *typep = builtin_type_int;
d2e4a39e 2277 return (LONGEST) - which;
14f9c5c9
AS
2278 }
2279
2280 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
2281 type = TYPE_TARGET_TYPE (arr_type);
2282 else
2283 type = arr_type;
2284
2285 index_type_desc = ada_find_parallel_type (type, "___XA");
d2e4a39e 2286 if (index_type_desc == NULL)
14f9c5c9 2287 {
d2e4a39e
AS
2288 struct type *range_type;
2289 struct type *index_type;
14f9c5c9 2290
d2e4a39e 2291 while (n > 1)
4c4b4cd2
PH
2292 {
2293 type = TYPE_TARGET_TYPE (type);
2294 n -= 1;
2295 }
14f9c5c9
AS
2296
2297 range_type = TYPE_INDEX_TYPE (type);
2298 index_type = TYPE_TARGET_TYPE (range_type);
2299 if (TYPE_CODE (index_type) == TYPE_CODE_UNDEF)
4c4b4cd2 2300 index_type = builtin_type_long;
14f9c5c9 2301 if (typep != NULL)
4c4b4cd2 2302 *typep = index_type;
d2e4a39e 2303 return
4c4b4cd2
PH
2304 (LONGEST) (which == 0
2305 ? TYPE_LOW_BOUND (range_type)
2306 : TYPE_HIGH_BOUND (range_type));
14f9c5c9 2307 }
d2e4a39e 2308 else
14f9c5c9 2309 {
d2e4a39e 2310 struct type *index_type =
4c4b4cd2
PH
2311 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, n - 1),
2312 NULL, TYPE_OBJFILE (arr_type));
14f9c5c9 2313 if (typep != NULL)
4c4b4cd2 2314 *typep = TYPE_TARGET_TYPE (index_type);
d2e4a39e 2315 return
4c4b4cd2
PH
2316 (LONGEST) (which == 0
2317 ? TYPE_LOW_BOUND (index_type)
2318 : TYPE_HIGH_BOUND (index_type));
14f9c5c9
AS
2319 }
2320}
2321
2322/* Given that arr is an array value, returns the lower bound of the
2323 nth index (numbering from 1) if which is 0, and the upper bound if
4c4b4cd2
PH
2324 which is 1. This routine will also work for arrays with bounds
2325 supplied by run-time quantities other than discriminants. */
14f9c5c9 2326
d2e4a39e 2327struct value *
4dc81987 2328ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 2329{
df407dfe 2330 struct type *arr_type = value_type (arr);
14f9c5c9
AS
2331
2332 if (ada_is_packed_array_type (arr_type))
2333 return ada_array_bound (decode_packed_array (arr), n, which);
4c4b4cd2 2334 else if (ada_is_simple_array_type (arr_type))
14f9c5c9 2335 {
d2e4a39e 2336 struct type *type;
14f9c5c9
AS
2337 LONGEST v = ada_array_bound_from_type (arr_type, n, which, &type);
2338 return value_from_longest (type, v);
2339 }
2340 else
2341 return desc_one_bound (desc_bounds (arr), n, which);
2342}
2343
2344/* Given that arr is an array value, returns the length of the
2345 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
2346 supplied by run-time quantities other than discriminants.
2347 Does not work for arrays indexed by enumeration types with representation
2348 clauses at the moment. */
14f9c5c9 2349
d2e4a39e
AS
2350struct value *
2351ada_array_length (struct value *arr, int n)
14f9c5c9 2352{
df407dfe 2353 struct type *arr_type = ada_check_typedef (value_type (arr));
14f9c5c9
AS
2354
2355 if (ada_is_packed_array_type (arr_type))
2356 return ada_array_length (decode_packed_array (arr), n);
2357
4c4b4cd2 2358 if (ada_is_simple_array_type (arr_type))
14f9c5c9 2359 {
d2e4a39e 2360 struct type *type;
14f9c5c9 2361 LONGEST v =
4c4b4cd2
PH
2362 ada_array_bound_from_type (arr_type, n, 1, &type) -
2363 ada_array_bound_from_type (arr_type, n, 0, NULL) + 1;
14f9c5c9
AS
2364 return value_from_longest (type, v);
2365 }
2366 else
d2e4a39e 2367 return
72d5681a 2368 value_from_longest (builtin_type_int,
4c4b4cd2
PH
2369 value_as_long (desc_one_bound (desc_bounds (arr),
2370 n, 1))
2371 - value_as_long (desc_one_bound (desc_bounds (arr),
2372 n, 0)) + 1);
2373}
2374
2375/* An empty array whose type is that of ARR_TYPE (an array type),
2376 with bounds LOW to LOW-1. */
2377
2378static struct value *
2379empty_array (struct type *arr_type, int low)
2380{
6c038f32 2381 struct type *index_type =
0b5d8877
PH
2382 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type)),
2383 low, low - 1);
2384 struct type *elt_type = ada_array_element_type (arr_type, 1);
2385 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 2386}
14f9c5c9 2387\f
d2e4a39e 2388
4c4b4cd2 2389 /* Name resolution */
14f9c5c9 2390
4c4b4cd2
PH
2391/* The "decoded" name for the user-definable Ada operator corresponding
2392 to OP. */
14f9c5c9 2393
d2e4a39e 2394static const char *
4c4b4cd2 2395ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
2396{
2397 int i;
2398
4c4b4cd2 2399 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
2400 {
2401 if (ada_opname_table[i].op == op)
4c4b4cd2 2402 return ada_opname_table[i].decoded;
14f9c5c9 2403 }
323e0a4a 2404 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
2405}
2406
2407
4c4b4cd2
PH
2408/* Same as evaluate_type (*EXP), but resolves ambiguous symbol
2409 references (marked by OP_VAR_VALUE nodes in which the symbol has an
2410 undefined namespace) and converts operators that are
2411 user-defined into appropriate function calls. If CONTEXT_TYPE is
14f9c5c9
AS
2412 non-null, it provides a preferred result type [at the moment, only
2413 type void has any effect---causing procedures to be preferred over
2414 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
4c4b4cd2 2415 return type is preferred. May change (expand) *EXP. */
14f9c5c9 2416
4c4b4cd2
PH
2417static void
2418resolve (struct expression **expp, int void_context_p)
14f9c5c9
AS
2419{
2420 int pc;
2421 pc = 0;
4c4b4cd2 2422 resolve_subexp (expp, &pc, 1, void_context_p ? builtin_type_void : NULL);
14f9c5c9
AS
2423}
2424
4c4b4cd2
PH
2425/* Resolve the operator of the subexpression beginning at
2426 position *POS of *EXPP. "Resolving" consists of replacing
2427 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
2428 with their resolutions, replacing built-in operators with
2429 function calls to user-defined operators, where appropriate, and,
2430 when DEPROCEDURE_P is non-zero, converting function-valued variables
2431 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
2432 are as in ada_resolve, above. */
14f9c5c9 2433
d2e4a39e 2434static struct value *
4c4b4cd2 2435resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
76a01679 2436 struct type *context_type)
14f9c5c9
AS
2437{
2438 int pc = *pos;
2439 int i;
4c4b4cd2 2440 struct expression *exp; /* Convenience: == *expp. */
14f9c5c9 2441 enum exp_opcode op = (*expp)->elts[pc].opcode;
4c4b4cd2
PH
2442 struct value **argvec; /* Vector of operand types (alloca'ed). */
2443 int nargs; /* Number of operands. */
14f9c5c9
AS
2444
2445 argvec = NULL;
2446 nargs = 0;
2447 exp = *expp;
2448
4c4b4cd2 2449 /* Pass one: resolve operands, saving their types and updating *pos. */
14f9c5c9
AS
2450 switch (op)
2451 {
4c4b4cd2
PH
2452 case OP_FUNCALL:
2453 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679
JB
2454 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
2455 *pos += 7;
4c4b4cd2
PH
2456 else
2457 {
2458 *pos += 3;
2459 resolve_subexp (expp, pos, 0, NULL);
2460 }
2461 nargs = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9
AS
2462 break;
2463
4c4b4cd2
PH
2464 case UNOP_QUAL:
2465 *pos += 3;
2466 resolve_subexp (expp, pos, 1, exp->elts[pc + 1].type);
14f9c5c9
AS
2467 break;
2468
14f9c5c9 2469 case UNOP_ADDR:
4c4b4cd2
PH
2470 *pos += 1;
2471 resolve_subexp (expp, pos, 0, NULL);
2472 break;
2473
2474 case OP_ATR_MODULUS:
2475 *pos += 4;
2476 break;
2477
2478 case OP_ATR_SIZE:
2479 case OP_ATR_TAG:
2480 *pos += 1;
14f9c5c9 2481 nargs = 1;
4c4b4cd2
PH
2482 break;
2483
2484 case OP_ATR_FIRST:
2485 case OP_ATR_LAST:
2486 case OP_ATR_LENGTH:
2487 case OP_ATR_POS:
2488 case OP_ATR_VAL:
14f9c5c9 2489 *pos += 1;
4c4b4cd2
PH
2490 nargs = 2;
2491 break;
2492
2493 case OP_ATR_MIN:
2494 case OP_ATR_MAX:
2495 *pos += 1;
2496 nargs = 3;
14f9c5c9
AS
2497 break;
2498
2499 case BINOP_ASSIGN:
2500 {
4c4b4cd2
PH
2501 struct value *arg1;
2502
2503 *pos += 1;
2504 arg1 = resolve_subexp (expp, pos, 0, NULL);
2505 if (arg1 == NULL)
2506 resolve_subexp (expp, pos, 1, NULL);
2507 else
df407dfe 2508 resolve_subexp (expp, pos, 1, value_type (arg1));
4c4b4cd2 2509 break;
14f9c5c9
AS
2510 }
2511
4c4b4cd2
PH
2512 case UNOP_CAST:
2513 case UNOP_IN_RANGE:
2514 *pos += 3;
2515 nargs = 1;
2516 break;
14f9c5c9 2517
4c4b4cd2
PH
2518 case BINOP_ADD:
2519 case BINOP_SUB:
2520 case BINOP_MUL:
2521 case BINOP_DIV:
2522 case BINOP_REM:
2523 case BINOP_MOD:
2524 case BINOP_EXP:
2525 case BINOP_CONCAT:
2526 case BINOP_LOGICAL_AND:
2527 case BINOP_LOGICAL_OR:
2528 case BINOP_BITWISE_AND:
2529 case BINOP_BITWISE_IOR:
2530 case BINOP_BITWISE_XOR:
14f9c5c9 2531
4c4b4cd2
PH
2532 case BINOP_EQUAL:
2533 case BINOP_NOTEQUAL:
2534 case BINOP_LESS:
2535 case BINOP_GTR:
2536 case BINOP_LEQ:
2537 case BINOP_GEQ:
14f9c5c9 2538
4c4b4cd2
PH
2539 case BINOP_REPEAT:
2540 case BINOP_SUBSCRIPT:
2541 case BINOP_COMMA:
2542 *pos += 1;
2543 nargs = 2;
2544 break;
14f9c5c9 2545
4c4b4cd2
PH
2546 case UNOP_NEG:
2547 case UNOP_PLUS:
2548 case UNOP_LOGICAL_NOT:
2549 case UNOP_ABS:
2550 case UNOP_IND:
2551 *pos += 1;
2552 nargs = 1;
2553 break;
14f9c5c9 2554
4c4b4cd2
PH
2555 case OP_LONG:
2556 case OP_DOUBLE:
2557 case OP_VAR_VALUE:
2558 *pos += 4;
2559 break;
14f9c5c9 2560
4c4b4cd2
PH
2561 case OP_TYPE:
2562 case OP_BOOL:
2563 case OP_LAST:
2564 case OP_REGISTER:
2565 case OP_INTERNALVAR:
2566 *pos += 3;
2567 break;
14f9c5c9 2568
4c4b4cd2
PH
2569 case UNOP_MEMVAL:
2570 *pos += 3;
2571 nargs = 1;
2572 break;
2573
2574 case STRUCTOP_STRUCT:
2575 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2576 nargs = 1;
2577 break;
2578
2579 case OP_STRING:
19c1ef65
PH
2580 (*pos) += 3
2581 + BYTES_TO_EXP_ELEM (longest_to_int (exp->elts[pc + 1].longconst)
2582 + 1);
4c4b4cd2
PH
2583 break;
2584
2585 case TERNOP_SLICE:
2586 case TERNOP_IN_RANGE:
2587 *pos += 1;
2588 nargs = 3;
2589 break;
2590
2591 case BINOP_IN_BOUNDS:
2592 *pos += 3;
2593 nargs = 2;
14f9c5c9 2594 break;
4c4b4cd2
PH
2595
2596 default:
323e0a4a 2597 error (_("Unexpected operator during name resolution"));
14f9c5c9
AS
2598 }
2599
76a01679 2600 argvec = (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
4c4b4cd2
PH
2601 for (i = 0; i < nargs; i += 1)
2602 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
2603 argvec[i] = NULL;
2604 exp = *expp;
2605
2606 /* Pass two: perform any resolution on principal operator. */
14f9c5c9
AS
2607 switch (op)
2608 {
2609 default:
2610 break;
2611
14f9c5c9 2612 case OP_VAR_VALUE:
4c4b4cd2 2613 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
2614 {
2615 struct ada_symbol_info *candidates;
2616 int n_candidates;
2617
2618 n_candidates =
2619 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2620 (exp->elts[pc + 2].symbol),
2621 exp->elts[pc + 1].block, VAR_DOMAIN,
2622 &candidates);
2623
2624 if (n_candidates > 1)
2625 {
2626 /* Types tend to get re-introduced locally, so if there
2627 are any local symbols that are not types, first filter
2628 out all types. */
2629 int j;
2630 for (j = 0; j < n_candidates; j += 1)
2631 switch (SYMBOL_CLASS (candidates[j].sym))
2632 {
2633 case LOC_REGISTER:
2634 case LOC_ARG:
2635 case LOC_REF_ARG:
2636 case LOC_REGPARM:
2637 case LOC_REGPARM_ADDR:
2638 case LOC_LOCAL:
2639 case LOC_LOCAL_ARG:
2640 case LOC_BASEREG:
2641 case LOC_BASEREG_ARG:
2642 case LOC_COMPUTED:
2643 case LOC_COMPUTED_ARG:
2644 goto FoundNonType;
2645 default:
2646 break;
2647 }
2648 FoundNonType:
2649 if (j < n_candidates)
2650 {
2651 j = 0;
2652 while (j < n_candidates)
2653 {
2654 if (SYMBOL_CLASS (candidates[j].sym) == LOC_TYPEDEF)
2655 {
2656 candidates[j] = candidates[n_candidates - 1];
2657 n_candidates -= 1;
2658 }
2659 else
2660 j += 1;
2661 }
2662 }
2663 }
2664
2665 if (n_candidates == 0)
323e0a4a 2666 error (_("No definition found for %s"),
76a01679
JB
2667 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2668 else if (n_candidates == 1)
2669 i = 0;
2670 else if (deprocedure_p
2671 && !is_nonfunction (candidates, n_candidates))
2672 {
06d5cf63
JB
2673 i = ada_resolve_function
2674 (candidates, n_candidates, NULL, 0,
2675 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
2676 context_type);
76a01679 2677 if (i < 0)
323e0a4a 2678 error (_("Could not find a match for %s"),
76a01679
JB
2679 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2680 }
2681 else
2682 {
323e0a4a 2683 printf_filtered (_("Multiple matches for %s\n"),
76a01679
JB
2684 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2685 user_select_syms (candidates, n_candidates, 1);
2686 i = 0;
2687 }
2688
2689 exp->elts[pc + 1].block = candidates[i].block;
2690 exp->elts[pc + 2].symbol = candidates[i].sym;
1265e4aa
JB
2691 if (innermost_block == NULL
2692 || contained_in (candidates[i].block, innermost_block))
76a01679
JB
2693 innermost_block = candidates[i].block;
2694 }
2695
2696 if (deprocedure_p
2697 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
2698 == TYPE_CODE_FUNC))
2699 {
2700 replace_operator_with_call (expp, pc, 0, 0,
2701 exp->elts[pc + 2].symbol,
2702 exp->elts[pc + 1].block);
2703 exp = *expp;
2704 }
14f9c5c9
AS
2705 break;
2706
2707 case OP_FUNCALL:
2708 {
4c4b4cd2 2709 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679 2710 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
4c4b4cd2
PH
2711 {
2712 struct ada_symbol_info *candidates;
2713 int n_candidates;
2714
2715 n_candidates =
76a01679
JB
2716 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2717 (exp->elts[pc + 5].symbol),
2718 exp->elts[pc + 4].block, VAR_DOMAIN,
2719 &candidates);
4c4b4cd2
PH
2720 if (n_candidates == 1)
2721 i = 0;
2722 else
2723 {
06d5cf63
JB
2724 i = ada_resolve_function
2725 (candidates, n_candidates,
2726 argvec, nargs,
2727 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
2728 context_type);
4c4b4cd2 2729 if (i < 0)
323e0a4a 2730 error (_("Could not find a match for %s"),
4c4b4cd2
PH
2731 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
2732 }
2733
2734 exp->elts[pc + 4].block = candidates[i].block;
2735 exp->elts[pc + 5].symbol = candidates[i].sym;
1265e4aa
JB
2736 if (innermost_block == NULL
2737 || contained_in (candidates[i].block, innermost_block))
4c4b4cd2
PH
2738 innermost_block = candidates[i].block;
2739 }
14f9c5c9
AS
2740 }
2741 break;
2742 case BINOP_ADD:
2743 case BINOP_SUB:
2744 case BINOP_MUL:
2745 case BINOP_DIV:
2746 case BINOP_REM:
2747 case BINOP_MOD:
2748 case BINOP_CONCAT:
2749 case BINOP_BITWISE_AND:
2750 case BINOP_BITWISE_IOR:
2751 case BINOP_BITWISE_XOR:
2752 case BINOP_EQUAL:
2753 case BINOP_NOTEQUAL:
2754 case BINOP_LESS:
2755 case BINOP_GTR:
2756 case BINOP_LEQ:
2757 case BINOP_GEQ:
2758 case BINOP_EXP:
2759 case UNOP_NEG:
2760 case UNOP_PLUS:
2761 case UNOP_LOGICAL_NOT:
2762 case UNOP_ABS:
2763 if (possible_user_operator_p (op, argvec))
4c4b4cd2
PH
2764 {
2765 struct ada_symbol_info *candidates;
2766 int n_candidates;
2767
2768 n_candidates =
2769 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
2770 (struct block *) NULL, VAR_DOMAIN,
2771 &candidates);
2772 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
76a01679 2773 ada_decoded_op_name (op), NULL);
4c4b4cd2
PH
2774 if (i < 0)
2775 break;
2776
76a01679
JB
2777 replace_operator_with_call (expp, pc, nargs, 1,
2778 candidates[i].sym, candidates[i].block);
4c4b4cd2
PH
2779 exp = *expp;
2780 }
14f9c5c9 2781 break;
4c4b4cd2
PH
2782
2783 case OP_TYPE:
2784 return NULL;
14f9c5c9
AS
2785 }
2786
2787 *pos = pc;
2788 return evaluate_subexp_type (exp, pos);
2789}
2790
2791/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
4c4b4cd2
PH
2792 MAY_DEREF is non-zero, the formal may be a pointer and the actual
2793 a non-pointer. A type of 'void' (which is never a valid expression type)
2794 by convention matches anything. */
14f9c5c9 2795/* The term "match" here is rather loose. The match is heuristic and
4c4b4cd2 2796 liberal. FIXME: TOO liberal, in fact. */
14f9c5c9
AS
2797
2798static int
4dc81987 2799ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9 2800{
61ee279c
PH
2801 ftype = ada_check_typedef (ftype);
2802 atype = ada_check_typedef (atype);
14f9c5c9
AS
2803
2804 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
2805 ftype = TYPE_TARGET_TYPE (ftype);
2806 if (TYPE_CODE (atype) == TYPE_CODE_REF)
2807 atype = TYPE_TARGET_TYPE (atype);
2808
d2e4a39e 2809 if (TYPE_CODE (ftype) == TYPE_CODE_VOID
14f9c5c9
AS
2810 || TYPE_CODE (atype) == TYPE_CODE_VOID)
2811 return 1;
2812
d2e4a39e 2813 switch (TYPE_CODE (ftype))
14f9c5c9
AS
2814 {
2815 default:
2816 return 1;
2817 case TYPE_CODE_PTR:
2818 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
4c4b4cd2
PH
2819 return ada_type_match (TYPE_TARGET_TYPE (ftype),
2820 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e 2821 else
1265e4aa
JB
2822 return (may_deref
2823 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
14f9c5c9
AS
2824 case TYPE_CODE_INT:
2825 case TYPE_CODE_ENUM:
2826 case TYPE_CODE_RANGE:
2827 switch (TYPE_CODE (atype))
4c4b4cd2
PH
2828 {
2829 case TYPE_CODE_INT:
2830 case TYPE_CODE_ENUM:
2831 case TYPE_CODE_RANGE:
2832 return 1;
2833 default:
2834 return 0;
2835 }
14f9c5c9
AS
2836
2837 case TYPE_CODE_ARRAY:
d2e4a39e 2838 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
4c4b4cd2 2839 || ada_is_array_descriptor_type (atype));
14f9c5c9
AS
2840
2841 case TYPE_CODE_STRUCT:
4c4b4cd2
PH
2842 if (ada_is_array_descriptor_type (ftype))
2843 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
2844 || ada_is_array_descriptor_type (atype));
14f9c5c9 2845 else
4c4b4cd2
PH
2846 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
2847 && !ada_is_array_descriptor_type (atype));
14f9c5c9
AS
2848
2849 case TYPE_CODE_UNION:
2850 case TYPE_CODE_FLT:
2851 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
2852 }
2853}
2854
2855/* Return non-zero if the formals of FUNC "sufficiently match" the
2856 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
2857 may also be an enumeral, in which case it is treated as a 0-
4c4b4cd2 2858 argument function. */
14f9c5c9
AS
2859
2860static int
d2e4a39e 2861ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
14f9c5c9
AS
2862{
2863 int i;
d2e4a39e 2864 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 2865
1265e4aa
JB
2866 if (SYMBOL_CLASS (func) == LOC_CONST
2867 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
14f9c5c9
AS
2868 return (n_actuals == 0);
2869 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
2870 return 0;
2871
2872 if (TYPE_NFIELDS (func_type) != n_actuals)
2873 return 0;
2874
2875 for (i = 0; i < n_actuals; i += 1)
2876 {
4c4b4cd2 2877 if (actuals[i] == NULL)
76a01679
JB
2878 return 0;
2879 else
2880 {
61ee279c 2881 struct type *ftype = ada_check_typedef (TYPE_FIELD_TYPE (func_type, i));
df407dfe 2882 struct type *atype = ada_check_typedef (value_type (actuals[i]));
4c4b4cd2 2883
76a01679
JB
2884 if (!ada_type_match (ftype, atype, 1))
2885 return 0;
2886 }
14f9c5c9
AS
2887 }
2888 return 1;
2889}
2890
2891/* False iff function type FUNC_TYPE definitely does not produce a value
2892 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
2893 FUNC_TYPE is not a valid function type with a non-null return type
2894 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
2895
2896static int
d2e4a39e 2897return_match (struct type *func_type, struct type *context_type)
14f9c5c9 2898{
d2e4a39e 2899 struct type *return_type;
14f9c5c9
AS
2900
2901 if (func_type == NULL)
2902 return 1;
2903
4c4b4cd2
PH
2904 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
2905 return_type = base_type (TYPE_TARGET_TYPE (func_type));
2906 else
2907 return_type = base_type (func_type);
14f9c5c9
AS
2908 if (return_type == NULL)
2909 return 1;
2910
4c4b4cd2 2911 context_type = base_type (context_type);
14f9c5c9
AS
2912
2913 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
2914 return context_type == NULL || return_type == context_type;
2915 else if (context_type == NULL)
2916 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
2917 else
2918 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
2919}
2920
2921
4c4b4cd2 2922/* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
14f9c5c9 2923 function (if any) that matches the types of the NARGS arguments in
4c4b4cd2
PH
2924 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
2925 that returns that type, then eliminate matches that don't. If
2926 CONTEXT_TYPE is void and there is at least one match that does not
2927 return void, eliminate all matches that do.
2928
14f9c5c9
AS
2929 Asks the user if there is more than one match remaining. Returns -1
2930 if there is no such symbol or none is selected. NAME is used
4c4b4cd2
PH
2931 solely for messages. May re-arrange and modify SYMS in
2932 the process; the index returned is for the modified vector. */
14f9c5c9 2933
4c4b4cd2
PH
2934static int
2935ada_resolve_function (struct ada_symbol_info syms[],
2936 int nsyms, struct value **args, int nargs,
2937 const char *name, struct type *context_type)
14f9c5c9
AS
2938{
2939 int k;
4c4b4cd2 2940 int m; /* Number of hits */
d2e4a39e
AS
2941 struct type *fallback;
2942 struct type *return_type;
14f9c5c9
AS
2943
2944 return_type = context_type;
2945 if (context_type == NULL)
2946 fallback = builtin_type_void;
2947 else
2948 fallback = NULL;
2949
d2e4a39e 2950 m = 0;
14f9c5c9
AS
2951 while (1)
2952 {
2953 for (k = 0; k < nsyms; k += 1)
4c4b4cd2 2954 {
61ee279c 2955 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].sym));
4c4b4cd2
PH
2956
2957 if (ada_args_match (syms[k].sym, args, nargs)
2958 && return_match (type, return_type))
2959 {
2960 syms[m] = syms[k];
2961 m += 1;
2962 }
2963 }
14f9c5c9 2964 if (m > 0 || return_type == fallback)
4c4b4cd2 2965 break;
14f9c5c9 2966 else
4c4b4cd2 2967 return_type = fallback;
14f9c5c9
AS
2968 }
2969
2970 if (m == 0)
2971 return -1;
2972 else if (m > 1)
2973 {
323e0a4a 2974 printf_filtered (_("Multiple matches for %s\n"), name);
4c4b4cd2 2975 user_select_syms (syms, m, 1);
14f9c5c9
AS
2976 return 0;
2977 }
2978 return 0;
2979}
2980
4c4b4cd2
PH
2981/* Returns true (non-zero) iff decoded name N0 should appear before N1
2982 in a listing of choices during disambiguation (see sort_choices, below).
2983 The idea is that overloadings of a subprogram name from the
2984 same package should sort in their source order. We settle for ordering
2985 such symbols by their trailing number (__N or $N). */
2986
14f9c5c9 2987static int
4c4b4cd2 2988encoded_ordered_before (char *N0, char *N1)
14f9c5c9
AS
2989{
2990 if (N1 == NULL)
2991 return 0;
2992 else if (N0 == NULL)
2993 return 1;
2994 else
2995 {
2996 int k0, k1;
d2e4a39e 2997 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
4c4b4cd2 2998 ;
d2e4a39e 2999 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
4c4b4cd2 3000 ;
d2e4a39e 3001 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
4c4b4cd2
PH
3002 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3003 {
3004 int n0, n1;
3005 n0 = k0;
3006 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3007 n0 -= 1;
3008 n1 = k1;
3009 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3010 n1 -= 1;
3011 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3012 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3013 }
14f9c5c9
AS
3014 return (strcmp (N0, N1) < 0);
3015 }
3016}
d2e4a39e 3017
4c4b4cd2
PH
3018/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3019 encoded names. */
3020
d2e4a39e 3021static void
4c4b4cd2 3022sort_choices (struct ada_symbol_info syms[], int nsyms)
14f9c5c9 3023{
4c4b4cd2 3024 int i;
d2e4a39e 3025 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3026 {
4c4b4cd2 3027 struct ada_symbol_info sym = syms[i];
14f9c5c9
AS
3028 int j;
3029
d2e4a39e 3030 for (j = i - 1; j >= 0; j -= 1)
4c4b4cd2
PH
3031 {
3032 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].sym),
3033 SYMBOL_LINKAGE_NAME (sym.sym)))
3034 break;
3035 syms[j + 1] = syms[j];
3036 }
d2e4a39e 3037 syms[j + 1] = sym;
14f9c5c9
AS
3038 }
3039}
3040
4c4b4cd2
PH
3041/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3042 by asking the user (if necessary), returning the number selected,
3043 and setting the first elements of SYMS items. Error if no symbols
3044 selected. */
14f9c5c9
AS
3045
3046/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
4c4b4cd2 3047 to be re-integrated one of these days. */
14f9c5c9
AS
3048
3049int
4c4b4cd2 3050user_select_syms (struct ada_symbol_info *syms, int nsyms, int max_results)
14f9c5c9
AS
3051{
3052 int i;
d2e4a39e 3053 int *chosen = (int *) alloca (sizeof (int) * nsyms);
14f9c5c9
AS
3054 int n_chosen;
3055 int first_choice = (max_results == 1) ? 1 : 2;
3056
3057 if (max_results < 1)
323e0a4a 3058 error (_("Request to select 0 symbols!"));
14f9c5c9
AS
3059 if (nsyms <= 1)
3060 return nsyms;
3061
323e0a4a 3062 printf_unfiltered (_("[0] cancel\n"));
14f9c5c9 3063 if (max_results > 1)
323e0a4a 3064 printf_unfiltered (_("[1] all\n"));
14f9c5c9 3065
4c4b4cd2 3066 sort_choices (syms, nsyms);
14f9c5c9
AS
3067
3068 for (i = 0; i < nsyms; i += 1)
3069 {
4c4b4cd2
PH
3070 if (syms[i].sym == NULL)
3071 continue;
3072
3073 if (SYMBOL_CLASS (syms[i].sym) == LOC_BLOCK)
3074 {
76a01679
JB
3075 struct symtab_and_line sal =
3076 find_function_start_sal (syms[i].sym, 1);
323e0a4a
AC
3077 if (sal.symtab == NULL)
3078 printf_unfiltered (_("[%d] %s at <no source file available>:%d\n"),
3079 i + first_choice,
3080 SYMBOL_PRINT_NAME (syms[i].sym),
3081 sal.line);
3082 else
3083 printf_unfiltered (_("[%d] %s at %s:%d\n"), i + first_choice,
3084 SYMBOL_PRINT_NAME (syms[i].sym),
3085 sal.symtab->filename, sal.line);
4c4b4cd2
PH
3086 continue;
3087 }
d2e4a39e 3088 else
4c4b4cd2
PH
3089 {
3090 int is_enumeral =
3091 (SYMBOL_CLASS (syms[i].sym) == LOC_CONST
3092 && SYMBOL_TYPE (syms[i].sym) != NULL
3093 && TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) == TYPE_CODE_ENUM);
3094 struct symtab *symtab = symtab_for_sym (syms[i].sym);
3095
3096 if (SYMBOL_LINE (syms[i].sym) != 0 && symtab != NULL)
323e0a4a 3097 printf_unfiltered (_("[%d] %s at %s:%d\n"),
4c4b4cd2
PH
3098 i + first_choice,
3099 SYMBOL_PRINT_NAME (syms[i].sym),
3100 symtab->filename, SYMBOL_LINE (syms[i].sym));
76a01679
JB
3101 else if (is_enumeral
3102 && TYPE_NAME (SYMBOL_TYPE (syms[i].sym)) != NULL)
4c4b4cd2 3103 {
a3f17187 3104 printf_unfiltered (("[%d] "), i + first_choice);
76a01679
JB
3105 ada_print_type (SYMBOL_TYPE (syms[i].sym), NULL,
3106 gdb_stdout, -1, 0);
323e0a4a 3107 printf_unfiltered (_("'(%s) (enumeral)\n"),
4c4b4cd2
PH
3108 SYMBOL_PRINT_NAME (syms[i].sym));
3109 }
3110 else if (symtab != NULL)
3111 printf_unfiltered (is_enumeral
323e0a4a
AC
3112 ? _("[%d] %s in %s (enumeral)\n")
3113 : _("[%d] %s at %s:?\n"),
4c4b4cd2
PH
3114 i + first_choice,
3115 SYMBOL_PRINT_NAME (syms[i].sym),
3116 symtab->filename);
3117 else
3118 printf_unfiltered (is_enumeral
323e0a4a
AC
3119 ? _("[%d] %s (enumeral)\n")
3120 : _("[%d] %s at ?\n"),
4c4b4cd2
PH
3121 i + first_choice,
3122 SYMBOL_PRINT_NAME (syms[i].sym));
3123 }
14f9c5c9 3124 }
d2e4a39e 3125
14f9c5c9 3126 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
4c4b4cd2 3127 "overload-choice");
14f9c5c9
AS
3128
3129 for (i = 0; i < n_chosen; i += 1)
4c4b4cd2 3130 syms[i] = syms[chosen[i]];
14f9c5c9
AS
3131
3132 return n_chosen;
3133}
3134
3135/* Read and validate a set of numeric choices from the user in the
4c4b4cd2 3136 range 0 .. N_CHOICES-1. Place the results in increasing
14f9c5c9
AS
3137 order in CHOICES[0 .. N-1], and return N.
3138
3139 The user types choices as a sequence of numbers on one line
3140 separated by blanks, encoding them as follows:
3141
4c4b4cd2 3142 + A choice of 0 means to cancel the selection, throwing an error.
14f9c5c9
AS
3143 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3144 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3145
4c4b4cd2 3146 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9
AS
3147
3148 ANNOTATION_SUFFIX, if present, is used to annotate the input
4c4b4cd2 3149 prompts (for use with the -f switch). */
14f9c5c9
AS
3150
3151int
d2e4a39e 3152get_selections (int *choices, int n_choices, int max_results,
4c4b4cd2 3153 int is_all_choice, char *annotation_suffix)
14f9c5c9 3154{
d2e4a39e
AS
3155 char *args;
3156 const char *prompt;
14f9c5c9
AS
3157 int n_chosen;
3158 int first_choice = is_all_choice ? 2 : 1;
d2e4a39e 3159
14f9c5c9
AS
3160 prompt = getenv ("PS2");
3161 if (prompt == NULL)
3162 prompt = ">";
3163
a3f17187 3164 printf_unfiltered (("%s "), prompt);
14f9c5c9
AS
3165 gdb_flush (gdb_stdout);
3166
3167 args = command_line_input ((char *) NULL, 0, annotation_suffix);
d2e4a39e 3168
14f9c5c9 3169 if (args == NULL)
323e0a4a 3170 error_no_arg (_("one or more choice numbers"));
14f9c5c9
AS
3171
3172 n_chosen = 0;
76a01679 3173
4c4b4cd2
PH
3174 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3175 order, as given in args. Choices are validated. */
14f9c5c9
AS
3176 while (1)
3177 {
d2e4a39e 3178 char *args2;
14f9c5c9
AS
3179 int choice, j;
3180
3181 while (isspace (*args))
4c4b4cd2 3182 args += 1;
14f9c5c9 3183 if (*args == '\0' && n_chosen == 0)
323e0a4a 3184 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3185 else if (*args == '\0')
4c4b4cd2 3186 break;
14f9c5c9
AS
3187
3188 choice = strtol (args, &args2, 10);
d2e4a39e 3189 if (args == args2 || choice < 0
4c4b4cd2 3190 || choice > n_choices + first_choice - 1)
323e0a4a 3191 error (_("Argument must be choice number"));
14f9c5c9
AS
3192 args = args2;
3193
d2e4a39e 3194 if (choice == 0)
323e0a4a 3195 error (_("cancelled"));
14f9c5c9
AS
3196
3197 if (choice < first_choice)
4c4b4cd2
PH
3198 {
3199 n_chosen = n_choices;
3200 for (j = 0; j < n_choices; j += 1)
3201 choices[j] = j;
3202 break;
3203 }
14f9c5c9
AS
3204 choice -= first_choice;
3205
d2e4a39e 3206 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
4c4b4cd2
PH
3207 {
3208 }
14f9c5c9
AS
3209
3210 if (j < 0 || choice != choices[j])
4c4b4cd2
PH
3211 {
3212 int k;
3213 for (k = n_chosen - 1; k > j; k -= 1)
3214 choices[k + 1] = choices[k];
3215 choices[j + 1] = choice;
3216 n_chosen += 1;
3217 }
14f9c5c9
AS
3218 }
3219
3220 if (n_chosen > max_results)
323e0a4a 3221 error (_("Select no more than %d of the above"), max_results);
d2e4a39e 3222
14f9c5c9
AS
3223 return n_chosen;
3224}
3225
4c4b4cd2
PH
3226/* Replace the operator of length OPLEN at position PC in *EXPP with a call
3227 on the function identified by SYM and BLOCK, and taking NARGS
3228 arguments. Update *EXPP as needed to hold more space. */
14f9c5c9
AS
3229
3230static void
d2e4a39e 3231replace_operator_with_call (struct expression **expp, int pc, int nargs,
4c4b4cd2
PH
3232 int oplen, struct symbol *sym,
3233 struct block *block)
14f9c5c9
AS
3234{
3235 /* A new expression, with 6 more elements (3 for funcall, 4 for function
4c4b4cd2 3236 symbol, -oplen for operator being replaced). */
d2e4a39e 3237 struct expression *newexp = (struct expression *)
14f9c5c9 3238 xmalloc (sizeof (struct expression)
4c4b4cd2 3239 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
d2e4a39e 3240 struct expression *exp = *expp;
14f9c5c9
AS
3241
3242 newexp->nelts = exp->nelts + 7 - oplen;
3243 newexp->language_defn = exp->language_defn;
3244 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
d2e4a39e 3245 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
4c4b4cd2 3246 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
14f9c5c9
AS
3247
3248 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
3249 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
3250
3251 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
3252 newexp->elts[pc + 4].block = block;
3253 newexp->elts[pc + 5].symbol = sym;
3254
3255 *expp = newexp;
aacb1f0a 3256 xfree (exp);
d2e4a39e 3257}
14f9c5c9
AS
3258
3259/* Type-class predicates */
3260
4c4b4cd2
PH
3261/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3262 or FLOAT). */
14f9c5c9
AS
3263
3264static int
d2e4a39e 3265numeric_type_p (struct type *type)
14f9c5c9
AS
3266{
3267 if (type == NULL)
3268 return 0;
d2e4a39e
AS
3269 else
3270 {
3271 switch (TYPE_CODE (type))
4c4b4cd2
PH
3272 {
3273 case TYPE_CODE_INT:
3274 case TYPE_CODE_FLT:
3275 return 1;
3276 case TYPE_CODE_RANGE:
3277 return (type == TYPE_TARGET_TYPE (type)
3278 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3279 default:
3280 return 0;
3281 }
d2e4a39e 3282 }
14f9c5c9
AS
3283}
3284
4c4b4cd2 3285/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
3286
3287static int
d2e4a39e 3288integer_type_p (struct type *type)
14f9c5c9
AS
3289{
3290 if (type == NULL)
3291 return 0;
d2e4a39e
AS
3292 else
3293 {
3294 switch (TYPE_CODE (type))
4c4b4cd2
PH
3295 {
3296 case TYPE_CODE_INT:
3297 return 1;
3298 case TYPE_CODE_RANGE:
3299 return (type == TYPE_TARGET_TYPE (type)
3300 || integer_type_p (TYPE_TARGET_TYPE (type)));
3301 default:
3302 return 0;
3303 }
d2e4a39e 3304 }
14f9c5c9
AS
3305}
3306
4c4b4cd2 3307/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
3308
3309static int
d2e4a39e 3310scalar_type_p (struct type *type)
14f9c5c9
AS
3311{
3312 if (type == NULL)
3313 return 0;
d2e4a39e
AS
3314 else
3315 {
3316 switch (TYPE_CODE (type))
4c4b4cd2
PH
3317 {
3318 case TYPE_CODE_INT:
3319 case TYPE_CODE_RANGE:
3320 case TYPE_CODE_ENUM:
3321 case TYPE_CODE_FLT:
3322 return 1;
3323 default:
3324 return 0;
3325 }
d2e4a39e 3326 }
14f9c5c9
AS
3327}
3328
4c4b4cd2 3329/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
3330
3331static int
d2e4a39e 3332discrete_type_p (struct type *type)
14f9c5c9
AS
3333{
3334 if (type == NULL)
3335 return 0;
d2e4a39e
AS
3336 else
3337 {
3338 switch (TYPE_CODE (type))
4c4b4cd2
PH
3339 {
3340 case TYPE_CODE_INT:
3341 case TYPE_CODE_RANGE:
3342 case TYPE_CODE_ENUM:
3343 return 1;
3344 default:
3345 return 0;
3346 }
d2e4a39e 3347 }
14f9c5c9
AS
3348}
3349
4c4b4cd2
PH
3350/* Returns non-zero if OP with operands in the vector ARGS could be
3351 a user-defined function. Errs on the side of pre-defined operators
3352 (i.e., result 0). */
14f9c5c9
AS
3353
3354static int
d2e4a39e 3355possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 3356{
76a01679 3357 struct type *type0 =
df407dfe 3358 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 3359 struct type *type1 =
df407dfe 3360 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 3361
4c4b4cd2
PH
3362 if (type0 == NULL)
3363 return 0;
3364
14f9c5c9
AS
3365 switch (op)
3366 {
3367 default:
3368 return 0;
3369
3370 case BINOP_ADD:
3371 case BINOP_SUB:
3372 case BINOP_MUL:
3373 case BINOP_DIV:
d2e4a39e 3374 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
3375
3376 case BINOP_REM:
3377 case BINOP_MOD:
3378 case BINOP_BITWISE_AND:
3379 case BINOP_BITWISE_IOR:
3380 case BINOP_BITWISE_XOR:
d2e4a39e 3381 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3382
3383 case BINOP_EQUAL:
3384 case BINOP_NOTEQUAL:
3385 case BINOP_LESS:
3386 case BINOP_GTR:
3387 case BINOP_LEQ:
3388 case BINOP_GEQ:
d2e4a39e 3389 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
3390
3391 case BINOP_CONCAT:
1265e4aa
JB
3392 return
3393 ((TYPE_CODE (type0) != TYPE_CODE_ARRAY
3394 && (TYPE_CODE (type0) != TYPE_CODE_PTR
3395 || TYPE_CODE (TYPE_TARGET_TYPE (type0)) != TYPE_CODE_ARRAY))
3396 || (TYPE_CODE (type1) != TYPE_CODE_ARRAY
3397 && (TYPE_CODE (type1) != TYPE_CODE_PTR
c3e5cd34
PH
3398 || (TYPE_CODE (TYPE_TARGET_TYPE (type1))
3399 != TYPE_CODE_ARRAY))));
14f9c5c9
AS
3400
3401 case BINOP_EXP:
d2e4a39e 3402 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3403
3404 case UNOP_NEG:
3405 case UNOP_PLUS:
3406 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
3407 case UNOP_ABS:
3408 return (!numeric_type_p (type0));
14f9c5c9
AS
3409
3410 }
3411}
3412\f
4c4b4cd2 3413 /* Renaming */
14f9c5c9 3414
4c4b4cd2
PH
3415/* NOTE: In the following, we assume that a renaming type's name may
3416 have an ___XD suffix. It would be nice if this went away at some
3417 point. */
14f9c5c9
AS
3418
3419/* If TYPE encodes a renaming, returns the renaming suffix, which
4c4b4cd2
PH
3420 is XR for an object renaming, XRP for a procedure renaming, XRE for
3421 an exception renaming, and XRS for a subprogram renaming. Returns
3422 NULL if NAME encodes none of these. */
3423
d2e4a39e
AS
3424const char *
3425ada_renaming_type (struct type *type)
14f9c5c9
AS
3426{
3427 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_ENUM)
3428 {
d2e4a39e
AS
3429 const char *name = type_name_no_tag (type);
3430 const char *suffix = (name == NULL) ? NULL : strstr (name, "___XR");
3431 if (suffix == NULL
4c4b4cd2
PH
3432 || (suffix[5] != '\000' && strchr ("PES_", suffix[5]) == NULL))
3433 return NULL;
14f9c5c9 3434 else
4c4b4cd2 3435 return suffix + 3;
14f9c5c9
AS
3436 }
3437 else
3438 return NULL;
3439}
3440
4c4b4cd2
PH
3441/* Return non-zero iff SYM encodes an object renaming. */
3442
14f9c5c9 3443int
d2e4a39e 3444ada_is_object_renaming (struct symbol *sym)
14f9c5c9 3445{
d2e4a39e
AS
3446 const char *renaming_type = ada_renaming_type (SYMBOL_TYPE (sym));
3447 return renaming_type != NULL
14f9c5c9
AS
3448 && (renaming_type[2] == '\0' || renaming_type[2] == '_');
3449}
3450
3451/* Assuming that SYM encodes a non-object renaming, returns the original
4c4b4cd2
PH
3452 name of the renamed entity. The name is good until the end of
3453 parsing. */
3454
3455char *
d2e4a39e 3456ada_simple_renamed_entity (struct symbol *sym)
14f9c5c9 3457{
d2e4a39e
AS
3458 struct type *type;
3459 const char *raw_name;
14f9c5c9 3460 int len;
d2e4a39e 3461 char *result;
14f9c5c9
AS
3462
3463 type = SYMBOL_TYPE (sym);
3464 if (type == NULL || TYPE_NFIELDS (type) < 1)
323e0a4a 3465 error (_("Improperly encoded renaming."));
14f9c5c9
AS
3466
3467 raw_name = TYPE_FIELD_NAME (type, 0);
3468 len = (raw_name == NULL ? 0 : strlen (raw_name)) - 5;
3469 if (len <= 0)
323e0a4a 3470 error (_("Improperly encoded renaming."));
14f9c5c9
AS
3471
3472 result = xmalloc (len + 1);
14f9c5c9
AS
3473 strncpy (result, raw_name, len);
3474 result[len] = '\000';
3475 return result;
3476}
14f9c5c9 3477\f
d2e4a39e 3478
4c4b4cd2 3479 /* Evaluation: Function Calls */
14f9c5c9 3480
4c4b4cd2
PH
3481/* Return an lvalue containing the value VAL. This is the identity on
3482 lvalues, and otherwise has the side-effect of pushing a copy of VAL
3483 on the stack, using and updating *SP as the stack pointer, and
3484 returning an lvalue whose VALUE_ADDRESS points to the copy. */
14f9c5c9 3485
d2e4a39e 3486static struct value *
4c4b4cd2 3487ensure_lval (struct value *val, CORE_ADDR *sp)
14f9c5c9 3488{
c3e5cd34
PH
3489 if (! VALUE_LVAL (val))
3490 {
df407dfe 3491 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
c3e5cd34
PH
3492
3493 /* The following is taken from the structure-return code in
3494 call_function_by_hand. FIXME: Therefore, some refactoring seems
3495 indicated. */
3496 if (INNER_THAN (1, 2))
3497 {
3498 /* Stack grows downward. Align SP and VALUE_ADDRESS (val) after
3499 reserving sufficient space. */
3500 *sp -= len;
3501 if (gdbarch_frame_align_p (current_gdbarch))
3502 *sp = gdbarch_frame_align (current_gdbarch, *sp);
3503 VALUE_ADDRESS (val) = *sp;
3504 }
3505 else
3506 {
3507 /* Stack grows upward. Align the frame, allocate space, and
3508 then again, re-align the frame. */
3509 if (gdbarch_frame_align_p (current_gdbarch))
3510 *sp = gdbarch_frame_align (current_gdbarch, *sp);
3511 VALUE_ADDRESS (val) = *sp;
3512 *sp += len;
3513 if (gdbarch_frame_align_p (current_gdbarch))
3514 *sp = gdbarch_frame_align (current_gdbarch, *sp);
3515 }
14f9c5c9 3516
990a07ab 3517 write_memory (VALUE_ADDRESS (val), value_contents_raw (val), len);
c3e5cd34 3518 }
14f9c5c9
AS
3519
3520 return val;
3521}
3522
3523/* Return the value ACTUAL, converted to be an appropriate value for a
3524 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3525 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 3526 values not residing in memory, updating it as needed. */
14f9c5c9 3527
d2e4a39e
AS
3528static struct value *
3529convert_actual (struct value *actual, struct type *formal_type0,
4c4b4cd2 3530 CORE_ADDR *sp)
14f9c5c9 3531{
df407dfe 3532 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 3533 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e
AS
3534 struct type *formal_target =
3535 TYPE_CODE (formal_type) == TYPE_CODE_PTR
61ee279c 3536 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e
AS
3537 struct type *actual_target =
3538 TYPE_CODE (actual_type) == TYPE_CODE_PTR
61ee279c 3539 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 3540
4c4b4cd2 3541 if (ada_is_array_descriptor_type (formal_target)
14f9c5c9
AS
3542 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
3543 return make_array_descriptor (formal_type, actual, sp);
3544 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR)
3545 {
3546 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4c4b4cd2
PH
3547 && ada_is_array_descriptor_type (actual_target))
3548 return desc_data (actual);
14f9c5c9 3549 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4c4b4cd2
PH
3550 {
3551 if (VALUE_LVAL (actual) != lval_memory)
3552 {
3553 struct value *val;
df407dfe 3554 actual_type = ada_check_typedef (value_type (actual));
4c4b4cd2 3555 val = allocate_value (actual_type);
990a07ab 3556 memcpy ((char *) value_contents_raw (val),
0fd88904 3557 (char *) value_contents (actual),
4c4b4cd2
PH
3558 TYPE_LENGTH (actual_type));
3559 actual = ensure_lval (val, sp);
3560 }
3561 return value_addr (actual);
3562 }
14f9c5c9
AS
3563 }
3564 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
3565 return ada_value_ind (actual);
3566
3567 return actual;
3568}
3569
3570
4c4b4cd2
PH
3571/* Push a descriptor of type TYPE for array value ARR on the stack at
3572 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 3573 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
3574 to-descriptor type rather than a descriptor type), a struct value *
3575 representing a pointer to this descriptor. */
14f9c5c9 3576
d2e4a39e
AS
3577static struct value *
3578make_array_descriptor (struct type *type, struct value *arr, CORE_ADDR *sp)
14f9c5c9 3579{
d2e4a39e
AS
3580 struct type *bounds_type = desc_bounds_type (type);
3581 struct type *desc_type = desc_base_type (type);
3582 struct value *descriptor = allocate_value (desc_type);
3583 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 3584 int i;
d2e4a39e 3585
df407dfe 3586 for (i = ada_array_arity (ada_check_typedef (value_type (arr))); i > 0; i -= 1)
14f9c5c9 3587 {
0fd88904 3588 modify_general_field (value_contents_writeable (bounds),
4c4b4cd2
PH
3589 value_as_long (ada_array_bound (arr, i, 0)),
3590 desc_bound_bitpos (bounds_type, i, 0),
3591 desc_bound_bitsize (bounds_type, i, 0));
0fd88904 3592 modify_general_field (value_contents_writeable (bounds),
4c4b4cd2
PH
3593 value_as_long (ada_array_bound (arr, i, 1)),
3594 desc_bound_bitpos (bounds_type, i, 1),
3595 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 3596 }
d2e4a39e 3597
4c4b4cd2 3598 bounds = ensure_lval (bounds, sp);
d2e4a39e 3599
0fd88904 3600 modify_general_field (value_contents_writeable (descriptor),
76a01679
JB
3601 VALUE_ADDRESS (ensure_lval (arr, sp)),
3602 fat_pntr_data_bitpos (desc_type),
3603 fat_pntr_data_bitsize (desc_type));
4c4b4cd2 3604
0fd88904 3605 modify_general_field (value_contents_writeable (descriptor),
4c4b4cd2
PH
3606 VALUE_ADDRESS (bounds),
3607 fat_pntr_bounds_bitpos (desc_type),
3608 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 3609
4c4b4cd2 3610 descriptor = ensure_lval (descriptor, sp);
14f9c5c9
AS
3611
3612 if (TYPE_CODE (type) == TYPE_CODE_PTR)
3613 return value_addr (descriptor);
3614 else
3615 return descriptor;
3616}
3617
3618
4c4b4cd2 3619/* Assuming a dummy frame has been established on the target, perform any
14f9c5c9 3620 conversions needed for calling function FUNC on the NARGS actual
4c4b4cd2 3621 parameters in ARGS, other than standard C conversions. Does
14f9c5c9 3622 nothing if FUNC does not have Ada-style prototype data, or if NARGS
4c4b4cd2 3623 does not match the number of arguments expected. Use *SP as a
14f9c5c9 3624 stack pointer for additional data that must be pushed, updating its
4c4b4cd2 3625 value as needed. */
14f9c5c9
AS
3626
3627void
d2e4a39e 3628ada_convert_actuals (struct value *func, int nargs, struct value *args[],
4c4b4cd2 3629 CORE_ADDR *sp)
14f9c5c9
AS
3630{
3631 int i;
3632
df407dfe
AC
3633 if (TYPE_NFIELDS (value_type (func)) == 0
3634 || nargs != TYPE_NFIELDS (value_type (func)))
14f9c5c9
AS
3635 return;
3636
3637 for (i = 0; i < nargs; i += 1)
d2e4a39e 3638 args[i] =
df407dfe 3639 convert_actual (args[i], TYPE_FIELD_TYPE (value_type (func), i), sp);
14f9c5c9 3640}
14f9c5c9 3641\f
963a6417
PH
3642/* Dummy definitions for an experimental caching module that is not
3643 * used in the public sources. */
96d887e8 3644
96d887e8
PH
3645static int
3646lookup_cached_symbol (const char *name, domain_enum namespace,
76a01679
JB
3647 struct symbol **sym, struct block **block,
3648 struct symtab **symtab)
96d887e8
PH
3649{
3650 return 0;
3651}
3652
3653static void
3654cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
76a01679 3655 struct block *block, struct symtab *symtab)
96d887e8
PH
3656{
3657}
4c4b4cd2
PH
3658\f
3659 /* Symbol Lookup */
3660
3661/* Return the result of a standard (literal, C-like) lookup of NAME in
3662 given DOMAIN, visible from lexical block BLOCK. */
3663
3664static struct symbol *
3665standard_lookup (const char *name, const struct block *block,
3666 domain_enum domain)
3667{
3668 struct symbol *sym;
3669 struct symtab *symtab;
3670
3671 if (lookup_cached_symbol (name, domain, &sym, NULL, NULL))
3672 return sym;
76a01679
JB
3673 sym =
3674 lookup_symbol_in_language (name, block, domain, language_c, 0, &symtab);
4c4b4cd2
PH
3675 cache_symbol (name, domain, sym, block_found, symtab);
3676 return sym;
3677}
3678
3679
3680/* Non-zero iff there is at least one non-function/non-enumeral symbol
3681 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
3682 since they contend in overloading in the same way. */
3683static int
3684is_nonfunction (struct ada_symbol_info syms[], int n)
3685{
3686 int i;
3687
3688 for (i = 0; i < n; i += 1)
3689 if (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_FUNC
3690 && (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_ENUM
3691 || SYMBOL_CLASS (syms[i].sym) != LOC_CONST))
14f9c5c9
AS
3692 return 1;
3693
3694 return 0;
3695}
3696
3697/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 3698 struct types. Otherwise, they may not. */
14f9c5c9
AS
3699
3700static int
d2e4a39e 3701equiv_types (struct type *type0, struct type *type1)
14f9c5c9 3702{
d2e4a39e 3703 if (type0 == type1)
14f9c5c9 3704 return 1;
d2e4a39e 3705 if (type0 == NULL || type1 == NULL
14f9c5c9
AS
3706 || TYPE_CODE (type0) != TYPE_CODE (type1))
3707 return 0;
d2e4a39e 3708 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
14f9c5c9
AS
3709 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
3710 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 3711 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 3712 return 1;
d2e4a39e 3713
14f9c5c9
AS
3714 return 0;
3715}
3716
3717/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 3718 no more defined than that of SYM1. */
14f9c5c9
AS
3719
3720static int
d2e4a39e 3721lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
3722{
3723 if (sym0 == sym1)
3724 return 1;
176620f1 3725 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
3726 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
3727 return 0;
3728
d2e4a39e 3729 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
3730 {
3731 case LOC_UNDEF:
3732 return 1;
3733 case LOC_TYPEDEF:
3734 {
4c4b4cd2
PH
3735 struct type *type0 = SYMBOL_TYPE (sym0);
3736 struct type *type1 = SYMBOL_TYPE (sym1);
3737 char *name0 = SYMBOL_LINKAGE_NAME (sym0);
3738 char *name1 = SYMBOL_LINKAGE_NAME (sym1);
3739 int len0 = strlen (name0);
3740 return
3741 TYPE_CODE (type0) == TYPE_CODE (type1)
3742 && (equiv_types (type0, type1)
3743 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
3744 && strncmp (name1 + len0, "___XV", 5) == 0));
14f9c5c9
AS
3745 }
3746 case LOC_CONST:
3747 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4c4b4cd2 3748 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
d2e4a39e
AS
3749 default:
3750 return 0;
14f9c5c9
AS
3751 }
3752}
3753
4c4b4cd2
PH
3754/* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct ada_symbol_info
3755 records in OBSTACKP. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
3756
3757static void
76a01679
JB
3758add_defn_to_vec (struct obstack *obstackp,
3759 struct symbol *sym,
3760 struct block *block, struct symtab *symtab)
14f9c5c9
AS
3761{
3762 int i;
3763 size_t tmp;
4c4b4cd2 3764 struct ada_symbol_info *prevDefns = defns_collected (obstackp, 0);
14f9c5c9 3765
d2e4a39e 3766 if (SYMBOL_TYPE (sym) != NULL)
61ee279c 3767 SYMBOL_TYPE (sym) = ada_check_typedef (SYMBOL_TYPE (sym));
4c4b4cd2
PH
3768 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
3769 {
3770 if (lesseq_defined_than (sym, prevDefns[i].sym))
3771 return;
3772 else if (lesseq_defined_than (prevDefns[i].sym, sym))
3773 {
3774 prevDefns[i].sym = sym;
3775 prevDefns[i].block = block;
76a01679 3776 prevDefns[i].symtab = symtab;
4c4b4cd2 3777 return;
76a01679 3778 }
4c4b4cd2
PH
3779 }
3780
3781 {
3782 struct ada_symbol_info info;
3783
3784 info.sym = sym;
3785 info.block = block;
3786 info.symtab = symtab;
3787 obstack_grow (obstackp, &info, sizeof (struct ada_symbol_info));
3788 }
3789}
3790
3791/* Number of ada_symbol_info structures currently collected in
3792 current vector in *OBSTACKP. */
3793
76a01679
JB
3794static int
3795num_defns_collected (struct obstack *obstackp)
4c4b4cd2
PH
3796{
3797 return obstack_object_size (obstackp) / sizeof (struct ada_symbol_info);
3798}
3799
3800/* Vector of ada_symbol_info structures currently collected in current
3801 vector in *OBSTACKP. If FINISH, close off the vector and return
3802 its final address. */
3803
76a01679 3804static struct ada_symbol_info *
4c4b4cd2
PH
3805defns_collected (struct obstack *obstackp, int finish)
3806{
3807 if (finish)
3808 return obstack_finish (obstackp);
3809 else
3810 return (struct ada_symbol_info *) obstack_base (obstackp);
3811}
3812
96d887e8
PH
3813/* Look, in partial_symtab PST, for symbol NAME in given namespace.
3814 Check the global symbols if GLOBAL, the static symbols if not.
3815 Do wild-card match if WILD. */
4c4b4cd2 3816
96d887e8
PH
3817static struct partial_symbol *
3818ada_lookup_partial_symbol (struct partial_symtab *pst, const char *name,
3819 int global, domain_enum namespace, int wild)
4c4b4cd2 3820{
96d887e8
PH
3821 struct partial_symbol **start;
3822 int name_len = strlen (name);
3823 int length = (global ? pst->n_global_syms : pst->n_static_syms);
3824 int i;
4c4b4cd2 3825
96d887e8 3826 if (length == 0)
4c4b4cd2 3827 {
96d887e8 3828 return (NULL);
4c4b4cd2
PH
3829 }
3830
96d887e8
PH
3831 start = (global ?
3832 pst->objfile->global_psymbols.list + pst->globals_offset :
3833 pst->objfile->static_psymbols.list + pst->statics_offset);
4c4b4cd2 3834
96d887e8 3835 if (wild)
4c4b4cd2 3836 {
96d887e8
PH
3837 for (i = 0; i < length; i += 1)
3838 {
3839 struct partial_symbol *psym = start[i];
4c4b4cd2 3840
1265e4aa
JB
3841 if (SYMBOL_DOMAIN (psym) == namespace
3842 && wild_match (name, name_len, SYMBOL_LINKAGE_NAME (psym)))
96d887e8
PH
3843 return psym;
3844 }
3845 return NULL;
4c4b4cd2 3846 }
96d887e8
PH
3847 else
3848 {
3849 if (global)
3850 {
3851 int U;
3852 i = 0;
3853 U = length - 1;
3854 while (U - i > 4)
3855 {
3856 int M = (U + i) >> 1;
3857 struct partial_symbol *psym = start[M];
3858 if (SYMBOL_LINKAGE_NAME (psym)[0] < name[0])
3859 i = M + 1;
3860 else if (SYMBOL_LINKAGE_NAME (psym)[0] > name[0])
3861 U = M - 1;
3862 else if (strcmp (SYMBOL_LINKAGE_NAME (psym), name) < 0)
3863 i = M + 1;
3864 else
3865 U = M;
3866 }
3867 }
3868 else
3869 i = 0;
4c4b4cd2 3870
96d887e8
PH
3871 while (i < length)
3872 {
3873 struct partial_symbol *psym = start[i];
4c4b4cd2 3874
96d887e8
PH
3875 if (SYMBOL_DOMAIN (psym) == namespace)
3876 {
3877 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (psym), name_len);
4c4b4cd2 3878
96d887e8
PH
3879 if (cmp < 0)
3880 {
3881 if (global)
3882 break;
3883 }
3884 else if (cmp == 0
3885 && is_name_suffix (SYMBOL_LINKAGE_NAME (psym)
76a01679 3886 + name_len))
96d887e8
PH
3887 return psym;
3888 }
3889 i += 1;
3890 }
4c4b4cd2 3891
96d887e8
PH
3892 if (global)
3893 {
3894 int U;
3895 i = 0;
3896 U = length - 1;
3897 while (U - i > 4)
3898 {
3899 int M = (U + i) >> 1;
3900 struct partial_symbol *psym = start[M];
3901 if (SYMBOL_LINKAGE_NAME (psym)[0] < '_')
3902 i = M + 1;
3903 else if (SYMBOL_LINKAGE_NAME (psym)[0] > '_')
3904 U = M - 1;
3905 else if (strcmp (SYMBOL_LINKAGE_NAME (psym), "_ada_") < 0)
3906 i = M + 1;
3907 else
3908 U = M;
3909 }
3910 }
3911 else
3912 i = 0;
4c4b4cd2 3913
96d887e8
PH
3914 while (i < length)
3915 {
3916 struct partial_symbol *psym = start[i];
4c4b4cd2 3917
96d887e8
PH
3918 if (SYMBOL_DOMAIN (psym) == namespace)
3919 {
3920 int cmp;
4c4b4cd2 3921
96d887e8
PH
3922 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (psym)[0];
3923 if (cmp == 0)
3924 {
3925 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (psym), 5);
3926 if (cmp == 0)
3927 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (psym) + 5,
76a01679 3928 name_len);
96d887e8 3929 }
4c4b4cd2 3930
96d887e8
PH
3931 if (cmp < 0)
3932 {
3933 if (global)
3934 break;
3935 }
3936 else if (cmp == 0
3937 && is_name_suffix (SYMBOL_LINKAGE_NAME (psym)
76a01679 3938 + name_len + 5))
96d887e8
PH
3939 return psym;
3940 }
3941 i += 1;
3942 }
3943 }
3944 return NULL;
4c4b4cd2
PH
3945}
3946
96d887e8 3947/* Find a symbol table containing symbol SYM or NULL if none. */
4c4b4cd2 3948
96d887e8
PH
3949static struct symtab *
3950symtab_for_sym (struct symbol *sym)
4c4b4cd2 3951{
96d887e8
PH
3952 struct symtab *s;
3953 struct objfile *objfile;
3954 struct block *b;
3955 struct symbol *tmp_sym;
3956 struct dict_iterator iter;
3957 int j;
4c4b4cd2 3958
96d887e8
PH
3959 ALL_SYMTABS (objfile, s)
3960 {
3961 switch (SYMBOL_CLASS (sym))
3962 {
3963 case LOC_CONST:
3964 case LOC_STATIC:
3965 case LOC_TYPEDEF:
3966 case LOC_REGISTER:
3967 case LOC_LABEL:
3968 case LOC_BLOCK:
3969 case LOC_CONST_BYTES:
76a01679
JB
3970 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3971 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
3972 return s;
3973 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3974 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
3975 return s;
96d887e8
PH
3976 break;
3977 default:
3978 break;
3979 }
3980 switch (SYMBOL_CLASS (sym))
3981 {
3982 case LOC_REGISTER:
3983 case LOC_ARG:
3984 case LOC_REF_ARG:
3985 case LOC_REGPARM:
3986 case LOC_REGPARM_ADDR:
3987 case LOC_LOCAL:
3988 case LOC_TYPEDEF:
3989 case LOC_LOCAL_ARG:
3990 case LOC_BASEREG:
3991 case LOC_BASEREG_ARG:
3992 case LOC_COMPUTED:
3993 case LOC_COMPUTED_ARG:
76a01679
JB
3994 for (j = FIRST_LOCAL_BLOCK;
3995 j < BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)); j += 1)
3996 {
3997 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), j);
3998 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
3999 return s;
4000 }
4001 break;
96d887e8
PH
4002 default:
4003 break;
4004 }
4005 }
4006 return NULL;
4c4b4cd2
PH
4007}
4008
96d887e8
PH
4009/* Return a minimal symbol matching NAME according to Ada decoding
4010 rules. Returns NULL if there is no such minimal symbol. Names
4011 prefixed with "standard__" are handled specially: "standard__" is
4012 first stripped off, and only static and global symbols are searched. */
4c4b4cd2 4013
96d887e8
PH
4014struct minimal_symbol *
4015ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4016{
4c4b4cd2 4017 struct objfile *objfile;
96d887e8
PH
4018 struct minimal_symbol *msymbol;
4019 int wild_match;
4c4b4cd2 4020
96d887e8 4021 if (strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
4c4b4cd2 4022 {
96d887e8 4023 name += sizeof ("standard__") - 1;
4c4b4cd2 4024 wild_match = 0;
4c4b4cd2
PH
4025 }
4026 else
96d887e8 4027 wild_match = (strstr (name, "__") == NULL);
4c4b4cd2 4028
96d887e8
PH
4029 ALL_MSYMBOLS (objfile, msymbol)
4030 {
4031 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match)
4032 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4033 return msymbol;
4034 }
4c4b4cd2 4035
96d887e8
PH
4036 return NULL;
4037}
4c4b4cd2 4038
96d887e8
PH
4039/* For all subprograms that statically enclose the subprogram of the
4040 selected frame, add symbols matching identifier NAME in DOMAIN
4041 and their blocks to the list of data in OBSTACKP, as for
4042 ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
4043 wildcard prefix. */
4c4b4cd2 4044
96d887e8
PH
4045static void
4046add_symbols_from_enclosing_procs (struct obstack *obstackp,
76a01679 4047 const char *name, domain_enum namespace,
96d887e8
PH
4048 int wild_match)
4049{
96d887e8 4050}
14f9c5c9 4051
96d887e8 4052/* FIXME: The next two routines belong in symtab.c */
14f9c5c9 4053
76a01679
JB
4054static void
4055restore_language (void *lang)
96d887e8
PH
4056{
4057 set_language ((enum language) lang);
4058}
4c4b4cd2 4059
96d887e8
PH
4060/* As for lookup_symbol, but performed as if the current language
4061 were LANG. */
4c4b4cd2 4062
96d887e8
PH
4063struct symbol *
4064lookup_symbol_in_language (const char *name, const struct block *block,
76a01679
JB
4065 domain_enum domain, enum language lang,
4066 int *is_a_field_of_this, struct symtab **symtab)
96d887e8 4067{
76a01679
JB
4068 struct cleanup *old_chain
4069 = make_cleanup (restore_language, (void *) current_language->la_language);
96d887e8
PH
4070 struct symbol *result;
4071 set_language (lang);
4072 result = lookup_symbol (name, block, domain, is_a_field_of_this, symtab);
4073 do_cleanups (old_chain);
4074 return result;
4075}
14f9c5c9 4076
96d887e8
PH
4077/* True if TYPE is definitely an artificial type supplied to a symbol
4078 for which no debugging information was given in the symbol file. */
14f9c5c9 4079
96d887e8
PH
4080static int
4081is_nondebugging_type (struct type *type)
4082{
4083 char *name = ada_type_name (type);
4084 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4085}
4c4b4cd2 4086
96d887e8
PH
4087/* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4088 duplicate other symbols in the list (The only case I know of where
4089 this happens is when object files containing stabs-in-ecoff are
4090 linked with files containing ordinary ecoff debugging symbols (or no
4091 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4092 Returns the number of items in the modified list. */
4c4b4cd2 4093
96d887e8
PH
4094static int
4095remove_extra_symbols (struct ada_symbol_info *syms, int nsyms)
4096{
4097 int i, j;
4c4b4cd2 4098
96d887e8
PH
4099 i = 0;
4100 while (i < nsyms)
4101 {
4102 if (SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL
4103 && SYMBOL_CLASS (syms[i].sym) == LOC_STATIC
4104 && is_nondebugging_type (SYMBOL_TYPE (syms[i].sym)))
4105 {
4106 for (j = 0; j < nsyms; j += 1)
4107 {
4108 if (i != j
4109 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4110 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
76a01679 4111 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0
96d887e8
PH
4112 && SYMBOL_CLASS (syms[i].sym) == SYMBOL_CLASS (syms[j].sym)
4113 && SYMBOL_VALUE_ADDRESS (syms[i].sym)
4114 == SYMBOL_VALUE_ADDRESS (syms[j].sym))
4c4b4cd2 4115 {
96d887e8
PH
4116 int k;
4117 for (k = i + 1; k < nsyms; k += 1)
76a01679 4118 syms[k - 1] = syms[k];
96d887e8
PH
4119 nsyms -= 1;
4120 goto NextSymbol;
4c4b4cd2 4121 }
4c4b4cd2 4122 }
4c4b4cd2 4123 }
96d887e8
PH
4124 i += 1;
4125 NextSymbol:
4126 ;
14f9c5c9 4127 }
96d887e8 4128 return nsyms;
14f9c5c9
AS
4129}
4130
96d887e8
PH
4131/* Given a type that corresponds to a renaming entity, use the type name
4132 to extract the scope (package name or function name, fully qualified,
4133 and following the GNAT encoding convention) where this renaming has been
4134 defined. The string returned needs to be deallocated after use. */
4c4b4cd2 4135
96d887e8
PH
4136static char *
4137xget_renaming_scope (struct type *renaming_type)
14f9c5c9 4138{
96d887e8
PH
4139 /* The renaming types adhere to the following convention:
4140 <scope>__<rename>___<XR extension>.
4141 So, to extract the scope, we search for the "___XR" extension,
4142 and then backtrack until we find the first "__". */
76a01679 4143
96d887e8
PH
4144 const char *name = type_name_no_tag (renaming_type);
4145 char *suffix = strstr (name, "___XR");
4146 char *last;
4147 int scope_len;
4148 char *scope;
14f9c5c9 4149
96d887e8
PH
4150 /* Now, backtrack a bit until we find the first "__". Start looking
4151 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 4152
96d887e8
PH
4153 for (last = suffix - 3; last > name; last--)
4154 if (last[0] == '_' && last[1] == '_')
4155 break;
76a01679 4156
96d887e8 4157 /* Make a copy of scope and return it. */
14f9c5c9 4158
96d887e8
PH
4159 scope_len = last - name;
4160 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
14f9c5c9 4161
96d887e8
PH
4162 strncpy (scope, name, scope_len);
4163 scope[scope_len] = '\0';
4c4b4cd2 4164
96d887e8 4165 return scope;
4c4b4cd2
PH
4166}
4167
96d887e8 4168/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 4169
96d887e8
PH
4170static int
4171is_package_name (const char *name)
4c4b4cd2 4172{
96d887e8
PH
4173 /* Here, We take advantage of the fact that no symbols are generated
4174 for packages, while symbols are generated for each function.
4175 So the condition for NAME represent a package becomes equivalent
4176 to NAME not existing in our list of symbols. There is only one
4177 small complication with library-level functions (see below). */
4c4b4cd2 4178
96d887e8 4179 char *fun_name;
76a01679 4180
96d887e8
PH
4181 /* If it is a function that has not been defined at library level,
4182 then we should be able to look it up in the symbols. */
4183 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4184 return 0;
14f9c5c9 4185
96d887e8
PH
4186 /* Library-level function names start with "_ada_". See if function
4187 "_ada_" followed by NAME can be found. */
14f9c5c9 4188
96d887e8
PH
4189 /* Do a quick check that NAME does not contain "__", since library-level
4190 functions names can not contain "__" in them. */
4191 if (strstr (name, "__") != NULL)
4192 return 0;
4c4b4cd2 4193
b435e160 4194 fun_name = xstrprintf ("_ada_%s", name);
14f9c5c9 4195
96d887e8
PH
4196 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
4197}
14f9c5c9 4198
96d887e8
PH
4199/* Return nonzero if SYM corresponds to a renaming entity that is
4200 visible from FUNCTION_NAME. */
14f9c5c9 4201
96d887e8
PH
4202static int
4203renaming_is_visible (const struct symbol *sym, char *function_name)
4204{
4205 char *scope = xget_renaming_scope (SYMBOL_TYPE (sym));
d2e4a39e 4206
96d887e8 4207 make_cleanup (xfree, scope);
14f9c5c9 4208
96d887e8
PH
4209 /* If the rename has been defined in a package, then it is visible. */
4210 if (is_package_name (scope))
4211 return 1;
14f9c5c9 4212
96d887e8
PH
4213 /* Check that the rename is in the current function scope by checking
4214 that its name starts with SCOPE. */
76a01679 4215
96d887e8
PH
4216 /* If the function name starts with "_ada_", it means that it is
4217 a library-level function. Strip this prefix before doing the
4218 comparison, as the encoding for the renaming does not contain
4219 this prefix. */
4220 if (strncmp (function_name, "_ada_", 5) == 0)
4221 function_name += 5;
f26caa11 4222
96d887e8 4223 return (strncmp (function_name, scope, strlen (scope)) == 0);
f26caa11
PH
4224}
4225
96d887e8
PH
4226/* Iterates over the SYMS list and remove any entry that corresponds to
4227 a renaming entity that is not visible from the function associated
4228 with CURRENT_BLOCK.
4229
4230 Rationale:
4231 GNAT emits a type following a specified encoding for each renaming
4232 entity. Unfortunately, STABS currently does not support the definition
4233 of types that are local to a given lexical block, so all renamings types
4234 are emitted at library level. As a consequence, if an application
4235 contains two renaming entities using the same name, and a user tries to
4236 print the value of one of these entities, the result of the ada symbol
4237 lookup will also contain the wrong renaming type.
f26caa11 4238
96d887e8
PH
4239 This function partially covers for this limitation by attempting to
4240 remove from the SYMS list renaming symbols that should be visible
4241 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4242 method with the current information available. The implementation
4243 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4244
4245 - When the user tries to print a rename in a function while there
4246 is another rename entity defined in a package: Normally, the
4247 rename in the function has precedence over the rename in the
4248 package, so the latter should be removed from the list. This is
4249 currently not the case.
4250
4251 - This function will incorrectly remove valid renames if
4252 the CURRENT_BLOCK corresponds to a function which symbol name
4253 has been changed by an "Export" pragma. As a consequence,
4254 the user will be unable to print such rename entities. */
4c4b4cd2 4255
14f9c5c9 4256static int
96d887e8 4257remove_out_of_scope_renamings (struct ada_symbol_info *syms,
76a01679 4258 int nsyms, struct block *current_block)
4c4b4cd2
PH
4259{
4260 struct symbol *current_function;
4261 char *current_function_name;
4262 int i;
4263
4264 /* Extract the function name associated to CURRENT_BLOCK.
4265 Abort if unable to do so. */
76a01679 4266
4c4b4cd2
PH
4267 if (current_block == NULL)
4268 return nsyms;
76a01679 4269
4c4b4cd2
PH
4270 current_function = block_function (current_block);
4271 if (current_function == NULL)
4272 return nsyms;
4273
4274 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
4275 if (current_function_name == NULL)
4276 return nsyms;
4277
4278 /* Check each of the symbols, and remove it from the list if it is
4279 a type corresponding to a renaming that is out of the scope of
4280 the current block. */
4281
4282 i = 0;
4283 while (i < nsyms)
4284 {
4285 if (ada_is_object_renaming (syms[i].sym)
4286 && !renaming_is_visible (syms[i].sym, current_function_name))
4287 {
4288 int j;
4289 for (j = i + 1; j < nsyms; j++)
76a01679 4290 syms[j - 1] = syms[j];
4c4b4cd2
PH
4291 nsyms -= 1;
4292 }
4293 else
4294 i += 1;
4295 }
4296
4297 return nsyms;
4298}
4299
4300/* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing
4301 scope and in global scopes, returning the number of matches. Sets
4302 *RESULTS to point to a vector of (SYM,BLOCK,SYMTAB) triples,
4303 indicating the symbols found and the blocks and symbol tables (if
4304 any) in which they were found. This vector are transient---good only to
4305 the next call of ada_lookup_symbol_list. Any non-function/non-enumeral
4306 symbol match within the nest of blocks whose innermost member is BLOCK0,
4307 is the one match returned (no other matches in that or
4308 enclosing blocks is returned). If there are any matches in or
4309 surrounding BLOCK0, then these alone are returned. Otherwise, the
4310 search extends to global and file-scope (static) symbol tables.
4311 Names prefixed with "standard__" are handled specially: "standard__"
4312 is first stripped off, and only static and global symbols are searched. */
14f9c5c9
AS
4313
4314int
4c4b4cd2 4315ada_lookup_symbol_list (const char *name0, const struct block *block0,
76a01679
JB
4316 domain_enum namespace,
4317 struct ada_symbol_info **results)
14f9c5c9
AS
4318{
4319 struct symbol *sym;
4320 struct symtab *s;
4321 struct partial_symtab *ps;
4322 struct blockvector *bv;
4323 struct objfile *objfile;
14f9c5c9 4324 struct block *block;
4c4b4cd2 4325 const char *name;
14f9c5c9 4326 struct minimal_symbol *msymbol;
4c4b4cd2 4327 int wild_match;
14f9c5c9 4328 int cacheIfUnique;
4c4b4cd2
PH
4329 int block_depth;
4330 int ndefns;
14f9c5c9 4331
4c4b4cd2
PH
4332 obstack_free (&symbol_list_obstack, NULL);
4333 obstack_init (&symbol_list_obstack);
14f9c5c9 4334
14f9c5c9
AS
4335 cacheIfUnique = 0;
4336
4337 /* Search specified block and its superiors. */
4338
4c4b4cd2
PH
4339 wild_match = (strstr (name0, "__") == NULL);
4340 name = name0;
76a01679
JB
4341 block = (struct block *) block0; /* FIXME: No cast ought to be
4342 needed, but adding const will
4343 have a cascade effect. */
4c4b4cd2
PH
4344 if (strncmp (name0, "standard__", sizeof ("standard__") - 1) == 0)
4345 {
4346 wild_match = 0;
4347 block = NULL;
4348 name = name0 + sizeof ("standard__") - 1;
4349 }
4350
4351 block_depth = 0;
14f9c5c9
AS
4352 while (block != NULL)
4353 {
4c4b4cd2 4354 block_depth += 1;
76a01679
JB
4355 ada_add_block_symbols (&symbol_list_obstack, block, name,
4356 namespace, NULL, NULL, wild_match);
14f9c5c9 4357
4c4b4cd2
PH
4358 /* If we found a non-function match, assume that's the one. */
4359 if (is_nonfunction (defns_collected (&symbol_list_obstack, 0),
76a01679 4360 num_defns_collected (&symbol_list_obstack)))
4c4b4cd2 4361 goto done;
14f9c5c9
AS
4362
4363 block = BLOCK_SUPERBLOCK (block);
4364 }
4365
4c4b4cd2
PH
4366 /* If no luck so far, try to find NAME as a local symbol in some lexically
4367 enclosing subprogram. */
4368 if (num_defns_collected (&symbol_list_obstack) == 0 && block_depth > 2)
4369 add_symbols_from_enclosing_procs (&symbol_list_obstack,
76a01679 4370 name, namespace, wild_match);
4c4b4cd2
PH
4371
4372 /* If we found ANY matches among non-global symbols, we're done. */
14f9c5c9 4373
4c4b4cd2 4374 if (num_defns_collected (&symbol_list_obstack) > 0)
14f9c5c9 4375 goto done;
d2e4a39e 4376
14f9c5c9 4377 cacheIfUnique = 1;
4c4b4cd2
PH
4378 if (lookup_cached_symbol (name0, namespace, &sym, &block, &s))
4379 {
4380 if (sym != NULL)
4381 add_defn_to_vec (&symbol_list_obstack, sym, block, s);
4382 goto done;
4383 }
14f9c5c9
AS
4384
4385 /* Now add symbols from all global blocks: symbol tables, minimal symbol
4c4b4cd2 4386 tables, and psymtab's. */
14f9c5c9
AS
4387
4388 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
4389 {
4390 QUIT;
4391 if (!s->primary)
4392 continue;
4393 bv = BLOCKVECTOR (s);
4394 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
76a01679
JB
4395 ada_add_block_symbols (&symbol_list_obstack, block, name, namespace,
4396 objfile, s, wild_match);
d2e4a39e 4397 }
14f9c5c9 4398
4c4b4cd2 4399 if (namespace == VAR_DOMAIN)
14f9c5c9
AS
4400 {
4401 ALL_MSYMBOLS (objfile, msymbol)
d2e4a39e 4402 {
4c4b4cd2
PH
4403 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match))
4404 {
4405 switch (MSYMBOL_TYPE (msymbol))
4406 {
4407 case mst_solib_trampoline:
4408 break;
4409 default:
4410 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
4411 if (s != NULL)
4412 {
4413 int ndefns0 = num_defns_collected (&symbol_list_obstack);
4414 QUIT;
4415 bv = BLOCKVECTOR (s);
4416 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4417 ada_add_block_symbols (&symbol_list_obstack, block,
4418 SYMBOL_LINKAGE_NAME (msymbol),
4419 namespace, objfile, s, wild_match);
76a01679 4420
4c4b4cd2
PH
4421 if (num_defns_collected (&symbol_list_obstack) == ndefns0)
4422 {
4423 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
4424 ada_add_block_symbols (&symbol_list_obstack, block,
4425 SYMBOL_LINKAGE_NAME (msymbol),
4426 namespace, objfile, s,
4427 wild_match);
4428 }
4429 }
4430 }
4431 }
d2e4a39e 4432 }
14f9c5c9 4433 }
d2e4a39e 4434
14f9c5c9 4435 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
4436 {
4437 QUIT;
4438 if (!ps->readin
4c4b4cd2 4439 && ada_lookup_partial_symbol (ps, name, 1, namespace, wild_match))
d2e4a39e 4440 {
4c4b4cd2
PH
4441 s = PSYMTAB_TO_SYMTAB (ps);
4442 if (!s->primary)
4443 continue;
4444 bv = BLOCKVECTOR (s);
4445 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4446 ada_add_block_symbols (&symbol_list_obstack, block, name,
76a01679 4447 namespace, objfile, s, wild_match);
d2e4a39e
AS
4448 }
4449 }
4450
4c4b4cd2 4451 /* Now add symbols from all per-file blocks if we've gotten no hits
14f9c5c9 4452 (Not strictly correct, but perhaps better than an error).
4c4b4cd2 4453 Do the symtabs first, then check the psymtabs. */
d2e4a39e 4454
4c4b4cd2 4455 if (num_defns_collected (&symbol_list_obstack) == 0)
14f9c5c9
AS
4456 {
4457
4458 ALL_SYMTABS (objfile, s)
d2e4a39e 4459 {
4c4b4cd2
PH
4460 QUIT;
4461 if (!s->primary)
4462 continue;
4463 bv = BLOCKVECTOR (s);
4464 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
76a01679
JB
4465 ada_add_block_symbols (&symbol_list_obstack, block, name, namespace,
4466 objfile, s, wild_match);
d2e4a39e
AS
4467 }
4468
14f9c5c9 4469 ALL_PSYMTABS (objfile, ps)
d2e4a39e 4470 {
4c4b4cd2
PH
4471 QUIT;
4472 if (!ps->readin
4473 && ada_lookup_partial_symbol (ps, name, 0, namespace, wild_match))
4474 {
4475 s = PSYMTAB_TO_SYMTAB (ps);
4476 bv = BLOCKVECTOR (s);
4477 if (!s->primary)
4478 continue;
4479 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
76a01679
JB
4480 ada_add_block_symbols (&symbol_list_obstack, block, name,
4481 namespace, objfile, s, wild_match);
4c4b4cd2 4482 }
d2e4a39e
AS
4483 }
4484 }
14f9c5c9 4485
4c4b4cd2
PH
4486done:
4487 ndefns = num_defns_collected (&symbol_list_obstack);
4488 *results = defns_collected (&symbol_list_obstack, 1);
4489
4490 ndefns = remove_extra_symbols (*results, ndefns);
4491
d2e4a39e 4492 if (ndefns == 0)
4c4b4cd2 4493 cache_symbol (name0, namespace, NULL, NULL, NULL);
14f9c5c9 4494
4c4b4cd2 4495 if (ndefns == 1 && cacheIfUnique)
76a01679
JB
4496 cache_symbol (name0, namespace, (*results)[0].sym, (*results)[0].block,
4497 (*results)[0].symtab);
14f9c5c9 4498
4c4b4cd2
PH
4499 ndefns = remove_out_of_scope_renamings (*results, ndefns,
4500 (struct block *) block0);
14f9c5c9 4501
14f9c5c9
AS
4502 return ndefns;
4503}
4504
4c4b4cd2
PH
4505/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
4506 scope and in global scopes, or NULL if none. NAME is folded and
4507 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
714e53ab
PH
4508 choosing the first symbol if there are multiple choices.
4509 *IS_A_FIELD_OF_THIS is set to 0 and *SYMTAB is set to the symbol
4510 table in which the symbol was found (in both cases, these
4511 assignments occur only if the pointers are non-null). */
4512
d2e4a39e 4513struct symbol *
4c4b4cd2
PH
4514ada_lookup_symbol (const char *name, const struct block *block0,
4515 domain_enum namespace, int *is_a_field_of_this,
76a01679 4516 struct symtab **symtab)
14f9c5c9 4517{
4c4b4cd2 4518 struct ada_symbol_info *candidates;
14f9c5c9
AS
4519 int n_candidates;
4520
4c4b4cd2
PH
4521 n_candidates = ada_lookup_symbol_list (ada_encode (ada_fold_name (name)),
4522 block0, namespace, &candidates);
14f9c5c9
AS
4523
4524 if (n_candidates == 0)
4525 return NULL;
4c4b4cd2
PH
4526
4527 if (is_a_field_of_this != NULL)
4528 *is_a_field_of_this = 0;
4529
76a01679 4530 if (symtab != NULL)
4c4b4cd2
PH
4531 {
4532 *symtab = candidates[0].symtab;
76a01679
JB
4533 if (*symtab == NULL && candidates[0].block != NULL)
4534 {
4535 struct objfile *objfile;
4536 struct symtab *s;
4537 struct block *b;
4538 struct blockvector *bv;
4539
4540 /* Search the list of symtabs for one which contains the
4541 address of the start of this block. */
4542 ALL_SYMTABS (objfile, s)
4543 {
4544 bv = BLOCKVECTOR (s);
4545 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4546 if (BLOCK_START (b) <= BLOCK_START (candidates[0].block)
4547 && BLOCK_END (b) > BLOCK_START (candidates[0].block))
4548 {
4549 *symtab = s;
4550 return fixup_symbol_section (candidates[0].sym, objfile);
4551 }
4552 return fixup_symbol_section (candidates[0].sym, NULL);
4553 }
4554 }
4555 }
4c4b4cd2
PH
4556 return candidates[0].sym;
4557}
14f9c5c9 4558
4c4b4cd2
PH
4559static struct symbol *
4560ada_lookup_symbol_nonlocal (const char *name,
76a01679
JB
4561 const char *linkage_name,
4562 const struct block *block,
4563 const domain_enum domain, struct symtab **symtab)
4c4b4cd2
PH
4564{
4565 if (linkage_name == NULL)
4566 linkage_name = name;
76a01679
JB
4567 return ada_lookup_symbol (linkage_name, block_static_block (block), domain,
4568 NULL, symtab);
14f9c5c9
AS
4569}
4570
4571
4c4b4cd2
PH
4572/* True iff STR is a possible encoded suffix of a normal Ada name
4573 that is to be ignored for matching purposes. Suffixes of parallel
4574 names (e.g., XVE) are not included here. Currently, the possible suffixes
4575 are given by either of the regular expression:
4576
19c1ef65
PH
4577 (__[0-9]+)?\.[0-9]+ [nested subprogram suffix, on platforms such
4578 as GNU/Linux]
4c4b4cd2 4579 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
61ee279c 4580 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
14f9c5c9 4581 */
4c4b4cd2 4582
14f9c5c9 4583static int
d2e4a39e 4584is_name_suffix (const char *str)
14f9c5c9
AS
4585{
4586 int k;
4c4b4cd2
PH
4587 const char *matching;
4588 const int len = strlen (str);
4589
4590 /* (__[0-9]+)?\.[0-9]+ */
4591 matching = str;
4592 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
4593 {
4594 matching += 3;
4595 while (isdigit (matching[0]))
4596 matching += 1;
4597 if (matching[0] == '\0')
4598 return 1;
4599 }
4600
4601 if (matching[0] == '.')
4602 {
4603 matching += 1;
4604 while (isdigit (matching[0]))
4605 matching += 1;
4606 if (matching[0] == '\0')
4607 return 1;
4608 }
4609
4610 /* ___[0-9]+ */
4611 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
4612 {
4613 matching = str + 3;
4614 while (isdigit (matching[0]))
4615 matching += 1;
4616 if (matching[0] == '\0')
4617 return 1;
4618 }
4619
4620 /* ??? We should not modify STR directly, as we are doing below. This
4621 is fine in this case, but may become problematic later if we find
4622 that this alternative did not work, and want to try matching
4623 another one from the begining of STR. Since we modified it, we
4624 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
4625 if (str[0] == 'X')
4626 {
4627 str += 1;
d2e4a39e 4628 while (str[0] != '_' && str[0] != '\0')
4c4b4cd2
PH
4629 {
4630 if (str[0] != 'n' && str[0] != 'b')
4631 return 0;
4632 str += 1;
4633 }
14f9c5c9
AS
4634 }
4635 if (str[0] == '\000')
4636 return 1;
d2e4a39e 4637 if (str[0] == '_')
14f9c5c9
AS
4638 {
4639 if (str[1] != '_' || str[2] == '\000')
4c4b4cd2 4640 return 0;
d2e4a39e 4641 if (str[2] == '_')
4c4b4cd2 4642 {
61ee279c
PH
4643 if (strcmp (str + 3, "JM") == 0)
4644 return 1;
4645 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
4646 the LJM suffix in favor of the JM one. But we will
4647 still accept LJM as a valid suffix for a reasonable
4648 amount of time, just to allow ourselves to debug programs
4649 compiled using an older version of GNAT. */
4c4b4cd2
PH
4650 if (strcmp (str + 3, "LJM") == 0)
4651 return 1;
4652 if (str[3] != 'X')
4653 return 0;
1265e4aa
JB
4654 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
4655 || str[4] == 'U' || str[4] == 'P')
4c4b4cd2
PH
4656 return 1;
4657 if (str[4] == 'R' && str[5] != 'T')
4658 return 1;
4659 return 0;
4660 }
4661 if (!isdigit (str[2]))
4662 return 0;
4663 for (k = 3; str[k] != '\0'; k += 1)
4664 if (!isdigit (str[k]) && str[k] != '_')
4665 return 0;
14f9c5c9
AS
4666 return 1;
4667 }
4c4b4cd2 4668 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 4669 {
4c4b4cd2
PH
4670 for (k = 2; str[k] != '\0'; k += 1)
4671 if (!isdigit (str[k]) && str[k] != '_')
4672 return 0;
14f9c5c9
AS
4673 return 1;
4674 }
4675 return 0;
4676}
d2e4a39e 4677
4c4b4cd2
PH
4678/* Return nonzero if the given string starts with a dot ('.')
4679 followed by zero or more digits.
4680
4681 Note: brobecker/2003-11-10: A forward declaration has not been
4682 added at the begining of this file yet, because this function
4683 is only used to work around a problem found during wild matching
4684 when trying to match minimal symbol names against symbol names
4685 obtained from dwarf-2 data. This function is therefore currently
4686 only used in wild_match() and is likely to be deleted when the
4687 problem in dwarf-2 is fixed. */
4688
4689static int
4690is_dot_digits_suffix (const char *str)
4691{
4692 if (str[0] != '.')
4693 return 0;
4694
4695 str++;
4696 while (isdigit (str[0]))
4697 str++;
4698 return (str[0] == '\0');
4699}
4700
4701/* True if NAME represents a name of the form A1.A2....An, n>=1 and
4702 PATN[0..PATN_LEN-1] = Ak.Ak+1.....An for some k >= 1. Ignores
4703 informational suffixes of NAME (i.e., for which is_name_suffix is
4704 true). */
4705
14f9c5c9 4706static int
4c4b4cd2 4707wild_match (const char *patn0, int patn_len, const char *name0)
14f9c5c9
AS
4708{
4709 int name_len;
4c4b4cd2
PH
4710 char *name;
4711 char *patn;
4712
4713 /* FIXME: brobecker/2003-11-10: For some reason, the symbol name
4714 stored in the symbol table for nested function names is sometimes
4715 different from the name of the associated entity stored in
4716 the dwarf-2 data: This is the case for nested subprograms, where
4717 the minimal symbol name contains a trailing ".[:digit:]+" suffix,
4718 while the symbol name from the dwarf-2 data does not.
4719
4720 Although the DWARF-2 standard documents that entity names stored
4721 in the dwarf-2 data should be identical to the name as seen in
4722 the source code, GNAT takes a different approach as we already use
4723 a special encoding mechanism to convey the information so that
4724 a C debugger can still use the information generated to debug
4725 Ada programs. A corollary is that the symbol names in the dwarf-2
4726 data should match the names found in the symbol table. I therefore
4727 consider this issue as a compiler defect.
76a01679 4728
4c4b4cd2
PH
4729 Until the compiler is properly fixed, we work-around the problem
4730 by ignoring such suffixes during the match. We do so by making
4731 a copy of PATN0 and NAME0, and then by stripping such a suffix
4732 if present. We then perform the match on the resulting strings. */
4733 {
4734 char *dot;
4735 name_len = strlen (name0);
4736
4737 name = (char *) alloca ((name_len + 1) * sizeof (char));
4738 strcpy (name, name0);
4739 dot = strrchr (name, '.');
4740 if (dot != NULL && is_dot_digits_suffix (dot))
4741 *dot = '\0';
4742
4743 patn = (char *) alloca ((patn_len + 1) * sizeof (char));
4744 strncpy (patn, patn0, patn_len);
4745 patn[patn_len] = '\0';
4746 dot = strrchr (patn, '.');
4747 if (dot != NULL && is_dot_digits_suffix (dot))
4748 {
4749 *dot = '\0';
4750 patn_len = dot - patn;
4751 }
4752 }
4753
4754 /* Now perform the wild match. */
14f9c5c9
AS
4755
4756 name_len = strlen (name);
4c4b4cd2
PH
4757 if (name_len >= patn_len + 5 && strncmp (name, "_ada_", 5) == 0
4758 && strncmp (patn, name + 5, patn_len) == 0
d2e4a39e 4759 && is_name_suffix (name + patn_len + 5))
14f9c5c9
AS
4760 return 1;
4761
d2e4a39e 4762 while (name_len >= patn_len)
14f9c5c9 4763 {
4c4b4cd2
PH
4764 if (strncmp (patn, name, patn_len) == 0
4765 && is_name_suffix (name + patn_len))
4766 return 1;
4767 do
4768 {
4769 name += 1;
4770 name_len -= 1;
4771 }
d2e4a39e 4772 while (name_len > 0
4c4b4cd2 4773 && name[0] != '.' && (name[0] != '_' || name[1] != '_'));
14f9c5c9 4774 if (name_len <= 0)
4c4b4cd2 4775 return 0;
14f9c5c9 4776 if (name[0] == '_')
4c4b4cd2
PH
4777 {
4778 if (!islower (name[2]))
4779 return 0;
4780 name += 2;
4781 name_len -= 2;
4782 }
14f9c5c9 4783 else
4c4b4cd2
PH
4784 {
4785 if (!islower (name[1]))
4786 return 0;
4787 name += 1;
4788 name_len -= 1;
4789 }
96d887e8
PH
4790 }
4791
4792 return 0;
4793}
4794
4795
4796/* Add symbols from BLOCK matching identifier NAME in DOMAIN to
4797 vector *defn_symbols, updating the list of symbols in OBSTACKP
4798 (if necessary). If WILD, treat as NAME with a wildcard prefix.
4799 OBJFILE is the section containing BLOCK.
4800 SYMTAB is recorded with each symbol added. */
4801
4802static void
4803ada_add_block_symbols (struct obstack *obstackp,
76a01679 4804 struct block *block, const char *name,
96d887e8
PH
4805 domain_enum domain, struct objfile *objfile,
4806 struct symtab *symtab, int wild)
4807{
4808 struct dict_iterator iter;
4809 int name_len = strlen (name);
4810 /* A matching argument symbol, if any. */
4811 struct symbol *arg_sym;
4812 /* Set true when we find a matching non-argument symbol. */
4813 int found_sym;
4814 struct symbol *sym;
4815
4816 arg_sym = NULL;
4817 found_sym = 0;
4818 if (wild)
4819 {
4820 struct symbol *sym;
4821 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 4822 {
1265e4aa
JB
4823 if (SYMBOL_DOMAIN (sym) == domain
4824 && wild_match (name, name_len, SYMBOL_LINKAGE_NAME (sym)))
76a01679
JB
4825 {
4826 switch (SYMBOL_CLASS (sym))
4827 {
4828 case LOC_ARG:
4829 case LOC_LOCAL_ARG:
4830 case LOC_REF_ARG:
4831 case LOC_REGPARM:
4832 case LOC_REGPARM_ADDR:
4833 case LOC_BASEREG_ARG:
4834 case LOC_COMPUTED_ARG:
4835 arg_sym = sym;
4836 break;
4837 case LOC_UNRESOLVED:
4838 continue;
4839 default:
4840 found_sym = 1;
4841 add_defn_to_vec (obstackp,
4842 fixup_symbol_section (sym, objfile),
4843 block, symtab);
4844 break;
4845 }
4846 }
4847 }
96d887e8
PH
4848 }
4849 else
4850 {
4851 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679
JB
4852 {
4853 if (SYMBOL_DOMAIN (sym) == domain)
4854 {
4855 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym), name_len);
4856 if (cmp == 0
4857 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len))
4858 {
4859 switch (SYMBOL_CLASS (sym))
4860 {
4861 case LOC_ARG:
4862 case LOC_LOCAL_ARG:
4863 case LOC_REF_ARG:
4864 case LOC_REGPARM:
4865 case LOC_REGPARM_ADDR:
4866 case LOC_BASEREG_ARG:
4867 case LOC_COMPUTED_ARG:
4868 arg_sym = sym;
4869 break;
4870 case LOC_UNRESOLVED:
4871 break;
4872 default:
4873 found_sym = 1;
4874 add_defn_to_vec (obstackp,
4875 fixup_symbol_section (sym, objfile),
4876 block, symtab);
4877 break;
4878 }
4879 }
4880 }
4881 }
96d887e8
PH
4882 }
4883
4884 if (!found_sym && arg_sym != NULL)
4885 {
76a01679
JB
4886 add_defn_to_vec (obstackp,
4887 fixup_symbol_section (arg_sym, objfile),
4888 block, symtab);
96d887e8
PH
4889 }
4890
4891 if (!wild)
4892 {
4893 arg_sym = NULL;
4894 found_sym = 0;
4895
4896 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679
JB
4897 {
4898 if (SYMBOL_DOMAIN (sym) == domain)
4899 {
4900 int cmp;
4901
4902 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
4903 if (cmp == 0)
4904 {
4905 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (sym), 5);
4906 if (cmp == 0)
4907 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
4908 name_len);
4909 }
4910
4911 if (cmp == 0
4912 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
4913 {
4914 switch (SYMBOL_CLASS (sym))
4915 {
4916 case LOC_ARG:
4917 case LOC_LOCAL_ARG:
4918 case LOC_REF_ARG:
4919 case LOC_REGPARM:
4920 case LOC_REGPARM_ADDR:
4921 case LOC_BASEREG_ARG:
4922 case LOC_COMPUTED_ARG:
4923 arg_sym = sym;
4924 break;
4925 case LOC_UNRESOLVED:
4926 break;
4927 default:
4928 found_sym = 1;
4929 add_defn_to_vec (obstackp,
4930 fixup_symbol_section (sym, objfile),
4931 block, symtab);
4932 break;
4933 }
4934 }
4935 }
76a01679 4936 }
96d887e8
PH
4937
4938 /* NOTE: This really shouldn't be needed for _ada_ symbols.
4939 They aren't parameters, right? */
4940 if (!found_sym && arg_sym != NULL)
4941 {
4942 add_defn_to_vec (obstackp,
76a01679
JB
4943 fixup_symbol_section (arg_sym, objfile),
4944 block, symtab);
96d887e8
PH
4945 }
4946 }
4947}
4948\f
963a6417 4949 /* Field Access */
96d887e8 4950
963a6417
PH
4951/* True if field number FIELD_NUM in struct or union type TYPE is supposed
4952 to be invisible to users. */
96d887e8 4953
963a6417
PH
4954int
4955ada_is_ignored_field (struct type *type, int field_num)
96d887e8 4956{
963a6417
PH
4957 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
4958 return 1;
4959 else
96d887e8 4960 {
963a6417
PH
4961 const char *name = TYPE_FIELD_NAME (type, field_num);
4962 return (name == NULL
4963 || (name[0] == '_' && strncmp (name, "_parent", 7) != 0));
96d887e8 4964 }
963a6417 4965}
96d887e8 4966
963a6417
PH
4967/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
4968 pointer or reference type whose ultimate target has a tag field. */
96d887e8 4969
963a6417
PH
4970int
4971ada_is_tagged_type (struct type *type, int refok)
4972{
4973 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
4974}
96d887e8 4975
963a6417 4976/* True iff TYPE represents the type of X'Tag */
96d887e8 4977
963a6417
PH
4978int
4979ada_is_tag_type (struct type *type)
4980{
4981 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
4982 return 0;
4983 else
96d887e8 4984 {
963a6417
PH
4985 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
4986 return (name != NULL
4987 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 4988 }
96d887e8
PH
4989}
4990
963a6417 4991/* The type of the tag on VAL. */
76a01679 4992
963a6417
PH
4993struct type *
4994ada_tag_type (struct value *val)
96d887e8 4995{
df407dfe 4996 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0, NULL);
963a6417 4997}
96d887e8 4998
963a6417 4999/* The value of the tag on VAL. */
96d887e8 5000
963a6417
PH
5001struct value *
5002ada_value_tag (struct value *val)
5003{
5004 return ada_value_struct_elt (val, "_tag", "record");
96d887e8
PH
5005}
5006
963a6417
PH
5007/* The value of the tag on the object of type TYPE whose contents are
5008 saved at VALADDR, if it is non-null, or is at memory address
5009 ADDRESS. */
96d887e8 5010
963a6417 5011static struct value *
10a2c479 5012value_tag_from_contents_and_address (struct type *type,
fc1a4b47 5013 const gdb_byte *valaddr,
963a6417 5014 CORE_ADDR address)
96d887e8 5015{
963a6417
PH
5016 int tag_byte_offset, dummy1, dummy2;
5017 struct type *tag_type;
5018 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
5019 &dummy1, &dummy2))
96d887e8 5020 {
fc1a4b47 5021 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
5022 ? NULL
5023 : valaddr + tag_byte_offset);
963a6417 5024 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 5025
963a6417 5026 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 5027 }
963a6417
PH
5028 return NULL;
5029}
96d887e8 5030
963a6417
PH
5031static struct type *
5032type_from_tag (struct value *tag)
5033{
5034 const char *type_name = ada_tag_name (tag);
5035 if (type_name != NULL)
5036 return ada_find_any_type (ada_encode (type_name));
5037 return NULL;
5038}
96d887e8 5039
963a6417
PH
5040struct tag_args
5041{
5042 struct value *tag;
5043 char *name;
5044};
4c4b4cd2
PH
5045
5046/* Wrapper function used by ada_tag_name. Given a struct tag_args*
5047 value ARGS, sets ARGS->name to the tag name of ARGS->tag.
5048 The value stored in ARGS->name is valid until the next call to
5049 ada_tag_name_1. */
5050
5051static int
5052ada_tag_name_1 (void *args0)
5053{
5054 struct tag_args *args = (struct tag_args *) args0;
5055 static char name[1024];
76a01679 5056 char *p;
4c4b4cd2
PH
5057 struct value *val;
5058 args->name = NULL;
5059 val = ada_value_struct_elt (args->tag, "tsd", NULL);
5060 if (val == NULL)
5061 return 0;
5062 val = ada_value_struct_elt (val, "expanded_name", NULL);
5063 if (val == NULL)
5064 return 0;
5065 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5066 for (p = name; *p != '\0'; p += 1)
5067 if (isalpha (*p))
5068 *p = tolower (*p);
5069 args->name = name;
5070 return 0;
5071}
5072
5073/* The type name of the dynamic type denoted by the 'tag value TAG, as
5074 * a C string. */
5075
5076const char *
5077ada_tag_name (struct value *tag)
5078{
5079 struct tag_args args;
df407dfe 5080 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 5081 return NULL;
76a01679 5082 args.tag = tag;
4c4b4cd2
PH
5083 args.name = NULL;
5084 catch_errors (ada_tag_name_1, &args, NULL, RETURN_MASK_ALL);
5085 return args.name;
5086}
5087
5088/* The parent type of TYPE, or NULL if none. */
14f9c5c9 5089
d2e4a39e 5090struct type *
ebf56fd3 5091ada_parent_type (struct type *type)
14f9c5c9
AS
5092{
5093 int i;
5094
61ee279c 5095 type = ada_check_typedef (type);
14f9c5c9
AS
5096
5097 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5098 return NULL;
5099
5100 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5101 if (ada_is_parent_field (type, i))
61ee279c 5102 return ada_check_typedef (TYPE_FIELD_TYPE (type, i));
14f9c5c9
AS
5103
5104 return NULL;
5105}
5106
4c4b4cd2
PH
5107/* True iff field number FIELD_NUM of structure type TYPE contains the
5108 parent-type (inherited) fields of a derived type. Assumes TYPE is
5109 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
5110
5111int
ebf56fd3 5112ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 5113{
61ee279c 5114 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
4c4b4cd2
PH
5115 return (name != NULL
5116 && (strncmp (name, "PARENT", 6) == 0
5117 || strncmp (name, "_parent", 7) == 0));
14f9c5c9
AS
5118}
5119
4c4b4cd2 5120/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 5121 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 5122 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 5123 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 5124 structures. */
14f9c5c9
AS
5125
5126int
ebf56fd3 5127ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 5128{
d2e4a39e
AS
5129 const char *name = TYPE_FIELD_NAME (type, field_num);
5130 return (name != NULL
4c4b4cd2
PH
5131 && (strncmp (name, "PARENT", 6) == 0
5132 || strcmp (name, "REP") == 0
5133 || strncmp (name, "_parent", 7) == 0
5134 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
5135}
5136
4c4b4cd2
PH
5137/* True iff field number FIELD_NUM of structure or union type TYPE
5138 is a variant wrapper. Assumes TYPE is a structure type with at least
5139 FIELD_NUM+1 fields. */
14f9c5c9
AS
5140
5141int
ebf56fd3 5142ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 5143{
d2e4a39e 5144 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
14f9c5c9 5145 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
4c4b4cd2 5146 || (is_dynamic_field (type, field_num)
c3e5cd34
PH
5147 && (TYPE_CODE (TYPE_TARGET_TYPE (field_type))
5148 == TYPE_CODE_UNION)));
14f9c5c9
AS
5149}
5150
5151/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 5152 whose discriminants are contained in the record type OUTER_TYPE,
14f9c5c9
AS
5153 returns the type of the controlling discriminant for the variant. */
5154
d2e4a39e 5155struct type *
ebf56fd3 5156ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 5157{
d2e4a39e 5158 char *name = ada_variant_discrim_name (var_type);
76a01679 5159 struct type *type =
4c4b4cd2 5160 ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
14f9c5c9
AS
5161 if (type == NULL)
5162 return builtin_type_int;
5163 else
5164 return type;
5165}
5166
4c4b4cd2 5167/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 5168 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 5169 represents a 'when others' clause; otherwise 0. */
14f9c5c9
AS
5170
5171int
ebf56fd3 5172ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 5173{
d2e4a39e 5174 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
5175 return (name != NULL && name[0] == 'O');
5176}
5177
5178/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
5179 returns the name of the discriminant controlling the variant.
5180 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 5181
d2e4a39e 5182char *
ebf56fd3 5183ada_variant_discrim_name (struct type *type0)
14f9c5c9 5184{
d2e4a39e 5185 static char *result = NULL;
14f9c5c9 5186 static size_t result_len = 0;
d2e4a39e
AS
5187 struct type *type;
5188 const char *name;
5189 const char *discrim_end;
5190 const char *discrim_start;
14f9c5c9
AS
5191
5192 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
5193 type = TYPE_TARGET_TYPE (type0);
5194 else
5195 type = type0;
5196
5197 name = ada_type_name (type);
5198
5199 if (name == NULL || name[0] == '\000')
5200 return "";
5201
5202 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
5203 discrim_end -= 1)
5204 {
4c4b4cd2
PH
5205 if (strncmp (discrim_end, "___XVN", 6) == 0)
5206 break;
14f9c5c9
AS
5207 }
5208 if (discrim_end == name)
5209 return "";
5210
d2e4a39e 5211 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
5212 discrim_start -= 1)
5213 {
d2e4a39e 5214 if (discrim_start == name + 1)
4c4b4cd2 5215 return "";
76a01679 5216 if ((discrim_start > name + 3
4c4b4cd2
PH
5217 && strncmp (discrim_start - 3, "___", 3) == 0)
5218 || discrim_start[-1] == '.')
5219 break;
14f9c5c9
AS
5220 }
5221
5222 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
5223 strncpy (result, discrim_start, discrim_end - discrim_start);
d2e4a39e 5224 result[discrim_end - discrim_start] = '\0';
14f9c5c9
AS
5225 return result;
5226}
5227
4c4b4cd2
PH
5228/* Scan STR for a subtype-encoded number, beginning at position K.
5229 Put the position of the character just past the number scanned in
5230 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
5231 Return 1 if there was a valid number at the given position, and 0
5232 otherwise. A "subtype-encoded" number consists of the absolute value
5233 in decimal, followed by the letter 'm' to indicate a negative number.
5234 Assumes 0m does not occur. */
14f9c5c9
AS
5235
5236int
d2e4a39e 5237ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
5238{
5239 ULONGEST RU;
5240
d2e4a39e 5241 if (!isdigit (str[k]))
14f9c5c9
AS
5242 return 0;
5243
4c4b4cd2 5244 /* Do it the hard way so as not to make any assumption about
14f9c5c9 5245 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 5246 LONGEST. */
14f9c5c9
AS
5247 RU = 0;
5248 while (isdigit (str[k]))
5249 {
d2e4a39e 5250 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
5251 k += 1;
5252 }
5253
d2e4a39e 5254 if (str[k] == 'm')
14f9c5c9
AS
5255 {
5256 if (R != NULL)
4c4b4cd2 5257 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
5258 k += 1;
5259 }
5260 else if (R != NULL)
5261 *R = (LONGEST) RU;
5262
4c4b4cd2 5263 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
5264 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
5265 number representable as a LONGEST (although either would probably work
5266 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 5267 above is always equivalent to the negative of RU. */
14f9c5c9
AS
5268
5269 if (new_k != NULL)
5270 *new_k = k;
5271 return 1;
5272}
5273
4c4b4cd2
PH
5274/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
5275 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
5276 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 5277
d2e4a39e 5278int
ebf56fd3 5279ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 5280{
d2e4a39e 5281 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
5282 int p;
5283
5284 p = 0;
5285 while (1)
5286 {
d2e4a39e 5287 switch (name[p])
4c4b4cd2
PH
5288 {
5289 case '\0':
5290 return 0;
5291 case 'S':
5292 {
5293 LONGEST W;
5294 if (!ada_scan_number (name, p + 1, &W, &p))
5295 return 0;
5296 if (val == W)
5297 return 1;
5298 break;
5299 }
5300 case 'R':
5301 {
5302 LONGEST L, U;
5303 if (!ada_scan_number (name, p + 1, &L, &p)
5304 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
5305 return 0;
5306 if (val >= L && val <= U)
5307 return 1;
5308 break;
5309 }
5310 case 'O':
5311 return 1;
5312 default:
5313 return 0;
5314 }
5315 }
5316}
5317
5318/* FIXME: Lots of redundancy below. Try to consolidate. */
5319
5320/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
5321 ARG_TYPE, extract and return the value of one of its (non-static)
5322 fields. FIELDNO says which field. Differs from value_primitive_field
5323 only in that it can handle packed values of arbitrary type. */
14f9c5c9 5324
4c4b4cd2 5325static struct value *
d2e4a39e 5326ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
4c4b4cd2 5327 struct type *arg_type)
14f9c5c9 5328{
14f9c5c9
AS
5329 struct type *type;
5330
61ee279c 5331 arg_type = ada_check_typedef (arg_type);
14f9c5c9
AS
5332 type = TYPE_FIELD_TYPE (arg_type, fieldno);
5333
4c4b4cd2 5334 /* Handle packed fields. */
14f9c5c9
AS
5335
5336 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
5337 {
5338 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
5339 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 5340
0fd88904 5341 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
4c4b4cd2
PH
5342 offset + bit_pos / 8,
5343 bit_pos % 8, bit_size, type);
14f9c5c9
AS
5344 }
5345 else
5346 return value_primitive_field (arg1, offset, fieldno, arg_type);
5347}
5348
4c4b4cd2
PH
5349/* Find field with name NAME in object of type TYPE. If found, return 1
5350 after setting *FIELD_TYPE_P to the field's type, *BYTE_OFFSET_P to
5351 OFFSET + the byte offset of the field within an object of that type,
5352 *BIT_OFFSET_P to the bit offset modulo byte size of the field, and
5353 *BIT_SIZE_P to its size in bits if the field is packed, and 0 otherwise.
5354 Looks inside wrappers for the field. Returns 0 if field not
5355 found. */
5356static int
76a01679
JB
5357find_struct_field (char *name, struct type *type, int offset,
5358 struct type **field_type_p,
5359 int *byte_offset_p, int *bit_offset_p, int *bit_size_p)
4c4b4cd2
PH
5360{
5361 int i;
5362
61ee279c 5363 type = ada_check_typedef (type);
4c4b4cd2
PH
5364 *field_type_p = NULL;
5365 *byte_offset_p = *bit_offset_p = *bit_size_p = 0;
76a01679 5366
4c4b4cd2
PH
5367 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i -= 1)
5368 {
5369 int bit_pos = TYPE_FIELD_BITPOS (type, i);
5370 int fld_offset = offset + bit_pos / 8;
5371 char *t_field_name = TYPE_FIELD_NAME (type, i);
76a01679 5372
4c4b4cd2
PH
5373 if (t_field_name == NULL)
5374 continue;
5375
5376 else if (field_name_match (t_field_name, name))
76a01679
JB
5377 {
5378 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5379 *field_type_p = TYPE_FIELD_TYPE (type, i);
5380 *byte_offset_p = fld_offset;
5381 *bit_offset_p = bit_pos % 8;
5382 *bit_size_p = bit_size;
5383 return 1;
5384 }
4c4b4cd2
PH
5385 else if (ada_is_wrapper_field (type, i))
5386 {
76a01679
JB
5387 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
5388 field_type_p, byte_offset_p, bit_offset_p,
5389 bit_size_p))
5390 return 1;
5391 }
4c4b4cd2
PH
5392 else if (ada_is_variant_part (type, i))
5393 {
5394 int j;
61ee279c 5395 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2
PH
5396
5397 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
5398 {
76a01679
JB
5399 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
5400 fld_offset
5401 + TYPE_FIELD_BITPOS (field_type, j) / 8,
5402 field_type_p, byte_offset_p,
5403 bit_offset_p, bit_size_p))
5404 return 1;
4c4b4cd2
PH
5405 }
5406 }
5407 }
5408 return 0;
5409}
5410
5411
14f9c5c9 5412
4c4b4cd2 5413/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
5414 and search in it assuming it has (class) type TYPE.
5415 If found, return value, else return NULL.
5416
4c4b4cd2 5417 Searches recursively through wrapper fields (e.g., '_parent'). */
14f9c5c9 5418
4c4b4cd2 5419static struct value *
d2e4a39e 5420ada_search_struct_field (char *name, struct value *arg, int offset,
4c4b4cd2 5421 struct type *type)
14f9c5c9
AS
5422{
5423 int i;
61ee279c 5424 type = ada_check_typedef (type);
14f9c5c9 5425
d2e4a39e 5426 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i -= 1)
14f9c5c9
AS
5427 {
5428 char *t_field_name = TYPE_FIELD_NAME (type, i);
5429
5430 if (t_field_name == NULL)
4c4b4cd2 5431 continue;
14f9c5c9
AS
5432
5433 else if (field_name_match (t_field_name, name))
4c4b4cd2 5434 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
5435
5436 else if (ada_is_wrapper_field (type, i))
4c4b4cd2 5437 {
06d5cf63
JB
5438 struct value *v = /* Do not let indent join lines here. */
5439 ada_search_struct_field (name, arg,
5440 offset + TYPE_FIELD_BITPOS (type, i) / 8,
5441 TYPE_FIELD_TYPE (type, i));
4c4b4cd2
PH
5442 if (v != NULL)
5443 return v;
5444 }
14f9c5c9
AS
5445
5446 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
5447 {
5448 int j;
61ee279c 5449 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2
PH
5450 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
5451
5452 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
5453 {
06d5cf63
JB
5454 struct value *v = ada_search_struct_field /* Force line break. */
5455 (name, arg,
5456 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
5457 TYPE_FIELD_TYPE (field_type, j));
4c4b4cd2
PH
5458 if (v != NULL)
5459 return v;
5460 }
5461 }
14f9c5c9
AS
5462 }
5463 return NULL;
5464}
d2e4a39e 5465
4c4b4cd2
PH
5466/* Given ARG, a value of type (pointer or reference to a)*
5467 structure/union, extract the component named NAME from the ultimate
5468 target structure/union and return it as a value with its
5469 appropriate type. If ARG is a pointer or reference and the field
5470 is not packed, returns a reference to the field, otherwise the
5471 value of the field (an lvalue if ARG is an lvalue).
14f9c5c9 5472
4c4b4cd2
PH
5473 The routine searches for NAME among all members of the structure itself
5474 and (recursively) among all members of any wrapper members
14f9c5c9
AS
5475 (e.g., '_parent').
5476
4c4b4cd2
PH
5477 ERR is a name (for use in error messages) that identifies the class
5478 of entity that ARG is supposed to be. ERR may be null, indicating
5479 that on error, the function simply returns NULL, and does not
5480 throw an error. (FIXME: True only if ARG is a pointer or reference
5481 at the moment). */
14f9c5c9 5482
d2e4a39e 5483struct value *
ebf56fd3 5484ada_value_struct_elt (struct value *arg, char *name, char *err)
14f9c5c9 5485{
4c4b4cd2 5486 struct type *t, *t1;
d2e4a39e 5487 struct value *v;
14f9c5c9 5488
4c4b4cd2 5489 v = NULL;
df407dfe 5490 t1 = t = ada_check_typedef (value_type (arg));
4c4b4cd2
PH
5491 if (TYPE_CODE (t) == TYPE_CODE_REF)
5492 {
5493 t1 = TYPE_TARGET_TYPE (t);
5494 if (t1 == NULL)
76a01679
JB
5495 {
5496 if (err == NULL)
5497 return NULL;
5498 else
323e0a4a 5499 error (_("Bad value type in a %s."), err);
76a01679 5500 }
61ee279c 5501 t1 = ada_check_typedef (t1);
4c4b4cd2 5502 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679 5503 {
994b9211 5504 arg = coerce_ref (arg);
76a01679
JB
5505 t = t1;
5506 }
4c4b4cd2 5507 }
14f9c5c9 5508
4c4b4cd2
PH
5509 while (TYPE_CODE (t) == TYPE_CODE_PTR)
5510 {
5511 t1 = TYPE_TARGET_TYPE (t);
5512 if (t1 == NULL)
76a01679
JB
5513 {
5514 if (err == NULL)
5515 return NULL;
5516 else
323e0a4a 5517 error (_("Bad value type in a %s."), err);
76a01679 5518 }
61ee279c 5519 t1 = ada_check_typedef (t1);
4c4b4cd2 5520 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679
JB
5521 {
5522 arg = value_ind (arg);
5523 t = t1;
5524 }
4c4b4cd2 5525 else
76a01679 5526 break;
4c4b4cd2 5527 }
14f9c5c9 5528
4c4b4cd2 5529 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
14f9c5c9 5530 {
4c4b4cd2 5531 if (err == NULL)
76a01679 5532 return NULL;
4c4b4cd2 5533 else
323e0a4a 5534 error (_("Attempt to extract a component of a value that is not a %s."),
76a01679 5535 err);
14f9c5c9
AS
5536 }
5537
4c4b4cd2
PH
5538 if (t1 == t)
5539 v = ada_search_struct_field (name, arg, 0, t);
5540 else
5541 {
5542 int bit_offset, bit_size, byte_offset;
5543 struct type *field_type;
5544 CORE_ADDR address;
5545
76a01679
JB
5546 if (TYPE_CODE (t) == TYPE_CODE_PTR)
5547 address = value_as_address (arg);
4c4b4cd2 5548 else
0fd88904 5549 address = unpack_pointer (t, value_contents (arg));
14f9c5c9 5550
4c4b4cd2 5551 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL);
76a01679
JB
5552 if (find_struct_field (name, t1, 0,
5553 &field_type, &byte_offset, &bit_offset,
5554 &bit_size))
5555 {
5556 if (bit_size != 0)
5557 {
714e53ab
PH
5558 if (TYPE_CODE (t) == TYPE_CODE_REF)
5559 arg = ada_coerce_ref (arg);
5560 else
5561 arg = ada_value_ind (arg);
76a01679
JB
5562 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
5563 bit_offset, bit_size,
5564 field_type);
5565 }
5566 else
5567 v = value_from_pointer (lookup_reference_type (field_type),
5568 address + byte_offset);
5569 }
5570 }
5571
4c4b4cd2 5572 if (v == NULL && err != NULL)
323e0a4a 5573 error (_("There is no member named %s."), name);
14f9c5c9
AS
5574
5575 return v;
5576}
5577
5578/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
5579 If DISPP is non-null, add its byte displacement from the beginning of a
5580 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
5581 work for packed fields).
5582
5583 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 5584 followed by "___".
14f9c5c9 5585
4c4b4cd2
PH
5586 TYPE can be either a struct or union. If REFOK, TYPE may also
5587 be a (pointer or reference)+ to a struct or union, and the
5588 ultimate target type will be searched.
14f9c5c9
AS
5589
5590 Looks recursively into variant clauses and parent types.
5591
4c4b4cd2
PH
5592 If NOERR is nonzero, return NULL if NAME is not suitably defined or
5593 TYPE is not a type of the right kind. */
14f9c5c9 5594
4c4b4cd2 5595static struct type *
76a01679
JB
5596ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
5597 int noerr, int *dispp)
14f9c5c9
AS
5598{
5599 int i;
5600
5601 if (name == NULL)
5602 goto BadName;
5603
76a01679 5604 if (refok && type != NULL)
4c4b4cd2
PH
5605 while (1)
5606 {
61ee279c 5607 type = ada_check_typedef (type);
76a01679
JB
5608 if (TYPE_CODE (type) != TYPE_CODE_PTR
5609 && TYPE_CODE (type) != TYPE_CODE_REF)
5610 break;
5611 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 5612 }
14f9c5c9 5613
76a01679 5614 if (type == NULL
1265e4aa
JB
5615 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
5616 && TYPE_CODE (type) != TYPE_CODE_UNION))
14f9c5c9 5617 {
4c4b4cd2 5618 if (noerr)
76a01679 5619 return NULL;
4c4b4cd2 5620 else
76a01679
JB
5621 {
5622 target_terminal_ours ();
5623 gdb_flush (gdb_stdout);
323e0a4a
AC
5624 if (type == NULL)
5625 error (_("Type (null) is not a structure or union type"));
5626 else
5627 {
5628 /* XXX: type_sprint */
5629 fprintf_unfiltered (gdb_stderr, _("Type "));
5630 type_print (type, "", gdb_stderr, -1);
5631 error (_(" is not a structure or union type"));
5632 }
76a01679 5633 }
14f9c5c9
AS
5634 }
5635
5636 type = to_static_fixed_type (type);
5637
5638 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5639 {
5640 char *t_field_name = TYPE_FIELD_NAME (type, i);
5641 struct type *t;
5642 int disp;
d2e4a39e 5643
14f9c5c9 5644 if (t_field_name == NULL)
4c4b4cd2 5645 continue;
14f9c5c9
AS
5646
5647 else if (field_name_match (t_field_name, name))
4c4b4cd2
PH
5648 {
5649 if (dispp != NULL)
5650 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
61ee279c 5651 return ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2 5652 }
14f9c5c9
AS
5653
5654 else if (ada_is_wrapper_field (type, i))
4c4b4cd2
PH
5655 {
5656 disp = 0;
5657 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
5658 0, 1, &disp);
5659 if (t != NULL)
5660 {
5661 if (dispp != NULL)
5662 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
5663 return t;
5664 }
5665 }
14f9c5c9
AS
5666
5667 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
5668 {
5669 int j;
61ee279c 5670 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
4c4b4cd2
PH
5671
5672 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
5673 {
5674 disp = 0;
5675 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type, j),
5676 name, 0, 1, &disp);
5677 if (t != NULL)
5678 {
5679 if (dispp != NULL)
5680 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
5681 return t;
5682 }
5683 }
5684 }
14f9c5c9
AS
5685
5686 }
5687
5688BadName:
d2e4a39e 5689 if (!noerr)
14f9c5c9
AS
5690 {
5691 target_terminal_ours ();
5692 gdb_flush (gdb_stdout);
323e0a4a
AC
5693 if (name == NULL)
5694 {
5695 /* XXX: type_sprint */
5696 fprintf_unfiltered (gdb_stderr, _("Type "));
5697 type_print (type, "", gdb_stderr, -1);
5698 error (_(" has no component named <null>"));
5699 }
5700 else
5701 {
5702 /* XXX: type_sprint */
5703 fprintf_unfiltered (gdb_stderr, _("Type "));
5704 type_print (type, "", gdb_stderr, -1);
5705 error (_(" has no component named %s"), name);
5706 }
14f9c5c9
AS
5707 }
5708
5709 return NULL;
5710}
5711
5712/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
5713 within a value of type OUTER_TYPE that is stored in GDB at
4c4b4cd2
PH
5714 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
5715 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 5716
d2e4a39e 5717int
ebf56fd3 5718ada_which_variant_applies (struct type *var_type, struct type *outer_type,
fc1a4b47 5719 const gdb_byte *outer_valaddr)
14f9c5c9
AS
5720{
5721 int others_clause;
5722 int i;
5723 int disp;
d2e4a39e
AS
5724 struct type *discrim_type;
5725 char *discrim_name = ada_variant_discrim_name (var_type);
14f9c5c9
AS
5726 LONGEST discrim_val;
5727
5728 disp = 0;
d2e4a39e 5729 discrim_type =
4c4b4cd2 5730 ada_lookup_struct_elt_type (outer_type, discrim_name, 1, 1, &disp);
14f9c5c9
AS
5731 if (discrim_type == NULL)
5732 return -1;
5733 discrim_val = unpack_long (discrim_type, outer_valaddr + disp);
5734
5735 others_clause = -1;
5736 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
5737 {
5738 if (ada_is_others_clause (var_type, i))
4c4b4cd2 5739 others_clause = i;
14f9c5c9 5740 else if (ada_in_variant (discrim_val, var_type, i))
4c4b4cd2 5741 return i;
14f9c5c9
AS
5742 }
5743
5744 return others_clause;
5745}
d2e4a39e 5746\f
14f9c5c9
AS
5747
5748
4c4b4cd2 5749 /* Dynamic-Sized Records */
14f9c5c9
AS
5750
5751/* Strategy: The type ostensibly attached to a value with dynamic size
5752 (i.e., a size that is not statically recorded in the debugging
5753 data) does not accurately reflect the size or layout of the value.
5754 Our strategy is to convert these values to values with accurate,
4c4b4cd2 5755 conventional types that are constructed on the fly. */
14f9c5c9
AS
5756
5757/* There is a subtle and tricky problem here. In general, we cannot
5758 determine the size of dynamic records without its data. However,
5759 the 'struct value' data structure, which GDB uses to represent
5760 quantities in the inferior process (the target), requires the size
5761 of the type at the time of its allocation in order to reserve space
5762 for GDB's internal copy of the data. That's why the
5763 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 5764 rather than struct value*s.
14f9c5c9
AS
5765
5766 However, GDB's internal history variables ($1, $2, etc.) are
5767 struct value*s containing internal copies of the data that are not, in
5768 general, the same as the data at their corresponding addresses in
5769 the target. Fortunately, the types we give to these values are all
5770 conventional, fixed-size types (as per the strategy described
5771 above), so that we don't usually have to perform the
5772 'to_fixed_xxx_type' conversions to look at their values.
5773 Unfortunately, there is one exception: if one of the internal
5774 history variables is an array whose elements are unconstrained
5775 records, then we will need to create distinct fixed types for each
5776 element selected. */
5777
5778/* The upshot of all of this is that many routines take a (type, host
5779 address, target address) triple as arguments to represent a value.
5780 The host address, if non-null, is supposed to contain an internal
5781 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 5782 target at the target address. */
14f9c5c9
AS
5783
5784/* Assuming that VAL0 represents a pointer value, the result of
5785 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 5786 dynamic-sized types. */
14f9c5c9 5787
d2e4a39e
AS
5788struct value *
5789ada_value_ind (struct value *val0)
14f9c5c9 5790{
d2e4a39e 5791 struct value *val = unwrap_value (value_ind (val0));
4c4b4cd2 5792 return ada_to_fixed_value (val);
14f9c5c9
AS
5793}
5794
5795/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
5796 qualifiers on VAL0. */
5797
d2e4a39e
AS
5798static struct value *
5799ada_coerce_ref (struct value *val0)
5800{
df407dfe 5801 if (TYPE_CODE (value_type (val0)) == TYPE_CODE_REF)
d2e4a39e
AS
5802 {
5803 struct value *val = val0;
994b9211 5804 val = coerce_ref (val);
d2e4a39e 5805 val = unwrap_value (val);
4c4b4cd2 5806 return ada_to_fixed_value (val);
d2e4a39e
AS
5807 }
5808 else
14f9c5c9
AS
5809 return val0;
5810}
5811
5812/* Return OFF rounded upward if necessary to a multiple of
4c4b4cd2 5813 ALIGNMENT (a power of 2). */
14f9c5c9
AS
5814
5815static unsigned int
ebf56fd3 5816align_value (unsigned int off, unsigned int alignment)
14f9c5c9
AS
5817{
5818 return (off + alignment - 1) & ~(alignment - 1);
5819}
5820
4c4b4cd2 5821/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
5822
5823static unsigned int
ebf56fd3 5824field_alignment (struct type *type, int f)
14f9c5c9 5825{
d2e4a39e 5826 const char *name = TYPE_FIELD_NAME (type, f);
14f9c5c9
AS
5827 int len = (name == NULL) ? 0 : strlen (name);
5828 int align_offset;
5829
4c4b4cd2
PH
5830 if (!isdigit (name[len - 1]))
5831 return 1;
14f9c5c9 5832
d2e4a39e 5833 if (isdigit (name[len - 2]))
14f9c5c9
AS
5834 align_offset = len - 2;
5835 else
5836 align_offset = len - 1;
5837
4c4b4cd2 5838 if (align_offset < 7 || strncmp ("___XV", name + align_offset - 6, 5) != 0)
14f9c5c9
AS
5839 return TARGET_CHAR_BIT;
5840
4c4b4cd2
PH
5841 return atoi (name + align_offset) * TARGET_CHAR_BIT;
5842}
5843
5844/* Find a symbol named NAME. Ignores ambiguity. */
5845
5846struct symbol *
5847ada_find_any_symbol (const char *name)
5848{
5849 struct symbol *sym;
5850
5851 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
5852 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
5853 return sym;
5854
5855 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
5856 return sym;
14f9c5c9
AS
5857}
5858
5859/* Find a type named NAME. Ignores ambiguity. */
4c4b4cd2 5860
d2e4a39e 5861struct type *
ebf56fd3 5862ada_find_any_type (const char *name)
14f9c5c9 5863{
4c4b4cd2 5864 struct symbol *sym = ada_find_any_symbol (name);
14f9c5c9 5865
14f9c5c9
AS
5866 if (sym != NULL)
5867 return SYMBOL_TYPE (sym);
5868
5869 return NULL;
5870}
5871
4c4b4cd2
PH
5872/* Given a symbol NAME and its associated BLOCK, search all symbols
5873 for its ___XR counterpart, which is the ``renaming'' symbol
5874 associated to NAME. Return this symbol if found, return
5875 NULL otherwise. */
5876
5877struct symbol *
5878ada_find_renaming_symbol (const char *name, struct block *block)
5879{
5880 const struct symbol *function_sym = block_function (block);
5881 char *rename;
5882
5883 if (function_sym != NULL)
5884 {
5885 /* If the symbol is defined inside a function, NAME is not fully
5886 qualified. This means we need to prepend the function name
5887 as well as adding the ``___XR'' suffix to build the name of
5888 the associated renaming symbol. */
5889 char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
5890 const int function_name_len = strlen (function_name);
76a01679
JB
5891 const int rename_len = function_name_len + 2 /* "__" */
5892 + strlen (name) + 6 /* "___XR\0" */ ;
4c4b4cd2
PH
5893
5894 /* Library-level functions are a special case, as GNAT adds
5895 a ``_ada_'' prefix to the function name to avoid namespace
5896 pollution. However, the renaming symbol themselves do not
5897 have this prefix, so we need to skip this prefix if present. */
5898 if (function_name_len > 5 /* "_ada_" */
5899 && strstr (function_name, "_ada_") == function_name)
5900 function_name = function_name + 5;
5901
5902 rename = (char *) alloca (rename_len * sizeof (char));
5903 sprintf (rename, "%s__%s___XR", function_name, name);
5904 }
5905 else
5906 {
5907 const int rename_len = strlen (name) + 6;
5908 rename = (char *) alloca (rename_len * sizeof (char));
5909 sprintf (rename, "%s___XR", name);
5910 }
5911
5912 return ada_find_any_symbol (rename);
5913}
5914
14f9c5c9 5915/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 5916 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 5917 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
5918 otherwise return 0. */
5919
14f9c5c9 5920int
d2e4a39e 5921ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
5922{
5923 if (type1 == NULL)
5924 return 1;
5925 else if (type0 == NULL)
5926 return 0;
5927 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
5928 return 1;
5929 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
5930 return 0;
4c4b4cd2
PH
5931 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
5932 return 1;
14f9c5c9
AS
5933 else if (ada_is_packed_array_type (type0))
5934 return 1;
4c4b4cd2
PH
5935 else if (ada_is_array_descriptor_type (type0)
5936 && !ada_is_array_descriptor_type (type1))
14f9c5c9 5937 return 1;
d2e4a39e 5938 else if (ada_renaming_type (type0) != NULL
4c4b4cd2 5939 && ada_renaming_type (type1) == NULL)
14f9c5c9
AS
5940 return 1;
5941 return 0;
5942}
5943
5944/* The name of TYPE, which is either its TYPE_NAME, or, if that is
4c4b4cd2
PH
5945 null, its TYPE_TAG_NAME. Null if TYPE is null. */
5946
d2e4a39e
AS
5947char *
5948ada_type_name (struct type *type)
14f9c5c9 5949{
d2e4a39e 5950 if (type == NULL)
14f9c5c9
AS
5951 return NULL;
5952 else if (TYPE_NAME (type) != NULL)
5953 return TYPE_NAME (type);
5954 else
5955 return TYPE_TAG_NAME (type);
5956}
5957
5958/* Find a parallel type to TYPE whose name is formed by appending
4c4b4cd2 5959 SUFFIX to the name of TYPE. */
14f9c5c9 5960
d2e4a39e 5961struct type *
ebf56fd3 5962ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 5963{
d2e4a39e 5964 static char *name;
14f9c5c9 5965 static size_t name_len = 0;
14f9c5c9 5966 int len;
d2e4a39e
AS
5967 char *typename = ada_type_name (type);
5968
14f9c5c9
AS
5969 if (typename == NULL)
5970 return NULL;
5971
5972 len = strlen (typename);
5973
d2e4a39e 5974 GROW_VECT (name, name_len, len + strlen (suffix) + 1);
14f9c5c9
AS
5975
5976 strcpy (name, typename);
5977 strcpy (name + len, suffix);
5978
5979 return ada_find_any_type (name);
5980}
5981
5982
5983/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 5984 type describing its fields. Otherwise, return NULL. */
14f9c5c9 5985
d2e4a39e
AS
5986static struct type *
5987dynamic_template_type (struct type *type)
14f9c5c9 5988{
61ee279c 5989 type = ada_check_typedef (type);
14f9c5c9
AS
5990
5991 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
d2e4a39e 5992 || ada_type_name (type) == NULL)
14f9c5c9 5993 return NULL;
d2e4a39e 5994 else
14f9c5c9
AS
5995 {
5996 int len = strlen (ada_type_name (type));
4c4b4cd2
PH
5997 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
5998 return type;
14f9c5c9 5999 else
4c4b4cd2 6000 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
6001 }
6002}
6003
6004/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 6005 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 6006
d2e4a39e
AS
6007static int
6008is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
6009{
6010 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
d2e4a39e 6011 return name != NULL
14f9c5c9
AS
6012 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
6013 && strstr (name, "___XVL") != NULL;
6014}
6015
4c4b4cd2
PH
6016/* The index of the variant field of TYPE, or -1 if TYPE does not
6017 represent a variant record type. */
14f9c5c9 6018
d2e4a39e 6019static int
4c4b4cd2 6020variant_field_index (struct type *type)
14f9c5c9
AS
6021{
6022 int f;
6023
4c4b4cd2
PH
6024 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
6025 return -1;
6026
6027 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
6028 {
6029 if (ada_is_variant_part (type, f))
6030 return f;
6031 }
6032 return -1;
14f9c5c9
AS
6033}
6034
4c4b4cd2
PH
6035/* A record type with no fields. */
6036
d2e4a39e
AS
6037static struct type *
6038empty_record (struct objfile *objfile)
14f9c5c9 6039{
d2e4a39e 6040 struct type *type = alloc_type (objfile);
14f9c5c9
AS
6041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6042 TYPE_NFIELDS (type) = 0;
6043 TYPE_FIELDS (type) = NULL;
6044 TYPE_NAME (type) = "<empty>";
6045 TYPE_TAG_NAME (type) = NULL;
6046 TYPE_FLAGS (type) = 0;
6047 TYPE_LENGTH (type) = 0;
6048 return type;
6049}
6050
6051/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
6052 the value of type TYPE at VALADDR or ADDRESS (see comments at
6053 the beginning of this section) VAL according to GNAT conventions.
6054 DVAL0 should describe the (portion of a) record that contains any
df407dfe 6055 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
6056 an outer-level type (i.e., as opposed to a branch of a variant.) A
6057 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 6058 of the variant.
14f9c5c9 6059
4c4b4cd2
PH
6060 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
6061 length are not statically known are discarded. As a consequence,
6062 VALADDR, ADDRESS and DVAL0 are ignored.
6063
6064 NOTE: Limitations: For now, we assume that dynamic fields and
6065 variants occupy whole numbers of bytes. However, they need not be
6066 byte-aligned. */
6067
6068struct type *
10a2c479 6069ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 6070 const gdb_byte *valaddr,
4c4b4cd2
PH
6071 CORE_ADDR address, struct value *dval0,
6072 int keep_dynamic_fields)
14f9c5c9 6073{
d2e4a39e
AS
6074 struct value *mark = value_mark ();
6075 struct value *dval;
6076 struct type *rtype;
14f9c5c9 6077 int nfields, bit_len;
4c4b4cd2 6078 int variant_field;
14f9c5c9 6079 long off;
4c4b4cd2 6080 int fld_bit_len, bit_incr;
14f9c5c9
AS
6081 int f;
6082
4c4b4cd2
PH
6083 /* Compute the number of fields in this record type that are going
6084 to be processed: unless keep_dynamic_fields, this includes only
6085 fields whose position and length are static will be processed. */
6086 if (keep_dynamic_fields)
6087 nfields = TYPE_NFIELDS (type);
6088 else
6089 {
6090 nfields = 0;
76a01679 6091 while (nfields < TYPE_NFIELDS (type)
4c4b4cd2
PH
6092 && !ada_is_variant_part (type, nfields)
6093 && !is_dynamic_field (type, nfields))
6094 nfields++;
6095 }
6096
14f9c5c9
AS
6097 rtype = alloc_type (TYPE_OBJFILE (type));
6098 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
6099 INIT_CPLUS_SPECIFIC (rtype);
6100 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e 6101 TYPE_FIELDS (rtype) = (struct field *)
14f9c5c9
AS
6102 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
6103 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
6104 TYPE_NAME (rtype) = ada_type_name (type);
6105 TYPE_TAG_NAME (rtype) = NULL;
4c4b4cd2 6106 TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9 6107
d2e4a39e
AS
6108 off = 0;
6109 bit_len = 0;
4c4b4cd2
PH
6110 variant_field = -1;
6111
14f9c5c9
AS
6112 for (f = 0; f < nfields; f += 1)
6113 {
6c038f32
PH
6114 off = align_value (off, field_alignment (type, f))
6115 + TYPE_FIELD_BITPOS (type, f);
14f9c5c9 6116 TYPE_FIELD_BITPOS (rtype, f) = off;
d2e4a39e 6117 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 6118
d2e4a39e 6119 if (ada_is_variant_part (type, f))
4c4b4cd2
PH
6120 {
6121 variant_field = f;
6122 fld_bit_len = bit_incr = 0;
6123 }
14f9c5c9 6124 else if (is_dynamic_field (type, f))
4c4b4cd2
PH
6125 {
6126 if (dval0 == NULL)
6127 dval = value_from_contents_and_address (rtype, valaddr, address);
6128 else
6129 dval = dval0;
6130
6131 TYPE_FIELD_TYPE (rtype, f) =
6132 ada_to_fixed_type
6133 (ada_get_base_type
6134 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f))),
6135 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
6136 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
6137 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
6138 bit_incr = fld_bit_len =
6139 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
6140 }
14f9c5c9 6141 else
4c4b4cd2
PH
6142 {
6143 TYPE_FIELD_TYPE (rtype, f) = TYPE_FIELD_TYPE (type, f);
6144 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
6145 if (TYPE_FIELD_BITSIZE (type, f) > 0)
6146 bit_incr = fld_bit_len =
6147 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
6148 else
6149 bit_incr = fld_bit_len =
6150 TYPE_LENGTH (TYPE_FIELD_TYPE (type, f)) * TARGET_CHAR_BIT;
6151 }
14f9c5c9 6152 if (off + fld_bit_len > bit_len)
4c4b4cd2 6153 bit_len = off + fld_bit_len;
14f9c5c9 6154 off += bit_incr;
4c4b4cd2
PH
6155 TYPE_LENGTH (rtype) =
6156 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 6157 }
4c4b4cd2
PH
6158
6159 /* We handle the variant part, if any, at the end because of certain
6160 odd cases in which it is re-ordered so as NOT the last field of
6161 the record. This can happen in the presence of representation
6162 clauses. */
6163 if (variant_field >= 0)
6164 {
6165 struct type *branch_type;
6166
6167 off = TYPE_FIELD_BITPOS (rtype, variant_field);
6168
6169 if (dval0 == NULL)
6170 dval = value_from_contents_and_address (rtype, valaddr, address);
6171 else
6172 dval = dval0;
6173
6174 branch_type =
6175 to_fixed_variant_branch_type
6176 (TYPE_FIELD_TYPE (type, variant_field),
6177 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
6178 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
6179 if (branch_type == NULL)
6180 {
6181 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
6182 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
6183 TYPE_NFIELDS (rtype) -= 1;
6184 }
6185 else
6186 {
6187 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
6188 TYPE_FIELD_NAME (rtype, variant_field) = "S";
6189 fld_bit_len =
6190 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
6191 TARGET_CHAR_BIT;
6192 if (off + fld_bit_len > bit_len)
6193 bit_len = off + fld_bit_len;
6194 TYPE_LENGTH (rtype) =
6195 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
6196 }
6197 }
6198
714e53ab
PH
6199 /* According to exp_dbug.ads, the size of TYPE for variable-size records
6200 should contain the alignment of that record, which should be a strictly
6201 positive value. If null or negative, then something is wrong, most
6202 probably in the debug info. In that case, we don't round up the size
6203 of the resulting type. If this record is not part of another structure,
6204 the current RTYPE length might be good enough for our purposes. */
6205 if (TYPE_LENGTH (type) <= 0)
6206 {
323e0a4a
AC
6207 if (TYPE_NAME (rtype))
6208 warning (_("Invalid type size for `%s' detected: %d."),
6209 TYPE_NAME (rtype), TYPE_LENGTH (type));
6210 else
6211 warning (_("Invalid type size for <unnamed> detected: %d."),
6212 TYPE_LENGTH (type));
714e53ab
PH
6213 }
6214 else
6215 {
6216 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype),
6217 TYPE_LENGTH (type));
6218 }
14f9c5c9
AS
6219
6220 value_free_to_mark (mark);
d2e4a39e 6221 if (TYPE_LENGTH (rtype) > varsize_limit)
323e0a4a 6222 error (_("record type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
6223 return rtype;
6224}
6225
4c4b4cd2
PH
6226/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
6227 of 1. */
14f9c5c9 6228
d2e4a39e 6229static struct type *
fc1a4b47 6230template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
4c4b4cd2
PH
6231 CORE_ADDR address, struct value *dval0)
6232{
6233 return ada_template_to_fixed_record_type_1 (type, valaddr,
6234 address, dval0, 1);
6235}
6236
6237/* An ordinary record type in which ___XVL-convention fields and
6238 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
6239 static approximations, containing all possible fields. Uses
6240 no runtime values. Useless for use in values, but that's OK,
6241 since the results are used only for type determinations. Works on both
6242 structs and unions. Representation note: to save space, we memorize
6243 the result of this function in the TYPE_TARGET_TYPE of the
6244 template type. */
6245
6246static struct type *
6247template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
6248{
6249 struct type *type;
6250 int nfields;
6251 int f;
6252
4c4b4cd2
PH
6253 if (TYPE_TARGET_TYPE (type0) != NULL)
6254 return TYPE_TARGET_TYPE (type0);
6255
6256 nfields = TYPE_NFIELDS (type0);
6257 type = type0;
14f9c5c9
AS
6258
6259 for (f = 0; f < nfields; f += 1)
6260 {
61ee279c 6261 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type0, f));
4c4b4cd2 6262 struct type *new_type;
14f9c5c9 6263
4c4b4cd2
PH
6264 if (is_dynamic_field (type0, f))
6265 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
14f9c5c9 6266 else
4c4b4cd2
PH
6267 new_type = to_static_fixed_type (field_type);
6268 if (type == type0 && new_type != field_type)
6269 {
6270 TYPE_TARGET_TYPE (type0) = type = alloc_type (TYPE_OBJFILE (type0));
6271 TYPE_CODE (type) = TYPE_CODE (type0);
6272 INIT_CPLUS_SPECIFIC (type);
6273 TYPE_NFIELDS (type) = nfields;
6274 TYPE_FIELDS (type) = (struct field *)
6275 TYPE_ALLOC (type, nfields * sizeof (struct field));
6276 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
6277 sizeof (struct field) * nfields);
6278 TYPE_NAME (type) = ada_type_name (type0);
6279 TYPE_TAG_NAME (type) = NULL;
6280 TYPE_FLAGS (type) |= TYPE_FLAG_FIXED_INSTANCE;
6281 TYPE_LENGTH (type) = 0;
6282 }
6283 TYPE_FIELD_TYPE (type, f) = new_type;
6284 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
14f9c5c9 6285 }
14f9c5c9
AS
6286 return type;
6287}
6288
4c4b4cd2
PH
6289/* Given an object of type TYPE whose contents are at VALADDR and
6290 whose address in memory is ADDRESS, returns a revision of TYPE --
6291 a non-dynamic-sized record with a variant part -- in which
6292 the variant part is replaced with the appropriate branch. Looks
6293 for discriminant values in DVAL0, which can be NULL if the record
6294 contains the necessary discriminant values. */
6295
d2e4a39e 6296static struct type *
fc1a4b47 6297to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
4c4b4cd2 6298 CORE_ADDR address, struct value *dval0)
14f9c5c9 6299{
d2e4a39e 6300 struct value *mark = value_mark ();
4c4b4cd2 6301 struct value *dval;
d2e4a39e 6302 struct type *rtype;
14f9c5c9
AS
6303 struct type *branch_type;
6304 int nfields = TYPE_NFIELDS (type);
4c4b4cd2 6305 int variant_field = variant_field_index (type);
14f9c5c9 6306
4c4b4cd2 6307 if (variant_field == -1)
14f9c5c9
AS
6308 return type;
6309
4c4b4cd2
PH
6310 if (dval0 == NULL)
6311 dval = value_from_contents_and_address (type, valaddr, address);
6312 else
6313 dval = dval0;
6314
14f9c5c9
AS
6315 rtype = alloc_type (TYPE_OBJFILE (type));
6316 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
4c4b4cd2
PH
6317 INIT_CPLUS_SPECIFIC (rtype);
6318 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e
AS
6319 TYPE_FIELDS (rtype) =
6320 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
6321 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
4c4b4cd2 6322 sizeof (struct field) * nfields);
14f9c5c9
AS
6323 TYPE_NAME (rtype) = ada_type_name (type);
6324 TYPE_TAG_NAME (rtype) = NULL;
4c4b4cd2 6325 TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
6326 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
6327
4c4b4cd2
PH
6328 branch_type = to_fixed_variant_branch_type
6329 (TYPE_FIELD_TYPE (type, variant_field),
d2e4a39e 6330 cond_offset_host (valaddr,
4c4b4cd2
PH
6331 TYPE_FIELD_BITPOS (type, variant_field)
6332 / TARGET_CHAR_BIT),
d2e4a39e 6333 cond_offset_target (address,
4c4b4cd2
PH
6334 TYPE_FIELD_BITPOS (type, variant_field)
6335 / TARGET_CHAR_BIT), dval);
d2e4a39e 6336 if (branch_type == NULL)
14f9c5c9 6337 {
4c4b4cd2
PH
6338 int f;
6339 for (f = variant_field + 1; f < nfields; f += 1)
6340 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
14f9c5c9 6341 TYPE_NFIELDS (rtype) -= 1;
14f9c5c9
AS
6342 }
6343 else
6344 {
4c4b4cd2
PH
6345 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
6346 TYPE_FIELD_NAME (rtype, variant_field) = "S";
6347 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 6348 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 6349 }
4c4b4cd2 6350 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
d2e4a39e 6351
4c4b4cd2 6352 value_free_to_mark (mark);
14f9c5c9
AS
6353 return rtype;
6354}
6355
6356/* An ordinary record type (with fixed-length fields) that describes
6357 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
6358 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
6359 should be in DVAL, a record value; it may be NULL if the object
6360 at ADDR itself contains any necessary discriminant values.
6361 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
6362 values from the record are needed. Except in the case that DVAL,
6363 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
6364 unchecked) is replaced by a particular branch of the variant.
6365
6366 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
6367 is questionable and may be removed. It can arise during the
6368 processing of an unconstrained-array-of-record type where all the
6369 variant branches have exactly the same size. This is because in
6370 such cases, the compiler does not bother to use the XVS convention
6371 when encoding the record. I am currently dubious of this
6372 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 6373
d2e4a39e 6374static struct type *
fc1a4b47 6375to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
4c4b4cd2 6376 CORE_ADDR address, struct value *dval)
14f9c5c9 6377{
d2e4a39e 6378 struct type *templ_type;
14f9c5c9 6379
4c4b4cd2
PH
6380 if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
6381 return type0;
6382
d2e4a39e 6383 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
6384
6385 if (templ_type != NULL)
6386 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
6387 else if (variant_field_index (type0) >= 0)
6388 {
6389 if (dval == NULL && valaddr == NULL && address == 0)
6390 return type0;
6391 return to_record_with_fixed_variant_part (type0, valaddr, address,
6392 dval);
6393 }
14f9c5c9
AS
6394 else
6395 {
4c4b4cd2 6396 TYPE_FLAGS (type0) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
6397 return type0;
6398 }
6399
6400}
6401
6402/* An ordinary record type (with fixed-length fields) that describes
6403 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
6404 union type. Any necessary discriminants' values should be in DVAL,
6405 a record value. That is, this routine selects the appropriate
6406 branch of the union at ADDR according to the discriminant value
4c4b4cd2 6407 indicated in the union's type name. */
14f9c5c9 6408
d2e4a39e 6409static struct type *
fc1a4b47 6410to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
4c4b4cd2 6411 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
6412{
6413 int which;
d2e4a39e
AS
6414 struct type *templ_type;
6415 struct type *var_type;
14f9c5c9
AS
6416
6417 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
6418 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 6419 else
14f9c5c9
AS
6420 var_type = var_type0;
6421
6422 templ_type = ada_find_parallel_type (var_type, "___XVU");
6423
6424 if (templ_type != NULL)
6425 var_type = templ_type;
6426
d2e4a39e
AS
6427 which =
6428 ada_which_variant_applies (var_type,
0fd88904 6429 value_type (dval), value_contents (dval));
14f9c5c9
AS
6430
6431 if (which < 0)
6432 return empty_record (TYPE_OBJFILE (var_type));
6433 else if (is_dynamic_field (var_type, which))
4c4b4cd2 6434 return to_fixed_record_type
d2e4a39e
AS
6435 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
6436 valaddr, address, dval);
4c4b4cd2 6437 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
d2e4a39e
AS
6438 return
6439 to_fixed_record_type
6440 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
14f9c5c9
AS
6441 else
6442 return TYPE_FIELD_TYPE (var_type, which);
6443}
6444
6445/* Assuming that TYPE0 is an array type describing the type of a value
6446 at ADDR, and that DVAL describes a record containing any
6447 discriminants used in TYPE0, returns a type for the value that
6448 contains no dynamic components (that is, no components whose sizes
6449 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
6450 true, gives an error message if the resulting type's size is over
4c4b4cd2 6451 varsize_limit. */
14f9c5c9 6452
d2e4a39e
AS
6453static struct type *
6454to_fixed_array_type (struct type *type0, struct value *dval,
4c4b4cd2 6455 int ignore_too_big)
14f9c5c9 6456{
d2e4a39e
AS
6457 struct type *index_type_desc;
6458 struct type *result;
14f9c5c9 6459
4c4b4cd2
PH
6460 if (ada_is_packed_array_type (type0) /* revisit? */
6461 || (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE))
6462 return type0;
14f9c5c9
AS
6463
6464 index_type_desc = ada_find_parallel_type (type0, "___XA");
6465 if (index_type_desc == NULL)
6466 {
61ee279c 6467 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
14f9c5c9 6468 /* NOTE: elt_type---the fixed version of elt_type0---should never
4c4b4cd2
PH
6469 depend on the contents of the array in properly constructed
6470 debugging data. */
d2e4a39e 6471 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval);
14f9c5c9
AS
6472
6473 if (elt_type0 == elt_type)
4c4b4cd2 6474 result = type0;
14f9c5c9 6475 else
4c4b4cd2
PH
6476 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
6477 elt_type, TYPE_INDEX_TYPE (type0));
14f9c5c9
AS
6478 }
6479 else
6480 {
6481 int i;
6482 struct type *elt_type0;
6483
6484 elt_type0 = type0;
6485 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
4c4b4cd2 6486 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
6487
6488 /* NOTE: result---the fixed version of elt_type0---should never
4c4b4cd2
PH
6489 depend on the contents of the array in properly constructed
6490 debugging data. */
61ee279c 6491 result = ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval);
14f9c5c9 6492 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
4c4b4cd2
PH
6493 {
6494 struct type *range_type =
6495 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, i),
6496 dval, TYPE_OBJFILE (type0));
6497 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
6498 result, range_type);
6499 }
d2e4a39e 6500 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
323e0a4a 6501 error (_("array type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
6502 }
6503
4c4b4cd2 6504 TYPE_FLAGS (result) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9 6505 return result;
d2e4a39e 6506}
14f9c5c9
AS
6507
6508
6509/* A standard type (containing no dynamically sized components)
6510 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
6511 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2
PH
6512 and may be NULL if there are none, or if the object of type TYPE at
6513 ADDRESS or in VALADDR contains these discriminants. */
14f9c5c9 6514
d2e4a39e 6515struct type *
fc1a4b47 6516ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
4c4b4cd2 6517 CORE_ADDR address, struct value *dval)
14f9c5c9 6518{
61ee279c 6519 type = ada_check_typedef (type);
d2e4a39e
AS
6520 switch (TYPE_CODE (type))
6521 {
6522 default:
14f9c5c9 6523 return type;
d2e4a39e 6524 case TYPE_CODE_STRUCT:
4c4b4cd2 6525 {
76a01679
JB
6526 struct type *static_type = to_static_fixed_type (type);
6527 if (ada_is_tagged_type (static_type, 0))
6528 {
6529 struct type *real_type =
6530 type_from_tag (value_tag_from_contents_and_address (static_type,
6531 valaddr,
6532 address));
6533 if (real_type != NULL)
6534 type = real_type;
6535 }
6536 return to_fixed_record_type (type, valaddr, address, NULL);
4c4b4cd2 6537 }
d2e4a39e 6538 case TYPE_CODE_ARRAY:
4c4b4cd2 6539 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
6540 case TYPE_CODE_UNION:
6541 if (dval == NULL)
4c4b4cd2 6542 return type;
d2e4a39e 6543 else
4c4b4cd2 6544 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 6545 }
14f9c5c9
AS
6546}
6547
6548/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 6549 TYPE0, but based on no runtime data. */
14f9c5c9 6550
d2e4a39e
AS
6551static struct type *
6552to_static_fixed_type (struct type *type0)
14f9c5c9 6553{
d2e4a39e 6554 struct type *type;
14f9c5c9
AS
6555
6556 if (type0 == NULL)
6557 return NULL;
6558
4c4b4cd2
PH
6559 if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
6560 return type0;
6561
61ee279c 6562 type0 = ada_check_typedef (type0);
d2e4a39e 6563
14f9c5c9
AS
6564 switch (TYPE_CODE (type0))
6565 {
6566 default:
6567 return type0;
6568 case TYPE_CODE_STRUCT:
6569 type = dynamic_template_type (type0);
d2e4a39e 6570 if (type != NULL)
4c4b4cd2
PH
6571 return template_to_static_fixed_type (type);
6572 else
6573 return template_to_static_fixed_type (type0);
14f9c5c9
AS
6574 case TYPE_CODE_UNION:
6575 type = ada_find_parallel_type (type0, "___XVU");
6576 if (type != NULL)
4c4b4cd2
PH
6577 return template_to_static_fixed_type (type);
6578 else
6579 return template_to_static_fixed_type (type0);
14f9c5c9
AS
6580 }
6581}
6582
4c4b4cd2
PH
6583/* A static approximation of TYPE with all type wrappers removed. */
6584
d2e4a39e
AS
6585static struct type *
6586static_unwrap_type (struct type *type)
14f9c5c9
AS
6587{
6588 if (ada_is_aligner_type (type))
6589 {
61ee279c 6590 struct type *type1 = TYPE_FIELD_TYPE (ada_check_typedef (type), 0);
14f9c5c9 6591 if (ada_type_name (type1) == NULL)
4c4b4cd2 6592 TYPE_NAME (type1) = ada_type_name (type);
14f9c5c9
AS
6593
6594 return static_unwrap_type (type1);
6595 }
d2e4a39e 6596 else
14f9c5c9 6597 {
d2e4a39e
AS
6598 struct type *raw_real_type = ada_get_base_type (type);
6599 if (raw_real_type == type)
4c4b4cd2 6600 return type;
14f9c5c9 6601 else
4c4b4cd2 6602 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
6603 }
6604}
6605
6606/* In some cases, incomplete and private types require
4c4b4cd2 6607 cross-references that are not resolved as records (for example,
14f9c5c9
AS
6608 type Foo;
6609 type FooP is access Foo;
6610 V: FooP;
6611 type Foo is array ...;
4c4b4cd2 6612 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
6613 cross-references to such types, we instead substitute for FooP a
6614 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 6615 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
6616
6617/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
6618 exists, otherwise TYPE. */
6619
d2e4a39e 6620struct type *
61ee279c 6621ada_check_typedef (struct type *type)
14f9c5c9
AS
6622{
6623 CHECK_TYPEDEF (type);
6624 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
6625 || (TYPE_FLAGS (type) & TYPE_FLAG_STUB) == 0
6626 || TYPE_TAG_NAME (type) == NULL)
6627 return type;
d2e4a39e 6628 else
14f9c5c9 6629 {
d2e4a39e
AS
6630 char *name = TYPE_TAG_NAME (type);
6631 struct type *type1 = ada_find_any_type (name);
14f9c5c9
AS
6632 return (type1 == NULL) ? type : type1;
6633 }
6634}
6635
6636/* A value representing the data at VALADDR/ADDRESS as described by
6637 type TYPE0, but with a standard (static-sized) type that correctly
6638 describes it. If VAL0 is not NULL and TYPE0 already is a standard
6639 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 6640 creation of struct values]. */
14f9c5c9 6641
4c4b4cd2
PH
6642static struct value *
6643ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
6644 struct value *val0)
14f9c5c9 6645{
4c4b4cd2 6646 struct type *type = ada_to_fixed_type (type0, 0, address, NULL);
14f9c5c9
AS
6647 if (type == type0 && val0 != NULL)
6648 return val0;
d2e4a39e 6649 else
4c4b4cd2
PH
6650 return value_from_contents_and_address (type, 0, address);
6651}
6652
6653/* A value representing VAL, but with a standard (static-sized) type
6654 that correctly describes it. Does not necessarily create a new
6655 value. */
6656
6657static struct value *
6658ada_to_fixed_value (struct value *val)
6659{
df407dfe
AC
6660 return ada_to_fixed_value_create (value_type (val),
6661 VALUE_ADDRESS (val) + value_offset (val),
4c4b4cd2 6662 val);
14f9c5c9
AS
6663}
6664
4c4b4cd2 6665/* A value representing VAL, but with a standard (static-sized) type
14f9c5c9
AS
6666 chosen to approximate the real type of VAL as well as possible, but
6667 without consulting any runtime values. For Ada dynamic-sized
4c4b4cd2 6668 types, therefore, the type of the result is likely to be inaccurate. */
14f9c5c9 6669
d2e4a39e
AS
6670struct value *
6671ada_to_static_fixed_value (struct value *val)
14f9c5c9 6672{
d2e4a39e 6673 struct type *type =
df407dfe
AC
6674 to_static_fixed_type (static_unwrap_type (value_type (val)));
6675 if (type == value_type (val))
14f9c5c9
AS
6676 return val;
6677 else
4c4b4cd2 6678 return coerce_unspec_val_to_type (val, type);
14f9c5c9 6679}
d2e4a39e 6680\f
14f9c5c9 6681
14f9c5c9
AS
6682/* Attributes */
6683
4c4b4cd2
PH
6684/* Table mapping attribute numbers to names.
6685 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 6686
d2e4a39e 6687static const char *attribute_names[] = {
14f9c5c9
AS
6688 "<?>",
6689
d2e4a39e 6690 "first",
14f9c5c9
AS
6691 "last",
6692 "length",
6693 "image",
14f9c5c9
AS
6694 "max",
6695 "min",
4c4b4cd2
PH
6696 "modulus",
6697 "pos",
6698 "size",
6699 "tag",
14f9c5c9 6700 "val",
14f9c5c9
AS
6701 0
6702};
6703
d2e4a39e 6704const char *
4c4b4cd2 6705ada_attribute_name (enum exp_opcode n)
14f9c5c9 6706{
4c4b4cd2
PH
6707 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
6708 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
6709 else
6710 return attribute_names[0];
6711}
6712
4c4b4cd2 6713/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 6714
4c4b4cd2
PH
6715static LONGEST
6716pos_atr (struct value *arg)
14f9c5c9 6717{
df407dfe 6718 struct type *type = value_type (arg);
14f9c5c9 6719
d2e4a39e 6720 if (!discrete_type_p (type))
323e0a4a 6721 error (_("'POS only defined on discrete types"));
14f9c5c9
AS
6722
6723 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
6724 {
6725 int i;
6726 LONGEST v = value_as_long (arg);
6727
d2e4a39e 6728 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
4c4b4cd2
PH
6729 {
6730 if (v == TYPE_FIELD_BITPOS (type, i))
6731 return i;
6732 }
323e0a4a 6733 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9
AS
6734 }
6735 else
4c4b4cd2
PH
6736 return value_as_long (arg);
6737}
6738
6739static struct value *
6740value_pos_atr (struct value *arg)
6741{
72d5681a 6742 return value_from_longest (builtin_type_int, pos_atr (arg));
14f9c5c9
AS
6743}
6744
4c4b4cd2 6745/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 6746
d2e4a39e
AS
6747static struct value *
6748value_val_atr (struct type *type, struct value *arg)
14f9c5c9 6749{
d2e4a39e 6750 if (!discrete_type_p (type))
323e0a4a 6751 error (_("'VAL only defined on discrete types"));
df407dfe 6752 if (!integer_type_p (value_type (arg)))
323e0a4a 6753 error (_("'VAL requires integral argument"));
14f9c5c9
AS
6754
6755 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
6756 {
6757 long pos = value_as_long (arg);
6758 if (pos < 0 || pos >= TYPE_NFIELDS (type))
323e0a4a 6759 error (_("argument to 'VAL out of range"));
d2e4a39e 6760 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
14f9c5c9
AS
6761 }
6762 else
6763 return value_from_longest (type, value_as_long (arg));
6764}
14f9c5c9 6765\f
d2e4a39e 6766
4c4b4cd2 6767 /* Evaluation */
14f9c5c9 6768
4c4b4cd2
PH
6769/* True if TYPE appears to be an Ada character type.
6770 [At the moment, this is true only for Character and Wide_Character;
6771 It is a heuristic test that could stand improvement]. */
14f9c5c9 6772
d2e4a39e
AS
6773int
6774ada_is_character_type (struct type *type)
14f9c5c9 6775{
d2e4a39e
AS
6776 const char *name = ada_type_name (type);
6777 return
14f9c5c9 6778 name != NULL
d2e4a39e 6779 && (TYPE_CODE (type) == TYPE_CODE_CHAR
4c4b4cd2
PH
6780 || TYPE_CODE (type) == TYPE_CODE_INT
6781 || TYPE_CODE (type) == TYPE_CODE_RANGE)
6782 && (strcmp (name, "character") == 0
6783 || strcmp (name, "wide_character") == 0
6784 || strcmp (name, "unsigned char") == 0);
14f9c5c9
AS
6785}
6786
4c4b4cd2 6787/* True if TYPE appears to be an Ada string type. */
14f9c5c9
AS
6788
6789int
ebf56fd3 6790ada_is_string_type (struct type *type)
14f9c5c9 6791{
61ee279c 6792 type = ada_check_typedef (type);
d2e4a39e 6793 if (type != NULL
14f9c5c9 6794 && TYPE_CODE (type) != TYPE_CODE_PTR
76a01679
JB
6795 && (ada_is_simple_array_type (type)
6796 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
6797 && ada_array_arity (type) == 1)
6798 {
6799 struct type *elttype = ada_array_element_type (type, 1);
6800
6801 return ada_is_character_type (elttype);
6802 }
d2e4a39e 6803 else
14f9c5c9
AS
6804 return 0;
6805}
6806
6807
6808/* True if TYPE is a struct type introduced by the compiler to force the
6809 alignment of a value. Such types have a single field with a
4c4b4cd2 6810 distinctive name. */
14f9c5c9
AS
6811
6812int
ebf56fd3 6813ada_is_aligner_type (struct type *type)
14f9c5c9 6814{
61ee279c 6815 type = ada_check_typedef (type);
714e53ab
PH
6816
6817 /* If we can find a parallel XVS type, then the XVS type should
6818 be used instead of this type. And hence, this is not an aligner
6819 type. */
6820 if (ada_find_parallel_type (type, "___XVS") != NULL)
6821 return 0;
6822
14f9c5c9 6823 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2
PH
6824 && TYPE_NFIELDS (type) == 1
6825 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
14f9c5c9
AS
6826}
6827
6828/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 6829 the parallel type. */
14f9c5c9 6830
d2e4a39e
AS
6831struct type *
6832ada_get_base_type (struct type *raw_type)
14f9c5c9 6833{
d2e4a39e
AS
6834 struct type *real_type_namer;
6835 struct type *raw_real_type;
14f9c5c9
AS
6836
6837 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
6838 return raw_type;
6839
6840 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 6841 if (real_type_namer == NULL
14f9c5c9
AS
6842 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
6843 || TYPE_NFIELDS (real_type_namer) != 1)
6844 return raw_type;
6845
6846 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
d2e4a39e 6847 if (raw_real_type == NULL)
14f9c5c9
AS
6848 return raw_type;
6849 else
6850 return raw_real_type;
d2e4a39e 6851}
14f9c5c9 6852
4c4b4cd2 6853/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 6854
d2e4a39e
AS
6855struct type *
6856ada_aligned_type (struct type *type)
14f9c5c9
AS
6857{
6858 if (ada_is_aligner_type (type))
6859 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
6860 else
6861 return ada_get_base_type (type);
6862}
6863
6864
6865/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 6866 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 6867
fc1a4b47
AC
6868const gdb_byte *
6869ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 6870{
d2e4a39e 6871 if (ada_is_aligner_type (type))
14f9c5c9 6872 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2
PH
6873 valaddr +
6874 TYPE_FIELD_BITPOS (type,
6875 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
6876 else
6877 return valaddr;
6878}
6879
4c4b4cd2
PH
6880
6881
14f9c5c9 6882/* The printed representation of an enumeration literal with encoded
4c4b4cd2 6883 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
6884const char *
6885ada_enum_name (const char *name)
14f9c5c9 6886{
4c4b4cd2
PH
6887 static char *result;
6888 static size_t result_len = 0;
d2e4a39e 6889 char *tmp;
14f9c5c9 6890
4c4b4cd2
PH
6891 /* First, unqualify the enumeration name:
6892 1. Search for the last '.' character. If we find one, then skip
76a01679
JB
6893 all the preceeding characters, the unqualified name starts
6894 right after that dot.
4c4b4cd2 6895 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
6896 translates dots into "__". Search forward for double underscores,
6897 but stop searching when we hit an overloading suffix, which is
6898 of the form "__" followed by digits. */
4c4b4cd2 6899
c3e5cd34
PH
6900 tmp = strrchr (name, '.');
6901 if (tmp != NULL)
4c4b4cd2
PH
6902 name = tmp + 1;
6903 else
14f9c5c9 6904 {
4c4b4cd2
PH
6905 while ((tmp = strstr (name, "__")) != NULL)
6906 {
6907 if (isdigit (tmp[2]))
6908 break;
6909 else
6910 name = tmp + 2;
6911 }
14f9c5c9
AS
6912 }
6913
6914 if (name[0] == 'Q')
6915 {
14f9c5c9
AS
6916 int v;
6917 if (name[1] == 'U' || name[1] == 'W')
4c4b4cd2
PH
6918 {
6919 if (sscanf (name + 2, "%x", &v) != 1)
6920 return name;
6921 }
14f9c5c9 6922 else
4c4b4cd2 6923 return name;
14f9c5c9 6924
4c4b4cd2 6925 GROW_VECT (result, result_len, 16);
14f9c5c9 6926 if (isascii (v) && isprint (v))
4c4b4cd2 6927 sprintf (result, "'%c'", v);
14f9c5c9 6928 else if (name[1] == 'U')
4c4b4cd2 6929 sprintf (result, "[\"%02x\"]", v);
14f9c5c9 6930 else
4c4b4cd2 6931 sprintf (result, "[\"%04x\"]", v);
14f9c5c9
AS
6932
6933 return result;
6934 }
d2e4a39e 6935 else
4c4b4cd2 6936 {
c3e5cd34
PH
6937 tmp = strstr (name, "__");
6938 if (tmp == NULL)
6939 tmp = strstr (name, "$");
6940 if (tmp != NULL)
4c4b4cd2
PH
6941 {
6942 GROW_VECT (result, result_len, tmp - name + 1);
6943 strncpy (result, name, tmp - name);
6944 result[tmp - name] = '\0';
6945 return result;
6946 }
6947
6948 return name;
6949 }
14f9c5c9
AS
6950}
6951
d2e4a39e 6952static struct value *
ebf56fd3 6953evaluate_subexp (struct type *expect_type, struct expression *exp, int *pos,
4c4b4cd2 6954 enum noside noside)
14f9c5c9 6955{
76a01679 6956 return (*exp->language_defn->la_exp_desc->evaluate_exp)
4c4b4cd2 6957 (expect_type, exp, pos, noside);
14f9c5c9
AS
6958}
6959
6960/* Evaluate the subexpression of EXP starting at *POS as for
6961 evaluate_type, updating *POS to point just past the evaluated
4c4b4cd2 6962 expression. */
14f9c5c9 6963
d2e4a39e
AS
6964static struct value *
6965evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 6966{
4c4b4cd2 6967 return (*exp->language_defn->la_exp_desc->evaluate_exp)
14f9c5c9
AS
6968 (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
6969}
6970
6971/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 6972 value it wraps. */
14f9c5c9 6973
d2e4a39e
AS
6974static struct value *
6975unwrap_value (struct value *val)
14f9c5c9 6976{
df407dfe 6977 struct type *type = ada_check_typedef (value_type (val));
14f9c5c9
AS
6978 if (ada_is_aligner_type (type))
6979 {
d2e4a39e 6980 struct value *v = value_struct_elt (&val, NULL, "F",
4c4b4cd2 6981 NULL, "internal structure");
df407dfe 6982 struct type *val_type = ada_check_typedef (value_type (v));
14f9c5c9 6983 if (ada_type_name (val_type) == NULL)
4c4b4cd2 6984 TYPE_NAME (val_type) = ada_type_name (type);
14f9c5c9
AS
6985
6986 return unwrap_value (v);
6987 }
d2e4a39e 6988 else
14f9c5c9 6989 {
d2e4a39e 6990 struct type *raw_real_type =
61ee279c 6991 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 6992
14f9c5c9 6993 if (type == raw_real_type)
4c4b4cd2 6994 return val;
14f9c5c9 6995
d2e4a39e 6996 return
4c4b4cd2
PH
6997 coerce_unspec_val_to_type
6998 (val, ada_to_fixed_type (raw_real_type, 0,
df407dfe 6999 VALUE_ADDRESS (val) + value_offset (val),
4c4b4cd2 7000 NULL));
14f9c5c9
AS
7001 }
7002}
d2e4a39e
AS
7003
7004static struct value *
7005cast_to_fixed (struct type *type, struct value *arg)
14f9c5c9
AS
7006{
7007 LONGEST val;
7008
df407dfe 7009 if (type == value_type (arg))
14f9c5c9 7010 return arg;
df407dfe 7011 else if (ada_is_fixed_point_type (value_type (arg)))
d2e4a39e 7012 val = ada_float_to_fixed (type,
df407dfe 7013 ada_fixed_to_float (value_type (arg),
4c4b4cd2 7014 value_as_long (arg)));
d2e4a39e 7015 else
14f9c5c9 7016 {
d2e4a39e 7017 DOUBLEST argd =
4c4b4cd2 7018 value_as_double (value_cast (builtin_type_double, value_copy (arg)));
14f9c5c9
AS
7019 val = ada_float_to_fixed (type, argd);
7020 }
7021
7022 return value_from_longest (type, val);
7023}
7024
d2e4a39e
AS
7025static struct value *
7026cast_from_fixed_to_double (struct value *arg)
14f9c5c9 7027{
df407dfe 7028 DOUBLEST val = ada_fixed_to_float (value_type (arg),
4c4b4cd2 7029 value_as_long (arg));
14f9c5c9
AS
7030 return value_from_double (builtin_type_double, val);
7031}
7032
4c4b4cd2
PH
7033/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
7034 return the converted value. */
7035
d2e4a39e
AS
7036static struct value *
7037coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 7038{
df407dfe 7039 struct type *type2 = value_type (val);
14f9c5c9
AS
7040 if (type == type2)
7041 return val;
7042
61ee279c
PH
7043 type2 = ada_check_typedef (type2);
7044 type = ada_check_typedef (type);
14f9c5c9 7045
d2e4a39e
AS
7046 if (TYPE_CODE (type2) == TYPE_CODE_PTR
7047 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
7048 {
7049 val = ada_value_ind (val);
df407dfe 7050 type2 = value_type (val);
14f9c5c9
AS
7051 }
7052
d2e4a39e 7053 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
14f9c5c9
AS
7054 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
7055 {
7056 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
4c4b4cd2
PH
7057 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
7058 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
323e0a4a 7059 error (_("Incompatible types in assignment"));
04624583 7060 deprecated_set_value_type (val, type);
14f9c5c9 7061 }
d2e4a39e 7062 return val;
14f9c5c9
AS
7063}
7064
4c4b4cd2
PH
7065static struct value *
7066ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
7067{
7068 struct value *val;
7069 struct type *type1, *type2;
7070 LONGEST v, v1, v2;
7071
994b9211
AC
7072 arg1 = coerce_ref (arg1);
7073 arg2 = coerce_ref (arg2);
df407dfe
AC
7074 type1 = base_type (ada_check_typedef (value_type (arg1)));
7075 type2 = base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 7076
76a01679
JB
7077 if (TYPE_CODE (type1) != TYPE_CODE_INT
7078 || TYPE_CODE (type2) != TYPE_CODE_INT)
4c4b4cd2
PH
7079 return value_binop (arg1, arg2, op);
7080
76a01679 7081 switch (op)
4c4b4cd2
PH
7082 {
7083 case BINOP_MOD:
7084 case BINOP_DIV:
7085 case BINOP_REM:
7086 break;
7087 default:
7088 return value_binop (arg1, arg2, op);
7089 }
7090
7091 v2 = value_as_long (arg2);
7092 if (v2 == 0)
323e0a4a 7093 error (_("second operand of %s must not be zero."), op_string (op));
4c4b4cd2
PH
7094
7095 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
7096 return value_binop (arg1, arg2, op);
7097
7098 v1 = value_as_long (arg1);
7099 switch (op)
7100 {
7101 case BINOP_DIV:
7102 v = v1 / v2;
76a01679
JB
7103 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
7104 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
7105 break;
7106 case BINOP_REM:
7107 v = v1 % v2;
76a01679
JB
7108 if (v * v1 < 0)
7109 v -= v2;
4c4b4cd2
PH
7110 break;
7111 default:
7112 /* Should not reach this point. */
7113 v = 0;
7114 }
7115
7116 val = allocate_value (type1);
990a07ab 7117 store_unsigned_integer (value_contents_raw (val),
df407dfe 7118 TYPE_LENGTH (value_type (val)), v);
4c4b4cd2
PH
7119 return val;
7120}
7121
7122static int
7123ada_value_equal (struct value *arg1, struct value *arg2)
7124{
df407dfe
AC
7125 if (ada_is_direct_array_type (value_type (arg1))
7126 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2
PH
7127 {
7128 arg1 = ada_coerce_to_simple_array (arg1);
7129 arg2 = ada_coerce_to_simple_array (arg2);
df407dfe
AC
7130 if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
7131 || TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
323e0a4a 7132 error (_("Attempt to compare array with non-array"));
4c4b4cd2 7133 /* FIXME: The following works only for types whose
76a01679
JB
7134 representations use all bits (no padding or undefined bits)
7135 and do not have user-defined equality. */
7136 return
df407dfe 7137 TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
0fd88904 7138 && memcmp (value_contents (arg1), value_contents (arg2),
df407dfe 7139 TYPE_LENGTH (value_type (arg1))) == 0;
4c4b4cd2
PH
7140 }
7141 return value_equal (arg1, arg2);
7142}
7143
d2e4a39e 7144struct value *
ebf56fd3 7145ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
4c4b4cd2 7146 int *pos, enum noside noside)
14f9c5c9
AS
7147{
7148 enum exp_opcode op;
14f9c5c9
AS
7149 int tem, tem2, tem3;
7150 int pc;
7151 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
7152 struct type *type;
7153 int nargs;
d2e4a39e 7154 struct value **argvec;
14f9c5c9 7155
d2e4a39e
AS
7156 pc = *pos;
7157 *pos += 1;
14f9c5c9
AS
7158 op = exp->elts[pc].opcode;
7159
d2e4a39e 7160 switch (op)
14f9c5c9
AS
7161 {
7162 default:
7163 *pos -= 1;
d2e4a39e 7164 return
4c4b4cd2
PH
7165 unwrap_value (evaluate_subexp_standard
7166 (expect_type, exp, pos, noside));
7167
7168 case OP_STRING:
7169 {
76a01679
JB
7170 struct value *result;
7171 *pos -= 1;
7172 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
7173 /* The result type will have code OP_STRING, bashed there from
7174 OP_ARRAY. Bash it back. */
df407dfe
AC
7175 if (TYPE_CODE (value_type (result)) == TYPE_CODE_STRING)
7176 TYPE_CODE (value_type (result)) = TYPE_CODE_ARRAY;
76a01679 7177 return result;
4c4b4cd2 7178 }
14f9c5c9
AS
7179
7180 case UNOP_CAST:
7181 (*pos) += 2;
7182 type = exp->elts[pc + 1].type;
7183 arg1 = evaluate_subexp (type, exp, pos, noside);
7184 if (noside == EVAL_SKIP)
4c4b4cd2 7185 goto nosideret;
df407dfe 7186 if (type != ada_check_typedef (value_type (arg1)))
4c4b4cd2
PH
7187 {
7188 if (ada_is_fixed_point_type (type))
7189 arg1 = cast_to_fixed (type, arg1);
df407dfe 7190 else if (ada_is_fixed_point_type (value_type (arg1)))
4c4b4cd2
PH
7191 arg1 = value_cast (type, cast_from_fixed_to_double (arg1));
7192 else if (VALUE_LVAL (arg1) == lval_memory)
7193 {
7194 /* This is in case of the really obscure (and undocumented,
7195 but apparently expected) case of (Foo) Bar.all, where Bar
7196 is an integer constant and Foo is a dynamic-sized type.
7197 If we don't do this, ARG1 will simply be relabeled with
7198 TYPE. */
7199 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7200 return value_zero (to_static_fixed_type (type), not_lval);
7201 arg1 =
7202 ada_to_fixed_value_create
df407dfe 7203 (type, VALUE_ADDRESS (arg1) + value_offset (arg1), 0);
4c4b4cd2
PH
7204 }
7205 else
7206 arg1 = value_cast (type, arg1);
7207 }
14f9c5c9
AS
7208 return arg1;
7209
4c4b4cd2
PH
7210 case UNOP_QUAL:
7211 (*pos) += 2;
7212 type = exp->elts[pc + 1].type;
7213 return ada_evaluate_subexp (type, exp, pos, noside);
7214
14f9c5c9
AS
7215 case BINOP_ASSIGN:
7216 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
df407dfe 7217 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
14f9c5c9 7218 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 7219 return arg1;
df407dfe
AC
7220 if (ada_is_fixed_point_type (value_type (arg1)))
7221 arg2 = cast_to_fixed (value_type (arg1), arg2);
7222 else if (ada_is_fixed_point_type (value_type (arg2)))
76a01679 7223 error
323e0a4a 7224 (_("Fixed-point values must be assigned to fixed-point variables"));
d2e4a39e 7225 else
df407dfe 7226 arg2 = coerce_for_assign (value_type (arg1), arg2);
4c4b4cd2 7227 return ada_value_assign (arg1, arg2);
14f9c5c9
AS
7228
7229 case BINOP_ADD:
7230 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
7231 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
7232 if (noside == EVAL_SKIP)
4c4b4cd2 7233 goto nosideret;
df407dfe
AC
7234 if ((ada_is_fixed_point_type (value_type (arg1))
7235 || ada_is_fixed_point_type (value_type (arg2)))
7236 && value_type (arg1) != value_type (arg2))
323e0a4a 7237 error (_("Operands of fixed-point addition must have the same type"));
df407dfe 7238 return value_cast (value_type (arg1), value_add (arg1, arg2));
14f9c5c9
AS
7239
7240 case BINOP_SUB:
7241 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
7242 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
7243 if (noside == EVAL_SKIP)
4c4b4cd2 7244 goto nosideret;
df407dfe
AC
7245 if ((ada_is_fixed_point_type (value_type (arg1))
7246 || ada_is_fixed_point_type (value_type (arg2)))
7247 && value_type (arg1) != value_type (arg2))
323e0a4a 7248 error (_("Operands of fixed-point subtraction must have the same type"));
df407dfe 7249 return value_cast (value_type (arg1), value_sub (arg1, arg2));
14f9c5c9
AS
7250
7251 case BINOP_MUL:
7252 case BINOP_DIV:
7253 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7254 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7255 if (noside == EVAL_SKIP)
4c4b4cd2
PH
7256 goto nosideret;
7257 else if (noside == EVAL_AVOID_SIDE_EFFECTS
76a01679 7258 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
df407dfe 7259 return value_zero (value_type (arg1), not_lval);
14f9c5c9 7260 else
4c4b4cd2 7261 {
df407dfe 7262 if (ada_is_fixed_point_type (value_type (arg1)))
4c4b4cd2 7263 arg1 = cast_from_fixed_to_double (arg1);
df407dfe 7264 if (ada_is_fixed_point_type (value_type (arg2)))
4c4b4cd2
PH
7265 arg2 = cast_from_fixed_to_double (arg2);
7266 return ada_value_binop (arg1, arg2, op);
7267 }
7268
7269 case BINOP_REM:
7270 case BINOP_MOD:
7271 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7272 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7273 if (noside == EVAL_SKIP)
76a01679 7274 goto nosideret;
4c4b4cd2 7275 else if (noside == EVAL_AVOID_SIDE_EFFECTS
76a01679 7276 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
df407dfe 7277 return value_zero (value_type (arg1), not_lval);
14f9c5c9 7278 else
76a01679 7279 return ada_value_binop (arg1, arg2, op);
14f9c5c9 7280
4c4b4cd2
PH
7281 case BINOP_EQUAL:
7282 case BINOP_NOTEQUAL:
14f9c5c9 7283 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
df407dfe 7284 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
14f9c5c9 7285 if (noside == EVAL_SKIP)
76a01679 7286 goto nosideret;
4c4b4cd2 7287 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 7288 tem = 0;
4c4b4cd2 7289 else
76a01679 7290 tem = ada_value_equal (arg1, arg2);
4c4b4cd2 7291 if (op == BINOP_NOTEQUAL)
76a01679 7292 tem = !tem;
4c4b4cd2
PH
7293 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
7294
7295 case UNOP_NEG:
7296 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7297 if (noside == EVAL_SKIP)
7298 goto nosideret;
df407dfe
AC
7299 else if (ada_is_fixed_point_type (value_type (arg1)))
7300 return value_cast (value_type (arg1), value_neg (arg1));
14f9c5c9 7301 else
4c4b4cd2
PH
7302 return value_neg (arg1);
7303
14f9c5c9
AS
7304 case OP_VAR_VALUE:
7305 *pos -= 1;
7306 if (noside == EVAL_SKIP)
4c4b4cd2
PH
7307 {
7308 *pos += 4;
7309 goto nosideret;
7310 }
7311 else if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
7312 /* Only encountered when an unresolved symbol occurs in a
7313 context other than a function call, in which case, it is
7314 illegal. */
323e0a4a 7315 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2 7316 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
14f9c5c9 7317 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
7318 {
7319 *pos += 4;
7320 return value_zero
7321 (to_static_fixed_type
7322 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
7323 not_lval);
7324 }
d2e4a39e 7325 else
4c4b4cd2
PH
7326 {
7327 arg1 =
7328 unwrap_value (evaluate_subexp_standard
7329 (expect_type, exp, pos, noside));
7330 return ada_to_fixed_value (arg1);
7331 }
7332
7333 case OP_FUNCALL:
7334 (*pos) += 2;
7335
7336 /* Allocate arg vector, including space for the function to be
7337 called in argvec[0] and a terminating NULL. */
7338 nargs = longest_to_int (exp->elts[pc + 1].longconst);
7339 argvec =
7340 (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
7341
7342 if (exp->elts[*pos].opcode == OP_VAR_VALUE
76a01679 7343 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
323e0a4a 7344 error (_("Unexpected unresolved symbol, %s, during evaluation"),
4c4b4cd2
PH
7345 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
7346 else
7347 {
7348 for (tem = 0; tem <= nargs; tem += 1)
7349 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7350 argvec[tem] = 0;
7351
7352 if (noside == EVAL_SKIP)
7353 goto nosideret;
7354 }
7355
df407dfe 7356 if (ada_is_packed_array_type (desc_base_type (value_type (argvec[0]))))
4c4b4cd2 7357 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
df407dfe
AC
7358 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_REF
7359 || (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
76a01679 7360 && VALUE_LVAL (argvec[0]) == lval_memory))
4c4b4cd2
PH
7361 argvec[0] = value_addr (argvec[0]);
7362
df407dfe 7363 type = ada_check_typedef (value_type (argvec[0]));
4c4b4cd2
PH
7364 if (TYPE_CODE (type) == TYPE_CODE_PTR)
7365 {
61ee279c 7366 switch (TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type))))
4c4b4cd2
PH
7367 {
7368 case TYPE_CODE_FUNC:
61ee279c 7369 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
7370 break;
7371 case TYPE_CODE_ARRAY:
7372 break;
7373 case TYPE_CODE_STRUCT:
7374 if (noside != EVAL_AVOID_SIDE_EFFECTS)
7375 argvec[0] = ada_value_ind (argvec[0]);
61ee279c 7376 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
4c4b4cd2
PH
7377 break;
7378 default:
323e0a4a 7379 error (_("cannot subscript or call something of type `%s'"),
df407dfe 7380 ada_type_name (value_type (argvec[0])));
4c4b4cd2
PH
7381 break;
7382 }
7383 }
7384
7385 switch (TYPE_CODE (type))
7386 {
7387 case TYPE_CODE_FUNC:
7388 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7389 return allocate_value (TYPE_TARGET_TYPE (type));
7390 return call_function_by_hand (argvec[0], nargs, argvec + 1);
7391 case TYPE_CODE_STRUCT:
7392 {
7393 int arity;
7394
4c4b4cd2
PH
7395 arity = ada_array_arity (type);
7396 type = ada_array_element_type (type, nargs);
7397 if (type == NULL)
323e0a4a 7398 error (_("cannot subscript or call a record"));
4c4b4cd2 7399 if (arity != nargs)
323e0a4a 7400 error (_("wrong number of subscripts; expecting %d"), arity);
4c4b4cd2
PH
7401 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7402 return allocate_value (ada_aligned_type (type));
7403 return
7404 unwrap_value (ada_value_subscript
7405 (argvec[0], nargs, argvec + 1));
7406 }
7407 case TYPE_CODE_ARRAY:
7408 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7409 {
7410 type = ada_array_element_type (type, nargs);
7411 if (type == NULL)
323e0a4a 7412 error (_("element type of array unknown"));
4c4b4cd2
PH
7413 else
7414 return allocate_value (ada_aligned_type (type));
7415 }
7416 return
7417 unwrap_value (ada_value_subscript
7418 (ada_coerce_to_simple_array (argvec[0]),
7419 nargs, argvec + 1));
7420 case TYPE_CODE_PTR: /* Pointer to array */
7421 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
7422 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7423 {
7424 type = ada_array_element_type (type, nargs);
7425 if (type == NULL)
323e0a4a 7426 error (_("element type of array unknown"));
4c4b4cd2
PH
7427 else
7428 return allocate_value (ada_aligned_type (type));
7429 }
7430 return
7431 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
7432 nargs, argvec + 1));
7433
7434 default:
323e0a4a
AC
7435 error (_("Attempt to index or call something other than an \
7436array or function"));
4c4b4cd2
PH
7437 }
7438
7439 case TERNOP_SLICE:
7440 {
7441 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7442 struct value *low_bound_val =
7443 evaluate_subexp (NULL_TYPE, exp, pos, noside);
714e53ab
PH
7444 struct value *high_bound_val =
7445 evaluate_subexp (NULL_TYPE, exp, pos, noside);
7446 LONGEST low_bound;
7447 LONGEST high_bound;
994b9211
AC
7448 low_bound_val = coerce_ref (low_bound_val);
7449 high_bound_val = coerce_ref (high_bound_val);
714e53ab
PH
7450 low_bound = pos_atr (low_bound_val);
7451 high_bound = pos_atr (high_bound_val);
963a6417 7452
4c4b4cd2
PH
7453 if (noside == EVAL_SKIP)
7454 goto nosideret;
7455
4c4b4cd2
PH
7456 /* If this is a reference to an aligner type, then remove all
7457 the aligners. */
df407dfe
AC
7458 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
7459 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
7460 TYPE_TARGET_TYPE (value_type (array)) =
7461 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
4c4b4cd2 7462
df407dfe 7463 if (ada_is_packed_array_type (value_type (array)))
323e0a4a 7464 error (_("cannot slice a packed array"));
4c4b4cd2
PH
7465
7466 /* If this is a reference to an array or an array lvalue,
7467 convert to a pointer. */
df407dfe
AC
7468 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
7469 || (TYPE_CODE (value_type (array)) == TYPE_CODE_ARRAY
4c4b4cd2
PH
7470 && VALUE_LVAL (array) == lval_memory))
7471 array = value_addr (array);
7472
1265e4aa 7473 if (noside == EVAL_AVOID_SIDE_EFFECTS
61ee279c 7474 && ada_is_array_descriptor_type (ada_check_typedef
df407dfe 7475 (value_type (array))))
0b5d8877 7476 return empty_array (ada_type_of_array (array, 0), low_bound);
4c4b4cd2
PH
7477
7478 array = ada_coerce_to_simple_array_ptr (array);
7479
714e53ab
PH
7480 /* If we have more than one level of pointer indirection,
7481 dereference the value until we get only one level. */
df407dfe
AC
7482 while (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR
7483 && (TYPE_CODE (TYPE_TARGET_TYPE (value_type (array)))
714e53ab
PH
7484 == TYPE_CODE_PTR))
7485 array = value_ind (array);
7486
7487 /* Make sure we really do have an array type before going further,
7488 to avoid a SEGV when trying to get the index type or the target
7489 type later down the road if the debug info generated by
7490 the compiler is incorrect or incomplete. */
df407dfe 7491 if (!ada_is_simple_array_type (value_type (array)))
323e0a4a 7492 error (_("cannot take slice of non-array"));
714e53ab 7493
df407dfe 7494 if (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR)
4c4b4cd2 7495 {
0b5d8877 7496 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 7497 return empty_array (TYPE_TARGET_TYPE (value_type (array)),
4c4b4cd2
PH
7498 low_bound);
7499 else
7500 {
7501 struct type *arr_type0 =
df407dfe 7502 to_fixed_array_type (TYPE_TARGET_TYPE (value_type (array)),
4c4b4cd2 7503 NULL, 1);
0b5d8877 7504 return ada_value_slice_ptr (array, arr_type0,
6c038f32
PH
7505 (int) low_bound,
7506 (int) high_bound);
4c4b4cd2
PH
7507 }
7508 }
7509 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7510 return array;
7511 else if (high_bound < low_bound)
df407dfe 7512 return empty_array (value_type (array), low_bound);
4c4b4cd2 7513 else
0b5d8877 7514 return ada_value_slice (array, (int) low_bound, (int) high_bound);
4c4b4cd2 7515 }
14f9c5c9 7516
4c4b4cd2
PH
7517 case UNOP_IN_RANGE:
7518 (*pos) += 2;
7519 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7520 type = exp->elts[pc + 1].type;
14f9c5c9 7521
14f9c5c9 7522 if (noside == EVAL_SKIP)
4c4b4cd2 7523 goto nosideret;
14f9c5c9 7524
4c4b4cd2
PH
7525 switch (TYPE_CODE (type))
7526 {
7527 default:
323e0a4a
AC
7528 lim_warning (_("Membership test incompletely implemented; \
7529always returns true"));
4c4b4cd2
PH
7530 return value_from_longest (builtin_type_int, (LONGEST) 1);
7531
7532 case TYPE_CODE_RANGE:
76a01679 7533 arg2 = value_from_longest (builtin_type_int, TYPE_LOW_BOUND (type));
4c4b4cd2
PH
7534 arg3 = value_from_longest (builtin_type_int,
7535 TYPE_HIGH_BOUND (type));
7536 return
7537 value_from_longest (builtin_type_int,
7538 (value_less (arg1, arg3)
7539 || value_equal (arg1, arg3))
7540 && (value_less (arg2, arg1)
7541 || value_equal (arg2, arg1)));
7542 }
7543
7544 case BINOP_IN_BOUNDS:
14f9c5c9 7545 (*pos) += 2;
4c4b4cd2
PH
7546 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7547 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 7548
4c4b4cd2
PH
7549 if (noside == EVAL_SKIP)
7550 goto nosideret;
14f9c5c9 7551
4c4b4cd2
PH
7552 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7553 return value_zero (builtin_type_int, not_lval);
14f9c5c9 7554
4c4b4cd2 7555 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 7556
df407dfe 7557 if (tem < 1 || tem > ada_array_arity (value_type (arg2)))
323e0a4a 7558 error (_("invalid dimension number to 'range"));
14f9c5c9 7559
4c4b4cd2
PH
7560 arg3 = ada_array_bound (arg2, tem, 1);
7561 arg2 = ada_array_bound (arg2, tem, 0);
d2e4a39e 7562
4c4b4cd2
PH
7563 return
7564 value_from_longest (builtin_type_int,
7565 (value_less (arg1, arg3)
7566 || value_equal (arg1, arg3))
7567 && (value_less (arg2, arg1)
7568 || value_equal (arg2, arg1)));
7569
7570 case TERNOP_IN_RANGE:
7571 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7572 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7573 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7574
7575 if (noside == EVAL_SKIP)
7576 goto nosideret;
7577
7578 return
7579 value_from_longest (builtin_type_int,
7580 (value_less (arg1, arg3)
7581 || value_equal (arg1, arg3))
7582 && (value_less (arg2, arg1)
7583 || value_equal (arg2, arg1)));
7584
7585 case OP_ATR_FIRST:
7586 case OP_ATR_LAST:
7587 case OP_ATR_LENGTH:
7588 {
76a01679
JB
7589 struct type *type_arg;
7590 if (exp->elts[*pos].opcode == OP_TYPE)
7591 {
7592 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7593 arg1 = NULL;
7594 type_arg = exp->elts[pc + 2].type;
7595 }
7596 else
7597 {
7598 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7599 type_arg = NULL;
7600 }
7601
7602 if (exp->elts[*pos].opcode != OP_LONG)
323e0a4a 7603 error (_("Invalid operand to '%s"), ada_attribute_name (op));
76a01679
JB
7604 tem = longest_to_int (exp->elts[*pos + 2].longconst);
7605 *pos += 4;
7606
7607 if (noside == EVAL_SKIP)
7608 goto nosideret;
7609
7610 if (type_arg == NULL)
7611 {
7612 arg1 = ada_coerce_ref (arg1);
7613
df407dfe 7614 if (ada_is_packed_array_type (value_type (arg1)))
76a01679
JB
7615 arg1 = ada_coerce_to_simple_array (arg1);
7616
df407dfe 7617 if (tem < 1 || tem > ada_array_arity (value_type (arg1)))
323e0a4a 7618 error (_("invalid dimension number to '%s"),
76a01679
JB
7619 ada_attribute_name (op));
7620
7621 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7622 {
df407dfe 7623 type = ada_index_type (value_type (arg1), tem);
76a01679
JB
7624 if (type == NULL)
7625 error
323e0a4a 7626 (_("attempt to take bound of something that is not an array"));
76a01679
JB
7627 return allocate_value (type);
7628 }
7629
7630 switch (op)
7631 {
7632 default: /* Should never happen. */
323e0a4a 7633 error (_("unexpected attribute encountered"));
76a01679
JB
7634 case OP_ATR_FIRST:
7635 return ada_array_bound (arg1, tem, 0);
7636 case OP_ATR_LAST:
7637 return ada_array_bound (arg1, tem, 1);
7638 case OP_ATR_LENGTH:
7639 return ada_array_length (arg1, tem);
7640 }
7641 }
7642 else if (discrete_type_p (type_arg))
7643 {
7644 struct type *range_type;
7645 char *name = ada_type_name (type_arg);
7646 range_type = NULL;
7647 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
7648 range_type =
7649 to_fixed_range_type (name, NULL, TYPE_OBJFILE (type_arg));
7650 if (range_type == NULL)
7651 range_type = type_arg;
7652 switch (op)
7653 {
7654 default:
323e0a4a 7655 error (_("unexpected attribute encountered"));
76a01679
JB
7656 case OP_ATR_FIRST:
7657 return discrete_type_low_bound (range_type);
7658 case OP_ATR_LAST:
7659 return discrete_type_high_bound (range_type);
7660 case OP_ATR_LENGTH:
323e0a4a 7661 error (_("the 'length attribute applies only to array types"));
76a01679
JB
7662 }
7663 }
7664 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
323e0a4a 7665 error (_("unimplemented type attribute"));
76a01679
JB
7666 else
7667 {
7668 LONGEST low, high;
7669
7670 if (ada_is_packed_array_type (type_arg))
7671 type_arg = decode_packed_array_type (type_arg);
7672
7673 if (tem < 1 || tem > ada_array_arity (type_arg))
323e0a4a 7674 error (_("invalid dimension number to '%s"),
76a01679
JB
7675 ada_attribute_name (op));
7676
7677 type = ada_index_type (type_arg, tem);
7678 if (type == NULL)
7679 error
323e0a4a 7680 (_("attempt to take bound of something that is not an array"));
76a01679
JB
7681 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7682 return allocate_value (type);
7683
7684 switch (op)
7685 {
7686 default:
323e0a4a 7687 error (_("unexpected attribute encountered"));
76a01679
JB
7688 case OP_ATR_FIRST:
7689 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
7690 return value_from_longest (type, low);
7691 case OP_ATR_LAST:
7692 high = ada_array_bound_from_type (type_arg, tem, 1, &type);
7693 return value_from_longest (type, high);
7694 case OP_ATR_LENGTH:
7695 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
7696 high = ada_array_bound_from_type (type_arg, tem, 1, NULL);
7697 return value_from_longest (type, high - low + 1);
7698 }
7699 }
14f9c5c9
AS
7700 }
7701
4c4b4cd2
PH
7702 case OP_ATR_TAG:
7703 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7704 if (noside == EVAL_SKIP)
76a01679 7705 goto nosideret;
4c4b4cd2
PH
7706
7707 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 7708 return value_zero (ada_tag_type (arg1), not_lval);
4c4b4cd2
PH
7709
7710 return ada_value_tag (arg1);
7711
7712 case OP_ATR_MIN:
7713 case OP_ATR_MAX:
7714 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
7715 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7716 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7717 if (noside == EVAL_SKIP)
76a01679 7718 goto nosideret;
d2e4a39e 7719 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 7720 return value_zero (value_type (arg1), not_lval);
14f9c5c9 7721 else
76a01679
JB
7722 return value_binop (arg1, arg2,
7723 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
14f9c5c9 7724
4c4b4cd2
PH
7725 case OP_ATR_MODULUS:
7726 {
76a01679
JB
7727 struct type *type_arg = exp->elts[pc + 2].type;
7728 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
4c4b4cd2 7729
76a01679
JB
7730 if (noside == EVAL_SKIP)
7731 goto nosideret;
4c4b4cd2 7732
76a01679 7733 if (!ada_is_modular_type (type_arg))
323e0a4a 7734 error (_("'modulus must be applied to modular type"));
4c4b4cd2 7735
76a01679
JB
7736 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
7737 ada_modulus (type_arg));
4c4b4cd2
PH
7738 }
7739
7740
7741 case OP_ATR_POS:
7742 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
7743 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7744 if (noside == EVAL_SKIP)
76a01679 7745 goto nosideret;
4c4b4cd2 7746 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
72d5681a 7747 return value_zero (builtin_type_int, not_lval);
14f9c5c9 7748 else
76a01679 7749 return value_pos_atr (arg1);
14f9c5c9 7750
4c4b4cd2
PH
7751 case OP_ATR_SIZE:
7752 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7753 if (noside == EVAL_SKIP)
76a01679 7754 goto nosideret;
4c4b4cd2 7755 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
72d5681a 7756 return value_zero (builtin_type_int, not_lval);
4c4b4cd2 7757 else
72d5681a 7758 return value_from_longest (builtin_type_int,
76a01679 7759 TARGET_CHAR_BIT
df407dfe 7760 * TYPE_LENGTH (value_type (arg1)));
4c4b4cd2
PH
7761
7762 case OP_ATR_VAL:
7763 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9 7764 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4c4b4cd2 7765 type = exp->elts[pc + 2].type;
14f9c5c9 7766 if (noside == EVAL_SKIP)
76a01679 7767 goto nosideret;
4c4b4cd2 7768 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 7769 return value_zero (type, not_lval);
4c4b4cd2 7770 else
76a01679 7771 return value_val_atr (type, arg1);
4c4b4cd2
PH
7772
7773 case BINOP_EXP:
7774 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7775 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7776 if (noside == EVAL_SKIP)
7777 goto nosideret;
7778 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
df407dfe 7779 return value_zero (value_type (arg1), not_lval);
4c4b4cd2
PH
7780 else
7781 return value_binop (arg1, arg2, op);
7782
7783 case UNOP_PLUS:
7784 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7785 if (noside == EVAL_SKIP)
7786 goto nosideret;
7787 else
7788 return arg1;
7789
7790 case UNOP_ABS:
7791 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7792 if (noside == EVAL_SKIP)
7793 goto nosideret;
df407dfe 7794 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
4c4b4cd2 7795 return value_neg (arg1);
14f9c5c9 7796 else
4c4b4cd2 7797 return arg1;
14f9c5c9
AS
7798
7799 case UNOP_IND:
7800 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
61ee279c 7801 expect_type = TYPE_TARGET_TYPE (ada_check_typedef (expect_type));
14f9c5c9
AS
7802 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
7803 if (noside == EVAL_SKIP)
4c4b4cd2 7804 goto nosideret;
df407dfe 7805 type = ada_check_typedef (value_type (arg1));
14f9c5c9 7806 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
7807 {
7808 if (ada_is_array_descriptor_type (type))
7809 /* GDB allows dereferencing GNAT array descriptors. */
7810 {
7811 struct type *arrType = ada_type_of_array (arg1, 0);
7812 if (arrType == NULL)
323e0a4a 7813 error (_("Attempt to dereference null array pointer."));
00a4c844 7814 return value_at_lazy (arrType, 0);
4c4b4cd2
PH
7815 }
7816 else if (TYPE_CODE (type) == TYPE_CODE_PTR
7817 || TYPE_CODE (type) == TYPE_CODE_REF
7818 /* In C you can dereference an array to get the 1st elt. */
7819 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
714e53ab
PH
7820 {
7821 type = to_static_fixed_type
7822 (ada_aligned_type
7823 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
7824 check_size (type);
7825 return value_zero (type, lval_memory);
7826 }
4c4b4cd2
PH
7827 else if (TYPE_CODE (type) == TYPE_CODE_INT)
7828 /* GDB allows dereferencing an int. */
7829 return value_zero (builtin_type_int, lval_memory);
7830 else
323e0a4a 7831 error (_("Attempt to take contents of a non-pointer value."));
4c4b4cd2 7832 }
76a01679 7833 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
df407dfe 7834 type = ada_check_typedef (value_type (arg1));
d2e4a39e 7835
4c4b4cd2
PH
7836 if (ada_is_array_descriptor_type (type))
7837 /* GDB allows dereferencing GNAT array descriptors. */
7838 return ada_coerce_to_simple_array (arg1);
14f9c5c9 7839 else
4c4b4cd2 7840 return ada_value_ind (arg1);
14f9c5c9
AS
7841
7842 case STRUCTOP_STRUCT:
7843 tem = longest_to_int (exp->elts[pc + 1].longconst);
7844 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
7845 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7846 if (noside == EVAL_SKIP)
4c4b4cd2 7847 goto nosideret;
14f9c5c9 7848 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 7849 {
df407dfe 7850 struct type *type1 = value_type (arg1);
76a01679
JB
7851 if (ada_is_tagged_type (type1, 1))
7852 {
7853 type = ada_lookup_struct_elt_type (type1,
7854 &exp->elts[pc + 2].string,
7855 1, 1, NULL);
7856 if (type == NULL)
7857 /* In this case, we assume that the field COULD exist
7858 in some extension of the type. Return an object of
7859 "type" void, which will match any formal
7860 (see ada_type_match). */
7861 return value_zero (builtin_type_void, lval_memory);
7862 }
7863 else
7864 type =
7865 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
7866 0, NULL);
7867
7868 return value_zero (ada_aligned_type (type), lval_memory);
7869 }
14f9c5c9 7870 else
76a01679
JB
7871 return
7872 ada_to_fixed_value (unwrap_value
7873 (ada_value_struct_elt
7874 (arg1, &exp->elts[pc + 2].string, "record")));
14f9c5c9 7875 case OP_TYPE:
4c4b4cd2
PH
7876 /* The value is not supposed to be used. This is here to make it
7877 easier to accommodate expressions that contain types. */
14f9c5c9
AS
7878 (*pos) += 2;
7879 if (noside == EVAL_SKIP)
4c4b4cd2 7880 goto nosideret;
14f9c5c9 7881 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 7882 return allocate_value (builtin_type_void);
14f9c5c9 7883 else
323e0a4a 7884 error (_("Attempt to use a type name as an expression"));
14f9c5c9
AS
7885 }
7886
7887nosideret:
7888 return value_from_longest (builtin_type_long, (LONGEST) 1);
7889}
14f9c5c9 7890\f
d2e4a39e 7891
4c4b4cd2 7892 /* Fixed point */
14f9c5c9
AS
7893
7894/* If TYPE encodes an Ada fixed-point type, return the suffix of the
7895 type name that encodes the 'small and 'delta information.
4c4b4cd2 7896 Otherwise, return NULL. */
14f9c5c9 7897
d2e4a39e 7898static const char *
ebf56fd3 7899fixed_type_info (struct type *type)
14f9c5c9 7900{
d2e4a39e 7901 const char *name = ada_type_name (type);
14f9c5c9
AS
7902 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
7903
d2e4a39e
AS
7904 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
7905 {
14f9c5c9
AS
7906 const char *tail = strstr (name, "___XF_");
7907 if (tail == NULL)
4c4b4cd2 7908 return NULL;
d2e4a39e 7909 else
4c4b4cd2 7910 return tail + 5;
14f9c5c9
AS
7911 }
7912 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
7913 return fixed_type_info (TYPE_TARGET_TYPE (type));
7914 else
7915 return NULL;
7916}
7917
4c4b4cd2 7918/* Returns non-zero iff TYPE represents an Ada fixed-point type. */
14f9c5c9
AS
7919
7920int
ebf56fd3 7921ada_is_fixed_point_type (struct type *type)
14f9c5c9
AS
7922{
7923 return fixed_type_info (type) != NULL;
7924}
7925
4c4b4cd2
PH
7926/* Return non-zero iff TYPE represents a System.Address type. */
7927
7928int
7929ada_is_system_address_type (struct type *type)
7930{
7931 return (TYPE_NAME (type)
7932 && strcmp (TYPE_NAME (type), "system__address") == 0);
7933}
7934
14f9c5c9
AS
7935/* Assuming that TYPE is the representation of an Ada fixed-point
7936 type, return its delta, or -1 if the type is malformed and the
4c4b4cd2 7937 delta cannot be determined. */
14f9c5c9
AS
7938
7939DOUBLEST
ebf56fd3 7940ada_delta (struct type *type)
14f9c5c9
AS
7941{
7942 const char *encoding = fixed_type_info (type);
7943 long num, den;
7944
7945 if (sscanf (encoding, "_%ld_%ld", &num, &den) < 2)
7946 return -1.0;
d2e4a39e 7947 else
14f9c5c9
AS
7948 return (DOUBLEST) num / (DOUBLEST) den;
7949}
7950
7951/* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
4c4b4cd2 7952 factor ('SMALL value) associated with the type. */
14f9c5c9
AS
7953
7954static DOUBLEST
ebf56fd3 7955scaling_factor (struct type *type)
14f9c5c9
AS
7956{
7957 const char *encoding = fixed_type_info (type);
7958 unsigned long num0, den0, num1, den1;
7959 int n;
d2e4a39e 7960
14f9c5c9
AS
7961 n = sscanf (encoding, "_%lu_%lu_%lu_%lu", &num0, &den0, &num1, &den1);
7962
7963 if (n < 2)
7964 return 1.0;
7965 else if (n == 4)
7966 return (DOUBLEST) num1 / (DOUBLEST) den1;
d2e4a39e 7967 else
14f9c5c9
AS
7968 return (DOUBLEST) num0 / (DOUBLEST) den0;
7969}
7970
7971
7972/* Assuming that X is the representation of a value of fixed-point
4c4b4cd2 7973 type TYPE, return its floating-point equivalent. */
14f9c5c9
AS
7974
7975DOUBLEST
ebf56fd3 7976ada_fixed_to_float (struct type *type, LONGEST x)
14f9c5c9 7977{
d2e4a39e 7978 return (DOUBLEST) x *scaling_factor (type);
14f9c5c9
AS
7979}
7980
4c4b4cd2
PH
7981/* The representation of a fixed-point value of type TYPE
7982 corresponding to the value X. */
14f9c5c9
AS
7983
7984LONGEST
ebf56fd3 7985ada_float_to_fixed (struct type *type, DOUBLEST x)
14f9c5c9
AS
7986{
7987 return (LONGEST) (x / scaling_factor (type) + 0.5);
7988}
7989
7990
4c4b4cd2 7991 /* VAX floating formats */
14f9c5c9
AS
7992
7993/* Non-zero iff TYPE represents one of the special VAX floating-point
4c4b4cd2
PH
7994 types. */
7995
14f9c5c9 7996int
d2e4a39e 7997ada_is_vax_floating_type (struct type *type)
14f9c5c9 7998{
d2e4a39e 7999 int name_len =
14f9c5c9 8000 (ada_type_name (type) == NULL) ? 0 : strlen (ada_type_name (type));
d2e4a39e 8001 return
14f9c5c9 8002 name_len > 6
d2e4a39e 8003 && (TYPE_CODE (type) == TYPE_CODE_INT
4c4b4cd2
PH
8004 || TYPE_CODE (type) == TYPE_CODE_RANGE)
8005 && strncmp (ada_type_name (type) + name_len - 6, "___XF", 5) == 0;
14f9c5c9
AS
8006}
8007
8008/* The type of special VAX floating-point type this is, assuming
4c4b4cd2
PH
8009 ada_is_vax_floating_point. */
8010
14f9c5c9 8011int
d2e4a39e 8012ada_vax_float_type_suffix (struct type *type)
14f9c5c9 8013{
d2e4a39e 8014 return ada_type_name (type)[strlen (ada_type_name (type)) - 1];
14f9c5c9
AS
8015}
8016
4c4b4cd2 8017/* A value representing the special debugging function that outputs
14f9c5c9 8018 VAX floating-point values of the type represented by TYPE. Assumes
4c4b4cd2
PH
8019 ada_is_vax_floating_type (TYPE). */
8020
d2e4a39e
AS
8021struct value *
8022ada_vax_float_print_function (struct type *type)
8023{
8024 switch (ada_vax_float_type_suffix (type))
8025 {
8026 case 'F':
8027 return get_var_value ("DEBUG_STRING_F", 0);
8028 case 'D':
8029 return get_var_value ("DEBUG_STRING_D", 0);
8030 case 'G':
8031 return get_var_value ("DEBUG_STRING_G", 0);
8032 default:
323e0a4a 8033 error (_("invalid VAX floating-point type"));
d2e4a39e 8034 }
14f9c5c9 8035}
14f9c5c9 8036\f
d2e4a39e 8037
4c4b4cd2 8038 /* Range types */
14f9c5c9
AS
8039
8040/* Scan STR beginning at position K for a discriminant name, and
8041 return the value of that discriminant field of DVAL in *PX. If
8042 PNEW_K is not null, put the position of the character beyond the
8043 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 8044 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
8045
8046static int
07d8f827 8047scan_discrim_bound (char *str, int k, struct value *dval, LONGEST * px,
76a01679 8048 int *pnew_k)
14f9c5c9
AS
8049{
8050 static char *bound_buffer = NULL;
8051 static size_t bound_buffer_len = 0;
8052 char *bound;
8053 char *pend;
d2e4a39e 8054 struct value *bound_val;
14f9c5c9
AS
8055
8056 if (dval == NULL || str == NULL || str[k] == '\0')
8057 return 0;
8058
d2e4a39e 8059 pend = strstr (str + k, "__");
14f9c5c9
AS
8060 if (pend == NULL)
8061 {
d2e4a39e 8062 bound = str + k;
14f9c5c9
AS
8063 k += strlen (bound);
8064 }
d2e4a39e 8065 else
14f9c5c9 8066 {
d2e4a39e 8067 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
14f9c5c9 8068 bound = bound_buffer;
d2e4a39e
AS
8069 strncpy (bound_buffer, str + k, pend - (str + k));
8070 bound[pend - (str + k)] = '\0';
8071 k = pend - str;
14f9c5c9 8072 }
d2e4a39e 8073
df407dfe 8074 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
8075 if (bound_val == NULL)
8076 return 0;
8077
8078 *px = value_as_long (bound_val);
8079 if (pnew_k != NULL)
8080 *pnew_k = k;
8081 return 1;
8082}
8083
8084/* Value of variable named NAME in the current environment. If
8085 no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
8086 otherwise causes an error with message ERR_MSG. */
8087
d2e4a39e
AS
8088static struct value *
8089get_var_value (char *name, char *err_msg)
14f9c5c9 8090{
4c4b4cd2 8091 struct ada_symbol_info *syms;
14f9c5c9
AS
8092 int nsyms;
8093
4c4b4cd2
PH
8094 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
8095 &syms);
14f9c5c9
AS
8096
8097 if (nsyms != 1)
8098 {
8099 if (err_msg == NULL)
4c4b4cd2 8100 return 0;
14f9c5c9 8101 else
8a3fe4f8 8102 error (("%s"), err_msg);
14f9c5c9
AS
8103 }
8104
4c4b4cd2 8105 return value_of_variable (syms[0].sym, syms[0].block);
14f9c5c9 8106}
d2e4a39e 8107
14f9c5c9 8108/* Value of integer variable named NAME in the current environment. If
4c4b4cd2
PH
8109 no such variable found, returns 0, and sets *FLAG to 0. If
8110 successful, sets *FLAG to 1. */
8111
14f9c5c9 8112LONGEST
4c4b4cd2 8113get_int_var_value (char *name, int *flag)
14f9c5c9 8114{
4c4b4cd2 8115 struct value *var_val = get_var_value (name, 0);
d2e4a39e 8116
14f9c5c9
AS
8117 if (var_val == 0)
8118 {
8119 if (flag != NULL)
4c4b4cd2 8120 *flag = 0;
14f9c5c9
AS
8121 return 0;
8122 }
8123 else
8124 {
8125 if (flag != NULL)
4c4b4cd2 8126 *flag = 1;
14f9c5c9
AS
8127 return value_as_long (var_val);
8128 }
8129}
d2e4a39e 8130
14f9c5c9
AS
8131
8132/* Return a range type whose base type is that of the range type named
8133 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 8134 from NAME according to the GNAT range encoding conventions.
14f9c5c9
AS
8135 Extract discriminant values, if needed, from DVAL. If a new type
8136 must be created, allocate in OBJFILE's space. The bounds
8137 information, in general, is encoded in NAME, the base type given in
4c4b4cd2 8138 the named range type. */
14f9c5c9 8139
d2e4a39e 8140static struct type *
ebf56fd3 8141to_fixed_range_type (char *name, struct value *dval, struct objfile *objfile)
14f9c5c9
AS
8142{
8143 struct type *raw_type = ada_find_any_type (name);
8144 struct type *base_type;
d2e4a39e 8145 char *subtype_info;
14f9c5c9
AS
8146
8147 if (raw_type == NULL)
8148 base_type = builtin_type_int;
8149 else if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
8150 base_type = TYPE_TARGET_TYPE (raw_type);
8151 else
8152 base_type = raw_type;
8153
8154 subtype_info = strstr (name, "___XD");
8155 if (subtype_info == NULL)
8156 return raw_type;
8157 else
8158 {
8159 static char *name_buf = NULL;
8160 static size_t name_len = 0;
8161 int prefix_len = subtype_info - name;
8162 LONGEST L, U;
8163 struct type *type;
8164 char *bounds_str;
8165 int n;
8166
8167 GROW_VECT (name_buf, name_len, prefix_len + 5);
8168 strncpy (name_buf, name, prefix_len);
8169 name_buf[prefix_len] = '\0';
8170
8171 subtype_info += 5;
8172 bounds_str = strchr (subtype_info, '_');
8173 n = 1;
8174
d2e4a39e 8175 if (*subtype_info == 'L')
4c4b4cd2
PH
8176 {
8177 if (!ada_scan_number (bounds_str, n, &L, &n)
8178 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
8179 return raw_type;
8180 if (bounds_str[n] == '_')
8181 n += 2;
8182 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
8183 n += 1;
8184 subtype_info += 1;
8185 }
d2e4a39e 8186 else
4c4b4cd2
PH
8187 {
8188 int ok;
8189 strcpy (name_buf + prefix_len, "___L");
8190 L = get_int_var_value (name_buf, &ok);
8191 if (!ok)
8192 {
323e0a4a 8193 lim_warning (_("Unknown lower bound, using 1."));
4c4b4cd2
PH
8194 L = 1;
8195 }
8196 }
14f9c5c9 8197
d2e4a39e 8198 if (*subtype_info == 'U')
4c4b4cd2
PH
8199 {
8200 if (!ada_scan_number (bounds_str, n, &U, &n)
8201 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
8202 return raw_type;
8203 }
d2e4a39e 8204 else
4c4b4cd2
PH
8205 {
8206 int ok;
8207 strcpy (name_buf + prefix_len, "___U");
8208 U = get_int_var_value (name_buf, &ok);
8209 if (!ok)
8210 {
323e0a4a 8211 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
4c4b4cd2
PH
8212 U = L;
8213 }
8214 }
14f9c5c9 8215
d2e4a39e 8216 if (objfile == NULL)
4c4b4cd2 8217 objfile = TYPE_OBJFILE (base_type);
14f9c5c9 8218 type = create_range_type (alloc_type (objfile), base_type, L, U);
d2e4a39e 8219 TYPE_NAME (type) = name;
14f9c5c9
AS
8220 return type;
8221 }
8222}
8223
4c4b4cd2
PH
8224/* True iff NAME is the name of a range type. */
8225
14f9c5c9 8226int
d2e4a39e 8227ada_is_range_type_name (const char *name)
14f9c5c9
AS
8228{
8229 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 8230}
14f9c5c9 8231\f
d2e4a39e 8232
4c4b4cd2
PH
8233 /* Modular types */
8234
8235/* True iff TYPE is an Ada modular type. */
14f9c5c9 8236
14f9c5c9 8237int
d2e4a39e 8238ada_is_modular_type (struct type *type)
14f9c5c9 8239{
4c4b4cd2 8240 struct type *subranged_type = base_type (type);
14f9c5c9
AS
8241
8242 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
4c4b4cd2
PH
8243 && TYPE_CODE (subranged_type) != TYPE_CODE_ENUM
8244 && TYPE_UNSIGNED (subranged_type));
14f9c5c9
AS
8245}
8246
4c4b4cd2
PH
8247/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
8248
61ee279c 8249ULONGEST
d2e4a39e 8250ada_modulus (struct type * type)
14f9c5c9 8251{
61ee279c 8252 return (ULONGEST) TYPE_HIGH_BOUND (type) + 1;
14f9c5c9 8253}
d2e4a39e 8254\f
4c4b4cd2
PH
8255 /* Operators */
8256/* Information about operators given special treatment in functions
8257 below. */
8258/* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
8259
8260#define ADA_OPERATORS \
8261 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
8262 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
8263 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
8264 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
8265 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
8266 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
8267 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
8268 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
8269 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
8270 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
8271 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
8272 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
8273 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
8274 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
8275 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
8276 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0)
8277
8278static void
8279ada_operator_length (struct expression *exp, int pc, int *oplenp, int *argsp)
8280{
8281 switch (exp->elts[pc - 1].opcode)
8282 {
76a01679 8283 default:
4c4b4cd2
PH
8284 operator_length_standard (exp, pc, oplenp, argsp);
8285 break;
8286
8287#define OP_DEFN(op, len, args, binop) \
8288 case op: *oplenp = len; *argsp = args; break;
8289 ADA_OPERATORS;
8290#undef OP_DEFN
8291 }
8292}
8293
8294static char *
8295ada_op_name (enum exp_opcode opcode)
8296{
8297 switch (opcode)
8298 {
76a01679 8299 default:
4c4b4cd2
PH
8300 return op_name_standard (opcode);
8301#define OP_DEFN(op, len, args, binop) case op: return #op;
8302 ADA_OPERATORS;
8303#undef OP_DEFN
8304 }
8305}
8306
8307/* As for operator_length, but assumes PC is pointing at the first
8308 element of the operator, and gives meaningful results only for the
8309 Ada-specific operators. */
8310
8311static void
76a01679
JB
8312ada_forward_operator_length (struct expression *exp, int pc,
8313 int *oplenp, int *argsp)
4c4b4cd2 8314{
76a01679 8315 switch (exp->elts[pc].opcode)
4c4b4cd2
PH
8316 {
8317 default:
8318 *oplenp = *argsp = 0;
8319 break;
8320#define OP_DEFN(op, len, args, binop) \
8321 case op: *oplenp = len; *argsp = args; break;
8322 ADA_OPERATORS;
8323#undef OP_DEFN
8324 }
8325}
8326
8327static int
8328ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
8329{
8330 enum exp_opcode op = exp->elts[elt].opcode;
8331 int oplen, nargs;
8332 int pc = elt;
8333 int i;
76a01679 8334
4c4b4cd2
PH
8335 ada_forward_operator_length (exp, elt, &oplen, &nargs);
8336
76a01679 8337 switch (op)
4c4b4cd2 8338 {
76a01679 8339 /* Ada attributes ('Foo). */
4c4b4cd2
PH
8340 case OP_ATR_FIRST:
8341 case OP_ATR_LAST:
8342 case OP_ATR_LENGTH:
8343 case OP_ATR_IMAGE:
8344 case OP_ATR_MAX:
8345 case OP_ATR_MIN:
8346 case OP_ATR_MODULUS:
8347 case OP_ATR_POS:
8348 case OP_ATR_SIZE:
8349 case OP_ATR_TAG:
8350 case OP_ATR_VAL:
8351 break;
8352
8353 case UNOP_IN_RANGE:
8354 case UNOP_QUAL:
323e0a4a
AC
8355 /* XXX: gdb_sprint_host_address, type_sprint */
8356 fprintf_filtered (stream, _("Type @"));
4c4b4cd2
PH
8357 gdb_print_host_address (exp->elts[pc + 1].type, stream);
8358 fprintf_filtered (stream, " (");
8359 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
8360 fprintf_filtered (stream, ")");
8361 break;
8362 case BINOP_IN_BOUNDS:
8363 fprintf_filtered (stream, " (%d)", (int) exp->elts[pc + 2].longconst);
8364 break;
8365 case TERNOP_IN_RANGE:
8366 break;
8367
8368 default:
8369 return dump_subexp_body_standard (exp, stream, elt);
8370 }
8371
8372 elt += oplen;
8373 for (i = 0; i < nargs; i += 1)
8374 elt = dump_subexp (exp, stream, elt);
8375
8376 return elt;
8377}
8378
8379/* The Ada extension of print_subexp (q.v.). */
8380
76a01679
JB
8381static void
8382ada_print_subexp (struct expression *exp, int *pos,
8383 struct ui_file *stream, enum precedence prec)
4c4b4cd2
PH
8384{
8385 int oplen, nargs;
8386 int pc = *pos;
8387 enum exp_opcode op = exp->elts[pc].opcode;
8388
8389 ada_forward_operator_length (exp, pc, &oplen, &nargs);
8390
8391 switch (op)
8392 {
8393 default:
8394 print_subexp_standard (exp, pos, stream, prec);
8395 return;
8396
8397 case OP_VAR_VALUE:
8398 *pos += oplen;
8399 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
8400 return;
8401
8402 case BINOP_IN_BOUNDS:
323e0a4a 8403 /* XXX: sprint_subexp */
4c4b4cd2
PH
8404 *pos += oplen;
8405 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 8406 fputs_filtered (" in ", stream);
4c4b4cd2 8407 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 8408 fputs_filtered ("'range", stream);
4c4b4cd2 8409 if (exp->elts[pc + 1].longconst > 1)
76a01679
JB
8410 fprintf_filtered (stream, "(%ld)",
8411 (long) exp->elts[pc + 1].longconst);
4c4b4cd2
PH
8412 return;
8413
8414 case TERNOP_IN_RANGE:
8415 *pos += oplen;
8416 if (prec >= PREC_EQUAL)
76a01679 8417 fputs_filtered ("(", stream);
323e0a4a 8418 /* XXX: sprint_subexp */
4c4b4cd2 8419 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 8420 fputs_filtered (" in ", stream);
4c4b4cd2
PH
8421 print_subexp (exp, pos, stream, PREC_EQUAL);
8422 fputs_filtered (" .. ", stream);
8423 print_subexp (exp, pos, stream, PREC_EQUAL);
8424 if (prec >= PREC_EQUAL)
76a01679
JB
8425 fputs_filtered (")", stream);
8426 return;
4c4b4cd2
PH
8427
8428 case OP_ATR_FIRST:
8429 case OP_ATR_LAST:
8430 case OP_ATR_LENGTH:
8431 case OP_ATR_IMAGE:
8432 case OP_ATR_MAX:
8433 case OP_ATR_MIN:
8434 case OP_ATR_MODULUS:
8435 case OP_ATR_POS:
8436 case OP_ATR_SIZE:
8437 case OP_ATR_TAG:
8438 case OP_ATR_VAL:
8439 *pos += oplen;
8440 if (exp->elts[*pos].opcode == OP_TYPE)
76a01679
JB
8441 {
8442 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
8443 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0);
8444 *pos += 3;
8445 }
4c4b4cd2 8446 else
76a01679 8447 print_subexp (exp, pos, stream, PREC_SUFFIX);
4c4b4cd2
PH
8448 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
8449 if (nargs > 1)
76a01679
JB
8450 {
8451 int tem;
8452 for (tem = 1; tem < nargs; tem += 1)
8453 {
8454 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
8455 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
8456 }
8457 fputs_filtered (")", stream);
8458 }
4c4b4cd2 8459 return;
14f9c5c9 8460
4c4b4cd2
PH
8461 case UNOP_QUAL:
8462 *pos += oplen;
8463 type_print (exp->elts[pc + 1].type, "", stream, 0);
8464 fputs_filtered ("'(", stream);
8465 print_subexp (exp, pos, stream, PREC_PREFIX);
8466 fputs_filtered (")", stream);
8467 return;
14f9c5c9 8468
4c4b4cd2
PH
8469 case UNOP_IN_RANGE:
8470 *pos += oplen;
323e0a4a 8471 /* XXX: sprint_subexp */
4c4b4cd2 8472 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 8473 fputs_filtered (" in ", stream);
4c4b4cd2
PH
8474 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0);
8475 return;
8476 }
8477}
14f9c5c9
AS
8478
8479/* Table mapping opcodes into strings for printing operators
8480 and precedences of the operators. */
8481
d2e4a39e
AS
8482static const struct op_print ada_op_print_tab[] = {
8483 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
8484 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
8485 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
8486 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
8487 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
8488 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
8489 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
8490 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
8491 {"<=", BINOP_LEQ, PREC_ORDER, 0},
8492 {">=", BINOP_GEQ, PREC_ORDER, 0},
8493 {">", BINOP_GTR, PREC_ORDER, 0},
8494 {"<", BINOP_LESS, PREC_ORDER, 0},
8495 {">>", BINOP_RSH, PREC_SHIFT, 0},
8496 {"<<", BINOP_LSH, PREC_SHIFT, 0},
8497 {"+", BINOP_ADD, PREC_ADD, 0},
8498 {"-", BINOP_SUB, PREC_ADD, 0},
8499 {"&", BINOP_CONCAT, PREC_ADD, 0},
8500 {"*", BINOP_MUL, PREC_MUL, 0},
8501 {"/", BINOP_DIV, PREC_MUL, 0},
8502 {"rem", BINOP_REM, PREC_MUL, 0},
8503 {"mod", BINOP_MOD, PREC_MUL, 0},
8504 {"**", BINOP_EXP, PREC_REPEAT, 0},
8505 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
8506 {"-", UNOP_NEG, PREC_PREFIX, 0},
8507 {"+", UNOP_PLUS, PREC_PREFIX, 0},
8508 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
8509 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
8510 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
4c4b4cd2
PH
8511 {".all", UNOP_IND, PREC_SUFFIX, 1},
8512 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
8513 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
d2e4a39e 8514 {NULL, 0, 0, 0}
14f9c5c9
AS
8515};
8516\f
6c038f32 8517 /* Fundamental Ada Types */
14f9c5c9
AS
8518
8519/* Create a fundamental Ada type using default reasonable for the current
8520 target machine.
8521
8522 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
8523 define fundamental types such as "int" or "double". Others (stabs or
8524 DWARF version 2, etc) do define fundamental types. For the formats which
8525 don't provide fundamental types, gdb can create such types using this
8526 function.
8527
8528 FIXME: Some compilers distinguish explicitly signed integral types
8529 (signed short, signed int, signed long) from "regular" integral types
8530 (short, int, long) in the debugging information. There is some dis-
8531 agreement as to how useful this feature is. In particular, gcc does
8532 not support this. Also, only some debugging formats allow the
8533 distinction to be passed on to a debugger. For now, we always just
8534 use "short", "int", or "long" as the type name, for both the implicit
8535 and explicitly signed types. This also makes life easier for the
8536 gdb test suite since we don't have to account for the differences
8537 in output depending upon what the compiler and debugging format
8538 support. We will probably have to re-examine the issue when gdb
8539 starts taking it's fundamental type information directly from the
8540 debugging information supplied by the compiler. fnf@cygnus.com */
8541
8542static struct type *
ebf56fd3 8543ada_create_fundamental_type (struct objfile *objfile, int typeid)
14f9c5c9
AS
8544{
8545 struct type *type = NULL;
8546
8547 switch (typeid)
8548 {
d2e4a39e
AS
8549 default:
8550 /* FIXME: For now, if we are asked to produce a type not in this
8551 language, create the equivalent of a C integer type with the
8552 name "<?type?>". When all the dust settles from the type
4c4b4cd2 8553 reconstruction work, this should probably become an error. */
d2e4a39e 8554 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8555 TARGET_INT_BIT / TARGET_CHAR_BIT,
8556 0, "<?type?>", objfile);
323e0a4a 8557 warning (_("internal error: no Ada fundamental type %d"), typeid);
d2e4a39e
AS
8558 break;
8559 case FT_VOID:
8560 type = init_type (TYPE_CODE_VOID,
4c4b4cd2
PH
8561 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8562 0, "void", objfile);
d2e4a39e
AS
8563 break;
8564 case FT_CHAR:
8565 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8566 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8567 0, "character", objfile);
d2e4a39e
AS
8568 break;
8569 case FT_SIGNED_CHAR:
8570 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8571 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8572 0, "signed char", objfile);
d2e4a39e
AS
8573 break;
8574 case FT_UNSIGNED_CHAR:
8575 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8576 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8577 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
d2e4a39e
AS
8578 break;
8579 case FT_SHORT:
8580 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8581 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8582 0, "short_integer", objfile);
d2e4a39e
AS
8583 break;
8584 case FT_SIGNED_SHORT:
8585 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8586 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8587 0, "short_integer", objfile);
d2e4a39e
AS
8588 break;
8589 case FT_UNSIGNED_SHORT:
8590 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8591 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8592 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
d2e4a39e
AS
8593 break;
8594 case FT_INTEGER:
8595 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8596 TARGET_INT_BIT / TARGET_CHAR_BIT,
8597 0, "integer", objfile);
d2e4a39e
AS
8598 break;
8599 case FT_SIGNED_INTEGER:
72d5681a
PH
8600 type = init_type (TYPE_CODE_INT, TARGET_INT_BIT /
8601 TARGET_CHAR_BIT,
8602 0, "integer", objfile); /* FIXME -fnf */
d2e4a39e
AS
8603 break;
8604 case FT_UNSIGNED_INTEGER:
8605 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8606 TARGET_INT_BIT / TARGET_CHAR_BIT,
8607 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
d2e4a39e
AS
8608 break;
8609 case FT_LONG:
8610 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8611 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8612 0, "long_integer", objfile);
d2e4a39e
AS
8613 break;
8614 case FT_SIGNED_LONG:
8615 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8616 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8617 0, "long_integer", objfile);
d2e4a39e
AS
8618 break;
8619 case FT_UNSIGNED_LONG:
8620 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8621 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8622 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
d2e4a39e
AS
8623 break;
8624 case FT_LONG_LONG:
8625 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8626 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8627 0, "long_long_integer", objfile);
d2e4a39e
AS
8628 break;
8629 case FT_SIGNED_LONG_LONG:
8630 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8631 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8632 0, "long_long_integer", objfile);
d2e4a39e
AS
8633 break;
8634 case FT_UNSIGNED_LONG_LONG:
8635 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
8636 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8637 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
d2e4a39e
AS
8638 break;
8639 case FT_FLOAT:
8640 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
8641 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
8642 0, "float", objfile);
d2e4a39e
AS
8643 break;
8644 case FT_DBL_PREC_FLOAT:
8645 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
8646 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
8647 0, "long_float", objfile);
d2e4a39e
AS
8648 break;
8649 case FT_EXT_PREC_FLOAT:
8650 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
8651 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
8652 0, "long_long_float", objfile);
d2e4a39e
AS
8653 break;
8654 }
14f9c5c9
AS
8655 return (type);
8656}
8657
72d5681a
PH
8658enum ada_primitive_types {
8659 ada_primitive_type_int,
8660 ada_primitive_type_long,
8661 ada_primitive_type_short,
8662 ada_primitive_type_char,
8663 ada_primitive_type_float,
8664 ada_primitive_type_double,
8665 ada_primitive_type_void,
8666 ada_primitive_type_long_long,
8667 ada_primitive_type_long_double,
8668 ada_primitive_type_natural,
8669 ada_primitive_type_positive,
8670 ada_primitive_type_system_address,
8671 nr_ada_primitive_types
8672};
6c038f32
PH
8673
8674static void
72d5681a
PH
8675ada_language_arch_info (struct gdbarch *current_gdbarch,
8676 struct language_arch_info *lai)
8677{
8678 const struct builtin_type *builtin = builtin_type (current_gdbarch);
8679 lai->primitive_type_vector
8680 = GDBARCH_OBSTACK_CALLOC (current_gdbarch, nr_ada_primitive_types + 1,
8681 struct type *);
8682 lai->primitive_type_vector [ada_primitive_type_int] =
6c038f32
PH
8683 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
8684 0, "integer", (struct objfile *) NULL);
72d5681a 8685 lai->primitive_type_vector [ada_primitive_type_long] =
6c038f32
PH
8686 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
8687 0, "long_integer", (struct objfile *) NULL);
72d5681a 8688 lai->primitive_type_vector [ada_primitive_type_short] =
6c038f32
PH
8689 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8690 0, "short_integer", (struct objfile *) NULL);
61ee279c
PH
8691 lai->string_char_type =
8692 lai->primitive_type_vector [ada_primitive_type_char] =
6c038f32
PH
8693 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8694 0, "character", (struct objfile *) NULL);
72d5681a 8695 lai->primitive_type_vector [ada_primitive_type_float] =
6c038f32
PH
8696 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
8697 0, "float", (struct objfile *) NULL);
72d5681a 8698 lai->primitive_type_vector [ada_primitive_type_double] =
6c038f32
PH
8699 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
8700 0, "long_float", (struct objfile *) NULL);
72d5681a 8701 lai->primitive_type_vector [ada_primitive_type_long_long] =
6c038f32
PH
8702 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8703 0, "long_long_integer", (struct objfile *) NULL);
72d5681a 8704 lai->primitive_type_vector [ada_primitive_type_long_double] =
6c038f32
PH
8705 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
8706 0, "long_long_float", (struct objfile *) NULL);
72d5681a 8707 lai->primitive_type_vector [ada_primitive_type_natural] =
6c038f32
PH
8708 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
8709 0, "natural", (struct objfile *) NULL);
72d5681a 8710 lai->primitive_type_vector [ada_primitive_type_positive] =
6c038f32
PH
8711 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
8712 0, "positive", (struct objfile *) NULL);
72d5681a 8713 lai->primitive_type_vector [ada_primitive_type_void] = builtin->builtin_void;
6c038f32 8714
72d5681a 8715 lai->primitive_type_vector [ada_primitive_type_system_address] =
6c038f32
PH
8716 lookup_pointer_type (init_type (TYPE_CODE_VOID, 1, 0, "void",
8717 (struct objfile *) NULL));
72d5681a
PH
8718 TYPE_NAME (lai->primitive_type_vector [ada_primitive_type_system_address])
8719 = "system__address";
6c038f32 8720}
6c038f32
PH
8721\f
8722 /* Language vector */
8723
8724/* Not really used, but needed in the ada_language_defn. */
8725
8726static void
8727emit_char (int c, struct ui_file *stream, int quoter)
8728{
8729 ada_emit_char (c, stream, quoter, 1);
8730}
8731
8732static int
8733parse (void)
8734{
8735 warnings_issued = 0;
8736 return ada_parse ();
8737}
8738
8739static const struct exp_descriptor ada_exp_descriptor = {
8740 ada_print_subexp,
8741 ada_operator_length,
8742 ada_op_name,
8743 ada_dump_subexp_body,
8744 ada_evaluate_subexp
8745};
8746
8747const struct language_defn ada_language_defn = {
8748 "ada", /* Language name */
8749 language_ada,
72d5681a 8750 NULL,
6c038f32
PH
8751 range_check_off,
8752 type_check_off,
8753 case_sensitive_on, /* Yes, Ada is case-insensitive, but
8754 that's not quite what this means. */
6c038f32
PH
8755 array_row_major,
8756 &ada_exp_descriptor,
8757 parse,
8758 ada_error,
8759 resolve,
8760 ada_printchar, /* Print a character constant */
8761 ada_printstr, /* Function to print string constant */
8762 emit_char, /* Function to print single char (not used) */
8763 ada_create_fundamental_type, /* Create fundamental type in this language */
8764 ada_print_type, /* Print a type using appropriate syntax */
8765 ada_val_print, /* Print a value using appropriate syntax */
8766 ada_value_print, /* Print a top-level value */
8767 NULL, /* Language specific skip_trampoline */
8768 NULL, /* value_of_this */
8769 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
8770 basic_lookup_transparent_type, /* lookup_transparent_type */
8771 ada_la_decode, /* Language specific symbol demangler */
8772 NULL, /* Language specific class_name_from_physname */
8773 ada_op_print_tab, /* expression operators for printing */
8774 0, /* c-style arrays */
8775 1, /* String lower bound */
72d5681a 8776 NULL,
6c038f32 8777 ada_get_gdb_completer_word_break_characters,
72d5681a 8778 ada_language_arch_info,
e79af960 8779 ada_print_array_index,
6c038f32
PH
8780 LANG_MAGIC
8781};
8782
d2e4a39e 8783void
6c038f32 8784_initialize_ada_language (void)
14f9c5c9 8785{
6c038f32
PH
8786 add_language (&ada_language_defn);
8787
8788 varsize_limit = 65536;
6c038f32
PH
8789
8790 obstack_init (&symbol_list_obstack);
8791
8792 decoded_names_store = htab_create_alloc
8793 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
8794 NULL, xcalloc, xfree);
14f9c5c9 8795}
This page took 0.80884 seconds and 4 git commands to generate.