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