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