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