]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
gdb: remove TYPE_FIELD_BITSIZE
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
213516ef 3 Copyright (C) 1992-2023 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"
033bc52b 60#include "gdbsupport/selftest.h"
4de283e4 61#include <algorithm>
03070ee9 62#include "ada-exp.h"
315e4ebb 63#include "charset.h"
013a623f 64#include "ax-gdb.h"
ccefe4c4 65
d2e4a39e 66static struct type *desc_base_type (struct type *);
14f9c5c9 67
d2e4a39e 68static struct type *desc_bounds_type (struct type *);
14f9c5c9 69
d2e4a39e 70static struct value *desc_bounds (struct value *);
14f9c5c9 71
d2e4a39e 72static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 73
d2e4a39e 74static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 75
556bdfd4 76static struct type *desc_data_target_type (struct type *);
14f9c5c9 77
d2e4a39e 78static struct value *desc_data (struct value *);
14f9c5c9 79
d2e4a39e 80static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 81
d2e4a39e 82static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 83
d2e4a39e 84static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 85
d2e4a39e 86static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 87
d2e4a39e 88static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 89
d2e4a39e 90static struct type *desc_index_type (struct type *, int);
14f9c5c9 91
d2e4a39e 92static int desc_arity (struct type *);
14f9c5c9 93
d2e4a39e 94static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 95
40bc484c 96static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 97
d1183b06 98static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
99 const struct block *,
100 const lookup_name_info &lookup_name,
101 domain_enum, struct objfile *);
14f9c5c9 102
d1183b06
TT
103static void ada_add_all_symbols (std::vector<struct block_symbol> &,
104 const struct block *,
b5ec771e
PA
105 const lookup_name_info &lookup_name,
106 domain_enum, int, int *);
22cee43f 107
d1183b06 108static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 109
d1183b06
TT
110static void add_defn_to_vec (std::vector<struct block_symbol> &,
111 struct symbol *,
dda83cd7 112 const struct block *);
14f9c5c9 113
d2e4a39e 114static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 115
4c4b4cd2 116static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 117
d2e4a39e 118static int numeric_type_p (struct type *);
14f9c5c9 119
d2e4a39e 120static int integer_type_p (struct type *);
14f9c5c9 121
d2e4a39e 122static int scalar_type_p (struct type *);
14f9c5c9 123
d2e4a39e 124static int discrete_type_p (struct type *);
14f9c5c9 125
a121b7c1 126static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 127 int, int);
4c4b4cd2 128
b4ba55a1 129static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 130 const char *);
b4ba55a1 131
d2e4a39e 132static int is_dynamic_field (struct type *, int);
14f9c5c9 133
10a2c479 134static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 135 const gdb_byte *,
dda83cd7 136 CORE_ADDR, struct value *);
4c4b4cd2
PH
137
138static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 139
28c85d6c 140static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 141
d2e4a39e 142static struct type *to_static_fixed_type (struct type *);
f192137b 143static struct type *static_unwrap_type (struct type *type);
14f9c5c9 144
d2e4a39e 145static struct value *unwrap_value (struct value *);
14f9c5c9 146
ad82864c 147static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 148
ad82864c 149static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 150
ad82864c
JB
151static long decode_packed_array_bitsize (struct type *);
152
153static struct value *decode_constrained_packed_array (struct value *);
154
ad82864c 155static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 156
d2e4a39e 157static struct value *value_subscript_packed (struct value *, int,
dda83cd7 158 struct value **);
14f9c5c9 159
4c4b4cd2 160static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 161 struct type *);
14f9c5c9 162
d2e4a39e 163static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 164
d2e4a39e 165static int equiv_types (struct type *, struct type *);
14f9c5c9 166
d2e4a39e 167static int is_name_suffix (const char *);
14f9c5c9 168
59c8a30b 169static int advance_wild_match (const char **, const char *, char);
73589123 170
b5ec771e 171static bool wild_match (const char *name, const char *patn);
14f9c5c9 172
d2e4a39e 173static struct value *ada_coerce_ref (struct value *);
14f9c5c9 174
4c4b4cd2
PH
175static LONGEST pos_atr (struct value *);
176
53a47a3e
TT
177static struct value *val_atr (struct type *, LONGEST);
178
4c4b4cd2 179static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 180 domain_enum);
14f9c5c9 181
108d56a4 182static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 183 struct type *);
4c4b4cd2 184
0d5cff50 185static int find_struct_field (const char *, struct type *, int,
dda83cd7 186 struct type **, int *, int *, int *, int *);
4c4b4cd2 187
d1183b06 188static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 189 struct value **, int, const char *,
7056f312 190 struct type *, bool);
4c4b4cd2 191
4c4b4cd2
PH
192static int ada_is_direct_array_type (struct type *);
193
52ce6436
PH
194static struct value *ada_index_struct_field (int, struct value *, int,
195 struct type *);
196
cf608cc4 197static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
198
199
852dff6c 200static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
201
202static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
203 (const lookup_name_info &lookup_name);
204
4c4b4cd2
PH
205\f
206
315e4ebb
TT
207/* The character set used for source files. */
208static const char *ada_source_charset;
209
210/* The string "UTF-8". This is here so we can check for the UTF-8
211 charset using == rather than strcmp. */
212static const char ada_utf8[] = "UTF-8";
213
214/* Each entry in the UTF-32 case-folding table is of this form. */
215struct utf8_entry
216{
217 /* The start and end, inclusive, of this range of codepoints. */
218 uint32_t start, end;
219 /* The delta to apply to get the upper-case form. 0 if this is
220 already upper-case. */
221 int upper_delta;
222 /* The delta to apply to get the lower-case form. 0 if this is
223 already lower-case. */
224 int lower_delta;
225
226 bool operator< (uint32_t val) const
227 {
228 return end < val;
229 }
230};
231
232static const utf8_entry ada_case_fold[] =
233{
234#include "ada-casefold.h"
235};
236
237\f
238
67cb5b2d 239static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
240#ifdef VMS
241 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
242#else
14f9c5c9 243 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 244#endif
14f9c5c9 245
4c4b4cd2 246/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 247static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 248 = "__gnat_ada_main_program_name";
14f9c5c9 249
4c4b4cd2
PH
250/* Limit on the number of warnings to raise per expression evaluation. */
251static int warning_limit = 2;
252
253/* Number of warning messages issued; reset to 0 by cleanups after
254 expression evaluation. */
255static int warnings_issued = 0;
256
27087b7f 257static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
258 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
259};
260
27087b7f 261static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
262 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
263};
264
c6044dd1
JB
265/* Maintenance-related settings for this module. */
266
267static struct cmd_list_element *maint_set_ada_cmdlist;
268static struct cmd_list_element *maint_show_ada_cmdlist;
269
c6044dd1
JB
270/* The "maintenance ada set/show ignore-descriptive-type" value. */
271
491144b5 272static bool ada_ignore_descriptive_types_p = false;
c6044dd1 273
e802dbe0
JB
274 /* Inferior-specific data. */
275
276/* Per-inferior data for this module. */
277
278struct ada_inferior_data
279{
280 /* The ada__tags__type_specific_data type, which is used when decoding
281 tagged types. With older versions of GNAT, this type was directly
282 accessible through a component ("tsd") in the object tag. But this
283 is no longer the case, so we cache it for each inferior. */
f37b313d 284 struct type *tsd_type = nullptr;
3eecfa55
JB
285
286 /* The exception_support_info data. This data is used to determine
287 how to implement support for Ada exception catchpoints in a given
288 inferior. */
f37b313d 289 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
290};
291
292/* Our key to this module's inferior data. */
08b8a139 293static const registry<inferior>::key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
294
295/* Return our inferior data for the given inferior (INF).
296
297 This function always returns a valid pointer to an allocated
298 ada_inferior_data structure. If INF's inferior data has not
299 been previously set, this functions creates a new one with all
300 fields set to zero, sets INF's inferior to it, and then returns
301 a pointer to that newly allocated ada_inferior_data. */
302
303static struct ada_inferior_data *
304get_ada_inferior_data (struct inferior *inf)
305{
306 struct ada_inferior_data *data;
307
f37b313d 308 data = ada_inferior_data.get (inf);
e802dbe0 309 if (data == NULL)
f37b313d 310 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
311
312 return data;
313}
314
315/* Perform all necessary cleanups regarding our module's inferior data
316 that is required after the inferior INF just exited. */
317
318static void
319ada_inferior_exit (struct inferior *inf)
320{
f37b313d 321 ada_inferior_data.clear (inf);
e802dbe0
JB
322}
323
ee01b665
JB
324
325 /* program-space-specific data. */
326
9d1c303d
TT
327/* The result of a symbol lookup to be stored in our symbol cache. */
328
329struct cache_entry
ee01b665 330{
9d1c303d
TT
331 /* The name used to perform the lookup. */
332 std::string name;
333 /* The namespace used during the lookup. */
334 domain_enum domain = UNDEF_DOMAIN;
335 /* The symbol returned by the lookup, or NULL if no matching symbol
336 was found. */
337 struct symbol *sym = nullptr;
338 /* The block where the symbol was found, or NULL if no matching
339 symbol was found. */
340 const struct block *block = nullptr;
ee01b665
JB
341};
342
9d1c303d
TT
343/* The symbol cache uses this type when searching. */
344
345struct cache_entry_search
346{
347 const char *name;
348 domain_enum domain;
349
350 hashval_t hash () const
351 {
352 /* This must agree with hash_cache_entry, below. */
353 return htab_hash_string (name);
354 }
355};
356
357/* Hash function for cache_entry. */
358
359static hashval_t
360hash_cache_entry (const void *v)
361{
362 const cache_entry *entry = (const cache_entry *) v;
363 return htab_hash_string (entry->name.c_str ());
364}
365
366/* Equality function for cache_entry. */
367
368static int
369eq_cache_entry (const void *a, const void *b)
370{
371 const cache_entry *entrya = (const cache_entry *) a;
372 const cache_entry_search *entryb = (const cache_entry_search *) b;
373
374 return entrya->domain == entryb->domain && entrya->name == entryb->name;
375}
376
ee01b665 377/* Key to our per-program-space data. */
9d1c303d 378static const registry<program_space>::key<htab, htab_deleter>
08b8a139 379 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
9d1c303d 386static htab_t
ee01b665
JB
387get_ada_pspace_data (struct program_space *pspace)
388{
9d1c303d
TT
389 htab_t data = ada_pspace_data_handle.get (pspace);
390 if (data == nullptr)
391 {
392 data = htab_create_alloc (10, hash_cache_entry, eq_cache_entry,
393 htab_delete_entry<cache_entry>,
394 xcalloc, xfree);
395 ada_pspace_data_handle.set (pspace, data);
396 }
ee01b665
JB
397
398 return data;
399}
400
dda83cd7 401 /* Utilities */
4c4b4cd2 402
720d1a40 403/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 404 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
405
406 Normally, we really expect a typedef type to only have 1 typedef layer.
407 In other words, we really expect the target type of a typedef type to be
408 a non-typedef type. This is particularly true for Ada units, because
409 the language does not have a typedef vs not-typedef distinction.
410 In that respect, the Ada compiler has been trying to eliminate as many
411 typedef definitions in the debugging information, since they generally
412 do not bring any extra information (we still use typedef under certain
413 circumstances related mostly to the GNAT encoding).
414
415 Unfortunately, we have seen situations where the debugging information
416 generated by the compiler leads to such multiple typedef layers. For
417 instance, consider the following example with stabs:
418
419 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
420 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
421
422 This is an error in the debugging information which causes type
423 pck__float_array___XUP to be defined twice, and the second time,
424 it is defined as a typedef of a typedef.
425
426 This is on the fringe of legality as far as debugging information is
427 concerned, and certainly unexpected. But it is easy to handle these
428 situations correctly, so we can afford to be lenient in this case. */
429
430static struct type *
431ada_typedef_target_type (struct type *type)
432{
78134374 433 while (type->code () == TYPE_CODE_TYPEDEF)
27710edb 434 type = type->target_type ();
720d1a40
JB
435 return type;
436}
437
41d27058
JB
438/* Given DECODED_NAME a string holding a symbol name in its
439 decoded form (ie using the Ada dotted notation), returns
440 its unqualified name. */
441
442static const char *
443ada_unqualified_name (const char *decoded_name)
444{
2b0f535a
JB
445 const char *result;
446
447 /* If the decoded name starts with '<', it means that the encoded
448 name does not follow standard naming conventions, and thus that
449 it is not your typical Ada symbol name. Trying to unqualify it
450 is therefore pointless and possibly erroneous. */
451 if (decoded_name[0] == '<')
452 return decoded_name;
453
454 result = strrchr (decoded_name, '.');
41d27058
JB
455 if (result != NULL)
456 result++; /* Skip the dot... */
457 else
458 result = decoded_name;
459
460 return result;
461}
462
39e7af3e 463/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 464
39e7af3e 465static std::string
41d27058
JB
466add_angle_brackets (const char *str)
467{
39e7af3e 468 return string_printf ("<%s>", str);
41d27058 469}
96d887e8 470
14f9c5c9 471/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 472 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
473
474static int
ebf56fd3 475field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
476{
477 int len = strlen (target);
5b4ee69b 478
d2e4a39e 479 return
4c4b4cd2
PH
480 (strncmp (field_name, target, len) == 0
481 && (field_name[len] == '\0'
dda83cd7
SM
482 || (startswith (field_name + len, "___")
483 && strcmp (field_name + strlen (field_name) - 6,
484 "___XVN") != 0)));
14f9c5c9
AS
485}
486
487
872c8b51
JB
488/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
489 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
490 and return its index. This function also handles fields whose name
491 have ___ suffixes because the compiler sometimes alters their name
492 by adding such a suffix to represent fields with certain constraints.
493 If the field could not be found, return a negative number if
494 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
495
496int
497ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 498 int maybe_missing)
4c4b4cd2
PH
499{
500 int fieldno;
872c8b51
JB
501 struct type *struct_type = check_typedef ((struct type *) type);
502
1f704f76 503 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 504 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
505 return fieldno;
506
507 if (!maybe_missing)
323e0a4a 508 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 509 field_name, struct_type->name ());
4c4b4cd2
PH
510
511 return -1;
512}
513
514/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
515
516int
d2e4a39e 517ada_name_prefix_len (const char *name)
14f9c5c9
AS
518{
519 if (name == NULL)
520 return 0;
d2e4a39e 521 else
14f9c5c9 522 {
d2e4a39e 523 const char *p = strstr (name, "___");
5b4ee69b 524
14f9c5c9 525 if (p == NULL)
dda83cd7 526 return strlen (name);
14f9c5c9 527 else
dda83cd7 528 return p - name;
14f9c5c9
AS
529 }
530}
531
4c4b4cd2
PH
532/* Return non-zero if SUFFIX is a suffix of STR.
533 Return zero if STR is null. */
534
14f9c5c9 535static int
d2e4a39e 536is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
537{
538 int len1, len2;
5b4ee69b 539
14f9c5c9
AS
540 if (str == NULL)
541 return 0;
542 len1 = strlen (str);
543 len2 = strlen (suffix);
4c4b4cd2 544 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
545}
546
4c4b4cd2
PH
547/* The contents of value VAL, treated as a value of type TYPE. The
548 result is an lval in memory if VAL is. */
14f9c5c9 549
d2e4a39e 550static struct value *
4c4b4cd2 551coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 552{
61ee279c 553 type = ada_check_typedef (type);
d0c97917 554 if (val->type () == type)
4c4b4cd2 555 return val;
d2e4a39e 556 else
14f9c5c9 557 {
4c4b4cd2
PH
558 struct value *result;
559
d00664db 560 if (val->optimized_out ())
b27556e3 561 result = value::allocate_optimized_out (type);
3ee3b270 562 else if (val->lazy ()
f73e424f 563 /* Be careful not to make a lazy not_lval value. */
736355f2 564 || (val->lval () != not_lval
d0c97917 565 && type->length () > val->type ()->length ()))
cbe793af 566 result = value::allocate_lazy (type);
41e8491f
JK
567 else
568 {
317c3ed9 569 result = value::allocate (type);
6c49729e 570 val->contents_copy (result, 0, 0, type->length ());
41e8491f 571 }
8181b7b6 572 result->set_component_location (val);
f49d5fa2 573 result->set_bitsize (val->bitsize ());
5011c493 574 result->set_bitpos (val->bitpos ());
736355f2 575 if (result->lval () == lval_memory)
9feb2d07 576 result->set_address (val->address ());
14f9c5c9
AS
577 return result;
578 }
579}
580
fc1a4b47
AC
581static const gdb_byte *
582cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
583{
584 if (valaddr == NULL)
585 return NULL;
586 else
587 return valaddr + offset;
588}
589
590static CORE_ADDR
ebf56fd3 591cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
592{
593 if (address == 0)
594 return 0;
d2e4a39e 595 else
14f9c5c9
AS
596 return address + offset;
597}
598
4c4b4cd2
PH
599/* Issue a warning (as for the definition of warning in utils.c, but
600 with exactly one argument rather than ...), unless the limit on the
601 number of warnings has passed during the evaluation of the current
602 expression. */
a2249542 603
77109804
AC
604/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
605 provided by "complaint". */
a0b31db1 606static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 607
14f9c5c9 608static void
a2249542 609lim_warning (const char *format, ...)
14f9c5c9 610{
a2249542 611 va_list args;
a2249542 612
5b4ee69b 613 va_start (args, format);
4c4b4cd2
PH
614 warnings_issued += 1;
615 if (warnings_issued <= warning_limit)
a2249542
MK
616 vwarning (format, args);
617
618 va_end (args);
4c4b4cd2
PH
619}
620
0963b4bd 621/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 622static LONGEST
c3e5cd34 623max_of_size (int size)
4c4b4cd2 624{
76a01679 625 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 626
76a01679 627 return top_bit | (top_bit - 1);
4c4b4cd2
PH
628}
629
0963b4bd 630/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 631static LONGEST
c3e5cd34 632min_of_size (int size)
4c4b4cd2 633{
c3e5cd34 634 return -max_of_size (size) - 1;
4c4b4cd2
PH
635}
636
0963b4bd 637/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 638static ULONGEST
c3e5cd34 639umax_of_size (int size)
4c4b4cd2 640{
76a01679 641 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 642
76a01679 643 return top_bit | (top_bit - 1);
4c4b4cd2
PH
644}
645
0963b4bd 646/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
647static LONGEST
648max_of_type (struct type *t)
4c4b4cd2 649{
c6d940a9 650 if (t->is_unsigned ())
df86565b 651 return (LONGEST) umax_of_size (t->length ());
c3e5cd34 652 else
df86565b 653 return max_of_size (t->length ());
c3e5cd34
PH
654}
655
0963b4bd 656/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
657static LONGEST
658min_of_type (struct type *t)
659{
c6d940a9 660 if (t->is_unsigned ())
c3e5cd34
PH
661 return 0;
662 else
df86565b 663 return min_of_size (t->length ());
4c4b4cd2
PH
664}
665
666/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
667LONGEST
668ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 669{
b249d2c2 670 type = resolve_dynamic_type (type, {}, 0);
78134374 671 switch (type->code ())
4c4b4cd2
PH
672 {
673 case TYPE_CODE_RANGE:
d1fd641e
SM
674 {
675 const dynamic_prop &high = type->bounds ()->high;
676
9c0fb734 677 if (high.is_constant ())
d1fd641e
SM
678 return high.const_val ();
679 else
680 {
681 gdb_assert (high.kind () == PROP_UNDEFINED);
682
683 /* This happens when trying to evaluate a type's dynamic bound
684 without a live target. There is nothing relevant for us to
685 return here, so return 0. */
686 return 0;
687 }
688 }
4c4b4cd2 689 case TYPE_CODE_ENUM:
970db518 690 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
691 case TYPE_CODE_BOOL:
692 return 1;
693 case TYPE_CODE_CHAR:
76a01679 694 case TYPE_CODE_INT:
690cc4eb 695 return max_of_type (type);
4c4b4cd2 696 default:
43bbcdc2 697 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
698 }
699}
700
14e75d8e 701/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
702LONGEST
703ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 704{
b249d2c2 705 type = resolve_dynamic_type (type, {}, 0);
78134374 706 switch (type->code ())
4c4b4cd2
PH
707 {
708 case TYPE_CODE_RANGE:
d1fd641e
SM
709 {
710 const dynamic_prop &low = type->bounds ()->low;
711
9c0fb734 712 if (low.is_constant ())
d1fd641e
SM
713 return low.const_val ();
714 else
715 {
716 gdb_assert (low.kind () == PROP_UNDEFINED);
717
718 /* This happens when trying to evaluate a type's dynamic bound
719 without a live target. There is nothing relevant for us to
720 return here, so return 0. */
721 return 0;
722 }
723 }
4c4b4cd2 724 case TYPE_CODE_ENUM:
970db518 725 return type->field (0).loc_enumval ();
690cc4eb
PH
726 case TYPE_CODE_BOOL:
727 return 0;
728 case TYPE_CODE_CHAR:
76a01679 729 case TYPE_CODE_INT:
690cc4eb 730 return min_of_type (type);
4c4b4cd2 731 default:
43bbcdc2 732 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
733 }
734}
735
736/* The identity on non-range types. For range types, the underlying
76a01679 737 non-range scalar type. */
4c4b4cd2
PH
738
739static struct type *
18af8284 740get_base_type (struct type *type)
4c4b4cd2 741{
78134374 742 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 743 {
27710edb 744 if (type == type->target_type () || type->target_type () == NULL)
dda83cd7 745 return type;
27710edb 746 type = type->target_type ();
4c4b4cd2
PH
747 }
748 return type;
14f9c5c9 749}
41246937
JB
750
751/* Return a decoded version of the given VALUE. This means returning
752 a value whose type is obtained by applying all the GNAT-specific
85102364 753 encodings, making the resulting type a static but standard description
41246937
JB
754 of the initial type. */
755
756struct value *
757ada_get_decoded_value (struct value *value)
758{
d0c97917 759 struct type *type = ada_check_typedef (value->type ());
41246937
JB
760
761 if (ada_is_array_descriptor_type (type)
762 || (ada_is_constrained_packed_array_type (type)
dda83cd7 763 && type->code () != TYPE_CODE_PTR))
41246937 764 {
78134374 765 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 766 value = ada_coerce_to_simple_array_ptr (value);
41246937 767 else
dda83cd7 768 value = ada_coerce_to_simple_array (value);
41246937
JB
769 }
770 else
771 value = ada_to_fixed_value (value);
772
773 return value;
774}
775
776/* Same as ada_get_decoded_value, but with the given TYPE.
777 Because there is no associated actual value for this type,
778 the resulting type might be a best-effort approximation in
779 the case of dynamic types. */
780
781struct type *
782ada_get_decoded_type (struct type *type)
783{
784 type = to_static_fixed_type (type);
785 if (ada_is_constrained_packed_array_type (type))
786 type = ada_coerce_to_simple_array_type (type);
787 return type;
788}
789
4c4b4cd2 790\f
76a01679 791
dda83cd7 792 /* Language Selection */
14f9c5c9 793
96d887e8
PH
794/* If the main procedure is written in Ada, then return its name.
795 The result is good until the next call. Return NULL if the main
796 procedure doesn't appear to be in Ada. */
797
6f63b61d
TT
798const char *
799ada_main_name ()
96d887e8 800{
3b7344d5 801 struct bound_minimal_symbol msym;
e83e4e24 802 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 803
96d887e8
PH
804 /* For Ada, the name of the main procedure is stored in a specific
805 string constant, generated by the binder. Look for that symbol,
806 extract its address, and then read that string. If we didn't find
807 that string, then most probably the main procedure is not written
808 in Ada. */
809 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
810
3b7344d5 811 if (msym.minsym != NULL)
96d887e8 812 {
4aeddc50 813 CORE_ADDR main_program_name_addr = msym.value_address ();
96d887e8 814 if (main_program_name_addr == 0)
dda83cd7 815 error (_("Invalid address for Ada main program name."));
96d887e8 816
66920317 817 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 818 return main_program_name.get ();
96d887e8
PH
819 }
820
821 /* The main procedure doesn't seem to be in Ada. */
822 return NULL;
823}
14f9c5c9 824\f
dda83cd7 825 /* Symbols */
d2e4a39e 826
4c4b4cd2
PH
827/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
828 of NULLs. */
14f9c5c9 829
d2e4a39e
AS
830const struct ada_opname_map ada_opname_table[] = {
831 {"Oadd", "\"+\"", BINOP_ADD},
832 {"Osubtract", "\"-\"", BINOP_SUB},
833 {"Omultiply", "\"*\"", BINOP_MUL},
834 {"Odivide", "\"/\"", BINOP_DIV},
835 {"Omod", "\"mod\"", BINOP_MOD},
836 {"Orem", "\"rem\"", BINOP_REM},
837 {"Oexpon", "\"**\"", BINOP_EXP},
838 {"Olt", "\"<\"", BINOP_LESS},
839 {"Ole", "\"<=\"", BINOP_LEQ},
840 {"Ogt", "\">\"", BINOP_GTR},
841 {"Oge", "\">=\"", BINOP_GEQ},
842 {"Oeq", "\"=\"", BINOP_EQUAL},
843 {"One", "\"/=\"", BINOP_NOTEQUAL},
844 {"Oand", "\"and\"", BINOP_BITWISE_AND},
845 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
846 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
847 {"Oconcat", "\"&\"", BINOP_CONCAT},
848 {"Oabs", "\"abs\"", UNOP_ABS},
849 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
850 {"Oadd", "\"+\"", UNOP_PLUS},
851 {"Osubtract", "\"-\"", UNOP_NEG},
852 {NULL, NULL}
14f9c5c9
AS
853};
854
965bc1df
TT
855/* If STR is a decoded version of a compiler-provided suffix (like the
856 "[cold]" in "symbol[cold]"), return true. Otherwise, return
857 false. */
858
859static bool
860is_compiler_suffix (const char *str)
861{
862 gdb_assert (*str == '[');
863 ++str;
864 while (*str != '\0' && isalpha (*str))
865 ++str;
866 /* We accept a missing "]" in order to support completion. */
867 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
868}
869
315e4ebb
TT
870/* Append a non-ASCII character to RESULT. */
871static void
872append_hex_encoded (std::string &result, uint32_t one_char)
873{
874 if (one_char <= 0xff)
875 {
876 result.append ("U");
877 result.append (phex (one_char, 1));
878 }
879 else if (one_char <= 0xffff)
880 {
881 result.append ("W");
882 result.append (phex (one_char, 2));
883 }
884 else
885 {
886 result.append ("WW");
887 result.append (phex (one_char, 4));
888 }
889}
890
891/* Return a string that is a copy of the data in STORAGE, with
892 non-ASCII characters replaced by the appropriate hex encoding. A
893 template is used because, for UTF-8, we actually want to work with
894 UTF-32 codepoints. */
895template<typename T>
896std::string
897copy_and_hex_encode (struct obstack *storage)
898{
899 const T *chars = (T *) obstack_base (storage);
900 int num_chars = obstack_object_size (storage) / sizeof (T);
901 std::string result;
902 for (int i = 0; i < num_chars; ++i)
903 {
904 if (chars[i] <= 0x7f)
905 {
906 /* The host character set has to be a superset of ASCII, as
907 are all the other character sets we can use. */
908 result.push_back (chars[i]);
909 }
910 else
911 append_hex_encoded (result, chars[i]);
912 }
913 return result;
914}
915
5c4258f4 916/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 917 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 918 Otherwise, return the empty string in that case. */
4c4b4cd2 919
5c4258f4 920static std::string
b5ec771e 921ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 922{
4c4b4cd2 923 if (decoded == NULL)
5c4258f4 924 return {};
14f9c5c9 925
5c4258f4 926 std::string encoding_buffer;
315e4ebb 927 bool saw_non_ascii = false;
5c4258f4 928 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 929 {
315e4ebb
TT
930 if ((*p & 0x80) != 0)
931 saw_non_ascii = true;
932
cdc7bb92 933 if (*p == '.')
5c4258f4 934 encoding_buffer.append ("__");
965bc1df
TT
935 else if (*p == '[' && is_compiler_suffix (p))
936 {
937 encoding_buffer = encoding_buffer + "." + (p + 1);
938 if (encoding_buffer.back () == ']')
939 encoding_buffer.pop_back ();
940 break;
941 }
14f9c5c9 942 else if (*p == '"')
dda83cd7
SM
943 {
944 const struct ada_opname_map *mapping;
945
946 for (mapping = ada_opname_table;
947 mapping->encoded != NULL
948 && !startswith (p, mapping->decoded); mapping += 1)
949 ;
950 if (mapping->encoded == NULL)
b5ec771e
PA
951 {
952 if (throw_errors)
953 error (_("invalid Ada operator name: %s"), p);
954 else
5c4258f4 955 return {};
b5ec771e 956 }
5c4258f4 957 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
958 break;
959 }
d2e4a39e 960 else
5c4258f4 961 encoding_buffer.push_back (*p);
14f9c5c9
AS
962 }
963
315e4ebb
TT
964 /* If a non-ASCII character is seen, we must convert it to the
965 appropriate hex form. As this is more expensive, we keep track
966 of whether it is even necessary. */
967 if (saw_non_ascii)
968 {
969 auto_obstack storage;
970 bool is_utf8 = ada_source_charset == ada_utf8;
971 try
972 {
973 convert_between_encodings
974 (host_charset (),
975 is_utf8 ? HOST_UTF32 : ada_source_charset,
976 (const gdb_byte *) encoding_buffer.c_str (),
977 encoding_buffer.length (), 1,
978 &storage, translit_none);
979 }
980 catch (const gdb_exception &)
981 {
982 static bool warned = false;
983
984 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
985 might like to know why. */
986 if (!warned)
987 {
988 warned = true;
989 warning (_("charset conversion failure for '%s'.\n"
990 "You may have the wrong value for 'set ada source-charset'."),
991 encoding_buffer.c_str ());
992 }
993
994 /* We don't try to recover from errors. */
995 return encoding_buffer;
996 }
997
998 if (is_utf8)
999 return copy_and_hex_encode<uint32_t> (&storage);
1000 return copy_and_hex_encode<gdb_byte> (&storage);
1001 }
1002
4c4b4cd2 1003 return encoding_buffer;
14f9c5c9
AS
1004}
1005
315e4ebb
TT
1006/* Find the entry for C in the case-folding table. Return nullptr if
1007 the entry does not cover C. */
1008static const utf8_entry *
1009find_case_fold_entry (uint32_t c)
b5ec771e 1010{
315e4ebb
TT
1011 auto iter = std::lower_bound (std::begin (ada_case_fold),
1012 std::end (ada_case_fold),
1013 c);
1014 if (iter == std::end (ada_case_fold)
1015 || c < iter->start
1016 || c > iter->end)
1017 return nullptr;
1018 return &*iter;
b5ec771e
PA
1019}
1020
14f9c5c9 1021/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1022 quotes, unfolded, but with the quotes stripped away. If
1023 THROW_ON_ERROR is true, encoding failures will throw an exception
1024 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1025
5f9febe0 1026static const char *
315e4ebb 1027ada_fold_name (gdb::string_view name, bool throw_on_error = false)
14f9c5c9 1028{
5f9febe0 1029 static std::string fold_storage;
14f9c5c9 1030
6a780b67 1031 if (!name.empty () && name[0] == '\'')
01573d73 1032 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
1033 else
1034 {
315e4ebb
TT
1035 /* Why convert to UTF-32 and implement our own case-folding,
1036 rather than convert to wchar_t and use the platform's
1037 functions? I'm glad you asked.
1038
1039 The main problem is that GNAT implements an unusual rule for
1040 case folding. For ASCII letters, letters in single-byte
1041 encodings (such as ISO-8859-*), and Unicode letters that fit
1042 in a single byte (i.e., code point is <= 0xff), the letter is
1043 folded to lower case. Other Unicode letters are folded to
1044 upper case.
1045
1046 This rule means that the code must be able to examine the
1047 value of the character. And, some hosts do not use Unicode
1048 for wchar_t, so examining the value of such characters is
1049 forbidden. */
1050 auto_obstack storage;
1051 try
1052 {
1053 convert_between_encodings
1054 (host_charset (), HOST_UTF32,
1055 (const gdb_byte *) name.data (),
1056 name.length (), 1,
1057 &storage, translit_none);
1058 }
1059 catch (const gdb_exception &)
1060 {
1061 if (throw_on_error)
1062 throw;
1063
1064 static bool warned = false;
1065
1066 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1067 might like to know why. */
1068 if (!warned)
1069 {
1070 warned = true;
1071 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1072 "This normally should not happen, please file a bug report."),
1073 gdb::to_string (name).c_str (), host_charset ());
1074 }
1075
1076 /* We don't try to recover from errors; just return the
1077 original string. */
1078 fold_storage = gdb::to_string (name);
1079 return fold_storage.c_str ();
1080 }
1081
1082 bool is_utf8 = ada_source_charset == ada_utf8;
1083 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1084 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1085 for (int i = 0; i < num_chars; ++i)
1086 {
1087 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1088 if (entry != nullptr)
1089 {
1090 uint32_t low = chars[i] + entry->lower_delta;
1091 if (!is_utf8 || low <= 0xff)
1092 chars[i] = low;
1093 else
1094 chars[i] = chars[i] + entry->upper_delta;
1095 }
1096 }
1097
1098 /* Now convert back to ordinary characters. */
1099 auto_obstack reconverted;
1100 try
1101 {
1102 convert_between_encodings (HOST_UTF32,
1103 host_charset (),
1104 (const gdb_byte *) chars,
1105 num_chars * sizeof (uint32_t),
1106 sizeof (uint32_t),
1107 &reconverted,
1108 translit_none);
1109 obstack_1grow (&reconverted, '\0');
1110 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1111 }
1112 catch (const gdb_exception &)
1113 {
1114 if (throw_on_error)
1115 throw;
1116
1117 static bool warned = false;
1118
1119 /* Converting back from UTF-32 shouldn't normally fail, but
1120 there are some host encodings without upper/lower
1121 equivalence. */
1122 if (!warned)
1123 {
1124 warned = true;
1125 warning (_("could not convert the lower-cased variant of '%s'\n"
1126 "from UTF-32 to the host encoding (%s)."),
1127 gdb::to_string (name).c_str (), host_charset ());
1128 }
1129
1130 /* We don't try to recover from errors; just return the
1131 original string. */
1132 fold_storage = gdb::to_string (name);
1133 }
14f9c5c9
AS
1134 }
1135
5f9febe0 1136 return fold_storage.c_str ();
14f9c5c9
AS
1137}
1138
5fea9794
TT
1139/* The "encoded" form of DECODED, according to GNAT conventions. If
1140 FOLD is true (the default), case-fold any ordinary symbol. Symbols
1141 with <...> quoting are not folded in any case. */
315e4ebb
TT
1142
1143std::string
5fea9794 1144ada_encode (const char *decoded, bool fold)
315e4ebb 1145{
5fea9794 1146 if (fold && decoded[0] != '<')
315e4ebb
TT
1147 decoded = ada_fold_name (decoded);
1148 return ada_encode_1 (decoded, true);
1149}
1150
529cad9c
PH
1151/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1152
1153static int
1154is_lower_alphanum (const char c)
1155{
1156 return (isdigit (c) || (isalpha (c) && islower (c)));
1157}
1158
c90092fe
JB
1159/* ENCODED is the linkage name of a symbol and LEN contains its length.
1160 This function saves in LEN the length of that same symbol name but
1161 without either of these suffixes:
29480c32
JB
1162 . .{DIGIT}+
1163 . ${DIGIT}+
1164 . ___{DIGIT}+
1165 . __{DIGIT}+.
c90092fe 1166
29480c32
JB
1167 These are suffixes introduced by the compiler for entities such as
1168 nested subprogram for instance, in order to avoid name clashes.
1169 They do not serve any purpose for the debugger. */
1170
1171static void
1172ada_remove_trailing_digits (const char *encoded, int *len)
1173{
1174 if (*len > 1 && isdigit (encoded[*len - 1]))
1175 {
1176 int i = *len - 2;
5b4ee69b 1177
29480c32 1178 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1179 i--;
29480c32 1180 if (i >= 0 && encoded[i] == '.')
dda83cd7 1181 *len = i;
29480c32 1182 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1183 *len = i;
61012eef 1184 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1185 *len = i - 2;
61012eef 1186 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1187 *len = i - 1;
29480c32
JB
1188 }
1189}
1190
1191/* Remove the suffix introduced by the compiler for protected object
1192 subprograms. */
1193
1194static void
1195ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1196{
1197 /* Remove trailing N. */
1198
1199 /* Protected entry subprograms are broken into two
1200 separate subprograms: The first one is unprotected, and has
1201 a 'N' suffix; the second is the protected version, and has
0963b4bd 1202 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1203 the protection. Since the P subprograms are internally generated,
1204 we leave these names undecoded, giving the user a clue that this
1205 entity is internal. */
1206
1207 if (*len > 1
1208 && encoded[*len - 1] == 'N'
1209 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1210 *len = *len - 1;
1211}
1212
965bc1df
TT
1213/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1214 then update *LEN to remove the suffix and return the offset of the
1215 character just past the ".". Otherwise, return -1. */
1216
1217static int
1218remove_compiler_suffix (const char *encoded, int *len)
1219{
1220 int offset = *len - 1;
1221 while (offset > 0 && isalpha (encoded[offset]))
1222 --offset;
1223 if (offset > 0 && encoded[offset] == '.')
1224 {
1225 *len = offset;
1226 return offset + 1;
1227 }
1228 return -1;
1229}
1230
315e4ebb
TT
1231/* Convert an ASCII hex string to a number. Reads exactly N
1232 characters from STR. Returns true on success, false if one of the
1233 digits was not a hex digit. */
1234static bool
1235convert_hex (const char *str, int n, uint32_t *out)
1236{
1237 uint32_t result = 0;
1238
1239 for (int i = 0; i < n; ++i)
1240 {
1241 if (!isxdigit (str[i]))
1242 return false;
1243 result <<= 4;
1244 result |= fromhex (str[i]);
1245 }
1246
1247 *out = result;
1248 return true;
1249}
1250
1251/* Convert a wide character from its ASCII hex representation in STR
1252 (consisting of exactly N characters) to the host encoding,
1253 appending the resulting bytes to OUT. If N==2 and the Ada source
1254 charset is not UTF-8, then hex refers to an encoding in the
1255 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1256 Return false and do not modify OUT on conversion failure. */
1257static bool
1258convert_from_hex_encoded (std::string &out, const char *str, int n)
1259{
1260 uint32_t value;
1261
1262 if (!convert_hex (str, n, &value))
1263 return false;
1264 try
1265 {
1266 auto_obstack bytes;
1267 /* In the 'U' case, the hex digits encode the character in the
1268 Ada source charset. However, if the source charset is UTF-8,
1269 this really means it is a single-byte UTF-32 character. */
1270 if (n == 2 && ada_source_charset != ada_utf8)
1271 {
1272 gdb_byte one_char = (gdb_byte) value;
1273
1274 convert_between_encodings (ada_source_charset, host_charset (),
1275 &one_char,
1276 sizeof (one_char), sizeof (one_char),
1277 &bytes, translit_none);
1278 }
1279 else
1280 convert_between_encodings (HOST_UTF32, host_charset (),
1281 (const gdb_byte *) &value,
1282 sizeof (value), sizeof (value),
1283 &bytes, translit_none);
1284 obstack_1grow (&bytes, '\0');
1285 out.append ((const char *) obstack_base (&bytes));
1286 }
1287 catch (const gdb_exception &)
1288 {
1289 /* On failure, the caller will just let the encoded form
1290 through, which seems basically reasonable. */
1291 return false;
1292 }
1293
1294 return true;
1295}
1296
8a3df5ac 1297/* See ada-lang.h. */
14f9c5c9 1298
f945dedf 1299std::string
5c94f938 1300ada_decode (const char *encoded, bool wrap, bool operators)
14f9c5c9 1301{
36f5ca53 1302 int i;
14f9c5c9 1303 int len0;
d2e4a39e 1304 const char *p;
14f9c5c9 1305 int at_start_name;
f945dedf 1306 std::string decoded;
965bc1df 1307 int suffix = -1;
d2e4a39e 1308
0d81f350
JG
1309 /* With function descriptors on PPC64, the value of a symbol named
1310 ".FN", if it exists, is the entry point of the function "FN". */
1311 if (encoded[0] == '.')
1312 encoded += 1;
1313
29480c32
JB
1314 /* The name of the Ada main procedure starts with "_ada_".
1315 This prefix is not part of the decoded name, so skip this part
1316 if we see this prefix. */
61012eef 1317 if (startswith (encoded, "_ada_"))
4c4b4cd2 1318 encoded += 5;
81eaa506
TT
1319 /* The "___ghost_" prefix is used for ghost entities. Normally
1320 these aren't preserved but when they are, it's useful to see
1321 them. */
1322 if (startswith (encoded, "___ghost_"))
1323 encoded += 9;
14f9c5c9 1324
29480c32
JB
1325 /* If the name starts with '_', then it is not a properly encoded
1326 name, so do not attempt to decode it. Similarly, if the name
1327 starts with '<', the name should not be decoded. */
4c4b4cd2 1328 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1329 goto Suppress;
1330
4c4b4cd2 1331 len0 = strlen (encoded);
4c4b4cd2 1332
965bc1df
TT
1333 suffix = remove_compiler_suffix (encoded, &len0);
1334
29480c32
JB
1335 ada_remove_trailing_digits (encoded, &len0);
1336 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1337
4c4b4cd2
PH
1338 /* Remove the ___X.* suffix if present. Do not forget to verify that
1339 the suffix is located before the current "end" of ENCODED. We want
1340 to avoid re-matching parts of ENCODED that have previously been
1341 marked as discarded (by decrementing LEN0). */
1342 p = strstr (encoded, "___");
1343 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1344 {
1345 if (p[3] == 'X')
dda83cd7 1346 len0 = p - encoded;
14f9c5c9 1347 else
dda83cd7 1348 goto Suppress;
14f9c5c9 1349 }
4c4b4cd2 1350
29480c32
JB
1351 /* Remove any trailing TKB suffix. It tells us that this symbol
1352 is for the body of a task, but that information does not actually
1353 appear in the decoded name. */
1354
61012eef 1355 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1356 len0 -= 3;
76a01679 1357
a10967fa
JB
1358 /* Remove any trailing TB suffix. The TB suffix is slightly different
1359 from the TKB suffix because it is used for non-anonymous task
1360 bodies. */
1361
61012eef 1362 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1363 len0 -= 2;
1364
29480c32
JB
1365 /* Remove trailing "B" suffixes. */
1366 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1367
61012eef 1368 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1369 len0 -= 1;
1370
29480c32
JB
1371 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1372
4c4b4cd2 1373 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1374 {
4c4b4cd2
PH
1375 i = len0 - 2;
1376 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1377 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1378 i -= 1;
4c4b4cd2 1379 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1380 len0 = i - 1;
033bc52b 1381 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1382 len0 = i;
d2e4a39e 1383 }
14f9c5c9 1384
29480c32
JB
1385 /* The first few characters that are not alphabetic are not part
1386 of any encoding we use, so we can copy them over verbatim. */
1387
36f5ca53
TT
1388 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1389 decoded.push_back (encoded[i]);
14f9c5c9
AS
1390
1391 at_start_name = 1;
1392 while (i < len0)
1393 {
29480c32 1394 /* Is this a symbol function? */
5c94f938 1395 if (operators && at_start_name && encoded[i] == 'O')
dda83cd7
SM
1396 {
1397 int k;
1398
1399 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1400 {
1401 int op_len = strlen (ada_opname_table[k].encoded);
1402 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1403 op_len - 1) == 0)
1404 && !isalnum (encoded[i + op_len]))
1405 {
36f5ca53 1406 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1407 at_start_name = 0;
1408 i += op_len;
dda83cd7
SM
1409 break;
1410 }
1411 }
1412 if (ada_opname_table[k].encoded != NULL)
1413 continue;
1414 }
14f9c5c9
AS
1415 at_start_name = 0;
1416
529cad9c 1417 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1418 into "." (just below). */
529cad9c 1419
61012eef 1420 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1421 i += 2;
529cad9c 1422
29480c32 1423 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1424 be translated into "." (just below). These are internal names
1425 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1426
1427 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1428 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1429 && isdigit (encoded [i+4]))
1430 {
1431 int k = i + 5;
1432
1433 while (k < len0 && isdigit (encoded[k]))
1434 k++; /* Skip any extra digit. */
1435
1436 /* Double-check that the "__B_{DIGITS}+" sequence we found
1437 is indeed followed by "__". */
1438 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1439 i = k;
1440 }
29480c32 1441
529cad9c
PH
1442 /* Remove _E{DIGITS}+[sb] */
1443
1444 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1445 of subprograms created by the compiler for each entry. The first
1446 one implements the actual entry code, and has a suffix following
1447 the convention above; the second one implements the barrier and
1448 uses the same convention as above, except that the 'E' is replaced
1449 by a 'B'.
529cad9c 1450
dda83cd7
SM
1451 Just as above, we do not decode the name of barrier functions
1452 to give the user a clue that the code he is debugging has been
1453 internally generated. */
529cad9c
PH
1454
1455 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1456 && isdigit (encoded[i+2]))
1457 {
1458 int k = i + 3;
1459
1460 while (k < len0 && isdigit (encoded[k]))
1461 k++;
1462
1463 if (k < len0
1464 && (encoded[k] == 'b' || encoded[k] == 's'))
1465 {
1466 k++;
1467 /* Just as an extra precaution, make sure that if this
1468 suffix is followed by anything else, it is a '_'.
1469 Otherwise, we matched this sequence by accident. */
1470 if (k == len0
1471 || (k < len0 && encoded[k] == '_'))
1472 i = k;
1473 }
1474 }
529cad9c
PH
1475
1476 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1477 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1478
1479 if (i < len0 + 3
dda83cd7
SM
1480 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1481 {
1482 /* Backtrack a bit up until we reach either the begining of
1483 the encoded name, or "__". Make sure that we only find
1484 digits or lowercase characters. */
1485 const char *ptr = encoded + i - 1;
1486
1487 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1488 ptr--;
1489 if (ptr < encoded
1490 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1491 i++;
1492 }
529cad9c 1493
315e4ebb
TT
1494 if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
1495 {
1496 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1497 {
1498 i += 3;
1499 continue;
1500 }
1501 }
1502 else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
1503 {
1504 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1505 {
1506 i += 5;
1507 continue;
1508 }
1509 }
1510 else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
1511 && isxdigit (encoded[i + 2]))
1512 {
1513 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1514 {
1515 i += 10;
1516 continue;
1517 }
1518 }
1519
4c4b4cd2 1520 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1521 {
1522 /* This is a X[bn]* sequence not separated from the previous
1523 part of the name with a non-alpha-numeric character (in other
1524 words, immediately following an alpha-numeric character), then
1525 verify that it is placed at the end of the encoded name. If
1526 not, then the encoding is not valid and we should abort the
1527 decoding. Otherwise, just skip it, it is used in body-nested
1528 package names. */
1529 do
1530 i += 1;
1531 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1532 if (i < len0)
1533 goto Suppress;
1534 }
cdc7bb92 1535 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1536 {
1537 /* Replace '__' by '.'. */
36f5ca53 1538 decoded.push_back ('.');
dda83cd7
SM
1539 at_start_name = 1;
1540 i += 2;
dda83cd7 1541 }
14f9c5c9 1542 else
dda83cd7
SM
1543 {
1544 /* It's a character part of the decoded name, so just copy it
1545 over. */
36f5ca53 1546 decoded.push_back (encoded[i]);
dda83cd7 1547 i += 1;
dda83cd7 1548 }
14f9c5c9 1549 }
14f9c5c9 1550
29480c32
JB
1551 /* Decoded names should never contain any uppercase character.
1552 Double-check this, and abort the decoding if we find one. */
1553
5c94f938
TT
1554 if (operators)
1555 {
1556 for (i = 0; i < decoded.length(); ++i)
1557 if (isupper (decoded[i]) || decoded[i] == ' ')
1558 goto Suppress;
1559 }
14f9c5c9 1560
965bc1df
TT
1561 /* If the compiler added a suffix, append it now. */
1562 if (suffix >= 0)
1563 decoded = decoded + "[" + &encoded[suffix] + "]";
1564
f945dedf 1565 return decoded;
14f9c5c9
AS
1566
1567Suppress:
8a3df5ac
TT
1568 if (!wrap)
1569 return {};
1570
4c4b4cd2 1571 if (encoded[0] == '<')
f945dedf 1572 decoded = encoded;
14f9c5c9 1573 else
f945dedf 1574 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1575 return decoded;
4c4b4cd2
PH
1576}
1577
033bc52b
TT
1578#ifdef GDB_SELF_TEST
1579
1580static void
1581ada_decode_tests ()
1582{
1583 /* This isn't valid, but used to cause a crash. PR gdb/30639. The
1584 result does not really matter very much. */
1585 SELF_CHECK (ada_decode ("44") == "44");
1586}
1587
1588#endif
1589
4c4b4cd2
PH
1590/* Table for keeping permanent unique copies of decoded names. Once
1591 allocated, names in this table are never released. While this is a
1592 storage leak, it should not be significant unless there are massive
1593 changes in the set of decoded names in successive versions of a
1594 symbol table loaded during a single session. */
1595static struct htab *decoded_names_store;
1596
1597/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1598 in the language-specific part of GSYMBOL, if it has not been
1599 previously computed. Tries to save the decoded name in the same
1600 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1601 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1602 GSYMBOL).
4c4b4cd2
PH
1603 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1604 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1605 when a decoded name is cached in it. */
4c4b4cd2 1606
45e6c716 1607const char *
f85f34ed 1608ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1609{
f85f34ed
TT
1610 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1611 const char **resultp =
615b3f62 1612 &gsymbol->language_specific.demangled_name;
5b4ee69b 1613
f85f34ed 1614 if (!gsymbol->ada_mangled)
4c4b4cd2 1615 {
4d4eaa30 1616 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1617 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1618
f85f34ed 1619 gsymbol->ada_mangled = 1;
5b4ee69b 1620
f85f34ed 1621 if (obstack != NULL)
f945dedf 1622 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1623 else
dda83cd7 1624 {
f85f34ed
TT
1625 /* Sometimes, we can't find a corresponding objfile, in
1626 which case, we put the result on the heap. Since we only
1627 decode when needed, we hope this usually does not cause a
1628 significant memory leak (FIXME). */
1629
dda83cd7
SM
1630 char **slot = (char **) htab_find_slot (decoded_names_store,
1631 decoded.c_str (), INSERT);
5b4ee69b 1632
dda83cd7
SM
1633 if (*slot == NULL)
1634 *slot = xstrdup (decoded.c_str ());
1635 *resultp = *slot;
1636 }
4c4b4cd2 1637 }
14f9c5c9 1638
4c4b4cd2
PH
1639 return *resultp;
1640}
76a01679 1641
14f9c5c9 1642\f
d2e4a39e 1643
dda83cd7 1644 /* Arrays */
14f9c5c9 1645
28c85d6c
JB
1646/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1647 generated by the GNAT compiler to describe the index type used
1648 for each dimension of an array, check whether it follows the latest
1649 known encoding. If not, fix it up to conform to the latest encoding.
1650 Otherwise, do nothing. This function also does nothing if
1651 INDEX_DESC_TYPE is NULL.
1652
85102364 1653 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1654 Initially, the information would be provided through the name of each
1655 field of the structure type only, while the type of these fields was
1656 described as unspecified and irrelevant. The debugger was then expected
1657 to perform a global type lookup using the name of that field in order
1658 to get access to the full index type description. Because these global
1659 lookups can be very expensive, the encoding was later enhanced to make
1660 the global lookup unnecessary by defining the field type as being
1661 the full index type description.
1662
1663 The purpose of this routine is to allow us to support older versions
1664 of the compiler by detecting the use of the older encoding, and by
1665 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1666 we essentially replace each field's meaningless type by the associated
1667 index subtype). */
1668
1669void
1670ada_fixup_array_indexes_type (struct type *index_desc_type)
1671{
1672 int i;
1673
1674 if (index_desc_type == NULL)
1675 return;
1f704f76 1676 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1677
1678 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1679 to check one field only, no need to check them all). If not, return
1680 now.
1681
1682 If our INDEX_DESC_TYPE was generated using the older encoding,
1683 the field type should be a meaningless integer type whose name
1684 is not equal to the field name. */
940da03e
SM
1685 if (index_desc_type->field (0).type ()->name () != NULL
1686 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1687 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1688 return;
1689
1690 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1691 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1692 {
33d16dd9 1693 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1694 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1695
1696 if (raw_type)
5d14b6e5 1697 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1698 }
1699}
1700
4c4b4cd2
PH
1701/* The desc_* routines return primitive portions of array descriptors
1702 (fat pointers). */
14f9c5c9
AS
1703
1704/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1705 level of indirection, if needed. */
1706
d2e4a39e
AS
1707static struct type *
1708desc_base_type (struct type *type)
14f9c5c9
AS
1709{
1710 if (type == NULL)
1711 return NULL;
61ee279c 1712 type = ada_check_typedef (type);
78134374 1713 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1714 type = ada_typedef_target_type (type);
1715
1265e4aa 1716 if (type != NULL
78134374 1717 && (type->code () == TYPE_CODE_PTR
dda83cd7 1718 || type->code () == TYPE_CODE_REF))
27710edb 1719 return ada_check_typedef (type->target_type ());
14f9c5c9
AS
1720 else
1721 return type;
1722}
1723
4c4b4cd2
PH
1724/* True iff TYPE indicates a "thin" array pointer type. */
1725
14f9c5c9 1726static int
d2e4a39e 1727is_thin_pntr (struct type *type)
14f9c5c9 1728{
d2e4a39e 1729 return
14f9c5c9
AS
1730 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1731 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1732}
1733
4c4b4cd2
PH
1734/* The descriptor type for thin pointer type TYPE. */
1735
d2e4a39e
AS
1736static struct type *
1737thin_descriptor_type (struct type *type)
14f9c5c9 1738{
d2e4a39e 1739 struct type *base_type = desc_base_type (type);
5b4ee69b 1740
14f9c5c9
AS
1741 if (base_type == NULL)
1742 return NULL;
1743 if (is_suffix (ada_type_name (base_type), "___XVE"))
1744 return base_type;
d2e4a39e 1745 else
14f9c5c9 1746 {
d2e4a39e 1747 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1748
14f9c5c9 1749 if (alt_type == NULL)
dda83cd7 1750 return base_type;
14f9c5c9 1751 else
dda83cd7 1752 return alt_type;
14f9c5c9
AS
1753 }
1754}
1755
4c4b4cd2
PH
1756/* A pointer to the array data for thin-pointer value VAL. */
1757
d2e4a39e
AS
1758static struct value *
1759thin_data_pntr (struct value *val)
14f9c5c9 1760{
d0c97917 1761 struct type *type = ada_check_typedef (val->type ());
556bdfd4 1762 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1763
556bdfd4
UW
1764 data_type = lookup_pointer_type (data_type);
1765
78134374 1766 if (type->code () == TYPE_CODE_PTR)
cda03344 1767 return value_cast (data_type, val->copy ());
d2e4a39e 1768 else
9feb2d07 1769 return value_from_longest (data_type, val->address ());
14f9c5c9
AS
1770}
1771
4c4b4cd2
PH
1772/* True iff TYPE indicates a "thick" array pointer type. */
1773
14f9c5c9 1774static int
d2e4a39e 1775is_thick_pntr (struct type *type)
14f9c5c9
AS
1776{
1777 type = desc_base_type (type);
78134374 1778 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1779 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1780}
1781
4c4b4cd2
PH
1782/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1783 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1784
d2e4a39e
AS
1785static struct type *
1786desc_bounds_type (struct type *type)
14f9c5c9 1787{
d2e4a39e 1788 struct type *r;
14f9c5c9
AS
1789
1790 type = desc_base_type (type);
1791
1792 if (type == NULL)
1793 return NULL;
1794 else if (is_thin_pntr (type))
1795 {
1796 type = thin_descriptor_type (type);
1797 if (type == NULL)
dda83cd7 1798 return NULL;
14f9c5c9
AS
1799 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1800 if (r != NULL)
dda83cd7 1801 return ada_check_typedef (r);
14f9c5c9 1802 }
78134374 1803 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1804 {
1805 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1806 if (r != NULL)
27710edb 1807 return ada_check_typedef (ada_check_typedef (r)->target_type ());
14f9c5c9
AS
1808 }
1809 return NULL;
1810}
1811
1812/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1813 one, a pointer to its bounds data. Otherwise NULL. */
1814
d2e4a39e
AS
1815static struct value *
1816desc_bounds (struct value *arr)
14f9c5c9 1817{
d0c97917 1818 struct type *type = ada_check_typedef (arr->type ());
5b4ee69b 1819
d2e4a39e 1820 if (is_thin_pntr (type))
14f9c5c9 1821 {
d2e4a39e 1822 struct type *bounds_type =
dda83cd7 1823 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1824 LONGEST addr;
1825
4cdfadb1 1826 if (bounds_type == NULL)
dda83cd7 1827 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1828
1829 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1830 since desc_type is an XVE-encoded type (and shouldn't be),
1831 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1832 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1833 addr = value_as_long (arr);
d2e4a39e 1834 else
9feb2d07 1835 addr = arr->address ();
14f9c5c9 1836
d2e4a39e 1837 return
dda83cd7 1838 value_from_longest (lookup_pointer_type (bounds_type),
df86565b 1839 addr - bounds_type->length ());
14f9c5c9
AS
1840 }
1841
1842 else if (is_thick_pntr (type))
05e522ef 1843 {
158cc4fe 1844 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef 1845 _("Bad GNAT array descriptor"));
d0c97917 1846 struct type *p_bounds_type = p_bounds->type ();
05e522ef
JB
1847
1848 if (p_bounds_type
78134374 1849 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef 1850 {
27710edb 1851 struct type *target_type = p_bounds_type->target_type ();
05e522ef 1852
e46d3488 1853 if (target_type->is_stub ())
05e522ef
JB
1854 p_bounds = value_cast (lookup_pointer_type
1855 (ada_check_typedef (target_type)),
1856 p_bounds);
1857 }
1858 else
1859 error (_("Bad GNAT array descriptor"));
1860
1861 return p_bounds;
1862 }
14f9c5c9
AS
1863 else
1864 return NULL;
1865}
1866
4c4b4cd2
PH
1867/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1868 position of the field containing the address of the bounds data. */
1869
14f9c5c9 1870static int
d2e4a39e 1871fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1872{
b610c045 1873 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1874}
1875
1876/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1877 size of the field containing the address of the bounds data. */
1878
14f9c5c9 1879static int
d2e4a39e 1880fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1881{
1882 type = desc_base_type (type);
1883
3757d2d4
SM
1884 if (type->field (1).bitsize () > 0)
1885 return type->field (1).bitsize ();
14f9c5c9 1886 else
df86565b 1887 return 8 * ada_check_typedef (type->field (1).type ())->length ();
14f9c5c9
AS
1888}
1889
4c4b4cd2 1890/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1891 pointer to one, the type of its array data (a array-with-no-bounds type);
1892 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1893 data. */
4c4b4cd2 1894
d2e4a39e 1895static struct type *
556bdfd4 1896desc_data_target_type (struct type *type)
14f9c5c9
AS
1897{
1898 type = desc_base_type (type);
1899
4c4b4cd2 1900 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1901 if (is_thin_pntr (type))
940da03e 1902 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1903 else if (is_thick_pntr (type))
556bdfd4
UW
1904 {
1905 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1906
1907 if (data_type
78134374 1908 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
27710edb 1909 return ada_check_typedef (data_type->target_type ());
556bdfd4
UW
1910 }
1911
1912 return NULL;
14f9c5c9
AS
1913}
1914
1915/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1916 its array data. */
4c4b4cd2 1917
d2e4a39e
AS
1918static struct value *
1919desc_data (struct value *arr)
14f9c5c9 1920{
d0c97917 1921 struct type *type = arr->type ();
5b4ee69b 1922
14f9c5c9
AS
1923 if (is_thin_pntr (type))
1924 return thin_data_pntr (arr);
1925 else if (is_thick_pntr (type))
158cc4fe 1926 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1927 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1928 else
1929 return NULL;
1930}
1931
1932
1933/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1934 position of the field containing the address of the data. */
1935
14f9c5c9 1936static int
d2e4a39e 1937fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1938{
b610c045 1939 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1940}
1941
1942/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1943 size of the field containing the address of the data. */
1944
14f9c5c9 1945static int
d2e4a39e 1946fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1947{
1948 type = desc_base_type (type);
1949
3757d2d4
SM
1950 if (type->field (0).bitsize () > 0)
1951 return type->field (0).bitsize ();
d2e4a39e 1952 else
df86565b 1953 return TARGET_CHAR_BIT * type->field (0).type ()->length ();
14f9c5c9
AS
1954}
1955
4c4b4cd2 1956/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1957 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1958 bound, if WHICH is 1. The first bound is I=1. */
1959
d2e4a39e
AS
1960static struct value *
1961desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1962{
250106a7
TT
1963 char bound_name[20];
1964 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1965 which ? 'U' : 'L', i - 1);
158cc4fe 1966 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1967 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1968}
1969
1970/* If BOUNDS is an array-bounds structure type, return the bit position
1971 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1972 bound, if WHICH is 1. The first bound is I=1. */
1973
14f9c5c9 1974static int
d2e4a39e 1975desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1976{
b610c045 1977 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1978}
1979
1980/* If BOUNDS is an array-bounds structure type, return the bit field size
1981 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1982 bound, if WHICH is 1. The first bound is I=1. */
1983
76a01679 1984static int
d2e4a39e 1985desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1986{
1987 type = desc_base_type (type);
1988
3757d2d4
SM
1989 if (type->field (2 * i + which - 2).bitsize () > 0)
1990 return type->field (2 * i + which - 2).bitsize ();
d2e4a39e 1991 else
df86565b 1992 return 8 * type->field (2 * i + which - 2).type ()->length ();
14f9c5c9
AS
1993}
1994
1995/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1996 Ith bound (numbering from 1). Otherwise, NULL. */
1997
d2e4a39e
AS
1998static struct type *
1999desc_index_type (struct type *type, int i)
14f9c5c9
AS
2000{
2001 type = desc_base_type (type);
2002
78134374 2003 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
2004 {
2005 char bound_name[20];
2006 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
2007 return lookup_struct_elt_type (type, bound_name, 1);
2008 }
d2e4a39e 2009 else
14f9c5c9
AS
2010 return NULL;
2011}
2012
4c4b4cd2
PH
2013/* The number of index positions in the array-bounds type TYPE.
2014 Return 0 if TYPE is NULL. */
2015
14f9c5c9 2016static int
d2e4a39e 2017desc_arity (struct type *type)
14f9c5c9
AS
2018{
2019 type = desc_base_type (type);
2020
2021 if (type != NULL)
1f704f76 2022 return type->num_fields () / 2;
14f9c5c9
AS
2023 return 0;
2024}
2025
4c4b4cd2
PH
2026/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2027 an array descriptor type (representing an unconstrained array
2028 type). */
2029
76a01679
JB
2030static int
2031ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2032{
2033 if (type == NULL)
2034 return 0;
61ee279c 2035 type = ada_check_typedef (type);
78134374 2036 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2037 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2038}
2039
52ce6436 2040/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2041 * to one. */
52ce6436 2042
2c0b251b 2043static int
52ce6436
PH
2044ada_is_array_type (struct type *type)
2045{
78134374
SM
2046 while (type != NULL
2047 && (type->code () == TYPE_CODE_PTR
2048 || type->code () == TYPE_CODE_REF))
27710edb 2049 type = type->target_type ();
52ce6436
PH
2050 return ada_is_direct_array_type (type);
2051}
2052
4c4b4cd2 2053/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2054
14f9c5c9 2055int
4c4b4cd2 2056ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2057{
2058 if (type == NULL)
2059 return 0;
61ee279c 2060 type = ada_check_typedef (type);
78134374
SM
2061 return (type->code () == TYPE_CODE_ARRAY
2062 || (type->code () == TYPE_CODE_PTR
27710edb 2063 && (ada_check_typedef (type->target_type ())->code ()
78134374 2064 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2065}
2066
4c4b4cd2
PH
2067/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2068
14f9c5c9 2069int
4c4b4cd2 2070ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2071{
556bdfd4 2072 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2073
2074 if (type == NULL)
2075 return 0;
61ee279c 2076 type = ada_check_typedef (type);
556bdfd4 2077 return (data_type != NULL
78134374 2078 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2079 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2080}
2081
4c4b4cd2 2082/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2083 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2084 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2085 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2086 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2087 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2088 a descriptor. */
de93309a
SM
2089
2090static struct type *
d2e4a39e 2091ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2092{
d0c97917
TT
2093 if (ada_is_constrained_packed_array_type (arr->type ()))
2094 return decode_constrained_packed_array_type (arr->type ());
14f9c5c9 2095
d0c97917
TT
2096 if (!ada_is_array_descriptor_type (arr->type ()))
2097 return arr->type ();
d2e4a39e
AS
2098
2099 if (!bounds)
ad82864c
JB
2100 {
2101 struct type *array_type =
d0c97917 2102 ada_check_typedef (desc_data_target_type (arr->type ()));
ad82864c 2103
d0c97917 2104 if (ada_is_unconstrained_packed_array_type (arr->type ()))
886176b8
SM
2105 array_type->field (0).set_bitsize
2106 (decode_packed_array_bitsize (arr->type ()));
2107
ad82864c
JB
2108 return array_type;
2109 }
14f9c5c9
AS
2110 else
2111 {
d2e4a39e 2112 struct type *elt_type;
14f9c5c9 2113 int arity;
d2e4a39e 2114 struct value *descriptor;
14f9c5c9 2115
d0c97917
TT
2116 elt_type = ada_array_element_type (arr->type (), -1);
2117 arity = ada_array_arity (arr->type ());
14f9c5c9 2118
d2e4a39e 2119 if (elt_type == NULL || arity == 0)
d0c97917 2120 return ada_check_typedef (arr->type ());
14f9c5c9
AS
2121
2122 descriptor = desc_bounds (arr);
d2e4a39e 2123 if (value_as_long (descriptor) == 0)
dda83cd7 2124 return NULL;
d2e4a39e 2125 while (arity > 0)
dda83cd7 2126 {
9fa83a7a 2127 type_allocator alloc (arr->type ());
dda83cd7
SM
2128 struct value *low = desc_one_bound (descriptor, arity, 0);
2129 struct value *high = desc_one_bound (descriptor, arity, 1);
2130
2131 arity -= 1;
e727c536
TT
2132 struct type *range_type
2133 = create_static_range_type (alloc, low->type (),
2134 longest_to_int (value_as_long (low)),
2135 longest_to_int (value_as_long (high)));
9e76b17a 2136 elt_type = create_array_type (alloc, elt_type, range_type);
ad82864c 2137
d0c97917 2138 if (ada_is_unconstrained_packed_array_type (arr->type ()))
e67ad678
JB
2139 {
2140 /* We need to store the element packed bitsize, as well as
dda83cd7 2141 recompute the array size, because it was previously
e67ad678
JB
2142 computed based on the unpacked element size. */
2143 LONGEST lo = value_as_long (low);
2144 LONGEST hi = value_as_long (high);
2145
886176b8
SM
2146 elt_type->field (0).set_bitsize
2147 (decode_packed_array_bitsize (arr->type ()));
2148
e67ad678 2149 /* If the array has no element, then the size is already
dda83cd7 2150 zero, and does not need to be recomputed. */
e67ad678
JB
2151 if (lo < hi)
2152 {
2153 int array_bitsize =
3757d2d4 2154 (hi - lo + 1) * elt_type->field (0).bitsize ();
e67ad678 2155
9e76b17a 2156 elt_type->set_length ((array_bitsize + 7) / 8);
e67ad678
JB
2157 }
2158 }
dda83cd7 2159 }
14f9c5c9
AS
2160
2161 return lookup_pointer_type (elt_type);
2162 }
2163}
2164
2165/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2166 Otherwise, returns either a standard GDB array with bounds set
2167 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2168 GDB array. Returns NULL if ARR is a null fat pointer. */
2169
d2e4a39e
AS
2170struct value *
2171ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2172{
d0c97917 2173 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2174 {
d2e4a39e 2175 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2176
14f9c5c9 2177 if (arrType == NULL)
dda83cd7 2178 return NULL;
cda03344 2179 return value_cast (arrType, desc_data (arr)->copy ());
14f9c5c9 2180 }
d0c97917 2181 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2182 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2183 else
2184 return arr;
2185}
2186
2187/* If ARR does not represent an array, returns ARR unchanged.
2188 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2189 be ARR itself if it already is in the proper form). */
2190
720d1a40 2191struct value *
d2e4a39e 2192ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2193{
d0c97917 2194 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2195 {
d2e4a39e 2196 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2197
14f9c5c9 2198 if (arrVal == NULL)
dda83cd7 2199 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2200 return value_ind (arrVal);
2201 }
d0c97917 2202 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2203 return decode_constrained_packed_array (arr);
d2e4a39e 2204 else
14f9c5c9
AS
2205 return arr;
2206}
2207
2208/* If TYPE represents a GNAT array type, return it translated to an
2209 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2210 packing). For other types, is the identity. */
2211
d2e4a39e
AS
2212struct type *
2213ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2214{
ad82864c
JB
2215 if (ada_is_constrained_packed_array_type (type))
2216 return decode_constrained_packed_array_type (type);
17280b9f
UW
2217
2218 if (ada_is_array_descriptor_type (type))
556bdfd4 2219 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2220
2221 return type;
14f9c5c9
AS
2222}
2223
4c4b4cd2
PH
2224/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2225
ad82864c 2226static int
57567375 2227ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2228{
2229 if (type == NULL)
2230 return 0;
4c4b4cd2 2231 type = desc_base_type (type);
61ee279c 2232 type = ada_check_typedef (type);
d2e4a39e 2233 return
14f9c5c9
AS
2234 ada_type_name (type) != NULL
2235 && strstr (ada_type_name (type), "___XP") != NULL;
2236}
2237
ad82864c
JB
2238/* Non-zero iff TYPE represents a standard GNAT constrained
2239 packed-array type. */
2240
2241int
2242ada_is_constrained_packed_array_type (struct type *type)
2243{
57567375 2244 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2245 && !ada_is_array_descriptor_type (type);
2246}
2247
2248/* Non-zero iff TYPE represents an array descriptor for a
2249 unconstrained packed-array type. */
2250
2251static int
2252ada_is_unconstrained_packed_array_type (struct type *type)
2253{
57567375
TT
2254 if (!ada_is_array_descriptor_type (type))
2255 return 0;
2256
2257 if (ada_is_gnat_encoded_packed_array_type (type))
2258 return 1;
2259
2260 /* If we saw GNAT encodings, then the above code is sufficient.
2261 However, with minimal encodings, we will just have a thick
2262 pointer instead. */
2263 if (is_thick_pntr (type))
2264 {
2265 type = desc_base_type (type);
2266 /* The structure's first field is a pointer to an array, so this
2267 fetches the array type. */
27710edb 2268 type = type->field (0).type ()->target_type ();
af5300fe
TV
2269 if (type->code () == TYPE_CODE_TYPEDEF)
2270 type = ada_typedef_target_type (type);
57567375 2271 /* Now we can see if the array elements are packed. */
3757d2d4 2272 return type->field (0).bitsize () > 0;
57567375
TT
2273 }
2274
2275 return 0;
ad82864c
JB
2276}
2277
c9a28cbe
TT
2278/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2279 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2280
2281static bool
2282ada_is_any_packed_array_type (struct type *type)
2283{
2284 return (ada_is_constrained_packed_array_type (type)
2285 || (type->code () == TYPE_CODE_ARRAY
3757d2d4 2286 && type->field (0).bitsize () % 8 != 0));
c9a28cbe
TT
2287}
2288
ad82864c
JB
2289/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2290 return the size of its elements in bits. */
2291
2292static long
2293decode_packed_array_bitsize (struct type *type)
2294{
0d5cff50
DE
2295 const char *raw_name;
2296 const char *tail;
ad82864c
JB
2297 long bits;
2298
720d1a40
JB
2299 /* Access to arrays implemented as fat pointers are encoded as a typedef
2300 of the fat pointer type. We need the name of the fat pointer type
2301 to do the decoding, so strip the typedef layer. */
78134374 2302 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2303 type = ada_typedef_target_type (type);
2304
2305 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2306 if (!raw_name)
2307 raw_name = ada_type_name (desc_base_type (type));
2308
2309 if (!raw_name)
2310 return 0;
2311
2312 tail = strstr (raw_name, "___XP");
57567375
TT
2313 if (tail == nullptr)
2314 {
2315 gdb_assert (is_thick_pntr (type));
2316 /* The structure's first field is a pointer to an array, so this
2317 fetches the array type. */
27710edb 2318 type = type->field (0).type ()->target_type ();
57567375 2319 /* Now we can see if the array elements are packed. */
3757d2d4 2320 return type->field (0).bitsize ();
57567375 2321 }
ad82864c
JB
2322
2323 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2324 {
2325 lim_warning
2326 (_("could not understand bit size information on packed array"));
2327 return 0;
2328 }
2329
2330 return bits;
2331}
2332
14f9c5c9
AS
2333/* Given that TYPE is a standard GDB array type with all bounds filled
2334 in, and that the element size of its ultimate scalar constituents
2335 (that is, either its elements, or, if it is an array of arrays, its
2336 elements' elements, etc.) is *ELT_BITS, return an identical type,
2337 but with the bit sizes of its elements (and those of any
2338 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2339 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2340 in bits.
2341
2342 Note that, for arrays whose index type has an XA encoding where
2343 a bound references a record discriminant, getting that discriminant,
2344 and therefore the actual value of that bound, is not possible
2345 because none of the given parameters gives us access to the record.
2346 This function assumes that it is OK in the context where it is being
2347 used to return an array whose bounds are still dynamic and where
2348 the length is arbitrary. */
4c4b4cd2 2349
d2e4a39e 2350static struct type *
ad82864c 2351constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2352{
d2e4a39e
AS
2353 struct type *new_elt_type;
2354 struct type *new_type;
99b1c762
JB
2355 struct type *index_type_desc;
2356 struct type *index_type;
14f9c5c9
AS
2357 LONGEST low_bound, high_bound;
2358
61ee279c 2359 type = ada_check_typedef (type);
78134374 2360 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2361 return type;
2362
99b1c762
JB
2363 index_type_desc = ada_find_parallel_type (type, "___XA");
2364 if (index_type_desc)
940da03e 2365 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2366 NULL);
2367 else
3d967001 2368 index_type = type->index_type ();
99b1c762 2369
9e76b17a 2370 type_allocator alloc (type);
ad82864c 2371 new_elt_type =
27710edb 2372 constrained_packed_array_type (ada_check_typedef (type->target_type ()),
ad82864c 2373 elt_bits);
9e76b17a 2374 new_type = create_array_type (alloc, new_elt_type, index_type);
886176b8 2375 new_type->field (0).set_bitsize (*elt_bits);
d0e39ea2 2376 new_type->set_name (ada_type_name (type));
14f9c5c9 2377
78134374 2378 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2379 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2380 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2381 low_bound = high_bound = 0;
2382 if (high_bound < low_bound)
b6cdbc9a
SM
2383 {
2384 *elt_bits = 0;
2385 new_type->set_length (0);
2386 }
d2e4a39e 2387 else
14f9c5c9
AS
2388 {
2389 *elt_bits *= (high_bound - low_bound + 1);
b6cdbc9a 2390 new_type->set_length ((*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
14f9c5c9
AS
2391 }
2392
9cdd0d12 2393 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2394 return new_type;
2395}
2396
ad82864c
JB
2397/* The array type encoded by TYPE, where
2398 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2399
d2e4a39e 2400static struct type *
ad82864c 2401decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2402{
0d5cff50 2403 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2404 char *name;
0d5cff50 2405 const char *tail;
d2e4a39e 2406 struct type *shadow_type;
14f9c5c9 2407 long bits;
14f9c5c9 2408
727e3d2e
JB
2409 if (!raw_name)
2410 raw_name = ada_type_name (desc_base_type (type));
2411
2412 if (!raw_name)
2413 return NULL;
2414
2415 name = (char *) alloca (strlen (raw_name) + 1);
2416 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2417 type = desc_base_type (type);
2418
14f9c5c9
AS
2419 memcpy (name, raw_name, tail - raw_name);
2420 name[tail - raw_name] = '\000';
2421
b4ba55a1
JB
2422 shadow_type = ada_find_parallel_type_with_name (type, name);
2423
2424 if (shadow_type == NULL)
14f9c5c9 2425 {
323e0a4a 2426 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2427 return NULL;
2428 }
f168693b 2429 shadow_type = check_typedef (shadow_type);
14f9c5c9 2430
78134374 2431 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2432 {
0963b4bd
MS
2433 lim_warning (_("could not understand bounds "
2434 "information on packed array"));
14f9c5c9
AS
2435 return NULL;
2436 }
d2e4a39e 2437
ad82864c
JB
2438 bits = decode_packed_array_bitsize (type);
2439 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2440}
2441
a7400e44
TT
2442/* Helper function for decode_constrained_packed_array. Set the field
2443 bitsize on a series of packed arrays. Returns the number of
2444 elements in TYPE. */
2445
2446static LONGEST
2447recursively_update_array_bitsize (struct type *type)
2448{
2449 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2450
2451 LONGEST low, high;
1f8d2881 2452 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2453 || low > high)
2454 return 0;
2455 LONGEST our_len = high - low + 1;
2456
27710edb 2457 struct type *elt_type = type->target_type ();
a7400e44
TT
2458 if (elt_type->code () == TYPE_CODE_ARRAY)
2459 {
2460 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
3757d2d4 2461 LONGEST elt_bitsize = elt_len * elt_type->field (0).bitsize ();
886176b8 2462 type->field (0).set_bitsize (elt_bitsize);
a7400e44 2463
b6cdbc9a
SM
2464 type->set_length (((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2465 / HOST_CHAR_BIT));
a7400e44
TT
2466 }
2467
2468 return our_len;
2469}
2470
ad82864c
JB
2471/* Given that ARR is a struct value *indicating a GNAT constrained packed
2472 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2473 standard GDB array type except that the BITSIZEs of the array
2474 target types are set to the number of bits in each element, and the
4c4b4cd2 2475 type length is set appropriately. */
14f9c5c9 2476
d2e4a39e 2477static struct value *
ad82864c 2478decode_constrained_packed_array (struct value *arr)
14f9c5c9 2479{
4c4b4cd2 2480 struct type *type;
14f9c5c9 2481
11aa919a
PMR
2482 /* If our value is a pointer, then dereference it. Likewise if
2483 the value is a reference. Make sure that this operation does not
2484 cause the target type to be fixed, as this would indirectly cause
2485 this array to be decoded. The rest of the routine assumes that
2486 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2487 and "value_ind" routines to perform the dereferencing, as opposed
2488 to using "ada_coerce_ref" or "ada_value_ind". */
2489 arr = coerce_ref (arr);
d0c97917 2490 if (ada_check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
284614f0 2491 arr = value_ind (arr);
4c4b4cd2 2492
d0c97917 2493 type = decode_constrained_packed_array_type (arr->type ());
14f9c5c9
AS
2494 if (type == NULL)
2495 {
323e0a4a 2496 error (_("can't unpack array"));
14f9c5c9
AS
2497 return NULL;
2498 }
61ee279c 2499
a7400e44
TT
2500 /* Decoding the packed array type could not correctly set the field
2501 bitsizes for any dimension except the innermost, because the
2502 bounds may be variable and were not passed to that function. So,
2503 we further resolve the array bounds here and then update the
2504 sizes. */
efaf1ae0 2505 const gdb_byte *valaddr = arr->contents_for_printing ().data ();
9feb2d07 2506 CORE_ADDR address = arr->address ();
a7400e44 2507 gdb::array_view<const gdb_byte> view
df86565b 2508 = gdb::make_array_view (valaddr, type->length ());
a7400e44
TT
2509 type = resolve_dynamic_type (type, view, address);
2510 recursively_update_array_bitsize (type);
2511
d0c97917
TT
2512 if (type_byte_order (arr->type ()) == BFD_ENDIAN_BIG
2513 && ada_is_modular_type (arr->type ()))
61ee279c
PH
2514 {
2515 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2516 array with no wrapper. In order to interpret the value through
2517 the (left-justified) packed array type we just built, we must
2518 first left-justify it. */
61ee279c
PH
2519 int bit_size, bit_pos;
2520 ULONGEST mod;
2521
d0c97917 2522 mod = ada_modulus (arr->type ()) - 1;
61ee279c
PH
2523 bit_size = 0;
2524 while (mod > 0)
2525 {
2526 bit_size += 1;
2527 mod >>= 1;
2528 }
d0c97917 2529 bit_pos = HOST_CHAR_BIT * arr->type ()->length () - bit_size;
61ee279c
PH
2530 arr = ada_value_primitive_packed_val (arr, NULL,
2531 bit_pos / HOST_CHAR_BIT,
2532 bit_pos % HOST_CHAR_BIT,
2533 bit_size,
2534 type);
2535 }
2536
4c4b4cd2 2537 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2538}
2539
2540
2541/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2542 given in IND. ARR must be a simple array. */
14f9c5c9 2543
d2e4a39e
AS
2544static struct value *
2545value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2546{
2547 int i;
2548 int bits, elt_off, bit_off;
2549 long elt_total_bit_offset;
d2e4a39e
AS
2550 struct type *elt_type;
2551 struct value *v;
14f9c5c9
AS
2552
2553 bits = 0;
2554 elt_total_bit_offset = 0;
d0c97917 2555 elt_type = ada_check_typedef (arr->type ());
d2e4a39e 2556 for (i = 0; i < arity; i += 1)
14f9c5c9 2557 {
78134374 2558 if (elt_type->code () != TYPE_CODE_ARRAY
3757d2d4 2559 || elt_type->field (0).bitsize () == 0)
dda83cd7
SM
2560 error
2561 (_("attempt to do packed indexing of "
0963b4bd 2562 "something other than a packed array"));
14f9c5c9 2563 else
dda83cd7
SM
2564 {
2565 struct type *range_type = elt_type->index_type ();
2566 LONGEST lowerbound, upperbound;
2567 LONGEST idx;
2568
1f8d2881 2569 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2570 {
2571 lim_warning (_("don't know bounds of array"));
2572 lowerbound = upperbound = 0;
2573 }
2574
2575 idx = pos_atr (ind[i]);
2576 if (idx < lowerbound || idx > upperbound)
2577 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2578 (long) idx);
3757d2d4 2579 bits = elt_type->field (0).bitsize ();
dda83cd7 2580 elt_total_bit_offset += (idx - lowerbound) * bits;
27710edb 2581 elt_type = ada_check_typedef (elt_type->target_type ());
dda83cd7 2582 }
14f9c5c9
AS
2583 }
2584 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2585 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2586
2587 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2588 bits, elt_type);
14f9c5c9
AS
2589 return v;
2590}
2591
4c4b4cd2 2592/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2593
2594static int
d2e4a39e 2595has_negatives (struct type *type)
14f9c5c9 2596{
78134374 2597 switch (type->code ())
d2e4a39e
AS
2598 {
2599 default:
2600 return 0;
2601 case TYPE_CODE_INT:
c6d940a9 2602 return !type->is_unsigned ();
d2e4a39e 2603 case TYPE_CODE_RANGE:
5537ddd0 2604 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2605 }
14f9c5c9 2606}
d2e4a39e 2607
f93fca70 2608/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2609 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2610 the unpacked buffer.
14f9c5c9 2611
5b639dea
JB
2612 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2613 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2614
f93fca70
JB
2615 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2616 zero otherwise.
14f9c5c9 2617
f93fca70 2618 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2619
f93fca70
JB
2620 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2621
2622static void
2623ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2624 gdb_byte *unpacked, int unpacked_len,
2625 int is_big_endian, int is_signed_type,
2626 int is_scalar)
2627{
a1c95e6b
JB
2628 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2629 int src_idx; /* Index into the source area */
2630 int src_bytes_left; /* Number of source bytes left to process. */
2631 int srcBitsLeft; /* Number of source bits left to move */
2632 int unusedLS; /* Number of bits in next significant
dda83cd7 2633 byte of source that are unused */
a1c95e6b 2634
a1c95e6b
JB
2635 int unpacked_idx; /* Index into the unpacked buffer */
2636 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2637
4c4b4cd2 2638 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2639 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2640 unsigned char sign;
a1c95e6b 2641
4c4b4cd2
PH
2642 /* Transmit bytes from least to most significant; delta is the direction
2643 the indices move. */
f93fca70 2644 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2645
5b639dea
JB
2646 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2647 bits from SRC. .*/
2648 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2649 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2650 bit_size, unpacked_len);
2651
14f9c5c9 2652 srcBitsLeft = bit_size;
086ca51f 2653 src_bytes_left = src_len;
f93fca70 2654 unpacked_bytes_left = unpacked_len;
14f9c5c9 2655 sign = 0;
f93fca70
JB
2656
2657 if (is_big_endian)
14f9c5c9 2658 {
086ca51f 2659 src_idx = src_len - 1;
f93fca70
JB
2660 if (is_signed_type
2661 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2662 sign = ~0;
d2e4a39e
AS
2663
2664 unusedLS =
dda83cd7
SM
2665 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2666 % HOST_CHAR_BIT;
14f9c5c9 2667
f93fca70
JB
2668 if (is_scalar)
2669 {
dda83cd7
SM
2670 accumSize = 0;
2671 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2672 }
2673 else
2674 {
dda83cd7
SM
2675 /* Non-scalar values must be aligned at a byte boundary... */
2676 accumSize =
2677 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2678 /* ... And are placed at the beginning (most-significant) bytes
2679 of the target. */
2680 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2681 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2682 }
14f9c5c9 2683 }
d2e4a39e 2684 else
14f9c5c9
AS
2685 {
2686 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2687
086ca51f 2688 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2689 unusedLS = bit_offset;
2690 accumSize = 0;
2691
f93fca70 2692 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2693 sign = ~0;
14f9c5c9 2694 }
d2e4a39e 2695
14f9c5c9 2696 accum = 0;
086ca51f 2697 while (src_bytes_left > 0)
14f9c5c9
AS
2698 {
2699 /* Mask for removing bits of the next source byte that are not
dda83cd7 2700 part of the value. */
d2e4a39e 2701 unsigned int unusedMSMask =
dda83cd7
SM
2702 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2703 1;
4c4b4cd2 2704 /* Sign-extend bits for this byte. */
14f9c5c9 2705 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2706
d2e4a39e 2707 accum |=
dda83cd7 2708 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2709 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2710 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2711 {
2712 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2713 accumSize -= HOST_CHAR_BIT;
2714 accum >>= HOST_CHAR_BIT;
2715 unpacked_bytes_left -= 1;
2716 unpacked_idx += delta;
2717 }
14f9c5c9
AS
2718 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2719 unusedLS = 0;
086ca51f
JB
2720 src_bytes_left -= 1;
2721 src_idx += delta;
14f9c5c9 2722 }
086ca51f 2723 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2724 {
2725 accum |= sign << accumSize;
db297a65 2726 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2727 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2728 if (accumSize < 0)
2729 accumSize = 0;
14f9c5c9 2730 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2731 unpacked_bytes_left -= 1;
2732 unpacked_idx += delta;
14f9c5c9 2733 }
f93fca70
JB
2734}
2735
2736/* Create a new value of type TYPE from the contents of OBJ starting
2737 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2738 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2739 assigning through the result will set the field fetched from.
2740 VALADDR is ignored unless OBJ is NULL, in which case,
2741 VALADDR+OFFSET must address the start of storage containing the
2742 packed value. The value returned in this case is never an lval.
2743 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2744
2745struct value *
2746ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2747 long offset, int bit_offset, int bit_size,
dda83cd7 2748 struct type *type)
f93fca70
JB
2749{
2750 struct value *v;
bfb1c796 2751 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2752 gdb_byte *unpacked;
220475ed 2753 const int is_scalar = is_scalar_type (type);
d5a22e77 2754 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2755 gdb::byte_vector staging;
f93fca70
JB
2756
2757 type = ada_check_typedef (type);
2758
d0a9e810 2759 if (obj == NULL)
bfb1c796 2760 src = valaddr + offset;
d0a9e810 2761 else
efaf1ae0 2762 src = obj->contents ().data () + offset;
d0a9e810
JB
2763
2764 if (is_dynamic_type (type))
2765 {
2766 /* The length of TYPE might by dynamic, so we need to resolve
2767 TYPE in order to know its actual size, which we then use
2768 to create the contents buffer of the value we return.
2769 The difficulty is that the data containing our object is
2770 packed, and therefore maybe not at a byte boundary. So, what
2771 we do, is unpack the data into a byte-aligned buffer, and then
2772 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2773 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2774 staging.resize (staging_len);
d0a9e810
JB
2775
2776 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2777 staging.data (), staging.size (),
d0a9e810
JB
2778 is_big_endian, has_negatives (type),
2779 is_scalar);
b249d2c2 2780 type = resolve_dynamic_type (type, staging, 0);
df86565b 2781 if (type->length () < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
0cafa88c
JB
2782 {
2783 /* This happens when the length of the object is dynamic,
2784 and is actually smaller than the space reserved for it.
2785 For instance, in an array of variant records, the bit_size
2786 we're given is the array stride, which is constant and
2787 normally equal to the maximum size of its element.
2788 But, in reality, each element only actually spans a portion
2789 of that stride. */
df86565b 2790 bit_size = type->length () * HOST_CHAR_BIT;
0cafa88c 2791 }
d0a9e810
JB
2792 }
2793
f93fca70
JB
2794 if (obj == NULL)
2795 {
317c3ed9 2796 v = value::allocate (type);
bfb1c796 2797 src = valaddr + offset;
f93fca70 2798 }
736355f2 2799 else if (obj->lval () == lval_memory && obj->lazy ())
f93fca70 2800 {
0cafa88c 2801 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2802 gdb_byte *buf;
0cafa88c 2803
9feb2d07 2804 v = value_at (type, obj->address () + offset);
bfb1c796 2805 buf = (gdb_byte *) alloca (src_len);
9feb2d07 2806 read_memory (v->address (), buf, src_len);
bfb1c796 2807 src = buf;
f93fca70
JB
2808 }
2809 else
2810 {
317c3ed9 2811 v = value::allocate (type);
efaf1ae0 2812 src = obj->contents ().data () + offset;
f93fca70
JB
2813 }
2814
2815 if (obj != NULL)
2816 {
2817 long new_offset = offset;
2818
8181b7b6 2819 v->set_component_location (obj);
5011c493 2820 v->set_bitpos (bit_offset + obj->bitpos ());
f49d5fa2 2821 v->set_bitsize (bit_size);
5011c493 2822 if (v->bitpos () >= HOST_CHAR_BIT)
dda83cd7 2823 {
f93fca70 2824 ++new_offset;
5011c493 2825 v->set_bitpos (v->bitpos () - HOST_CHAR_BIT);
dda83cd7 2826 }
76675c4d 2827 v->set_offset (new_offset);
f93fca70
JB
2828
2829 /* Also set the parent value. This is needed when trying to
2830 assign a new value (in inferior memory). */
fac7bdaa 2831 v->set_parent (obj);
f93fca70
JB
2832 }
2833 else
f49d5fa2 2834 v->set_bitsize (bit_size);
bbe912ba 2835 unpacked = v->contents_writeable ().data ();
f93fca70
JB
2836
2837 if (bit_size == 0)
2838 {
df86565b 2839 memset (unpacked, 0, type->length ());
f93fca70
JB
2840 return v;
2841 }
2842
df86565b 2843 if (staging.size () == type->length ())
f93fca70 2844 {
d0a9e810
JB
2845 /* Small short-cut: If we've unpacked the data into a buffer
2846 of the same size as TYPE's length, then we can reuse that,
2847 instead of doing the unpacking again. */
d5722aa2 2848 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2849 }
d0a9e810
JB
2850 else
2851 ada_unpack_from_contents (src, bit_offset, bit_size,
df86565b 2852 unpacked, type->length (),
d0a9e810 2853 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2854
14f9c5c9
AS
2855 return v;
2856}
d2e4a39e 2857
14f9c5c9
AS
2858/* Store the contents of FROMVAL into the location of TOVAL.
2859 Return a new value with the location of TOVAL and contents of
2860 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2861 floating-point or non-scalar types. */
14f9c5c9 2862
d2e4a39e
AS
2863static struct value *
2864ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2865{
d0c97917 2866 struct type *type = toval->type ();
f49d5fa2 2867 int bits = toval->bitsize ();
14f9c5c9 2868
52ce6436
PH
2869 toval = ada_coerce_ref (toval);
2870 fromval = ada_coerce_ref (fromval);
2871
d0c97917 2872 if (ada_is_direct_array_type (toval->type ()))
52ce6436 2873 toval = ada_coerce_to_simple_array (toval);
d0c97917 2874 if (ada_is_direct_array_type (fromval->type ()))
52ce6436
PH
2875 fromval = ada_coerce_to_simple_array (fromval);
2876
4b53ca88 2877 if (!toval->deprecated_modifiable ())
323e0a4a 2878 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2879
736355f2 2880 if (toval->lval () == lval_memory
14f9c5c9 2881 && bits > 0
78134374 2882 && (type->code () == TYPE_CODE_FLT
dda83cd7 2883 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2884 {
5011c493 2885 int len = (toval->bitpos ()
df407dfe 2886 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2887 int from_size;
224c3ddb 2888 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2889 struct value *val;
9feb2d07 2890 CORE_ADDR to_addr = toval->address ();
14f9c5c9 2891
78134374 2892 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2893 fromval = value_cast (type, fromval);
14f9c5c9 2894
52ce6436 2895 read_memory (to_addr, buffer, len);
f49d5fa2 2896 from_size = fromval->bitsize ();
aced2898 2897 if (from_size == 0)
d0c97917 2898 from_size = fromval->type ()->length () * TARGET_CHAR_BIT;
d48e62f4 2899
d5a22e77 2900 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4 2901 ULONGEST from_offset = 0;
d0c97917 2902 if (is_big_endian && is_scalar_type (fromval->type ()))
d48e62f4 2903 from_offset = from_size - bits;
5011c493 2904 copy_bitwise (buffer, toval->bitpos (),
efaf1ae0 2905 fromval->contents ().data (), from_offset,
d48e62f4 2906 bits, is_big_endian);
972daa01 2907 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2908
cda03344 2909 val = toval->copy ();
bbe912ba 2910 memcpy (val->contents_raw ().data (),
efaf1ae0 2911 fromval->contents ().data (),
df86565b 2912 type->length ());
81ae560c 2913 val->deprecated_set_type (type);
d2e4a39e 2914
14f9c5c9
AS
2915 return val;
2916 }
2917
2918 return value_assign (toval, fromval);
2919}
2920
2921
7c512744
JB
2922/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2923 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2924 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2925 COMPONENT, and not the inferior's memory. The current contents
2926 of COMPONENT are ignored.
2927
2928 Although not part of the initial design, this function also works
2929 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2930 had a null address, and COMPONENT had an address which is equal to
2931 its offset inside CONTAINER. */
2932
52ce6436
PH
2933static void
2934value_assign_to_component (struct value *container, struct value *component,
2935 struct value *val)
2936{
2937 LONGEST offset_in_container =
9feb2d07 2938 (LONGEST) (component->address () - container->address ());
7c512744 2939 int bit_offset_in_container =
5011c493 2940 component->bitpos () - container->bitpos ();
52ce6436 2941 int bits;
7c512744 2942
d0c97917 2943 val = value_cast (component->type (), val);
52ce6436 2944
f49d5fa2 2945 if (component->bitsize () == 0)
d0c97917 2946 bits = TARGET_CHAR_BIT * component->type ()->length ();
52ce6436 2947 else
f49d5fa2 2948 bits = component->bitsize ();
52ce6436 2949
d0c97917 2950 if (type_byte_order (container->type ()) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2951 {
2952 int src_offset;
2953
d0c97917 2954 if (is_scalar_type (check_typedef (component->type ())))
dda83cd7 2955 src_offset
d0c97917 2956 = component->type ()->length () * TARGET_CHAR_BIT - bits;
2a62dfa9
JB
2957 else
2958 src_offset = 0;
bbe912ba 2959 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2960 + offset_in_container),
5011c493 2961 container->bitpos () + bit_offset_in_container,
efaf1ae0 2962 val->contents ().data (), src_offset, bits, 1);
2a62dfa9 2963 }
52ce6436 2964 else
bbe912ba 2965 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2966 + offset_in_container),
5011c493 2967 container->bitpos () + bit_offset_in_container,
efaf1ae0 2968 val->contents ().data (), 0, bits, 0);
7c512744
JB
2969}
2970
736ade86
XR
2971/* Determine if TYPE is an access to an unconstrained array. */
2972
d91e9ea8 2973bool
736ade86
XR
2974ada_is_access_to_unconstrained_array (struct type *type)
2975{
78134374 2976 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2977 && is_thick_pntr (ada_typedef_target_type (type)));
2978}
2979
4c4b4cd2
PH
2980/* The value of the element of array ARR at the ARITY indices given in IND.
2981 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2982 thereto. */
2983
d2e4a39e
AS
2984struct value *
2985ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2986{
2987 int k;
d2e4a39e
AS
2988 struct value *elt;
2989 struct type *elt_type;
14f9c5c9
AS
2990
2991 elt = ada_coerce_to_simple_array (arr);
2992
d0c97917 2993 elt_type = ada_check_typedef (elt->type ());
78134374 2994 if (elt_type->code () == TYPE_CODE_ARRAY
3757d2d4 2995 && elt_type->field (0).bitsize () > 0)
14f9c5c9
AS
2996 return value_subscript_packed (elt, arity, ind);
2997
2998 for (k = 0; k < arity; k += 1)
2999 {
27710edb 3000 struct type *saved_elt_type = elt_type->target_type ();
b9c50e9a 3001
78134374 3002 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3003 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3004
2497b498 3005 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3006
3007 if (ada_is_access_to_unconstrained_array (saved_elt_type)
d0c97917 3008 && elt->type ()->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3009 {
3010 /* The element is a typedef to an unconstrained array,
3011 except that the value_subscript call stripped the
3012 typedef layer. The typedef layer is GNAT's way to
3013 specify that the element is, at the source level, an
3014 access to the unconstrained array, rather than the
3015 unconstrained array. So, we need to restore that
3016 typedef layer, which we can do by forcing the element's
3017 type back to its original type. Otherwise, the returned
3018 value is going to be printed as the array, rather
3019 than as an access. Another symptom of the same issue
3020 would be that an expression trying to dereference the
3021 element would also be improperly rejected. */
81ae560c 3022 elt->deprecated_set_type (saved_elt_type);
b9c50e9a
XR
3023 }
3024
d0c97917 3025 elt_type = ada_check_typedef (elt->type ());
14f9c5c9 3026 }
b9c50e9a 3027
14f9c5c9
AS
3028 return elt;
3029}
3030
deede10c
JB
3031/* Assuming ARR is a pointer to a GDB array, the value of the element
3032 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3033 Does not read the entire array into memory.
3034
3035 Note: Unlike what one would expect, this function is used instead of
3036 ada_value_subscript for basically all non-packed array types. The reason
3037 for this is that a side effect of doing our own pointer arithmetics instead
3038 of relying on value_subscript is that there is no implicit typedef peeling.
3039 This is important for arrays of array accesses, where it allows us to
3040 preserve the fact that the array's element is an array access, where the
3041 access part os encoded in a typedef layer. */
14f9c5c9 3042
2c0b251b 3043static struct value *
deede10c 3044ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3045{
3046 int k;
919e6dbe 3047 struct value *array_ind = ada_value_ind (arr);
deede10c 3048 struct type *type
463b870d 3049 = check_typedef (array_ind->enclosing_type ());
919e6dbe 3050
78134374 3051 if (type->code () == TYPE_CODE_ARRAY
3757d2d4 3052 && type->field (0).bitsize () > 0)
919e6dbe 3053 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3054
3055 for (k = 0; k < arity; k += 1)
3056 {
3057 LONGEST lwb, upb;
14f9c5c9 3058
78134374 3059 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3060 error (_("too many subscripts (%d expected)"), k);
27710edb 3061 arr = value_cast (lookup_pointer_type (type->target_type ()),
cda03344 3062 arr->copy ());
3d967001 3063 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3064 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
27710edb 3065 type = type->target_type ();
14f9c5c9
AS
3066 }
3067
3068 return value_ind (arr);
3069}
3070
0b5d8877 3071/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3072 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3073 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3074 this array is LOW, as per Ada rules. */
0b5d8877 3075static struct value *
f5938064 3076ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3077 int low, int high)
0b5d8877 3078{
b0dd7688 3079 struct type *type0 = ada_check_typedef (type);
27710edb 3080 struct type *base_index_type = type0->index_type ()->target_type ();
e727c536 3081 type_allocator alloc (base_index_type);
0c9c3474 3082 struct type *index_type
e727c536 3083 = create_static_range_type (alloc, base_index_type, low, high);
9fe561ab 3084 struct type *slice_type = create_array_type_with_stride
9e76b17a 3085 (alloc, type0->target_type (), index_type,
24e99c6c 3086 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
3757d2d4 3087 type0->field (0).bitsize ());
3d967001 3088 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 3089 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3090 CORE_ADDR base;
3091
6244c119
SM
3092 low_pos = discrete_position (base_index_type, low);
3093 base_low_pos = discrete_position (base_index_type, base_low);
3094
3095 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3096 {
3097 warning (_("unable to get positions in slice, use bounds instead"));
3098 low_pos = low;
3099 base_low_pos = base_low;
3100 }
5b4ee69b 3101
3757d2d4 3102 ULONGEST stride = slice_type->field (0).bitsize () / 8;
7ff5b937 3103 if (stride == 0)
df86565b 3104 stride = type0->target_type ()->length ();
7ff5b937 3105
6244c119 3106 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3107 return value_at_lazy (slice_type, base);
0b5d8877
PH
3108}
3109
3110
3111static struct value *
3112ada_value_slice (struct value *array, int low, int high)
3113{
d0c97917 3114 struct type *type = ada_check_typedef (array->type ());
27710edb 3115 struct type *base_index_type = type->index_type ()->target_type ();
e727c536 3116 type_allocator alloc (type->index_type ());
0c9c3474 3117 struct type *index_type
e727c536 3118 = create_static_range_type (alloc, type->index_type (), low, high);
9fe561ab 3119 struct type *slice_type = create_array_type_with_stride
9e76b17a 3120 (alloc, type->target_type (), index_type,
24e99c6c 3121 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
3757d2d4 3122 type->field (0).bitsize ());
6244c119
SM
3123 gdb::optional<LONGEST> low_pos, high_pos;
3124
5b4ee69b 3125
6244c119
SM
3126 low_pos = discrete_position (base_index_type, low);
3127 high_pos = discrete_position (base_index_type, high);
3128
3129 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3130 {
3131 warning (_("unable to get positions in slice, use bounds instead"));
3132 low_pos = low;
3133 high_pos = high;
3134 }
3135
3136 return value_cast (slice_type,
6244c119 3137 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3138}
3139
14f9c5c9
AS
3140/* If type is a record type in the form of a standard GNAT array
3141 descriptor, returns the number of dimensions for type. If arr is a
3142 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3143 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3144
3145int
d2e4a39e 3146ada_array_arity (struct type *type)
14f9c5c9
AS
3147{
3148 int arity;
3149
3150 if (type == NULL)
3151 return 0;
3152
3153 type = desc_base_type (type);
3154
3155 arity = 0;
78134374 3156 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3157 return desc_arity (desc_bounds_type (type));
d2e4a39e 3158 else
78134374 3159 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3160 {
dda83cd7 3161 arity += 1;
27710edb 3162 type = ada_check_typedef (type->target_type ());
14f9c5c9 3163 }
d2e4a39e 3164
14f9c5c9
AS
3165 return arity;
3166}
3167
3168/* If TYPE is a record type in the form of a standard GNAT array
3169 descriptor or a simple array type, returns the element type for
3170 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3171 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3172
d2e4a39e
AS
3173struct type *
3174ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3175{
3176 type = desc_base_type (type);
3177
78134374 3178 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3179 {
3180 int k;
d2e4a39e 3181 struct type *p_array_type;
14f9c5c9 3182
556bdfd4 3183 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3184
3185 k = ada_array_arity (type);
3186 if (k == 0)
dda83cd7 3187 return NULL;
d2e4a39e 3188
4c4b4cd2 3189 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3190 if (nindices >= 0 && k > nindices)
dda83cd7 3191 k = nindices;
d2e4a39e 3192 while (k > 0 && p_array_type != NULL)
dda83cd7 3193 {
27710edb 3194 p_array_type = ada_check_typedef (p_array_type->target_type ());
dda83cd7
SM
3195 k -= 1;
3196 }
14f9c5c9
AS
3197 return p_array_type;
3198 }
78134374 3199 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3200 {
78134374 3201 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7 3202 {
27710edb 3203 type = type->target_type ();
6a40c6e4
TT
3204 /* A multi-dimensional array is represented using a sequence
3205 of array types. If one of these types has a name, then
3206 it is not another dimension of the outer array, but
3207 rather the element type of the outermost array. */
3208 if (type->name () != nullptr)
3209 break;
dda83cd7
SM
3210 nindices -= 1;
3211 }
14f9c5c9
AS
3212 return type;
3213 }
3214
3215 return NULL;
3216}
3217
08a057e6 3218/* See ada-lang.h. */
14f9c5c9 3219
08a057e6 3220struct type *
1eea4ebd 3221ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3222{
4c4b4cd2
PH
3223 struct type *result_type;
3224
14f9c5c9
AS
3225 type = desc_base_type (type);
3226
1eea4ebd
UW
3227 if (n < 0 || n > ada_array_arity (type))
3228 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3229
4c4b4cd2 3230 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3231 {
3232 int i;
3233
3234 for (i = 1; i < n; i += 1)
2869ac4b
TT
3235 {
3236 type = ada_check_typedef (type);
27710edb 3237 type = type->target_type ();
2869ac4b 3238 }
27710edb 3239 result_type = ada_check_typedef (type)->index_type ()->target_type ();
4c4b4cd2 3240 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3241 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3242 perhaps stabsread.c would make more sense. */
78134374 3243 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3244 result_type = NULL;
14f9c5c9 3245 }
d2e4a39e 3246 else
1eea4ebd
UW
3247 {
3248 result_type = desc_index_type (desc_bounds_type (type), n);
3249 if (result_type == NULL)
3250 error (_("attempt to take bound of something that is not an array"));
3251 }
3252
3253 return result_type;
14f9c5c9
AS
3254}
3255
3256/* Given that arr is an array type, returns the lower bound of the
3257 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3258 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3259 array-descriptor type. It works for other arrays with bounds supplied
3260 by run-time quantities other than discriminants. */
14f9c5c9 3261
abb68b3e 3262static LONGEST
fb5e3d5c 3263ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3264{
8a48ac95 3265 struct type *type, *index_type_desc, *index_type;
1ce677a4 3266 int i;
262452ec
JK
3267
3268 gdb_assert (which == 0 || which == 1);
14f9c5c9 3269
ad82864c
JB
3270 if (ada_is_constrained_packed_array_type (arr_type))
3271 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3272
4c4b4cd2 3273 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
66cf9350 3274 return - which;
14f9c5c9 3275
78134374 3276 if (arr_type->code () == TYPE_CODE_PTR)
27710edb 3277 type = arr_type->target_type ();
14f9c5c9
AS
3278 else
3279 type = arr_type;
3280
22c4c60c 3281 if (type->is_fixed_instance ())
bafffb51
JB
3282 {
3283 /* The array has already been fixed, so we do not need to
3284 check the parallel ___XA type again. That encoding has
3285 already been applied, so ignore it now. */
3286 index_type_desc = NULL;
3287 }
3288 else
3289 {
3290 index_type_desc = ada_find_parallel_type (type, "___XA");
3291 ada_fixup_array_indexes_type (index_type_desc);
3292 }
3293
262452ec 3294 if (index_type_desc != NULL)
940da03e 3295 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3296 NULL);
262452ec 3297 else
8a48ac95
JB
3298 {
3299 struct type *elt_type = check_typedef (type);
3300
3301 for (i = 1; i < n; i++)
27710edb 3302 elt_type = check_typedef (elt_type->target_type ());
8a48ac95 3303
3d967001 3304 index_type = elt_type->index_type ();
8a48ac95 3305 }
262452ec 3306
66cf9350
TT
3307 return (which == 0
3308 ? ada_discrete_type_low_bound (index_type)
3309 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3310}
3311
3312/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3313 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3314 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3315 supplied by run-time quantities other than discriminants. */
14f9c5c9 3316
1eea4ebd 3317static LONGEST
4dc81987 3318ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3319{
eb479039
JB
3320 struct type *arr_type;
3321
d0c97917 3322 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3323 arr = value_ind (arr);
463b870d 3324 arr_type = arr->enclosing_type ();
14f9c5c9 3325
ad82864c
JB
3326 if (ada_is_constrained_packed_array_type (arr_type))
3327 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3328 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3329 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3330 else
1eea4ebd 3331 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3332}
3333
3334/* Given that arr is an array value, returns the length of the
3335 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3336 supplied by run-time quantities other than discriminants.
3337 Does not work for arrays indexed by enumeration types with representation
3338 clauses at the moment. */
14f9c5c9 3339
1eea4ebd 3340static LONGEST
d2e4a39e 3341ada_array_length (struct value *arr, int n)
14f9c5c9 3342{
aa715135
JG
3343 struct type *arr_type, *index_type;
3344 int low, high;
eb479039 3345
d0c97917 3346 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3347 arr = value_ind (arr);
463b870d 3348 arr_type = arr->enclosing_type ();
14f9c5c9 3349
ad82864c
JB
3350 if (ada_is_constrained_packed_array_type (arr_type))
3351 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3352
4c4b4cd2 3353 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3354 {
3355 low = ada_array_bound_from_type (arr_type, n, 0);
3356 high = ada_array_bound_from_type (arr_type, n, 1);
3357 }
14f9c5c9 3358 else
aa715135
JG
3359 {
3360 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3361 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3362 }
3363
f168693b 3364 arr_type = check_typedef (arr_type);
7150d33c 3365 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3366 if (index_type != NULL)
3367 {
3368 struct type *base_type;
78134374 3369 if (index_type->code () == TYPE_CODE_RANGE)
27710edb 3370 base_type = index_type->target_type ();
aa715135
JG
3371 else
3372 base_type = index_type;
3373
3374 low = pos_atr (value_from_longest (base_type, low));
3375 high = pos_atr (value_from_longest (base_type, high));
3376 }
3377 return high - low + 1;
4c4b4cd2
PH
3378}
3379
bff8c71f
TT
3380/* An array whose type is that of ARR_TYPE (an array type), with
3381 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3382 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3383
3384static struct value *
bff8c71f 3385empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3386{
b0dd7688 3387 struct type *arr_type0 = ada_check_typedef (arr_type);
e727c536 3388 type_allocator alloc (arr_type0->index_type ()->target_type ());
0c9c3474
SA
3389 struct type *index_type
3390 = create_static_range_type
e727c536 3391 (alloc, arr_type0->index_type ()->target_type (), low,
bff8c71f 3392 high < low ? low - 1 : high);
b0dd7688 3393 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3394
9e76b17a 3395 return value::allocate (create_array_type (alloc, elt_type, index_type));
14f9c5c9 3396}
14f9c5c9 3397\f
d2e4a39e 3398
dda83cd7 3399 /* Name resolution */
14f9c5c9 3400
4c4b4cd2
PH
3401/* The "decoded" name for the user-definable Ada operator corresponding
3402 to OP. */
14f9c5c9 3403
d2e4a39e 3404static const char *
4c4b4cd2 3405ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3406{
3407 int i;
3408
4c4b4cd2 3409 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3410 {
3411 if (ada_opname_table[i].op == op)
dda83cd7 3412 return ada_opname_table[i].decoded;
14f9c5c9 3413 }
323e0a4a 3414 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3415}
3416
de93309a
SM
3417/* Returns true (non-zero) iff decoded name N0 should appear before N1
3418 in a listing of choices during disambiguation (see sort_choices, below).
3419 The idea is that overloadings of a subprogram name from the
3420 same package should sort in their source order. We settle for ordering
3421 such symbols by their trailing number (__N or $N). */
14f9c5c9 3422
de93309a
SM
3423static int
3424encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3425{
de93309a
SM
3426 if (N1 == NULL)
3427 return 0;
3428 else if (N0 == NULL)
3429 return 1;
3430 else
3431 {
3432 int k0, k1;
30b15541 3433
de93309a 3434 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3435 ;
de93309a 3436 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3437 ;
de93309a 3438 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3439 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3440 {
3441 int n0, n1;
3442
3443 n0 = k0;
3444 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3445 n0 -= 1;
3446 n1 = k1;
3447 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3448 n1 -= 1;
3449 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3450 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3451 }
de93309a
SM
3452 return (strcmp (N0, N1) < 0);
3453 }
14f9c5c9
AS
3454}
3455
de93309a
SM
3456/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3457 encoded names. */
14f9c5c9 3458
de93309a
SM
3459static void
3460sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3461{
14f9c5c9 3462 int i;
14f9c5c9 3463
de93309a 3464 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3465 {
de93309a
SM
3466 struct block_symbol sym = syms[i];
3467 int j;
3468
3469 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3470 {
3471 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3472 sym.symbol->linkage_name ()))
3473 break;
3474 syms[j + 1] = syms[j];
3475 }
de93309a
SM
3476 syms[j + 1] = sym;
3477 }
3478}
14f9c5c9 3479
de93309a
SM
3480/* Whether GDB should display formals and return types for functions in the
3481 overloads selection menu. */
3482static bool print_signatures = true;
4c4b4cd2 3483
de93309a
SM
3484/* Print the signature for SYM on STREAM according to the FLAGS options. For
3485 all but functions, the signature is just the name of the symbol. For
3486 functions, this is the name of the function, the list of types for formals
3487 and the return type (if any). */
4c4b4cd2 3488
de93309a
SM
3489static void
3490ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3491 const struct type_print_options *flags)
3492{
5f9c5a63 3493 struct type *type = sym->type ();
14f9c5c9 3494
6cb06a8c 3495 gdb_printf (stream, "%s", sym->print_name ());
de93309a
SM
3496 if (!print_signatures
3497 || type == NULL
78134374 3498 || type->code () != TYPE_CODE_FUNC)
de93309a 3499 return;
4c4b4cd2 3500
1f704f76 3501 if (type->num_fields () > 0)
de93309a
SM
3502 {
3503 int i;
14f9c5c9 3504
6cb06a8c 3505 gdb_printf (stream, " (");
1f704f76 3506 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3507 {
3508 if (i > 0)
6cb06a8c 3509 gdb_printf (stream, "; ");
940da03e 3510 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3511 flags);
3512 }
6cb06a8c 3513 gdb_printf (stream, ")");
de93309a 3514 }
27710edb
SM
3515 if (type->target_type () != NULL
3516 && type->target_type ()->code () != TYPE_CODE_VOID)
de93309a 3517 {
6cb06a8c 3518 gdb_printf (stream, " return ");
27710edb 3519 ada_print_type (type->target_type (), NULL, stream, -1, 0, flags);
de93309a
SM
3520 }
3521}
14f9c5c9 3522
de93309a
SM
3523/* Read and validate a set of numeric choices from the user in the
3524 range 0 .. N_CHOICES-1. Place the results in increasing
3525 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3526
de93309a
SM
3527 The user types choices as a sequence of numbers on one line
3528 separated by blanks, encoding them as follows:
14f9c5c9 3529
de93309a
SM
3530 + A choice of 0 means to cancel the selection, throwing an error.
3531 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3532 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3533
de93309a 3534 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3535
de93309a
SM
3536 ANNOTATION_SUFFIX, if present, is used to annotate the input
3537 prompts (for use with the -f switch). */
14f9c5c9 3538
de93309a
SM
3539static int
3540get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3541 int is_all_choice, const char *annotation_suffix)
de93309a 3542{
992a7040 3543 const char *args;
de93309a
SM
3544 const char *prompt;
3545 int n_chosen;
3546 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3547
de93309a
SM
3548 prompt = getenv ("PS2");
3549 if (prompt == NULL)
3550 prompt = "> ";
4c4b4cd2 3551
f8631e5e
SM
3552 std::string buffer;
3553 args = command_line_input (buffer, 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)
27710edb 3901 ftype = ftype->target_type ();
78134374 3902 if (atype->code () == TYPE_CODE_REF)
27710edb 3903 atype = atype->target_type ();
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;
27710edb 3912 atype = atype->target_type ();
db2534b7 3913 /* This can only happen if the actual argument is 'null'. */
df86565b 3914 if (atype->code () == TYPE_CODE_INT && atype->length () == 0)
db2534b7 3915 return 1;
27710edb 3916 return ada_type_match (ftype->target_type (), 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 ());
d0c97917 3975 struct type *atype = ada_check_typedef (actuals[i]->type ());
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)
27710edb 3998 return_type = get_base_type (func_type->target_type ());
de93309a
SM
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:
27710edb
SM
4090 return (type == type->target_type ()
4091 || numeric_type_p (type->target_type ()));
dda83cd7
SM
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:
27710edb
SM
4112 return (type == type->target_type ()
4113 || integer_type_p (type->target_type ()));
dda83cd7
SM
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
98847c1e
TT
4143/* True iff TYPE is discrete, as defined in the Ada Reference Manual.
4144 This essentially means one of (INT, RANGE, ENUM) -- but note that
4145 "enum" includes character and boolean as well. */
14f9c5c9
AS
4146
4147static int
d2e4a39e 4148discrete_type_p (struct type *type)
14f9c5c9
AS
4149{
4150 if (type == NULL)
4151 return 0;
d2e4a39e
AS
4152 else
4153 {
78134374 4154 switch (type->code ())
dda83cd7
SM
4155 {
4156 case TYPE_CODE_INT:
4157 case TYPE_CODE_RANGE:
4158 case TYPE_CODE_ENUM:
4159 case TYPE_CODE_BOOL:
98847c1e 4160 case TYPE_CODE_CHAR:
dda83cd7
SM
4161 return 1;
4162 default:
4163 return 0;
4164 }
d2e4a39e 4165 }
14f9c5c9
AS
4166}
4167
4c4b4cd2
PH
4168/* Returns non-zero if OP with operands in the vector ARGS could be
4169 a user-defined function. Errs on the side of pre-defined operators
4170 (i.e., result 0). */
14f9c5c9
AS
4171
4172static int
d2e4a39e 4173possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4174{
76a01679 4175 struct type *type0 =
d0c97917 4176 (args[0] == NULL) ? NULL : ada_check_typedef (args[0]->type ());
d2e4a39e 4177 struct type *type1 =
d0c97917 4178 (args[1] == NULL) ? NULL : ada_check_typedef (args[1]->type ());
d2e4a39e 4179
4c4b4cd2
PH
4180 if (type0 == NULL)
4181 return 0;
4182
14f9c5c9
AS
4183 switch (op)
4184 {
4185 default:
4186 return 0;
4187
4188 case BINOP_ADD:
4189 case BINOP_SUB:
4190 case BINOP_MUL:
4191 case BINOP_DIV:
d2e4a39e 4192 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4193
4194 case BINOP_REM:
4195 case BINOP_MOD:
4196 case BINOP_BITWISE_AND:
4197 case BINOP_BITWISE_IOR:
4198 case BINOP_BITWISE_XOR:
d2e4a39e 4199 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4200
4201 case BINOP_EQUAL:
4202 case BINOP_NOTEQUAL:
4203 case BINOP_LESS:
4204 case BINOP_GTR:
4205 case BINOP_LEQ:
4206 case BINOP_GEQ:
d2e4a39e 4207 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4208
4209 case BINOP_CONCAT:
ee90b9ab 4210 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4211
4212 case BINOP_EXP:
d2e4a39e 4213 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4214
4215 case UNOP_NEG:
4216 case UNOP_PLUS:
4217 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4218 case UNOP_ABS:
4219 return (!numeric_type_p (type0));
14f9c5c9
AS
4220
4221 }
4222}
4223\f
dda83cd7 4224 /* Renaming */
14f9c5c9 4225
aeb5907d
JB
4226/* NOTES:
4227
4228 1. In the following, we assume that a renaming type's name may
4229 have an ___XD suffix. It would be nice if this went away at some
4230 point.
4231 2. We handle both the (old) purely type-based representation of
4232 renamings and the (new) variable-based encoding. At some point,
4233 it is devoutly to be hoped that the former goes away
4234 (FIXME: hilfinger-2007-07-09).
4235 3. Subprogram renamings are not implemented, although the XRS
4236 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4237
4238/* If SYM encodes a renaming,
4239
4240 <renaming> renames <renamed entity>,
4241
4242 sets *LEN to the length of the renamed entity's name,
4243 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4244 the string describing the subcomponent selected from the renamed
0963b4bd 4245 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4246 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4247 are undefined). Otherwise, returns a value indicating the category
4248 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4249 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4250 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4251 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4252 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4253 may be NULL, in which case they are not assigned.
4254
4255 [Currently, however, GCC does not generate subprogram renamings.] */
4256
4257enum ada_renaming_category
4258ada_parse_renaming (struct symbol *sym,
4259 const char **renamed_entity, int *len,
4260 const char **renaming_expr)
4261{
4262 enum ada_renaming_category kind;
4263 const char *info;
4264 const char *suffix;
4265
4266 if (sym == NULL)
4267 return ADA_NOT_RENAMING;
66d7f48f 4268 switch (sym->aclass ())
14f9c5c9 4269 {
aeb5907d
JB
4270 default:
4271 return ADA_NOT_RENAMING;
aeb5907d
JB
4272 case LOC_LOCAL:
4273 case LOC_STATIC:
4274 case LOC_COMPUTED:
4275 case LOC_OPTIMIZED_OUT:
987012b8 4276 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4277 if (info == NULL)
4278 return ADA_NOT_RENAMING;
4279 switch (info[5])
4280 {
4281 case '_':
4282 kind = ADA_OBJECT_RENAMING;
4283 info += 6;
4284 break;
4285 case 'E':
4286 kind = ADA_EXCEPTION_RENAMING;
4287 info += 7;
4288 break;
4289 case 'P':
4290 kind = ADA_PACKAGE_RENAMING;
4291 info += 7;
4292 break;
4293 case 'S':
4294 kind = ADA_SUBPROGRAM_RENAMING;
4295 info += 7;
4296 break;
4297 default:
4298 return ADA_NOT_RENAMING;
4299 }
14f9c5c9 4300 }
4c4b4cd2 4301
de93309a
SM
4302 if (renamed_entity != NULL)
4303 *renamed_entity = info;
4304 suffix = strstr (info, "___XE");
4305 if (suffix == NULL || suffix == info)
4306 return ADA_NOT_RENAMING;
4307 if (len != NULL)
4308 *len = strlen (info) - strlen (suffix);
4309 suffix += 5;
4310 if (renaming_expr != NULL)
4311 *renaming_expr = suffix;
4312 return kind;
4313}
4314
4315/* Compute the value of the given RENAMING_SYM, which is expected to
4316 be a symbol encoding a renaming expression. BLOCK is the block
4317 used to evaluate the renaming. */
4318
4319static struct value *
4320ada_read_renaming_var_value (struct symbol *renaming_sym,
4321 const struct block *block)
4322{
4323 const char *sym_name;
4324
987012b8 4325 sym_name = renaming_sym->linkage_name ();
de93309a 4326 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
43048e46 4327 return expr->evaluate ();
de93309a
SM
4328}
4329\f
4330
dda83cd7 4331 /* Evaluation: Function Calls */
de93309a
SM
4332
4333/* Return an lvalue containing the value VAL. This is the identity on
4334 lvalues, and otherwise has the side-effect of allocating memory
4335 in the inferior where a copy of the value contents is copied. */
4336
4337static struct value *
4338ensure_lval (struct value *val)
4339{
736355f2
TT
4340 if (val->lval () == not_lval
4341 || val->lval () == lval_internalvar)
de93309a 4342 {
d0c97917 4343 int len = ada_check_typedef (val->type ())->length ();
de93309a 4344 const CORE_ADDR addr =
dda83cd7 4345 value_as_long (value_allocate_space_in_inferior (len));
de93309a 4346
6f9c9d71 4347 val->set_lval (lval_memory);
9feb2d07 4348 val->set_address (addr);
efaf1ae0 4349 write_memory (addr, val->contents ().data (), len);
de93309a
SM
4350 }
4351
4352 return val;
4353}
4354
4355/* Given ARG, a value of type (pointer or reference to a)*
4356 structure/union, extract the component named NAME from the ultimate
4357 target structure/union and return it as a value with its
4358 appropriate type.
4359
4360 The routine searches for NAME among all members of the structure itself
4361 and (recursively) among all members of any wrapper members
4362 (e.g., '_parent').
4363
4364 If NO_ERR, then simply return NULL in case of error, rather than
4365 calling error. */
4366
4367static struct value *
4368ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4369{
4370 struct type *t, *t1;
4371 struct value *v;
4372 int check_tag;
4373
4374 v = NULL;
d0c97917 4375 t1 = t = ada_check_typedef (arg->type ());
78134374 4376 if (t->code () == TYPE_CODE_REF)
de93309a 4377 {
27710edb 4378 t1 = t->target_type ();
de93309a
SM
4379 if (t1 == NULL)
4380 goto BadValue;
4381 t1 = ada_check_typedef (t1);
78134374 4382 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4383 {
4384 arg = coerce_ref (arg);
4385 t = t1;
4386 }
de93309a
SM
4387 }
4388
78134374 4389 while (t->code () == TYPE_CODE_PTR)
de93309a 4390 {
27710edb 4391 t1 = t->target_type ();
de93309a
SM
4392 if (t1 == NULL)
4393 goto BadValue;
4394 t1 = ada_check_typedef (t1);
78134374 4395 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4396 {
4397 arg = value_ind (arg);
4398 t = t1;
4399 }
de93309a 4400 else
dda83cd7 4401 break;
de93309a 4402 }
aeb5907d 4403
78134374 4404 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4405 goto BadValue;
52ce6436 4406
de93309a
SM
4407 if (t1 == t)
4408 v = ada_search_struct_field (name, arg, 0, t);
4409 else
4410 {
4411 int bit_offset, bit_size, byte_offset;
4412 struct type *field_type;
4413 CORE_ADDR address;
a5ee536b 4414
78134374 4415 if (t->code () == TYPE_CODE_PTR)
9feb2d07 4416 address = ada_value_ind (arg)->address ();
de93309a 4417 else
9feb2d07 4418 address = ada_coerce_ref (arg)->address ();
d2e4a39e 4419
de93309a 4420 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4421 the case where the type is a reference to a tagged type, but
4422 we have to be careful to exclude pointers to tagged types.
4423 The latter should be shown as usual (as a pointer), whereas
4424 a reference should mostly be transparent to the user. */
14f9c5c9 4425
de93309a 4426 if (ada_is_tagged_type (t1, 0)
dda83cd7 4427 || (t1->code () == TYPE_CODE_REF
27710edb 4428 && ada_is_tagged_type (t1->target_type (), 0)))
dda83cd7
SM
4429 {
4430 /* We first try to find the searched field in the current type.
de93309a 4431 If not found then let's look in the fixed type. */
14f9c5c9 4432
dda83cd7 4433 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4434 nullptr, nullptr, nullptr,
4435 nullptr, nullptr))
de93309a
SM
4436 check_tag = 1;
4437 else
4438 check_tag = 0;
dda83cd7 4439 }
de93309a
SM
4440 else
4441 check_tag = 0;
c3e5cd34 4442
de93309a
SM
4443 /* Convert to fixed type in all cases, so that we have proper
4444 offsets to each field in unconstrained record types. */
4445 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4446 address, NULL, check_tag);
4447
24aa1b02
TT
4448 /* Resolve the dynamic type as well. */
4449 arg = value_from_contents_and_address (t1, nullptr, address);
d0c97917 4450 t1 = arg->type ();
24aa1b02 4451
de93309a 4452 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4453 &field_type, &byte_offset, &bit_offset,
4454 &bit_size, NULL))
4455 {
4456 if (bit_size != 0)
4457 {
4458 if (t->code () == TYPE_CODE_REF)
4459 arg = ada_coerce_ref (arg);
4460 else
4461 arg = ada_value_ind (arg);
4462 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4463 bit_offset, bit_size,
4464 field_type);
4465 }
4466 else
4467 v = value_at_lazy (field_type, address + byte_offset);
4468 }
c3e5cd34 4469 }
14f9c5c9 4470
de93309a
SM
4471 if (v != NULL || no_err)
4472 return v;
4473 else
4474 error (_("There is no member named %s."), name);
4475
4476 BadValue:
4477 if (no_err)
4478 return NULL;
4479 else
4480 error (_("Attempt to extract a component of "
4481 "a value that is not a record."));
14f9c5c9
AS
4482}
4483
4484/* Return the value ACTUAL, converted to be an appropriate value for a
4485 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4486 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4487 values not residing in memory, updating it as needed. */
14f9c5c9 4488
a93c0eb6 4489struct value *
40bc484c 4490ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4491{
d0c97917 4492 struct type *actual_type = ada_check_typedef (actual->type ());
61ee279c 4493 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4494 struct type *formal_target =
78134374 4495 formal_type->code () == TYPE_CODE_PTR
27710edb 4496 ? ada_check_typedef (formal_type->target_type ()) : formal_type;
d2e4a39e 4497 struct type *actual_target =
78134374 4498 actual_type->code () == TYPE_CODE_PTR
27710edb 4499 ? ada_check_typedef (actual_type->target_type ()) : actual_type;
14f9c5c9 4500
4c4b4cd2 4501 if (ada_is_array_descriptor_type (formal_target)
78134374 4502 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4503 return make_array_descriptor (formal_type, actual);
78134374
SM
4504 else if (formal_type->code () == TYPE_CODE_PTR
4505 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4506 {
a84a8a0d 4507 struct value *result;
5b4ee69b 4508
78134374 4509 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4510 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4511 result = desc_data (actual);
78134374 4512 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7 4513 {
736355f2 4514 if (actual->lval () != lval_memory)
dda83cd7
SM
4515 {
4516 struct value *val;
4517
d0c97917 4518 actual_type = ada_check_typedef (actual->type ());
317c3ed9 4519 val = value::allocate (actual_type);
efaf1ae0 4520 copy (actual->contents (), val->contents_raw ());
dda83cd7
SM
4521 actual = ensure_lval (val);
4522 }
4523 result = value_addr (actual);
4524 }
a84a8a0d
JB
4525 else
4526 return actual;
b1af9e97 4527 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4528 }
78134374 4529 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4530 return ada_value_ind (actual);
8344af1e
JB
4531 else if (ada_is_aligner_type (formal_type))
4532 {
4533 /* We need to turn this parameter into an aligner type
4534 as well. */
317c3ed9 4535 struct value *aligner = value::allocate (formal_type);
8344af1e
JB
4536 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4537
4538 value_assign_to_component (aligner, component, actual);
4539 return aligner;
4540 }
14f9c5c9
AS
4541
4542 return actual;
4543}
4544
438c98a1
JB
4545/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4546 type TYPE. This is usually an inefficient no-op except on some targets
4547 (such as AVR) where the representation of a pointer and an address
4548 differs. */
4549
4550static CORE_ADDR
4551value_pointer (struct value *value, struct type *type)
4552{
df86565b 4553 unsigned len = type->length ();
224c3ddb 4554 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4555 CORE_ADDR addr;
4556
9feb2d07 4557 addr = value->address ();
8ee511af 4558 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4559 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4560 return addr;
4561}
4562
14f9c5c9 4563
4c4b4cd2
PH
4564/* Push a descriptor of type TYPE for array value ARR on the stack at
4565 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4566 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4567 to-descriptor type rather than a descriptor type), a struct value *
4568 representing a pointer to this descriptor. */
14f9c5c9 4569
d2e4a39e 4570static struct value *
40bc484c 4571make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4572{
d2e4a39e
AS
4573 struct type *bounds_type = desc_bounds_type (type);
4574 struct type *desc_type = desc_base_type (type);
317c3ed9
TT
4575 struct value *descriptor = value::allocate (desc_type);
4576 struct value *bounds = value::allocate (bounds_type);
14f9c5c9 4577 int i;
d2e4a39e 4578
d0c97917 4579 for (i = ada_array_arity (ada_check_typedef (arr->type ()));
0963b4bd 4580 i > 0; i -= 1)
14f9c5c9 4581 {
d0c97917 4582 modify_field (bounds->type (),
bbe912ba 4583 bounds->contents_writeable ().data (),
19f220c3
JK
4584 ada_array_bound (arr, i, 0),
4585 desc_bound_bitpos (bounds_type, i, 0),
4586 desc_bound_bitsize (bounds_type, i, 0));
d0c97917 4587 modify_field (bounds->type (),
bbe912ba 4588 bounds->contents_writeable ().data (),
19f220c3
JK
4589 ada_array_bound (arr, i, 1),
4590 desc_bound_bitpos (bounds_type, i, 1),
4591 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4592 }
d2e4a39e 4593
40bc484c 4594 bounds = ensure_lval (bounds);
d2e4a39e 4595
d0c97917 4596 modify_field (descriptor->type (),
bbe912ba 4597 descriptor->contents_writeable ().data (),
19f220c3 4598 value_pointer (ensure_lval (arr),
940da03e 4599 desc_type->field (0).type ()),
19f220c3
JK
4600 fat_pntr_data_bitpos (desc_type),
4601 fat_pntr_data_bitsize (desc_type));
4602
d0c97917 4603 modify_field (descriptor->type (),
bbe912ba 4604 descriptor->contents_writeable ().data (),
19f220c3 4605 value_pointer (bounds,
940da03e 4606 desc_type->field (1).type ()),
19f220c3
JK
4607 fat_pntr_bounds_bitpos (desc_type),
4608 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4609
40bc484c 4610 descriptor = ensure_lval (descriptor);
14f9c5c9 4611
78134374 4612 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4613 return value_addr (descriptor);
4614 else
4615 return descriptor;
4616}
14f9c5c9 4617\f
dda83cd7 4618 /* Symbol Cache Module */
3d9434b5 4619
3d9434b5 4620/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4621 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4622 on the type of entity being printed, the cache can make it as much
4623 as an order of magnitude faster than without it.
4624
4625 The descriptive type DWARF extension has significantly reduced
4626 the need for this cache, at least when DWARF is being used. However,
4627 even in this case, some expensive name-based symbol searches are still
4628 sometimes necessary - to find an XVZ variable, mostly. */
4629
3d9434b5
JB
4630/* Clear all entries from the symbol cache. */
4631
4632static void
bdcccc56 4633ada_clear_symbol_cache ()
3d9434b5 4634{
9d1c303d 4635 ada_pspace_data_handle.clear (current_program_space);
3d9434b5
JB
4636}
4637
fe978cb0 4638/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4639 Return 1 if found, 0 otherwise.
4640
4641 If an entry was found and SYM is not NULL, set *SYM to the entry's
4642 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4643
96d887e8 4644static int
fe978cb0 4645lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4646 struct symbol **sym, const struct block **block)
96d887e8 4647{
9d1c303d
TT
4648 htab_t tab = get_ada_pspace_data (current_program_space);
4649 cache_entry_search search;
4650 search.name = name;
4651 search.domain = domain;
3d9434b5 4652
9d1c303d
TT
4653 cache_entry *e = (cache_entry *) htab_find_with_hash (tab, &search,
4654 search.hash ());
4655 if (e == nullptr)
3d9434b5 4656 return 0;
9d1c303d
TT
4657 if (sym != nullptr)
4658 *sym = e->sym;
4659 if (block != nullptr)
4660 *block = e->block;
3d9434b5 4661 return 1;
96d887e8
PH
4662}
4663
3d9434b5 4664/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4665 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4666
96d887e8 4667static void
fe978cb0 4668cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4669 const struct block *block)
96d887e8 4670{
1994afbf
DE
4671 /* Symbols for builtin types don't have a block.
4672 For now don't cache such symbols. */
7b3ecc75 4673 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4674 return;
4675
3d9434b5
JB
4676 /* If the symbol is a local symbol, then do not cache it, as a search
4677 for that symbol depends on the context. To determine whether
4678 the symbol is local or not, we check the block where we found it
4679 against the global and static blocks of its associated symtab. */
63d609de
SM
4680 if (sym != nullptr)
4681 {
4682 const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
4683
4684 if (bv.global_block () != block && bv.static_block () != block)
4685 return;
4686 }
3d9434b5 4687
9d1c303d
TT
4688 htab_t tab = get_ada_pspace_data (current_program_space);
4689 cache_entry_search search;
4690 search.name = name;
4691 search.domain = domain;
4692
4693 void **slot = htab_find_slot_with_hash (tab, &search,
4694 search.hash (), INSERT);
4695
4696 cache_entry *e = new cache_entry;
4697 e->name = name;
fe978cb0 4698 e->domain = domain;
9d1c303d 4699 e->sym = sym;
3d9434b5 4700 e->block = block;
9d1c303d
TT
4701
4702 *slot = e;
96d887e8 4703}
4c4b4cd2 4704\f
dda83cd7 4705 /* Symbol Lookup */
4c4b4cd2 4706
b5ec771e
PA
4707/* Return the symbol name match type that should be used used when
4708 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4709
4710 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4711 for Ada lookups. */
c0431670 4712
b5ec771e
PA
4713static symbol_name_match_type
4714name_match_type_from_name (const char *lookup_name)
c0431670 4715{
b5ec771e
PA
4716 return (strstr (lookup_name, "__") == NULL
4717 ? symbol_name_match_type::WILD
4718 : symbol_name_match_type::FULL);
c0431670
JB
4719}
4720
4c4b4cd2
PH
4721/* Return the result of a standard (literal, C-like) lookup of NAME in
4722 given DOMAIN, visible from lexical block BLOCK. */
4723
4724static struct symbol *
4725standard_lookup (const char *name, const struct block *block,
dda83cd7 4726 domain_enum domain)
4c4b4cd2 4727{
acbd605d 4728 /* Initialize it just to avoid a GCC false warning. */
6640a367 4729 struct block_symbol sym = {};
4c4b4cd2 4730
d12307c1
PMR
4731 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4732 return sym.symbol;
a2cd4f14 4733 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4734 cache_symbol (name, domain, sym.symbol, sym.block);
4735 return sym.symbol;
4c4b4cd2
PH
4736}
4737
4738
4739/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4740 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4741 since they contend in overloading in the same way. */
4742static int
d1183b06 4743is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4744{
d1183b06 4745 for (const block_symbol &sym : syms)
5f9c5a63
SM
4746 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4747 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4748 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4749 return 1;
4750
4751 return 0;
4752}
4753
4754/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4755 struct types. Otherwise, they may not. */
14f9c5c9
AS
4756
4757static int
d2e4a39e 4758equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4759{
d2e4a39e 4760 if (type0 == type1)
14f9c5c9 4761 return 1;
d2e4a39e 4762 if (type0 == NULL || type1 == NULL
78134374 4763 || type0->code () != type1->code ())
14f9c5c9 4764 return 0;
78134374
SM
4765 if ((type0->code () == TYPE_CODE_STRUCT
4766 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4767 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4768 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4769 return 1;
d2e4a39e 4770
14f9c5c9
AS
4771 return 0;
4772}
4773
4774/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4775 no more defined than that of SYM1. */
14f9c5c9
AS
4776
4777static int
d2e4a39e 4778lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4779{
4780 if (sym0 == sym1)
4781 return 1;
6c9c307c 4782 if (sym0->domain () != sym1->domain ()
66d7f48f 4783 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4784 return 0;
4785
66d7f48f 4786 switch (sym0->aclass ())
14f9c5c9
AS
4787 {
4788 case LOC_UNDEF:
4789 return 1;
4790 case LOC_TYPEDEF:
4791 {
5f9c5a63
SM
4792 struct type *type0 = sym0->type ();
4793 struct type *type1 = sym1->type ();
dda83cd7
SM
4794 const char *name0 = sym0->linkage_name ();
4795 const char *name1 = sym1->linkage_name ();
4796 int len0 = strlen (name0);
4797
4798 return
4799 type0->code () == type1->code ()
4800 && (equiv_types (type0, type1)
4801 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4802 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4803 }
4804 case LOC_CONST:
4aeddc50 4805 return sym0->value_longest () == sym1->value_longest ()
5f9c5a63 4806 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4807
4808 case LOC_STATIC:
4809 {
dda83cd7
SM
4810 const char *name0 = sym0->linkage_name ();
4811 const char *name1 = sym1->linkage_name ();
4812 return (strcmp (name0, name1) == 0
4aeddc50 4813 && sym0->value_address () == sym1->value_address ());
4b610737
TT
4814 }
4815
d2e4a39e
AS
4816 default:
4817 return 0;
14f9c5c9
AS
4818 }
4819}
4820
d1183b06
TT
4821/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4822 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4823
4824static void
d1183b06 4825add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4826 struct symbol *sym,
4827 const struct block *block)
14f9c5c9 4828{
529cad9c
PH
4829 /* Do not try to complete stub types, as the debugger is probably
4830 already scanning all symbols matching a certain name at the
4831 time when this function is called. Trying to replace the stub
4832 type by its associated full type will cause us to restart a scan
4833 which may lead to an infinite recursion. Instead, the client
4834 collecting the matching symbols will end up collecting several
4835 matches, with at least one of them complete. It can then filter
4836 out the stub ones if needed. */
4837
d1183b06 4838 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4839 {
d1183b06 4840 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4841 return;
d1183b06 4842 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4843 {
d1183b06
TT
4844 result[i].symbol = sym;
4845 result[i].block = block;
dda83cd7
SM
4846 return;
4847 }
4c4b4cd2
PH
4848 }
4849
d1183b06
TT
4850 struct block_symbol info;
4851 info.symbol = sym;
4852 info.block = block;
4853 result.push_back (info);
4c4b4cd2
PH
4854}
4855
7c7b6655
TT
4856/* Return a bound minimal symbol matching NAME according to Ada
4857 decoding rules. Returns an invalid symbol if there is no such
4858 minimal symbol. Names prefixed with "standard__" are handled
4859 specially: "standard__" is first stripped off, and only static and
4860 global symbols are searched. */
4c4b4cd2 4861
7c7b6655 4862struct bound_minimal_symbol
06a670e2 4863ada_lookup_simple_minsym (const char *name, struct objfile *objfile)
4c4b4cd2 4864{
7c7b6655 4865 struct bound_minimal_symbol result;
4c4b4cd2 4866
b5ec771e
PA
4867 symbol_name_match_type match_type = name_match_type_from_name (name);
4868 lookup_name_info lookup_name (name, match_type);
4869
4870 symbol_name_matcher_ftype *match_name
4871 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4872
06a670e2
MM
4873 gdbarch_iterate_over_objfiles_in_search_order
4874 (objfile != NULL ? objfile->arch () : target_gdbarch (),
4875 [&result, lookup_name, match_name] (struct objfile *obj)
4876 {
4877 for (minimal_symbol *msymbol : obj->msymbols ())
4878 {
4879 if (match_name (msymbol->linkage_name (), lookup_name, nullptr)
4880 && msymbol->type () != mst_solib_trampoline)
4881 {
4882 result.minsym = msymbol;
4883 result.objfile = obj;
4884 return 1;
4885 }
4886 }
4887
4888 return 0;
4889 }, objfile);
4c4b4cd2 4890
7c7b6655 4891 return result;
96d887e8 4892}
4c4b4cd2 4893
96d887e8
PH
4894/* True if TYPE is definitely an artificial type supplied to a symbol
4895 for which no debugging information was given in the symbol file. */
14f9c5c9 4896
96d887e8
PH
4897static int
4898is_nondebugging_type (struct type *type)
4899{
0d5cff50 4900 const char *name = ada_type_name (type);
5b4ee69b 4901
96d887e8
PH
4902 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4903}
4c4b4cd2 4904
8f17729f
JB
4905/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4906 that are deemed "identical" for practical purposes.
4907
4908 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4909 types and that their number of enumerals is identical (in other
1f704f76 4910 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4911
4912static int
4913ada_identical_enum_types_p (struct type *type1, struct type *type2)
4914{
4915 int i;
4916
4917 /* The heuristic we use here is fairly conservative. We consider
4918 that 2 enumerate types are identical if they have the same
4919 number of enumerals and that all enumerals have the same
4920 underlying value and name. */
4921
4922 /* All enums in the type should have an identical underlying value. */
1f704f76 4923 for (i = 0; i < type1->num_fields (); i++)
970db518 4924 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4925 return 0;
4926
4927 /* All enumerals should also have the same name (modulo any numerical
4928 suffix). */
1f704f76 4929 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4930 {
33d16dd9
SM
4931 const char *name_1 = type1->field (i).name ();
4932 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4933 int len_1 = strlen (name_1);
4934 int len_2 = strlen (name_2);
4935
33d16dd9
SM
4936 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4937 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4938 if (len_1 != len_2
33d16dd9
SM
4939 || strncmp (type1->field (i).name (),
4940 type2->field (i).name (),
8f17729f
JB
4941 len_1) != 0)
4942 return 0;
4943 }
4944
4945 return 1;
4946}
4947
4948/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4949 that are deemed "identical" for practical purposes. Sometimes,
4950 enumerals are not strictly identical, but their types are so similar
4951 that they can be considered identical.
4952
4953 For instance, consider the following code:
4954
4955 type Color is (Black, Red, Green, Blue, White);
4956 type RGB_Color is new Color range Red .. Blue;
4957
4958 Type RGB_Color is a subrange of an implicit type which is a copy
4959 of type Color. If we call that implicit type RGB_ColorB ("B" is
4960 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4961 As a result, when an expression references any of the enumeral
4962 by name (Eg. "print green"), the expression is technically
4963 ambiguous and the user should be asked to disambiguate. But
4964 doing so would only hinder the user, since it wouldn't matter
4965 what choice he makes, the outcome would always be the same.
4966 So, for practical purposes, we consider them as the same. */
4967
4968static int
54d343a2 4969symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4970{
4971 int i;
4972
4973 /* Before performing a thorough comparison check of each type,
4974 we perform a series of inexpensive checks. We expect that these
4975 checks will quickly fail in the vast majority of cases, and thus
4976 help prevent the unnecessary use of a more expensive comparison.
4977 Said comparison also expects us to make some of these checks
4978 (see ada_identical_enum_types_p). */
4979
4980 /* Quick check: All symbols should have an enum type. */
54d343a2 4981 for (i = 0; i < syms.size (); i++)
5f9c5a63 4982 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
4983 return 0;
4984
4985 /* Quick check: They should all have the same value. */
54d343a2 4986 for (i = 1; i < syms.size (); i++)
4aeddc50 4987 if (syms[i].symbol->value_longest () != syms[0].symbol->value_longest ())
8f17729f
JB
4988 return 0;
4989
4990 /* Quick check: They should all have the same number of enumerals. */
54d343a2 4991 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
4992 if (syms[i].symbol->type ()->num_fields ()
4993 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
4994 return 0;
4995
4996 /* All the sanity checks passed, so we might have a set of
4997 identical enumeration types. Perform a more complete
4998 comparison of the type of each symbol. */
54d343a2 4999 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5000 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5001 syms[0].symbol->type ()))
8f17729f
JB
5002 return 0;
5003
5004 return 1;
5005}
5006
54d343a2 5007/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5008 duplicate other symbols in the list (The only case I know of where
5009 this happens is when object files containing stabs-in-ecoff are
5010 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5011 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5012
d1183b06 5013static void
ff4631e2 5014remove_extra_symbols (std::vector<struct block_symbol> &syms)
96d887e8
PH
5015{
5016 int i, j;
4c4b4cd2 5017
8f17729f
JB
5018 /* We should never be called with less than 2 symbols, as there
5019 cannot be any extra symbol in that case. But it's easy to
5020 handle, since we have nothing to do in that case. */
ff4631e2 5021 if (syms.size () < 2)
d1183b06 5022 return;
8f17729f 5023
96d887e8 5024 i = 0;
ff4631e2 5025 while (i < syms.size ())
96d887e8 5026 {
44a37a98 5027 bool remove_p = false;
339c13b6
JB
5028
5029 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5030 the get rid of the stub. */
339c13b6 5031
ff4631e2
TT
5032 if (syms[i].symbol->type ()->is_stub ()
5033 && syms[i].symbol->linkage_name () != NULL)
dda83cd7 5034 {
44a37a98 5035 for (j = 0; !remove_p && j < syms.size (); j++)
dda83cd7
SM
5036 {
5037 if (j != i
ff4631e2
TT
5038 && !syms[j].symbol->type ()->is_stub ()
5039 && syms[j].symbol->linkage_name () != NULL
5040 && strcmp (syms[i].symbol->linkage_name (),
5041 syms[j].symbol->linkage_name ()) == 0)
44a37a98 5042 remove_p = true;
dda83cd7
SM
5043 }
5044 }
339c13b6
JB
5045
5046 /* Two symbols with the same name, same class and same address
dda83cd7 5047 should be identical. */
339c13b6 5048
ff4631e2
TT
5049 else if (syms[i].symbol->linkage_name () != NULL
5050 && syms[i].symbol->aclass () == LOC_STATIC
5051 && is_nondebugging_type (syms[i].symbol->type ()))
dda83cd7 5052 {
44a37a98 5053 for (j = 0; !remove_p && j < syms.size (); j += 1)
dda83cd7
SM
5054 {
5055 if (i != j
ff4631e2
TT
5056 && syms[j].symbol->linkage_name () != NULL
5057 && strcmp (syms[i].symbol->linkage_name (),
5058 syms[j].symbol->linkage_name ()) == 0
5059 && (syms[i].symbol->aclass ()
5060 == syms[j].symbol->aclass ())
5061 && syms[i].symbol->value_address ()
5062 == syms[j].symbol->value_address ())
44a37a98 5063 remove_p = true;
dda83cd7
SM
5064 }
5065 }
339c13b6 5066
e9151f7d
TT
5067 /* Two functions with the same block are identical. */
5068
5069 else if (syms[i].symbol->aclass () == LOC_BLOCK)
5070 {
5071 for (j = 0; !remove_p && j < syms.size (); j += 1)
5072 {
5073 if (i != j
5074 && syms[j].symbol->aclass () == LOC_BLOCK
5075 && (syms[i].symbol->value_block ()
5076 == syms[j].symbol->value_block ()))
5077 remove_p = true;
5078 }
5079 }
5080
a35ddb44 5081 if (remove_p)
ff4631e2 5082 syms.erase (syms.begin () + i);
1b788fb6
TT
5083 else
5084 i += 1;
14f9c5c9 5085 }
8f17729f
JB
5086
5087 /* If all the remaining symbols are identical enumerals, then
5088 just keep the first one and discard the rest.
5089
5090 Unlike what we did previously, we do not discard any entry
5091 unless they are ALL identical. This is because the symbol
5092 comparison is not a strict comparison, but rather a practical
5093 comparison. If all symbols are considered identical, then
5094 we can just go ahead and use the first one and discard the rest.
5095 But if we cannot reduce the list to a single element, we have
5096 to ask the user to disambiguate anyways. And if we have to
5097 present a multiple-choice menu, it's less confusing if the list
5098 isn't missing some choices that were identical and yet distinct. */
ff4631e2
TT
5099 if (symbols_are_identical_enums (syms))
5100 syms.resize (1);
14f9c5c9
AS
5101}
5102
96d887e8
PH
5103/* Given a type that corresponds to a renaming entity, use the type name
5104 to extract the scope (package name or function name, fully qualified,
5105 and following the GNAT encoding convention) where this renaming has been
49d83361 5106 defined. */
4c4b4cd2 5107
49d83361 5108static std::string
96d887e8 5109xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5110{
96d887e8 5111 /* The renaming types adhere to the following convention:
0963b4bd 5112 <scope>__<rename>___<XR extension>.
96d887e8
PH
5113 So, to extract the scope, we search for the "___XR" extension,
5114 and then backtrack until we find the first "__". */
76a01679 5115
7d93a1e0 5116 const char *name = renaming_type->name ();
108d56a4
SM
5117 const char *suffix = strstr (name, "___XR");
5118 const char *last;
14f9c5c9 5119
96d887e8
PH
5120 /* Now, backtrack a bit until we find the first "__". Start looking
5121 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5122
96d887e8
PH
5123 for (last = suffix - 3; last > name; last--)
5124 if (last[0] == '_' && last[1] == '_')
5125 break;
76a01679 5126
96d887e8 5127 /* Make a copy of scope and return it. */
49d83361 5128 return std::string (name, last);
4c4b4cd2
PH
5129}
5130
96d887e8 5131/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5132
96d887e8
PH
5133static int
5134is_package_name (const char *name)
4c4b4cd2 5135{
96d887e8
PH
5136 /* Here, We take advantage of the fact that no symbols are generated
5137 for packages, while symbols are generated for each function.
5138 So the condition for NAME represent a package becomes equivalent
5139 to NAME not existing in our list of symbols. There is only one
5140 small complication with library-level functions (see below). */
4c4b4cd2 5141
96d887e8
PH
5142 /* If it is a function that has not been defined at library level,
5143 then we should be able to look it up in the symbols. */
5144 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5145 return 0;
14f9c5c9 5146
96d887e8
PH
5147 /* Library-level function names start with "_ada_". See if function
5148 "_ada_" followed by NAME can be found. */
14f9c5c9 5149
96d887e8 5150 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5151 functions names cannot contain "__" in them. */
96d887e8
PH
5152 if (strstr (name, "__") != NULL)
5153 return 0;
4c4b4cd2 5154
528e1572 5155 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5156
528e1572 5157 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5158}
14f9c5c9 5159
96d887e8 5160/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5161 not visible from FUNCTION_NAME. */
14f9c5c9 5162
96d887e8 5163static int
0d5cff50 5164old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5165{
66d7f48f 5166 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5167 return 0;
5168
5f9c5a63 5169 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5170
96d887e8 5171 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5172 if (is_package_name (scope.c_str ()))
5173 return 0;
14f9c5c9 5174
96d887e8
PH
5175 /* Check that the rename is in the current function scope by checking
5176 that its name starts with SCOPE. */
76a01679 5177
96d887e8
PH
5178 /* If the function name starts with "_ada_", it means that it is
5179 a library-level function. Strip this prefix before doing the
5180 comparison, as the encoding for the renaming does not contain
5181 this prefix. */
61012eef 5182 if (startswith (function_name, "_ada_"))
96d887e8 5183 function_name += 5;
f26caa11 5184
49d83361 5185 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5186}
5187
aeb5907d
JB
5188/* Remove entries from SYMS that corresponds to a renaming entity that
5189 is not visible from the function associated with CURRENT_BLOCK or
5190 that is superfluous due to the presence of more specific renaming
5191 information. Places surviving symbols in the initial entries of
d1183b06
TT
5192 SYMS.
5193
96d887e8 5194 Rationale:
aeb5907d
JB
5195 First, in cases where an object renaming is implemented as a
5196 reference variable, GNAT may produce both the actual reference
5197 variable and the renaming encoding. In this case, we discard the
5198 latter.
5199
5200 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5201 entity. Unfortunately, STABS currently does not support the definition
5202 of types that are local to a given lexical block, so all renamings types
5203 are emitted at library level. As a consequence, if an application
5204 contains two renaming entities using the same name, and a user tries to
5205 print the value of one of these entities, the result of the ada symbol
5206 lookup will also contain the wrong renaming type.
f26caa11 5207
96d887e8
PH
5208 This function partially covers for this limitation by attempting to
5209 remove from the SYMS list renaming symbols that should be visible
5210 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5211 method with the current information available. The implementation
5212 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5213
5214 - When the user tries to print a rename in a function while there
dda83cd7
SM
5215 is another rename entity defined in a package: Normally, the
5216 rename in the function has precedence over the rename in the
5217 package, so the latter should be removed from the list. This is
5218 currently not the case.
5219
96d887e8 5220 - This function will incorrectly remove valid renames if
dda83cd7
SM
5221 the CURRENT_BLOCK corresponds to a function which symbol name
5222 has been changed by an "Export" pragma. As a consequence,
5223 the user will be unable to print such rename entities. */
4c4b4cd2 5224
d1183b06 5225static void
54d343a2
TT
5226remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5227 const struct block *current_block)
4c4b4cd2
PH
5228{
5229 struct symbol *current_function;
0d5cff50 5230 const char *current_function_name;
4c4b4cd2 5231 int i;
aeb5907d
JB
5232 int is_new_style_renaming;
5233
5234 /* If there is both a renaming foo___XR... encoded as a variable and
5235 a simple variable foo in the same block, discard the latter.
0963b4bd 5236 First, zero out such symbols, then compress. */
aeb5907d 5237 is_new_style_renaming = 0;
54d343a2 5238 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5239 {
54d343a2
TT
5240 struct symbol *sym = (*syms)[i].symbol;
5241 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5242 const char *name;
5243 const char *suffix;
5244
66d7f48f 5245 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5246 continue;
987012b8 5247 name = sym->linkage_name ();
aeb5907d
JB
5248 suffix = strstr (name, "___XR");
5249
5250 if (suffix != NULL)
5251 {
5252 int name_len = suffix - name;
5253 int j;
5b4ee69b 5254
aeb5907d 5255 is_new_style_renaming = 1;
54d343a2
TT
5256 for (j = 0; j < syms->size (); j += 1)
5257 if (i != j && (*syms)[j].symbol != NULL
987012b8 5258 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5259 name_len) == 0
54d343a2
TT
5260 && block == (*syms)[j].block)
5261 (*syms)[j].symbol = NULL;
aeb5907d
JB
5262 }
5263 }
5264 if (is_new_style_renaming)
5265 {
5266 int j, k;
5267
54d343a2
TT
5268 for (j = k = 0; j < syms->size (); j += 1)
5269 if ((*syms)[j].symbol != NULL)
aeb5907d 5270 {
54d343a2 5271 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5272 k += 1;
5273 }
d1183b06
TT
5274 syms->resize (k);
5275 return;
aeb5907d 5276 }
4c4b4cd2
PH
5277
5278 /* Extract the function name associated to CURRENT_BLOCK.
5279 Abort if unable to do so. */
76a01679 5280
4c4b4cd2 5281 if (current_block == NULL)
d1183b06 5282 return;
76a01679 5283
3c9d0506 5284 current_function = current_block->linkage_function ();
4c4b4cd2 5285 if (current_function == NULL)
d1183b06 5286 return;
4c4b4cd2 5287
987012b8 5288 current_function_name = current_function->linkage_name ();
4c4b4cd2 5289 if (current_function_name == NULL)
d1183b06 5290 return;
4c4b4cd2
PH
5291
5292 /* Check each of the symbols, and remove it from the list if it is
5293 a type corresponding to a renaming that is out of the scope of
5294 the current block. */
5295
5296 i = 0;
54d343a2 5297 while (i < syms->size ())
4c4b4cd2 5298 {
54d343a2 5299 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5300 == ADA_OBJECT_RENAMING
5301 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5302 current_function_name))
5303 syms->erase (syms->begin () + i);
4c4b4cd2 5304 else
dda83cd7 5305 i += 1;
4c4b4cd2 5306 }
4c4b4cd2
PH
5307}
5308
d1183b06 5309/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5310 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5311
cd458349 5312 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5313
5314static void
d1183b06 5315ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5316 const lookup_name_info &lookup_name,
5317 const struct block *block, domain_enum domain)
339c13b6 5318{
339c13b6
JB
5319 while (block != NULL)
5320 {
d1183b06 5321 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5322
ba8694b6
TT
5323 /* If we found a non-function match, assume that's the one. We
5324 only check this when finding a function boundary, so that we
5325 can accumulate all results from intervening blocks first. */
6c00f721 5326 if (block->function () != nullptr && is_nonfunction (result))
dda83cd7 5327 return;
339c13b6 5328
f135fe72 5329 block = block->superblock ();
339c13b6 5330 }
339c13b6
JB
5331}
5332
2315bb2d 5333/* An object of this type is used as the callback argument when
40658b94 5334 calling the map_matching_symbols method. */
ccefe4c4 5335
40658b94 5336struct match_data
ccefe4c4 5337{
1bfa81ac
TT
5338 explicit match_data (std::vector<struct block_symbol> *rp)
5339 : resultp (rp)
5340 {
5341 }
5342 DISABLE_COPY_AND_ASSIGN (match_data);
5343
2315bb2d
TT
5344 bool operator() (struct block_symbol *bsym);
5345
1bfa81ac 5346 struct objfile *objfile = nullptr;
d1183b06 5347 std::vector<struct block_symbol> *resultp;
1bfa81ac 5348 struct symbol *arg_sym = nullptr;
1178743e 5349 bool found_sym = false;
ccefe4c4
TT
5350};
5351
2315bb2d
TT
5352/* A callback for add_nonlocal_symbols that adds symbol, found in
5353 BSYM, to a list of symbols. */
ccefe4c4 5354
2315bb2d
TT
5355bool
5356match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5357{
199b4314
TT
5358 const struct block *block = bsym->block;
5359 struct symbol *sym = bsym->symbol;
5360
40658b94
PH
5361 if (sym == NULL)
5362 {
2315bb2d 5363 if (!found_sym && arg_sym != NULL)
dae58e04 5364 add_defn_to_vec (*resultp, arg_sym, block);
2315bb2d
TT
5365 found_sym = false;
5366 arg_sym = NULL;
40658b94
PH
5367 }
5368 else
5369 {
66d7f48f 5370 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5371 return true;
d9743061 5372 else if (sym->is_argument ())
2315bb2d 5373 arg_sym = sym;
40658b94
PH
5374 else
5375 {
2315bb2d 5376 found_sym = true;
dae58e04 5377 add_defn_to_vec (*resultp, sym, block);
40658b94
PH
5378 }
5379 }
199b4314 5380 return true;
40658b94
PH
5381}
5382
b5ec771e
PA
5383/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5384 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5385 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5386
5387static int
d1183b06 5388ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5389 const struct block *block,
b5ec771e
PA
5390 const lookup_name_info &lookup_name,
5391 domain_enum domain)
22cee43f
PMR
5392{
5393 struct using_direct *renaming;
d1183b06 5394 int defns_mark = result.size ();
22cee43f 5395
b5ec771e
PA
5396 symbol_name_matcher_ftype *name_match
5397 = ada_get_symbol_name_matcher (lookup_name);
5398
3c45e9f9 5399 for (renaming = block->get_using ();
22cee43f
PMR
5400 renaming != NULL;
5401 renaming = renaming->next)
5402 {
5403 const char *r_name;
22cee43f
PMR
5404
5405 /* Avoid infinite recursions: skip this renaming if we are actually
5406 already traversing it.
5407
5408 Currently, symbol lookup in Ada don't use the namespace machinery from
5409 C++/Fortran support: skip namespace imports that use them. */
5410 if (renaming->searched
5411 || (renaming->import_src != NULL
5412 && renaming->import_src[0] != '\0')
5413 || (renaming->import_dest != NULL
5414 && renaming->import_dest[0] != '\0'))
5415 continue;
5416 renaming->searched = 1;
5417
5418 /* TODO: here, we perform another name-based symbol lookup, which can
5419 pull its own multiple overloads. In theory, we should be able to do
5420 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5421 not a simple name. But in order to do this, we would need to enhance
5422 the DWARF reader to associate a symbol to this renaming, instead of a
5423 name. So, for now, we do something simpler: re-use the C++/Fortran
5424 namespace machinery. */
5425 r_name = (renaming->alias != NULL
5426 ? renaming->alias
5427 : renaming->declaration);
b5ec771e
PA
5428 if (name_match (r_name, lookup_name, NULL))
5429 {
5430 lookup_name_info decl_lookup_name (renaming->declaration,
5431 lookup_name.match_type ());
d1183b06 5432 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5433 1, NULL);
5434 }
22cee43f
PMR
5435 renaming->searched = 0;
5436 }
d1183b06 5437 return result.size () != defns_mark;
22cee43f
PMR
5438}
5439
db230ce3
JB
5440/* Implements compare_names, but only applying the comparision using
5441 the given CASING. */
5b4ee69b 5442
40658b94 5443static int
db230ce3
JB
5444compare_names_with_case (const char *string1, const char *string2,
5445 enum case_sensitivity casing)
40658b94
PH
5446{
5447 while (*string1 != '\0' && *string2 != '\0')
5448 {
db230ce3
JB
5449 char c1, c2;
5450
40658b94
PH
5451 if (isspace (*string1) || isspace (*string2))
5452 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5453
5454 if (casing == case_sensitive_off)
5455 {
5456 c1 = tolower (*string1);
5457 c2 = tolower (*string2);
5458 }
5459 else
5460 {
5461 c1 = *string1;
5462 c2 = *string2;
5463 }
5464 if (c1 != c2)
40658b94 5465 break;
db230ce3 5466
40658b94
PH
5467 string1 += 1;
5468 string2 += 1;
5469 }
db230ce3 5470
40658b94
PH
5471 switch (*string1)
5472 {
5473 case '(':
5474 return strcmp_iw_ordered (string1, string2);
5475 case '_':
5476 if (*string2 == '\0')
5477 {
052874e8 5478 if (is_name_suffix (string1))
40658b94
PH
5479 return 0;
5480 else
1a1d5513 5481 return 1;
40658b94 5482 }
dbb8534f 5483 /* FALLTHROUGH */
40658b94
PH
5484 default:
5485 if (*string2 == '(')
5486 return strcmp_iw_ordered (string1, string2);
5487 else
db230ce3
JB
5488 {
5489 if (casing == case_sensitive_off)
5490 return tolower (*string1) - tolower (*string2);
5491 else
5492 return *string1 - *string2;
5493 }
40658b94 5494 }
ccefe4c4
TT
5495}
5496
db230ce3
JB
5497/* Compare STRING1 to STRING2, with results as for strcmp.
5498 Compatible with strcmp_iw_ordered in that...
5499
5500 strcmp_iw_ordered (STRING1, STRING2) <= 0
5501
5502 ... implies...
5503
5504 compare_names (STRING1, STRING2) <= 0
5505
5506 (they may differ as to what symbols compare equal). */
5507
5508static int
5509compare_names (const char *string1, const char *string2)
5510{
5511 int result;
5512
5513 /* Similar to what strcmp_iw_ordered does, we need to perform
5514 a case-insensitive comparison first, and only resort to
5515 a second, case-sensitive, comparison if the first one was
5516 not sufficient to differentiate the two strings. */
5517
5518 result = compare_names_with_case (string1, string2, case_sensitive_off);
5519 if (result == 0)
5520 result = compare_names_with_case (string1, string2, case_sensitive_on);
5521
5522 return result;
5523}
5524
b5ec771e
PA
5525/* Convenience function to get at the Ada encoded lookup name for
5526 LOOKUP_NAME, as a C string. */
5527
5528static const char *
5529ada_lookup_name (const lookup_name_info &lookup_name)
5530{
5531 return lookup_name.ada ().lookup_name ().c_str ();
5532}
5533
0b7b2c2a
TT
5534/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5535 for OBJFILE, then walk the objfile's symtabs and update the
5536 results. */
5537
5538static void
5539map_matching_symbols (struct objfile *objfile,
5540 const lookup_name_info &lookup_name,
5541 bool is_wild_match,
5542 domain_enum domain,
5543 int global,
5544 match_data &data)
5545{
5546 data.objfile = objfile;
5547 objfile->expand_matching_symbols (lookup_name, domain, global,
5548 is_wild_match ? nullptr : compare_names);
5549
5550 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5551 for (compunit_symtab *symtab : objfile->compunits ())
5552 {
5553 const struct block *block
63d609de 5554 = symtab->blockvector ()->block (block_kind);
0b7b2c2a
TT
5555 if (!iterate_over_symbols_terminated (block, lookup_name,
5556 domain, data))
5557 break;
5558 }
5559}
5560
1bfa81ac 5561/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5562 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5563 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5564 symbols otherwise. */
339c13b6
JB
5565
5566static void
d1183b06 5567add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5568 const lookup_name_info &lookup_name,
5569 domain_enum domain, int global)
339c13b6 5570{
1bfa81ac 5571 struct match_data data (&result);
339c13b6 5572
b5ec771e
PA
5573 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5574
2030c079 5575 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5576 {
0b7b2c2a
TT
5577 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5578 global, data);
22cee43f 5579
b669c953 5580 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5581 {
5582 const struct block *global_block
63d609de 5583 = cu->blockvector ()->global_block ();
22cee43f 5584
d1183b06 5585 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5586 domain))
1178743e 5587 data.found_sym = true;
22cee43f 5588 }
40658b94
PH
5589 }
5590
d1183b06 5591 if (result.empty () && global && !is_wild_match)
40658b94 5592 {
b5ec771e 5593 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5594 std::string bracket_name = std::string ("<_ada_") + name + '>';
5595 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5596
2030c079 5597 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5598 map_matching_symbols (objfile, name1, false, domain, global, data);
5599 }
339c13b6
JB
5600}
5601
b5ec771e
PA
5602/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5603 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5604 returning the number of matches. Add these to RESULT.
4eeaa230 5605
22cee43f
PMR
5606 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5607 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5608 is the one match returned (no other matches in that or
d9680e73 5609 enclosing blocks is returned). If there are any matches in or
22cee43f 5610 surrounding BLOCK, then these alone are returned.
4eeaa230 5611
b5ec771e
PA
5612 Names prefixed with "standard__" are handled specially:
5613 "standard__" is first stripped off (by the lookup_name
5614 constructor), and only static and global symbols are searched.
14f9c5c9 5615
22cee43f
PMR
5616 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5617 to lookup global symbols. */
5618
5619static void
d1183b06 5620ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5621 const struct block *block,
b5ec771e 5622 const lookup_name_info &lookup_name,
22cee43f
PMR
5623 domain_enum domain,
5624 int full_search,
5625 int *made_global_lookup_p)
14f9c5c9
AS
5626{
5627 struct symbol *sym;
14f9c5c9 5628
22cee43f
PMR
5629 if (made_global_lookup_p)
5630 *made_global_lookup_p = 0;
339c13b6
JB
5631
5632 /* Special case: If the user specifies a symbol name inside package
5633 Standard, do a non-wild matching of the symbol name without
5634 the "standard__" prefix. This was primarily introduced in order
5635 to allow the user to specifically access the standard exceptions
5636 using, for instance, Standard.Constraint_Error when Constraint_Error
5637 is ambiguous (due to the user defining its own Constraint_Error
5638 entity inside its program). */
b5ec771e
PA
5639 if (lookup_name.ada ().standard_p ())
5640 block = NULL;
4c4b4cd2 5641
339c13b6 5642 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5643
4eeaa230
DE
5644 if (block != NULL)
5645 {
5646 if (full_search)
d1183b06 5647 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5648 else
5649 {
5650 /* In the !full_search case we're are being called by
4009ee92 5651 iterate_over_symbols, and we don't want to search
4eeaa230 5652 superblocks. */
d1183b06 5653 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5654 }
d1183b06 5655 if (!result.empty () || !full_search)
22cee43f 5656 return;
4eeaa230 5657 }
d2e4a39e 5658
339c13b6
JB
5659 /* No non-global symbols found. Check our cache to see if we have
5660 already performed this search before. If we have, then return
5661 the same result. */
5662
b5ec771e
PA
5663 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5664 domain, &sym, &block))
4c4b4cd2
PH
5665 {
5666 if (sym != NULL)
d1183b06 5667 add_defn_to_vec (result, sym, block);
22cee43f 5668 return;
4c4b4cd2 5669 }
14f9c5c9 5670
22cee43f
PMR
5671 if (made_global_lookup_p)
5672 *made_global_lookup_p = 1;
b1eedac9 5673
339c13b6
JB
5674 /* Search symbols from all global blocks. */
5675
d1183b06 5676 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5677
4c4b4cd2 5678 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5679 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5680
d1183b06
TT
5681 if (result.empty ())
5682 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5683}
5684
b5ec771e 5685/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5686 is non-zero, enclosing scope and in global scopes.
5687
5688 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5689 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5690
5691 When full_search is non-zero, any non-function/non-enumeral
5692 symbol match within the nest of blocks whose innermost member is BLOCK,
5693 is the one match returned (no other matches in that or
5694 enclosing blocks is returned). If there are any matches in or
5695 surrounding BLOCK, then these alone are returned.
5696
5697 Names prefixed with "standard__" are handled specially: "standard__"
5698 is first stripped off, and only static and global symbols are searched. */
5699
d1183b06 5700static std::vector<struct block_symbol>
b5ec771e
PA
5701ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5702 const struct block *block,
22cee43f 5703 domain_enum domain,
22cee43f
PMR
5704 int full_search)
5705{
22cee43f 5706 int syms_from_global_search;
d1183b06 5707 std::vector<struct block_symbol> results;
22cee43f 5708
d1183b06 5709 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5710 domain, full_search, &syms_from_global_search);
14f9c5c9 5711
ff4631e2 5712 remove_extra_symbols (results);
4c4b4cd2 5713
d1183b06 5714 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5715 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5716
d1183b06 5717 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5718 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5719 results[0].symbol, results[0].block);
ec6a20c2 5720
d1183b06
TT
5721 remove_irrelevant_renamings (&results, block);
5722 return results;
14f9c5c9
AS
5723}
5724
b5ec771e 5725/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5726 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5727
4eeaa230
DE
5728 See ada_lookup_symbol_list_worker for further details. */
5729
d1183b06 5730std::vector<struct block_symbol>
b5ec771e 5731ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5732 domain_enum domain)
4eeaa230 5733{
b5ec771e
PA
5734 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5735 lookup_name_info lookup_name (name, name_match_type);
5736
d1183b06 5737 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5738}
5739
4e5c77fe
JB
5740/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5741 to 1, but choosing the first symbol found if there are multiple
5742 choices.
5743
5e2336be
JB
5744 The result is stored in *INFO, which must be non-NULL.
5745 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5746
5747void
5748ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5749 domain_enum domain,
d12307c1 5750 struct block_symbol *info)
14f9c5c9 5751{
b5ec771e
PA
5752 /* Since we already have an encoded name, wrap it in '<>' to force a
5753 verbatim match. Otherwise, if the name happens to not look like
5754 an encoded name (because it doesn't include a "__"),
5755 ada_lookup_name_info would re-encode/fold it again, and that
5756 would e.g., incorrectly lowercase object renaming names like
5757 "R28b" -> "r28b". */
12932e2c 5758 std::string verbatim = add_angle_brackets (name);
b5ec771e 5759
5e2336be 5760 gdb_assert (info != NULL);
65392b3e 5761 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5762}
aeb5907d
JB
5763
5764/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5765 scope and in global scopes, or NULL if none. NAME is folded and
5766 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5767 choosing the first symbol if there are multiple choices. */
4e5c77fe 5768
d12307c1 5769struct block_symbol
aeb5907d 5770ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5771 domain_enum domain)
aeb5907d 5772{
d1183b06
TT
5773 std::vector<struct block_symbol> candidates
5774 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5775
d1183b06 5776 if (candidates.empty ())
54d343a2 5777 return {};
f98fc17b 5778
dae58e04 5779 return candidates[0];
4c4b4cd2 5780}
14f9c5c9 5781
14f9c5c9 5782
4c4b4cd2
PH
5783/* True iff STR is a possible encoded suffix of a normal Ada name
5784 that is to be ignored for matching purposes. Suffixes of parallel
5785 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5786 are given by any of the regular expressions:
4c4b4cd2 5787
babe1480
JB
5788 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5789 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5790 TKB [subprogram suffix for task bodies]
babe1480 5791 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5792 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5793
5794 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5795 match is performed. This sequence is used to differentiate homonyms,
5796 is an optional part of a valid name suffix. */
4c4b4cd2 5797
14f9c5c9 5798static int
d2e4a39e 5799is_name_suffix (const char *str)
14f9c5c9
AS
5800{
5801 int k;
4c4b4cd2
PH
5802 const char *matching;
5803 const int len = strlen (str);
5804
babe1480
JB
5805 /* Skip optional leading __[0-9]+. */
5806
4c4b4cd2
PH
5807 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5808 {
babe1480
JB
5809 str += 3;
5810 while (isdigit (str[0]))
dda83cd7 5811 str += 1;
4c4b4cd2 5812 }
babe1480
JB
5813
5814 /* [.$][0-9]+ */
4c4b4cd2 5815
babe1480 5816 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5817 {
babe1480 5818 matching = str + 1;
4c4b4cd2 5819 while (isdigit (matching[0]))
dda83cd7 5820 matching += 1;
4c4b4cd2 5821 if (matching[0] == '\0')
dda83cd7 5822 return 1;
4c4b4cd2
PH
5823 }
5824
5825 /* ___[0-9]+ */
babe1480 5826
4c4b4cd2
PH
5827 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5828 {
5829 matching = str + 3;
5830 while (isdigit (matching[0]))
dda83cd7 5831 matching += 1;
4c4b4cd2 5832 if (matching[0] == '\0')
dda83cd7 5833 return 1;
4c4b4cd2
PH
5834 }
5835
9ac7f98e
JB
5836 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5837
5838 if (strcmp (str, "TKB") == 0)
5839 return 1;
5840
529cad9c
PH
5841#if 0
5842 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5843 with a N at the end. Unfortunately, the compiler uses the same
5844 convention for other internal types it creates. So treating
529cad9c 5845 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5846 some regressions. For instance, consider the case of an enumerated
5847 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5848 name ends with N.
5849 Having a single character like this as a suffix carrying some
0963b4bd 5850 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5851 to be something like "_N" instead. In the meantime, do not do
5852 the following check. */
5853 /* Protected Object Subprograms */
5854 if (len == 1 && str [0] == 'N')
5855 return 1;
5856#endif
5857
5858 /* _E[0-9]+[bs]$ */
5859 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5860 {
5861 matching = str + 3;
5862 while (isdigit (matching[0]))
dda83cd7 5863 matching += 1;
529cad9c 5864 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5865 && matching [1] == '\0')
5866 return 1;
529cad9c
PH
5867 }
5868
4c4b4cd2
PH
5869 /* ??? We should not modify STR directly, as we are doing below. This
5870 is fine in this case, but may become problematic later if we find
5871 that this alternative did not work, and want to try matching
5872 another one from the begining of STR. Since we modified it, we
5873 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5874 if (str[0] == 'X')
5875 {
5876 str += 1;
d2e4a39e 5877 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5878 {
5879 if (str[0] != 'n' && str[0] != 'b')
5880 return 0;
5881 str += 1;
5882 }
14f9c5c9 5883 }
babe1480 5884
14f9c5c9
AS
5885 if (str[0] == '\000')
5886 return 1;
babe1480 5887
d2e4a39e 5888 if (str[0] == '_')
14f9c5c9
AS
5889 {
5890 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5891 return 0;
d2e4a39e 5892 if (str[2] == '_')
dda83cd7
SM
5893 {
5894 if (strcmp (str + 3, "JM") == 0)
5895 return 1;
5896 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5897 the LJM suffix in favor of the JM one. But we will
5898 still accept LJM as a valid suffix for a reasonable
5899 amount of time, just to allow ourselves to debug programs
5900 compiled using an older version of GNAT. */
5901 if (strcmp (str + 3, "LJM") == 0)
5902 return 1;
5903 if (str[3] != 'X')
5904 return 0;
5905 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5906 || str[4] == 'U' || str[4] == 'P')
5907 return 1;
5908 if (str[4] == 'R' && str[5] != 'T')
5909 return 1;
5910 return 0;
5911 }
4c4b4cd2 5912 if (!isdigit (str[2]))
dda83cd7 5913 return 0;
4c4b4cd2 5914 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5915 if (!isdigit (str[k]) && str[k] != '_')
5916 return 0;
14f9c5c9
AS
5917 return 1;
5918 }
4c4b4cd2 5919 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5920 {
4c4b4cd2 5921 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5922 if (!isdigit (str[k]) && str[k] != '_')
5923 return 0;
14f9c5c9
AS
5924 return 1;
5925 }
5926 return 0;
5927}
d2e4a39e 5928
aeb5907d
JB
5929/* Return non-zero if the string starting at NAME and ending before
5930 NAME_END contains no capital letters. */
529cad9c
PH
5931
5932static int
5933is_valid_name_for_wild_match (const char *name0)
5934{
f945dedf 5935 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5936 int i;
5937
5823c3ef
JB
5938 /* If the decoded name starts with an angle bracket, it means that
5939 NAME0 does not follow the GNAT encoding format. It should then
5940 not be allowed as a possible wild match. */
5941 if (decoded_name[0] == '<')
5942 return 0;
5943
529cad9c
PH
5944 for (i=0; decoded_name[i] != '\0'; i++)
5945 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5946 return 0;
5947
5948 return 1;
5949}
5950
59c8a30b
JB
5951/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5952 character which could start a simple name. Assumes that *NAMEP points
5953 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5954
14f9c5c9 5955static int
59c8a30b 5956advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5957{
73589123 5958 const char *name = *namep;
5b4ee69b 5959
5823c3ef 5960 while (1)
14f9c5c9 5961 {
59c8a30b 5962 char t0, t1;
73589123
PH
5963
5964 t0 = *name;
5965 if (t0 == '_')
5966 {
5967 t1 = name[1];
5968 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5969 {
5970 name += 1;
61012eef 5971 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5972 break;
5973 else
5974 name += 1;
5975 }
aa27d0b3
JB
5976 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5977 || name[2] == target0))
73589123
PH
5978 {
5979 name += 2;
5980 break;
5981 }
86b44259
TT
5982 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5983 {
5984 /* Names like "pkg__B_N__name", where N is a number, are
5985 block-local. We can handle these by simply skipping
5986 the "B_" here. */
5987 name += 4;
5988 }
73589123
PH
5989 else
5990 return 0;
5991 }
5992 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5993 name += 1;
5994 else
5823c3ef 5995 return 0;
73589123
PH
5996 }
5997
5998 *namep = name;
5999 return 1;
6000}
6001
b5ec771e
PA
6002/* Return true iff NAME encodes a name of the form prefix.PATN.
6003 Ignores any informational suffixes of NAME (i.e., for which
6004 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6005 simple name. */
73589123 6006
b5ec771e 6007static bool
73589123
PH
6008wild_match (const char *name, const char *patn)
6009{
22e048c9 6010 const char *p;
73589123
PH
6011 const char *name0 = name;
6012
81eaa506
TT
6013 if (startswith (name, "___ghost_"))
6014 name += 9;
6015
73589123
PH
6016 while (1)
6017 {
6018 const char *match = name;
6019
6020 if (*name == *patn)
6021 {
6022 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6023 if (*p != *name)
6024 break;
6025 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6026 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6027
6028 if (name[-1] == '_')
6029 name -= 1;
6030 }
6031 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6032 return false;
96d887e8 6033 }
96d887e8
PH
6034}
6035
d1183b06 6036/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6037 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6038
6039static void
d1183b06 6040ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6041 const struct block *block,
6042 const lookup_name_info &lookup_name,
6043 domain_enum domain, struct objfile *objfile)
96d887e8 6044{
96d887e8
PH
6045 /* A matching argument symbol, if any. */
6046 struct symbol *arg_sym;
6047 /* Set true when we find a matching non-argument symbol. */
1178743e 6048 bool found_sym;
96d887e8
PH
6049
6050 arg_sym = NULL;
1178743e 6051 found_sym = false;
1c49bb45 6052 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
96d887e8 6053 {
6c9c307c 6054 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
b5ec771e 6055 {
66d7f48f 6056 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6057 {
d9743061 6058 if (sym->is_argument ())
b5ec771e
PA
6059 arg_sym = sym;
6060 else
6061 {
1178743e 6062 found_sym = true;
dae58e04 6063 add_defn_to_vec (result, sym, block);
b5ec771e
PA
6064 }
6065 }
6066 }
96d887e8
PH
6067 }
6068
22cee43f
PMR
6069 /* Handle renamings. */
6070
d1183b06 6071 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6072 found_sym = true;
22cee43f 6073
96d887e8
PH
6074 if (!found_sym && arg_sym != NULL)
6075 {
dae58e04 6076 add_defn_to_vec (result, arg_sym, block);
96d887e8
PH
6077 }
6078
b5ec771e 6079 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6080 {
6081 arg_sym = NULL;
1178743e 6082 found_sym = false;
b5ec771e
PA
6083 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6084 const char *name = ada_lookup_name.c_str ();
6085 size_t name_len = ada_lookup_name.size ();
96d887e8 6086
548a89df 6087 for (struct symbol *sym : block_iterator_range (block))
76a01679 6088 {
dda83cd7 6089 if (symbol_matches_domain (sym->language (),
6c9c307c 6090 sym->domain (), domain))
dda83cd7
SM
6091 {
6092 int cmp;
6093
6094 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6095 if (cmp == 0)
6096 {
6097 cmp = !startswith (sym->linkage_name (), "_ada_");
6098 if (cmp == 0)
6099 cmp = strncmp (name, sym->linkage_name () + 5,
6100 name_len);
6101 }
6102
6103 if (cmp == 0
6104 && is_name_suffix (sym->linkage_name () + name_len + 5))
6105 {
66d7f48f 6106 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6107 {
d9743061 6108 if (sym->is_argument ())
2a2d4dc3
AS
6109 arg_sym = sym;
6110 else
6111 {
1178743e 6112 found_sym = true;
dae58e04 6113 add_defn_to_vec (result, sym, block);
2a2d4dc3
AS
6114 }
6115 }
dda83cd7
SM
6116 }
6117 }
76a01679 6118 }
96d887e8
PH
6119
6120 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6121 They aren't parameters, right? */
96d887e8 6122 if (!found_sym && arg_sym != NULL)
dda83cd7 6123 {
dae58e04 6124 add_defn_to_vec (result, arg_sym, block);
dda83cd7 6125 }
96d887e8
PH
6126 }
6127}
6128\f
41d27058 6129
dda83cd7 6130 /* Symbol Completion */
41d27058 6131
b5ec771e 6132/* See symtab.h. */
41d27058 6133
b5ec771e
PA
6134bool
6135ada_lookup_name_info::matches
6136 (const char *sym_name,
6137 symbol_name_match_type match_type,
a207cff2 6138 completion_match_result *comp_match_res) const
41d27058 6139{
b5ec771e
PA
6140 bool match = false;
6141 const char *text = m_encoded_name.c_str ();
6142 size_t text_len = m_encoded_name.size ();
41d27058
JB
6143
6144 /* First, test against the fully qualified name of the symbol. */
6145
6146 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6147 match = true;
41d27058 6148
f945dedf 6149 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6150 if (match && !m_encoded_p)
41d27058
JB
6151 {
6152 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6153 that iff we are doing a verbatim match, the decoded version
6154 of the symbol name starts with '<'. Otherwise, this symbol name
6155 is not a suitable completion. */
41d27058 6156
f945dedf 6157 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6158 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6159 }
6160
b5ec771e 6161 if (match && !m_verbatim_p)
41d27058
JB
6162 {
6163 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6164 be done is to verify that the potentially matching symbol name
6165 does not include capital letters, because the ada-mode would
6166 not be able to understand these symbol names without the
6167 angle bracket notation. */
41d27058
JB
6168 const char *tmp;
6169
6170 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6171 if (*tmp != '\0')
b5ec771e 6172 match = false;
41d27058
JB
6173 }
6174
6175 /* Second: Try wild matching... */
6176
b5ec771e 6177 if (!match && m_wild_match_p)
41d27058
JB
6178 {
6179 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6180 may represent an unqualified symbol name. We therefore must
6181 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6182 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6183
6184 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6185 match = true;
41d27058
JB
6186 }
6187
b5ec771e 6188 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6189
6190 if (!match)
b5ec771e 6191 return false;
41d27058 6192
a207cff2 6193 if (comp_match_res != NULL)
b5ec771e 6194 {
a207cff2 6195 std::string &match_str = comp_match_res->match.storage ();
41d27058 6196
b5ec771e 6197 if (!m_encoded_p)
a207cff2 6198 match_str = ada_decode (sym_name);
b5ec771e
PA
6199 else
6200 {
6201 if (m_verbatim_p)
6202 match_str = add_angle_brackets (sym_name);
6203 else
6204 match_str = sym_name;
41d27058 6205
b5ec771e 6206 }
a207cff2
PA
6207
6208 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6209 }
6210
b5ec771e 6211 return true;
41d27058
JB
6212}
6213
dda83cd7 6214 /* Field Access */
96d887e8 6215
73fb9985
JB
6216/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6217 for tagged types. */
6218
6219static int
6220ada_is_dispatch_table_ptr_type (struct type *type)
6221{
0d5cff50 6222 const char *name;
73fb9985 6223
78134374 6224 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6225 return 0;
6226
27710edb 6227 name = type->target_type ()->name ();
73fb9985
JB
6228 if (name == NULL)
6229 return 0;
6230
6231 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6232}
6233
ac4a2da4
JG
6234/* Return non-zero if TYPE is an interface tag. */
6235
6236static int
6237ada_is_interface_tag (struct type *type)
6238{
7d93a1e0 6239 const char *name = type->name ();
ac4a2da4
JG
6240
6241 if (name == NULL)
6242 return 0;
6243
6244 return (strcmp (name, "ada__tags__interface_tag") == 0);
6245}
6246
963a6417
PH
6247/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6248 to be invisible to users. */
96d887e8 6249
963a6417
PH
6250int
6251ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6252{
1f704f76 6253 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6254 return 1;
ffde82bf 6255
73fb9985
JB
6256 /* Check the name of that field. */
6257 {
33d16dd9 6258 const char *name = type->field (field_num).name ();
73fb9985
JB
6259
6260 /* Anonymous field names should not be printed.
6261 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6262 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6263 if (name == NULL)
6264 return 1;
6265
ffde82bf
JB
6266 /* Normally, fields whose name start with an underscore ("_")
6267 are fields that have been internally generated by the compiler,
6268 and thus should not be printed. The "_parent" field is special,
6269 however: This is a field internally generated by the compiler
6270 for tagged types, and it contains the components inherited from
6271 the parent type. This field should not be printed as is, but
6272 should not be ignored either. */
61012eef 6273 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985 6274 return 1;
d537777d
TT
6275
6276 /* The compiler doesn't document this, but sometimes it emits
6277 a field whose name starts with a capital letter, like 'V148s'.
6278 These aren't marked as artificial in any way, but we know they
6279 should be ignored. However, wrapper fields should not be
6280 ignored. */
6281 if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
6282 {
6283 /* Wrapper field. */
6284 }
6285 else if (isupper (name[0]))
6286 return 1;
73fb9985
JB
6287 }
6288
ac4a2da4
JG
6289 /* If this is the dispatch table of a tagged type or an interface tag,
6290 then ignore. */
73fb9985 6291 if (ada_is_tagged_type (type, 1)
940da03e
SM
6292 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6293 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6294 return 1;
6295
6296 /* Not a special field, so it should not be ignored. */
6297 return 0;
963a6417 6298}
96d887e8 6299
963a6417 6300/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6301 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6302
963a6417
PH
6303int
6304ada_is_tagged_type (struct type *type, int refok)
6305{
988f6b3d 6306 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6307}
96d887e8 6308
963a6417 6309/* True iff TYPE represents the type of X'Tag */
96d887e8 6310
963a6417
PH
6311int
6312ada_is_tag_type (struct type *type)
6313{
460efde1
JB
6314 type = ada_check_typedef (type);
6315
78134374 6316 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6317 return 0;
6318 else
96d887e8 6319 {
27710edb 6320 const char *name = ada_type_name (type->target_type ());
5b4ee69b 6321
963a6417 6322 return (name != NULL
dda83cd7 6323 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6324 }
96d887e8
PH
6325}
6326
963a6417 6327/* The type of the tag on VAL. */
76a01679 6328
de93309a 6329static struct type *
963a6417 6330ada_tag_type (struct value *val)
96d887e8 6331{
d0c97917 6332 return ada_lookup_struct_elt_type (val->type (), "_tag", 1, 0);
963a6417 6333}
96d887e8 6334
b50d69b5
JG
6335/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6336 retired at Ada 05). */
6337
6338static int
6339is_ada95_tag (struct value *tag)
6340{
6341 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6342}
6343
963a6417 6344/* The value of the tag on VAL. */
96d887e8 6345
de93309a 6346static struct value *
963a6417
PH
6347ada_value_tag (struct value *val)
6348{
03ee6b2e 6349 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6350}
6351
963a6417
PH
6352/* The value of the tag on the object of type TYPE whose contents are
6353 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6354 ADDRESS. */
96d887e8 6355
963a6417 6356static struct value *
10a2c479 6357value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6358 const gdb_byte *valaddr,
dda83cd7 6359 CORE_ADDR address)
96d887e8 6360{
b5385fc0 6361 int tag_byte_offset;
963a6417 6362 struct type *tag_type;
5b4ee69b 6363
4d1795ac
TT
6364 gdb::array_view<const gdb_byte> contents;
6365 if (valaddr != nullptr)
df86565b 6366 contents = gdb::make_array_view (valaddr, type->length ());
4d1795ac
TT
6367 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6368 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6369 NULL, NULL, NULL))
96d887e8 6370 {
fc1a4b47 6371 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6372 ? NULL
6373 : valaddr + tag_byte_offset);
963a6417 6374 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6375
963a6417 6376 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6377 }
963a6417
PH
6378 return NULL;
6379}
96d887e8 6380
963a6417
PH
6381static struct type *
6382type_from_tag (struct value *tag)
6383{
f5272a3b 6384 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6385
963a6417 6386 if (type_name != NULL)
5c4258f4 6387 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6388 return NULL;
6389}
96d887e8 6390
b50d69b5
JG
6391/* Given a value OBJ of a tagged type, return a value of this
6392 type at the base address of the object. The base address, as
6393 defined in Ada.Tags, it is the address of the primary tag of
6394 the object, and therefore where the field values of its full
6395 view can be fetched. */
6396
6397struct value *
6398ada_tag_value_at_base_address (struct value *obj)
6399{
b50d69b5
JG
6400 struct value *val;
6401 LONGEST offset_to_top = 0;
6402 struct type *ptr_type, *obj_type;
6403 struct value *tag;
6404 CORE_ADDR base_address;
6405
d0c97917 6406 obj_type = obj->type ();
b50d69b5 6407
33b5899f 6408 /* It is the responsibility of the caller to deref pointers. */
b50d69b5 6409
78134374 6410 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6411 return obj;
6412
6413 tag = ada_value_tag (obj);
6414 if (!tag)
6415 return obj;
6416
6417 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6418
6419 if (is_ada95_tag (tag))
6420 return obj;
6421
d537777d
TT
6422 struct type *offset_type
6423 = language_lookup_primitive_type (language_def (language_ada),
6424 target_gdbarch(), "storage_offset");
6425 ptr_type = lookup_pointer_type (offset_type);
b50d69b5
JG
6426 val = value_cast (ptr_type, tag);
6427 if (!val)
6428 return obj;
6429
6430 /* It is perfectly possible that an exception be raised while
6431 trying to determine the base address, just like for the tag;
6432 see ada_tag_name for more details. We do not print the error
6433 message for the same reason. */
6434
a70b8144 6435 try
b50d69b5
JG
6436 {
6437 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6438 }
6439
230d2906 6440 catch (const gdb_exception_error &e)
492d29ea
PA
6441 {
6442 return obj;
6443 }
b50d69b5
JG
6444
6445 /* If offset is null, nothing to do. */
6446
6447 if (offset_to_top == 0)
6448 return obj;
6449
6450 /* -1 is a special case in Ada.Tags; however, what should be done
6451 is not quite clear from the documentation. So do nothing for
6452 now. */
6453
6454 if (offset_to_top == -1)
6455 return obj;
6456
d537777d
TT
6457 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6458 top is used. In this situation the offset is stored just after
6459 the tag, in the object itself. */
df86565b 6460 ULONGEST last = (((ULONGEST) 1) << (8 * offset_type->length () - 1)) - 1;
d537777d
TT
6461 if (offset_to_top == last)
6462 {
6463 struct value *tem = value_addr (tag);
6464 tem = value_ptradd (tem, 1);
6465 tem = value_cast (ptr_type, tem);
6466 offset_to_top = value_as_long (value_ind (tem));
6467 }
05527d8c
TV
6468
6469 if (offset_to_top > 0)
d537777d
TT
6470 {
6471 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6472 from the base address. This was however incompatible with
6473 C++ dispatch table: C++ uses a *negative* value to *add*
6474 to the base address. Ada's convention has therefore been
6475 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6476 use the same convention. Here, we support both cases by
6477 checking the sign of OFFSET_TO_TOP. */
6478 offset_to_top = -offset_to_top;
6479 }
08f49010 6480
9feb2d07 6481 base_address = obj->address () + offset_to_top;
b50d69b5
JG
6482 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6483
6484 /* Make sure that we have a proper tag at the new address.
6485 Otherwise, offset_to_top is bogus (which can happen when
6486 the object is not initialized yet). */
6487
6488 if (!tag)
6489 return obj;
6490
6491 obj_type = type_from_tag (tag);
6492
6493 if (!obj_type)
6494 return obj;
6495
6496 return value_from_contents_and_address (obj_type, NULL, base_address);
6497}
6498
1b611343
JB
6499/* Return the "ada__tags__type_specific_data" type. */
6500
6501static struct type *
6502ada_get_tsd_type (struct inferior *inf)
963a6417 6503{
1b611343 6504 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6505
1b611343
JB
6506 if (data->tsd_type == 0)
6507 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6508 return data->tsd_type;
6509}
529cad9c 6510
1b611343
JB
6511/* Return the TSD (type-specific data) associated to the given TAG.
6512 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6513
1b611343 6514 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6515
1b611343
JB
6516static struct value *
6517ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6518{
4c4b4cd2 6519 struct value *val;
1b611343 6520 struct type *type;
5b4ee69b 6521
1b611343
JB
6522 /* First option: The TSD is simply stored as a field of our TAG.
6523 Only older versions of GNAT would use this format, but we have
6524 to test it first, because there are no visible markers for
6525 the current approach except the absence of that field. */
529cad9c 6526
1b611343
JB
6527 val = ada_value_struct_elt (tag, "tsd", 1);
6528 if (val)
6529 return val;
e802dbe0 6530
1b611343
JB
6531 /* Try the second representation for the dispatch table (in which
6532 there is no explicit 'tsd' field in the referent of the tag pointer,
6533 and instead the tsd pointer is stored just before the dispatch
6534 table. */
e802dbe0 6535
1b611343
JB
6536 type = ada_get_tsd_type (current_inferior());
6537 if (type == NULL)
6538 return NULL;
6539 type = lookup_pointer_type (lookup_pointer_type (type));
6540 val = value_cast (type, tag);
6541 if (val == NULL)
6542 return NULL;
6543 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6544}
6545
1b611343
JB
6546/* Given the TSD of a tag (type-specific data), return a string
6547 containing the name of the associated type.
6548
f5272a3b 6549 May return NULL if we are unable to determine the tag name. */
1b611343 6550
f5272a3b 6551static gdb::unique_xmalloc_ptr<char>
1b611343 6552ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6553{
1b611343 6554 struct value *val;
529cad9c 6555
1b611343 6556 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6557 if (val == NULL)
1b611343 6558 return NULL;
66920317
TT
6559 gdb::unique_xmalloc_ptr<char> buffer
6560 = target_read_string (value_as_address (val), INT_MAX);
6561 if (buffer == nullptr)
f5272a3b
TT
6562 return nullptr;
6563
315e4ebb 6564 try
f5272a3b 6565 {
315e4ebb
TT
6566 /* Let this throw an exception on error. If the data is
6567 uninitialized, we'd rather not have the user see a
6568 warning. */
6569 const char *folded = ada_fold_name (buffer.get (), true);
6570 return make_unique_xstrdup (folded);
6571 }
6572 catch (const gdb_exception &)
6573 {
6574 return nullptr;
f5272a3b 6575 }
4c4b4cd2
PH
6576}
6577
6578/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6579 a C string.
6580
6581 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6582 determine the name of that tag. */
4c4b4cd2 6583
f5272a3b 6584gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6585ada_tag_name (struct value *tag)
6586{
f5272a3b 6587 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6588
d0c97917 6589 if (!ada_is_tag_type (tag->type ()))
4c4b4cd2 6590 return NULL;
1b611343
JB
6591
6592 /* It is perfectly possible that an exception be raised while trying
6593 to determine the TAG's name, even under normal circumstances:
6594 The associated variable may be uninitialized or corrupted, for
6595 instance. We do not let any exception propagate past this point.
6596 instead we return NULL.
6597
6598 We also do not print the error message either (which often is very
6599 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6600 the caller print a more meaningful message if necessary. */
a70b8144 6601 try
1b611343
JB
6602 {
6603 struct value *tsd = ada_get_tsd_from_tag (tag);
6604
6605 if (tsd != NULL)
6606 name = ada_tag_name_from_tsd (tsd);
6607 }
230d2906 6608 catch (const gdb_exception_error &e)
492d29ea
PA
6609 {
6610 }
1b611343
JB
6611
6612 return name;
4c4b4cd2
PH
6613}
6614
6615/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6616
d2e4a39e 6617struct type *
ebf56fd3 6618ada_parent_type (struct type *type)
14f9c5c9
AS
6619{
6620 int i;
6621
61ee279c 6622 type = ada_check_typedef (type);
14f9c5c9 6623
78134374 6624 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6625 return NULL;
6626
1f704f76 6627 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6628 if (ada_is_parent_field (type, i))
0c1f74cf 6629 {
dda83cd7 6630 struct type *parent_type = type->field (i).type ();
0c1f74cf 6631
dda83cd7
SM
6632 /* If the _parent field is a pointer, then dereference it. */
6633 if (parent_type->code () == TYPE_CODE_PTR)
27710edb 6634 parent_type = parent_type->target_type ();
dda83cd7
SM
6635 /* If there is a parallel XVS type, get the actual base type. */
6636 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6637
dda83cd7 6638 return ada_check_typedef (parent_type);
0c1f74cf 6639 }
14f9c5c9
AS
6640
6641 return NULL;
6642}
6643
4c4b4cd2
PH
6644/* True iff field number FIELD_NUM of structure type TYPE contains the
6645 parent-type (inherited) fields of a derived type. Assumes TYPE is
6646 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6647
6648int
ebf56fd3 6649ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6650{
33d16dd9 6651 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6652
4c4b4cd2 6653 return (name != NULL
dda83cd7
SM
6654 && (startswith (name, "PARENT")
6655 || startswith (name, "_parent")));
14f9c5c9
AS
6656}
6657
4c4b4cd2 6658/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6659 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6660 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6661 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6662 structures. */
14f9c5c9
AS
6663
6664int
ebf56fd3 6665ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6666{
33d16dd9 6667 const char *name = type->field (field_num).name ();
5b4ee69b 6668
dddc0e16
JB
6669 if (name != NULL && strcmp (name, "RETVAL") == 0)
6670 {
6671 /* This happens in functions with "out" or "in out" parameters
6672 which are passed by copy. For such functions, GNAT describes
6673 the function's return type as being a struct where the return
6674 value is in a field called RETVAL, and where the other "out"
6675 or "in out" parameters are fields of that struct. This is not
6676 a wrapper. */
6677 return 0;
6678 }
6679
d2e4a39e 6680 return (name != NULL
dda83cd7
SM
6681 && (startswith (name, "PARENT")
6682 || strcmp (name, "REP") == 0
6683 || startswith (name, "_parent")
6684 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6685}
6686
4c4b4cd2
PH
6687/* True iff field number FIELD_NUM of structure or union type TYPE
6688 is a variant wrapper. Assumes TYPE is a structure type with at least
6689 FIELD_NUM+1 fields. */
14f9c5c9
AS
6690
6691int
ebf56fd3 6692ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6693{
8ecb59f8
TT
6694 /* Only Ada types are eligible. */
6695 if (!ADA_TYPE_P (type))
6696 return 0;
6697
940da03e 6698 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6699
78134374
SM
6700 return (field_type->code () == TYPE_CODE_UNION
6701 || (is_dynamic_field (type, field_num)
27710edb 6702 && (field_type->target_type ()->code ()
c3e5cd34 6703 == TYPE_CODE_UNION)));
14f9c5c9
AS
6704}
6705
6706/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6707 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6708 returns the type of the controlling discriminant for the variant.
6709 May return NULL if the type could not be found. */
14f9c5c9 6710
d2e4a39e 6711struct type *
ebf56fd3 6712ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6713{
a121b7c1 6714 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6715
988f6b3d 6716 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6717}
6718
4c4b4cd2 6719/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6720 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6721 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6722
de93309a 6723static int
ebf56fd3 6724ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6725{
33d16dd9 6726 const char *name = type->field (field_num).name ();
5b4ee69b 6727
14f9c5c9
AS
6728 return (name != NULL && name[0] == 'O');
6729}
6730
6731/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6732 returns the name of the discriminant controlling the variant.
6733 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6734
a121b7c1 6735const char *
ebf56fd3 6736ada_variant_discrim_name (struct type *type0)
14f9c5c9 6737{
5f9febe0 6738 static std::string result;
d2e4a39e
AS
6739 struct type *type;
6740 const char *name;
6741 const char *discrim_end;
6742 const char *discrim_start;
14f9c5c9 6743
78134374 6744 if (type0->code () == TYPE_CODE_PTR)
27710edb 6745 type = type0->target_type ();
14f9c5c9
AS
6746 else
6747 type = type0;
6748
6749 name = ada_type_name (type);
6750
6751 if (name == NULL || name[0] == '\000')
6752 return "";
6753
6754 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6755 discrim_end -= 1)
6756 {
61012eef 6757 if (startswith (discrim_end, "___XVN"))
dda83cd7 6758 break;
14f9c5c9
AS
6759 }
6760 if (discrim_end == name)
6761 return "";
6762
d2e4a39e 6763 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6764 discrim_start -= 1)
6765 {
d2e4a39e 6766 if (discrim_start == name + 1)
dda83cd7 6767 return "";
76a01679 6768 if ((discrim_start > name + 3
dda83cd7
SM
6769 && startswith (discrim_start - 3, "___"))
6770 || discrim_start[-1] == '.')
6771 break;
14f9c5c9
AS
6772 }
6773
5f9febe0
TT
6774 result = std::string (discrim_start, discrim_end - discrim_start);
6775 return result.c_str ();
14f9c5c9
AS
6776}
6777
4c4b4cd2
PH
6778/* Scan STR for a subtype-encoded number, beginning at position K.
6779 Put the position of the character just past the number scanned in
6780 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6781 Return 1 if there was a valid number at the given position, and 0
6782 otherwise. A "subtype-encoded" number consists of the absolute value
6783 in decimal, followed by the letter 'm' to indicate a negative number.
6784 Assumes 0m does not occur. */
14f9c5c9
AS
6785
6786int
d2e4a39e 6787ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6788{
6789 ULONGEST RU;
6790
d2e4a39e 6791 if (!isdigit (str[k]))
14f9c5c9
AS
6792 return 0;
6793
4c4b4cd2 6794 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6795 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6796 LONGEST. */
14f9c5c9
AS
6797 RU = 0;
6798 while (isdigit (str[k]))
6799 {
d2e4a39e 6800 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6801 k += 1;
6802 }
6803
d2e4a39e 6804 if (str[k] == 'm')
14f9c5c9
AS
6805 {
6806 if (R != NULL)
dda83cd7 6807 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6808 k += 1;
6809 }
6810 else if (R != NULL)
6811 *R = (LONGEST) RU;
6812
4c4b4cd2 6813 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6814 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6815 number representable as a LONGEST (although either would probably work
6816 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6817 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6818
6819 if (new_k != NULL)
6820 *new_k = k;
6821 return 1;
6822}
6823
4c4b4cd2
PH
6824/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6825 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6826 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6827
de93309a 6828static int
ebf56fd3 6829ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6830{
33d16dd9 6831 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6832 int p;
6833
6834 p = 0;
6835 while (1)
6836 {
d2e4a39e 6837 switch (name[p])
dda83cd7
SM
6838 {
6839 case '\0':
6840 return 0;
6841 case 'S':
6842 {
6843 LONGEST W;
6844
6845 if (!ada_scan_number (name, p + 1, &W, &p))
6846 return 0;
6847 if (val == W)
6848 return 1;
6849 break;
6850 }
6851 case 'R':
6852 {
6853 LONGEST L, U;
6854
6855 if (!ada_scan_number (name, p + 1, &L, &p)
6856 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6857 return 0;
6858 if (val >= L && val <= U)
6859 return 1;
6860 break;
6861 }
6862 case 'O':
6863 return 1;
6864 default:
6865 return 0;
6866 }
4c4b4cd2
PH
6867 }
6868}
6869
0963b4bd 6870/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6871
6872/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6873 ARG_TYPE, extract and return the value of one of its (non-static)
6874 fields. FIELDNO says which field. Differs from value_primitive_field
6875 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6876
5eb68a39 6877struct value *
d2e4a39e 6878ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6879 struct type *arg_type)
14f9c5c9 6880{
14f9c5c9
AS
6881 struct type *type;
6882
61ee279c 6883 arg_type = ada_check_typedef (arg_type);
940da03e 6884 type = arg_type->field (fieldno).type ();
14f9c5c9 6885
4504bbde
TT
6886 /* Handle packed fields. It might be that the field is not packed
6887 relative to its containing structure, but the structure itself is
6888 packed; in this case we must take the bit-field path. */
3757d2d4 6889 if (arg_type->field (fieldno).bitsize () != 0 || arg1->bitpos () != 0)
14f9c5c9 6890 {
b610c045 6891 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
3757d2d4 6892 int bit_size = arg_type->field (fieldno).bitsize ();
d2e4a39e 6893
50888e42 6894 return ada_value_primitive_packed_val (arg1,
efaf1ae0 6895 arg1->contents ().data (),
dda83cd7
SM
6896 offset + bit_pos / 8,
6897 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6898 }
6899 else
6c49729e 6900 return arg1->primitive_field (offset, fieldno, arg_type);
14f9c5c9
AS
6901}
6902
52ce6436
PH
6903/* Find field with name NAME in object of type TYPE. If found,
6904 set the following for each argument that is non-null:
6905 - *FIELD_TYPE_P to the field's type;
6906 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6907 an object of that type;
6908 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6909 - *BIT_SIZE_P to its size in bits if the field is packed, and
6910 0 otherwise;
6911 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6912 fields up to but not including the desired field, or by the total
6913 number of fields if not found. A NULL value of NAME never
6914 matches; the function just counts visible fields in this case.
6915
828d5846
XR
6916 Notice that we need to handle when a tagged record hierarchy
6917 has some components with the same name, like in this scenario:
6918
6919 type Top_T is tagged record
dda83cd7
SM
6920 N : Integer := 1;
6921 U : Integer := 974;
6922 A : Integer := 48;
828d5846
XR
6923 end record;
6924
6925 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6926 N : Character := 'a';
6927 C : Integer := 3;
828d5846
XR
6928 end record;
6929
6930 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6931 N : Float := 4.0;
6932 C : Character := '5';
6933 X : Integer := 6;
6934 A : Character := 'J';
828d5846
XR
6935 end record;
6936
6937 Let's say we now have a variable declared and initialized as follow:
6938
6939 TC : Top_A := new Bottom_T;
6940
6941 And then we use this variable to call this function
6942
6943 procedure Assign (Obj: in out Top_T; TV : Integer);
6944
6945 as follow:
6946
6947 Assign (Top_T (B), 12);
6948
6949 Now, we're in the debugger, and we're inside that procedure
6950 then and we want to print the value of obj.c:
6951
6952 Usually, the tagged record or one of the parent type owns the
6953 component to print and there's no issue but in this particular
6954 case, what does it mean to ask for Obj.C? Since the actual
6955 type for object is type Bottom_T, it could mean two things: type
6956 component C from the Middle_T view, but also component C from
6957 Bottom_T. So in that "undefined" case, when the component is
6958 not found in the non-resolved type (which includes all the
6959 components of the parent type), then resolve it and see if we
6960 get better luck once expanded.
6961
6962 In the case of homonyms in the derived tagged type, we don't
6963 guaranty anything, and pick the one that's easiest for us
6964 to program.
6965
0963b4bd 6966 Returns 1 if found, 0 otherwise. */
52ce6436 6967
4c4b4cd2 6968static int
0d5cff50 6969find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6970 struct type **field_type_p,
6971 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6972 int *index_p)
4c4b4cd2
PH
6973{
6974 int i;
828d5846 6975 int parent_offset = -1;
4c4b4cd2 6976
61ee279c 6977 type = ada_check_typedef (type);
76a01679 6978
52ce6436
PH
6979 if (field_type_p != NULL)
6980 *field_type_p = NULL;
6981 if (byte_offset_p != NULL)
d5d6fca5 6982 *byte_offset_p = 0;
52ce6436
PH
6983 if (bit_offset_p != NULL)
6984 *bit_offset_p = 0;
6985 if (bit_size_p != NULL)
6986 *bit_size_p = 0;
6987
1f704f76 6988 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 6989 {
4d1795ac
TT
6990 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6991 type. However, we only need the values to be correct when
6992 the caller asks for them. */
6993 int bit_pos = 0, fld_offset = 0;
6994 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6995 {
b610c045 6996 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
6997 fld_offset = offset + bit_pos / 8;
6998 }
6999
33d16dd9 7000 const char *t_field_name = type->field (i).name ();
76a01679 7001
4c4b4cd2 7002 if (t_field_name == NULL)
dda83cd7 7003 continue;
4c4b4cd2 7004
828d5846 7005 else if (ada_is_parent_field (type, i))
dda83cd7 7006 {
828d5846
XR
7007 /* This is a field pointing us to the parent type of a tagged
7008 type. As hinted in this function's documentation, we give
7009 preference to fields in the current record first, so what
7010 we do here is just record the index of this field before
7011 we skip it. If it turns out we couldn't find our field
7012 in the current record, then we'll get back to it and search
7013 inside it whether the field might exist in the parent. */
7014
dda83cd7
SM
7015 parent_offset = i;
7016 continue;
7017 }
828d5846 7018
52ce6436 7019 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7 7020 {
3757d2d4 7021 int bit_size = type->field (i).bitsize ();
5b4ee69b 7022
52ce6436 7023 if (field_type_p != NULL)
940da03e 7024 *field_type_p = type->field (i).type ();
52ce6436
PH
7025 if (byte_offset_p != NULL)
7026 *byte_offset_p = fld_offset;
7027 if (bit_offset_p != NULL)
7028 *bit_offset_p = bit_pos % 8;
7029 if (bit_size_p != NULL)
7030 *bit_size_p = bit_size;
dda83cd7
SM
7031 return 1;
7032 }
4c4b4cd2 7033 else if (ada_is_wrapper_field (type, i))
dda83cd7 7034 {
940da03e 7035 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7036 field_type_p, byte_offset_p, bit_offset_p,
7037 bit_size_p, index_p))
dda83cd7
SM
7038 return 1;
7039 }
4c4b4cd2 7040 else if (ada_is_variant_part (type, i))
dda83cd7 7041 {
52ce6436
PH
7042 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7043 fixed type?? */
dda83cd7
SM
7044 int j;
7045 struct type *field_type
940da03e 7046 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7047
dda83cd7
SM
7048 for (j = 0; j < field_type->num_fields (); j += 1)
7049 {
7050 if (find_struct_field (name, field_type->field (j).type (),
7051 fld_offset
b610c045 7052 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7053 field_type_p, byte_offset_p,
7054 bit_offset_p, bit_size_p, index_p))
7055 return 1;
7056 }
7057 }
52ce6436
PH
7058 else if (index_p != NULL)
7059 *index_p += 1;
4c4b4cd2 7060 }
828d5846
XR
7061
7062 /* Field not found so far. If this is a tagged type which
7063 has a parent, try finding that field in the parent now. */
7064
7065 if (parent_offset != -1)
7066 {
4d1795ac
TT
7067 /* As above, only compute the offset when truly needed. */
7068 int fld_offset = offset;
7069 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7070 {
b610c045 7071 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7072 fld_offset += bit_pos / 8;
7073 }
828d5846 7074
940da03e 7075 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7076 fld_offset, field_type_p, byte_offset_p,
7077 bit_offset_p, bit_size_p, index_p))
7078 return 1;
828d5846
XR
7079 }
7080
4c4b4cd2
PH
7081 return 0;
7082}
7083
0963b4bd 7084/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7085
52ce6436
PH
7086static int
7087num_visible_fields (struct type *type)
7088{
7089 int n;
5b4ee69b 7090
52ce6436
PH
7091 n = 0;
7092 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7093 return n;
7094}
14f9c5c9 7095
4c4b4cd2 7096/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7097 and search in it assuming it has (class) type TYPE.
7098 If found, return value, else return NULL.
7099
828d5846
XR
7100 Searches recursively through wrapper fields (e.g., '_parent').
7101
7102 In the case of homonyms in the tagged types, please refer to the
7103 long explanation in find_struct_field's function documentation. */
14f9c5c9 7104
4c4b4cd2 7105static struct value *
108d56a4 7106ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7107 struct type *type)
14f9c5c9
AS
7108{
7109 int i;
828d5846 7110 int parent_offset = -1;
14f9c5c9 7111
5b4ee69b 7112 type = ada_check_typedef (type);
1f704f76 7113 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7114 {
33d16dd9 7115 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7116
7117 if (t_field_name == NULL)
dda83cd7 7118 continue;
14f9c5c9 7119
828d5846 7120 else if (ada_is_parent_field (type, i))
dda83cd7 7121 {
828d5846
XR
7122 /* This is a field pointing us to the parent type of a tagged
7123 type. As hinted in this function's documentation, we give
7124 preference to fields in the current record first, so what
7125 we do here is just record the index of this field before
7126 we skip it. If it turns out we couldn't find our field
7127 in the current record, then we'll get back to it and search
7128 inside it whether the field might exist in the parent. */
7129
dda83cd7
SM
7130 parent_offset = i;
7131 continue;
7132 }
828d5846 7133
14f9c5c9 7134 else if (field_name_match (t_field_name, name))
dda83cd7 7135 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7136
7137 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7138 {
7139 struct value *v = /* Do not let indent join lines here. */
7140 ada_search_struct_field (name, arg,
b610c045 7141 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7142 type->field (i).type ());
5b4ee69b 7143
dda83cd7
SM
7144 if (v != NULL)
7145 return v;
7146 }
14f9c5c9
AS
7147
7148 else if (ada_is_variant_part (type, i))
dda83cd7 7149 {
0963b4bd 7150 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7151 int j;
7152 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7153 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7154
dda83cd7
SM
7155 for (j = 0; j < field_type->num_fields (); j += 1)
7156 {
7157 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7158 break. */
dda83cd7 7159 (name, arg,
b610c045 7160 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7161 field_type->field (j).type ());
5b4ee69b 7162
dda83cd7
SM
7163 if (v != NULL)
7164 return v;
7165 }
7166 }
14f9c5c9 7167 }
828d5846
XR
7168
7169 /* Field not found so far. If this is a tagged type which
7170 has a parent, try finding that field in the parent now. */
7171
7172 if (parent_offset != -1)
7173 {
7174 struct value *v = ada_search_struct_field (
b610c045 7175 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7176 type->field (parent_offset).type ());
828d5846
XR
7177
7178 if (v != NULL)
dda83cd7 7179 return v;
828d5846
XR
7180 }
7181
14f9c5c9
AS
7182 return NULL;
7183}
d2e4a39e 7184
52ce6436
PH
7185static struct value *ada_index_struct_field_1 (int *, struct value *,
7186 int, struct type *);
7187
7188
7189/* Return field #INDEX in ARG, where the index is that returned by
7190 * find_struct_field through its INDEX_P argument. Adjust the address
7191 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7192 * If found, return value, else return NULL. */
52ce6436
PH
7193
7194static struct value *
7195ada_index_struct_field (int index, struct value *arg, int offset,
7196 struct type *type)
7197{
7198 return ada_index_struct_field_1 (&index, arg, offset, type);
7199}
7200
7201
7202/* Auxiliary function for ada_index_struct_field. Like
7203 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7204 * *INDEX_P. */
52ce6436
PH
7205
7206static struct value *
7207ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7208 struct type *type)
7209{
7210 int i;
7211 type = ada_check_typedef (type);
7212
1f704f76 7213 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7214 {
33d16dd9 7215 if (type->field (i).name () == NULL)
dda83cd7 7216 continue;
52ce6436 7217 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7218 {
7219 struct value *v = /* Do not let indent join lines here. */
7220 ada_index_struct_field_1 (index_p, arg,
b610c045 7221 offset + type->field (i).loc_bitpos () / 8,
940da03e 7222 type->field (i).type ());
5b4ee69b 7223
dda83cd7
SM
7224 if (v != NULL)
7225 return v;
7226 }
52ce6436
PH
7227
7228 else if (ada_is_variant_part (type, i))
dda83cd7 7229 {
52ce6436 7230 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7231 find_struct_field. */
52ce6436 7232 error (_("Cannot assign this kind of variant record"));
dda83cd7 7233 }
52ce6436 7234 else if (*index_p == 0)
dda83cd7 7235 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7236 else
7237 *index_p -= 1;
7238 }
7239 return NULL;
7240}
7241
3b4de39c 7242/* Return a string representation of type TYPE. */
99bbb428 7243
3b4de39c 7244static std::string
99bbb428
PA
7245type_as_string (struct type *type)
7246{
d7e74731 7247 string_file tmp_stream;
99bbb428 7248
d7e74731 7249 type_print (type, "", &tmp_stream, -1);
99bbb428 7250
5d10a204 7251 return tmp_stream.release ();
99bbb428
PA
7252}
7253
14f9c5c9 7254/* Given a type TYPE, look up the type of the component of type named NAME.
14f9c5c9
AS
7255
7256 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7257 followed by "___".
14f9c5c9 7258
0963b4bd 7259 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7260 be a (pointer or reference)+ to a struct or union, and the
7261 ultimate target type will be searched.
14f9c5c9
AS
7262
7263 Looks recursively into variant clauses and parent types.
7264
828d5846
XR
7265 In the case of homonyms in the tagged types, please refer to the
7266 long explanation in find_struct_field's function documentation.
7267
4c4b4cd2
PH
7268 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7269 TYPE is not a type of the right kind. */
14f9c5c9 7270
4c4b4cd2 7271static struct type *
a121b7c1 7272ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7273 int noerr)
14f9c5c9 7274{
14f9c5c9
AS
7275 if (name == NULL)
7276 goto BadName;
7277
76a01679 7278 if (refok && type != NULL)
4c4b4cd2
PH
7279 while (1)
7280 {
dda83cd7
SM
7281 type = ada_check_typedef (type);
7282 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7283 break;
27710edb 7284 type = type->target_type ();
4c4b4cd2 7285 }
14f9c5c9 7286
76a01679 7287 if (type == NULL
78134374
SM
7288 || (type->code () != TYPE_CODE_STRUCT
7289 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7290 {
4c4b4cd2 7291 if (noerr)
dda83cd7 7292 return NULL;
99bbb428 7293
3b4de39c
PA
7294 error (_("Type %s is not a structure or union type"),
7295 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7296 }
7297
7298 type = to_static_fixed_type (type);
7299
f0874f41
TT
7300 struct type *result;
7301 find_struct_field (name, type, 0, &result, nullptr, nullptr, nullptr,
7302 nullptr);
7303 if (result != nullptr)
7304 return result;
828d5846 7305
14f9c5c9 7306BadName:
d2e4a39e 7307 if (!noerr)
14f9c5c9 7308 {
2b2798cc 7309 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7310
7311 error (_("Type %s has no component named %s"),
3b4de39c 7312 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7313 }
7314
7315 return NULL;
7316}
7317
b1f33ddd
JB
7318/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7319 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7320 represents an unchecked union (that is, the variant part of a
0963b4bd 7321 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7322
7323static int
7324is_unchecked_variant (struct type *var_type, struct type *outer_type)
7325{
a121b7c1 7326 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7327
988f6b3d 7328 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7329}
7330
7331
14f9c5c9 7332/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7333 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7334 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7335
d2e4a39e 7336int
d8af9068 7337ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7338{
7339 int others_clause;
7340 int i;
a121b7c1 7341 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7342 struct value *discrim;
14f9c5c9
AS
7343 LONGEST discrim_val;
7344
012370f6
TT
7345 /* Using plain value_from_contents_and_address here causes problems
7346 because we will end up trying to resolve a type that is currently
7347 being constructed. */
0c281816
JB
7348 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7349 if (discrim == NULL)
14f9c5c9 7350 return -1;
0c281816 7351 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7352
7353 others_clause = -1;
1f704f76 7354 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7355 {
7356 if (ada_is_others_clause (var_type, i))
dda83cd7 7357 others_clause = i;
14f9c5c9 7358 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7359 return i;
14f9c5c9
AS
7360 }
7361
7362 return others_clause;
7363}
d2e4a39e 7364\f
14f9c5c9
AS
7365
7366
dda83cd7 7367 /* Dynamic-Sized Records */
14f9c5c9
AS
7368
7369/* Strategy: The type ostensibly attached to a value with dynamic size
7370 (i.e., a size that is not statically recorded in the debugging
7371 data) does not accurately reflect the size or layout of the value.
7372 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7373 conventional types that are constructed on the fly. */
14f9c5c9
AS
7374
7375/* There is a subtle and tricky problem here. In general, we cannot
7376 determine the size of dynamic records without its data. However,
7377 the 'struct value' data structure, which GDB uses to represent
7378 quantities in the inferior process (the target), requires the size
7379 of the type at the time of its allocation in order to reserve space
7380 for GDB's internal copy of the data. That's why the
7381 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7382 rather than struct value*s.
14f9c5c9
AS
7383
7384 However, GDB's internal history variables ($1, $2, etc.) are
7385 struct value*s containing internal copies of the data that are not, in
7386 general, the same as the data at their corresponding addresses in
7387 the target. Fortunately, the types we give to these values are all
7388 conventional, fixed-size types (as per the strategy described
7389 above), so that we don't usually have to perform the
7390 'to_fixed_xxx_type' conversions to look at their values.
7391 Unfortunately, there is one exception: if one of the internal
7392 history variables is an array whose elements are unconstrained
7393 records, then we will need to create distinct fixed types for each
7394 element selected. */
7395
7396/* The upshot of all of this is that many routines take a (type, host
7397 address, target address) triple as arguments to represent a value.
7398 The host address, if non-null, is supposed to contain an internal
7399 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7400 target at the target address. */
14f9c5c9
AS
7401
7402/* Assuming that VAL0 represents a pointer value, the result of
7403 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7404 dynamic-sized types. */
14f9c5c9 7405
d2e4a39e
AS
7406struct value *
7407ada_value_ind (struct value *val0)
14f9c5c9 7408{
c48db5ca 7409 struct value *val = value_ind (val0);
5b4ee69b 7410
d0c97917 7411 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7412 val = ada_tag_value_at_base_address (val);
7413
4c4b4cd2 7414 return ada_to_fixed_value (val);
14f9c5c9
AS
7415}
7416
7417/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7418 qualifiers on VAL0. */
7419
d2e4a39e
AS
7420static struct value *
7421ada_coerce_ref (struct value *val0)
7422{
d0c97917 7423 if (val0->type ()->code () == TYPE_CODE_REF)
d2e4a39e
AS
7424 {
7425 struct value *val = val0;
5b4ee69b 7426
994b9211 7427 val = coerce_ref (val);
b50d69b5 7428
d0c97917 7429 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7430 val = ada_tag_value_at_base_address (val);
7431
4c4b4cd2 7432 return ada_to_fixed_value (val);
d2e4a39e
AS
7433 }
7434 else
14f9c5c9
AS
7435 return val0;
7436}
7437
4c4b4cd2 7438/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7439
7440static unsigned int
ebf56fd3 7441field_alignment (struct type *type, int f)
14f9c5c9 7442{
33d16dd9 7443 const char *name = type->field (f).name ();
64a1bf19 7444 int len;
14f9c5c9
AS
7445 int align_offset;
7446
64a1bf19
JB
7447 /* The field name should never be null, unless the debugging information
7448 is somehow malformed. In this case, we assume the field does not
7449 require any alignment. */
7450 if (name == NULL)
7451 return 1;
7452
7453 len = strlen (name);
7454
4c4b4cd2
PH
7455 if (!isdigit (name[len - 1]))
7456 return 1;
14f9c5c9 7457
d2e4a39e 7458 if (isdigit (name[len - 2]))
14f9c5c9
AS
7459 align_offset = len - 2;
7460 else
7461 align_offset = len - 1;
7462
61012eef 7463 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7464 return TARGET_CHAR_BIT;
7465
4c4b4cd2
PH
7466 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7467}
7468
852dff6c 7469/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7470
852dff6c
JB
7471static struct symbol *
7472ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7473{
7474 struct symbol *sym;
7475
7476 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
66d7f48f 7477 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7478 return sym;
7479
4186eb54
KS
7480 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7481 return sym;
14f9c5c9
AS
7482}
7483
dddfab26
UW
7484/* Find a type named NAME. Ignores ambiguity. This routine will look
7485 solely for types defined by debug info, it will not search the GDB
7486 primitive types. */
4c4b4cd2 7487
852dff6c 7488static struct type *
ebf56fd3 7489ada_find_any_type (const char *name)
14f9c5c9 7490{
852dff6c 7491 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7492
14f9c5c9 7493 if (sym != NULL)
5f9c5a63 7494 return sym->type ();
14f9c5c9 7495
dddfab26 7496 return NULL;
14f9c5c9
AS
7497}
7498
739593e0
JB
7499/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7500 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7501 symbol, in which case it is returned. Otherwise, this looks for
7502 symbols whose name is that of NAME_SYM suffixed with "___XR".
7503 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7504
c0e70c62
TT
7505static bool
7506ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7507{
987012b8 7508 const char *name = name_sym->linkage_name ();
c0e70c62 7509 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7510}
7511
14f9c5c9 7512/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7513 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7514 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7515 otherwise return 0. */
7516
14f9c5c9 7517int
d2e4a39e 7518ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7519{
7520 if (type1 == NULL)
7521 return 1;
7522 else if (type0 == NULL)
7523 return 0;
78134374 7524 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7525 return 1;
78134374 7526 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7527 return 0;
7d93a1e0 7528 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7529 return 1;
ad82864c 7530 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7531 return 1;
4c4b4cd2 7532 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7533 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7534 return 1;
aeb5907d
JB
7535 else
7536 {
7d93a1e0
SM
7537 const char *type0_name = type0->name ();
7538 const char *type1_name = type1->name ();
aeb5907d
JB
7539
7540 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7541 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7542 return 1;
7543 }
14f9c5c9
AS
7544 return 0;
7545}
7546
e86ca25f
TT
7547/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7548 null. */
4c4b4cd2 7549
0d5cff50 7550const char *
d2e4a39e 7551ada_type_name (struct type *type)
14f9c5c9 7552{
d2e4a39e 7553 if (type == NULL)
14f9c5c9 7554 return NULL;
7d93a1e0 7555 return type->name ();
14f9c5c9
AS
7556}
7557
b4ba55a1
JB
7558/* Search the list of "descriptive" types associated to TYPE for a type
7559 whose name is NAME. */
7560
7561static struct type *
7562find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7563{
931e5bc3 7564 struct type *result, *tmp;
b4ba55a1 7565
c6044dd1
JB
7566 if (ada_ignore_descriptive_types_p)
7567 return NULL;
7568
b4ba55a1
JB
7569 /* If there no descriptive-type info, then there is no parallel type
7570 to be found. */
7571 if (!HAVE_GNAT_AUX_INFO (type))
7572 return NULL;
7573
7574 result = TYPE_DESCRIPTIVE_TYPE (type);
7575 while (result != NULL)
7576 {
0d5cff50 7577 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7578
7579 if (result_name == NULL)
dda83cd7
SM
7580 {
7581 warning (_("unexpected null name on descriptive type"));
7582 return NULL;
7583 }
b4ba55a1
JB
7584
7585 /* If the names match, stop. */
7586 if (strcmp (result_name, name) == 0)
7587 break;
7588
7589 /* Otherwise, look at the next item on the list, if any. */
7590 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7591 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7592 else
7593 tmp = NULL;
7594
7595 /* If not found either, try after having resolved the typedef. */
7596 if (tmp != NULL)
7597 result = tmp;
b4ba55a1 7598 else
931e5bc3 7599 {
f168693b 7600 result = check_typedef (result);
931e5bc3
JG
7601 if (HAVE_GNAT_AUX_INFO (result))
7602 result = TYPE_DESCRIPTIVE_TYPE (result);
7603 else
7604 result = NULL;
7605 }
b4ba55a1
JB
7606 }
7607
7608 /* If we didn't find a match, see whether this is a packed array. With
7609 older compilers, the descriptive type information is either absent or
7610 irrelevant when it comes to packed arrays so the above lookup fails.
7611 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7612 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7613 return ada_find_any_type (name);
7614
7615 return result;
7616}
7617
7618/* Find a parallel type to TYPE with the specified NAME, using the
7619 descriptive type taken from the debugging information, if available,
7620 and otherwise using the (slower) name-based method. */
7621
7622static struct type *
7623ada_find_parallel_type_with_name (struct type *type, const char *name)
7624{
7625 struct type *result = NULL;
7626
7627 if (HAVE_GNAT_AUX_INFO (type))
7628 result = find_parallel_type_by_descriptive_type (type, name);
7629 else
7630 result = ada_find_any_type (name);
7631
7632 return result;
7633}
7634
7635/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7636 SUFFIX to the name of TYPE. */
14f9c5c9 7637
d2e4a39e 7638struct type *
ebf56fd3 7639ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7640{
0d5cff50 7641 char *name;
fe978cb0 7642 const char *type_name = ada_type_name (type);
14f9c5c9 7643 int len;
d2e4a39e 7644
fe978cb0 7645 if (type_name == NULL)
14f9c5c9
AS
7646 return NULL;
7647
fe978cb0 7648 len = strlen (type_name);
14f9c5c9 7649
b4ba55a1 7650 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7651
fe978cb0 7652 strcpy (name, type_name);
14f9c5c9
AS
7653 strcpy (name + len, suffix);
7654
b4ba55a1 7655 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7656}
7657
14f9c5c9 7658/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7659 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7660
d2e4a39e
AS
7661static struct type *
7662dynamic_template_type (struct type *type)
14f9c5c9 7663{
61ee279c 7664 type = ada_check_typedef (type);
14f9c5c9 7665
78134374 7666 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7667 || ada_type_name (type) == NULL)
14f9c5c9 7668 return NULL;
d2e4a39e 7669 else
14f9c5c9
AS
7670 {
7671 int len = strlen (ada_type_name (type));
5b4ee69b 7672
4c4b4cd2 7673 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7674 return type;
14f9c5c9 7675 else
dda83cd7 7676 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7677 }
7678}
7679
7680/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7681 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7682
d2e4a39e
AS
7683static int
7684is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7685{
33d16dd9 7686 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7687
d2e4a39e 7688 return name != NULL
940da03e 7689 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7690 && strstr (name, "___XVL") != NULL;
7691}
7692
4c4b4cd2
PH
7693/* The index of the variant field of TYPE, or -1 if TYPE does not
7694 represent a variant record type. */
14f9c5c9 7695
d2e4a39e 7696static int
4c4b4cd2 7697variant_field_index (struct type *type)
14f9c5c9
AS
7698{
7699 int f;
7700
78134374 7701 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7702 return -1;
7703
1f704f76 7704 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7705 {
7706 if (ada_is_variant_part (type, f))
dda83cd7 7707 return f;
4c4b4cd2
PH
7708 }
7709 return -1;
14f9c5c9
AS
7710}
7711
4c4b4cd2
PH
7712/* A record type with no fields. */
7713
d2e4a39e 7714static struct type *
fe978cb0 7715empty_record (struct type *templ)
14f9c5c9 7716{
9fa83a7a 7717 struct type *type = type_allocator (templ).new_type ();
5b4ee69b 7718
67607e24 7719 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7720 INIT_NONE_SPECIFIC (type);
d0e39ea2 7721 type->set_name ("<empty>");
b6cdbc9a 7722 type->set_length (0);
14f9c5c9
AS
7723 return type;
7724}
7725
7726/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7727 the value of type TYPE at VALADDR or ADDRESS (see comments at
7728 the beginning of this section) VAL according to GNAT conventions.
7729 DVAL0 should describe the (portion of a) record that contains any
d0c97917 7730 necessary discriminants. It should be NULL if VAL->type () is
14f9c5c9
AS
7731 an outer-level type (i.e., as opposed to a branch of a variant.) A
7732 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7733 of the variant.
14f9c5c9 7734
4c4b4cd2
PH
7735 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7736 length are not statically known are discarded. As a consequence,
7737 VALADDR, ADDRESS and DVAL0 are ignored.
7738
7739 NOTE: Limitations: For now, we assume that dynamic fields and
7740 variants occupy whole numbers of bytes. However, they need not be
7741 byte-aligned. */
7742
7743struct type *
10a2c479 7744ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7745 const gdb_byte *valaddr,
dda83cd7
SM
7746 CORE_ADDR address, struct value *dval0,
7747 int keep_dynamic_fields)
14f9c5c9 7748{
d2e4a39e
AS
7749 struct value *dval;
7750 struct type *rtype;
14f9c5c9 7751 int nfields, bit_len;
4c4b4cd2 7752 int variant_field;
14f9c5c9 7753 long off;
d94e4f4f 7754 int fld_bit_len;
14f9c5c9
AS
7755 int f;
7756
65558ca5
TT
7757 scoped_value_mark mark;
7758
4c4b4cd2
PH
7759 /* Compute the number of fields in this record type that are going
7760 to be processed: unless keep_dynamic_fields, this includes only
7761 fields whose position and length are static will be processed. */
7762 if (keep_dynamic_fields)
1f704f76 7763 nfields = type->num_fields ();
4c4b4cd2
PH
7764 else
7765 {
7766 nfields = 0;
1f704f76 7767 while (nfields < type->num_fields ()
dda83cd7
SM
7768 && !ada_is_variant_part (type, nfields)
7769 && !is_dynamic_field (type, nfields))
7770 nfields++;
4c4b4cd2
PH
7771 }
7772
9fa83a7a 7773 rtype = type_allocator (type).new_type ();
67607e24 7774 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7775 INIT_NONE_SPECIFIC (rtype);
2774f2da 7776 rtype->alloc_fields (nfields);
d0e39ea2 7777 rtype->set_name (ada_type_name (type));
9cdd0d12 7778 rtype->set_is_fixed_instance (true);
14f9c5c9 7779
d2e4a39e
AS
7780 off = 0;
7781 bit_len = 0;
4c4b4cd2
PH
7782 variant_field = -1;
7783
14f9c5c9
AS
7784 for (f = 0; f < nfields; f += 1)
7785 {
a89febbd 7786 off = align_up (off, field_alignment (type, f))
b610c045 7787 + type->field (f).loc_bitpos ();
cd3f655c 7788 rtype->field (f).set_loc_bitpos (off);
886176b8 7789 rtype->field (f).set_bitsize (0);
14f9c5c9 7790
d2e4a39e 7791 if (ada_is_variant_part (type, f))
dda83cd7
SM
7792 {
7793 variant_field = f;
7794 fld_bit_len = 0;
7795 }
14f9c5c9 7796 else if (is_dynamic_field (type, f))
dda83cd7 7797 {
284614f0
JB
7798 const gdb_byte *field_valaddr = valaddr;
7799 CORE_ADDR field_address = address;
27710edb 7800 struct type *field_type = type->field (f).type ()->target_type ();
284614f0 7801
dda83cd7 7802 if (dval0 == NULL)
b5304971 7803 {
012370f6
TT
7804 /* Using plain value_from_contents_and_address here
7805 causes problems because we will end up trying to
7806 resolve a type that is currently being
7807 constructed. */
7808 dval = value_from_contents_and_address_unresolved (rtype,
7809 valaddr,
7810 address);
d0c97917 7811 rtype = dval->type ();
b5304971 7812 }
dda83cd7
SM
7813 else
7814 dval = dval0;
4c4b4cd2 7815
284614f0
JB
7816 /* If the type referenced by this field is an aligner type, we need
7817 to unwrap that aligner type, because its size might not be set.
7818 Keeping the aligner type would cause us to compute the wrong
7819 size for this field, impacting the offset of the all the fields
7820 that follow this one. */
7821 if (ada_is_aligner_type (field_type))
7822 {
b610c045 7823 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7824
7825 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7826 field_address = cond_offset_target (field_address, field_offset);
7827 field_type = ada_aligned_type (field_type);
7828 }
7829
7830 field_valaddr = cond_offset_host (field_valaddr,
7831 off / TARGET_CHAR_BIT);
7832 field_address = cond_offset_target (field_address,
7833 off / TARGET_CHAR_BIT);
7834
7835 /* Get the fixed type of the field. Note that, in this case,
7836 we do not want to get the real type out of the tag: if
7837 the current field is the parent part of a tagged record,
7838 we will get the tag of the object. Clearly wrong: the real
7839 type of the parent is not the real type of the child. We
7840 would end up in an infinite loop. */
7841 field_type = ada_get_base_type (field_type);
7842 field_type = ada_to_fixed_type (field_type, field_valaddr,
7843 field_address, dval, 0);
7844
5d14b6e5 7845 rtype->field (f).set_type (field_type);
33d16dd9 7846 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7847 /* The multiplication can potentially overflow. But because
7848 the field length has been size-checked just above, and
7849 assuming that the maximum size is a reasonable value,
7850 an overflow should not happen in practice. So rather than
7851 adding overflow recovery code to this already complex code,
7852 we just assume that it's not going to happen. */
df86565b 7853 fld_bit_len = rtype->field (f).type ()->length () * TARGET_CHAR_BIT;
dda83cd7 7854 }
14f9c5c9 7855 else
dda83cd7 7856 {
5ded5331
JB
7857 /* Note: If this field's type is a typedef, it is important
7858 to preserve the typedef layer.
7859
7860 Otherwise, we might be transforming a typedef to a fat
7861 pointer (encoding a pointer to an unconstrained array),
7862 into a basic fat pointer (encoding an unconstrained
7863 array). As both types are implemented using the same
7864 structure, the typedef is the only clue which allows us
7865 to distinguish between the two options. Stripping it
7866 would prevent us from printing this field appropriately. */
dda83cd7 7867 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7868 rtype->field (f).set_name (type->field (f).name ());
3757d2d4 7869 if (type->field (f).bitsize () > 0)
886176b8 7870 {
3757d2d4 7871 fld_bit_len = type->field (f).bitsize ();
886176b8
SM
7872 rtype->field (f).set_bitsize (fld_bit_len);
7873 }
dda83cd7 7874 else
5ded5331 7875 {
940da03e 7876 struct type *field_type = type->field (f).type ();
5ded5331
JB
7877
7878 /* We need to be careful of typedefs when computing
7879 the length of our field. If this is a typedef,
7880 get the length of the target type, not the length
7881 of the typedef. */
78134374 7882 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7883 field_type = ada_typedef_target_type (field_type);
7884
dda83cd7 7885 fld_bit_len =
df86565b 7886 ada_check_typedef (field_type)->length () * TARGET_CHAR_BIT;
5ded5331 7887 }
dda83cd7 7888 }
14f9c5c9 7889 if (off + fld_bit_len > bit_len)
dda83cd7 7890 bit_len = off + fld_bit_len;
d94e4f4f 7891 off += fld_bit_len;
b6cdbc9a 7892 rtype->set_length (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
14f9c5c9 7893 }
4c4b4cd2
PH
7894
7895 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7896 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7897 the record. This can happen in the presence of representation
7898 clauses. */
7899 if (variant_field >= 0)
7900 {
7901 struct type *branch_type;
7902
b610c045 7903 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
7904
7905 if (dval0 == NULL)
9f1f738a 7906 {
012370f6
TT
7907 /* Using plain value_from_contents_and_address here causes
7908 problems because we will end up trying to resolve a type
7909 that is currently being constructed. */
7910 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7911 address);
d0c97917 7912 rtype = dval->type ();
9f1f738a 7913 }
4c4b4cd2 7914 else
dda83cd7 7915 dval = dval0;
4c4b4cd2
PH
7916
7917 branch_type =
dda83cd7
SM
7918 to_fixed_variant_branch_type
7919 (type->field (variant_field).type (),
7920 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7921 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7922 if (branch_type == NULL)
dda83cd7
SM
7923 {
7924 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7925 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7926 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 7927 }
4c4b4cd2 7928 else
dda83cd7
SM
7929 {
7930 rtype->field (variant_field).set_type (branch_type);
d3fd12df 7931 rtype->field (variant_field).set_name ("S");
dda83cd7 7932 fld_bit_len =
df86565b 7933 rtype->field (variant_field).type ()->length () * TARGET_CHAR_BIT;
dda83cd7
SM
7934 if (off + fld_bit_len > bit_len)
7935 bit_len = off + fld_bit_len;
b6cdbc9a
SM
7936
7937 rtype->set_length
7938 (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
dda83cd7 7939 }
4c4b4cd2
PH
7940 }
7941
714e53ab
PH
7942 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7943 should contain the alignment of that record, which should be a strictly
7944 positive value. If null or negative, then something is wrong, most
7945 probably in the debug info. In that case, we don't round up the size
0963b4bd 7946 of the resulting type. If this record is not part of another structure,
714e53ab 7947 the current RTYPE length might be good enough for our purposes. */
df86565b 7948 if (type->length () <= 0)
714e53ab 7949 {
7d93a1e0 7950 if (rtype->name ())
cc1defb1 7951 warning (_("Invalid type size for `%s' detected: %s."),
df86565b 7952 rtype->name (), pulongest (type->length ()));
323e0a4a 7953 else
cc1defb1 7954 warning (_("Invalid type size for <unnamed> detected: %s."),
df86565b 7955 pulongest (type->length ()));
714e53ab
PH
7956 }
7957 else
df86565b 7958 rtype->set_length (align_up (rtype->length (), type->length ()));
14f9c5c9 7959
14f9c5c9
AS
7960 return rtype;
7961}
7962
4c4b4cd2
PH
7963/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7964 of 1. */
14f9c5c9 7965
d2e4a39e 7966static struct type *
fc1a4b47 7967template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 7968 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
7969{
7970 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 7971 address, dval0, 1);
4c4b4cd2
PH
7972}
7973
7974/* An ordinary record type in which ___XVL-convention fields and
7975 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7976 static approximations, containing all possible fields. Uses
7977 no runtime values. Useless for use in values, but that's OK,
7978 since the results are used only for type determinations. Works on both
7979 structs and unions. Representation note: to save space, we memorize
27710edb 7980 the result of this function in the type::target_type of the
4c4b4cd2
PH
7981 template type. */
7982
7983static struct type *
7984template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
7985{
7986 struct type *type;
7987 int nfields;
7988 int f;
7989
9e195661 7990 /* No need no do anything if the input type is already fixed. */
22c4c60c 7991 if (type0->is_fixed_instance ())
9e195661
PMR
7992 return type0;
7993
7994 /* Likewise if we already have computed the static approximation. */
27710edb
SM
7995 if (type0->target_type () != NULL)
7996 return type0->target_type ();
4c4b4cd2 7997
9e195661 7998 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 7999 type = type0;
1f704f76 8000 nfields = type0->num_fields ();
9e195661
PMR
8001
8002 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8003 recompute all over next time. */
8a50fdce 8004 type0->set_target_type (type);
14f9c5c9
AS
8005
8006 for (f = 0; f < nfields; f += 1)
8007 {
940da03e 8008 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8009 struct type *new_type;
14f9c5c9 8010
4c4b4cd2 8011 if (is_dynamic_field (type0, f))
460efde1
JB
8012 {
8013 field_type = ada_check_typedef (field_type);
27710edb 8014 new_type = to_static_fixed_type (field_type->target_type ());
460efde1 8015 }
14f9c5c9 8016 else
dda83cd7 8017 new_type = static_unwrap_type (field_type);
9e195661
PMR
8018
8019 if (new_type != field_type)
8020 {
8021 /* Clone TYPE0 only the first time we get a new field type. */
8022 if (type == type0)
8023 {
9fa83a7a 8024 type = type_allocator (type0).new_type ();
8a50fdce 8025 type0->set_target_type (type);
78134374 8026 type->set_code (type0->code ());
8ecb59f8 8027 INIT_NONE_SPECIFIC (type);
3cabb6b0 8028
2774f2da 8029 type->copy_fields (type0);
3cabb6b0 8030
d0e39ea2 8031 type->set_name (ada_type_name (type0));
9cdd0d12 8032 type->set_is_fixed_instance (true);
b6cdbc9a 8033 type->set_length (0);
9e195661 8034 }
5d14b6e5 8035 type->field (f).set_type (new_type);
33d16dd9 8036 type->field (f).set_name (type0->field (f).name ());
9e195661 8037 }
14f9c5c9 8038 }
9e195661 8039
14f9c5c9
AS
8040 return type;
8041}
8042
4c4b4cd2 8043/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8044 whose address in memory is ADDRESS, returns a revision of TYPE,
8045 which should be a non-dynamic-sized record, in which the variant
8046 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8047 for discriminant values in DVAL0, which can be NULL if the record
8048 contains the necessary discriminant values. */
8049
d2e4a39e 8050static struct type *
fc1a4b47 8051to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8052 CORE_ADDR address, struct value *dval0)
14f9c5c9 8053{
4c4b4cd2 8054 struct value *dval;
d2e4a39e 8055 struct type *rtype;
14f9c5c9 8056 struct type *branch_type;
1f704f76 8057 int nfields = type->num_fields ();
4c4b4cd2 8058 int variant_field = variant_field_index (type);
14f9c5c9 8059
4c4b4cd2 8060 if (variant_field == -1)
14f9c5c9
AS
8061 return type;
8062
65558ca5 8063 scoped_value_mark mark;
4c4b4cd2 8064 if (dval0 == NULL)
9f1f738a
SA
8065 {
8066 dval = value_from_contents_and_address (type, valaddr, address);
d0c97917 8067 type = dval->type ();
9f1f738a 8068 }
4c4b4cd2
PH
8069 else
8070 dval = dval0;
8071
9fa83a7a 8072 rtype = type_allocator (type).new_type ();
67607e24 8073 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8074 INIT_NONE_SPECIFIC (rtype);
2774f2da 8075 rtype->copy_fields (type);
3cabb6b0 8076
d0e39ea2 8077 rtype->set_name (ada_type_name (type));
9cdd0d12 8078 rtype->set_is_fixed_instance (true);
df86565b 8079 rtype->set_length (type->length ());
14f9c5c9 8080
4c4b4cd2 8081 branch_type = to_fixed_variant_branch_type
940da03e 8082 (type->field (variant_field).type (),
d2e4a39e 8083 cond_offset_host (valaddr,
b610c045 8084 type->field (variant_field).loc_bitpos ()
dda83cd7 8085 / TARGET_CHAR_BIT),
d2e4a39e 8086 cond_offset_target (address,
b610c045 8087 type->field (variant_field).loc_bitpos ()
dda83cd7 8088 / TARGET_CHAR_BIT), dval);
d2e4a39e 8089 if (branch_type == NULL)
14f9c5c9 8090 {
4c4b4cd2 8091 int f;
5b4ee69b 8092
4c4b4cd2 8093 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8094 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8095 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8096 }
8097 else
8098 {
5d14b6e5 8099 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8100 rtype->field (variant_field).set_name ("S");
886176b8 8101 rtype->field (variant_field).set_bitsize (0);
df86565b 8102 rtype->set_length (rtype->length () + branch_type->length ());
14f9c5c9 8103 }
b6cdbc9a 8104
df86565b
SM
8105 rtype->set_length (rtype->length ()
8106 - type->field (variant_field).type ()->length ());
d2e4a39e 8107
14f9c5c9
AS
8108 return rtype;
8109}
8110
8111/* An ordinary record type (with fixed-length fields) that describes
8112 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8113 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8114 should be in DVAL, a record value; it may be NULL if the object
8115 at ADDR itself contains any necessary discriminant values.
8116 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8117 values from the record are needed. Except in the case that DVAL,
8118 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8119 unchecked) is replaced by a particular branch of the variant.
8120
8121 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8122 is questionable and may be removed. It can arise during the
8123 processing of an unconstrained-array-of-record type where all the
8124 variant branches have exactly the same size. This is because in
8125 such cases, the compiler does not bother to use the XVS convention
8126 when encoding the record. I am currently dubious of this
8127 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8128
d2e4a39e 8129static struct type *
fc1a4b47 8130to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8131 CORE_ADDR address, struct value *dval)
14f9c5c9 8132{
d2e4a39e 8133 struct type *templ_type;
14f9c5c9 8134
22c4c60c 8135 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8136 return type0;
8137
d2e4a39e 8138 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8139
8140 if (templ_type != NULL)
8141 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8142 else if (variant_field_index (type0) >= 0)
8143 {
8144 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8145 return type0;
4c4b4cd2 8146 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8147 dval);
4c4b4cd2 8148 }
14f9c5c9
AS
8149 else
8150 {
9cdd0d12 8151 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8152 return type0;
8153 }
8154
8155}
8156
8157/* An ordinary record type (with fixed-length fields) that describes
8158 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8159 union type. Any necessary discriminants' values should be in DVAL,
8160 a record value. That is, this routine selects the appropriate
8161 branch of the union at ADDR according to the discriminant value
b1f33ddd 8162 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8163 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8164
d2e4a39e 8165static struct type *
fc1a4b47 8166to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8167 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8168{
8169 int which;
d2e4a39e
AS
8170 struct type *templ_type;
8171 struct type *var_type;
14f9c5c9 8172
78134374 8173 if (var_type0->code () == TYPE_CODE_PTR)
27710edb 8174 var_type = var_type0->target_type ();
d2e4a39e 8175 else
14f9c5c9
AS
8176 var_type = var_type0;
8177
8178 templ_type = ada_find_parallel_type (var_type, "___XVU");
8179
8180 if (templ_type != NULL)
8181 var_type = templ_type;
8182
d0c97917 8183 if (is_unchecked_variant (var_type, dval->type ()))
b1f33ddd 8184 return var_type0;
d8af9068 8185 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8186
8187 if (which < 0)
e9bb382b 8188 return empty_record (var_type);
14f9c5c9 8189 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8190 return to_fixed_record_type
27710edb 8191 (var_type->field (which).type ()->target_type(), valaddr, address, dval);
940da03e 8192 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8193 return
8194 to_fixed_record_type
940da03e 8195 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8196 else
940da03e 8197 return var_type->field (which).type ();
14f9c5c9
AS
8198}
8199
8908fca5
JB
8200/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8201 ENCODING_TYPE, a type following the GNAT conventions for discrete
8202 type encodings, only carries redundant information. */
8203
8204static int
8205ada_is_redundant_range_encoding (struct type *range_type,
8206 struct type *encoding_type)
8207{
108d56a4 8208 const char *bounds_str;
8908fca5
JB
8209 int n;
8210 LONGEST lo, hi;
8211
78134374 8212 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8213
78134374
SM
8214 if (get_base_type (range_type)->code ()
8215 != get_base_type (encoding_type)->code ())
005e2509
JB
8216 {
8217 /* The compiler probably used a simple base type to describe
8218 the range type instead of the range's actual base type,
8219 expecting us to get the real base type from the encoding
8220 anyway. In this situation, the encoding cannot be ignored
8221 as redundant. */
8222 return 0;
8223 }
8224
8908fca5
JB
8225 if (is_dynamic_type (range_type))
8226 return 0;
8227
7d93a1e0 8228 if (encoding_type->name () == NULL)
8908fca5
JB
8229 return 0;
8230
7d93a1e0 8231 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8232 if (bounds_str == NULL)
8233 return 0;
8234
8235 n = 8; /* Skip "___XDLU_". */
8236 if (!ada_scan_number (bounds_str, n, &lo, &n))
8237 return 0;
5537ddd0 8238 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8239 return 0;
8240
8241 n += 2; /* Skip the "__" separator between the two bounds. */
8242 if (!ada_scan_number (bounds_str, n, &hi, &n))
8243 return 0;
5537ddd0 8244 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8245 return 0;
8246
8247 return 1;
8248}
8249
8250/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8251 a type following the GNAT encoding for describing array type
8252 indices, only carries redundant information. */
8253
8254static int
8255ada_is_redundant_index_type_desc (struct type *array_type,
8256 struct type *desc_type)
8257{
8258 struct type *this_layer = check_typedef (array_type);
8259 int i;
8260
1f704f76 8261 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8262 {
3d967001 8263 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8264 desc_type->field (i).type ()))
8908fca5 8265 return 0;
27710edb 8266 this_layer = check_typedef (this_layer->target_type ());
8908fca5
JB
8267 }
8268
8269 return 1;
8270}
8271
14f9c5c9
AS
8272/* Assuming that TYPE0 is an array type describing the type of a value
8273 at ADDR, and that DVAL describes a record containing any
8274 discriminants used in TYPE0, returns a type for the value that
8275 contains no dynamic components (that is, no components whose sizes
8276 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8277 true, gives an error message if the resulting type's size is over
4c4b4cd2 8278 varsize_limit. */
14f9c5c9 8279
d2e4a39e
AS
8280static struct type *
8281to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8282 int ignore_too_big)
14f9c5c9 8283{
d2e4a39e
AS
8284 struct type *index_type_desc;
8285 struct type *result;
ad82864c 8286 int constrained_packed_array_p;
931e5bc3 8287 static const char *xa_suffix = "___XA";
14f9c5c9 8288
b0dd7688 8289 type0 = ada_check_typedef (type0);
22c4c60c 8290 if (type0->is_fixed_instance ())
4c4b4cd2 8291 return type0;
14f9c5c9 8292
ad82864c
JB
8293 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8294 if (constrained_packed_array_p)
75fd6a26
TT
8295 {
8296 type0 = decode_constrained_packed_array_type (type0);
8297 if (type0 == nullptr)
8298 error (_("could not decode constrained packed array type"));
8299 }
284614f0 8300
931e5bc3
JG
8301 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8302
8303 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8304 encoding suffixed with 'P' may still be generated. If so,
8305 it should be used to find the XA type. */
8306
8307 if (index_type_desc == NULL)
8308 {
1da0522e 8309 const char *type_name = ada_type_name (type0);
931e5bc3 8310
1da0522e 8311 if (type_name != NULL)
931e5bc3 8312 {
1da0522e 8313 const int len = strlen (type_name);
931e5bc3
JG
8314 char *name = (char *) alloca (len + strlen (xa_suffix));
8315
1da0522e 8316 if (type_name[len - 1] == 'P')
931e5bc3 8317 {
1da0522e 8318 strcpy (name, type_name);
931e5bc3
JG
8319 strcpy (name + len - 1, xa_suffix);
8320 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8321 }
8322 }
8323 }
8324
28c85d6c 8325 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8326 if (index_type_desc != NULL
8327 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8328 {
8329 /* Ignore this ___XA parallel type, as it does not bring any
8330 useful information. This allows us to avoid creating fixed
8331 versions of the array's index types, which would be identical
8332 to the original ones. This, in turn, can also help avoid
8333 the creation of fixed versions of the array itself. */
8334 index_type_desc = NULL;
8335 }
8336
14f9c5c9
AS
8337 if (index_type_desc == NULL)
8338 {
27710edb 8339 struct type *elt_type0 = ada_check_typedef (type0->target_type ());
5b4ee69b 8340
14f9c5c9 8341 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8342 depend on the contents of the array in properly constructed
8343 debugging data. */
529cad9c 8344 /* Create a fixed version of the array element type.
dda83cd7
SM
8345 We're not providing the address of an element here,
8346 and thus the actual object value cannot be inspected to do
8347 the conversion. This should not be a problem, since arrays of
8348 unconstrained objects are not allowed. In particular, all
8349 the elements of an array of a tagged type should all be of
8350 the same type specified in the debugging info. No need to
8351 consult the object tag. */
1ed6ede0 8352 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8353
284614f0
JB
8354 /* Make sure we always create a new array type when dealing with
8355 packed array types, since we're going to fix-up the array
8356 type length and element bitsize a little further down. */
ad82864c 8357 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8358 result = type0;
14f9c5c9 8359 else
9e76b17a
TT
8360 {
8361 type_allocator alloc (type0);
8362 result = create_array_type (alloc, elt_type, type0->index_type ());
8363 }
14f9c5c9
AS
8364 }
8365 else
8366 {
8367 int i;
8368 struct type *elt_type0;
8369
8370 elt_type0 = type0;
1f704f76 8371 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
27710edb 8372 elt_type0 = elt_type0->target_type ();
14f9c5c9
AS
8373
8374 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8375 depend on the contents of the array in properly constructed
8376 debugging data. */
529cad9c 8377 /* Create a fixed version of the array element type.
dda83cd7
SM
8378 We're not providing the address of an element here,
8379 and thus the actual object value cannot be inspected to do
8380 the conversion. This should not be a problem, since arrays of
8381 unconstrained objects are not allowed. In particular, all
8382 the elements of an array of a tagged type should all be of
8383 the same type specified in the debugging info. No need to
8384 consult the object tag. */
1ed6ede0 8385 result =
dda83cd7 8386 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8387
8388 elt_type0 = type0;
1f704f76 8389 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8390 {
8391 struct type *range_type =
8392 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8393
9e76b17a
TT
8394 type_allocator alloc (elt_type0);
8395 result = create_array_type (alloc, result, range_type);
27710edb 8396 elt_type0 = elt_type0->target_type ();
dda83cd7 8397 }
14f9c5c9
AS
8398 }
8399
2e6fda7d
JB
8400 /* We want to preserve the type name. This can be useful when
8401 trying to get the type name of a value that has already been
8402 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8403 result->set_name (type0->name ());
2e6fda7d 8404
ad82864c 8405 if (constrained_packed_array_p)
284614f0
JB
8406 {
8407 /* So far, the resulting type has been created as if the original
8408 type was a regular (non-packed) array type. As a result, the
8409 bitsize of the array elements needs to be set again, and the array
8410 length needs to be recomputed based on that bitsize. */
df86565b 8411 int len = result->length () / result->target_type ()->length ();
3757d2d4 8412 int elt_bitsize = type0->field (0).bitsize ();
284614f0 8413
3757d2d4 8414 result->field (0).set_bitsize (elt_bitsize);
b6cdbc9a 8415 result->set_length (len * elt_bitsize / HOST_CHAR_BIT);
df86565b
SM
8416 if (result->length () * HOST_CHAR_BIT < len * elt_bitsize)
8417 result->set_length (result->length () + 1);
284614f0
JB
8418 }
8419
9cdd0d12 8420 result->set_is_fixed_instance (true);
14f9c5c9 8421 return result;
d2e4a39e 8422}
14f9c5c9
AS
8423
8424
8425/* A standard type (containing no dynamically sized components)
8426 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8427 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8428 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8429 ADDRESS or in VALADDR contains these discriminants.
8430
1ed6ede0
JB
8431 If CHECK_TAG is not null, in the case of tagged types, this function
8432 attempts to locate the object's tag and use it to compute the actual
8433 type. However, when ADDRESS is null, we cannot use it to determine the
8434 location of the tag, and therefore compute the tagged type's actual type.
8435 So we return the tagged type without consulting the tag. */
529cad9c 8436
f192137b
JB
8437static struct type *
8438ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8439 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8440{
61ee279c 8441 type = ada_check_typedef (type);
8ecb59f8
TT
8442
8443 /* Only un-fixed types need to be handled here. */
8444 if (!HAVE_GNAT_AUX_INFO (type))
8445 return type;
8446
78134374 8447 switch (type->code ())
d2e4a39e
AS
8448 {
8449 default:
14f9c5c9 8450 return type;
d2e4a39e 8451 case TYPE_CODE_STRUCT:
4c4b4cd2 8452 {
dda83cd7
SM
8453 struct type *static_type = to_static_fixed_type (type);
8454 struct type *fixed_record_type =
8455 to_fixed_record_type (type, valaddr, address, NULL);
8456
8457 /* If STATIC_TYPE is a tagged type and we know the object's address,
8458 then we can determine its tag, and compute the object's actual
8459 type from there. Note that we have to use the fixed record
8460 type (the parent part of the record may have dynamic fields
8461 and the way the location of _tag is expressed may depend on
8462 them). */
8463
8464 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8465 {
b50d69b5
JG
8466 struct value *tag =
8467 value_tag_from_contents_and_address
8468 (fixed_record_type,
8469 valaddr,
8470 address);
8471 struct type *real_type = type_from_tag (tag);
8472 struct value *obj =
8473 value_from_contents_and_address (fixed_record_type,
8474 valaddr,
8475 address);
d0c97917 8476 fixed_record_type = obj->type ();
dda83cd7
SM
8477 if (real_type != NULL)
8478 return to_fixed_record_type
b50d69b5 8479 (real_type, NULL,
9feb2d07 8480 ada_tag_value_at_base_address (obj)->address (), NULL);
dda83cd7
SM
8481 }
8482
8483 /* Check to see if there is a parallel ___XVZ variable.
8484 If there is, then it provides the actual size of our type. */
8485 else if (ada_type_name (fixed_record_type) != NULL)
8486 {
8487 const char *name = ada_type_name (fixed_record_type);
8488 char *xvz_name
224c3ddb 8489 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8490 bool xvz_found = false;
dda83cd7 8491 LONGEST size;
4af88198 8492
dda83cd7 8493 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8494 try
eccab96d
JB
8495 {
8496 xvz_found = get_int_var_value (xvz_name, size);
8497 }
230d2906 8498 catch (const gdb_exception_error &except)
eccab96d
JB
8499 {
8500 /* We found the variable, but somehow failed to read
8501 its value. Rethrow the same error, but with a little
8502 bit more information, to help the user understand
8503 what went wrong (Eg: the variable might have been
8504 optimized out). */
8505 throw_error (except.error,
8506 _("unable to read value of %s (%s)"),
3d6e9d23 8507 xvz_name, except.what ());
eccab96d 8508 }
eccab96d 8509
df86565b 8510 if (xvz_found && fixed_record_type->length () != size)
dda83cd7
SM
8511 {
8512 fixed_record_type = copy_type (fixed_record_type);
b6cdbc9a 8513 fixed_record_type->set_length (size);
dda83cd7
SM
8514
8515 /* The FIXED_RECORD_TYPE may have be a stub. We have
8516 observed this when the debugging info is STABS, and
8517 apparently it is something that is hard to fix.
8518
8519 In practice, we don't need the actual type definition
8520 at all, because the presence of the XVZ variable allows us
8521 to assume that there must be a XVS type as well, which we
8522 should be able to use later, when we need the actual type
8523 definition.
8524
8525 In the meantime, pretend that the "fixed" type we are
8526 returning is NOT a stub, because this can cause trouble
8527 when using this type to create new types targeting it.
8528 Indeed, the associated creation routines often check
8529 whether the target type is a stub and will try to replace
8530 it, thus using a type with the wrong size. This, in turn,
8531 might cause the new type to have the wrong size too.
8532 Consider the case of an array, for instance, where the size
8533 of the array is computed from the number of elements in
8534 our array multiplied by the size of its element. */
b4b73759 8535 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8536 }
8537 }
8538 return fixed_record_type;
4c4b4cd2 8539 }
d2e4a39e 8540 case TYPE_CODE_ARRAY:
4c4b4cd2 8541 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8542 case TYPE_CODE_UNION:
8543 if (dval == NULL)
dda83cd7 8544 return type;
d2e4a39e 8545 else
dda83cd7 8546 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8547 }
14f9c5c9
AS
8548}
8549
f192137b
JB
8550/* The same as ada_to_fixed_type_1, except that it preserves the type
8551 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8552
8553 The typedef layer needs be preserved in order to differentiate between
8554 arrays and array pointers when both types are implemented using the same
8555 fat pointer. In the array pointer case, the pointer is encoded as
8556 a typedef of the pointer type. For instance, considering:
8557
8558 type String_Access is access String;
8559 S1 : String_Access := null;
8560
8561 To the debugger, S1 is defined as a typedef of type String. But
8562 to the user, it is a pointer. So if the user tries to print S1,
8563 we should not dereference the array, but print the array address
8564 instead.
8565
8566 If we didn't preserve the typedef layer, we would lose the fact that
8567 the type is to be presented as a pointer (needs de-reference before
8568 being printed). And we would also use the source-level type name. */
f192137b
JB
8569
8570struct type *
8571ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8572 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8573
8574{
8575 struct type *fixed_type =
8576 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8577
96dbd2c1
JB
8578 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8579 then preserve the typedef layer.
8580
8581 Implementation note: We can only check the main-type portion of
8582 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8583 from TYPE now returns a type that has the same instance flags
8584 as TYPE. For instance, if TYPE is a "typedef const", and its
8585 target type is a "struct", then the typedef elimination will return
8586 a "const" version of the target type. See check_typedef for more
8587 details about how the typedef layer elimination is done.
8588
8589 brobecker/2010-11-19: It seems to me that the only case where it is
8590 useful to preserve the typedef layer is when dealing with fat pointers.
8591 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8592 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8593 because we call check_typedef/ada_check_typedef pretty much everywhere.
8594 */
78134374 8595 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8596 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8597 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8598 return type;
8599
8600 return fixed_type;
8601}
8602
14f9c5c9 8603/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8604 TYPE0, but based on no runtime data. */
14f9c5c9 8605
d2e4a39e
AS
8606static struct type *
8607to_static_fixed_type (struct type *type0)
14f9c5c9 8608{
d2e4a39e 8609 struct type *type;
14f9c5c9
AS
8610
8611 if (type0 == NULL)
8612 return NULL;
8613
22c4c60c 8614 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8615 return type0;
8616
61ee279c 8617 type0 = ada_check_typedef (type0);
d2e4a39e 8618
78134374 8619 switch (type0->code ())
14f9c5c9
AS
8620 {
8621 default:
8622 return type0;
8623 case TYPE_CODE_STRUCT:
8624 type = dynamic_template_type (type0);
d2e4a39e 8625 if (type != NULL)
dda83cd7 8626 return template_to_static_fixed_type (type);
4c4b4cd2 8627 else
dda83cd7 8628 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8629 case TYPE_CODE_UNION:
8630 type = ada_find_parallel_type (type0, "___XVU");
8631 if (type != NULL)
dda83cd7 8632 return template_to_static_fixed_type (type);
4c4b4cd2 8633 else
dda83cd7 8634 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8635 }
8636}
8637
4c4b4cd2
PH
8638/* A static approximation of TYPE with all type wrappers removed. */
8639
d2e4a39e
AS
8640static struct type *
8641static_unwrap_type (struct type *type)
14f9c5c9
AS
8642{
8643 if (ada_is_aligner_type (type))
8644 {
940da03e 8645 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8646 if (ada_type_name (type1) == NULL)
d0e39ea2 8647 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8648
8649 return static_unwrap_type (type1);
8650 }
d2e4a39e 8651 else
14f9c5c9 8652 {
d2e4a39e 8653 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8654
d2e4a39e 8655 if (raw_real_type == type)
dda83cd7 8656 return type;
14f9c5c9 8657 else
dda83cd7 8658 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8659 }
8660}
8661
8662/* In some cases, incomplete and private types require
4c4b4cd2 8663 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8664 type Foo;
8665 type FooP is access Foo;
8666 V: FooP;
8667 type Foo is array ...;
4c4b4cd2 8668 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8669 cross-references to such types, we instead substitute for FooP a
8670 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8671 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8672
8673/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8674 exists, otherwise TYPE. */
8675
d2e4a39e 8676struct type *
61ee279c 8677ada_check_typedef (struct type *type)
14f9c5c9 8678{
727e3d2e
JB
8679 if (type == NULL)
8680 return NULL;
8681
736ade86
XR
8682 /* If our type is an access to an unconstrained array, which is encoded
8683 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8684 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8685 what allows us to distinguish between fat pointers that represent
8686 array types, and fat pointers that represent array access types
8687 (in both cases, the compiler implements them as fat pointers). */
736ade86 8688 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8689 return type;
8690
f168693b 8691 type = check_typedef (type);
78134374 8692 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8693 || !type->is_stub ()
7d93a1e0 8694 || type->name () == NULL)
14f9c5c9 8695 return type;
d2e4a39e 8696 else
14f9c5c9 8697 {
7d93a1e0 8698 const char *name = type->name ();
d2e4a39e 8699 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8700
05e522ef 8701 if (type1 == NULL)
dda83cd7 8702 return type;
05e522ef
JB
8703
8704 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8705 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8706 types, only for the typedef-to-array types). If that's the case,
8707 strip the typedef layer. */
78134374 8708 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8709 type1 = ada_check_typedef (type1);
8710
8711 return type1;
14f9c5c9
AS
8712 }
8713}
8714
8715/* A value representing the data at VALADDR/ADDRESS as described by
8716 type TYPE0, but with a standard (static-sized) type that correctly
8717 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8718 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8719 creation of struct values]. */
14f9c5c9 8720
4c4b4cd2
PH
8721static struct value *
8722ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8723 struct value *val0)
14f9c5c9 8724{
1ed6ede0 8725 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8726
14f9c5c9
AS
8727 if (type == type0 && val0 != NULL)
8728 return val0;
cc0e770c 8729
736355f2 8730 if (val0->lval () != lval_memory)
cc0e770c
JB
8731 {
8732 /* Our value does not live in memory; it could be a convenience
8733 variable, for instance. Create a not_lval value using val0's
8734 contents. */
efaf1ae0 8735 return value_from_contents (type, val0->contents ().data ());
cc0e770c
JB
8736 }
8737
8738 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8739}
8740
8741/* A value representing VAL, but with a standard (static-sized) type
8742 that correctly describes it. Does not necessarily create a new
8743 value. */
8744
0c3acc09 8745struct value *
4c4b4cd2
PH
8746ada_to_fixed_value (struct value *val)
8747{
c48db5ca 8748 val = unwrap_value (val);
9feb2d07 8749 val = ada_to_fixed_value_create (val->type (), val->address (), val);
c48db5ca 8750 return val;
14f9c5c9 8751}
d2e4a39e 8752\f
14f9c5c9 8753
14f9c5c9
AS
8754/* Attributes */
8755
4c4b4cd2 8756/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8757
4c4b4cd2
PH
8758static LONGEST
8759pos_atr (struct value *arg)
14f9c5c9 8760{
24209737 8761 struct value *val = coerce_ref (arg);
d0c97917 8762 struct type *type = val->type ();
14f9c5c9 8763
d2e4a39e 8764 if (!discrete_type_p (type))
323e0a4a 8765 error (_("'POS only defined on discrete types"));
14f9c5c9 8766
6244c119
SM
8767 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8768 if (!result.has_value ())
aa715135 8769 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8770
6244c119 8771 return *result;
4c4b4cd2
PH
8772}
8773
7631cf6c 8774struct value *
7992accc
TT
8775ada_pos_atr (struct type *expect_type,
8776 struct expression *exp,
8777 enum noside noside, enum exp_opcode op,
8778 struct value *arg)
4c4b4cd2 8779{
7992accc
TT
8780 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8781 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8782 return value::zero (type, not_lval);
3cb382c9 8783 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8784}
8785
4c4b4cd2 8786/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8787
d2e4a39e 8788static struct value *
53a47a3e 8789val_atr (struct type *type, LONGEST val)
14f9c5c9 8790{
53a47a3e 8791 gdb_assert (discrete_type_p (type));
0bc2354b 8792 if (type->code () == TYPE_CODE_RANGE)
27710edb 8793 type = type->target_type ();
78134374 8794 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8795 {
53a47a3e 8796 if (val < 0 || val >= type->num_fields ())
dda83cd7 8797 error (_("argument to 'VAL out of range"));
970db518 8798 val = type->field (val).loc_enumval ();
14f9c5c9 8799 }
53a47a3e
TT
8800 return value_from_longest (type, val);
8801}
8802
9e99f48f 8803struct value *
22f6f797
TT
8804ada_val_atr (struct expression *exp, enum noside noside, struct type *type,
8805 struct value *arg)
53a47a3e 8806{
3848abd6 8807 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8808 return value::zero (type, not_lval);
3848abd6 8809
53a47a3e
TT
8810 if (!discrete_type_p (type))
8811 error (_("'VAL only defined on discrete types"));
d0c97917 8812 if (!integer_type_p (arg->type ()))
53a47a3e
TT
8813 error (_("'VAL requires integral argument"));
8814
8815 return val_atr (type, value_as_long (arg));
14f9c5c9 8816}
22f6f797
TT
8817
8818/* Implementation of the enum_rep attribute. */
8819struct value *
8820ada_atr_enum_rep (struct expression *exp, enum noside noside, struct type *type,
8821 struct value *arg)
8822{
8823 struct type *inttype = builtin_type (exp->gdbarch)->builtin_int;
8824 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8825 return value::zero (inttype, not_lval);
8826
8827 if (type->code () == TYPE_CODE_RANGE)
8828 type = type->target_type ();
8829 if (type->code () != TYPE_CODE_ENUM)
8830 error (_("'Enum_Rep only defined on enum types"));
8831 if (!types_equal (type, arg->type ()))
8832 error (_("'Enum_Rep requires argument to have same type as enum"));
8833
8834 return value_cast (inttype, arg);
8835}
8836
8837/* Implementation of the enum_val attribute. */
8838struct value *
8839ada_atr_enum_val (struct expression *exp, enum noside noside, struct type *type,
8840 struct value *arg)
8841{
8842 struct type *original_type = type;
8843 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8844 return value::zero (original_type, not_lval);
8845
8846 if (type->code () == TYPE_CODE_RANGE)
8847 type = type->target_type ();
8848 if (type->code () != TYPE_CODE_ENUM)
8849 error (_("'Enum_Val only defined on enum types"));
8850 if (!integer_type_p (arg->type ()))
8851 error (_("'Enum_Val requires integral argument"));
8852
8853 LONGEST value = value_as_long (arg);
8854 for (int i = 0; i < type->num_fields (); ++i)
8855 {
8856 if (type->field (i).loc_enumval () == value)
8857 return value_from_longest (original_type, value);
8858 }
8859
8860 error (_("value %s not found in enum"), plongest (value));
8861}
8862
14f9c5c9 8863\f
d2e4a39e 8864
dda83cd7 8865 /* Evaluation */
14f9c5c9 8866
4c4b4cd2
PH
8867/* True if TYPE appears to be an Ada character type.
8868 [At the moment, this is true only for Character and Wide_Character;
8869 It is a heuristic test that could stand improvement]. */
14f9c5c9 8870
fc913e53 8871bool
d2e4a39e 8872ada_is_character_type (struct type *type)
14f9c5c9 8873{
7b9f71f2
JB
8874 const char *name;
8875
8876 /* If the type code says it's a character, then assume it really is,
8877 and don't check any further. */
78134374 8878 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8879 return true;
7b9f71f2
JB
8880
8881 /* Otherwise, assume it's a character type iff it is a discrete type
8882 with a known character type name. */
8883 name = ada_type_name (type);
8884 return (name != NULL
dda83cd7
SM
8885 && (type->code () == TYPE_CODE_INT
8886 || type->code () == TYPE_CODE_RANGE)
8887 && (strcmp (name, "character") == 0
8888 || strcmp (name, "wide_character") == 0
8889 || strcmp (name, "wide_wide_character") == 0
8890 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8891}
8892
4c4b4cd2 8893/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8894
fc913e53 8895bool
ebf56fd3 8896ada_is_string_type (struct type *type)
14f9c5c9 8897{
61ee279c 8898 type = ada_check_typedef (type);
d2e4a39e 8899 if (type != NULL
78134374 8900 && type->code () != TYPE_CODE_PTR
76a01679 8901 && (ada_is_simple_array_type (type)
dda83cd7 8902 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8903 && ada_array_arity (type) == 1)
8904 {
8905 struct type *elttype = ada_array_element_type (type, 1);
8906
8907 return ada_is_character_type (elttype);
8908 }
d2e4a39e 8909 else
fc913e53 8910 return false;
14f9c5c9
AS
8911}
8912
5bf03f13
JB
8913/* The compiler sometimes provides a parallel XVS type for a given
8914 PAD type. Normally, it is safe to follow the PAD type directly,
8915 but older versions of the compiler have a bug that causes the offset
8916 of its "F" field to be wrong. Following that field in that case
8917 would lead to incorrect results, but this can be worked around
8918 by ignoring the PAD type and using the associated XVS type instead.
8919
8920 Set to True if the debugger should trust the contents of PAD types.
8921 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8922static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8923
8924/* True if TYPE is a struct type introduced by the compiler to force the
8925 alignment of a value. Such types have a single field with a
4c4b4cd2 8926 distinctive name. */
14f9c5c9
AS
8927
8928int
ebf56fd3 8929ada_is_aligner_type (struct type *type)
14f9c5c9 8930{
61ee279c 8931 type = ada_check_typedef (type);
714e53ab 8932
5bf03f13 8933 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
8934 return 0;
8935
78134374 8936 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 8937 && type->num_fields () == 1
33d16dd9 8938 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
8939}
8940
8941/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 8942 the parallel type. */
14f9c5c9 8943
d2e4a39e
AS
8944struct type *
8945ada_get_base_type (struct type *raw_type)
14f9c5c9 8946{
d2e4a39e
AS
8947 struct type *real_type_namer;
8948 struct type *raw_real_type;
14f9c5c9 8949
78134374 8950 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
8951 return raw_type;
8952
284614f0
JB
8953 if (ada_is_aligner_type (raw_type))
8954 /* The encoding specifies that we should always use the aligner type.
8955 So, even if this aligner type has an associated XVS type, we should
8956 simply ignore it.
8957
8958 According to the compiler gurus, an XVS type parallel to an aligner
8959 type may exist because of a stabs limitation. In stabs, aligner
8960 types are empty because the field has a variable-sized type, and
8961 thus cannot actually be used as an aligner type. As a result,
8962 we need the associated parallel XVS type to decode the type.
8963 Since the policy in the compiler is to not change the internal
8964 representation based on the debugging info format, we sometimes
8965 end up having a redundant XVS type parallel to the aligner type. */
8966 return raw_type;
8967
14f9c5c9 8968 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 8969 if (real_type_namer == NULL
78134374 8970 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 8971 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
8972 return raw_type;
8973
940da03e 8974 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
8975 {
8976 /* This is an older encoding form where the base type needs to be
85102364 8977 looked up by name. We prefer the newer encoding because it is
f80d3ff2 8978 more efficient. */
33d16dd9 8979 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
8980 if (raw_real_type == NULL)
8981 return raw_type;
8982 else
8983 return raw_real_type;
8984 }
8985
8986 /* The field in our XVS type is a reference to the base type. */
27710edb 8987 return real_type_namer->field (0).type ()->target_type ();
d2e4a39e 8988}
14f9c5c9 8989
4c4b4cd2 8990/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 8991
d2e4a39e
AS
8992struct type *
8993ada_aligned_type (struct type *type)
14f9c5c9
AS
8994{
8995 if (ada_is_aligner_type (type))
940da03e 8996 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
8997 else
8998 return ada_get_base_type (type);
8999}
9000
9001
9002/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9003 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9004
fc1a4b47
AC
9005const gdb_byte *
9006ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9007{
d2e4a39e 9008 if (ada_is_aligner_type (type))
b610c045
SM
9009 return ada_aligned_value_addr
9010 (type->field (0).type (),
9011 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
9012 else
9013 return valaddr;
9014}
9015
4c4b4cd2
PH
9016
9017
14f9c5c9 9018/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9019 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9020const char *
9021ada_enum_name (const char *name)
14f9c5c9 9022{
5f9febe0 9023 static std::string storage;
e6a959d6 9024 const char *tmp;
14f9c5c9 9025
4c4b4cd2
PH
9026 /* First, unqualify the enumeration name:
9027 1. Search for the last '.' character. If we find one, then skip
177b42fe 9028 all the preceding characters, the unqualified name starts
76a01679 9029 right after that dot.
4c4b4cd2 9030 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9031 translates dots into "__". Search forward for double underscores,
9032 but stop searching when we hit an overloading suffix, which is
9033 of the form "__" followed by digits. */
4c4b4cd2 9034
c3e5cd34
PH
9035 tmp = strrchr (name, '.');
9036 if (tmp != NULL)
4c4b4cd2
PH
9037 name = tmp + 1;
9038 else
14f9c5c9 9039 {
4c4b4cd2 9040 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9041 {
9042 if (isdigit (tmp[2]))
9043 break;
9044 else
9045 name = tmp + 2;
9046 }
14f9c5c9
AS
9047 }
9048
9049 if (name[0] == 'Q')
9050 {
14f9c5c9 9051 int v;
5b4ee69b 9052
14f9c5c9 9053 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9054 {
a7041de8
TT
9055 int offset = 2;
9056 if (name[1] == 'W' && name[2] == 'W')
9057 {
9058 /* Also handle the QWW case. */
9059 ++offset;
9060 }
9061 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9062 return name;
9063 }
272560b5
TT
9064 else if (((name[1] >= '0' && name[1] <= '9')
9065 || (name[1] >= 'a' && name[1] <= 'z'))
9066 && name[2] == '\0')
9067 {
5f9febe0
TT
9068 storage = string_printf ("'%c'", name[1]);
9069 return storage.c_str ();
272560b5 9070 }
14f9c5c9 9071 else
dda83cd7 9072 return name;
14f9c5c9
AS
9073
9074 if (isascii (v) && isprint (v))
5f9febe0 9075 storage = string_printf ("'%c'", v);
14f9c5c9 9076 else if (name[1] == 'U')
a7041de8
TT
9077 storage = string_printf ("'[\"%02x\"]'", v);
9078 else if (name[2] != 'W')
9079 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9080 else
a7041de8 9081 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9082
5f9febe0 9083 return storage.c_str ();
14f9c5c9 9084 }
d2e4a39e 9085 else
4c4b4cd2 9086 {
c3e5cd34
PH
9087 tmp = strstr (name, "__");
9088 if (tmp == NULL)
9089 tmp = strstr (name, "$");
9090 if (tmp != NULL)
dda83cd7 9091 {
5f9febe0
TT
9092 storage = std::string (name, tmp - name);
9093 return storage.c_str ();
dda83cd7 9094 }
4c4b4cd2
PH
9095
9096 return name;
9097 }
14f9c5c9
AS
9098}
9099
013a623f
TT
9100/* If TYPE is a dynamic type, return the base type. Otherwise, if
9101 there is no parallel type, return nullptr. */
9102
9103static struct type *
9104find_base_type (struct type *type)
9105{
9106 struct type *raw_real_type
9107 = ada_check_typedef (ada_get_base_type (type));
9108
9109 /* No parallel XVS or XVE type. */
9110 if (type == raw_real_type
9111 && ada_find_parallel_type (type, "___XVE") == nullptr)
9112 return nullptr;
9113
9114 return raw_real_type;
9115}
9116
14f9c5c9 9117/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9118 value it wraps. */
14f9c5c9 9119
d2e4a39e
AS
9120static struct value *
9121unwrap_value (struct value *val)
14f9c5c9 9122{
d0c97917 9123 struct type *type = ada_check_typedef (val->type ());
5b4ee69b 9124
14f9c5c9
AS
9125 if (ada_is_aligner_type (type))
9126 {
de4d072f 9127 struct value *v = ada_value_struct_elt (val, "F", 0);
d0c97917 9128 struct type *val_type = ada_check_typedef (v->type ());
5b4ee69b 9129
14f9c5c9 9130 if (ada_type_name (val_type) == NULL)
d0e39ea2 9131 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9132
9133 return unwrap_value (v);
9134 }
d2e4a39e 9135 else
14f9c5c9 9136 {
013a623f
TT
9137 struct type *raw_real_type = find_base_type (type);
9138 if (raw_real_type == nullptr)
5bf03f13 9139 return val;
14f9c5c9 9140
d2e4a39e 9141 return
dda83cd7
SM
9142 coerce_unspec_val_to_type
9143 (val, ada_to_fixed_type (raw_real_type, 0,
9feb2d07 9144 val->address (),
dda83cd7 9145 NULL, 1));
14f9c5c9
AS
9146 }
9147}
d2e4a39e 9148
d99dcf51
JB
9149/* Given two array types T1 and T2, return nonzero iff both arrays
9150 contain the same number of elements. */
9151
9152static int
9153ada_same_array_size_p (struct type *t1, struct type *t2)
9154{
9155 LONGEST lo1, hi1, lo2, hi2;
9156
9157 /* Get the array bounds in order to verify that the size of
9158 the two arrays match. */
9159 if (!get_array_bounds (t1, &lo1, &hi1)
9160 || !get_array_bounds (t2, &lo2, &hi2))
9161 error (_("unable to determine array bounds"));
9162
9163 /* To make things easier for size comparison, normalize a bit
9164 the case of empty arrays by making sure that the difference
9165 between upper bound and lower bound is always -1. */
9166 if (lo1 > hi1)
9167 hi1 = lo1 - 1;
9168 if (lo2 > hi2)
9169 hi2 = lo2 - 1;
9170
9171 return (hi1 - lo1 == hi2 - lo2);
9172}
9173
9174/* Assuming that VAL is an array of integrals, and TYPE represents
9175 an array with the same number of elements, but with wider integral
9176 elements, return an array "casted" to TYPE. In practice, this
9177 means that the returned array is built by casting each element
9178 of the original array into TYPE's (wider) element type. */
9179
9180static struct value *
9181ada_promote_array_of_integrals (struct type *type, struct value *val)
9182{
27710edb 9183 struct type *elt_type = type->target_type ();
d99dcf51 9184 LONGEST lo, hi;
d99dcf51
JB
9185 LONGEST i;
9186
9187 /* Verify that both val and type are arrays of scalars, and
9188 that the size of val's elements is smaller than the size
9189 of type's element. */
78134374 9190 gdb_assert (type->code () == TYPE_CODE_ARRAY);
27710edb 9191 gdb_assert (is_integral_type (type->target_type ()));
d0c97917
TT
9192 gdb_assert (val->type ()->code () == TYPE_CODE_ARRAY);
9193 gdb_assert (is_integral_type (val->type ()->target_type ()));
df86565b 9194 gdb_assert (type->target_type ()->length ()
d0c97917 9195 > val->type ()->target_type ()->length ());
d99dcf51
JB
9196
9197 if (!get_array_bounds (type, &lo, &hi))
9198 error (_("unable to determine array bounds"));
9199
317c3ed9 9200 value *res = value::allocate (type);
bbe912ba 9201 gdb::array_view<gdb_byte> res_contents = res->contents_writeable ();
d99dcf51
JB
9202
9203 /* Promote each array element. */
9204 for (i = 0; i < hi - lo + 1; i++)
9205 {
9206 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
df86565b 9207 int elt_len = elt_type->length ();
d99dcf51 9208
efaf1ae0 9209 copy (elt->contents_all (), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9210 }
9211
9212 return res;
9213}
9214
4c4b4cd2
PH
9215/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9216 return the converted value. */
9217
d2e4a39e
AS
9218static struct value *
9219coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9220{
d0c97917 9221 struct type *type2 = val->type ();
5b4ee69b 9222
14f9c5c9
AS
9223 if (type == type2)
9224 return val;
9225
61ee279c
PH
9226 type2 = ada_check_typedef (type2);
9227 type = ada_check_typedef (type);
14f9c5c9 9228
78134374
SM
9229 if (type2->code () == TYPE_CODE_PTR
9230 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9231 {
9232 val = ada_value_ind (val);
d0c97917 9233 type2 = val->type ();
14f9c5c9
AS
9234 }
9235
78134374
SM
9236 if (type2->code () == TYPE_CODE_ARRAY
9237 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9238 {
d99dcf51
JB
9239 if (!ada_same_array_size_p (type, type2))
9240 error (_("cannot assign arrays of different length"));
9241
27710edb
SM
9242 if (is_integral_type (type->target_type ())
9243 && is_integral_type (type2->target_type ())
df86565b 9244 && type2->target_type ()->length () < type->target_type ()->length ())
d99dcf51
JB
9245 {
9246 /* Allow implicit promotion of the array elements to
9247 a wider type. */
9248 return ada_promote_array_of_integrals (type, val);
9249 }
9250
df86565b 9251 if (type2->target_type ()->length () != type->target_type ()->length ())
dda83cd7 9252 error (_("Incompatible types in assignment"));
81ae560c 9253 val->deprecated_set_type (type);
14f9c5c9 9254 }
d2e4a39e 9255 return val;
14f9c5c9
AS
9256}
9257
4c4b4cd2
PH
9258static struct value *
9259ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9260{
4c4b4cd2 9261 struct type *type1, *type2;
4c4b4cd2 9262
994b9211
AC
9263 arg1 = coerce_ref (arg1);
9264 arg2 = coerce_ref (arg2);
d0c97917
TT
9265 type1 = get_base_type (ada_check_typedef (arg1->type ()));
9266 type2 = get_base_type (ada_check_typedef (arg2->type ()));
4c4b4cd2 9267
78134374
SM
9268 if (type1->code () != TYPE_CODE_INT
9269 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9270 return value_binop (arg1, arg2, op);
9271
76a01679 9272 switch (op)
4c4b4cd2
PH
9273 {
9274 case BINOP_MOD:
9275 case BINOP_DIV:
9276 case BINOP_REM:
9277 break;
9278 default:
9279 return value_binop (arg1, arg2, op);
9280 }
9281
70050808
TT
9282 gdb_mpz v2 = value_as_mpz (arg2);
9283 if (v2.sgn () == 0)
b0f9164c
TT
9284 {
9285 const char *name;
9286 if (op == BINOP_MOD)
9287 name = "mod";
9288 else if (op == BINOP_DIV)
9289 name = "/";
9290 else
9291 {
9292 gdb_assert (op == BINOP_REM);
9293 name = "rem";
9294 }
9295
9296 error (_("second operand of %s must not be zero."), name);
9297 }
4c4b4cd2 9298
c6d940a9 9299 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9300 return value_binop (arg1, arg2, op);
9301
70050808
TT
9302 gdb_mpz v1 = value_as_mpz (arg1);
9303 gdb_mpz v;
4c4b4cd2
PH
9304 switch (op)
9305 {
9306 case BINOP_DIV:
9307 v = v1 / v2;
4c4b4cd2
PH
9308 break;
9309 case BINOP_REM:
9310 v = v1 % v2;
76a01679 9311 if (v * v1 < 0)
dda83cd7 9312 v -= v2;
4c4b4cd2
PH
9313 break;
9314 default:
9315 /* Should not reach this point. */
70050808 9316 gdb_assert_not_reached ("invalid operator");
4c4b4cd2
PH
9317 }
9318
70050808 9319 return value_from_mpz (type1, v);
4c4b4cd2
PH
9320}
9321
9322static int
9323ada_value_equal (struct value *arg1, struct value *arg2)
9324{
d0c97917
TT
9325 if (ada_is_direct_array_type (arg1->type ())
9326 || ada_is_direct_array_type (arg2->type ()))
4c4b4cd2 9327 {
79e8fcaa
JB
9328 struct type *arg1_type, *arg2_type;
9329
f58b38bf 9330 /* Automatically dereference any array reference before
dda83cd7 9331 we attempt to perform the comparison. */
f58b38bf
JB
9332 arg1 = ada_coerce_ref (arg1);
9333 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9334
4c4b4cd2
PH
9335 arg1 = ada_coerce_to_simple_array (arg1);
9336 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa 9337
d0c97917
TT
9338 arg1_type = ada_check_typedef (arg1->type ());
9339 arg2_type = ada_check_typedef (arg2->type ());
79e8fcaa 9340
78134374 9341 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9342 || arg2_type->code () != TYPE_CODE_ARRAY)
9343 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9344 /* FIXME: The following works only for types whose
dda83cd7
SM
9345 representations use all bits (no padding or undefined bits)
9346 and do not have user-defined equality. */
df86565b 9347 return (arg1_type->length () == arg2_type->length ()
efaf1ae0
TT
9348 && memcmp (arg1->contents ().data (),
9349 arg2->contents ().data (),
df86565b 9350 arg1_type->length ()) == 0);
4c4b4cd2
PH
9351 }
9352 return value_equal (arg1, arg2);
9353}
9354
d3c54a1c
TT
9355namespace expr
9356{
9357
9358bool
9359check_objfile (const std::unique_ptr<ada_component> &comp,
9360 struct objfile *objfile)
9361{
9362 return comp->uses_objfile (objfile);
9363}
9364
9365/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9366 component of LHS (a simple array or a record). Does not modify the
9367 inferior's memory, nor does it modify LHS (unless LHS ==
9368 CONTAINER). */
52ce6436
PH
9369
9370static void
9371assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9372 struct expression *exp, operation_up &arg)
52ce6436 9373{
d3c54a1c
TT
9374 scoped_value_mark mark;
9375
52ce6436 9376 struct value *elt;
d0c97917 9377 struct type *lhs_type = check_typedef (lhs->type ());
5b4ee69b 9378
78134374 9379 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9380 {
22601c15
UW
9381 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9382 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9383
52ce6436
PH
9384 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9385 }
9386 else
9387 {
d0c97917 9388 elt = ada_index_struct_field (index, lhs, 0, lhs->type ());
c48db5ca 9389 elt = ada_to_fixed_value (elt);
52ce6436
PH
9390 }
9391
d3c54a1c
TT
9392 ada_aggregate_operation *ag_op
9393 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9394 if (ag_op != nullptr)
9395 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9396 else
d3c54a1c
TT
9397 value_assign_to_component (container, elt,
9398 arg->evaluate (nullptr, exp,
9399 EVAL_NORMAL));
9400}
52ce6436 9401
d3c54a1c
TT
9402bool
9403ada_aggregate_component::uses_objfile (struct objfile *objfile)
9404{
9405 for (const auto &item : m_components)
9406 if (item->uses_objfile (objfile))
9407 return true;
9408 return false;
9409}
9410
9411void
9412ada_aggregate_component::dump (ui_file *stream, int depth)
9413{
6cb06a8c 9414 gdb_printf (stream, _("%*sAggregate\n"), depth, "");
d3c54a1c
TT
9415 for (const auto &item : m_components)
9416 item->dump (stream, depth + 1);
9417}
9418
9419void
9420ada_aggregate_component::assign (struct value *container,
9421 struct value *lhs, struct expression *exp,
9422 std::vector<LONGEST> &indices,
9423 LONGEST low, LONGEST high)
9424{
9425 for (auto &item : m_components)
9426 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9427}
9428
207582c0 9429/* See ada-exp.h. */
52ce6436 9430
207582c0 9431value *
d3c54a1c
TT
9432ada_aggregate_operation::assign_aggregate (struct value *container,
9433 struct value *lhs,
9434 struct expression *exp)
52ce6436
PH
9435{
9436 struct type *lhs_type;
52ce6436 9437 LONGEST low_index, high_index;
52ce6436
PH
9438
9439 container = ada_coerce_ref (container);
d0c97917 9440 if (ada_is_direct_array_type (container->type ()))
52ce6436
PH
9441 container = ada_coerce_to_simple_array (container);
9442 lhs = ada_coerce_ref (lhs);
4b53ca88 9443 if (!lhs->deprecated_modifiable ())
52ce6436
PH
9444 error (_("Left operand of assignment is not a modifiable lvalue."));
9445
d0c97917 9446 lhs_type = check_typedef (lhs->type ());
52ce6436
PH
9447 if (ada_is_direct_array_type (lhs_type))
9448 {
9449 lhs = ada_coerce_to_simple_array (lhs);
d0c97917 9450 lhs_type = check_typedef (lhs->type ());
cf88be68
SM
9451 low_index = lhs_type->bounds ()->low.const_val ();
9452 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9453 }
78134374 9454 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9455 {
9456 low_index = 0;
9457 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9458 }
9459 else
9460 error (_("Left-hand side must be array or record."));
9461
cf608cc4 9462 std::vector<LONGEST> indices (4);
52ce6436
PH
9463 indices[0] = indices[1] = low_index - 1;
9464 indices[2] = indices[3] = high_index + 1;
52ce6436 9465
d3c54a1c
TT
9466 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9467 low_index, high_index);
207582c0
TT
9468
9469 return container;
d3c54a1c
TT
9470}
9471
9472bool
9473ada_positional_component::uses_objfile (struct objfile *objfile)
9474{
9475 return m_op->uses_objfile (objfile);
9476}
52ce6436 9477
d3c54a1c
TT
9478void
9479ada_positional_component::dump (ui_file *stream, int depth)
9480{
6cb06a8c
TT
9481 gdb_printf (stream, _("%*sPositional, index = %d\n"),
9482 depth, "", m_index);
d3c54a1c 9483 m_op->dump (stream, depth + 1);
52ce6436 9484}
d3c54a1c 9485
52ce6436 9486/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9487 construct, given that the positions are relative to lower bound
9488 LOW, where HIGH is the upper bound. Record the position in
9489 INDICES. CONTAINER is as for assign_aggregate. */
9490void
9491ada_positional_component::assign (struct value *container,
9492 struct value *lhs, struct expression *exp,
9493 std::vector<LONGEST> &indices,
9494 LONGEST low, LONGEST high)
52ce6436 9495{
d3c54a1c
TT
9496 LONGEST ind = m_index + low;
9497
52ce6436 9498 if (ind - 1 == high)
e1d5a0d2 9499 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9500 if (ind <= high)
9501 {
cf608cc4 9502 add_component_interval (ind, ind, indices);
d3c54a1c 9503 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9504 }
52ce6436
PH
9505}
9506
d3c54a1c
TT
9507bool
9508ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9509{
9510 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9511}
9512
9513void
9514ada_discrete_range_association::dump (ui_file *stream, int depth)
9515{
6cb06a8c 9516 gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
a88c4354
TT
9517 m_low->dump (stream, depth + 1);
9518 m_high->dump (stream, depth + 1);
9519}
9520
9521void
9522ada_discrete_range_association::assign (struct value *container,
9523 struct value *lhs,
9524 struct expression *exp,
9525 std::vector<LONGEST> &indices,
9526 LONGEST low, LONGEST high,
9527 operation_up &op)
9528{
9529 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9530 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9531
9532 if (lower <= upper && (lower < low || upper > high))
9533 error (_("Index in component association out of bounds."));
9534
9535 add_component_interval (lower, upper, indices);
9536 while (lower <= upper)
9537 {
9538 assign_component (container, lhs, lower, exp, op);
9539 lower += 1;
9540 }
9541}
9542
9543bool
9544ada_name_association::uses_objfile (struct objfile *objfile)
9545{
9546 return m_val->uses_objfile (objfile);
9547}
9548
9549void
9550ada_name_association::dump (ui_file *stream, int depth)
9551{
6cb06a8c 9552 gdb_printf (stream, _("%*sName:\n"), depth, "");
a88c4354
TT
9553 m_val->dump (stream, depth + 1);
9554}
9555
9556void
9557ada_name_association::assign (struct value *container,
9558 struct value *lhs,
9559 struct expression *exp,
9560 std::vector<LONGEST> &indices,
9561 LONGEST low, LONGEST high,
9562 operation_up &op)
9563{
9564 int index;
9565
d0c97917 9566 if (ada_is_direct_array_type (lhs->type ()))
a88c4354
TT
9567 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9568 EVAL_NORMAL)));
9569 else
9570 {
9571 ada_string_operation *strop
9572 = dynamic_cast<ada_string_operation *> (m_val.get ());
9573
9574 const char *name;
9575 if (strop != nullptr)
9576 name = strop->get_name ();
9577 else
9578 {
9579 ada_var_value_operation *vvo
9580 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
94c5098e 9581 if (vvo == nullptr)
a88c4354
TT
9582 error (_("Invalid record component association."));
9583 name = vvo->get_symbol ()->natural_name ();
94c5098e
TT
9584 /* In this scenario, the user wrote (name => expr), but
9585 write_name_assoc found some fully-qualified name and
9586 substituted it. This happens because, at parse time, the
9587 meaning of the expression isn't known; but here we know
9588 that just the base name was supplied and it refers to the
9589 name of a field. */
9590 name = ada_unqualified_name (name);
a88c4354
TT
9591 }
9592
9593 index = 0;
d0c97917 9594 if (! find_struct_field (name, lhs->type (), 0,
a88c4354
TT
9595 NULL, NULL, NULL, NULL, &index))
9596 error (_("Unknown component name: %s."), name);
9597 }
9598
9599 add_component_interval (index, index, indices);
9600 assign_component (container, lhs, index, exp, op);
9601}
9602
9603bool
9604ada_choices_component::uses_objfile (struct objfile *objfile)
9605{
9606 if (m_op->uses_objfile (objfile))
9607 return true;
9608 for (const auto &item : m_assocs)
9609 if (item->uses_objfile (objfile))
9610 return true;
9611 return false;
9612}
9613
9614void
9615ada_choices_component::dump (ui_file *stream, int depth)
9616{
6cb06a8c 9617 gdb_printf (stream, _("%*sChoices:\n"), depth, "");
a88c4354
TT
9618 m_op->dump (stream, depth + 1);
9619 for (const auto &item : m_assocs)
9620 item->dump (stream, depth + 1);
9621}
9622
9623/* Assign into the components of LHS indexed by the OP_CHOICES
9624 construct at *POS, updating *POS past the construct, given that
9625 the allowable indices are LOW..HIGH. Record the indices assigned
9626 to in INDICES. CONTAINER is as for assign_aggregate. */
9627void
9628ada_choices_component::assign (struct value *container,
9629 struct value *lhs, struct expression *exp,
9630 std::vector<LONGEST> &indices,
9631 LONGEST low, LONGEST high)
9632{
9633 for (auto &item : m_assocs)
9634 item->assign (container, lhs, exp, indices, low, high, m_op);
9635}
9636
9637bool
9638ada_others_component::uses_objfile (struct objfile *objfile)
9639{
9640 return m_op->uses_objfile (objfile);
9641}
9642
9643void
9644ada_others_component::dump (ui_file *stream, int depth)
9645{
6cb06a8c 9646 gdb_printf (stream, _("%*sOthers:\n"), depth, "");
a88c4354
TT
9647 m_op->dump (stream, depth + 1);
9648}
9649
9650/* Assign the value of the expression in the OP_OTHERS construct in
9651 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9652 have not been previously assigned. The index intervals already assigned
9653 are in INDICES. CONTAINER is as for assign_aggregate. */
9654void
9655ada_others_component::assign (struct value *container,
9656 struct value *lhs, struct expression *exp,
9657 std::vector<LONGEST> &indices,
9658 LONGEST low, LONGEST high)
9659{
9660 int num_indices = indices.size ();
9661 for (int i = 0; i < num_indices - 2; i += 2)
9662 {
9663 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9664 assign_component (container, lhs, ind, exp, m_op);
9665 }
9666}
9667
9668struct value *
9669ada_assign_operation::evaluate (struct type *expect_type,
9670 struct expression *exp,
9671 enum noside noside)
9672{
9673 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
b3a27d2f 9674 scoped_restore save_lhs = make_scoped_restore (&m_current, arg1);
a88c4354
TT
9675
9676 ada_aggregate_operation *ag_op
9677 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9678 if (ag_op != nullptr)
9679 {
9680 if (noside != EVAL_NORMAL)
9681 return arg1;
9682
207582c0 9683 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9684 return ada_value_assign (arg1, arg1);
9685 }
9686 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9687 except if the lhs of our assignment is a convenience variable.
9688 In the case of assigning to a convenience variable, the lhs
9689 should be exactly the result of the evaluation of the rhs. */
d0c97917 9690 struct type *type = arg1->type ();
736355f2 9691 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9692 type = NULL;
9693 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9694 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354 9695 return arg1;
736355f2 9696 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9697 {
9698 /* Nothing. */
9699 }
9700 else
d0c97917 9701 arg2 = coerce_for_assign (arg1->type (), arg2);
a88c4354
TT
9702 return ada_value_assign (arg1, arg2);
9703}
9704
9705} /* namespace expr */
9706
cf608cc4
TT
9707/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9708 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9709 overlap. */
52ce6436
PH
9710static void
9711add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9712 std::vector<LONGEST> &indices)
52ce6436
PH
9713{
9714 int i, j;
5b4ee69b 9715
cf608cc4
TT
9716 int size = indices.size ();
9717 for (i = 0; i < size; i += 2) {
52ce6436
PH
9718 if (high >= indices[i] && low <= indices[i + 1])
9719 {
9720 int kh;
5b4ee69b 9721
cf608cc4 9722 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9723 if (high < indices[kh])
9724 break;
9725 if (low < indices[i])
9726 indices[i] = low;
9727 indices[i + 1] = indices[kh - 1];
9728 if (high > indices[i + 1])
9729 indices[i + 1] = high;
cf608cc4
TT
9730 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9731 indices.resize (kh - i - 2);
52ce6436
PH
9732 return;
9733 }
9734 else if (high < indices[i])
9735 break;
9736 }
9737
cf608cc4 9738 indices.resize (indices.size () + 2);
d4813f10 9739 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9740 indices[j] = indices[j - 2];
9741 indices[i] = low;
9742 indices[i + 1] = high;
9743}
9744
6e48bd2c
JB
9745/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9746 is different. */
9747
9748static struct value *
b7e22850 9749ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c 9750{
d0c97917 9751 if (type == ada_check_typedef (arg2->type ()))
6e48bd2c
JB
9752 return arg2;
9753
6e48bd2c
JB
9754 return value_cast (type, arg2);
9755}
9756
284614f0
JB
9757/* Evaluating Ada expressions, and printing their result.
9758 ------------------------------------------------------
9759
21649b50
JB
9760 1. Introduction:
9761 ----------------
9762
284614f0
JB
9763 We usually evaluate an Ada expression in order to print its value.
9764 We also evaluate an expression in order to print its type, which
9765 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9766 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9767 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9768 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9769 similar.
9770
9771 Evaluating expressions is a little more complicated for Ada entities
9772 than it is for entities in languages such as C. The main reason for
9773 this is that Ada provides types whose definition might be dynamic.
9774 One example of such types is variant records. Or another example
9775 would be an array whose bounds can only be known at run time.
9776
9777 The following description is a general guide as to what should be
9778 done (and what should NOT be done) in order to evaluate an expression
9779 involving such types, and when. This does not cover how the semantic
9780 information is encoded by GNAT as this is covered separatly. For the
9781 document used as the reference for the GNAT encoding, see exp_dbug.ads
9782 in the GNAT sources.
9783
9784 Ideally, we should embed each part of this description next to its
9785 associated code. Unfortunately, the amount of code is so vast right
9786 now that it's hard to see whether the code handling a particular
9787 situation might be duplicated or not. One day, when the code is
9788 cleaned up, this guide might become redundant with the comments
9789 inserted in the code, and we might want to remove it.
9790
21649b50
JB
9791 2. ``Fixing'' an Entity, the Simple Case:
9792 -----------------------------------------
9793
284614f0
JB
9794 When evaluating Ada expressions, the tricky issue is that they may
9795 reference entities whose type contents and size are not statically
9796 known. Consider for instance a variant record:
9797
9798 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9799 case Empty is
9800 when True => null;
9801 when False => Value : Integer;
9802 end case;
284614f0
JB
9803 end record;
9804 Yes : Rec := (Empty => False, Value => 1);
9805 No : Rec := (empty => True);
9806
9807 The size and contents of that record depends on the value of the
33b5899f 9808 discriminant (Rec.Empty). At this point, neither the debugging
284614f0
JB
9809 information nor the associated type structure in GDB are able to
9810 express such dynamic types. So what the debugger does is to create
9811 "fixed" versions of the type that applies to the specific object.
30baf67b 9812 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9813 which means creating its associated fixed type.
9814
9815 Example: when printing the value of variable "Yes" above, its fixed
9816 type would look like this:
9817
9818 type Rec is record
dda83cd7
SM
9819 Empty : Boolean;
9820 Value : Integer;
284614f0
JB
9821 end record;
9822
9823 On the other hand, if we printed the value of "No", its fixed type
9824 would become:
9825
9826 type Rec is record
dda83cd7 9827 Empty : Boolean;
284614f0
JB
9828 end record;
9829
9830 Things become a little more complicated when trying to fix an entity
9831 with a dynamic type that directly contains another dynamic type,
9832 such as an array of variant records, for instance. There are
9833 two possible cases: Arrays, and records.
9834
21649b50
JB
9835 3. ``Fixing'' Arrays:
9836 ---------------------
9837
9838 The type structure in GDB describes an array in terms of its bounds,
9839 and the type of its elements. By design, all elements in the array
9840 have the same type and we cannot represent an array of variant elements
9841 using the current type structure in GDB. When fixing an array,
9842 we cannot fix the array element, as we would potentially need one
9843 fixed type per element of the array. As a result, the best we can do
9844 when fixing an array is to produce an array whose bounds and size
9845 are correct (allowing us to read it from memory), but without having
9846 touched its element type. Fixing each element will be done later,
9847 when (if) necessary.
9848
9849 Arrays are a little simpler to handle than records, because the same
9850 amount of memory is allocated for each element of the array, even if
1b536f04 9851 the amount of space actually used by each element differs from element
21649b50 9852 to element. Consider for instance the following array of type Rec:
284614f0
JB
9853
9854 type Rec_Array is array (1 .. 2) of Rec;
9855
1b536f04
JB
9856 The actual amount of memory occupied by each element might be different
9857 from element to element, depending on the value of their discriminant.
21649b50 9858 But the amount of space reserved for each element in the array remains
1b536f04 9859 fixed regardless. So we simply need to compute that size using
21649b50
JB
9860 the debugging information available, from which we can then determine
9861 the array size (we multiply the number of elements of the array by
9862 the size of each element).
9863
9864 The simplest case is when we have an array of a constrained element
9865 type. For instance, consider the following type declarations:
9866
dda83cd7
SM
9867 type Bounded_String (Max_Size : Integer) is
9868 Length : Integer;
9869 Buffer : String (1 .. Max_Size);
9870 end record;
9871 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9872
9873 In this case, the compiler describes the array as an array of
9874 variable-size elements (identified by its XVS suffix) for which
9875 the size can be read in the parallel XVZ variable.
9876
9877 In the case of an array of an unconstrained element type, the compiler
9878 wraps the array element inside a private PAD type. This type should not
9879 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9880 that we also use the adjective "aligner" in our code to designate
9881 these wrapper types.
9882
1b536f04 9883 In some cases, the size allocated for each element is statically
21649b50
JB
9884 known. In that case, the PAD type already has the correct size,
9885 and the array element should remain unfixed.
9886
9887 But there are cases when this size is not statically known.
9888 For instance, assuming that "Five" is an integer variable:
284614f0 9889
dda83cd7
SM
9890 type Dynamic is array (1 .. Five) of Integer;
9891 type Wrapper (Has_Length : Boolean := False) is record
9892 Data : Dynamic;
9893 case Has_Length is
9894 when True => Length : Integer;
9895 when False => null;
9896 end case;
9897 end record;
9898 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9899
dda83cd7
SM
9900 Hello : Wrapper_Array := (others => (Has_Length => True,
9901 Data => (others => 17),
9902 Length => 1));
284614f0
JB
9903
9904
9905 The debugging info would describe variable Hello as being an
9906 array of a PAD type. The size of that PAD type is not statically
9907 known, but can be determined using a parallel XVZ variable.
9908 In that case, a copy of the PAD type with the correct size should
9909 be used for the fixed array.
9910
21649b50
JB
9911 3. ``Fixing'' record type objects:
9912 ----------------------------------
9913
9914 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9915 record types. In this case, in order to compute the associated
9916 fixed type, we need to determine the size and offset of each of
9917 its components. This, in turn, requires us to compute the fixed
9918 type of each of these components.
9919
9920 Consider for instance the example:
9921
dda83cd7
SM
9922 type Bounded_String (Max_Size : Natural) is record
9923 Str : String (1 .. Max_Size);
9924 Length : Natural;
9925 end record;
9926 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9927
9928 In that case, the position of field "Length" depends on the size
9929 of field Str, which itself depends on the value of the Max_Size
21649b50 9930 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9931 we need to fix the type of field Str. Therefore, fixing a variant
9932 record requires us to fix each of its components.
9933
9934 However, if a component does not have a dynamic size, the component
9935 should not be fixed. In particular, fields that use a PAD type
9936 should not fixed. Here is an example where this might happen
9937 (assuming type Rec above):
9938
9939 type Container (Big : Boolean) is record
dda83cd7
SM
9940 First : Rec;
9941 After : Integer;
9942 case Big is
9943 when True => Another : Integer;
9944 when False => null;
9945 end case;
284614f0
JB
9946 end record;
9947 My_Container : Container := (Big => False,
dda83cd7
SM
9948 First => (Empty => True),
9949 After => 42);
284614f0
JB
9950
9951 In that example, the compiler creates a PAD type for component First,
9952 whose size is constant, and then positions the component After just
9953 right after it. The offset of component After is therefore constant
9954 in this case.
9955
9956 The debugger computes the position of each field based on an algorithm
9957 that uses, among other things, the actual position and size of the field
21649b50
JB
9958 preceding it. Let's now imagine that the user is trying to print
9959 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
9960 end up computing the offset of field After based on the size of the
9961 fixed version of field First. And since in our example First has
9962 only one actual field, the size of the fixed type is actually smaller
9963 than the amount of space allocated to that field, and thus we would
9964 compute the wrong offset of field After.
9965
21649b50
JB
9966 To make things more complicated, we need to watch out for dynamic
9967 components of variant records (identified by the ___XVL suffix in
9968 the component name). Even if the target type is a PAD type, the size
9969 of that type might not be statically known. So the PAD type needs
9970 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9971 we might end up with the wrong size for our component. This can be
9972 observed with the following type declarations:
284614f0 9973
dda83cd7
SM
9974 type Octal is new Integer range 0 .. 7;
9975 type Octal_Array is array (Positive range <>) of Octal;
9976 pragma Pack (Octal_Array);
284614f0 9977
dda83cd7
SM
9978 type Octal_Buffer (Size : Positive) is record
9979 Buffer : Octal_Array (1 .. Size);
9980 Length : Integer;
9981 end record;
284614f0
JB
9982
9983 In that case, Buffer is a PAD type whose size is unset and needs
9984 to be computed by fixing the unwrapped type.
9985
21649b50
JB
9986 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9987 ----------------------------------------------------------
9988
9989 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
9990 thus far, be actually fixed?
9991
9992 The answer is: Only when referencing that element. For instance
9993 when selecting one component of a record, this specific component
9994 should be fixed at that point in time. Or when printing the value
9995 of a record, each component should be fixed before its value gets
9996 printed. Similarly for arrays, the element of the array should be
9997 fixed when printing each element of the array, or when extracting
9998 one element out of that array. On the other hand, fixing should
9999 not be performed on the elements when taking a slice of an array!
10000
31432a67 10001 Note that one of the side effects of miscomputing the offset and
284614f0
JB
10002 size of each field is that we end up also miscomputing the size
10003 of the containing type. This can have adverse results when computing
10004 the value of an entity. GDB fetches the value of an entity based
10005 on the size of its type, and thus a wrong size causes GDB to fetch
10006 the wrong amount of memory. In the case where the computed size is
10007 too small, GDB fetches too little data to print the value of our
31432a67 10008 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
10009 past the buffer containing the data =:-o. */
10010
62d4bd94
TT
10011/* A helper function for TERNOP_IN_RANGE. */
10012
10013static value *
10014eval_ternop_in_range (struct type *expect_type, struct expression *exp,
10015 enum noside noside,
10016 value *arg1, value *arg2, value *arg3)
10017{
62d4bd94
TT
10018 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10019 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10020 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10021 return
10022 value_from_longest (type,
10023 (value_less (arg1, arg3)
10024 || value_equal (arg1, arg3))
10025 && (value_less (arg2, arg1)
10026 || value_equal (arg2, arg1)));
10027}
10028
82390ab8
TT
10029/* A helper function for UNOP_NEG. */
10030
7c15d377 10031value *
82390ab8
TT
10032ada_unop_neg (struct type *expect_type,
10033 struct expression *exp,
10034 enum noside noside, enum exp_opcode op,
10035 struct value *arg1)
10036{
82390ab8
TT
10037 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10038 return value_neg (arg1);
10039}
10040
7efc87ff
TT
10041/* A helper function for UNOP_IN_RANGE. */
10042
95d49dfb 10043value *
7efc87ff
TT
10044ada_unop_in_range (struct type *expect_type,
10045 struct expression *exp,
10046 enum noside noside, enum exp_opcode op,
10047 struct value *arg1, struct type *type)
10048{
7efc87ff
TT
10049 struct value *arg2, *arg3;
10050 switch (type->code ())
10051 {
10052 default:
10053 lim_warning (_("Membership test incompletely implemented; "
10054 "always returns true"));
10055 type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10056 return value_from_longest (type, 1);
7efc87ff
TT
10057
10058 case TYPE_CODE_RANGE:
10059 arg2 = value_from_longest (type,
10060 type->bounds ()->low.const_val ());
10061 arg3 = value_from_longest (type,
10062 type->bounds ()->high.const_val ());
10063 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10064 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10065 type = language_bool_type (exp->language_defn, exp->gdbarch);
10066 return
10067 value_from_longest (type,
10068 (value_less (arg1, arg3)
10069 || value_equal (arg1, arg3))
10070 && (value_less (arg2, arg1)
10071 || value_equal (arg2, arg1)));
10072 }
10073}
10074
020dbabe
TT
10075/* A helper function for OP_ATR_TAG. */
10076
7c15d377 10077value *
020dbabe
TT
10078ada_atr_tag (struct type *expect_type,
10079 struct expression *exp,
10080 enum noside noside, enum exp_opcode op,
10081 struct value *arg1)
10082{
10083 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10084 return value::zero (ada_tag_type (arg1), not_lval);
020dbabe
TT
10085
10086 return ada_value_tag (arg1);
10087}
10088
68c75735
TT
10089/* A helper function for OP_ATR_SIZE. */
10090
7c15d377 10091value *
68c75735
TT
10092ada_atr_size (struct type *expect_type,
10093 struct expression *exp,
10094 enum noside noside, enum exp_opcode op,
10095 struct value *arg1)
10096{
d0c97917 10097 struct type *type = arg1->type ();
68c75735
TT
10098
10099 /* If the argument is a reference, then dereference its type, since
10100 the user is really asking for the size of the actual object,
10101 not the size of the pointer. */
10102 if (type->code () == TYPE_CODE_REF)
27710edb 10103 type = type->target_type ();
68c75735 10104
0b2b0b82 10105 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10106 return value::zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
68c75735
TT
10107 else
10108 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
df86565b 10109 TARGET_CHAR_BIT * type->length ());
68c75735
TT
10110}
10111
d05e24e6
TT
10112/* A helper function for UNOP_ABS. */
10113
7c15d377 10114value *
d05e24e6
TT
10115ada_abs (struct type *expect_type,
10116 struct expression *exp,
10117 enum noside noside, enum exp_opcode op,
10118 struct value *arg1)
10119{
10120 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
ee7bb294 10121 if (value_less (arg1, value::zero (arg1->type (), not_lval)))
d05e24e6
TT
10122 return value_neg (arg1);
10123 else
10124 return arg1;
10125}
10126
faa1dfd7
TT
10127/* A helper function for BINOP_MUL. */
10128
d9e7db06 10129value *
faa1dfd7
TT
10130ada_mult_binop (struct type *expect_type,
10131 struct expression *exp,
10132 enum noside noside, enum exp_opcode op,
10133 struct value *arg1, struct value *arg2)
10134{
10135 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10136 {
10137 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
ee7bb294 10138 return value::zero (arg1->type (), not_lval);
faa1dfd7
TT
10139 }
10140 else
10141 {
10142 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10143 return ada_value_binop (arg1, arg2, op);
10144 }
10145}
10146
214b13ac
TT
10147/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10148
6e8fb7b7 10149value *
214b13ac
TT
10150ada_equal_binop (struct type *expect_type,
10151 struct expression *exp,
10152 enum noside noside, enum exp_opcode op,
10153 struct value *arg1, struct value *arg2)
10154{
10155 int tem;
10156 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10157 tem = 0;
10158 else
10159 {
10160 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10161 tem = ada_value_equal (arg1, arg2);
10162 }
10163 if (op == BINOP_NOTEQUAL)
10164 tem = !tem;
10165 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10166 return value_from_longest (type, tem);
214b13ac
TT
10167}
10168
5ce19db8
TT
10169/* A helper function for TERNOP_SLICE. */
10170
1b1ebfab 10171value *
5ce19db8
TT
10172ada_ternop_slice (struct expression *exp,
10173 enum noside noside,
10174 struct value *array, struct value *low_bound_val,
10175 struct value *high_bound_val)
10176{
10177 LONGEST low_bound;
10178 LONGEST high_bound;
10179
10180 low_bound_val = coerce_ref (low_bound_val);
10181 high_bound_val = coerce_ref (high_bound_val);
10182 low_bound = value_as_long (low_bound_val);
10183 high_bound = value_as_long (high_bound_val);
10184
10185 /* If this is a reference to an aligner type, then remove all
10186 the aligners. */
d0c97917
TT
10187 if (array->type ()->code () == TYPE_CODE_REF
10188 && ada_is_aligner_type (array->type ()->target_type ()))
10189 array->type ()->set_target_type
10190 (ada_aligned_type (array->type ()->target_type ()));
5ce19db8 10191
d0c97917 10192 if (ada_is_any_packed_array_type (array->type ()))
5ce19db8
TT
10193 error (_("cannot slice a packed array"));
10194
10195 /* If this is a reference to an array or an array lvalue,
10196 convert to a pointer. */
d0c97917
TT
10197 if (array->type ()->code () == TYPE_CODE_REF
10198 || (array->type ()->code () == TYPE_CODE_ARRAY
736355f2 10199 && array->lval () == lval_memory))
5ce19db8
TT
10200 array = value_addr (array);
10201
10202 if (noside == EVAL_AVOID_SIDE_EFFECTS
10203 && ada_is_array_descriptor_type (ada_check_typedef
d0c97917 10204 (array->type ())))
5ce19db8
TT
10205 return empty_array (ada_type_of_array (array, 0), low_bound,
10206 high_bound);
10207
10208 array = ada_coerce_to_simple_array_ptr (array);
10209
10210 /* If we have more than one level of pointer indirection,
10211 dereference the value until we get only one level. */
d0c97917
TT
10212 while (array->type ()->code () == TYPE_CODE_PTR
10213 && (array->type ()->target_type ()->code ()
5ce19db8
TT
10214 == TYPE_CODE_PTR))
10215 array = value_ind (array);
10216
10217 /* Make sure we really do have an array type before going further,
10218 to avoid a SEGV when trying to get the index type or the target
10219 type later down the road if the debug info generated by
10220 the compiler is incorrect or incomplete. */
d0c97917 10221 if (!ada_is_simple_array_type (array->type ()))
5ce19db8
TT
10222 error (_("cannot take slice of non-array"));
10223
d0c97917 10224 if (ada_check_typedef (array->type ())->code ()
5ce19db8
TT
10225 == TYPE_CODE_PTR)
10226 {
d0c97917 10227 struct type *type0 = ada_check_typedef (array->type ());
5ce19db8
TT
10228
10229 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
27710edb 10230 return empty_array (type0->target_type (), low_bound, high_bound);
5ce19db8
TT
10231 else
10232 {
10233 struct type *arr_type0 =
27710edb 10234 to_fixed_array_type (type0->target_type (), NULL, 1);
5ce19db8
TT
10235
10236 return ada_value_slice_from_ptr (array, arr_type0,
10237 longest_to_int (low_bound),
10238 longest_to_int (high_bound));
10239 }
10240 }
10241 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10242 return array;
10243 else if (high_bound < low_bound)
d0c97917 10244 return empty_array (array->type (), low_bound, high_bound);
5ce19db8
TT
10245 else
10246 return ada_value_slice (array, longest_to_int (low_bound),
10247 longest_to_int (high_bound));
10248}
10249
b467efaa
TT
10250/* A helper function for BINOP_IN_BOUNDS. */
10251
82c3886e 10252value *
b467efaa
TT
10253ada_binop_in_bounds (struct expression *exp, enum noside noside,
10254 struct value *arg1, struct value *arg2, int n)
10255{
10256 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10257 {
10258 struct type *type = language_bool_type (exp->language_defn,
10259 exp->gdbarch);
ee7bb294 10260 return value::zero (type, not_lval);
b467efaa
TT
10261 }
10262
d0c97917 10263 struct type *type = ada_index_type (arg2->type (), n, "range");
b467efaa 10264 if (!type)
d0c97917 10265 type = arg1->type ();
b467efaa
TT
10266
10267 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10268 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10269
10270 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10271 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10272 type = language_bool_type (exp->language_defn, exp->gdbarch);
10273 return value_from_longest (type,
10274 (value_less (arg1, arg3)
10275 || value_equal (arg1, arg3))
10276 && (value_less (arg2, arg1)
10277 || value_equal (arg2, arg1)));
10278}
10279
b84564fc
TT
10280/* A helper function for some attribute operations. */
10281
10282static value *
10283ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10284 struct value *arg1, struct type *type_arg, int tem)
10285{
1e5ae3d1
TT
10286 const char *attr_name = nullptr;
10287 if (op == OP_ATR_FIRST)
10288 attr_name = "first";
10289 else if (op == OP_ATR_LAST)
10290 attr_name = "last";
10291
b84564fc
TT
10292 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10293 {
10294 if (type_arg == NULL)
d0c97917 10295 type_arg = arg1->type ();
b84564fc
TT
10296
10297 if (ada_is_constrained_packed_array_type (type_arg))
10298 type_arg = decode_constrained_packed_array_type (type_arg);
10299
10300 if (!discrete_type_p (type_arg))
10301 {
10302 switch (op)
10303 {
10304 default: /* Should never happen. */
10305 error (_("unexpected attribute encountered"));
10306 case OP_ATR_FIRST:
10307 case OP_ATR_LAST:
10308 type_arg = ada_index_type (type_arg, tem,
1e5ae3d1 10309 attr_name);
b84564fc
TT
10310 break;
10311 case OP_ATR_LENGTH:
10312 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10313 break;
10314 }
10315 }
10316
ee7bb294 10317 return value::zero (type_arg, not_lval);
b84564fc
TT
10318 }
10319 else if (type_arg == NULL)
10320 {
10321 arg1 = ada_coerce_ref (arg1);
10322
d0c97917 10323 if (ada_is_constrained_packed_array_type (arg1->type ()))
b84564fc
TT
10324 arg1 = ada_coerce_to_simple_array (arg1);
10325
10326 struct type *type;
10327 if (op == OP_ATR_LENGTH)
10328 type = builtin_type (exp->gdbarch)->builtin_int;
10329 else
10330 {
d0c97917 10331 type = ada_index_type (arg1->type (), tem,
1e5ae3d1 10332 attr_name);
b84564fc
TT
10333 if (type == NULL)
10334 type = builtin_type (exp->gdbarch)->builtin_int;
10335 }
10336
10337 switch (op)
10338 {
10339 default: /* Should never happen. */
10340 error (_("unexpected attribute encountered"));
10341 case OP_ATR_FIRST:
10342 return value_from_longest
10343 (type, ada_array_bound (arg1, tem, 0));
10344 case OP_ATR_LAST:
10345 return value_from_longest
10346 (type, ada_array_bound (arg1, tem, 1));
10347 case OP_ATR_LENGTH:
10348 return value_from_longest
10349 (type, ada_array_length (arg1, tem));
10350 }
10351 }
10352 else if (discrete_type_p (type_arg))
10353 {
10354 struct type *range_type;
10355 const char *name = ada_type_name (type_arg);
10356
10357 range_type = NULL;
10358 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10359 range_type = to_fixed_range_type (type_arg, NULL);
10360 if (range_type == NULL)
10361 range_type = type_arg;
10362 switch (op)
10363 {
10364 default:
10365 error (_("unexpected attribute encountered"));
10366 case OP_ATR_FIRST:
10367 return value_from_longest
10368 (range_type, ada_discrete_type_low_bound (range_type));
10369 case OP_ATR_LAST:
10370 return value_from_longest
10371 (range_type, ada_discrete_type_high_bound (range_type));
10372 case OP_ATR_LENGTH:
10373 error (_("the 'length attribute applies only to array types"));
10374 }
10375 }
10376 else if (type_arg->code () == TYPE_CODE_FLT)
10377 error (_("unimplemented type attribute"));
10378 else
10379 {
10380 LONGEST low, high;
10381
10382 if (ada_is_constrained_packed_array_type (type_arg))
10383 type_arg = decode_constrained_packed_array_type (type_arg);
10384
10385 struct type *type;
10386 if (op == OP_ATR_LENGTH)
10387 type = builtin_type (exp->gdbarch)->builtin_int;
10388 else
10389 {
1e5ae3d1 10390 type = ada_index_type (type_arg, tem, attr_name);
b84564fc
TT
10391 if (type == NULL)
10392 type = builtin_type (exp->gdbarch)->builtin_int;
10393 }
10394
10395 switch (op)
10396 {
10397 default:
10398 error (_("unexpected attribute encountered"));
10399 case OP_ATR_FIRST:
10400 low = ada_array_bound_from_type (type_arg, tem, 0);
10401 return value_from_longest (type, low);
10402 case OP_ATR_LAST:
10403 high = ada_array_bound_from_type (type_arg, tem, 1);
10404 return value_from_longest (type, high);
10405 case OP_ATR_LENGTH:
10406 low = ada_array_bound_from_type (type_arg, tem, 0);
10407 high = ada_array_bound_from_type (type_arg, tem, 1);
10408 return value_from_longest (type, high - low + 1);
10409 }
10410 }
10411}
10412
38dc70cf
TT
10413/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10414
6ad3b8bf 10415struct value *
38dc70cf
TT
10416ada_binop_minmax (struct type *expect_type,
10417 struct expression *exp,
10418 enum noside noside, enum exp_opcode op,
10419 struct value *arg1, struct value *arg2)
10420{
10421 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10422 return value::zero (arg1->type (), not_lval);
38dc70cf
TT
10423 else
10424 {
10425 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10426 return value_binop (arg1, arg2, op);
38dc70cf
TT
10427 }
10428}
10429
dd5fd283
TT
10430/* A helper function for BINOP_EXP. */
10431
065ec826 10432struct value *
dd5fd283
TT
10433ada_binop_exp (struct type *expect_type,
10434 struct expression *exp,
10435 enum noside noside, enum exp_opcode op,
10436 struct value *arg1, struct value *arg2)
10437{
10438 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10439 return value::zero (arg1->type (), not_lval);
dd5fd283
TT
10440 else
10441 {
10442 /* For integer exponentiation operations,
10443 only promote the first argument. */
d0c97917 10444 if (is_integral_type (arg2->type ()))
dd5fd283
TT
10445 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10446 else
10447 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10448
10449 return value_binop (arg1, arg2, op);
10450 }
10451}
10452
03070ee9
TT
10453namespace expr
10454{
10455
8b12db26
TT
10456/* See ada-exp.h. */
10457
10458operation_up
10459ada_resolvable::replace (operation_up &&owner,
10460 struct expression *exp,
10461 bool deprocedure_p,
10462 bool parse_completion,
10463 innermost_block_tracker *tracker,
10464 struct type *context_type)
10465{
10466 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10467 return (make_operation<ada_funcall_operation>
10468 (std::move (owner),
10469 std::vector<operation_up> ()));
10470 return std::move (owner);
10471}
10472
c9f66f00 10473/* Convert the character literal whose value would be VAL to the
03adb248
TT
10474 appropriate value of type TYPE, if there is a translation.
10475 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10476 the literal 'A' (VAL == 65), returns 0. */
10477
10478static LONGEST
10479convert_char_literal (struct type *type, LONGEST val)
10480{
c9f66f00 10481 char name[12];
03adb248
TT
10482 int f;
10483
10484 if (type == NULL)
10485 return val;
10486 type = check_typedef (type);
10487 if (type->code () != TYPE_CODE_ENUM)
10488 return val;
10489
10490 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10491 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10492 else if (val >= 0 && val < 256)
10493 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10494 else if (val >= 0 && val < 0x10000)
10495 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10496 else
c9f66f00 10497 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10498 size_t len = strlen (name);
10499 for (f = 0; f < type->num_fields (); f += 1)
10500 {
10501 /* Check the suffix because an enum constant in a package will
10502 have a name like "pkg__QUxx". This is safe enough because we
10503 already have the correct type, and because mangling means
10504 there can't be clashes. */
33d16dd9 10505 const char *ename = type->field (f).name ();
03adb248
TT
10506 size_t elen = strlen (ename);
10507
10508 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10509 return type->field (f).loc_enumval ();
03adb248
TT
10510 }
10511 return val;
10512}
10513
b1b9c411
TT
10514value *
10515ada_char_operation::evaluate (struct type *expect_type,
10516 struct expression *exp,
10517 enum noside noside)
10518{
10519 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10520 if (expect_type != nullptr)
10521 result = ada_value_cast (expect_type, result);
10522 return result;
10523}
10524
03adb248
TT
10525/* See ada-exp.h. */
10526
10527operation_up
10528ada_char_operation::replace (operation_up &&owner,
10529 struct expression *exp,
10530 bool deprocedure_p,
10531 bool parse_completion,
10532 innermost_block_tracker *tracker,
10533 struct type *context_type)
10534{
10535 operation_up result = std::move (owner);
10536
10537 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10538 {
5309ce2f 10539 LONGEST val = as_longest ();
03adb248
TT
10540 gdb_assert (result.get () == this);
10541 std::get<0> (m_storage) = context_type;
5309ce2f 10542 std::get<1> (m_storage) = convert_char_literal (context_type, val);
03adb248
TT
10543 }
10544
b1b9c411 10545 return result;
03adb248
TT
10546}
10547
03070ee9
TT
10548value *
10549ada_wrapped_operation::evaluate (struct type *expect_type,
10550 struct expression *exp,
10551 enum noside noside)
10552{
10553 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10554 if (noside == EVAL_NORMAL)
10555 result = unwrap_value (result);
10556
10557 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10558 then we need to perform the conversion manually, because
10559 evaluate_subexp_standard doesn't do it. This conversion is
10560 necessary in Ada because the different kinds of float/fixed
10561 types in Ada have different representations.
10562
10563 Similarly, we need to perform the conversion from OP_LONG
10564 ourselves. */
10565 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10566 result = ada_value_cast (expect_type, result);
10567
10568 return result;
10569}
10570
013a623f
TT
10571void
10572ada_wrapped_operation::do_generate_ax (struct expression *exp,
10573 struct agent_expr *ax,
10574 struct axs_value *value,
10575 struct type *cast_type)
10576{
10577 std::get<0> (m_storage)->generate_ax (exp, ax, value, cast_type);
10578
10579 struct type *type = value->type;
10580 if (ada_is_aligner_type (type))
10581 error (_("Aligner types cannot be handled in agent expressions"));
10582 else if (find_base_type (type) != nullptr)
10583 error (_("Dynamic types cannot be handled in agent expressions"));
10584}
10585
42fecb61
TT
10586value *
10587ada_string_operation::evaluate (struct type *expect_type,
10588 struct expression *exp,
10589 enum noside noside)
10590{
fc18a21b
TT
10591 struct type *char_type;
10592 if (expect_type != nullptr && ada_is_string_type (expect_type))
10593 char_type = ada_array_element_type (expect_type, 1);
10594 else
10595 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10596
10597 const std::string &str = std::get<0> (m_storage);
10598 const char *encoding;
df86565b 10599 switch (char_type->length ())
fc18a21b
TT
10600 {
10601 case 1:
10602 {
10603 /* Simply copy over the data -- this isn't perhaps strictly
10604 correct according to the encodings, but it is gdb's
10605 historical behavior. */
10606 struct type *stringtype
10607 = lookup_array_range_type (char_type, 1, str.length ());
317c3ed9 10608 struct value *val = value::allocate (stringtype);
bbe912ba 10609 memcpy (val->contents_raw ().data (), str.c_str (),
fc18a21b
TT
10610 str.length ());
10611 return val;
10612 }
10613
10614 case 2:
10615 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10616 encoding = "UTF-16BE";
10617 else
10618 encoding = "UTF-16LE";
10619 break;
10620
10621 case 4:
10622 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10623 encoding = "UTF-32BE";
10624 else
10625 encoding = "UTF-32LE";
10626 break;
10627
10628 default:
10629 error (_("unexpected character type size %s"),
df86565b 10630 pulongest (char_type->length ()));
fc18a21b
TT
10631 }
10632
10633 auto_obstack converted;
10634 convert_between_encodings (host_charset (), encoding,
10635 (const gdb_byte *) str.c_str (),
10636 str.length (), 1,
10637 &converted, translit_none);
10638
10639 struct type *stringtype
10640 = lookup_array_range_type (char_type, 1,
10641 obstack_object_size (&converted)
df86565b 10642 / char_type->length ());
317c3ed9 10643 struct value *val = value::allocate (stringtype);
bbe912ba 10644 memcpy (val->contents_raw ().data (),
fc18a21b
TT
10645 obstack_base (&converted),
10646 obstack_object_size (&converted));
10647 return val;
42fecb61
TT
10648}
10649
b1b9c411
TT
10650value *
10651ada_concat_operation::evaluate (struct type *expect_type,
10652 struct expression *exp,
10653 enum noside noside)
10654{
10655 /* If one side is a literal, evaluate the other side first so that
10656 the expected type can be set properly. */
10657 const operation_up &lhs_expr = std::get<0> (m_storage);
10658 const operation_up &rhs_expr = std::get<1> (m_storage);
10659
10660 value *lhs, *rhs;
10661 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10662 {
10663 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10664 lhs = lhs_expr->evaluate (rhs->type (), exp, noside);
b1b9c411
TT
10665 }
10666 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10667 {
10668 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10669 struct type *rhs_type = check_typedef (rhs->type ());
b1b9c411
TT
10670 struct type *elt_type = nullptr;
10671 if (rhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10672 elt_type = rhs_type->target_type ();
b1b9c411
TT
10673 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10674 }
10675 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10676 {
10677 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10678 rhs = rhs_expr->evaluate (lhs->type (), exp, noside);
b1b9c411
TT
10679 }
10680 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10681 {
10682 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10683 struct type *lhs_type = check_typedef (lhs->type ());
b1b9c411
TT
10684 struct type *elt_type = nullptr;
10685 if (lhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10686 elt_type = lhs_type->target_type ();
b1b9c411
TT
10687 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10688 }
10689 else
10690 return concat_operation::evaluate (expect_type, exp, noside);
10691
10692 return value_concat (lhs, rhs);
10693}
10694
cc6bd32e
TT
10695value *
10696ada_qual_operation::evaluate (struct type *expect_type,
10697 struct expression *exp,
10698 enum noside noside)
10699{
10700 struct type *type = std::get<1> (m_storage);
10701 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10702}
10703
fc715eb2
TT
10704value *
10705ada_ternop_range_operation::evaluate (struct type *expect_type,
10706 struct expression *exp,
10707 enum noside noside)
10708{
10709 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10710 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10711 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10712 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10713}
10714
73796c73
TT
10715value *
10716ada_binop_addsub_operation::evaluate (struct type *expect_type,
10717 struct expression *exp,
10718 enum noside noside)
10719{
10720 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10721 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10722
5bd5fecd 10723 auto do_op = [this] (LONGEST x, LONGEST y)
73796c73
TT
10724 {
10725 if (std::get<0> (m_storage) == BINOP_ADD)
10726 return x + y;
10727 return x - y;
10728 };
10729
d0c97917 10730 if (arg1->type ()->code () == TYPE_CODE_PTR)
73796c73 10731 return (value_from_longest
d0c97917 10732 (arg1->type (),
73796c73 10733 do_op (value_as_long (arg1), value_as_long (arg2))));
d0c97917 10734 if (arg2->type ()->code () == TYPE_CODE_PTR)
73796c73 10735 return (value_from_longest
d0c97917 10736 (arg2->type (),
73796c73
TT
10737 do_op (value_as_long (arg1), value_as_long (arg2))));
10738 /* Preserve the original type for use by the range case below.
10739 We cannot cast the result to a reference type, so if ARG1 is
10740 a reference type, find its underlying type. */
d0c97917 10741 struct type *type = arg1->type ();
73796c73 10742 while (type->code () == TYPE_CODE_REF)
27710edb 10743 type = type->target_type ();
73796c73
TT
10744 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10745 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10746 /* We need to special-case the result with a range.
10747 This is done for the benefit of "ptype". gdb's Ada support
10748 historically used the LHS to set the result type here, so
10749 preserve this behavior. */
10750 if (type->code () == TYPE_CODE_RANGE)
10751 arg1 = value_cast (type, arg1);
10752 return arg1;
10753}
10754
60fa02ca
TT
10755value *
10756ada_unop_atr_operation::evaluate (struct type *expect_type,
10757 struct expression *exp,
10758 enum noside noside)
10759{
10760 struct type *type_arg = nullptr;
10761 value *val = nullptr;
10762
10763 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10764 {
10765 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10766 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 10767 type_arg = tem->type ();
60fa02ca
TT
10768 }
10769 else
10770 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10771
10772 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10773 val, type_arg, std::get<2> (m_storage));
10774}
10775
3f4a0053
TT
10776value *
10777ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10778 struct expression *exp,
10779 enum noside noside)
10780{
10781 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10782 return value::zero (expect_type, not_lval);
3f4a0053 10783
9c79936b
TT
10784 const bound_minimal_symbol &b = std::get<0> (m_storage);
10785 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10786
10787 val = ada_value_cast (expect_type, val);
10788
10789 /* Follow the Ada language semantics that do not allow taking
10790 an address of the result of a cast (view conversion in Ada). */
736355f2 10791 if (val->lval () == lval_memory)
3f4a0053 10792 {
3ee3b270 10793 if (val->lazy ())
78259c36 10794 val->fetch_lazy ();
6f9c9d71 10795 val->set_lval (not_lval);
3f4a0053
TT
10796 }
10797 return val;
10798}
10799
99a3b1e7
TT
10800value *
10801ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10802 struct expression *exp,
10803 enum noside noside)
10804{
10805 value *val = evaluate_var_value (noside,
9e5e03df
TT
10806 std::get<0> (m_storage).block,
10807 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10808
10809 val = ada_value_cast (expect_type, val);
10810
10811 /* Follow the Ada language semantics that do not allow taking
10812 an address of the result of a cast (view conversion in Ada). */
736355f2 10813 if (val->lval () == lval_memory)
99a3b1e7 10814 {
3ee3b270 10815 if (val->lazy ())
78259c36 10816 val->fetch_lazy ();
6f9c9d71 10817 val->set_lval (not_lval);
99a3b1e7
TT
10818 }
10819 return val;
10820}
10821
10822value *
10823ada_var_value_operation::evaluate (struct type *expect_type,
10824 struct expression *exp,
10825 enum noside noside)
10826{
9e5e03df 10827 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10828
6c9c307c 10829 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10830 /* Only encountered when an unresolved symbol occurs in a
10831 context other than a function call, in which case, it is
10832 invalid. */
10833 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10834 sym->print_name ());
10835
10836 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10837 {
5f9c5a63 10838 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10839 /* Check to see if this is a tagged type. We also need to handle
10840 the case where the type is a reference to a tagged type, but
10841 we have to be careful to exclude pointers to tagged types.
10842 The latter should be shown as usual (as a pointer), whereas
10843 a reference should mostly be transparent to the user. */
10844 if (ada_is_tagged_type (type, 0)
10845 || (type->code () == TYPE_CODE_REF
27710edb 10846 && ada_is_tagged_type (type->target_type (), 0)))
99a3b1e7
TT
10847 {
10848 /* Tagged types are a little special in the fact that the real
10849 type is dynamic and can only be determined by inspecting the
10850 object's tag. This means that we need to get the object's
10851 value first (EVAL_NORMAL) and then extract the actual object
10852 type from its tag.
10853
10854 Note that we cannot skip the final step where we extract
10855 the object type from its tag, because the EVAL_NORMAL phase
10856 results in dynamic components being resolved into fixed ones.
10857 This can cause problems when trying to print the type
10858 description of tagged types whose parent has a dynamic size:
10859 We use the type name of the "_parent" component in order
10860 to print the name of the ancestor type in the type description.
10861 If that component had a dynamic size, the resolution into
10862 a fixed type would result in the loss of that type name,
10863 thus preventing us from printing the name of the ancestor
10864 type in the type description. */
9863c3b5 10865 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10866
10867 if (type->code () != TYPE_CODE_REF)
10868 {
10869 struct type *actual_type;
10870
10871 actual_type = type_from_tag (ada_value_tag (arg1));
10872 if (actual_type == NULL)
10873 /* If, for some reason, we were unable to determine
10874 the actual type from the tag, then use the static
10875 approximation that we just computed as a fallback.
10876 This can happen if the debugging information is
10877 incomplete, for instance. */
10878 actual_type = type;
ee7bb294 10879 return value::zero (actual_type, not_lval);
99a3b1e7
TT
10880 }
10881 else
10882 {
10883 /* In the case of a ref, ada_coerce_ref takes care
10884 of determining the actual type. But the evaluation
10885 should return a ref as it should be valid to ask
10886 for its address; so rebuild a ref after coerce. */
10887 arg1 = ada_coerce_ref (arg1);
10888 return value_ref (arg1, TYPE_CODE_REF);
10889 }
10890 }
10891
10892 /* Records and unions for which GNAT encodings have been
10893 generated need to be statically fixed as well.
10894 Otherwise, non-static fixing produces a type where
10895 all dynamic properties are removed, which prevents "ptype"
10896 from being able to completely describe the type.
10897 For instance, a case statement in a variant record would be
10898 replaced by the relevant components based on the actual
10899 value of the discriminants. */
10900 if ((type->code () == TYPE_CODE_STRUCT
10901 && dynamic_template_type (type) != NULL)
10902 || (type->code () == TYPE_CODE_UNION
10903 && ada_find_parallel_type (type, "___XVU") != NULL))
ee7bb294 10904 return value::zero (to_static_fixed_type (type), not_lval);
99a3b1e7
TT
10905 }
10906
10907 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10908 return ada_to_fixed_value (arg1);
10909}
10910
d8a4ed8a
TT
10911bool
10912ada_var_value_operation::resolve (struct expression *exp,
10913 bool deprocedure_p,
10914 bool parse_completion,
10915 innermost_block_tracker *tracker,
10916 struct type *context_type)
10917{
9e5e03df 10918 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10919 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10920 {
10921 block_symbol resolved
9e5e03df 10922 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10923 context_type, parse_completion,
10924 deprocedure_p, tracker);
9e5e03df 10925 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10926 }
10927
10928 if (deprocedure_p
5f9c5a63 10929 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10930 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10931 return true;
10932
10933 return false;
10934}
10935
013a623f
TT
10936void
10937ada_var_value_operation::do_generate_ax (struct expression *exp,
10938 struct agent_expr *ax,
10939 struct axs_value *value,
10940 struct type *cast_type)
10941{
10942 symbol *sym = std::get<0> (m_storage).symbol;
10943
10944 if (sym->domain () == UNDEF_DOMAIN)
10945 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10946 sym->print_name ());
10947
10948 struct type *type = static_unwrap_type (sym->type ());
10949 if (ada_is_tagged_type (type, 0)
10950 || (type->code () == TYPE_CODE_REF
10951 && ada_is_tagged_type (type->target_type (), 0)))
10952 error (_("Tagged types cannot be handled in agent expressions"));
10953
10954 if ((type->code () == TYPE_CODE_STRUCT
10955 && dynamic_template_type (type) != NULL)
10956 || (type->code () == TYPE_CODE_UNION
10957 && ada_find_parallel_type (type, "___XVU") != NULL))
10958 error (_("Dynamic types cannot be handled in agent expressions"));
10959
10960 var_value_operation::do_generate_ax (exp, ax, value, cast_type);
10961}
10962
e8c33fa1
TT
10963value *
10964ada_unop_ind_operation::evaluate (struct type *expect_type,
10965 struct expression *exp,
10966 enum noside noside)
10967{
10968 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10969
d0c97917 10970 struct type *type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
10971 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10972 {
10973 if (ada_is_array_descriptor_type (type))
10974 /* GDB allows dereferencing GNAT array descriptors. */
10975 {
10976 struct type *arrType = ada_type_of_array (arg1, 0);
10977
10978 if (arrType == NULL)
10979 error (_("Attempt to dereference null array pointer."));
10980 return value_at_lazy (arrType, 0);
10981 }
10982 else if (type->code () == TYPE_CODE_PTR
10983 || type->code () == TYPE_CODE_REF
10984 /* In C you can dereference an array to get the 1st elt. */
10985 || type->code () == TYPE_CODE_ARRAY)
10986 {
10987 /* As mentioned in the OP_VAR_VALUE case, tagged types can
10988 only be determined by inspecting the object's tag.
10989 This means that we need to evaluate completely the
10990 expression in order to get its type. */
10991
10992 if ((type->code () == TYPE_CODE_REF
10993 || type->code () == TYPE_CODE_PTR)
27710edb 10994 && ada_is_tagged_type (type->target_type (), 0))
e8c33fa1
TT
10995 {
10996 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
10997 EVAL_NORMAL);
d0c97917 10998 type = ada_value_ind (arg1)->type ();
e8c33fa1
TT
10999 }
11000 else
11001 {
11002 type = to_static_fixed_type
11003 (ada_aligned_type
27710edb 11004 (ada_check_typedef (type->target_type ())));
e8c33fa1 11005 }
ee7bb294 11006 return value::zero (type, lval_memory);
e8c33fa1
TT
11007 }
11008 else if (type->code () == TYPE_CODE_INT)
11009 {
11010 /* GDB allows dereferencing an int. */
11011 if (expect_type == NULL)
ee7bb294 11012 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
e8c33fa1
TT
11013 lval_memory);
11014 else
11015 {
11016 expect_type =
11017 to_static_fixed_type (ada_aligned_type (expect_type));
ee7bb294 11018 return value::zero (expect_type, lval_memory);
e8c33fa1
TT
11019 }
11020 }
11021 else
11022 error (_("Attempt to take contents of a non-pointer value."));
11023 }
11024 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
d0c97917 11025 type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
11026
11027 if (type->code () == TYPE_CODE_INT)
11028 /* GDB allows dereferencing an int. If we were given
11029 the expect_type, then use that as the target type.
11030 Otherwise, assume that the target type is an int. */
11031 {
11032 if (expect_type != NULL)
11033 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11034 arg1));
11035 else
11036 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11037 (CORE_ADDR) value_as_address (arg1));
11038 }
11039
11040 if (ada_is_array_descriptor_type (type))
11041 /* GDB allows dereferencing GNAT array descriptors. */
11042 return ada_coerce_to_simple_array (arg1);
11043 else
11044 return ada_value_ind (arg1);
11045}
11046
ebc06ad8
TT
11047value *
11048ada_structop_operation::evaluate (struct type *expect_type,
11049 struct expression *exp,
11050 enum noside noside)
11051{
11052 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11053 const char *str = std::get<1> (m_storage).c_str ();
11054 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11055 {
11056 struct type *type;
d0c97917 11057 struct type *type1 = arg1->type ();
ebc06ad8
TT
11058
11059 if (ada_is_tagged_type (type1, 1))
11060 {
11061 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11062
11063 /* If the field is not found, check if it exists in the
11064 extension of this object's type. This means that we
11065 need to evaluate completely the expression. */
11066
11067 if (type == NULL)
11068 {
11069 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11070 EVAL_NORMAL);
11071 arg1 = ada_value_struct_elt (arg1, str, 0);
11072 arg1 = unwrap_value (arg1);
d0c97917 11073 type = ada_to_fixed_value (arg1)->type ();
ebc06ad8
TT
11074 }
11075 }
11076 else
11077 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11078
ee7bb294 11079 return value::zero (ada_aligned_type (type), lval_memory);
ebc06ad8
TT
11080 }
11081 else
11082 {
11083 arg1 = ada_value_struct_elt (arg1, str, 0);
11084 arg1 = unwrap_value (arg1);
11085 return ada_to_fixed_value (arg1);
11086 }
11087}
11088
efe3af2f
TT
11089value *
11090ada_funcall_operation::evaluate (struct type *expect_type,
11091 struct expression *exp,
11092 enum noside noside)
11093{
11094 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11095 int nargs = args_up.size ();
11096 std::vector<value *> argvec (nargs);
11097 operation_up &callee_op = std::get<0> (m_storage);
11098
11099 ada_var_value_operation *avv
11100 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11101 if (avv != nullptr
6c9c307c 11102 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11103 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11104 avv->get_symbol ()->print_name ());
11105
11106 value *callee = callee_op->evaluate (nullptr, exp, noside);
11107 for (int i = 0; i < args_up.size (); ++i)
11108 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11109
11110 if (ada_is_constrained_packed_array_type
d0c97917 11111 (desc_base_type (callee->type ())))
efe3af2f 11112 callee = ada_coerce_to_simple_array (callee);
d0c97917 11113 else if (callee->type ()->code () == TYPE_CODE_ARRAY
3757d2d4 11114 && callee->type ()->field (0).bitsize () != 0)
efe3af2f
TT
11115 /* This is a packed array that has already been fixed, and
11116 therefore already coerced to a simple array. Nothing further
11117 to do. */
11118 ;
d0c97917 11119 else if (callee->type ()->code () == TYPE_CODE_REF)
efe3af2f
TT
11120 {
11121 /* Make sure we dereference references so that all the code below
11122 feels like it's really handling the referenced value. Wrapping
11123 types (for alignment) may be there, so make sure we strip them as
11124 well. */
11125 callee = ada_to_fixed_value (coerce_ref (callee));
11126 }
d0c97917 11127 else if (callee->type ()->code () == TYPE_CODE_ARRAY
736355f2 11128 && callee->lval () == lval_memory)
efe3af2f
TT
11129 callee = value_addr (callee);
11130
d0c97917 11131 struct type *type = ada_check_typedef (callee->type ());
efe3af2f
TT
11132
11133 /* Ada allows us to implicitly dereference arrays when subscripting
11134 them. So, if this is an array typedef (encoding use for array
11135 access types encoded as fat pointers), strip it now. */
11136 if (type->code () == TYPE_CODE_TYPEDEF)
11137 type = ada_typedef_target_type (type);
11138
11139 if (type->code () == TYPE_CODE_PTR)
11140 {
27710edb 11141 switch (ada_check_typedef (type->target_type ())->code ())
efe3af2f
TT
11142 {
11143 case TYPE_CODE_FUNC:
27710edb 11144 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11145 break;
11146 case TYPE_CODE_ARRAY:
11147 break;
11148 case TYPE_CODE_STRUCT:
11149 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11150 callee = ada_value_ind (callee);
27710edb 11151 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11152 break;
11153 default:
11154 error (_("cannot subscript or call something of type `%s'"),
d0c97917 11155 ada_type_name (callee->type ()));
efe3af2f
TT
11156 break;
11157 }
11158 }
11159
11160 switch (type->code ())
11161 {
11162 case TYPE_CODE_FUNC:
11163 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11164 {
27710edb 11165 if (type->target_type () == NULL)
efe3af2f 11166 error_call_unknown_return_type (NULL);
317c3ed9 11167 return value::allocate (type->target_type ());
efe3af2f 11168 }
61f9fb1e 11169 return call_function_by_hand (callee, expect_type, argvec);
efe3af2f
TT
11170 case TYPE_CODE_INTERNAL_FUNCTION:
11171 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11172 /* We don't know anything about what the internal
11173 function might return, but we have to return
11174 something. */
ee7bb294 11175 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
efe3af2f
TT
11176 not_lval);
11177 else
11178 return call_internal_function (exp->gdbarch, exp->language_defn,
11179 callee, nargs,
11180 argvec.data ());
11181
d3c54a1c
TT
11182 case TYPE_CODE_STRUCT:
11183 {
11184 int arity;
4c4b4cd2 11185
d3c54a1c
TT
11186 arity = ada_array_arity (type);
11187 type = ada_array_element_type (type, nargs);
11188 if (type == NULL)
11189 error (_("cannot subscript or call a record"));
11190 if (arity != nargs)
11191 error (_("wrong number of subscripts; expecting %d"), arity);
11192 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 11193 return value::zero (ada_aligned_type (type), lval_memory);
d3c54a1c
TT
11194 return
11195 unwrap_value (ada_value_subscript
11196 (callee, nargs, argvec.data ()));
11197 }
11198 case TYPE_CODE_ARRAY:
14f9c5c9 11199 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11200 {
d3c54a1c
TT
11201 type = ada_array_element_type (type, nargs);
11202 if (type == NULL)
11203 error (_("element type of array unknown"));
dda83cd7 11204 else
ee7bb294 11205 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11206 }
d3c54a1c
TT
11207 return
11208 unwrap_value (ada_value_subscript
11209 (ada_coerce_to_simple_array (callee),
11210 nargs, argvec.data ()));
11211 case TYPE_CODE_PTR: /* Pointer to array */
11212 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11213 {
27710edb 11214 type = to_fixed_array_type (type->target_type (), NULL, 1);
d3c54a1c
TT
11215 type = ada_array_element_type (type, nargs);
11216 if (type == NULL)
11217 error (_("element type of array unknown"));
96967637 11218 else
ee7bb294 11219 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11220 }
d3c54a1c
TT
11221 return
11222 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11223 argvec.data ()));
6b0d7253 11224
d3c54a1c
TT
11225 default:
11226 error (_("Attempt to index or call something other than an "
11227 "array or function"));
11228 }
11229}
5b4ee69b 11230
d3c54a1c
TT
11231bool
11232ada_funcall_operation::resolve (struct expression *exp,
11233 bool deprocedure_p,
11234 bool parse_completion,
11235 innermost_block_tracker *tracker,
11236 struct type *context_type)
11237{
11238 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11239
d3c54a1c
TT
11240 ada_var_value_operation *avv
11241 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11242 if (avv == nullptr)
11243 return false;
5ec18f2b 11244
d3c54a1c 11245 symbol *sym = avv->get_symbol ();
6c9c307c 11246 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11247 return false;
dda83cd7 11248
d3c54a1c
TT
11249 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11250 int nargs = args_up.size ();
11251 std::vector<value *> argvec (nargs);
284614f0 11252
d3c54a1c
TT
11253 for (int i = 0; i < args_up.size (); ++i)
11254 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11255
d3c54a1c
TT
11256 const block *block = avv->get_block ();
11257 block_symbol resolved
11258 = ada_resolve_funcall (sym, block,
11259 context_type, parse_completion,
11260 nargs, argvec.data (),
11261 tracker);
11262
11263 std::get<0> (m_storage)
9e5e03df 11264 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11265 return false;
11266}
11267
11268bool
11269ada_ternop_slice_operation::resolve (struct expression *exp,
11270 bool deprocedure_p,
11271 bool parse_completion,
11272 innermost_block_tracker *tracker,
11273 struct type *context_type)
11274{
11275 /* Historically this check was done during resolution, so we
11276 continue that here. */
11277 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11278 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 11279 if (ada_is_any_packed_array_type (v->type ()))
d3c54a1c
TT
11280 error (_("cannot slice a packed array"));
11281 return false;
11282}
14f9c5c9 11283
14f9c5c9 11284}
d3c54a1c 11285
14f9c5c9 11286\f
d2e4a39e 11287
4c4b4cd2
PH
11288/* Return non-zero iff TYPE represents a System.Address type. */
11289
11290int
11291ada_is_system_address_type (struct type *type)
11292{
7d93a1e0 11293 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11294}
11295
14f9c5c9 11296\f
d2e4a39e 11297
dda83cd7 11298 /* Range types */
14f9c5c9
AS
11299
11300/* Scan STR beginning at position K for a discriminant name, and
11301 return the value of that discriminant field of DVAL in *PX. If
11302 PNEW_K is not null, put the position of the character beyond the
11303 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11304 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11305
11306static int
108d56a4 11307scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11308 int *pnew_k)
14f9c5c9 11309{
5f9febe0 11310 static std::string storage;
5da1a4d3 11311 const char *pstart, *pend, *bound;
d2e4a39e 11312 struct value *bound_val;
14f9c5c9
AS
11313
11314 if (dval == NULL || str == NULL || str[k] == '\0')
11315 return 0;
11316
5da1a4d3
SM
11317 pstart = str + k;
11318 pend = strstr (pstart, "__");
14f9c5c9
AS
11319 if (pend == NULL)
11320 {
5da1a4d3 11321 bound = pstart;
14f9c5c9
AS
11322 k += strlen (bound);
11323 }
d2e4a39e 11324 else
14f9c5c9 11325 {
5da1a4d3
SM
11326 int len = pend - pstart;
11327
11328 /* Strip __ and beyond. */
5f9febe0
TT
11329 storage = std::string (pstart, len);
11330 bound = storage.c_str ();
d2e4a39e 11331 k = pend - str;
14f9c5c9 11332 }
d2e4a39e 11333
d0c97917 11334 bound_val = ada_search_struct_field (bound, dval, 0, dval->type ());
14f9c5c9
AS
11335 if (bound_val == NULL)
11336 return 0;
11337
11338 *px = value_as_long (bound_val);
11339 if (pnew_k != NULL)
11340 *pnew_k = k;
11341 return 1;
11342}
11343
25a1127b
TT
11344/* Value of variable named NAME. Only exact matches are considered.
11345 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11346 otherwise causes an error with message ERR_MSG. */
11347
d2e4a39e 11348static struct value *
edb0c9cb 11349get_var_value (const char *name, const char *err_msg)
14f9c5c9 11350{
25a1127b
TT
11351 std::string quoted_name = add_angle_brackets (name);
11352
11353 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11354
d1183b06
TT
11355 std::vector<struct block_symbol> syms
11356 = ada_lookup_symbol_list_worker (lookup_name,
11357 get_selected_block (0),
11358 VAR_DOMAIN, 1);
14f9c5c9 11359
d1183b06 11360 if (syms.size () != 1)
14f9c5c9
AS
11361 {
11362 if (err_msg == NULL)
dda83cd7 11363 return 0;
14f9c5c9 11364 else
dda83cd7 11365 error (("%s"), err_msg);
14f9c5c9
AS
11366 }
11367
54d343a2 11368 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11369}
d2e4a39e 11370
edb0c9cb
PA
11371/* Value of integer variable named NAME in the current environment.
11372 If no such variable is found, returns false. Otherwise, sets VALUE
11373 to the variable's value and returns true. */
4c4b4cd2 11374
edb0c9cb
PA
11375bool
11376get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11377{
4c4b4cd2 11378 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11379
14f9c5c9 11380 if (var_val == 0)
edb0c9cb
PA
11381 return false;
11382
11383 value = value_as_long (var_val);
11384 return true;
14f9c5c9 11385}
d2e4a39e 11386
14f9c5c9
AS
11387
11388/* Return a range type whose base type is that of the range type named
11389 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11390 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11391 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11392 corresponding range type from debug information; fall back to using it
11393 if symbol lookup fails. If a new type must be created, allocate it
11394 like ORIG_TYPE was. The bounds information, in general, is encoded
11395 in NAME, the base type given in the named range type. */
14f9c5c9 11396
d2e4a39e 11397static struct type *
28c85d6c 11398to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11399{
0d5cff50 11400 const char *name;
14f9c5c9 11401 struct type *base_type;
108d56a4 11402 const char *subtype_info;
14f9c5c9 11403
28c85d6c 11404 gdb_assert (raw_type != NULL);
7d93a1e0 11405 gdb_assert (raw_type->name () != NULL);
dddfab26 11406
78134374 11407 if (raw_type->code () == TYPE_CODE_RANGE)
27710edb 11408 base_type = raw_type->target_type ();
14f9c5c9
AS
11409 else
11410 base_type = raw_type;
11411
7d93a1e0 11412 name = raw_type->name ();
14f9c5c9
AS
11413 subtype_info = strstr (name, "___XD");
11414 if (subtype_info == NULL)
690cc4eb 11415 {
43bbcdc2
PH
11416 LONGEST L = ada_discrete_type_low_bound (raw_type);
11417 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11418
690cc4eb
PH
11419 if (L < INT_MIN || U > INT_MAX)
11420 return raw_type;
11421 else
e727c536
TT
11422 {
11423 type_allocator alloc (raw_type);
11424 return create_static_range_type (alloc, raw_type, L, U);
11425 }
690cc4eb 11426 }
14f9c5c9
AS
11427 else
11428 {
14f9c5c9
AS
11429 int prefix_len = subtype_info - name;
11430 LONGEST L, U;
11431 struct type *type;
108d56a4 11432 const char *bounds_str;
14f9c5c9
AS
11433 int n;
11434
14f9c5c9
AS
11435 subtype_info += 5;
11436 bounds_str = strchr (subtype_info, '_');
11437 n = 1;
11438
d2e4a39e 11439 if (*subtype_info == 'L')
dda83cd7
SM
11440 {
11441 if (!ada_scan_number (bounds_str, n, &L, &n)
11442 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11443 return raw_type;
11444 if (bounds_str[n] == '_')
11445 n += 2;
11446 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11447 n += 1;
11448 subtype_info += 1;
11449 }
d2e4a39e 11450 else
dda83cd7 11451 {
5f9febe0
TT
11452 std::string name_buf = std::string (name, prefix_len) + "___L";
11453 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11454 {
11455 lim_warning (_("Unknown lower bound, using 1."));
11456 L = 1;
11457 }
11458 }
14f9c5c9 11459
d2e4a39e 11460 if (*subtype_info == 'U')
dda83cd7
SM
11461 {
11462 if (!ada_scan_number (bounds_str, n, &U, &n)
11463 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11464 return raw_type;
11465 }
d2e4a39e 11466 else
dda83cd7 11467 {
5f9febe0
TT
11468 std::string name_buf = std::string (name, prefix_len) + "___U";
11469 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11470 {
11471 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11472 U = L;
11473 }
11474 }
14f9c5c9 11475
e727c536
TT
11476 type_allocator alloc (raw_type);
11477 type = create_static_range_type (alloc, base_type, L, U);
f5a91472 11478 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11479 to match the size of the base_type, which is not what we want.
11480 Set it back to the original range type's length. */
df86565b 11481 type->set_length (raw_type->length ());
d0e39ea2 11482 type->set_name (name);
14f9c5c9
AS
11483 return type;
11484 }
11485}
11486
4c4b4cd2
PH
11487/* True iff NAME is the name of a range type. */
11488
14f9c5c9 11489int
d2e4a39e 11490ada_is_range_type_name (const char *name)
14f9c5c9
AS
11491{
11492 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11493}
14f9c5c9 11494\f
d2e4a39e 11495
dda83cd7 11496 /* Modular types */
4c4b4cd2
PH
11497
11498/* True iff TYPE is an Ada modular type. */
14f9c5c9 11499
14f9c5c9 11500int
d2e4a39e 11501ada_is_modular_type (struct type *type)
14f9c5c9 11502{
18af8284 11503 struct type *subranged_type = get_base_type (type);
14f9c5c9 11504
78134374 11505 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11506 && subranged_type->code () == TYPE_CODE_INT
11507 && subranged_type->is_unsigned ());
14f9c5c9
AS
11508}
11509
4c4b4cd2
PH
11510/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11511
61ee279c 11512ULONGEST
0056e4d5 11513ada_modulus (struct type *type)
14f9c5c9 11514{
5e500d33
SM
11515 const dynamic_prop &high = type->bounds ()->high;
11516
9c0fb734 11517 if (high.is_constant ())
5e500d33
SM
11518 return (ULONGEST) high.const_val () + 1;
11519
11520 /* If TYPE is unresolved, the high bound might be a location list. Return
11521 0, for lack of a better value to return. */
11522 return 0;
14f9c5c9 11523}
d2e4a39e 11524\f
f7f9143b
JB
11525
11526/* Ada exception catchpoint support:
11527 ---------------------------------
11528
11529 We support 3 kinds of exception catchpoints:
11530 . catchpoints on Ada exceptions
11531 . catchpoints on unhandled Ada exceptions
11532 . catchpoints on failed assertions
11533
11534 Exceptions raised during failed assertions, or unhandled exceptions
11535 could perfectly be caught with the general catchpoint on Ada exceptions.
11536 However, we can easily differentiate these two special cases, and having
11537 the option to distinguish these two cases from the rest can be useful
11538 to zero-in on certain situations.
11539
11540 Exception catchpoints are a specialized form of breakpoint,
11541 since they rely on inserting breakpoints inside known routines
11542 of the GNAT runtime. The implementation therefore uses a standard
11543 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11544 of breakpoint_ops.
11545
0259addd
JB
11546 Support in the runtime for exception catchpoints have been changed
11547 a few times already, and these changes affect the implementation
11548 of these catchpoints. In order to be able to support several
11549 variants of the runtime, we use a sniffer that will determine
28010a5d 11550 the runtime variant used by the program being debugged. */
f7f9143b 11551
82eacd52
JB
11552/* Ada's standard exceptions.
11553
11554 The Ada 83 standard also defined Numeric_Error. But there so many
11555 situations where it was unclear from the Ada 83 Reference Manual
11556 (RM) whether Constraint_Error or Numeric_Error should be raised,
11557 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11558 Interpretation saying that anytime the RM says that Numeric_Error
11559 should be raised, the implementation may raise Constraint_Error.
11560 Ada 95 went one step further and pretty much removed Numeric_Error
11561 from the list of standard exceptions (it made it a renaming of
11562 Constraint_Error, to help preserve compatibility when compiling
11563 an Ada83 compiler). As such, we do not include Numeric_Error from
11564 this list of standard exceptions. */
3d0b0fa3 11565
27087b7f 11566static const char * const standard_exc[] = {
3d0b0fa3
JB
11567 "constraint_error",
11568 "program_error",
11569 "storage_error",
11570 "tasking_error"
11571};
11572
0259addd
JB
11573typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11574
11575/* A structure that describes how to support exception catchpoints
11576 for a given executable. */
11577
11578struct exception_support_info
11579{
11580 /* The name of the symbol to break on in order to insert
11581 a catchpoint on exceptions. */
11582 const char *catch_exception_sym;
11583
11584 /* The name of the symbol to break on in order to insert
11585 a catchpoint on unhandled exceptions. */
11586 const char *catch_exception_unhandled_sym;
11587
11588 /* The name of the symbol to break on in order to insert
11589 a catchpoint on failed assertions. */
11590 const char *catch_assert_sym;
11591
9f757bf7
XR
11592 /* The name of the symbol to break on in order to insert
11593 a catchpoint on exception handling. */
11594 const char *catch_handlers_sym;
11595
0259addd
JB
11596 /* Assuming that the inferior just triggered an unhandled exception
11597 catchpoint, this function is responsible for returning the address
11598 in inferior memory where the name of that exception is stored.
11599 Return zero if the address could not be computed. */
11600 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11601};
11602
11603static CORE_ADDR ada_unhandled_exception_name_addr (void);
11604static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11605
11606/* The following exception support info structure describes how to
11607 implement exception catchpoints with the latest version of the
ca683e3a 11608 Ada runtime (as of 2019-08-??). */
0259addd
JB
11609
11610static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11611{
11612 "__gnat_debug_raise_exception", /* catch_exception_sym */
11613 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11614 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11615 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11616 ada_unhandled_exception_name_addr
11617};
11618
11619/* The following exception support info structure describes how to
11620 implement exception catchpoints with an earlier version of the
11621 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11622
11623static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11624{
11625 "__gnat_debug_raise_exception", /* catch_exception_sym */
11626 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11627 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11628 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11629 ada_unhandled_exception_name_addr
11630};
11631
11632/* The following exception support info structure describes how to
11633 implement exception catchpoints with a slightly older version
11634 of the Ada runtime. */
11635
11636static const struct exception_support_info exception_support_info_fallback =
11637{
11638 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11639 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11640 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11641 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11642 ada_unhandled_exception_name_addr_from_raise
11643};
11644
f17011e0
JB
11645/* Return nonzero if we can detect the exception support routines
11646 described in EINFO.
11647
11648 This function errors out if an abnormal situation is detected
11649 (for instance, if we find the exception support routines, but
11650 that support is found to be incomplete). */
11651
11652static int
11653ada_has_this_exception_support (const struct exception_support_info *einfo)
11654{
11655 struct symbol *sym;
11656
11657 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11658 that should be compiled with debugging information. As a result, we
11659 expect to find that symbol in the symtabs. */
11660
11661 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11662 if (sym == NULL)
a6af7abe
JB
11663 {
11664 /* Perhaps we did not find our symbol because the Ada runtime was
11665 compiled without debugging info, or simply stripped of it.
11666 It happens on some GNU/Linux distributions for instance, where
11667 users have to install a separate debug package in order to get
11668 the runtime's debugging info. In that situation, let the user
11669 know why we cannot insert an Ada exception catchpoint.
11670
11671 Note: Just for the purpose of inserting our Ada exception
11672 catchpoint, we could rely purely on the associated minimal symbol.
11673 But we would be operating in degraded mode anyway, since we are
11674 still lacking the debugging info needed later on to extract
11675 the name of the exception being raised (this name is printed in
11676 the catchpoint message, and is also used when trying to catch
11677 a specific exception). We do not handle this case for now. */
3b7344d5 11678 struct bound_minimal_symbol msym
1c8e84b0
JB
11679 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11680
60f62e2b 11681 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
a6af7abe
JB
11682 error (_("Your Ada runtime appears to be missing some debugging "
11683 "information.\nCannot insert Ada exception catchpoint "
11684 "in this configuration."));
11685
11686 return 0;
11687 }
f17011e0
JB
11688
11689 /* Make sure that the symbol we found corresponds to a function. */
11690
66d7f48f 11691 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11692 error (_("Symbol \"%s\" is not a function (class = %d)"),
11693 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11694
11695 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11696 if (sym == NULL)
11697 {
11698 struct bound_minimal_symbol msym
11699 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11700
60f62e2b 11701 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
ca683e3a
AO
11702 error (_("Your Ada runtime appears to be missing some debugging "
11703 "information.\nCannot insert Ada exception catchpoint "
11704 "in this configuration."));
11705
11706 return 0;
11707 }
11708
11709 /* Make sure that the symbol we found corresponds to a function. */
11710
66d7f48f 11711 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11712 error (_("Symbol \"%s\" is not a function (class = %d)"),
11713 sym->linkage_name (), sym->aclass ());
f17011e0
JB
11714
11715 return 1;
11716}
11717
0259addd
JB
11718/* Inspect the Ada runtime and determine which exception info structure
11719 should be used to provide support for exception catchpoints.
11720
3eecfa55
JB
11721 This function will always set the per-inferior exception_info,
11722 or raise an error. */
0259addd
JB
11723
11724static void
11725ada_exception_support_info_sniffer (void)
11726{
3eecfa55 11727 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11728
11729 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11730 if (data->exception_info != NULL)
0259addd
JB
11731 return;
11732
11733 /* Check the latest (default) exception support info. */
f17011e0 11734 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11735 {
3eecfa55 11736 data->exception_info = &default_exception_support_info;
0259addd
JB
11737 return;
11738 }
11739
ca683e3a
AO
11740 /* Try the v0 exception suport info. */
11741 if (ada_has_this_exception_support (&exception_support_info_v0))
11742 {
11743 data->exception_info = &exception_support_info_v0;
11744 return;
11745 }
11746
0259addd 11747 /* Try our fallback exception suport info. */
f17011e0 11748 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11749 {
3eecfa55 11750 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11751 return;
11752 }
11753
2c4c710f
TT
11754 throw_error (NOT_FOUND_ERROR,
11755 _("Could not find Ada runtime exception support"));
0259addd
JB
11756}
11757
f7f9143b
JB
11758/* True iff FRAME is very likely to be that of a function that is
11759 part of the runtime system. This is all very heuristic, but is
11760 intended to be used as advice as to what frames are uninteresting
11761 to most users. */
11762
11763static int
bd2b40ac 11764is_known_support_routine (frame_info_ptr frame)
f7f9143b 11765{
692465f1 11766 enum language func_lang;
f7f9143b 11767 int i;
f35a17b5 11768 const char *fullname;
f7f9143b 11769
4ed6b5be
JB
11770 /* If this code does not have any debugging information (no symtab),
11771 This cannot be any user code. */
f7f9143b 11772
51abb421 11773 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11774 if (sal.symtab == NULL)
11775 return 1;
11776
4ed6b5be
JB
11777 /* If there is a symtab, but the associated source file cannot be
11778 located, then assume this is not user code: Selecting a frame
11779 for which we cannot display the code would not be very helpful
11780 for the user. This should also take care of case such as VxWorks
11781 where the kernel has some debugging info provided for a few units. */
f7f9143b 11782
f35a17b5
JK
11783 fullname = symtab_to_fullname (sal.symtab);
11784 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11785 return 1;
11786
85102364 11787 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11788 We also check the name of the objfile against the name of some
11789 known system libraries that sometimes come with debugging info
11790 too. */
11791
f7f9143b
JB
11792 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11793 {
11794 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11795 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11796 return 1;
3c86fae3
SM
11797 if (sal.symtab->compunit ()->objfile () != NULL
11798 && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
dda83cd7 11799 return 1;
f7f9143b
JB
11800 }
11801
4ed6b5be 11802 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11803
c6dc63a1
TT
11804 gdb::unique_xmalloc_ptr<char> func_name
11805 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11806 if (func_name == NULL)
11807 return 1;
11808
11809 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11810 {
11811 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11812 if (re_exec (func_name.get ()))
11813 return 1;
f7f9143b
JB
11814 }
11815
11816 return 0;
11817}
11818
11819/* Find the first frame that contains debugging information and that is not
11820 part of the Ada run-time, starting from FI and moving upward. */
11821
0ef643c8 11822void
bd2b40ac 11823ada_find_printable_frame (frame_info_ptr fi)
f7f9143b
JB
11824{
11825 for (; fi != NULL; fi = get_prev_frame (fi))
11826 {
11827 if (!is_known_support_routine (fi))
dda83cd7
SM
11828 {
11829 select_frame (fi);
11830 break;
11831 }
f7f9143b
JB
11832 }
11833
11834}
11835
11836/* Assuming that the inferior just triggered an unhandled exception
11837 catchpoint, return the address in inferior memory where the name
11838 of the exception is stored.
11839
11840 Return zero if the address could not be computed. */
11841
11842static CORE_ADDR
11843ada_unhandled_exception_name_addr (void)
0259addd
JB
11844{
11845 return parse_and_eval_address ("e.full_name");
11846}
11847
11848/* Same as ada_unhandled_exception_name_addr, except that this function
11849 should be used when the inferior uses an older version of the runtime,
11850 where the exception name needs to be extracted from a specific frame
11851 several frames up in the callstack. */
11852
11853static CORE_ADDR
11854ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11855{
11856 int frame_level;
bd2b40ac 11857 frame_info_ptr fi;
3eecfa55 11858 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11859
11860 /* To determine the name of this exception, we need to select
11861 the frame corresponding to RAISE_SYM_NAME. This frame is
11862 at least 3 levels up, so we simply skip the first 3 frames
11863 without checking the name of their associated function. */
11864 fi = get_current_frame ();
11865 for (frame_level = 0; frame_level < 3; frame_level += 1)
11866 if (fi != NULL)
11867 fi = get_prev_frame (fi);
11868
11869 while (fi != NULL)
11870 {
692465f1
JB
11871 enum language func_lang;
11872
c6dc63a1
TT
11873 gdb::unique_xmalloc_ptr<char> func_name
11874 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11875 if (func_name != NULL)
11876 {
dda83cd7 11877 if (strcmp (func_name.get (),
55b87a52
KS
11878 data->exception_info->catch_exception_sym) == 0)
11879 break; /* We found the frame we were looking for... */
55b87a52 11880 }
fb44b1a7 11881 fi = get_prev_frame (fi);
f7f9143b
JB
11882 }
11883
11884 if (fi == NULL)
11885 return 0;
11886
11887 select_frame (fi);
11888 return parse_and_eval_address ("id.full_name");
11889}
11890
11891/* Assuming the inferior just triggered an Ada exception catchpoint
11892 (of any type), return the address in inferior memory where the name
11893 of the exception is stored, if applicable.
11894
45db7c09
PA
11895 Assumes the selected frame is the current frame.
11896
f7f9143b
JB
11897 Return zero if the address could not be computed, or if not relevant. */
11898
11899static CORE_ADDR
7bd86313 11900ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex)
f7f9143b 11901{
3eecfa55
JB
11902 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11903
f7f9143b
JB
11904 switch (ex)
11905 {
761269c8 11906 case ada_catch_exception:
dda83cd7
SM
11907 return (parse_and_eval_address ("e.full_name"));
11908 break;
f7f9143b 11909
761269c8 11910 case ada_catch_exception_unhandled:
dda83cd7
SM
11911 return data->exception_info->unhandled_exception_name_addr ();
11912 break;
9f757bf7
XR
11913
11914 case ada_catch_handlers:
dda83cd7 11915 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11916 name. */
dda83cd7 11917 break;
9f757bf7 11918
761269c8 11919 case ada_catch_assert:
dda83cd7
SM
11920 return 0; /* Exception name is not relevant in this case. */
11921 break;
f7f9143b
JB
11922
11923 default:
f34652de 11924 internal_error (_("unexpected catchpoint type"));
dda83cd7 11925 break;
f7f9143b
JB
11926 }
11927
11928 return 0; /* Should never be reached. */
11929}
11930
e547c119
JB
11931/* Assuming the inferior is stopped at an exception catchpoint,
11932 return the message which was associated to the exception, if
11933 available. Return NULL if the message could not be retrieved.
11934
e547c119
JB
11935 Note: The exception message can be associated to an exception
11936 either through the use of the Raise_Exception function, or
11937 more simply (Ada 2005 and later), via:
11938
11939 raise Exception_Name with "exception message";
11940
11941 */
11942
6f46ac85 11943static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11944ada_exception_message_1 (void)
11945{
11946 struct value *e_msg_val;
e547c119 11947 int e_msg_len;
e547c119
JB
11948
11949 /* For runtimes that support this feature, the exception message
11950 is passed as an unbounded string argument called "message". */
11951 e_msg_val = parse_and_eval ("message");
11952 if (e_msg_val == NULL)
11953 return NULL; /* Exception message not supported. */
11954
11955 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
11956 gdb_assert (e_msg_val != NULL);
d0c97917 11957 e_msg_len = e_msg_val->type ()->length ();
e547c119
JB
11958
11959 /* If the message string is empty, then treat it as if there was
11960 no exception message. */
11961 if (e_msg_len <= 0)
11962 return NULL;
11963
15f3b077 11964 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
9feb2d07 11965 read_memory (e_msg_val->address (), (gdb_byte *) e_msg.get (),
15f3b077
TT
11966 e_msg_len);
11967 e_msg.get ()[e_msg_len] = '\0';
11968
11969 return e_msg;
e547c119
JB
11970}
11971
11972/* Same as ada_exception_message_1, except that all exceptions are
11973 contained here (returning NULL instead). */
11974
6f46ac85 11975static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11976ada_exception_message (void)
11977{
6f46ac85 11978 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 11979
a70b8144 11980 try
e547c119
JB
11981 {
11982 e_msg = ada_exception_message_1 ();
11983 }
230d2906 11984 catch (const gdb_exception_error &e)
e547c119 11985 {
6f46ac85 11986 e_msg.reset (nullptr);
e547c119 11987 }
e547c119
JB
11988
11989 return e_msg;
11990}
11991
f7f9143b
JB
11992/* Same as ada_exception_name_addr_1, except that it intercepts and contains
11993 any error that ada_exception_name_addr_1 might cause to be thrown.
11994 When an error is intercepted, a warning with the error message is printed,
11995 and zero is returned. */
11996
11997static CORE_ADDR
7bd86313 11998ada_exception_name_addr (enum ada_exception_catchpoint_kind ex)
f7f9143b 11999{
f7f9143b
JB
12000 CORE_ADDR result = 0;
12001
a70b8144 12002 try
f7f9143b 12003 {
7bd86313 12004 result = ada_exception_name_addr_1 (ex);
f7f9143b
JB
12005 }
12006
230d2906 12007 catch (const gdb_exception_error &e)
f7f9143b 12008 {
3d6e9d23 12009 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12010 return 0;
12011 }
12012
12013 return result;
12014}
12015
cb7de75e 12016static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12017 (const char *excep_string,
12018 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12019
12020/* Ada catchpoints.
12021
12022 In the case of catchpoints on Ada exceptions, the catchpoint will
12023 stop the target on every exception the program throws. When a user
12024 specifies the name of a specific exception, we translate this
12025 request into a condition expression (in text form), and then parse
12026 it into an expression stored in each of the catchpoint's locations.
12027 We then use this condition to check whether the exception that was
12028 raised is the one the user is interested in. If not, then the
12029 target is resumed again. We store the name of the requested
12030 exception, in order to be able to re-set the condition expression
12031 when symbols change. */
12032
c1fc2657 12033/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12034
74421c0b 12035struct ada_catchpoint : public code_breakpoint
28010a5d 12036{
73063f51 12037 ada_catchpoint (struct gdbarch *gdbarch_,
bd21b6c9 12038 enum ada_exception_catchpoint_kind kind,
2c4c710f 12039 const char *cond_string,
bd21b6c9
PA
12040 bool tempflag,
12041 bool enabled,
898db0f7
TT
12042 bool from_tty,
12043 std::string &&excep_string_)
2c4c710f 12044 : code_breakpoint (gdbarch_, bp_catchpoint, tempflag, cond_string),
03f531ea 12045 m_excep_string (std::move (excep_string_)),
73063f51 12046 m_kind (kind)
37f6a7f4 12047 {
74421c0b 12048 /* Unlike most code_breakpoint types, Ada catchpoints are
bd21b6c9 12049 pspace-specific. */
2c4c710f 12050 pspace = current_program_space;
bd21b6c9 12051 enable_state = enabled ? bp_enabled : bp_disabled;
bd21b6c9 12052 language = language_ada;
95f2fe27
TT
12053
12054 re_set ();
37f6a7f4
TT
12055 }
12056
ae72050b
TT
12057 struct bp_location *allocate_location () override;
12058 void re_set () override;
12059 void check_status (struct bpstat *bs) override;
7bd86313 12060 enum print_stop_action print_it (const bpstat *bs) const override;
5e632eca 12061 bool print_one (const bp_location **) const override;
b713485d 12062 void print_mention () const override;
4d1ae558 12063 void print_recreate (struct ui_file *fp) const override;
ae72050b 12064
03f531ea
TT
12065private:
12066
971149cb
TT
12067 /* A helper function for check_status. Returns true if we should
12068 stop for this breakpoint hit. If the user specified a specific
12069 exception, we only want to cause a stop if the program thrown
12070 that exception. */
12071 bool should_stop_exception (const struct bp_location *bl) const;
12072
28010a5d 12073 /* The name of the specific exception the user specified. */
03f531ea 12074 std::string m_excep_string;
37f6a7f4
TT
12075
12076 /* What kind of catchpoint this is. */
12077 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12078};
12079
8cd0bf5e
PA
12080/* An instance of this type is used to represent an Ada catchpoint
12081 breakpoint location. */
12082
12083class ada_catchpoint_location : public bp_location
12084{
12085public:
12086 explicit ada_catchpoint_location (ada_catchpoint *owner)
12087 : bp_location (owner, bp_loc_software_breakpoint)
12088 {}
12089
12090 /* The condition that checks whether the exception that was raised
12091 is the specific exception the user specified on catchpoint
12092 creation. */
12093 expression_up excep_cond_expr;
12094};
12095
2c4c710f
TT
12096static struct symtab_and_line ada_exception_sal
12097 (enum ada_exception_catchpoint_kind ex);
12098
95f2fe27
TT
12099/* Implement the RE_SET method in the structure for all exception
12100 catchpoint kinds. */
28010a5d 12101
95f2fe27
TT
12102void
12103ada_catchpoint::re_set ()
28010a5d 12104{
2c4c710f
TT
12105 std::vector<symtab_and_line> sals;
12106 try
12107 {
12108 struct symtab_and_line sal = ada_exception_sal (m_kind);
12109 sals.push_back (sal);
12110 }
12111 catch (const gdb_exception_error &ex)
12112 {
12113 /* For NOT_FOUND_ERROR, the breakpoint will be pending. */
12114 if (ex.error != NOT_FOUND_ERROR)
12115 throw;
12116 }
12117
12118 update_breakpoint_locations (this, pspace, sals, {});
95f2fe27
TT
12119
12120 /* Reparse the exception conditional expressions. One for each
12121 location. */
12122
28010a5d 12123 /* Nothing to do if there's no specific exception to catch. */
03f531ea 12124 if (m_excep_string.empty ())
28010a5d
PA
12125 return;
12126
12127 /* Same if there are no locations... */
95f2fe27 12128 if (!has_locations ())
28010a5d
PA
12129 return;
12130
fccf9de1 12131 /* Compute the condition expression in text form, from the specific
33b5899f 12132 exception we want to catch. */
fccf9de1 12133 std::string cond_string
03f531ea 12134 = ada_exception_catchpoint_cond_string (m_excep_string.c_str (), m_kind);
28010a5d 12135
fccf9de1
TT
12136 /* Iterate over all the catchpoint's locations, and parse an
12137 expression for each. */
95f2fe27 12138 for (bp_location &bl : locations ())
28010a5d 12139 {
b00b30b2
SM
12140 ada_catchpoint_location &ada_loc
12141 = static_cast<ada_catchpoint_location &> (bl);
4d01a485 12142 expression_up exp;
28010a5d 12143
b00b30b2 12144 if (!bl.shlib_disabled)
28010a5d 12145 {
bbc13ae3 12146 const char *s;
28010a5d 12147
cb7de75e 12148 s = cond_string.c_str ();
a70b8144 12149 try
28010a5d 12150 {
b00b30b2 12151 exp = parse_exp_1 (&s, bl.address, block_for_pc (bl.address), 0);
28010a5d 12152 }
230d2906 12153 catch (const gdb_exception_error &e)
849f2b52
JB
12154 {
12155 warning (_("failed to reevaluate internal exception condition "
12156 "for catchpoint %d: %s"),
95f2fe27 12157 number, e.what ());
849f2b52 12158 }
28010a5d
PA
12159 }
12160
b00b30b2 12161 ada_loc.excep_cond_expr = std::move (exp);
28010a5d 12162 }
28010a5d
PA
12163}
12164
ae72050b
TT
12165/* Implement the ALLOCATE_LOCATION method in the structure for all
12166 exception catchpoint kinds. */
28010a5d 12167
ae72050b
TT
12168struct bp_location *
12169ada_catchpoint::allocate_location ()
28010a5d 12170{
ae72050b 12171 return new ada_catchpoint_location (this);
28010a5d
PA
12172}
12173
971149cb 12174/* See declaration. */
28010a5d 12175
971149cb
TT
12176bool
12177ada_catchpoint::should_stop_exception (const struct bp_location *bl) const
28010a5d
PA
12178{
12179 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12180 const struct ada_catchpoint_location *ada_loc
12181 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12182 bool stop;
28010a5d 12183
37f6a7f4
TT
12184 struct internalvar *var = lookup_internalvar ("_ada_exception");
12185 if (c->m_kind == ada_catch_assert)
12186 clear_internalvar (var);
12187 else
12188 {
12189 try
12190 {
12191 const char *expr;
12192
12193 if (c->m_kind == ada_catch_handlers)
12194 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12195 ".all.occurrence.id");
12196 else
12197 expr = "e";
12198
12199 struct value *exc = parse_and_eval (expr);
12200 set_internalvar (var, exc);
12201 }
12202 catch (const gdb_exception_error &ex)
12203 {
12204 clear_internalvar (var);
12205 }
12206 }
12207
28010a5d 12208 /* With no specific exception, should always stop. */
03f531ea 12209 if (c->m_excep_string.empty ())
7ebaa5f7 12210 return true;
28010a5d
PA
12211
12212 if (ada_loc->excep_cond_expr == NULL)
12213 {
12214 /* We will have a NULL expression if back when we were creating
12215 the expressions, this location's had failed to parse. */
7ebaa5f7 12216 return true;
28010a5d
PA
12217 }
12218
7ebaa5f7 12219 stop = true;
a70b8144 12220 try
28010a5d 12221 {
65558ca5 12222 scoped_value_mark mark;
43048e46 12223 stop = value_true (ada_loc->excep_cond_expr->evaluate ());
28010a5d 12224 }
b1ffd112 12225 catch (const gdb_exception_error &ex)
492d29ea
PA
12226 {
12227 exception_fprintf (gdb_stderr, ex,
12228 _("Error in testing exception condition:\n"));
12229 }
492d29ea 12230
28010a5d
PA
12231 return stop;
12232}
12233
ae72050b
TT
12234/* Implement the CHECK_STATUS method in the structure for all
12235 exception catchpoint kinds. */
28010a5d 12236
ae72050b
TT
12237void
12238ada_catchpoint::check_status (bpstat *bs)
28010a5d 12239{
b6433ede 12240 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12241}
12242
ae72050b
TT
12243/* Implement the PRINT_IT method in the structure for all exception
12244 catchpoint kinds. */
f7f9143b 12245
ae72050b 12246enum print_stop_action
7bd86313 12247ada_catchpoint::print_it (const bpstat *bs) const
f7f9143b 12248{
79a45e25 12249 struct ui_out *uiout = current_uiout;
348d480f 12250
ae72050b 12251 annotate_catchpoint (number);
f7f9143b 12252
112e8700 12253 if (uiout->is_mi_like_p ())
f7f9143b 12254 {
112e8700 12255 uiout->field_string ("reason",
956a9fb9 12256 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ae72050b 12257 uiout->field_string ("disp", bpdisp_text (disposition));
f7f9143b
JB
12258 }
12259
ae72050b 12260 uiout->text (disposition == disp_del
112e8700 12261 ? "\nTemporary catchpoint " : "\nCatchpoint ");
78805ff8 12262 print_num_locno (bs, uiout);
112e8700 12263 uiout->text (", ");
f7f9143b 12264
45db7c09
PA
12265 /* ada_exception_name_addr relies on the selected frame being the
12266 current frame. Need to do this here because this function may be
12267 called more than once when printing a stop, and below, we'll
12268 select the first frame past the Ada run-time (see
12269 ada_find_printable_frame). */
12270 select_frame (get_current_frame ());
12271
ae72050b 12272 switch (m_kind)
f7f9143b 12273 {
761269c8
JB
12274 case ada_catch_exception:
12275 case ada_catch_exception_unhandled:
9f757bf7 12276 case ada_catch_handlers:
956a9fb9 12277 {
7bd86313 12278 const CORE_ADDR addr = ada_exception_name_addr (m_kind);
956a9fb9
JB
12279 char exception_name[256];
12280
12281 if (addr != 0)
12282 {
c714b426
PA
12283 read_memory (addr, (gdb_byte *) exception_name,
12284 sizeof (exception_name) - 1);
956a9fb9
JB
12285 exception_name [sizeof (exception_name) - 1] = '\0';
12286 }
12287 else
12288 {
12289 /* For some reason, we were unable to read the exception
12290 name. This could happen if the Runtime was compiled
12291 without debugging info, for instance. In that case,
12292 just replace the exception name by the generic string
12293 "exception" - it will read as "an exception" in the
12294 notification we are about to print. */
967cff16 12295 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12296 }
12297 /* In the case of unhandled exception breakpoints, we print
12298 the exception name as "unhandled EXCEPTION_NAME", to make
12299 it clearer to the user which kind of catchpoint just got
12300 hit. We used ui_out_text to make sure that this extra
12301 info does not pollute the exception name in the MI case. */
ae72050b 12302 if (m_kind == ada_catch_exception_unhandled)
112e8700
SM
12303 uiout->text ("unhandled ");
12304 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12305 }
12306 break;
761269c8 12307 case ada_catch_assert:
956a9fb9
JB
12308 /* In this case, the name of the exception is not really
12309 important. Just print "failed assertion" to make it clearer
12310 that his program just hit an assertion-failure catchpoint.
12311 We used ui_out_text because this info does not belong in
12312 the MI output. */
112e8700 12313 uiout->text ("failed assertion");
956a9fb9 12314 break;
f7f9143b 12315 }
e547c119 12316
6f46ac85 12317 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12318 if (exception_message != NULL)
12319 {
e547c119 12320 uiout->text (" (");
6f46ac85 12321 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12322 uiout->text (")");
e547c119
JB
12323 }
12324
112e8700 12325 uiout->text (" at ");
956a9fb9 12326 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12327
12328 return PRINT_SRC_AND_LOC;
12329}
12330
ae72050b
TT
12331/* Implement the PRINT_ONE method in the structure for all exception
12332 catchpoint kinds. */
f7f9143b 12333
ae72050b 12334bool
5e632eca 12335ada_catchpoint::print_one (const bp_location **last_loc) const
f7f9143b 12336{
79a45e25 12337 struct ui_out *uiout = current_uiout;
79a45b7d
TT
12338 struct value_print_options opts;
12339
12340 get_user_print_options (&opts);
f06f1252 12341
79a45b7d 12342 if (opts.addressprint)
f06f1252 12343 uiout->field_skip ("addr");
f7f9143b
JB
12344
12345 annotate_field (5);
ae72050b 12346 switch (m_kind)
f7f9143b 12347 {
761269c8 12348 case ada_catch_exception:
03f531ea 12349 if (!m_excep_string.empty ())
dda83cd7 12350 {
bc18fbb5 12351 std::string msg = string_printf (_("`%s' Ada exception"),
03f531ea 12352 m_excep_string.c_str ());
28010a5d 12353
dda83cd7
SM
12354 uiout->field_string ("what", msg);
12355 }
12356 else
12357 uiout->field_string ("what", "all Ada exceptions");
12358
12359 break;
f7f9143b 12360
761269c8 12361 case ada_catch_exception_unhandled:
dda83cd7
SM
12362 uiout->field_string ("what", "unhandled Ada exceptions");
12363 break;
f7f9143b 12364
9f757bf7 12365 case ada_catch_handlers:
03f531ea 12366 if (!m_excep_string.empty ())
dda83cd7 12367 {
9f757bf7
XR
12368 uiout->field_fmt ("what",
12369 _("`%s' Ada exception handlers"),
03f531ea 12370 m_excep_string.c_str ());
dda83cd7
SM
12371 }
12372 else
9f757bf7 12373 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12374 break;
9f757bf7 12375
761269c8 12376 case ada_catch_assert:
dda83cd7
SM
12377 uiout->field_string ("what", "failed Ada assertions");
12378 break;
f7f9143b
JB
12379
12380 default:
f34652de 12381 internal_error (_("unexpected catchpoint type"));
dda83cd7 12382 break;
f7f9143b 12383 }
c01e038b
TT
12384
12385 return true;
f7f9143b
JB
12386}
12387
12388/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12389 for all exception catchpoint kinds. */
12390
ae72050b 12391void
b713485d 12392ada_catchpoint::print_mention () const
f7f9143b 12393{
79a45e25 12394 struct ui_out *uiout = current_uiout;
28010a5d 12395
ae72050b 12396 uiout->text (disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12397 : _("Catchpoint "));
ae72050b 12398 uiout->field_signed ("bkptno", number);
112e8700 12399 uiout->text (": ");
00eb2c4a 12400
ae72050b 12401 switch (m_kind)
f7f9143b 12402 {
761269c8 12403 case ada_catch_exception:
03f531ea 12404 if (!m_excep_string.empty ())
00eb2c4a 12405 {
862d101a 12406 std::string info = string_printf (_("`%s' Ada exception"),
03f531ea 12407 m_excep_string.c_str ());
4915bfdc 12408 uiout->text (info);
00eb2c4a 12409 }
dda83cd7
SM
12410 else
12411 uiout->text (_("all Ada exceptions"));
12412 break;
f7f9143b 12413
761269c8 12414 case ada_catch_exception_unhandled:
dda83cd7
SM
12415 uiout->text (_("unhandled Ada exceptions"));
12416 break;
9f757bf7
XR
12417
12418 case ada_catch_handlers:
03f531ea 12419 if (!m_excep_string.empty ())
9f757bf7
XR
12420 {
12421 std::string info
12422 = string_printf (_("`%s' Ada exception handlers"),
03f531ea 12423 m_excep_string.c_str ());
4915bfdc 12424 uiout->text (info);
9f757bf7 12425 }
dda83cd7
SM
12426 else
12427 uiout->text (_("all Ada exceptions handlers"));
12428 break;
9f757bf7 12429
761269c8 12430 case ada_catch_assert:
dda83cd7
SM
12431 uiout->text (_("failed Ada assertions"));
12432 break;
f7f9143b
JB
12433
12434 default:
f34652de 12435 internal_error (_("unexpected catchpoint type"));
dda83cd7 12436 break;
f7f9143b
JB
12437 }
12438}
12439
ae72050b
TT
12440/* Implement the PRINT_RECREATE method in the structure for all
12441 exception catchpoint kinds. */
6149aea9 12442
ae72050b 12443void
4d1ae558 12444ada_catchpoint::print_recreate (struct ui_file *fp) const
6149aea9 12445{
ae72050b 12446 switch (m_kind)
6149aea9 12447 {
761269c8 12448 case ada_catch_exception:
6cb06a8c 12449 gdb_printf (fp, "catch exception");
03f531ea
TT
12450 if (!m_excep_string.empty ())
12451 gdb_printf (fp, " %s", m_excep_string.c_str ());
6149aea9
PA
12452 break;
12453
761269c8 12454 case ada_catch_exception_unhandled:
6cb06a8c 12455 gdb_printf (fp, "catch exception unhandled");
6149aea9
PA
12456 break;
12457
9f757bf7 12458 case ada_catch_handlers:
6cb06a8c 12459 gdb_printf (fp, "catch handlers");
9f757bf7
XR
12460 break;
12461
761269c8 12462 case ada_catch_assert:
6cb06a8c 12463 gdb_printf (fp, "catch assert");
6149aea9
PA
12464 break;
12465
12466 default:
f34652de 12467 internal_error (_("unexpected catchpoint type"));
6149aea9 12468 }
04d0163c 12469 print_recreate_thread (fp);
6149aea9
PA
12470}
12471
f06f1252
TT
12472/* See ada-lang.h. */
12473
12474bool
12475is_ada_exception_catchpoint (breakpoint *bp)
12476{
ae72050b 12477 return dynamic_cast<ada_catchpoint *> (bp) != nullptr;
f06f1252
TT
12478}
12479
f7f9143b
JB
12480/* Split the arguments specified in a "catch exception" command.
12481 Set EX to the appropriate catchpoint type.
28010a5d 12482 Set EXCEP_STRING to the name of the specific exception if
5845583d 12483 specified by the user.
9f757bf7
XR
12484 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12485 "catch handlers" command. False otherwise.
5845583d
JB
12486 If a condition is found at the end of the arguments, the condition
12487 expression is stored in COND_STRING (memory must be deallocated
12488 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12489
12490static void
a121b7c1 12491catch_ada_exception_command_split (const char *args,
9f757bf7 12492 bool is_catch_handlers_cmd,
dda83cd7 12493 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12494 std::string *excep_string,
12495 std::string *cond_string)
f7f9143b 12496{
bc18fbb5 12497 std::string exception_name;
f7f9143b 12498
bc18fbb5
TT
12499 exception_name = extract_arg (&args);
12500 if (exception_name == "if")
5845583d
JB
12501 {
12502 /* This is not an exception name; this is the start of a condition
12503 expression for a catchpoint on all exceptions. So, "un-get"
12504 this token, and set exception_name to NULL. */
bc18fbb5 12505 exception_name.clear ();
5845583d
JB
12506 args -= 2;
12507 }
f7f9143b 12508
5845583d 12509 /* Check to see if we have a condition. */
f7f9143b 12510
f1735a53 12511 args = skip_spaces (args);
61012eef 12512 if (startswith (args, "if")
5845583d
JB
12513 && (isspace (args[2]) || args[2] == '\0'))
12514 {
12515 args += 2;
f1735a53 12516 args = skip_spaces (args);
5845583d
JB
12517
12518 if (args[0] == '\0')
dda83cd7 12519 error (_("Condition missing after `if' keyword"));
bc18fbb5 12520 *cond_string = args;
5845583d
JB
12521
12522 args += strlen (args);
12523 }
12524
12525 /* Check that we do not have any more arguments. Anything else
12526 is unexpected. */
f7f9143b
JB
12527
12528 if (args[0] != '\0')
12529 error (_("Junk at end of expression"));
12530
9f757bf7
XR
12531 if (is_catch_handlers_cmd)
12532 {
12533 /* Catch handling of exceptions. */
12534 *ex = ada_catch_handlers;
12535 *excep_string = exception_name;
12536 }
bc18fbb5 12537 else if (exception_name.empty ())
f7f9143b
JB
12538 {
12539 /* Catch all exceptions. */
761269c8 12540 *ex = ada_catch_exception;
bc18fbb5 12541 excep_string->clear ();
f7f9143b 12542 }
bc18fbb5 12543 else if (exception_name == "unhandled")
f7f9143b
JB
12544 {
12545 /* Catch unhandled exceptions. */
761269c8 12546 *ex = ada_catch_exception_unhandled;
bc18fbb5 12547 excep_string->clear ();
f7f9143b
JB
12548 }
12549 else
12550 {
12551 /* Catch a specific exception. */
761269c8 12552 *ex = ada_catch_exception;
28010a5d 12553 *excep_string = exception_name;
f7f9143b
JB
12554 }
12555}
12556
12557/* Return the name of the symbol on which we should break in order to
12558 implement a catchpoint of the EX kind. */
12559
12560static const char *
761269c8 12561ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12562{
3eecfa55
JB
12563 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12564
12565 gdb_assert (data->exception_info != NULL);
0259addd 12566
f7f9143b
JB
12567 switch (ex)
12568 {
761269c8 12569 case ada_catch_exception:
dda83cd7
SM
12570 return (data->exception_info->catch_exception_sym);
12571 break;
761269c8 12572 case ada_catch_exception_unhandled:
dda83cd7
SM
12573 return (data->exception_info->catch_exception_unhandled_sym);
12574 break;
761269c8 12575 case ada_catch_assert:
dda83cd7
SM
12576 return (data->exception_info->catch_assert_sym);
12577 break;
9f757bf7 12578 case ada_catch_handlers:
dda83cd7
SM
12579 return (data->exception_info->catch_handlers_sym);
12580 break;
f7f9143b 12581 default:
f34652de 12582 internal_error (_("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12583 }
12584}
12585
f7f9143b
JB
12586/* Return the condition that will be used to match the current exception
12587 being raised with the exception that the user wants to catch. This
12588 assumes that this condition is used when the inferior just triggered
12589 an exception catchpoint.
cb7de75e 12590 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12591
cb7de75e 12592static std::string
9f757bf7 12593ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12594 enum ada_exception_catchpoint_kind ex)
f7f9143b 12595{
fccf9de1 12596 bool is_standard_exc = false;
cb7de75e 12597 std::string result;
9f757bf7
XR
12598
12599 if (ex == ada_catch_handlers)
12600 {
12601 /* For exception handlers catchpoints, the condition string does
dda83cd7 12602 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12603 result = ("long_integer (GNAT_GCC_exception_Access"
12604 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12605 }
12606 else
fccf9de1 12607 result = "long_integer (e)";
3d0b0fa3 12608
0963b4bd 12609 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12610 runtime units that have been compiled without debugging info; if
28010a5d 12611 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12612 exception (e.g. "constraint_error") then, during the evaluation
12613 of the condition expression, the symbol lookup on this name would
0963b4bd 12614 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12615 may then be set only on user-defined exceptions which have the
12616 same not-fully-qualified name (e.g. my_package.constraint_error).
12617
12618 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12619 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12620 exception constraint_error" is rewritten into "catch exception
12621 standard.constraint_error".
12622
85102364 12623 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12624 the inferior program, then the only way to specify this exception as a
12625 breakpoint condition is to use its fully-qualified named:
fccf9de1 12626 e.g. my_package.constraint_error. */
3d0b0fa3 12627
696d6f4d 12628 for (const char *name : standard_exc)
3d0b0fa3 12629 {
696d6f4d 12630 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12631 {
fccf9de1 12632 is_standard_exc = true;
9f757bf7 12633 break;
3d0b0fa3
JB
12634 }
12635 }
9f757bf7 12636
fccf9de1
TT
12637 result += " = ";
12638
12639 if (is_standard_exc)
12640 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12641 else
12642 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12643
9f757bf7 12644 return result;
f7f9143b
JB
12645}
12646
2c4c710f
TT
12647/* Return the symtab_and_line that should be used to insert an
12648 exception catchpoint of the TYPE kind. */
f7f9143b
JB
12649
12650static struct symtab_and_line
2c4c710f 12651ada_exception_sal (enum ada_exception_catchpoint_kind ex)
f7f9143b
JB
12652{
12653 const char *sym_name;
12654 struct symbol *sym;
f7f9143b 12655
0259addd
JB
12656 /* First, find out which exception support info to use. */
12657 ada_exception_support_info_sniffer ();
12658
12659 /* Then lookup the function on which we will break in order to catch
f7f9143b 12660 the Ada exceptions requested by the user. */
f7f9143b
JB
12661 sym_name = ada_exception_sym_name (ex);
12662 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12663
57aff202 12664 if (sym == NULL)
2c4c710f
TT
12665 throw_error (NOT_FOUND_ERROR, _("Catchpoint symbol not found: %s"),
12666 sym_name);
57aff202 12667
66d7f48f 12668 if (sym->aclass () != LOC_BLOCK)
57aff202 12669 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b 12670
f17011e0 12671 return find_function_start_sal (sym, 1);
f7f9143b
JB
12672}
12673
b4a5b78b 12674/* Create an Ada exception catchpoint.
f7f9143b 12675
b4a5b78b 12676 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12677
bc18fbb5 12678 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12679 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12680 of the exception to which this catchpoint applies.
2df4d1d5 12681
bc18fbb5 12682 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12683
b4a5b78b
JB
12684 TEMPFLAG, if nonzero, means that the underlying breakpoint
12685 should be temporary.
28010a5d 12686
b4a5b78b 12687 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12688
349774ef 12689void
28010a5d 12690create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12691 enum ada_exception_catchpoint_kind ex_kind,
898db0f7 12692 std::string &&excep_string,
56ecd069 12693 const std::string &cond_string,
28010a5d 12694 int tempflag,
12d67b37 12695 int enabled,
28010a5d
PA
12696 int from_tty)
12697{
bd21b6c9 12698 std::unique_ptr<ada_catchpoint> c
2c4c710f
TT
12699 (new ada_catchpoint (gdbarch, ex_kind,
12700 cond_string.empty () ? nullptr : cond_string.c_str (),
898db0f7
TT
12701 tempflag, enabled, from_tty,
12702 std::move (excep_string)));
b270e6f9 12703 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12704}
12705
9ac4176b
PA
12706/* Implement the "catch exception" command. */
12707
12708static void
eb4c3f4a 12709catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12710 struct cmd_list_element *command)
12711{
a121b7c1 12712 const char *arg = arg_entry;
9ac4176b
PA
12713 struct gdbarch *gdbarch = get_current_arch ();
12714 int tempflag;
761269c8 12715 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12716 std::string excep_string;
56ecd069 12717 std::string cond_string;
9ac4176b 12718
0f8e2034 12719 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12720
12721 if (!arg)
12722 arg = "";
9f757bf7 12723 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12724 &cond_string);
9f757bf7 12725 create_ada_exception_catchpoint (gdbarch, ex_kind,
898db0f7 12726 std::move (excep_string), cond_string,
9f757bf7
XR
12727 tempflag, 1 /* enabled */,
12728 from_tty);
12729}
12730
12731/* Implement the "catch handlers" command. */
12732
12733static void
12734catch_ada_handlers_command (const char *arg_entry, int from_tty,
12735 struct cmd_list_element *command)
12736{
12737 const char *arg = arg_entry;
12738 struct gdbarch *gdbarch = get_current_arch ();
12739 int tempflag;
12740 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12741 std::string excep_string;
56ecd069 12742 std::string cond_string;
9f757bf7 12743
0f8e2034 12744 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12745
12746 if (!arg)
12747 arg = "";
12748 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12749 &cond_string);
b4a5b78b 12750 create_ada_exception_catchpoint (gdbarch, ex_kind,
898db0f7 12751 std::move (excep_string), cond_string,
349774ef
JB
12752 tempflag, 1 /* enabled */,
12753 from_tty);
9ac4176b
PA
12754}
12755
71bed2db
TT
12756/* Completion function for the Ada "catch" commands. */
12757
12758static void
12759catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12760 const char *text, const char *word)
12761{
12762 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12763
12764 for (const ada_exc_info &info : exceptions)
12765 {
12766 if (startswith (info.name, word))
b02f78f9 12767 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12768 }
12769}
12770
b4a5b78b 12771/* Split the arguments specified in a "catch assert" command.
5845583d 12772
b4a5b78b
JB
12773 ARGS contains the command's arguments (or the empty string if
12774 no arguments were passed).
5845583d
JB
12775
12776 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12777 (the memory needs to be deallocated after use). */
5845583d 12778
b4a5b78b 12779static void
56ecd069 12780catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12781{
f1735a53 12782 args = skip_spaces (args);
f7f9143b 12783
5845583d 12784 /* Check whether a condition was provided. */
61012eef 12785 if (startswith (args, "if")
5845583d 12786 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12787 {
5845583d 12788 args += 2;
f1735a53 12789 args = skip_spaces (args);
5845583d 12790 if (args[0] == '\0')
dda83cd7 12791 error (_("condition missing after `if' keyword"));
56ecd069 12792 cond_string.assign (args);
f7f9143b
JB
12793 }
12794
5845583d
JB
12795 /* Otherwise, there should be no other argument at the end of
12796 the command. */
12797 else if (args[0] != '\0')
12798 error (_("Junk at end of arguments."));
f7f9143b
JB
12799}
12800
9ac4176b
PA
12801/* Implement the "catch assert" command. */
12802
12803static void
eb4c3f4a 12804catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12805 struct cmd_list_element *command)
12806{
a121b7c1 12807 const char *arg = arg_entry;
9ac4176b
PA
12808 struct gdbarch *gdbarch = get_current_arch ();
12809 int tempflag;
56ecd069 12810 std::string cond_string;
9ac4176b 12811
0f8e2034 12812 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12813
12814 if (!arg)
12815 arg = "";
56ecd069 12816 catch_ada_assert_command_split (arg, cond_string);
761269c8 12817 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
898db0f7 12818 {}, cond_string,
349774ef
JB
12819 tempflag, 1 /* enabled */,
12820 from_tty);
9ac4176b 12821}
778865d3
JB
12822
12823/* Return non-zero if the symbol SYM is an Ada exception object. */
12824
12825static int
12826ada_is_exception_sym (struct symbol *sym)
12827{
5f9c5a63 12828 const char *type_name = sym->type ()->name ();
778865d3 12829
66d7f48f
SM
12830 return (sym->aclass () != LOC_TYPEDEF
12831 && sym->aclass () != LOC_BLOCK
12832 && sym->aclass () != LOC_CONST
12833 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12834 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12835}
12836
12837/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12838 Ada exception object. This matches all exceptions except the ones
12839 defined by the Ada language. */
12840
12841static int
12842ada_is_non_standard_exception_sym (struct symbol *sym)
12843{
778865d3
JB
12844 if (!ada_is_exception_sym (sym))
12845 return 0;
12846
696d6f4d
TT
12847 for (const char *name : standard_exc)
12848 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12849 return 0; /* A standard exception. */
12850
12851 /* Numeric_Error is also a standard exception, so exclude it.
12852 See the STANDARD_EXC description for more details as to why
12853 this exception is not listed in that array. */
987012b8 12854 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12855 return 0;
12856
12857 return 1;
12858}
12859
ab816a27 12860/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12861 objects.
12862
12863 The comparison is determined first by exception name, and then
12864 by exception address. */
12865
ab816a27 12866bool
cc536b21 12867ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12868{
778865d3
JB
12869 int result;
12870
ab816a27
TT
12871 result = strcmp (name, other.name);
12872 if (result < 0)
12873 return true;
12874 if (result == 0 && addr < other.addr)
12875 return true;
12876 return false;
12877}
778865d3 12878
ab816a27 12879bool
cc536b21 12880ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12881{
12882 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12883}
12884
12885/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12886 routine, but keeping the first SKIP elements untouched.
12887
12888 All duplicates are also removed. */
12889
12890static void
ab816a27 12891sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12892 int skip)
12893{
ab816a27
TT
12894 std::sort (exceptions->begin () + skip, exceptions->end ());
12895 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12896 exceptions->end ());
778865d3
JB
12897}
12898
778865d3
JB
12899/* Add all exceptions defined by the Ada standard whose name match
12900 a regular expression.
12901
12902 If PREG is not NULL, then this regexp_t object is used to
12903 perform the symbol name matching. Otherwise, no name-based
12904 filtering is performed.
12905
12906 EXCEPTIONS is a vector of exceptions to which matching exceptions
12907 gets pushed. */
12908
12909static void
2d7cc5c7 12910ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12911 std::vector<ada_exc_info> *exceptions)
778865d3 12912{
696d6f4d 12913 for (const char *name : standard_exc)
778865d3 12914 {
696d6f4d 12915 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3 12916 {
4326580d
MM
12917 symbol_name_match_type match_type = name_match_type_from_name (name);
12918 lookup_name_info lookup_name (name, match_type);
778865d3 12919
4326580d
MM
12920 symbol_name_matcher_ftype *match_name
12921 = ada_get_symbol_name_matcher (lookup_name);
778865d3 12922
4326580d
MM
12923 /* Iterate over all objfiles irrespective of scope or linker
12924 namespaces so we get all exceptions anywhere in the
12925 progspace. */
12926 for (objfile *objfile : current_program_space->objfiles ())
12927 {
12928 for (minimal_symbol *msymbol : objfile->msymbols ())
12929 {
12930 if (match_name (msymbol->linkage_name (), lookup_name,
12931 nullptr)
12932 && msymbol->type () != mst_solib_trampoline)
12933 {
12934 ada_exc_info info
12935 = {name, msymbol->value_address (objfile)};
12936
12937 exceptions->push_back (info);
12938 }
12939 }
778865d3
JB
12940 }
12941 }
12942 }
12943}
12944
12945/* Add all Ada exceptions defined locally and accessible from the given
12946 FRAME.
12947
12948 If PREG is not NULL, then this regexp_t object is used to
12949 perform the symbol name matching. Otherwise, no name-based
12950 filtering is performed.
12951
12952 EXCEPTIONS is a vector of exceptions to which matching exceptions
12953 gets pushed. */
12954
12955static void
2d7cc5c7 12956ada_add_exceptions_from_frame (compiled_regex *preg,
bd2b40ac 12957 frame_info_ptr frame,
ab816a27 12958 std::vector<ada_exc_info> *exceptions)
778865d3 12959{
3977b71f 12960 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
12961
12962 while (block != 0)
12963 {
548a89df 12964 for (struct symbol *sym : block_iterator_range (block))
778865d3 12965 {
66d7f48f 12966 switch (sym->aclass ())
778865d3
JB
12967 {
12968 case LOC_TYPEDEF:
12969 case LOC_BLOCK:
12970 case LOC_CONST:
12971 break;
12972 default:
12973 if (ada_is_exception_sym (sym))
12974 {
987012b8 12975 struct ada_exc_info info = {sym->print_name (),
4aeddc50 12976 sym->value_address ()};
778865d3 12977
ab816a27 12978 exceptions->push_back (info);
778865d3
JB
12979 }
12980 }
12981 }
6c00f721 12982 if (block->function () != NULL)
778865d3 12983 break;
f135fe72 12984 block = block->superblock ();
778865d3
JB
12985 }
12986}
12987
14bc53a8
PA
12988/* Return true if NAME matches PREG or if PREG is NULL. */
12989
12990static bool
2d7cc5c7 12991name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
12992{
12993 return (preg == NULL
f945dedf 12994 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
12995}
12996
778865d3
JB
12997/* Add all exceptions defined globally whose name name match
12998 a regular expression, excluding standard exceptions.
12999
13000 The reason we exclude standard exceptions is that they need
13001 to be handled separately: Standard exceptions are defined inside
13002 a runtime unit which is normally not compiled with debugging info,
13003 and thus usually do not show up in our symbol search. However,
13004 if the unit was in fact built with debugging info, we need to
13005 exclude them because they would duplicate the entry we found
13006 during the special loop that specifically searches for those
13007 standard exceptions.
13008
13009 If PREG is not NULL, then this regexp_t object is used to
13010 perform the symbol name matching. Otherwise, no name-based
13011 filtering is performed.
13012
13013 EXCEPTIONS is a vector of exceptions to which matching exceptions
13014 gets pushed. */
13015
13016static void
2d7cc5c7 13017ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13018 std::vector<ada_exc_info> *exceptions)
778865d3 13019{
14bc53a8
PA
13020 /* In Ada, the symbol "search name" is a linkage name, whereas the
13021 regular expression used to do the matching refers to the natural
13022 name. So match against the decoded name. */
13023 expand_symtabs_matching (NULL,
b5ec771e 13024 lookup_name_info::match_any (),
14bc53a8
PA
13025 [&] (const char *search_name)
13026 {
f945dedf
CB
13027 std::string decoded = ada_decode (search_name);
13028 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13029 },
13030 NULL,
03a8ea51 13031 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 13032 VARIABLES_DOMAIN);
778865d3 13033
4326580d
MM
13034 /* Iterate over all objfiles irrespective of scope or linker namespaces
13035 so we get all exceptions anywhere in the progspace. */
2030c079 13036 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13037 {
b669c953 13038 for (compunit_symtab *s : objfile->compunits ())
778865d3 13039 {
af39c5c8 13040 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13041 int i;
778865d3 13042
d8aeb77f
TT
13043 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13044 {
63d609de 13045 const struct block *b = bv->block (i);
778865d3 13046
548a89df 13047 for (struct symbol *sym : block_iterator_range (b))
d8aeb77f 13048 if (ada_is_non_standard_exception_sym (sym)
987012b8 13049 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13050 {
13051 struct ada_exc_info info
4aeddc50 13052 = {sym->print_name (), sym->value_address ()};
d8aeb77f
TT
13053
13054 exceptions->push_back (info);
13055 }
13056 }
778865d3
JB
13057 }
13058 }
13059}
13060
13061/* Implements ada_exceptions_list with the regular expression passed
13062 as a regex_t, rather than a string.
13063
13064 If not NULL, PREG is used to filter out exceptions whose names
13065 do not match. Otherwise, all exceptions are listed. */
13066
ab816a27 13067static std::vector<ada_exc_info>
2d7cc5c7 13068ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13069{
ab816a27 13070 std::vector<ada_exc_info> result;
778865d3
JB
13071 int prev_len;
13072
13073 /* First, list the known standard exceptions. These exceptions
13074 need to be handled separately, as they are usually defined in
13075 runtime units that have been compiled without debugging info. */
13076
13077 ada_add_standard_exceptions (preg, &result);
13078
13079 /* Next, find all exceptions whose scope is local and accessible
13080 from the currently selected frame. */
13081
13082 if (has_stack_frames ())
13083 {
ab816a27 13084 prev_len = result.size ();
778865d3
JB
13085 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13086 &result);
ab816a27 13087 if (result.size () > prev_len)
778865d3
JB
13088 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13089 }
13090
13091 /* Add all exceptions whose scope is global. */
13092
ab816a27 13093 prev_len = result.size ();
778865d3 13094 ada_add_global_exceptions (preg, &result);
ab816a27 13095 if (result.size () > prev_len)
778865d3
JB
13096 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13097
778865d3
JB
13098 return result;
13099}
13100
13101/* Return a vector of ada_exc_info.
13102
13103 If REGEXP is NULL, all exceptions are included in the result.
13104 Otherwise, it should contain a valid regular expression,
13105 and only the exceptions whose names match that regular expression
13106 are included in the result.
13107
13108 The exceptions are sorted in the following order:
13109 - Standard exceptions (defined by the Ada language), in
13110 alphabetical order;
13111 - Exceptions only visible from the current frame, in
13112 alphabetical order;
13113 - Exceptions whose scope is global, in alphabetical order. */
13114
ab816a27 13115std::vector<ada_exc_info>
778865d3
JB
13116ada_exceptions_list (const char *regexp)
13117{
2d7cc5c7
PA
13118 if (regexp == NULL)
13119 return ada_exceptions_list_1 (NULL);
778865d3 13120
2d7cc5c7
PA
13121 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13122 return ada_exceptions_list_1 (&reg);
778865d3
JB
13123}
13124
13125/* Implement the "info exceptions" command. */
13126
13127static void
1d12d88f 13128info_exceptions_command (const char *regexp, int from_tty)
778865d3 13129{
778865d3 13130 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13131
ab816a27 13132 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13133
13134 if (regexp != NULL)
6cb06a8c 13135 gdb_printf
778865d3
JB
13136 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13137 else
6cb06a8c 13138 gdb_printf (_("All defined Ada exceptions:\n"));
778865d3 13139
ab816a27 13140 for (const ada_exc_info &info : exceptions)
6cb06a8c 13141 gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13142}
13143
6c038f32
PH
13144\f
13145 /* Language vector */
13146
b5ec771e
PA
13147/* symbol_name_matcher_ftype adapter for wild_match. */
13148
13149static bool
13150do_wild_match (const char *symbol_search_name,
13151 const lookup_name_info &lookup_name,
a207cff2 13152 completion_match_result *comp_match_res)
b5ec771e
PA
13153{
13154 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13155}
13156
13157/* symbol_name_matcher_ftype adapter for full_match. */
13158
13159static bool
13160do_full_match (const char *symbol_search_name,
13161 const lookup_name_info &lookup_name,
a207cff2 13162 completion_match_result *comp_match_res)
b5ec771e 13163{
959d6a67
TT
13164 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13165
13166 /* If both symbols start with "_ada_", just let the loop below
13167 handle the comparison. However, if only the symbol name starts
13168 with "_ada_", skip the prefix and let the match proceed as
13169 usual. */
13170 if (startswith (symbol_search_name, "_ada_")
13171 && !startswith (lname, "_ada"))
86b44259 13172 symbol_search_name += 5;
81eaa506
TT
13173 /* Likewise for ghost entities. */
13174 if (startswith (symbol_search_name, "___ghost_")
13175 && !startswith (lname, "___ghost_"))
13176 symbol_search_name += 9;
86b44259 13177
86b44259
TT
13178 int uscore_count = 0;
13179 while (*lname != '\0')
13180 {
13181 if (*symbol_search_name != *lname)
13182 {
13183 if (*symbol_search_name == 'B' && uscore_count == 2
13184 && symbol_search_name[1] == '_')
13185 {
13186 symbol_search_name += 2;
13187 while (isdigit (*symbol_search_name))
13188 ++symbol_search_name;
13189 if (symbol_search_name[0] == '_'
13190 && symbol_search_name[1] == '_')
13191 {
13192 symbol_search_name += 2;
13193 continue;
13194 }
13195 }
13196 return false;
13197 }
13198
13199 if (*symbol_search_name == '_')
13200 ++uscore_count;
13201 else
13202 uscore_count = 0;
13203
13204 ++symbol_search_name;
13205 ++lname;
13206 }
13207
13208 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13209}
13210
a2cd4f14
JB
13211/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13212
13213static bool
13214do_exact_match (const char *symbol_search_name,
13215 const lookup_name_info &lookup_name,
13216 completion_match_result *comp_match_res)
13217{
13218 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13219}
13220
b5ec771e
PA
13221/* Build the Ada lookup name for LOOKUP_NAME. */
13222
13223ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13224{
e0802d59 13225 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13226
6a780b67 13227 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13228 {
13229 if (user_name.back () == '>')
e0802d59 13230 m_encoded_name
5ac58899 13231 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13232 else
e0802d59 13233 m_encoded_name
5ac58899 13234 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13235 m_encoded_p = true;
13236 m_verbatim_p = true;
13237 m_wild_match_p = false;
13238 m_standard_p = false;
13239 }
13240 else
13241 {
13242 m_verbatim_p = false;
13243
e0802d59 13244 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13245
13246 if (!m_encoded_p)
13247 {
e0802d59 13248 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13249 m_encoded_name = ada_encode_1 (folded, false);
13250 if (m_encoded_name.empty ())
5ac58899 13251 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13252 }
13253 else
5ac58899 13254 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13255
13256 /* Handle the 'package Standard' special case. See description
13257 of m_standard_p. */
13258 if (startswith (m_encoded_name.c_str (), "standard__"))
13259 {
13260 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13261 m_standard_p = true;
13262 }
13263 else
13264 m_standard_p = false;
74ccd7f5 13265
b5ec771e
PA
13266 /* If the name contains a ".", then the user is entering a fully
13267 qualified entity name, and the match must not be done in wild
13268 mode. Similarly, if the user wants to complete what looks
13269 like an encoded name, the match must not be done in wild
13270 mode. Also, in the standard__ special case always do
13271 non-wild matching. */
13272 m_wild_match_p
13273 = (lookup_name.match_type () != symbol_name_match_type::FULL
13274 && !m_encoded_p
13275 && !m_standard_p
13276 && user_name.find ('.') == std::string::npos);
13277 }
13278}
13279
13280/* symbol_name_matcher_ftype method for Ada. This only handles
13281 completion mode. */
13282
13283static bool
13284ada_symbol_name_matches (const char *symbol_search_name,
13285 const lookup_name_info &lookup_name,
a207cff2 13286 completion_match_result *comp_match_res)
74ccd7f5 13287{
b5ec771e
PA
13288 return lookup_name.ada ().matches (symbol_search_name,
13289 lookup_name.match_type (),
a207cff2 13290 comp_match_res);
b5ec771e
PA
13291}
13292
de63c46b
PA
13293/* A name matcher that matches the symbol name exactly, with
13294 strcmp. */
13295
13296static bool
13297literal_symbol_name_matcher (const char *symbol_search_name,
13298 const lookup_name_info &lookup_name,
13299 completion_match_result *comp_match_res)
13300{
e0802d59 13301 gdb::string_view name_view = lookup_name.name ();
de63c46b 13302
e0802d59
TT
13303 if (lookup_name.completion_mode ()
13304 ? (strncmp (symbol_search_name, name_view.data (),
13305 name_view.size ()) == 0)
13306 : symbol_search_name == name_view)
de63c46b
PA
13307 {
13308 if (comp_match_res != NULL)
13309 comp_match_res->set_match (symbol_search_name);
13310 return true;
13311 }
13312 else
13313 return false;
13314}
13315
c9debfb9 13316/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13317 Ada. */
13318
13319static symbol_name_matcher_ftype *
13320ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13321{
de63c46b
PA
13322 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13323 return literal_symbol_name_matcher;
13324
b5ec771e
PA
13325 if (lookup_name.completion_mode ())
13326 return ada_symbol_name_matches;
74ccd7f5 13327 else
b5ec771e
PA
13328 {
13329 if (lookup_name.ada ().wild_match_p ())
13330 return do_wild_match;
a2cd4f14
JB
13331 else if (lookup_name.ada ().verbatim_p ())
13332 return do_exact_match;
b5ec771e
PA
13333 else
13334 return do_full_match;
13335 }
74ccd7f5
JB
13336}
13337
0874fd07
AB
13338/* Class representing the Ada language. */
13339
13340class ada_language : public language_defn
13341{
13342public:
13343 ada_language ()
0e25e767 13344 : language_defn (language_ada)
0874fd07 13345 { /* Nothing. */ }
5bd40f2a 13346
6f7664a9
AB
13347 /* See language.h. */
13348
13349 const char *name () const override
13350 { return "ada"; }
13351
13352 /* See language.h. */
13353
13354 const char *natural_name () const override
13355 { return "Ada"; }
13356
e171d6f1
AB
13357 /* See language.h. */
13358
13359 const std::vector<const char *> &filename_extensions () const override
13360 {
13361 static const std::vector<const char *> extensions
13362 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13363 return extensions;
13364 }
13365
5bd40f2a
AB
13366 /* Print an array element index using the Ada syntax. */
13367
13368 void print_array_index (struct type *index_type,
13369 LONGEST index,
13370 struct ui_file *stream,
13371 const value_print_options *options) const override
13372 {
13373 struct value *index_value = val_atr (index_type, index);
13374
00c696a6 13375 value_print (index_value, stream, options);
6cb06a8c 13376 gdb_printf (stream, " => ");
5bd40f2a 13377 }
15e5fd35
AB
13378
13379 /* Implement the "read_var_value" language_defn method for Ada. */
13380
13381 struct value *read_var_value (struct symbol *var,
13382 const struct block *var_block,
bd2b40ac 13383 frame_info_ptr frame) const override
15e5fd35
AB
13384 {
13385 /* The only case where default_read_var_value is not sufficient
13386 is when VAR is a renaming... */
13387 if (frame != nullptr)
13388 {
13389 const struct block *frame_block = get_frame_block (frame, NULL);
13390 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13391 return ada_read_renaming_var_value (var, frame_block);
13392 }
13393
13394 /* This is a typical case where we expect the default_read_var_value
13395 function to work. */
13396 return language_defn::read_var_value (var, var_block, frame);
13397 }
1fb314aa 13398
2c71f639 13399 /* See language.h. */
496feb16 13400 bool symbol_printing_suppressed (struct symbol *symbol) const override
2c71f639 13401 {
496feb16 13402 return symbol->is_artificial ();
2c71f639
TV
13403 }
13404
baab3753
AB
13405 /* See language.h. */
13406 struct value *value_string (struct gdbarch *gdbarch,
13407 const char *ptr, ssize_t len) const override
13408 {
13409 struct type *type = language_string_char_type (this, gdbarch);
13410 value *val = ::value_string (ptr, len, type);
13411 /* VAL will be a TYPE_CODE_STRING, but Ada only knows how to print
13412 strings that are arrays of characters, so fix the type now. */
13413 gdb_assert (val->type ()->code () == TYPE_CODE_STRING);
13414 val->type ()->set_code (TYPE_CODE_ARRAY);
13415 return val;
13416 }
13417
1fb314aa
AB
13418 /* See language.h. */
13419 void language_arch_info (struct gdbarch *gdbarch,
13420 struct language_arch_info *lai) const override
13421 {
13422 const struct builtin_type *builtin = builtin_type (gdbarch);
13423
7bea47f0
AB
13424 /* Helper function to allow shorter lines below. */
13425 auto add = [&] (struct type *t)
13426 {
13427 lai->add_primitive_type (t);
13428 };
13429
cc495054 13430 type_allocator alloc (gdbarch);
2d39ccd3 13431 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13432 0, "integer"));
2d39ccd3 13433 add (init_integer_type (alloc, gdbarch_long_bit (gdbarch),
7bea47f0 13434 0, "long_integer"));
2d39ccd3 13435 add (init_integer_type (alloc, gdbarch_short_bit (gdbarch),
7bea47f0 13436 0, "short_integer"));
f50b437c 13437 struct type *char_type = init_character_type (alloc, TARGET_CHAR_BIT,
c9f66f00 13438 1, "character");
7bea47f0
AB
13439 lai->set_string_char_type (char_type);
13440 add (char_type);
f50b437c
TT
13441 add (init_character_type (alloc, 16, 1, "wide_character"));
13442 add (init_character_type (alloc, 32, 1, "wide_wide_character"));
77c5f496 13443 add (init_float_type (alloc, gdbarch_float_bit (gdbarch),
7bea47f0 13444 "float", gdbarch_float_format (gdbarch)));
77c5f496 13445 add (init_float_type (alloc, gdbarch_double_bit (gdbarch),
7bea47f0 13446 "long_float", gdbarch_double_format (gdbarch)));
2d39ccd3 13447 add (init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
7bea47f0 13448 0, "long_long_integer"));
e49831ba
TT
13449 add (init_integer_type (alloc, 128, 0, "long_long_long_integer"));
13450 add (init_integer_type (alloc, 128, 1, "unsigned_long_long_long_integer"));
77c5f496 13451 add (init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
7bea47f0
AB
13452 "long_long_float",
13453 gdbarch_long_double_format (gdbarch)));
2d39ccd3 13454 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13455 0, "natural"));
2d39ccd3 13456 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0
AB
13457 0, "positive"));
13458 add (builtin->builtin_void);
13459
13460 struct type *system_addr_ptr
cc495054
TT
13461 = lookup_pointer_type (alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT,
13462 "void"));
7bea47f0
AB
13463 system_addr_ptr->set_name ("system__address");
13464 add (system_addr_ptr);
1fb314aa
AB
13465
13466 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13467 type. This is a signed integral type whose size is the same as
13468 the size of addresses. */
df86565b 13469 unsigned int addr_length = system_addr_ptr->length ();
2d39ccd3 13470 add (init_integer_type (alloc, addr_length * HOST_CHAR_BIT, 0,
7bea47f0 13471 "storage_offset"));
1fb314aa 13472
7bea47f0 13473 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13474 }
4009ee92
AB
13475
13476 /* See language.h. */
13477
13478 bool iterate_over_symbols
13479 (const struct block *block, const lookup_name_info &name,
13480 domain_enum domain,
13481 gdb::function_view<symbol_found_callback_ftype> callback) const override
13482 {
d1183b06
TT
13483 std::vector<struct block_symbol> results
13484 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13485 for (block_symbol &sym : results)
13486 {
13487 if (!callback (&sym))
13488 return false;
13489 }
13490
13491 return true;
13492 }
6f827019
AB
13493
13494 /* See language.h. */
3456e70c
TT
13495 bool sniff_from_mangled_name
13496 (const char *mangled,
13497 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13498 {
13499 std::string demangled = ada_decode (mangled);
13500
13501 *out = NULL;
13502
13503 if (demangled != mangled && demangled[0] != '<')
13504 {
13505 /* Set the gsymbol language to Ada, but still return 0.
13506 Two reasons for that:
13507
13508 1. For Ada, we prefer computing the symbol's decoded name
13509 on the fly rather than pre-compute it, in order to save
13510 memory (Ada projects are typically very large).
13511
13512 2. There are some areas in the definition of the GNAT
13513 encoding where, with a bit of bad luck, we might be able
13514 to decode a non-Ada symbol, generating an incorrect
13515 demangled name (Eg: names ending with "TB" for instance
13516 are identified as task bodies and so stripped from
13517 the decoded name returned).
13518
13519 Returning true, here, but not setting *DEMANGLED, helps us get
13520 a little bit of the best of both worlds. Because we're last,
13521 we should not affect any of the other languages that were
13522 able to demangle the symbol before us; we get to correctly
13523 tag Ada symbols as such; and even if we incorrectly tagged a
13524 non-Ada symbol, which should be rare, any routing through the
13525 Ada language should be transparent (Ada tries to behave much
13526 like C/C++ with non-Ada symbols). */
13527 return true;
13528 }
13529
13530 return false;
13531 }
fbfb0a46
AB
13532
13533 /* See language.h. */
13534
3456e70c
TT
13535 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13536 int options) const override
0a50df5d 13537 {
3456e70c 13538 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13539 }
13540
13541 /* See language.h. */
13542
fbfb0a46
AB
13543 void print_type (struct type *type, const char *varstring,
13544 struct ui_file *stream, int show, int level,
13545 const struct type_print_options *flags) const override
13546 {
13547 ada_print_type (type, varstring, stream, show, level, flags);
13548 }
c9debfb9 13549
53fc67f8
AB
13550 /* See language.h. */
13551
13552 const char *word_break_characters (void) const override
13553 {
13554 return ada_completer_word_break_characters;
13555 }
13556
7e56227d
AB
13557 /* See language.h. */
13558
13559 void collect_symbol_completion_matches (completion_tracker &tracker,
13560 complete_symbol_mode mode,
13561 symbol_name_match_type name_match_type,
13562 const char *text, const char *word,
13563 enum type_code code) const override
13564 {
7e56227d 13565 const struct block *b, *surrounding_static_block = 0;
7e56227d
AB
13566
13567 gdb_assert (code == TYPE_CODE_UNDEF);
13568
13569 lookup_name_info lookup_name (text, name_match_type, true);
13570
13571 /* First, look at the partial symtab symbols. */
13572 expand_symtabs_matching (NULL,
13573 lookup_name,
13574 NULL,
13575 NULL,
03a8ea51 13576 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13577 ALL_DOMAIN);
13578
13579 /* At this point scan through the misc symbol vectors and add each
13580 symbol you find to the list. Eventually we want to ignore
13581 anything that isn't a text symbol (everything else will be
13582 handled by the psymtab code above). */
13583
13584 for (objfile *objfile : current_program_space->objfiles ())
13585 {
13586 for (minimal_symbol *msymbol : objfile->msymbols ())
13587 {
13588 QUIT;
13589
13590 if (completion_skip_symbol (mode, msymbol))
13591 continue;
13592
13593 language symbol_language = msymbol->language ();
13594
13595 /* Ada minimal symbols won't have their language set to Ada. If
13596 we let completion_list_add_name compare using the
13597 default/C-like matcher, then when completing e.g., symbols in a
13598 package named "pck", we'd match internal Ada symbols like
13599 "pckS", which are invalid in an Ada expression, unless you wrap
13600 them in '<' '>' to request a verbatim match.
13601
13602 Unfortunately, some Ada encoded names successfully demangle as
13603 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13604 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13605 with the wrong language set. Paper over that issue here. */
129bce36 13606 if (symbol_language == language_unknown
7e56227d
AB
13607 || symbol_language == language_cplus)
13608 symbol_language = language_ada;
13609
13610 completion_list_add_name (tracker,
13611 symbol_language,
13612 msymbol->linkage_name (),
13613 lookup_name, text, word);
13614 }
13615 }
13616
13617 /* Search upwards from currently selected frame (so that we can
13618 complete on local vars. */
13619
f135fe72 13620 for (b = get_selected_block (0); b != NULL; b = b->superblock ())
7e56227d 13621 {
f135fe72 13622 if (!b->superblock ())
7e56227d
AB
13623 surrounding_static_block = b; /* For elmin of dups */
13624
548a89df 13625 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13626 {
13627 if (completion_skip_symbol (mode, sym))
13628 continue;
13629
13630 completion_list_add_name (tracker,
13631 sym->language (),
13632 sym->linkage_name (),
13633 lookup_name, text, word);
13634 }
13635 }
13636
13637 /* Go through the symtabs and check the externs and statics for
13638 symbols which match. */
13639
13640 for (objfile *objfile : current_program_space->objfiles ())
13641 {
13642 for (compunit_symtab *s : objfile->compunits ())
13643 {
13644 QUIT;
63d609de 13645 b = s->blockvector ()->global_block ();
548a89df 13646 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13647 {
13648 if (completion_skip_symbol (mode, sym))
13649 continue;
13650
13651 completion_list_add_name (tracker,
13652 sym->language (),
13653 sym->linkage_name (),
13654 lookup_name, text, word);
13655 }
13656 }
13657 }
13658
13659 for (objfile *objfile : current_program_space->objfiles ())
13660 {
13661 for (compunit_symtab *s : objfile->compunits ())
13662 {
13663 QUIT;
63d609de 13664 b = s->blockvector ()->static_block ();
7e56227d
AB
13665 /* Don't do this block twice. */
13666 if (b == surrounding_static_block)
13667 continue;
548a89df 13668 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13669 {
13670 if (completion_skip_symbol (mode, sym))
13671 continue;
13672
13673 completion_list_add_name (tracker,
13674 sym->language (),
13675 sym->linkage_name (),
13676 lookup_name, text, word);
13677 }
13678 }
13679 }
13680 }
13681
f16a9f57
AB
13682 /* See language.h. */
13683
13684 gdb::unique_xmalloc_ptr<char> watch_location_expression
13685 (struct type *type, CORE_ADDR addr) const override
13686 {
27710edb 13687 type = check_typedef (check_typedef (type)->target_type ());
f16a9f57 13688 std::string name = type_to_string (type);
8579fd13 13689 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13690 }
13691
a1d1fa3e
AB
13692 /* See language.h. */
13693
13694 void value_print (struct value *val, struct ui_file *stream,
13695 const struct value_print_options *options) const override
13696 {
13697 return ada_value_print (val, stream, options);
13698 }
13699
ebe2334e
AB
13700 /* See language.h. */
13701
13702 void value_print_inner
13703 (struct value *val, struct ui_file *stream, int recurse,
13704 const struct value_print_options *options) const override
13705 {
13706 return ada_value_print_inner (val, stream, recurse, options);
13707 }
13708
a78a19b1
AB
13709 /* See language.h. */
13710
13711 struct block_symbol lookup_symbol_nonlocal
13712 (const char *name, const struct block *block,
13713 const domain_enum domain) const override
13714 {
13715 struct block_symbol sym;
13716
78004096
TT
13717 sym = ada_lookup_symbol (name,
13718 (block == nullptr
13719 ? nullptr
d24e14a0 13720 : block->static_block ()),
78004096 13721 domain);
a78a19b1
AB
13722 if (sym.symbol != NULL)
13723 return sym;
13724
13725 /* If we haven't found a match at this point, try the primitive
13726 types. In other languages, this search is performed before
13727 searching for global symbols in order to short-circuit that
13728 global-symbol search if it happens that the name corresponds
13729 to a primitive type. But we cannot do the same in Ada, because
13730 it is perfectly legitimate for a program to declare a type which
13731 has the same name as a standard type. If looking up a type in
13732 that situation, we have traditionally ignored the primitive type
13733 in favor of user-defined types. This is why, unlike most other
13734 languages, we search the primitive types this late and only after
13735 having searched the global symbols without success. */
13736
13737 if (domain == VAR_DOMAIN)
13738 {
13739 struct gdbarch *gdbarch;
13740
13741 if (block == NULL)
13742 gdbarch = target_gdbarch ();
13743 else
7f5937df 13744 gdbarch = block->gdbarch ();
a78a19b1
AB
13745 sym.symbol
13746 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13747 if (sym.symbol != NULL)
13748 return sym;
13749 }
13750
13751 return {};
13752 }
13753
87afa652
AB
13754 /* See language.h. */
13755
13756 int parser (struct parser_state *ps) const override
13757 {
13758 warnings_issued = 0;
13759 return ada_parse (ps);
13760 }
13761
ec8cec5b
AB
13762 /* See language.h. */
13763
13764 void emitchar (int ch, struct type *chtype,
13765 struct ui_file *stream, int quoter) const override
13766 {
13767 ada_emit_char (ch, chtype, stream, quoter, 1);
13768 }
13769
52b50f2c
AB
13770 /* See language.h. */
13771
13772 void printchar (int ch, struct type *chtype,
13773 struct ui_file *stream) const override
13774 {
13775 ada_printchar (ch, chtype, stream);
13776 }
13777
d711ee67
AB
13778 /* See language.h. */
13779
13780 void printstr (struct ui_file *stream, struct type *elttype,
13781 const gdb_byte *string, unsigned int length,
13782 const char *encoding, int force_ellipses,
13783 const struct value_print_options *options) const override
13784 {
13785 ada_printstr (stream, elttype, string, length, encoding,
13786 force_ellipses, options);
13787 }
13788
4ffc13fb
AB
13789 /* See language.h. */
13790
13791 void print_typedef (struct type *type, struct symbol *new_symbol,
13792 struct ui_file *stream) const override
13793 {
13794 ada_print_typedef (type, new_symbol, stream);
13795 }
13796
39e7ecca
AB
13797 /* See language.h. */
13798
13799 bool is_string_type_p (struct type *type) const override
13800 {
13801 return ada_is_string_type (type);
13802 }
13803
22e3f3ed
AB
13804 /* See language.h. */
13805
13806 const char *struct_too_deep_ellipsis () const override
13807 { return "(...)"; }
39e7ecca 13808
67bd3fd5
AB
13809 /* See language.h. */
13810
13811 bool c_style_arrays_p () const override
13812 { return false; }
13813
d3355e4d
AB
13814 /* See language.h. */
13815
13816 bool store_sym_names_in_linkage_form_p () const override
13817 { return true; }
13818
b63a3f3f
AB
13819 /* See language.h. */
13820
13821 const struct lang_varobj_ops *varobj_ops () const override
13822 { return &ada_varobj_ops; }
13823
c9debfb9
AB
13824protected:
13825 /* See language.h. */
13826
13827 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13828 (const lookup_name_info &lookup_name) const override
13829 {
13830 return ada_get_symbol_name_matcher (lookup_name);
13831 }
0874fd07
AB
13832};
13833
13834/* Single instance of the Ada language class. */
13835
13836static ada_language ada_language_defn;
13837
5bf03f13
JB
13838/* Command-list for the "set/show ada" prefix command. */
13839static struct cmd_list_element *set_ada_list;
13840static struct cmd_list_element *show_ada_list;
13841
3d9434b5
JB
13842/* This module's 'new_objfile' observer. */
13843
13844static void
13845ada_new_objfile_observer (struct objfile *objfile)
13846{
13847 ada_clear_symbol_cache ();
13848}
13849
13850/* This module's 'free_objfile' observer. */
13851
13852static void
13853ada_free_objfile_observer (struct objfile *objfile)
13854{
13855 ada_clear_symbol_cache ();
13856}
13857
315e4ebb
TT
13858/* Charsets known to GNAT. */
13859static const char * const gnat_source_charsets[] =
13860{
13861 /* Note that code below assumes that the default comes first.
13862 Latin-1 is the default here, because that is also GNAT's
13863 default. */
13864 "ISO-8859-1",
13865 "ISO-8859-2",
13866 "ISO-8859-3",
13867 "ISO-8859-4",
13868 "ISO-8859-5",
13869 "ISO-8859-15",
13870 "CP437",
13871 "CP850",
13872 /* Note that this value is special-cased in the encoder and
13873 decoder. */
13874 ada_utf8,
13875 nullptr
13876};
13877
6c265988 13878void _initialize_ada_language ();
d2e4a39e 13879void
6c265988 13880_initialize_ada_language ()
14f9c5c9 13881{
f54bdb6d
SM
13882 add_setshow_prefix_cmd
13883 ("ada", no_class,
13884 _("Prefix command for changing Ada-specific settings."),
13885 _("Generic command for showing Ada-specific settings."),
13886 &set_ada_list, &show_ada_list,
13887 &setlist, &showlist);
5bf03f13
JB
13888
13889 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13890 &trust_pad_over_xvs, _("\
590042fc
PW
13891Enable or disable an optimization trusting PAD types over XVS types."), _("\
13892Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13893 _("\
5bf03f13
JB
13894This is related to the encoding used by the GNAT compiler. The debugger\n\
13895should normally trust the contents of PAD types, but certain older versions\n\
13896of GNAT have a bug that sometimes causes the information in the PAD type\n\
13897to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13898work around this bug. It is always safe to turn this option \"off\", but\n\
13899this incurs a slight performance penalty, so it is recommended to NOT change\n\
13900this option to \"off\" unless necessary."),
dda83cd7 13901 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13902
d72413e6
PMR
13903 add_setshow_boolean_cmd ("print-signatures", class_vars,
13904 &print_signatures, _("\
13905Enable or disable the output of formal and return types for functions in the \
590042fc 13906overloads selection menu."), _("\
d72413e6 13907Show whether the output of formal and return types for functions in the \
590042fc 13908overloads selection menu is activated."),
d72413e6
PMR
13909 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13910
315e4ebb
TT
13911 ada_source_charset = gnat_source_charsets[0];
13912 add_setshow_enum_cmd ("source-charset", class_files,
13913 gnat_source_charsets,
13914 &ada_source_charset, _("\
13915Set the Ada source character set."), _("\
13916Show the Ada source character set."), _("\
13917The character set used for Ada source files.\n\
13918This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13919 nullptr, nullptr,
13920 &set_ada_list, &show_ada_list);
13921
9ac4176b
PA
13922 add_catch_command ("exception", _("\
13923Catch Ada exceptions, when raised.\n\
9bf7038b 13924Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
13925Without any argument, stop when any Ada exception is raised.\n\
13926If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13927being raised does not have a handler (and will therefore lead to the task's\n\
13928termination).\n\
13929Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
13930raised is the same as ARG.\n\
13931CONDITION is a boolean expression that is evaluated to see whether the\n\
13932exception should cause a stop."),
9ac4176b 13933 catch_ada_exception_command,
71bed2db 13934 catch_ada_completer,
9ac4176b
PA
13935 CATCH_PERMANENT,
13936 CATCH_TEMPORARY);
9f757bf7
XR
13937
13938 add_catch_command ("handlers", _("\
13939Catch Ada exceptions, when handled.\n\
9bf7038b
TT
13940Usage: catch handlers [ARG] [if CONDITION]\n\
13941Without any argument, stop when any Ada exception is handled.\n\
13942With an argument, catch only exceptions with the given name.\n\
13943CONDITION is a boolean expression that is evaluated to see whether the\n\
13944exception should cause a stop."),
9f757bf7 13945 catch_ada_handlers_command,
dda83cd7 13946 catch_ada_completer,
9f757bf7
XR
13947 CATCH_PERMANENT,
13948 CATCH_TEMPORARY);
9ac4176b
PA
13949 add_catch_command ("assert", _("\
13950Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
13951Usage: catch assert [if CONDITION]\n\
13952CONDITION is a boolean expression that is evaluated to see whether the\n\
13953exception should cause a stop."),
9ac4176b 13954 catch_assert_command,
dda83cd7 13955 NULL,
9ac4176b
PA
13956 CATCH_PERMANENT,
13957 CATCH_TEMPORARY);
13958
778865d3
JB
13959 add_info ("exceptions", info_exceptions_command,
13960 _("\
13961List all Ada exception names.\n\
9bf7038b 13962Usage: info exceptions [REGEXP]\n\
778865d3
JB
13963If a regular expression is passed as an argument, only those matching\n\
13964the regular expression are listed."));
13965
f54bdb6d
SM
13966 add_setshow_prefix_cmd ("ada", class_maintenance,
13967 _("Set Ada maintenance-related variables."),
13968 _("Show Ada maintenance-related variables."),
13969 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
13970 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
13971
13972 add_setshow_boolean_cmd
13973 ("ignore-descriptive-types", class_maintenance,
13974 &ada_ignore_descriptive_types_p,
13975 _("Set whether descriptive types generated by GNAT should be ignored."),
13976 _("Show whether descriptive types generated by GNAT should be ignored."),
13977 _("\
13978When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
13979DWARF attribute."),
13980 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
13981
2698f5ea
TT
13982 decoded_names_store = htab_create_alloc (256, htab_hash_string,
13983 htab_eq_string,
459a2e4c 13984 NULL, xcalloc, xfree);
6b69afc4 13985
3d9434b5 13986 /* The ada-lang observers. */
c90e7d63
SM
13987 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
13988 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
13989 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
033bc52b
TT
13990
13991#ifdef GDB_SELF_TEST
13992 selftests::register_test ("ada-decode", ada_decode_tests);
13993#endif
14f9c5c9 13994}