]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
Make ada_catchpoint_location's owner ctor parameter be ada_catchpoint
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
4a94e368 3 Copyright (C) 1992-2022 Free Software Foundation, Inc.
14f9c5c9 4
a9762ec7 5 This file is part of GDB.
14f9c5c9 6
a9762ec7
JB
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
14f9c5c9 11
a9762ec7
JB
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
14f9c5c9 16
a9762ec7
JB
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 19
96d887e8 20
4c4b4cd2 21#include "defs.h"
14f9c5c9 22#include <ctype.h>
d322d6d6 23#include "gdbsupport/gdb_regex.h"
4de283e4
TT
24#include "frame.h"
25#include "symtab.h"
26#include "gdbtypes.h"
14f9c5c9 27#include "gdbcmd.h"
4de283e4
TT
28#include "expression.h"
29#include "parser-defs.h"
30#include "language.h"
31#include "varobj.h"
4de283e4
TT
32#include "inferior.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "breakpoint.h"
14f9c5c9 36#include "gdbcore.h"
4c4b4cd2 37#include "hashtab.h"
bf31fd38 38#include "gdbsupport/gdb_obstack.h"
4de283e4
TT
39#include "ada-lang.h"
40#include "completer.h"
4de283e4
TT
41#include "ui-out.h"
42#include "block.h"
04714b91 43#include "infcall.h"
4de283e4
TT
44#include "annotate.h"
45#include "valprint.h"
d55e5aa6 46#include "source.h"
4de283e4 47#include "observable.h"
692465f1 48#include "stack.h"
79d43c61 49#include "typeprint.h"
4de283e4 50#include "namespace.h"
7f6aba03 51#include "cli/cli-style.h"
0f8e2034 52#include "cli/cli-decode.h"
4de283e4 53
40bc484c 54#include "value.h"
4de283e4
TT
55#include "mi/mi-common.h"
56#include "arch-utils.h"
57#include "cli/cli-utils.h"
268a13a5
TT
58#include "gdbsupport/function-view.h"
59#include "gdbsupport/byte-vector.h"
4de283e4 60#include <algorithm>
03070ee9 61#include "ada-exp.h"
315e4ebb 62#include "charset.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_args_match (struct symbol *, struct value **, int);
14f9c5c9 101
40bc484c 102static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 103
d1183b06 104static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
105 const struct block *,
106 const lookup_name_info &lookup_name,
107 domain_enum, struct objfile *);
14f9c5c9 108
d1183b06
TT
109static void ada_add_all_symbols (std::vector<struct block_symbol> &,
110 const struct block *,
b5ec771e
PA
111 const lookup_name_info &lookup_name,
112 domain_enum, int, int *);
22cee43f 113
d1183b06 114static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 115
d1183b06
TT
116static void add_defn_to_vec (std::vector<struct block_symbol> &,
117 struct symbol *,
dda83cd7 118 const struct block *);
14f9c5c9 119
d2e4a39e 120static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 121
4c4b4cd2 122static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 123
d2e4a39e 124static int numeric_type_p (struct type *);
14f9c5c9 125
d2e4a39e 126static int integer_type_p (struct type *);
14f9c5c9 127
d2e4a39e 128static int scalar_type_p (struct type *);
14f9c5c9 129
d2e4a39e 130static int discrete_type_p (struct type *);
14f9c5c9 131
a121b7c1 132static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 133 int, int);
4c4b4cd2 134
b4ba55a1 135static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 136 const char *);
b4ba55a1 137
d2e4a39e 138static int is_dynamic_field (struct type *, int);
14f9c5c9 139
10a2c479 140static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 141 const gdb_byte *,
dda83cd7 142 CORE_ADDR, struct value *);
4c4b4cd2
PH
143
144static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 145
28c85d6c 146static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 147
d2e4a39e 148static struct type *to_static_fixed_type (struct type *);
f192137b 149static struct type *static_unwrap_type (struct type *type);
14f9c5c9 150
d2e4a39e 151static struct value *unwrap_value (struct value *);
14f9c5c9 152
ad82864c 153static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 154
ad82864c 155static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 156
ad82864c
JB
157static long decode_packed_array_bitsize (struct type *);
158
159static struct value *decode_constrained_packed_array (struct value *);
160
ad82864c 161static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 162
d2e4a39e 163static struct value *value_subscript_packed (struct value *, int,
dda83cd7 164 struct value **);
14f9c5c9 165
4c4b4cd2 166static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 167 struct type *);
14f9c5c9 168
d2e4a39e 169static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 170
d2e4a39e 171static int equiv_types (struct type *, struct type *);
14f9c5c9 172
d2e4a39e 173static int is_name_suffix (const char *);
14f9c5c9 174
59c8a30b 175static int advance_wild_match (const char **, const char *, char);
73589123 176
b5ec771e 177static bool wild_match (const char *name, const char *patn);
14f9c5c9 178
d2e4a39e 179static struct value *ada_coerce_ref (struct value *);
14f9c5c9 180
4c4b4cd2
PH
181static LONGEST pos_atr (struct value *);
182
53a47a3e
TT
183static struct value *val_atr (struct type *, LONGEST);
184
4c4b4cd2 185static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 186 domain_enum);
14f9c5c9 187
108d56a4 188static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 189 struct type *);
4c4b4cd2 190
0d5cff50 191static int find_struct_field (const char *, struct type *, int,
dda83cd7 192 struct type **, int *, int *, int *, int *);
4c4b4cd2 193
d1183b06 194static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 195 struct value **, int, const char *,
7056f312 196 struct type *, bool);
4c4b4cd2 197
4c4b4cd2
PH
198static int ada_is_direct_array_type (struct type *);
199
52ce6436
PH
200static struct value *ada_index_struct_field (int, struct value *, int,
201 struct type *);
202
cf608cc4 203static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
204
205
852dff6c 206static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
207
208static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
209 (const lookup_name_info &lookup_name);
210
4c4b4cd2
PH
211\f
212
315e4ebb
TT
213/* The character set used for source files. */
214static const char *ada_source_charset;
215
216/* The string "UTF-8". This is here so we can check for the UTF-8
217 charset using == rather than strcmp. */
218static const char ada_utf8[] = "UTF-8";
219
220/* Each entry in the UTF-32 case-folding table is of this form. */
221struct utf8_entry
222{
223 /* The start and end, inclusive, of this range of codepoints. */
224 uint32_t start, end;
225 /* The delta to apply to get the upper-case form. 0 if this is
226 already upper-case. */
227 int upper_delta;
228 /* The delta to apply to get the lower-case form. 0 if this is
229 already lower-case. */
230 int lower_delta;
231
232 bool operator< (uint32_t val) const
233 {
234 return end < val;
235 }
236};
237
238static const utf8_entry ada_case_fold[] =
239{
240#include "ada-casefold.h"
241};
242
243\f
244
ee01b665
JB
245/* The result of a symbol lookup to be stored in our symbol cache. */
246
247struct cache_entry
248{
249 /* The name used to perform the lookup. */
250 const char *name;
251 /* The namespace used during the lookup. */
fe978cb0 252 domain_enum domain;
ee01b665
JB
253 /* The symbol returned by the lookup, or NULL if no matching symbol
254 was found. */
255 struct symbol *sym;
256 /* The block where the symbol was found, or NULL if no matching
257 symbol was found. */
258 const struct block *block;
259 /* A pointer to the next entry with the same hash. */
260 struct cache_entry *next;
261};
262
263/* The Ada symbol cache, used to store the result of Ada-mode symbol
264 lookups in the course of executing the user's commands.
265
266 The cache is implemented using a simple, fixed-sized hash.
267 The size is fixed on the grounds that there are not likely to be
268 all that many symbols looked up during any given session, regardless
269 of the size of the symbol table. If we decide to go to a resizable
270 table, let's just use the stuff from libiberty instead. */
271
272#define HASH_SIZE 1009
273
274struct ada_symbol_cache
275{
276 /* An obstack used to store the entries in our cache. */
bdcccc56 277 struct auto_obstack cache_space;
ee01b665
JB
278
279 /* The root of the hash table used to implement our symbol cache. */
bdcccc56 280 struct cache_entry *root[HASH_SIZE] {};
ee01b665
JB
281};
282
67cb5b2d 283static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
284#ifdef VMS
285 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
286#else
14f9c5c9 287 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 288#endif
14f9c5c9 289
4c4b4cd2 290/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 291static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 292 = "__gnat_ada_main_program_name";
14f9c5c9 293
4c4b4cd2
PH
294/* Limit on the number of warnings to raise per expression evaluation. */
295static int warning_limit = 2;
296
297/* Number of warning messages issued; reset to 0 by cleanups after
298 expression evaluation. */
299static int warnings_issued = 0;
300
27087b7f 301static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
302 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
303};
304
27087b7f 305static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
306 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
307};
308
c6044dd1
JB
309/* Maintenance-related settings for this module. */
310
311static struct cmd_list_element *maint_set_ada_cmdlist;
312static struct cmd_list_element *maint_show_ada_cmdlist;
313
c6044dd1
JB
314/* The "maintenance ada set/show ignore-descriptive-type" value. */
315
491144b5 316static bool ada_ignore_descriptive_types_p = false;
c6044dd1 317
e802dbe0
JB
318 /* Inferior-specific data. */
319
320/* Per-inferior data for this module. */
321
322struct ada_inferior_data
323{
324 /* The ada__tags__type_specific_data type, which is used when decoding
325 tagged types. With older versions of GNAT, this type was directly
326 accessible through a component ("tsd") in the object tag. But this
327 is no longer the case, so we cache it for each inferior. */
f37b313d 328 struct type *tsd_type = nullptr;
3eecfa55
JB
329
330 /* The exception_support_info data. This data is used to determine
331 how to implement support for Ada exception catchpoints in a given
332 inferior. */
f37b313d 333 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
334};
335
336/* Our key to this module's inferior data. */
f37b313d 337static const struct inferior_key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
338
339/* Return our inferior data for the given inferior (INF).
340
341 This function always returns a valid pointer to an allocated
342 ada_inferior_data structure. If INF's inferior data has not
343 been previously set, this functions creates a new one with all
344 fields set to zero, sets INF's inferior to it, and then returns
345 a pointer to that newly allocated ada_inferior_data. */
346
347static struct ada_inferior_data *
348get_ada_inferior_data (struct inferior *inf)
349{
350 struct ada_inferior_data *data;
351
f37b313d 352 data = ada_inferior_data.get (inf);
e802dbe0 353 if (data == NULL)
f37b313d 354 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
355
356 return data;
357}
358
359/* Perform all necessary cleanups regarding our module's inferior data
360 that is required after the inferior INF just exited. */
361
362static void
363ada_inferior_exit (struct inferior *inf)
364{
f37b313d 365 ada_inferior_data.clear (inf);
e802dbe0
JB
366}
367
ee01b665
JB
368
369 /* program-space-specific data. */
370
371/* This module's per-program-space data. */
372struct ada_pspace_data
373{
374 /* The Ada symbol cache. */
bdcccc56 375 std::unique_ptr<ada_symbol_cache> sym_cache;
ee01b665
JB
376};
377
378/* Key to our per-program-space data. */
f37b313d 379static const struct program_space_key<ada_pspace_data> ada_pspace_data_handle;
ee01b665
JB
380
381/* Return this module's data for the given program space (PSPACE).
382 If not is found, add a zero'ed one now.
383
384 This function always returns a valid object. */
385
386static struct ada_pspace_data *
387get_ada_pspace_data (struct program_space *pspace)
388{
389 struct ada_pspace_data *data;
390
f37b313d 391 data = ada_pspace_data_handle.get (pspace);
ee01b665 392 if (data == NULL)
f37b313d 393 data = ada_pspace_data_handle.emplace (pspace);
ee01b665
JB
394
395 return data;
396}
397
dda83cd7 398 /* Utilities */
4c4b4cd2 399
720d1a40 400/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 401 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
402
403 Normally, we really expect a typedef type to only have 1 typedef layer.
404 In other words, we really expect the target type of a typedef type to be
405 a non-typedef type. This is particularly true for Ada units, because
406 the language does not have a typedef vs not-typedef distinction.
407 In that respect, the Ada compiler has been trying to eliminate as many
408 typedef definitions in the debugging information, since they generally
409 do not bring any extra information (we still use typedef under certain
410 circumstances related mostly to the GNAT encoding).
411
412 Unfortunately, we have seen situations where the debugging information
413 generated by the compiler leads to such multiple typedef layers. For
414 instance, consider the following example with stabs:
415
416 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
417 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
418
419 This is an error in the debugging information which causes type
420 pck__float_array___XUP to be defined twice, and the second time,
421 it is defined as a typedef of a typedef.
422
423 This is on the fringe of legality as far as debugging information is
424 concerned, and certainly unexpected. But it is easy to handle these
425 situations correctly, so we can afford to be lenient in this case. */
426
427static struct type *
428ada_typedef_target_type (struct type *type)
429{
78134374 430 while (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
431 type = TYPE_TARGET_TYPE (type);
432 return type;
433}
434
41d27058
JB
435/* Given DECODED_NAME a string holding a symbol name in its
436 decoded form (ie using the Ada dotted notation), returns
437 its unqualified name. */
438
439static const char *
440ada_unqualified_name (const char *decoded_name)
441{
2b0f535a
JB
442 const char *result;
443
444 /* If the decoded name starts with '<', it means that the encoded
445 name does not follow standard naming conventions, and thus that
446 it is not your typical Ada symbol name. Trying to unqualify it
447 is therefore pointless and possibly erroneous. */
448 if (decoded_name[0] == '<')
449 return decoded_name;
450
451 result = strrchr (decoded_name, '.');
41d27058
JB
452 if (result != NULL)
453 result++; /* Skip the dot... */
454 else
455 result = decoded_name;
456
457 return result;
458}
459
39e7af3e 460/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 461
39e7af3e 462static std::string
41d27058
JB
463add_angle_brackets (const char *str)
464{
39e7af3e 465 return string_printf ("<%s>", str);
41d27058 466}
96d887e8 467
14f9c5c9 468/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 469 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
470
471static int
ebf56fd3 472field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
473{
474 int len = strlen (target);
5b4ee69b 475
d2e4a39e 476 return
4c4b4cd2
PH
477 (strncmp (field_name, target, len) == 0
478 && (field_name[len] == '\0'
dda83cd7
SM
479 || (startswith (field_name + len, "___")
480 && strcmp (field_name + strlen (field_name) - 6,
481 "___XVN") != 0)));
14f9c5c9
AS
482}
483
484
872c8b51
JB
485/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
486 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
487 and return its index. This function also handles fields whose name
488 have ___ suffixes because the compiler sometimes alters their name
489 by adding such a suffix to represent fields with certain constraints.
490 If the field could not be found, return a negative number if
491 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
492
493int
494ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 495 int maybe_missing)
4c4b4cd2
PH
496{
497 int fieldno;
872c8b51
JB
498 struct type *struct_type = check_typedef ((struct type *) type);
499
1f704f76 500 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 501 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
502 return fieldno;
503
504 if (!maybe_missing)
323e0a4a 505 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 506 field_name, struct_type->name ());
4c4b4cd2
PH
507
508 return -1;
509}
510
511/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
512
513int
d2e4a39e 514ada_name_prefix_len (const char *name)
14f9c5c9
AS
515{
516 if (name == NULL)
517 return 0;
d2e4a39e 518 else
14f9c5c9 519 {
d2e4a39e 520 const char *p = strstr (name, "___");
5b4ee69b 521
14f9c5c9 522 if (p == NULL)
dda83cd7 523 return strlen (name);
14f9c5c9 524 else
dda83cd7 525 return p - name;
14f9c5c9
AS
526 }
527}
528
4c4b4cd2
PH
529/* Return non-zero if SUFFIX is a suffix of STR.
530 Return zero if STR is null. */
531
14f9c5c9 532static int
d2e4a39e 533is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
534{
535 int len1, len2;
5b4ee69b 536
14f9c5c9
AS
537 if (str == NULL)
538 return 0;
539 len1 = strlen (str);
540 len2 = strlen (suffix);
4c4b4cd2 541 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
542}
543
4c4b4cd2
PH
544/* The contents of value VAL, treated as a value of type TYPE. The
545 result is an lval in memory if VAL is. */
14f9c5c9 546
d2e4a39e 547static struct value *
4c4b4cd2 548coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 549{
61ee279c 550 type = ada_check_typedef (type);
df407dfe 551 if (value_type (val) == type)
4c4b4cd2 552 return val;
d2e4a39e 553 else
14f9c5c9 554 {
4c4b4cd2
PH
555 struct value *result;
556
f73e424f
TT
557 if (value_optimized_out (val))
558 result = allocate_optimized_out_value (type);
559 else if (value_lazy (val)
560 /* Be careful not to make a lazy not_lval value. */
561 || (VALUE_LVAL (val) != not_lval
562 && TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val))))
41e8491f
JK
563 result = allocate_value_lazy (type);
564 else
565 {
566 result = allocate_value (type);
f73e424f 567 value_contents_copy (result, 0, val, 0, TYPE_LENGTH (type));
41e8491f 568 }
74bcbdf3 569 set_value_component_location (result, val);
9bbda503
AC
570 set_value_bitsize (result, value_bitsize (val));
571 set_value_bitpos (result, value_bitpos (val));
c408a94f
TT
572 if (VALUE_LVAL (result) == lval_memory)
573 set_value_address (result, value_address (val));
14f9c5c9
AS
574 return result;
575 }
576}
577
fc1a4b47
AC
578static const gdb_byte *
579cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
580{
581 if (valaddr == NULL)
582 return NULL;
583 else
584 return valaddr + offset;
585}
586
587static CORE_ADDR
ebf56fd3 588cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
589{
590 if (address == 0)
591 return 0;
d2e4a39e 592 else
14f9c5c9
AS
593 return address + offset;
594}
595
4c4b4cd2
PH
596/* Issue a warning (as for the definition of warning in utils.c, but
597 with exactly one argument rather than ...), unless the limit on the
598 number of warnings has passed during the evaluation of the current
599 expression. */
a2249542 600
77109804
AC
601/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
602 provided by "complaint". */
a0b31db1 603static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 604
14f9c5c9 605static void
a2249542 606lim_warning (const char *format, ...)
14f9c5c9 607{
a2249542 608 va_list args;
a2249542 609
5b4ee69b 610 va_start (args, format);
4c4b4cd2
PH
611 warnings_issued += 1;
612 if (warnings_issued <= warning_limit)
a2249542
MK
613 vwarning (format, args);
614
615 va_end (args);
4c4b4cd2
PH
616}
617
0963b4bd 618/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 619static LONGEST
c3e5cd34 620max_of_size (int size)
4c4b4cd2 621{
76a01679 622 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 623
76a01679 624 return top_bit | (top_bit - 1);
4c4b4cd2
PH
625}
626
0963b4bd 627/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 628static LONGEST
c3e5cd34 629min_of_size (int size)
4c4b4cd2 630{
c3e5cd34 631 return -max_of_size (size) - 1;
4c4b4cd2
PH
632}
633
0963b4bd 634/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 635static ULONGEST
c3e5cd34 636umax_of_size (int size)
4c4b4cd2 637{
76a01679 638 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 639
76a01679 640 return top_bit | (top_bit - 1);
4c4b4cd2
PH
641}
642
0963b4bd 643/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
644static LONGEST
645max_of_type (struct type *t)
4c4b4cd2 646{
c6d940a9 647 if (t->is_unsigned ())
c3e5cd34
PH
648 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
649 else
650 return max_of_size (TYPE_LENGTH (t));
651}
652
0963b4bd 653/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
654static LONGEST
655min_of_type (struct type *t)
656{
c6d940a9 657 if (t->is_unsigned ())
c3e5cd34
PH
658 return 0;
659 else
660 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
661}
662
663/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
664LONGEST
665ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 666{
b249d2c2 667 type = resolve_dynamic_type (type, {}, 0);
78134374 668 switch (type->code ())
4c4b4cd2
PH
669 {
670 case TYPE_CODE_RANGE:
d1fd641e
SM
671 {
672 const dynamic_prop &high = type->bounds ()->high;
673
674 if (high.kind () == PROP_CONST)
675 return high.const_val ();
676 else
677 {
678 gdb_assert (high.kind () == PROP_UNDEFINED);
679
680 /* This happens when trying to evaluate a type's dynamic bound
681 without a live target. There is nothing relevant for us to
682 return here, so return 0. */
683 return 0;
684 }
685 }
4c4b4cd2 686 case TYPE_CODE_ENUM:
970db518 687 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
688 case TYPE_CODE_BOOL:
689 return 1;
690 case TYPE_CODE_CHAR:
76a01679 691 case TYPE_CODE_INT:
690cc4eb 692 return max_of_type (type);
4c4b4cd2 693 default:
43bbcdc2 694 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
695 }
696}
697
14e75d8e 698/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
699LONGEST
700ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 701{
b249d2c2 702 type = resolve_dynamic_type (type, {}, 0);
78134374 703 switch (type->code ())
4c4b4cd2
PH
704 {
705 case TYPE_CODE_RANGE:
d1fd641e
SM
706 {
707 const dynamic_prop &low = type->bounds ()->low;
708
709 if (low.kind () == PROP_CONST)
710 return low.const_val ();
711 else
712 {
713 gdb_assert (low.kind () == PROP_UNDEFINED);
714
715 /* This happens when trying to evaluate a type's dynamic bound
716 without a live target. There is nothing relevant for us to
717 return here, so return 0. */
718 return 0;
719 }
720 }
4c4b4cd2 721 case TYPE_CODE_ENUM:
970db518 722 return type->field (0).loc_enumval ();
690cc4eb
PH
723 case TYPE_CODE_BOOL:
724 return 0;
725 case TYPE_CODE_CHAR:
76a01679 726 case TYPE_CODE_INT:
690cc4eb 727 return min_of_type (type);
4c4b4cd2 728 default:
43bbcdc2 729 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
730 }
731}
732
733/* The identity on non-range types. For range types, the underlying
76a01679 734 non-range scalar type. */
4c4b4cd2
PH
735
736static struct type *
18af8284 737get_base_type (struct type *type)
4c4b4cd2 738{
78134374 739 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 740 {
76a01679 741 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
dda83cd7 742 return type;
4c4b4cd2
PH
743 type = TYPE_TARGET_TYPE (type);
744 }
745 return type;
14f9c5c9 746}
41246937
JB
747
748/* Return a decoded version of the given VALUE. This means returning
749 a value whose type is obtained by applying all the GNAT-specific
85102364 750 encodings, making the resulting type a static but standard description
41246937
JB
751 of the initial type. */
752
753struct value *
754ada_get_decoded_value (struct value *value)
755{
756 struct type *type = ada_check_typedef (value_type (value));
757
758 if (ada_is_array_descriptor_type (type)
759 || (ada_is_constrained_packed_array_type (type)
dda83cd7 760 && type->code () != TYPE_CODE_PTR))
41246937 761 {
78134374 762 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 763 value = ada_coerce_to_simple_array_ptr (value);
41246937 764 else
dda83cd7 765 value = ada_coerce_to_simple_array (value);
41246937
JB
766 }
767 else
768 value = ada_to_fixed_value (value);
769
770 return value;
771}
772
773/* Same as ada_get_decoded_value, but with the given TYPE.
774 Because there is no associated actual value for this type,
775 the resulting type might be a best-effort approximation in
776 the case of dynamic types. */
777
778struct type *
779ada_get_decoded_type (struct type *type)
780{
781 type = to_static_fixed_type (type);
782 if (ada_is_constrained_packed_array_type (type))
783 type = ada_coerce_to_simple_array_type (type);
784 return type;
785}
786
4c4b4cd2 787\f
76a01679 788
dda83cd7 789 /* Language Selection */
14f9c5c9
AS
790
791/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 792 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 793
de93309a 794static enum language
ccefe4c4 795ada_update_initial_language (enum language lang)
14f9c5c9 796{
cafb3438 797 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 798 return language_ada;
14f9c5c9
AS
799
800 return lang;
801}
96d887e8
PH
802
803/* If the main procedure is written in Ada, then return its name.
804 The result is good until the next call. Return NULL if the main
805 procedure doesn't appear to be in Ada. */
806
807char *
808ada_main_name (void)
809{
3b7344d5 810 struct bound_minimal_symbol msym;
e83e4e24 811 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 812
96d887e8
PH
813 /* For Ada, the name of the main procedure is stored in a specific
814 string constant, generated by the binder. Look for that symbol,
815 extract its address, and then read that string. If we didn't find
816 that string, then most probably the main procedure is not written
817 in Ada. */
818 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
819
3b7344d5 820 if (msym.minsym != NULL)
96d887e8 821 {
4aeddc50 822 CORE_ADDR main_program_name_addr = msym.value_address ();
96d887e8 823 if (main_program_name_addr == 0)
dda83cd7 824 error (_("Invalid address for Ada main program name."));
96d887e8 825
66920317 826 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 827 return main_program_name.get ();
96d887e8
PH
828 }
829
830 /* The main procedure doesn't seem to be in Ada. */
831 return NULL;
832}
14f9c5c9 833\f
dda83cd7 834 /* Symbols */
d2e4a39e 835
4c4b4cd2
PH
836/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
837 of NULLs. */
14f9c5c9 838
d2e4a39e
AS
839const struct ada_opname_map ada_opname_table[] = {
840 {"Oadd", "\"+\"", BINOP_ADD},
841 {"Osubtract", "\"-\"", BINOP_SUB},
842 {"Omultiply", "\"*\"", BINOP_MUL},
843 {"Odivide", "\"/\"", BINOP_DIV},
844 {"Omod", "\"mod\"", BINOP_MOD},
845 {"Orem", "\"rem\"", BINOP_REM},
846 {"Oexpon", "\"**\"", BINOP_EXP},
847 {"Olt", "\"<\"", BINOP_LESS},
848 {"Ole", "\"<=\"", BINOP_LEQ},
849 {"Ogt", "\">\"", BINOP_GTR},
850 {"Oge", "\">=\"", BINOP_GEQ},
851 {"Oeq", "\"=\"", BINOP_EQUAL},
852 {"One", "\"/=\"", BINOP_NOTEQUAL},
853 {"Oand", "\"and\"", BINOP_BITWISE_AND},
854 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
855 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
856 {"Oconcat", "\"&\"", BINOP_CONCAT},
857 {"Oabs", "\"abs\"", UNOP_ABS},
858 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
859 {"Oadd", "\"+\"", UNOP_PLUS},
860 {"Osubtract", "\"-\"", UNOP_NEG},
861 {NULL, NULL}
14f9c5c9
AS
862};
863
965bc1df
TT
864/* If STR is a decoded version of a compiler-provided suffix (like the
865 "[cold]" in "symbol[cold]"), return true. Otherwise, return
866 false. */
867
868static bool
869is_compiler_suffix (const char *str)
870{
871 gdb_assert (*str == '[');
872 ++str;
873 while (*str != '\0' && isalpha (*str))
874 ++str;
875 /* We accept a missing "]" in order to support completion. */
876 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
877}
878
315e4ebb
TT
879/* Append a non-ASCII character to RESULT. */
880static void
881append_hex_encoded (std::string &result, uint32_t one_char)
882{
883 if (one_char <= 0xff)
884 {
885 result.append ("U");
886 result.append (phex (one_char, 1));
887 }
888 else if (one_char <= 0xffff)
889 {
890 result.append ("W");
891 result.append (phex (one_char, 2));
892 }
893 else
894 {
895 result.append ("WW");
896 result.append (phex (one_char, 4));
897 }
898}
899
900/* Return a string that is a copy of the data in STORAGE, with
901 non-ASCII characters replaced by the appropriate hex encoding. A
902 template is used because, for UTF-8, we actually want to work with
903 UTF-32 codepoints. */
904template<typename T>
905std::string
906copy_and_hex_encode (struct obstack *storage)
907{
908 const T *chars = (T *) obstack_base (storage);
909 int num_chars = obstack_object_size (storage) / sizeof (T);
910 std::string result;
911 for (int i = 0; i < num_chars; ++i)
912 {
913 if (chars[i] <= 0x7f)
914 {
915 /* The host character set has to be a superset of ASCII, as
916 are all the other character sets we can use. */
917 result.push_back (chars[i]);
918 }
919 else
920 append_hex_encoded (result, chars[i]);
921 }
922 return result;
923}
924
5c4258f4 925/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 926 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 927 Otherwise, return the empty string in that case. */
4c4b4cd2 928
5c4258f4 929static std::string
b5ec771e 930ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 931{
4c4b4cd2 932 if (decoded == NULL)
5c4258f4 933 return {};
14f9c5c9 934
5c4258f4 935 std::string encoding_buffer;
315e4ebb 936 bool saw_non_ascii = false;
5c4258f4 937 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 938 {
315e4ebb
TT
939 if ((*p & 0x80) != 0)
940 saw_non_ascii = true;
941
cdc7bb92 942 if (*p == '.')
5c4258f4 943 encoding_buffer.append ("__");
965bc1df
TT
944 else if (*p == '[' && is_compiler_suffix (p))
945 {
946 encoding_buffer = encoding_buffer + "." + (p + 1);
947 if (encoding_buffer.back () == ']')
948 encoding_buffer.pop_back ();
949 break;
950 }
14f9c5c9 951 else if (*p == '"')
dda83cd7
SM
952 {
953 const struct ada_opname_map *mapping;
954
955 for (mapping = ada_opname_table;
956 mapping->encoded != NULL
957 && !startswith (p, mapping->decoded); mapping += 1)
958 ;
959 if (mapping->encoded == NULL)
b5ec771e
PA
960 {
961 if (throw_errors)
962 error (_("invalid Ada operator name: %s"), p);
963 else
5c4258f4 964 return {};
b5ec771e 965 }
5c4258f4 966 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
967 break;
968 }
d2e4a39e 969 else
5c4258f4 970 encoding_buffer.push_back (*p);
14f9c5c9
AS
971 }
972
315e4ebb
TT
973 /* If a non-ASCII character is seen, we must convert it to the
974 appropriate hex form. As this is more expensive, we keep track
975 of whether it is even necessary. */
976 if (saw_non_ascii)
977 {
978 auto_obstack storage;
979 bool is_utf8 = ada_source_charset == ada_utf8;
980 try
981 {
982 convert_between_encodings
983 (host_charset (),
984 is_utf8 ? HOST_UTF32 : ada_source_charset,
985 (const gdb_byte *) encoding_buffer.c_str (),
986 encoding_buffer.length (), 1,
987 &storage, translit_none);
988 }
989 catch (const gdb_exception &)
990 {
991 static bool warned = false;
992
993 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
994 might like to know why. */
995 if (!warned)
996 {
997 warned = true;
998 warning (_("charset conversion failure for '%s'.\n"
999 "You may have the wrong value for 'set ada source-charset'."),
1000 encoding_buffer.c_str ());
1001 }
1002
1003 /* We don't try to recover from errors. */
1004 return encoding_buffer;
1005 }
1006
1007 if (is_utf8)
1008 return copy_and_hex_encode<uint32_t> (&storage);
1009 return copy_and_hex_encode<gdb_byte> (&storage);
1010 }
1011
4c4b4cd2 1012 return encoding_buffer;
14f9c5c9
AS
1013}
1014
315e4ebb
TT
1015/* Find the entry for C in the case-folding table. Return nullptr if
1016 the entry does not cover C. */
1017static const utf8_entry *
1018find_case_fold_entry (uint32_t c)
b5ec771e 1019{
315e4ebb
TT
1020 auto iter = std::lower_bound (std::begin (ada_case_fold),
1021 std::end (ada_case_fold),
1022 c);
1023 if (iter == std::end (ada_case_fold)
1024 || c < iter->start
1025 || c > iter->end)
1026 return nullptr;
1027 return &*iter;
b5ec771e
PA
1028}
1029
14f9c5c9 1030/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1031 quotes, unfolded, but with the quotes stripped away. If
1032 THROW_ON_ERROR is true, encoding failures will throw an exception
1033 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1034
5f9febe0 1035static const char *
315e4ebb 1036ada_fold_name (gdb::string_view name, bool throw_on_error = false)
14f9c5c9 1037{
5f9febe0 1038 static std::string fold_storage;
14f9c5c9 1039
6a780b67 1040 if (!name.empty () && name[0] == '\'')
01573d73 1041 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
1042 else
1043 {
315e4ebb
TT
1044 /* Why convert to UTF-32 and implement our own case-folding,
1045 rather than convert to wchar_t and use the platform's
1046 functions? I'm glad you asked.
1047
1048 The main problem is that GNAT implements an unusual rule for
1049 case folding. For ASCII letters, letters in single-byte
1050 encodings (such as ISO-8859-*), and Unicode letters that fit
1051 in a single byte (i.e., code point is <= 0xff), the letter is
1052 folded to lower case. Other Unicode letters are folded to
1053 upper case.
1054
1055 This rule means that the code must be able to examine the
1056 value of the character. And, some hosts do not use Unicode
1057 for wchar_t, so examining the value of such characters is
1058 forbidden. */
1059 auto_obstack storage;
1060 try
1061 {
1062 convert_between_encodings
1063 (host_charset (), HOST_UTF32,
1064 (const gdb_byte *) name.data (),
1065 name.length (), 1,
1066 &storage, translit_none);
1067 }
1068 catch (const gdb_exception &)
1069 {
1070 if (throw_on_error)
1071 throw;
1072
1073 static bool warned = false;
1074
1075 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1076 might like to know why. */
1077 if (!warned)
1078 {
1079 warned = true;
1080 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1081 "This normally should not happen, please file a bug report."),
1082 gdb::to_string (name).c_str (), host_charset ());
1083 }
1084
1085 /* We don't try to recover from errors; just return the
1086 original string. */
1087 fold_storage = gdb::to_string (name);
1088 return fold_storage.c_str ();
1089 }
1090
1091 bool is_utf8 = ada_source_charset == ada_utf8;
1092 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1093 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1094 for (int i = 0; i < num_chars; ++i)
1095 {
1096 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1097 if (entry != nullptr)
1098 {
1099 uint32_t low = chars[i] + entry->lower_delta;
1100 if (!is_utf8 || low <= 0xff)
1101 chars[i] = low;
1102 else
1103 chars[i] = chars[i] + entry->upper_delta;
1104 }
1105 }
1106
1107 /* Now convert back to ordinary characters. */
1108 auto_obstack reconverted;
1109 try
1110 {
1111 convert_between_encodings (HOST_UTF32,
1112 host_charset (),
1113 (const gdb_byte *) chars,
1114 num_chars * sizeof (uint32_t),
1115 sizeof (uint32_t),
1116 &reconverted,
1117 translit_none);
1118 obstack_1grow (&reconverted, '\0');
1119 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1120 }
1121 catch (const gdb_exception &)
1122 {
1123 if (throw_on_error)
1124 throw;
1125
1126 static bool warned = false;
1127
1128 /* Converting back from UTF-32 shouldn't normally fail, but
1129 there are some host encodings without upper/lower
1130 equivalence. */
1131 if (!warned)
1132 {
1133 warned = true;
1134 warning (_("could not convert the lower-cased variant of '%s'\n"
1135 "from UTF-32 to the host encoding (%s)."),
1136 gdb::to_string (name).c_str (), host_charset ());
1137 }
1138
1139 /* We don't try to recover from errors; just return the
1140 original string. */
1141 fold_storage = gdb::to_string (name);
1142 }
14f9c5c9
AS
1143 }
1144
5f9febe0 1145 return fold_storage.c_str ();
14f9c5c9
AS
1146}
1147
315e4ebb
TT
1148/* The "encoded" form of DECODED, according to GNAT conventions. */
1149
1150std::string
1151ada_encode (const char *decoded)
1152{
1153 if (decoded[0] != '<')
1154 decoded = ada_fold_name (decoded);
1155 return ada_encode_1 (decoded, true);
1156}
1157
529cad9c
PH
1158/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1159
1160static int
1161is_lower_alphanum (const char c)
1162{
1163 return (isdigit (c) || (isalpha (c) && islower (c)));
1164}
1165
c90092fe
JB
1166/* ENCODED is the linkage name of a symbol and LEN contains its length.
1167 This function saves in LEN the length of that same symbol name but
1168 without either of these suffixes:
29480c32
JB
1169 . .{DIGIT}+
1170 . ${DIGIT}+
1171 . ___{DIGIT}+
1172 . __{DIGIT}+.
c90092fe 1173
29480c32
JB
1174 These are suffixes introduced by the compiler for entities such as
1175 nested subprogram for instance, in order to avoid name clashes.
1176 They do not serve any purpose for the debugger. */
1177
1178static void
1179ada_remove_trailing_digits (const char *encoded, int *len)
1180{
1181 if (*len > 1 && isdigit (encoded[*len - 1]))
1182 {
1183 int i = *len - 2;
5b4ee69b 1184
29480c32 1185 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1186 i--;
29480c32 1187 if (i >= 0 && encoded[i] == '.')
dda83cd7 1188 *len = i;
29480c32 1189 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1190 *len = i;
61012eef 1191 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1192 *len = i - 2;
61012eef 1193 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1194 *len = i - 1;
29480c32
JB
1195 }
1196}
1197
1198/* Remove the suffix introduced by the compiler for protected object
1199 subprograms. */
1200
1201static void
1202ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1203{
1204 /* Remove trailing N. */
1205
1206 /* Protected entry subprograms are broken into two
1207 separate subprograms: The first one is unprotected, and has
1208 a 'N' suffix; the second is the protected version, and has
0963b4bd 1209 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1210 the protection. Since the P subprograms are internally generated,
1211 we leave these names undecoded, giving the user a clue that this
1212 entity is internal. */
1213
1214 if (*len > 1
1215 && encoded[*len - 1] == 'N'
1216 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1217 *len = *len - 1;
1218}
1219
965bc1df
TT
1220/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1221 then update *LEN to remove the suffix and return the offset of the
1222 character just past the ".". Otherwise, return -1. */
1223
1224static int
1225remove_compiler_suffix (const char *encoded, int *len)
1226{
1227 int offset = *len - 1;
1228 while (offset > 0 && isalpha (encoded[offset]))
1229 --offset;
1230 if (offset > 0 && encoded[offset] == '.')
1231 {
1232 *len = offset;
1233 return offset + 1;
1234 }
1235 return -1;
1236}
1237
315e4ebb
TT
1238/* Convert an ASCII hex string to a number. Reads exactly N
1239 characters from STR. Returns true on success, false if one of the
1240 digits was not a hex digit. */
1241static bool
1242convert_hex (const char *str, int n, uint32_t *out)
1243{
1244 uint32_t result = 0;
1245
1246 for (int i = 0; i < n; ++i)
1247 {
1248 if (!isxdigit (str[i]))
1249 return false;
1250 result <<= 4;
1251 result |= fromhex (str[i]);
1252 }
1253
1254 *out = result;
1255 return true;
1256}
1257
1258/* Convert a wide character from its ASCII hex representation in STR
1259 (consisting of exactly N characters) to the host encoding,
1260 appending the resulting bytes to OUT. If N==2 and the Ada source
1261 charset is not UTF-8, then hex refers to an encoding in the
1262 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1263 Return false and do not modify OUT on conversion failure. */
1264static bool
1265convert_from_hex_encoded (std::string &out, const char *str, int n)
1266{
1267 uint32_t value;
1268
1269 if (!convert_hex (str, n, &value))
1270 return false;
1271 try
1272 {
1273 auto_obstack bytes;
1274 /* In the 'U' case, the hex digits encode the character in the
1275 Ada source charset. However, if the source charset is UTF-8,
1276 this really means it is a single-byte UTF-32 character. */
1277 if (n == 2 && ada_source_charset != ada_utf8)
1278 {
1279 gdb_byte one_char = (gdb_byte) value;
1280
1281 convert_between_encodings (ada_source_charset, host_charset (),
1282 &one_char,
1283 sizeof (one_char), sizeof (one_char),
1284 &bytes, translit_none);
1285 }
1286 else
1287 convert_between_encodings (HOST_UTF32, host_charset (),
1288 (const gdb_byte *) &value,
1289 sizeof (value), sizeof (value),
1290 &bytes, translit_none);
1291 obstack_1grow (&bytes, '\0');
1292 out.append ((const char *) obstack_base (&bytes));
1293 }
1294 catch (const gdb_exception &)
1295 {
1296 /* On failure, the caller will just let the encoded form
1297 through, which seems basically reasonable. */
1298 return false;
1299 }
1300
1301 return true;
1302}
1303
8a3df5ac 1304/* See ada-lang.h. */
14f9c5c9 1305
f945dedf 1306std::string
5c94f938 1307ada_decode (const char *encoded, bool wrap, bool operators)
14f9c5c9 1308{
36f5ca53 1309 int i;
14f9c5c9 1310 int len0;
d2e4a39e 1311 const char *p;
14f9c5c9 1312 int at_start_name;
f945dedf 1313 std::string decoded;
965bc1df 1314 int suffix = -1;
d2e4a39e 1315
0d81f350
JG
1316 /* With function descriptors on PPC64, the value of a symbol named
1317 ".FN", if it exists, is the entry point of the function "FN". */
1318 if (encoded[0] == '.')
1319 encoded += 1;
1320
29480c32
JB
1321 /* The name of the Ada main procedure starts with "_ada_".
1322 This prefix is not part of the decoded name, so skip this part
1323 if we see this prefix. */
61012eef 1324 if (startswith (encoded, "_ada_"))
4c4b4cd2 1325 encoded += 5;
81eaa506
TT
1326 /* The "___ghost_" prefix is used for ghost entities. Normally
1327 these aren't preserved but when they are, it's useful to see
1328 them. */
1329 if (startswith (encoded, "___ghost_"))
1330 encoded += 9;
14f9c5c9 1331
29480c32
JB
1332 /* If the name starts with '_', then it is not a properly encoded
1333 name, so do not attempt to decode it. Similarly, if the name
1334 starts with '<', the name should not be decoded. */
4c4b4cd2 1335 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1336 goto Suppress;
1337
4c4b4cd2 1338 len0 = strlen (encoded);
4c4b4cd2 1339
965bc1df
TT
1340 suffix = remove_compiler_suffix (encoded, &len0);
1341
29480c32
JB
1342 ada_remove_trailing_digits (encoded, &len0);
1343 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1344
4c4b4cd2
PH
1345 /* Remove the ___X.* suffix if present. Do not forget to verify that
1346 the suffix is located before the current "end" of ENCODED. We want
1347 to avoid re-matching parts of ENCODED that have previously been
1348 marked as discarded (by decrementing LEN0). */
1349 p = strstr (encoded, "___");
1350 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1351 {
1352 if (p[3] == 'X')
dda83cd7 1353 len0 = p - encoded;
14f9c5c9 1354 else
dda83cd7 1355 goto Suppress;
14f9c5c9 1356 }
4c4b4cd2 1357
29480c32
JB
1358 /* Remove any trailing TKB suffix. It tells us that this symbol
1359 is for the body of a task, but that information does not actually
1360 appear in the decoded name. */
1361
61012eef 1362 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1363 len0 -= 3;
76a01679 1364
a10967fa
JB
1365 /* Remove any trailing TB suffix. The TB suffix is slightly different
1366 from the TKB suffix because it is used for non-anonymous task
1367 bodies. */
1368
61012eef 1369 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1370 len0 -= 2;
1371
29480c32
JB
1372 /* Remove trailing "B" suffixes. */
1373 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1374
61012eef 1375 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1376 len0 -= 1;
1377
29480c32
JB
1378 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1379
4c4b4cd2 1380 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1381 {
4c4b4cd2
PH
1382 i = len0 - 2;
1383 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1384 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1385 i -= 1;
4c4b4cd2 1386 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1387 len0 = i - 1;
4c4b4cd2 1388 else if (encoded[i] == '$')
dda83cd7 1389 len0 = i;
d2e4a39e 1390 }
14f9c5c9 1391
29480c32
JB
1392 /* The first few characters that are not alphabetic are not part
1393 of any encoding we use, so we can copy them over verbatim. */
1394
36f5ca53
TT
1395 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1396 decoded.push_back (encoded[i]);
14f9c5c9
AS
1397
1398 at_start_name = 1;
1399 while (i < len0)
1400 {
29480c32 1401 /* Is this a symbol function? */
5c94f938 1402 if (operators && at_start_name && encoded[i] == 'O')
dda83cd7
SM
1403 {
1404 int k;
1405
1406 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1407 {
1408 int op_len = strlen (ada_opname_table[k].encoded);
1409 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1410 op_len - 1) == 0)
1411 && !isalnum (encoded[i + op_len]))
1412 {
36f5ca53 1413 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1414 at_start_name = 0;
1415 i += op_len;
dda83cd7
SM
1416 break;
1417 }
1418 }
1419 if (ada_opname_table[k].encoded != NULL)
1420 continue;
1421 }
14f9c5c9
AS
1422 at_start_name = 0;
1423
529cad9c 1424 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1425 into "." (just below). */
529cad9c 1426
61012eef 1427 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1428 i += 2;
529cad9c 1429
29480c32 1430 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1431 be translated into "." (just below). These are internal names
1432 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1433
1434 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1435 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1436 && isdigit (encoded [i+4]))
1437 {
1438 int k = i + 5;
1439
1440 while (k < len0 && isdigit (encoded[k]))
1441 k++; /* Skip any extra digit. */
1442
1443 /* Double-check that the "__B_{DIGITS}+" sequence we found
1444 is indeed followed by "__". */
1445 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1446 i = k;
1447 }
29480c32 1448
529cad9c
PH
1449 /* Remove _E{DIGITS}+[sb] */
1450
1451 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1452 of subprograms created by the compiler for each entry. The first
1453 one implements the actual entry code, and has a suffix following
1454 the convention above; the second one implements the barrier and
1455 uses the same convention as above, except that the 'E' is replaced
1456 by a 'B'.
529cad9c 1457
dda83cd7
SM
1458 Just as above, we do not decode the name of barrier functions
1459 to give the user a clue that the code he is debugging has been
1460 internally generated. */
529cad9c
PH
1461
1462 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1463 && isdigit (encoded[i+2]))
1464 {
1465 int k = i + 3;
1466
1467 while (k < len0 && isdigit (encoded[k]))
1468 k++;
1469
1470 if (k < len0
1471 && (encoded[k] == 'b' || encoded[k] == 's'))
1472 {
1473 k++;
1474 /* Just as an extra precaution, make sure that if this
1475 suffix is followed by anything else, it is a '_'.
1476 Otherwise, we matched this sequence by accident. */
1477 if (k == len0
1478 || (k < len0 && encoded[k] == '_'))
1479 i = k;
1480 }
1481 }
529cad9c
PH
1482
1483 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1484 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1485
1486 if (i < len0 + 3
dda83cd7
SM
1487 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1488 {
1489 /* Backtrack a bit up until we reach either the begining of
1490 the encoded name, or "__". Make sure that we only find
1491 digits or lowercase characters. */
1492 const char *ptr = encoded + i - 1;
1493
1494 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1495 ptr--;
1496 if (ptr < encoded
1497 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1498 i++;
1499 }
529cad9c 1500
315e4ebb
TT
1501 if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
1502 {
1503 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1504 {
1505 i += 3;
1506 continue;
1507 }
1508 }
1509 else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
1510 {
1511 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1512 {
1513 i += 5;
1514 continue;
1515 }
1516 }
1517 else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
1518 && isxdigit (encoded[i + 2]))
1519 {
1520 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1521 {
1522 i += 10;
1523 continue;
1524 }
1525 }
1526
4c4b4cd2 1527 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1528 {
1529 /* This is a X[bn]* sequence not separated from the previous
1530 part of the name with a non-alpha-numeric character (in other
1531 words, immediately following an alpha-numeric character), then
1532 verify that it is placed at the end of the encoded name. If
1533 not, then the encoding is not valid and we should abort the
1534 decoding. Otherwise, just skip it, it is used in body-nested
1535 package names. */
1536 do
1537 i += 1;
1538 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1539 if (i < len0)
1540 goto Suppress;
1541 }
cdc7bb92 1542 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1543 {
1544 /* Replace '__' by '.'. */
36f5ca53 1545 decoded.push_back ('.');
dda83cd7
SM
1546 at_start_name = 1;
1547 i += 2;
dda83cd7 1548 }
14f9c5c9 1549 else
dda83cd7
SM
1550 {
1551 /* It's a character part of the decoded name, so just copy it
1552 over. */
36f5ca53 1553 decoded.push_back (encoded[i]);
dda83cd7 1554 i += 1;
dda83cd7 1555 }
14f9c5c9 1556 }
14f9c5c9 1557
29480c32
JB
1558 /* Decoded names should never contain any uppercase character.
1559 Double-check this, and abort the decoding if we find one. */
1560
5c94f938
TT
1561 if (operators)
1562 {
1563 for (i = 0; i < decoded.length(); ++i)
1564 if (isupper (decoded[i]) || decoded[i] == ' ')
1565 goto Suppress;
1566 }
14f9c5c9 1567
965bc1df
TT
1568 /* If the compiler added a suffix, append it now. */
1569 if (suffix >= 0)
1570 decoded = decoded + "[" + &encoded[suffix] + "]";
1571
f945dedf 1572 return decoded;
14f9c5c9
AS
1573
1574Suppress:
8a3df5ac
TT
1575 if (!wrap)
1576 return {};
1577
4c4b4cd2 1578 if (encoded[0] == '<')
f945dedf 1579 decoded = encoded;
14f9c5c9 1580 else
f945dedf 1581 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1582 return decoded;
4c4b4cd2
PH
1583}
1584
1585/* Table for keeping permanent unique copies of decoded names. Once
1586 allocated, names in this table are never released. While this is a
1587 storage leak, it should not be significant unless there are massive
1588 changes in the set of decoded names in successive versions of a
1589 symbol table loaded during a single session. */
1590static struct htab *decoded_names_store;
1591
1592/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1593 in the language-specific part of GSYMBOL, if it has not been
1594 previously computed. Tries to save the decoded name in the same
1595 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1596 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1597 GSYMBOL).
4c4b4cd2
PH
1598 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1599 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1600 when a decoded name is cached in it. */
4c4b4cd2 1601
45e6c716 1602const char *
f85f34ed 1603ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1604{
f85f34ed
TT
1605 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1606 const char **resultp =
615b3f62 1607 &gsymbol->language_specific.demangled_name;
5b4ee69b 1608
f85f34ed 1609 if (!gsymbol->ada_mangled)
4c4b4cd2 1610 {
4d4eaa30 1611 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1612 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1613
f85f34ed 1614 gsymbol->ada_mangled = 1;
5b4ee69b 1615
f85f34ed 1616 if (obstack != NULL)
f945dedf 1617 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1618 else
dda83cd7 1619 {
f85f34ed
TT
1620 /* Sometimes, we can't find a corresponding objfile, in
1621 which case, we put the result on the heap. Since we only
1622 decode when needed, we hope this usually does not cause a
1623 significant memory leak (FIXME). */
1624
dda83cd7
SM
1625 char **slot = (char **) htab_find_slot (decoded_names_store,
1626 decoded.c_str (), INSERT);
5b4ee69b 1627
dda83cd7
SM
1628 if (*slot == NULL)
1629 *slot = xstrdup (decoded.c_str ());
1630 *resultp = *slot;
1631 }
4c4b4cd2 1632 }
14f9c5c9 1633
4c4b4cd2
PH
1634 return *resultp;
1635}
76a01679 1636
14f9c5c9 1637\f
d2e4a39e 1638
dda83cd7 1639 /* Arrays */
14f9c5c9 1640
28c85d6c
JB
1641/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1642 generated by the GNAT compiler to describe the index type used
1643 for each dimension of an array, check whether it follows the latest
1644 known encoding. If not, fix it up to conform to the latest encoding.
1645 Otherwise, do nothing. This function also does nothing if
1646 INDEX_DESC_TYPE is NULL.
1647
85102364 1648 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1649 Initially, the information would be provided through the name of each
1650 field of the structure type only, while the type of these fields was
1651 described as unspecified and irrelevant. The debugger was then expected
1652 to perform a global type lookup using the name of that field in order
1653 to get access to the full index type description. Because these global
1654 lookups can be very expensive, the encoding was later enhanced to make
1655 the global lookup unnecessary by defining the field type as being
1656 the full index type description.
1657
1658 The purpose of this routine is to allow us to support older versions
1659 of the compiler by detecting the use of the older encoding, and by
1660 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1661 we essentially replace each field's meaningless type by the associated
1662 index subtype). */
1663
1664void
1665ada_fixup_array_indexes_type (struct type *index_desc_type)
1666{
1667 int i;
1668
1669 if (index_desc_type == NULL)
1670 return;
1f704f76 1671 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1672
1673 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1674 to check one field only, no need to check them all). If not, return
1675 now.
1676
1677 If our INDEX_DESC_TYPE was generated using the older encoding,
1678 the field type should be a meaningless integer type whose name
1679 is not equal to the field name. */
940da03e
SM
1680 if (index_desc_type->field (0).type ()->name () != NULL
1681 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1682 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1683 return;
1684
1685 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1686 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1687 {
33d16dd9 1688 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1689 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1690
1691 if (raw_type)
5d14b6e5 1692 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1693 }
1694}
1695
4c4b4cd2
PH
1696/* The desc_* routines return primitive portions of array descriptors
1697 (fat pointers). */
14f9c5c9
AS
1698
1699/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1700 level of indirection, if needed. */
1701
d2e4a39e
AS
1702static struct type *
1703desc_base_type (struct type *type)
14f9c5c9
AS
1704{
1705 if (type == NULL)
1706 return NULL;
61ee279c 1707 type = ada_check_typedef (type);
78134374 1708 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1709 type = ada_typedef_target_type (type);
1710
1265e4aa 1711 if (type != NULL
78134374 1712 && (type->code () == TYPE_CODE_PTR
dda83cd7 1713 || type->code () == TYPE_CODE_REF))
61ee279c 1714 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1715 else
1716 return type;
1717}
1718
4c4b4cd2
PH
1719/* True iff TYPE indicates a "thin" array pointer type. */
1720
14f9c5c9 1721static int
d2e4a39e 1722is_thin_pntr (struct type *type)
14f9c5c9 1723{
d2e4a39e 1724 return
14f9c5c9
AS
1725 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1726 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1727}
1728
4c4b4cd2
PH
1729/* The descriptor type for thin pointer type TYPE. */
1730
d2e4a39e
AS
1731static struct type *
1732thin_descriptor_type (struct type *type)
14f9c5c9 1733{
d2e4a39e 1734 struct type *base_type = desc_base_type (type);
5b4ee69b 1735
14f9c5c9
AS
1736 if (base_type == NULL)
1737 return NULL;
1738 if (is_suffix (ada_type_name (base_type), "___XVE"))
1739 return base_type;
d2e4a39e 1740 else
14f9c5c9 1741 {
d2e4a39e 1742 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1743
14f9c5c9 1744 if (alt_type == NULL)
dda83cd7 1745 return base_type;
14f9c5c9 1746 else
dda83cd7 1747 return alt_type;
14f9c5c9
AS
1748 }
1749}
1750
4c4b4cd2
PH
1751/* A pointer to the array data for thin-pointer value VAL. */
1752
d2e4a39e
AS
1753static struct value *
1754thin_data_pntr (struct value *val)
14f9c5c9 1755{
828292f2 1756 struct type *type = ada_check_typedef (value_type (val));
556bdfd4 1757 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1758
556bdfd4
UW
1759 data_type = lookup_pointer_type (data_type);
1760
78134374 1761 if (type->code () == TYPE_CODE_PTR)
556bdfd4 1762 return value_cast (data_type, value_copy (val));
d2e4a39e 1763 else
42ae5230 1764 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1765}
1766
4c4b4cd2
PH
1767/* True iff TYPE indicates a "thick" array pointer type. */
1768
14f9c5c9 1769static int
d2e4a39e 1770is_thick_pntr (struct type *type)
14f9c5c9
AS
1771{
1772 type = desc_base_type (type);
78134374 1773 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1774 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1775}
1776
4c4b4cd2
PH
1777/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1778 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1779
d2e4a39e
AS
1780static struct type *
1781desc_bounds_type (struct type *type)
14f9c5c9 1782{
d2e4a39e 1783 struct type *r;
14f9c5c9
AS
1784
1785 type = desc_base_type (type);
1786
1787 if (type == NULL)
1788 return NULL;
1789 else if (is_thin_pntr (type))
1790 {
1791 type = thin_descriptor_type (type);
1792 if (type == NULL)
dda83cd7 1793 return NULL;
14f9c5c9
AS
1794 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1795 if (r != NULL)
dda83cd7 1796 return ada_check_typedef (r);
14f9c5c9 1797 }
78134374 1798 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1799 {
1800 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1801 if (r != NULL)
dda83cd7 1802 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1803 }
1804 return NULL;
1805}
1806
1807/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1808 one, a pointer to its bounds data. Otherwise NULL. */
1809
d2e4a39e
AS
1810static struct value *
1811desc_bounds (struct value *arr)
14f9c5c9 1812{
df407dfe 1813 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1814
d2e4a39e 1815 if (is_thin_pntr (type))
14f9c5c9 1816 {
d2e4a39e 1817 struct type *bounds_type =
dda83cd7 1818 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1819 LONGEST addr;
1820
4cdfadb1 1821 if (bounds_type == NULL)
dda83cd7 1822 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1823
1824 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1825 since desc_type is an XVE-encoded type (and shouldn't be),
1826 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1827 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1828 addr = value_as_long (arr);
d2e4a39e 1829 else
dda83cd7 1830 addr = value_address (arr);
14f9c5c9 1831
d2e4a39e 1832 return
dda83cd7
SM
1833 value_from_longest (lookup_pointer_type (bounds_type),
1834 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1835 }
1836
1837 else if (is_thick_pntr (type))
05e522ef 1838 {
158cc4fe 1839 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef
JB
1840 _("Bad GNAT array descriptor"));
1841 struct type *p_bounds_type = value_type (p_bounds);
1842
1843 if (p_bounds_type
78134374 1844 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef
JB
1845 {
1846 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1847
e46d3488 1848 if (target_type->is_stub ())
05e522ef
JB
1849 p_bounds = value_cast (lookup_pointer_type
1850 (ada_check_typedef (target_type)),
1851 p_bounds);
1852 }
1853 else
1854 error (_("Bad GNAT array descriptor"));
1855
1856 return p_bounds;
1857 }
14f9c5c9
AS
1858 else
1859 return NULL;
1860}
1861
4c4b4cd2
PH
1862/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1863 position of the field containing the address of the bounds data. */
1864
14f9c5c9 1865static int
d2e4a39e 1866fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1867{
b610c045 1868 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1869}
1870
1871/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1872 size of the field containing the address of the bounds data. */
1873
14f9c5c9 1874static int
d2e4a39e 1875fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1876{
1877 type = desc_base_type (type);
1878
d2e4a39e 1879 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1880 return TYPE_FIELD_BITSIZE (type, 1);
1881 else
940da03e 1882 return 8 * TYPE_LENGTH (ada_check_typedef (type->field (1).type ()));
14f9c5c9
AS
1883}
1884
4c4b4cd2 1885/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1886 pointer to one, the type of its array data (a array-with-no-bounds type);
1887 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1888 data. */
4c4b4cd2 1889
d2e4a39e 1890static struct type *
556bdfd4 1891desc_data_target_type (struct type *type)
14f9c5c9
AS
1892{
1893 type = desc_base_type (type);
1894
4c4b4cd2 1895 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1896 if (is_thin_pntr (type))
940da03e 1897 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1898 else if (is_thick_pntr (type))
556bdfd4
UW
1899 {
1900 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1901
1902 if (data_type
78134374 1903 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
05e522ef 1904 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1905 }
1906
1907 return NULL;
14f9c5c9
AS
1908}
1909
1910/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1911 its array data. */
4c4b4cd2 1912
d2e4a39e
AS
1913static struct value *
1914desc_data (struct value *arr)
14f9c5c9 1915{
df407dfe 1916 struct type *type = value_type (arr);
5b4ee69b 1917
14f9c5c9
AS
1918 if (is_thin_pntr (type))
1919 return thin_data_pntr (arr);
1920 else if (is_thick_pntr (type))
158cc4fe 1921 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1922 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1923 else
1924 return NULL;
1925}
1926
1927
1928/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1929 position of the field containing the address of the data. */
1930
14f9c5c9 1931static int
d2e4a39e 1932fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1933{
b610c045 1934 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1935}
1936
1937/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1938 size of the field containing the address of the data. */
1939
14f9c5c9 1940static int
d2e4a39e 1941fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1942{
1943 type = desc_base_type (type);
1944
1945 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1946 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1947 else
940da03e 1948 return TARGET_CHAR_BIT * TYPE_LENGTH (type->field (0).type ());
14f9c5c9
AS
1949}
1950
4c4b4cd2 1951/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1952 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1953 bound, if WHICH is 1. The first bound is I=1. */
1954
d2e4a39e
AS
1955static struct value *
1956desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1957{
250106a7
TT
1958 char bound_name[20];
1959 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1960 which ? 'U' : 'L', i - 1);
158cc4fe 1961 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1962 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1963}
1964
1965/* If BOUNDS is an array-bounds structure type, return the bit position
1966 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1967 bound, if WHICH is 1. The first bound is I=1. */
1968
14f9c5c9 1969static int
d2e4a39e 1970desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1971{
b610c045 1972 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1973}
1974
1975/* If BOUNDS is an array-bounds structure type, return the bit field size
1976 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1977 bound, if WHICH is 1. The first bound is I=1. */
1978
76a01679 1979static int
d2e4a39e 1980desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1981{
1982 type = desc_base_type (type);
1983
d2e4a39e
AS
1984 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1985 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1986 else
940da03e 1987 return 8 * TYPE_LENGTH (type->field (2 * i + which - 2).type ());
14f9c5c9
AS
1988}
1989
1990/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1991 Ith bound (numbering from 1). Otherwise, NULL. */
1992
d2e4a39e
AS
1993static struct type *
1994desc_index_type (struct type *type, int i)
14f9c5c9
AS
1995{
1996 type = desc_base_type (type);
1997
78134374 1998 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
1999 {
2000 char bound_name[20];
2001 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
2002 return lookup_struct_elt_type (type, bound_name, 1);
2003 }
d2e4a39e 2004 else
14f9c5c9
AS
2005 return NULL;
2006}
2007
4c4b4cd2
PH
2008/* The number of index positions in the array-bounds type TYPE.
2009 Return 0 if TYPE is NULL. */
2010
14f9c5c9 2011static int
d2e4a39e 2012desc_arity (struct type *type)
14f9c5c9
AS
2013{
2014 type = desc_base_type (type);
2015
2016 if (type != NULL)
1f704f76 2017 return type->num_fields () / 2;
14f9c5c9
AS
2018 return 0;
2019}
2020
4c4b4cd2
PH
2021/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2022 an array descriptor type (representing an unconstrained array
2023 type). */
2024
76a01679
JB
2025static int
2026ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2027{
2028 if (type == NULL)
2029 return 0;
61ee279c 2030 type = ada_check_typedef (type);
78134374 2031 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2032 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2033}
2034
52ce6436 2035/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2036 * to one. */
52ce6436 2037
2c0b251b 2038static int
52ce6436
PH
2039ada_is_array_type (struct type *type)
2040{
78134374
SM
2041 while (type != NULL
2042 && (type->code () == TYPE_CODE_PTR
2043 || type->code () == TYPE_CODE_REF))
52ce6436
PH
2044 type = TYPE_TARGET_TYPE (type);
2045 return ada_is_direct_array_type (type);
2046}
2047
4c4b4cd2 2048/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2049
14f9c5c9 2050int
4c4b4cd2 2051ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2052{
2053 if (type == NULL)
2054 return 0;
61ee279c 2055 type = ada_check_typedef (type);
78134374
SM
2056 return (type->code () == TYPE_CODE_ARRAY
2057 || (type->code () == TYPE_CODE_PTR
2058 && (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ()
2059 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2060}
2061
4c4b4cd2
PH
2062/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2063
14f9c5c9 2064int
4c4b4cd2 2065ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2066{
556bdfd4 2067 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2068
2069 if (type == NULL)
2070 return 0;
61ee279c 2071 type = ada_check_typedef (type);
556bdfd4 2072 return (data_type != NULL
78134374 2073 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2074 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2075}
2076
2077/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 2078 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 2079 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
2080 is still needed. */
2081
14f9c5c9 2082int
ebf56fd3 2083ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 2084{
d2e4a39e 2085 return
14f9c5c9 2086 type != NULL
78134374 2087 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 2088 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 2089 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 2090 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
2091}
2092
2093
4c4b4cd2 2094/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2095 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2096 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2097 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2098 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2099 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2100 a descriptor. */
de93309a
SM
2101
2102static struct type *
d2e4a39e 2103ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2104{
ad82864c
JB
2105 if (ada_is_constrained_packed_array_type (value_type (arr)))
2106 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 2107
df407dfe
AC
2108 if (!ada_is_array_descriptor_type (value_type (arr)))
2109 return value_type (arr);
d2e4a39e
AS
2110
2111 if (!bounds)
ad82864c
JB
2112 {
2113 struct type *array_type =
2114 ada_check_typedef (desc_data_target_type (value_type (arr)));
2115
2116 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
2117 TYPE_FIELD_BITSIZE (array_type, 0) =
2118 decode_packed_array_bitsize (value_type (arr));
2119
2120 return array_type;
2121 }
14f9c5c9
AS
2122 else
2123 {
d2e4a39e 2124 struct type *elt_type;
14f9c5c9 2125 int arity;
d2e4a39e 2126 struct value *descriptor;
14f9c5c9 2127
df407dfe
AC
2128 elt_type = ada_array_element_type (value_type (arr), -1);
2129 arity = ada_array_arity (value_type (arr));
14f9c5c9 2130
d2e4a39e 2131 if (elt_type == NULL || arity == 0)
dda83cd7 2132 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
2133
2134 descriptor = desc_bounds (arr);
d2e4a39e 2135 if (value_as_long (descriptor) == 0)
dda83cd7 2136 return NULL;
d2e4a39e 2137 while (arity > 0)
dda83cd7
SM
2138 {
2139 struct type *range_type = alloc_type_copy (value_type (arr));
2140 struct type *array_type = alloc_type_copy (value_type (arr));
2141 struct value *low = desc_one_bound (descriptor, arity, 0);
2142 struct value *high = desc_one_bound (descriptor, arity, 1);
2143
2144 arity -= 1;
2145 create_static_range_type (range_type, value_type (low),
0c9c3474
SA
2146 longest_to_int (value_as_long (low)),
2147 longest_to_int (value_as_long (high)));
dda83cd7 2148 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
2149
2150 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
e67ad678
JB
2151 {
2152 /* We need to store the element packed bitsize, as well as
dda83cd7 2153 recompute the array size, because it was previously
e67ad678
JB
2154 computed based on the unpacked element size. */
2155 LONGEST lo = value_as_long (low);
2156 LONGEST hi = value_as_long (high);
2157
2158 TYPE_FIELD_BITSIZE (elt_type, 0) =
2159 decode_packed_array_bitsize (value_type (arr));
2160 /* If the array has no element, then the size is already
dda83cd7 2161 zero, and does not need to be recomputed. */
e67ad678
JB
2162 if (lo < hi)
2163 {
2164 int array_bitsize =
dda83cd7 2165 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678
JB
2166
2167 TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
2168 }
2169 }
dda83cd7 2170 }
14f9c5c9
AS
2171
2172 return lookup_pointer_type (elt_type);
2173 }
2174}
2175
2176/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2177 Otherwise, returns either a standard GDB array with bounds set
2178 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2179 GDB array. Returns NULL if ARR is a null fat pointer. */
2180
d2e4a39e
AS
2181struct value *
2182ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2183{
df407dfe 2184 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2185 {
d2e4a39e 2186 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2187
14f9c5c9 2188 if (arrType == NULL)
dda83cd7 2189 return NULL;
14f9c5c9
AS
2190 return value_cast (arrType, value_copy (desc_data (arr)));
2191 }
ad82864c
JB
2192 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2193 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2194 else
2195 return arr;
2196}
2197
2198/* If ARR does not represent an array, returns ARR unchanged.
2199 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2200 be ARR itself if it already is in the proper form). */
2201
720d1a40 2202struct value *
d2e4a39e 2203ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2204{
df407dfe 2205 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2206 {
d2e4a39e 2207 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2208
14f9c5c9 2209 if (arrVal == NULL)
dda83cd7 2210 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2211 return value_ind (arrVal);
2212 }
ad82864c
JB
2213 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2214 return decode_constrained_packed_array (arr);
d2e4a39e 2215 else
14f9c5c9
AS
2216 return arr;
2217}
2218
2219/* If TYPE represents a GNAT array type, return it translated to an
2220 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2221 packing). For other types, is the identity. */
2222
d2e4a39e
AS
2223struct type *
2224ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2225{
ad82864c
JB
2226 if (ada_is_constrained_packed_array_type (type))
2227 return decode_constrained_packed_array_type (type);
17280b9f
UW
2228
2229 if (ada_is_array_descriptor_type (type))
556bdfd4 2230 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2231
2232 return type;
14f9c5c9
AS
2233}
2234
4c4b4cd2
PH
2235/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2236
ad82864c 2237static int
57567375 2238ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2239{
2240 if (type == NULL)
2241 return 0;
4c4b4cd2 2242 type = desc_base_type (type);
61ee279c 2243 type = ada_check_typedef (type);
d2e4a39e 2244 return
14f9c5c9
AS
2245 ada_type_name (type) != NULL
2246 && strstr (ada_type_name (type), "___XP") != NULL;
2247}
2248
ad82864c
JB
2249/* Non-zero iff TYPE represents a standard GNAT constrained
2250 packed-array type. */
2251
2252int
2253ada_is_constrained_packed_array_type (struct type *type)
2254{
57567375 2255 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2256 && !ada_is_array_descriptor_type (type);
2257}
2258
2259/* Non-zero iff TYPE represents an array descriptor for a
2260 unconstrained packed-array type. */
2261
2262static int
2263ada_is_unconstrained_packed_array_type (struct type *type)
2264{
57567375
TT
2265 if (!ada_is_array_descriptor_type (type))
2266 return 0;
2267
2268 if (ada_is_gnat_encoded_packed_array_type (type))
2269 return 1;
2270
2271 /* If we saw GNAT encodings, then the above code is sufficient.
2272 However, with minimal encodings, we will just have a thick
2273 pointer instead. */
2274 if (is_thick_pntr (type))
2275 {
2276 type = desc_base_type (type);
2277 /* The structure's first field is a pointer to an array, so this
2278 fetches the array type. */
2279 type = TYPE_TARGET_TYPE (type->field (0).type ());
af5300fe
TV
2280 if (type->code () == TYPE_CODE_TYPEDEF)
2281 type = ada_typedef_target_type (type);
57567375
TT
2282 /* Now we can see if the array elements are packed. */
2283 return TYPE_FIELD_BITSIZE (type, 0) > 0;
2284 }
2285
2286 return 0;
ad82864c
JB
2287}
2288
c9a28cbe
TT
2289/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2290 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2291
2292static bool
2293ada_is_any_packed_array_type (struct type *type)
2294{
2295 return (ada_is_constrained_packed_array_type (type)
2296 || (type->code () == TYPE_CODE_ARRAY
2297 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
2298}
2299
ad82864c
JB
2300/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2301 return the size of its elements in bits. */
2302
2303static long
2304decode_packed_array_bitsize (struct type *type)
2305{
0d5cff50
DE
2306 const char *raw_name;
2307 const char *tail;
ad82864c
JB
2308 long bits;
2309
720d1a40
JB
2310 /* Access to arrays implemented as fat pointers are encoded as a typedef
2311 of the fat pointer type. We need the name of the fat pointer type
2312 to do the decoding, so strip the typedef layer. */
78134374 2313 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2314 type = ada_typedef_target_type (type);
2315
2316 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2317 if (!raw_name)
2318 raw_name = ada_type_name (desc_base_type (type));
2319
2320 if (!raw_name)
2321 return 0;
2322
2323 tail = strstr (raw_name, "___XP");
57567375
TT
2324 if (tail == nullptr)
2325 {
2326 gdb_assert (is_thick_pntr (type));
2327 /* The structure's first field is a pointer to an array, so this
2328 fetches the array type. */
2329 type = TYPE_TARGET_TYPE (type->field (0).type ());
2330 /* Now we can see if the array elements are packed. */
2331 return TYPE_FIELD_BITSIZE (type, 0);
2332 }
ad82864c
JB
2333
2334 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2335 {
2336 lim_warning
2337 (_("could not understand bit size information on packed array"));
2338 return 0;
2339 }
2340
2341 return bits;
2342}
2343
14f9c5c9
AS
2344/* Given that TYPE is a standard GDB array type with all bounds filled
2345 in, and that the element size of its ultimate scalar constituents
2346 (that is, either its elements, or, if it is an array of arrays, its
2347 elements' elements, etc.) is *ELT_BITS, return an identical type,
2348 but with the bit sizes of its elements (and those of any
2349 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2350 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2351 in bits.
2352
2353 Note that, for arrays whose index type has an XA encoding where
2354 a bound references a record discriminant, getting that discriminant,
2355 and therefore the actual value of that bound, is not possible
2356 because none of the given parameters gives us access to the record.
2357 This function assumes that it is OK in the context where it is being
2358 used to return an array whose bounds are still dynamic and where
2359 the length is arbitrary. */
4c4b4cd2 2360
d2e4a39e 2361static struct type *
ad82864c 2362constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2363{
d2e4a39e
AS
2364 struct type *new_elt_type;
2365 struct type *new_type;
99b1c762
JB
2366 struct type *index_type_desc;
2367 struct type *index_type;
14f9c5c9
AS
2368 LONGEST low_bound, high_bound;
2369
61ee279c 2370 type = ada_check_typedef (type);
78134374 2371 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2372 return type;
2373
99b1c762
JB
2374 index_type_desc = ada_find_parallel_type (type, "___XA");
2375 if (index_type_desc)
940da03e 2376 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2377 NULL);
2378 else
3d967001 2379 index_type = type->index_type ();
99b1c762 2380
e9bb382b 2381 new_type = alloc_type_copy (type);
ad82864c
JB
2382 new_elt_type =
2383 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
2384 elt_bits);
99b1c762 2385 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9 2386 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2387 new_type->set_name (ada_type_name (type));
14f9c5c9 2388
78134374 2389 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2390 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2391 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2392 low_bound = high_bound = 0;
2393 if (high_bound < low_bound)
2394 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 2395 else
14f9c5c9
AS
2396 {
2397 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 2398 TYPE_LENGTH (new_type) =
dda83cd7 2399 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
2400 }
2401
9cdd0d12 2402 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2403 return new_type;
2404}
2405
ad82864c
JB
2406/* The array type encoded by TYPE, where
2407 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2408
d2e4a39e 2409static struct type *
ad82864c 2410decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2411{
0d5cff50 2412 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2413 char *name;
0d5cff50 2414 const char *tail;
d2e4a39e 2415 struct type *shadow_type;
14f9c5c9 2416 long bits;
14f9c5c9 2417
727e3d2e
JB
2418 if (!raw_name)
2419 raw_name = ada_type_name (desc_base_type (type));
2420
2421 if (!raw_name)
2422 return NULL;
2423
2424 name = (char *) alloca (strlen (raw_name) + 1);
2425 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2426 type = desc_base_type (type);
2427
14f9c5c9
AS
2428 memcpy (name, raw_name, tail - raw_name);
2429 name[tail - raw_name] = '\000';
2430
b4ba55a1
JB
2431 shadow_type = ada_find_parallel_type_with_name (type, name);
2432
2433 if (shadow_type == NULL)
14f9c5c9 2434 {
323e0a4a 2435 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2436 return NULL;
2437 }
f168693b 2438 shadow_type = check_typedef (shadow_type);
14f9c5c9 2439
78134374 2440 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2441 {
0963b4bd
MS
2442 lim_warning (_("could not understand bounds "
2443 "information on packed array"));
14f9c5c9
AS
2444 return NULL;
2445 }
d2e4a39e 2446
ad82864c
JB
2447 bits = decode_packed_array_bitsize (type);
2448 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2449}
2450
a7400e44
TT
2451/* Helper function for decode_constrained_packed_array. Set the field
2452 bitsize on a series of packed arrays. Returns the number of
2453 elements in TYPE. */
2454
2455static LONGEST
2456recursively_update_array_bitsize (struct type *type)
2457{
2458 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2459
2460 LONGEST low, high;
1f8d2881 2461 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2462 || low > high)
2463 return 0;
2464 LONGEST our_len = high - low + 1;
2465
2466 struct type *elt_type = TYPE_TARGET_TYPE (type);
2467 if (elt_type->code () == TYPE_CODE_ARRAY)
2468 {
2469 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2470 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2471 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2472
2473 TYPE_LENGTH (type) = ((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2474 / HOST_CHAR_BIT);
2475 }
2476
2477 return our_len;
2478}
2479
ad82864c
JB
2480/* Given that ARR is a struct value *indicating a GNAT constrained packed
2481 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2482 standard GDB array type except that the BITSIZEs of the array
2483 target types are set to the number of bits in each element, and the
4c4b4cd2 2484 type length is set appropriately. */
14f9c5c9 2485
d2e4a39e 2486static struct value *
ad82864c 2487decode_constrained_packed_array (struct value *arr)
14f9c5c9 2488{
4c4b4cd2 2489 struct type *type;
14f9c5c9 2490
11aa919a
PMR
2491 /* If our value is a pointer, then dereference it. Likewise if
2492 the value is a reference. Make sure that this operation does not
2493 cause the target type to be fixed, as this would indirectly cause
2494 this array to be decoded. The rest of the routine assumes that
2495 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2496 and "value_ind" routines to perform the dereferencing, as opposed
2497 to using "ada_coerce_ref" or "ada_value_ind". */
2498 arr = coerce_ref (arr);
78134374 2499 if (ada_check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
284614f0 2500 arr = value_ind (arr);
4c4b4cd2 2501
ad82864c 2502 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2503 if (type == NULL)
2504 {
323e0a4a 2505 error (_("can't unpack array"));
14f9c5c9
AS
2506 return NULL;
2507 }
61ee279c 2508
a7400e44
TT
2509 /* Decoding the packed array type could not correctly set the field
2510 bitsizes for any dimension except the innermost, because the
2511 bounds may be variable and were not passed to that function. So,
2512 we further resolve the array bounds here and then update the
2513 sizes. */
50888e42 2514 const gdb_byte *valaddr = value_contents_for_printing (arr).data ();
a7400e44
TT
2515 CORE_ADDR address = value_address (arr);
2516 gdb::array_view<const gdb_byte> view
2517 = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
2518 type = resolve_dynamic_type (type, view, address);
2519 recursively_update_array_bitsize (type);
2520
d5a22e77 2521 if (type_byte_order (value_type (arr)) == BFD_ENDIAN_BIG
32c9a795 2522 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2523 {
2524 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2525 array with no wrapper. In order to interpret the value through
2526 the (left-justified) packed array type we just built, we must
2527 first left-justify it. */
61ee279c
PH
2528 int bit_size, bit_pos;
2529 ULONGEST mod;
2530
df407dfe 2531 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2532 bit_size = 0;
2533 while (mod > 0)
2534 {
2535 bit_size += 1;
2536 mod >>= 1;
2537 }
df407dfe 2538 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2539 arr = ada_value_primitive_packed_val (arr, NULL,
2540 bit_pos / HOST_CHAR_BIT,
2541 bit_pos % HOST_CHAR_BIT,
2542 bit_size,
2543 type);
2544 }
2545
4c4b4cd2 2546 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2547}
2548
2549
2550/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2551 given in IND. ARR must be a simple array. */
14f9c5c9 2552
d2e4a39e
AS
2553static struct value *
2554value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2555{
2556 int i;
2557 int bits, elt_off, bit_off;
2558 long elt_total_bit_offset;
d2e4a39e
AS
2559 struct type *elt_type;
2560 struct value *v;
14f9c5c9
AS
2561
2562 bits = 0;
2563 elt_total_bit_offset = 0;
df407dfe 2564 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2565 for (i = 0; i < arity; i += 1)
14f9c5c9 2566 {
78134374 2567 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2568 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2569 error
2570 (_("attempt to do packed indexing of "
0963b4bd 2571 "something other than a packed array"));
14f9c5c9 2572 else
dda83cd7
SM
2573 {
2574 struct type *range_type = elt_type->index_type ();
2575 LONGEST lowerbound, upperbound;
2576 LONGEST idx;
2577
1f8d2881 2578 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2579 {
2580 lim_warning (_("don't know bounds of array"));
2581 lowerbound = upperbound = 0;
2582 }
2583
2584 idx = pos_atr (ind[i]);
2585 if (idx < lowerbound || idx > upperbound)
2586 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2587 (long) idx);
dda83cd7
SM
2588 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2589 elt_total_bit_offset += (idx - lowerbound) * bits;
2590 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2591 }
14f9c5c9
AS
2592 }
2593 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2594 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2595
2596 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2597 bits, elt_type);
14f9c5c9
AS
2598 return v;
2599}
2600
4c4b4cd2 2601/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2602
2603static int
d2e4a39e 2604has_negatives (struct type *type)
14f9c5c9 2605{
78134374 2606 switch (type->code ())
d2e4a39e
AS
2607 {
2608 default:
2609 return 0;
2610 case TYPE_CODE_INT:
c6d940a9 2611 return !type->is_unsigned ();
d2e4a39e 2612 case TYPE_CODE_RANGE:
5537ddd0 2613 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2614 }
14f9c5c9 2615}
d2e4a39e 2616
f93fca70 2617/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2618 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2619 the unpacked buffer.
14f9c5c9 2620
5b639dea
JB
2621 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2622 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2623
f93fca70
JB
2624 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2625 zero otherwise.
14f9c5c9 2626
f93fca70 2627 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2628
f93fca70
JB
2629 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2630
2631static void
2632ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2633 gdb_byte *unpacked, int unpacked_len,
2634 int is_big_endian, int is_signed_type,
2635 int is_scalar)
2636{
a1c95e6b
JB
2637 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2638 int src_idx; /* Index into the source area */
2639 int src_bytes_left; /* Number of source bytes left to process. */
2640 int srcBitsLeft; /* Number of source bits left to move */
2641 int unusedLS; /* Number of bits in next significant
dda83cd7 2642 byte of source that are unused */
a1c95e6b 2643
a1c95e6b
JB
2644 int unpacked_idx; /* Index into the unpacked buffer */
2645 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2646
4c4b4cd2 2647 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2648 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2649 unsigned char sign;
a1c95e6b 2650
4c4b4cd2
PH
2651 /* Transmit bytes from least to most significant; delta is the direction
2652 the indices move. */
f93fca70 2653 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2654
5b639dea
JB
2655 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2656 bits from SRC. .*/
2657 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2658 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2659 bit_size, unpacked_len);
2660
14f9c5c9 2661 srcBitsLeft = bit_size;
086ca51f 2662 src_bytes_left = src_len;
f93fca70 2663 unpacked_bytes_left = unpacked_len;
14f9c5c9 2664 sign = 0;
f93fca70
JB
2665
2666 if (is_big_endian)
14f9c5c9 2667 {
086ca51f 2668 src_idx = src_len - 1;
f93fca70
JB
2669 if (is_signed_type
2670 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2671 sign = ~0;
d2e4a39e
AS
2672
2673 unusedLS =
dda83cd7
SM
2674 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2675 % HOST_CHAR_BIT;
14f9c5c9 2676
f93fca70
JB
2677 if (is_scalar)
2678 {
dda83cd7
SM
2679 accumSize = 0;
2680 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2681 }
2682 else
2683 {
dda83cd7
SM
2684 /* Non-scalar values must be aligned at a byte boundary... */
2685 accumSize =
2686 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2687 /* ... And are placed at the beginning (most-significant) bytes
2688 of the target. */
2689 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2690 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2691 }
14f9c5c9 2692 }
d2e4a39e 2693 else
14f9c5c9
AS
2694 {
2695 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2696
086ca51f 2697 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2698 unusedLS = bit_offset;
2699 accumSize = 0;
2700
f93fca70 2701 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2702 sign = ~0;
14f9c5c9 2703 }
d2e4a39e 2704
14f9c5c9 2705 accum = 0;
086ca51f 2706 while (src_bytes_left > 0)
14f9c5c9
AS
2707 {
2708 /* Mask for removing bits of the next source byte that are not
dda83cd7 2709 part of the value. */
d2e4a39e 2710 unsigned int unusedMSMask =
dda83cd7
SM
2711 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2712 1;
4c4b4cd2 2713 /* Sign-extend bits for this byte. */
14f9c5c9 2714 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2715
d2e4a39e 2716 accum |=
dda83cd7 2717 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2718 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2719 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2720 {
2721 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2722 accumSize -= HOST_CHAR_BIT;
2723 accum >>= HOST_CHAR_BIT;
2724 unpacked_bytes_left -= 1;
2725 unpacked_idx += delta;
2726 }
14f9c5c9
AS
2727 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2728 unusedLS = 0;
086ca51f
JB
2729 src_bytes_left -= 1;
2730 src_idx += delta;
14f9c5c9 2731 }
086ca51f 2732 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2733 {
2734 accum |= sign << accumSize;
db297a65 2735 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2736 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2737 if (accumSize < 0)
2738 accumSize = 0;
14f9c5c9 2739 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2740 unpacked_bytes_left -= 1;
2741 unpacked_idx += delta;
14f9c5c9 2742 }
f93fca70
JB
2743}
2744
2745/* Create a new value of type TYPE from the contents of OBJ starting
2746 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2747 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2748 assigning through the result will set the field fetched from.
2749 VALADDR is ignored unless OBJ is NULL, in which case,
2750 VALADDR+OFFSET must address the start of storage containing the
2751 packed value. The value returned in this case is never an lval.
2752 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2753
2754struct value *
2755ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2756 long offset, int bit_offset, int bit_size,
dda83cd7 2757 struct type *type)
f93fca70
JB
2758{
2759 struct value *v;
bfb1c796 2760 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2761 gdb_byte *unpacked;
220475ed 2762 const int is_scalar = is_scalar_type (type);
d5a22e77 2763 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2764 gdb::byte_vector staging;
f93fca70
JB
2765
2766 type = ada_check_typedef (type);
2767
d0a9e810 2768 if (obj == NULL)
bfb1c796 2769 src = valaddr + offset;
d0a9e810 2770 else
50888e42 2771 src = value_contents (obj).data () + offset;
d0a9e810
JB
2772
2773 if (is_dynamic_type (type))
2774 {
2775 /* The length of TYPE might by dynamic, so we need to resolve
2776 TYPE in order to know its actual size, which we then use
2777 to create the contents buffer of the value we return.
2778 The difficulty is that the data containing our object is
2779 packed, and therefore maybe not at a byte boundary. So, what
2780 we do, is unpack the data into a byte-aligned buffer, and then
2781 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2782 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2783 staging.resize (staging_len);
d0a9e810
JB
2784
2785 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2786 staging.data (), staging.size (),
d0a9e810
JB
2787 is_big_endian, has_negatives (type),
2788 is_scalar);
b249d2c2 2789 type = resolve_dynamic_type (type, staging, 0);
0cafa88c
JB
2790 if (TYPE_LENGTH (type) < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
2791 {
2792 /* This happens when the length of the object is dynamic,
2793 and is actually smaller than the space reserved for it.
2794 For instance, in an array of variant records, the bit_size
2795 we're given is the array stride, which is constant and
2796 normally equal to the maximum size of its element.
2797 But, in reality, each element only actually spans a portion
2798 of that stride. */
2799 bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
2800 }
d0a9e810
JB
2801 }
2802
f93fca70
JB
2803 if (obj == NULL)
2804 {
2805 v = allocate_value (type);
bfb1c796 2806 src = valaddr + offset;
f93fca70
JB
2807 }
2808 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2809 {
0cafa88c 2810 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2811 gdb_byte *buf;
0cafa88c 2812
f93fca70 2813 v = value_at (type, value_address (obj) + offset);
bfb1c796
PA
2814 buf = (gdb_byte *) alloca (src_len);
2815 read_memory (value_address (v), buf, src_len);
2816 src = buf;
f93fca70
JB
2817 }
2818 else
2819 {
2820 v = allocate_value (type);
50888e42 2821 src = value_contents (obj).data () + offset;
f93fca70
JB
2822 }
2823
2824 if (obj != NULL)
2825 {
2826 long new_offset = offset;
2827
2828 set_value_component_location (v, obj);
2829 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2830 set_value_bitsize (v, bit_size);
2831 if (value_bitpos (v) >= HOST_CHAR_BIT)
dda83cd7 2832 {
f93fca70 2833 ++new_offset;
dda83cd7
SM
2834 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2835 }
f93fca70
JB
2836 set_value_offset (v, new_offset);
2837
2838 /* Also set the parent value. This is needed when trying to
2839 assign a new value (in inferior memory). */
2840 set_value_parent (v, obj);
2841 }
2842 else
2843 set_value_bitsize (v, bit_size);
50888e42 2844 unpacked = value_contents_writeable (v).data ();
f93fca70
JB
2845
2846 if (bit_size == 0)
2847 {
2848 memset (unpacked, 0, TYPE_LENGTH (type));
2849 return v;
2850 }
2851
d5722aa2 2852 if (staging.size () == TYPE_LENGTH (type))
f93fca70 2853 {
d0a9e810
JB
2854 /* Small short-cut: If we've unpacked the data into a buffer
2855 of the same size as TYPE's length, then we can reuse that,
2856 instead of doing the unpacking again. */
d5722aa2 2857 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2858 }
d0a9e810
JB
2859 else
2860 ada_unpack_from_contents (src, bit_offset, bit_size,
2861 unpacked, TYPE_LENGTH (type),
2862 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2863
14f9c5c9
AS
2864 return v;
2865}
d2e4a39e 2866
14f9c5c9
AS
2867/* Store the contents of FROMVAL into the location of TOVAL.
2868 Return a new value with the location of TOVAL and contents of
2869 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2870 floating-point or non-scalar types. */
14f9c5c9 2871
d2e4a39e
AS
2872static struct value *
2873ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2874{
df407dfe
AC
2875 struct type *type = value_type (toval);
2876 int bits = value_bitsize (toval);
14f9c5c9 2877
52ce6436
PH
2878 toval = ada_coerce_ref (toval);
2879 fromval = ada_coerce_ref (fromval);
2880
2881 if (ada_is_direct_array_type (value_type (toval)))
2882 toval = ada_coerce_to_simple_array (toval);
2883 if (ada_is_direct_array_type (value_type (fromval)))
2884 fromval = ada_coerce_to_simple_array (fromval);
2885
88e3b34b 2886 if (!deprecated_value_modifiable (toval))
323e0a4a 2887 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2888
d2e4a39e 2889 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2890 && bits > 0
78134374 2891 && (type->code () == TYPE_CODE_FLT
dda83cd7 2892 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2893 {
df407dfe
AC
2894 int len = (value_bitpos (toval)
2895 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2896 int from_size;
224c3ddb 2897 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2898 struct value *val;
42ae5230 2899 CORE_ADDR to_addr = value_address (toval);
14f9c5c9 2900
78134374 2901 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2902 fromval = value_cast (type, fromval);
14f9c5c9 2903
52ce6436 2904 read_memory (to_addr, buffer, len);
aced2898
PH
2905 from_size = value_bitsize (fromval);
2906 if (from_size == 0)
2907 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
d48e62f4 2908
d5a22e77 2909 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4
TT
2910 ULONGEST from_offset = 0;
2911 if (is_big_endian && is_scalar_type (value_type (fromval)))
2912 from_offset = from_size - bits;
2913 copy_bitwise (buffer, value_bitpos (toval),
50888e42 2914 value_contents (fromval).data (), from_offset,
d48e62f4 2915 bits, is_big_endian);
972daa01 2916 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2917
14f9c5c9 2918 val = value_copy (toval);
fb2a515f
SM
2919 memcpy (value_contents_raw (val).data (),
2920 value_contents (fromval).data (),
2921 TYPE_LENGTH (type));
04624583 2922 deprecated_set_value_type (val, type);
d2e4a39e 2923
14f9c5c9
AS
2924 return val;
2925 }
2926
2927 return value_assign (toval, fromval);
2928}
2929
2930
7c512744
JB
2931/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2932 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2933 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2934 COMPONENT, and not the inferior's memory. The current contents
2935 of COMPONENT are ignored.
2936
2937 Although not part of the initial design, this function also works
2938 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2939 had a null address, and COMPONENT had an address which is equal to
2940 its offset inside CONTAINER. */
2941
52ce6436
PH
2942static void
2943value_assign_to_component (struct value *container, struct value *component,
2944 struct value *val)
2945{
2946 LONGEST offset_in_container =
42ae5230 2947 (LONGEST) (value_address (component) - value_address (container));
7c512744 2948 int bit_offset_in_container =
52ce6436
PH
2949 value_bitpos (component) - value_bitpos (container);
2950 int bits;
7c512744 2951
52ce6436
PH
2952 val = value_cast (value_type (component), val);
2953
2954 if (value_bitsize (component) == 0)
2955 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2956 else
2957 bits = value_bitsize (component);
2958
d5a22e77 2959 if (type_byte_order (value_type (container)) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2960 {
2961 int src_offset;
2962
2963 if (is_scalar_type (check_typedef (value_type (component))))
dda83cd7 2964 src_offset
2a62dfa9
JB
2965 = TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits;
2966 else
2967 src_offset = 0;
50888e42
SM
2968 copy_bitwise ((value_contents_writeable (container).data ()
2969 + offset_in_container),
a99bc3d2 2970 value_bitpos (container) + bit_offset_in_container,
50888e42 2971 value_contents (val).data (), src_offset, bits, 1);
2a62dfa9 2972 }
52ce6436 2973 else
50888e42
SM
2974 copy_bitwise ((value_contents_writeable (container).data ()
2975 + offset_in_container),
a99bc3d2 2976 value_bitpos (container) + bit_offset_in_container,
50888e42 2977 value_contents (val).data (), 0, bits, 0);
7c512744
JB
2978}
2979
736ade86
XR
2980/* Determine if TYPE is an access to an unconstrained array. */
2981
d91e9ea8 2982bool
736ade86
XR
2983ada_is_access_to_unconstrained_array (struct type *type)
2984{
78134374 2985 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2986 && is_thick_pntr (ada_typedef_target_type (type)));
2987}
2988
4c4b4cd2
PH
2989/* The value of the element of array ARR at the ARITY indices given in IND.
2990 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2991 thereto. */
2992
d2e4a39e
AS
2993struct value *
2994ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2995{
2996 int k;
d2e4a39e
AS
2997 struct value *elt;
2998 struct type *elt_type;
14f9c5c9
AS
2999
3000 elt = ada_coerce_to_simple_array (arr);
3001
df407dfe 3002 elt_type = ada_check_typedef (value_type (elt));
78134374 3003 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
3004 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
3005 return value_subscript_packed (elt, arity, ind);
3006
3007 for (k = 0; k < arity; k += 1)
3008 {
b9c50e9a
XR
3009 struct type *saved_elt_type = TYPE_TARGET_TYPE (elt_type);
3010
78134374 3011 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3012 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3013
2497b498 3014 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3015
3016 if (ada_is_access_to_unconstrained_array (saved_elt_type)
78134374 3017 && value_type (elt)->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3018 {
3019 /* The element is a typedef to an unconstrained array,
3020 except that the value_subscript call stripped the
3021 typedef layer. The typedef layer is GNAT's way to
3022 specify that the element is, at the source level, an
3023 access to the unconstrained array, rather than the
3024 unconstrained array. So, we need to restore that
3025 typedef layer, which we can do by forcing the element's
3026 type back to its original type. Otherwise, the returned
3027 value is going to be printed as the array, rather
3028 than as an access. Another symptom of the same issue
3029 would be that an expression trying to dereference the
3030 element would also be improperly rejected. */
3031 deprecated_set_value_type (elt, saved_elt_type);
3032 }
3033
3034 elt_type = ada_check_typedef (value_type (elt));
14f9c5c9 3035 }
b9c50e9a 3036
14f9c5c9
AS
3037 return elt;
3038}
3039
deede10c
JB
3040/* Assuming ARR is a pointer to a GDB array, the value of the element
3041 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3042 Does not read the entire array into memory.
3043
3044 Note: Unlike what one would expect, this function is used instead of
3045 ada_value_subscript for basically all non-packed array types. The reason
3046 for this is that a side effect of doing our own pointer arithmetics instead
3047 of relying on value_subscript is that there is no implicit typedef peeling.
3048 This is important for arrays of array accesses, where it allows us to
3049 preserve the fact that the array's element is an array access, where the
3050 access part os encoded in a typedef layer. */
14f9c5c9 3051
2c0b251b 3052static struct value *
deede10c 3053ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3054{
3055 int k;
919e6dbe 3056 struct value *array_ind = ada_value_ind (arr);
deede10c 3057 struct type *type
919e6dbe
PMR
3058 = check_typedef (value_enclosing_type (array_ind));
3059
78134374 3060 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
3061 && TYPE_FIELD_BITSIZE (type, 0) > 0)
3062 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3063
3064 for (k = 0; k < arity; k += 1)
3065 {
3066 LONGEST lwb, upb;
14f9c5c9 3067
78134374 3068 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3069 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 3070 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
dda83cd7 3071 value_copy (arr));
3d967001 3072 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3073 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
14f9c5c9
AS
3074 type = TYPE_TARGET_TYPE (type);
3075 }
3076
3077 return value_ind (arr);
3078}
3079
0b5d8877 3080/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3081 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3082 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3083 this array is LOW, as per Ada rules. */
0b5d8877 3084static struct value *
f5938064 3085ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3086 int low, int high)
0b5d8877 3087{
b0dd7688 3088 struct type *type0 = ada_check_typedef (type);
3d967001 3089 struct type *base_index_type = TYPE_TARGET_TYPE (type0->index_type ());
0c9c3474 3090 struct type *index_type
aa715135 3091 = create_static_range_type (NULL, base_index_type, low, high);
9fe561ab
JB
3092 struct type *slice_type = create_array_type_with_stride
3093 (NULL, TYPE_TARGET_TYPE (type0), index_type,
24e99c6c 3094 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3095 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 3096 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 3097 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3098 CORE_ADDR base;
3099
6244c119
SM
3100 low_pos = discrete_position (base_index_type, low);
3101 base_low_pos = discrete_position (base_index_type, base_low);
3102
3103 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3104 {
3105 warning (_("unable to get positions in slice, use bounds instead"));
3106 low_pos = low;
3107 base_low_pos = base_low;
3108 }
5b4ee69b 3109
7ff5b937
TT
3110 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
3111 if (stride == 0)
3112 stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
3113
6244c119 3114 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3115 return value_at_lazy (slice_type, base);
0b5d8877
PH
3116}
3117
3118
3119static struct value *
3120ada_value_slice (struct value *array, int low, int high)
3121{
b0dd7688 3122 struct type *type = ada_check_typedef (value_type (array));
3d967001 3123 struct type *base_index_type = TYPE_TARGET_TYPE (type->index_type ());
0c9c3474 3124 struct type *index_type
3d967001 3125 = create_static_range_type (NULL, type->index_type (), low, high);
9fe561ab
JB
3126 struct type *slice_type = create_array_type_with_stride
3127 (NULL, TYPE_TARGET_TYPE (type), index_type,
24e99c6c 3128 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3129 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
3130 gdb::optional<LONGEST> low_pos, high_pos;
3131
5b4ee69b 3132
6244c119
SM
3133 low_pos = discrete_position (base_index_type, low);
3134 high_pos = discrete_position (base_index_type, high);
3135
3136 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3137 {
3138 warning (_("unable to get positions in slice, use bounds instead"));
3139 low_pos = low;
3140 high_pos = high;
3141 }
3142
3143 return value_cast (slice_type,
6244c119 3144 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3145}
3146
14f9c5c9
AS
3147/* If type is a record type in the form of a standard GNAT array
3148 descriptor, returns the number of dimensions for type. If arr is a
3149 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3150 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3151
3152int
d2e4a39e 3153ada_array_arity (struct type *type)
14f9c5c9
AS
3154{
3155 int arity;
3156
3157 if (type == NULL)
3158 return 0;
3159
3160 type = desc_base_type (type);
3161
3162 arity = 0;
78134374 3163 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3164 return desc_arity (desc_bounds_type (type));
d2e4a39e 3165 else
78134374 3166 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3167 {
dda83cd7
SM
3168 arity += 1;
3169 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 3170 }
d2e4a39e 3171
14f9c5c9
AS
3172 return arity;
3173}
3174
3175/* If TYPE is a record type in the form of a standard GNAT array
3176 descriptor or a simple array type, returns the element type for
3177 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3178 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3179
d2e4a39e
AS
3180struct type *
3181ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3182{
3183 type = desc_base_type (type);
3184
78134374 3185 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3186 {
3187 int k;
d2e4a39e 3188 struct type *p_array_type;
14f9c5c9 3189
556bdfd4 3190 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3191
3192 k = ada_array_arity (type);
3193 if (k == 0)
dda83cd7 3194 return NULL;
d2e4a39e 3195
4c4b4cd2 3196 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3197 if (nindices >= 0 && k > nindices)
dda83cd7 3198 k = nindices;
d2e4a39e 3199 while (k > 0 && p_array_type != NULL)
dda83cd7
SM
3200 {
3201 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
3202 k -= 1;
3203 }
14f9c5c9
AS
3204 return p_array_type;
3205 }
78134374 3206 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3207 {
78134374 3208 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7
SM
3209 {
3210 type = TYPE_TARGET_TYPE (type);
3211 nindices -= 1;
3212 }
14f9c5c9
AS
3213 return type;
3214 }
3215
3216 return NULL;
3217}
3218
08a057e6 3219/* See ada-lang.h. */
14f9c5c9 3220
08a057e6 3221struct type *
1eea4ebd 3222ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3223{
4c4b4cd2
PH
3224 struct type *result_type;
3225
14f9c5c9
AS
3226 type = desc_base_type (type);
3227
1eea4ebd
UW
3228 if (n < 0 || n > ada_array_arity (type))
3229 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3230
4c4b4cd2 3231 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3232 {
3233 int i;
3234
3235 for (i = 1; i < n; i += 1)
2869ac4b
TT
3236 {
3237 type = ada_check_typedef (type);
3238 type = TYPE_TARGET_TYPE (type);
3239 }
3240 result_type = TYPE_TARGET_TYPE (ada_check_typedef (type)->index_type ());
4c4b4cd2 3241 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3242 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3243 perhaps stabsread.c would make more sense. */
78134374 3244 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3245 result_type = NULL;
14f9c5c9 3246 }
d2e4a39e 3247 else
1eea4ebd
UW
3248 {
3249 result_type = desc_index_type (desc_bounds_type (type), n);
3250 if (result_type == NULL)
3251 error (_("attempt to take bound of something that is not an array"));
3252 }
3253
3254 return result_type;
14f9c5c9
AS
3255}
3256
3257/* Given that arr is an array type, returns the lower bound of the
3258 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3259 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3260 array-descriptor type. It works for other arrays with bounds supplied
3261 by run-time quantities other than discriminants. */
14f9c5c9 3262
abb68b3e 3263static LONGEST
fb5e3d5c 3264ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3265{
8a48ac95 3266 struct type *type, *index_type_desc, *index_type;
1ce677a4 3267 int i;
262452ec
JK
3268
3269 gdb_assert (which == 0 || which == 1);
14f9c5c9 3270
ad82864c
JB
3271 if (ada_is_constrained_packed_array_type (arr_type))
3272 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3273
4c4b4cd2 3274 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 3275 return (LONGEST) - which;
14f9c5c9 3276
78134374 3277 if (arr_type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
3278 type = TYPE_TARGET_TYPE (arr_type);
3279 else
3280 type = arr_type;
3281
22c4c60c 3282 if (type->is_fixed_instance ())
bafffb51
JB
3283 {
3284 /* The array has already been fixed, so we do not need to
3285 check the parallel ___XA type again. That encoding has
3286 already been applied, so ignore it now. */
3287 index_type_desc = NULL;
3288 }
3289 else
3290 {
3291 index_type_desc = ada_find_parallel_type (type, "___XA");
3292 ada_fixup_array_indexes_type (index_type_desc);
3293 }
3294
262452ec 3295 if (index_type_desc != NULL)
940da03e 3296 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3297 NULL);
262452ec 3298 else
8a48ac95
JB
3299 {
3300 struct type *elt_type = check_typedef (type);
3301
3302 for (i = 1; i < n; i++)
3303 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
3304
3d967001 3305 index_type = elt_type->index_type ();
8a48ac95 3306 }
262452ec 3307
43bbcdc2
PH
3308 return
3309 (LONGEST) (which == 0
dda83cd7
SM
3310 ? ada_discrete_type_low_bound (index_type)
3311 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3312}
3313
3314/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3315 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3316 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3317 supplied by run-time quantities other than discriminants. */
14f9c5c9 3318
1eea4ebd 3319static LONGEST
4dc81987 3320ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3321{
eb479039
JB
3322 struct type *arr_type;
3323
78134374 3324 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3325 arr = value_ind (arr);
3326 arr_type = value_enclosing_type (arr);
14f9c5c9 3327
ad82864c
JB
3328 if (ada_is_constrained_packed_array_type (arr_type))
3329 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3330 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3331 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3332 else
1eea4ebd 3333 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3334}
3335
3336/* Given that arr is an array value, returns the length of the
3337 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3338 supplied by run-time quantities other than discriminants.
3339 Does not work for arrays indexed by enumeration types with representation
3340 clauses at the moment. */
14f9c5c9 3341
1eea4ebd 3342static LONGEST
d2e4a39e 3343ada_array_length (struct value *arr, int n)
14f9c5c9 3344{
aa715135
JG
3345 struct type *arr_type, *index_type;
3346 int low, high;
eb479039 3347
78134374 3348 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3349 arr = value_ind (arr);
3350 arr_type = value_enclosing_type (arr);
14f9c5c9 3351
ad82864c
JB
3352 if (ada_is_constrained_packed_array_type (arr_type))
3353 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3354
4c4b4cd2 3355 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3356 {
3357 low = ada_array_bound_from_type (arr_type, n, 0);
3358 high = ada_array_bound_from_type (arr_type, n, 1);
3359 }
14f9c5c9 3360 else
aa715135
JG
3361 {
3362 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3363 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3364 }
3365
f168693b 3366 arr_type = check_typedef (arr_type);
7150d33c 3367 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3368 if (index_type != NULL)
3369 {
3370 struct type *base_type;
78134374 3371 if (index_type->code () == TYPE_CODE_RANGE)
aa715135
JG
3372 base_type = TYPE_TARGET_TYPE (index_type);
3373 else
3374 base_type = index_type;
3375
3376 low = pos_atr (value_from_longest (base_type, low));
3377 high = pos_atr (value_from_longest (base_type, high));
3378 }
3379 return high - low + 1;
4c4b4cd2
PH
3380}
3381
bff8c71f
TT
3382/* An array whose type is that of ARR_TYPE (an array type), with
3383 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3384 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3385
3386static struct value *
bff8c71f 3387empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3388{
b0dd7688 3389 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3390 struct type *index_type
3391 = create_static_range_type
dda83cd7 3392 (NULL, TYPE_TARGET_TYPE (arr_type0->index_type ()), low,
bff8c71f 3393 high < low ? low - 1 : high);
b0dd7688 3394 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3395
0b5d8877 3396 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3397}
14f9c5c9 3398\f
d2e4a39e 3399
dda83cd7 3400 /* Name resolution */
14f9c5c9 3401
4c4b4cd2
PH
3402/* The "decoded" name for the user-definable Ada operator corresponding
3403 to OP. */
14f9c5c9 3404
d2e4a39e 3405static const char *
4c4b4cd2 3406ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3407{
3408 int i;
3409
4c4b4cd2 3410 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3411 {
3412 if (ada_opname_table[i].op == op)
dda83cd7 3413 return ada_opname_table[i].decoded;
14f9c5c9 3414 }
323e0a4a 3415 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3416}
3417
de93309a
SM
3418/* Returns true (non-zero) iff decoded name N0 should appear before N1
3419 in a listing of choices during disambiguation (see sort_choices, below).
3420 The idea is that overloadings of a subprogram name from the
3421 same package should sort in their source order. We settle for ordering
3422 such symbols by their trailing number (__N or $N). */
14f9c5c9 3423
de93309a
SM
3424static int
3425encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3426{
de93309a
SM
3427 if (N1 == NULL)
3428 return 0;
3429 else if (N0 == NULL)
3430 return 1;
3431 else
3432 {
3433 int k0, k1;
30b15541 3434
de93309a 3435 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3436 ;
de93309a 3437 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3438 ;
de93309a 3439 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3440 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3441 {
3442 int n0, n1;
3443
3444 n0 = k0;
3445 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3446 n0 -= 1;
3447 n1 = k1;
3448 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3449 n1 -= 1;
3450 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3451 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3452 }
de93309a
SM
3453 return (strcmp (N0, N1) < 0);
3454 }
14f9c5c9
AS
3455}
3456
de93309a
SM
3457/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3458 encoded names. */
14f9c5c9 3459
de93309a
SM
3460static void
3461sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3462{
14f9c5c9 3463 int i;
14f9c5c9 3464
de93309a 3465 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3466 {
de93309a
SM
3467 struct block_symbol sym = syms[i];
3468 int j;
3469
3470 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3471 {
3472 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3473 sym.symbol->linkage_name ()))
3474 break;
3475 syms[j + 1] = syms[j];
3476 }
de93309a
SM
3477 syms[j + 1] = sym;
3478 }
3479}
14f9c5c9 3480
de93309a
SM
3481/* Whether GDB should display formals and return types for functions in the
3482 overloads selection menu. */
3483static bool print_signatures = true;
4c4b4cd2 3484
de93309a
SM
3485/* Print the signature for SYM on STREAM according to the FLAGS options. For
3486 all but functions, the signature is just the name of the symbol. For
3487 functions, this is the name of the function, the list of types for formals
3488 and the return type (if any). */
4c4b4cd2 3489
de93309a
SM
3490static void
3491ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3492 const struct type_print_options *flags)
3493{
5f9c5a63 3494 struct type *type = sym->type ();
14f9c5c9 3495
6cb06a8c 3496 gdb_printf (stream, "%s", sym->print_name ());
de93309a
SM
3497 if (!print_signatures
3498 || type == NULL
78134374 3499 || type->code () != TYPE_CODE_FUNC)
de93309a 3500 return;
4c4b4cd2 3501
1f704f76 3502 if (type->num_fields () > 0)
de93309a
SM
3503 {
3504 int i;
14f9c5c9 3505
6cb06a8c 3506 gdb_printf (stream, " (");
1f704f76 3507 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3508 {
3509 if (i > 0)
6cb06a8c 3510 gdb_printf (stream, "; ");
940da03e 3511 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3512 flags);
3513 }
6cb06a8c 3514 gdb_printf (stream, ")");
de93309a
SM
3515 }
3516 if (TYPE_TARGET_TYPE (type) != NULL
78134374 3517 && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
de93309a 3518 {
6cb06a8c 3519 gdb_printf (stream, " return ");
de93309a
SM
3520 ada_print_type (TYPE_TARGET_TYPE (type), NULL, stream, -1, 0, flags);
3521 }
3522}
14f9c5c9 3523
de93309a
SM
3524/* Read and validate a set of numeric choices from the user in the
3525 range 0 .. N_CHOICES-1. Place the results in increasing
3526 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3527
de93309a
SM
3528 The user types choices as a sequence of numbers on one line
3529 separated by blanks, encoding them as follows:
14f9c5c9 3530
de93309a
SM
3531 + A choice of 0 means to cancel the selection, throwing an error.
3532 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3533 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3534
de93309a 3535 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3536
de93309a
SM
3537 ANNOTATION_SUFFIX, if present, is used to annotate the input
3538 prompts (for use with the -f switch). */
14f9c5c9 3539
de93309a
SM
3540static int
3541get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3542 int is_all_choice, const char *annotation_suffix)
de93309a 3543{
992a7040 3544 const char *args;
de93309a
SM
3545 const char *prompt;
3546 int n_chosen;
3547 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3548
de93309a
SM
3549 prompt = getenv ("PS2");
3550 if (prompt == NULL)
3551 prompt = "> ";
4c4b4cd2 3552
de93309a 3553 args = command_line_input (prompt, annotation_suffix);
4c4b4cd2 3554
de93309a
SM
3555 if (args == NULL)
3556 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3557
de93309a 3558 n_chosen = 0;
4c4b4cd2 3559
de93309a
SM
3560 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3561 order, as given in args. Choices are validated. */
3562 while (1)
14f9c5c9 3563 {
de93309a
SM
3564 char *args2;
3565 int choice, j;
76a01679 3566
de93309a
SM
3567 args = skip_spaces (args);
3568 if (*args == '\0' && n_chosen == 0)
dda83cd7 3569 error_no_arg (_("one or more choice numbers"));
de93309a 3570 else if (*args == '\0')
dda83cd7 3571 break;
76a01679 3572
de93309a
SM
3573 choice = strtol (args, &args2, 10);
3574 if (args == args2 || choice < 0
dda83cd7
SM
3575 || choice > n_choices + first_choice - 1)
3576 error (_("Argument must be choice number"));
de93309a 3577 args = args2;
76a01679 3578
de93309a 3579 if (choice == 0)
dda83cd7 3580 error (_("cancelled"));
76a01679 3581
de93309a 3582 if (choice < first_choice)
dda83cd7
SM
3583 {
3584 n_chosen = n_choices;
3585 for (j = 0; j < n_choices; j += 1)
3586 choices[j] = j;
3587 break;
3588 }
de93309a 3589 choice -= first_choice;
76a01679 3590
de93309a 3591 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3592 {
3593 }
4c4b4cd2 3594
de93309a 3595 if (j < 0 || choice != choices[j])
dda83cd7
SM
3596 {
3597 int k;
4c4b4cd2 3598
dda83cd7
SM
3599 for (k = n_chosen - 1; k > j; k -= 1)
3600 choices[k + 1] = choices[k];
3601 choices[j + 1] = choice;
3602 n_chosen += 1;
3603 }
14f9c5c9
AS
3604 }
3605
de93309a
SM
3606 if (n_chosen > max_results)
3607 error (_("Select no more than %d of the above"), max_results);
3608
3609 return n_chosen;
14f9c5c9
AS
3610}
3611
de93309a
SM
3612/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3613 by asking the user (if necessary), returning the number selected,
3614 and setting the first elements of SYMS items. Error if no symbols
3615 selected. */
3616
3617/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3618 to be re-integrated one of these days. */
14f9c5c9
AS
3619
3620static int
de93309a 3621user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3622{
de93309a
SM
3623 int i;
3624 int *chosen = XALLOCAVEC (int , nsyms);
3625 int n_chosen;
3626 int first_choice = (max_results == 1) ? 1 : 2;
3627 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3628
de93309a
SM
3629 if (max_results < 1)
3630 error (_("Request to select 0 symbols!"));
3631 if (nsyms <= 1)
3632 return nsyms;
14f9c5c9 3633
de93309a
SM
3634 if (select_mode == multiple_symbols_cancel)
3635 error (_("\
3636canceled because the command is ambiguous\n\
3637See set/show multiple-symbol."));
14f9c5c9 3638
de93309a
SM
3639 /* If select_mode is "all", then return all possible symbols.
3640 Only do that if more than one symbol can be selected, of course.
3641 Otherwise, display the menu as usual. */
3642 if (select_mode == multiple_symbols_all && max_results > 1)
3643 return nsyms;
14f9c5c9 3644
6cb06a8c 3645 gdb_printf (_("[0] cancel\n"));
de93309a 3646 if (max_results > 1)
6cb06a8c 3647 gdb_printf (_("[1] all\n"));
14f9c5c9 3648
de93309a 3649 sort_choices (syms, nsyms);
14f9c5c9 3650
de93309a
SM
3651 for (i = 0; i < nsyms; i += 1)
3652 {
3653 if (syms[i].symbol == NULL)
dda83cd7 3654 continue;
14f9c5c9 3655
66d7f48f 3656 if (syms[i].symbol->aclass () == LOC_BLOCK)
dda83cd7
SM
3657 {
3658 struct symtab_and_line sal =
3659 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3660
6cb06a8c 3661 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3662 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3663 &type_print_raw_options);
3664 if (sal.symtab == NULL)
6cb06a8c
TT
3665 gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
3666 metadata_style.style ().ptr (), nullptr, sal.line);
de93309a 3667 else
6cb06a8c 3668 gdb_printf
de93309a
SM
3669 (_(" at %ps:%d\n"),
3670 styled_string (file_name_style.style (),
3671 symtab_to_filename_for_display (sal.symtab)),
3672 sal.line);
dda83cd7
SM
3673 continue;
3674 }
76a01679 3675 else
dda83cd7
SM
3676 {
3677 int is_enumeral =
66d7f48f 3678 (syms[i].symbol->aclass () == LOC_CONST
5f9c5a63
SM
3679 && syms[i].symbol->type () != NULL
3680 && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
de93309a 3681 struct symtab *symtab = NULL;
4c4b4cd2 3682
7b3ecc75 3683 if (syms[i].symbol->is_objfile_owned ())
4206d69e 3684 symtab = syms[i].symbol->symtab ();
de93309a 3685
5d0027b9 3686 if (syms[i].symbol->line () != 0 && symtab != NULL)
de93309a 3687 {
6cb06a8c 3688 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3689 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3690 &type_print_raw_options);
6cb06a8c
TT
3691 gdb_printf (_(" at %s:%d\n"),
3692 symtab_to_filename_for_display (symtab),
3693 syms[i].symbol->line ());
de93309a 3694 }
dda83cd7 3695 else if (is_enumeral
5f9c5a63 3696 && syms[i].symbol->type ()->name () != NULL)
dda83cd7 3697 {
6cb06a8c 3698 gdb_printf (("[%d] "), i + first_choice);
5f9c5a63 3699 ada_print_type (syms[i].symbol->type (), NULL,
dda83cd7 3700 gdb_stdout, -1, 0, &type_print_raw_options);
6cb06a8c
TT
3701 gdb_printf (_("'(%s) (enumeral)\n"),
3702 syms[i].symbol->print_name ());
dda83cd7 3703 }
de93309a
SM
3704 else
3705 {
6cb06a8c 3706 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3707 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3708 &type_print_raw_options);
3709
3710 if (symtab != NULL)
6cb06a8c
TT
3711 gdb_printf (is_enumeral
3712 ? _(" in %s (enumeral)\n")
3713 : _(" at %s:?\n"),
3714 symtab_to_filename_for_display (symtab));
de93309a 3715 else
6cb06a8c
TT
3716 gdb_printf (is_enumeral
3717 ? _(" (enumeral)\n")
3718 : _(" at ?\n"));
de93309a 3719 }
dda83cd7 3720 }
14f9c5c9 3721 }
14f9c5c9 3722
de93309a 3723 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3724 "overload-choice");
14f9c5c9 3725
de93309a
SM
3726 for (i = 0; i < n_chosen; i += 1)
3727 syms[i] = syms[chosen[i]];
14f9c5c9 3728
de93309a
SM
3729 return n_chosen;
3730}
14f9c5c9 3731
cd9a3148
TT
3732/* See ada-lang.h. */
3733
3734block_symbol
7056f312 3735ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3736 int nargs, value *argvec[])
3737{
3738 if (possible_user_operator_p (op, argvec))
3739 {
3740 std::vector<struct block_symbol> candidates
3741 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3742 NULL, VAR_DOMAIN);
3743
3744 int i = ada_resolve_function (candidates, argvec,
3745 nargs, ada_decoded_op_name (op), NULL,
3746 parse_completion);
3747 if (i >= 0)
3748 return candidates[i];
3749 }
3750 return {};
3751}
3752
3753/* See ada-lang.h. */
3754
3755block_symbol
3756ada_resolve_funcall (struct symbol *sym, const struct block *block,
3757 struct type *context_type,
7056f312 3758 bool parse_completion,
cd9a3148
TT
3759 int nargs, value *argvec[],
3760 innermost_block_tracker *tracker)
3761{
3762 std::vector<struct block_symbol> candidates
3763 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3764
3765 int i;
3766 if (candidates.size () == 1)
3767 i = 0;
3768 else
3769 {
3770 i = ada_resolve_function
3771 (candidates,
3772 argvec, nargs,
3773 sym->linkage_name (),
3774 context_type, parse_completion);
3775 if (i < 0)
3776 error (_("Could not find a match for %s"), sym->print_name ());
3777 }
3778
3779 tracker->update (candidates[i]);
3780 return candidates[i];
3781}
3782
ba8694b6
TT
3783/* Resolve a mention of a name where the context type is an
3784 enumeration type. */
3785
3786static int
3787ada_resolve_enum (std::vector<struct block_symbol> &syms,
3788 const char *name, struct type *context_type,
3789 bool parse_completion)
3790{
3791 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3792 context_type = ada_check_typedef (context_type);
3793
3794 for (int i = 0; i < syms.size (); ++i)
3795 {
3796 /* We already know the name matches, so we're just looking for
3797 an element of the correct enum type. */
5f9c5a63 3798 if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
ba8694b6
TT
3799 return i;
3800 }
3801
3802 error (_("No name '%s' in enumeration type '%s'"), name,
3803 ada_type_name (context_type));
3804}
3805
cd9a3148
TT
3806/* See ada-lang.h. */
3807
3808block_symbol
3809ada_resolve_variable (struct symbol *sym, const struct block *block,
3810 struct type *context_type,
7056f312 3811 bool parse_completion,
cd9a3148
TT
3812 int deprocedure_p,
3813 innermost_block_tracker *tracker)
3814{
3815 std::vector<struct block_symbol> candidates
3816 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3817
3818 if (std::any_of (candidates.begin (),
3819 candidates.end (),
3820 [] (block_symbol &bsym)
3821 {
66d7f48f 3822 switch (bsym.symbol->aclass ())
cd9a3148
TT
3823 {
3824 case LOC_REGISTER:
3825 case LOC_ARG:
3826 case LOC_REF_ARG:
3827 case LOC_REGPARM_ADDR:
3828 case LOC_LOCAL:
3829 case LOC_COMPUTED:
3830 return true;
3831 default:
3832 return false;
3833 }
3834 }))
3835 {
3836 /* Types tend to get re-introduced locally, so if there
3837 are any local symbols that are not types, first filter
3838 out all types. */
3839 candidates.erase
3840 (std::remove_if
3841 (candidates.begin (),
3842 candidates.end (),
3843 [] (block_symbol &bsym)
3844 {
66d7f48f 3845 return bsym.symbol->aclass () == LOC_TYPEDEF;
cd9a3148
TT
3846 }),
3847 candidates.end ());
3848 }
3849
2c71f639
TV
3850 /* Filter out artificial symbols. */
3851 candidates.erase
3852 (std::remove_if
3853 (candidates.begin (),
3854 candidates.end (),
3855 [] (block_symbol &bsym)
3856 {
496feb16 3857 return bsym.symbol->is_artificial ();
2c71f639
TV
3858 }),
3859 candidates.end ());
3860
cd9a3148
TT
3861 int i;
3862 if (candidates.empty ())
3863 error (_("No definition found for %s"), sym->print_name ());
3864 else if (candidates.size () == 1)
3865 i = 0;
ba8694b6
TT
3866 else if (context_type != nullptr
3867 && context_type->code () == TYPE_CODE_ENUM)
3868 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3869 parse_completion);
cd9a3148
TT
3870 else if (deprocedure_p && !is_nonfunction (candidates))
3871 {
3872 i = ada_resolve_function
3873 (candidates, NULL, 0,
3874 sym->linkage_name (),
3875 context_type, parse_completion);
3876 if (i < 0)
3877 error (_("Could not find a match for %s"), sym->print_name ());
3878 }
3879 else
3880 {
6cb06a8c 3881 gdb_printf (_("Multiple matches for %s\n"), sym->print_name ());
cd9a3148
TT
3882 user_select_syms (candidates.data (), candidates.size (), 1);
3883 i = 0;
3884 }
3885
3886 tracker->update (candidates[i]);
3887 return candidates[i];
3888}
3889
db2534b7 3890/* Return non-zero if formal type FTYPE matches actual type ATYPE. */
de93309a
SM
3891/* The term "match" here is rather loose. The match is heuristic and
3892 liberal. */
14f9c5c9 3893
de93309a 3894static int
db2534b7 3895ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3896{
de93309a
SM
3897 ftype = ada_check_typedef (ftype);
3898 atype = ada_check_typedef (atype);
14f9c5c9 3899
78134374 3900 if (ftype->code () == TYPE_CODE_REF)
de93309a 3901 ftype = TYPE_TARGET_TYPE (ftype);
78134374 3902 if (atype->code () == TYPE_CODE_REF)
de93309a 3903 atype = TYPE_TARGET_TYPE (atype);
14f9c5c9 3904
78134374 3905 switch (ftype->code ())
14f9c5c9 3906 {
de93309a 3907 default:
78134374 3908 return ftype->code () == atype->code ();
de93309a 3909 case TYPE_CODE_PTR:
db2534b7
TT
3910 if (atype->code () != TYPE_CODE_PTR)
3911 return 0;
3912 atype = TYPE_TARGET_TYPE (atype);
3913 /* This can only happen if the actual argument is 'null'. */
3914 if (atype->code () == TYPE_CODE_INT && TYPE_LENGTH (atype) == 0)
3915 return 1;
3916 return ada_type_match (TYPE_TARGET_TYPE (ftype), atype);
de93309a
SM
3917 case TYPE_CODE_INT:
3918 case TYPE_CODE_ENUM:
3919 case TYPE_CODE_RANGE:
78134374 3920 switch (atype->code ())
dda83cd7
SM
3921 {
3922 case TYPE_CODE_INT:
3923 case TYPE_CODE_ENUM:
3924 case TYPE_CODE_RANGE:
3925 return 1;
3926 default:
3927 return 0;
3928 }
d2e4a39e 3929
de93309a 3930 case TYPE_CODE_ARRAY:
78134374 3931 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3932 || ada_is_array_descriptor_type (atype));
14f9c5c9 3933
de93309a
SM
3934 case TYPE_CODE_STRUCT:
3935 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3936 return (atype->code () == TYPE_CODE_ARRAY
3937 || ada_is_array_descriptor_type (atype));
de93309a 3938 else
dda83cd7
SM
3939 return (atype->code () == TYPE_CODE_STRUCT
3940 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3941
de93309a
SM
3942 case TYPE_CODE_UNION:
3943 case TYPE_CODE_FLT:
78134374 3944 return (atype->code () == ftype->code ());
de93309a 3945 }
14f9c5c9
AS
3946}
3947
de93309a
SM
3948/* Return non-zero if the formals of FUNC "sufficiently match" the
3949 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3950 may also be an enumeral, in which case it is treated as a 0-
3951 argument function. */
14f9c5c9 3952
de93309a
SM
3953static int
3954ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3955{
3956 int i;
5f9c5a63 3957 struct type *func_type = func->type ();
14f9c5c9 3958
66d7f48f 3959 if (func->aclass () == LOC_CONST
78134374 3960 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3961 return (n_actuals == 0);
78134374 3962 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3963 return 0;
14f9c5c9 3964
1f704f76 3965 if (func_type->num_fields () != n_actuals)
de93309a 3966 return 0;
14f9c5c9 3967
de93309a
SM
3968 for (i = 0; i < n_actuals; i += 1)
3969 {
3970 if (actuals[i] == NULL)
dda83cd7 3971 return 0;
de93309a 3972 else
dda83cd7
SM
3973 {
3974 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
3975 struct type *atype = ada_check_typedef (value_type (actuals[i]));
14f9c5c9 3976
db2534b7 3977 if (!ada_type_match (ftype, atype))
dda83cd7
SM
3978 return 0;
3979 }
de93309a
SM
3980 }
3981 return 1;
3982}
d2e4a39e 3983
de93309a
SM
3984/* False iff function type FUNC_TYPE definitely does not produce a value
3985 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3986 FUNC_TYPE is not a valid function type with a non-null return type
3987 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 3988
de93309a
SM
3989static int
3990return_match (struct type *func_type, struct type *context_type)
3991{
3992 struct type *return_type;
d2e4a39e 3993
de93309a
SM
3994 if (func_type == NULL)
3995 return 1;
14f9c5c9 3996
78134374 3997 if (func_type->code () == TYPE_CODE_FUNC)
de93309a
SM
3998 return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
3999 else
4000 return_type = get_base_type (func_type);
4001 if (return_type == NULL)
4002 return 1;
76a01679 4003
de93309a 4004 context_type = get_base_type (context_type);
14f9c5c9 4005
78134374 4006 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
4007 return context_type == NULL || return_type == context_type;
4008 else if (context_type == NULL)
78134374 4009 return return_type->code () != TYPE_CODE_VOID;
de93309a 4010 else
78134374 4011 return return_type->code () == context_type->code ();
de93309a 4012}
14f9c5c9 4013
14f9c5c9 4014
1bfa81ac 4015/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
4016 function (if any) that matches the types of the NARGS arguments in
4017 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4018 that returns that type, then eliminate matches that don't. If
4019 CONTEXT_TYPE is void and there is at least one match that does not
4020 return void, eliminate all matches that do.
14f9c5c9 4021
de93309a
SM
4022 Asks the user if there is more than one match remaining. Returns -1
4023 if there is no such symbol or none is selected. NAME is used
4024 solely for messages. May re-arrange and modify SYMS in
4025 the process; the index returned is for the modified vector. */
14f9c5c9 4026
de93309a 4027static int
d1183b06
TT
4028ada_resolve_function (std::vector<struct block_symbol> &syms,
4029 struct value **args, int nargs,
dda83cd7 4030 const char *name, struct type *context_type,
7056f312 4031 bool parse_completion)
de93309a
SM
4032{
4033 int fallback;
4034 int k;
4035 int m; /* Number of hits */
14f9c5c9 4036
de93309a
SM
4037 m = 0;
4038 /* In the first pass of the loop, we only accept functions matching
4039 context_type. If none are found, we add a second pass of the loop
4040 where every function is accepted. */
4041 for (fallback = 0; m == 0 && fallback < 2; fallback++)
4042 {
d1183b06 4043 for (k = 0; k < syms.size (); k += 1)
dda83cd7 4044 {
5f9c5a63 4045 struct type *type = ada_check_typedef (syms[k].symbol->type ());
5b4ee69b 4046
dda83cd7
SM
4047 if (ada_args_match (syms[k].symbol, args, nargs)
4048 && (fallback || return_match (type, context_type)))
4049 {
4050 syms[m] = syms[k];
4051 m += 1;
4052 }
4053 }
14f9c5c9
AS
4054 }
4055
de93309a
SM
4056 /* If we got multiple matches, ask the user which one to use. Don't do this
4057 interactive thing during completion, though, as the purpose of the
4058 completion is providing a list of all possible matches. Prompting the
4059 user to filter it down would be completely unexpected in this case. */
4060 if (m == 0)
4061 return -1;
4062 else if (m > 1 && !parse_completion)
4063 {
6cb06a8c 4064 gdb_printf (_("Multiple matches for %s\n"), name);
d1183b06 4065 user_select_syms (syms.data (), m, 1);
de93309a
SM
4066 return 0;
4067 }
4068 return 0;
14f9c5c9
AS
4069}
4070
14f9c5c9
AS
4071/* Type-class predicates */
4072
4c4b4cd2
PH
4073/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4074 or FLOAT). */
14f9c5c9
AS
4075
4076static int
d2e4a39e 4077numeric_type_p (struct type *type)
14f9c5c9
AS
4078{
4079 if (type == NULL)
4080 return 0;
d2e4a39e
AS
4081 else
4082 {
78134374 4083 switch (type->code ())
dda83cd7
SM
4084 {
4085 case TYPE_CODE_INT:
4086 case TYPE_CODE_FLT:
c04da66c 4087 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4088 return 1;
4089 case TYPE_CODE_RANGE:
4090 return (type == TYPE_TARGET_TYPE (type)
4091 || numeric_type_p (TYPE_TARGET_TYPE (type)));
4092 default:
4093 return 0;
4094 }
d2e4a39e 4095 }
14f9c5c9
AS
4096}
4097
4c4b4cd2 4098/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4099
4100static int
d2e4a39e 4101integer_type_p (struct type *type)
14f9c5c9
AS
4102{
4103 if (type == NULL)
4104 return 0;
d2e4a39e
AS
4105 else
4106 {
78134374 4107 switch (type->code ())
dda83cd7
SM
4108 {
4109 case TYPE_CODE_INT:
4110 return 1;
4111 case TYPE_CODE_RANGE:
4112 return (type == TYPE_TARGET_TYPE (type)
4113 || integer_type_p (TYPE_TARGET_TYPE (type)));
4114 default:
4115 return 0;
4116 }
d2e4a39e 4117 }
14f9c5c9
AS
4118}
4119
4c4b4cd2 4120/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4121
4122static int
d2e4a39e 4123scalar_type_p (struct type *type)
14f9c5c9
AS
4124{
4125 if (type == NULL)
4126 return 0;
d2e4a39e
AS
4127 else
4128 {
78134374 4129 switch (type->code ())
dda83cd7
SM
4130 {
4131 case TYPE_CODE_INT:
4132 case TYPE_CODE_RANGE:
4133 case TYPE_CODE_ENUM:
4134 case TYPE_CODE_FLT:
c04da66c 4135 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4136 return 1;
4137 default:
4138 return 0;
4139 }
d2e4a39e 4140 }
14f9c5c9
AS
4141}
4142
4c4b4cd2 4143/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
4144
4145static int
d2e4a39e 4146discrete_type_p (struct type *type)
14f9c5c9
AS
4147{
4148 if (type == NULL)
4149 return 0;
d2e4a39e
AS
4150 else
4151 {
78134374 4152 switch (type->code ())
dda83cd7
SM
4153 {
4154 case TYPE_CODE_INT:
4155 case TYPE_CODE_RANGE:
4156 case TYPE_CODE_ENUM:
4157 case TYPE_CODE_BOOL:
4158 return 1;
4159 default:
4160 return 0;
4161 }
d2e4a39e 4162 }
14f9c5c9
AS
4163}
4164
4c4b4cd2
PH
4165/* Returns non-zero if OP with operands in the vector ARGS could be
4166 a user-defined function. Errs on the side of pre-defined operators
4167 (i.e., result 0). */
14f9c5c9
AS
4168
4169static int
d2e4a39e 4170possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4171{
76a01679 4172 struct type *type0 =
df407dfe 4173 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 4174 struct type *type1 =
df407dfe 4175 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 4176
4c4b4cd2
PH
4177 if (type0 == NULL)
4178 return 0;
4179
14f9c5c9
AS
4180 switch (op)
4181 {
4182 default:
4183 return 0;
4184
4185 case BINOP_ADD:
4186 case BINOP_SUB:
4187 case BINOP_MUL:
4188 case BINOP_DIV:
d2e4a39e 4189 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4190
4191 case BINOP_REM:
4192 case BINOP_MOD:
4193 case BINOP_BITWISE_AND:
4194 case BINOP_BITWISE_IOR:
4195 case BINOP_BITWISE_XOR:
d2e4a39e 4196 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4197
4198 case BINOP_EQUAL:
4199 case BINOP_NOTEQUAL:
4200 case BINOP_LESS:
4201 case BINOP_GTR:
4202 case BINOP_LEQ:
4203 case BINOP_GEQ:
d2e4a39e 4204 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4205
4206 case BINOP_CONCAT:
ee90b9ab 4207 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4208
4209 case BINOP_EXP:
d2e4a39e 4210 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4211
4212 case UNOP_NEG:
4213 case UNOP_PLUS:
4214 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4215 case UNOP_ABS:
4216 return (!numeric_type_p (type0));
14f9c5c9
AS
4217
4218 }
4219}
4220\f
dda83cd7 4221 /* Renaming */
14f9c5c9 4222
aeb5907d
JB
4223/* NOTES:
4224
4225 1. In the following, we assume that a renaming type's name may
4226 have an ___XD suffix. It would be nice if this went away at some
4227 point.
4228 2. We handle both the (old) purely type-based representation of
4229 renamings and the (new) variable-based encoding. At some point,
4230 it is devoutly to be hoped that the former goes away
4231 (FIXME: hilfinger-2007-07-09).
4232 3. Subprogram renamings are not implemented, although the XRS
4233 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4234
4235/* If SYM encodes a renaming,
4236
4237 <renaming> renames <renamed entity>,
4238
4239 sets *LEN to the length of the renamed entity's name,
4240 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4241 the string describing the subcomponent selected from the renamed
0963b4bd 4242 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4243 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4244 are undefined). Otherwise, returns a value indicating the category
4245 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4246 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4247 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4248 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4249 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4250 may be NULL, in which case they are not assigned.
4251
4252 [Currently, however, GCC does not generate subprogram renamings.] */
4253
4254enum ada_renaming_category
4255ada_parse_renaming (struct symbol *sym,
4256 const char **renamed_entity, int *len,
4257 const char **renaming_expr)
4258{
4259 enum ada_renaming_category kind;
4260 const char *info;
4261 const char *suffix;
4262
4263 if (sym == NULL)
4264 return ADA_NOT_RENAMING;
66d7f48f 4265 switch (sym->aclass ())
14f9c5c9 4266 {
aeb5907d
JB
4267 default:
4268 return ADA_NOT_RENAMING;
aeb5907d
JB
4269 case LOC_LOCAL:
4270 case LOC_STATIC:
4271 case LOC_COMPUTED:
4272 case LOC_OPTIMIZED_OUT:
987012b8 4273 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4274 if (info == NULL)
4275 return ADA_NOT_RENAMING;
4276 switch (info[5])
4277 {
4278 case '_':
4279 kind = ADA_OBJECT_RENAMING;
4280 info += 6;
4281 break;
4282 case 'E':
4283 kind = ADA_EXCEPTION_RENAMING;
4284 info += 7;
4285 break;
4286 case 'P':
4287 kind = ADA_PACKAGE_RENAMING;
4288 info += 7;
4289 break;
4290 case 'S':
4291 kind = ADA_SUBPROGRAM_RENAMING;
4292 info += 7;
4293 break;
4294 default:
4295 return ADA_NOT_RENAMING;
4296 }
14f9c5c9 4297 }
4c4b4cd2 4298
de93309a
SM
4299 if (renamed_entity != NULL)
4300 *renamed_entity = info;
4301 suffix = strstr (info, "___XE");
4302 if (suffix == NULL || suffix == info)
4303 return ADA_NOT_RENAMING;
4304 if (len != NULL)
4305 *len = strlen (info) - strlen (suffix);
4306 suffix += 5;
4307 if (renaming_expr != NULL)
4308 *renaming_expr = suffix;
4309 return kind;
4310}
4311
4312/* Compute the value of the given RENAMING_SYM, which is expected to
4313 be a symbol encoding a renaming expression. BLOCK is the block
4314 used to evaluate the renaming. */
4315
4316static struct value *
4317ada_read_renaming_var_value (struct symbol *renaming_sym,
4318 const struct block *block)
4319{
4320 const char *sym_name;
4321
987012b8 4322 sym_name = renaming_sym->linkage_name ();
de93309a
SM
4323 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
4324 return evaluate_expression (expr.get ());
4325}
4326\f
4327
dda83cd7 4328 /* Evaluation: Function Calls */
de93309a
SM
4329
4330/* Return an lvalue containing the value VAL. This is the identity on
4331 lvalues, and otherwise has the side-effect of allocating memory
4332 in the inferior where a copy of the value contents is copied. */
4333
4334static struct value *
4335ensure_lval (struct value *val)
4336{
4337 if (VALUE_LVAL (val) == not_lval
4338 || VALUE_LVAL (val) == lval_internalvar)
4339 {
4340 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
4341 const CORE_ADDR addr =
dda83cd7 4342 value_as_long (value_allocate_space_in_inferior (len));
de93309a
SM
4343
4344 VALUE_LVAL (val) = lval_memory;
4345 set_value_address (val, addr);
50888e42 4346 write_memory (addr, value_contents (val).data (), len);
de93309a
SM
4347 }
4348
4349 return val;
4350}
4351
4352/* Given ARG, a value of type (pointer or reference to a)*
4353 structure/union, extract the component named NAME from the ultimate
4354 target structure/union and return it as a value with its
4355 appropriate type.
4356
4357 The routine searches for NAME among all members of the structure itself
4358 and (recursively) among all members of any wrapper members
4359 (e.g., '_parent').
4360
4361 If NO_ERR, then simply return NULL in case of error, rather than
4362 calling error. */
4363
4364static struct value *
4365ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4366{
4367 struct type *t, *t1;
4368 struct value *v;
4369 int check_tag;
4370
4371 v = NULL;
4372 t1 = t = ada_check_typedef (value_type (arg));
78134374 4373 if (t->code () == TYPE_CODE_REF)
de93309a
SM
4374 {
4375 t1 = TYPE_TARGET_TYPE (t);
4376 if (t1 == NULL)
4377 goto BadValue;
4378 t1 = ada_check_typedef (t1);
78134374 4379 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4380 {
4381 arg = coerce_ref (arg);
4382 t = t1;
4383 }
de93309a
SM
4384 }
4385
78134374 4386 while (t->code () == TYPE_CODE_PTR)
de93309a
SM
4387 {
4388 t1 = TYPE_TARGET_TYPE (t);
4389 if (t1 == NULL)
4390 goto BadValue;
4391 t1 = ada_check_typedef (t1);
78134374 4392 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4393 {
4394 arg = value_ind (arg);
4395 t = t1;
4396 }
de93309a 4397 else
dda83cd7 4398 break;
de93309a 4399 }
aeb5907d 4400
78134374 4401 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4402 goto BadValue;
52ce6436 4403
de93309a
SM
4404 if (t1 == t)
4405 v = ada_search_struct_field (name, arg, 0, t);
4406 else
4407 {
4408 int bit_offset, bit_size, byte_offset;
4409 struct type *field_type;
4410 CORE_ADDR address;
a5ee536b 4411
78134374 4412 if (t->code () == TYPE_CODE_PTR)
de93309a
SM
4413 address = value_address (ada_value_ind (arg));
4414 else
4415 address = value_address (ada_coerce_ref (arg));
d2e4a39e 4416
de93309a 4417 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4418 the case where the type is a reference to a tagged type, but
4419 we have to be careful to exclude pointers to tagged types.
4420 The latter should be shown as usual (as a pointer), whereas
4421 a reference should mostly be transparent to the user. */
14f9c5c9 4422
de93309a 4423 if (ada_is_tagged_type (t1, 0)
dda83cd7
SM
4424 || (t1->code () == TYPE_CODE_REF
4425 && ada_is_tagged_type (TYPE_TARGET_TYPE (t1), 0)))
4426 {
4427 /* We first try to find the searched field in the current type.
de93309a 4428 If not found then let's look in the fixed type. */
14f9c5c9 4429
dda83cd7 4430 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4431 nullptr, nullptr, nullptr,
4432 nullptr, nullptr))
de93309a
SM
4433 check_tag = 1;
4434 else
4435 check_tag = 0;
dda83cd7 4436 }
de93309a
SM
4437 else
4438 check_tag = 0;
c3e5cd34 4439
de93309a
SM
4440 /* Convert to fixed type in all cases, so that we have proper
4441 offsets to each field in unconstrained record types. */
4442 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4443 address, NULL, check_tag);
4444
24aa1b02
TT
4445 /* Resolve the dynamic type as well. */
4446 arg = value_from_contents_and_address (t1, nullptr, address);
4447 t1 = value_type (arg);
4448
de93309a 4449 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4450 &field_type, &byte_offset, &bit_offset,
4451 &bit_size, NULL))
4452 {
4453 if (bit_size != 0)
4454 {
4455 if (t->code () == TYPE_CODE_REF)
4456 arg = ada_coerce_ref (arg);
4457 else
4458 arg = ada_value_ind (arg);
4459 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4460 bit_offset, bit_size,
4461 field_type);
4462 }
4463 else
4464 v = value_at_lazy (field_type, address + byte_offset);
4465 }
c3e5cd34 4466 }
14f9c5c9 4467
de93309a
SM
4468 if (v != NULL || no_err)
4469 return v;
4470 else
4471 error (_("There is no member named %s."), name);
4472
4473 BadValue:
4474 if (no_err)
4475 return NULL;
4476 else
4477 error (_("Attempt to extract a component of "
4478 "a value that is not a record."));
14f9c5c9
AS
4479}
4480
4481/* Return the value ACTUAL, converted to be an appropriate value for a
4482 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4483 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4484 values not residing in memory, updating it as needed. */
14f9c5c9 4485
a93c0eb6 4486struct value *
40bc484c 4487ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4488{
df407dfe 4489 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 4490 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4491 struct type *formal_target =
78134374 4492 formal_type->code () == TYPE_CODE_PTR
61ee279c 4493 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e 4494 struct type *actual_target =
78134374 4495 actual_type->code () == TYPE_CODE_PTR
61ee279c 4496 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 4497
4c4b4cd2 4498 if (ada_is_array_descriptor_type (formal_target)
78134374 4499 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4500 return make_array_descriptor (formal_type, actual);
78134374
SM
4501 else if (formal_type->code () == TYPE_CODE_PTR
4502 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4503 {
a84a8a0d 4504 struct value *result;
5b4ee69b 4505
78134374 4506 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4507 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4508 result = desc_data (actual);
78134374 4509 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7
SM
4510 {
4511 if (VALUE_LVAL (actual) != lval_memory)
4512 {
4513 struct value *val;
4514
4515 actual_type = ada_check_typedef (value_type (actual));
4516 val = allocate_value (actual_type);
4bce7cda 4517 copy (value_contents (actual), value_contents_raw (val));
dda83cd7
SM
4518 actual = ensure_lval (val);
4519 }
4520 result = value_addr (actual);
4521 }
a84a8a0d
JB
4522 else
4523 return actual;
b1af9e97 4524 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4525 }
78134374 4526 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4527 return ada_value_ind (actual);
8344af1e
JB
4528 else if (ada_is_aligner_type (formal_type))
4529 {
4530 /* We need to turn this parameter into an aligner type
4531 as well. */
4532 struct value *aligner = allocate_value (formal_type);
4533 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4534
4535 value_assign_to_component (aligner, component, actual);
4536 return aligner;
4537 }
14f9c5c9
AS
4538
4539 return actual;
4540}
4541
438c98a1
JB
4542/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4543 type TYPE. This is usually an inefficient no-op except on some targets
4544 (such as AVR) where the representation of a pointer and an address
4545 differs. */
4546
4547static CORE_ADDR
4548value_pointer (struct value *value, struct type *type)
4549{
438c98a1 4550 unsigned len = TYPE_LENGTH (type);
224c3ddb 4551 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4552 CORE_ADDR addr;
4553
4554 addr = value_address (value);
8ee511af 4555 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4556 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4557 return addr;
4558}
4559
14f9c5c9 4560
4c4b4cd2
PH
4561/* Push a descriptor of type TYPE for array value ARR on the stack at
4562 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4563 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4564 to-descriptor type rather than a descriptor type), a struct value *
4565 representing a pointer to this descriptor. */
14f9c5c9 4566
d2e4a39e 4567static struct value *
40bc484c 4568make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4569{
d2e4a39e
AS
4570 struct type *bounds_type = desc_bounds_type (type);
4571 struct type *desc_type = desc_base_type (type);
4572 struct value *descriptor = allocate_value (desc_type);
4573 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4574 int i;
d2e4a39e 4575
0963b4bd
MS
4576 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4577 i > 0; i -= 1)
14f9c5c9 4578 {
50888e42
SM
4579 modify_field (value_type (bounds),
4580 value_contents_writeable (bounds).data (),
19f220c3
JK
4581 ada_array_bound (arr, i, 0),
4582 desc_bound_bitpos (bounds_type, i, 0),
4583 desc_bound_bitsize (bounds_type, i, 0));
50888e42
SM
4584 modify_field (value_type (bounds),
4585 value_contents_writeable (bounds).data (),
19f220c3
JK
4586 ada_array_bound (arr, i, 1),
4587 desc_bound_bitpos (bounds_type, i, 1),
4588 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4589 }
d2e4a39e 4590
40bc484c 4591 bounds = ensure_lval (bounds);
d2e4a39e 4592
19f220c3 4593 modify_field (value_type (descriptor),
50888e42 4594 value_contents_writeable (descriptor).data (),
19f220c3 4595 value_pointer (ensure_lval (arr),
940da03e 4596 desc_type->field (0).type ()),
19f220c3
JK
4597 fat_pntr_data_bitpos (desc_type),
4598 fat_pntr_data_bitsize (desc_type));
4599
4600 modify_field (value_type (descriptor),
50888e42 4601 value_contents_writeable (descriptor).data (),
19f220c3 4602 value_pointer (bounds,
940da03e 4603 desc_type->field (1).type ()),
19f220c3
JK
4604 fat_pntr_bounds_bitpos (desc_type),
4605 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4606
40bc484c 4607 descriptor = ensure_lval (descriptor);
14f9c5c9 4608
78134374 4609 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4610 return value_addr (descriptor);
4611 else
4612 return descriptor;
4613}
14f9c5c9 4614\f
dda83cd7 4615 /* Symbol Cache Module */
3d9434b5 4616
3d9434b5 4617/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4618 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4619 on the type of entity being printed, the cache can make it as much
4620 as an order of magnitude faster than without it.
4621
4622 The descriptive type DWARF extension has significantly reduced
4623 the need for this cache, at least when DWARF is being used. However,
4624 even in this case, some expensive name-based symbol searches are still
4625 sometimes necessary - to find an XVZ variable, mostly. */
4626
ee01b665
JB
4627/* Return the symbol cache associated to the given program space PSPACE.
4628 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4629
ee01b665
JB
4630static struct ada_symbol_cache *
4631ada_get_symbol_cache (struct program_space *pspace)
4632{
4633 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4634
bdcccc56
TT
4635 if (pspace_data->sym_cache == nullptr)
4636 pspace_data->sym_cache.reset (new ada_symbol_cache);
ee01b665 4637
bdcccc56 4638 return pspace_data->sym_cache.get ();
ee01b665 4639}
3d9434b5
JB
4640
4641/* Clear all entries from the symbol cache. */
4642
4643static void
bdcccc56 4644ada_clear_symbol_cache ()
3d9434b5 4645{
bdcccc56
TT
4646 struct ada_pspace_data *pspace_data
4647 = get_ada_pspace_data (current_program_space);
ee01b665 4648
bdcccc56
TT
4649 if (pspace_data->sym_cache != nullptr)
4650 pspace_data->sym_cache.reset ();
3d9434b5
JB
4651}
4652
fe978cb0 4653/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4654 Return it if found, or NULL otherwise. */
4655
4656static struct cache_entry **
fe978cb0 4657find_entry (const char *name, domain_enum domain)
3d9434b5 4658{
ee01b665
JB
4659 struct ada_symbol_cache *sym_cache
4660 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4661 int h = msymbol_hash (name) % HASH_SIZE;
4662 struct cache_entry **e;
4663
ee01b665 4664 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4665 {
fe978cb0 4666 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
dda83cd7 4667 return e;
3d9434b5
JB
4668 }
4669 return NULL;
4670}
4671
fe978cb0 4672/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4673 Return 1 if found, 0 otherwise.
4674
4675 If an entry was found and SYM is not NULL, set *SYM to the entry's
4676 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4677
96d887e8 4678static int
fe978cb0 4679lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4680 struct symbol **sym, const struct block **block)
96d887e8 4681{
fe978cb0 4682 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4683
4684 if (e == NULL)
4685 return 0;
4686 if (sym != NULL)
4687 *sym = (*e)->sym;
4688 if (block != NULL)
4689 *block = (*e)->block;
4690 return 1;
96d887e8
PH
4691}
4692
3d9434b5 4693/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4694 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4695
96d887e8 4696static void
fe978cb0 4697cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4698 const struct block *block)
96d887e8 4699{
ee01b665
JB
4700 struct ada_symbol_cache *sym_cache
4701 = ada_get_symbol_cache (current_program_space);
3d9434b5 4702 int h;
3d9434b5
JB
4703 struct cache_entry *e;
4704
1994afbf
DE
4705 /* Symbols for builtin types don't have a block.
4706 For now don't cache such symbols. */
7b3ecc75 4707 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4708 return;
4709
3d9434b5
JB
4710 /* If the symbol is a local symbol, then do not cache it, as a search
4711 for that symbol depends on the context. To determine whether
4712 the symbol is local or not, we check the block where we found it
4713 against the global and static blocks of its associated symtab. */
63d609de
SM
4714 if (sym != nullptr)
4715 {
4716 const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
4717
4718 if (bv.global_block () != block && bv.static_block () != block)
4719 return;
4720 }
3d9434b5
JB
4721
4722 h = msymbol_hash (name) % HASH_SIZE;
e39db4db 4723 e = XOBNEW (&sym_cache->cache_space, cache_entry);
ee01b665
JB
4724 e->next = sym_cache->root[h];
4725 sym_cache->root[h] = e;
2ef5453b 4726 e->name = obstack_strdup (&sym_cache->cache_space, name);
3d9434b5 4727 e->sym = sym;
fe978cb0 4728 e->domain = domain;
3d9434b5 4729 e->block = block;
96d887e8 4730}
4c4b4cd2 4731\f
dda83cd7 4732 /* Symbol Lookup */
4c4b4cd2 4733
b5ec771e
PA
4734/* Return the symbol name match type that should be used used when
4735 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4736
4737 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4738 for Ada lookups. */
c0431670 4739
b5ec771e
PA
4740static symbol_name_match_type
4741name_match_type_from_name (const char *lookup_name)
c0431670 4742{
b5ec771e
PA
4743 return (strstr (lookup_name, "__") == NULL
4744 ? symbol_name_match_type::WILD
4745 : symbol_name_match_type::FULL);
c0431670
JB
4746}
4747
4c4b4cd2
PH
4748/* Return the result of a standard (literal, C-like) lookup of NAME in
4749 given DOMAIN, visible from lexical block BLOCK. */
4750
4751static struct symbol *
4752standard_lookup (const char *name, const struct block *block,
dda83cd7 4753 domain_enum domain)
4c4b4cd2 4754{
acbd605d 4755 /* Initialize it just to avoid a GCC false warning. */
6640a367 4756 struct block_symbol sym = {};
4c4b4cd2 4757
d12307c1
PMR
4758 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4759 return sym.symbol;
a2cd4f14 4760 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4761 cache_symbol (name, domain, sym.symbol, sym.block);
4762 return sym.symbol;
4c4b4cd2
PH
4763}
4764
4765
4766/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4767 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4768 since they contend in overloading in the same way. */
4769static int
d1183b06 4770is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4771{
d1183b06 4772 for (const block_symbol &sym : syms)
5f9c5a63
SM
4773 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4774 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4775 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4776 return 1;
4777
4778 return 0;
4779}
4780
4781/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4782 struct types. Otherwise, they may not. */
14f9c5c9
AS
4783
4784static int
d2e4a39e 4785equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4786{
d2e4a39e 4787 if (type0 == type1)
14f9c5c9 4788 return 1;
d2e4a39e 4789 if (type0 == NULL || type1 == NULL
78134374 4790 || type0->code () != type1->code ())
14f9c5c9 4791 return 0;
78134374
SM
4792 if ((type0->code () == TYPE_CODE_STRUCT
4793 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4794 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4795 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4796 return 1;
d2e4a39e 4797
14f9c5c9
AS
4798 return 0;
4799}
4800
4801/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4802 no more defined than that of SYM1. */
14f9c5c9
AS
4803
4804static int
d2e4a39e 4805lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4806{
4807 if (sym0 == sym1)
4808 return 1;
6c9c307c 4809 if (sym0->domain () != sym1->domain ()
66d7f48f 4810 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4811 return 0;
4812
66d7f48f 4813 switch (sym0->aclass ())
14f9c5c9
AS
4814 {
4815 case LOC_UNDEF:
4816 return 1;
4817 case LOC_TYPEDEF:
4818 {
5f9c5a63
SM
4819 struct type *type0 = sym0->type ();
4820 struct type *type1 = sym1->type ();
dda83cd7
SM
4821 const char *name0 = sym0->linkage_name ();
4822 const char *name1 = sym1->linkage_name ();
4823 int len0 = strlen (name0);
4824
4825 return
4826 type0->code () == type1->code ()
4827 && (equiv_types (type0, type1)
4828 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4829 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4830 }
4831 case LOC_CONST:
4aeddc50 4832 return sym0->value_longest () == sym1->value_longest ()
5f9c5a63 4833 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4834
4835 case LOC_STATIC:
4836 {
dda83cd7
SM
4837 const char *name0 = sym0->linkage_name ();
4838 const char *name1 = sym1->linkage_name ();
4839 return (strcmp (name0, name1) == 0
4aeddc50 4840 && sym0->value_address () == sym1->value_address ());
4b610737
TT
4841 }
4842
d2e4a39e
AS
4843 default:
4844 return 0;
14f9c5c9
AS
4845 }
4846}
4847
d1183b06
TT
4848/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4849 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4850
4851static void
d1183b06 4852add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4853 struct symbol *sym,
4854 const struct block *block)
14f9c5c9 4855{
529cad9c
PH
4856 /* Do not try to complete stub types, as the debugger is probably
4857 already scanning all symbols matching a certain name at the
4858 time when this function is called. Trying to replace the stub
4859 type by its associated full type will cause us to restart a scan
4860 which may lead to an infinite recursion. Instead, the client
4861 collecting the matching symbols will end up collecting several
4862 matches, with at least one of them complete. It can then filter
4863 out the stub ones if needed. */
4864
d1183b06 4865 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4866 {
d1183b06 4867 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4868 return;
d1183b06 4869 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4870 {
d1183b06
TT
4871 result[i].symbol = sym;
4872 result[i].block = block;
dda83cd7
SM
4873 return;
4874 }
4c4b4cd2
PH
4875 }
4876
d1183b06
TT
4877 struct block_symbol info;
4878 info.symbol = sym;
4879 info.block = block;
4880 result.push_back (info);
4c4b4cd2
PH
4881}
4882
7c7b6655
TT
4883/* Return a bound minimal symbol matching NAME according to Ada
4884 decoding rules. Returns an invalid symbol if there is no such
4885 minimal symbol. Names prefixed with "standard__" are handled
4886 specially: "standard__" is first stripped off, and only static and
4887 global symbols are searched. */
4c4b4cd2 4888
7c7b6655 4889struct bound_minimal_symbol
96d887e8 4890ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4891{
7c7b6655 4892 struct bound_minimal_symbol result;
4c4b4cd2 4893
b5ec771e
PA
4894 symbol_name_match_type match_type = name_match_type_from_name (name);
4895 lookup_name_info lookup_name (name, match_type);
4896
4897 symbol_name_matcher_ftype *match_name
4898 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4899
2030c079 4900 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4901 {
7932255d 4902 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf 4903 {
c9d95fa3 4904 if (match_name (msymbol->linkage_name (), lookup_name, NULL)
60f62e2b 4905 && msymbol->type () != mst_solib_trampoline)
5325b9bf
TT
4906 {
4907 result.minsym = msymbol;
4908 result.objfile = objfile;
4909 break;
4910 }
4911 }
4912 }
4c4b4cd2 4913
7c7b6655 4914 return result;
96d887e8 4915}
4c4b4cd2 4916
96d887e8
PH
4917/* True if TYPE is definitely an artificial type supplied to a symbol
4918 for which no debugging information was given in the symbol file. */
14f9c5c9 4919
96d887e8
PH
4920static int
4921is_nondebugging_type (struct type *type)
4922{
0d5cff50 4923 const char *name = ada_type_name (type);
5b4ee69b 4924
96d887e8
PH
4925 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4926}
4c4b4cd2 4927
8f17729f
JB
4928/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4929 that are deemed "identical" for practical purposes.
4930
4931 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4932 types and that their number of enumerals is identical (in other
1f704f76 4933 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4934
4935static int
4936ada_identical_enum_types_p (struct type *type1, struct type *type2)
4937{
4938 int i;
4939
4940 /* The heuristic we use here is fairly conservative. We consider
4941 that 2 enumerate types are identical if they have the same
4942 number of enumerals and that all enumerals have the same
4943 underlying value and name. */
4944
4945 /* All enums in the type should have an identical underlying value. */
1f704f76 4946 for (i = 0; i < type1->num_fields (); i++)
970db518 4947 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4948 return 0;
4949
4950 /* All enumerals should also have the same name (modulo any numerical
4951 suffix). */
1f704f76 4952 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4953 {
33d16dd9
SM
4954 const char *name_1 = type1->field (i).name ();
4955 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4956 int len_1 = strlen (name_1);
4957 int len_2 = strlen (name_2);
4958
33d16dd9
SM
4959 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4960 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4961 if (len_1 != len_2
33d16dd9
SM
4962 || strncmp (type1->field (i).name (),
4963 type2->field (i).name (),
8f17729f
JB
4964 len_1) != 0)
4965 return 0;
4966 }
4967
4968 return 1;
4969}
4970
4971/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4972 that are deemed "identical" for practical purposes. Sometimes,
4973 enumerals are not strictly identical, but their types are so similar
4974 that they can be considered identical.
4975
4976 For instance, consider the following code:
4977
4978 type Color is (Black, Red, Green, Blue, White);
4979 type RGB_Color is new Color range Red .. Blue;
4980
4981 Type RGB_Color is a subrange of an implicit type which is a copy
4982 of type Color. If we call that implicit type RGB_ColorB ("B" is
4983 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4984 As a result, when an expression references any of the enumeral
4985 by name (Eg. "print green"), the expression is technically
4986 ambiguous and the user should be asked to disambiguate. But
4987 doing so would only hinder the user, since it wouldn't matter
4988 what choice he makes, the outcome would always be the same.
4989 So, for practical purposes, we consider them as the same. */
4990
4991static int
54d343a2 4992symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4993{
4994 int i;
4995
4996 /* Before performing a thorough comparison check of each type,
4997 we perform a series of inexpensive checks. We expect that these
4998 checks will quickly fail in the vast majority of cases, and thus
4999 help prevent the unnecessary use of a more expensive comparison.
5000 Said comparison also expects us to make some of these checks
5001 (see ada_identical_enum_types_p). */
5002
5003 /* Quick check: All symbols should have an enum type. */
54d343a2 5004 for (i = 0; i < syms.size (); i++)
5f9c5a63 5005 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
5006 return 0;
5007
5008 /* Quick check: They should all have the same value. */
54d343a2 5009 for (i = 1; i < syms.size (); i++)
4aeddc50 5010 if (syms[i].symbol->value_longest () != syms[0].symbol->value_longest ())
8f17729f
JB
5011 return 0;
5012
5013 /* Quick check: They should all have the same number of enumerals. */
54d343a2 5014 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5015 if (syms[i].symbol->type ()->num_fields ()
5016 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
5017 return 0;
5018
5019 /* All the sanity checks passed, so we might have a set of
5020 identical enumeration types. Perform a more complete
5021 comparison of the type of each symbol. */
54d343a2 5022 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5023 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5024 syms[0].symbol->type ()))
8f17729f
JB
5025 return 0;
5026
5027 return 1;
5028}
5029
54d343a2 5030/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5031 duplicate other symbols in the list (The only case I know of where
5032 this happens is when object files containing stabs-in-ecoff are
5033 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5034 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5035
d1183b06 5036static void
54d343a2 5037remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
5038{
5039 int i, j;
4c4b4cd2 5040
8f17729f
JB
5041 /* We should never be called with less than 2 symbols, as there
5042 cannot be any extra symbol in that case. But it's easy to
5043 handle, since we have nothing to do in that case. */
54d343a2 5044 if (syms->size () < 2)
d1183b06 5045 return;
8f17729f 5046
96d887e8 5047 i = 0;
54d343a2 5048 while (i < syms->size ())
96d887e8 5049 {
a35ddb44 5050 int remove_p = 0;
339c13b6
JB
5051
5052 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5053 the get rid of the stub. */
339c13b6 5054
5f9c5a63 5055 if ((*syms)[i].symbol->type ()->is_stub ()
dda83cd7
SM
5056 && (*syms)[i].symbol->linkage_name () != NULL)
5057 {
5058 for (j = 0; j < syms->size (); j++)
5059 {
5060 if (j != i
5f9c5a63 5061 && !(*syms)[j].symbol->type ()->is_stub ()
dda83cd7
SM
5062 && (*syms)[j].symbol->linkage_name () != NULL
5063 && strcmp ((*syms)[i].symbol->linkage_name (),
5064 (*syms)[j].symbol->linkage_name ()) == 0)
5065 remove_p = 1;
5066 }
5067 }
339c13b6
JB
5068
5069 /* Two symbols with the same name, same class and same address
dda83cd7 5070 should be identical. */
339c13b6 5071
987012b8 5072 else if ((*syms)[i].symbol->linkage_name () != NULL
66d7f48f 5073 && (*syms)[i].symbol->aclass () == LOC_STATIC
5f9c5a63 5074 && is_nondebugging_type ((*syms)[i].symbol->type ()))
dda83cd7
SM
5075 {
5076 for (j = 0; j < syms->size (); j += 1)
5077 {
5078 if (i != j
5079 && (*syms)[j].symbol->linkage_name () != NULL
5080 && strcmp ((*syms)[i].symbol->linkage_name (),
5081 (*syms)[j].symbol->linkage_name ()) == 0
66d7f48f
SM
5082 && ((*syms)[i].symbol->aclass ()
5083 == (*syms)[j].symbol->aclass ())
4aeddc50
SM
5084 && (*syms)[i].symbol->value_address ()
5085 == (*syms)[j].symbol->value_address ())
dda83cd7
SM
5086 remove_p = 1;
5087 }
5088 }
339c13b6 5089
a35ddb44 5090 if (remove_p)
54d343a2 5091 syms->erase (syms->begin () + i);
1b788fb6
TT
5092 else
5093 i += 1;
14f9c5c9 5094 }
8f17729f
JB
5095
5096 /* If all the remaining symbols are identical enumerals, then
5097 just keep the first one and discard the rest.
5098
5099 Unlike what we did previously, we do not discard any entry
5100 unless they are ALL identical. This is because the symbol
5101 comparison is not a strict comparison, but rather a practical
5102 comparison. If all symbols are considered identical, then
5103 we can just go ahead and use the first one and discard the rest.
5104 But if we cannot reduce the list to a single element, we have
5105 to ask the user to disambiguate anyways. And if we have to
5106 present a multiple-choice menu, it's less confusing if the list
5107 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
5108 if (symbols_are_identical_enums (*syms))
5109 syms->resize (1);
14f9c5c9
AS
5110}
5111
96d887e8
PH
5112/* Given a type that corresponds to a renaming entity, use the type name
5113 to extract the scope (package name or function name, fully qualified,
5114 and following the GNAT encoding convention) where this renaming has been
49d83361 5115 defined. */
4c4b4cd2 5116
49d83361 5117static std::string
96d887e8 5118xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5119{
96d887e8 5120 /* The renaming types adhere to the following convention:
0963b4bd 5121 <scope>__<rename>___<XR extension>.
96d887e8
PH
5122 So, to extract the scope, we search for the "___XR" extension,
5123 and then backtrack until we find the first "__". */
76a01679 5124
7d93a1e0 5125 const char *name = renaming_type->name ();
108d56a4
SM
5126 const char *suffix = strstr (name, "___XR");
5127 const char *last;
14f9c5c9 5128
96d887e8
PH
5129 /* Now, backtrack a bit until we find the first "__". Start looking
5130 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5131
96d887e8
PH
5132 for (last = suffix - 3; last > name; last--)
5133 if (last[0] == '_' && last[1] == '_')
5134 break;
76a01679 5135
96d887e8 5136 /* Make a copy of scope and return it. */
49d83361 5137 return std::string (name, last);
4c4b4cd2
PH
5138}
5139
96d887e8 5140/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5141
96d887e8
PH
5142static int
5143is_package_name (const char *name)
4c4b4cd2 5144{
96d887e8
PH
5145 /* Here, We take advantage of the fact that no symbols are generated
5146 for packages, while symbols are generated for each function.
5147 So the condition for NAME represent a package becomes equivalent
5148 to NAME not existing in our list of symbols. There is only one
5149 small complication with library-level functions (see below). */
4c4b4cd2 5150
96d887e8
PH
5151 /* If it is a function that has not been defined at library level,
5152 then we should be able to look it up in the symbols. */
5153 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5154 return 0;
14f9c5c9 5155
96d887e8
PH
5156 /* Library-level function names start with "_ada_". See if function
5157 "_ada_" followed by NAME can be found. */
14f9c5c9 5158
96d887e8 5159 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5160 functions names cannot contain "__" in them. */
96d887e8
PH
5161 if (strstr (name, "__") != NULL)
5162 return 0;
4c4b4cd2 5163
528e1572 5164 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5165
528e1572 5166 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5167}
14f9c5c9 5168
96d887e8 5169/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5170 not visible from FUNCTION_NAME. */
14f9c5c9 5171
96d887e8 5172static int
0d5cff50 5173old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5174{
66d7f48f 5175 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5176 return 0;
5177
5f9c5a63 5178 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5179
96d887e8 5180 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5181 if (is_package_name (scope.c_str ()))
5182 return 0;
14f9c5c9 5183
96d887e8
PH
5184 /* Check that the rename is in the current function scope by checking
5185 that its name starts with SCOPE. */
76a01679 5186
96d887e8
PH
5187 /* If the function name starts with "_ada_", it means that it is
5188 a library-level function. Strip this prefix before doing the
5189 comparison, as the encoding for the renaming does not contain
5190 this prefix. */
61012eef 5191 if (startswith (function_name, "_ada_"))
96d887e8 5192 function_name += 5;
f26caa11 5193
49d83361 5194 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5195}
5196
aeb5907d
JB
5197/* Remove entries from SYMS that corresponds to a renaming entity that
5198 is not visible from the function associated with CURRENT_BLOCK or
5199 that is superfluous due to the presence of more specific renaming
5200 information. Places surviving symbols in the initial entries of
d1183b06
TT
5201 SYMS.
5202
96d887e8 5203 Rationale:
aeb5907d
JB
5204 First, in cases where an object renaming is implemented as a
5205 reference variable, GNAT may produce both the actual reference
5206 variable and the renaming encoding. In this case, we discard the
5207 latter.
5208
5209 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5210 entity. Unfortunately, STABS currently does not support the definition
5211 of types that are local to a given lexical block, so all renamings types
5212 are emitted at library level. As a consequence, if an application
5213 contains two renaming entities using the same name, and a user tries to
5214 print the value of one of these entities, the result of the ada symbol
5215 lookup will also contain the wrong renaming type.
f26caa11 5216
96d887e8
PH
5217 This function partially covers for this limitation by attempting to
5218 remove from the SYMS list renaming symbols that should be visible
5219 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5220 method with the current information available. The implementation
5221 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5222
5223 - When the user tries to print a rename in a function while there
dda83cd7
SM
5224 is another rename entity defined in a package: Normally, the
5225 rename in the function has precedence over the rename in the
5226 package, so the latter should be removed from the list. This is
5227 currently not the case.
5228
96d887e8 5229 - This function will incorrectly remove valid renames if
dda83cd7
SM
5230 the CURRENT_BLOCK corresponds to a function which symbol name
5231 has been changed by an "Export" pragma. As a consequence,
5232 the user will be unable to print such rename entities. */
4c4b4cd2 5233
d1183b06 5234static void
54d343a2
TT
5235remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5236 const struct block *current_block)
4c4b4cd2
PH
5237{
5238 struct symbol *current_function;
0d5cff50 5239 const char *current_function_name;
4c4b4cd2 5240 int i;
aeb5907d
JB
5241 int is_new_style_renaming;
5242
5243 /* If there is both a renaming foo___XR... encoded as a variable and
5244 a simple variable foo in the same block, discard the latter.
0963b4bd 5245 First, zero out such symbols, then compress. */
aeb5907d 5246 is_new_style_renaming = 0;
54d343a2 5247 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5248 {
54d343a2
TT
5249 struct symbol *sym = (*syms)[i].symbol;
5250 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5251 const char *name;
5252 const char *suffix;
5253
66d7f48f 5254 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5255 continue;
987012b8 5256 name = sym->linkage_name ();
aeb5907d
JB
5257 suffix = strstr (name, "___XR");
5258
5259 if (suffix != NULL)
5260 {
5261 int name_len = suffix - name;
5262 int j;
5b4ee69b 5263
aeb5907d 5264 is_new_style_renaming = 1;
54d343a2
TT
5265 for (j = 0; j < syms->size (); j += 1)
5266 if (i != j && (*syms)[j].symbol != NULL
987012b8 5267 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5268 name_len) == 0
54d343a2
TT
5269 && block == (*syms)[j].block)
5270 (*syms)[j].symbol = NULL;
aeb5907d
JB
5271 }
5272 }
5273 if (is_new_style_renaming)
5274 {
5275 int j, k;
5276
54d343a2
TT
5277 for (j = k = 0; j < syms->size (); j += 1)
5278 if ((*syms)[j].symbol != NULL)
aeb5907d 5279 {
54d343a2 5280 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5281 k += 1;
5282 }
d1183b06
TT
5283 syms->resize (k);
5284 return;
aeb5907d 5285 }
4c4b4cd2
PH
5286
5287 /* Extract the function name associated to CURRENT_BLOCK.
5288 Abort if unable to do so. */
76a01679 5289
4c4b4cd2 5290 if (current_block == NULL)
d1183b06 5291 return;
76a01679 5292
7f0df278 5293 current_function = block_linkage_function (current_block);
4c4b4cd2 5294 if (current_function == NULL)
d1183b06 5295 return;
4c4b4cd2 5296
987012b8 5297 current_function_name = current_function->linkage_name ();
4c4b4cd2 5298 if (current_function_name == NULL)
d1183b06 5299 return;
4c4b4cd2
PH
5300
5301 /* Check each of the symbols, and remove it from the list if it is
5302 a type corresponding to a renaming that is out of the scope of
5303 the current block. */
5304
5305 i = 0;
54d343a2 5306 while (i < syms->size ())
4c4b4cd2 5307 {
54d343a2 5308 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5309 == ADA_OBJECT_RENAMING
5310 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5311 current_function_name))
5312 syms->erase (syms->begin () + i);
4c4b4cd2 5313 else
dda83cd7 5314 i += 1;
4c4b4cd2 5315 }
4c4b4cd2
PH
5316}
5317
d1183b06 5318/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5319 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5320
cd458349 5321 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5322
5323static void
d1183b06 5324ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5325 const lookup_name_info &lookup_name,
5326 const struct block *block, domain_enum domain)
339c13b6 5327{
339c13b6
JB
5328 while (block != NULL)
5329 {
d1183b06 5330 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5331
ba8694b6
TT
5332 /* If we found a non-function match, assume that's the one. We
5333 only check this when finding a function boundary, so that we
5334 can accumulate all results from intervening blocks first. */
6c00f721 5335 if (block->function () != nullptr && is_nonfunction (result))
dda83cd7 5336 return;
339c13b6 5337
f135fe72 5338 block = block->superblock ();
339c13b6 5339 }
339c13b6
JB
5340}
5341
2315bb2d 5342/* An object of this type is used as the callback argument when
40658b94 5343 calling the map_matching_symbols method. */
ccefe4c4 5344
40658b94 5345struct match_data
ccefe4c4 5346{
1bfa81ac
TT
5347 explicit match_data (std::vector<struct block_symbol> *rp)
5348 : resultp (rp)
5349 {
5350 }
5351 DISABLE_COPY_AND_ASSIGN (match_data);
5352
2315bb2d
TT
5353 bool operator() (struct block_symbol *bsym);
5354
1bfa81ac 5355 struct objfile *objfile = nullptr;
d1183b06 5356 std::vector<struct block_symbol> *resultp;
1bfa81ac 5357 struct symbol *arg_sym = nullptr;
1178743e 5358 bool found_sym = false;
ccefe4c4
TT
5359};
5360
2315bb2d
TT
5361/* A callback for add_nonlocal_symbols that adds symbol, found in
5362 BSYM, to a list of symbols. */
ccefe4c4 5363
2315bb2d
TT
5364bool
5365match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5366{
199b4314
TT
5367 const struct block *block = bsym->block;
5368 struct symbol *sym = bsym->symbol;
5369
40658b94
PH
5370 if (sym == NULL)
5371 {
2315bb2d
TT
5372 if (!found_sym && arg_sym != NULL)
5373 add_defn_to_vec (*resultp,
5374 fixup_symbol_section (arg_sym, objfile),
40658b94 5375 block);
2315bb2d
TT
5376 found_sym = false;
5377 arg_sym = NULL;
40658b94
PH
5378 }
5379 else
5380 {
66d7f48f 5381 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5382 return true;
d9743061 5383 else if (sym->is_argument ())
2315bb2d 5384 arg_sym = sym;
40658b94
PH
5385 else
5386 {
2315bb2d
TT
5387 found_sym = true;
5388 add_defn_to_vec (*resultp,
5389 fixup_symbol_section (sym, objfile),
40658b94
PH
5390 block);
5391 }
5392 }
199b4314 5393 return true;
40658b94
PH
5394}
5395
b5ec771e
PA
5396/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5397 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5398 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5399
5400static int
d1183b06 5401ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5402 const struct block *block,
b5ec771e
PA
5403 const lookup_name_info &lookup_name,
5404 domain_enum domain)
22cee43f
PMR
5405{
5406 struct using_direct *renaming;
d1183b06 5407 int defns_mark = result.size ();
22cee43f 5408
b5ec771e
PA
5409 symbol_name_matcher_ftype *name_match
5410 = ada_get_symbol_name_matcher (lookup_name);
5411
22cee43f
PMR
5412 for (renaming = block_using (block);
5413 renaming != NULL;
5414 renaming = renaming->next)
5415 {
5416 const char *r_name;
22cee43f
PMR
5417
5418 /* Avoid infinite recursions: skip this renaming if we are actually
5419 already traversing it.
5420
5421 Currently, symbol lookup in Ada don't use the namespace machinery from
5422 C++/Fortran support: skip namespace imports that use them. */
5423 if (renaming->searched
5424 || (renaming->import_src != NULL
5425 && renaming->import_src[0] != '\0')
5426 || (renaming->import_dest != NULL
5427 && renaming->import_dest[0] != '\0'))
5428 continue;
5429 renaming->searched = 1;
5430
5431 /* TODO: here, we perform another name-based symbol lookup, which can
5432 pull its own multiple overloads. In theory, we should be able to do
5433 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5434 not a simple name. But in order to do this, we would need to enhance
5435 the DWARF reader to associate a symbol to this renaming, instead of a
5436 name. So, for now, we do something simpler: re-use the C++/Fortran
5437 namespace machinery. */
5438 r_name = (renaming->alias != NULL
5439 ? renaming->alias
5440 : renaming->declaration);
b5ec771e
PA
5441 if (name_match (r_name, lookup_name, NULL))
5442 {
5443 lookup_name_info decl_lookup_name (renaming->declaration,
5444 lookup_name.match_type ());
d1183b06 5445 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5446 1, NULL);
5447 }
22cee43f
PMR
5448 renaming->searched = 0;
5449 }
d1183b06 5450 return result.size () != defns_mark;
22cee43f
PMR
5451}
5452
db230ce3
JB
5453/* Implements compare_names, but only applying the comparision using
5454 the given CASING. */
5b4ee69b 5455
40658b94 5456static int
db230ce3
JB
5457compare_names_with_case (const char *string1, const char *string2,
5458 enum case_sensitivity casing)
40658b94
PH
5459{
5460 while (*string1 != '\0' && *string2 != '\0')
5461 {
db230ce3
JB
5462 char c1, c2;
5463
40658b94
PH
5464 if (isspace (*string1) || isspace (*string2))
5465 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5466
5467 if (casing == case_sensitive_off)
5468 {
5469 c1 = tolower (*string1);
5470 c2 = tolower (*string2);
5471 }
5472 else
5473 {
5474 c1 = *string1;
5475 c2 = *string2;
5476 }
5477 if (c1 != c2)
40658b94 5478 break;
db230ce3 5479
40658b94
PH
5480 string1 += 1;
5481 string2 += 1;
5482 }
db230ce3 5483
40658b94
PH
5484 switch (*string1)
5485 {
5486 case '(':
5487 return strcmp_iw_ordered (string1, string2);
5488 case '_':
5489 if (*string2 == '\0')
5490 {
052874e8 5491 if (is_name_suffix (string1))
40658b94
PH
5492 return 0;
5493 else
1a1d5513 5494 return 1;
40658b94 5495 }
dbb8534f 5496 /* FALLTHROUGH */
40658b94
PH
5497 default:
5498 if (*string2 == '(')
5499 return strcmp_iw_ordered (string1, string2);
5500 else
db230ce3
JB
5501 {
5502 if (casing == case_sensitive_off)
5503 return tolower (*string1) - tolower (*string2);
5504 else
5505 return *string1 - *string2;
5506 }
40658b94 5507 }
ccefe4c4
TT
5508}
5509
db230ce3
JB
5510/* Compare STRING1 to STRING2, with results as for strcmp.
5511 Compatible with strcmp_iw_ordered in that...
5512
5513 strcmp_iw_ordered (STRING1, STRING2) <= 0
5514
5515 ... implies...
5516
5517 compare_names (STRING1, STRING2) <= 0
5518
5519 (they may differ as to what symbols compare equal). */
5520
5521static int
5522compare_names (const char *string1, const char *string2)
5523{
5524 int result;
5525
5526 /* Similar to what strcmp_iw_ordered does, we need to perform
5527 a case-insensitive comparison first, and only resort to
5528 a second, case-sensitive, comparison if the first one was
5529 not sufficient to differentiate the two strings. */
5530
5531 result = compare_names_with_case (string1, string2, case_sensitive_off);
5532 if (result == 0)
5533 result = compare_names_with_case (string1, string2, case_sensitive_on);
5534
5535 return result;
5536}
5537
b5ec771e
PA
5538/* Convenience function to get at the Ada encoded lookup name for
5539 LOOKUP_NAME, as a C string. */
5540
5541static const char *
5542ada_lookup_name (const lookup_name_info &lookup_name)
5543{
5544 return lookup_name.ada ().lookup_name ().c_str ();
5545}
5546
0b7b2c2a
TT
5547/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5548 for OBJFILE, then walk the objfile's symtabs and update the
5549 results. */
5550
5551static void
5552map_matching_symbols (struct objfile *objfile,
5553 const lookup_name_info &lookup_name,
5554 bool is_wild_match,
5555 domain_enum domain,
5556 int global,
5557 match_data &data)
5558{
5559 data.objfile = objfile;
5560 objfile->expand_matching_symbols (lookup_name, domain, global,
5561 is_wild_match ? nullptr : compare_names);
5562
5563 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5564 for (compunit_symtab *symtab : objfile->compunits ())
5565 {
5566 const struct block *block
63d609de 5567 = symtab->blockvector ()->block (block_kind);
0b7b2c2a
TT
5568 if (!iterate_over_symbols_terminated (block, lookup_name,
5569 domain, data))
5570 break;
5571 }
5572}
5573
1bfa81ac 5574/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5575 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5576 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5577 symbols otherwise. */
339c13b6
JB
5578
5579static void
d1183b06 5580add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5581 const lookup_name_info &lookup_name,
5582 domain_enum domain, int global)
339c13b6 5583{
1bfa81ac 5584 struct match_data data (&result);
339c13b6 5585
b5ec771e
PA
5586 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5587
2030c079 5588 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5589 {
0b7b2c2a
TT
5590 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5591 global, data);
22cee43f 5592
b669c953 5593 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5594 {
5595 const struct block *global_block
63d609de 5596 = cu->blockvector ()->global_block ();
22cee43f 5597
d1183b06 5598 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5599 domain))
1178743e 5600 data.found_sym = true;
22cee43f 5601 }
40658b94
PH
5602 }
5603
d1183b06 5604 if (result.empty () && global && !is_wild_match)
40658b94 5605 {
b5ec771e 5606 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5607 std::string bracket_name = std::string ("<_ada_") + name + '>';
5608 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5609
2030c079 5610 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5611 map_matching_symbols (objfile, name1, false, domain, global, data);
5612 }
339c13b6
JB
5613}
5614
b5ec771e
PA
5615/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5616 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5617 returning the number of matches. Add these to RESULT.
4eeaa230 5618
22cee43f
PMR
5619 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5620 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5621 is the one match returned (no other matches in that or
d9680e73 5622 enclosing blocks is returned). If there are any matches in or
22cee43f 5623 surrounding BLOCK, then these alone are returned.
4eeaa230 5624
b5ec771e
PA
5625 Names prefixed with "standard__" are handled specially:
5626 "standard__" is first stripped off (by the lookup_name
5627 constructor), and only static and global symbols are searched.
14f9c5c9 5628
22cee43f
PMR
5629 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5630 to lookup global symbols. */
5631
5632static void
d1183b06 5633ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5634 const struct block *block,
b5ec771e 5635 const lookup_name_info &lookup_name,
22cee43f
PMR
5636 domain_enum domain,
5637 int full_search,
5638 int *made_global_lookup_p)
14f9c5c9
AS
5639{
5640 struct symbol *sym;
14f9c5c9 5641
22cee43f
PMR
5642 if (made_global_lookup_p)
5643 *made_global_lookup_p = 0;
339c13b6
JB
5644
5645 /* Special case: If the user specifies a symbol name inside package
5646 Standard, do a non-wild matching of the symbol name without
5647 the "standard__" prefix. This was primarily introduced in order
5648 to allow the user to specifically access the standard exceptions
5649 using, for instance, Standard.Constraint_Error when Constraint_Error
5650 is ambiguous (due to the user defining its own Constraint_Error
5651 entity inside its program). */
b5ec771e
PA
5652 if (lookup_name.ada ().standard_p ())
5653 block = NULL;
4c4b4cd2 5654
339c13b6 5655 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5656
4eeaa230
DE
5657 if (block != NULL)
5658 {
5659 if (full_search)
d1183b06 5660 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5661 else
5662 {
5663 /* In the !full_search case we're are being called by
4009ee92 5664 iterate_over_symbols, and we don't want to search
4eeaa230 5665 superblocks. */
d1183b06 5666 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5667 }
d1183b06 5668 if (!result.empty () || !full_search)
22cee43f 5669 return;
4eeaa230 5670 }
d2e4a39e 5671
339c13b6
JB
5672 /* No non-global symbols found. Check our cache to see if we have
5673 already performed this search before. If we have, then return
5674 the same result. */
5675
b5ec771e
PA
5676 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5677 domain, &sym, &block))
4c4b4cd2
PH
5678 {
5679 if (sym != NULL)
d1183b06 5680 add_defn_to_vec (result, sym, block);
22cee43f 5681 return;
4c4b4cd2 5682 }
14f9c5c9 5683
22cee43f
PMR
5684 if (made_global_lookup_p)
5685 *made_global_lookup_p = 1;
b1eedac9 5686
339c13b6
JB
5687 /* Search symbols from all global blocks. */
5688
d1183b06 5689 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5690
4c4b4cd2 5691 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5692 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5693
d1183b06
TT
5694 if (result.empty ())
5695 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5696}
5697
b5ec771e 5698/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5699 is non-zero, enclosing scope and in global scopes.
5700
5701 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5702 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5703
5704 When full_search is non-zero, any non-function/non-enumeral
5705 symbol match within the nest of blocks whose innermost member is BLOCK,
5706 is the one match returned (no other matches in that or
5707 enclosing blocks is returned). If there are any matches in or
5708 surrounding BLOCK, then these alone are returned.
5709
5710 Names prefixed with "standard__" are handled specially: "standard__"
5711 is first stripped off, and only static and global symbols are searched. */
5712
d1183b06 5713static std::vector<struct block_symbol>
b5ec771e
PA
5714ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5715 const struct block *block,
22cee43f 5716 domain_enum domain,
22cee43f
PMR
5717 int full_search)
5718{
22cee43f 5719 int syms_from_global_search;
d1183b06 5720 std::vector<struct block_symbol> results;
22cee43f 5721
d1183b06 5722 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5723 domain, full_search, &syms_from_global_search);
14f9c5c9 5724
d1183b06 5725 remove_extra_symbols (&results);
4c4b4cd2 5726
d1183b06 5727 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5728 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5729
d1183b06 5730 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5731 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5732 results[0].symbol, results[0].block);
ec6a20c2 5733
d1183b06
TT
5734 remove_irrelevant_renamings (&results, block);
5735 return results;
14f9c5c9
AS
5736}
5737
b5ec771e 5738/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5739 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5740
4eeaa230
DE
5741 See ada_lookup_symbol_list_worker for further details. */
5742
d1183b06 5743std::vector<struct block_symbol>
b5ec771e 5744ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5745 domain_enum domain)
4eeaa230 5746{
b5ec771e
PA
5747 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5748 lookup_name_info lookup_name (name, name_match_type);
5749
d1183b06 5750 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5751}
5752
4e5c77fe
JB
5753/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5754 to 1, but choosing the first symbol found if there are multiple
5755 choices.
5756
5e2336be
JB
5757 The result is stored in *INFO, which must be non-NULL.
5758 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5759
5760void
5761ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5762 domain_enum domain,
d12307c1 5763 struct block_symbol *info)
14f9c5c9 5764{
b5ec771e
PA
5765 /* Since we already have an encoded name, wrap it in '<>' to force a
5766 verbatim match. Otherwise, if the name happens to not look like
5767 an encoded name (because it doesn't include a "__"),
5768 ada_lookup_name_info would re-encode/fold it again, and that
5769 would e.g., incorrectly lowercase object renaming names like
5770 "R28b" -> "r28b". */
12932e2c 5771 std::string verbatim = add_angle_brackets (name);
b5ec771e 5772
5e2336be 5773 gdb_assert (info != NULL);
65392b3e 5774 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5775}
aeb5907d
JB
5776
5777/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5778 scope and in global scopes, or NULL if none. NAME is folded and
5779 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5780 choosing the first symbol if there are multiple choices. */
4e5c77fe 5781
d12307c1 5782struct block_symbol
aeb5907d 5783ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5784 domain_enum domain)
aeb5907d 5785{
d1183b06
TT
5786 std::vector<struct block_symbol> candidates
5787 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5788
d1183b06 5789 if (candidates.empty ())
54d343a2 5790 return {};
f98fc17b
PA
5791
5792 block_symbol info = candidates[0];
5793 info.symbol = fixup_symbol_section (info.symbol, NULL);
d12307c1 5794 return info;
4c4b4cd2 5795}
14f9c5c9 5796
14f9c5c9 5797
4c4b4cd2
PH
5798/* True iff STR is a possible encoded suffix of a normal Ada name
5799 that is to be ignored for matching purposes. Suffixes of parallel
5800 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5801 are given by any of the regular expressions:
4c4b4cd2 5802
babe1480
JB
5803 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5804 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5805 TKB [subprogram suffix for task bodies]
babe1480 5806 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5807 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5808
5809 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5810 match is performed. This sequence is used to differentiate homonyms,
5811 is an optional part of a valid name suffix. */
4c4b4cd2 5812
14f9c5c9 5813static int
d2e4a39e 5814is_name_suffix (const char *str)
14f9c5c9
AS
5815{
5816 int k;
4c4b4cd2
PH
5817 const char *matching;
5818 const int len = strlen (str);
5819
babe1480
JB
5820 /* Skip optional leading __[0-9]+. */
5821
4c4b4cd2
PH
5822 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5823 {
babe1480
JB
5824 str += 3;
5825 while (isdigit (str[0]))
dda83cd7 5826 str += 1;
4c4b4cd2 5827 }
babe1480
JB
5828
5829 /* [.$][0-9]+ */
4c4b4cd2 5830
babe1480 5831 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5832 {
babe1480 5833 matching = str + 1;
4c4b4cd2 5834 while (isdigit (matching[0]))
dda83cd7 5835 matching += 1;
4c4b4cd2 5836 if (matching[0] == '\0')
dda83cd7 5837 return 1;
4c4b4cd2
PH
5838 }
5839
5840 /* ___[0-9]+ */
babe1480 5841
4c4b4cd2
PH
5842 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5843 {
5844 matching = str + 3;
5845 while (isdigit (matching[0]))
dda83cd7 5846 matching += 1;
4c4b4cd2 5847 if (matching[0] == '\0')
dda83cd7 5848 return 1;
4c4b4cd2
PH
5849 }
5850
9ac7f98e
JB
5851 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5852
5853 if (strcmp (str, "TKB") == 0)
5854 return 1;
5855
529cad9c
PH
5856#if 0
5857 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5858 with a N at the end. Unfortunately, the compiler uses the same
5859 convention for other internal types it creates. So treating
529cad9c 5860 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5861 some regressions. For instance, consider the case of an enumerated
5862 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5863 name ends with N.
5864 Having a single character like this as a suffix carrying some
0963b4bd 5865 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5866 to be something like "_N" instead. In the meantime, do not do
5867 the following check. */
5868 /* Protected Object Subprograms */
5869 if (len == 1 && str [0] == 'N')
5870 return 1;
5871#endif
5872
5873 /* _E[0-9]+[bs]$ */
5874 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5875 {
5876 matching = str + 3;
5877 while (isdigit (matching[0]))
dda83cd7 5878 matching += 1;
529cad9c 5879 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5880 && matching [1] == '\0')
5881 return 1;
529cad9c
PH
5882 }
5883
4c4b4cd2
PH
5884 /* ??? We should not modify STR directly, as we are doing below. This
5885 is fine in this case, but may become problematic later if we find
5886 that this alternative did not work, and want to try matching
5887 another one from the begining of STR. Since we modified it, we
5888 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5889 if (str[0] == 'X')
5890 {
5891 str += 1;
d2e4a39e 5892 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5893 {
5894 if (str[0] != 'n' && str[0] != 'b')
5895 return 0;
5896 str += 1;
5897 }
14f9c5c9 5898 }
babe1480 5899
14f9c5c9
AS
5900 if (str[0] == '\000')
5901 return 1;
babe1480 5902
d2e4a39e 5903 if (str[0] == '_')
14f9c5c9
AS
5904 {
5905 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5906 return 0;
d2e4a39e 5907 if (str[2] == '_')
dda83cd7
SM
5908 {
5909 if (strcmp (str + 3, "JM") == 0)
5910 return 1;
5911 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5912 the LJM suffix in favor of the JM one. But we will
5913 still accept LJM as a valid suffix for a reasonable
5914 amount of time, just to allow ourselves to debug programs
5915 compiled using an older version of GNAT. */
5916 if (strcmp (str + 3, "LJM") == 0)
5917 return 1;
5918 if (str[3] != 'X')
5919 return 0;
5920 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5921 || str[4] == 'U' || str[4] == 'P')
5922 return 1;
5923 if (str[4] == 'R' && str[5] != 'T')
5924 return 1;
5925 return 0;
5926 }
4c4b4cd2 5927 if (!isdigit (str[2]))
dda83cd7 5928 return 0;
4c4b4cd2 5929 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5930 if (!isdigit (str[k]) && str[k] != '_')
5931 return 0;
14f9c5c9
AS
5932 return 1;
5933 }
4c4b4cd2 5934 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5935 {
4c4b4cd2 5936 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5937 if (!isdigit (str[k]) && str[k] != '_')
5938 return 0;
14f9c5c9
AS
5939 return 1;
5940 }
5941 return 0;
5942}
d2e4a39e 5943
aeb5907d
JB
5944/* Return non-zero if the string starting at NAME and ending before
5945 NAME_END contains no capital letters. */
529cad9c
PH
5946
5947static int
5948is_valid_name_for_wild_match (const char *name0)
5949{
f945dedf 5950 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5951 int i;
5952
5823c3ef
JB
5953 /* If the decoded name starts with an angle bracket, it means that
5954 NAME0 does not follow the GNAT encoding format. It should then
5955 not be allowed as a possible wild match. */
5956 if (decoded_name[0] == '<')
5957 return 0;
5958
529cad9c
PH
5959 for (i=0; decoded_name[i] != '\0'; i++)
5960 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5961 return 0;
5962
5963 return 1;
5964}
5965
59c8a30b
JB
5966/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5967 character which could start a simple name. Assumes that *NAMEP points
5968 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5969
14f9c5c9 5970static int
59c8a30b 5971advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5972{
73589123 5973 const char *name = *namep;
5b4ee69b 5974
5823c3ef 5975 while (1)
14f9c5c9 5976 {
59c8a30b 5977 char t0, t1;
73589123
PH
5978
5979 t0 = *name;
5980 if (t0 == '_')
5981 {
5982 t1 = name[1];
5983 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5984 {
5985 name += 1;
61012eef 5986 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5987 break;
5988 else
5989 name += 1;
5990 }
aa27d0b3
JB
5991 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5992 || name[2] == target0))
73589123
PH
5993 {
5994 name += 2;
5995 break;
5996 }
86b44259
TT
5997 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5998 {
5999 /* Names like "pkg__B_N__name", where N is a number, are
6000 block-local. We can handle these by simply skipping
6001 the "B_" here. */
6002 name += 4;
6003 }
73589123
PH
6004 else
6005 return 0;
6006 }
6007 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
6008 name += 1;
6009 else
5823c3ef 6010 return 0;
73589123
PH
6011 }
6012
6013 *namep = name;
6014 return 1;
6015}
6016
b5ec771e
PA
6017/* Return true iff NAME encodes a name of the form prefix.PATN.
6018 Ignores any informational suffixes of NAME (i.e., for which
6019 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6020 simple name. */
73589123 6021
b5ec771e 6022static bool
73589123
PH
6023wild_match (const char *name, const char *patn)
6024{
22e048c9 6025 const char *p;
73589123
PH
6026 const char *name0 = name;
6027
81eaa506
TT
6028 if (startswith (name, "___ghost_"))
6029 name += 9;
6030
73589123
PH
6031 while (1)
6032 {
6033 const char *match = name;
6034
6035 if (*name == *patn)
6036 {
6037 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6038 if (*p != *name)
6039 break;
6040 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6041 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6042
6043 if (name[-1] == '_')
6044 name -= 1;
6045 }
6046 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6047 return false;
96d887e8 6048 }
96d887e8
PH
6049}
6050
d1183b06 6051/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6052 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6053
6054static void
d1183b06 6055ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6056 const struct block *block,
6057 const lookup_name_info &lookup_name,
6058 domain_enum domain, struct objfile *objfile)
96d887e8 6059{
8157b174 6060 struct block_iterator iter;
96d887e8
PH
6061 /* A matching argument symbol, if any. */
6062 struct symbol *arg_sym;
6063 /* Set true when we find a matching non-argument symbol. */
1178743e 6064 bool found_sym;
96d887e8
PH
6065 struct symbol *sym;
6066
6067 arg_sym = NULL;
1178743e 6068 found_sym = false;
b5ec771e
PA
6069 for (sym = block_iter_match_first (block, lookup_name, &iter);
6070 sym != NULL;
6071 sym = block_iter_match_next (lookup_name, &iter))
96d887e8 6072 {
6c9c307c 6073 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
b5ec771e 6074 {
66d7f48f 6075 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6076 {
d9743061 6077 if (sym->is_argument ())
b5ec771e
PA
6078 arg_sym = sym;
6079 else
6080 {
1178743e 6081 found_sym = true;
d1183b06 6082 add_defn_to_vec (result,
b5ec771e
PA
6083 fixup_symbol_section (sym, objfile),
6084 block);
6085 }
6086 }
6087 }
96d887e8
PH
6088 }
6089
22cee43f
PMR
6090 /* Handle renamings. */
6091
d1183b06 6092 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6093 found_sym = true;
22cee43f 6094
96d887e8
PH
6095 if (!found_sym && arg_sym != NULL)
6096 {
d1183b06 6097 add_defn_to_vec (result,
dda83cd7
SM
6098 fixup_symbol_section (arg_sym, objfile),
6099 block);
96d887e8
PH
6100 }
6101
b5ec771e 6102 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6103 {
6104 arg_sym = NULL;
1178743e 6105 found_sym = false;
b5ec771e
PA
6106 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6107 const char *name = ada_lookup_name.c_str ();
6108 size_t name_len = ada_lookup_name.size ();
96d887e8
PH
6109
6110 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 6111 {
dda83cd7 6112 if (symbol_matches_domain (sym->language (),
6c9c307c 6113 sym->domain (), domain))
dda83cd7
SM
6114 {
6115 int cmp;
6116
6117 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6118 if (cmp == 0)
6119 {
6120 cmp = !startswith (sym->linkage_name (), "_ada_");
6121 if (cmp == 0)
6122 cmp = strncmp (name, sym->linkage_name () + 5,
6123 name_len);
6124 }
6125
6126 if (cmp == 0
6127 && is_name_suffix (sym->linkage_name () + name_len + 5))
6128 {
66d7f48f 6129 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6130 {
d9743061 6131 if (sym->is_argument ())
2a2d4dc3
AS
6132 arg_sym = sym;
6133 else
6134 {
1178743e 6135 found_sym = true;
d1183b06 6136 add_defn_to_vec (result,
2a2d4dc3
AS
6137 fixup_symbol_section (sym, objfile),
6138 block);
6139 }
6140 }
dda83cd7
SM
6141 }
6142 }
76a01679 6143 }
96d887e8
PH
6144
6145 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6146 They aren't parameters, right? */
96d887e8 6147 if (!found_sym && arg_sym != NULL)
dda83cd7 6148 {
d1183b06 6149 add_defn_to_vec (result,
dda83cd7
SM
6150 fixup_symbol_section (arg_sym, objfile),
6151 block);
6152 }
96d887e8
PH
6153 }
6154}
6155\f
41d27058 6156
dda83cd7 6157 /* Symbol Completion */
41d27058 6158
b5ec771e 6159/* See symtab.h. */
41d27058 6160
b5ec771e
PA
6161bool
6162ada_lookup_name_info::matches
6163 (const char *sym_name,
6164 symbol_name_match_type match_type,
a207cff2 6165 completion_match_result *comp_match_res) const
41d27058 6166{
b5ec771e
PA
6167 bool match = false;
6168 const char *text = m_encoded_name.c_str ();
6169 size_t text_len = m_encoded_name.size ();
41d27058
JB
6170
6171 /* First, test against the fully qualified name of the symbol. */
6172
6173 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6174 match = true;
41d27058 6175
f945dedf 6176 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6177 if (match && !m_encoded_p)
41d27058
JB
6178 {
6179 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6180 that iff we are doing a verbatim match, the decoded version
6181 of the symbol name starts with '<'. Otherwise, this symbol name
6182 is not a suitable completion. */
41d27058 6183
f945dedf 6184 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6185 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6186 }
6187
b5ec771e 6188 if (match && !m_verbatim_p)
41d27058
JB
6189 {
6190 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6191 be done is to verify that the potentially matching symbol name
6192 does not include capital letters, because the ada-mode would
6193 not be able to understand these symbol names without the
6194 angle bracket notation. */
41d27058
JB
6195 const char *tmp;
6196
6197 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6198 if (*tmp != '\0')
b5ec771e 6199 match = false;
41d27058
JB
6200 }
6201
6202 /* Second: Try wild matching... */
6203
b5ec771e 6204 if (!match && m_wild_match_p)
41d27058
JB
6205 {
6206 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6207 may represent an unqualified symbol name. We therefore must
6208 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6209 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6210
6211 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6212 match = true;
41d27058
JB
6213 }
6214
b5ec771e 6215 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6216
6217 if (!match)
b5ec771e 6218 return false;
41d27058 6219
a207cff2 6220 if (comp_match_res != NULL)
b5ec771e 6221 {
a207cff2 6222 std::string &match_str = comp_match_res->match.storage ();
41d27058 6223
b5ec771e 6224 if (!m_encoded_p)
a207cff2 6225 match_str = ada_decode (sym_name);
b5ec771e
PA
6226 else
6227 {
6228 if (m_verbatim_p)
6229 match_str = add_angle_brackets (sym_name);
6230 else
6231 match_str = sym_name;
41d27058 6232
b5ec771e 6233 }
a207cff2
PA
6234
6235 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6236 }
6237
b5ec771e 6238 return true;
41d27058
JB
6239}
6240
dda83cd7 6241 /* Field Access */
96d887e8 6242
73fb9985
JB
6243/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6244 for tagged types. */
6245
6246static int
6247ada_is_dispatch_table_ptr_type (struct type *type)
6248{
0d5cff50 6249 const char *name;
73fb9985 6250
78134374 6251 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6252 return 0;
6253
7d93a1e0 6254 name = TYPE_TARGET_TYPE (type)->name ();
73fb9985
JB
6255 if (name == NULL)
6256 return 0;
6257
6258 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6259}
6260
ac4a2da4
JG
6261/* Return non-zero if TYPE is an interface tag. */
6262
6263static int
6264ada_is_interface_tag (struct type *type)
6265{
7d93a1e0 6266 const char *name = type->name ();
ac4a2da4
JG
6267
6268 if (name == NULL)
6269 return 0;
6270
6271 return (strcmp (name, "ada__tags__interface_tag") == 0);
6272}
6273
963a6417
PH
6274/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6275 to be invisible to users. */
96d887e8 6276
963a6417
PH
6277int
6278ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6279{
1f704f76 6280 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6281 return 1;
ffde82bf 6282
73fb9985
JB
6283 /* Check the name of that field. */
6284 {
33d16dd9 6285 const char *name = type->field (field_num).name ();
73fb9985
JB
6286
6287 /* Anonymous field names should not be printed.
6288 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6289 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6290 if (name == NULL)
6291 return 1;
6292
ffde82bf
JB
6293 /* Normally, fields whose name start with an underscore ("_")
6294 are fields that have been internally generated by the compiler,
6295 and thus should not be printed. The "_parent" field is special,
6296 however: This is a field internally generated by the compiler
6297 for tagged types, and it contains the components inherited from
6298 the parent type. This field should not be printed as is, but
6299 should not be ignored either. */
61012eef 6300 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985 6301 return 1;
d537777d
TT
6302
6303 /* The compiler doesn't document this, but sometimes it emits
6304 a field whose name starts with a capital letter, like 'V148s'.
6305 These aren't marked as artificial in any way, but we know they
6306 should be ignored. However, wrapper fields should not be
6307 ignored. */
6308 if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
6309 {
6310 /* Wrapper field. */
6311 }
6312 else if (isupper (name[0]))
6313 return 1;
73fb9985
JB
6314 }
6315
ac4a2da4
JG
6316 /* If this is the dispatch table of a tagged type or an interface tag,
6317 then ignore. */
73fb9985 6318 if (ada_is_tagged_type (type, 1)
940da03e
SM
6319 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6320 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6321 return 1;
6322
6323 /* Not a special field, so it should not be ignored. */
6324 return 0;
963a6417 6325}
96d887e8 6326
963a6417 6327/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6328 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6329
963a6417
PH
6330int
6331ada_is_tagged_type (struct type *type, int refok)
6332{
988f6b3d 6333 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6334}
96d887e8 6335
963a6417 6336/* True iff TYPE represents the type of X'Tag */
96d887e8 6337
963a6417
PH
6338int
6339ada_is_tag_type (struct type *type)
6340{
460efde1
JB
6341 type = ada_check_typedef (type);
6342
78134374 6343 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6344 return 0;
6345 else
96d887e8 6346 {
963a6417 6347 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 6348
963a6417 6349 return (name != NULL
dda83cd7 6350 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6351 }
96d887e8
PH
6352}
6353
963a6417 6354/* The type of the tag on VAL. */
76a01679 6355
de93309a 6356static struct type *
963a6417 6357ada_tag_type (struct value *val)
96d887e8 6358{
988f6b3d 6359 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0);
963a6417 6360}
96d887e8 6361
b50d69b5
JG
6362/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6363 retired at Ada 05). */
6364
6365static int
6366is_ada95_tag (struct value *tag)
6367{
6368 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6369}
6370
963a6417 6371/* The value of the tag on VAL. */
96d887e8 6372
de93309a 6373static struct value *
963a6417
PH
6374ada_value_tag (struct value *val)
6375{
03ee6b2e 6376 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6377}
6378
963a6417
PH
6379/* The value of the tag on the object of type TYPE whose contents are
6380 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6381 ADDRESS. */
96d887e8 6382
963a6417 6383static struct value *
10a2c479 6384value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6385 const gdb_byte *valaddr,
dda83cd7 6386 CORE_ADDR address)
96d887e8 6387{
b5385fc0 6388 int tag_byte_offset;
963a6417 6389 struct type *tag_type;
5b4ee69b 6390
4d1795ac
TT
6391 gdb::array_view<const gdb_byte> contents;
6392 if (valaddr != nullptr)
6393 contents = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
6394 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6395 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6396 NULL, NULL, NULL))
96d887e8 6397 {
fc1a4b47 6398 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6399 ? NULL
6400 : valaddr + tag_byte_offset);
963a6417 6401 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6402
963a6417 6403 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6404 }
963a6417
PH
6405 return NULL;
6406}
96d887e8 6407
963a6417
PH
6408static struct type *
6409type_from_tag (struct value *tag)
6410{
f5272a3b 6411 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6412
963a6417 6413 if (type_name != NULL)
5c4258f4 6414 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6415 return NULL;
6416}
96d887e8 6417
b50d69b5
JG
6418/* Given a value OBJ of a tagged type, return a value of this
6419 type at the base address of the object. The base address, as
6420 defined in Ada.Tags, it is the address of the primary tag of
6421 the object, and therefore where the field values of its full
6422 view can be fetched. */
6423
6424struct value *
6425ada_tag_value_at_base_address (struct value *obj)
6426{
b50d69b5
JG
6427 struct value *val;
6428 LONGEST offset_to_top = 0;
6429 struct type *ptr_type, *obj_type;
6430 struct value *tag;
6431 CORE_ADDR base_address;
6432
6433 obj_type = value_type (obj);
6434
6435 /* It is the responsability of the caller to deref pointers. */
6436
78134374 6437 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6438 return obj;
6439
6440 tag = ada_value_tag (obj);
6441 if (!tag)
6442 return obj;
6443
6444 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6445
6446 if (is_ada95_tag (tag))
6447 return obj;
6448
d537777d
TT
6449 struct type *offset_type
6450 = language_lookup_primitive_type (language_def (language_ada),
6451 target_gdbarch(), "storage_offset");
6452 ptr_type = lookup_pointer_type (offset_type);
b50d69b5
JG
6453 val = value_cast (ptr_type, tag);
6454 if (!val)
6455 return obj;
6456
6457 /* It is perfectly possible that an exception be raised while
6458 trying to determine the base address, just like for the tag;
6459 see ada_tag_name for more details. We do not print the error
6460 message for the same reason. */
6461
a70b8144 6462 try
b50d69b5
JG
6463 {
6464 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6465 }
6466
230d2906 6467 catch (const gdb_exception_error &e)
492d29ea
PA
6468 {
6469 return obj;
6470 }
b50d69b5
JG
6471
6472 /* If offset is null, nothing to do. */
6473
6474 if (offset_to_top == 0)
6475 return obj;
6476
6477 /* -1 is a special case in Ada.Tags; however, what should be done
6478 is not quite clear from the documentation. So do nothing for
6479 now. */
6480
6481 if (offset_to_top == -1)
6482 return obj;
6483
d537777d
TT
6484 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6485 top is used. In this situation the offset is stored just after
6486 the tag, in the object itself. */
6487 ULONGEST last = (((ULONGEST) 1) << (8 * TYPE_LENGTH (offset_type) - 1)) - 1;
6488 if (offset_to_top == last)
6489 {
6490 struct value *tem = value_addr (tag);
6491 tem = value_ptradd (tem, 1);
6492 tem = value_cast (ptr_type, tem);
6493 offset_to_top = value_as_long (value_ind (tem));
6494 }
6495 else if (offset_to_top > 0)
6496 {
6497 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6498 from the base address. This was however incompatible with
6499 C++ dispatch table: C++ uses a *negative* value to *add*
6500 to the base address. Ada's convention has therefore been
6501 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6502 use the same convention. Here, we support both cases by
6503 checking the sign of OFFSET_TO_TOP. */
6504 offset_to_top = -offset_to_top;
6505 }
08f49010
XR
6506
6507 base_address = value_address (obj) + offset_to_top;
b50d69b5
JG
6508 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6509
6510 /* Make sure that we have a proper tag at the new address.
6511 Otherwise, offset_to_top is bogus (which can happen when
6512 the object is not initialized yet). */
6513
6514 if (!tag)
6515 return obj;
6516
6517 obj_type = type_from_tag (tag);
6518
6519 if (!obj_type)
6520 return obj;
6521
6522 return value_from_contents_and_address (obj_type, NULL, base_address);
6523}
6524
1b611343
JB
6525/* Return the "ada__tags__type_specific_data" type. */
6526
6527static struct type *
6528ada_get_tsd_type (struct inferior *inf)
963a6417 6529{
1b611343 6530 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6531
1b611343
JB
6532 if (data->tsd_type == 0)
6533 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6534 return data->tsd_type;
6535}
529cad9c 6536
1b611343
JB
6537/* Return the TSD (type-specific data) associated to the given TAG.
6538 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6539
1b611343 6540 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6541
1b611343
JB
6542static struct value *
6543ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6544{
4c4b4cd2 6545 struct value *val;
1b611343 6546 struct type *type;
5b4ee69b 6547
1b611343
JB
6548 /* First option: The TSD is simply stored as a field of our TAG.
6549 Only older versions of GNAT would use this format, but we have
6550 to test it first, because there are no visible markers for
6551 the current approach except the absence of that field. */
529cad9c 6552
1b611343
JB
6553 val = ada_value_struct_elt (tag, "tsd", 1);
6554 if (val)
6555 return val;
e802dbe0 6556
1b611343
JB
6557 /* Try the second representation for the dispatch table (in which
6558 there is no explicit 'tsd' field in the referent of the tag pointer,
6559 and instead the tsd pointer is stored just before the dispatch
6560 table. */
e802dbe0 6561
1b611343
JB
6562 type = ada_get_tsd_type (current_inferior());
6563 if (type == NULL)
6564 return NULL;
6565 type = lookup_pointer_type (lookup_pointer_type (type));
6566 val = value_cast (type, tag);
6567 if (val == NULL)
6568 return NULL;
6569 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6570}
6571
1b611343
JB
6572/* Given the TSD of a tag (type-specific data), return a string
6573 containing the name of the associated type.
6574
f5272a3b 6575 May return NULL if we are unable to determine the tag name. */
1b611343 6576
f5272a3b 6577static gdb::unique_xmalloc_ptr<char>
1b611343 6578ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6579{
1b611343 6580 struct value *val;
529cad9c 6581
1b611343 6582 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6583 if (val == NULL)
1b611343 6584 return NULL;
66920317
TT
6585 gdb::unique_xmalloc_ptr<char> buffer
6586 = target_read_string (value_as_address (val), INT_MAX);
6587 if (buffer == nullptr)
f5272a3b
TT
6588 return nullptr;
6589
315e4ebb 6590 try
f5272a3b 6591 {
315e4ebb
TT
6592 /* Let this throw an exception on error. If the data is
6593 uninitialized, we'd rather not have the user see a
6594 warning. */
6595 const char *folded = ada_fold_name (buffer.get (), true);
6596 return make_unique_xstrdup (folded);
6597 }
6598 catch (const gdb_exception &)
6599 {
6600 return nullptr;
f5272a3b 6601 }
4c4b4cd2
PH
6602}
6603
6604/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6605 a C string.
6606
6607 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6608 determine the name of that tag. */
4c4b4cd2 6609
f5272a3b 6610gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6611ada_tag_name (struct value *tag)
6612{
f5272a3b 6613 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6614
df407dfe 6615 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 6616 return NULL;
1b611343
JB
6617
6618 /* It is perfectly possible that an exception be raised while trying
6619 to determine the TAG's name, even under normal circumstances:
6620 The associated variable may be uninitialized or corrupted, for
6621 instance. We do not let any exception propagate past this point.
6622 instead we return NULL.
6623
6624 We also do not print the error message either (which often is very
6625 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6626 the caller print a more meaningful message if necessary. */
a70b8144 6627 try
1b611343
JB
6628 {
6629 struct value *tsd = ada_get_tsd_from_tag (tag);
6630
6631 if (tsd != NULL)
6632 name = ada_tag_name_from_tsd (tsd);
6633 }
230d2906 6634 catch (const gdb_exception_error &e)
492d29ea
PA
6635 {
6636 }
1b611343
JB
6637
6638 return name;
4c4b4cd2
PH
6639}
6640
6641/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6642
d2e4a39e 6643struct type *
ebf56fd3 6644ada_parent_type (struct type *type)
14f9c5c9
AS
6645{
6646 int i;
6647
61ee279c 6648 type = ada_check_typedef (type);
14f9c5c9 6649
78134374 6650 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6651 return NULL;
6652
1f704f76 6653 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6654 if (ada_is_parent_field (type, i))
0c1f74cf 6655 {
dda83cd7 6656 struct type *parent_type = type->field (i).type ();
0c1f74cf 6657
dda83cd7
SM
6658 /* If the _parent field is a pointer, then dereference it. */
6659 if (parent_type->code () == TYPE_CODE_PTR)
6660 parent_type = TYPE_TARGET_TYPE (parent_type);
6661 /* If there is a parallel XVS type, get the actual base type. */
6662 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6663
dda83cd7 6664 return ada_check_typedef (parent_type);
0c1f74cf 6665 }
14f9c5c9
AS
6666
6667 return NULL;
6668}
6669
4c4b4cd2
PH
6670/* True iff field number FIELD_NUM of structure type TYPE contains the
6671 parent-type (inherited) fields of a derived type. Assumes TYPE is
6672 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6673
6674int
ebf56fd3 6675ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6676{
33d16dd9 6677 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6678
4c4b4cd2 6679 return (name != NULL
dda83cd7
SM
6680 && (startswith (name, "PARENT")
6681 || startswith (name, "_parent")));
14f9c5c9
AS
6682}
6683
4c4b4cd2 6684/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6685 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6686 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6687 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6688 structures. */
14f9c5c9
AS
6689
6690int
ebf56fd3 6691ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6692{
33d16dd9 6693 const char *name = type->field (field_num).name ();
5b4ee69b 6694
dddc0e16
JB
6695 if (name != NULL && strcmp (name, "RETVAL") == 0)
6696 {
6697 /* This happens in functions with "out" or "in out" parameters
6698 which are passed by copy. For such functions, GNAT describes
6699 the function's return type as being a struct where the return
6700 value is in a field called RETVAL, and where the other "out"
6701 or "in out" parameters are fields of that struct. This is not
6702 a wrapper. */
6703 return 0;
6704 }
6705
d2e4a39e 6706 return (name != NULL
dda83cd7
SM
6707 && (startswith (name, "PARENT")
6708 || strcmp (name, "REP") == 0
6709 || startswith (name, "_parent")
6710 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6711}
6712
4c4b4cd2
PH
6713/* True iff field number FIELD_NUM of structure or union type TYPE
6714 is a variant wrapper. Assumes TYPE is a structure type with at least
6715 FIELD_NUM+1 fields. */
14f9c5c9
AS
6716
6717int
ebf56fd3 6718ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6719{
8ecb59f8
TT
6720 /* Only Ada types are eligible. */
6721 if (!ADA_TYPE_P (type))
6722 return 0;
6723
940da03e 6724 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6725
78134374
SM
6726 return (field_type->code () == TYPE_CODE_UNION
6727 || (is_dynamic_field (type, field_num)
6728 && (TYPE_TARGET_TYPE (field_type)->code ()
c3e5cd34 6729 == TYPE_CODE_UNION)));
14f9c5c9
AS
6730}
6731
6732/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6733 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6734 returns the type of the controlling discriminant for the variant.
6735 May return NULL if the type could not be found. */
14f9c5c9 6736
d2e4a39e 6737struct type *
ebf56fd3 6738ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6739{
a121b7c1 6740 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6741
988f6b3d 6742 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6743}
6744
4c4b4cd2 6745/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6746 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6747 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6748
de93309a 6749static int
ebf56fd3 6750ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6751{
33d16dd9 6752 const char *name = type->field (field_num).name ();
5b4ee69b 6753
14f9c5c9
AS
6754 return (name != NULL && name[0] == 'O');
6755}
6756
6757/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6758 returns the name of the discriminant controlling the variant.
6759 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6760
a121b7c1 6761const char *
ebf56fd3 6762ada_variant_discrim_name (struct type *type0)
14f9c5c9 6763{
5f9febe0 6764 static std::string result;
d2e4a39e
AS
6765 struct type *type;
6766 const char *name;
6767 const char *discrim_end;
6768 const char *discrim_start;
14f9c5c9 6769
78134374 6770 if (type0->code () == TYPE_CODE_PTR)
14f9c5c9
AS
6771 type = TYPE_TARGET_TYPE (type0);
6772 else
6773 type = type0;
6774
6775 name = ada_type_name (type);
6776
6777 if (name == NULL || name[0] == '\000')
6778 return "";
6779
6780 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6781 discrim_end -= 1)
6782 {
61012eef 6783 if (startswith (discrim_end, "___XVN"))
dda83cd7 6784 break;
14f9c5c9
AS
6785 }
6786 if (discrim_end == name)
6787 return "";
6788
d2e4a39e 6789 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6790 discrim_start -= 1)
6791 {
d2e4a39e 6792 if (discrim_start == name + 1)
dda83cd7 6793 return "";
76a01679 6794 if ((discrim_start > name + 3
dda83cd7
SM
6795 && startswith (discrim_start - 3, "___"))
6796 || discrim_start[-1] == '.')
6797 break;
14f9c5c9
AS
6798 }
6799
5f9febe0
TT
6800 result = std::string (discrim_start, discrim_end - discrim_start);
6801 return result.c_str ();
14f9c5c9
AS
6802}
6803
4c4b4cd2
PH
6804/* Scan STR for a subtype-encoded number, beginning at position K.
6805 Put the position of the character just past the number scanned in
6806 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6807 Return 1 if there was a valid number at the given position, and 0
6808 otherwise. A "subtype-encoded" number consists of the absolute value
6809 in decimal, followed by the letter 'm' to indicate a negative number.
6810 Assumes 0m does not occur. */
14f9c5c9
AS
6811
6812int
d2e4a39e 6813ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6814{
6815 ULONGEST RU;
6816
d2e4a39e 6817 if (!isdigit (str[k]))
14f9c5c9
AS
6818 return 0;
6819
4c4b4cd2 6820 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6821 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6822 LONGEST. */
14f9c5c9
AS
6823 RU = 0;
6824 while (isdigit (str[k]))
6825 {
d2e4a39e 6826 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6827 k += 1;
6828 }
6829
d2e4a39e 6830 if (str[k] == 'm')
14f9c5c9
AS
6831 {
6832 if (R != NULL)
dda83cd7 6833 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6834 k += 1;
6835 }
6836 else if (R != NULL)
6837 *R = (LONGEST) RU;
6838
4c4b4cd2 6839 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6840 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6841 number representable as a LONGEST (although either would probably work
6842 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6843 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6844
6845 if (new_k != NULL)
6846 *new_k = k;
6847 return 1;
6848}
6849
4c4b4cd2
PH
6850/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6851 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6852 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6853
de93309a 6854static int
ebf56fd3 6855ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6856{
33d16dd9 6857 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6858 int p;
6859
6860 p = 0;
6861 while (1)
6862 {
d2e4a39e 6863 switch (name[p])
dda83cd7
SM
6864 {
6865 case '\0':
6866 return 0;
6867 case 'S':
6868 {
6869 LONGEST W;
6870
6871 if (!ada_scan_number (name, p + 1, &W, &p))
6872 return 0;
6873 if (val == W)
6874 return 1;
6875 break;
6876 }
6877 case 'R':
6878 {
6879 LONGEST L, U;
6880
6881 if (!ada_scan_number (name, p + 1, &L, &p)
6882 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6883 return 0;
6884 if (val >= L && val <= U)
6885 return 1;
6886 break;
6887 }
6888 case 'O':
6889 return 1;
6890 default:
6891 return 0;
6892 }
4c4b4cd2
PH
6893 }
6894}
6895
0963b4bd 6896/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6897
6898/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6899 ARG_TYPE, extract and return the value of one of its (non-static)
6900 fields. FIELDNO says which field. Differs from value_primitive_field
6901 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6902
5eb68a39 6903struct value *
d2e4a39e 6904ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6905 struct type *arg_type)
14f9c5c9 6906{
14f9c5c9
AS
6907 struct type *type;
6908
61ee279c 6909 arg_type = ada_check_typedef (arg_type);
940da03e 6910 type = arg_type->field (fieldno).type ();
14f9c5c9 6911
4504bbde
TT
6912 /* Handle packed fields. It might be that the field is not packed
6913 relative to its containing structure, but the structure itself is
6914 packed; in this case we must take the bit-field path. */
6915 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || value_bitpos (arg1) != 0)
14f9c5c9 6916 {
b610c045 6917 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
14f9c5c9 6918 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6919
50888e42
SM
6920 return ada_value_primitive_packed_val (arg1,
6921 value_contents (arg1).data (),
dda83cd7
SM
6922 offset + bit_pos / 8,
6923 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6924 }
6925 else
6926 return value_primitive_field (arg1, offset, fieldno, arg_type);
6927}
6928
52ce6436
PH
6929/* Find field with name NAME in object of type TYPE. If found,
6930 set the following for each argument that is non-null:
6931 - *FIELD_TYPE_P to the field's type;
6932 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6933 an object of that type;
6934 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6935 - *BIT_SIZE_P to its size in bits if the field is packed, and
6936 0 otherwise;
6937 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6938 fields up to but not including the desired field, or by the total
6939 number of fields if not found. A NULL value of NAME never
6940 matches; the function just counts visible fields in this case.
6941
828d5846
XR
6942 Notice that we need to handle when a tagged record hierarchy
6943 has some components with the same name, like in this scenario:
6944
6945 type Top_T is tagged record
dda83cd7
SM
6946 N : Integer := 1;
6947 U : Integer := 974;
6948 A : Integer := 48;
828d5846
XR
6949 end record;
6950
6951 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6952 N : Character := 'a';
6953 C : Integer := 3;
828d5846
XR
6954 end record;
6955
6956 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6957 N : Float := 4.0;
6958 C : Character := '5';
6959 X : Integer := 6;
6960 A : Character := 'J';
828d5846
XR
6961 end record;
6962
6963 Let's say we now have a variable declared and initialized as follow:
6964
6965 TC : Top_A := new Bottom_T;
6966
6967 And then we use this variable to call this function
6968
6969 procedure Assign (Obj: in out Top_T; TV : Integer);
6970
6971 as follow:
6972
6973 Assign (Top_T (B), 12);
6974
6975 Now, we're in the debugger, and we're inside that procedure
6976 then and we want to print the value of obj.c:
6977
6978 Usually, the tagged record or one of the parent type owns the
6979 component to print and there's no issue but in this particular
6980 case, what does it mean to ask for Obj.C? Since the actual
6981 type for object is type Bottom_T, it could mean two things: type
6982 component C from the Middle_T view, but also component C from
6983 Bottom_T. So in that "undefined" case, when the component is
6984 not found in the non-resolved type (which includes all the
6985 components of the parent type), then resolve it and see if we
6986 get better luck once expanded.
6987
6988 In the case of homonyms in the derived tagged type, we don't
6989 guaranty anything, and pick the one that's easiest for us
6990 to program.
6991
0963b4bd 6992 Returns 1 if found, 0 otherwise. */
52ce6436 6993
4c4b4cd2 6994static int
0d5cff50 6995find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6996 struct type **field_type_p,
6997 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6998 int *index_p)
4c4b4cd2
PH
6999{
7000 int i;
828d5846 7001 int parent_offset = -1;
4c4b4cd2 7002
61ee279c 7003 type = ada_check_typedef (type);
76a01679 7004
52ce6436
PH
7005 if (field_type_p != NULL)
7006 *field_type_p = NULL;
7007 if (byte_offset_p != NULL)
d5d6fca5 7008 *byte_offset_p = 0;
52ce6436
PH
7009 if (bit_offset_p != NULL)
7010 *bit_offset_p = 0;
7011 if (bit_size_p != NULL)
7012 *bit_size_p = 0;
7013
1f704f76 7014 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 7015 {
4d1795ac
TT
7016 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
7017 type. However, we only need the values to be correct when
7018 the caller asks for them. */
7019 int bit_pos = 0, fld_offset = 0;
7020 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7021 {
b610c045 7022 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
7023 fld_offset = offset + bit_pos / 8;
7024 }
7025
33d16dd9 7026 const char *t_field_name = type->field (i).name ();
76a01679 7027
4c4b4cd2 7028 if (t_field_name == NULL)
dda83cd7 7029 continue;
4c4b4cd2 7030
828d5846 7031 else if (ada_is_parent_field (type, i))
dda83cd7 7032 {
828d5846
XR
7033 /* This is a field pointing us to the parent type of a tagged
7034 type. As hinted in this function's documentation, we give
7035 preference to fields in the current record first, so what
7036 we do here is just record the index of this field before
7037 we skip it. If it turns out we couldn't find our field
7038 in the current record, then we'll get back to it and search
7039 inside it whether the field might exist in the parent. */
7040
dda83cd7
SM
7041 parent_offset = i;
7042 continue;
7043 }
828d5846 7044
52ce6436 7045 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
7046 {
7047 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 7048
52ce6436 7049 if (field_type_p != NULL)
940da03e 7050 *field_type_p = type->field (i).type ();
52ce6436
PH
7051 if (byte_offset_p != NULL)
7052 *byte_offset_p = fld_offset;
7053 if (bit_offset_p != NULL)
7054 *bit_offset_p = bit_pos % 8;
7055 if (bit_size_p != NULL)
7056 *bit_size_p = bit_size;
dda83cd7
SM
7057 return 1;
7058 }
4c4b4cd2 7059 else if (ada_is_wrapper_field (type, i))
dda83cd7 7060 {
940da03e 7061 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7062 field_type_p, byte_offset_p, bit_offset_p,
7063 bit_size_p, index_p))
dda83cd7
SM
7064 return 1;
7065 }
4c4b4cd2 7066 else if (ada_is_variant_part (type, i))
dda83cd7 7067 {
52ce6436
PH
7068 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7069 fixed type?? */
dda83cd7
SM
7070 int j;
7071 struct type *field_type
940da03e 7072 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7073
dda83cd7
SM
7074 for (j = 0; j < field_type->num_fields (); j += 1)
7075 {
7076 if (find_struct_field (name, field_type->field (j).type (),
7077 fld_offset
b610c045 7078 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7079 field_type_p, byte_offset_p,
7080 bit_offset_p, bit_size_p, index_p))
7081 return 1;
7082 }
7083 }
52ce6436
PH
7084 else if (index_p != NULL)
7085 *index_p += 1;
4c4b4cd2 7086 }
828d5846
XR
7087
7088 /* Field not found so far. If this is a tagged type which
7089 has a parent, try finding that field in the parent now. */
7090
7091 if (parent_offset != -1)
7092 {
4d1795ac
TT
7093 /* As above, only compute the offset when truly needed. */
7094 int fld_offset = offset;
7095 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7096 {
b610c045 7097 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7098 fld_offset += bit_pos / 8;
7099 }
828d5846 7100
940da03e 7101 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7102 fld_offset, field_type_p, byte_offset_p,
7103 bit_offset_p, bit_size_p, index_p))
7104 return 1;
828d5846
XR
7105 }
7106
4c4b4cd2
PH
7107 return 0;
7108}
7109
0963b4bd 7110/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7111
52ce6436
PH
7112static int
7113num_visible_fields (struct type *type)
7114{
7115 int n;
5b4ee69b 7116
52ce6436
PH
7117 n = 0;
7118 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7119 return n;
7120}
14f9c5c9 7121
4c4b4cd2 7122/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7123 and search in it assuming it has (class) type TYPE.
7124 If found, return value, else return NULL.
7125
828d5846
XR
7126 Searches recursively through wrapper fields (e.g., '_parent').
7127
7128 In the case of homonyms in the tagged types, please refer to the
7129 long explanation in find_struct_field's function documentation. */
14f9c5c9 7130
4c4b4cd2 7131static struct value *
108d56a4 7132ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7133 struct type *type)
14f9c5c9
AS
7134{
7135 int i;
828d5846 7136 int parent_offset = -1;
14f9c5c9 7137
5b4ee69b 7138 type = ada_check_typedef (type);
1f704f76 7139 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7140 {
33d16dd9 7141 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7142
7143 if (t_field_name == NULL)
dda83cd7 7144 continue;
14f9c5c9 7145
828d5846 7146 else if (ada_is_parent_field (type, i))
dda83cd7 7147 {
828d5846
XR
7148 /* This is a field pointing us to the parent type of a tagged
7149 type. As hinted in this function's documentation, we give
7150 preference to fields in the current record first, so what
7151 we do here is just record the index of this field before
7152 we skip it. If it turns out we couldn't find our field
7153 in the current record, then we'll get back to it and search
7154 inside it whether the field might exist in the parent. */
7155
dda83cd7
SM
7156 parent_offset = i;
7157 continue;
7158 }
828d5846 7159
14f9c5c9 7160 else if (field_name_match (t_field_name, name))
dda83cd7 7161 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7162
7163 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7164 {
7165 struct value *v = /* Do not let indent join lines here. */
7166 ada_search_struct_field (name, arg,
b610c045 7167 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7168 type->field (i).type ());
5b4ee69b 7169
dda83cd7
SM
7170 if (v != NULL)
7171 return v;
7172 }
14f9c5c9
AS
7173
7174 else if (ada_is_variant_part (type, i))
dda83cd7 7175 {
0963b4bd 7176 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7177 int j;
7178 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7179 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7180
dda83cd7
SM
7181 for (j = 0; j < field_type->num_fields (); j += 1)
7182 {
7183 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7184 break. */
dda83cd7 7185 (name, arg,
b610c045 7186 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7187 field_type->field (j).type ());
5b4ee69b 7188
dda83cd7
SM
7189 if (v != NULL)
7190 return v;
7191 }
7192 }
14f9c5c9 7193 }
828d5846
XR
7194
7195 /* Field not found so far. If this is a tagged type which
7196 has a parent, try finding that field in the parent now. */
7197
7198 if (parent_offset != -1)
7199 {
7200 struct value *v = ada_search_struct_field (
b610c045 7201 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7202 type->field (parent_offset).type ());
828d5846
XR
7203
7204 if (v != NULL)
dda83cd7 7205 return v;
828d5846
XR
7206 }
7207
14f9c5c9
AS
7208 return NULL;
7209}
d2e4a39e 7210
52ce6436
PH
7211static struct value *ada_index_struct_field_1 (int *, struct value *,
7212 int, struct type *);
7213
7214
7215/* Return field #INDEX in ARG, where the index is that returned by
7216 * find_struct_field through its INDEX_P argument. Adjust the address
7217 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7218 * If found, return value, else return NULL. */
52ce6436
PH
7219
7220static struct value *
7221ada_index_struct_field (int index, struct value *arg, int offset,
7222 struct type *type)
7223{
7224 return ada_index_struct_field_1 (&index, arg, offset, type);
7225}
7226
7227
7228/* Auxiliary function for ada_index_struct_field. Like
7229 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7230 * *INDEX_P. */
52ce6436
PH
7231
7232static struct value *
7233ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7234 struct type *type)
7235{
7236 int i;
7237 type = ada_check_typedef (type);
7238
1f704f76 7239 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7240 {
33d16dd9 7241 if (type->field (i).name () == NULL)
dda83cd7 7242 continue;
52ce6436 7243 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7244 {
7245 struct value *v = /* Do not let indent join lines here. */
7246 ada_index_struct_field_1 (index_p, arg,
b610c045 7247 offset + type->field (i).loc_bitpos () / 8,
940da03e 7248 type->field (i).type ());
5b4ee69b 7249
dda83cd7
SM
7250 if (v != NULL)
7251 return v;
7252 }
52ce6436
PH
7253
7254 else if (ada_is_variant_part (type, i))
dda83cd7 7255 {
52ce6436 7256 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7257 find_struct_field. */
52ce6436 7258 error (_("Cannot assign this kind of variant record"));
dda83cd7 7259 }
52ce6436 7260 else if (*index_p == 0)
dda83cd7 7261 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7262 else
7263 *index_p -= 1;
7264 }
7265 return NULL;
7266}
7267
3b4de39c 7268/* Return a string representation of type TYPE. */
99bbb428 7269
3b4de39c 7270static std::string
99bbb428
PA
7271type_as_string (struct type *type)
7272{
d7e74731 7273 string_file tmp_stream;
99bbb428 7274
d7e74731 7275 type_print (type, "", &tmp_stream, -1);
99bbb428 7276
5d10a204 7277 return tmp_stream.release ();
99bbb428
PA
7278}
7279
14f9c5c9 7280/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7281 If DISPP is non-null, add its byte displacement from the beginning of a
7282 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7283 work for packed fields).
7284
7285 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7286 followed by "___".
14f9c5c9 7287
0963b4bd 7288 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7289 be a (pointer or reference)+ to a struct or union, and the
7290 ultimate target type will be searched.
14f9c5c9
AS
7291
7292 Looks recursively into variant clauses and parent types.
7293
828d5846
XR
7294 In the case of homonyms in the tagged types, please refer to the
7295 long explanation in find_struct_field's function documentation.
7296
4c4b4cd2
PH
7297 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7298 TYPE is not a type of the right kind. */
14f9c5c9 7299
4c4b4cd2 7300static struct type *
a121b7c1 7301ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7302 int noerr)
14f9c5c9
AS
7303{
7304 int i;
828d5846 7305 int parent_offset = -1;
14f9c5c9
AS
7306
7307 if (name == NULL)
7308 goto BadName;
7309
76a01679 7310 if (refok && type != NULL)
4c4b4cd2
PH
7311 while (1)
7312 {
dda83cd7
SM
7313 type = ada_check_typedef (type);
7314 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7315 break;
7316 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 7317 }
14f9c5c9 7318
76a01679 7319 if (type == NULL
78134374
SM
7320 || (type->code () != TYPE_CODE_STRUCT
7321 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7322 {
4c4b4cd2 7323 if (noerr)
dda83cd7 7324 return NULL;
99bbb428 7325
3b4de39c
PA
7326 error (_("Type %s is not a structure or union type"),
7327 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7328 }
7329
7330 type = to_static_fixed_type (type);
7331
1f704f76 7332 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7333 {
33d16dd9 7334 const char *t_field_name = type->field (i).name ();
14f9c5c9 7335 struct type *t;
d2e4a39e 7336
14f9c5c9 7337 if (t_field_name == NULL)
dda83cd7 7338 continue;
14f9c5c9 7339
828d5846 7340 else if (ada_is_parent_field (type, i))
dda83cd7 7341 {
828d5846
XR
7342 /* This is a field pointing us to the parent type of a tagged
7343 type. As hinted in this function's documentation, we give
7344 preference to fields in the current record first, so what
7345 we do here is just record the index of this field before
7346 we skip it. If it turns out we couldn't find our field
7347 in the current record, then we'll get back to it and search
7348 inside it whether the field might exist in the parent. */
7349
dda83cd7
SM
7350 parent_offset = i;
7351 continue;
7352 }
828d5846 7353
14f9c5c9 7354 else if (field_name_match (t_field_name, name))
940da03e 7355 return type->field (i).type ();
14f9c5c9
AS
7356
7357 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7358 {
7359 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7360 0, 1);
7361 if (t != NULL)
988f6b3d 7362 return t;
dda83cd7 7363 }
14f9c5c9
AS
7364
7365 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7366 {
7367 int j;
7368 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7369
dda83cd7
SM
7370 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7371 {
b1f33ddd 7372 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7373 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7374 generates these for unchecked variant types. Revisit
dda83cd7 7375 if the compiler changes this practice. */
33d16dd9 7376 const char *v_field_name = field_type->field (j).name ();
988f6b3d 7377
b1f33ddd
JB
7378 if (v_field_name != NULL
7379 && field_name_match (v_field_name, name))
940da03e 7380 t = field_type->field (j).type ();
b1f33ddd 7381 else
940da03e 7382 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7383 name, 0, 1);
b1f33ddd 7384
dda83cd7 7385 if (t != NULL)
988f6b3d 7386 return t;
dda83cd7
SM
7387 }
7388 }
14f9c5c9
AS
7389
7390 }
7391
828d5846
XR
7392 /* Field not found so far. If this is a tagged type which
7393 has a parent, try finding that field in the parent now. */
7394
7395 if (parent_offset != -1)
7396 {
dda83cd7 7397 struct type *t;
828d5846 7398
dda83cd7
SM
7399 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7400 name, 0, 1);
7401 if (t != NULL)
828d5846
XR
7402 return t;
7403 }
7404
14f9c5c9 7405BadName:
d2e4a39e 7406 if (!noerr)
14f9c5c9 7407 {
2b2798cc 7408 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7409
7410 error (_("Type %s has no component named %s"),
3b4de39c 7411 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7412 }
7413
7414 return NULL;
7415}
7416
b1f33ddd
JB
7417/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7418 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7419 represents an unchecked union (that is, the variant part of a
0963b4bd 7420 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7421
7422static int
7423is_unchecked_variant (struct type *var_type, struct type *outer_type)
7424{
a121b7c1 7425 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7426
988f6b3d 7427 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7428}
7429
7430
14f9c5c9 7431/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7432 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7433 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7434
d2e4a39e 7435int
d8af9068 7436ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7437{
7438 int others_clause;
7439 int i;
a121b7c1 7440 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7441 struct value *discrim;
14f9c5c9
AS
7442 LONGEST discrim_val;
7443
012370f6
TT
7444 /* Using plain value_from_contents_and_address here causes problems
7445 because we will end up trying to resolve a type that is currently
7446 being constructed. */
0c281816
JB
7447 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7448 if (discrim == NULL)
14f9c5c9 7449 return -1;
0c281816 7450 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7451
7452 others_clause = -1;
1f704f76 7453 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7454 {
7455 if (ada_is_others_clause (var_type, i))
dda83cd7 7456 others_clause = i;
14f9c5c9 7457 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7458 return i;
14f9c5c9
AS
7459 }
7460
7461 return others_clause;
7462}
d2e4a39e 7463\f
14f9c5c9
AS
7464
7465
dda83cd7 7466 /* Dynamic-Sized Records */
14f9c5c9
AS
7467
7468/* Strategy: The type ostensibly attached to a value with dynamic size
7469 (i.e., a size that is not statically recorded in the debugging
7470 data) does not accurately reflect the size or layout of the value.
7471 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7472 conventional types that are constructed on the fly. */
14f9c5c9
AS
7473
7474/* There is a subtle and tricky problem here. In general, we cannot
7475 determine the size of dynamic records without its data. However,
7476 the 'struct value' data structure, which GDB uses to represent
7477 quantities in the inferior process (the target), requires the size
7478 of the type at the time of its allocation in order to reserve space
7479 for GDB's internal copy of the data. That's why the
7480 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7481 rather than struct value*s.
14f9c5c9
AS
7482
7483 However, GDB's internal history variables ($1, $2, etc.) are
7484 struct value*s containing internal copies of the data that are not, in
7485 general, the same as the data at their corresponding addresses in
7486 the target. Fortunately, the types we give to these values are all
7487 conventional, fixed-size types (as per the strategy described
7488 above), so that we don't usually have to perform the
7489 'to_fixed_xxx_type' conversions to look at their values.
7490 Unfortunately, there is one exception: if one of the internal
7491 history variables is an array whose elements are unconstrained
7492 records, then we will need to create distinct fixed types for each
7493 element selected. */
7494
7495/* The upshot of all of this is that many routines take a (type, host
7496 address, target address) triple as arguments to represent a value.
7497 The host address, if non-null, is supposed to contain an internal
7498 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7499 target at the target address. */
14f9c5c9
AS
7500
7501/* Assuming that VAL0 represents a pointer value, the result of
7502 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7503 dynamic-sized types. */
14f9c5c9 7504
d2e4a39e
AS
7505struct value *
7506ada_value_ind (struct value *val0)
14f9c5c9 7507{
c48db5ca 7508 struct value *val = value_ind (val0);
5b4ee69b 7509
b50d69b5
JG
7510 if (ada_is_tagged_type (value_type (val), 0))
7511 val = ada_tag_value_at_base_address (val);
7512
4c4b4cd2 7513 return ada_to_fixed_value (val);
14f9c5c9
AS
7514}
7515
7516/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7517 qualifiers on VAL0. */
7518
d2e4a39e
AS
7519static struct value *
7520ada_coerce_ref (struct value *val0)
7521{
78134374 7522 if (value_type (val0)->code () == TYPE_CODE_REF)
d2e4a39e
AS
7523 {
7524 struct value *val = val0;
5b4ee69b 7525
994b9211 7526 val = coerce_ref (val);
b50d69b5
JG
7527
7528 if (ada_is_tagged_type (value_type (val), 0))
7529 val = ada_tag_value_at_base_address (val);
7530
4c4b4cd2 7531 return ada_to_fixed_value (val);
d2e4a39e
AS
7532 }
7533 else
14f9c5c9
AS
7534 return val0;
7535}
7536
4c4b4cd2 7537/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7538
7539static unsigned int
ebf56fd3 7540field_alignment (struct type *type, int f)
14f9c5c9 7541{
33d16dd9 7542 const char *name = type->field (f).name ();
64a1bf19 7543 int len;
14f9c5c9
AS
7544 int align_offset;
7545
64a1bf19
JB
7546 /* The field name should never be null, unless the debugging information
7547 is somehow malformed. In this case, we assume the field does not
7548 require any alignment. */
7549 if (name == NULL)
7550 return 1;
7551
7552 len = strlen (name);
7553
4c4b4cd2
PH
7554 if (!isdigit (name[len - 1]))
7555 return 1;
14f9c5c9 7556
d2e4a39e 7557 if (isdigit (name[len - 2]))
14f9c5c9
AS
7558 align_offset = len - 2;
7559 else
7560 align_offset = len - 1;
7561
61012eef 7562 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7563 return TARGET_CHAR_BIT;
7564
4c4b4cd2
PH
7565 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7566}
7567
852dff6c 7568/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7569
852dff6c
JB
7570static struct symbol *
7571ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7572{
7573 struct symbol *sym;
7574
7575 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
66d7f48f 7576 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7577 return sym;
7578
4186eb54
KS
7579 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7580 return sym;
14f9c5c9
AS
7581}
7582
dddfab26
UW
7583/* Find a type named NAME. Ignores ambiguity. This routine will look
7584 solely for types defined by debug info, it will not search the GDB
7585 primitive types. */
4c4b4cd2 7586
852dff6c 7587static struct type *
ebf56fd3 7588ada_find_any_type (const char *name)
14f9c5c9 7589{
852dff6c 7590 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7591
14f9c5c9 7592 if (sym != NULL)
5f9c5a63 7593 return sym->type ();
14f9c5c9 7594
dddfab26 7595 return NULL;
14f9c5c9
AS
7596}
7597
739593e0
JB
7598/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7599 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7600 symbol, in which case it is returned. Otherwise, this looks for
7601 symbols whose name is that of NAME_SYM suffixed with "___XR".
7602 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7603
c0e70c62
TT
7604static bool
7605ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7606{
987012b8 7607 const char *name = name_sym->linkage_name ();
c0e70c62 7608 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7609}
7610
14f9c5c9 7611/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7612 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7613 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7614 otherwise return 0. */
7615
14f9c5c9 7616int
d2e4a39e 7617ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7618{
7619 if (type1 == NULL)
7620 return 1;
7621 else if (type0 == NULL)
7622 return 0;
78134374 7623 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7624 return 1;
78134374 7625 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7626 return 0;
7d93a1e0 7627 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7628 return 1;
ad82864c 7629 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7630 return 1;
4c4b4cd2 7631 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7632 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7633 return 1;
aeb5907d
JB
7634 else
7635 {
7d93a1e0
SM
7636 const char *type0_name = type0->name ();
7637 const char *type1_name = type1->name ();
aeb5907d
JB
7638
7639 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7640 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7641 return 1;
7642 }
14f9c5c9
AS
7643 return 0;
7644}
7645
e86ca25f
TT
7646/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7647 null. */
4c4b4cd2 7648
0d5cff50 7649const char *
d2e4a39e 7650ada_type_name (struct type *type)
14f9c5c9 7651{
d2e4a39e 7652 if (type == NULL)
14f9c5c9 7653 return NULL;
7d93a1e0 7654 return type->name ();
14f9c5c9
AS
7655}
7656
b4ba55a1
JB
7657/* Search the list of "descriptive" types associated to TYPE for a type
7658 whose name is NAME. */
7659
7660static struct type *
7661find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7662{
931e5bc3 7663 struct type *result, *tmp;
b4ba55a1 7664
c6044dd1
JB
7665 if (ada_ignore_descriptive_types_p)
7666 return NULL;
7667
b4ba55a1
JB
7668 /* If there no descriptive-type info, then there is no parallel type
7669 to be found. */
7670 if (!HAVE_GNAT_AUX_INFO (type))
7671 return NULL;
7672
7673 result = TYPE_DESCRIPTIVE_TYPE (type);
7674 while (result != NULL)
7675 {
0d5cff50 7676 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7677
7678 if (result_name == NULL)
dda83cd7
SM
7679 {
7680 warning (_("unexpected null name on descriptive type"));
7681 return NULL;
7682 }
b4ba55a1
JB
7683
7684 /* If the names match, stop. */
7685 if (strcmp (result_name, name) == 0)
7686 break;
7687
7688 /* Otherwise, look at the next item on the list, if any. */
7689 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7690 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7691 else
7692 tmp = NULL;
7693
7694 /* If not found either, try after having resolved the typedef. */
7695 if (tmp != NULL)
7696 result = tmp;
b4ba55a1 7697 else
931e5bc3 7698 {
f168693b 7699 result = check_typedef (result);
931e5bc3
JG
7700 if (HAVE_GNAT_AUX_INFO (result))
7701 result = TYPE_DESCRIPTIVE_TYPE (result);
7702 else
7703 result = NULL;
7704 }
b4ba55a1
JB
7705 }
7706
7707 /* If we didn't find a match, see whether this is a packed array. With
7708 older compilers, the descriptive type information is either absent or
7709 irrelevant when it comes to packed arrays so the above lookup fails.
7710 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7711 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7712 return ada_find_any_type (name);
7713
7714 return result;
7715}
7716
7717/* Find a parallel type to TYPE with the specified NAME, using the
7718 descriptive type taken from the debugging information, if available,
7719 and otherwise using the (slower) name-based method. */
7720
7721static struct type *
7722ada_find_parallel_type_with_name (struct type *type, const char *name)
7723{
7724 struct type *result = NULL;
7725
7726 if (HAVE_GNAT_AUX_INFO (type))
7727 result = find_parallel_type_by_descriptive_type (type, name);
7728 else
7729 result = ada_find_any_type (name);
7730
7731 return result;
7732}
7733
7734/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7735 SUFFIX to the name of TYPE. */
14f9c5c9 7736
d2e4a39e 7737struct type *
ebf56fd3 7738ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7739{
0d5cff50 7740 char *name;
fe978cb0 7741 const char *type_name = ada_type_name (type);
14f9c5c9 7742 int len;
d2e4a39e 7743
fe978cb0 7744 if (type_name == NULL)
14f9c5c9
AS
7745 return NULL;
7746
fe978cb0 7747 len = strlen (type_name);
14f9c5c9 7748
b4ba55a1 7749 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7750
fe978cb0 7751 strcpy (name, type_name);
14f9c5c9
AS
7752 strcpy (name + len, suffix);
7753
b4ba55a1 7754 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7755}
7756
14f9c5c9 7757/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7758 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7759
d2e4a39e
AS
7760static struct type *
7761dynamic_template_type (struct type *type)
14f9c5c9 7762{
61ee279c 7763 type = ada_check_typedef (type);
14f9c5c9 7764
78134374 7765 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7766 || ada_type_name (type) == NULL)
14f9c5c9 7767 return NULL;
d2e4a39e 7768 else
14f9c5c9
AS
7769 {
7770 int len = strlen (ada_type_name (type));
5b4ee69b 7771
4c4b4cd2 7772 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7773 return type;
14f9c5c9 7774 else
dda83cd7 7775 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7776 }
7777}
7778
7779/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7780 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7781
d2e4a39e
AS
7782static int
7783is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7784{
33d16dd9 7785 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7786
d2e4a39e 7787 return name != NULL
940da03e 7788 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7789 && strstr (name, "___XVL") != NULL;
7790}
7791
4c4b4cd2
PH
7792/* The index of the variant field of TYPE, or -1 if TYPE does not
7793 represent a variant record type. */
14f9c5c9 7794
d2e4a39e 7795static int
4c4b4cd2 7796variant_field_index (struct type *type)
14f9c5c9
AS
7797{
7798 int f;
7799
78134374 7800 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7801 return -1;
7802
1f704f76 7803 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7804 {
7805 if (ada_is_variant_part (type, f))
dda83cd7 7806 return f;
4c4b4cd2
PH
7807 }
7808 return -1;
14f9c5c9
AS
7809}
7810
4c4b4cd2
PH
7811/* A record type with no fields. */
7812
d2e4a39e 7813static struct type *
fe978cb0 7814empty_record (struct type *templ)
14f9c5c9 7815{
fe978cb0 7816 struct type *type = alloc_type_copy (templ);
5b4ee69b 7817
67607e24 7818 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7819 INIT_NONE_SPECIFIC (type);
d0e39ea2 7820 type->set_name ("<empty>");
14f9c5c9
AS
7821 TYPE_LENGTH (type) = 0;
7822 return type;
7823}
7824
7825/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7826 the value of type TYPE at VALADDR or ADDRESS (see comments at
7827 the beginning of this section) VAL according to GNAT conventions.
7828 DVAL0 should describe the (portion of a) record that contains any
df407dfe 7829 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
7830 an outer-level type (i.e., as opposed to a branch of a variant.) A
7831 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7832 of the variant.
14f9c5c9 7833
4c4b4cd2
PH
7834 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7835 length are not statically known are discarded. As a consequence,
7836 VALADDR, ADDRESS and DVAL0 are ignored.
7837
7838 NOTE: Limitations: For now, we assume that dynamic fields and
7839 variants occupy whole numbers of bytes. However, they need not be
7840 byte-aligned. */
7841
7842struct type *
10a2c479 7843ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7844 const gdb_byte *valaddr,
dda83cd7
SM
7845 CORE_ADDR address, struct value *dval0,
7846 int keep_dynamic_fields)
14f9c5c9 7847{
d2e4a39e
AS
7848 struct value *mark = value_mark ();
7849 struct value *dval;
7850 struct type *rtype;
14f9c5c9 7851 int nfields, bit_len;
4c4b4cd2 7852 int variant_field;
14f9c5c9 7853 long off;
d94e4f4f 7854 int fld_bit_len;
14f9c5c9
AS
7855 int f;
7856
4c4b4cd2
PH
7857 /* Compute the number of fields in this record type that are going
7858 to be processed: unless keep_dynamic_fields, this includes only
7859 fields whose position and length are static will be processed. */
7860 if (keep_dynamic_fields)
1f704f76 7861 nfields = type->num_fields ();
4c4b4cd2
PH
7862 else
7863 {
7864 nfields = 0;
1f704f76 7865 while (nfields < type->num_fields ()
dda83cd7
SM
7866 && !ada_is_variant_part (type, nfields)
7867 && !is_dynamic_field (type, nfields))
7868 nfields++;
4c4b4cd2
PH
7869 }
7870
e9bb382b 7871 rtype = alloc_type_copy (type);
67607e24 7872 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7873 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7874 rtype->set_num_fields (nfields);
3cabb6b0
SM
7875 rtype->set_fields
7876 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7877 rtype->set_name (ada_type_name (type));
9cdd0d12 7878 rtype->set_is_fixed_instance (true);
14f9c5c9 7879
d2e4a39e
AS
7880 off = 0;
7881 bit_len = 0;
4c4b4cd2
PH
7882 variant_field = -1;
7883
14f9c5c9
AS
7884 for (f = 0; f < nfields; f += 1)
7885 {
a89febbd 7886 off = align_up (off, field_alignment (type, f))
b610c045 7887 + type->field (f).loc_bitpos ();
cd3f655c 7888 rtype->field (f).set_loc_bitpos (off);
d2e4a39e 7889 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7890
d2e4a39e 7891 if (ada_is_variant_part (type, f))
dda83cd7
SM
7892 {
7893 variant_field = f;
7894 fld_bit_len = 0;
7895 }
14f9c5c9 7896 else if (is_dynamic_field (type, f))
dda83cd7 7897 {
284614f0
JB
7898 const gdb_byte *field_valaddr = valaddr;
7899 CORE_ADDR field_address = address;
7900 struct type *field_type =
940da03e 7901 TYPE_TARGET_TYPE (type->field (f).type ());
284614f0 7902
dda83cd7 7903 if (dval0 == NULL)
b5304971 7904 {
012370f6
TT
7905 /* Using plain value_from_contents_and_address here
7906 causes problems because we will end up trying to
7907 resolve a type that is currently being
7908 constructed. */
7909 dval = value_from_contents_and_address_unresolved (rtype,
7910 valaddr,
7911 address);
9f1f738a 7912 rtype = value_type (dval);
b5304971 7913 }
dda83cd7
SM
7914 else
7915 dval = dval0;
4c4b4cd2 7916
284614f0
JB
7917 /* If the type referenced by this field is an aligner type, we need
7918 to unwrap that aligner type, because its size might not be set.
7919 Keeping the aligner type would cause us to compute the wrong
7920 size for this field, impacting the offset of the all the fields
7921 that follow this one. */
7922 if (ada_is_aligner_type (field_type))
7923 {
b610c045 7924 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7925
7926 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7927 field_address = cond_offset_target (field_address, field_offset);
7928 field_type = ada_aligned_type (field_type);
7929 }
7930
7931 field_valaddr = cond_offset_host (field_valaddr,
7932 off / TARGET_CHAR_BIT);
7933 field_address = cond_offset_target (field_address,
7934 off / TARGET_CHAR_BIT);
7935
7936 /* Get the fixed type of the field. Note that, in this case,
7937 we do not want to get the real type out of the tag: if
7938 the current field is the parent part of a tagged record,
7939 we will get the tag of the object. Clearly wrong: the real
7940 type of the parent is not the real type of the child. We
7941 would end up in an infinite loop. */
7942 field_type = ada_get_base_type (field_type);
7943 field_type = ada_to_fixed_type (field_type, field_valaddr,
7944 field_address, dval, 0);
7945
5d14b6e5 7946 rtype->field (f).set_type (field_type);
33d16dd9 7947 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7948 /* The multiplication can potentially overflow. But because
7949 the field length has been size-checked just above, and
7950 assuming that the maximum size is a reasonable value,
7951 an overflow should not happen in practice. So rather than
7952 adding overflow recovery code to this already complex code,
7953 we just assume that it's not going to happen. */
dda83cd7
SM
7954 fld_bit_len =
7955 TYPE_LENGTH (rtype->field (f).type ()) * TARGET_CHAR_BIT;
7956 }
14f9c5c9 7957 else
dda83cd7 7958 {
5ded5331
JB
7959 /* Note: If this field's type is a typedef, it is important
7960 to preserve the typedef layer.
7961
7962 Otherwise, we might be transforming a typedef to a fat
7963 pointer (encoding a pointer to an unconstrained array),
7964 into a basic fat pointer (encoding an unconstrained
7965 array). As both types are implemented using the same
7966 structure, the typedef is the only clue which allows us
7967 to distinguish between the two options. Stripping it
7968 would prevent us from printing this field appropriately. */
dda83cd7 7969 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7970 rtype->field (f).set_name (type->field (f).name ());
dda83cd7
SM
7971 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7972 fld_bit_len =
7973 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7974 else
5ded5331 7975 {
940da03e 7976 struct type *field_type = type->field (f).type ();
5ded5331
JB
7977
7978 /* We need to be careful of typedefs when computing
7979 the length of our field. If this is a typedef,
7980 get the length of the target type, not the length
7981 of the typedef. */
78134374 7982 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7983 field_type = ada_typedef_target_type (field_type);
7984
dda83cd7
SM
7985 fld_bit_len =
7986 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
5ded5331 7987 }
dda83cd7 7988 }
14f9c5c9 7989 if (off + fld_bit_len > bit_len)
dda83cd7 7990 bit_len = off + fld_bit_len;
d94e4f4f 7991 off += fld_bit_len;
4c4b4cd2 7992 TYPE_LENGTH (rtype) =
dda83cd7 7993 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7994 }
4c4b4cd2
PH
7995
7996 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7997 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7998 the record. This can happen in the presence of representation
7999 clauses. */
8000 if (variant_field >= 0)
8001 {
8002 struct type *branch_type;
8003
b610c045 8004 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
8005
8006 if (dval0 == NULL)
9f1f738a 8007 {
012370f6
TT
8008 /* Using plain value_from_contents_and_address here causes
8009 problems because we will end up trying to resolve a type
8010 that is currently being constructed. */
8011 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
8012 address);
9f1f738a
SA
8013 rtype = value_type (dval);
8014 }
4c4b4cd2 8015 else
dda83cd7 8016 dval = dval0;
4c4b4cd2
PH
8017
8018 branch_type =
dda83cd7
SM
8019 to_fixed_variant_branch_type
8020 (type->field (variant_field).type (),
8021 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
8022 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 8023 if (branch_type == NULL)
dda83cd7
SM
8024 {
8025 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
8026 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8027 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 8028 }
4c4b4cd2 8029 else
dda83cd7
SM
8030 {
8031 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8032 rtype->field (variant_field).set_name ("S");
dda83cd7
SM
8033 fld_bit_len =
8034 TYPE_LENGTH (rtype->field (variant_field).type ()) *
8035 TARGET_CHAR_BIT;
8036 if (off + fld_bit_len > bit_len)
8037 bit_len = off + fld_bit_len;
8038 TYPE_LENGTH (rtype) =
8039 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
8040 }
4c4b4cd2
PH
8041 }
8042
714e53ab
PH
8043 /* According to exp_dbug.ads, the size of TYPE for variable-size records
8044 should contain the alignment of that record, which should be a strictly
8045 positive value. If null or negative, then something is wrong, most
8046 probably in the debug info. In that case, we don't round up the size
0963b4bd 8047 of the resulting type. If this record is not part of another structure,
714e53ab
PH
8048 the current RTYPE length might be good enough for our purposes. */
8049 if (TYPE_LENGTH (type) <= 0)
8050 {
7d93a1e0 8051 if (rtype->name ())
cc1defb1 8052 warning (_("Invalid type size for `%s' detected: %s."),
7d93a1e0 8053 rtype->name (), pulongest (TYPE_LENGTH (type)));
323e0a4a 8054 else
cc1defb1
KS
8055 warning (_("Invalid type size for <unnamed> detected: %s."),
8056 pulongest (TYPE_LENGTH (type)));
714e53ab
PH
8057 }
8058 else
8059 {
a89febbd
TT
8060 TYPE_LENGTH (rtype) = align_up (TYPE_LENGTH (rtype),
8061 TYPE_LENGTH (type));
714e53ab 8062 }
14f9c5c9
AS
8063
8064 value_free_to_mark (mark);
14f9c5c9
AS
8065 return rtype;
8066}
8067
4c4b4cd2
PH
8068/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8069 of 1. */
14f9c5c9 8070
d2e4a39e 8071static struct type *
fc1a4b47 8072template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8073 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
8074{
8075 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 8076 address, dval0, 1);
4c4b4cd2
PH
8077}
8078
8079/* An ordinary record type in which ___XVL-convention fields and
8080 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8081 static approximations, containing all possible fields. Uses
8082 no runtime values. Useless for use in values, but that's OK,
8083 since the results are used only for type determinations. Works on both
8084 structs and unions. Representation note: to save space, we memorize
8085 the result of this function in the TYPE_TARGET_TYPE of the
8086 template type. */
8087
8088static struct type *
8089template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8090{
8091 struct type *type;
8092 int nfields;
8093 int f;
8094
9e195661 8095 /* No need no do anything if the input type is already fixed. */
22c4c60c 8096 if (type0->is_fixed_instance ())
9e195661
PMR
8097 return type0;
8098
8099 /* Likewise if we already have computed the static approximation. */
4c4b4cd2
PH
8100 if (TYPE_TARGET_TYPE (type0) != NULL)
8101 return TYPE_TARGET_TYPE (type0);
8102
9e195661 8103 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8104 type = type0;
1f704f76 8105 nfields = type0->num_fields ();
9e195661
PMR
8106
8107 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8108 recompute all over next time. */
8109 TYPE_TARGET_TYPE (type0) = type;
14f9c5c9
AS
8110
8111 for (f = 0; f < nfields; f += 1)
8112 {
940da03e 8113 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8114 struct type *new_type;
14f9c5c9 8115
4c4b4cd2 8116 if (is_dynamic_field (type0, f))
460efde1
JB
8117 {
8118 field_type = ada_check_typedef (field_type);
dda83cd7 8119 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
460efde1 8120 }
14f9c5c9 8121 else
dda83cd7 8122 new_type = static_unwrap_type (field_type);
9e195661
PMR
8123
8124 if (new_type != field_type)
8125 {
8126 /* Clone TYPE0 only the first time we get a new field type. */
8127 if (type == type0)
8128 {
8129 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
78134374 8130 type->set_code (type0->code ());
8ecb59f8 8131 INIT_NONE_SPECIFIC (type);
5e33d5f4 8132 type->set_num_fields (nfields);
3cabb6b0
SM
8133
8134 field *fields =
8135 ((struct field *)
8136 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 8137 memcpy (fields, type0->fields (),
9e195661 8138 sizeof (struct field) * nfields);
3cabb6b0
SM
8139 type->set_fields (fields);
8140
d0e39ea2 8141 type->set_name (ada_type_name (type0));
9cdd0d12 8142 type->set_is_fixed_instance (true);
9e195661
PMR
8143 TYPE_LENGTH (type) = 0;
8144 }
5d14b6e5 8145 type->field (f).set_type (new_type);
33d16dd9 8146 type->field (f).set_name (type0->field (f).name ());
9e195661 8147 }
14f9c5c9 8148 }
9e195661 8149
14f9c5c9
AS
8150 return type;
8151}
8152
4c4b4cd2 8153/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8154 whose address in memory is ADDRESS, returns a revision of TYPE,
8155 which should be a non-dynamic-sized record, in which the variant
8156 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8157 for discriminant values in DVAL0, which can be NULL if the record
8158 contains the necessary discriminant values. */
8159
d2e4a39e 8160static struct type *
fc1a4b47 8161to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8162 CORE_ADDR address, struct value *dval0)
14f9c5c9 8163{
d2e4a39e 8164 struct value *mark = value_mark ();
4c4b4cd2 8165 struct value *dval;
d2e4a39e 8166 struct type *rtype;
14f9c5c9 8167 struct type *branch_type;
1f704f76 8168 int nfields = type->num_fields ();
4c4b4cd2 8169 int variant_field = variant_field_index (type);
14f9c5c9 8170
4c4b4cd2 8171 if (variant_field == -1)
14f9c5c9
AS
8172 return type;
8173
4c4b4cd2 8174 if (dval0 == NULL)
9f1f738a
SA
8175 {
8176 dval = value_from_contents_and_address (type, valaddr, address);
8177 type = value_type (dval);
8178 }
4c4b4cd2
PH
8179 else
8180 dval = dval0;
8181
e9bb382b 8182 rtype = alloc_type_copy (type);
67607e24 8183 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8184 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 8185 rtype->set_num_fields (nfields);
3cabb6b0
SM
8186
8187 field *fields =
d2e4a39e 8188 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 8189 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
8190 rtype->set_fields (fields);
8191
d0e39ea2 8192 rtype->set_name (ada_type_name (type));
9cdd0d12 8193 rtype->set_is_fixed_instance (true);
14f9c5c9
AS
8194 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
8195
4c4b4cd2 8196 branch_type = to_fixed_variant_branch_type
940da03e 8197 (type->field (variant_field).type (),
d2e4a39e 8198 cond_offset_host (valaddr,
b610c045 8199 type->field (variant_field).loc_bitpos ()
dda83cd7 8200 / TARGET_CHAR_BIT),
d2e4a39e 8201 cond_offset_target (address,
b610c045 8202 type->field (variant_field).loc_bitpos ()
dda83cd7 8203 / TARGET_CHAR_BIT), dval);
d2e4a39e 8204 if (branch_type == NULL)
14f9c5c9 8205 {
4c4b4cd2 8206 int f;
5b4ee69b 8207
4c4b4cd2 8208 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8209 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8210 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8211 }
8212 else
8213 {
5d14b6e5 8214 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8215 rtype->field (variant_field).set_name ("S");
4c4b4cd2 8216 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 8217 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 8218 }
940da03e 8219 TYPE_LENGTH (rtype) -= TYPE_LENGTH (type->field (variant_field).type ());
d2e4a39e 8220
4c4b4cd2 8221 value_free_to_mark (mark);
14f9c5c9
AS
8222 return rtype;
8223}
8224
8225/* An ordinary record type (with fixed-length fields) that describes
8226 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8227 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8228 should be in DVAL, a record value; it may be NULL if the object
8229 at ADDR itself contains any necessary discriminant values.
8230 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8231 values from the record are needed. Except in the case that DVAL,
8232 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8233 unchecked) is replaced by a particular branch of the variant.
8234
8235 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8236 is questionable and may be removed. It can arise during the
8237 processing of an unconstrained-array-of-record type where all the
8238 variant branches have exactly the same size. This is because in
8239 such cases, the compiler does not bother to use the XVS convention
8240 when encoding the record. I am currently dubious of this
8241 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8242
d2e4a39e 8243static struct type *
fc1a4b47 8244to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8245 CORE_ADDR address, struct value *dval)
14f9c5c9 8246{
d2e4a39e 8247 struct type *templ_type;
14f9c5c9 8248
22c4c60c 8249 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8250 return type0;
8251
d2e4a39e 8252 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8253
8254 if (templ_type != NULL)
8255 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8256 else if (variant_field_index (type0) >= 0)
8257 {
8258 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8259 return type0;
4c4b4cd2 8260 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8261 dval);
4c4b4cd2 8262 }
14f9c5c9
AS
8263 else
8264 {
9cdd0d12 8265 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8266 return type0;
8267 }
8268
8269}
8270
8271/* An ordinary record type (with fixed-length fields) that describes
8272 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8273 union type. Any necessary discriminants' values should be in DVAL,
8274 a record value. That is, this routine selects the appropriate
8275 branch of the union at ADDR according to the discriminant value
b1f33ddd 8276 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8277 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8278
d2e4a39e 8279static struct type *
fc1a4b47 8280to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8281 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8282{
8283 int which;
d2e4a39e
AS
8284 struct type *templ_type;
8285 struct type *var_type;
14f9c5c9 8286
78134374 8287 if (var_type0->code () == TYPE_CODE_PTR)
14f9c5c9 8288 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 8289 else
14f9c5c9
AS
8290 var_type = var_type0;
8291
8292 templ_type = ada_find_parallel_type (var_type, "___XVU");
8293
8294 if (templ_type != NULL)
8295 var_type = templ_type;
8296
b1f33ddd
JB
8297 if (is_unchecked_variant (var_type, value_type (dval)))
8298 return var_type0;
d8af9068 8299 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8300
8301 if (which < 0)
e9bb382b 8302 return empty_record (var_type);
14f9c5c9 8303 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8304 return to_fixed_record_type
940da03e 8305 (TYPE_TARGET_TYPE (var_type->field (which).type ()),
d2e4a39e 8306 valaddr, address, dval);
940da03e 8307 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8308 return
8309 to_fixed_record_type
940da03e 8310 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8311 else
940da03e 8312 return var_type->field (which).type ();
14f9c5c9
AS
8313}
8314
8908fca5
JB
8315/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8316 ENCODING_TYPE, a type following the GNAT conventions for discrete
8317 type encodings, only carries redundant information. */
8318
8319static int
8320ada_is_redundant_range_encoding (struct type *range_type,
8321 struct type *encoding_type)
8322{
108d56a4 8323 const char *bounds_str;
8908fca5
JB
8324 int n;
8325 LONGEST lo, hi;
8326
78134374 8327 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8328
78134374
SM
8329 if (get_base_type (range_type)->code ()
8330 != get_base_type (encoding_type)->code ())
005e2509
JB
8331 {
8332 /* The compiler probably used a simple base type to describe
8333 the range type instead of the range's actual base type,
8334 expecting us to get the real base type from the encoding
8335 anyway. In this situation, the encoding cannot be ignored
8336 as redundant. */
8337 return 0;
8338 }
8339
8908fca5
JB
8340 if (is_dynamic_type (range_type))
8341 return 0;
8342
7d93a1e0 8343 if (encoding_type->name () == NULL)
8908fca5
JB
8344 return 0;
8345
7d93a1e0 8346 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8347 if (bounds_str == NULL)
8348 return 0;
8349
8350 n = 8; /* Skip "___XDLU_". */
8351 if (!ada_scan_number (bounds_str, n, &lo, &n))
8352 return 0;
5537ddd0 8353 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8354 return 0;
8355
8356 n += 2; /* Skip the "__" separator between the two bounds. */
8357 if (!ada_scan_number (bounds_str, n, &hi, &n))
8358 return 0;
5537ddd0 8359 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8360 return 0;
8361
8362 return 1;
8363}
8364
8365/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8366 a type following the GNAT encoding for describing array type
8367 indices, only carries redundant information. */
8368
8369static int
8370ada_is_redundant_index_type_desc (struct type *array_type,
8371 struct type *desc_type)
8372{
8373 struct type *this_layer = check_typedef (array_type);
8374 int i;
8375
1f704f76 8376 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8377 {
3d967001 8378 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8379 desc_type->field (i).type ()))
8908fca5
JB
8380 return 0;
8381 this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
8382 }
8383
8384 return 1;
8385}
8386
14f9c5c9
AS
8387/* Assuming that TYPE0 is an array type describing the type of a value
8388 at ADDR, and that DVAL describes a record containing any
8389 discriminants used in TYPE0, returns a type for the value that
8390 contains no dynamic components (that is, no components whose sizes
8391 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8392 true, gives an error message if the resulting type's size is over
4c4b4cd2 8393 varsize_limit. */
14f9c5c9 8394
d2e4a39e
AS
8395static struct type *
8396to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8397 int ignore_too_big)
14f9c5c9 8398{
d2e4a39e
AS
8399 struct type *index_type_desc;
8400 struct type *result;
ad82864c 8401 int constrained_packed_array_p;
931e5bc3 8402 static const char *xa_suffix = "___XA";
14f9c5c9 8403
b0dd7688 8404 type0 = ada_check_typedef (type0);
22c4c60c 8405 if (type0->is_fixed_instance ())
4c4b4cd2 8406 return type0;
14f9c5c9 8407
ad82864c
JB
8408 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8409 if (constrained_packed_array_p)
75fd6a26
TT
8410 {
8411 type0 = decode_constrained_packed_array_type (type0);
8412 if (type0 == nullptr)
8413 error (_("could not decode constrained packed array type"));
8414 }
284614f0 8415
931e5bc3
JG
8416 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8417
8418 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8419 encoding suffixed with 'P' may still be generated. If so,
8420 it should be used to find the XA type. */
8421
8422 if (index_type_desc == NULL)
8423 {
1da0522e 8424 const char *type_name = ada_type_name (type0);
931e5bc3 8425
1da0522e 8426 if (type_name != NULL)
931e5bc3 8427 {
1da0522e 8428 const int len = strlen (type_name);
931e5bc3
JG
8429 char *name = (char *) alloca (len + strlen (xa_suffix));
8430
1da0522e 8431 if (type_name[len - 1] == 'P')
931e5bc3 8432 {
1da0522e 8433 strcpy (name, type_name);
931e5bc3
JG
8434 strcpy (name + len - 1, xa_suffix);
8435 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8436 }
8437 }
8438 }
8439
28c85d6c 8440 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8441 if (index_type_desc != NULL
8442 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8443 {
8444 /* Ignore this ___XA parallel type, as it does not bring any
8445 useful information. This allows us to avoid creating fixed
8446 versions of the array's index types, which would be identical
8447 to the original ones. This, in turn, can also help avoid
8448 the creation of fixed versions of the array itself. */
8449 index_type_desc = NULL;
8450 }
8451
14f9c5c9
AS
8452 if (index_type_desc == NULL)
8453 {
61ee279c 8454 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 8455
14f9c5c9 8456 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8457 depend on the contents of the array in properly constructed
8458 debugging data. */
529cad9c 8459 /* Create a fixed version of the array element type.
dda83cd7
SM
8460 We're not providing the address of an element here,
8461 and thus the actual object value cannot be inspected to do
8462 the conversion. This should not be a problem, since arrays of
8463 unconstrained objects are not allowed. In particular, all
8464 the elements of an array of a tagged type should all be of
8465 the same type specified in the debugging info. No need to
8466 consult the object tag. */
1ed6ede0 8467 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8468
284614f0
JB
8469 /* Make sure we always create a new array type when dealing with
8470 packed array types, since we're going to fix-up the array
8471 type length and element bitsize a little further down. */
ad82864c 8472 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8473 result = type0;
14f9c5c9 8474 else
dda83cd7
SM
8475 result = create_array_type (alloc_type_copy (type0),
8476 elt_type, type0->index_type ());
14f9c5c9
AS
8477 }
8478 else
8479 {
8480 int i;
8481 struct type *elt_type0;
8482
8483 elt_type0 = type0;
1f704f76 8484 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
dda83cd7 8485 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8486
8487 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8488 depend on the contents of the array in properly constructed
8489 debugging data. */
529cad9c 8490 /* Create a fixed version of the array element type.
dda83cd7
SM
8491 We're not providing the address of an element here,
8492 and thus the actual object value cannot be inspected to do
8493 the conversion. This should not be a problem, since arrays of
8494 unconstrained objects are not allowed. In particular, all
8495 the elements of an array of a tagged type should all be of
8496 the same type specified in the debugging info. No need to
8497 consult the object tag. */
1ed6ede0 8498 result =
dda83cd7 8499 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8500
8501 elt_type0 = type0;
1f704f76 8502 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8503 {
8504 struct type *range_type =
8505 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8506
dda83cd7
SM
8507 result = create_array_type (alloc_type_copy (elt_type0),
8508 result, range_type);
1ce677a4 8509 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
dda83cd7 8510 }
14f9c5c9
AS
8511 }
8512
2e6fda7d
JB
8513 /* We want to preserve the type name. This can be useful when
8514 trying to get the type name of a value that has already been
8515 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8516 result->set_name (type0->name ());
2e6fda7d 8517
ad82864c 8518 if (constrained_packed_array_p)
284614f0
JB
8519 {
8520 /* So far, the resulting type has been created as if the original
8521 type was a regular (non-packed) array type. As a result, the
8522 bitsize of the array elements needs to be set again, and the array
8523 length needs to be recomputed based on that bitsize. */
8524 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
8525 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8526
8527 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
8528 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
8529 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
dda83cd7 8530 TYPE_LENGTH (result)++;
284614f0
JB
8531 }
8532
9cdd0d12 8533 result->set_is_fixed_instance (true);
14f9c5c9 8534 return result;
d2e4a39e 8535}
14f9c5c9
AS
8536
8537
8538/* A standard type (containing no dynamically sized components)
8539 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8540 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8541 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8542 ADDRESS or in VALADDR contains these discriminants.
8543
1ed6ede0
JB
8544 If CHECK_TAG is not null, in the case of tagged types, this function
8545 attempts to locate the object's tag and use it to compute the actual
8546 type. However, when ADDRESS is null, we cannot use it to determine the
8547 location of the tag, and therefore compute the tagged type's actual type.
8548 So we return the tagged type without consulting the tag. */
529cad9c 8549
f192137b
JB
8550static struct type *
8551ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8552 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8553{
61ee279c 8554 type = ada_check_typedef (type);
8ecb59f8
TT
8555
8556 /* Only un-fixed types need to be handled here. */
8557 if (!HAVE_GNAT_AUX_INFO (type))
8558 return type;
8559
78134374 8560 switch (type->code ())
d2e4a39e
AS
8561 {
8562 default:
14f9c5c9 8563 return type;
d2e4a39e 8564 case TYPE_CODE_STRUCT:
4c4b4cd2 8565 {
dda83cd7
SM
8566 struct type *static_type = to_static_fixed_type (type);
8567 struct type *fixed_record_type =
8568 to_fixed_record_type (type, valaddr, address, NULL);
8569
8570 /* If STATIC_TYPE is a tagged type and we know the object's address,
8571 then we can determine its tag, and compute the object's actual
8572 type from there. Note that we have to use the fixed record
8573 type (the parent part of the record may have dynamic fields
8574 and the way the location of _tag is expressed may depend on
8575 them). */
8576
8577 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8578 {
b50d69b5
JG
8579 struct value *tag =
8580 value_tag_from_contents_and_address
8581 (fixed_record_type,
8582 valaddr,
8583 address);
8584 struct type *real_type = type_from_tag (tag);
8585 struct value *obj =
8586 value_from_contents_and_address (fixed_record_type,
8587 valaddr,
8588 address);
dda83cd7
SM
8589 fixed_record_type = value_type (obj);
8590 if (real_type != NULL)
8591 return to_fixed_record_type
b50d69b5
JG
8592 (real_type, NULL,
8593 value_address (ada_tag_value_at_base_address (obj)), NULL);
dda83cd7
SM
8594 }
8595
8596 /* Check to see if there is a parallel ___XVZ variable.
8597 If there is, then it provides the actual size of our type. */
8598 else if (ada_type_name (fixed_record_type) != NULL)
8599 {
8600 const char *name = ada_type_name (fixed_record_type);
8601 char *xvz_name
224c3ddb 8602 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8603 bool xvz_found = false;
dda83cd7 8604 LONGEST size;
4af88198 8605
dda83cd7 8606 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8607 try
eccab96d
JB
8608 {
8609 xvz_found = get_int_var_value (xvz_name, size);
8610 }
230d2906 8611 catch (const gdb_exception_error &except)
eccab96d
JB
8612 {
8613 /* We found the variable, but somehow failed to read
8614 its value. Rethrow the same error, but with a little
8615 bit more information, to help the user understand
8616 what went wrong (Eg: the variable might have been
8617 optimized out). */
8618 throw_error (except.error,
8619 _("unable to read value of %s (%s)"),
3d6e9d23 8620 xvz_name, except.what ());
eccab96d 8621 }
eccab96d 8622
dda83cd7
SM
8623 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
8624 {
8625 fixed_record_type = copy_type (fixed_record_type);
8626 TYPE_LENGTH (fixed_record_type) = size;
8627
8628 /* The FIXED_RECORD_TYPE may have be a stub. We have
8629 observed this when the debugging info is STABS, and
8630 apparently it is something that is hard to fix.
8631
8632 In practice, we don't need the actual type definition
8633 at all, because the presence of the XVZ variable allows us
8634 to assume that there must be a XVS type as well, which we
8635 should be able to use later, when we need the actual type
8636 definition.
8637
8638 In the meantime, pretend that the "fixed" type we are
8639 returning is NOT a stub, because this can cause trouble
8640 when using this type to create new types targeting it.
8641 Indeed, the associated creation routines often check
8642 whether the target type is a stub and will try to replace
8643 it, thus using a type with the wrong size. This, in turn,
8644 might cause the new type to have the wrong size too.
8645 Consider the case of an array, for instance, where the size
8646 of the array is computed from the number of elements in
8647 our array multiplied by the size of its element. */
b4b73759 8648 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8649 }
8650 }
8651 return fixed_record_type;
4c4b4cd2 8652 }
d2e4a39e 8653 case TYPE_CODE_ARRAY:
4c4b4cd2 8654 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8655 case TYPE_CODE_UNION:
8656 if (dval == NULL)
dda83cd7 8657 return type;
d2e4a39e 8658 else
dda83cd7 8659 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8660 }
14f9c5c9
AS
8661}
8662
f192137b
JB
8663/* The same as ada_to_fixed_type_1, except that it preserves the type
8664 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8665
8666 The typedef layer needs be preserved in order to differentiate between
8667 arrays and array pointers when both types are implemented using the same
8668 fat pointer. In the array pointer case, the pointer is encoded as
8669 a typedef of the pointer type. For instance, considering:
8670
8671 type String_Access is access String;
8672 S1 : String_Access := null;
8673
8674 To the debugger, S1 is defined as a typedef of type String. But
8675 to the user, it is a pointer. So if the user tries to print S1,
8676 we should not dereference the array, but print the array address
8677 instead.
8678
8679 If we didn't preserve the typedef layer, we would lose the fact that
8680 the type is to be presented as a pointer (needs de-reference before
8681 being printed). And we would also use the source-level type name. */
f192137b
JB
8682
8683struct type *
8684ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8685 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8686
8687{
8688 struct type *fixed_type =
8689 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8690
96dbd2c1
JB
8691 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8692 then preserve the typedef layer.
8693
8694 Implementation note: We can only check the main-type portion of
8695 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8696 from TYPE now returns a type that has the same instance flags
8697 as TYPE. For instance, if TYPE is a "typedef const", and its
8698 target type is a "struct", then the typedef elimination will return
8699 a "const" version of the target type. See check_typedef for more
8700 details about how the typedef layer elimination is done.
8701
8702 brobecker/2010-11-19: It seems to me that the only case where it is
8703 useful to preserve the typedef layer is when dealing with fat pointers.
8704 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8705 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8706 because we call check_typedef/ada_check_typedef pretty much everywhere.
8707 */
78134374 8708 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8709 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8710 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8711 return type;
8712
8713 return fixed_type;
8714}
8715
14f9c5c9 8716/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8717 TYPE0, but based on no runtime data. */
14f9c5c9 8718
d2e4a39e
AS
8719static struct type *
8720to_static_fixed_type (struct type *type0)
14f9c5c9 8721{
d2e4a39e 8722 struct type *type;
14f9c5c9
AS
8723
8724 if (type0 == NULL)
8725 return NULL;
8726
22c4c60c 8727 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8728 return type0;
8729
61ee279c 8730 type0 = ada_check_typedef (type0);
d2e4a39e 8731
78134374 8732 switch (type0->code ())
14f9c5c9
AS
8733 {
8734 default:
8735 return type0;
8736 case TYPE_CODE_STRUCT:
8737 type = dynamic_template_type (type0);
d2e4a39e 8738 if (type != NULL)
dda83cd7 8739 return template_to_static_fixed_type (type);
4c4b4cd2 8740 else
dda83cd7 8741 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8742 case TYPE_CODE_UNION:
8743 type = ada_find_parallel_type (type0, "___XVU");
8744 if (type != NULL)
dda83cd7 8745 return template_to_static_fixed_type (type);
4c4b4cd2 8746 else
dda83cd7 8747 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8748 }
8749}
8750
4c4b4cd2
PH
8751/* A static approximation of TYPE with all type wrappers removed. */
8752
d2e4a39e
AS
8753static struct type *
8754static_unwrap_type (struct type *type)
14f9c5c9
AS
8755{
8756 if (ada_is_aligner_type (type))
8757 {
940da03e 8758 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8759 if (ada_type_name (type1) == NULL)
d0e39ea2 8760 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8761
8762 return static_unwrap_type (type1);
8763 }
d2e4a39e 8764 else
14f9c5c9 8765 {
d2e4a39e 8766 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8767
d2e4a39e 8768 if (raw_real_type == type)
dda83cd7 8769 return type;
14f9c5c9 8770 else
dda83cd7 8771 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8772 }
8773}
8774
8775/* In some cases, incomplete and private types require
4c4b4cd2 8776 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8777 type Foo;
8778 type FooP is access Foo;
8779 V: FooP;
8780 type Foo is array ...;
4c4b4cd2 8781 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8782 cross-references to such types, we instead substitute for FooP a
8783 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8784 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8785
8786/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8787 exists, otherwise TYPE. */
8788
d2e4a39e 8789struct type *
61ee279c 8790ada_check_typedef (struct type *type)
14f9c5c9 8791{
727e3d2e
JB
8792 if (type == NULL)
8793 return NULL;
8794
736ade86
XR
8795 /* If our type is an access to an unconstrained array, which is encoded
8796 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8797 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8798 what allows us to distinguish between fat pointers that represent
8799 array types, and fat pointers that represent array access types
8800 (in both cases, the compiler implements them as fat pointers). */
736ade86 8801 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8802 return type;
8803
f168693b 8804 type = check_typedef (type);
78134374 8805 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8806 || !type->is_stub ()
7d93a1e0 8807 || type->name () == NULL)
14f9c5c9 8808 return type;
d2e4a39e 8809 else
14f9c5c9 8810 {
7d93a1e0 8811 const char *name = type->name ();
d2e4a39e 8812 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8813
05e522ef 8814 if (type1 == NULL)
dda83cd7 8815 return type;
05e522ef
JB
8816
8817 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8818 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8819 types, only for the typedef-to-array types). If that's the case,
8820 strip the typedef layer. */
78134374 8821 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8822 type1 = ada_check_typedef (type1);
8823
8824 return type1;
14f9c5c9
AS
8825 }
8826}
8827
8828/* A value representing the data at VALADDR/ADDRESS as described by
8829 type TYPE0, but with a standard (static-sized) type that correctly
8830 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8831 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8832 creation of struct values]. */
14f9c5c9 8833
4c4b4cd2
PH
8834static struct value *
8835ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8836 struct value *val0)
14f9c5c9 8837{
1ed6ede0 8838 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8839
14f9c5c9
AS
8840 if (type == type0 && val0 != NULL)
8841 return val0;
cc0e770c
JB
8842
8843 if (VALUE_LVAL (val0) != lval_memory)
8844 {
8845 /* Our value does not live in memory; it could be a convenience
8846 variable, for instance. Create a not_lval value using val0's
8847 contents. */
50888e42 8848 return value_from_contents (type, value_contents (val0).data ());
cc0e770c
JB
8849 }
8850
8851 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8852}
8853
8854/* A value representing VAL, but with a standard (static-sized) type
8855 that correctly describes it. Does not necessarily create a new
8856 value. */
8857
0c3acc09 8858struct value *
4c4b4cd2
PH
8859ada_to_fixed_value (struct value *val)
8860{
c48db5ca 8861 val = unwrap_value (val);
d8ce9127 8862 val = ada_to_fixed_value_create (value_type (val), value_address (val), val);
c48db5ca 8863 return val;
14f9c5c9 8864}
d2e4a39e 8865\f
14f9c5c9 8866
14f9c5c9
AS
8867/* Attributes */
8868
4c4b4cd2
PH
8869/* Table mapping attribute numbers to names.
8870 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8871
27087b7f 8872static const char * const attribute_names[] = {
14f9c5c9
AS
8873 "<?>",
8874
d2e4a39e 8875 "first",
14f9c5c9
AS
8876 "last",
8877 "length",
8878 "image",
14f9c5c9
AS
8879 "max",
8880 "min",
4c4b4cd2
PH
8881 "modulus",
8882 "pos",
8883 "size",
8884 "tag",
14f9c5c9 8885 "val",
14f9c5c9
AS
8886 0
8887};
8888
de93309a 8889static const char *
4c4b4cd2 8890ada_attribute_name (enum exp_opcode n)
14f9c5c9 8891{
4c4b4cd2
PH
8892 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8893 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8894 else
8895 return attribute_names[0];
8896}
8897
4c4b4cd2 8898/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8899
4c4b4cd2
PH
8900static LONGEST
8901pos_atr (struct value *arg)
14f9c5c9 8902{
24209737
PH
8903 struct value *val = coerce_ref (arg);
8904 struct type *type = value_type (val);
14f9c5c9 8905
d2e4a39e 8906 if (!discrete_type_p (type))
323e0a4a 8907 error (_("'POS only defined on discrete types"));
14f9c5c9 8908
6244c119
SM
8909 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8910 if (!result.has_value ())
aa715135 8911 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8912
6244c119 8913 return *result;
4c4b4cd2
PH
8914}
8915
7631cf6c 8916struct value *
7992accc
TT
8917ada_pos_atr (struct type *expect_type,
8918 struct expression *exp,
8919 enum noside noside, enum exp_opcode op,
8920 struct value *arg)
4c4b4cd2 8921{
7992accc
TT
8922 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8923 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8924 return value_zero (type, not_lval);
3cb382c9 8925 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8926}
8927
4c4b4cd2 8928/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8929
d2e4a39e 8930static struct value *
53a47a3e 8931val_atr (struct type *type, LONGEST val)
14f9c5c9 8932{
53a47a3e 8933 gdb_assert (discrete_type_p (type));
0bc2354b
TT
8934 if (type->code () == TYPE_CODE_RANGE)
8935 type = TYPE_TARGET_TYPE (type);
78134374 8936 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8937 {
53a47a3e 8938 if (val < 0 || val >= type->num_fields ())
dda83cd7 8939 error (_("argument to 'VAL out of range"));
970db518 8940 val = type->field (val).loc_enumval ();
14f9c5c9 8941 }
53a47a3e
TT
8942 return value_from_longest (type, val);
8943}
8944
9e99f48f 8945struct value *
3848abd6 8946ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8947{
3848abd6
TT
8948 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8949 return value_zero (type, not_lval);
8950
53a47a3e
TT
8951 if (!discrete_type_p (type))
8952 error (_("'VAL only defined on discrete types"));
8953 if (!integer_type_p (value_type (arg)))
8954 error (_("'VAL requires integral argument"));
8955
8956 return val_atr (type, value_as_long (arg));
14f9c5c9 8957}
14f9c5c9 8958\f
d2e4a39e 8959
dda83cd7 8960 /* Evaluation */
14f9c5c9 8961
4c4b4cd2
PH
8962/* True if TYPE appears to be an Ada character type.
8963 [At the moment, this is true only for Character and Wide_Character;
8964 It is a heuristic test that could stand improvement]. */
14f9c5c9 8965
fc913e53 8966bool
d2e4a39e 8967ada_is_character_type (struct type *type)
14f9c5c9 8968{
7b9f71f2
JB
8969 const char *name;
8970
8971 /* If the type code says it's a character, then assume it really is,
8972 and don't check any further. */
78134374 8973 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8974 return true;
7b9f71f2
JB
8975
8976 /* Otherwise, assume it's a character type iff it is a discrete type
8977 with a known character type name. */
8978 name = ada_type_name (type);
8979 return (name != NULL
dda83cd7
SM
8980 && (type->code () == TYPE_CODE_INT
8981 || type->code () == TYPE_CODE_RANGE)
8982 && (strcmp (name, "character") == 0
8983 || strcmp (name, "wide_character") == 0
8984 || strcmp (name, "wide_wide_character") == 0
8985 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8986}
8987
4c4b4cd2 8988/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8989
fc913e53 8990bool
ebf56fd3 8991ada_is_string_type (struct type *type)
14f9c5c9 8992{
61ee279c 8993 type = ada_check_typedef (type);
d2e4a39e 8994 if (type != NULL
78134374 8995 && type->code () != TYPE_CODE_PTR
76a01679 8996 && (ada_is_simple_array_type (type)
dda83cd7 8997 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8998 && ada_array_arity (type) == 1)
8999 {
9000 struct type *elttype = ada_array_element_type (type, 1);
9001
9002 return ada_is_character_type (elttype);
9003 }
d2e4a39e 9004 else
fc913e53 9005 return false;
14f9c5c9
AS
9006}
9007
5bf03f13
JB
9008/* The compiler sometimes provides a parallel XVS type for a given
9009 PAD type. Normally, it is safe to follow the PAD type directly,
9010 but older versions of the compiler have a bug that causes the offset
9011 of its "F" field to be wrong. Following that field in that case
9012 would lead to incorrect results, but this can be worked around
9013 by ignoring the PAD type and using the associated XVS type instead.
9014
9015 Set to True if the debugger should trust the contents of PAD types.
9016 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 9017static bool trust_pad_over_xvs = true;
14f9c5c9
AS
9018
9019/* True if TYPE is a struct type introduced by the compiler to force the
9020 alignment of a value. Such types have a single field with a
4c4b4cd2 9021 distinctive name. */
14f9c5c9
AS
9022
9023int
ebf56fd3 9024ada_is_aligner_type (struct type *type)
14f9c5c9 9025{
61ee279c 9026 type = ada_check_typedef (type);
714e53ab 9027
5bf03f13 9028 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
9029 return 0;
9030
78134374 9031 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 9032 && type->num_fields () == 1
33d16dd9 9033 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
9034}
9035
9036/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 9037 the parallel type. */
14f9c5c9 9038
d2e4a39e
AS
9039struct type *
9040ada_get_base_type (struct type *raw_type)
14f9c5c9 9041{
d2e4a39e
AS
9042 struct type *real_type_namer;
9043 struct type *raw_real_type;
14f9c5c9 9044
78134374 9045 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
9046 return raw_type;
9047
284614f0
JB
9048 if (ada_is_aligner_type (raw_type))
9049 /* The encoding specifies that we should always use the aligner type.
9050 So, even if this aligner type has an associated XVS type, we should
9051 simply ignore it.
9052
9053 According to the compiler gurus, an XVS type parallel to an aligner
9054 type may exist because of a stabs limitation. In stabs, aligner
9055 types are empty because the field has a variable-sized type, and
9056 thus cannot actually be used as an aligner type. As a result,
9057 we need the associated parallel XVS type to decode the type.
9058 Since the policy in the compiler is to not change the internal
9059 representation based on the debugging info format, we sometimes
9060 end up having a redundant XVS type parallel to the aligner type. */
9061 return raw_type;
9062
14f9c5c9 9063 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9064 if (real_type_namer == NULL
78134374 9065 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 9066 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
9067 return raw_type;
9068
940da03e 9069 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
9070 {
9071 /* This is an older encoding form where the base type needs to be
85102364 9072 looked up by name. We prefer the newer encoding because it is
f80d3ff2 9073 more efficient. */
33d16dd9 9074 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
9075 if (raw_real_type == NULL)
9076 return raw_type;
9077 else
9078 return raw_real_type;
9079 }
9080
9081 /* The field in our XVS type is a reference to the base type. */
940da03e 9082 return TYPE_TARGET_TYPE (real_type_namer->field (0).type ());
d2e4a39e 9083}
14f9c5c9 9084
4c4b4cd2 9085/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9086
d2e4a39e
AS
9087struct type *
9088ada_aligned_type (struct type *type)
14f9c5c9
AS
9089{
9090 if (ada_is_aligner_type (type))
940da03e 9091 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
9092 else
9093 return ada_get_base_type (type);
9094}
9095
9096
9097/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9098 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9099
fc1a4b47
AC
9100const gdb_byte *
9101ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9102{
d2e4a39e 9103 if (ada_is_aligner_type (type))
b610c045
SM
9104 return ada_aligned_value_addr
9105 (type->field (0).type (),
9106 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
9107 else
9108 return valaddr;
9109}
9110
4c4b4cd2
PH
9111
9112
14f9c5c9 9113/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9114 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9115const char *
9116ada_enum_name (const char *name)
14f9c5c9 9117{
5f9febe0 9118 static std::string storage;
e6a959d6 9119 const char *tmp;
14f9c5c9 9120
4c4b4cd2
PH
9121 /* First, unqualify the enumeration name:
9122 1. Search for the last '.' character. If we find one, then skip
177b42fe 9123 all the preceding characters, the unqualified name starts
76a01679 9124 right after that dot.
4c4b4cd2 9125 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9126 translates dots into "__". Search forward for double underscores,
9127 but stop searching when we hit an overloading suffix, which is
9128 of the form "__" followed by digits. */
4c4b4cd2 9129
c3e5cd34
PH
9130 tmp = strrchr (name, '.');
9131 if (tmp != NULL)
4c4b4cd2
PH
9132 name = tmp + 1;
9133 else
14f9c5c9 9134 {
4c4b4cd2 9135 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9136 {
9137 if (isdigit (tmp[2]))
9138 break;
9139 else
9140 name = tmp + 2;
9141 }
14f9c5c9
AS
9142 }
9143
9144 if (name[0] == 'Q')
9145 {
14f9c5c9 9146 int v;
5b4ee69b 9147
14f9c5c9 9148 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9149 {
a7041de8
TT
9150 int offset = 2;
9151 if (name[1] == 'W' && name[2] == 'W')
9152 {
9153 /* Also handle the QWW case. */
9154 ++offset;
9155 }
9156 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9157 return name;
9158 }
272560b5
TT
9159 else if (((name[1] >= '0' && name[1] <= '9')
9160 || (name[1] >= 'a' && name[1] <= 'z'))
9161 && name[2] == '\0')
9162 {
5f9febe0
TT
9163 storage = string_printf ("'%c'", name[1]);
9164 return storage.c_str ();
272560b5 9165 }
14f9c5c9 9166 else
dda83cd7 9167 return name;
14f9c5c9
AS
9168
9169 if (isascii (v) && isprint (v))
5f9febe0 9170 storage = string_printf ("'%c'", v);
14f9c5c9 9171 else if (name[1] == 'U')
a7041de8
TT
9172 storage = string_printf ("'[\"%02x\"]'", v);
9173 else if (name[2] != 'W')
9174 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9175 else
a7041de8 9176 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9177
5f9febe0 9178 return storage.c_str ();
14f9c5c9 9179 }
d2e4a39e 9180 else
4c4b4cd2 9181 {
c3e5cd34
PH
9182 tmp = strstr (name, "__");
9183 if (tmp == NULL)
9184 tmp = strstr (name, "$");
9185 if (tmp != NULL)
dda83cd7 9186 {
5f9febe0
TT
9187 storage = std::string (name, tmp - name);
9188 return storage.c_str ();
dda83cd7 9189 }
4c4b4cd2
PH
9190
9191 return name;
9192 }
14f9c5c9
AS
9193}
9194
14f9c5c9 9195/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9196 value it wraps. */
14f9c5c9 9197
d2e4a39e
AS
9198static struct value *
9199unwrap_value (struct value *val)
14f9c5c9 9200{
df407dfe 9201 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 9202
14f9c5c9
AS
9203 if (ada_is_aligner_type (type))
9204 {
de4d072f 9205 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 9206 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 9207
14f9c5c9 9208 if (ada_type_name (val_type) == NULL)
d0e39ea2 9209 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9210
9211 return unwrap_value (v);
9212 }
d2e4a39e 9213 else
14f9c5c9 9214 {
d2e4a39e 9215 struct type *raw_real_type =
dda83cd7 9216 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 9217
5bf03f13
JB
9218 /* If there is no parallel XVS or XVE type, then the value is
9219 already unwrapped. Return it without further modification. */
9220 if ((type == raw_real_type)
9221 && ada_find_parallel_type (type, "___XVE") == NULL)
9222 return val;
14f9c5c9 9223
d2e4a39e 9224 return
dda83cd7
SM
9225 coerce_unspec_val_to_type
9226 (val, ada_to_fixed_type (raw_real_type, 0,
9227 value_address (val),
9228 NULL, 1));
14f9c5c9
AS
9229 }
9230}
d2e4a39e 9231
d99dcf51
JB
9232/* Given two array types T1 and T2, return nonzero iff both arrays
9233 contain the same number of elements. */
9234
9235static int
9236ada_same_array_size_p (struct type *t1, struct type *t2)
9237{
9238 LONGEST lo1, hi1, lo2, hi2;
9239
9240 /* Get the array bounds in order to verify that the size of
9241 the two arrays match. */
9242 if (!get_array_bounds (t1, &lo1, &hi1)
9243 || !get_array_bounds (t2, &lo2, &hi2))
9244 error (_("unable to determine array bounds"));
9245
9246 /* To make things easier for size comparison, normalize a bit
9247 the case of empty arrays by making sure that the difference
9248 between upper bound and lower bound is always -1. */
9249 if (lo1 > hi1)
9250 hi1 = lo1 - 1;
9251 if (lo2 > hi2)
9252 hi2 = lo2 - 1;
9253
9254 return (hi1 - lo1 == hi2 - lo2);
9255}
9256
9257/* Assuming that VAL is an array of integrals, and TYPE represents
9258 an array with the same number of elements, but with wider integral
9259 elements, return an array "casted" to TYPE. In practice, this
9260 means that the returned array is built by casting each element
9261 of the original array into TYPE's (wider) element type. */
9262
9263static struct value *
9264ada_promote_array_of_integrals (struct type *type, struct value *val)
9265{
9266 struct type *elt_type = TYPE_TARGET_TYPE (type);
9267 LONGEST lo, hi;
d99dcf51
JB
9268 LONGEST i;
9269
9270 /* Verify that both val and type are arrays of scalars, and
9271 that the size of val's elements is smaller than the size
9272 of type's element. */
78134374 9273 gdb_assert (type->code () == TYPE_CODE_ARRAY);
d99dcf51 9274 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
78134374 9275 gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY);
d99dcf51
JB
9276 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
9277 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
9278 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
9279
9280 if (!get_array_bounds (type, &lo, &hi))
9281 error (_("unable to determine array bounds"));
9282
4bce7cda
SM
9283 value *res = allocate_value (type);
9284 gdb::array_view<gdb_byte> res_contents = value_contents_writeable (res);
d99dcf51
JB
9285
9286 /* Promote each array element. */
9287 for (i = 0; i < hi - lo + 1; i++)
9288 {
9289 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
4bce7cda 9290 int elt_len = TYPE_LENGTH (elt_type);
d99dcf51 9291
4bce7cda 9292 copy (value_contents_all (elt), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9293 }
9294
9295 return res;
9296}
9297
4c4b4cd2
PH
9298/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9299 return the converted value. */
9300
d2e4a39e
AS
9301static struct value *
9302coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9303{
df407dfe 9304 struct type *type2 = value_type (val);
5b4ee69b 9305
14f9c5c9
AS
9306 if (type == type2)
9307 return val;
9308
61ee279c
PH
9309 type2 = ada_check_typedef (type2);
9310 type = ada_check_typedef (type);
14f9c5c9 9311
78134374
SM
9312 if (type2->code () == TYPE_CODE_PTR
9313 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9314 {
9315 val = ada_value_ind (val);
df407dfe 9316 type2 = value_type (val);
14f9c5c9
AS
9317 }
9318
78134374
SM
9319 if (type2->code () == TYPE_CODE_ARRAY
9320 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9321 {
d99dcf51
JB
9322 if (!ada_same_array_size_p (type, type2))
9323 error (_("cannot assign arrays of different length"));
9324
9325 if (is_integral_type (TYPE_TARGET_TYPE (type))
9326 && is_integral_type (TYPE_TARGET_TYPE (type2))
9327 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
9328 < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9329 {
9330 /* Allow implicit promotion of the array elements to
9331 a wider type. */
9332 return ada_promote_array_of_integrals (type, val);
9333 }
9334
9335 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
dda83cd7
SM
9336 != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9337 error (_("Incompatible types in assignment"));
04624583 9338 deprecated_set_value_type (val, type);
14f9c5c9 9339 }
d2e4a39e 9340 return val;
14f9c5c9
AS
9341}
9342
4c4b4cd2
PH
9343static struct value *
9344ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9345{
9346 struct value *val;
9347 struct type *type1, *type2;
9348 LONGEST v, v1, v2;
9349
994b9211
AC
9350 arg1 = coerce_ref (arg1);
9351 arg2 = coerce_ref (arg2);
18af8284
JB
9352 type1 = get_base_type (ada_check_typedef (value_type (arg1)));
9353 type2 = get_base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 9354
78134374
SM
9355 if (type1->code () != TYPE_CODE_INT
9356 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9357 return value_binop (arg1, arg2, op);
9358
76a01679 9359 switch (op)
4c4b4cd2
PH
9360 {
9361 case BINOP_MOD:
9362 case BINOP_DIV:
9363 case BINOP_REM:
9364 break;
9365 default:
9366 return value_binop (arg1, arg2, op);
9367 }
9368
9369 v2 = value_as_long (arg2);
9370 if (v2 == 0)
b0f9164c
TT
9371 {
9372 const char *name;
9373 if (op == BINOP_MOD)
9374 name = "mod";
9375 else if (op == BINOP_DIV)
9376 name = "/";
9377 else
9378 {
9379 gdb_assert (op == BINOP_REM);
9380 name = "rem";
9381 }
9382
9383 error (_("second operand of %s must not be zero."), name);
9384 }
4c4b4cd2 9385
c6d940a9 9386 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9387 return value_binop (arg1, arg2, op);
9388
9389 v1 = value_as_long (arg1);
9390 switch (op)
9391 {
9392 case BINOP_DIV:
9393 v = v1 / v2;
76a01679 9394 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
dda83cd7 9395 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9396 break;
9397 case BINOP_REM:
9398 v = v1 % v2;
76a01679 9399 if (v * v1 < 0)
dda83cd7 9400 v -= v2;
4c4b4cd2
PH
9401 break;
9402 default:
9403 /* Should not reach this point. */
9404 v = 0;
9405 }
9406
9407 val = allocate_value (type1);
50888e42 9408 store_unsigned_integer (value_contents_raw (val).data (),
dda83cd7 9409 TYPE_LENGTH (value_type (val)),
34877895 9410 type_byte_order (type1), v);
4c4b4cd2
PH
9411 return val;
9412}
9413
9414static int
9415ada_value_equal (struct value *arg1, struct value *arg2)
9416{
df407dfe
AC
9417 if (ada_is_direct_array_type (value_type (arg1))
9418 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 9419 {
79e8fcaa
JB
9420 struct type *arg1_type, *arg2_type;
9421
f58b38bf 9422 /* Automatically dereference any array reference before
dda83cd7 9423 we attempt to perform the comparison. */
f58b38bf
JB
9424 arg1 = ada_coerce_ref (arg1);
9425 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9426
4c4b4cd2
PH
9427 arg1 = ada_coerce_to_simple_array (arg1);
9428 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa
JB
9429
9430 arg1_type = ada_check_typedef (value_type (arg1));
9431 arg2_type = ada_check_typedef (value_type (arg2));
9432
78134374 9433 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9434 || arg2_type->code () != TYPE_CODE_ARRAY)
9435 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9436 /* FIXME: The following works only for types whose
dda83cd7
SM
9437 representations use all bits (no padding or undefined bits)
9438 and do not have user-defined equality. */
79e8fcaa 9439 return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
50888e42
SM
9440 && memcmp (value_contents (arg1).data (),
9441 value_contents (arg2).data (),
79e8fcaa 9442 TYPE_LENGTH (arg1_type)) == 0);
4c4b4cd2
PH
9443 }
9444 return value_equal (arg1, arg2);
9445}
9446
d3c54a1c
TT
9447namespace expr
9448{
9449
9450bool
9451check_objfile (const std::unique_ptr<ada_component> &comp,
9452 struct objfile *objfile)
9453{
9454 return comp->uses_objfile (objfile);
9455}
9456
9457/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9458 component of LHS (a simple array or a record). Does not modify the
9459 inferior's memory, nor does it modify LHS (unless LHS ==
9460 CONTAINER). */
52ce6436
PH
9461
9462static void
9463assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9464 struct expression *exp, operation_up &arg)
52ce6436 9465{
d3c54a1c
TT
9466 scoped_value_mark mark;
9467
52ce6436 9468 struct value *elt;
0e2da9f0 9469 struct type *lhs_type = check_typedef (value_type (lhs));
5b4ee69b 9470
78134374 9471 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9472 {
22601c15
UW
9473 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9474 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9475
52ce6436
PH
9476 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9477 }
9478 else
9479 {
9480 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
c48db5ca 9481 elt = ada_to_fixed_value (elt);
52ce6436
PH
9482 }
9483
d3c54a1c
TT
9484 ada_aggregate_operation *ag_op
9485 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9486 if (ag_op != nullptr)
9487 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9488 else
d3c54a1c
TT
9489 value_assign_to_component (container, elt,
9490 arg->evaluate (nullptr, exp,
9491 EVAL_NORMAL));
9492}
52ce6436 9493
d3c54a1c
TT
9494bool
9495ada_aggregate_component::uses_objfile (struct objfile *objfile)
9496{
9497 for (const auto &item : m_components)
9498 if (item->uses_objfile (objfile))
9499 return true;
9500 return false;
9501}
9502
9503void
9504ada_aggregate_component::dump (ui_file *stream, int depth)
9505{
6cb06a8c 9506 gdb_printf (stream, _("%*sAggregate\n"), depth, "");
d3c54a1c
TT
9507 for (const auto &item : m_components)
9508 item->dump (stream, depth + 1);
9509}
9510
9511void
9512ada_aggregate_component::assign (struct value *container,
9513 struct value *lhs, struct expression *exp,
9514 std::vector<LONGEST> &indices,
9515 LONGEST low, LONGEST high)
9516{
9517 for (auto &item : m_components)
9518 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9519}
9520
207582c0 9521/* See ada-exp.h. */
52ce6436 9522
207582c0 9523value *
d3c54a1c
TT
9524ada_aggregate_operation::assign_aggregate (struct value *container,
9525 struct value *lhs,
9526 struct expression *exp)
52ce6436
PH
9527{
9528 struct type *lhs_type;
52ce6436 9529 LONGEST low_index, high_index;
52ce6436
PH
9530
9531 container = ada_coerce_ref (container);
9532 if (ada_is_direct_array_type (value_type (container)))
9533 container = ada_coerce_to_simple_array (container);
9534 lhs = ada_coerce_ref (lhs);
9535 if (!deprecated_value_modifiable (lhs))
9536 error (_("Left operand of assignment is not a modifiable lvalue."));
9537
0e2da9f0 9538 lhs_type = check_typedef (value_type (lhs));
52ce6436
PH
9539 if (ada_is_direct_array_type (lhs_type))
9540 {
9541 lhs = ada_coerce_to_simple_array (lhs);
0e2da9f0 9542 lhs_type = check_typedef (value_type (lhs));
cf88be68
SM
9543 low_index = lhs_type->bounds ()->low.const_val ();
9544 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9545 }
78134374 9546 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9547 {
9548 low_index = 0;
9549 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9550 }
9551 else
9552 error (_("Left-hand side must be array or record."));
9553
cf608cc4 9554 std::vector<LONGEST> indices (4);
52ce6436
PH
9555 indices[0] = indices[1] = low_index - 1;
9556 indices[2] = indices[3] = high_index + 1;
52ce6436 9557
d3c54a1c
TT
9558 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9559 low_index, high_index);
207582c0
TT
9560
9561 return container;
d3c54a1c
TT
9562}
9563
9564bool
9565ada_positional_component::uses_objfile (struct objfile *objfile)
9566{
9567 return m_op->uses_objfile (objfile);
9568}
52ce6436 9569
d3c54a1c
TT
9570void
9571ada_positional_component::dump (ui_file *stream, int depth)
9572{
6cb06a8c
TT
9573 gdb_printf (stream, _("%*sPositional, index = %d\n"),
9574 depth, "", m_index);
d3c54a1c 9575 m_op->dump (stream, depth + 1);
52ce6436 9576}
d3c54a1c 9577
52ce6436 9578/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9579 construct, given that the positions are relative to lower bound
9580 LOW, where HIGH is the upper bound. Record the position in
9581 INDICES. CONTAINER is as for assign_aggregate. */
9582void
9583ada_positional_component::assign (struct value *container,
9584 struct value *lhs, struct expression *exp,
9585 std::vector<LONGEST> &indices,
9586 LONGEST low, LONGEST high)
52ce6436 9587{
d3c54a1c
TT
9588 LONGEST ind = m_index + low;
9589
52ce6436 9590 if (ind - 1 == high)
e1d5a0d2 9591 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9592 if (ind <= high)
9593 {
cf608cc4 9594 add_component_interval (ind, ind, indices);
d3c54a1c 9595 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9596 }
52ce6436
PH
9597}
9598
d3c54a1c
TT
9599bool
9600ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9601{
9602 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9603}
9604
9605void
9606ada_discrete_range_association::dump (ui_file *stream, int depth)
9607{
6cb06a8c 9608 gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
a88c4354
TT
9609 m_low->dump (stream, depth + 1);
9610 m_high->dump (stream, depth + 1);
9611}
9612
9613void
9614ada_discrete_range_association::assign (struct value *container,
9615 struct value *lhs,
9616 struct expression *exp,
9617 std::vector<LONGEST> &indices,
9618 LONGEST low, LONGEST high,
9619 operation_up &op)
9620{
9621 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9622 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9623
9624 if (lower <= upper && (lower < low || upper > high))
9625 error (_("Index in component association out of bounds."));
9626
9627 add_component_interval (lower, upper, indices);
9628 while (lower <= upper)
9629 {
9630 assign_component (container, lhs, lower, exp, op);
9631 lower += 1;
9632 }
9633}
9634
9635bool
9636ada_name_association::uses_objfile (struct objfile *objfile)
9637{
9638 return m_val->uses_objfile (objfile);
9639}
9640
9641void
9642ada_name_association::dump (ui_file *stream, int depth)
9643{
6cb06a8c 9644 gdb_printf (stream, _("%*sName:\n"), depth, "");
a88c4354
TT
9645 m_val->dump (stream, depth + 1);
9646}
9647
9648void
9649ada_name_association::assign (struct value *container,
9650 struct value *lhs,
9651 struct expression *exp,
9652 std::vector<LONGEST> &indices,
9653 LONGEST low, LONGEST high,
9654 operation_up &op)
9655{
9656 int index;
9657
9658 if (ada_is_direct_array_type (value_type (lhs)))
9659 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9660 EVAL_NORMAL)));
9661 else
9662 {
9663 ada_string_operation *strop
9664 = dynamic_cast<ada_string_operation *> (m_val.get ());
9665
9666 const char *name;
9667 if (strop != nullptr)
9668 name = strop->get_name ();
9669 else
9670 {
9671 ada_var_value_operation *vvo
9672 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
9673 if (vvo != nullptr)
9674 error (_("Invalid record component association."));
9675 name = vvo->get_symbol ()->natural_name ();
9676 }
9677
9678 index = 0;
9679 if (! find_struct_field (name, value_type (lhs), 0,
9680 NULL, NULL, NULL, NULL, &index))
9681 error (_("Unknown component name: %s."), name);
9682 }
9683
9684 add_component_interval (index, index, indices);
9685 assign_component (container, lhs, index, exp, op);
9686}
9687
9688bool
9689ada_choices_component::uses_objfile (struct objfile *objfile)
9690{
9691 if (m_op->uses_objfile (objfile))
9692 return true;
9693 for (const auto &item : m_assocs)
9694 if (item->uses_objfile (objfile))
9695 return true;
9696 return false;
9697}
9698
9699void
9700ada_choices_component::dump (ui_file *stream, int depth)
9701{
6cb06a8c 9702 gdb_printf (stream, _("%*sChoices:\n"), depth, "");
a88c4354
TT
9703 m_op->dump (stream, depth + 1);
9704 for (const auto &item : m_assocs)
9705 item->dump (stream, depth + 1);
9706}
9707
9708/* Assign into the components of LHS indexed by the OP_CHOICES
9709 construct at *POS, updating *POS past the construct, given that
9710 the allowable indices are LOW..HIGH. Record the indices assigned
9711 to in INDICES. CONTAINER is as for assign_aggregate. */
9712void
9713ada_choices_component::assign (struct value *container,
9714 struct value *lhs, struct expression *exp,
9715 std::vector<LONGEST> &indices,
9716 LONGEST low, LONGEST high)
9717{
9718 for (auto &item : m_assocs)
9719 item->assign (container, lhs, exp, indices, low, high, m_op);
9720}
9721
9722bool
9723ada_others_component::uses_objfile (struct objfile *objfile)
9724{
9725 return m_op->uses_objfile (objfile);
9726}
9727
9728void
9729ada_others_component::dump (ui_file *stream, int depth)
9730{
6cb06a8c 9731 gdb_printf (stream, _("%*sOthers:\n"), depth, "");
a88c4354
TT
9732 m_op->dump (stream, depth + 1);
9733}
9734
9735/* Assign the value of the expression in the OP_OTHERS construct in
9736 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9737 have not been previously assigned. The index intervals already assigned
9738 are in INDICES. CONTAINER is as for assign_aggregate. */
9739void
9740ada_others_component::assign (struct value *container,
9741 struct value *lhs, struct expression *exp,
9742 std::vector<LONGEST> &indices,
9743 LONGEST low, LONGEST high)
9744{
9745 int num_indices = indices.size ();
9746 for (int i = 0; i < num_indices - 2; i += 2)
9747 {
9748 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9749 assign_component (container, lhs, ind, exp, m_op);
9750 }
9751}
9752
9753struct value *
9754ada_assign_operation::evaluate (struct type *expect_type,
9755 struct expression *exp,
9756 enum noside noside)
9757{
9758 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
9759
9760 ada_aggregate_operation *ag_op
9761 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9762 if (ag_op != nullptr)
9763 {
9764 if (noside != EVAL_NORMAL)
9765 return arg1;
9766
207582c0 9767 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9768 return ada_value_assign (arg1, arg1);
9769 }
9770 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9771 except if the lhs of our assignment is a convenience variable.
9772 In the case of assigning to a convenience variable, the lhs
9773 should be exactly the result of the evaluation of the rhs. */
9774 struct type *type = value_type (arg1);
9775 if (VALUE_LVAL (arg1) == lval_internalvar)
9776 type = NULL;
9777 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9778 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354
TT
9779 return arg1;
9780 if (VALUE_LVAL (arg1) == lval_internalvar)
9781 {
9782 /* Nothing. */
9783 }
9784 else
9785 arg2 = coerce_for_assign (value_type (arg1), arg2);
9786 return ada_value_assign (arg1, arg2);
9787}
9788
9789} /* namespace expr */
9790
cf608cc4
TT
9791/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9792 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9793 overlap. */
52ce6436
PH
9794static void
9795add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9796 std::vector<LONGEST> &indices)
52ce6436
PH
9797{
9798 int i, j;
5b4ee69b 9799
cf608cc4
TT
9800 int size = indices.size ();
9801 for (i = 0; i < size; i += 2) {
52ce6436
PH
9802 if (high >= indices[i] && low <= indices[i + 1])
9803 {
9804 int kh;
5b4ee69b 9805
cf608cc4 9806 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9807 if (high < indices[kh])
9808 break;
9809 if (low < indices[i])
9810 indices[i] = low;
9811 indices[i + 1] = indices[kh - 1];
9812 if (high > indices[i + 1])
9813 indices[i + 1] = high;
cf608cc4
TT
9814 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9815 indices.resize (kh - i - 2);
52ce6436
PH
9816 return;
9817 }
9818 else if (high < indices[i])
9819 break;
9820 }
9821
cf608cc4 9822 indices.resize (indices.size () + 2);
d4813f10 9823 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9824 indices[j] = indices[j - 2];
9825 indices[i] = low;
9826 indices[i + 1] = high;
9827}
9828
6e48bd2c
JB
9829/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9830 is different. */
9831
9832static struct value *
b7e22850 9833ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c
JB
9834{
9835 if (type == ada_check_typedef (value_type (arg2)))
9836 return arg2;
9837
6e48bd2c
JB
9838 return value_cast (type, arg2);
9839}
9840
284614f0
JB
9841/* Evaluating Ada expressions, and printing their result.
9842 ------------------------------------------------------
9843
21649b50
JB
9844 1. Introduction:
9845 ----------------
9846
284614f0
JB
9847 We usually evaluate an Ada expression in order to print its value.
9848 We also evaluate an expression in order to print its type, which
9849 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9850 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9851 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9852 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9853 similar.
9854
9855 Evaluating expressions is a little more complicated for Ada entities
9856 than it is for entities in languages such as C. The main reason for
9857 this is that Ada provides types whose definition might be dynamic.
9858 One example of such types is variant records. Or another example
9859 would be an array whose bounds can only be known at run time.
9860
9861 The following description is a general guide as to what should be
9862 done (and what should NOT be done) in order to evaluate an expression
9863 involving such types, and when. This does not cover how the semantic
9864 information is encoded by GNAT as this is covered separatly. For the
9865 document used as the reference for the GNAT encoding, see exp_dbug.ads
9866 in the GNAT sources.
9867
9868 Ideally, we should embed each part of this description next to its
9869 associated code. Unfortunately, the amount of code is so vast right
9870 now that it's hard to see whether the code handling a particular
9871 situation might be duplicated or not. One day, when the code is
9872 cleaned up, this guide might become redundant with the comments
9873 inserted in the code, and we might want to remove it.
9874
21649b50
JB
9875 2. ``Fixing'' an Entity, the Simple Case:
9876 -----------------------------------------
9877
284614f0
JB
9878 When evaluating Ada expressions, the tricky issue is that they may
9879 reference entities whose type contents and size are not statically
9880 known. Consider for instance a variant record:
9881
9882 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9883 case Empty is
9884 when True => null;
9885 when False => Value : Integer;
9886 end case;
284614f0
JB
9887 end record;
9888 Yes : Rec := (Empty => False, Value => 1);
9889 No : Rec := (empty => True);
9890
9891 The size and contents of that record depends on the value of the
9892 descriminant (Rec.Empty). At this point, neither the debugging
9893 information nor the associated type structure in GDB are able to
9894 express such dynamic types. So what the debugger does is to create
9895 "fixed" versions of the type that applies to the specific object.
30baf67b 9896 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9897 which means creating its associated fixed type.
9898
9899 Example: when printing the value of variable "Yes" above, its fixed
9900 type would look like this:
9901
9902 type Rec is record
dda83cd7
SM
9903 Empty : Boolean;
9904 Value : Integer;
284614f0
JB
9905 end record;
9906
9907 On the other hand, if we printed the value of "No", its fixed type
9908 would become:
9909
9910 type Rec is record
dda83cd7 9911 Empty : Boolean;
284614f0
JB
9912 end record;
9913
9914 Things become a little more complicated when trying to fix an entity
9915 with a dynamic type that directly contains another dynamic type,
9916 such as an array of variant records, for instance. There are
9917 two possible cases: Arrays, and records.
9918
21649b50
JB
9919 3. ``Fixing'' Arrays:
9920 ---------------------
9921
9922 The type structure in GDB describes an array in terms of its bounds,
9923 and the type of its elements. By design, all elements in the array
9924 have the same type and we cannot represent an array of variant elements
9925 using the current type structure in GDB. When fixing an array,
9926 we cannot fix the array element, as we would potentially need one
9927 fixed type per element of the array. As a result, the best we can do
9928 when fixing an array is to produce an array whose bounds and size
9929 are correct (allowing us to read it from memory), but without having
9930 touched its element type. Fixing each element will be done later,
9931 when (if) necessary.
9932
9933 Arrays are a little simpler to handle than records, because the same
9934 amount of memory is allocated for each element of the array, even if
1b536f04 9935 the amount of space actually used by each element differs from element
21649b50 9936 to element. Consider for instance the following array of type Rec:
284614f0
JB
9937
9938 type Rec_Array is array (1 .. 2) of Rec;
9939
1b536f04
JB
9940 The actual amount of memory occupied by each element might be different
9941 from element to element, depending on the value of their discriminant.
21649b50 9942 But the amount of space reserved for each element in the array remains
1b536f04 9943 fixed regardless. So we simply need to compute that size using
21649b50
JB
9944 the debugging information available, from which we can then determine
9945 the array size (we multiply the number of elements of the array by
9946 the size of each element).
9947
9948 The simplest case is when we have an array of a constrained element
9949 type. For instance, consider the following type declarations:
9950
dda83cd7
SM
9951 type Bounded_String (Max_Size : Integer) is
9952 Length : Integer;
9953 Buffer : String (1 .. Max_Size);
9954 end record;
9955 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9956
9957 In this case, the compiler describes the array as an array of
9958 variable-size elements (identified by its XVS suffix) for which
9959 the size can be read in the parallel XVZ variable.
9960
9961 In the case of an array of an unconstrained element type, the compiler
9962 wraps the array element inside a private PAD type. This type should not
9963 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9964 that we also use the adjective "aligner" in our code to designate
9965 these wrapper types.
9966
1b536f04 9967 In some cases, the size allocated for each element is statically
21649b50
JB
9968 known. In that case, the PAD type already has the correct size,
9969 and the array element should remain unfixed.
9970
9971 But there are cases when this size is not statically known.
9972 For instance, assuming that "Five" is an integer variable:
284614f0 9973
dda83cd7
SM
9974 type Dynamic is array (1 .. Five) of Integer;
9975 type Wrapper (Has_Length : Boolean := False) is record
9976 Data : Dynamic;
9977 case Has_Length is
9978 when True => Length : Integer;
9979 when False => null;
9980 end case;
9981 end record;
9982 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9983
dda83cd7
SM
9984 Hello : Wrapper_Array := (others => (Has_Length => True,
9985 Data => (others => 17),
9986 Length => 1));
284614f0
JB
9987
9988
9989 The debugging info would describe variable Hello as being an
9990 array of a PAD type. The size of that PAD type is not statically
9991 known, but can be determined using a parallel XVZ variable.
9992 In that case, a copy of the PAD type with the correct size should
9993 be used for the fixed array.
9994
21649b50
JB
9995 3. ``Fixing'' record type objects:
9996 ----------------------------------
9997
9998 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9999 record types. In this case, in order to compute the associated
10000 fixed type, we need to determine the size and offset of each of
10001 its components. This, in turn, requires us to compute the fixed
10002 type of each of these components.
10003
10004 Consider for instance the example:
10005
dda83cd7
SM
10006 type Bounded_String (Max_Size : Natural) is record
10007 Str : String (1 .. Max_Size);
10008 Length : Natural;
10009 end record;
10010 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
10011
10012 In that case, the position of field "Length" depends on the size
10013 of field Str, which itself depends on the value of the Max_Size
21649b50 10014 discriminant. In order to fix the type of variable My_String,
284614f0
JB
10015 we need to fix the type of field Str. Therefore, fixing a variant
10016 record requires us to fix each of its components.
10017
10018 However, if a component does not have a dynamic size, the component
10019 should not be fixed. In particular, fields that use a PAD type
10020 should not fixed. Here is an example where this might happen
10021 (assuming type Rec above):
10022
10023 type Container (Big : Boolean) is record
dda83cd7
SM
10024 First : Rec;
10025 After : Integer;
10026 case Big is
10027 when True => Another : Integer;
10028 when False => null;
10029 end case;
284614f0
JB
10030 end record;
10031 My_Container : Container := (Big => False,
dda83cd7
SM
10032 First => (Empty => True),
10033 After => 42);
284614f0
JB
10034
10035 In that example, the compiler creates a PAD type for component First,
10036 whose size is constant, and then positions the component After just
10037 right after it. The offset of component After is therefore constant
10038 in this case.
10039
10040 The debugger computes the position of each field based on an algorithm
10041 that uses, among other things, the actual position and size of the field
21649b50
JB
10042 preceding it. Let's now imagine that the user is trying to print
10043 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
10044 end up computing the offset of field After based on the size of the
10045 fixed version of field First. And since in our example First has
10046 only one actual field, the size of the fixed type is actually smaller
10047 than the amount of space allocated to that field, and thus we would
10048 compute the wrong offset of field After.
10049
21649b50
JB
10050 To make things more complicated, we need to watch out for dynamic
10051 components of variant records (identified by the ___XVL suffix in
10052 the component name). Even if the target type is a PAD type, the size
10053 of that type might not be statically known. So the PAD type needs
10054 to be unwrapped and the resulting type needs to be fixed. Otherwise,
10055 we might end up with the wrong size for our component. This can be
10056 observed with the following type declarations:
284614f0 10057
dda83cd7
SM
10058 type Octal is new Integer range 0 .. 7;
10059 type Octal_Array is array (Positive range <>) of Octal;
10060 pragma Pack (Octal_Array);
284614f0 10061
dda83cd7
SM
10062 type Octal_Buffer (Size : Positive) is record
10063 Buffer : Octal_Array (1 .. Size);
10064 Length : Integer;
10065 end record;
284614f0
JB
10066
10067 In that case, Buffer is a PAD type whose size is unset and needs
10068 to be computed by fixing the unwrapped type.
10069
21649b50
JB
10070 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
10071 ----------------------------------------------------------
10072
10073 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
10074 thus far, be actually fixed?
10075
10076 The answer is: Only when referencing that element. For instance
10077 when selecting one component of a record, this specific component
10078 should be fixed at that point in time. Or when printing the value
10079 of a record, each component should be fixed before its value gets
10080 printed. Similarly for arrays, the element of the array should be
10081 fixed when printing each element of the array, or when extracting
10082 one element out of that array. On the other hand, fixing should
10083 not be performed on the elements when taking a slice of an array!
10084
31432a67 10085 Note that one of the side effects of miscomputing the offset and
284614f0
JB
10086 size of each field is that we end up also miscomputing the size
10087 of the containing type. This can have adverse results when computing
10088 the value of an entity. GDB fetches the value of an entity based
10089 on the size of its type, and thus a wrong size causes GDB to fetch
10090 the wrong amount of memory. In the case where the computed size is
10091 too small, GDB fetches too little data to print the value of our
31432a67 10092 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
10093 past the buffer containing the data =:-o. */
10094
62d4bd94
TT
10095/* A helper function for TERNOP_IN_RANGE. */
10096
10097static value *
10098eval_ternop_in_range (struct type *expect_type, struct expression *exp,
10099 enum noside noside,
10100 value *arg1, value *arg2, value *arg3)
10101{
62d4bd94
TT
10102 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10103 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10104 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10105 return
10106 value_from_longest (type,
10107 (value_less (arg1, arg3)
10108 || value_equal (arg1, arg3))
10109 && (value_less (arg2, arg1)
10110 || value_equal (arg2, arg1)));
10111}
10112
82390ab8
TT
10113/* A helper function for UNOP_NEG. */
10114
7c15d377 10115value *
82390ab8
TT
10116ada_unop_neg (struct type *expect_type,
10117 struct expression *exp,
10118 enum noside noside, enum exp_opcode op,
10119 struct value *arg1)
10120{
82390ab8
TT
10121 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10122 return value_neg (arg1);
10123}
10124
7efc87ff
TT
10125/* A helper function for UNOP_IN_RANGE. */
10126
95d49dfb 10127value *
7efc87ff
TT
10128ada_unop_in_range (struct type *expect_type,
10129 struct expression *exp,
10130 enum noside noside, enum exp_opcode op,
10131 struct value *arg1, struct type *type)
10132{
7efc87ff
TT
10133 struct value *arg2, *arg3;
10134 switch (type->code ())
10135 {
10136 default:
10137 lim_warning (_("Membership test incompletely implemented; "
10138 "always returns true"));
10139 type = language_bool_type (exp->language_defn, exp->gdbarch);
10140 return value_from_longest (type, (LONGEST) 1);
10141
10142 case TYPE_CODE_RANGE:
10143 arg2 = value_from_longest (type,
10144 type->bounds ()->low.const_val ());
10145 arg3 = value_from_longest (type,
10146 type->bounds ()->high.const_val ());
10147 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10148 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10149 type = language_bool_type (exp->language_defn, exp->gdbarch);
10150 return
10151 value_from_longest (type,
10152 (value_less (arg1, arg3)
10153 || value_equal (arg1, arg3))
10154 && (value_less (arg2, arg1)
10155 || value_equal (arg2, arg1)));
10156 }
10157}
10158
020dbabe
TT
10159/* A helper function for OP_ATR_TAG. */
10160
7c15d377 10161value *
020dbabe
TT
10162ada_atr_tag (struct type *expect_type,
10163 struct expression *exp,
10164 enum noside noside, enum exp_opcode op,
10165 struct value *arg1)
10166{
10167 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10168 return value_zero (ada_tag_type (arg1), not_lval);
10169
10170 return ada_value_tag (arg1);
10171}
10172
68c75735
TT
10173/* A helper function for OP_ATR_SIZE. */
10174
7c15d377 10175value *
68c75735
TT
10176ada_atr_size (struct type *expect_type,
10177 struct expression *exp,
10178 enum noside noside, enum exp_opcode op,
10179 struct value *arg1)
10180{
10181 struct type *type = value_type (arg1);
10182
10183 /* If the argument is a reference, then dereference its type, since
10184 the user is really asking for the size of the actual object,
10185 not the size of the pointer. */
10186 if (type->code () == TYPE_CODE_REF)
10187 type = TYPE_TARGET_TYPE (type);
10188
0b2b0b82 10189 if (noside == EVAL_AVOID_SIDE_EFFECTS)
68c75735
TT
10190 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
10191 else
10192 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
10193 TARGET_CHAR_BIT * TYPE_LENGTH (type));
10194}
10195
d05e24e6
TT
10196/* A helper function for UNOP_ABS. */
10197
7c15d377 10198value *
d05e24e6
TT
10199ada_abs (struct type *expect_type,
10200 struct expression *exp,
10201 enum noside noside, enum exp_opcode op,
10202 struct value *arg1)
10203{
10204 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10205 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
10206 return value_neg (arg1);
10207 else
10208 return arg1;
10209}
10210
faa1dfd7
TT
10211/* A helper function for BINOP_MUL. */
10212
d9e7db06 10213value *
faa1dfd7
TT
10214ada_mult_binop (struct type *expect_type,
10215 struct expression *exp,
10216 enum noside noside, enum exp_opcode op,
10217 struct value *arg1, struct value *arg2)
10218{
10219 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10220 {
10221 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10222 return value_zero (value_type (arg1), not_lval);
10223 }
10224 else
10225 {
10226 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10227 return ada_value_binop (arg1, arg2, op);
10228 }
10229}
10230
214b13ac
TT
10231/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10232
6e8fb7b7 10233value *
214b13ac
TT
10234ada_equal_binop (struct type *expect_type,
10235 struct expression *exp,
10236 enum noside noside, enum exp_opcode op,
10237 struct value *arg1, struct value *arg2)
10238{
10239 int tem;
10240 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10241 tem = 0;
10242 else
10243 {
10244 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10245 tem = ada_value_equal (arg1, arg2);
10246 }
10247 if (op == BINOP_NOTEQUAL)
10248 tem = !tem;
10249 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10250 return value_from_longest (type, (LONGEST) tem);
10251}
10252
5ce19db8
TT
10253/* A helper function for TERNOP_SLICE. */
10254
1b1ebfab 10255value *
5ce19db8
TT
10256ada_ternop_slice (struct expression *exp,
10257 enum noside noside,
10258 struct value *array, struct value *low_bound_val,
10259 struct value *high_bound_val)
10260{
10261 LONGEST low_bound;
10262 LONGEST high_bound;
10263
10264 low_bound_val = coerce_ref (low_bound_val);
10265 high_bound_val = coerce_ref (high_bound_val);
10266 low_bound = value_as_long (low_bound_val);
10267 high_bound = value_as_long (high_bound_val);
10268
10269 /* If this is a reference to an aligner type, then remove all
10270 the aligners. */
10271 if (value_type (array)->code () == TYPE_CODE_REF
10272 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
10273 TYPE_TARGET_TYPE (value_type (array)) =
10274 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
10275
10276 if (ada_is_any_packed_array_type (value_type (array)))
10277 error (_("cannot slice a packed array"));
10278
10279 /* If this is a reference to an array or an array lvalue,
10280 convert to a pointer. */
10281 if (value_type (array)->code () == TYPE_CODE_REF
10282 || (value_type (array)->code () == TYPE_CODE_ARRAY
10283 && VALUE_LVAL (array) == lval_memory))
10284 array = value_addr (array);
10285
10286 if (noside == EVAL_AVOID_SIDE_EFFECTS
10287 && ada_is_array_descriptor_type (ada_check_typedef
10288 (value_type (array))))
10289 return empty_array (ada_type_of_array (array, 0), low_bound,
10290 high_bound);
10291
10292 array = ada_coerce_to_simple_array_ptr (array);
10293
10294 /* If we have more than one level of pointer indirection,
10295 dereference the value until we get only one level. */
10296 while (value_type (array)->code () == TYPE_CODE_PTR
10297 && (TYPE_TARGET_TYPE (value_type (array))->code ()
10298 == TYPE_CODE_PTR))
10299 array = value_ind (array);
10300
10301 /* Make sure we really do have an array type before going further,
10302 to avoid a SEGV when trying to get the index type or the target
10303 type later down the road if the debug info generated by
10304 the compiler is incorrect or incomplete. */
10305 if (!ada_is_simple_array_type (value_type (array)))
10306 error (_("cannot take slice of non-array"));
10307
10308 if (ada_check_typedef (value_type (array))->code ()
10309 == TYPE_CODE_PTR)
10310 {
10311 struct type *type0 = ada_check_typedef (value_type (array));
10312
10313 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
10314 return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
10315 else
10316 {
10317 struct type *arr_type0 =
10318 to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
10319
10320 return ada_value_slice_from_ptr (array, arr_type0,
10321 longest_to_int (low_bound),
10322 longest_to_int (high_bound));
10323 }
10324 }
10325 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10326 return array;
10327 else if (high_bound < low_bound)
10328 return empty_array (value_type (array), low_bound, high_bound);
10329 else
10330 return ada_value_slice (array, longest_to_int (low_bound),
10331 longest_to_int (high_bound));
10332}
10333
b467efaa
TT
10334/* A helper function for BINOP_IN_BOUNDS. */
10335
82c3886e 10336value *
b467efaa
TT
10337ada_binop_in_bounds (struct expression *exp, enum noside noside,
10338 struct value *arg1, struct value *arg2, int n)
10339{
10340 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10341 {
10342 struct type *type = language_bool_type (exp->language_defn,
10343 exp->gdbarch);
10344 return value_zero (type, not_lval);
10345 }
10346
10347 struct type *type = ada_index_type (value_type (arg2), n, "range");
10348 if (!type)
10349 type = value_type (arg1);
10350
10351 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10352 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10353
10354 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10355 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10356 type = language_bool_type (exp->language_defn, exp->gdbarch);
10357 return value_from_longest (type,
10358 (value_less (arg1, arg3)
10359 || value_equal (arg1, arg3))
10360 && (value_less (arg2, arg1)
10361 || value_equal (arg2, arg1)));
10362}
10363
b84564fc
TT
10364/* A helper function for some attribute operations. */
10365
10366static value *
10367ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10368 struct value *arg1, struct type *type_arg, int tem)
10369{
10370 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10371 {
10372 if (type_arg == NULL)
10373 type_arg = value_type (arg1);
10374
10375 if (ada_is_constrained_packed_array_type (type_arg))
10376 type_arg = decode_constrained_packed_array_type (type_arg);
10377
10378 if (!discrete_type_p (type_arg))
10379 {
10380 switch (op)
10381 {
10382 default: /* Should never happen. */
10383 error (_("unexpected attribute encountered"));
10384 case OP_ATR_FIRST:
10385 case OP_ATR_LAST:
10386 type_arg = ada_index_type (type_arg, tem,
10387 ada_attribute_name (op));
10388 break;
10389 case OP_ATR_LENGTH:
10390 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10391 break;
10392 }
10393 }
10394
10395 return value_zero (type_arg, not_lval);
10396 }
10397 else if (type_arg == NULL)
10398 {
10399 arg1 = ada_coerce_ref (arg1);
10400
10401 if (ada_is_constrained_packed_array_type (value_type (arg1)))
10402 arg1 = ada_coerce_to_simple_array (arg1);
10403
10404 struct type *type;
10405 if (op == OP_ATR_LENGTH)
10406 type = builtin_type (exp->gdbarch)->builtin_int;
10407 else
10408 {
10409 type = ada_index_type (value_type (arg1), tem,
10410 ada_attribute_name (op));
10411 if (type == NULL)
10412 type = builtin_type (exp->gdbarch)->builtin_int;
10413 }
10414
10415 switch (op)
10416 {
10417 default: /* Should never happen. */
10418 error (_("unexpected attribute encountered"));
10419 case OP_ATR_FIRST:
10420 return value_from_longest
10421 (type, ada_array_bound (arg1, tem, 0));
10422 case OP_ATR_LAST:
10423 return value_from_longest
10424 (type, ada_array_bound (arg1, tem, 1));
10425 case OP_ATR_LENGTH:
10426 return value_from_longest
10427 (type, ada_array_length (arg1, tem));
10428 }
10429 }
10430 else if (discrete_type_p (type_arg))
10431 {
10432 struct type *range_type;
10433 const char *name = ada_type_name (type_arg);
10434
10435 range_type = NULL;
10436 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10437 range_type = to_fixed_range_type (type_arg, NULL);
10438 if (range_type == NULL)
10439 range_type = type_arg;
10440 switch (op)
10441 {
10442 default:
10443 error (_("unexpected attribute encountered"));
10444 case OP_ATR_FIRST:
10445 return value_from_longest
10446 (range_type, ada_discrete_type_low_bound (range_type));
10447 case OP_ATR_LAST:
10448 return value_from_longest
10449 (range_type, ada_discrete_type_high_bound (range_type));
10450 case OP_ATR_LENGTH:
10451 error (_("the 'length attribute applies only to array types"));
10452 }
10453 }
10454 else if (type_arg->code () == TYPE_CODE_FLT)
10455 error (_("unimplemented type attribute"));
10456 else
10457 {
10458 LONGEST low, high;
10459
10460 if (ada_is_constrained_packed_array_type (type_arg))
10461 type_arg = decode_constrained_packed_array_type (type_arg);
10462
10463 struct type *type;
10464 if (op == OP_ATR_LENGTH)
10465 type = builtin_type (exp->gdbarch)->builtin_int;
10466 else
10467 {
10468 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10469 if (type == NULL)
10470 type = builtin_type (exp->gdbarch)->builtin_int;
10471 }
10472
10473 switch (op)
10474 {
10475 default:
10476 error (_("unexpected attribute encountered"));
10477 case OP_ATR_FIRST:
10478 low = ada_array_bound_from_type (type_arg, tem, 0);
10479 return value_from_longest (type, low);
10480 case OP_ATR_LAST:
10481 high = ada_array_bound_from_type (type_arg, tem, 1);
10482 return value_from_longest (type, high);
10483 case OP_ATR_LENGTH:
10484 low = ada_array_bound_from_type (type_arg, tem, 0);
10485 high = ada_array_bound_from_type (type_arg, tem, 1);
10486 return value_from_longest (type, high - low + 1);
10487 }
10488 }
10489}
10490
38dc70cf
TT
10491/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10492
6ad3b8bf 10493struct value *
38dc70cf
TT
10494ada_binop_minmax (struct type *expect_type,
10495 struct expression *exp,
10496 enum noside noside, enum exp_opcode op,
10497 struct value *arg1, struct value *arg2)
10498{
10499 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10500 return value_zero (value_type (arg1), not_lval);
10501 else
10502 {
10503 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10504 return value_binop (arg1, arg2, op);
38dc70cf
TT
10505 }
10506}
10507
dd5fd283
TT
10508/* A helper function for BINOP_EXP. */
10509
065ec826 10510struct value *
dd5fd283
TT
10511ada_binop_exp (struct type *expect_type,
10512 struct expression *exp,
10513 enum noside noside, enum exp_opcode op,
10514 struct value *arg1, struct value *arg2)
10515{
10516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10517 return value_zero (value_type (arg1), not_lval);
10518 else
10519 {
10520 /* For integer exponentiation operations,
10521 only promote the first argument. */
10522 if (is_integral_type (value_type (arg2)))
10523 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10524 else
10525 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10526
10527 return value_binop (arg1, arg2, op);
10528 }
10529}
10530
03070ee9
TT
10531namespace expr
10532{
10533
8b12db26
TT
10534/* See ada-exp.h. */
10535
10536operation_up
10537ada_resolvable::replace (operation_up &&owner,
10538 struct expression *exp,
10539 bool deprocedure_p,
10540 bool parse_completion,
10541 innermost_block_tracker *tracker,
10542 struct type *context_type)
10543{
10544 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10545 return (make_operation<ada_funcall_operation>
10546 (std::move (owner),
10547 std::vector<operation_up> ()));
10548 return std::move (owner);
10549}
10550
c9f66f00 10551/* Convert the character literal whose value would be VAL to the
03adb248
TT
10552 appropriate value of type TYPE, if there is a translation.
10553 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10554 the literal 'A' (VAL == 65), returns 0. */
10555
10556static LONGEST
10557convert_char_literal (struct type *type, LONGEST val)
10558{
c9f66f00 10559 char name[12];
03adb248
TT
10560 int f;
10561
10562 if (type == NULL)
10563 return val;
10564 type = check_typedef (type);
10565 if (type->code () != TYPE_CODE_ENUM)
10566 return val;
10567
10568 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10569 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10570 else if (val >= 0 && val < 256)
10571 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10572 else if (val >= 0 && val < 0x10000)
10573 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10574 else
c9f66f00 10575 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10576 size_t len = strlen (name);
10577 for (f = 0; f < type->num_fields (); f += 1)
10578 {
10579 /* Check the suffix because an enum constant in a package will
10580 have a name like "pkg__QUxx". This is safe enough because we
10581 already have the correct type, and because mangling means
10582 there can't be clashes. */
33d16dd9 10583 const char *ename = type->field (f).name ();
03adb248
TT
10584 size_t elen = strlen (ename);
10585
10586 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10587 return type->field (f).loc_enumval ();
03adb248
TT
10588 }
10589 return val;
10590}
10591
b1b9c411
TT
10592value *
10593ada_char_operation::evaluate (struct type *expect_type,
10594 struct expression *exp,
10595 enum noside noside)
10596{
10597 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10598 if (expect_type != nullptr)
10599 result = ada_value_cast (expect_type, result);
10600 return result;
10601}
10602
03adb248
TT
10603/* See ada-exp.h. */
10604
10605operation_up
10606ada_char_operation::replace (operation_up &&owner,
10607 struct expression *exp,
10608 bool deprocedure_p,
10609 bool parse_completion,
10610 innermost_block_tracker *tracker,
10611 struct type *context_type)
10612{
10613 operation_up result = std::move (owner);
10614
10615 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10616 {
10617 gdb_assert (result.get () == this);
10618 std::get<0> (m_storage) = context_type;
10619 std::get<1> (m_storage)
10620 = convert_char_literal (context_type, std::get<1> (m_storage));
10621 }
10622
b1b9c411 10623 return result;
03adb248
TT
10624}
10625
03070ee9
TT
10626value *
10627ada_wrapped_operation::evaluate (struct type *expect_type,
10628 struct expression *exp,
10629 enum noside noside)
10630{
10631 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10632 if (noside == EVAL_NORMAL)
10633 result = unwrap_value (result);
10634
10635 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10636 then we need to perform the conversion manually, because
10637 evaluate_subexp_standard doesn't do it. This conversion is
10638 necessary in Ada because the different kinds of float/fixed
10639 types in Ada have different representations.
10640
10641 Similarly, we need to perform the conversion from OP_LONG
10642 ourselves. */
10643 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10644 result = ada_value_cast (expect_type, result);
10645
10646 return result;
10647}
10648
42fecb61
TT
10649value *
10650ada_string_operation::evaluate (struct type *expect_type,
10651 struct expression *exp,
10652 enum noside noside)
10653{
fc18a21b
TT
10654 struct type *char_type;
10655 if (expect_type != nullptr && ada_is_string_type (expect_type))
10656 char_type = ada_array_element_type (expect_type, 1);
10657 else
10658 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10659
10660 const std::string &str = std::get<0> (m_storage);
10661 const char *encoding;
10662 switch (TYPE_LENGTH (char_type))
10663 {
10664 case 1:
10665 {
10666 /* Simply copy over the data -- this isn't perhaps strictly
10667 correct according to the encodings, but it is gdb's
10668 historical behavior. */
10669 struct type *stringtype
10670 = lookup_array_range_type (char_type, 1, str.length ());
10671 struct value *val = allocate_value (stringtype);
10672 memcpy (value_contents_raw (val).data (), str.c_str (),
10673 str.length ());
10674 return val;
10675 }
10676
10677 case 2:
10678 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10679 encoding = "UTF-16BE";
10680 else
10681 encoding = "UTF-16LE";
10682 break;
10683
10684 case 4:
10685 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10686 encoding = "UTF-32BE";
10687 else
10688 encoding = "UTF-32LE";
10689 break;
10690
10691 default:
10692 error (_("unexpected character type size %s"),
10693 pulongest (TYPE_LENGTH (char_type)));
10694 }
10695
10696 auto_obstack converted;
10697 convert_between_encodings (host_charset (), encoding,
10698 (const gdb_byte *) str.c_str (),
10699 str.length (), 1,
10700 &converted, translit_none);
10701
10702 struct type *stringtype
10703 = lookup_array_range_type (char_type, 1,
10704 obstack_object_size (&converted)
10705 / TYPE_LENGTH (char_type));
10706 struct value *val = allocate_value (stringtype);
10707 memcpy (value_contents_raw (val).data (),
10708 obstack_base (&converted),
10709 obstack_object_size (&converted));
10710 return val;
42fecb61
TT
10711}
10712
b1b9c411
TT
10713value *
10714ada_concat_operation::evaluate (struct type *expect_type,
10715 struct expression *exp,
10716 enum noside noside)
10717{
10718 /* If one side is a literal, evaluate the other side first so that
10719 the expected type can be set properly. */
10720 const operation_up &lhs_expr = std::get<0> (m_storage);
10721 const operation_up &rhs_expr = std::get<1> (m_storage);
10722
10723 value *lhs, *rhs;
10724 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10725 {
10726 rhs = rhs_expr->evaluate (nullptr, exp, noside);
10727 lhs = lhs_expr->evaluate (value_type (rhs), exp, noside);
10728 }
10729 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10730 {
10731 rhs = rhs_expr->evaluate (nullptr, exp, noside);
10732 struct type *rhs_type = check_typedef (value_type (rhs));
10733 struct type *elt_type = nullptr;
10734 if (rhs_type->code () == TYPE_CODE_ARRAY)
10735 elt_type = TYPE_TARGET_TYPE (rhs_type);
10736 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10737 }
10738 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10739 {
10740 lhs = lhs_expr->evaluate (nullptr, exp, noside);
10741 rhs = rhs_expr->evaluate (value_type (lhs), exp, noside);
10742 }
10743 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10744 {
10745 lhs = lhs_expr->evaluate (nullptr, exp, noside);
10746 struct type *lhs_type = check_typedef (value_type (lhs));
10747 struct type *elt_type = nullptr;
10748 if (lhs_type->code () == TYPE_CODE_ARRAY)
10749 elt_type = TYPE_TARGET_TYPE (lhs_type);
10750 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10751 }
10752 else
10753 return concat_operation::evaluate (expect_type, exp, noside);
10754
10755 return value_concat (lhs, rhs);
10756}
10757
cc6bd32e
TT
10758value *
10759ada_qual_operation::evaluate (struct type *expect_type,
10760 struct expression *exp,
10761 enum noside noside)
10762{
10763 struct type *type = std::get<1> (m_storage);
10764 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10765}
10766
fc715eb2
TT
10767value *
10768ada_ternop_range_operation::evaluate (struct type *expect_type,
10769 struct expression *exp,
10770 enum noside noside)
10771{
10772 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10773 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10774 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10775 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10776}
10777
73796c73
TT
10778value *
10779ada_binop_addsub_operation::evaluate (struct type *expect_type,
10780 struct expression *exp,
10781 enum noside noside)
10782{
10783 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10784 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10785
10786 auto do_op = [=] (LONGEST x, LONGEST y)
10787 {
10788 if (std::get<0> (m_storage) == BINOP_ADD)
10789 return x + y;
10790 return x - y;
10791 };
10792
10793 if (value_type (arg1)->code () == TYPE_CODE_PTR)
10794 return (value_from_longest
10795 (value_type (arg1),
10796 do_op (value_as_long (arg1), value_as_long (arg2))));
10797 if (value_type (arg2)->code () == TYPE_CODE_PTR)
10798 return (value_from_longest
10799 (value_type (arg2),
10800 do_op (value_as_long (arg1), value_as_long (arg2))));
10801 /* Preserve the original type for use by the range case below.
10802 We cannot cast the result to a reference type, so if ARG1 is
10803 a reference type, find its underlying type. */
10804 struct type *type = value_type (arg1);
10805 while (type->code () == TYPE_CODE_REF)
10806 type = TYPE_TARGET_TYPE (type);
10807 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10808 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10809 /* We need to special-case the result with a range.
10810 This is done for the benefit of "ptype". gdb's Ada support
10811 historically used the LHS to set the result type here, so
10812 preserve this behavior. */
10813 if (type->code () == TYPE_CODE_RANGE)
10814 arg1 = value_cast (type, arg1);
10815 return arg1;
10816}
10817
60fa02ca
TT
10818value *
10819ada_unop_atr_operation::evaluate (struct type *expect_type,
10820 struct expression *exp,
10821 enum noside noside)
10822{
10823 struct type *type_arg = nullptr;
10824 value *val = nullptr;
10825
10826 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10827 {
10828 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10829 EVAL_AVOID_SIDE_EFFECTS);
10830 type_arg = value_type (tem);
10831 }
10832 else
10833 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10834
10835 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10836 val, type_arg, std::get<2> (m_storage));
10837}
10838
3f4a0053
TT
10839value *
10840ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10841 struct expression *exp,
10842 enum noside noside)
10843{
10844 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10845 return value_zero (expect_type, not_lval);
10846
9c79936b
TT
10847 const bound_minimal_symbol &b = std::get<0> (m_storage);
10848 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10849
10850 val = ada_value_cast (expect_type, val);
10851
10852 /* Follow the Ada language semantics that do not allow taking
10853 an address of the result of a cast (view conversion in Ada). */
10854 if (VALUE_LVAL (val) == lval_memory)
10855 {
10856 if (value_lazy (val))
10857 value_fetch_lazy (val);
10858 VALUE_LVAL (val) = not_lval;
10859 }
10860 return val;
10861}
10862
99a3b1e7
TT
10863value *
10864ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10865 struct expression *exp,
10866 enum noside noside)
10867{
10868 value *val = evaluate_var_value (noside,
9e5e03df
TT
10869 std::get<0> (m_storage).block,
10870 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10871
10872 val = ada_value_cast (expect_type, val);
10873
10874 /* Follow the Ada language semantics that do not allow taking
10875 an address of the result of a cast (view conversion in Ada). */
10876 if (VALUE_LVAL (val) == lval_memory)
10877 {
10878 if (value_lazy (val))
10879 value_fetch_lazy (val);
10880 VALUE_LVAL (val) = not_lval;
10881 }
10882 return val;
10883}
10884
10885value *
10886ada_var_value_operation::evaluate (struct type *expect_type,
10887 struct expression *exp,
10888 enum noside noside)
10889{
9e5e03df 10890 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10891
6c9c307c 10892 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10893 /* Only encountered when an unresolved symbol occurs in a
10894 context other than a function call, in which case, it is
10895 invalid. */
10896 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10897 sym->print_name ());
10898
10899 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10900 {
5f9c5a63 10901 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10902 /* Check to see if this is a tagged type. We also need to handle
10903 the case where the type is a reference to a tagged type, but
10904 we have to be careful to exclude pointers to tagged types.
10905 The latter should be shown as usual (as a pointer), whereas
10906 a reference should mostly be transparent to the user. */
10907 if (ada_is_tagged_type (type, 0)
10908 || (type->code () == TYPE_CODE_REF
10909 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
10910 {
10911 /* Tagged types are a little special in the fact that the real
10912 type is dynamic and can only be determined by inspecting the
10913 object's tag. This means that we need to get the object's
10914 value first (EVAL_NORMAL) and then extract the actual object
10915 type from its tag.
10916
10917 Note that we cannot skip the final step where we extract
10918 the object type from its tag, because the EVAL_NORMAL phase
10919 results in dynamic components being resolved into fixed ones.
10920 This can cause problems when trying to print the type
10921 description of tagged types whose parent has a dynamic size:
10922 We use the type name of the "_parent" component in order
10923 to print the name of the ancestor type in the type description.
10924 If that component had a dynamic size, the resolution into
10925 a fixed type would result in the loss of that type name,
10926 thus preventing us from printing the name of the ancestor
10927 type in the type description. */
9863c3b5 10928 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10929
10930 if (type->code () != TYPE_CODE_REF)
10931 {
10932 struct type *actual_type;
10933
10934 actual_type = type_from_tag (ada_value_tag (arg1));
10935 if (actual_type == NULL)
10936 /* If, for some reason, we were unable to determine
10937 the actual type from the tag, then use the static
10938 approximation that we just computed as a fallback.
10939 This can happen if the debugging information is
10940 incomplete, for instance. */
10941 actual_type = type;
10942 return value_zero (actual_type, not_lval);
10943 }
10944 else
10945 {
10946 /* In the case of a ref, ada_coerce_ref takes care
10947 of determining the actual type. But the evaluation
10948 should return a ref as it should be valid to ask
10949 for its address; so rebuild a ref after coerce. */
10950 arg1 = ada_coerce_ref (arg1);
10951 return value_ref (arg1, TYPE_CODE_REF);
10952 }
10953 }
10954
10955 /* Records and unions for which GNAT encodings have been
10956 generated need to be statically fixed as well.
10957 Otherwise, non-static fixing produces a type where
10958 all dynamic properties are removed, which prevents "ptype"
10959 from being able to completely describe the type.
10960 For instance, a case statement in a variant record would be
10961 replaced by the relevant components based on the actual
10962 value of the discriminants. */
10963 if ((type->code () == TYPE_CODE_STRUCT
10964 && dynamic_template_type (type) != NULL)
10965 || (type->code () == TYPE_CODE_UNION
10966 && ada_find_parallel_type (type, "___XVU") != NULL))
10967 return value_zero (to_static_fixed_type (type), not_lval);
10968 }
10969
10970 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10971 return ada_to_fixed_value (arg1);
10972}
10973
d8a4ed8a
TT
10974bool
10975ada_var_value_operation::resolve (struct expression *exp,
10976 bool deprocedure_p,
10977 bool parse_completion,
10978 innermost_block_tracker *tracker,
10979 struct type *context_type)
10980{
9e5e03df 10981 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10982 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10983 {
10984 block_symbol resolved
9e5e03df 10985 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10986 context_type, parse_completion,
10987 deprocedure_p, tracker);
9e5e03df 10988 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10989 }
10990
10991 if (deprocedure_p
5f9c5a63 10992 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10993 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10994 return true;
10995
10996 return false;
10997}
10998
9e99f48f
TT
10999value *
11000ada_atr_val_operation::evaluate (struct type *expect_type,
11001 struct expression *exp,
11002 enum noside noside)
11003{
11004 value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
11005 return ada_val_atr (noside, std::get<0> (m_storage), arg);
11006}
11007
e8c33fa1
TT
11008value *
11009ada_unop_ind_operation::evaluate (struct type *expect_type,
11010 struct expression *exp,
11011 enum noside noside)
11012{
11013 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
11014
11015 struct type *type = ada_check_typedef (value_type (arg1));
11016 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11017 {
11018 if (ada_is_array_descriptor_type (type))
11019 /* GDB allows dereferencing GNAT array descriptors. */
11020 {
11021 struct type *arrType = ada_type_of_array (arg1, 0);
11022
11023 if (arrType == NULL)
11024 error (_("Attempt to dereference null array pointer."));
11025 return value_at_lazy (arrType, 0);
11026 }
11027 else if (type->code () == TYPE_CODE_PTR
11028 || type->code () == TYPE_CODE_REF
11029 /* In C you can dereference an array to get the 1st elt. */
11030 || type->code () == TYPE_CODE_ARRAY)
11031 {
11032 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11033 only be determined by inspecting the object's tag.
11034 This means that we need to evaluate completely the
11035 expression in order to get its type. */
11036
11037 if ((type->code () == TYPE_CODE_REF
11038 || type->code () == TYPE_CODE_PTR)
11039 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
11040 {
11041 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11042 EVAL_NORMAL);
11043 type = value_type (ada_value_ind (arg1));
11044 }
11045 else
11046 {
11047 type = to_static_fixed_type
11048 (ada_aligned_type
11049 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
11050 }
e8c33fa1
TT
11051 return value_zero (type, lval_memory);
11052 }
11053 else if (type->code () == TYPE_CODE_INT)
11054 {
11055 /* GDB allows dereferencing an int. */
11056 if (expect_type == NULL)
11057 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11058 lval_memory);
11059 else
11060 {
11061 expect_type =
11062 to_static_fixed_type (ada_aligned_type (expect_type));
11063 return value_zero (expect_type, lval_memory);
11064 }
11065 }
11066 else
11067 error (_("Attempt to take contents of a non-pointer value."));
11068 }
11069 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
11070 type = ada_check_typedef (value_type (arg1));
11071
11072 if (type->code () == TYPE_CODE_INT)
11073 /* GDB allows dereferencing an int. If we were given
11074 the expect_type, then use that as the target type.
11075 Otherwise, assume that the target type is an int. */
11076 {
11077 if (expect_type != NULL)
11078 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11079 arg1));
11080 else
11081 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11082 (CORE_ADDR) value_as_address (arg1));
11083 }
11084
11085 if (ada_is_array_descriptor_type (type))
11086 /* GDB allows dereferencing GNAT array descriptors. */
11087 return ada_coerce_to_simple_array (arg1);
11088 else
11089 return ada_value_ind (arg1);
11090}
11091
ebc06ad8
TT
11092value *
11093ada_structop_operation::evaluate (struct type *expect_type,
11094 struct expression *exp,
11095 enum noside noside)
11096{
11097 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11098 const char *str = std::get<1> (m_storage).c_str ();
11099 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11100 {
11101 struct type *type;
11102 struct type *type1 = value_type (arg1);
11103
11104 if (ada_is_tagged_type (type1, 1))
11105 {
11106 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11107
11108 /* If the field is not found, check if it exists in the
11109 extension of this object's type. This means that we
11110 need to evaluate completely the expression. */
11111
11112 if (type == NULL)
11113 {
11114 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11115 EVAL_NORMAL);
11116 arg1 = ada_value_struct_elt (arg1, str, 0);
11117 arg1 = unwrap_value (arg1);
11118 type = value_type (ada_to_fixed_value (arg1));
11119 }
11120 }
11121 else
11122 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11123
11124 return value_zero (ada_aligned_type (type), lval_memory);
11125 }
11126 else
11127 {
11128 arg1 = ada_value_struct_elt (arg1, str, 0);
11129 arg1 = unwrap_value (arg1);
11130 return ada_to_fixed_value (arg1);
11131 }
11132}
11133
efe3af2f
TT
11134value *
11135ada_funcall_operation::evaluate (struct type *expect_type,
11136 struct expression *exp,
11137 enum noside noside)
11138{
11139 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11140 int nargs = args_up.size ();
11141 std::vector<value *> argvec (nargs);
11142 operation_up &callee_op = std::get<0> (m_storage);
11143
11144 ada_var_value_operation *avv
11145 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11146 if (avv != nullptr
6c9c307c 11147 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11148 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11149 avv->get_symbol ()->print_name ());
11150
11151 value *callee = callee_op->evaluate (nullptr, exp, noside);
11152 for (int i = 0; i < args_up.size (); ++i)
11153 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11154
11155 if (ada_is_constrained_packed_array_type
11156 (desc_base_type (value_type (callee))))
11157 callee = ada_coerce_to_simple_array (callee);
11158 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
11159 && TYPE_FIELD_BITSIZE (value_type (callee), 0) != 0)
11160 /* This is a packed array that has already been fixed, and
11161 therefore already coerced to a simple array. Nothing further
11162 to do. */
11163 ;
11164 else if (value_type (callee)->code () == TYPE_CODE_REF)
11165 {
11166 /* Make sure we dereference references so that all the code below
11167 feels like it's really handling the referenced value. Wrapping
11168 types (for alignment) may be there, so make sure we strip them as
11169 well. */
11170 callee = ada_to_fixed_value (coerce_ref (callee));
11171 }
11172 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
11173 && VALUE_LVAL (callee) == lval_memory)
11174 callee = value_addr (callee);
11175
11176 struct type *type = ada_check_typedef (value_type (callee));
11177
11178 /* Ada allows us to implicitly dereference arrays when subscripting
11179 them. So, if this is an array typedef (encoding use for array
11180 access types encoded as fat pointers), strip it now. */
11181 if (type->code () == TYPE_CODE_TYPEDEF)
11182 type = ada_typedef_target_type (type);
11183
11184 if (type->code () == TYPE_CODE_PTR)
11185 {
11186 switch (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ())
11187 {
11188 case TYPE_CODE_FUNC:
11189 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11190 break;
11191 case TYPE_CODE_ARRAY:
11192 break;
11193 case TYPE_CODE_STRUCT:
11194 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11195 callee = ada_value_ind (callee);
11196 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11197 break;
11198 default:
11199 error (_("cannot subscript or call something of type `%s'"),
11200 ada_type_name (value_type (callee)));
11201 break;
11202 }
11203 }
11204
11205 switch (type->code ())
11206 {
11207 case TYPE_CODE_FUNC:
11208 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11209 {
11210 if (TYPE_TARGET_TYPE (type) == NULL)
11211 error_call_unknown_return_type (NULL);
11212 return allocate_value (TYPE_TARGET_TYPE (type));
11213 }
11214 return call_function_by_hand (callee, NULL, argvec);
11215 case TYPE_CODE_INTERNAL_FUNCTION:
11216 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11217 /* We don't know anything about what the internal
11218 function might return, but we have to return
11219 something. */
11220 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11221 not_lval);
11222 else
11223 return call_internal_function (exp->gdbarch, exp->language_defn,
11224 callee, nargs,
11225 argvec.data ());
11226
d3c54a1c
TT
11227 case TYPE_CODE_STRUCT:
11228 {
11229 int arity;
4c4b4cd2 11230
d3c54a1c
TT
11231 arity = ada_array_arity (type);
11232 type = ada_array_element_type (type, nargs);
11233 if (type == NULL)
11234 error (_("cannot subscript or call a record"));
11235 if (arity != nargs)
11236 error (_("wrong number of subscripts; expecting %d"), arity);
11237 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11238 return value_zero (ada_aligned_type (type), lval_memory);
11239 return
11240 unwrap_value (ada_value_subscript
11241 (callee, nargs, argvec.data ()));
11242 }
11243 case TYPE_CODE_ARRAY:
14f9c5c9 11244 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11245 {
d3c54a1c
TT
11246 type = ada_array_element_type (type, nargs);
11247 if (type == NULL)
11248 error (_("element type of array unknown"));
dda83cd7 11249 else
d3c54a1c 11250 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 11251 }
d3c54a1c
TT
11252 return
11253 unwrap_value (ada_value_subscript
11254 (ada_coerce_to_simple_array (callee),
11255 nargs, argvec.data ()));
11256 case TYPE_CODE_PTR: /* Pointer to array */
11257 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11258 {
d3c54a1c
TT
11259 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
11260 type = ada_array_element_type (type, nargs);
11261 if (type == NULL)
11262 error (_("element type of array unknown"));
96967637 11263 else
d3c54a1c 11264 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 11265 }
d3c54a1c
TT
11266 return
11267 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11268 argvec.data ()));
6b0d7253 11269
d3c54a1c
TT
11270 default:
11271 error (_("Attempt to index or call something other than an "
11272 "array or function"));
11273 }
11274}
5b4ee69b 11275
d3c54a1c
TT
11276bool
11277ada_funcall_operation::resolve (struct expression *exp,
11278 bool deprocedure_p,
11279 bool parse_completion,
11280 innermost_block_tracker *tracker,
11281 struct type *context_type)
11282{
11283 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11284
d3c54a1c
TT
11285 ada_var_value_operation *avv
11286 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11287 if (avv == nullptr)
11288 return false;
5ec18f2b 11289
d3c54a1c 11290 symbol *sym = avv->get_symbol ();
6c9c307c 11291 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11292 return false;
dda83cd7 11293
d3c54a1c
TT
11294 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11295 int nargs = args_up.size ();
11296 std::vector<value *> argvec (nargs);
284614f0 11297
d3c54a1c
TT
11298 for (int i = 0; i < args_up.size (); ++i)
11299 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11300
d3c54a1c
TT
11301 const block *block = avv->get_block ();
11302 block_symbol resolved
11303 = ada_resolve_funcall (sym, block,
11304 context_type, parse_completion,
11305 nargs, argvec.data (),
11306 tracker);
11307
11308 std::get<0> (m_storage)
9e5e03df 11309 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11310 return false;
11311}
11312
11313bool
11314ada_ternop_slice_operation::resolve (struct expression *exp,
11315 bool deprocedure_p,
11316 bool parse_completion,
11317 innermost_block_tracker *tracker,
11318 struct type *context_type)
11319{
11320 /* Historically this check was done during resolution, so we
11321 continue that here. */
11322 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11323 EVAL_AVOID_SIDE_EFFECTS);
11324 if (ada_is_any_packed_array_type (value_type (v)))
11325 error (_("cannot slice a packed array"));
11326 return false;
11327}
14f9c5c9 11328
14f9c5c9 11329}
d3c54a1c 11330
14f9c5c9 11331\f
d2e4a39e 11332
4c4b4cd2
PH
11333/* Return non-zero iff TYPE represents a System.Address type. */
11334
11335int
11336ada_is_system_address_type (struct type *type)
11337{
7d93a1e0 11338 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11339}
11340
14f9c5c9 11341\f
d2e4a39e 11342
dda83cd7 11343 /* Range types */
14f9c5c9
AS
11344
11345/* Scan STR beginning at position K for a discriminant name, and
11346 return the value of that discriminant field of DVAL in *PX. If
11347 PNEW_K is not null, put the position of the character beyond the
11348 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11349 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11350
11351static int
108d56a4 11352scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11353 int *pnew_k)
14f9c5c9 11354{
5f9febe0 11355 static std::string storage;
5da1a4d3 11356 const char *pstart, *pend, *bound;
d2e4a39e 11357 struct value *bound_val;
14f9c5c9
AS
11358
11359 if (dval == NULL || str == NULL || str[k] == '\0')
11360 return 0;
11361
5da1a4d3
SM
11362 pstart = str + k;
11363 pend = strstr (pstart, "__");
14f9c5c9
AS
11364 if (pend == NULL)
11365 {
5da1a4d3 11366 bound = pstart;
14f9c5c9
AS
11367 k += strlen (bound);
11368 }
d2e4a39e 11369 else
14f9c5c9 11370 {
5da1a4d3
SM
11371 int len = pend - pstart;
11372
11373 /* Strip __ and beyond. */
5f9febe0
TT
11374 storage = std::string (pstart, len);
11375 bound = storage.c_str ();
d2e4a39e 11376 k = pend - str;
14f9c5c9 11377 }
d2e4a39e 11378
df407dfe 11379 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
11380 if (bound_val == NULL)
11381 return 0;
11382
11383 *px = value_as_long (bound_val);
11384 if (pnew_k != NULL)
11385 *pnew_k = k;
11386 return 1;
11387}
11388
25a1127b
TT
11389/* Value of variable named NAME. Only exact matches are considered.
11390 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11391 otherwise causes an error with message ERR_MSG. */
11392
d2e4a39e 11393static struct value *
edb0c9cb 11394get_var_value (const char *name, const char *err_msg)
14f9c5c9 11395{
25a1127b
TT
11396 std::string quoted_name = add_angle_brackets (name);
11397
11398 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11399
d1183b06
TT
11400 std::vector<struct block_symbol> syms
11401 = ada_lookup_symbol_list_worker (lookup_name,
11402 get_selected_block (0),
11403 VAR_DOMAIN, 1);
14f9c5c9 11404
d1183b06 11405 if (syms.size () != 1)
14f9c5c9
AS
11406 {
11407 if (err_msg == NULL)
dda83cd7 11408 return 0;
14f9c5c9 11409 else
dda83cd7 11410 error (("%s"), err_msg);
14f9c5c9
AS
11411 }
11412
54d343a2 11413 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11414}
d2e4a39e 11415
edb0c9cb
PA
11416/* Value of integer variable named NAME in the current environment.
11417 If no such variable is found, returns false. Otherwise, sets VALUE
11418 to the variable's value and returns true. */
4c4b4cd2 11419
edb0c9cb
PA
11420bool
11421get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11422{
4c4b4cd2 11423 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11424
14f9c5c9 11425 if (var_val == 0)
edb0c9cb
PA
11426 return false;
11427
11428 value = value_as_long (var_val);
11429 return true;
14f9c5c9 11430}
d2e4a39e 11431
14f9c5c9
AS
11432
11433/* Return a range type whose base type is that of the range type named
11434 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11435 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11436 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11437 corresponding range type from debug information; fall back to using it
11438 if symbol lookup fails. If a new type must be created, allocate it
11439 like ORIG_TYPE was. The bounds information, in general, is encoded
11440 in NAME, the base type given in the named range type. */
14f9c5c9 11441
d2e4a39e 11442static struct type *
28c85d6c 11443to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11444{
0d5cff50 11445 const char *name;
14f9c5c9 11446 struct type *base_type;
108d56a4 11447 const char *subtype_info;
14f9c5c9 11448
28c85d6c 11449 gdb_assert (raw_type != NULL);
7d93a1e0 11450 gdb_assert (raw_type->name () != NULL);
dddfab26 11451
78134374 11452 if (raw_type->code () == TYPE_CODE_RANGE)
14f9c5c9
AS
11453 base_type = TYPE_TARGET_TYPE (raw_type);
11454 else
11455 base_type = raw_type;
11456
7d93a1e0 11457 name = raw_type->name ();
14f9c5c9
AS
11458 subtype_info = strstr (name, "___XD");
11459 if (subtype_info == NULL)
690cc4eb 11460 {
43bbcdc2
PH
11461 LONGEST L = ada_discrete_type_low_bound (raw_type);
11462 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11463
690cc4eb
PH
11464 if (L < INT_MIN || U > INT_MAX)
11465 return raw_type;
11466 else
0c9c3474
SA
11467 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
11468 L, U);
690cc4eb 11469 }
14f9c5c9
AS
11470 else
11471 {
14f9c5c9
AS
11472 int prefix_len = subtype_info - name;
11473 LONGEST L, U;
11474 struct type *type;
108d56a4 11475 const char *bounds_str;
14f9c5c9
AS
11476 int n;
11477
14f9c5c9
AS
11478 subtype_info += 5;
11479 bounds_str = strchr (subtype_info, '_');
11480 n = 1;
11481
d2e4a39e 11482 if (*subtype_info == 'L')
dda83cd7
SM
11483 {
11484 if (!ada_scan_number (bounds_str, n, &L, &n)
11485 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11486 return raw_type;
11487 if (bounds_str[n] == '_')
11488 n += 2;
11489 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11490 n += 1;
11491 subtype_info += 1;
11492 }
d2e4a39e 11493 else
dda83cd7 11494 {
5f9febe0
TT
11495 std::string name_buf = std::string (name, prefix_len) + "___L";
11496 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11497 {
11498 lim_warning (_("Unknown lower bound, using 1."));
11499 L = 1;
11500 }
11501 }
14f9c5c9 11502
d2e4a39e 11503 if (*subtype_info == 'U')
dda83cd7
SM
11504 {
11505 if (!ada_scan_number (bounds_str, n, &U, &n)
11506 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11507 return raw_type;
11508 }
d2e4a39e 11509 else
dda83cd7 11510 {
5f9febe0
TT
11511 std::string name_buf = std::string (name, prefix_len) + "___U";
11512 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11513 {
11514 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11515 U = L;
11516 }
11517 }
14f9c5c9 11518
0c9c3474
SA
11519 type = create_static_range_type (alloc_type_copy (raw_type),
11520 base_type, L, U);
f5a91472 11521 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11522 to match the size of the base_type, which is not what we want.
11523 Set it back to the original range type's length. */
f5a91472 11524 TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
d0e39ea2 11525 type->set_name (name);
14f9c5c9
AS
11526 return type;
11527 }
11528}
11529
4c4b4cd2
PH
11530/* True iff NAME is the name of a range type. */
11531
14f9c5c9 11532int
d2e4a39e 11533ada_is_range_type_name (const char *name)
14f9c5c9
AS
11534{
11535 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11536}
14f9c5c9 11537\f
d2e4a39e 11538
dda83cd7 11539 /* Modular types */
4c4b4cd2
PH
11540
11541/* True iff TYPE is an Ada modular type. */
14f9c5c9 11542
14f9c5c9 11543int
d2e4a39e 11544ada_is_modular_type (struct type *type)
14f9c5c9 11545{
18af8284 11546 struct type *subranged_type = get_base_type (type);
14f9c5c9 11547
78134374 11548 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11549 && subranged_type->code () == TYPE_CODE_INT
11550 && subranged_type->is_unsigned ());
14f9c5c9
AS
11551}
11552
4c4b4cd2
PH
11553/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11554
61ee279c 11555ULONGEST
0056e4d5 11556ada_modulus (struct type *type)
14f9c5c9 11557{
5e500d33
SM
11558 const dynamic_prop &high = type->bounds ()->high;
11559
11560 if (high.kind () == PROP_CONST)
11561 return (ULONGEST) high.const_val () + 1;
11562
11563 /* If TYPE is unresolved, the high bound might be a location list. Return
11564 0, for lack of a better value to return. */
11565 return 0;
14f9c5c9 11566}
d2e4a39e 11567\f
f7f9143b
JB
11568
11569/* Ada exception catchpoint support:
11570 ---------------------------------
11571
11572 We support 3 kinds of exception catchpoints:
11573 . catchpoints on Ada exceptions
11574 . catchpoints on unhandled Ada exceptions
11575 . catchpoints on failed assertions
11576
11577 Exceptions raised during failed assertions, or unhandled exceptions
11578 could perfectly be caught with the general catchpoint on Ada exceptions.
11579 However, we can easily differentiate these two special cases, and having
11580 the option to distinguish these two cases from the rest can be useful
11581 to zero-in on certain situations.
11582
11583 Exception catchpoints are a specialized form of breakpoint,
11584 since they rely on inserting breakpoints inside known routines
11585 of the GNAT runtime. The implementation therefore uses a standard
11586 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11587 of breakpoint_ops.
11588
0259addd
JB
11589 Support in the runtime for exception catchpoints have been changed
11590 a few times already, and these changes affect the implementation
11591 of these catchpoints. In order to be able to support several
11592 variants of the runtime, we use a sniffer that will determine
28010a5d 11593 the runtime variant used by the program being debugged. */
f7f9143b 11594
82eacd52
JB
11595/* Ada's standard exceptions.
11596
11597 The Ada 83 standard also defined Numeric_Error. But there so many
11598 situations where it was unclear from the Ada 83 Reference Manual
11599 (RM) whether Constraint_Error or Numeric_Error should be raised,
11600 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11601 Interpretation saying that anytime the RM says that Numeric_Error
11602 should be raised, the implementation may raise Constraint_Error.
11603 Ada 95 went one step further and pretty much removed Numeric_Error
11604 from the list of standard exceptions (it made it a renaming of
11605 Constraint_Error, to help preserve compatibility when compiling
11606 an Ada83 compiler). As such, we do not include Numeric_Error from
11607 this list of standard exceptions. */
3d0b0fa3 11608
27087b7f 11609static const char * const standard_exc[] = {
3d0b0fa3
JB
11610 "constraint_error",
11611 "program_error",
11612 "storage_error",
11613 "tasking_error"
11614};
11615
0259addd
JB
11616typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11617
11618/* A structure that describes how to support exception catchpoints
11619 for a given executable. */
11620
11621struct exception_support_info
11622{
11623 /* The name of the symbol to break on in order to insert
11624 a catchpoint on exceptions. */
11625 const char *catch_exception_sym;
11626
11627 /* The name of the symbol to break on in order to insert
11628 a catchpoint on unhandled exceptions. */
11629 const char *catch_exception_unhandled_sym;
11630
11631 /* The name of the symbol to break on in order to insert
11632 a catchpoint on failed assertions. */
11633 const char *catch_assert_sym;
11634
9f757bf7
XR
11635 /* The name of the symbol to break on in order to insert
11636 a catchpoint on exception handling. */
11637 const char *catch_handlers_sym;
11638
0259addd
JB
11639 /* Assuming that the inferior just triggered an unhandled exception
11640 catchpoint, this function is responsible for returning the address
11641 in inferior memory where the name of that exception is stored.
11642 Return zero if the address could not be computed. */
11643 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11644};
11645
11646static CORE_ADDR ada_unhandled_exception_name_addr (void);
11647static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11648
11649/* The following exception support info structure describes how to
11650 implement exception catchpoints with the latest version of the
ca683e3a 11651 Ada runtime (as of 2019-08-??). */
0259addd
JB
11652
11653static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11654{
11655 "__gnat_debug_raise_exception", /* catch_exception_sym */
11656 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11657 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11658 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11659 ada_unhandled_exception_name_addr
11660};
11661
11662/* The following exception support info structure describes how to
11663 implement exception catchpoints with an earlier version of the
11664 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11665
11666static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11667{
11668 "__gnat_debug_raise_exception", /* catch_exception_sym */
11669 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11670 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11671 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11672 ada_unhandled_exception_name_addr
11673};
11674
11675/* The following exception support info structure describes how to
11676 implement exception catchpoints with a slightly older version
11677 of the Ada runtime. */
11678
11679static const struct exception_support_info exception_support_info_fallback =
11680{
11681 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11682 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11683 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11684 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11685 ada_unhandled_exception_name_addr_from_raise
11686};
11687
f17011e0
JB
11688/* Return nonzero if we can detect the exception support routines
11689 described in EINFO.
11690
11691 This function errors out if an abnormal situation is detected
11692 (for instance, if we find the exception support routines, but
11693 that support is found to be incomplete). */
11694
11695static int
11696ada_has_this_exception_support (const struct exception_support_info *einfo)
11697{
11698 struct symbol *sym;
11699
11700 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11701 that should be compiled with debugging information. As a result, we
11702 expect to find that symbol in the symtabs. */
11703
11704 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11705 if (sym == NULL)
a6af7abe
JB
11706 {
11707 /* Perhaps we did not find our symbol because the Ada runtime was
11708 compiled without debugging info, or simply stripped of it.
11709 It happens on some GNU/Linux distributions for instance, where
11710 users have to install a separate debug package in order to get
11711 the runtime's debugging info. In that situation, let the user
11712 know why we cannot insert an Ada exception catchpoint.
11713
11714 Note: Just for the purpose of inserting our Ada exception
11715 catchpoint, we could rely purely on the associated minimal symbol.
11716 But we would be operating in degraded mode anyway, since we are
11717 still lacking the debugging info needed later on to extract
11718 the name of the exception being raised (this name is printed in
11719 the catchpoint message, and is also used when trying to catch
11720 a specific exception). We do not handle this case for now. */
3b7344d5 11721 struct bound_minimal_symbol msym
1c8e84b0
JB
11722 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11723
60f62e2b 11724 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
a6af7abe
JB
11725 error (_("Your Ada runtime appears to be missing some debugging "
11726 "information.\nCannot insert Ada exception catchpoint "
11727 "in this configuration."));
11728
11729 return 0;
11730 }
f17011e0
JB
11731
11732 /* Make sure that the symbol we found corresponds to a function. */
11733
66d7f48f 11734 if (sym->aclass () != LOC_BLOCK)
ca683e3a
AO
11735 {
11736 error (_("Symbol \"%s\" is not a function (class = %d)"),
66d7f48f 11737 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11738 return 0;
11739 }
11740
11741 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11742 if (sym == NULL)
11743 {
11744 struct bound_minimal_symbol msym
11745 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11746
60f62e2b 11747 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
ca683e3a
AO
11748 error (_("Your Ada runtime appears to be missing some debugging "
11749 "information.\nCannot insert Ada exception catchpoint "
11750 "in this configuration."));
11751
11752 return 0;
11753 }
11754
11755 /* Make sure that the symbol we found corresponds to a function. */
11756
66d7f48f 11757 if (sym->aclass () != LOC_BLOCK)
ca683e3a
AO
11758 {
11759 error (_("Symbol \"%s\" is not a function (class = %d)"),
66d7f48f 11760 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11761 return 0;
11762 }
f17011e0
JB
11763
11764 return 1;
11765}
11766
0259addd
JB
11767/* Inspect the Ada runtime and determine which exception info structure
11768 should be used to provide support for exception catchpoints.
11769
3eecfa55
JB
11770 This function will always set the per-inferior exception_info,
11771 or raise an error. */
0259addd
JB
11772
11773static void
11774ada_exception_support_info_sniffer (void)
11775{
3eecfa55 11776 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11777
11778 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11779 if (data->exception_info != NULL)
0259addd
JB
11780 return;
11781
11782 /* Check the latest (default) exception support info. */
f17011e0 11783 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11784 {
3eecfa55 11785 data->exception_info = &default_exception_support_info;
0259addd
JB
11786 return;
11787 }
11788
ca683e3a
AO
11789 /* Try the v0 exception suport info. */
11790 if (ada_has_this_exception_support (&exception_support_info_v0))
11791 {
11792 data->exception_info = &exception_support_info_v0;
11793 return;
11794 }
11795
0259addd 11796 /* Try our fallback exception suport info. */
f17011e0 11797 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11798 {
3eecfa55 11799 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11800 return;
11801 }
11802
11803 /* Sometimes, it is normal for us to not be able to find the routine
11804 we are looking for. This happens when the program is linked with
11805 the shared version of the GNAT runtime, and the program has not been
11806 started yet. Inform the user of these two possible causes if
11807 applicable. */
11808
ccefe4c4 11809 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11810 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11811
11812 /* If the symbol does not exist, then check that the program is
11813 already started, to make sure that shared libraries have been
11814 loaded. If it is not started, this may mean that the symbol is
11815 in a shared library. */
11816
e99b03dc 11817 if (inferior_ptid.pid () == 0)
0259addd
JB
11818 error (_("Unable to insert catchpoint. Try to start the program first."));
11819
11820 /* At this point, we know that we are debugging an Ada program and
11821 that the inferior has been started, but we still are not able to
0963b4bd 11822 find the run-time symbols. That can mean that we are in
0259addd
JB
11823 configurable run time mode, or that a-except as been optimized
11824 out by the linker... In any case, at this point it is not worth
11825 supporting this feature. */
11826
7dda8cff 11827 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11828}
11829
f7f9143b
JB
11830/* True iff FRAME is very likely to be that of a function that is
11831 part of the runtime system. This is all very heuristic, but is
11832 intended to be used as advice as to what frames are uninteresting
11833 to most users. */
11834
11835static int
11836is_known_support_routine (struct frame_info *frame)
11837{
692465f1 11838 enum language func_lang;
f7f9143b 11839 int i;
f35a17b5 11840 const char *fullname;
f7f9143b 11841
4ed6b5be
JB
11842 /* If this code does not have any debugging information (no symtab),
11843 This cannot be any user code. */
f7f9143b 11844
51abb421 11845 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11846 if (sal.symtab == NULL)
11847 return 1;
11848
4ed6b5be
JB
11849 /* If there is a symtab, but the associated source file cannot be
11850 located, then assume this is not user code: Selecting a frame
11851 for which we cannot display the code would not be very helpful
11852 for the user. This should also take care of case such as VxWorks
11853 where the kernel has some debugging info provided for a few units. */
f7f9143b 11854
f35a17b5
JK
11855 fullname = symtab_to_fullname (sal.symtab);
11856 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11857 return 1;
11858
85102364 11859 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11860 We also check the name of the objfile against the name of some
11861 known system libraries that sometimes come with debugging info
11862 too. */
11863
f7f9143b
JB
11864 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11865 {
11866 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11867 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11868 return 1;
3c86fae3
SM
11869 if (sal.symtab->compunit ()->objfile () != NULL
11870 && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
dda83cd7 11871 return 1;
f7f9143b
JB
11872 }
11873
4ed6b5be 11874 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11875
c6dc63a1
TT
11876 gdb::unique_xmalloc_ptr<char> func_name
11877 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11878 if (func_name == NULL)
11879 return 1;
11880
11881 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11882 {
11883 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11884 if (re_exec (func_name.get ()))
11885 return 1;
f7f9143b
JB
11886 }
11887
11888 return 0;
11889}
11890
11891/* Find the first frame that contains debugging information and that is not
11892 part of the Ada run-time, starting from FI and moving upward. */
11893
0ef643c8 11894void
f7f9143b
JB
11895ada_find_printable_frame (struct frame_info *fi)
11896{
11897 for (; fi != NULL; fi = get_prev_frame (fi))
11898 {
11899 if (!is_known_support_routine (fi))
dda83cd7
SM
11900 {
11901 select_frame (fi);
11902 break;
11903 }
f7f9143b
JB
11904 }
11905
11906}
11907
11908/* Assuming that the inferior just triggered an unhandled exception
11909 catchpoint, return the address in inferior memory where the name
11910 of the exception is stored.
11911
11912 Return zero if the address could not be computed. */
11913
11914static CORE_ADDR
11915ada_unhandled_exception_name_addr (void)
0259addd
JB
11916{
11917 return parse_and_eval_address ("e.full_name");
11918}
11919
11920/* Same as ada_unhandled_exception_name_addr, except that this function
11921 should be used when the inferior uses an older version of the runtime,
11922 where the exception name needs to be extracted from a specific frame
11923 several frames up in the callstack. */
11924
11925static CORE_ADDR
11926ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11927{
11928 int frame_level;
11929 struct frame_info *fi;
3eecfa55 11930 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11931
11932 /* To determine the name of this exception, we need to select
11933 the frame corresponding to RAISE_SYM_NAME. This frame is
11934 at least 3 levels up, so we simply skip the first 3 frames
11935 without checking the name of their associated function. */
11936 fi = get_current_frame ();
11937 for (frame_level = 0; frame_level < 3; frame_level += 1)
11938 if (fi != NULL)
11939 fi = get_prev_frame (fi);
11940
11941 while (fi != NULL)
11942 {
692465f1
JB
11943 enum language func_lang;
11944
c6dc63a1
TT
11945 gdb::unique_xmalloc_ptr<char> func_name
11946 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11947 if (func_name != NULL)
11948 {
dda83cd7 11949 if (strcmp (func_name.get (),
55b87a52
KS
11950 data->exception_info->catch_exception_sym) == 0)
11951 break; /* We found the frame we were looking for... */
55b87a52 11952 }
fb44b1a7 11953 fi = get_prev_frame (fi);
f7f9143b
JB
11954 }
11955
11956 if (fi == NULL)
11957 return 0;
11958
11959 select_frame (fi);
11960 return parse_and_eval_address ("id.full_name");
11961}
11962
11963/* Assuming the inferior just triggered an Ada exception catchpoint
11964 (of any type), return the address in inferior memory where the name
11965 of the exception is stored, if applicable.
11966
45db7c09
PA
11967 Assumes the selected frame is the current frame.
11968
f7f9143b
JB
11969 Return zero if the address could not be computed, or if not relevant. */
11970
11971static CORE_ADDR
7bd86313 11972ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex)
f7f9143b 11973{
3eecfa55
JB
11974 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11975
f7f9143b
JB
11976 switch (ex)
11977 {
761269c8 11978 case ada_catch_exception:
dda83cd7
SM
11979 return (parse_and_eval_address ("e.full_name"));
11980 break;
f7f9143b 11981
761269c8 11982 case ada_catch_exception_unhandled:
dda83cd7
SM
11983 return data->exception_info->unhandled_exception_name_addr ();
11984 break;
9f757bf7
XR
11985
11986 case ada_catch_handlers:
dda83cd7 11987 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11988 name. */
dda83cd7 11989 break;
9f757bf7 11990
761269c8 11991 case ada_catch_assert:
dda83cd7
SM
11992 return 0; /* Exception name is not relevant in this case. */
11993 break;
f7f9143b
JB
11994
11995 default:
dda83cd7
SM
11996 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
11997 break;
f7f9143b
JB
11998 }
11999
12000 return 0; /* Should never be reached. */
12001}
12002
e547c119
JB
12003/* Assuming the inferior is stopped at an exception catchpoint,
12004 return the message which was associated to the exception, if
12005 available. Return NULL if the message could not be retrieved.
12006
e547c119
JB
12007 Note: The exception message can be associated to an exception
12008 either through the use of the Raise_Exception function, or
12009 more simply (Ada 2005 and later), via:
12010
12011 raise Exception_Name with "exception message";
12012
12013 */
12014
6f46ac85 12015static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12016ada_exception_message_1 (void)
12017{
12018 struct value *e_msg_val;
e547c119 12019 int e_msg_len;
e547c119
JB
12020
12021 /* For runtimes that support this feature, the exception message
12022 is passed as an unbounded string argument called "message". */
12023 e_msg_val = parse_and_eval ("message");
12024 if (e_msg_val == NULL)
12025 return NULL; /* Exception message not supported. */
12026
12027 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
12028 gdb_assert (e_msg_val != NULL);
12029 e_msg_len = TYPE_LENGTH (value_type (e_msg_val));
12030
12031 /* If the message string is empty, then treat it as if there was
12032 no exception message. */
12033 if (e_msg_len <= 0)
12034 return NULL;
12035
15f3b077
TT
12036 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
12037 read_memory (value_address (e_msg_val), (gdb_byte *) e_msg.get (),
12038 e_msg_len);
12039 e_msg.get ()[e_msg_len] = '\0';
12040
12041 return e_msg;
e547c119
JB
12042}
12043
12044/* Same as ada_exception_message_1, except that all exceptions are
12045 contained here (returning NULL instead). */
12046
6f46ac85 12047static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12048ada_exception_message (void)
12049{
6f46ac85 12050 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 12051
a70b8144 12052 try
e547c119
JB
12053 {
12054 e_msg = ada_exception_message_1 ();
12055 }
230d2906 12056 catch (const gdb_exception_error &e)
e547c119 12057 {
6f46ac85 12058 e_msg.reset (nullptr);
e547c119 12059 }
e547c119
JB
12060
12061 return e_msg;
12062}
12063
f7f9143b
JB
12064/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12065 any error that ada_exception_name_addr_1 might cause to be thrown.
12066 When an error is intercepted, a warning with the error message is printed,
12067 and zero is returned. */
12068
12069static CORE_ADDR
7bd86313 12070ada_exception_name_addr (enum ada_exception_catchpoint_kind ex)
f7f9143b 12071{
f7f9143b
JB
12072 CORE_ADDR result = 0;
12073
a70b8144 12074 try
f7f9143b 12075 {
7bd86313 12076 result = ada_exception_name_addr_1 (ex);
f7f9143b
JB
12077 }
12078
230d2906 12079 catch (const gdb_exception_error &e)
f7f9143b 12080 {
3d6e9d23 12081 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12082 return 0;
12083 }
12084
12085 return result;
12086}
12087
cb7de75e 12088static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12089 (const char *excep_string,
12090 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12091
12092/* Ada catchpoints.
12093
12094 In the case of catchpoints on Ada exceptions, the catchpoint will
12095 stop the target on every exception the program throws. When a user
12096 specifies the name of a specific exception, we translate this
12097 request into a condition expression (in text form), and then parse
12098 it into an expression stored in each of the catchpoint's locations.
12099 We then use this condition to check whether the exception that was
12100 raised is the one the user is interested in. If not, then the
12101 target is resumed again. We store the name of the requested
12102 exception, in order to be able to re-set the condition expression
12103 when symbols change. */
12104
c1fc2657 12105/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12106
73a7911e 12107struct ada_catchpoint : public base_breakpoint
28010a5d 12108{
73063f51
TT
12109 ada_catchpoint (struct gdbarch *gdbarch_,
12110 enum ada_exception_catchpoint_kind kind)
12111 : base_breakpoint (gdbarch_, bp_catchpoint),
12112 m_kind (kind)
37f6a7f4
TT
12113 {
12114 }
12115
ae72050b
TT
12116 struct bp_location *allocate_location () override;
12117 void re_set () override;
12118 void check_status (struct bpstat *bs) override;
7bd86313 12119 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 12120 bool print_one (bp_location **) const override;
b713485d 12121 void print_mention () const override;
4d1ae558 12122 void print_recreate (struct ui_file *fp) const override;
ae72050b 12123
28010a5d 12124 /* The name of the specific exception the user specified. */
bc18fbb5 12125 std::string excep_string;
37f6a7f4
TT
12126
12127 /* What kind of catchpoint this is. */
12128 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12129};
12130
8cd0bf5e
PA
12131/* An instance of this type is used to represent an Ada catchpoint
12132 breakpoint location. */
12133
12134class ada_catchpoint_location : public bp_location
12135{
12136public:
12137 explicit ada_catchpoint_location (ada_catchpoint *owner)
12138 : bp_location (owner, bp_loc_software_breakpoint)
12139 {}
12140
12141 /* The condition that checks whether the exception that was raised
12142 is the specific exception the user specified on catchpoint
12143 creation. */
12144 expression_up excep_cond_expr;
12145};
12146
28010a5d
PA
12147/* Parse the exception condition string in the context of each of the
12148 catchpoint's locations, and store them for later evaluation. */
12149
12150static void
9f757bf7 12151create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 12152 enum ada_exception_catchpoint_kind ex)
28010a5d 12153{
28010a5d 12154 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 12155 if (c->excep_string.empty ())
28010a5d
PA
12156 return;
12157
12158 /* Same if there are no locations... */
c1fc2657 12159 if (c->loc == NULL)
28010a5d
PA
12160 return;
12161
fccf9de1
TT
12162 /* Compute the condition expression in text form, from the specific
12163 expection we want to catch. */
12164 std::string cond_string
12165 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 12166
fccf9de1
TT
12167 /* Iterate over all the catchpoint's locations, and parse an
12168 expression for each. */
40cb8ca5 12169 for (bp_location *bl : c->locations ())
28010a5d
PA
12170 {
12171 struct ada_catchpoint_location *ada_loc
fccf9de1 12172 = (struct ada_catchpoint_location *) bl;
4d01a485 12173 expression_up exp;
28010a5d 12174
fccf9de1 12175 if (!bl->shlib_disabled)
28010a5d 12176 {
bbc13ae3 12177 const char *s;
28010a5d 12178
cb7de75e 12179 s = cond_string.c_str ();
a70b8144 12180 try
28010a5d 12181 {
fccf9de1
TT
12182 exp = parse_exp_1 (&s, bl->address,
12183 block_for_pc (bl->address),
036e657b 12184 0);
28010a5d 12185 }
230d2906 12186 catch (const gdb_exception_error &e)
849f2b52
JB
12187 {
12188 warning (_("failed to reevaluate internal exception condition "
12189 "for catchpoint %d: %s"),
3d6e9d23 12190 c->number, e.what ());
849f2b52 12191 }
28010a5d
PA
12192 }
12193
b22e99fd 12194 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 12195 }
28010a5d
PA
12196}
12197
ae72050b
TT
12198/* Implement the ALLOCATE_LOCATION method in the structure for all
12199 exception catchpoint kinds. */
28010a5d 12200
ae72050b
TT
12201struct bp_location *
12202ada_catchpoint::allocate_location ()
28010a5d 12203{
ae72050b 12204 return new ada_catchpoint_location (this);
28010a5d
PA
12205}
12206
ae72050b
TT
12207/* Implement the RE_SET method in the structure for all exception
12208 catchpoint kinds. */
28010a5d 12209
ae72050b
TT
12210void
12211ada_catchpoint::re_set ()
28010a5d 12212{
28010a5d
PA
12213 /* Call the base class's method. This updates the catchpoint's
12214 locations. */
17796891 12215 this->base_breakpoint::re_set ();
28010a5d
PA
12216
12217 /* Reparse the exception conditional expressions. One for each
12218 location. */
ae72050b 12219 create_excep_cond_exprs (this, m_kind);
28010a5d
PA
12220}
12221
12222/* Returns true if we should stop for this breakpoint hit. If the
12223 user specified a specific exception, we only want to cause a stop
12224 if the program thrown that exception. */
12225
7ebaa5f7 12226static bool
28010a5d
PA
12227should_stop_exception (const struct bp_location *bl)
12228{
12229 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12230 const struct ada_catchpoint_location *ada_loc
12231 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12232 bool stop;
28010a5d 12233
37f6a7f4
TT
12234 struct internalvar *var = lookup_internalvar ("_ada_exception");
12235 if (c->m_kind == ada_catch_assert)
12236 clear_internalvar (var);
12237 else
12238 {
12239 try
12240 {
12241 const char *expr;
12242
12243 if (c->m_kind == ada_catch_handlers)
12244 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12245 ".all.occurrence.id");
12246 else
12247 expr = "e";
12248
12249 struct value *exc = parse_and_eval (expr);
12250 set_internalvar (var, exc);
12251 }
12252 catch (const gdb_exception_error &ex)
12253 {
12254 clear_internalvar (var);
12255 }
12256 }
12257
28010a5d 12258 /* With no specific exception, should always stop. */
bc18fbb5 12259 if (c->excep_string.empty ())
7ebaa5f7 12260 return true;
28010a5d
PA
12261
12262 if (ada_loc->excep_cond_expr == NULL)
12263 {
12264 /* We will have a NULL expression if back when we were creating
12265 the expressions, this location's had failed to parse. */
7ebaa5f7 12266 return true;
28010a5d
PA
12267 }
12268
7ebaa5f7 12269 stop = true;
a70b8144 12270 try
28010a5d
PA
12271 {
12272 struct value *mark;
12273
12274 mark = value_mark ();
4d01a485 12275 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
28010a5d
PA
12276 value_free_to_mark (mark);
12277 }
230d2906 12278 catch (const gdb_exception &ex)
492d29ea
PA
12279 {
12280 exception_fprintf (gdb_stderr, ex,
12281 _("Error in testing exception condition:\n"));
12282 }
492d29ea 12283
28010a5d
PA
12284 return stop;
12285}
12286
ae72050b
TT
12287/* Implement the CHECK_STATUS method in the structure for all
12288 exception catchpoint kinds. */
28010a5d 12289
ae72050b
TT
12290void
12291ada_catchpoint::check_status (bpstat *bs)
28010a5d 12292{
b6433ede 12293 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12294}
12295
ae72050b
TT
12296/* Implement the PRINT_IT method in the structure for all exception
12297 catchpoint kinds. */
f7f9143b 12298
ae72050b 12299enum print_stop_action
7bd86313 12300ada_catchpoint::print_it (const bpstat *bs) const
f7f9143b 12301{
79a45e25 12302 struct ui_out *uiout = current_uiout;
348d480f 12303
ae72050b 12304 annotate_catchpoint (number);
f7f9143b 12305
112e8700 12306 if (uiout->is_mi_like_p ())
f7f9143b 12307 {
112e8700 12308 uiout->field_string ("reason",
956a9fb9 12309 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ae72050b 12310 uiout->field_string ("disp", bpdisp_text (disposition));
f7f9143b
JB
12311 }
12312
ae72050b 12313 uiout->text (disposition == disp_del
112e8700 12314 ? "\nTemporary catchpoint " : "\nCatchpoint ");
ae72050b 12315 uiout->field_signed ("bkptno", number);
112e8700 12316 uiout->text (", ");
f7f9143b 12317
45db7c09
PA
12318 /* ada_exception_name_addr relies on the selected frame being the
12319 current frame. Need to do this here because this function may be
12320 called more than once when printing a stop, and below, we'll
12321 select the first frame past the Ada run-time (see
12322 ada_find_printable_frame). */
12323 select_frame (get_current_frame ());
12324
ae72050b 12325 switch (m_kind)
f7f9143b 12326 {
761269c8
JB
12327 case ada_catch_exception:
12328 case ada_catch_exception_unhandled:
9f757bf7 12329 case ada_catch_handlers:
956a9fb9 12330 {
7bd86313 12331 const CORE_ADDR addr = ada_exception_name_addr (m_kind);
956a9fb9
JB
12332 char exception_name[256];
12333
12334 if (addr != 0)
12335 {
c714b426
PA
12336 read_memory (addr, (gdb_byte *) exception_name,
12337 sizeof (exception_name) - 1);
956a9fb9
JB
12338 exception_name [sizeof (exception_name) - 1] = '\0';
12339 }
12340 else
12341 {
12342 /* For some reason, we were unable to read the exception
12343 name. This could happen if the Runtime was compiled
12344 without debugging info, for instance. In that case,
12345 just replace the exception name by the generic string
12346 "exception" - it will read as "an exception" in the
12347 notification we are about to print. */
967cff16 12348 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12349 }
12350 /* In the case of unhandled exception breakpoints, we print
12351 the exception name as "unhandled EXCEPTION_NAME", to make
12352 it clearer to the user which kind of catchpoint just got
12353 hit. We used ui_out_text to make sure that this extra
12354 info does not pollute the exception name in the MI case. */
ae72050b 12355 if (m_kind == ada_catch_exception_unhandled)
112e8700
SM
12356 uiout->text ("unhandled ");
12357 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12358 }
12359 break;
761269c8 12360 case ada_catch_assert:
956a9fb9
JB
12361 /* In this case, the name of the exception is not really
12362 important. Just print "failed assertion" to make it clearer
12363 that his program just hit an assertion-failure catchpoint.
12364 We used ui_out_text because this info does not belong in
12365 the MI output. */
112e8700 12366 uiout->text ("failed assertion");
956a9fb9 12367 break;
f7f9143b 12368 }
e547c119 12369
6f46ac85 12370 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12371 if (exception_message != NULL)
12372 {
e547c119 12373 uiout->text (" (");
6f46ac85 12374 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12375 uiout->text (")");
e547c119
JB
12376 }
12377
112e8700 12378 uiout->text (" at ");
956a9fb9 12379 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12380
12381 return PRINT_SRC_AND_LOC;
12382}
12383
ae72050b
TT
12384/* Implement the PRINT_ONE method in the structure for all exception
12385 catchpoint kinds. */
f7f9143b 12386
ae72050b 12387bool
a67bcaba 12388ada_catchpoint::print_one (bp_location **last_loc) const
f7f9143b 12389{
79a45e25 12390 struct ui_out *uiout = current_uiout;
79a45b7d
TT
12391 struct value_print_options opts;
12392
12393 get_user_print_options (&opts);
f06f1252 12394
79a45b7d 12395 if (opts.addressprint)
f06f1252 12396 uiout->field_skip ("addr");
f7f9143b
JB
12397
12398 annotate_field (5);
ae72050b 12399 switch (m_kind)
f7f9143b 12400 {
761269c8 12401 case ada_catch_exception:
ae72050b 12402 if (!excep_string.empty ())
dda83cd7 12403 {
bc18fbb5 12404 std::string msg = string_printf (_("`%s' Ada exception"),
ae72050b 12405 excep_string.c_str ());
28010a5d 12406
dda83cd7
SM
12407 uiout->field_string ("what", msg);
12408 }
12409 else
12410 uiout->field_string ("what", "all Ada exceptions");
12411
12412 break;
f7f9143b 12413
761269c8 12414 case ada_catch_exception_unhandled:
dda83cd7
SM
12415 uiout->field_string ("what", "unhandled Ada exceptions");
12416 break;
f7f9143b 12417
9f757bf7 12418 case ada_catch_handlers:
ae72050b 12419 if (!excep_string.empty ())
dda83cd7 12420 {
9f757bf7
XR
12421 uiout->field_fmt ("what",
12422 _("`%s' Ada exception handlers"),
ae72050b 12423 excep_string.c_str ());
dda83cd7
SM
12424 }
12425 else
9f757bf7 12426 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12427 break;
9f757bf7 12428
761269c8 12429 case ada_catch_assert:
dda83cd7
SM
12430 uiout->field_string ("what", "failed Ada assertions");
12431 break;
f7f9143b
JB
12432
12433 default:
dda83cd7
SM
12434 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12435 break;
f7f9143b 12436 }
c01e038b
TT
12437
12438 return true;
f7f9143b
JB
12439}
12440
12441/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12442 for all exception catchpoint kinds. */
12443
ae72050b 12444void
b713485d 12445ada_catchpoint::print_mention () const
f7f9143b 12446{
79a45e25 12447 struct ui_out *uiout = current_uiout;
28010a5d 12448
ae72050b 12449 uiout->text (disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12450 : _("Catchpoint "));
ae72050b 12451 uiout->field_signed ("bkptno", number);
112e8700 12452 uiout->text (": ");
00eb2c4a 12453
ae72050b 12454 switch (m_kind)
f7f9143b 12455 {
761269c8 12456 case ada_catch_exception:
ae72050b 12457 if (!excep_string.empty ())
00eb2c4a 12458 {
862d101a 12459 std::string info = string_printf (_("`%s' Ada exception"),
ae72050b 12460 excep_string.c_str ());
4915bfdc 12461 uiout->text (info);
00eb2c4a 12462 }
dda83cd7
SM
12463 else
12464 uiout->text (_("all Ada exceptions"));
12465 break;
f7f9143b 12466
761269c8 12467 case ada_catch_exception_unhandled:
dda83cd7
SM
12468 uiout->text (_("unhandled Ada exceptions"));
12469 break;
9f757bf7
XR
12470
12471 case ada_catch_handlers:
ae72050b 12472 if (!excep_string.empty ())
9f757bf7
XR
12473 {
12474 std::string info
12475 = string_printf (_("`%s' Ada exception handlers"),
ae72050b 12476 excep_string.c_str ());
4915bfdc 12477 uiout->text (info);
9f757bf7 12478 }
dda83cd7
SM
12479 else
12480 uiout->text (_("all Ada exceptions handlers"));
12481 break;
9f757bf7 12482
761269c8 12483 case ada_catch_assert:
dda83cd7
SM
12484 uiout->text (_("failed Ada assertions"));
12485 break;
f7f9143b
JB
12486
12487 default:
dda83cd7
SM
12488 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12489 break;
f7f9143b
JB
12490 }
12491}
12492
ae72050b
TT
12493/* Implement the PRINT_RECREATE method in the structure for all
12494 exception catchpoint kinds. */
6149aea9 12495
ae72050b 12496void
4d1ae558 12497ada_catchpoint::print_recreate (struct ui_file *fp) const
6149aea9 12498{
ae72050b 12499 switch (m_kind)
6149aea9 12500 {
761269c8 12501 case ada_catch_exception:
6cb06a8c 12502 gdb_printf (fp, "catch exception");
ae72050b
TT
12503 if (!excep_string.empty ())
12504 gdb_printf (fp, " %s", excep_string.c_str ());
6149aea9
PA
12505 break;
12506
761269c8 12507 case ada_catch_exception_unhandled:
6cb06a8c 12508 gdb_printf (fp, "catch exception unhandled");
6149aea9
PA
12509 break;
12510
9f757bf7 12511 case ada_catch_handlers:
6cb06a8c 12512 gdb_printf (fp, "catch handlers");
9f757bf7
XR
12513 break;
12514
761269c8 12515 case ada_catch_assert:
6cb06a8c 12516 gdb_printf (fp, "catch assert");
6149aea9
PA
12517 break;
12518
12519 default:
12520 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12521 }
04d0163c 12522 print_recreate_thread (fp);
6149aea9
PA
12523}
12524
f06f1252
TT
12525/* See ada-lang.h. */
12526
12527bool
12528is_ada_exception_catchpoint (breakpoint *bp)
12529{
ae72050b 12530 return dynamic_cast<ada_catchpoint *> (bp) != nullptr;
f06f1252
TT
12531}
12532
f7f9143b
JB
12533/* Split the arguments specified in a "catch exception" command.
12534 Set EX to the appropriate catchpoint type.
28010a5d 12535 Set EXCEP_STRING to the name of the specific exception if
5845583d 12536 specified by the user.
9f757bf7
XR
12537 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12538 "catch handlers" command. False otherwise.
5845583d
JB
12539 If a condition is found at the end of the arguments, the condition
12540 expression is stored in COND_STRING (memory must be deallocated
12541 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12542
12543static void
a121b7c1 12544catch_ada_exception_command_split (const char *args,
9f757bf7 12545 bool is_catch_handlers_cmd,
dda83cd7 12546 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12547 std::string *excep_string,
12548 std::string *cond_string)
f7f9143b 12549{
bc18fbb5 12550 std::string exception_name;
f7f9143b 12551
bc18fbb5
TT
12552 exception_name = extract_arg (&args);
12553 if (exception_name == "if")
5845583d
JB
12554 {
12555 /* This is not an exception name; this is the start of a condition
12556 expression for a catchpoint on all exceptions. So, "un-get"
12557 this token, and set exception_name to NULL. */
bc18fbb5 12558 exception_name.clear ();
5845583d
JB
12559 args -= 2;
12560 }
f7f9143b 12561
5845583d 12562 /* Check to see if we have a condition. */
f7f9143b 12563
f1735a53 12564 args = skip_spaces (args);
61012eef 12565 if (startswith (args, "if")
5845583d
JB
12566 && (isspace (args[2]) || args[2] == '\0'))
12567 {
12568 args += 2;
f1735a53 12569 args = skip_spaces (args);
5845583d
JB
12570
12571 if (args[0] == '\0')
dda83cd7 12572 error (_("Condition missing after `if' keyword"));
bc18fbb5 12573 *cond_string = args;
5845583d
JB
12574
12575 args += strlen (args);
12576 }
12577
12578 /* Check that we do not have any more arguments. Anything else
12579 is unexpected. */
f7f9143b
JB
12580
12581 if (args[0] != '\0')
12582 error (_("Junk at end of expression"));
12583
9f757bf7
XR
12584 if (is_catch_handlers_cmd)
12585 {
12586 /* Catch handling of exceptions. */
12587 *ex = ada_catch_handlers;
12588 *excep_string = exception_name;
12589 }
bc18fbb5 12590 else if (exception_name.empty ())
f7f9143b
JB
12591 {
12592 /* Catch all exceptions. */
761269c8 12593 *ex = ada_catch_exception;
bc18fbb5 12594 excep_string->clear ();
f7f9143b 12595 }
bc18fbb5 12596 else if (exception_name == "unhandled")
f7f9143b
JB
12597 {
12598 /* Catch unhandled exceptions. */
761269c8 12599 *ex = ada_catch_exception_unhandled;
bc18fbb5 12600 excep_string->clear ();
f7f9143b
JB
12601 }
12602 else
12603 {
12604 /* Catch a specific exception. */
761269c8 12605 *ex = ada_catch_exception;
28010a5d 12606 *excep_string = exception_name;
f7f9143b
JB
12607 }
12608}
12609
12610/* Return the name of the symbol on which we should break in order to
12611 implement a catchpoint of the EX kind. */
12612
12613static const char *
761269c8 12614ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12615{
3eecfa55
JB
12616 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12617
12618 gdb_assert (data->exception_info != NULL);
0259addd 12619
f7f9143b
JB
12620 switch (ex)
12621 {
761269c8 12622 case ada_catch_exception:
dda83cd7
SM
12623 return (data->exception_info->catch_exception_sym);
12624 break;
761269c8 12625 case ada_catch_exception_unhandled:
dda83cd7
SM
12626 return (data->exception_info->catch_exception_unhandled_sym);
12627 break;
761269c8 12628 case ada_catch_assert:
dda83cd7
SM
12629 return (data->exception_info->catch_assert_sym);
12630 break;
9f757bf7 12631 case ada_catch_handlers:
dda83cd7
SM
12632 return (data->exception_info->catch_handlers_sym);
12633 break;
f7f9143b 12634 default:
dda83cd7
SM
12635 internal_error (__FILE__, __LINE__,
12636 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12637 }
12638}
12639
f7f9143b
JB
12640/* Return the condition that will be used to match the current exception
12641 being raised with the exception that the user wants to catch. This
12642 assumes that this condition is used when the inferior just triggered
12643 an exception catchpoint.
cb7de75e 12644 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12645
cb7de75e 12646static std::string
9f757bf7 12647ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12648 enum ada_exception_catchpoint_kind ex)
f7f9143b 12649{
fccf9de1 12650 bool is_standard_exc = false;
cb7de75e 12651 std::string result;
9f757bf7
XR
12652
12653 if (ex == ada_catch_handlers)
12654 {
12655 /* For exception handlers catchpoints, the condition string does
dda83cd7 12656 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12657 result = ("long_integer (GNAT_GCC_exception_Access"
12658 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12659 }
12660 else
fccf9de1 12661 result = "long_integer (e)";
3d0b0fa3 12662
0963b4bd 12663 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12664 runtime units that have been compiled without debugging info; if
28010a5d 12665 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12666 exception (e.g. "constraint_error") then, during the evaluation
12667 of the condition expression, the symbol lookup on this name would
0963b4bd 12668 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12669 may then be set only on user-defined exceptions which have the
12670 same not-fully-qualified name (e.g. my_package.constraint_error).
12671
12672 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12673 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12674 exception constraint_error" is rewritten into "catch exception
12675 standard.constraint_error".
12676
85102364 12677 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12678 the inferior program, then the only way to specify this exception as a
12679 breakpoint condition is to use its fully-qualified named:
fccf9de1 12680 e.g. my_package.constraint_error. */
3d0b0fa3 12681
696d6f4d 12682 for (const char *name : standard_exc)
3d0b0fa3 12683 {
696d6f4d 12684 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12685 {
fccf9de1 12686 is_standard_exc = true;
9f757bf7 12687 break;
3d0b0fa3
JB
12688 }
12689 }
9f757bf7 12690
fccf9de1
TT
12691 result += " = ";
12692
12693 if (is_standard_exc)
12694 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12695 else
12696 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12697
9f757bf7 12698 return result;
f7f9143b
JB
12699}
12700
12701/* Return the symtab_and_line that should be used to insert an exception
12702 catchpoint of the TYPE kind.
12703
28010a5d
PA
12704 ADDR_STRING returns the name of the function where the real
12705 breakpoint that implements the catchpoints is set, depending on the
12706 type of catchpoint we need to create. */
f7f9143b
JB
12707
12708static struct symtab_and_line
bc18fbb5 12709ada_exception_sal (enum ada_exception_catchpoint_kind ex,
ae72050b 12710 std::string *addr_string)
f7f9143b
JB
12711{
12712 const char *sym_name;
12713 struct symbol *sym;
f7f9143b 12714
0259addd
JB
12715 /* First, find out which exception support info to use. */
12716 ada_exception_support_info_sniffer ();
12717
12718 /* Then lookup the function on which we will break in order to catch
f7f9143b 12719 the Ada exceptions requested by the user. */
f7f9143b
JB
12720 sym_name = ada_exception_sym_name (ex);
12721 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12722
57aff202
JB
12723 if (sym == NULL)
12724 error (_("Catchpoint symbol not found: %s"), sym_name);
12725
66d7f48f 12726 if (sym->aclass () != LOC_BLOCK)
57aff202 12727 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12728
12729 /* Set ADDR_STRING. */
cc12f4a8 12730 *addr_string = sym_name;
f7f9143b 12731
f17011e0 12732 return find_function_start_sal (sym, 1);
f7f9143b
JB
12733}
12734
b4a5b78b 12735/* Create an Ada exception catchpoint.
f7f9143b 12736
b4a5b78b 12737 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12738
bc18fbb5 12739 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12740 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12741 of the exception to which this catchpoint applies.
2df4d1d5 12742
bc18fbb5 12743 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12744
b4a5b78b
JB
12745 TEMPFLAG, if nonzero, means that the underlying breakpoint
12746 should be temporary.
28010a5d 12747
b4a5b78b 12748 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12749
349774ef 12750void
28010a5d 12751create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12752 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12753 const std::string &excep_string,
56ecd069 12754 const std::string &cond_string,
28010a5d 12755 int tempflag,
349774ef 12756 int disabled,
28010a5d
PA
12757 int from_tty)
12758{
cc12f4a8 12759 std::string addr_string;
ae72050b 12760 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string);
28010a5d 12761
73063f51 12762 std::unique_ptr<ada_catchpoint> c (new ada_catchpoint (gdbarch, ex_kind));
cc12f4a8 12763 init_ada_exception_breakpoint (c.get (), gdbarch, sal, addr_string.c_str (),
ae72050b 12764 tempflag, disabled, from_tty);
28010a5d 12765 c->excep_string = excep_string;
9f757bf7 12766 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12767 if (!cond_string.empty ())
733d554a 12768 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12769 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12770}
12771
9ac4176b
PA
12772/* Implement the "catch exception" command. */
12773
12774static void
eb4c3f4a 12775catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12776 struct cmd_list_element *command)
12777{
a121b7c1 12778 const char *arg = arg_entry;
9ac4176b
PA
12779 struct gdbarch *gdbarch = get_current_arch ();
12780 int tempflag;
761269c8 12781 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12782 std::string excep_string;
56ecd069 12783 std::string cond_string;
9ac4176b 12784
0f8e2034 12785 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12786
12787 if (!arg)
12788 arg = "";
9f757bf7 12789 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12790 &cond_string);
9f757bf7
XR
12791 create_ada_exception_catchpoint (gdbarch, ex_kind,
12792 excep_string, cond_string,
12793 tempflag, 1 /* enabled */,
12794 from_tty);
12795}
12796
12797/* Implement the "catch handlers" command. */
12798
12799static void
12800catch_ada_handlers_command (const char *arg_entry, int from_tty,
12801 struct cmd_list_element *command)
12802{
12803 const char *arg = arg_entry;
12804 struct gdbarch *gdbarch = get_current_arch ();
12805 int tempflag;
12806 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12807 std::string excep_string;
56ecd069 12808 std::string cond_string;
9f757bf7 12809
0f8e2034 12810 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12811
12812 if (!arg)
12813 arg = "";
12814 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12815 &cond_string);
b4a5b78b
JB
12816 create_ada_exception_catchpoint (gdbarch, ex_kind,
12817 excep_string, cond_string,
349774ef
JB
12818 tempflag, 1 /* enabled */,
12819 from_tty);
9ac4176b
PA
12820}
12821
71bed2db
TT
12822/* Completion function for the Ada "catch" commands. */
12823
12824static void
12825catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12826 const char *text, const char *word)
12827{
12828 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12829
12830 for (const ada_exc_info &info : exceptions)
12831 {
12832 if (startswith (info.name, word))
b02f78f9 12833 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12834 }
12835}
12836
b4a5b78b 12837/* Split the arguments specified in a "catch assert" command.
5845583d 12838
b4a5b78b
JB
12839 ARGS contains the command's arguments (or the empty string if
12840 no arguments were passed).
5845583d
JB
12841
12842 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12843 (the memory needs to be deallocated after use). */
5845583d 12844
b4a5b78b 12845static void
56ecd069 12846catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12847{
f1735a53 12848 args = skip_spaces (args);
f7f9143b 12849
5845583d 12850 /* Check whether a condition was provided. */
61012eef 12851 if (startswith (args, "if")
5845583d 12852 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12853 {
5845583d 12854 args += 2;
f1735a53 12855 args = skip_spaces (args);
5845583d 12856 if (args[0] == '\0')
dda83cd7 12857 error (_("condition missing after `if' keyword"));
56ecd069 12858 cond_string.assign (args);
f7f9143b
JB
12859 }
12860
5845583d
JB
12861 /* Otherwise, there should be no other argument at the end of
12862 the command. */
12863 else if (args[0] != '\0')
12864 error (_("Junk at end of arguments."));
f7f9143b
JB
12865}
12866
9ac4176b
PA
12867/* Implement the "catch assert" command. */
12868
12869static void
eb4c3f4a 12870catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12871 struct cmd_list_element *command)
12872{
a121b7c1 12873 const char *arg = arg_entry;
9ac4176b
PA
12874 struct gdbarch *gdbarch = get_current_arch ();
12875 int tempflag;
56ecd069 12876 std::string cond_string;
9ac4176b 12877
0f8e2034 12878 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12879
12880 if (!arg)
12881 arg = "";
56ecd069 12882 catch_ada_assert_command_split (arg, cond_string);
761269c8 12883 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 12884 "", cond_string,
349774ef
JB
12885 tempflag, 1 /* enabled */,
12886 from_tty);
9ac4176b 12887}
778865d3
JB
12888
12889/* Return non-zero if the symbol SYM is an Ada exception object. */
12890
12891static int
12892ada_is_exception_sym (struct symbol *sym)
12893{
5f9c5a63 12894 const char *type_name = sym->type ()->name ();
778865d3 12895
66d7f48f
SM
12896 return (sym->aclass () != LOC_TYPEDEF
12897 && sym->aclass () != LOC_BLOCK
12898 && sym->aclass () != LOC_CONST
12899 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12900 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12901}
12902
12903/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12904 Ada exception object. This matches all exceptions except the ones
12905 defined by the Ada language. */
12906
12907static int
12908ada_is_non_standard_exception_sym (struct symbol *sym)
12909{
778865d3
JB
12910 if (!ada_is_exception_sym (sym))
12911 return 0;
12912
696d6f4d
TT
12913 for (const char *name : standard_exc)
12914 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12915 return 0; /* A standard exception. */
12916
12917 /* Numeric_Error is also a standard exception, so exclude it.
12918 See the STANDARD_EXC description for more details as to why
12919 this exception is not listed in that array. */
987012b8 12920 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12921 return 0;
12922
12923 return 1;
12924}
12925
ab816a27 12926/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12927 objects.
12928
12929 The comparison is determined first by exception name, and then
12930 by exception address. */
12931
ab816a27 12932bool
cc536b21 12933ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12934{
778865d3
JB
12935 int result;
12936
ab816a27
TT
12937 result = strcmp (name, other.name);
12938 if (result < 0)
12939 return true;
12940 if (result == 0 && addr < other.addr)
12941 return true;
12942 return false;
12943}
778865d3 12944
ab816a27 12945bool
cc536b21 12946ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12947{
12948 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12949}
12950
12951/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12952 routine, but keeping the first SKIP elements untouched.
12953
12954 All duplicates are also removed. */
12955
12956static void
ab816a27 12957sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12958 int skip)
12959{
ab816a27
TT
12960 std::sort (exceptions->begin () + skip, exceptions->end ());
12961 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12962 exceptions->end ());
778865d3
JB
12963}
12964
778865d3
JB
12965/* Add all exceptions defined by the Ada standard whose name match
12966 a regular expression.
12967
12968 If PREG is not NULL, then this regexp_t object is used to
12969 perform the symbol name matching. Otherwise, no name-based
12970 filtering is performed.
12971
12972 EXCEPTIONS is a vector of exceptions to which matching exceptions
12973 gets pushed. */
12974
12975static void
2d7cc5c7 12976ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12977 std::vector<ada_exc_info> *exceptions)
778865d3 12978{
696d6f4d 12979 for (const char *name : standard_exc)
778865d3 12980 {
696d6f4d 12981 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3
JB
12982 {
12983 struct bound_minimal_symbol msymbol
696d6f4d 12984 = ada_lookup_simple_minsym (name);
778865d3
JB
12985
12986 if (msymbol.minsym != NULL)
12987 {
12988 struct ada_exc_info info
4aeddc50 12989 = {name, msymbol.value_address ()};
778865d3 12990
ab816a27 12991 exceptions->push_back (info);
778865d3
JB
12992 }
12993 }
12994 }
12995}
12996
12997/* Add all Ada exceptions defined locally and accessible from the given
12998 FRAME.
12999
13000 If PREG is not NULL, then this regexp_t object is used to
13001 perform the symbol name matching. Otherwise, no name-based
13002 filtering is performed.
13003
13004 EXCEPTIONS is a vector of exceptions to which matching exceptions
13005 gets pushed. */
13006
13007static void
2d7cc5c7
PA
13008ada_add_exceptions_from_frame (compiled_regex *preg,
13009 struct frame_info *frame,
ab816a27 13010 std::vector<ada_exc_info> *exceptions)
778865d3 13011{
3977b71f 13012 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
13013
13014 while (block != 0)
13015 {
13016 struct block_iterator iter;
13017 struct symbol *sym;
13018
13019 ALL_BLOCK_SYMBOLS (block, iter, sym)
13020 {
66d7f48f 13021 switch (sym->aclass ())
778865d3
JB
13022 {
13023 case LOC_TYPEDEF:
13024 case LOC_BLOCK:
13025 case LOC_CONST:
13026 break;
13027 default:
13028 if (ada_is_exception_sym (sym))
13029 {
987012b8 13030 struct ada_exc_info info = {sym->print_name (),
4aeddc50 13031 sym->value_address ()};
778865d3 13032
ab816a27 13033 exceptions->push_back (info);
778865d3
JB
13034 }
13035 }
13036 }
6c00f721 13037 if (block->function () != NULL)
778865d3 13038 break;
f135fe72 13039 block = block->superblock ();
778865d3
JB
13040 }
13041}
13042
14bc53a8
PA
13043/* Return true if NAME matches PREG or if PREG is NULL. */
13044
13045static bool
2d7cc5c7 13046name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
13047{
13048 return (preg == NULL
f945dedf 13049 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
13050}
13051
778865d3
JB
13052/* Add all exceptions defined globally whose name name match
13053 a regular expression, excluding standard exceptions.
13054
13055 The reason we exclude standard exceptions is that they need
13056 to be handled separately: Standard exceptions are defined inside
13057 a runtime unit which is normally not compiled with debugging info,
13058 and thus usually do not show up in our symbol search. However,
13059 if the unit was in fact built with debugging info, we need to
13060 exclude them because they would duplicate the entry we found
13061 during the special loop that specifically searches for those
13062 standard exceptions.
13063
13064 If PREG is not NULL, then this regexp_t object is used to
13065 perform the symbol name matching. Otherwise, no name-based
13066 filtering is performed.
13067
13068 EXCEPTIONS is a vector of exceptions to which matching exceptions
13069 gets pushed. */
13070
13071static void
2d7cc5c7 13072ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13073 std::vector<ada_exc_info> *exceptions)
778865d3 13074{
14bc53a8
PA
13075 /* In Ada, the symbol "search name" is a linkage name, whereas the
13076 regular expression used to do the matching refers to the natural
13077 name. So match against the decoded name. */
13078 expand_symtabs_matching (NULL,
b5ec771e 13079 lookup_name_info::match_any (),
14bc53a8
PA
13080 [&] (const char *search_name)
13081 {
f945dedf
CB
13082 std::string decoded = ada_decode (search_name);
13083 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13084 },
13085 NULL,
03a8ea51 13086 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 13087 VARIABLES_DOMAIN);
778865d3 13088
2030c079 13089 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13090 {
b669c953 13091 for (compunit_symtab *s : objfile->compunits ())
778865d3 13092 {
af39c5c8 13093 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13094 int i;
778865d3 13095
d8aeb77f
TT
13096 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13097 {
63d609de 13098 const struct block *b = bv->block (i);
d8aeb77f
TT
13099 struct block_iterator iter;
13100 struct symbol *sym;
778865d3 13101
d8aeb77f
TT
13102 ALL_BLOCK_SYMBOLS (b, iter, sym)
13103 if (ada_is_non_standard_exception_sym (sym)
987012b8 13104 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13105 {
13106 struct ada_exc_info info
4aeddc50 13107 = {sym->print_name (), sym->value_address ()};
d8aeb77f
TT
13108
13109 exceptions->push_back (info);
13110 }
13111 }
778865d3
JB
13112 }
13113 }
13114}
13115
13116/* Implements ada_exceptions_list with the regular expression passed
13117 as a regex_t, rather than a string.
13118
13119 If not NULL, PREG is used to filter out exceptions whose names
13120 do not match. Otherwise, all exceptions are listed. */
13121
ab816a27 13122static std::vector<ada_exc_info>
2d7cc5c7 13123ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13124{
ab816a27 13125 std::vector<ada_exc_info> result;
778865d3
JB
13126 int prev_len;
13127
13128 /* First, list the known standard exceptions. These exceptions
13129 need to be handled separately, as they are usually defined in
13130 runtime units that have been compiled without debugging info. */
13131
13132 ada_add_standard_exceptions (preg, &result);
13133
13134 /* Next, find all exceptions whose scope is local and accessible
13135 from the currently selected frame. */
13136
13137 if (has_stack_frames ())
13138 {
ab816a27 13139 prev_len = result.size ();
778865d3
JB
13140 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13141 &result);
ab816a27 13142 if (result.size () > prev_len)
778865d3
JB
13143 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13144 }
13145
13146 /* Add all exceptions whose scope is global. */
13147
ab816a27 13148 prev_len = result.size ();
778865d3 13149 ada_add_global_exceptions (preg, &result);
ab816a27 13150 if (result.size () > prev_len)
778865d3
JB
13151 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13152
778865d3
JB
13153 return result;
13154}
13155
13156/* Return a vector of ada_exc_info.
13157
13158 If REGEXP is NULL, all exceptions are included in the result.
13159 Otherwise, it should contain a valid regular expression,
13160 and only the exceptions whose names match that regular expression
13161 are included in the result.
13162
13163 The exceptions are sorted in the following order:
13164 - Standard exceptions (defined by the Ada language), in
13165 alphabetical order;
13166 - Exceptions only visible from the current frame, in
13167 alphabetical order;
13168 - Exceptions whose scope is global, in alphabetical order. */
13169
ab816a27 13170std::vector<ada_exc_info>
778865d3
JB
13171ada_exceptions_list (const char *regexp)
13172{
2d7cc5c7
PA
13173 if (regexp == NULL)
13174 return ada_exceptions_list_1 (NULL);
778865d3 13175
2d7cc5c7
PA
13176 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13177 return ada_exceptions_list_1 (&reg);
778865d3
JB
13178}
13179
13180/* Implement the "info exceptions" command. */
13181
13182static void
1d12d88f 13183info_exceptions_command (const char *regexp, int from_tty)
778865d3 13184{
778865d3 13185 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13186
ab816a27 13187 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13188
13189 if (regexp != NULL)
6cb06a8c 13190 gdb_printf
778865d3
JB
13191 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13192 else
6cb06a8c 13193 gdb_printf (_("All defined Ada exceptions:\n"));
778865d3 13194
ab816a27 13195 for (const ada_exc_info &info : exceptions)
6cb06a8c 13196 gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13197}
13198
6c038f32
PH
13199\f
13200 /* Language vector */
13201
b5ec771e
PA
13202/* symbol_name_matcher_ftype adapter for wild_match. */
13203
13204static bool
13205do_wild_match (const char *symbol_search_name,
13206 const lookup_name_info &lookup_name,
a207cff2 13207 completion_match_result *comp_match_res)
b5ec771e
PA
13208{
13209 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13210}
13211
13212/* symbol_name_matcher_ftype adapter for full_match. */
13213
13214static bool
13215do_full_match (const char *symbol_search_name,
13216 const lookup_name_info &lookup_name,
a207cff2 13217 completion_match_result *comp_match_res)
b5ec771e 13218{
959d6a67
TT
13219 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13220
13221 /* If both symbols start with "_ada_", just let the loop below
13222 handle the comparison. However, if only the symbol name starts
13223 with "_ada_", skip the prefix and let the match proceed as
13224 usual. */
13225 if (startswith (symbol_search_name, "_ada_")
13226 && !startswith (lname, "_ada"))
86b44259 13227 symbol_search_name += 5;
81eaa506
TT
13228 /* Likewise for ghost entities. */
13229 if (startswith (symbol_search_name, "___ghost_")
13230 && !startswith (lname, "___ghost_"))
13231 symbol_search_name += 9;
86b44259 13232
86b44259
TT
13233 int uscore_count = 0;
13234 while (*lname != '\0')
13235 {
13236 if (*symbol_search_name != *lname)
13237 {
13238 if (*symbol_search_name == 'B' && uscore_count == 2
13239 && symbol_search_name[1] == '_')
13240 {
13241 symbol_search_name += 2;
13242 while (isdigit (*symbol_search_name))
13243 ++symbol_search_name;
13244 if (symbol_search_name[0] == '_'
13245 && symbol_search_name[1] == '_')
13246 {
13247 symbol_search_name += 2;
13248 continue;
13249 }
13250 }
13251 return false;
13252 }
13253
13254 if (*symbol_search_name == '_')
13255 ++uscore_count;
13256 else
13257 uscore_count = 0;
13258
13259 ++symbol_search_name;
13260 ++lname;
13261 }
13262
13263 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13264}
13265
a2cd4f14
JB
13266/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13267
13268static bool
13269do_exact_match (const char *symbol_search_name,
13270 const lookup_name_info &lookup_name,
13271 completion_match_result *comp_match_res)
13272{
13273 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13274}
13275
b5ec771e
PA
13276/* Build the Ada lookup name for LOOKUP_NAME. */
13277
13278ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13279{
e0802d59 13280 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13281
6a780b67 13282 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13283 {
13284 if (user_name.back () == '>')
e0802d59 13285 m_encoded_name
5ac58899 13286 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13287 else
e0802d59 13288 m_encoded_name
5ac58899 13289 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13290 m_encoded_p = true;
13291 m_verbatim_p = true;
13292 m_wild_match_p = false;
13293 m_standard_p = false;
13294 }
13295 else
13296 {
13297 m_verbatim_p = false;
13298
e0802d59 13299 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13300
13301 if (!m_encoded_p)
13302 {
e0802d59 13303 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13304 m_encoded_name = ada_encode_1 (folded, false);
13305 if (m_encoded_name.empty ())
5ac58899 13306 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13307 }
13308 else
5ac58899 13309 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13310
13311 /* Handle the 'package Standard' special case. See description
13312 of m_standard_p. */
13313 if (startswith (m_encoded_name.c_str (), "standard__"))
13314 {
13315 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13316 m_standard_p = true;
13317 }
13318 else
13319 m_standard_p = false;
74ccd7f5 13320
b5ec771e
PA
13321 /* If the name contains a ".", then the user is entering a fully
13322 qualified entity name, and the match must not be done in wild
13323 mode. Similarly, if the user wants to complete what looks
13324 like an encoded name, the match must not be done in wild
13325 mode. Also, in the standard__ special case always do
13326 non-wild matching. */
13327 m_wild_match_p
13328 = (lookup_name.match_type () != symbol_name_match_type::FULL
13329 && !m_encoded_p
13330 && !m_standard_p
13331 && user_name.find ('.') == std::string::npos);
13332 }
13333}
13334
13335/* symbol_name_matcher_ftype method for Ada. This only handles
13336 completion mode. */
13337
13338static bool
13339ada_symbol_name_matches (const char *symbol_search_name,
13340 const lookup_name_info &lookup_name,
a207cff2 13341 completion_match_result *comp_match_res)
74ccd7f5 13342{
b5ec771e
PA
13343 return lookup_name.ada ().matches (symbol_search_name,
13344 lookup_name.match_type (),
a207cff2 13345 comp_match_res);
b5ec771e
PA
13346}
13347
de63c46b
PA
13348/* A name matcher that matches the symbol name exactly, with
13349 strcmp. */
13350
13351static bool
13352literal_symbol_name_matcher (const char *symbol_search_name,
13353 const lookup_name_info &lookup_name,
13354 completion_match_result *comp_match_res)
13355{
e0802d59 13356 gdb::string_view name_view = lookup_name.name ();
de63c46b 13357
e0802d59
TT
13358 if (lookup_name.completion_mode ()
13359 ? (strncmp (symbol_search_name, name_view.data (),
13360 name_view.size ()) == 0)
13361 : symbol_search_name == name_view)
de63c46b
PA
13362 {
13363 if (comp_match_res != NULL)
13364 comp_match_res->set_match (symbol_search_name);
13365 return true;
13366 }
13367 else
13368 return false;
13369}
13370
c9debfb9 13371/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13372 Ada. */
13373
13374static symbol_name_matcher_ftype *
13375ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13376{
de63c46b
PA
13377 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13378 return literal_symbol_name_matcher;
13379
b5ec771e
PA
13380 if (lookup_name.completion_mode ())
13381 return ada_symbol_name_matches;
74ccd7f5 13382 else
b5ec771e
PA
13383 {
13384 if (lookup_name.ada ().wild_match_p ())
13385 return do_wild_match;
a2cd4f14
JB
13386 else if (lookup_name.ada ().verbatim_p ())
13387 return do_exact_match;
b5ec771e
PA
13388 else
13389 return do_full_match;
13390 }
74ccd7f5
JB
13391}
13392
0874fd07
AB
13393/* Class representing the Ada language. */
13394
13395class ada_language : public language_defn
13396{
13397public:
13398 ada_language ()
0e25e767 13399 : language_defn (language_ada)
0874fd07 13400 { /* Nothing. */ }
5bd40f2a 13401
6f7664a9
AB
13402 /* See language.h. */
13403
13404 const char *name () const override
13405 { return "ada"; }
13406
13407 /* See language.h. */
13408
13409 const char *natural_name () const override
13410 { return "Ada"; }
13411
e171d6f1
AB
13412 /* See language.h. */
13413
13414 const std::vector<const char *> &filename_extensions () const override
13415 {
13416 static const std::vector<const char *> extensions
13417 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13418 return extensions;
13419 }
13420
5bd40f2a
AB
13421 /* Print an array element index using the Ada syntax. */
13422
13423 void print_array_index (struct type *index_type,
13424 LONGEST index,
13425 struct ui_file *stream,
13426 const value_print_options *options) const override
13427 {
13428 struct value *index_value = val_atr (index_type, index);
13429
00c696a6 13430 value_print (index_value, stream, options);
6cb06a8c 13431 gdb_printf (stream, " => ");
5bd40f2a 13432 }
15e5fd35
AB
13433
13434 /* Implement the "read_var_value" language_defn method for Ada. */
13435
13436 struct value *read_var_value (struct symbol *var,
13437 const struct block *var_block,
13438 struct frame_info *frame) const override
13439 {
13440 /* The only case where default_read_var_value is not sufficient
13441 is when VAR is a renaming... */
13442 if (frame != nullptr)
13443 {
13444 const struct block *frame_block = get_frame_block (frame, NULL);
13445 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13446 return ada_read_renaming_var_value (var, frame_block);
13447 }
13448
13449 /* This is a typical case where we expect the default_read_var_value
13450 function to work. */
13451 return language_defn::read_var_value (var, var_block, frame);
13452 }
1fb314aa 13453
2c71f639 13454 /* See language.h. */
496feb16 13455 bool symbol_printing_suppressed (struct symbol *symbol) const override
2c71f639 13456 {
496feb16 13457 return symbol->is_artificial ();
2c71f639
TV
13458 }
13459
1fb314aa
AB
13460 /* See language.h. */
13461 void language_arch_info (struct gdbarch *gdbarch,
13462 struct language_arch_info *lai) const override
13463 {
13464 const struct builtin_type *builtin = builtin_type (gdbarch);
13465
7bea47f0
AB
13466 /* Helper function to allow shorter lines below. */
13467 auto add = [&] (struct type *t)
13468 {
13469 lai->add_primitive_type (t);
13470 };
13471
13472 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13473 0, "integer"));
13474 add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13475 0, "long_integer"));
13476 add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13477 0, "short_integer"));
13478 struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
c9f66f00 13479 1, "character");
7bea47f0
AB
13480 lai->set_string_char_type (char_type);
13481 add (char_type);
c9f66f00
TT
13482 add (arch_character_type (gdbarch, 16, 1, "wide_character"));
13483 add (arch_character_type (gdbarch, 32, 1, "wide_wide_character"));
7bea47f0
AB
13484 add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
13485 "float", gdbarch_float_format (gdbarch)));
13486 add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13487 "long_float", gdbarch_double_format (gdbarch)));
13488 add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
13489 0, "long_long_integer"));
13490 add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
13491 "long_long_float",
13492 gdbarch_long_double_format (gdbarch)));
13493 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13494 0, "natural"));
13495 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13496 0, "positive"));
13497 add (builtin->builtin_void);
13498
13499 struct type *system_addr_ptr
1fb314aa
AB
13500 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
13501 "void"));
7bea47f0
AB
13502 system_addr_ptr->set_name ("system__address");
13503 add (system_addr_ptr);
1fb314aa
AB
13504
13505 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13506 type. This is a signed integral type whose size is the same as
13507 the size of addresses. */
7bea47f0
AB
13508 unsigned int addr_length = TYPE_LENGTH (system_addr_ptr);
13509 add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
13510 "storage_offset"));
1fb314aa 13511
7bea47f0 13512 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13513 }
4009ee92
AB
13514
13515 /* See language.h. */
13516
13517 bool iterate_over_symbols
13518 (const struct block *block, const lookup_name_info &name,
13519 domain_enum domain,
13520 gdb::function_view<symbol_found_callback_ftype> callback) const override
13521 {
d1183b06
TT
13522 std::vector<struct block_symbol> results
13523 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13524 for (block_symbol &sym : results)
13525 {
13526 if (!callback (&sym))
13527 return false;
13528 }
13529
13530 return true;
13531 }
6f827019
AB
13532
13533 /* See language.h. */
3456e70c
TT
13534 bool sniff_from_mangled_name
13535 (const char *mangled,
13536 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13537 {
13538 std::string demangled = ada_decode (mangled);
13539
13540 *out = NULL;
13541
13542 if (demangled != mangled && demangled[0] != '<')
13543 {
13544 /* Set the gsymbol language to Ada, but still return 0.
13545 Two reasons for that:
13546
13547 1. For Ada, we prefer computing the symbol's decoded name
13548 on the fly rather than pre-compute it, in order to save
13549 memory (Ada projects are typically very large).
13550
13551 2. There are some areas in the definition of the GNAT
13552 encoding where, with a bit of bad luck, we might be able
13553 to decode a non-Ada symbol, generating an incorrect
13554 demangled name (Eg: names ending with "TB" for instance
13555 are identified as task bodies and so stripped from
13556 the decoded name returned).
13557
13558 Returning true, here, but not setting *DEMANGLED, helps us get
13559 a little bit of the best of both worlds. Because we're last,
13560 we should not affect any of the other languages that were
13561 able to demangle the symbol before us; we get to correctly
13562 tag Ada symbols as such; and even if we incorrectly tagged a
13563 non-Ada symbol, which should be rare, any routing through the
13564 Ada language should be transparent (Ada tries to behave much
13565 like C/C++ with non-Ada symbols). */
13566 return true;
13567 }
13568
13569 return false;
13570 }
fbfb0a46
AB
13571
13572 /* See language.h. */
13573
3456e70c
TT
13574 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13575 int options) const override
0a50df5d 13576 {
3456e70c 13577 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13578 }
13579
13580 /* See language.h. */
13581
fbfb0a46
AB
13582 void print_type (struct type *type, const char *varstring,
13583 struct ui_file *stream, int show, int level,
13584 const struct type_print_options *flags) const override
13585 {
13586 ada_print_type (type, varstring, stream, show, level, flags);
13587 }
c9debfb9 13588
53fc67f8
AB
13589 /* See language.h. */
13590
13591 const char *word_break_characters (void) const override
13592 {
13593 return ada_completer_word_break_characters;
13594 }
13595
7e56227d
AB
13596 /* See language.h. */
13597
13598 void collect_symbol_completion_matches (completion_tracker &tracker,
13599 complete_symbol_mode mode,
13600 symbol_name_match_type name_match_type,
13601 const char *text, const char *word,
13602 enum type_code code) const override
13603 {
13604 struct symbol *sym;
13605 const struct block *b, *surrounding_static_block = 0;
13606 struct block_iterator iter;
13607
13608 gdb_assert (code == TYPE_CODE_UNDEF);
13609
13610 lookup_name_info lookup_name (text, name_match_type, true);
13611
13612 /* First, look at the partial symtab symbols. */
13613 expand_symtabs_matching (NULL,
13614 lookup_name,
13615 NULL,
13616 NULL,
03a8ea51 13617 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13618 ALL_DOMAIN);
13619
13620 /* At this point scan through the misc symbol vectors and add each
13621 symbol you find to the list. Eventually we want to ignore
13622 anything that isn't a text symbol (everything else will be
13623 handled by the psymtab code above). */
13624
13625 for (objfile *objfile : current_program_space->objfiles ())
13626 {
13627 for (minimal_symbol *msymbol : objfile->msymbols ())
13628 {
13629 QUIT;
13630
13631 if (completion_skip_symbol (mode, msymbol))
13632 continue;
13633
13634 language symbol_language = msymbol->language ();
13635
13636 /* Ada minimal symbols won't have their language set to Ada. If
13637 we let completion_list_add_name compare using the
13638 default/C-like matcher, then when completing e.g., symbols in a
13639 package named "pck", we'd match internal Ada symbols like
13640 "pckS", which are invalid in an Ada expression, unless you wrap
13641 them in '<' '>' to request a verbatim match.
13642
13643 Unfortunately, some Ada encoded names successfully demangle as
13644 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13645 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13646 with the wrong language set. Paper over that issue here. */
13647 if (symbol_language == language_auto
13648 || symbol_language == language_cplus)
13649 symbol_language = language_ada;
13650
13651 completion_list_add_name (tracker,
13652 symbol_language,
13653 msymbol->linkage_name (),
13654 lookup_name, text, word);
13655 }
13656 }
13657
13658 /* Search upwards from currently selected frame (so that we can
13659 complete on local vars. */
13660
f135fe72 13661 for (b = get_selected_block (0); b != NULL; b = b->superblock ())
7e56227d 13662 {
f135fe72 13663 if (!b->superblock ())
7e56227d
AB
13664 surrounding_static_block = b; /* For elmin of dups */
13665
13666 ALL_BLOCK_SYMBOLS (b, iter, sym)
13667 {
13668 if (completion_skip_symbol (mode, sym))
13669 continue;
13670
13671 completion_list_add_name (tracker,
13672 sym->language (),
13673 sym->linkage_name (),
13674 lookup_name, text, word);
13675 }
13676 }
13677
13678 /* Go through the symtabs and check the externs and statics for
13679 symbols which match. */
13680
13681 for (objfile *objfile : current_program_space->objfiles ())
13682 {
13683 for (compunit_symtab *s : objfile->compunits ())
13684 {
13685 QUIT;
63d609de 13686 b = s->blockvector ()->global_block ();
7e56227d
AB
13687 ALL_BLOCK_SYMBOLS (b, iter, sym)
13688 {
13689 if (completion_skip_symbol (mode, sym))
13690 continue;
13691
13692 completion_list_add_name (tracker,
13693 sym->language (),
13694 sym->linkage_name (),
13695 lookup_name, text, word);
13696 }
13697 }
13698 }
13699
13700 for (objfile *objfile : current_program_space->objfiles ())
13701 {
13702 for (compunit_symtab *s : objfile->compunits ())
13703 {
13704 QUIT;
63d609de 13705 b = s->blockvector ()->static_block ();
7e56227d
AB
13706 /* Don't do this block twice. */
13707 if (b == surrounding_static_block)
13708 continue;
13709 ALL_BLOCK_SYMBOLS (b, iter, sym)
13710 {
13711 if (completion_skip_symbol (mode, sym))
13712 continue;
13713
13714 completion_list_add_name (tracker,
13715 sym->language (),
13716 sym->linkage_name (),
13717 lookup_name, text, word);
13718 }
13719 }
13720 }
13721 }
13722
f16a9f57
AB
13723 /* See language.h. */
13724
13725 gdb::unique_xmalloc_ptr<char> watch_location_expression
13726 (struct type *type, CORE_ADDR addr) const override
13727 {
13728 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
13729 std::string name = type_to_string (type);
8579fd13 13730 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13731 }
13732
a1d1fa3e
AB
13733 /* See language.h. */
13734
13735 void value_print (struct value *val, struct ui_file *stream,
13736 const struct value_print_options *options) const override
13737 {
13738 return ada_value_print (val, stream, options);
13739 }
13740
ebe2334e
AB
13741 /* See language.h. */
13742
13743 void value_print_inner
13744 (struct value *val, struct ui_file *stream, int recurse,
13745 const struct value_print_options *options) const override
13746 {
13747 return ada_value_print_inner (val, stream, recurse, options);
13748 }
13749
a78a19b1
AB
13750 /* See language.h. */
13751
13752 struct block_symbol lookup_symbol_nonlocal
13753 (const char *name, const struct block *block,
13754 const domain_enum domain) const override
13755 {
13756 struct block_symbol sym;
13757
13758 sym = ada_lookup_symbol (name, block_static_block (block), domain);
13759 if (sym.symbol != NULL)
13760 return sym;
13761
13762 /* If we haven't found a match at this point, try the primitive
13763 types. In other languages, this search is performed before
13764 searching for global symbols in order to short-circuit that
13765 global-symbol search if it happens that the name corresponds
13766 to a primitive type. But we cannot do the same in Ada, because
13767 it is perfectly legitimate for a program to declare a type which
13768 has the same name as a standard type. If looking up a type in
13769 that situation, we have traditionally ignored the primitive type
13770 in favor of user-defined types. This is why, unlike most other
13771 languages, we search the primitive types this late and only after
13772 having searched the global symbols without success. */
13773
13774 if (domain == VAR_DOMAIN)
13775 {
13776 struct gdbarch *gdbarch;
13777
13778 if (block == NULL)
13779 gdbarch = target_gdbarch ();
13780 else
13781 gdbarch = block_gdbarch (block);
13782 sym.symbol
13783 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13784 if (sym.symbol != NULL)
13785 return sym;
13786 }
13787
13788 return {};
13789 }
13790
87afa652
AB
13791 /* See language.h. */
13792
13793 int parser (struct parser_state *ps) const override
13794 {
13795 warnings_issued = 0;
13796 return ada_parse (ps);
13797 }
13798
ec8cec5b
AB
13799 /* See language.h. */
13800
13801 void emitchar (int ch, struct type *chtype,
13802 struct ui_file *stream, int quoter) const override
13803 {
13804 ada_emit_char (ch, chtype, stream, quoter, 1);
13805 }
13806
52b50f2c
AB
13807 /* See language.h. */
13808
13809 void printchar (int ch, struct type *chtype,
13810 struct ui_file *stream) const override
13811 {
13812 ada_printchar (ch, chtype, stream);
13813 }
13814
d711ee67
AB
13815 /* See language.h. */
13816
13817 void printstr (struct ui_file *stream, struct type *elttype,
13818 const gdb_byte *string, unsigned int length,
13819 const char *encoding, int force_ellipses,
13820 const struct value_print_options *options) const override
13821 {
13822 ada_printstr (stream, elttype, string, length, encoding,
13823 force_ellipses, options);
13824 }
13825
4ffc13fb
AB
13826 /* See language.h. */
13827
13828 void print_typedef (struct type *type, struct symbol *new_symbol,
13829 struct ui_file *stream) const override
13830 {
13831 ada_print_typedef (type, new_symbol, stream);
13832 }
13833
39e7ecca
AB
13834 /* See language.h. */
13835
13836 bool is_string_type_p (struct type *type) const override
13837 {
13838 return ada_is_string_type (type);
13839 }
13840
22e3f3ed
AB
13841 /* See language.h. */
13842
13843 const char *struct_too_deep_ellipsis () const override
13844 { return "(...)"; }
39e7ecca 13845
67bd3fd5
AB
13846 /* See language.h. */
13847
13848 bool c_style_arrays_p () const override
13849 { return false; }
13850
d3355e4d
AB
13851 /* See language.h. */
13852
13853 bool store_sym_names_in_linkage_form_p () const override
13854 { return true; }
13855
b63a3f3f
AB
13856 /* See language.h. */
13857
13858 const struct lang_varobj_ops *varobj_ops () const override
13859 { return &ada_varobj_ops; }
13860
c9debfb9
AB
13861protected:
13862 /* See language.h. */
13863
13864 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13865 (const lookup_name_info &lookup_name) const override
13866 {
13867 return ada_get_symbol_name_matcher (lookup_name);
13868 }
0874fd07
AB
13869};
13870
13871/* Single instance of the Ada language class. */
13872
13873static ada_language ada_language_defn;
13874
5bf03f13
JB
13875/* Command-list for the "set/show ada" prefix command. */
13876static struct cmd_list_element *set_ada_list;
13877static struct cmd_list_element *show_ada_list;
13878
3d9434b5
JB
13879/* This module's 'new_objfile' observer. */
13880
13881static void
13882ada_new_objfile_observer (struct objfile *objfile)
13883{
13884 ada_clear_symbol_cache ();
13885}
13886
13887/* This module's 'free_objfile' observer. */
13888
13889static void
13890ada_free_objfile_observer (struct objfile *objfile)
13891{
13892 ada_clear_symbol_cache ();
13893}
13894
315e4ebb
TT
13895/* Charsets known to GNAT. */
13896static const char * const gnat_source_charsets[] =
13897{
13898 /* Note that code below assumes that the default comes first.
13899 Latin-1 is the default here, because that is also GNAT's
13900 default. */
13901 "ISO-8859-1",
13902 "ISO-8859-2",
13903 "ISO-8859-3",
13904 "ISO-8859-4",
13905 "ISO-8859-5",
13906 "ISO-8859-15",
13907 "CP437",
13908 "CP850",
13909 /* Note that this value is special-cased in the encoder and
13910 decoder. */
13911 ada_utf8,
13912 nullptr
13913};
13914
6c265988 13915void _initialize_ada_language ();
d2e4a39e 13916void
6c265988 13917_initialize_ada_language ()
14f9c5c9 13918{
f54bdb6d
SM
13919 add_setshow_prefix_cmd
13920 ("ada", no_class,
13921 _("Prefix command for changing Ada-specific settings."),
13922 _("Generic command for showing Ada-specific settings."),
13923 &set_ada_list, &show_ada_list,
13924 &setlist, &showlist);
5bf03f13
JB
13925
13926 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13927 &trust_pad_over_xvs, _("\
590042fc
PW
13928Enable or disable an optimization trusting PAD types over XVS types."), _("\
13929Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13930 _("\
5bf03f13
JB
13931This is related to the encoding used by the GNAT compiler. The debugger\n\
13932should normally trust the contents of PAD types, but certain older versions\n\
13933of GNAT have a bug that sometimes causes the information in the PAD type\n\
13934to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13935work around this bug. It is always safe to turn this option \"off\", but\n\
13936this incurs a slight performance penalty, so it is recommended to NOT change\n\
13937this option to \"off\" unless necessary."),
dda83cd7 13938 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13939
d72413e6
PMR
13940 add_setshow_boolean_cmd ("print-signatures", class_vars,
13941 &print_signatures, _("\
13942Enable or disable the output of formal and return types for functions in the \
590042fc 13943overloads selection menu."), _("\
d72413e6 13944Show whether the output of formal and return types for functions in the \
590042fc 13945overloads selection menu is activated."),
d72413e6
PMR
13946 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13947
315e4ebb
TT
13948 ada_source_charset = gnat_source_charsets[0];
13949 add_setshow_enum_cmd ("source-charset", class_files,
13950 gnat_source_charsets,
13951 &ada_source_charset, _("\
13952Set the Ada source character set."), _("\
13953Show the Ada source character set."), _("\
13954The character set used for Ada source files.\n\
13955This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13956 nullptr, nullptr,
13957 &set_ada_list, &show_ada_list);
13958
9ac4176b
PA
13959 add_catch_command ("exception", _("\
13960Catch Ada exceptions, when raised.\n\
9bf7038b 13961Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
13962Without any argument, stop when any Ada exception is raised.\n\
13963If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13964being raised does not have a handler (and will therefore lead to the task's\n\
13965termination).\n\
13966Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
13967raised is the same as ARG.\n\
13968CONDITION is a boolean expression that is evaluated to see whether the\n\
13969exception should cause a stop."),
9ac4176b 13970 catch_ada_exception_command,
71bed2db 13971 catch_ada_completer,
9ac4176b
PA
13972 CATCH_PERMANENT,
13973 CATCH_TEMPORARY);
9f757bf7
XR
13974
13975 add_catch_command ("handlers", _("\
13976Catch Ada exceptions, when handled.\n\
9bf7038b
TT
13977Usage: catch handlers [ARG] [if CONDITION]\n\
13978Without any argument, stop when any Ada exception is handled.\n\
13979With an argument, catch only exceptions with the given name.\n\
13980CONDITION is a boolean expression that is evaluated to see whether the\n\
13981exception should cause a stop."),
9f757bf7 13982 catch_ada_handlers_command,
dda83cd7 13983 catch_ada_completer,
9f757bf7
XR
13984 CATCH_PERMANENT,
13985 CATCH_TEMPORARY);
9ac4176b
PA
13986 add_catch_command ("assert", _("\
13987Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
13988Usage: catch assert [if CONDITION]\n\
13989CONDITION is a boolean expression that is evaluated to see whether the\n\
13990exception should cause a stop."),
9ac4176b 13991 catch_assert_command,
dda83cd7 13992 NULL,
9ac4176b
PA
13993 CATCH_PERMANENT,
13994 CATCH_TEMPORARY);
13995
778865d3
JB
13996 add_info ("exceptions", info_exceptions_command,
13997 _("\
13998List all Ada exception names.\n\
9bf7038b 13999Usage: info exceptions [REGEXP]\n\
778865d3
JB
14000If a regular expression is passed as an argument, only those matching\n\
14001the regular expression are listed."));
14002
f54bdb6d
SM
14003 add_setshow_prefix_cmd ("ada", class_maintenance,
14004 _("Set Ada maintenance-related variables."),
14005 _("Show Ada maintenance-related variables."),
14006 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
14007 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
14008
14009 add_setshow_boolean_cmd
14010 ("ignore-descriptive-types", class_maintenance,
14011 &ada_ignore_descriptive_types_p,
14012 _("Set whether descriptive types generated by GNAT should be ignored."),
14013 _("Show whether descriptive types generated by GNAT should be ignored."),
14014 _("\
14015When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14016DWARF attribute."),
14017 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14018
2698f5ea
TT
14019 decoded_names_store = htab_create_alloc (256, htab_hash_string,
14020 htab_eq_string,
459a2e4c 14021 NULL, xcalloc, xfree);
6b69afc4 14022
3d9434b5 14023 /* The ada-lang observers. */
c90e7d63
SM
14024 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
14025 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
14026 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
14f9c5c9 14027}