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