]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
Use the new symbol domains
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
1d506c26 3 Copyright (C) 1992-2024 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,
6c015214 101 domain_search_flags, struct objfile *);
14f9c5c9 102
d1183b06
TT
103static void ada_add_all_symbols (std::vector<struct block_symbol> &,
104 const struct block *,
b5ec771e 105 const lookup_name_info &lookup_name,
6c015214 106 domain_search_flags, 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
108d56a4 179static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 180 struct type *);
4c4b4cd2 181
0d5cff50 182static int find_struct_field (const char *, struct type *, int,
dda83cd7 183 struct type **, int *, int *, int *, int *);
4c4b4cd2 184
d1183b06 185static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 186 struct value **, int, const char *,
7056f312 187 struct type *, bool);
4c4b4cd2 188
4c4b4cd2
PH
189static int ada_is_direct_array_type (struct type *);
190
52ce6436
PH
191static struct value *ada_index_struct_field (int, struct value *, int,
192 struct type *);
193
cf608cc4 194static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
195
196
852dff6c 197static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
198
199static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
200 (const lookup_name_info &lookup_name);
201
ef136c7f
TV
202static int symbols_are_identical_enums
203 (const std::vector<struct block_symbol> &syms);
74c36641
TV
204
205static int ada_identical_enum_types_p (struct type *type1, struct type *type2);
4c4b4cd2
PH
206\f
207
315e4ebb
TT
208/* The character set used for source files. */
209static const char *ada_source_charset;
210
211/* The string "UTF-8". This is here so we can check for the UTF-8
212 charset using == rather than strcmp. */
213static const char ada_utf8[] = "UTF-8";
214
215/* Each entry in the UTF-32 case-folding table is of this form. */
216struct utf8_entry
217{
218 /* The start and end, inclusive, of this range of codepoints. */
219 uint32_t start, end;
220 /* The delta to apply to get the upper-case form. 0 if this is
221 already upper-case. */
222 int upper_delta;
223 /* The delta to apply to get the lower-case form. 0 if this is
224 already lower-case. */
225 int lower_delta;
226
227 bool operator< (uint32_t val) const
228 {
229 return end < val;
230 }
231};
232
233static const utf8_entry ada_case_fold[] =
234{
235#include "ada-casefold.h"
236};
237
238\f
239
67cb5b2d 240static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
241#ifdef VMS
242 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
243#else
14f9c5c9 244 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 245#endif
14f9c5c9 246
4c4b4cd2 247/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 248static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 249 = "__gnat_ada_main_program_name";
14f9c5c9 250
4c4b4cd2
PH
251/* Limit on the number of warnings to raise per expression evaluation. */
252static int warning_limit = 2;
253
254/* Number of warning messages issued; reset to 0 by cleanups after
255 expression evaluation. */
256static int warnings_issued = 0;
257
27087b7f 258static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
259 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
260};
261
27087b7f 262static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
263 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
264};
265
c6044dd1
JB
266/* Maintenance-related settings for this module. */
267
268static struct cmd_list_element *maint_set_ada_cmdlist;
269static struct cmd_list_element *maint_show_ada_cmdlist;
270
c6044dd1
JB
271/* The "maintenance ada set/show ignore-descriptive-type" value. */
272
491144b5 273static bool ada_ignore_descriptive_types_p = false;
c6044dd1 274
e802dbe0
JB
275 /* Inferior-specific data. */
276
277/* Per-inferior data for this module. */
278
279struct ada_inferior_data
280{
281 /* The ada__tags__type_specific_data type, which is used when decoding
282 tagged types. With older versions of GNAT, this type was directly
283 accessible through a component ("tsd") in the object tag. But this
284 is no longer the case, so we cache it for each inferior. */
f37b313d 285 struct type *tsd_type = nullptr;
3eecfa55
JB
286
287 /* The exception_support_info data. This data is used to determine
288 how to implement support for Ada exception catchpoints in a given
289 inferior. */
f37b313d 290 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
291};
292
293/* Our key to this module's inferior data. */
08b8a139 294static const registry<inferior>::key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
295
296/* Return our inferior data for the given inferior (INF).
297
298 This function always returns a valid pointer to an allocated
299 ada_inferior_data structure. If INF's inferior data has not
300 been previously set, this functions creates a new one with all
301 fields set to zero, sets INF's inferior to it, and then returns
302 a pointer to that newly allocated ada_inferior_data. */
303
304static struct ada_inferior_data *
305get_ada_inferior_data (struct inferior *inf)
306{
307 struct ada_inferior_data *data;
308
f37b313d 309 data = ada_inferior_data.get (inf);
e802dbe0 310 if (data == NULL)
f37b313d 311 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
312
313 return data;
314}
315
316/* Perform all necessary cleanups regarding our module's inferior data
317 that is required after the inferior INF just exited. */
318
319static void
320ada_inferior_exit (struct inferior *inf)
321{
f37b313d 322 ada_inferior_data.clear (inf);
e802dbe0
JB
323}
324
ee01b665
JB
325
326 /* program-space-specific data. */
327
9d1c303d
TT
328/* The result of a symbol lookup to be stored in our symbol cache. */
329
330struct cache_entry
ee01b665 331{
9d1c303d
TT
332 /* The name used to perform the lookup. */
333 std::string name;
334 /* The namespace used during the lookup. */
6c015214 335 domain_search_flags domain = 0;
9d1c303d
TT
336 /* The symbol returned by the lookup, or NULL if no matching symbol
337 was found. */
338 struct symbol *sym = nullptr;
339 /* The block where the symbol was found, or NULL if no matching
340 symbol was found. */
341 const struct block *block = nullptr;
ee01b665
JB
342};
343
9d1c303d
TT
344/* The symbol cache uses this type when searching. */
345
346struct cache_entry_search
347{
348 const char *name;
6c015214 349 domain_search_flags domain;
9d1c303d
TT
350
351 hashval_t hash () const
352 {
353 /* This must agree with hash_cache_entry, below. */
354 return htab_hash_string (name);
355 }
356};
357
358/* Hash function for cache_entry. */
359
360static hashval_t
361hash_cache_entry (const void *v)
362{
363 const cache_entry *entry = (const cache_entry *) v;
364 return htab_hash_string (entry->name.c_str ());
365}
366
367/* Equality function for cache_entry. */
368
369static int
370eq_cache_entry (const void *a, const void *b)
371{
372 const cache_entry *entrya = (const cache_entry *) a;
373 const cache_entry_search *entryb = (const cache_entry_search *) b;
374
375 return entrya->domain == entryb->domain && entrya->name == entryb->name;
376}
377
ee01b665 378/* Key to our per-program-space data. */
9d1c303d 379static const registry<program_space>::key<htab, htab_deleter>
08b8a139 380 ada_pspace_data_handle;
ee01b665
JB
381
382/* Return this module's data for the given program space (PSPACE).
383 If not is found, add a zero'ed one now.
384
385 This function always returns a valid object. */
386
9d1c303d 387static htab_t
ee01b665
JB
388get_ada_pspace_data (struct program_space *pspace)
389{
9d1c303d
TT
390 htab_t data = ada_pspace_data_handle.get (pspace);
391 if (data == nullptr)
392 {
393 data = htab_create_alloc (10, hash_cache_entry, eq_cache_entry,
394 htab_delete_entry<cache_entry>,
395 xcalloc, xfree);
396 ada_pspace_data_handle.set (pspace, data);
397 }
ee01b665
JB
398
399 return data;
400}
401
dda83cd7 402 /* Utilities */
4c4b4cd2 403
720d1a40 404/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 405 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
406
407 Normally, we really expect a typedef type to only have 1 typedef layer.
408 In other words, we really expect the target type of a typedef type to be
409 a non-typedef type. This is particularly true for Ada units, because
410 the language does not have a typedef vs not-typedef distinction.
411 In that respect, the Ada compiler has been trying to eliminate as many
412 typedef definitions in the debugging information, since they generally
413 do not bring any extra information (we still use typedef under certain
414 circumstances related mostly to the GNAT encoding).
415
416 Unfortunately, we have seen situations where the debugging information
417 generated by the compiler leads to such multiple typedef layers. For
418 instance, consider the following example with stabs:
419
420 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
421 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
422
423 This is an error in the debugging information which causes type
424 pck__float_array___XUP to be defined twice, and the second time,
425 it is defined as a typedef of a typedef.
426
427 This is on the fringe of legality as far as debugging information is
428 concerned, and certainly unexpected. But it is easy to handle these
429 situations correctly, so we can afford to be lenient in this case. */
430
431static struct type *
432ada_typedef_target_type (struct type *type)
433{
78134374 434 while (type->code () == TYPE_CODE_TYPEDEF)
27710edb 435 type = type->target_type ();
720d1a40
JB
436 return type;
437}
438
41d27058
JB
439/* Given DECODED_NAME a string holding a symbol name in its
440 decoded form (ie using the Ada dotted notation), returns
441 its unqualified name. */
442
443static const char *
444ada_unqualified_name (const char *decoded_name)
445{
2b0f535a
JB
446 const char *result;
447
448 /* If the decoded name starts with '<', it means that the encoded
449 name does not follow standard naming conventions, and thus that
450 it is not your typical Ada symbol name. Trying to unqualify it
451 is therefore pointless and possibly erroneous. */
452 if (decoded_name[0] == '<')
453 return decoded_name;
454
455 result = strrchr (decoded_name, '.');
41d27058
JB
456 if (result != NULL)
457 result++; /* Skip the dot... */
458 else
459 result = decoded_name;
460
461 return result;
462}
463
39e7af3e 464/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 465
39e7af3e 466static std::string
41d27058
JB
467add_angle_brackets (const char *str)
468{
39e7af3e 469 return string_printf ("<%s>", str);
41d27058 470}
96d887e8 471
14f9c5c9 472/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 473 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
474
475static int
ebf56fd3 476field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
477{
478 int len = strlen (target);
5b4ee69b 479
d2e4a39e 480 return
4c4b4cd2
PH
481 (strncmp (field_name, target, len) == 0
482 && (field_name[len] == '\0'
dda83cd7
SM
483 || (startswith (field_name + len, "___")
484 && strcmp (field_name + strlen (field_name) - 6,
485 "___XVN") != 0)));
14f9c5c9
AS
486}
487
488
872c8b51
JB
489/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
490 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
491 and return its index. This function also handles fields whose name
492 have ___ suffixes because the compiler sometimes alters their name
493 by adding such a suffix to represent fields with certain constraints.
494 If the field could not be found, return a negative number if
495 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
496
497int
498ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 499 int maybe_missing)
4c4b4cd2
PH
500{
501 int fieldno;
872c8b51
JB
502 struct type *struct_type = check_typedef ((struct type *) type);
503
1f704f76 504 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 505 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
506 return fieldno;
507
508 if (!maybe_missing)
323e0a4a 509 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 510 field_name, struct_type->name ());
4c4b4cd2
PH
511
512 return -1;
513}
514
515/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
516
517int
d2e4a39e 518ada_name_prefix_len (const char *name)
14f9c5c9
AS
519{
520 if (name == NULL)
521 return 0;
d2e4a39e 522 else
14f9c5c9 523 {
d2e4a39e 524 const char *p = strstr (name, "___");
5b4ee69b 525
14f9c5c9 526 if (p == NULL)
dda83cd7 527 return strlen (name);
14f9c5c9 528 else
dda83cd7 529 return p - name;
14f9c5c9
AS
530 }
531}
532
4c4b4cd2
PH
533/* Return non-zero if SUFFIX is a suffix of STR.
534 Return zero if STR is null. */
535
14f9c5c9 536static int
d2e4a39e 537is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
538{
539 int len1, len2;
5b4ee69b 540
14f9c5c9
AS
541 if (str == NULL)
542 return 0;
543 len1 = strlen (str);
544 len2 = strlen (suffix);
4c4b4cd2 545 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
546}
547
4c4b4cd2
PH
548/* The contents of value VAL, treated as a value of type TYPE. The
549 result is an lval in memory if VAL is. */
14f9c5c9 550
d2e4a39e 551static struct value *
4c4b4cd2 552coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 553{
61ee279c 554 type = ada_check_typedef (type);
d0c97917 555 if (val->type () == type)
4c4b4cd2 556 return val;
d2e4a39e 557 else
14f9c5c9 558 {
4c4b4cd2
PH
559 struct value *result;
560
d00664db 561 if (val->optimized_out ())
b27556e3 562 result = value::allocate_optimized_out (type);
3ee3b270 563 else if (val->lazy ()
f73e424f 564 /* Be careful not to make a lazy not_lval value. */
736355f2 565 || (val->lval () != not_lval
d0c97917 566 && type->length () > val->type ()->length ()))
cbe793af 567 result = value::allocate_lazy (type);
41e8491f
JK
568 else
569 {
317c3ed9 570 result = value::allocate (type);
6c49729e 571 val->contents_copy (result, 0, 0, type->length ());
41e8491f 572 }
8181b7b6 573 result->set_component_location (val);
f49d5fa2 574 result->set_bitsize (val->bitsize ());
5011c493 575 result->set_bitpos (val->bitpos ());
736355f2 576 if (result->lval () == lval_memory)
9feb2d07 577 result->set_address (val->address ());
14f9c5c9
AS
578 return result;
579 }
580}
581
fc1a4b47
AC
582static const gdb_byte *
583cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
584{
585 if (valaddr == NULL)
586 return NULL;
587 else
588 return valaddr + offset;
589}
590
591static CORE_ADDR
ebf56fd3 592cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
593{
594 if (address == 0)
595 return 0;
d2e4a39e 596 else
14f9c5c9
AS
597 return address + offset;
598}
599
4c4b4cd2
PH
600/* Issue a warning (as for the definition of warning in utils.c, but
601 with exactly one argument rather than ...), unless the limit on the
602 number of warnings has passed during the evaluation of the current
603 expression. */
a2249542 604
77109804
AC
605/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
606 provided by "complaint". */
a0b31db1 607static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 608
14f9c5c9 609static void
a2249542 610lim_warning (const char *format, ...)
14f9c5c9 611{
a2249542 612 va_list args;
a2249542 613
5b4ee69b 614 va_start (args, format);
4c4b4cd2
PH
615 warnings_issued += 1;
616 if (warnings_issued <= warning_limit)
a2249542
MK
617 vwarning (format, args);
618
619 va_end (args);
4c4b4cd2
PH
620}
621
0963b4bd 622/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 623static LONGEST
c3e5cd34 624max_of_size (int size)
4c4b4cd2 625{
76a01679 626 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 627
76a01679 628 return top_bit | (top_bit - 1);
4c4b4cd2
PH
629}
630
0963b4bd 631/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 632static LONGEST
c3e5cd34 633min_of_size (int size)
4c4b4cd2 634{
c3e5cd34 635 return -max_of_size (size) - 1;
4c4b4cd2
PH
636}
637
0963b4bd 638/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 639static ULONGEST
c3e5cd34 640umax_of_size (int size)
4c4b4cd2 641{
76a01679 642 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 643
76a01679 644 return top_bit | (top_bit - 1);
4c4b4cd2
PH
645}
646
0963b4bd 647/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
648static LONGEST
649max_of_type (struct type *t)
4c4b4cd2 650{
c6d940a9 651 if (t->is_unsigned ())
df86565b 652 return (LONGEST) umax_of_size (t->length ());
c3e5cd34 653 else
df86565b 654 return max_of_size (t->length ());
c3e5cd34
PH
655}
656
0963b4bd 657/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
658static LONGEST
659min_of_type (struct type *t)
660{
c6d940a9 661 if (t->is_unsigned ())
c3e5cd34
PH
662 return 0;
663 else
df86565b 664 return min_of_size (t->length ());
4c4b4cd2
PH
665}
666
667/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
668LONGEST
669ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 670{
b249d2c2 671 type = resolve_dynamic_type (type, {}, 0);
78134374 672 switch (type->code ())
4c4b4cd2
PH
673 {
674 case TYPE_CODE_RANGE:
d1fd641e
SM
675 {
676 const dynamic_prop &high = type->bounds ()->high;
677
9c0fb734 678 if (high.is_constant ())
d1fd641e
SM
679 return high.const_val ();
680 else
681 {
682 gdb_assert (high.kind () == PROP_UNDEFINED);
683
684 /* This happens when trying to evaluate a type's dynamic bound
685 without a live target. There is nothing relevant for us to
686 return here, so return 0. */
687 return 0;
688 }
689 }
4c4b4cd2 690 case TYPE_CODE_ENUM:
970db518 691 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
692 case TYPE_CODE_BOOL:
693 return 1;
694 case TYPE_CODE_CHAR:
76a01679 695 case TYPE_CODE_INT:
690cc4eb 696 return max_of_type (type);
4c4b4cd2 697 default:
43bbcdc2 698 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
699 }
700}
701
14e75d8e 702/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
703LONGEST
704ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 705{
b249d2c2 706 type = resolve_dynamic_type (type, {}, 0);
78134374 707 switch (type->code ())
4c4b4cd2
PH
708 {
709 case TYPE_CODE_RANGE:
d1fd641e
SM
710 {
711 const dynamic_prop &low = type->bounds ()->low;
712
9c0fb734 713 if (low.is_constant ())
d1fd641e
SM
714 return low.const_val ();
715 else
716 {
717 gdb_assert (low.kind () == PROP_UNDEFINED);
718
719 /* This happens when trying to evaluate a type's dynamic bound
720 without a live target. There is nothing relevant for us to
721 return here, so return 0. */
722 return 0;
723 }
724 }
4c4b4cd2 725 case TYPE_CODE_ENUM:
970db518 726 return type->field (0).loc_enumval ();
690cc4eb
PH
727 case TYPE_CODE_BOOL:
728 return 0;
729 case TYPE_CODE_CHAR:
76a01679 730 case TYPE_CODE_INT:
690cc4eb 731 return min_of_type (type);
4c4b4cd2 732 default:
43bbcdc2 733 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
734 }
735}
736
737/* The identity on non-range types. For range types, the underlying
76a01679 738 non-range scalar type. */
4c4b4cd2
PH
739
740static struct type *
18af8284 741get_base_type (struct type *type)
4c4b4cd2 742{
78134374 743 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 744 {
27710edb 745 if (type == type->target_type () || type->target_type () == NULL)
dda83cd7 746 return type;
27710edb 747 type = type->target_type ();
4c4b4cd2
PH
748 }
749 return type;
14f9c5c9 750}
41246937
JB
751
752/* Return a decoded version of the given VALUE. This means returning
753 a value whose type is obtained by applying all the GNAT-specific
85102364 754 encodings, making the resulting type a static but standard description
41246937
JB
755 of the initial type. */
756
757struct value *
758ada_get_decoded_value (struct value *value)
759{
d0c97917 760 struct type *type = ada_check_typedef (value->type ());
41246937
JB
761
762 if (ada_is_array_descriptor_type (type)
763 || (ada_is_constrained_packed_array_type (type)
dda83cd7 764 && type->code () != TYPE_CODE_PTR))
41246937 765 {
78134374 766 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 767 value = ada_coerce_to_simple_array_ptr (value);
41246937 768 else
dda83cd7 769 value = ada_coerce_to_simple_array (value);
41246937
JB
770 }
771 else
772 value = ada_to_fixed_value (value);
773
774 return value;
775}
776
777/* Same as ada_get_decoded_value, but with the given TYPE.
778 Because there is no associated actual value for this type,
779 the resulting type might be a best-effort approximation in
780 the case of dynamic types. */
781
782struct type *
783ada_get_decoded_type (struct type *type)
784{
785 type = to_static_fixed_type (type);
786 if (ada_is_constrained_packed_array_type (type))
787 type = ada_coerce_to_simple_array_type (type);
788 return type;
789}
790
4c4b4cd2 791\f
76a01679 792
dda83cd7 793 /* Language Selection */
14f9c5c9 794
96d887e8
PH
795/* If the main procedure is written in Ada, then return its name.
796 The result is good until the next call. Return NULL if the main
797 procedure doesn't appear to be in Ada. */
798
6f63b61d
TT
799const char *
800ada_main_name ()
96d887e8 801{
3b7344d5 802 struct bound_minimal_symbol msym;
e83e4e24 803 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 804
96d887e8
PH
805 /* For Ada, the name of the main procedure is stored in a specific
806 string constant, generated by the binder. Look for that symbol,
807 extract its address, and then read that string. If we didn't find
808 that string, then most probably the main procedure is not written
809 in Ada. */
810 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
811
3b7344d5 812 if (msym.minsym != NULL)
96d887e8 813 {
4aeddc50 814 CORE_ADDR main_program_name_addr = msym.value_address ();
96d887e8 815 if (main_program_name_addr == 0)
dda83cd7 816 error (_("Invalid address for Ada main program name."));
96d887e8 817
358be6e7
TT
818 /* Force trust_readonly, because we always want to fetch this
819 string from the executable, not from inferior memory. If the
820 user changes the exec-file and invokes "start", we want to
821 pick the "main" from the new executable, not one that may
822 come from the still-live inferior. */
823 scoped_restore save_trust_readonly
824 = make_scoped_restore (&trust_readonly, true);
66920317 825 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 826 return main_program_name.get ();
96d887e8
PH
827 }
828
829 /* The main procedure doesn't seem to be in Ada. */
830 return NULL;
831}
14f9c5c9 832\f
dda83cd7 833 /* Symbols */
d2e4a39e 834
4c4b4cd2
PH
835/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
836 of NULLs. */
14f9c5c9 837
d2e4a39e
AS
838const struct ada_opname_map ada_opname_table[] = {
839 {"Oadd", "\"+\"", BINOP_ADD},
840 {"Osubtract", "\"-\"", BINOP_SUB},
841 {"Omultiply", "\"*\"", BINOP_MUL},
842 {"Odivide", "\"/\"", BINOP_DIV},
843 {"Omod", "\"mod\"", BINOP_MOD},
844 {"Orem", "\"rem\"", BINOP_REM},
845 {"Oexpon", "\"**\"", BINOP_EXP},
846 {"Olt", "\"<\"", BINOP_LESS},
847 {"Ole", "\"<=\"", BINOP_LEQ},
848 {"Ogt", "\">\"", BINOP_GTR},
849 {"Oge", "\">=\"", BINOP_GEQ},
850 {"Oeq", "\"=\"", BINOP_EQUAL},
851 {"One", "\"/=\"", BINOP_NOTEQUAL},
852 {"Oand", "\"and\"", BINOP_BITWISE_AND},
853 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
854 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
855 {"Oconcat", "\"&\"", BINOP_CONCAT},
856 {"Oabs", "\"abs\"", UNOP_ABS},
857 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
858 {"Oadd", "\"+\"", UNOP_PLUS},
859 {"Osubtract", "\"-\"", UNOP_NEG},
860 {NULL, NULL}
14f9c5c9
AS
861};
862
965bc1df
TT
863/* If STR is a decoded version of a compiler-provided suffix (like the
864 "[cold]" in "symbol[cold]"), return true. Otherwise, return
865 false. */
866
867static bool
868is_compiler_suffix (const char *str)
869{
870 gdb_assert (*str == '[');
871 ++str;
872 while (*str != '\0' && isalpha (*str))
873 ++str;
874 /* We accept a missing "]" in order to support completion. */
875 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
876}
877
315e4ebb
TT
878/* Append a non-ASCII character to RESULT. */
879static void
880append_hex_encoded (std::string &result, uint32_t one_char)
881{
882 if (one_char <= 0xff)
883 {
884 result.append ("U");
885 result.append (phex (one_char, 1));
886 }
887 else if (one_char <= 0xffff)
888 {
889 result.append ("W");
890 result.append (phex (one_char, 2));
891 }
892 else
893 {
894 result.append ("WW");
895 result.append (phex (one_char, 4));
896 }
897}
898
899/* Return a string that is a copy of the data in STORAGE, with
900 non-ASCII characters replaced by the appropriate hex encoding. A
901 template is used because, for UTF-8, we actually want to work with
902 UTF-32 codepoints. */
903template<typename T>
904std::string
905copy_and_hex_encode (struct obstack *storage)
906{
907 const T *chars = (T *) obstack_base (storage);
908 int num_chars = obstack_object_size (storage) / sizeof (T);
909 std::string result;
910 for (int i = 0; i < num_chars; ++i)
911 {
912 if (chars[i] <= 0x7f)
913 {
914 /* The host character set has to be a superset of ASCII, as
915 are all the other character sets we can use. */
916 result.push_back (chars[i]);
917 }
918 else
919 append_hex_encoded (result, chars[i]);
920 }
921 return result;
922}
923
5c4258f4 924/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 925 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 926 Otherwise, return the empty string in that case. */
4c4b4cd2 927
5c4258f4 928static std::string
b5ec771e 929ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 930{
4c4b4cd2 931 if (decoded == NULL)
5c4258f4 932 return {};
14f9c5c9 933
5c4258f4 934 std::string encoding_buffer;
315e4ebb 935 bool saw_non_ascii = false;
5c4258f4 936 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 937 {
315e4ebb
TT
938 if ((*p & 0x80) != 0)
939 saw_non_ascii = true;
940
cdc7bb92 941 if (*p == '.')
5c4258f4 942 encoding_buffer.append ("__");
965bc1df
TT
943 else if (*p == '[' && is_compiler_suffix (p))
944 {
945 encoding_buffer = encoding_buffer + "." + (p + 1);
946 if (encoding_buffer.back () == ']')
947 encoding_buffer.pop_back ();
948 break;
949 }
14f9c5c9 950 else if (*p == '"')
dda83cd7
SM
951 {
952 const struct ada_opname_map *mapping;
953
954 for (mapping = ada_opname_table;
955 mapping->encoded != NULL
956 && !startswith (p, mapping->decoded); mapping += 1)
957 ;
958 if (mapping->encoded == NULL)
b5ec771e
PA
959 {
960 if (throw_errors)
961 error (_("invalid Ada operator name: %s"), p);
962 else
5c4258f4 963 return {};
b5ec771e 964 }
5c4258f4 965 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
966 break;
967 }
d2e4a39e 968 else
5c4258f4 969 encoding_buffer.push_back (*p);
14f9c5c9
AS
970 }
971
315e4ebb
TT
972 /* If a non-ASCII character is seen, we must convert it to the
973 appropriate hex form. As this is more expensive, we keep track
974 of whether it is even necessary. */
975 if (saw_non_ascii)
976 {
977 auto_obstack storage;
978 bool is_utf8 = ada_source_charset == ada_utf8;
979 try
980 {
981 convert_between_encodings
982 (host_charset (),
983 is_utf8 ? HOST_UTF32 : ada_source_charset,
984 (const gdb_byte *) encoding_buffer.c_str (),
985 encoding_buffer.length (), 1,
986 &storage, translit_none);
987 }
988 catch (const gdb_exception &)
989 {
990 static bool warned = false;
991
992 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
993 might like to know why. */
994 if (!warned)
995 {
996 warned = true;
997 warning (_("charset conversion failure for '%s'.\n"
998 "You may have the wrong value for 'set ada source-charset'."),
999 encoding_buffer.c_str ());
1000 }
1001
1002 /* We don't try to recover from errors. */
1003 return encoding_buffer;
1004 }
1005
1006 if (is_utf8)
1007 return copy_and_hex_encode<uint32_t> (&storage);
1008 return copy_and_hex_encode<gdb_byte> (&storage);
1009 }
1010
4c4b4cd2 1011 return encoding_buffer;
14f9c5c9
AS
1012}
1013
315e4ebb
TT
1014/* Find the entry for C in the case-folding table. Return nullptr if
1015 the entry does not cover C. */
1016static const utf8_entry *
1017find_case_fold_entry (uint32_t c)
b5ec771e 1018{
315e4ebb
TT
1019 auto iter = std::lower_bound (std::begin (ada_case_fold),
1020 std::end (ada_case_fold),
1021 c);
1022 if (iter == std::end (ada_case_fold)
1023 || c < iter->start
1024 || c > iter->end)
1025 return nullptr;
1026 return &*iter;
b5ec771e
PA
1027}
1028
14f9c5c9 1029/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1030 quotes, unfolded, but with the quotes stripped away. If
1031 THROW_ON_ERROR is true, encoding failures will throw an exception
1032 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1033
5f9febe0 1034static const char *
8082468f 1035ada_fold_name (std::string_view name, bool throw_on_error = false)
14f9c5c9 1036{
5f9febe0 1037 static std::string fold_storage;
14f9c5c9 1038
6a780b67 1039 if (!name.empty () && name[0] == '\'')
882b0505 1040 fold_storage = name.substr (1, name.size () - 2);
14f9c5c9
AS
1041 else
1042 {
315e4ebb
TT
1043 /* Why convert to UTF-32 and implement our own case-folding,
1044 rather than convert to wchar_t and use the platform's
1045 functions? I'm glad you asked.
1046
1047 The main problem is that GNAT implements an unusual rule for
1048 case folding. For ASCII letters, letters in single-byte
1049 encodings (such as ISO-8859-*), and Unicode letters that fit
1050 in a single byte (i.e., code point is <= 0xff), the letter is
1051 folded to lower case. Other Unicode letters are folded to
1052 upper case.
1053
1054 This rule means that the code must be able to examine the
1055 value of the character. And, some hosts do not use Unicode
1056 for wchar_t, so examining the value of such characters is
1057 forbidden. */
1058 auto_obstack storage;
1059 try
1060 {
1061 convert_between_encodings
1062 (host_charset (), HOST_UTF32,
1063 (const gdb_byte *) name.data (),
1064 name.length (), 1,
1065 &storage, translit_none);
1066 }
1067 catch (const gdb_exception &)
1068 {
1069 if (throw_on_error)
1070 throw;
1071
1072 static bool warned = false;
1073
1074 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1075 might like to know why. */
1076 if (!warned)
1077 {
1078 warned = true;
1079 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1080 "This normally should not happen, please file a bug report."),
882b0505 1081 std::string (name).c_str (), host_charset ());
315e4ebb
TT
1082 }
1083
1084 /* We don't try to recover from errors; just return the
1085 original string. */
882b0505 1086 fold_storage = name;
315e4ebb
TT
1087 return fold_storage.c_str ();
1088 }
1089
1090 bool is_utf8 = ada_source_charset == ada_utf8;
1091 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1092 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1093 for (int i = 0; i < num_chars; ++i)
1094 {
1095 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1096 if (entry != nullptr)
1097 {
1098 uint32_t low = chars[i] + entry->lower_delta;
1099 if (!is_utf8 || low <= 0xff)
1100 chars[i] = low;
1101 else
1102 chars[i] = chars[i] + entry->upper_delta;
1103 }
1104 }
1105
1106 /* Now convert back to ordinary characters. */
1107 auto_obstack reconverted;
1108 try
1109 {
1110 convert_between_encodings (HOST_UTF32,
1111 host_charset (),
1112 (const gdb_byte *) chars,
1113 num_chars * sizeof (uint32_t),
1114 sizeof (uint32_t),
1115 &reconverted,
1116 translit_none);
1117 obstack_1grow (&reconverted, '\0');
1118 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1119 }
1120 catch (const gdb_exception &)
1121 {
1122 if (throw_on_error)
1123 throw;
1124
1125 static bool warned = false;
1126
1127 /* Converting back from UTF-32 shouldn't normally fail, but
1128 there are some host encodings without upper/lower
1129 equivalence. */
1130 if (!warned)
1131 {
1132 warned = true;
1133 warning (_("could not convert the lower-cased variant of '%s'\n"
1134 "from UTF-32 to the host encoding (%s)."),
882b0505 1135 std::string (name).c_str (), host_charset ());
315e4ebb
TT
1136 }
1137
1138 /* We don't try to recover from errors; just return the
1139 original string. */
882b0505 1140 fold_storage = name;
315e4ebb 1141 }
14f9c5c9
AS
1142 }
1143
5f9febe0 1144 return fold_storage.c_str ();
14f9c5c9
AS
1145}
1146
5fea9794
TT
1147/* The "encoded" form of DECODED, according to GNAT conventions. If
1148 FOLD is true (the default), case-fold any ordinary symbol. Symbols
1149 with <...> quoting are not folded in any case. */
315e4ebb
TT
1150
1151std::string
5fea9794 1152ada_encode (const char *decoded, bool fold)
315e4ebb 1153{
5fea9794 1154 if (fold && decoded[0] != '<')
315e4ebb
TT
1155 decoded = ada_fold_name (decoded);
1156 return ada_encode_1 (decoded, true);
1157}
1158
529cad9c
PH
1159/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1160
1161static int
1162is_lower_alphanum (const char c)
1163{
1164 return (isdigit (c) || (isalpha (c) && islower (c)));
1165}
1166
c90092fe
JB
1167/* ENCODED is the linkage name of a symbol and LEN contains its length.
1168 This function saves in LEN the length of that same symbol name but
1169 without either of these suffixes:
29480c32
JB
1170 . .{DIGIT}+
1171 . ${DIGIT}+
1172 . ___{DIGIT}+
1173 . __{DIGIT}+.
c90092fe 1174
29480c32
JB
1175 These are suffixes introduced by the compiler for entities such as
1176 nested subprogram for instance, in order to avoid name clashes.
1177 They do not serve any purpose for the debugger. */
1178
1179static void
1180ada_remove_trailing_digits (const char *encoded, int *len)
1181{
1182 if (*len > 1 && isdigit (encoded[*len - 1]))
1183 {
1184 int i = *len - 2;
5b4ee69b 1185
29480c32 1186 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1187 i--;
29480c32 1188 if (i >= 0 && encoded[i] == '.')
dda83cd7 1189 *len = i;
29480c32 1190 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1191 *len = i;
61012eef 1192 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1193 *len = i - 2;
61012eef 1194 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1195 *len = i - 1;
29480c32
JB
1196 }
1197}
1198
1199/* Remove the suffix introduced by the compiler for protected object
1200 subprograms. */
1201
1202static void
1203ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1204{
1205 /* Remove trailing N. */
1206
1207 /* Protected entry subprograms are broken into two
1208 separate subprograms: The first one is unprotected, and has
1209 a 'N' suffix; the second is the protected version, and has
0963b4bd 1210 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1211 the protection. Since the P subprograms are internally generated,
1212 we leave these names undecoded, giving the user a clue that this
1213 entity is internal. */
1214
1215 if (*len > 1
1216 && encoded[*len - 1] == 'N'
1217 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1218 *len = *len - 1;
1219}
1220
965bc1df
TT
1221/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1222 then update *LEN to remove the suffix and return the offset of the
1223 character just past the ".". Otherwise, return -1. */
1224
1225static int
1226remove_compiler_suffix (const char *encoded, int *len)
1227{
1228 int offset = *len - 1;
1229 while (offset > 0 && isalpha (encoded[offset]))
1230 --offset;
1231 if (offset > 0 && encoded[offset] == '.')
1232 {
1233 *len = offset;
1234 return offset + 1;
1235 }
1236 return -1;
1237}
1238
315e4ebb
TT
1239/* Convert an ASCII hex string to a number. Reads exactly N
1240 characters from STR. Returns true on success, false if one of the
1241 digits was not a hex digit. */
1242static bool
1243convert_hex (const char *str, int n, uint32_t *out)
1244{
1245 uint32_t result = 0;
1246
1247 for (int i = 0; i < n; ++i)
1248 {
1249 if (!isxdigit (str[i]))
1250 return false;
1251 result <<= 4;
1252 result |= fromhex (str[i]);
1253 }
1254
1255 *out = result;
1256 return true;
1257}
1258
1259/* Convert a wide character from its ASCII hex representation in STR
1260 (consisting of exactly N characters) to the host encoding,
1261 appending the resulting bytes to OUT. If N==2 and the Ada source
1262 charset is not UTF-8, then hex refers to an encoding in the
1263 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1264 Return false and do not modify OUT on conversion failure. */
1265static bool
1266convert_from_hex_encoded (std::string &out, const char *str, int n)
1267{
1268 uint32_t value;
1269
1270 if (!convert_hex (str, n, &value))
1271 return false;
1272 try
1273 {
1274 auto_obstack bytes;
1275 /* In the 'U' case, the hex digits encode the character in the
1276 Ada source charset. However, if the source charset is UTF-8,
1277 this really means it is a single-byte UTF-32 character. */
1278 if (n == 2 && ada_source_charset != ada_utf8)
1279 {
1280 gdb_byte one_char = (gdb_byte) value;
1281
1282 convert_between_encodings (ada_source_charset, host_charset (),
1283 &one_char,
1284 sizeof (one_char), sizeof (one_char),
1285 &bytes, translit_none);
1286 }
1287 else
1288 convert_between_encodings (HOST_UTF32, host_charset (),
1289 (const gdb_byte *) &value,
1290 sizeof (value), sizeof (value),
1291 &bytes, translit_none);
1292 obstack_1grow (&bytes, '\0');
1293 out.append ((const char *) obstack_base (&bytes));
1294 }
1295 catch (const gdb_exception &)
1296 {
1297 /* On failure, the caller will just let the encoded form
1298 through, which seems basically reasonable. */
1299 return false;
1300 }
1301
1302 return true;
1303}
1304
8a3df5ac 1305/* See ada-lang.h. */
14f9c5c9 1306
f945dedf 1307std::string
957ce537 1308ada_decode (const char *encoded, bool wrap, bool operators, bool wide)
14f9c5c9 1309{
36f5ca53 1310 int i;
14f9c5c9 1311 int len0;
d2e4a39e 1312 const char *p;
14f9c5c9 1313 int at_start_name;
f945dedf 1314 std::string decoded;
965bc1df 1315 int suffix = -1;
d2e4a39e 1316
0d81f350
JG
1317 /* With function descriptors on PPC64, the value of a symbol named
1318 ".FN", if it exists, is the entry point of the function "FN". */
1319 if (encoded[0] == '.')
1320 encoded += 1;
1321
29480c32
JB
1322 /* The name of the Ada main procedure starts with "_ada_".
1323 This prefix is not part of the decoded name, so skip this part
1324 if we see this prefix. */
61012eef 1325 if (startswith (encoded, "_ada_"))
4c4b4cd2 1326 encoded += 5;
81eaa506
TT
1327 /* The "___ghost_" prefix is used for ghost entities. Normally
1328 these aren't preserved but when they are, it's useful to see
1329 them. */
1330 if (startswith (encoded, "___ghost_"))
1331 encoded += 9;
14f9c5c9 1332
29480c32
JB
1333 /* If the name starts with '_', then it is not a properly encoded
1334 name, so do not attempt to decode it. Similarly, if the name
1335 starts with '<', the name should not be decoded. */
4c4b4cd2 1336 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1337 goto Suppress;
1338
4c4b4cd2 1339 len0 = strlen (encoded);
4c4b4cd2 1340
965bc1df
TT
1341 suffix = remove_compiler_suffix (encoded, &len0);
1342
29480c32
JB
1343 ada_remove_trailing_digits (encoded, &len0);
1344 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1345
4c4b4cd2
PH
1346 /* Remove the ___X.* suffix if present. Do not forget to verify that
1347 the suffix is located before the current "end" of ENCODED. We want
1348 to avoid re-matching parts of ENCODED that have previously been
1349 marked as discarded (by decrementing LEN0). */
1350 p = strstr (encoded, "___");
1351 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1352 {
1353 if (p[3] == 'X')
dda83cd7 1354 len0 = p - encoded;
14f9c5c9 1355 else
dda83cd7 1356 goto Suppress;
14f9c5c9 1357 }
4c4b4cd2 1358
29480c32
JB
1359 /* Remove any trailing TKB suffix. It tells us that this symbol
1360 is for the body of a task, but that information does not actually
1361 appear in the decoded name. */
1362
61012eef 1363 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1364 len0 -= 3;
76a01679 1365
a10967fa
JB
1366 /* Remove any trailing TB suffix. The TB suffix is slightly different
1367 from the TKB suffix because it is used for non-anonymous task
1368 bodies. */
1369
61012eef 1370 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1371 len0 -= 2;
1372
29480c32
JB
1373 /* Remove trailing "B" suffixes. */
1374 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1375
61012eef 1376 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1377 len0 -= 1;
1378
29480c32
JB
1379 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1380
4c4b4cd2 1381 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1382 {
4c4b4cd2
PH
1383 i = len0 - 2;
1384 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1385 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1386 i -= 1;
4c4b4cd2 1387 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1388 len0 = i - 1;
033bc52b 1389 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1390 len0 = i;
d2e4a39e 1391 }
14f9c5c9 1392
29480c32
JB
1393 /* The first few characters that are not alphabetic are not part
1394 of any encoding we use, so we can copy them over verbatim. */
1395
36f5ca53
TT
1396 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1397 decoded.push_back (encoded[i]);
14f9c5c9
AS
1398
1399 at_start_name = 1;
1400 while (i < len0)
1401 {
29480c32 1402 /* Is this a symbol function? */
5c94f938 1403 if (operators && at_start_name && encoded[i] == 'O')
dda83cd7
SM
1404 {
1405 int k;
1406
1407 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1408 {
1409 int op_len = strlen (ada_opname_table[k].encoded);
1410 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1411 op_len - 1) == 0)
1412 && !isalnum (encoded[i + op_len]))
1413 {
36f5ca53 1414 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1415 at_start_name = 0;
1416 i += op_len;
dda83cd7
SM
1417 break;
1418 }
1419 }
1420 if (ada_opname_table[k].encoded != NULL)
1421 continue;
1422 }
14f9c5c9
AS
1423 at_start_name = 0;
1424
529cad9c 1425 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1426 into "." (just below). */
529cad9c 1427
61012eef 1428 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1429 i += 2;
529cad9c 1430
29480c32 1431 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1432 be translated into "." (just below). These are internal names
1433 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1434
1435 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1436 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1437 && isdigit (encoded [i+4]))
1438 {
1439 int k = i + 5;
1440
1441 while (k < len0 && isdigit (encoded[k]))
1442 k++; /* Skip any extra digit. */
1443
1444 /* Double-check that the "__B_{DIGITS}+" sequence we found
1445 is indeed followed by "__". */
1446 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1447 i = k;
1448 }
29480c32 1449
529cad9c
PH
1450 /* Remove _E{DIGITS}+[sb] */
1451
1452 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1453 of subprograms created by the compiler for each entry. The first
1454 one implements the actual entry code, and has a suffix following
1455 the convention above; the second one implements the barrier and
1456 uses the same convention as above, except that the 'E' is replaced
1457 by a 'B'.
529cad9c 1458
dda83cd7
SM
1459 Just as above, we do not decode the name of barrier functions
1460 to give the user a clue that the code he is debugging has been
1461 internally generated. */
529cad9c
PH
1462
1463 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1464 && isdigit (encoded[i+2]))
1465 {
1466 int k = i + 3;
1467
1468 while (k < len0 && isdigit (encoded[k]))
1469 k++;
1470
1471 if (k < len0
1472 && (encoded[k] == 'b' || encoded[k] == 's'))
1473 {
1474 k++;
1475 /* Just as an extra precaution, make sure that if this
1476 suffix is followed by anything else, it is a '_'.
1477 Otherwise, we matched this sequence by accident. */
1478 if (k == len0
1479 || (k < len0 && encoded[k] == '_'))
1480 i = k;
1481 }
1482 }
529cad9c
PH
1483
1484 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1485 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1486
1487 if (i < len0 + 3
dda83cd7
SM
1488 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1489 {
1490 /* Backtrack a bit up until we reach either the begining of
1491 the encoded name, or "__". Make sure that we only find
1492 digits or lowercase characters. */
1493 const char *ptr = encoded + i - 1;
1494
1495 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1496 ptr--;
1497 if (ptr < encoded
1498 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1499 i++;
1500 }
529cad9c 1501
957ce537 1502 if (wide && i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
315e4ebb
TT
1503 {
1504 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1505 {
1506 i += 3;
1507 continue;
1508 }
1509 }
957ce537 1510 else if (wide && i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
315e4ebb
TT
1511 {
1512 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1513 {
1514 i += 5;
1515 continue;
1516 }
1517 }
957ce537 1518 else if (wide && i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
315e4ebb
TT
1519 && isxdigit (encoded[i + 2]))
1520 {
1521 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1522 {
1523 i += 10;
1524 continue;
1525 }
1526 }
1527
4c4b4cd2 1528 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1529 {
1530 /* This is a X[bn]* sequence not separated from the previous
1531 part of the name with a non-alpha-numeric character (in other
1532 words, immediately following an alpha-numeric character), then
1533 verify that it is placed at the end of the encoded name. If
1534 not, then the encoding is not valid and we should abort the
1535 decoding. Otherwise, just skip it, it is used in body-nested
1536 package names. */
1537 do
1538 i += 1;
1539 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1540 if (i < len0)
1541 goto Suppress;
1542 }
cdc7bb92 1543 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1544 {
1545 /* Replace '__' by '.'. */
36f5ca53 1546 decoded.push_back ('.');
dda83cd7
SM
1547 at_start_name = 1;
1548 i += 2;
dda83cd7 1549 }
14f9c5c9 1550 else
dda83cd7
SM
1551 {
1552 /* It's a character part of the decoded name, so just copy it
1553 over. */
36f5ca53 1554 decoded.push_back (encoded[i]);
dda83cd7 1555 i += 1;
dda83cd7 1556 }
14f9c5c9 1557 }
14f9c5c9 1558
29480c32
JB
1559 /* Decoded names should never contain any uppercase character.
1560 Double-check this, and abort the decoding if we find one. */
1561
5c94f938
TT
1562 if (operators)
1563 {
1564 for (i = 0; i < decoded.length(); ++i)
1565 if (isupper (decoded[i]) || decoded[i] == ' ')
1566 goto Suppress;
1567 }
14f9c5c9 1568
965bc1df
TT
1569 /* If the compiler added a suffix, append it now. */
1570 if (suffix >= 0)
1571 decoded = decoded + "[" + &encoded[suffix] + "]";
1572
f945dedf 1573 return decoded;
14f9c5c9
AS
1574
1575Suppress:
8a3df5ac
TT
1576 if (!wrap)
1577 return {};
1578
4c4b4cd2 1579 if (encoded[0] == '<')
f945dedf 1580 decoded = encoded;
14f9c5c9 1581 else
f945dedf 1582 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1583 return decoded;
4c4b4cd2
PH
1584}
1585
033bc52b
TT
1586#ifdef GDB_SELF_TEST
1587
1588static void
1589ada_decode_tests ()
1590{
1591 /* This isn't valid, but used to cause a crash. PR gdb/30639. The
1592 result does not really matter very much. */
1593 SELF_CHECK (ada_decode ("44") == "44");
1594}
1595
1596#endif
1597
4c4b4cd2
PH
1598/* Table for keeping permanent unique copies of decoded names. Once
1599 allocated, names in this table are never released. While this is a
1600 storage leak, it should not be significant unless there are massive
1601 changes in the set of decoded names in successive versions of a
1602 symbol table loaded during a single session. */
1603static struct htab *decoded_names_store;
1604
1605/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1606 in the language-specific part of GSYMBOL, if it has not been
1607 previously computed. Tries to save the decoded name in the same
1608 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1609 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1610 GSYMBOL).
4c4b4cd2
PH
1611 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1612 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1613 when a decoded name is cached in it. */
4c4b4cd2 1614
45e6c716 1615const char *
f85f34ed 1616ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1617{
f85f34ed
TT
1618 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1619 const char **resultp =
615b3f62 1620 &gsymbol->language_specific.demangled_name;
5b4ee69b 1621
f85f34ed 1622 if (!gsymbol->ada_mangled)
4c4b4cd2 1623 {
4d4eaa30 1624 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1625 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1626
f85f34ed 1627 gsymbol->ada_mangled = 1;
5b4ee69b 1628
f85f34ed 1629 if (obstack != NULL)
f945dedf 1630 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1631 else
dda83cd7 1632 {
f85f34ed
TT
1633 /* Sometimes, we can't find a corresponding objfile, in
1634 which case, we put the result on the heap. Since we only
1635 decode when needed, we hope this usually does not cause a
1636 significant memory leak (FIXME). */
1637
dda83cd7
SM
1638 char **slot = (char **) htab_find_slot (decoded_names_store,
1639 decoded.c_str (), INSERT);
5b4ee69b 1640
dda83cd7
SM
1641 if (*slot == NULL)
1642 *slot = xstrdup (decoded.c_str ());
1643 *resultp = *slot;
1644 }
4c4b4cd2 1645 }
14f9c5c9 1646
4c4b4cd2
PH
1647 return *resultp;
1648}
76a01679 1649
14f9c5c9 1650\f
d2e4a39e 1651
dda83cd7 1652 /* Arrays */
14f9c5c9 1653
28c85d6c
JB
1654/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1655 generated by the GNAT compiler to describe the index type used
1656 for each dimension of an array, check whether it follows the latest
1657 known encoding. If not, fix it up to conform to the latest encoding.
1658 Otherwise, do nothing. This function also does nothing if
1659 INDEX_DESC_TYPE is NULL.
1660
85102364 1661 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1662 Initially, the information would be provided through the name of each
1663 field of the structure type only, while the type of these fields was
1664 described as unspecified and irrelevant. The debugger was then expected
1665 to perform a global type lookup using the name of that field in order
1666 to get access to the full index type description. Because these global
1667 lookups can be very expensive, the encoding was later enhanced to make
1668 the global lookup unnecessary by defining the field type as being
1669 the full index type description.
1670
1671 The purpose of this routine is to allow us to support older versions
1672 of the compiler by detecting the use of the older encoding, and by
1673 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1674 we essentially replace each field's meaningless type by the associated
1675 index subtype). */
1676
1677void
1678ada_fixup_array_indexes_type (struct type *index_desc_type)
1679{
1680 int i;
1681
1682 if (index_desc_type == NULL)
1683 return;
1f704f76 1684 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1685
1686 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1687 to check one field only, no need to check them all). If not, return
1688 now.
1689
1690 If our INDEX_DESC_TYPE was generated using the older encoding,
1691 the field type should be a meaningless integer type whose name
1692 is not equal to the field name. */
940da03e
SM
1693 if (index_desc_type->field (0).type ()->name () != NULL
1694 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1695 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1696 return;
1697
1698 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1699 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1700 {
33d16dd9 1701 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1702 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1703
1704 if (raw_type)
5d14b6e5 1705 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1706 }
1707}
1708
4c4b4cd2
PH
1709/* The desc_* routines return primitive portions of array descriptors
1710 (fat pointers). */
14f9c5c9
AS
1711
1712/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1713 level of indirection, if needed. */
1714
d2e4a39e
AS
1715static struct type *
1716desc_base_type (struct type *type)
14f9c5c9
AS
1717{
1718 if (type == NULL)
1719 return NULL;
61ee279c 1720 type = ada_check_typedef (type);
78134374 1721 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1722 type = ada_typedef_target_type (type);
1723
1265e4aa 1724 if (type != NULL
78134374 1725 && (type->code () == TYPE_CODE_PTR
dda83cd7 1726 || type->code () == TYPE_CODE_REF))
27710edb 1727 return ada_check_typedef (type->target_type ());
14f9c5c9
AS
1728 else
1729 return type;
1730}
1731
4c4b4cd2
PH
1732/* True iff TYPE indicates a "thin" array pointer type. */
1733
14f9c5c9 1734static int
d2e4a39e 1735is_thin_pntr (struct type *type)
14f9c5c9 1736{
d2e4a39e 1737 return
14f9c5c9
AS
1738 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1739 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1740}
1741
4c4b4cd2
PH
1742/* The descriptor type for thin pointer type TYPE. */
1743
d2e4a39e
AS
1744static struct type *
1745thin_descriptor_type (struct type *type)
14f9c5c9 1746{
d2e4a39e 1747 struct type *base_type = desc_base_type (type);
5b4ee69b 1748
14f9c5c9
AS
1749 if (base_type == NULL)
1750 return NULL;
1751 if (is_suffix (ada_type_name (base_type), "___XVE"))
1752 return base_type;
d2e4a39e 1753 else
14f9c5c9 1754 {
d2e4a39e 1755 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1756
14f9c5c9 1757 if (alt_type == NULL)
dda83cd7 1758 return base_type;
14f9c5c9 1759 else
dda83cd7 1760 return alt_type;
14f9c5c9
AS
1761 }
1762}
1763
4c4b4cd2
PH
1764/* A pointer to the array data for thin-pointer value VAL. */
1765
d2e4a39e
AS
1766static struct value *
1767thin_data_pntr (struct value *val)
14f9c5c9 1768{
d0c97917 1769 struct type *type = ada_check_typedef (val->type ());
556bdfd4 1770 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1771
556bdfd4
UW
1772 data_type = lookup_pointer_type (data_type);
1773
78134374 1774 if (type->code () == TYPE_CODE_PTR)
cda03344 1775 return value_cast (data_type, val->copy ());
d2e4a39e 1776 else
9feb2d07 1777 return value_from_longest (data_type, val->address ());
14f9c5c9
AS
1778}
1779
4c4b4cd2
PH
1780/* True iff TYPE indicates a "thick" array pointer type. */
1781
14f9c5c9 1782static int
d2e4a39e 1783is_thick_pntr (struct type *type)
14f9c5c9
AS
1784{
1785 type = desc_base_type (type);
78134374 1786 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1787 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1788}
1789
4c4b4cd2
PH
1790/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1791 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1792
d2e4a39e
AS
1793static struct type *
1794desc_bounds_type (struct type *type)
14f9c5c9 1795{
d2e4a39e 1796 struct type *r;
14f9c5c9
AS
1797
1798 type = desc_base_type (type);
1799
1800 if (type == NULL)
1801 return NULL;
1802 else if (is_thin_pntr (type))
1803 {
1804 type = thin_descriptor_type (type);
1805 if (type == NULL)
dda83cd7 1806 return NULL;
14f9c5c9
AS
1807 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1808 if (r != NULL)
dda83cd7 1809 return ada_check_typedef (r);
14f9c5c9 1810 }
78134374 1811 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1812 {
1813 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1814 if (r != NULL)
27710edb 1815 return ada_check_typedef (ada_check_typedef (r)->target_type ());
14f9c5c9
AS
1816 }
1817 return NULL;
1818}
1819
1820/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1821 one, a pointer to its bounds data. Otherwise NULL. */
1822
d2e4a39e
AS
1823static struct value *
1824desc_bounds (struct value *arr)
14f9c5c9 1825{
d0c97917 1826 struct type *type = ada_check_typedef (arr->type ());
5b4ee69b 1827
d2e4a39e 1828 if (is_thin_pntr (type))
14f9c5c9 1829 {
d2e4a39e 1830 struct type *bounds_type =
dda83cd7 1831 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1832 LONGEST addr;
1833
4cdfadb1 1834 if (bounds_type == NULL)
dda83cd7 1835 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1836
1837 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1838 since desc_type is an XVE-encoded type (and shouldn't be),
1839 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1840 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1841 addr = value_as_long (arr);
d2e4a39e 1842 else
9feb2d07 1843 addr = arr->address ();
14f9c5c9 1844
d2e4a39e 1845 return
dda83cd7 1846 value_from_longest (lookup_pointer_type (bounds_type),
df86565b 1847 addr - bounds_type->length ());
14f9c5c9
AS
1848 }
1849
1850 else if (is_thick_pntr (type))
05e522ef 1851 {
158cc4fe 1852 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef 1853 _("Bad GNAT array descriptor"));
d0c97917 1854 struct type *p_bounds_type = p_bounds->type ();
05e522ef
JB
1855
1856 if (p_bounds_type
78134374 1857 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef 1858 {
27710edb 1859 struct type *target_type = p_bounds_type->target_type ();
05e522ef 1860
e46d3488 1861 if (target_type->is_stub ())
05e522ef
JB
1862 p_bounds = value_cast (lookup_pointer_type
1863 (ada_check_typedef (target_type)),
1864 p_bounds);
1865 }
1866 else
1867 error (_("Bad GNAT array descriptor"));
1868
1869 return p_bounds;
1870 }
14f9c5c9
AS
1871 else
1872 return NULL;
1873}
1874
4c4b4cd2
PH
1875/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1876 position of the field containing the address of the bounds data. */
1877
14f9c5c9 1878static int
d2e4a39e 1879fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1880{
b610c045 1881 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1882}
1883
1884/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1885 size of the field containing the address of the bounds data. */
1886
14f9c5c9 1887static int
d2e4a39e 1888fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1889{
1890 type = desc_base_type (type);
1891
3757d2d4
SM
1892 if (type->field (1).bitsize () > 0)
1893 return type->field (1).bitsize ();
14f9c5c9 1894 else
df86565b 1895 return 8 * ada_check_typedef (type->field (1).type ())->length ();
14f9c5c9
AS
1896}
1897
4c4b4cd2 1898/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1899 pointer to one, the type of its array data (a array-with-no-bounds type);
1900 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1901 data. */
4c4b4cd2 1902
d2e4a39e 1903static struct type *
556bdfd4 1904desc_data_target_type (struct type *type)
14f9c5c9
AS
1905{
1906 type = desc_base_type (type);
1907
4c4b4cd2 1908 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1909 if (is_thin_pntr (type))
940da03e 1910 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1911 else if (is_thick_pntr (type))
556bdfd4
UW
1912 {
1913 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1914
1915 if (data_type
78134374 1916 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
27710edb 1917 return ada_check_typedef (data_type->target_type ());
556bdfd4
UW
1918 }
1919
1920 return NULL;
14f9c5c9
AS
1921}
1922
1923/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1924 its array data. */
4c4b4cd2 1925
d2e4a39e
AS
1926static struct value *
1927desc_data (struct value *arr)
14f9c5c9 1928{
d0c97917 1929 struct type *type = arr->type ();
5b4ee69b 1930
14f9c5c9
AS
1931 if (is_thin_pntr (type))
1932 return thin_data_pntr (arr);
1933 else if (is_thick_pntr (type))
158cc4fe 1934 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1935 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1936 else
1937 return NULL;
1938}
1939
1940
1941/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1942 position of the field containing the address of the data. */
1943
14f9c5c9 1944static int
d2e4a39e 1945fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1946{
b610c045 1947 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1948}
1949
1950/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1951 size of the field containing the address of the data. */
1952
14f9c5c9 1953static int
d2e4a39e 1954fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1955{
1956 type = desc_base_type (type);
1957
3757d2d4
SM
1958 if (type->field (0).bitsize () > 0)
1959 return type->field (0).bitsize ();
d2e4a39e 1960 else
df86565b 1961 return TARGET_CHAR_BIT * type->field (0).type ()->length ();
14f9c5c9
AS
1962}
1963
4c4b4cd2 1964/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1965 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1966 bound, if WHICH is 1. The first bound is I=1. */
1967
d2e4a39e
AS
1968static struct value *
1969desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1970{
250106a7
TT
1971 char bound_name[20];
1972 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1973 which ? 'U' : 'L', i - 1);
158cc4fe 1974 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1975 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1976}
1977
1978/* If BOUNDS is an array-bounds structure type, return the bit position
1979 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1980 bound, if WHICH is 1. The first bound is I=1. */
1981
14f9c5c9 1982static int
d2e4a39e 1983desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1984{
b610c045 1985 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1986}
1987
1988/* If BOUNDS is an array-bounds structure type, return the bit field size
1989 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1990 bound, if WHICH is 1. The first bound is I=1. */
1991
76a01679 1992static int
d2e4a39e 1993desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1994{
1995 type = desc_base_type (type);
1996
3757d2d4
SM
1997 if (type->field (2 * i + which - 2).bitsize () > 0)
1998 return type->field (2 * i + which - 2).bitsize ();
d2e4a39e 1999 else
df86565b 2000 return 8 * type->field (2 * i + which - 2).type ()->length ();
14f9c5c9
AS
2001}
2002
2003/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
2004 Ith bound (numbering from 1). Otherwise, NULL. */
2005
d2e4a39e
AS
2006static struct type *
2007desc_index_type (struct type *type, int i)
14f9c5c9
AS
2008{
2009 type = desc_base_type (type);
2010
78134374 2011 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
2012 {
2013 char bound_name[20];
2014 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
2015 return lookup_struct_elt_type (type, bound_name, 1);
2016 }
d2e4a39e 2017 else
14f9c5c9
AS
2018 return NULL;
2019}
2020
4c4b4cd2
PH
2021/* The number of index positions in the array-bounds type TYPE.
2022 Return 0 if TYPE is NULL. */
2023
14f9c5c9 2024static int
d2e4a39e 2025desc_arity (struct type *type)
14f9c5c9
AS
2026{
2027 type = desc_base_type (type);
2028
2029 if (type != NULL)
1f704f76 2030 return type->num_fields () / 2;
14f9c5c9
AS
2031 return 0;
2032}
2033
4c4b4cd2
PH
2034/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2035 an array descriptor type (representing an unconstrained array
2036 type). */
2037
76a01679
JB
2038static int
2039ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2040{
2041 if (type == NULL)
2042 return 0;
61ee279c 2043 type = ada_check_typedef (type);
78134374 2044 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2045 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2046}
2047
52ce6436 2048/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2049 * to one. */
52ce6436 2050
2c0b251b 2051static int
52ce6436
PH
2052ada_is_array_type (struct type *type)
2053{
78134374
SM
2054 while (type != NULL
2055 && (type->code () == TYPE_CODE_PTR
2056 || type->code () == TYPE_CODE_REF))
27710edb 2057 type = type->target_type ();
52ce6436
PH
2058 return ada_is_direct_array_type (type);
2059}
2060
4c4b4cd2 2061/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2062
14f9c5c9 2063int
4c4b4cd2 2064ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2065{
2066 if (type == NULL)
2067 return 0;
61ee279c 2068 type = ada_check_typedef (type);
78134374
SM
2069 return (type->code () == TYPE_CODE_ARRAY
2070 || (type->code () == TYPE_CODE_PTR
27710edb 2071 && (ada_check_typedef (type->target_type ())->code ()
78134374 2072 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2073}
2074
4c4b4cd2
PH
2075/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2076
14f9c5c9 2077int
4c4b4cd2 2078ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2079{
556bdfd4 2080 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2081
2082 if (type == NULL)
2083 return 0;
61ee279c 2084 type = ada_check_typedef (type);
556bdfd4 2085 return (data_type != NULL
78134374 2086 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2087 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2088}
2089
4c4b4cd2 2090/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2091 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2092 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2093 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2094 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2095 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2096 a descriptor. */
de93309a
SM
2097
2098static struct type *
d2e4a39e 2099ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2100{
d0c97917
TT
2101 if (ada_is_constrained_packed_array_type (arr->type ()))
2102 return decode_constrained_packed_array_type (arr->type ());
14f9c5c9 2103
d0c97917
TT
2104 if (!ada_is_array_descriptor_type (arr->type ()))
2105 return arr->type ();
d2e4a39e
AS
2106
2107 if (!bounds)
ad82864c
JB
2108 {
2109 struct type *array_type =
d0c97917 2110 ada_check_typedef (desc_data_target_type (arr->type ()));
ad82864c 2111
d0c97917 2112 if (ada_is_unconstrained_packed_array_type (arr->type ()))
886176b8
SM
2113 array_type->field (0).set_bitsize
2114 (decode_packed_array_bitsize (arr->type ()));
2115
ad82864c
JB
2116 return array_type;
2117 }
14f9c5c9
AS
2118 else
2119 {
d2e4a39e 2120 struct type *elt_type;
14f9c5c9 2121 int arity;
d2e4a39e 2122 struct value *descriptor;
14f9c5c9 2123
d0c97917
TT
2124 elt_type = ada_array_element_type (arr->type (), -1);
2125 arity = ada_array_arity (arr->type ());
14f9c5c9 2126
d2e4a39e 2127 if (elt_type == NULL || arity == 0)
d0c97917 2128 return ada_check_typedef (arr->type ());
14f9c5c9
AS
2129
2130 descriptor = desc_bounds (arr);
d2e4a39e 2131 if (value_as_long (descriptor) == 0)
dda83cd7 2132 return NULL;
d2e4a39e 2133 while (arity > 0)
dda83cd7 2134 {
9fa83a7a 2135 type_allocator alloc (arr->type ());
dda83cd7
SM
2136 struct value *low = desc_one_bound (descriptor, arity, 0);
2137 struct value *high = desc_one_bound (descriptor, arity, 1);
2138
2139 arity -= 1;
e727c536
TT
2140 struct type *range_type
2141 = create_static_range_type (alloc, low->type (),
2142 longest_to_int (value_as_long (low)),
2143 longest_to_int (value_as_long (high)));
9e76b17a 2144 elt_type = create_array_type (alloc, elt_type, range_type);
cf1eca3c 2145 INIT_GNAT_SPECIFIC (elt_type);
ad82864c 2146
d0c97917 2147 if (ada_is_unconstrained_packed_array_type (arr->type ()))
e67ad678
JB
2148 {
2149 /* We need to store the element packed bitsize, as well as
dda83cd7 2150 recompute the array size, because it was previously
e67ad678
JB
2151 computed based on the unpacked element size. */
2152 LONGEST lo = value_as_long (low);
2153 LONGEST hi = value_as_long (high);
2154
886176b8
SM
2155 elt_type->field (0).set_bitsize
2156 (decode_packed_array_bitsize (arr->type ()));
2157
e67ad678 2158 /* If the array has no element, then the size is already
dda83cd7 2159 zero, and does not need to be recomputed. */
e67ad678
JB
2160 if (lo < hi)
2161 {
2162 int array_bitsize =
3757d2d4 2163 (hi - lo + 1) * elt_type->field (0).bitsize ();
e67ad678 2164
9e76b17a 2165 elt_type->set_length ((array_bitsize + 7) / 8);
e67ad678
JB
2166 }
2167 }
dda83cd7 2168 }
14f9c5c9
AS
2169
2170 return lookup_pointer_type (elt_type);
2171 }
2172}
2173
2174/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2175 Otherwise, returns either a standard GDB array with bounds set
2176 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2177 GDB array. Returns NULL if ARR is a null fat pointer. */
2178
d2e4a39e
AS
2179struct value *
2180ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2181{
d0c97917 2182 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2183 {
d2e4a39e 2184 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2185
14f9c5c9 2186 if (arrType == NULL)
dda83cd7 2187 return NULL;
cda03344 2188 return value_cast (arrType, desc_data (arr)->copy ());
14f9c5c9 2189 }
d0c97917 2190 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2191 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2192 else
2193 return arr;
2194}
2195
2196/* If ARR does not represent an array, returns ARR unchanged.
2197 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2198 be ARR itself if it already is in the proper form). */
2199
720d1a40 2200struct value *
d2e4a39e 2201ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2202{
d0c97917 2203 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2204 {
d2e4a39e 2205 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2206
14f9c5c9 2207 if (arrVal == NULL)
dda83cd7 2208 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2209 return value_ind (arrVal);
2210 }
d0c97917 2211 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2212 return decode_constrained_packed_array (arr);
d2e4a39e 2213 else
14f9c5c9
AS
2214 return arr;
2215}
2216
2217/* If TYPE represents a GNAT array type, return it translated to an
2218 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2219 packing). For other types, is the identity. */
2220
d2e4a39e
AS
2221struct type *
2222ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2223{
ad82864c
JB
2224 if (ada_is_constrained_packed_array_type (type))
2225 return decode_constrained_packed_array_type (type);
17280b9f
UW
2226
2227 if (ada_is_array_descriptor_type (type))
556bdfd4 2228 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2229
2230 return type;
14f9c5c9
AS
2231}
2232
4c4b4cd2
PH
2233/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2234
ad82864c 2235static int
57567375 2236ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2237{
2238 if (type == NULL)
2239 return 0;
4c4b4cd2 2240 type = desc_base_type (type);
61ee279c 2241 type = ada_check_typedef (type);
d2e4a39e 2242 return
14f9c5c9
AS
2243 ada_type_name (type) != NULL
2244 && strstr (ada_type_name (type), "___XP") != NULL;
2245}
2246
ad82864c
JB
2247/* Non-zero iff TYPE represents a standard GNAT constrained
2248 packed-array type. */
2249
2250int
2251ada_is_constrained_packed_array_type (struct type *type)
2252{
57567375 2253 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2254 && !ada_is_array_descriptor_type (type);
2255}
2256
2257/* Non-zero iff TYPE represents an array descriptor for a
2258 unconstrained packed-array type. */
2259
2260static int
2261ada_is_unconstrained_packed_array_type (struct type *type)
2262{
57567375
TT
2263 if (!ada_is_array_descriptor_type (type))
2264 return 0;
2265
2266 if (ada_is_gnat_encoded_packed_array_type (type))
2267 return 1;
2268
2269 /* If we saw GNAT encodings, then the above code is sufficient.
2270 However, with minimal encodings, we will just have a thick
2271 pointer instead. */
2272 if (is_thick_pntr (type))
2273 {
2274 type = desc_base_type (type);
2275 /* The structure's first field is a pointer to an array, so this
2276 fetches the array type. */
27710edb 2277 type = type->field (0).type ()->target_type ();
af5300fe
TV
2278 if (type->code () == TYPE_CODE_TYPEDEF)
2279 type = ada_typedef_target_type (type);
57567375 2280 /* Now we can see if the array elements are packed. */
3757d2d4 2281 return type->field (0).bitsize () > 0;
57567375
TT
2282 }
2283
2284 return 0;
ad82864c
JB
2285}
2286
c9a28cbe
TT
2287/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2288 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2289
2290static bool
2291ada_is_any_packed_array_type (struct type *type)
2292{
2293 return (ada_is_constrained_packed_array_type (type)
2294 || (type->code () == TYPE_CODE_ARRAY
3757d2d4 2295 && type->field (0).bitsize () % 8 != 0));
c9a28cbe
TT
2296}
2297
ad82864c
JB
2298/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2299 return the size of its elements in bits. */
2300
2301static long
2302decode_packed_array_bitsize (struct type *type)
2303{
0d5cff50
DE
2304 const char *raw_name;
2305 const char *tail;
ad82864c
JB
2306 long bits;
2307
720d1a40
JB
2308 /* Access to arrays implemented as fat pointers are encoded as a typedef
2309 of the fat pointer type. We need the name of the fat pointer type
2310 to do the decoding, so strip the typedef layer. */
78134374 2311 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2312 type = ada_typedef_target_type (type);
2313
2314 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2315 if (!raw_name)
2316 raw_name = ada_type_name (desc_base_type (type));
2317
2318 if (!raw_name)
2319 return 0;
2320
2321 tail = strstr (raw_name, "___XP");
57567375
TT
2322 if (tail == nullptr)
2323 {
2324 gdb_assert (is_thick_pntr (type));
2325 /* The structure's first field is a pointer to an array, so this
2326 fetches the array type. */
27710edb 2327 type = type->field (0).type ()->target_type ();
57567375 2328 /* Now we can see if the array elements are packed. */
3757d2d4 2329 return type->field (0).bitsize ();
57567375 2330 }
ad82864c
JB
2331
2332 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2333 {
2334 lim_warning
2335 (_("could not understand bit size information on packed array"));
2336 return 0;
2337 }
2338
2339 return bits;
2340}
2341
14f9c5c9
AS
2342/* Given that TYPE is a standard GDB array type with all bounds filled
2343 in, and that the element size of its ultimate scalar constituents
2344 (that is, either its elements, or, if it is an array of arrays, its
2345 elements' elements, etc.) is *ELT_BITS, return an identical type,
2346 but with the bit sizes of its elements (and those of any
2347 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2348 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2349 in bits.
2350
2351 Note that, for arrays whose index type has an XA encoding where
2352 a bound references a record discriminant, getting that discriminant,
2353 and therefore the actual value of that bound, is not possible
2354 because none of the given parameters gives us access to the record.
2355 This function assumes that it is OK in the context where it is being
2356 used to return an array whose bounds are still dynamic and where
2357 the length is arbitrary. */
4c4b4cd2 2358
d2e4a39e 2359static struct type *
ad82864c 2360constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2361{
d2e4a39e
AS
2362 struct type *new_elt_type;
2363 struct type *new_type;
99b1c762
JB
2364 struct type *index_type_desc;
2365 struct type *index_type;
14f9c5c9
AS
2366 LONGEST low_bound, high_bound;
2367
61ee279c 2368 type = ada_check_typedef (type);
78134374 2369 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2370 return type;
2371
99b1c762
JB
2372 index_type_desc = ada_find_parallel_type (type, "___XA");
2373 if (index_type_desc)
940da03e 2374 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2375 NULL);
2376 else
3d967001 2377 index_type = type->index_type ();
99b1c762 2378
9e76b17a 2379 type_allocator alloc (type);
ad82864c 2380 new_elt_type =
27710edb 2381 constrained_packed_array_type (ada_check_typedef (type->target_type ()),
ad82864c 2382 elt_bits);
9e76b17a 2383 new_type = create_array_type (alloc, new_elt_type, index_type);
886176b8 2384 new_type->field (0).set_bitsize (*elt_bits);
d0e39ea2 2385 new_type->set_name (ada_type_name (type));
14f9c5c9 2386
78134374 2387 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2388 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2389 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2390 low_bound = high_bound = 0;
2391 if (high_bound < low_bound)
b6cdbc9a
SM
2392 {
2393 *elt_bits = 0;
2394 new_type->set_length (0);
2395 }
d2e4a39e 2396 else
14f9c5c9
AS
2397 {
2398 *elt_bits *= (high_bound - low_bound + 1);
b6cdbc9a 2399 new_type->set_length ((*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
14f9c5c9
AS
2400 }
2401
9cdd0d12 2402 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2403 return new_type;
2404}
2405
ad82864c
JB
2406/* The array type encoded by TYPE, where
2407 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2408
d2e4a39e 2409static struct type *
ad82864c 2410decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2411{
0d5cff50 2412 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2413 char *name;
0d5cff50 2414 const char *tail;
d2e4a39e 2415 struct type *shadow_type;
14f9c5c9 2416 long bits;
14f9c5c9 2417
727e3d2e
JB
2418 if (!raw_name)
2419 raw_name = ada_type_name (desc_base_type (type));
2420
2421 if (!raw_name)
2422 return NULL;
2423
2424 name = (char *) alloca (strlen (raw_name) + 1);
2425 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2426 type = desc_base_type (type);
2427
14f9c5c9
AS
2428 memcpy (name, raw_name, tail - raw_name);
2429 name[tail - raw_name] = '\000';
2430
b4ba55a1
JB
2431 shadow_type = ada_find_parallel_type_with_name (type, name);
2432
2433 if (shadow_type == NULL)
14f9c5c9 2434 {
323e0a4a 2435 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2436 return NULL;
2437 }
f168693b 2438 shadow_type = check_typedef (shadow_type);
14f9c5c9 2439
78134374 2440 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2441 {
0963b4bd
MS
2442 lim_warning (_("could not understand bounds "
2443 "information on packed array"));
14f9c5c9
AS
2444 return NULL;
2445 }
d2e4a39e 2446
ad82864c
JB
2447 bits = decode_packed_array_bitsize (type);
2448 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2449}
2450
a7400e44
TT
2451/* Helper function for decode_constrained_packed_array. Set the field
2452 bitsize on a series of packed arrays. Returns the number of
2453 elements in TYPE. */
2454
2455static LONGEST
2456recursively_update_array_bitsize (struct type *type)
2457{
2458 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2459
2460 LONGEST low, high;
1f8d2881 2461 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2462 || low > high)
2463 return 0;
2464 LONGEST our_len = high - low + 1;
2465
27710edb 2466 struct type *elt_type = type->target_type ();
a7400e44
TT
2467 if (elt_type->code () == TYPE_CODE_ARRAY)
2468 {
2469 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
3757d2d4 2470 LONGEST elt_bitsize = elt_len * elt_type->field (0).bitsize ();
886176b8 2471 type->field (0).set_bitsize (elt_bitsize);
a7400e44 2472
b6cdbc9a
SM
2473 type->set_length (((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2474 / HOST_CHAR_BIT));
a7400e44
TT
2475 }
2476
2477 return our_len;
2478}
2479
ad82864c
JB
2480/* Given that ARR is a struct value *indicating a GNAT constrained packed
2481 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2482 standard GDB array type except that the BITSIZEs of the array
2483 target types are set to the number of bits in each element, and the
4c4b4cd2 2484 type length is set appropriately. */
14f9c5c9 2485
d2e4a39e 2486static struct value *
ad82864c 2487decode_constrained_packed_array (struct value *arr)
14f9c5c9 2488{
4c4b4cd2 2489 struct type *type;
14f9c5c9 2490
11aa919a
PMR
2491 /* If our value is a pointer, then dereference it. Likewise if
2492 the value is a reference. Make sure that this operation does not
2493 cause the target type to be fixed, as this would indirectly cause
2494 this array to be decoded. The rest of the routine assumes that
2495 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2496 and "value_ind" routines to perform the dereferencing, as opposed
2497 to using "ada_coerce_ref" or "ada_value_ind". */
2498 arr = coerce_ref (arr);
d0c97917 2499 if (ada_check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
284614f0 2500 arr = value_ind (arr);
4c4b4cd2 2501
d0c97917 2502 type = decode_constrained_packed_array_type (arr->type ());
14f9c5c9
AS
2503 if (type == NULL)
2504 {
323e0a4a 2505 error (_("can't unpack array"));
14f9c5c9
AS
2506 return NULL;
2507 }
61ee279c 2508
a7400e44
TT
2509 /* Decoding the packed array type could not correctly set the field
2510 bitsizes for any dimension except the innermost, because the
2511 bounds may be variable and were not passed to that function. So,
2512 we further resolve the array bounds here and then update the
2513 sizes. */
efaf1ae0 2514 const gdb_byte *valaddr = arr->contents_for_printing ().data ();
9feb2d07 2515 CORE_ADDR address = arr->address ();
a7400e44 2516 gdb::array_view<const gdb_byte> view
df86565b 2517 = gdb::make_array_view (valaddr, type->length ());
a7400e44
TT
2518 type = resolve_dynamic_type (type, view, address);
2519 recursively_update_array_bitsize (type);
2520
d0c97917
TT
2521 if (type_byte_order (arr->type ()) == BFD_ENDIAN_BIG
2522 && ada_is_modular_type (arr->type ()))
61ee279c
PH
2523 {
2524 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2525 array with no wrapper. In order to interpret the value through
2526 the (left-justified) packed array type we just built, we must
2527 first left-justify it. */
61ee279c
PH
2528 int bit_size, bit_pos;
2529 ULONGEST mod;
2530
d0c97917 2531 mod = ada_modulus (arr->type ()) - 1;
61ee279c
PH
2532 bit_size = 0;
2533 while (mod > 0)
2534 {
2535 bit_size += 1;
2536 mod >>= 1;
2537 }
d0c97917 2538 bit_pos = HOST_CHAR_BIT * arr->type ()->length () - bit_size;
61ee279c
PH
2539 arr = ada_value_primitive_packed_val (arr, NULL,
2540 bit_pos / HOST_CHAR_BIT,
2541 bit_pos % HOST_CHAR_BIT,
2542 bit_size,
2543 type);
2544 }
2545
4c4b4cd2 2546 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2547}
2548
2549
2550/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2551 given in IND. ARR must be a simple array. */
14f9c5c9 2552
d2e4a39e
AS
2553static struct value *
2554value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2555{
2556 int i;
2557 int bits, elt_off, bit_off;
2558 long elt_total_bit_offset;
d2e4a39e
AS
2559 struct type *elt_type;
2560 struct value *v;
14f9c5c9
AS
2561
2562 bits = 0;
2563 elt_total_bit_offset = 0;
d0c97917 2564 elt_type = ada_check_typedef (arr->type ());
d2e4a39e 2565 for (i = 0; i < arity; i += 1)
14f9c5c9 2566 {
78134374 2567 if (elt_type->code () != TYPE_CODE_ARRAY
3757d2d4 2568 || elt_type->field (0).bitsize () == 0)
dda83cd7
SM
2569 error
2570 (_("attempt to do packed indexing of "
0963b4bd 2571 "something other than a packed array"));
14f9c5c9 2572 else
dda83cd7
SM
2573 {
2574 struct type *range_type = elt_type->index_type ();
2575 LONGEST lowerbound, upperbound;
2576 LONGEST idx;
2577
1f8d2881 2578 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2579 {
2580 lim_warning (_("don't know bounds of array"));
2581 lowerbound = upperbound = 0;
2582 }
2583
2584 idx = pos_atr (ind[i]);
2585 if (idx < lowerbound || idx > upperbound)
2586 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2587 (long) idx);
3757d2d4 2588 bits = elt_type->field (0).bitsize ();
dda83cd7 2589 elt_total_bit_offset += (idx - lowerbound) * bits;
27710edb 2590 elt_type = ada_check_typedef (elt_type->target_type ());
dda83cd7 2591 }
14f9c5c9
AS
2592 }
2593 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2594 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2595
2596 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2597 bits, elt_type);
14f9c5c9
AS
2598 return v;
2599}
2600
4c4b4cd2 2601/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2602
2603static int
d2e4a39e 2604has_negatives (struct type *type)
14f9c5c9 2605{
78134374 2606 switch (type->code ())
d2e4a39e
AS
2607 {
2608 default:
2609 return 0;
2610 case TYPE_CODE_INT:
c6d940a9 2611 return !type->is_unsigned ();
d2e4a39e 2612 case TYPE_CODE_RANGE:
5537ddd0 2613 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2614 }
14f9c5c9 2615}
d2e4a39e 2616
f93fca70 2617/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2618 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2619 the unpacked buffer.
14f9c5c9 2620
5b639dea
JB
2621 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2622 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2623
f93fca70
JB
2624 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2625 zero otherwise.
14f9c5c9 2626
f93fca70 2627 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2628
f93fca70
JB
2629 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2630
2631static void
2632ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2633 gdb_byte *unpacked, int unpacked_len,
2634 int is_big_endian, int is_signed_type,
2635 int is_scalar)
2636{
a1c95e6b
JB
2637 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2638 int src_idx; /* Index into the source area */
2639 int src_bytes_left; /* Number of source bytes left to process. */
2640 int srcBitsLeft; /* Number of source bits left to move */
2641 int unusedLS; /* Number of bits in next significant
dda83cd7 2642 byte of source that are unused */
a1c95e6b 2643
a1c95e6b
JB
2644 int unpacked_idx; /* Index into the unpacked buffer */
2645 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2646
4c4b4cd2 2647 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2648 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2649 unsigned char sign;
a1c95e6b 2650
4c4b4cd2
PH
2651 /* Transmit bytes from least to most significant; delta is the direction
2652 the indices move. */
f93fca70 2653 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2654
5b639dea
JB
2655 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2656 bits from SRC. .*/
2657 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2658 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2659 bit_size, unpacked_len);
2660
14f9c5c9 2661 srcBitsLeft = bit_size;
086ca51f 2662 src_bytes_left = src_len;
f93fca70 2663 unpacked_bytes_left = unpacked_len;
14f9c5c9 2664 sign = 0;
f93fca70
JB
2665
2666 if (is_big_endian)
14f9c5c9 2667 {
086ca51f 2668 src_idx = src_len - 1;
f93fca70
JB
2669 if (is_signed_type
2670 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2671 sign = ~0;
d2e4a39e
AS
2672
2673 unusedLS =
dda83cd7
SM
2674 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2675 % HOST_CHAR_BIT;
14f9c5c9 2676
f93fca70
JB
2677 if (is_scalar)
2678 {
dda83cd7
SM
2679 accumSize = 0;
2680 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2681 }
2682 else
2683 {
dda83cd7
SM
2684 /* Non-scalar values must be aligned at a byte boundary... */
2685 accumSize =
2686 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2687 /* ... And are placed at the beginning (most-significant) bytes
2688 of the target. */
2689 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2690 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2691 }
14f9c5c9 2692 }
d2e4a39e 2693 else
14f9c5c9
AS
2694 {
2695 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2696
086ca51f 2697 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2698 unusedLS = bit_offset;
2699 accumSize = 0;
2700
f93fca70 2701 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2702 sign = ~0;
14f9c5c9 2703 }
d2e4a39e 2704
14f9c5c9 2705 accum = 0;
086ca51f 2706 while (src_bytes_left > 0)
14f9c5c9
AS
2707 {
2708 /* Mask for removing bits of the next source byte that are not
dda83cd7 2709 part of the value. */
d2e4a39e 2710 unsigned int unusedMSMask =
dda83cd7
SM
2711 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2712 1;
4c4b4cd2 2713 /* Sign-extend bits for this byte. */
14f9c5c9 2714 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2715
d2e4a39e 2716 accum |=
dda83cd7 2717 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2718 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2719 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2720 {
2721 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2722 accumSize -= HOST_CHAR_BIT;
2723 accum >>= HOST_CHAR_BIT;
2724 unpacked_bytes_left -= 1;
2725 unpacked_idx += delta;
2726 }
14f9c5c9
AS
2727 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2728 unusedLS = 0;
086ca51f
JB
2729 src_bytes_left -= 1;
2730 src_idx += delta;
14f9c5c9 2731 }
086ca51f 2732 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2733 {
2734 accum |= sign << accumSize;
db297a65 2735 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2736 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2737 if (accumSize < 0)
2738 accumSize = 0;
14f9c5c9 2739 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2740 unpacked_bytes_left -= 1;
2741 unpacked_idx += delta;
14f9c5c9 2742 }
f93fca70
JB
2743}
2744
2745/* Create a new value of type TYPE from the contents of OBJ starting
2746 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2747 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2748 assigning through the result will set the field fetched from.
2749 VALADDR is ignored unless OBJ is NULL, in which case,
2750 VALADDR+OFFSET must address the start of storage containing the
2751 packed value. The value returned in this case is never an lval.
2752 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2753
2754struct value *
2755ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2756 long offset, int bit_offset, int bit_size,
dda83cd7 2757 struct type *type)
f93fca70
JB
2758{
2759 struct value *v;
bfb1c796 2760 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2761 gdb_byte *unpacked;
220475ed 2762 const int is_scalar = is_scalar_type (type);
d5a22e77 2763 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2764 gdb::byte_vector staging;
f93fca70
JB
2765
2766 type = ada_check_typedef (type);
2767
d0a9e810 2768 if (obj == NULL)
bfb1c796 2769 src = valaddr + offset;
d0a9e810 2770 else
efaf1ae0 2771 src = obj->contents ().data () + offset;
d0a9e810
JB
2772
2773 if (is_dynamic_type (type))
2774 {
2775 /* The length of TYPE might by dynamic, so we need to resolve
2776 TYPE in order to know its actual size, which we then use
2777 to create the contents buffer of the value we return.
2778 The difficulty is that the data containing our object is
2779 packed, and therefore maybe not at a byte boundary. So, what
2780 we do, is unpack the data into a byte-aligned buffer, and then
2781 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2782 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2783 staging.resize (staging_len);
d0a9e810
JB
2784
2785 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2786 staging.data (), staging.size (),
d0a9e810
JB
2787 is_big_endian, has_negatives (type),
2788 is_scalar);
b249d2c2 2789 type = resolve_dynamic_type (type, staging, 0);
df86565b 2790 if (type->length () < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
0cafa88c
JB
2791 {
2792 /* This happens when the length of the object is dynamic,
2793 and is actually smaller than the space reserved for it.
2794 For instance, in an array of variant records, the bit_size
2795 we're given is the array stride, which is constant and
2796 normally equal to the maximum size of its element.
2797 But, in reality, each element only actually spans a portion
2798 of that stride. */
df86565b 2799 bit_size = type->length () * HOST_CHAR_BIT;
0cafa88c 2800 }
d0a9e810
JB
2801 }
2802
f93fca70
JB
2803 if (obj == NULL)
2804 {
317c3ed9 2805 v = value::allocate (type);
bfb1c796 2806 src = valaddr + offset;
f93fca70 2807 }
736355f2 2808 else if (obj->lval () == lval_memory && obj->lazy ())
f93fca70 2809 {
0cafa88c 2810 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2811 gdb_byte *buf;
0cafa88c 2812
9feb2d07 2813 v = value_at (type, obj->address () + offset);
bfb1c796 2814 buf = (gdb_byte *) alloca (src_len);
9feb2d07 2815 read_memory (v->address (), buf, src_len);
bfb1c796 2816 src = buf;
f93fca70
JB
2817 }
2818 else
2819 {
317c3ed9 2820 v = value::allocate (type);
efaf1ae0 2821 src = obj->contents ().data () + offset;
f93fca70
JB
2822 }
2823
2824 if (obj != NULL)
2825 {
2826 long new_offset = offset;
2827
8181b7b6 2828 v->set_component_location (obj);
5011c493 2829 v->set_bitpos (bit_offset + obj->bitpos ());
f49d5fa2 2830 v->set_bitsize (bit_size);
5011c493 2831 if (v->bitpos () >= HOST_CHAR_BIT)
dda83cd7 2832 {
f93fca70 2833 ++new_offset;
5011c493 2834 v->set_bitpos (v->bitpos () - HOST_CHAR_BIT);
dda83cd7 2835 }
76675c4d 2836 v->set_offset (new_offset);
f93fca70
JB
2837
2838 /* Also set the parent value. This is needed when trying to
2839 assign a new value (in inferior memory). */
fac7bdaa 2840 v->set_parent (obj);
f93fca70
JB
2841 }
2842 else
f49d5fa2 2843 v->set_bitsize (bit_size);
bbe912ba 2844 unpacked = v->contents_writeable ().data ();
f93fca70
JB
2845
2846 if (bit_size == 0)
2847 {
df86565b 2848 memset (unpacked, 0, type->length ());
f93fca70
JB
2849 return v;
2850 }
2851
df86565b 2852 if (staging.size () == type->length ())
f93fca70 2853 {
d0a9e810
JB
2854 /* Small short-cut: If we've unpacked the data into a buffer
2855 of the same size as TYPE's length, then we can reuse that,
2856 instead of doing the unpacking again. */
d5722aa2 2857 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2858 }
d0a9e810
JB
2859 else
2860 ada_unpack_from_contents (src, bit_offset, bit_size,
df86565b 2861 unpacked, type->length (),
d0a9e810 2862 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2863
14f9c5c9
AS
2864 return v;
2865}
d2e4a39e 2866
14f9c5c9
AS
2867/* Store the contents of FROMVAL into the location of TOVAL.
2868 Return a new value with the location of TOVAL and contents of
2869 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2870 floating-point or non-scalar types. */
14f9c5c9 2871
d2e4a39e
AS
2872static struct value *
2873ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2874{
d0c97917 2875 struct type *type = toval->type ();
f49d5fa2 2876 int bits = toval->bitsize ();
14f9c5c9 2877
52ce6436
PH
2878 toval = ada_coerce_ref (toval);
2879 fromval = ada_coerce_ref (fromval);
2880
d0c97917 2881 if (ada_is_direct_array_type (toval->type ()))
52ce6436 2882 toval = ada_coerce_to_simple_array (toval);
d0c97917 2883 if (ada_is_direct_array_type (fromval->type ()))
52ce6436
PH
2884 fromval = ada_coerce_to_simple_array (fromval);
2885
4b53ca88 2886 if (!toval->deprecated_modifiable ())
323e0a4a 2887 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2888
736355f2 2889 if (toval->lval () == lval_memory
14f9c5c9 2890 && bits > 0
78134374 2891 && (type->code () == TYPE_CODE_FLT
dda83cd7 2892 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2893 {
5011c493 2894 int len = (toval->bitpos ()
df407dfe 2895 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2896 int from_size;
224c3ddb 2897 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2898 struct value *val;
9feb2d07 2899 CORE_ADDR to_addr = toval->address ();
14f9c5c9 2900
78134374 2901 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2902 fromval = value_cast (type, fromval);
14f9c5c9 2903
52ce6436 2904 read_memory (to_addr, buffer, len);
f49d5fa2 2905 from_size = fromval->bitsize ();
aced2898 2906 if (from_size == 0)
d0c97917 2907 from_size = fromval->type ()->length () * TARGET_CHAR_BIT;
d48e62f4 2908
d5a22e77 2909 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4 2910 ULONGEST from_offset = 0;
d0c97917 2911 if (is_big_endian && is_scalar_type (fromval->type ()))
d48e62f4 2912 from_offset = from_size - bits;
5011c493 2913 copy_bitwise (buffer, toval->bitpos (),
efaf1ae0 2914 fromval->contents ().data (), from_offset,
d48e62f4 2915 bits, is_big_endian);
972daa01 2916 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2917
cda03344 2918 val = toval->copy ();
bbe912ba 2919 memcpy (val->contents_raw ().data (),
efaf1ae0 2920 fromval->contents ().data (),
df86565b 2921 type->length ());
81ae560c 2922 val->deprecated_set_type (type);
d2e4a39e 2923
14f9c5c9
AS
2924 return val;
2925 }
2926
2927 return value_assign (toval, fromval);
2928}
2929
2930
7c512744
JB
2931/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2932 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2933 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2934 COMPONENT, and not the inferior's memory. The current contents
2935 of COMPONENT are ignored.
2936
2937 Although not part of the initial design, this function also works
2938 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2939 had a null address, and COMPONENT had an address which is equal to
2940 its offset inside CONTAINER. */
2941
52ce6436
PH
2942static void
2943value_assign_to_component (struct value *container, struct value *component,
2944 struct value *val)
2945{
2946 LONGEST offset_in_container =
9feb2d07 2947 (LONGEST) (component->address () - container->address ());
7c512744 2948 int bit_offset_in_container =
5011c493 2949 component->bitpos () - container->bitpos ();
52ce6436 2950 int bits;
7c512744 2951
d0c97917 2952 val = value_cast (component->type (), val);
52ce6436 2953
f49d5fa2 2954 if (component->bitsize () == 0)
d0c97917 2955 bits = TARGET_CHAR_BIT * component->type ()->length ();
52ce6436 2956 else
f49d5fa2 2957 bits = component->bitsize ();
52ce6436 2958
d0c97917 2959 if (type_byte_order (container->type ()) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2960 {
2961 int src_offset;
2962
d0c97917 2963 if (is_scalar_type (check_typedef (component->type ())))
dda83cd7 2964 src_offset
d0c97917 2965 = component->type ()->length () * TARGET_CHAR_BIT - bits;
2a62dfa9
JB
2966 else
2967 src_offset = 0;
bbe912ba 2968 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2969 + offset_in_container),
5011c493 2970 container->bitpos () + bit_offset_in_container,
efaf1ae0 2971 val->contents ().data (), src_offset, bits, 1);
2a62dfa9 2972 }
52ce6436 2973 else
bbe912ba 2974 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2975 + offset_in_container),
5011c493 2976 container->bitpos () + bit_offset_in_container,
efaf1ae0 2977 val->contents ().data (), 0, bits, 0);
7c512744
JB
2978}
2979
736ade86
XR
2980/* Determine if TYPE is an access to an unconstrained array. */
2981
d91e9ea8 2982bool
736ade86
XR
2983ada_is_access_to_unconstrained_array (struct type *type)
2984{
78134374 2985 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2986 && is_thick_pntr (ada_typedef_target_type (type)));
2987}
2988
4c4b4cd2
PH
2989/* The value of the element of array ARR at the ARITY indices given in IND.
2990 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2991 thereto. */
2992
d2e4a39e
AS
2993struct value *
2994ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2995{
2996 int k;
d2e4a39e
AS
2997 struct value *elt;
2998 struct type *elt_type;
14f9c5c9
AS
2999
3000 elt = ada_coerce_to_simple_array (arr);
3001
d0c97917 3002 elt_type = ada_check_typedef (elt->type ());
78134374 3003 if (elt_type->code () == TYPE_CODE_ARRAY
3757d2d4 3004 && elt_type->field (0).bitsize () > 0)
14f9c5c9
AS
3005 return value_subscript_packed (elt, arity, ind);
3006
3007 for (k = 0; k < arity; k += 1)
3008 {
27710edb 3009 struct type *saved_elt_type = elt_type->target_type ();
b9c50e9a 3010
78134374 3011 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3012 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3013
2497b498 3014 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3015
3016 if (ada_is_access_to_unconstrained_array (saved_elt_type)
d0c97917 3017 && elt->type ()->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3018 {
3019 /* The element is a typedef to an unconstrained array,
3020 except that the value_subscript call stripped the
3021 typedef layer. The typedef layer is GNAT's way to
3022 specify that the element is, at the source level, an
3023 access to the unconstrained array, rather than the
3024 unconstrained array. So, we need to restore that
3025 typedef layer, which we can do by forcing the element's
3026 type back to its original type. Otherwise, the returned
3027 value is going to be printed as the array, rather
3028 than as an access. Another symptom of the same issue
3029 would be that an expression trying to dereference the
3030 element would also be improperly rejected. */
81ae560c 3031 elt->deprecated_set_type (saved_elt_type);
b9c50e9a
XR
3032 }
3033
d0c97917 3034 elt_type = ada_check_typedef (elt->type ());
14f9c5c9 3035 }
b9c50e9a 3036
14f9c5c9
AS
3037 return elt;
3038}
3039
deede10c
JB
3040/* Assuming ARR is a pointer to a GDB array, the value of the element
3041 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3042 Does not read the entire array into memory.
3043
3044 Note: Unlike what one would expect, this function is used instead of
3045 ada_value_subscript for basically all non-packed array types. The reason
3046 for this is that a side effect of doing our own pointer arithmetics instead
3047 of relying on value_subscript is that there is no implicit typedef peeling.
3048 This is important for arrays of array accesses, where it allows us to
3049 preserve the fact that the array's element is an array access, where the
3050 access part os encoded in a typedef layer. */
14f9c5c9 3051
2c0b251b 3052static struct value *
deede10c 3053ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3054{
3055 int k;
919e6dbe 3056 struct value *array_ind = ada_value_ind (arr);
deede10c 3057 struct type *type
463b870d 3058 = check_typedef (array_ind->enclosing_type ());
919e6dbe 3059
78134374 3060 if (type->code () == TYPE_CODE_ARRAY
3757d2d4 3061 && type->field (0).bitsize () > 0)
919e6dbe 3062 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3063
3064 for (k = 0; k < arity; k += 1)
3065 {
3066 LONGEST lwb, upb;
14f9c5c9 3067
78134374 3068 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3069 error (_("too many subscripts (%d expected)"), k);
27710edb 3070 arr = value_cast (lookup_pointer_type (type->target_type ()),
cda03344 3071 arr->copy ());
3d967001 3072 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3073 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
27710edb 3074 type = type->target_type ();
14f9c5c9
AS
3075 }
3076
3077 return value_ind (arr);
3078}
3079
0b5d8877 3080/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3081 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3082 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3083 this array is LOW, as per Ada rules. */
0b5d8877 3084static struct value *
f5938064 3085ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3086 int low, int high)
0b5d8877 3087{
b0dd7688 3088 struct type *type0 = ada_check_typedef (type);
27710edb 3089 struct type *base_index_type = type0->index_type ()->target_type ();
e727c536 3090 type_allocator alloc (base_index_type);
0c9c3474 3091 struct type *index_type
e727c536 3092 = create_static_range_type (alloc, base_index_type, low, high);
9fe561ab 3093 struct type *slice_type = create_array_type_with_stride
9e76b17a 3094 (alloc, type0->target_type (), index_type,
24e99c6c 3095 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
3757d2d4 3096 type0->field (0).bitsize ());
3d967001 3097 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6b09f134 3098 std::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3099 CORE_ADDR base;
3100
6244c119
SM
3101 low_pos = discrete_position (base_index_type, low);
3102 base_low_pos = discrete_position (base_index_type, base_low);
3103
3104 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3105 {
3106 warning (_("unable to get positions in slice, use bounds instead"));
3107 low_pos = low;
3108 base_low_pos = base_low;
3109 }
5b4ee69b 3110
3757d2d4 3111 ULONGEST stride = slice_type->field (0).bitsize () / 8;
7ff5b937 3112 if (stride == 0)
df86565b 3113 stride = type0->target_type ()->length ();
7ff5b937 3114
6244c119 3115 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3116 return value_at_lazy (slice_type, base);
0b5d8877
PH
3117}
3118
3119
3120static struct value *
3121ada_value_slice (struct value *array, int low, int high)
3122{
d0c97917 3123 struct type *type = ada_check_typedef (array->type ());
27710edb 3124 struct type *base_index_type = type->index_type ()->target_type ();
e727c536 3125 type_allocator alloc (type->index_type ());
0c9c3474 3126 struct type *index_type
e727c536 3127 = create_static_range_type (alloc, type->index_type (), low, high);
9fe561ab 3128 struct type *slice_type = create_array_type_with_stride
9e76b17a 3129 (alloc, type->target_type (), index_type,
24e99c6c 3130 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
3757d2d4 3131 type->field (0).bitsize ());
6b09f134 3132 std::optional<LONGEST> low_pos, high_pos;
6244c119 3133
5b4ee69b 3134
6244c119
SM
3135 low_pos = discrete_position (base_index_type, low);
3136 high_pos = discrete_position (base_index_type, high);
3137
3138 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3139 {
3140 warning (_("unable to get positions in slice, use bounds instead"));
3141 low_pos = low;
3142 high_pos = high;
3143 }
3144
3145 return value_cast (slice_type,
6244c119 3146 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3147}
3148
14f9c5c9
AS
3149/* If type is a record type in the form of a standard GNAT array
3150 descriptor, returns the number of dimensions for type. If arr is a
3151 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3152 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3153
3154int
d2e4a39e 3155ada_array_arity (struct type *type)
14f9c5c9
AS
3156{
3157 int arity;
3158
3159 if (type == NULL)
3160 return 0;
3161
3162 type = desc_base_type (type);
3163
3164 arity = 0;
78134374 3165 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3166 return desc_arity (desc_bounds_type (type));
d2e4a39e 3167 else
78134374 3168 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3169 {
dda83cd7 3170 arity += 1;
27710edb 3171 type = ada_check_typedef (type->target_type ());
14f9c5c9 3172 }
d2e4a39e 3173
14f9c5c9
AS
3174 return arity;
3175}
3176
3177/* If TYPE is a record type in the form of a standard GNAT array
3178 descriptor or a simple array type, returns the element type for
3179 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3180 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3181
d2e4a39e
AS
3182struct type *
3183ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3184{
3185 type = desc_base_type (type);
3186
78134374 3187 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3188 {
3189 int k;
d2e4a39e 3190 struct type *p_array_type;
14f9c5c9 3191
556bdfd4 3192 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3193
3194 k = ada_array_arity (type);
3195 if (k == 0)
dda83cd7 3196 return NULL;
d2e4a39e 3197
4c4b4cd2 3198 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3199 if (nindices >= 0 && k > nindices)
dda83cd7 3200 k = nindices;
d2e4a39e 3201 while (k > 0 && p_array_type != NULL)
dda83cd7 3202 {
27710edb 3203 p_array_type = ada_check_typedef (p_array_type->target_type ());
dda83cd7
SM
3204 k -= 1;
3205 }
14f9c5c9
AS
3206 return p_array_type;
3207 }
78134374 3208 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3209 {
78134374 3210 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7 3211 {
27710edb 3212 type = type->target_type ();
6a40c6e4
TT
3213 /* A multi-dimensional array is represented using a sequence
3214 of array types. If one of these types has a name, then
3215 it is not another dimension of the outer array, but
3216 rather the element type of the outermost array. */
3217 if (type->name () != nullptr)
3218 break;
dda83cd7
SM
3219 nindices -= 1;
3220 }
14f9c5c9
AS
3221 return type;
3222 }
3223
3224 return NULL;
3225}
3226
08a057e6 3227/* See ada-lang.h. */
14f9c5c9 3228
08a057e6 3229struct type *
1eea4ebd 3230ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3231{
4c4b4cd2
PH
3232 struct type *result_type;
3233
14f9c5c9
AS
3234 type = desc_base_type (type);
3235
1eea4ebd
UW
3236 if (n < 0 || n > ada_array_arity (type))
3237 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3238
4c4b4cd2 3239 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3240 {
3241 int i;
3242
3243 for (i = 1; i < n; i += 1)
2869ac4b
TT
3244 {
3245 type = ada_check_typedef (type);
27710edb 3246 type = type->target_type ();
2869ac4b 3247 }
27710edb 3248 result_type = ada_check_typedef (type)->index_type ()->target_type ();
4c4b4cd2 3249 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3250 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3251 perhaps stabsread.c would make more sense. */
78134374 3252 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3253 result_type = NULL;
14f9c5c9 3254 }
d2e4a39e 3255 else
1eea4ebd
UW
3256 {
3257 result_type = desc_index_type (desc_bounds_type (type), n);
3258 if (result_type == NULL)
3259 error (_("attempt to take bound of something that is not an array"));
3260 }
3261
3262 return result_type;
14f9c5c9
AS
3263}
3264
3265/* Given that arr is an array type, returns the lower bound of the
3266 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3267 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3268 array-descriptor type. It works for other arrays with bounds supplied
3269 by run-time quantities other than discriminants. */
14f9c5c9 3270
abb68b3e 3271static LONGEST
fb5e3d5c 3272ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3273{
8a48ac95 3274 struct type *type, *index_type_desc, *index_type;
1ce677a4 3275 int i;
262452ec
JK
3276
3277 gdb_assert (which == 0 || which == 1);
14f9c5c9 3278
ad82864c
JB
3279 if (ada_is_constrained_packed_array_type (arr_type))
3280 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3281
4c4b4cd2 3282 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
66cf9350 3283 return - which;
14f9c5c9 3284
78134374 3285 if (arr_type->code () == TYPE_CODE_PTR)
27710edb 3286 type = arr_type->target_type ();
14f9c5c9
AS
3287 else
3288 type = arr_type;
3289
22c4c60c 3290 if (type->is_fixed_instance ())
bafffb51
JB
3291 {
3292 /* The array has already been fixed, so we do not need to
3293 check the parallel ___XA type again. That encoding has
3294 already been applied, so ignore it now. */
3295 index_type_desc = NULL;
3296 }
3297 else
3298 {
3299 index_type_desc = ada_find_parallel_type (type, "___XA");
3300 ada_fixup_array_indexes_type (index_type_desc);
3301 }
3302
262452ec 3303 if (index_type_desc != NULL)
940da03e 3304 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3305 NULL);
262452ec 3306 else
8a48ac95
JB
3307 {
3308 struct type *elt_type = check_typedef (type);
3309
3310 for (i = 1; i < n; i++)
27710edb 3311 elt_type = check_typedef (elt_type->target_type ());
8a48ac95 3312
3d967001 3313 index_type = elt_type->index_type ();
8a48ac95 3314 }
262452ec 3315
66cf9350
TT
3316 return (which == 0
3317 ? ada_discrete_type_low_bound (index_type)
3318 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3319}
3320
3321/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3322 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3323 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3324 supplied by run-time quantities other than discriminants. */
14f9c5c9 3325
1eea4ebd 3326static LONGEST
4dc81987 3327ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3328{
eb479039
JB
3329 struct type *arr_type;
3330
d0c97917 3331 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3332 arr = value_ind (arr);
463b870d 3333 arr_type = arr->enclosing_type ();
14f9c5c9 3334
ad82864c
JB
3335 if (ada_is_constrained_packed_array_type (arr_type))
3336 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3337 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3338 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3339 else
1eea4ebd 3340 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3341}
3342
3343/* Given that arr is an array value, returns the length of the
3344 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3345 supplied by run-time quantities other than discriminants.
3346 Does not work for arrays indexed by enumeration types with representation
3347 clauses at the moment. */
14f9c5c9 3348
1eea4ebd 3349static LONGEST
d2e4a39e 3350ada_array_length (struct value *arr, int n)
14f9c5c9 3351{
aa715135
JG
3352 struct type *arr_type, *index_type;
3353 int low, high;
eb479039 3354
d0c97917 3355 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3356 arr = value_ind (arr);
463b870d 3357 arr_type = arr->enclosing_type ();
14f9c5c9 3358
ad82864c
JB
3359 if (ada_is_constrained_packed_array_type (arr_type))
3360 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3361
4c4b4cd2 3362 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3363 {
3364 low = ada_array_bound_from_type (arr_type, n, 0);
3365 high = ada_array_bound_from_type (arr_type, n, 1);
3366 }
14f9c5c9 3367 else
aa715135
JG
3368 {
3369 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3370 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3371 }
3372
f168693b 3373 arr_type = check_typedef (arr_type);
7150d33c 3374 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3375 if (index_type != NULL)
3376 {
3377 struct type *base_type;
78134374 3378 if (index_type->code () == TYPE_CODE_RANGE)
27710edb 3379 base_type = index_type->target_type ();
aa715135
JG
3380 else
3381 base_type = index_type;
3382
3383 low = pos_atr (value_from_longest (base_type, low));
3384 high = pos_atr (value_from_longest (base_type, high));
3385 }
3386 return high - low + 1;
4c4b4cd2
PH
3387}
3388
bff8c71f
TT
3389/* An array whose type is that of ARR_TYPE (an array type), with
3390 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3391 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3392
3393static struct value *
bff8c71f 3394empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3395{
b0dd7688 3396 struct type *arr_type0 = ada_check_typedef (arr_type);
e727c536 3397 type_allocator alloc (arr_type0->index_type ()->target_type ());
0c9c3474
SA
3398 struct type *index_type
3399 = create_static_range_type
e727c536 3400 (alloc, arr_type0->index_type ()->target_type (), low,
bff8c71f 3401 high < low ? low - 1 : high);
b0dd7688 3402 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3403
9e76b17a 3404 return value::allocate (create_array_type (alloc, elt_type, index_type));
14f9c5c9 3405}
14f9c5c9 3406\f
d2e4a39e 3407
dda83cd7 3408 /* Name resolution */
14f9c5c9 3409
4c4b4cd2
PH
3410/* The "decoded" name for the user-definable Ada operator corresponding
3411 to OP. */
14f9c5c9 3412
d2e4a39e 3413static const char *
4c4b4cd2 3414ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3415{
3416 int i;
3417
4c4b4cd2 3418 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3419 {
3420 if (ada_opname_table[i].op == op)
dda83cd7 3421 return ada_opname_table[i].decoded;
14f9c5c9 3422 }
323e0a4a 3423 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3424}
3425
de93309a
SM
3426/* Returns true (non-zero) iff decoded name N0 should appear before N1
3427 in a listing of choices during disambiguation (see sort_choices, below).
3428 The idea is that overloadings of a subprogram name from the
3429 same package should sort in their source order. We settle for ordering
3430 such symbols by their trailing number (__N or $N). */
14f9c5c9 3431
de93309a
SM
3432static int
3433encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3434{
de93309a
SM
3435 if (N1 == NULL)
3436 return 0;
3437 else if (N0 == NULL)
3438 return 1;
3439 else
3440 {
3441 int k0, k1;
30b15541 3442
de93309a 3443 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3444 ;
de93309a 3445 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3446 ;
de93309a 3447 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3448 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3449 {
3450 int n0, n1;
3451
3452 n0 = k0;
3453 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3454 n0 -= 1;
3455 n1 = k1;
3456 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3457 n1 -= 1;
3458 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3459 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3460 }
de93309a
SM
3461 return (strcmp (N0, N1) < 0);
3462 }
14f9c5c9
AS
3463}
3464
de93309a
SM
3465/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3466 encoded names. */
14f9c5c9 3467
de93309a
SM
3468static void
3469sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3470{
14f9c5c9 3471 int i;
14f9c5c9 3472
de93309a 3473 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3474 {
de93309a
SM
3475 struct block_symbol sym = syms[i];
3476 int j;
3477
3478 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3479 {
3480 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3481 sym.symbol->linkage_name ()))
3482 break;
3483 syms[j + 1] = syms[j];
3484 }
de93309a
SM
3485 syms[j + 1] = sym;
3486 }
3487}
14f9c5c9 3488
de93309a
SM
3489/* Whether GDB should display formals and return types for functions in the
3490 overloads selection menu. */
3491static bool print_signatures = true;
4c4b4cd2 3492
de93309a
SM
3493/* Print the signature for SYM on STREAM according to the FLAGS options. For
3494 all but functions, the signature is just the name of the symbol. For
3495 functions, this is the name of the function, the list of types for formals
3496 and the return type (if any). */
4c4b4cd2 3497
de93309a
SM
3498static void
3499ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3500 const struct type_print_options *flags)
3501{
5f9c5a63 3502 struct type *type = sym->type ();
14f9c5c9 3503
6cb06a8c 3504 gdb_printf (stream, "%s", sym->print_name ());
de93309a
SM
3505 if (!print_signatures
3506 || type == NULL
78134374 3507 || type->code () != TYPE_CODE_FUNC)
de93309a 3508 return;
4c4b4cd2 3509
1f704f76 3510 if (type->num_fields () > 0)
de93309a
SM
3511 {
3512 int i;
14f9c5c9 3513
6cb06a8c 3514 gdb_printf (stream, " (");
1f704f76 3515 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3516 {
3517 if (i > 0)
6cb06a8c 3518 gdb_printf (stream, "; ");
940da03e 3519 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3520 flags);
3521 }
6cb06a8c 3522 gdb_printf (stream, ")");
de93309a 3523 }
27710edb
SM
3524 if (type->target_type () != NULL
3525 && type->target_type ()->code () != TYPE_CODE_VOID)
de93309a 3526 {
6cb06a8c 3527 gdb_printf (stream, " return ");
27710edb 3528 ada_print_type (type->target_type (), NULL, stream, -1, 0, flags);
de93309a
SM
3529 }
3530}
14f9c5c9 3531
de93309a
SM
3532/* Read and validate a set of numeric choices from the user in the
3533 range 0 .. N_CHOICES-1. Place the results in increasing
3534 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3535
de93309a
SM
3536 The user types choices as a sequence of numbers on one line
3537 separated by blanks, encoding them as follows:
14f9c5c9 3538
de93309a
SM
3539 + A choice of 0 means to cancel the selection, throwing an error.
3540 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3541 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3542
de93309a 3543 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3544
de93309a
SM
3545 ANNOTATION_SUFFIX, if present, is used to annotate the input
3546 prompts (for use with the -f switch). */
14f9c5c9 3547
de93309a
SM
3548static int
3549get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3550 int is_all_choice, const char *annotation_suffix)
de93309a 3551{
992a7040 3552 const char *args;
de93309a
SM
3553 const char *prompt;
3554 int n_chosen;
3555 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3556
de93309a
SM
3557 prompt = getenv ("PS2");
3558 if (prompt == NULL)
3559 prompt = "> ";
4c4b4cd2 3560
f8631e5e
SM
3561 std::string buffer;
3562 args = command_line_input (buffer, prompt, annotation_suffix);
4c4b4cd2 3563
de93309a
SM
3564 if (args == NULL)
3565 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3566
de93309a 3567 n_chosen = 0;
4c4b4cd2 3568
de93309a
SM
3569 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3570 order, as given in args. Choices are validated. */
3571 while (1)
14f9c5c9 3572 {
de93309a
SM
3573 char *args2;
3574 int choice, j;
76a01679 3575
de93309a
SM
3576 args = skip_spaces (args);
3577 if (*args == '\0' && n_chosen == 0)
dda83cd7 3578 error_no_arg (_("one or more choice numbers"));
de93309a 3579 else if (*args == '\0')
dda83cd7 3580 break;
76a01679 3581
de93309a
SM
3582 choice = strtol (args, &args2, 10);
3583 if (args == args2 || choice < 0
dda83cd7
SM
3584 || choice > n_choices + first_choice - 1)
3585 error (_("Argument must be choice number"));
de93309a 3586 args = args2;
76a01679 3587
de93309a 3588 if (choice == 0)
dda83cd7 3589 error (_("cancelled"));
76a01679 3590
de93309a 3591 if (choice < first_choice)
dda83cd7
SM
3592 {
3593 n_chosen = n_choices;
3594 for (j = 0; j < n_choices; j += 1)
3595 choices[j] = j;
3596 break;
3597 }
de93309a 3598 choice -= first_choice;
76a01679 3599
de93309a 3600 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3601 {
3602 }
4c4b4cd2 3603
de93309a 3604 if (j < 0 || choice != choices[j])
dda83cd7
SM
3605 {
3606 int k;
4c4b4cd2 3607
dda83cd7
SM
3608 for (k = n_chosen - 1; k > j; k -= 1)
3609 choices[k + 1] = choices[k];
3610 choices[j + 1] = choice;
3611 n_chosen += 1;
3612 }
14f9c5c9
AS
3613 }
3614
de93309a
SM
3615 if (n_chosen > max_results)
3616 error (_("Select no more than %d of the above"), max_results);
3617
3618 return n_chosen;
14f9c5c9
AS
3619}
3620
de93309a
SM
3621/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3622 by asking the user (if necessary), returning the number selected,
3623 and setting the first elements of SYMS items. Error if no symbols
3624 selected. */
3625
3626/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3627 to be re-integrated one of these days. */
14f9c5c9
AS
3628
3629static int
de93309a 3630user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3631{
de93309a
SM
3632 int i;
3633 int *chosen = XALLOCAVEC (int , nsyms);
3634 int n_chosen;
3635 int first_choice = (max_results == 1) ? 1 : 2;
3636 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3637
de93309a
SM
3638 if (max_results < 1)
3639 error (_("Request to select 0 symbols!"));
3640 if (nsyms <= 1)
3641 return nsyms;
14f9c5c9 3642
de93309a
SM
3643 if (select_mode == multiple_symbols_cancel)
3644 error (_("\
3645canceled because the command is ambiguous\n\
3646See set/show multiple-symbol."));
14f9c5c9 3647
de93309a
SM
3648 /* If select_mode is "all", then return all possible symbols.
3649 Only do that if more than one symbol can be selected, of course.
3650 Otherwise, display the menu as usual. */
3651 if (select_mode == multiple_symbols_all && max_results > 1)
3652 return nsyms;
14f9c5c9 3653
6cb06a8c 3654 gdb_printf (_("[0] cancel\n"));
de93309a 3655 if (max_results > 1)
6cb06a8c 3656 gdb_printf (_("[1] all\n"));
14f9c5c9 3657
de93309a 3658 sort_choices (syms, nsyms);
14f9c5c9 3659
de93309a
SM
3660 for (i = 0; i < nsyms; i += 1)
3661 {
3662 if (syms[i].symbol == NULL)
dda83cd7 3663 continue;
14f9c5c9 3664
66d7f48f 3665 if (syms[i].symbol->aclass () == LOC_BLOCK)
dda83cd7
SM
3666 {
3667 struct symtab_and_line sal =
3668 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3669
6cb06a8c 3670 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3671 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3672 &type_print_raw_options);
3673 if (sal.symtab == NULL)
6cb06a8c
TT
3674 gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
3675 metadata_style.style ().ptr (), nullptr, sal.line);
de93309a 3676 else
6cb06a8c 3677 gdb_printf
de93309a
SM
3678 (_(" at %ps:%d\n"),
3679 styled_string (file_name_style.style (),
3680 symtab_to_filename_for_display (sal.symtab)),
3681 sal.line);
dda83cd7
SM
3682 continue;
3683 }
76a01679 3684 else
dda83cd7
SM
3685 {
3686 int is_enumeral =
66d7f48f 3687 (syms[i].symbol->aclass () == LOC_CONST
5f9c5a63
SM
3688 && syms[i].symbol->type () != NULL
3689 && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
de93309a 3690 struct symtab *symtab = NULL;
4c4b4cd2 3691
7b3ecc75 3692 if (syms[i].symbol->is_objfile_owned ())
4206d69e 3693 symtab = syms[i].symbol->symtab ();
de93309a 3694
5d0027b9 3695 if (syms[i].symbol->line () != 0 && symtab != NULL)
de93309a 3696 {
6cb06a8c 3697 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3698 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3699 &type_print_raw_options);
6cb06a8c
TT
3700 gdb_printf (_(" at %s:%d\n"),
3701 symtab_to_filename_for_display (symtab),
3702 syms[i].symbol->line ());
de93309a 3703 }
dda83cd7 3704 else if (is_enumeral
5f9c5a63 3705 && syms[i].symbol->type ()->name () != NULL)
dda83cd7 3706 {
6cb06a8c 3707 gdb_printf (("[%d] "), i + first_choice);
5f9c5a63 3708 ada_print_type (syms[i].symbol->type (), NULL,
dda83cd7 3709 gdb_stdout, -1, 0, &type_print_raw_options);
6cb06a8c
TT
3710 gdb_printf (_("'(%s) (enumeral)\n"),
3711 syms[i].symbol->print_name ());
dda83cd7 3712 }
de93309a
SM
3713 else
3714 {
6cb06a8c 3715 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3716 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3717 &type_print_raw_options);
3718
3719 if (symtab != NULL)
6cb06a8c
TT
3720 gdb_printf (is_enumeral
3721 ? _(" in %s (enumeral)\n")
3722 : _(" at %s:?\n"),
3723 symtab_to_filename_for_display (symtab));
de93309a 3724 else
6cb06a8c
TT
3725 gdb_printf (is_enumeral
3726 ? _(" (enumeral)\n")
3727 : _(" at ?\n"));
de93309a 3728 }
dda83cd7 3729 }
14f9c5c9 3730 }
14f9c5c9 3731
de93309a 3732 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3733 "overload-choice");
14f9c5c9 3734
de93309a
SM
3735 for (i = 0; i < n_chosen; i += 1)
3736 syms[i] = syms[chosen[i]];
14f9c5c9 3737
de93309a
SM
3738 return n_chosen;
3739}
14f9c5c9 3740
cd9a3148
TT
3741/* See ada-lang.h. */
3742
3743block_symbol
7056f312 3744ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3745 int nargs, value *argvec[])
3746{
3747 if (possible_user_operator_p (op, argvec))
3748 {
3749 std::vector<struct block_symbol> candidates
3750 = ada_lookup_symbol_list (ada_decoded_op_name (op),
6c015214 3751 NULL, SEARCH_VFT);
cd9a3148
TT
3752
3753 int i = ada_resolve_function (candidates, argvec,
3754 nargs, ada_decoded_op_name (op), NULL,
3755 parse_completion);
3756 if (i >= 0)
3757 return candidates[i];
3758 }
3759 return {};
3760}
3761
3762/* See ada-lang.h. */
3763
3764block_symbol
3765ada_resolve_funcall (struct symbol *sym, const struct block *block,
3766 struct type *context_type,
7056f312 3767 bool parse_completion,
cd9a3148
TT
3768 int nargs, value *argvec[],
3769 innermost_block_tracker *tracker)
3770{
3771 std::vector<struct block_symbol> candidates
6c015214 3772 = ada_lookup_symbol_list (sym->linkage_name (), block, SEARCH_VFT);
cd9a3148
TT
3773
3774 int i;
3775 if (candidates.size () == 1)
3776 i = 0;
3777 else
3778 {
3779 i = ada_resolve_function
3780 (candidates,
3781 argvec, nargs,
3782 sym->linkage_name (),
3783 context_type, parse_completion);
3784 if (i < 0)
3785 error (_("Could not find a match for %s"), sym->print_name ());
3786 }
3787
3788 tracker->update (candidates[i]);
3789 return candidates[i];
3790}
3791
ba8694b6
TT
3792/* Resolve a mention of a name where the context type is an
3793 enumeration type. */
3794
3795static int
3796ada_resolve_enum (std::vector<struct block_symbol> &syms,
3797 const char *name, struct type *context_type,
3798 bool parse_completion)
3799{
3800 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3801 context_type = ada_check_typedef (context_type);
3802
74c36641
TV
3803 /* We already know the name matches, so we're just looking for
3804 an element of the correct enum type. */
3805 struct type *type1 = context_type;
3806 for (int i = 0; i < syms.size (); ++i)
3807 {
3808 struct type *type2 = ada_check_typedef (syms[i].symbol->type ());
3809 if (type1 == type2)
3810 return i;
3811 }
3812
ba8694b6
TT
3813 for (int i = 0; i < syms.size (); ++i)
3814 {
74c36641
TV
3815 struct type *type2 = ada_check_typedef (syms[i].symbol->type ());
3816 if (type1->num_fields () != type2->num_fields ())
3817 continue;
3818 if (strcmp (type1->name (), type2->name ()) != 0)
3819 continue;
3820 if (ada_identical_enum_types_p (type1, type2))
ba8694b6
TT
3821 return i;
3822 }
3823
3824 error (_("No name '%s' in enumeration type '%s'"), name,
3825 ada_type_name (context_type));
3826}
3827
cd9a3148
TT
3828/* See ada-lang.h. */
3829
3830block_symbol
3831ada_resolve_variable (struct symbol *sym, const struct block *block,
3832 struct type *context_type,
7056f312 3833 bool parse_completion,
cd9a3148
TT
3834 int deprocedure_p,
3835 innermost_block_tracker *tracker)
3836{
3837 std::vector<struct block_symbol> candidates
6c015214 3838 = ada_lookup_symbol_list (sym->linkage_name (), block, SEARCH_VFT);
cd9a3148
TT
3839
3840 if (std::any_of (candidates.begin (),
3841 candidates.end (),
3842 [] (block_symbol &bsym)
3843 {
66d7f48f 3844 switch (bsym.symbol->aclass ())
cd9a3148
TT
3845 {
3846 case LOC_REGISTER:
3847 case LOC_ARG:
3848 case LOC_REF_ARG:
3849 case LOC_REGPARM_ADDR:
3850 case LOC_LOCAL:
3851 case LOC_COMPUTED:
3852 return true;
3853 default:
3854 return false;
3855 }
3856 }))
3857 {
3858 /* Types tend to get re-introduced locally, so if there
3859 are any local symbols that are not types, first filter
3860 out all types. */
3861 candidates.erase
3862 (std::remove_if
3863 (candidates.begin (),
3864 candidates.end (),
3865 [] (block_symbol &bsym)
3866 {
66d7f48f 3867 return bsym.symbol->aclass () == LOC_TYPEDEF;
cd9a3148
TT
3868 }),
3869 candidates.end ());
3870 }
3871
2c71f639
TV
3872 /* Filter out artificial symbols. */
3873 candidates.erase
3874 (std::remove_if
3875 (candidates.begin (),
3876 candidates.end (),
3877 [] (block_symbol &bsym)
3878 {
496feb16 3879 return bsym.symbol->is_artificial ();
2c71f639
TV
3880 }),
3881 candidates.end ());
3882
cd9a3148
TT
3883 int i;
3884 if (candidates.empty ())
3885 error (_("No definition found for %s"), sym->print_name ());
3886 else if (candidates.size () == 1)
3887 i = 0;
ba8694b6
TT
3888 else if (context_type != nullptr
3889 && context_type->code () == TYPE_CODE_ENUM)
3890 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3891 parse_completion);
ef136c7f
TV
3892 else if (context_type == nullptr
3893 && symbols_are_identical_enums (candidates))
3894 {
3895 /* If all the remaining symbols are identical enumerals, then
3896 just keep the first one and discard the rest.
3897
3898 Unlike what we did previously, we do not discard any entry
3899 unless they are ALL identical. This is because the symbol
3900 comparison is not a strict comparison, but rather a practical
3901 comparison. If all symbols are considered identical, then
3902 we can just go ahead and use the first one and discard the rest.
3903 But if we cannot reduce the list to a single element, we have
3904 to ask the user to disambiguate anyways. And if we have to
3905 present a multiple-choice menu, it's less confusing if the list
3906 isn't missing some choices that were identical and yet distinct. */
3907 candidates.resize (1);
3908 i = 0;
3909 }
cd9a3148
TT
3910 else if (deprocedure_p && !is_nonfunction (candidates))
3911 {
3912 i = ada_resolve_function
3913 (candidates, NULL, 0,
3914 sym->linkage_name (),
3915 context_type, parse_completion);
3916 if (i < 0)
3917 error (_("Could not find a match for %s"), sym->print_name ());
3918 }
3919 else
3920 {
6cb06a8c 3921 gdb_printf (_("Multiple matches for %s\n"), sym->print_name ());
cd9a3148
TT
3922 user_select_syms (candidates.data (), candidates.size (), 1);
3923 i = 0;
3924 }
3925
3926 tracker->update (candidates[i]);
3927 return candidates[i];
3928}
3929
d56fdf1b
TT
3930static bool ada_type_match (struct type *ftype, struct type *atype);
3931
3932/* Helper for ada_type_match that checks that two array types are
3933 compatible. As with that function, FTYPE is the formal type and
3934 ATYPE is the actual type. */
3935
3936static bool
3937ada_type_match_arrays (struct type *ftype, struct type *atype)
3938{
3939 if (ftype->code () != TYPE_CODE_ARRAY
3940 && !ada_is_array_descriptor_type (ftype))
3941 return false;
3942 if (atype->code () != TYPE_CODE_ARRAY
3943 && !ada_is_array_descriptor_type (atype))
3944 return false;
3945
3946 if (ada_array_arity (ftype) != ada_array_arity (atype))
3947 return false;
3948
3949 struct type *f_elt_type = ada_array_element_type (ftype, -1);
3950 struct type *a_elt_type = ada_array_element_type (atype, -1);
3951 return ada_type_match (f_elt_type, a_elt_type);
3952}
3953
3954/* Return non-zero if formal type FTYPE matches actual type ATYPE.
3955 The term "match" here is rather loose. The match is heuristic and
3956 liberal -- while it tries to reject matches that are obviously
3957 incorrect, it may still let through some that do not strictly
3958 correspond to Ada rules. */
14f9c5c9 3959
1414fbf9 3960static bool
db2534b7 3961ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3962{
de93309a
SM
3963 ftype = ada_check_typedef (ftype);
3964 atype = ada_check_typedef (atype);
14f9c5c9 3965
78134374 3966 if (ftype->code () == TYPE_CODE_REF)
27710edb 3967 ftype = ftype->target_type ();
78134374 3968 if (atype->code () == TYPE_CODE_REF)
27710edb 3969 atype = atype->target_type ();
14f9c5c9 3970
78134374 3971 switch (ftype->code ())
14f9c5c9 3972 {
de93309a 3973 default:
78134374 3974 return ftype->code () == atype->code ();
de93309a 3975 case TYPE_CODE_PTR:
db2534b7 3976 if (atype->code () != TYPE_CODE_PTR)
1414fbf9 3977 return false;
27710edb 3978 atype = atype->target_type ();
db2534b7 3979 /* This can only happen if the actual argument is 'null'. */
df86565b 3980 if (atype->code () == TYPE_CODE_INT && atype->length () == 0)
1414fbf9 3981 return true;
27710edb 3982 return ada_type_match (ftype->target_type (), atype);
de93309a
SM
3983 case TYPE_CODE_INT:
3984 case TYPE_CODE_ENUM:
3985 case TYPE_CODE_RANGE:
78134374 3986 switch (atype->code ())
dda83cd7
SM
3987 {
3988 case TYPE_CODE_INT:
3989 case TYPE_CODE_ENUM:
3990 case TYPE_CODE_RANGE:
1414fbf9 3991 return true;
dda83cd7 3992 default:
1414fbf9 3993 return false;
dda83cd7 3994 }
d2e4a39e 3995
de93309a 3996 case TYPE_CODE_STRUCT:
d56fdf1b 3997 if (!ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3998 return (atype->code () == TYPE_CODE_STRUCT
3999 && !ada_is_array_descriptor_type (atype));
14f9c5c9 4000
d56fdf1b
TT
4001 [[fallthrough]];
4002 case TYPE_CODE_ARRAY:
4003 return ada_type_match_arrays (ftype, atype);
4004
de93309a
SM
4005 case TYPE_CODE_UNION:
4006 case TYPE_CODE_FLT:
78134374 4007 return (atype->code () == ftype->code ());
de93309a 4008 }
14f9c5c9
AS
4009}
4010
de93309a
SM
4011/* Return non-zero if the formals of FUNC "sufficiently match" the
4012 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
4013 may also be an enumeral, in which case it is treated as a 0-
4014 argument function. */
14f9c5c9 4015
de93309a
SM
4016static int
4017ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
4018{
4019 int i;
5f9c5a63 4020 struct type *func_type = func->type ();
14f9c5c9 4021
66d7f48f 4022 if (func->aclass () == LOC_CONST
78134374 4023 && func_type->code () == TYPE_CODE_ENUM)
de93309a 4024 return (n_actuals == 0);
78134374 4025 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 4026 return 0;
14f9c5c9 4027
1f704f76 4028 if (func_type->num_fields () != n_actuals)
de93309a 4029 return 0;
14f9c5c9 4030
de93309a
SM
4031 for (i = 0; i < n_actuals; i += 1)
4032 {
4033 if (actuals[i] == NULL)
dda83cd7 4034 return 0;
de93309a 4035 else
dda83cd7
SM
4036 {
4037 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
d0c97917 4038 struct type *atype = ada_check_typedef (actuals[i]->type ());
14f9c5c9 4039
db2534b7 4040 if (!ada_type_match (ftype, atype))
dda83cd7
SM
4041 return 0;
4042 }
de93309a
SM
4043 }
4044 return 1;
4045}
d2e4a39e 4046
de93309a
SM
4047/* False iff function type FUNC_TYPE definitely does not produce a value
4048 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
4049 FUNC_TYPE is not a valid function type with a non-null return type
4050 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 4051
de93309a
SM
4052static int
4053return_match (struct type *func_type, struct type *context_type)
4054{
4055 struct type *return_type;
d2e4a39e 4056
de93309a
SM
4057 if (func_type == NULL)
4058 return 1;
14f9c5c9 4059
78134374 4060 if (func_type->code () == TYPE_CODE_FUNC)
27710edb 4061 return_type = get_base_type (func_type->target_type ());
de93309a
SM
4062 else
4063 return_type = get_base_type (func_type);
4064 if (return_type == NULL)
4065 return 1;
76a01679 4066
de93309a 4067 context_type = get_base_type (context_type);
14f9c5c9 4068
78134374 4069 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
4070 return context_type == NULL || return_type == context_type;
4071 else if (context_type == NULL)
78134374 4072 return return_type->code () != TYPE_CODE_VOID;
de93309a 4073 else
78134374 4074 return return_type->code () == context_type->code ();
de93309a 4075}
14f9c5c9 4076
14f9c5c9 4077
1bfa81ac 4078/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
4079 function (if any) that matches the types of the NARGS arguments in
4080 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4081 that returns that type, then eliminate matches that don't. If
4082 CONTEXT_TYPE is void and there is at least one match that does not
4083 return void, eliminate all matches that do.
14f9c5c9 4084
de93309a
SM
4085 Asks the user if there is more than one match remaining. Returns -1
4086 if there is no such symbol or none is selected. NAME is used
4087 solely for messages. May re-arrange and modify SYMS in
4088 the process; the index returned is for the modified vector. */
14f9c5c9 4089
de93309a 4090static int
d1183b06
TT
4091ada_resolve_function (std::vector<struct block_symbol> &syms,
4092 struct value **args, int nargs,
dda83cd7 4093 const char *name, struct type *context_type,
7056f312 4094 bool parse_completion)
de93309a
SM
4095{
4096 int fallback;
4097 int k;
4098 int m; /* Number of hits */
14f9c5c9 4099
de93309a
SM
4100 m = 0;
4101 /* In the first pass of the loop, we only accept functions matching
4102 context_type. If none are found, we add a second pass of the loop
4103 where every function is accepted. */
4104 for (fallback = 0; m == 0 && fallback < 2; fallback++)
4105 {
d1183b06 4106 for (k = 0; k < syms.size (); k += 1)
dda83cd7 4107 {
5f9c5a63 4108 struct type *type = ada_check_typedef (syms[k].symbol->type ());
5b4ee69b 4109
dda83cd7
SM
4110 if (ada_args_match (syms[k].symbol, args, nargs)
4111 && (fallback || return_match (type, context_type)))
4112 {
4113 syms[m] = syms[k];
4114 m += 1;
4115 }
4116 }
14f9c5c9
AS
4117 }
4118
de93309a
SM
4119 /* If we got multiple matches, ask the user which one to use. Don't do this
4120 interactive thing during completion, though, as the purpose of the
4121 completion is providing a list of all possible matches. Prompting the
4122 user to filter it down would be completely unexpected in this case. */
4123 if (m == 0)
4124 return -1;
4125 else if (m > 1 && !parse_completion)
4126 {
6cb06a8c 4127 gdb_printf (_("Multiple matches for %s\n"), name);
d1183b06 4128 user_select_syms (syms.data (), m, 1);
de93309a
SM
4129 return 0;
4130 }
4131 return 0;
14f9c5c9
AS
4132}
4133
14f9c5c9
AS
4134/* Type-class predicates */
4135
4c4b4cd2
PH
4136/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4137 or FLOAT). */
14f9c5c9
AS
4138
4139static int
d2e4a39e 4140numeric_type_p (struct type *type)
14f9c5c9
AS
4141{
4142 if (type == NULL)
4143 return 0;
d2e4a39e
AS
4144 else
4145 {
78134374 4146 switch (type->code ())
dda83cd7
SM
4147 {
4148 case TYPE_CODE_INT:
4149 case TYPE_CODE_FLT:
c04da66c 4150 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4151 return 1;
4152 case TYPE_CODE_RANGE:
27710edb
SM
4153 return (type == type->target_type ()
4154 || numeric_type_p (type->target_type ()));
dda83cd7
SM
4155 default:
4156 return 0;
4157 }
d2e4a39e 4158 }
14f9c5c9
AS
4159}
4160
4c4b4cd2 4161/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4162
4163static int
d2e4a39e 4164integer_type_p (struct type *type)
14f9c5c9
AS
4165{
4166 if (type == NULL)
4167 return 0;
d2e4a39e
AS
4168 else
4169 {
78134374 4170 switch (type->code ())
dda83cd7
SM
4171 {
4172 case TYPE_CODE_INT:
4173 return 1;
4174 case TYPE_CODE_RANGE:
27710edb
SM
4175 return (type == type->target_type ()
4176 || integer_type_p (type->target_type ()));
dda83cd7
SM
4177 default:
4178 return 0;
4179 }
d2e4a39e 4180 }
14f9c5c9
AS
4181}
4182
4c4b4cd2 4183/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4184
4185static int
d2e4a39e 4186scalar_type_p (struct type *type)
14f9c5c9
AS
4187{
4188 if (type == NULL)
4189 return 0;
d2e4a39e
AS
4190 else
4191 {
78134374 4192 switch (type->code ())
dda83cd7
SM
4193 {
4194 case TYPE_CODE_INT:
4195 case TYPE_CODE_RANGE:
4196 case TYPE_CODE_ENUM:
4197 case TYPE_CODE_FLT:
c04da66c 4198 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4199 return 1;
4200 default:
4201 return 0;
4202 }
d2e4a39e 4203 }
14f9c5c9
AS
4204}
4205
98847c1e
TT
4206/* True iff TYPE is discrete, as defined in the Ada Reference Manual.
4207 This essentially means one of (INT, RANGE, ENUM) -- but note that
4208 "enum" includes character and boolean as well. */
14f9c5c9
AS
4209
4210static int
d2e4a39e 4211discrete_type_p (struct type *type)
14f9c5c9
AS
4212{
4213 if (type == NULL)
4214 return 0;
d2e4a39e
AS
4215 else
4216 {
78134374 4217 switch (type->code ())
dda83cd7
SM
4218 {
4219 case TYPE_CODE_INT:
4220 case TYPE_CODE_RANGE:
4221 case TYPE_CODE_ENUM:
4222 case TYPE_CODE_BOOL:
98847c1e 4223 case TYPE_CODE_CHAR:
dda83cd7
SM
4224 return 1;
4225 default:
4226 return 0;
4227 }
d2e4a39e 4228 }
14f9c5c9
AS
4229}
4230
4c4b4cd2
PH
4231/* Returns non-zero if OP with operands in the vector ARGS could be
4232 a user-defined function. Errs on the side of pre-defined operators
4233 (i.e., result 0). */
14f9c5c9
AS
4234
4235static int
d2e4a39e 4236possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4237{
76a01679 4238 struct type *type0 =
d0c97917 4239 (args[0] == NULL) ? NULL : ada_check_typedef (args[0]->type ());
d2e4a39e 4240 struct type *type1 =
d0c97917 4241 (args[1] == NULL) ? NULL : ada_check_typedef (args[1]->type ());
d2e4a39e 4242
4c4b4cd2
PH
4243 if (type0 == NULL)
4244 return 0;
4245
14f9c5c9
AS
4246 switch (op)
4247 {
4248 default:
4249 return 0;
4250
4251 case BINOP_ADD:
4252 case BINOP_SUB:
4253 case BINOP_MUL:
4254 case BINOP_DIV:
d2e4a39e 4255 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4256
4257 case BINOP_REM:
4258 case BINOP_MOD:
4259 case BINOP_BITWISE_AND:
4260 case BINOP_BITWISE_IOR:
4261 case BINOP_BITWISE_XOR:
d2e4a39e 4262 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4263
4264 case BINOP_EQUAL:
4265 case BINOP_NOTEQUAL:
4266 case BINOP_LESS:
4267 case BINOP_GTR:
4268 case BINOP_LEQ:
4269 case BINOP_GEQ:
d2e4a39e 4270 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4271
4272 case BINOP_CONCAT:
ee90b9ab 4273 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4274
4275 case BINOP_EXP:
d2e4a39e 4276 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4277
4278 case UNOP_NEG:
4279 case UNOP_PLUS:
4280 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4281 case UNOP_ABS:
4282 return (!numeric_type_p (type0));
14f9c5c9
AS
4283
4284 }
4285}
4286\f
dda83cd7 4287 /* Renaming */
14f9c5c9 4288
aeb5907d
JB
4289/* NOTES:
4290
4291 1. In the following, we assume that a renaming type's name may
4292 have an ___XD suffix. It would be nice if this went away at some
4293 point.
4294 2. We handle both the (old) purely type-based representation of
4295 renamings and the (new) variable-based encoding. At some point,
4296 it is devoutly to be hoped that the former goes away
4297 (FIXME: hilfinger-2007-07-09).
4298 3. Subprogram renamings are not implemented, although the XRS
4299 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4300
4301/* If SYM encodes a renaming,
4302
4303 <renaming> renames <renamed entity>,
4304
4305 sets *LEN to the length of the renamed entity's name,
4306 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4307 the string describing the subcomponent selected from the renamed
0963b4bd 4308 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4309 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4310 are undefined). Otherwise, returns a value indicating the category
4311 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4312 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4313 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4314 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4315 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4316 may be NULL, in which case they are not assigned.
4317
4318 [Currently, however, GCC does not generate subprogram renamings.] */
4319
4320enum ada_renaming_category
4321ada_parse_renaming (struct symbol *sym,
4322 const char **renamed_entity, int *len,
4323 const char **renaming_expr)
4324{
4325 enum ada_renaming_category kind;
4326 const char *info;
4327 const char *suffix;
4328
4329 if (sym == NULL)
4330 return ADA_NOT_RENAMING;
66d7f48f 4331 switch (sym->aclass ())
14f9c5c9 4332 {
aeb5907d
JB
4333 default:
4334 return ADA_NOT_RENAMING;
aeb5907d
JB
4335 case LOC_LOCAL:
4336 case LOC_STATIC:
4337 case LOC_COMPUTED:
4338 case LOC_OPTIMIZED_OUT:
987012b8 4339 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4340 if (info == NULL)
4341 return ADA_NOT_RENAMING;
4342 switch (info[5])
4343 {
4344 case '_':
4345 kind = ADA_OBJECT_RENAMING;
4346 info += 6;
4347 break;
4348 case 'E':
4349 kind = ADA_EXCEPTION_RENAMING;
4350 info += 7;
4351 break;
4352 case 'P':
4353 kind = ADA_PACKAGE_RENAMING;
4354 info += 7;
4355 break;
4356 case 'S':
4357 kind = ADA_SUBPROGRAM_RENAMING;
4358 info += 7;
4359 break;
4360 default:
4361 return ADA_NOT_RENAMING;
4362 }
14f9c5c9 4363 }
4c4b4cd2 4364
de93309a
SM
4365 if (renamed_entity != NULL)
4366 *renamed_entity = info;
4367 suffix = strstr (info, "___XE");
4368 if (suffix == NULL || suffix == info)
4369 return ADA_NOT_RENAMING;
4370 if (len != NULL)
4371 *len = strlen (info) - strlen (suffix);
4372 suffix += 5;
4373 if (renaming_expr != NULL)
4374 *renaming_expr = suffix;
4375 return kind;
4376}
4377
4378/* Compute the value of the given RENAMING_SYM, which is expected to
4379 be a symbol encoding a renaming expression. BLOCK is the block
4380 used to evaluate the renaming. */
4381
4382static struct value *
4383ada_read_renaming_var_value (struct symbol *renaming_sym,
4384 const struct block *block)
4385{
4386 const char *sym_name;
4387
987012b8 4388 sym_name = renaming_sym->linkage_name ();
de93309a 4389 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
43048e46 4390 return expr->evaluate ();
de93309a
SM
4391}
4392\f
4393
dda83cd7 4394 /* Evaluation: Function Calls */
de93309a
SM
4395
4396/* Return an lvalue containing the value VAL. This is the identity on
4397 lvalues, and otherwise has the side-effect of allocating memory
4398 in the inferior where a copy of the value contents is copied. */
4399
4400static struct value *
4401ensure_lval (struct value *val)
4402{
736355f2
TT
4403 if (val->lval () == not_lval
4404 || val->lval () == lval_internalvar)
de93309a 4405 {
d0c97917 4406 int len = ada_check_typedef (val->type ())->length ();
de93309a 4407 const CORE_ADDR addr =
dda83cd7 4408 value_as_long (value_allocate_space_in_inferior (len));
de93309a 4409
6f9c9d71 4410 val->set_lval (lval_memory);
9feb2d07 4411 val->set_address (addr);
efaf1ae0 4412 write_memory (addr, val->contents ().data (), len);
de93309a
SM
4413 }
4414
4415 return val;
4416}
4417
4418/* Given ARG, a value of type (pointer or reference to a)*
4419 structure/union, extract the component named NAME from the ultimate
4420 target structure/union and return it as a value with its
4421 appropriate type.
4422
4423 The routine searches for NAME among all members of the structure itself
4424 and (recursively) among all members of any wrapper members
4425 (e.g., '_parent').
4426
4427 If NO_ERR, then simply return NULL in case of error, rather than
4428 calling error. */
4429
4430static struct value *
4431ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4432{
4433 struct type *t, *t1;
4434 struct value *v;
4435 int check_tag;
4436
4437 v = NULL;
d0c97917 4438 t1 = t = ada_check_typedef (arg->type ());
78134374 4439 if (t->code () == TYPE_CODE_REF)
de93309a 4440 {
27710edb 4441 t1 = t->target_type ();
de93309a
SM
4442 if (t1 == NULL)
4443 goto BadValue;
4444 t1 = ada_check_typedef (t1);
78134374 4445 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4446 {
4447 arg = coerce_ref (arg);
4448 t = t1;
4449 }
de93309a
SM
4450 }
4451
78134374 4452 while (t->code () == TYPE_CODE_PTR)
de93309a 4453 {
27710edb 4454 t1 = t->target_type ();
de93309a
SM
4455 if (t1 == NULL)
4456 goto BadValue;
4457 t1 = ada_check_typedef (t1);
78134374 4458 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4459 {
4460 arg = value_ind (arg);
4461 t = t1;
4462 }
de93309a 4463 else
dda83cd7 4464 break;
de93309a 4465 }
aeb5907d 4466
78134374 4467 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4468 goto BadValue;
52ce6436 4469
de93309a
SM
4470 if (t1 == t)
4471 v = ada_search_struct_field (name, arg, 0, t);
4472 else
4473 {
4474 int bit_offset, bit_size, byte_offset;
4475 struct type *field_type;
4476 CORE_ADDR address;
a5ee536b 4477
78134374 4478 if (t->code () == TYPE_CODE_PTR)
9feb2d07 4479 address = ada_value_ind (arg)->address ();
de93309a 4480 else
9feb2d07 4481 address = ada_coerce_ref (arg)->address ();
d2e4a39e 4482
de93309a 4483 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4484 the case where the type is a reference to a tagged type, but
4485 we have to be careful to exclude pointers to tagged types.
4486 The latter should be shown as usual (as a pointer), whereas
4487 a reference should mostly be transparent to the user. */
14f9c5c9 4488
de93309a 4489 if (ada_is_tagged_type (t1, 0)
dda83cd7 4490 || (t1->code () == TYPE_CODE_REF
27710edb 4491 && ada_is_tagged_type (t1->target_type (), 0)))
dda83cd7
SM
4492 {
4493 /* We first try to find the searched field in the current type.
de93309a 4494 If not found then let's look in the fixed type. */
14f9c5c9 4495
dda83cd7 4496 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4497 nullptr, nullptr, nullptr,
4498 nullptr, nullptr))
de93309a
SM
4499 check_tag = 1;
4500 else
4501 check_tag = 0;
dda83cd7 4502 }
de93309a
SM
4503 else
4504 check_tag = 0;
c3e5cd34 4505
de93309a
SM
4506 /* Convert to fixed type in all cases, so that we have proper
4507 offsets to each field in unconstrained record types. */
4508 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4509 address, NULL, check_tag);
4510
24aa1b02
TT
4511 /* Resolve the dynamic type as well. */
4512 arg = value_from_contents_and_address (t1, nullptr, address);
d0c97917 4513 t1 = arg->type ();
24aa1b02 4514
de93309a 4515 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4516 &field_type, &byte_offset, &bit_offset,
4517 &bit_size, NULL))
4518 {
4519 if (bit_size != 0)
4520 {
4521 if (t->code () == TYPE_CODE_REF)
4522 arg = ada_coerce_ref (arg);
4523 else
4524 arg = ada_value_ind (arg);
4525 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4526 bit_offset, bit_size,
4527 field_type);
4528 }
4529 else
4530 v = value_at_lazy (field_type, address + byte_offset);
4531 }
c3e5cd34 4532 }
14f9c5c9 4533
de93309a
SM
4534 if (v != NULL || no_err)
4535 return v;
4536 else
4537 error (_("There is no member named %s."), name);
4538
4539 BadValue:
4540 if (no_err)
4541 return NULL;
4542 else
4543 error (_("Attempt to extract a component of "
4544 "a value that is not a record."));
14f9c5c9
AS
4545}
4546
4547/* Return the value ACTUAL, converted to be an appropriate value for a
4548 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4549 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4550 values not residing in memory, updating it as needed. */
14f9c5c9 4551
a93c0eb6 4552struct value *
40bc484c 4553ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4554{
d0c97917 4555 struct type *actual_type = ada_check_typedef (actual->type ());
61ee279c 4556 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4557 struct type *formal_target =
78134374 4558 formal_type->code () == TYPE_CODE_PTR
27710edb 4559 ? ada_check_typedef (formal_type->target_type ()) : formal_type;
d2e4a39e 4560 struct type *actual_target =
78134374 4561 actual_type->code () == TYPE_CODE_PTR
27710edb 4562 ? ada_check_typedef (actual_type->target_type ()) : actual_type;
14f9c5c9 4563
4c4b4cd2 4564 if (ada_is_array_descriptor_type (formal_target)
78134374 4565 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4566 return make_array_descriptor (formal_type, actual);
78134374
SM
4567 else if (formal_type->code () == TYPE_CODE_PTR
4568 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4569 {
a84a8a0d 4570 struct value *result;
5b4ee69b 4571
78134374 4572 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4573 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4574 result = desc_data (actual);
78134374 4575 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7 4576 {
736355f2 4577 if (actual->lval () != lval_memory)
dda83cd7
SM
4578 {
4579 struct value *val;
4580
d0c97917 4581 actual_type = ada_check_typedef (actual->type ());
317c3ed9 4582 val = value::allocate (actual_type);
efaf1ae0 4583 copy (actual->contents (), val->contents_raw ());
dda83cd7
SM
4584 actual = ensure_lval (val);
4585 }
4586 result = value_addr (actual);
4587 }
a84a8a0d
JB
4588 else
4589 return actual;
b1af9e97 4590 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4591 }
78134374 4592 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4593 return ada_value_ind (actual);
8344af1e
JB
4594 else if (ada_is_aligner_type (formal_type))
4595 {
4596 /* We need to turn this parameter into an aligner type
4597 as well. */
317c3ed9 4598 struct value *aligner = value::allocate (formal_type);
8344af1e
JB
4599 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4600
4601 value_assign_to_component (aligner, component, actual);
4602 return aligner;
4603 }
14f9c5c9
AS
4604
4605 return actual;
4606}
4607
438c98a1
JB
4608/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4609 type TYPE. This is usually an inefficient no-op except on some targets
4610 (such as AVR) where the representation of a pointer and an address
4611 differs. */
4612
4613static CORE_ADDR
4614value_pointer (struct value *value, struct type *type)
4615{
df86565b 4616 unsigned len = type->length ();
224c3ddb 4617 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4618 CORE_ADDR addr;
4619
9feb2d07 4620 addr = value->address ();
8ee511af 4621 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4622 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4623 return addr;
4624}
4625
14f9c5c9 4626
4c4b4cd2
PH
4627/* Push a descriptor of type TYPE for array value ARR on the stack at
4628 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4629 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4630 to-descriptor type rather than a descriptor type), a struct value *
4631 representing a pointer to this descriptor. */
14f9c5c9 4632
d2e4a39e 4633static struct value *
40bc484c 4634make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4635{
d2e4a39e
AS
4636 struct type *bounds_type = desc_bounds_type (type);
4637 struct type *desc_type = desc_base_type (type);
317c3ed9
TT
4638 struct value *descriptor = value::allocate (desc_type);
4639 struct value *bounds = value::allocate (bounds_type);
14f9c5c9 4640 int i;
d2e4a39e 4641
d0c97917 4642 for (i = ada_array_arity (ada_check_typedef (arr->type ()));
0963b4bd 4643 i > 0; i -= 1)
14f9c5c9 4644 {
d0c97917 4645 modify_field (bounds->type (),
bbe912ba 4646 bounds->contents_writeable ().data (),
19f220c3
JK
4647 ada_array_bound (arr, i, 0),
4648 desc_bound_bitpos (bounds_type, i, 0),
4649 desc_bound_bitsize (bounds_type, i, 0));
d0c97917 4650 modify_field (bounds->type (),
bbe912ba 4651 bounds->contents_writeable ().data (),
19f220c3
JK
4652 ada_array_bound (arr, i, 1),
4653 desc_bound_bitpos (bounds_type, i, 1),
4654 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4655 }
d2e4a39e 4656
40bc484c 4657 bounds = ensure_lval (bounds);
d2e4a39e 4658
d0c97917 4659 modify_field (descriptor->type (),
bbe912ba 4660 descriptor->contents_writeable ().data (),
19f220c3 4661 value_pointer (ensure_lval (arr),
940da03e 4662 desc_type->field (0).type ()),
19f220c3
JK
4663 fat_pntr_data_bitpos (desc_type),
4664 fat_pntr_data_bitsize (desc_type));
4665
d0c97917 4666 modify_field (descriptor->type (),
bbe912ba 4667 descriptor->contents_writeable ().data (),
19f220c3 4668 value_pointer (bounds,
940da03e 4669 desc_type->field (1).type ()),
19f220c3
JK
4670 fat_pntr_bounds_bitpos (desc_type),
4671 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4672
40bc484c 4673 descriptor = ensure_lval (descriptor);
14f9c5c9 4674
78134374 4675 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4676 return value_addr (descriptor);
4677 else
4678 return descriptor;
4679}
14f9c5c9 4680\f
dda83cd7 4681 /* Symbol Cache Module */
3d9434b5 4682
3d9434b5 4683/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4684 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4685 on the type of entity being printed, the cache can make it as much
4686 as an order of magnitude faster than without it.
4687
4688 The descriptive type DWARF extension has significantly reduced
4689 the need for this cache, at least when DWARF is being used. However,
4690 even in this case, some expensive name-based symbol searches are still
4691 sometimes necessary - to find an XVZ variable, mostly. */
4692
3d9434b5
JB
4693/* Clear all entries from the symbol cache. */
4694
4695static void
6114d650 4696ada_clear_symbol_cache (program_space *pspace)
3d9434b5 4697{
6114d650 4698 ada_pspace_data_handle.clear (pspace);
3d9434b5
JB
4699}
4700
fe978cb0 4701/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4702 Return 1 if found, 0 otherwise.
4703
4704 If an entry was found and SYM is not NULL, set *SYM to the entry's
4705 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4706
96d887e8 4707static int
6c015214 4708lookup_cached_symbol (const char *name, domain_search_flags domain,
dda83cd7 4709 struct symbol **sym, const struct block **block)
96d887e8 4710{
9d1c303d
TT
4711 htab_t tab = get_ada_pspace_data (current_program_space);
4712 cache_entry_search search;
4713 search.name = name;
4714 search.domain = domain;
3d9434b5 4715
9d1c303d
TT
4716 cache_entry *e = (cache_entry *) htab_find_with_hash (tab, &search,
4717 search.hash ());
4718 if (e == nullptr)
3d9434b5 4719 return 0;
9d1c303d
TT
4720 if (sym != nullptr)
4721 *sym = e->sym;
4722 if (block != nullptr)
4723 *block = e->block;
3d9434b5 4724 return 1;
96d887e8
PH
4725}
4726
3d9434b5 4727/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4728 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4729
96d887e8 4730static void
6c015214
TT
4731cache_symbol (const char *name, domain_search_flags domain,
4732 struct symbol *sym, const struct block *block)
96d887e8 4733{
1994afbf
DE
4734 /* Symbols for builtin types don't have a block.
4735 For now don't cache such symbols. */
7b3ecc75 4736 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4737 return;
4738
3d9434b5
JB
4739 /* If the symbol is a local symbol, then do not cache it, as a search
4740 for that symbol depends on the context. To determine whether
4741 the symbol is local or not, we check the block where we found it
4742 against the global and static blocks of its associated symtab. */
63d609de
SM
4743 if (sym != nullptr)
4744 {
4745 const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
4746
4747 if (bv.global_block () != block && bv.static_block () != block)
4748 return;
4749 }
3d9434b5 4750
9d1c303d
TT
4751 htab_t tab = get_ada_pspace_data (current_program_space);
4752 cache_entry_search search;
4753 search.name = name;
4754 search.domain = domain;
4755
4756 void **slot = htab_find_slot_with_hash (tab, &search,
4757 search.hash (), INSERT);
4758
4759 cache_entry *e = new cache_entry;
4760 e->name = name;
fe978cb0 4761 e->domain = domain;
9d1c303d 4762 e->sym = sym;
3d9434b5 4763 e->block = block;
9d1c303d
TT
4764
4765 *slot = e;
96d887e8 4766}
4c4b4cd2 4767\f
dda83cd7 4768 /* Symbol Lookup */
4c4b4cd2 4769
b5ec771e
PA
4770/* Return the symbol name match type that should be used used when
4771 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4772
4773 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4774 for Ada lookups. */
c0431670 4775
b5ec771e
PA
4776static symbol_name_match_type
4777name_match_type_from_name (const char *lookup_name)
c0431670 4778{
b5ec771e
PA
4779 return (strstr (lookup_name, "__") == NULL
4780 ? symbol_name_match_type::WILD
4781 : symbol_name_match_type::FULL);
c0431670
JB
4782}
4783
4c4b4cd2
PH
4784/* Return the result of a standard (literal, C-like) lookup of NAME in
4785 given DOMAIN, visible from lexical block BLOCK. */
4786
4787static struct symbol *
4788standard_lookup (const char *name, const struct block *block,
6c015214 4789 domain_search_flags domain)
4c4b4cd2 4790{
acbd605d 4791 /* Initialize it just to avoid a GCC false warning. */
6640a367 4792 struct block_symbol sym = {};
4c4b4cd2 4793
d12307c1
PMR
4794 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4795 return sym.symbol;
a2cd4f14 4796 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4797 cache_symbol (name, domain, sym.symbol, sym.block);
4798 return sym.symbol;
4c4b4cd2
PH
4799}
4800
4801
4802/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4803 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4804 since they contend in overloading in the same way. */
4805static int
d1183b06 4806is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4807{
d1183b06 4808 for (const block_symbol &sym : syms)
5f9c5a63
SM
4809 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4810 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4811 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4812 return 1;
4813
4814 return 0;
4815}
4816
4817/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4818 struct types. Otherwise, they may not. */
14f9c5c9
AS
4819
4820static int
d2e4a39e 4821equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4822{
d2e4a39e 4823 if (type0 == type1)
14f9c5c9 4824 return 1;
d2e4a39e 4825 if (type0 == NULL || type1 == NULL
78134374 4826 || type0->code () != type1->code ())
14f9c5c9 4827 return 0;
78134374
SM
4828 if ((type0->code () == TYPE_CODE_STRUCT
4829 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4830 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4831 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4832 return 1;
d2e4a39e 4833
14f9c5c9
AS
4834 return 0;
4835}
4836
4837/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4838 no more defined than that of SYM1. */
14f9c5c9
AS
4839
4840static int
d2e4a39e 4841lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4842{
4843 if (sym0 == sym1)
4844 return 1;
6c9c307c 4845 if (sym0->domain () != sym1->domain ()
66d7f48f 4846 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4847 return 0;
4848
66d7f48f 4849 switch (sym0->aclass ())
14f9c5c9
AS
4850 {
4851 case LOC_UNDEF:
4852 return 1;
4853 case LOC_TYPEDEF:
4854 {
5f9c5a63
SM
4855 struct type *type0 = sym0->type ();
4856 struct type *type1 = sym1->type ();
dda83cd7
SM
4857 const char *name0 = sym0->linkage_name ();
4858 const char *name1 = sym1->linkage_name ();
4859 int len0 = strlen (name0);
4860
4861 return
4862 type0->code () == type1->code ()
4863 && (equiv_types (type0, type1)
4864 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4865 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4866 }
4867 case LOC_CONST:
4aeddc50 4868 return sym0->value_longest () == sym1->value_longest ()
5f9c5a63 4869 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4870
4871 case LOC_STATIC:
4872 {
dda83cd7
SM
4873 const char *name0 = sym0->linkage_name ();
4874 const char *name1 = sym1->linkage_name ();
4875 return (strcmp (name0, name1) == 0
4aeddc50 4876 && sym0->value_address () == sym1->value_address ());
4b610737
TT
4877 }
4878
d2e4a39e
AS
4879 default:
4880 return 0;
14f9c5c9
AS
4881 }
4882}
4883
d1183b06
TT
4884/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4885 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4886
4887static void
d1183b06 4888add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4889 struct symbol *sym,
4890 const struct block *block)
14f9c5c9 4891{
529cad9c
PH
4892 /* Do not try to complete stub types, as the debugger is probably
4893 already scanning all symbols matching a certain name at the
4894 time when this function is called. Trying to replace the stub
4895 type by its associated full type will cause us to restart a scan
4896 which may lead to an infinite recursion. Instead, the client
4897 collecting the matching symbols will end up collecting several
4898 matches, with at least one of them complete. It can then filter
4899 out the stub ones if needed. */
4900
d1183b06 4901 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4902 {
d1183b06 4903 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4904 return;
d1183b06 4905 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4906 {
d1183b06
TT
4907 result[i].symbol = sym;
4908 result[i].block = block;
dda83cd7
SM
4909 return;
4910 }
4c4b4cd2
PH
4911 }
4912
d1183b06
TT
4913 struct block_symbol info;
4914 info.symbol = sym;
4915 info.block = block;
4916 result.push_back (info);
4c4b4cd2
PH
4917}
4918
7c7b6655
TT
4919/* Return a bound minimal symbol matching NAME according to Ada
4920 decoding rules. Returns an invalid symbol if there is no such
4921 minimal symbol. Names prefixed with "standard__" are handled
4922 specially: "standard__" is first stripped off, and only static and
4923 global symbols are searched. */
4c4b4cd2 4924
7c7b6655 4925struct bound_minimal_symbol
06a670e2 4926ada_lookup_simple_minsym (const char *name, struct objfile *objfile)
4c4b4cd2 4927{
7c7b6655 4928 struct bound_minimal_symbol result;
4c4b4cd2 4929
b5ec771e
PA
4930 symbol_name_match_type match_type = name_match_type_from_name (name);
4931 lookup_name_info lookup_name (name, match_type);
4932
4933 symbol_name_matcher_ftype *match_name
4934 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4935
06a670e2 4936 gdbarch_iterate_over_objfiles_in_search_order
99d9c3b9 4937 (objfile != NULL ? objfile->arch () : current_inferior ()->arch (),
06a670e2
MM
4938 [&result, lookup_name, match_name] (struct objfile *obj)
4939 {
4940 for (minimal_symbol *msymbol : obj->msymbols ())
4941 {
4942 if (match_name (msymbol->linkage_name (), lookup_name, nullptr)
4943 && msymbol->type () != mst_solib_trampoline)
4944 {
4945 result.minsym = msymbol;
4946 result.objfile = obj;
4947 return 1;
4948 }
4949 }
4950
4951 return 0;
4952 }, objfile);
4c4b4cd2 4953
7c7b6655 4954 return result;
96d887e8 4955}
4c4b4cd2 4956
96d887e8
PH
4957/* True if TYPE is definitely an artificial type supplied to a symbol
4958 for which no debugging information was given in the symbol file. */
14f9c5c9 4959
96d887e8
PH
4960static int
4961is_nondebugging_type (struct type *type)
4962{
0d5cff50 4963 const char *name = ada_type_name (type);
5b4ee69b 4964
96d887e8
PH
4965 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4966}
4c4b4cd2 4967
8f17729f
JB
4968/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4969 that are deemed "identical" for practical purposes.
4970
4971 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4972 types and that their number of enumerals is identical (in other
1f704f76 4973 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4974
4975static int
4976ada_identical_enum_types_p (struct type *type1, struct type *type2)
4977{
4978 int i;
4979
4980 /* The heuristic we use here is fairly conservative. We consider
4981 that 2 enumerate types are identical if they have the same
4982 number of enumerals and that all enumerals have the same
4983 underlying value and name. */
4984
4985 /* All enums in the type should have an identical underlying value. */
1f704f76 4986 for (i = 0; i < type1->num_fields (); i++)
970db518 4987 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4988 return 0;
4989
4990 /* All enumerals should also have the same name (modulo any numerical
4991 suffix). */
1f704f76 4992 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4993 {
33d16dd9
SM
4994 const char *name_1 = type1->field (i).name ();
4995 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4996 int len_1 = strlen (name_1);
4997 int len_2 = strlen (name_2);
4998
33d16dd9
SM
4999 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
5000 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 5001 if (len_1 != len_2
33d16dd9
SM
5002 || strncmp (type1->field (i).name (),
5003 type2->field (i).name (),
8f17729f
JB
5004 len_1) != 0)
5005 return 0;
5006 }
5007
5008 return 1;
5009}
5010
5011/* Return nonzero if all the symbols in SYMS are all enumeral symbols
5012 that are deemed "identical" for practical purposes. Sometimes,
5013 enumerals are not strictly identical, but their types are so similar
5014 that they can be considered identical.
5015
5016 For instance, consider the following code:
5017
5018 type Color is (Black, Red, Green, Blue, White);
5019 type RGB_Color is new Color range Red .. Blue;
5020
5021 Type RGB_Color is a subrange of an implicit type which is a copy
5022 of type Color. If we call that implicit type RGB_ColorB ("B" is
5023 for "Base Type"), then type RGB_ColorB is a copy of type Color.
5024 As a result, when an expression references any of the enumeral
5025 by name (Eg. "print green"), the expression is technically
5026 ambiguous and the user should be asked to disambiguate. But
5027 doing so would only hinder the user, since it wouldn't matter
5028 what choice he makes, the outcome would always be the same.
5029 So, for practical purposes, we consider them as the same. */
5030
5031static int
54d343a2 5032symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
5033{
5034 int i;
5035
5036 /* Before performing a thorough comparison check of each type,
5037 we perform a series of inexpensive checks. We expect that these
5038 checks will quickly fail in the vast majority of cases, and thus
5039 help prevent the unnecessary use of a more expensive comparison.
5040 Said comparison also expects us to make some of these checks
5041 (see ada_identical_enum_types_p). */
5042
5043 /* Quick check: All symbols should have an enum type. */
54d343a2 5044 for (i = 0; i < syms.size (); i++)
5f9c5a63 5045 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
5046 return 0;
5047
5048 /* Quick check: They should all have the same value. */
54d343a2 5049 for (i = 1; i < syms.size (); i++)
4aeddc50 5050 if (syms[i].symbol->value_longest () != syms[0].symbol->value_longest ())
8f17729f
JB
5051 return 0;
5052
5053 /* Quick check: They should all have the same number of enumerals. */
54d343a2 5054 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5055 if (syms[i].symbol->type ()->num_fields ()
5056 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
5057 return 0;
5058
5059 /* All the sanity checks passed, so we might have a set of
5060 identical enumeration types. Perform a more complete
5061 comparison of the type of each symbol. */
54d343a2 5062 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5063 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5064 syms[0].symbol->type ()))
8f17729f
JB
5065 return 0;
5066
5067 return 1;
5068}
5069
54d343a2 5070/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5071 duplicate other symbols in the list (The only case I know of where
5072 this happens is when object files containing stabs-in-ecoff are
5073 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5074 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5075
d1183b06 5076static void
ff4631e2 5077remove_extra_symbols (std::vector<struct block_symbol> &syms)
96d887e8
PH
5078{
5079 int i, j;
4c4b4cd2 5080
8f17729f
JB
5081 /* We should never be called with less than 2 symbols, as there
5082 cannot be any extra symbol in that case. But it's easy to
5083 handle, since we have nothing to do in that case. */
ff4631e2 5084 if (syms.size () < 2)
d1183b06 5085 return;
8f17729f 5086
96d887e8 5087 i = 0;
ff4631e2 5088 while (i < syms.size ())
96d887e8 5089 {
44a37a98 5090 bool remove_p = false;
339c13b6
JB
5091
5092 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5093 the get rid of the stub. */
339c13b6 5094
ff4631e2
TT
5095 if (syms[i].symbol->type ()->is_stub ()
5096 && syms[i].symbol->linkage_name () != NULL)
dda83cd7 5097 {
44a37a98 5098 for (j = 0; !remove_p && j < syms.size (); j++)
dda83cd7
SM
5099 {
5100 if (j != i
ff4631e2
TT
5101 && !syms[j].symbol->type ()->is_stub ()
5102 && syms[j].symbol->linkage_name () != NULL
5103 && strcmp (syms[i].symbol->linkage_name (),
5104 syms[j].symbol->linkage_name ()) == 0)
44a37a98 5105 remove_p = true;
dda83cd7
SM
5106 }
5107 }
339c13b6
JB
5108
5109 /* Two symbols with the same name, same class and same address
dda83cd7 5110 should be identical. */
339c13b6 5111
ff4631e2
TT
5112 else if (syms[i].symbol->linkage_name () != NULL
5113 && syms[i].symbol->aclass () == LOC_STATIC
5114 && is_nondebugging_type (syms[i].symbol->type ()))
dda83cd7 5115 {
44a37a98 5116 for (j = 0; !remove_p && j < syms.size (); j += 1)
dda83cd7
SM
5117 {
5118 if (i != j
ff4631e2
TT
5119 && syms[j].symbol->linkage_name () != NULL
5120 && strcmp (syms[i].symbol->linkage_name (),
5121 syms[j].symbol->linkage_name ()) == 0
5122 && (syms[i].symbol->aclass ()
5123 == syms[j].symbol->aclass ())
5124 && syms[i].symbol->value_address ()
5125 == syms[j].symbol->value_address ())
44a37a98 5126 remove_p = true;
dda83cd7
SM
5127 }
5128 }
339c13b6 5129
e9151f7d
TT
5130 /* Two functions with the same block are identical. */
5131
5132 else if (syms[i].symbol->aclass () == LOC_BLOCK)
5133 {
5134 for (j = 0; !remove_p && j < syms.size (); j += 1)
5135 {
5136 if (i != j
5137 && syms[j].symbol->aclass () == LOC_BLOCK
5138 && (syms[i].symbol->value_block ()
5139 == syms[j].symbol->value_block ()))
5140 remove_p = true;
5141 }
5142 }
5143
a35ddb44 5144 if (remove_p)
ff4631e2 5145 syms.erase (syms.begin () + i);
1b788fb6
TT
5146 else
5147 i += 1;
14f9c5c9 5148 }
14f9c5c9
AS
5149}
5150
96d887e8
PH
5151/* Given a type that corresponds to a renaming entity, use the type name
5152 to extract the scope (package name or function name, fully qualified,
5153 and following the GNAT encoding convention) where this renaming has been
49d83361 5154 defined. */
4c4b4cd2 5155
49d83361 5156static std::string
96d887e8 5157xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5158{
96d887e8 5159 /* The renaming types adhere to the following convention:
0963b4bd 5160 <scope>__<rename>___<XR extension>.
96d887e8
PH
5161 So, to extract the scope, we search for the "___XR" extension,
5162 and then backtrack until we find the first "__". */
76a01679 5163
7d93a1e0 5164 const char *name = renaming_type->name ();
108d56a4
SM
5165 const char *suffix = strstr (name, "___XR");
5166 const char *last;
14f9c5c9 5167
96d887e8
PH
5168 /* Now, backtrack a bit until we find the first "__". Start looking
5169 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5170
96d887e8
PH
5171 for (last = suffix - 3; last > name; last--)
5172 if (last[0] == '_' && last[1] == '_')
5173 break;
76a01679 5174
96d887e8 5175 /* Make a copy of scope and return it. */
49d83361 5176 return std::string (name, last);
4c4b4cd2
PH
5177}
5178
96d887e8 5179/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5180
96d887e8
PH
5181static int
5182is_package_name (const char *name)
4c4b4cd2 5183{
96d887e8
PH
5184 /* Here, We take advantage of the fact that no symbols are generated
5185 for packages, while symbols are generated for each function.
5186 So the condition for NAME represent a package becomes equivalent
5187 to NAME not existing in our list of symbols. There is only one
5188 small complication with library-level functions (see below). */
4c4b4cd2 5189
96d887e8
PH
5190 /* If it is a function that has not been defined at library level,
5191 then we should be able to look it up in the symbols. */
6c015214 5192 if (standard_lookup (name, NULL, SEARCH_VFT) != NULL)
96d887e8 5193 return 0;
14f9c5c9 5194
96d887e8
PH
5195 /* Library-level function names start with "_ada_". See if function
5196 "_ada_" followed by NAME can be found. */
14f9c5c9 5197
96d887e8 5198 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5199 functions names cannot contain "__" in them. */
96d887e8
PH
5200 if (strstr (name, "__") != NULL)
5201 return 0;
4c4b4cd2 5202
528e1572 5203 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5204
6c015214 5205 return (standard_lookup (fun_name.c_str (), NULL, SEARCH_VFT) == NULL);
96d887e8 5206}
14f9c5c9 5207
96d887e8 5208/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5209 not visible from FUNCTION_NAME. */
14f9c5c9 5210
96d887e8 5211static int
0d5cff50 5212old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5213{
66d7f48f 5214 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5215 return 0;
5216
5f9c5a63 5217 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5218
96d887e8 5219 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5220 if (is_package_name (scope.c_str ()))
5221 return 0;
14f9c5c9 5222
96d887e8
PH
5223 /* Check that the rename is in the current function scope by checking
5224 that its name starts with SCOPE. */
76a01679 5225
96d887e8
PH
5226 /* If the function name starts with "_ada_", it means that it is
5227 a library-level function. Strip this prefix before doing the
5228 comparison, as the encoding for the renaming does not contain
5229 this prefix. */
61012eef 5230 if (startswith (function_name, "_ada_"))
96d887e8 5231 function_name += 5;
f26caa11 5232
49d83361 5233 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5234}
5235
aeb5907d
JB
5236/* Remove entries from SYMS that corresponds to a renaming entity that
5237 is not visible from the function associated with CURRENT_BLOCK or
5238 that is superfluous due to the presence of more specific renaming
5239 information. Places surviving symbols in the initial entries of
d1183b06
TT
5240 SYMS.
5241
96d887e8 5242 Rationale:
aeb5907d
JB
5243 First, in cases where an object renaming is implemented as a
5244 reference variable, GNAT may produce both the actual reference
5245 variable and the renaming encoding. In this case, we discard the
5246 latter.
5247
5248 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5249 entity. Unfortunately, STABS currently does not support the definition
5250 of types that are local to a given lexical block, so all renamings types
5251 are emitted at library level. As a consequence, if an application
5252 contains two renaming entities using the same name, and a user tries to
5253 print the value of one of these entities, the result of the ada symbol
5254 lookup will also contain the wrong renaming type.
f26caa11 5255
96d887e8
PH
5256 This function partially covers for this limitation by attempting to
5257 remove from the SYMS list renaming symbols that should be visible
5258 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5259 method with the current information available. The implementation
5260 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5261
5262 - When the user tries to print a rename in a function while there
dda83cd7
SM
5263 is another rename entity defined in a package: Normally, the
5264 rename in the function has precedence over the rename in the
5265 package, so the latter should be removed from the list. This is
5266 currently not the case.
5267
96d887e8 5268 - This function will incorrectly remove valid renames if
dda83cd7
SM
5269 the CURRENT_BLOCK corresponds to a function which symbol name
5270 has been changed by an "Export" pragma. As a consequence,
5271 the user will be unable to print such rename entities. */
4c4b4cd2 5272
d1183b06 5273static void
54d343a2
TT
5274remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5275 const struct block *current_block)
4c4b4cd2
PH
5276{
5277 struct symbol *current_function;
0d5cff50 5278 const char *current_function_name;
4c4b4cd2 5279 int i;
aeb5907d
JB
5280 int is_new_style_renaming;
5281
5282 /* If there is both a renaming foo___XR... encoded as a variable and
5283 a simple variable foo in the same block, discard the latter.
0963b4bd 5284 First, zero out such symbols, then compress. */
aeb5907d 5285 is_new_style_renaming = 0;
54d343a2 5286 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5287 {
54d343a2
TT
5288 struct symbol *sym = (*syms)[i].symbol;
5289 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5290 const char *name;
5291 const char *suffix;
5292
66d7f48f 5293 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5294 continue;
987012b8 5295 name = sym->linkage_name ();
aeb5907d
JB
5296 suffix = strstr (name, "___XR");
5297
5298 if (suffix != NULL)
5299 {
5300 int name_len = suffix - name;
5301 int j;
5b4ee69b 5302
aeb5907d 5303 is_new_style_renaming = 1;
54d343a2
TT
5304 for (j = 0; j < syms->size (); j += 1)
5305 if (i != j && (*syms)[j].symbol != NULL
987012b8 5306 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5307 name_len) == 0
54d343a2
TT
5308 && block == (*syms)[j].block)
5309 (*syms)[j].symbol = NULL;
aeb5907d
JB
5310 }
5311 }
5312 if (is_new_style_renaming)
5313 {
5314 int j, k;
5315
54d343a2
TT
5316 for (j = k = 0; j < syms->size (); j += 1)
5317 if ((*syms)[j].symbol != NULL)
aeb5907d 5318 {
54d343a2 5319 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5320 k += 1;
5321 }
d1183b06
TT
5322 syms->resize (k);
5323 return;
aeb5907d 5324 }
4c4b4cd2
PH
5325
5326 /* Extract the function name associated to CURRENT_BLOCK.
5327 Abort if unable to do so. */
76a01679 5328
4c4b4cd2 5329 if (current_block == NULL)
d1183b06 5330 return;
76a01679 5331
3c9d0506 5332 current_function = current_block->linkage_function ();
4c4b4cd2 5333 if (current_function == NULL)
d1183b06 5334 return;
4c4b4cd2 5335
987012b8 5336 current_function_name = current_function->linkage_name ();
4c4b4cd2 5337 if (current_function_name == NULL)
d1183b06 5338 return;
4c4b4cd2
PH
5339
5340 /* Check each of the symbols, and remove it from the list if it is
5341 a type corresponding to a renaming that is out of the scope of
5342 the current block. */
5343
5344 i = 0;
54d343a2 5345 while (i < syms->size ())
4c4b4cd2 5346 {
54d343a2 5347 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5348 == ADA_OBJECT_RENAMING
5349 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5350 current_function_name))
5351 syms->erase (syms->begin () + i);
4c4b4cd2 5352 else
dda83cd7 5353 i += 1;
4c4b4cd2 5354 }
4c4b4cd2
PH
5355}
5356
d1183b06 5357/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5358 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5359
cd458349 5360 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5361
5362static void
d1183b06 5363ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e 5364 const lookup_name_info &lookup_name,
6c015214 5365 const struct block *block, domain_search_flags domain)
339c13b6 5366{
339c13b6
JB
5367 while (block != NULL)
5368 {
d1183b06 5369 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5370
ba8694b6
TT
5371 /* If we found a non-function match, assume that's the one. We
5372 only check this when finding a function boundary, so that we
5373 can accumulate all results from intervening blocks first. */
6c00f721 5374 if (block->function () != nullptr && is_nonfunction (result))
dda83cd7 5375 return;
339c13b6 5376
f135fe72 5377 block = block->superblock ();
339c13b6 5378 }
339c13b6
JB
5379}
5380
2315bb2d 5381/* An object of this type is used as the callback argument when
40658b94 5382 calling the map_matching_symbols method. */
ccefe4c4 5383
40658b94 5384struct match_data
ccefe4c4 5385{
1bfa81ac
TT
5386 explicit match_data (std::vector<struct block_symbol> *rp)
5387 : resultp (rp)
5388 {
5389 }
5390 DISABLE_COPY_AND_ASSIGN (match_data);
5391
2315bb2d
TT
5392 bool operator() (struct block_symbol *bsym);
5393
1bfa81ac 5394 struct objfile *objfile = nullptr;
d1183b06 5395 std::vector<struct block_symbol> *resultp;
1bfa81ac 5396 struct symbol *arg_sym = nullptr;
1178743e 5397 bool found_sym = false;
ccefe4c4
TT
5398};
5399
2315bb2d
TT
5400/* A callback for add_nonlocal_symbols that adds symbol, found in
5401 BSYM, to a list of symbols. */
ccefe4c4 5402
2315bb2d
TT
5403bool
5404match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5405{
199b4314
TT
5406 const struct block *block = bsym->block;
5407 struct symbol *sym = bsym->symbol;
5408
40658b94
PH
5409 if (sym == NULL)
5410 {
2315bb2d 5411 if (!found_sym && arg_sym != NULL)
dae58e04 5412 add_defn_to_vec (*resultp, arg_sym, block);
2315bb2d
TT
5413 found_sym = false;
5414 arg_sym = NULL;
40658b94
PH
5415 }
5416 else
5417 {
66d7f48f 5418 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5419 return true;
d9743061 5420 else if (sym->is_argument ())
2315bb2d 5421 arg_sym = sym;
40658b94
PH
5422 else
5423 {
2315bb2d 5424 found_sym = true;
dae58e04 5425 add_defn_to_vec (*resultp, sym, block);
40658b94
PH
5426 }
5427 }
199b4314 5428 return true;
40658b94
PH
5429}
5430
b5ec771e
PA
5431/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5432 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5433 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5434
5435static int
d1183b06 5436ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5437 const struct block *block,
b5ec771e 5438 const lookup_name_info &lookup_name,
6c015214 5439 domain_search_flags domain)
22cee43f
PMR
5440{
5441 struct using_direct *renaming;
d1183b06 5442 int defns_mark = result.size ();
22cee43f 5443
b5ec771e
PA
5444 symbol_name_matcher_ftype *name_match
5445 = ada_get_symbol_name_matcher (lookup_name);
5446
3c45e9f9 5447 for (renaming = block->get_using ();
22cee43f
PMR
5448 renaming != NULL;
5449 renaming = renaming->next)
5450 {
5451 const char *r_name;
22cee43f
PMR
5452
5453 /* Avoid infinite recursions: skip this renaming if we are actually
5454 already traversing it.
5455
5456 Currently, symbol lookup in Ada don't use the namespace machinery from
5457 C++/Fortran support: skip namespace imports that use them. */
5458 if (renaming->searched
5459 || (renaming->import_src != NULL
5460 && renaming->import_src[0] != '\0')
5461 || (renaming->import_dest != NULL
5462 && renaming->import_dest[0] != '\0'))
5463 continue;
5464 renaming->searched = 1;
5465
5466 /* TODO: here, we perform another name-based symbol lookup, which can
5467 pull its own multiple overloads. In theory, we should be able to do
5468 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5469 not a simple name. But in order to do this, we would need to enhance
5470 the DWARF reader to associate a symbol to this renaming, instead of a
5471 name. So, for now, we do something simpler: re-use the C++/Fortran
5472 namespace machinery. */
5473 r_name = (renaming->alias != NULL
5474 ? renaming->alias
5475 : renaming->declaration);
b5ec771e
PA
5476 if (name_match (r_name, lookup_name, NULL))
5477 {
5478 lookup_name_info decl_lookup_name (renaming->declaration,
5479 lookup_name.match_type ());
d1183b06 5480 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5481 1, NULL);
5482 }
22cee43f
PMR
5483 renaming->searched = 0;
5484 }
d1183b06 5485 return result.size () != defns_mark;
22cee43f
PMR
5486}
5487
b5ec771e
PA
5488/* Convenience function to get at the Ada encoded lookup name for
5489 LOOKUP_NAME, as a C string. */
5490
5491static const char *
5492ada_lookup_name (const lookup_name_info &lookup_name)
5493{
5494 return lookup_name.ada ().lookup_name ().c_str ();
5495}
5496
957ce537 5497/* A helper for add_nonlocal_symbols. Expand all necessary symtabs
0b7b2c2a
TT
5498 for OBJFILE, then walk the objfile's symtabs and update the
5499 results. */
5500
5501static void
5502map_matching_symbols (struct objfile *objfile,
5503 const lookup_name_info &lookup_name,
6c015214 5504 domain_search_flags domain,
0b7b2c2a
TT
5505 int global,
5506 match_data &data)
5507{
5508 data.objfile = objfile;
957ce537
TT
5509 objfile->expand_symtabs_matching (nullptr, &lookup_name,
5510 nullptr, nullptr,
5511 global
5512 ? SEARCH_GLOBAL_BLOCK
5513 : SEARCH_STATIC_BLOCK,
6c015214 5514 domain);
0b7b2c2a
TT
5515
5516 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5517 for (compunit_symtab *symtab : objfile->compunits ())
5518 {
5519 const struct block *block
63d609de 5520 = symtab->blockvector ()->block (block_kind);
0b7b2c2a
TT
5521 if (!iterate_over_symbols_terminated (block, lookup_name,
5522 domain, data))
5523 break;
5524 }
5525}
5526
1bfa81ac 5527/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5528 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5529 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5530 symbols otherwise. */
339c13b6
JB
5531
5532static void
d1183b06 5533add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e 5534 const lookup_name_info &lookup_name,
6c015214 5535 domain_search_flags domain, int global)
339c13b6 5536{
1bfa81ac 5537 struct match_data data (&result);
339c13b6 5538
b5ec771e
PA
5539 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5540
2030c079 5541 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5542 {
957ce537 5543 map_matching_symbols (objfile, lookup_name, domain, global, data);
22cee43f 5544
b669c953 5545 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5546 {
5547 const struct block *global_block
63d609de 5548 = cu->blockvector ()->global_block ();
22cee43f 5549
d1183b06 5550 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5551 domain))
1178743e 5552 data.found_sym = true;
22cee43f 5553 }
40658b94
PH
5554 }
5555
d1183b06 5556 if (result.empty () && global && !is_wild_match)
40658b94 5557 {
b5ec771e 5558 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5559 std::string bracket_name = std::string ("<_ada_") + name + '>';
5560 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5561
2030c079 5562 for (objfile *objfile : current_program_space->objfiles ())
957ce537 5563 map_matching_symbols (objfile, name1, domain, global, data);
0b7b2c2a 5564 }
339c13b6
JB
5565}
5566
b5ec771e
PA
5567/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5568 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5569 returning the number of matches. Add these to RESULT.
4eeaa230 5570
22cee43f
PMR
5571 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5572 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5573 is the one match returned (no other matches in that or
d9680e73 5574 enclosing blocks is returned). If there are any matches in or
22cee43f 5575 surrounding BLOCK, then these alone are returned.
4eeaa230 5576
b5ec771e
PA
5577 Names prefixed with "standard__" are handled specially:
5578 "standard__" is first stripped off (by the lookup_name
5579 constructor), and only static and global symbols are searched.
14f9c5c9 5580
22cee43f
PMR
5581 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5582 to lookup global symbols. */
5583
5584static void
d1183b06 5585ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5586 const struct block *block,
b5ec771e 5587 const lookup_name_info &lookup_name,
6c015214 5588 domain_search_flags domain,
22cee43f
PMR
5589 int full_search,
5590 int *made_global_lookup_p)
14f9c5c9
AS
5591{
5592 struct symbol *sym;
14f9c5c9 5593
22cee43f
PMR
5594 if (made_global_lookup_p)
5595 *made_global_lookup_p = 0;
339c13b6
JB
5596
5597 /* Special case: If the user specifies a symbol name inside package
5598 Standard, do a non-wild matching of the symbol name without
5599 the "standard__" prefix. This was primarily introduced in order
5600 to allow the user to specifically access the standard exceptions
5601 using, for instance, Standard.Constraint_Error when Constraint_Error
5602 is ambiguous (due to the user defining its own Constraint_Error
5603 entity inside its program). */
b5ec771e
PA
5604 if (lookup_name.ada ().standard_p ())
5605 block = NULL;
4c4b4cd2 5606
339c13b6 5607 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5608
4eeaa230
DE
5609 if (block != NULL)
5610 {
5611 if (full_search)
d1183b06 5612 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5613 else
5614 {
5615 /* In the !full_search case we're are being called by
4009ee92 5616 iterate_over_symbols, and we don't want to search
4eeaa230 5617 superblocks. */
d1183b06 5618 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5619 }
d1183b06 5620 if (!result.empty () || !full_search)
22cee43f 5621 return;
4eeaa230 5622 }
d2e4a39e 5623
339c13b6
JB
5624 /* No non-global symbols found. Check our cache to see if we have
5625 already performed this search before. If we have, then return
5626 the same result. */
5627
b5ec771e
PA
5628 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5629 domain, &sym, &block))
4c4b4cd2
PH
5630 {
5631 if (sym != NULL)
d1183b06 5632 add_defn_to_vec (result, sym, block);
22cee43f 5633 return;
4c4b4cd2 5634 }
14f9c5c9 5635
22cee43f
PMR
5636 if (made_global_lookup_p)
5637 *made_global_lookup_p = 1;
b1eedac9 5638
339c13b6
JB
5639 /* Search symbols from all global blocks. */
5640
d1183b06 5641 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5642
4c4b4cd2 5643 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5644 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5645
d1183b06
TT
5646 if (result.empty ())
5647 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5648}
5649
b5ec771e 5650/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5651 is non-zero, enclosing scope and in global scopes.
5652
5653 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5654 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5655
5656 When full_search is non-zero, any non-function/non-enumeral
5657 symbol match within the nest of blocks whose innermost member is BLOCK,
5658 is the one match returned (no other matches in that or
5659 enclosing blocks is returned). If there are any matches in or
5660 surrounding BLOCK, then these alone are returned.
5661
5662 Names prefixed with "standard__" are handled specially: "standard__"
5663 is first stripped off, and only static and global symbols are searched. */
5664
d1183b06 5665static std::vector<struct block_symbol>
b5ec771e
PA
5666ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5667 const struct block *block,
6c015214 5668 domain_search_flags domain,
22cee43f
PMR
5669 int full_search)
5670{
22cee43f 5671 int syms_from_global_search;
d1183b06 5672 std::vector<struct block_symbol> results;
22cee43f 5673
d1183b06 5674 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5675 domain, full_search, &syms_from_global_search);
14f9c5c9 5676
ff4631e2 5677 remove_extra_symbols (results);
4c4b4cd2 5678
d1183b06 5679 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5680 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5681
d1183b06 5682 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5683 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5684 results[0].symbol, results[0].block);
ec6a20c2 5685
d1183b06
TT
5686 remove_irrelevant_renamings (&results, block);
5687 return results;
14f9c5c9
AS
5688}
5689
b5ec771e 5690/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5691 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5692
4eeaa230
DE
5693 See ada_lookup_symbol_list_worker for further details. */
5694
d1183b06 5695std::vector<struct block_symbol>
b5ec771e 5696ada_lookup_symbol_list (const char *name, const struct block *block,
6c015214 5697 domain_search_flags domain)
4eeaa230 5698{
b5ec771e
PA
5699 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5700 lookup_name_info lookup_name (name, name_match_type);
5701
d1183b06 5702 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5703}
5704
4e5c77fe
JB
5705/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5706 to 1, but choosing the first symbol found if there are multiple
5707 choices.
5708
5e2336be
JB
5709 The result is stored in *INFO, which must be non-NULL.
5710 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5711
5712void
5713ada_lookup_encoded_symbol (const char *name, const struct block *block,
6c015214 5714 domain_search_flags domain,
d12307c1 5715 struct block_symbol *info)
14f9c5c9 5716{
b5ec771e
PA
5717 /* Since we already have an encoded name, wrap it in '<>' to force a
5718 verbatim match. Otherwise, if the name happens to not look like
5719 an encoded name (because it doesn't include a "__"),
5720 ada_lookup_name_info would re-encode/fold it again, and that
5721 would e.g., incorrectly lowercase object renaming names like
5722 "R28b" -> "r28b". */
12932e2c 5723 std::string verbatim = add_angle_brackets (name);
b5ec771e 5724
5e2336be 5725 gdb_assert (info != NULL);
65392b3e 5726 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5727}
aeb5907d
JB
5728
5729/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5730 scope and in global scopes, or NULL if none. NAME is folded and
5731 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5732 choosing the first symbol if there are multiple choices. */
4e5c77fe 5733
d12307c1 5734struct block_symbol
aeb5907d 5735ada_lookup_symbol (const char *name, const struct block *block0,
6c015214 5736 domain_search_flags domain)
aeb5907d 5737{
d1183b06
TT
5738 std::vector<struct block_symbol> candidates
5739 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5740
d1183b06 5741 if (candidates.empty ())
54d343a2 5742 return {};
f98fc17b 5743
dae58e04 5744 return candidates[0];
4c4b4cd2 5745}
14f9c5c9 5746
14f9c5c9 5747
4c4b4cd2
PH
5748/* True iff STR is a possible encoded suffix of a normal Ada name
5749 that is to be ignored for matching purposes. Suffixes of parallel
5750 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5751 are given by any of the regular expressions:
4c4b4cd2 5752
babe1480
JB
5753 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5754 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5755 TKB [subprogram suffix for task bodies]
babe1480 5756 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5757 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5758
5759 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5760 match is performed. This sequence is used to differentiate homonyms,
5761 is an optional part of a valid name suffix. */
4c4b4cd2 5762
14f9c5c9 5763static int
d2e4a39e 5764is_name_suffix (const char *str)
14f9c5c9
AS
5765{
5766 int k;
4c4b4cd2
PH
5767 const char *matching;
5768 const int len = strlen (str);
5769
babe1480
JB
5770 /* Skip optional leading __[0-9]+. */
5771
4c4b4cd2
PH
5772 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5773 {
babe1480
JB
5774 str += 3;
5775 while (isdigit (str[0]))
dda83cd7 5776 str += 1;
4c4b4cd2 5777 }
babe1480
JB
5778
5779 /* [.$][0-9]+ */
4c4b4cd2 5780
babe1480 5781 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5782 {
babe1480 5783 matching = str + 1;
4c4b4cd2 5784 while (isdigit (matching[0]))
dda83cd7 5785 matching += 1;
4c4b4cd2 5786 if (matching[0] == '\0')
dda83cd7 5787 return 1;
4c4b4cd2
PH
5788 }
5789
5790 /* ___[0-9]+ */
babe1480 5791
4c4b4cd2
PH
5792 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5793 {
5794 matching = str + 3;
5795 while (isdigit (matching[0]))
dda83cd7 5796 matching += 1;
4c4b4cd2 5797 if (matching[0] == '\0')
dda83cd7 5798 return 1;
4c4b4cd2
PH
5799 }
5800
9ac7f98e
JB
5801 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5802
5803 if (strcmp (str, "TKB") == 0)
5804 return 1;
5805
529cad9c
PH
5806#if 0
5807 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5808 with a N at the end. Unfortunately, the compiler uses the same
5809 convention for other internal types it creates. So treating
529cad9c 5810 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5811 some regressions. For instance, consider the case of an enumerated
5812 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5813 name ends with N.
5814 Having a single character like this as a suffix carrying some
0963b4bd 5815 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5816 to be something like "_N" instead. In the meantime, do not do
5817 the following check. */
5818 /* Protected Object Subprograms */
5819 if (len == 1 && str [0] == 'N')
5820 return 1;
5821#endif
5822
5823 /* _E[0-9]+[bs]$ */
5824 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5825 {
5826 matching = str + 3;
5827 while (isdigit (matching[0]))
dda83cd7 5828 matching += 1;
529cad9c 5829 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5830 && matching [1] == '\0')
5831 return 1;
529cad9c
PH
5832 }
5833
4c4b4cd2
PH
5834 /* ??? We should not modify STR directly, as we are doing below. This
5835 is fine in this case, but may become problematic later if we find
5836 that this alternative did not work, and want to try matching
5837 another one from the begining of STR. Since we modified it, we
5838 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5839 if (str[0] == 'X')
5840 {
5841 str += 1;
d2e4a39e 5842 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5843 {
5844 if (str[0] != 'n' && str[0] != 'b')
5845 return 0;
5846 str += 1;
5847 }
14f9c5c9 5848 }
babe1480 5849
14f9c5c9
AS
5850 if (str[0] == '\000')
5851 return 1;
babe1480 5852
d2e4a39e 5853 if (str[0] == '_')
14f9c5c9
AS
5854 {
5855 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5856 return 0;
d2e4a39e 5857 if (str[2] == '_')
dda83cd7
SM
5858 {
5859 if (strcmp (str + 3, "JM") == 0)
5860 return 1;
5861 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5862 the LJM suffix in favor of the JM one. But we will
5863 still accept LJM as a valid suffix for a reasonable
5864 amount of time, just to allow ourselves to debug programs
5865 compiled using an older version of GNAT. */
5866 if (strcmp (str + 3, "LJM") == 0)
5867 return 1;
5868 if (str[3] != 'X')
5869 return 0;
5870 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5871 || str[4] == 'U' || str[4] == 'P')
5872 return 1;
5873 if (str[4] == 'R' && str[5] != 'T')
5874 return 1;
5875 return 0;
5876 }
4c4b4cd2 5877 if (!isdigit (str[2]))
dda83cd7 5878 return 0;
4c4b4cd2 5879 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5880 if (!isdigit (str[k]) && str[k] != '_')
5881 return 0;
14f9c5c9
AS
5882 return 1;
5883 }
4c4b4cd2 5884 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5885 {
4c4b4cd2 5886 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5887 if (!isdigit (str[k]) && str[k] != '_')
5888 return 0;
14f9c5c9
AS
5889 return 1;
5890 }
5891 return 0;
5892}
d2e4a39e 5893
aeb5907d
JB
5894/* Return non-zero if the string starting at NAME and ending before
5895 NAME_END contains no capital letters. */
529cad9c
PH
5896
5897static int
5898is_valid_name_for_wild_match (const char *name0)
5899{
f945dedf 5900 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5901 int i;
5902
5823c3ef
JB
5903 /* If the decoded name starts with an angle bracket, it means that
5904 NAME0 does not follow the GNAT encoding format. It should then
5905 not be allowed as a possible wild match. */
5906 if (decoded_name[0] == '<')
5907 return 0;
5908
529cad9c
PH
5909 for (i=0; decoded_name[i] != '\0'; i++)
5910 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5911 return 0;
5912
5913 return 1;
5914}
5915
59c8a30b
JB
5916/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5917 character which could start a simple name. Assumes that *NAMEP points
5918 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5919
14f9c5c9 5920static int
59c8a30b 5921advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5922{
73589123 5923 const char *name = *namep;
5b4ee69b 5924
5823c3ef 5925 while (1)
14f9c5c9 5926 {
59c8a30b 5927 char t0, t1;
73589123
PH
5928
5929 t0 = *name;
5930 if (t0 == '_')
5931 {
5932 t1 = name[1];
5933 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5934 {
5935 name += 1;
61012eef 5936 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5937 break;
5938 else
5939 name += 1;
5940 }
aa27d0b3
JB
5941 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5942 || name[2] == target0))
73589123
PH
5943 {
5944 name += 2;
5945 break;
5946 }
86b44259
TT
5947 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5948 {
5949 /* Names like "pkg__B_N__name", where N is a number, are
5950 block-local. We can handle these by simply skipping
5951 the "B_" here. */
5952 name += 4;
5953 }
73589123
PH
5954 else
5955 return 0;
5956 }
5957 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5958 name += 1;
5959 else
5823c3ef 5960 return 0;
73589123
PH
5961 }
5962
5963 *namep = name;
5964 return 1;
5965}
5966
b5ec771e
PA
5967/* Return true iff NAME encodes a name of the form prefix.PATN.
5968 Ignores any informational suffixes of NAME (i.e., for which
5969 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
5970 simple name. */
73589123 5971
b5ec771e 5972static bool
73589123
PH
5973wild_match (const char *name, const char *patn)
5974{
22e048c9 5975 const char *p;
73589123
PH
5976 const char *name0 = name;
5977
81eaa506
TT
5978 if (startswith (name, "___ghost_"))
5979 name += 9;
5980
73589123
PH
5981 while (1)
5982 {
5983 const char *match = name;
5984
5985 if (*name == *patn)
5986 {
5987 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
5988 if (*p != *name)
5989 break;
5990 if (*p == '\0' && is_name_suffix (name))
b5ec771e 5991 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
5992
5993 if (name[-1] == '_')
5994 name -= 1;
5995 }
5996 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 5997 return false;
96d887e8 5998 }
96d887e8
PH
5999}
6000
d1183b06 6001/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6002 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6003
6004static void
d1183b06 6005ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6006 const struct block *block,
6007 const lookup_name_info &lookup_name,
6c015214 6008 domain_search_flags domain, struct objfile *objfile)
96d887e8 6009{
96d887e8
PH
6010 /* A matching argument symbol, if any. */
6011 struct symbol *arg_sym;
6012 /* Set true when we find a matching non-argument symbol. */
1178743e 6013 bool found_sym;
96d887e8
PH
6014
6015 arg_sym = NULL;
1178743e 6016 found_sym = false;
1c49bb45 6017 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
96d887e8 6018 {
911e1e79 6019 if (sym->matches (domain))
b5ec771e 6020 {
66d7f48f 6021 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6022 {
d9743061 6023 if (sym->is_argument ())
b5ec771e
PA
6024 arg_sym = sym;
6025 else
6026 {
1178743e 6027 found_sym = true;
dae58e04 6028 add_defn_to_vec (result, sym, block);
b5ec771e
PA
6029 }
6030 }
6031 }
96d887e8
PH
6032 }
6033
22cee43f
PMR
6034 /* Handle renamings. */
6035
d1183b06 6036 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6037 found_sym = true;
22cee43f 6038
96d887e8
PH
6039 if (!found_sym && arg_sym != NULL)
6040 {
dae58e04 6041 add_defn_to_vec (result, arg_sym, block);
96d887e8
PH
6042 }
6043
b5ec771e 6044 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6045 {
6046 arg_sym = NULL;
1178743e 6047 found_sym = false;
b5ec771e
PA
6048 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6049 const char *name = ada_lookup_name.c_str ();
6050 size_t name_len = ada_lookup_name.size ();
96d887e8 6051
548a89df 6052 for (struct symbol *sym : block_iterator_range (block))
76a01679 6053 {
911e1e79 6054 if (sym->matches (domain))
dda83cd7
SM
6055 {
6056 int cmp;
6057
6058 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6059 if (cmp == 0)
6060 {
6061 cmp = !startswith (sym->linkage_name (), "_ada_");
6062 if (cmp == 0)
6063 cmp = strncmp (name, sym->linkage_name () + 5,
6064 name_len);
6065 }
6066
6067 if (cmp == 0
6068 && is_name_suffix (sym->linkage_name () + name_len + 5))
6069 {
66d7f48f 6070 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6071 {
d9743061 6072 if (sym->is_argument ())
2a2d4dc3
AS
6073 arg_sym = sym;
6074 else
6075 {
1178743e 6076 found_sym = true;
dae58e04 6077 add_defn_to_vec (result, sym, block);
2a2d4dc3
AS
6078 }
6079 }
dda83cd7
SM
6080 }
6081 }
76a01679 6082 }
96d887e8
PH
6083
6084 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6085 They aren't parameters, right? */
96d887e8 6086 if (!found_sym && arg_sym != NULL)
dda83cd7 6087 {
dae58e04 6088 add_defn_to_vec (result, arg_sym, block);
dda83cd7 6089 }
96d887e8
PH
6090 }
6091}
6092\f
41d27058 6093
dda83cd7 6094 /* Symbol Completion */
41d27058 6095
b5ec771e 6096/* See symtab.h. */
41d27058 6097
b5ec771e
PA
6098bool
6099ada_lookup_name_info::matches
6100 (const char *sym_name,
6101 symbol_name_match_type match_type,
a207cff2 6102 completion_match_result *comp_match_res) const
41d27058 6103{
b5ec771e
PA
6104 bool match = false;
6105 const char *text = m_encoded_name.c_str ();
6106 size_t text_len = m_encoded_name.size ();
41d27058
JB
6107
6108 /* First, test against the fully qualified name of the symbol. */
6109
6110 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6111 match = true;
41d27058 6112
f945dedf 6113 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6114 if (match && !m_encoded_p)
41d27058
JB
6115 {
6116 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6117 that iff we are doing a verbatim match, the decoded version
6118 of the symbol name starts with '<'. Otherwise, this symbol name
6119 is not a suitable completion. */
41d27058 6120
f945dedf 6121 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6122 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6123 }
6124
b5ec771e 6125 if (match && !m_verbatim_p)
41d27058
JB
6126 {
6127 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6128 be done is to verify that the potentially matching symbol name
6129 does not include capital letters, because the ada-mode would
6130 not be able to understand these symbol names without the
6131 angle bracket notation. */
41d27058
JB
6132 const char *tmp;
6133
6134 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6135 if (*tmp != '\0')
b5ec771e 6136 match = false;
41d27058
JB
6137 }
6138
6139 /* Second: Try wild matching... */
6140
b5ec771e 6141 if (!match && m_wild_match_p)
41d27058
JB
6142 {
6143 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6144 may represent an unqualified symbol name. We therefore must
6145 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6146 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6147
6148 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6149 match = true;
41d27058
JB
6150 }
6151
b5ec771e 6152 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6153
6154 if (!match)
b5ec771e 6155 return false;
41d27058 6156
a207cff2 6157 if (comp_match_res != NULL)
b5ec771e 6158 {
a207cff2 6159 std::string &match_str = comp_match_res->match.storage ();
41d27058 6160
b5ec771e 6161 if (!m_encoded_p)
a207cff2 6162 match_str = ada_decode (sym_name);
b5ec771e
PA
6163 else
6164 {
6165 if (m_verbatim_p)
6166 match_str = add_angle_brackets (sym_name);
6167 else
6168 match_str = sym_name;
41d27058 6169
b5ec771e 6170 }
a207cff2
PA
6171
6172 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6173 }
6174
b5ec771e 6175 return true;
41d27058
JB
6176}
6177
dda83cd7 6178 /* Field Access */
96d887e8 6179
73fb9985
JB
6180/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6181 for tagged types. */
6182
6183static int
6184ada_is_dispatch_table_ptr_type (struct type *type)
6185{
0d5cff50 6186 const char *name;
73fb9985 6187
78134374 6188 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6189 return 0;
6190
27710edb 6191 name = type->target_type ()->name ();
73fb9985
JB
6192 if (name == NULL)
6193 return 0;
6194
6195 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6196}
6197
ac4a2da4
JG
6198/* Return non-zero if TYPE is an interface tag. */
6199
6200static int
6201ada_is_interface_tag (struct type *type)
6202{
7d93a1e0 6203 const char *name = type->name ();
ac4a2da4
JG
6204
6205 if (name == NULL)
6206 return 0;
6207
6208 return (strcmp (name, "ada__tags__interface_tag") == 0);
6209}
6210
963a6417
PH
6211/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6212 to be invisible to users. */
96d887e8 6213
963a6417
PH
6214int
6215ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6216{
1f704f76 6217 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6218 return 1;
ffde82bf 6219
73fb9985
JB
6220 /* Check the name of that field. */
6221 {
33d16dd9 6222 const char *name = type->field (field_num).name ();
73fb9985
JB
6223
6224 /* Anonymous field names should not be printed.
6225 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6226 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6227 if (name == NULL)
6228 return 1;
6229
ffde82bf
JB
6230 /* Normally, fields whose name start with an underscore ("_")
6231 are fields that have been internally generated by the compiler,
6232 and thus should not be printed. The "_parent" field is special,
6233 however: This is a field internally generated by the compiler
6234 for tagged types, and it contains the components inherited from
6235 the parent type. This field should not be printed as is, but
6236 should not be ignored either. */
61012eef 6237 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985 6238 return 1;
d537777d
TT
6239
6240 /* The compiler doesn't document this, but sometimes it emits
6241 a field whose name starts with a capital letter, like 'V148s'.
6242 These aren't marked as artificial in any way, but we know they
6243 should be ignored. However, wrapper fields should not be
6244 ignored. */
6245 if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
6246 {
6247 /* Wrapper field. */
6248 }
6249 else if (isupper (name[0]))
6250 return 1;
73fb9985
JB
6251 }
6252
ac4a2da4
JG
6253 /* If this is the dispatch table of a tagged type or an interface tag,
6254 then ignore. */
73fb9985 6255 if (ada_is_tagged_type (type, 1)
940da03e
SM
6256 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6257 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6258 return 1;
6259
6260 /* Not a special field, so it should not be ignored. */
6261 return 0;
963a6417 6262}
96d887e8 6263
963a6417 6264/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6265 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6266
963a6417
PH
6267int
6268ada_is_tagged_type (struct type *type, int refok)
6269{
988f6b3d 6270 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6271}
96d887e8 6272
963a6417 6273/* True iff TYPE represents the type of X'Tag */
96d887e8 6274
963a6417
PH
6275int
6276ada_is_tag_type (struct type *type)
6277{
460efde1
JB
6278 type = ada_check_typedef (type);
6279
78134374 6280 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6281 return 0;
6282 else
96d887e8 6283 {
27710edb 6284 const char *name = ada_type_name (type->target_type ());
5b4ee69b 6285
963a6417 6286 return (name != NULL
dda83cd7 6287 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6288 }
96d887e8
PH
6289}
6290
963a6417 6291/* The type of the tag on VAL. */
76a01679 6292
de93309a 6293static struct type *
963a6417 6294ada_tag_type (struct value *val)
96d887e8 6295{
d0c97917 6296 return ada_lookup_struct_elt_type (val->type (), "_tag", 1, 0);
963a6417 6297}
96d887e8 6298
b50d69b5
JG
6299/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6300 retired at Ada 05). */
6301
6302static int
6303is_ada95_tag (struct value *tag)
6304{
6305 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6306}
6307
963a6417 6308/* The value of the tag on VAL. */
96d887e8 6309
de93309a 6310static struct value *
963a6417
PH
6311ada_value_tag (struct value *val)
6312{
03ee6b2e 6313 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6314}
6315
963a6417
PH
6316/* The value of the tag on the object of type TYPE whose contents are
6317 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6318 ADDRESS. */
96d887e8 6319
963a6417 6320static struct value *
10a2c479 6321value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6322 const gdb_byte *valaddr,
dda83cd7 6323 CORE_ADDR address)
96d887e8 6324{
b5385fc0 6325 int tag_byte_offset;
963a6417 6326 struct type *tag_type;
5b4ee69b 6327
4d1795ac
TT
6328 gdb::array_view<const gdb_byte> contents;
6329 if (valaddr != nullptr)
df86565b 6330 contents = gdb::make_array_view (valaddr, type->length ());
4d1795ac
TT
6331 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6332 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6333 NULL, NULL, NULL))
96d887e8 6334 {
fc1a4b47 6335 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6336 ? NULL
6337 : valaddr + tag_byte_offset);
963a6417 6338 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6339
963a6417 6340 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6341 }
963a6417
PH
6342 return NULL;
6343}
96d887e8 6344
963a6417
PH
6345static struct type *
6346type_from_tag (struct value *tag)
6347{
f5272a3b 6348 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6349
963a6417 6350 if (type_name != NULL)
5c4258f4 6351 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6352 return NULL;
6353}
96d887e8 6354
b50d69b5
JG
6355/* Given a value OBJ of a tagged type, return a value of this
6356 type at the base address of the object. The base address, as
6357 defined in Ada.Tags, it is the address of the primary tag of
6358 the object, and therefore where the field values of its full
6359 view can be fetched. */
6360
6361struct value *
6362ada_tag_value_at_base_address (struct value *obj)
6363{
b50d69b5
JG
6364 struct value *val;
6365 LONGEST offset_to_top = 0;
6366 struct type *ptr_type, *obj_type;
6367 struct value *tag;
6368 CORE_ADDR base_address;
6369
d0c97917 6370 obj_type = obj->type ();
b50d69b5 6371
33b5899f 6372 /* It is the responsibility of the caller to deref pointers. */
b50d69b5 6373
78134374 6374 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6375 return obj;
6376
6377 tag = ada_value_tag (obj);
6378 if (!tag)
6379 return obj;
6380
6381 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6382
6383 if (is_ada95_tag (tag))
6384 return obj;
6385
d537777d
TT
6386 struct type *offset_type
6387 = language_lookup_primitive_type (language_def (language_ada),
99d9c3b9
SM
6388 current_inferior ()->arch (),
6389 "storage_offset");
d537777d 6390 ptr_type = lookup_pointer_type (offset_type);
b50d69b5
JG
6391 val = value_cast (ptr_type, tag);
6392 if (!val)
6393 return obj;
6394
6395 /* It is perfectly possible that an exception be raised while
6396 trying to determine the base address, just like for the tag;
6397 see ada_tag_name for more details. We do not print the error
6398 message for the same reason. */
6399
a70b8144 6400 try
b50d69b5
JG
6401 {
6402 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6403 }
6404
230d2906 6405 catch (const gdb_exception_error &e)
492d29ea
PA
6406 {
6407 return obj;
6408 }
b50d69b5
JG
6409
6410 /* If offset is null, nothing to do. */
6411
6412 if (offset_to_top == 0)
6413 return obj;
6414
6415 /* -1 is a special case in Ada.Tags; however, what should be done
6416 is not quite clear from the documentation. So do nothing for
6417 now. */
6418
6419 if (offset_to_top == -1)
6420 return obj;
6421
d537777d
TT
6422 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6423 top is used. In this situation the offset is stored just after
6424 the tag, in the object itself. */
df86565b 6425 ULONGEST last = (((ULONGEST) 1) << (8 * offset_type->length () - 1)) - 1;
d537777d
TT
6426 if (offset_to_top == last)
6427 {
6428 struct value *tem = value_addr (tag);
6429 tem = value_ptradd (tem, 1);
6430 tem = value_cast (ptr_type, tem);
6431 offset_to_top = value_as_long (value_ind (tem));
6432 }
05527d8c
TV
6433
6434 if (offset_to_top > 0)
d537777d
TT
6435 {
6436 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6437 from the base address. This was however incompatible with
6438 C++ dispatch table: C++ uses a *negative* value to *add*
6439 to the base address. Ada's convention has therefore been
6440 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6441 use the same convention. Here, we support both cases by
6442 checking the sign of OFFSET_TO_TOP. */
6443 offset_to_top = -offset_to_top;
6444 }
08f49010 6445
9feb2d07 6446 base_address = obj->address () + offset_to_top;
b50d69b5
JG
6447 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6448
6449 /* Make sure that we have a proper tag at the new address.
6450 Otherwise, offset_to_top is bogus (which can happen when
6451 the object is not initialized yet). */
6452
6453 if (!tag)
6454 return obj;
6455
6456 obj_type = type_from_tag (tag);
6457
6458 if (!obj_type)
6459 return obj;
6460
6461 return value_from_contents_and_address (obj_type, NULL, base_address);
6462}
6463
1b611343
JB
6464/* Return the "ada__tags__type_specific_data" type. */
6465
6466static struct type *
6467ada_get_tsd_type (struct inferior *inf)
963a6417 6468{
1b611343 6469 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6470
1b611343
JB
6471 if (data->tsd_type == 0)
6472 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6473 return data->tsd_type;
6474}
529cad9c 6475
1b611343
JB
6476/* Return the TSD (type-specific data) associated to the given TAG.
6477 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6478
1b611343 6479 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6480
1b611343
JB
6481static struct value *
6482ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6483{
4c4b4cd2 6484 struct value *val;
1b611343 6485 struct type *type;
5b4ee69b 6486
1b611343
JB
6487 /* First option: The TSD is simply stored as a field of our TAG.
6488 Only older versions of GNAT would use this format, but we have
6489 to test it first, because there are no visible markers for
6490 the current approach except the absence of that field. */
529cad9c 6491
1b611343
JB
6492 val = ada_value_struct_elt (tag, "tsd", 1);
6493 if (val)
6494 return val;
e802dbe0 6495
1b611343
JB
6496 /* Try the second representation for the dispatch table (in which
6497 there is no explicit 'tsd' field in the referent of the tag pointer,
6498 and instead the tsd pointer is stored just before the dispatch
6499 table. */
e802dbe0 6500
1b611343
JB
6501 type = ada_get_tsd_type (current_inferior());
6502 if (type == NULL)
6503 return NULL;
6504 type = lookup_pointer_type (lookup_pointer_type (type));
6505 val = value_cast (type, tag);
6506 if (val == NULL)
6507 return NULL;
6508 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6509}
6510
1b611343
JB
6511/* Given the TSD of a tag (type-specific data), return a string
6512 containing the name of the associated type.
6513
f5272a3b 6514 May return NULL if we are unable to determine the tag name. */
1b611343 6515
f5272a3b 6516static gdb::unique_xmalloc_ptr<char>
1b611343 6517ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6518{
1b611343 6519 struct value *val;
529cad9c 6520
1b611343 6521 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6522 if (val == NULL)
1b611343 6523 return NULL;
66920317
TT
6524 gdb::unique_xmalloc_ptr<char> buffer
6525 = target_read_string (value_as_address (val), INT_MAX);
6526 if (buffer == nullptr)
f5272a3b
TT
6527 return nullptr;
6528
315e4ebb 6529 try
f5272a3b 6530 {
315e4ebb
TT
6531 /* Let this throw an exception on error. If the data is
6532 uninitialized, we'd rather not have the user see a
6533 warning. */
6534 const char *folded = ada_fold_name (buffer.get (), true);
6535 return make_unique_xstrdup (folded);
6536 }
6537 catch (const gdb_exception &)
6538 {
6539 return nullptr;
f5272a3b 6540 }
4c4b4cd2
PH
6541}
6542
6543/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6544 a C string.
6545
6546 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6547 determine the name of that tag. */
4c4b4cd2 6548
f5272a3b 6549gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6550ada_tag_name (struct value *tag)
6551{
f5272a3b 6552 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6553
d0c97917 6554 if (!ada_is_tag_type (tag->type ()))
4c4b4cd2 6555 return NULL;
1b611343
JB
6556
6557 /* It is perfectly possible that an exception be raised while trying
6558 to determine the TAG's name, even under normal circumstances:
6559 The associated variable may be uninitialized or corrupted, for
6560 instance. We do not let any exception propagate past this point.
6561 instead we return NULL.
6562
6563 We also do not print the error message either (which often is very
6564 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6565 the caller print a more meaningful message if necessary. */
a70b8144 6566 try
1b611343
JB
6567 {
6568 struct value *tsd = ada_get_tsd_from_tag (tag);
6569
6570 if (tsd != NULL)
6571 name = ada_tag_name_from_tsd (tsd);
6572 }
230d2906 6573 catch (const gdb_exception_error &e)
492d29ea
PA
6574 {
6575 }
1b611343
JB
6576
6577 return name;
4c4b4cd2
PH
6578}
6579
6580/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6581
d2e4a39e 6582struct type *
ebf56fd3 6583ada_parent_type (struct type *type)
14f9c5c9
AS
6584{
6585 int i;
6586
61ee279c 6587 type = ada_check_typedef (type);
14f9c5c9 6588
78134374 6589 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6590 return NULL;
6591
1f704f76 6592 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6593 if (ada_is_parent_field (type, i))
0c1f74cf 6594 {
dda83cd7 6595 struct type *parent_type = type->field (i).type ();
0c1f74cf 6596
dda83cd7
SM
6597 /* If the _parent field is a pointer, then dereference it. */
6598 if (parent_type->code () == TYPE_CODE_PTR)
27710edb 6599 parent_type = parent_type->target_type ();
dda83cd7
SM
6600 /* If there is a parallel XVS type, get the actual base type. */
6601 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6602
dda83cd7 6603 return ada_check_typedef (parent_type);
0c1f74cf 6604 }
14f9c5c9
AS
6605
6606 return NULL;
6607}
6608
4c4b4cd2
PH
6609/* True iff field number FIELD_NUM of structure type TYPE contains the
6610 parent-type (inherited) fields of a derived type. Assumes TYPE is
6611 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6612
6613int
ebf56fd3 6614ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6615{
33d16dd9 6616 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6617
4c4b4cd2 6618 return (name != NULL
dda83cd7
SM
6619 && (startswith (name, "PARENT")
6620 || startswith (name, "_parent")));
14f9c5c9
AS
6621}
6622
4c4b4cd2 6623/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6624 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6625 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6626 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6627 structures. */
14f9c5c9
AS
6628
6629int
ebf56fd3 6630ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6631{
33d16dd9 6632 const char *name = type->field (field_num).name ();
5b4ee69b 6633
dddc0e16
JB
6634 if (name != NULL && strcmp (name, "RETVAL") == 0)
6635 {
6636 /* This happens in functions with "out" or "in out" parameters
6637 which are passed by copy. For such functions, GNAT describes
6638 the function's return type as being a struct where the return
6639 value is in a field called RETVAL, and where the other "out"
6640 or "in out" parameters are fields of that struct. This is not
6641 a wrapper. */
6642 return 0;
6643 }
6644
d2e4a39e 6645 return (name != NULL
dda83cd7
SM
6646 && (startswith (name, "PARENT")
6647 || strcmp (name, "REP") == 0
6648 || startswith (name, "_parent")
6649 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6650}
6651
4c4b4cd2
PH
6652/* True iff field number FIELD_NUM of structure or union type TYPE
6653 is a variant wrapper. Assumes TYPE is a structure type with at least
6654 FIELD_NUM+1 fields. */
14f9c5c9
AS
6655
6656int
ebf56fd3 6657ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6658{
8ecb59f8
TT
6659 /* Only Ada types are eligible. */
6660 if (!ADA_TYPE_P (type))
6661 return 0;
6662
940da03e 6663 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6664
78134374
SM
6665 return (field_type->code () == TYPE_CODE_UNION
6666 || (is_dynamic_field (type, field_num)
27710edb 6667 && (field_type->target_type ()->code ()
c3e5cd34 6668 == TYPE_CODE_UNION)));
14f9c5c9
AS
6669}
6670
6671/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6672 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6673 returns the type of the controlling discriminant for the variant.
6674 May return NULL if the type could not be found. */
14f9c5c9 6675
d2e4a39e 6676struct type *
ebf56fd3 6677ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6678{
a121b7c1 6679 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6680
988f6b3d 6681 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6682}
6683
4c4b4cd2 6684/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6685 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6686 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6687
de93309a 6688static int
ebf56fd3 6689ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6690{
33d16dd9 6691 const char *name = type->field (field_num).name ();
5b4ee69b 6692
14f9c5c9
AS
6693 return (name != NULL && name[0] == 'O');
6694}
6695
6696/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6697 returns the name of the discriminant controlling the variant.
6698 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6699
a121b7c1 6700const char *
ebf56fd3 6701ada_variant_discrim_name (struct type *type0)
14f9c5c9 6702{
5f9febe0 6703 static std::string result;
d2e4a39e
AS
6704 struct type *type;
6705 const char *name;
6706 const char *discrim_end;
6707 const char *discrim_start;
14f9c5c9 6708
78134374 6709 if (type0->code () == TYPE_CODE_PTR)
27710edb 6710 type = type0->target_type ();
14f9c5c9
AS
6711 else
6712 type = type0;
6713
6714 name = ada_type_name (type);
6715
6716 if (name == NULL || name[0] == '\000')
6717 return "";
6718
6719 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6720 discrim_end -= 1)
6721 {
61012eef 6722 if (startswith (discrim_end, "___XVN"))
dda83cd7 6723 break;
14f9c5c9
AS
6724 }
6725 if (discrim_end == name)
6726 return "";
6727
d2e4a39e 6728 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6729 discrim_start -= 1)
6730 {
d2e4a39e 6731 if (discrim_start == name + 1)
dda83cd7 6732 return "";
76a01679 6733 if ((discrim_start > name + 3
dda83cd7
SM
6734 && startswith (discrim_start - 3, "___"))
6735 || discrim_start[-1] == '.')
6736 break;
14f9c5c9
AS
6737 }
6738
5f9febe0
TT
6739 result = std::string (discrim_start, discrim_end - discrim_start);
6740 return result.c_str ();
14f9c5c9
AS
6741}
6742
4c4b4cd2
PH
6743/* Scan STR for a subtype-encoded number, beginning at position K.
6744 Put the position of the character just past the number scanned in
6745 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6746 Return 1 if there was a valid number at the given position, and 0
6747 otherwise. A "subtype-encoded" number consists of the absolute value
6748 in decimal, followed by the letter 'm' to indicate a negative number.
6749 Assumes 0m does not occur. */
14f9c5c9
AS
6750
6751int
d2e4a39e 6752ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6753{
6754 ULONGEST RU;
6755
d2e4a39e 6756 if (!isdigit (str[k]))
14f9c5c9
AS
6757 return 0;
6758
4c4b4cd2 6759 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6760 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6761 LONGEST. */
14f9c5c9
AS
6762 RU = 0;
6763 while (isdigit (str[k]))
6764 {
d2e4a39e 6765 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6766 k += 1;
6767 }
6768
d2e4a39e 6769 if (str[k] == 'm')
14f9c5c9
AS
6770 {
6771 if (R != NULL)
dda83cd7 6772 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6773 k += 1;
6774 }
6775 else if (R != NULL)
6776 *R = (LONGEST) RU;
6777
4c4b4cd2 6778 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6779 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6780 number representable as a LONGEST (although either would probably work
6781 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6782 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6783
6784 if (new_k != NULL)
6785 *new_k = k;
6786 return 1;
6787}
6788
4c4b4cd2
PH
6789/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6790 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6791 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6792
de93309a 6793static int
ebf56fd3 6794ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6795{
33d16dd9 6796 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6797 int p;
6798
6799 p = 0;
6800 while (1)
6801 {
d2e4a39e 6802 switch (name[p])
dda83cd7
SM
6803 {
6804 case '\0':
6805 return 0;
6806 case 'S':
6807 {
6808 LONGEST W;
6809
6810 if (!ada_scan_number (name, p + 1, &W, &p))
6811 return 0;
6812 if (val == W)
6813 return 1;
6814 break;
6815 }
6816 case 'R':
6817 {
6818 LONGEST L, U;
6819
6820 if (!ada_scan_number (name, p + 1, &L, &p)
6821 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6822 return 0;
6823 if (val >= L && val <= U)
6824 return 1;
6825 break;
6826 }
6827 case 'O':
6828 return 1;
6829 default:
6830 return 0;
6831 }
4c4b4cd2
PH
6832 }
6833}
6834
0963b4bd 6835/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6836
6837/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6838 ARG_TYPE, extract and return the value of one of its (non-static)
6839 fields. FIELDNO says which field. Differs from value_primitive_field
6840 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6841
5eb68a39 6842struct value *
d2e4a39e 6843ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6844 struct type *arg_type)
14f9c5c9 6845{
14f9c5c9
AS
6846 struct type *type;
6847
61ee279c 6848 arg_type = ada_check_typedef (arg_type);
940da03e 6849 type = arg_type->field (fieldno).type ();
14f9c5c9 6850
4504bbde
TT
6851 /* Handle packed fields. It might be that the field is not packed
6852 relative to its containing structure, but the structure itself is
6853 packed; in this case we must take the bit-field path. */
3757d2d4 6854 if (arg_type->field (fieldno).bitsize () != 0 || arg1->bitpos () != 0)
14f9c5c9 6855 {
b610c045 6856 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
3757d2d4 6857 int bit_size = arg_type->field (fieldno).bitsize ();
d2e4a39e 6858
50888e42 6859 return ada_value_primitive_packed_val (arg1,
efaf1ae0 6860 arg1->contents ().data (),
dda83cd7
SM
6861 offset + bit_pos / 8,
6862 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6863 }
6864 else
6c49729e 6865 return arg1->primitive_field (offset, fieldno, arg_type);
14f9c5c9
AS
6866}
6867
52ce6436
PH
6868/* Find field with name NAME in object of type TYPE. If found,
6869 set the following for each argument that is non-null:
6870 - *FIELD_TYPE_P to the field's type;
6871 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6872 an object of that type;
6873 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6874 - *BIT_SIZE_P to its size in bits if the field is packed, and
6875 0 otherwise;
6876 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6877 fields up to but not including the desired field, or by the total
6878 number of fields if not found. A NULL value of NAME never
6879 matches; the function just counts visible fields in this case.
6880
828d5846
XR
6881 Notice that we need to handle when a tagged record hierarchy
6882 has some components with the same name, like in this scenario:
6883
6884 type Top_T is tagged record
dda83cd7
SM
6885 N : Integer := 1;
6886 U : Integer := 974;
6887 A : Integer := 48;
828d5846
XR
6888 end record;
6889
6890 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6891 N : Character := 'a';
6892 C : Integer := 3;
828d5846
XR
6893 end record;
6894
6895 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6896 N : Float := 4.0;
6897 C : Character := '5';
6898 X : Integer := 6;
6899 A : Character := 'J';
828d5846
XR
6900 end record;
6901
6902 Let's say we now have a variable declared and initialized as follow:
6903
6904 TC : Top_A := new Bottom_T;
6905
6906 And then we use this variable to call this function
6907
6908 procedure Assign (Obj: in out Top_T; TV : Integer);
6909
6910 as follow:
6911
6912 Assign (Top_T (B), 12);
6913
6914 Now, we're in the debugger, and we're inside that procedure
6915 then and we want to print the value of obj.c:
6916
6917 Usually, the tagged record or one of the parent type owns the
6918 component to print and there's no issue but in this particular
6919 case, what does it mean to ask for Obj.C? Since the actual
6920 type for object is type Bottom_T, it could mean two things: type
6921 component C from the Middle_T view, but also component C from
6922 Bottom_T. So in that "undefined" case, when the component is
6923 not found in the non-resolved type (which includes all the
6924 components of the parent type), then resolve it and see if we
6925 get better luck once expanded.
6926
6927 In the case of homonyms in the derived tagged type, we don't
6928 guaranty anything, and pick the one that's easiest for us
6929 to program.
6930
0963b4bd 6931 Returns 1 if found, 0 otherwise. */
52ce6436 6932
4c4b4cd2 6933static int
0d5cff50 6934find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6935 struct type **field_type_p,
6936 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6937 int *index_p)
4c4b4cd2
PH
6938{
6939 int i;
828d5846 6940 int parent_offset = -1;
4c4b4cd2 6941
61ee279c 6942 type = ada_check_typedef (type);
76a01679 6943
52ce6436
PH
6944 if (field_type_p != NULL)
6945 *field_type_p = NULL;
6946 if (byte_offset_p != NULL)
d5d6fca5 6947 *byte_offset_p = 0;
52ce6436
PH
6948 if (bit_offset_p != NULL)
6949 *bit_offset_p = 0;
6950 if (bit_size_p != NULL)
6951 *bit_size_p = 0;
6952
1f704f76 6953 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 6954 {
4d1795ac
TT
6955 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6956 type. However, we only need the values to be correct when
6957 the caller asks for them. */
6958 int bit_pos = 0, fld_offset = 0;
6959 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6960 {
b610c045 6961 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
6962 fld_offset = offset + bit_pos / 8;
6963 }
6964
33d16dd9 6965 const char *t_field_name = type->field (i).name ();
76a01679 6966
4c4b4cd2 6967 if (t_field_name == NULL)
dda83cd7 6968 continue;
4c4b4cd2 6969
828d5846 6970 else if (ada_is_parent_field (type, i))
dda83cd7 6971 {
828d5846
XR
6972 /* This is a field pointing us to the parent type of a tagged
6973 type. As hinted in this function's documentation, we give
6974 preference to fields in the current record first, so what
6975 we do here is just record the index of this field before
6976 we skip it. If it turns out we couldn't find our field
6977 in the current record, then we'll get back to it and search
6978 inside it whether the field might exist in the parent. */
6979
dda83cd7
SM
6980 parent_offset = i;
6981 continue;
6982 }
828d5846 6983
52ce6436 6984 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7 6985 {
3757d2d4 6986 int bit_size = type->field (i).bitsize ();
5b4ee69b 6987
52ce6436 6988 if (field_type_p != NULL)
940da03e 6989 *field_type_p = type->field (i).type ();
52ce6436
PH
6990 if (byte_offset_p != NULL)
6991 *byte_offset_p = fld_offset;
6992 if (bit_offset_p != NULL)
6993 *bit_offset_p = bit_pos % 8;
6994 if (bit_size_p != NULL)
6995 *bit_size_p = bit_size;
dda83cd7
SM
6996 return 1;
6997 }
4c4b4cd2 6998 else if (ada_is_wrapper_field (type, i))
dda83cd7 6999 {
940da03e 7000 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7001 field_type_p, byte_offset_p, bit_offset_p,
7002 bit_size_p, index_p))
dda83cd7
SM
7003 return 1;
7004 }
4c4b4cd2 7005 else if (ada_is_variant_part (type, i))
dda83cd7 7006 {
52ce6436
PH
7007 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7008 fixed type?? */
dda83cd7
SM
7009 int j;
7010 struct type *field_type
940da03e 7011 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7012
dda83cd7
SM
7013 for (j = 0; j < field_type->num_fields (); j += 1)
7014 {
7015 if (find_struct_field (name, field_type->field (j).type (),
7016 fld_offset
b610c045 7017 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7018 field_type_p, byte_offset_p,
7019 bit_offset_p, bit_size_p, index_p))
7020 return 1;
7021 }
7022 }
52ce6436
PH
7023 else if (index_p != NULL)
7024 *index_p += 1;
4c4b4cd2 7025 }
828d5846
XR
7026
7027 /* Field not found so far. If this is a tagged type which
7028 has a parent, try finding that field in the parent now. */
7029
7030 if (parent_offset != -1)
7031 {
4d1795ac
TT
7032 /* As above, only compute the offset when truly needed. */
7033 int fld_offset = offset;
7034 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7035 {
b610c045 7036 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7037 fld_offset += bit_pos / 8;
7038 }
828d5846 7039
940da03e 7040 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7041 fld_offset, field_type_p, byte_offset_p,
7042 bit_offset_p, bit_size_p, index_p))
7043 return 1;
828d5846
XR
7044 }
7045
4c4b4cd2
PH
7046 return 0;
7047}
7048
0963b4bd 7049/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7050
52ce6436
PH
7051static int
7052num_visible_fields (struct type *type)
7053{
7054 int n;
5b4ee69b 7055
52ce6436
PH
7056 n = 0;
7057 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7058 return n;
7059}
14f9c5c9 7060
4c4b4cd2 7061/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7062 and search in it assuming it has (class) type TYPE.
7063 If found, return value, else return NULL.
7064
828d5846
XR
7065 Searches recursively through wrapper fields (e.g., '_parent').
7066
7067 In the case of homonyms in the tagged types, please refer to the
7068 long explanation in find_struct_field's function documentation. */
14f9c5c9 7069
4c4b4cd2 7070static struct value *
108d56a4 7071ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7072 struct type *type)
14f9c5c9
AS
7073{
7074 int i;
828d5846 7075 int parent_offset = -1;
14f9c5c9 7076
5b4ee69b 7077 type = ada_check_typedef (type);
1f704f76 7078 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7079 {
33d16dd9 7080 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7081
7082 if (t_field_name == NULL)
dda83cd7 7083 continue;
14f9c5c9 7084
828d5846 7085 else if (ada_is_parent_field (type, i))
dda83cd7 7086 {
828d5846
XR
7087 /* This is a field pointing us to the parent type of a tagged
7088 type. As hinted in this function's documentation, we give
7089 preference to fields in the current record first, so what
7090 we do here is just record the index of this field before
7091 we skip it. If it turns out we couldn't find our field
7092 in the current record, then we'll get back to it and search
7093 inside it whether the field might exist in the parent. */
7094
dda83cd7
SM
7095 parent_offset = i;
7096 continue;
7097 }
828d5846 7098
14f9c5c9 7099 else if (field_name_match (t_field_name, name))
dda83cd7 7100 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7101
7102 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7103 {
7104 struct value *v = /* Do not let indent join lines here. */
7105 ada_search_struct_field (name, arg,
b610c045 7106 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7107 type->field (i).type ());
5b4ee69b 7108
dda83cd7
SM
7109 if (v != NULL)
7110 return v;
7111 }
14f9c5c9
AS
7112
7113 else if (ada_is_variant_part (type, i))
dda83cd7 7114 {
0963b4bd 7115 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7116 int j;
7117 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7118 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7119
dda83cd7
SM
7120 for (j = 0; j < field_type->num_fields (); j += 1)
7121 {
7122 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7123 break. */
dda83cd7 7124 (name, arg,
b610c045 7125 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7126 field_type->field (j).type ());
5b4ee69b 7127
dda83cd7
SM
7128 if (v != NULL)
7129 return v;
7130 }
7131 }
14f9c5c9 7132 }
828d5846
XR
7133
7134 /* Field not found so far. If this is a tagged type which
7135 has a parent, try finding that field in the parent now. */
7136
7137 if (parent_offset != -1)
7138 {
7139 struct value *v = ada_search_struct_field (
b610c045 7140 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7141 type->field (parent_offset).type ());
828d5846
XR
7142
7143 if (v != NULL)
dda83cd7 7144 return v;
828d5846
XR
7145 }
7146
14f9c5c9
AS
7147 return NULL;
7148}
d2e4a39e 7149
52ce6436
PH
7150static struct value *ada_index_struct_field_1 (int *, struct value *,
7151 int, struct type *);
7152
7153
7154/* Return field #INDEX in ARG, where the index is that returned by
7155 * find_struct_field through its INDEX_P argument. Adjust the address
7156 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7157 * If found, return value, else return NULL. */
52ce6436
PH
7158
7159static struct value *
7160ada_index_struct_field (int index, struct value *arg, int offset,
7161 struct type *type)
7162{
7163 return ada_index_struct_field_1 (&index, arg, offset, type);
7164}
7165
7166
7167/* Auxiliary function for ada_index_struct_field. Like
7168 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7169 * *INDEX_P. */
52ce6436
PH
7170
7171static struct value *
7172ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7173 struct type *type)
7174{
7175 int i;
7176 type = ada_check_typedef (type);
7177
1f704f76 7178 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7179 {
33d16dd9 7180 if (type->field (i).name () == NULL)
dda83cd7 7181 continue;
52ce6436 7182 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7183 {
7184 struct value *v = /* Do not let indent join lines here. */
7185 ada_index_struct_field_1 (index_p, arg,
b610c045 7186 offset + type->field (i).loc_bitpos () / 8,
940da03e 7187 type->field (i).type ());
5b4ee69b 7188
dda83cd7
SM
7189 if (v != NULL)
7190 return v;
7191 }
52ce6436
PH
7192
7193 else if (ada_is_variant_part (type, i))
dda83cd7 7194 {
52ce6436 7195 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7196 find_struct_field. */
52ce6436 7197 error (_("Cannot assign this kind of variant record"));
dda83cd7 7198 }
52ce6436 7199 else if (*index_p == 0)
dda83cd7 7200 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7201 else
7202 *index_p -= 1;
7203 }
7204 return NULL;
7205}
7206
3b4de39c 7207/* Return a string representation of type TYPE. */
99bbb428 7208
3b4de39c 7209static std::string
99bbb428
PA
7210type_as_string (struct type *type)
7211{
d7e74731 7212 string_file tmp_stream;
99bbb428 7213
d7e74731 7214 type_print (type, "", &tmp_stream, -1);
99bbb428 7215
5d10a204 7216 return tmp_stream.release ();
99bbb428
PA
7217}
7218
14f9c5c9 7219/* Given a type TYPE, look up the type of the component of type named NAME.
14f9c5c9
AS
7220
7221 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7222 followed by "___".
14f9c5c9 7223
0963b4bd 7224 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7225 be a (pointer or reference)+ to a struct or union, and the
7226 ultimate target type will be searched.
14f9c5c9
AS
7227
7228 Looks recursively into variant clauses and parent types.
7229
828d5846
XR
7230 In the case of homonyms in the tagged types, please refer to the
7231 long explanation in find_struct_field's function documentation.
7232
4c4b4cd2
PH
7233 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7234 TYPE is not a type of the right kind. */
14f9c5c9 7235
4c4b4cd2 7236static struct type *
a121b7c1 7237ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7238 int noerr)
14f9c5c9 7239{
14f9c5c9
AS
7240 if (name == NULL)
7241 goto BadName;
7242
76a01679 7243 if (refok && type != NULL)
4c4b4cd2
PH
7244 while (1)
7245 {
dda83cd7
SM
7246 type = ada_check_typedef (type);
7247 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7248 break;
27710edb 7249 type = type->target_type ();
4c4b4cd2 7250 }
14f9c5c9 7251
76a01679 7252 if (type == NULL
78134374
SM
7253 || (type->code () != TYPE_CODE_STRUCT
7254 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7255 {
4c4b4cd2 7256 if (noerr)
dda83cd7 7257 return NULL;
99bbb428 7258
3b4de39c
PA
7259 error (_("Type %s is not a structure or union type"),
7260 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7261 }
7262
7263 type = to_static_fixed_type (type);
7264
f0874f41
TT
7265 struct type *result;
7266 find_struct_field (name, type, 0, &result, nullptr, nullptr, nullptr,
7267 nullptr);
7268 if (result != nullptr)
7269 return result;
828d5846 7270
14f9c5c9 7271BadName:
d2e4a39e 7272 if (!noerr)
14f9c5c9 7273 {
2b2798cc 7274 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7275
7276 error (_("Type %s has no component named %s"),
3b4de39c 7277 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7278 }
7279
7280 return NULL;
7281}
7282
b1f33ddd
JB
7283/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7284 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7285 represents an unchecked union (that is, the variant part of a
0963b4bd 7286 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7287
7288static int
7289is_unchecked_variant (struct type *var_type, struct type *outer_type)
7290{
a121b7c1 7291 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7292
988f6b3d 7293 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7294}
7295
7296
14f9c5c9 7297/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7298 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7299 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7300
d2e4a39e 7301int
d8af9068 7302ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7303{
7304 int others_clause;
7305 int i;
a121b7c1 7306 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7307 struct value *discrim;
14f9c5c9
AS
7308 LONGEST discrim_val;
7309
012370f6
TT
7310 /* Using plain value_from_contents_and_address here causes problems
7311 because we will end up trying to resolve a type that is currently
7312 being constructed. */
0c281816
JB
7313 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7314 if (discrim == NULL)
14f9c5c9 7315 return -1;
0c281816 7316 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7317
7318 others_clause = -1;
1f704f76 7319 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7320 {
7321 if (ada_is_others_clause (var_type, i))
dda83cd7 7322 others_clause = i;
14f9c5c9 7323 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7324 return i;
14f9c5c9
AS
7325 }
7326
7327 return others_clause;
7328}
d2e4a39e 7329\f
14f9c5c9
AS
7330
7331
dda83cd7 7332 /* Dynamic-Sized Records */
14f9c5c9
AS
7333
7334/* Strategy: The type ostensibly attached to a value with dynamic size
7335 (i.e., a size that is not statically recorded in the debugging
7336 data) does not accurately reflect the size or layout of the value.
7337 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7338 conventional types that are constructed on the fly. */
14f9c5c9
AS
7339
7340/* There is a subtle and tricky problem here. In general, we cannot
7341 determine the size of dynamic records without its data. However,
7342 the 'struct value' data structure, which GDB uses to represent
7343 quantities in the inferior process (the target), requires the size
7344 of the type at the time of its allocation in order to reserve space
7345 for GDB's internal copy of the data. That's why the
7346 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7347 rather than struct value*s.
14f9c5c9
AS
7348
7349 However, GDB's internal history variables ($1, $2, etc.) are
7350 struct value*s containing internal copies of the data that are not, in
7351 general, the same as the data at their corresponding addresses in
7352 the target. Fortunately, the types we give to these values are all
7353 conventional, fixed-size types (as per the strategy described
7354 above), so that we don't usually have to perform the
7355 'to_fixed_xxx_type' conversions to look at their values.
7356 Unfortunately, there is one exception: if one of the internal
7357 history variables is an array whose elements are unconstrained
7358 records, then we will need to create distinct fixed types for each
7359 element selected. */
7360
7361/* The upshot of all of this is that many routines take a (type, host
7362 address, target address) triple as arguments to represent a value.
7363 The host address, if non-null, is supposed to contain an internal
7364 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7365 target at the target address. */
14f9c5c9
AS
7366
7367/* Assuming that VAL0 represents a pointer value, the result of
7368 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7369 dynamic-sized types. */
14f9c5c9 7370
d2e4a39e
AS
7371struct value *
7372ada_value_ind (struct value *val0)
14f9c5c9 7373{
c48db5ca 7374 struct value *val = value_ind (val0);
5b4ee69b 7375
d0c97917 7376 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7377 val = ada_tag_value_at_base_address (val);
7378
4c4b4cd2 7379 return ada_to_fixed_value (val);
14f9c5c9
AS
7380}
7381
7382/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7383 qualifiers on VAL0. */
7384
d2e4a39e
AS
7385static struct value *
7386ada_coerce_ref (struct value *val0)
7387{
d0c97917 7388 if (val0->type ()->code () == TYPE_CODE_REF)
d2e4a39e
AS
7389 {
7390 struct value *val = val0;
5b4ee69b 7391
994b9211 7392 val = coerce_ref (val);
b50d69b5 7393
d0c97917 7394 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7395 val = ada_tag_value_at_base_address (val);
7396
4c4b4cd2 7397 return ada_to_fixed_value (val);
d2e4a39e
AS
7398 }
7399 else
14f9c5c9
AS
7400 return val0;
7401}
7402
4c4b4cd2 7403/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7404
7405static unsigned int
ebf56fd3 7406field_alignment (struct type *type, int f)
14f9c5c9 7407{
33d16dd9 7408 const char *name = type->field (f).name ();
64a1bf19 7409 int len;
14f9c5c9
AS
7410 int align_offset;
7411
64a1bf19
JB
7412 /* The field name should never be null, unless the debugging information
7413 is somehow malformed. In this case, we assume the field does not
7414 require any alignment. */
7415 if (name == NULL)
7416 return 1;
7417
7418 len = strlen (name);
7419
4c4b4cd2
PH
7420 if (!isdigit (name[len - 1]))
7421 return 1;
14f9c5c9 7422
d2e4a39e 7423 if (isdigit (name[len - 2]))
14f9c5c9
AS
7424 align_offset = len - 2;
7425 else
7426 align_offset = len - 1;
7427
61012eef 7428 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7429 return TARGET_CHAR_BIT;
7430
4c4b4cd2
PH
7431 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7432}
7433
852dff6c 7434/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7435
852dff6c
JB
7436static struct symbol *
7437ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7438{
7439 struct symbol *sym;
7440
6c015214 7441 sym = standard_lookup (name, get_selected_block (NULL), SEARCH_VFT);
66d7f48f 7442 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7443 return sym;
7444
6c015214 7445 sym = standard_lookup (name, NULL, SEARCH_STRUCT_DOMAIN);
4186eb54 7446 return sym;
14f9c5c9
AS
7447}
7448
dddfab26
UW
7449/* Find a type named NAME. Ignores ambiguity. This routine will look
7450 solely for types defined by debug info, it will not search the GDB
7451 primitive types. */
4c4b4cd2 7452
852dff6c 7453static struct type *
ebf56fd3 7454ada_find_any_type (const char *name)
14f9c5c9 7455{
852dff6c 7456 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7457
14f9c5c9 7458 if (sym != NULL)
5f9c5a63 7459 return sym->type ();
14f9c5c9 7460
dddfab26 7461 return NULL;
14f9c5c9
AS
7462}
7463
739593e0
JB
7464/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7465 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7466 symbol, in which case it is returned. Otherwise, this looks for
7467 symbols whose name is that of NAME_SYM suffixed with "___XR".
7468 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7469
c0e70c62
TT
7470static bool
7471ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7472{
987012b8 7473 const char *name = name_sym->linkage_name ();
c0e70c62 7474 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7475}
7476
14f9c5c9 7477/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7478 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7479 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7480 otherwise return 0. */
7481
14f9c5c9 7482int
d2e4a39e 7483ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7484{
7485 if (type1 == NULL)
7486 return 1;
7487 else if (type0 == NULL)
7488 return 0;
78134374 7489 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7490 return 1;
78134374 7491 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7492 return 0;
7d93a1e0 7493 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7494 return 1;
ad82864c 7495 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7496 return 1;
4c4b4cd2 7497 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7498 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7499 return 1;
aeb5907d
JB
7500 else
7501 {
7d93a1e0
SM
7502 const char *type0_name = type0->name ();
7503 const char *type1_name = type1->name ();
aeb5907d
JB
7504
7505 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7506 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7507 return 1;
7508 }
14f9c5c9
AS
7509 return 0;
7510}
7511
e86ca25f
TT
7512/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7513 null. */
4c4b4cd2 7514
0d5cff50 7515const char *
d2e4a39e 7516ada_type_name (struct type *type)
14f9c5c9 7517{
d2e4a39e 7518 if (type == NULL)
14f9c5c9 7519 return NULL;
7d93a1e0 7520 return type->name ();
14f9c5c9
AS
7521}
7522
b4ba55a1
JB
7523/* Search the list of "descriptive" types associated to TYPE for a type
7524 whose name is NAME. */
7525
7526static struct type *
7527find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7528{
931e5bc3 7529 struct type *result, *tmp;
b4ba55a1 7530
c6044dd1
JB
7531 if (ada_ignore_descriptive_types_p)
7532 return NULL;
7533
b4ba55a1
JB
7534 /* If there no descriptive-type info, then there is no parallel type
7535 to be found. */
7536 if (!HAVE_GNAT_AUX_INFO (type))
7537 return NULL;
7538
7539 result = TYPE_DESCRIPTIVE_TYPE (type);
7540 while (result != NULL)
7541 {
0d5cff50 7542 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7543
7544 if (result_name == NULL)
dda83cd7
SM
7545 {
7546 warning (_("unexpected null name on descriptive type"));
7547 return NULL;
7548 }
b4ba55a1
JB
7549
7550 /* If the names match, stop. */
7551 if (strcmp (result_name, name) == 0)
7552 break;
7553
7554 /* Otherwise, look at the next item on the list, if any. */
7555 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7556 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7557 else
7558 tmp = NULL;
7559
7560 /* If not found either, try after having resolved the typedef. */
7561 if (tmp != NULL)
7562 result = tmp;
b4ba55a1 7563 else
931e5bc3 7564 {
f168693b 7565 result = check_typedef (result);
931e5bc3
JG
7566 if (HAVE_GNAT_AUX_INFO (result))
7567 result = TYPE_DESCRIPTIVE_TYPE (result);
7568 else
7569 result = NULL;
7570 }
b4ba55a1
JB
7571 }
7572
7573 /* If we didn't find a match, see whether this is a packed array. With
7574 older compilers, the descriptive type information is either absent or
7575 irrelevant when it comes to packed arrays so the above lookup fails.
7576 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7577 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7578 return ada_find_any_type (name);
7579
7580 return result;
7581}
7582
7583/* Find a parallel type to TYPE with the specified NAME, using the
7584 descriptive type taken from the debugging information, if available,
7585 and otherwise using the (slower) name-based method. */
7586
7587static struct type *
7588ada_find_parallel_type_with_name (struct type *type, const char *name)
7589{
7590 struct type *result = NULL;
7591
7592 if (HAVE_GNAT_AUX_INFO (type))
7593 result = find_parallel_type_by_descriptive_type (type, name);
7594 else
7595 result = ada_find_any_type (name);
7596
7597 return result;
7598}
7599
7600/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7601 SUFFIX to the name of TYPE. */
14f9c5c9 7602
d2e4a39e 7603struct type *
ebf56fd3 7604ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7605{
0d5cff50 7606 char *name;
fe978cb0 7607 const char *type_name = ada_type_name (type);
14f9c5c9 7608 int len;
d2e4a39e 7609
fe978cb0 7610 if (type_name == NULL)
14f9c5c9
AS
7611 return NULL;
7612
fe978cb0 7613 len = strlen (type_name);
14f9c5c9 7614
b4ba55a1 7615 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7616
fe978cb0 7617 strcpy (name, type_name);
14f9c5c9
AS
7618 strcpy (name + len, suffix);
7619
b4ba55a1 7620 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7621}
7622
14f9c5c9 7623/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7624 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7625
d2e4a39e
AS
7626static struct type *
7627dynamic_template_type (struct type *type)
14f9c5c9 7628{
61ee279c 7629 type = ada_check_typedef (type);
14f9c5c9 7630
78134374 7631 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7632 || ada_type_name (type) == NULL)
14f9c5c9 7633 return NULL;
d2e4a39e 7634 else
14f9c5c9
AS
7635 {
7636 int len = strlen (ada_type_name (type));
5b4ee69b 7637
4c4b4cd2 7638 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7639 return type;
14f9c5c9 7640 else
dda83cd7 7641 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7642 }
7643}
7644
7645/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7646 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7647
d2e4a39e
AS
7648static int
7649is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7650{
33d16dd9 7651 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7652
d2e4a39e 7653 return name != NULL
940da03e 7654 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7655 && strstr (name, "___XVL") != NULL;
7656}
7657
4c4b4cd2
PH
7658/* The index of the variant field of TYPE, or -1 if TYPE does not
7659 represent a variant record type. */
14f9c5c9 7660
d2e4a39e 7661static int
4c4b4cd2 7662variant_field_index (struct type *type)
14f9c5c9
AS
7663{
7664 int f;
7665
78134374 7666 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7667 return -1;
7668
1f704f76 7669 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7670 {
7671 if (ada_is_variant_part (type, f))
dda83cd7 7672 return f;
4c4b4cd2
PH
7673 }
7674 return -1;
14f9c5c9
AS
7675}
7676
4c4b4cd2
PH
7677/* A record type with no fields. */
7678
d2e4a39e 7679static struct type *
fe978cb0 7680empty_record (struct type *templ)
14f9c5c9 7681{
9fa83a7a 7682 struct type *type = type_allocator (templ).new_type ();
5b4ee69b 7683
67607e24 7684 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7685 INIT_NONE_SPECIFIC (type);
d0e39ea2 7686 type->set_name ("<empty>");
b6cdbc9a 7687 type->set_length (0);
14f9c5c9
AS
7688 return type;
7689}
7690
7691/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7692 the value of type TYPE at VALADDR or ADDRESS (see comments at
7693 the beginning of this section) VAL according to GNAT conventions.
7694 DVAL0 should describe the (portion of a) record that contains any
d0c97917 7695 necessary discriminants. It should be NULL if VAL->type () is
14f9c5c9
AS
7696 an outer-level type (i.e., as opposed to a branch of a variant.) A
7697 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7698 of the variant.
14f9c5c9 7699
4c4b4cd2
PH
7700 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7701 length are not statically known are discarded. As a consequence,
7702 VALADDR, ADDRESS and DVAL0 are ignored.
7703
7704 NOTE: Limitations: For now, we assume that dynamic fields and
7705 variants occupy whole numbers of bytes. However, they need not be
7706 byte-aligned. */
7707
7708struct type *
10a2c479 7709ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7710 const gdb_byte *valaddr,
dda83cd7
SM
7711 CORE_ADDR address, struct value *dval0,
7712 int keep_dynamic_fields)
14f9c5c9 7713{
d2e4a39e
AS
7714 struct value *dval;
7715 struct type *rtype;
14f9c5c9 7716 int nfields, bit_len;
4c4b4cd2 7717 int variant_field;
14f9c5c9 7718 long off;
d94e4f4f 7719 int fld_bit_len;
14f9c5c9
AS
7720 int f;
7721
65558ca5
TT
7722 scoped_value_mark mark;
7723
4c4b4cd2
PH
7724 /* Compute the number of fields in this record type that are going
7725 to be processed: unless keep_dynamic_fields, this includes only
7726 fields whose position and length are static will be processed. */
7727 if (keep_dynamic_fields)
1f704f76 7728 nfields = type->num_fields ();
4c4b4cd2
PH
7729 else
7730 {
7731 nfields = 0;
1f704f76 7732 while (nfields < type->num_fields ()
dda83cd7
SM
7733 && !ada_is_variant_part (type, nfields)
7734 && !is_dynamic_field (type, nfields))
7735 nfields++;
4c4b4cd2
PH
7736 }
7737
9fa83a7a 7738 rtype = type_allocator (type).new_type ();
67607e24 7739 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7740 INIT_NONE_SPECIFIC (rtype);
2774f2da 7741 rtype->alloc_fields (nfields);
d0e39ea2 7742 rtype->set_name (ada_type_name (type));
9cdd0d12 7743 rtype->set_is_fixed_instance (true);
14f9c5c9 7744
d2e4a39e
AS
7745 off = 0;
7746 bit_len = 0;
4c4b4cd2
PH
7747 variant_field = -1;
7748
14f9c5c9
AS
7749 for (f = 0; f < nfields; f += 1)
7750 {
a89febbd 7751 off = align_up (off, field_alignment (type, f))
b610c045 7752 + type->field (f).loc_bitpos ();
cd3f655c 7753 rtype->field (f).set_loc_bitpos (off);
886176b8 7754 rtype->field (f).set_bitsize (0);
14f9c5c9 7755
d2e4a39e 7756 if (ada_is_variant_part (type, f))
dda83cd7
SM
7757 {
7758 variant_field = f;
7759 fld_bit_len = 0;
7760 }
14f9c5c9 7761 else if (is_dynamic_field (type, f))
dda83cd7 7762 {
284614f0
JB
7763 const gdb_byte *field_valaddr = valaddr;
7764 CORE_ADDR field_address = address;
27710edb 7765 struct type *field_type = type->field (f).type ()->target_type ();
284614f0 7766
dda83cd7 7767 if (dval0 == NULL)
b5304971 7768 {
012370f6
TT
7769 /* Using plain value_from_contents_and_address here
7770 causes problems because we will end up trying to
7771 resolve a type that is currently being
7772 constructed. */
7773 dval = value_from_contents_and_address_unresolved (rtype,
7774 valaddr,
7775 address);
d0c97917 7776 rtype = dval->type ();
b5304971 7777 }
dda83cd7
SM
7778 else
7779 dval = dval0;
4c4b4cd2 7780
284614f0
JB
7781 /* If the type referenced by this field is an aligner type, we need
7782 to unwrap that aligner type, because its size might not be set.
7783 Keeping the aligner type would cause us to compute the wrong
7784 size for this field, impacting the offset of the all the fields
7785 that follow this one. */
7786 if (ada_is_aligner_type (field_type))
7787 {
b610c045 7788 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7789
7790 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7791 field_address = cond_offset_target (field_address, field_offset);
7792 field_type = ada_aligned_type (field_type);
7793 }
7794
7795 field_valaddr = cond_offset_host (field_valaddr,
7796 off / TARGET_CHAR_BIT);
7797 field_address = cond_offset_target (field_address,
7798 off / TARGET_CHAR_BIT);
7799
7800 /* Get the fixed type of the field. Note that, in this case,
7801 we do not want to get the real type out of the tag: if
7802 the current field is the parent part of a tagged record,
7803 we will get the tag of the object. Clearly wrong: the real
7804 type of the parent is not the real type of the child. We
7805 would end up in an infinite loop. */
7806 field_type = ada_get_base_type (field_type);
7807 field_type = ada_to_fixed_type (field_type, field_valaddr,
7808 field_address, dval, 0);
7809
5d14b6e5 7810 rtype->field (f).set_type (field_type);
33d16dd9 7811 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7812 /* The multiplication can potentially overflow. But because
7813 the field length has been size-checked just above, and
7814 assuming that the maximum size is a reasonable value,
7815 an overflow should not happen in practice. So rather than
7816 adding overflow recovery code to this already complex code,
7817 we just assume that it's not going to happen. */
df86565b 7818 fld_bit_len = rtype->field (f).type ()->length () * TARGET_CHAR_BIT;
dda83cd7 7819 }
14f9c5c9 7820 else
dda83cd7 7821 {
5ded5331
JB
7822 /* Note: If this field's type is a typedef, it is important
7823 to preserve the typedef layer.
7824
7825 Otherwise, we might be transforming a typedef to a fat
7826 pointer (encoding a pointer to an unconstrained array),
7827 into a basic fat pointer (encoding an unconstrained
7828 array). As both types are implemented using the same
7829 structure, the typedef is the only clue which allows us
7830 to distinguish between the two options. Stripping it
7831 would prevent us from printing this field appropriately. */
dda83cd7 7832 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7833 rtype->field (f).set_name (type->field (f).name ());
3757d2d4 7834 if (type->field (f).bitsize () > 0)
886176b8 7835 {
3757d2d4 7836 fld_bit_len = type->field (f).bitsize ();
886176b8
SM
7837 rtype->field (f).set_bitsize (fld_bit_len);
7838 }
dda83cd7 7839 else
5ded5331 7840 {
940da03e 7841 struct type *field_type = type->field (f).type ();
5ded5331
JB
7842
7843 /* We need to be careful of typedefs when computing
7844 the length of our field. If this is a typedef,
7845 get the length of the target type, not the length
7846 of the typedef. */
78134374 7847 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7848 field_type = ada_typedef_target_type (field_type);
7849
dda83cd7 7850 fld_bit_len =
df86565b 7851 ada_check_typedef (field_type)->length () * TARGET_CHAR_BIT;
5ded5331 7852 }
dda83cd7 7853 }
14f9c5c9 7854 if (off + fld_bit_len > bit_len)
dda83cd7 7855 bit_len = off + fld_bit_len;
d94e4f4f 7856 off += fld_bit_len;
b6cdbc9a 7857 rtype->set_length (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
14f9c5c9 7858 }
4c4b4cd2
PH
7859
7860 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7861 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7862 the record. This can happen in the presence of representation
7863 clauses. */
7864 if (variant_field >= 0)
7865 {
7866 struct type *branch_type;
7867
b610c045 7868 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
7869
7870 if (dval0 == NULL)
9f1f738a 7871 {
012370f6
TT
7872 /* Using plain value_from_contents_and_address here causes
7873 problems because we will end up trying to resolve a type
7874 that is currently being constructed. */
7875 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7876 address);
d0c97917 7877 rtype = dval->type ();
9f1f738a 7878 }
4c4b4cd2 7879 else
dda83cd7 7880 dval = dval0;
4c4b4cd2
PH
7881
7882 branch_type =
dda83cd7
SM
7883 to_fixed_variant_branch_type
7884 (type->field (variant_field).type (),
7885 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7886 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7887 if (branch_type == NULL)
dda83cd7
SM
7888 {
7889 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7890 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7891 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 7892 }
4c4b4cd2 7893 else
dda83cd7
SM
7894 {
7895 rtype->field (variant_field).set_type (branch_type);
d3fd12df 7896 rtype->field (variant_field).set_name ("S");
dda83cd7 7897 fld_bit_len =
df86565b 7898 rtype->field (variant_field).type ()->length () * TARGET_CHAR_BIT;
dda83cd7
SM
7899 if (off + fld_bit_len > bit_len)
7900 bit_len = off + fld_bit_len;
b6cdbc9a
SM
7901
7902 rtype->set_length
7903 (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
dda83cd7 7904 }
4c4b4cd2
PH
7905 }
7906
714e53ab
PH
7907 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7908 should contain the alignment of that record, which should be a strictly
7909 positive value. If null or negative, then something is wrong, most
7910 probably in the debug info. In that case, we don't round up the size
0963b4bd 7911 of the resulting type. If this record is not part of another structure,
714e53ab 7912 the current RTYPE length might be good enough for our purposes. */
df86565b 7913 if (type->length () <= 0)
714e53ab 7914 {
7d93a1e0 7915 if (rtype->name ())
cc1defb1 7916 warning (_("Invalid type size for `%s' detected: %s."),
df86565b 7917 rtype->name (), pulongest (type->length ()));
323e0a4a 7918 else
cc1defb1 7919 warning (_("Invalid type size for <unnamed> detected: %s."),
df86565b 7920 pulongest (type->length ()));
714e53ab
PH
7921 }
7922 else
df86565b 7923 rtype->set_length (align_up (rtype->length (), type->length ()));
14f9c5c9 7924
14f9c5c9
AS
7925 return rtype;
7926}
7927
4c4b4cd2
PH
7928/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7929 of 1. */
14f9c5c9 7930
d2e4a39e 7931static struct type *
fc1a4b47 7932template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 7933 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
7934{
7935 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 7936 address, dval0, 1);
4c4b4cd2
PH
7937}
7938
7939/* An ordinary record type in which ___XVL-convention fields and
7940 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7941 static approximations, containing all possible fields. Uses
7942 no runtime values. Useless for use in values, but that's OK,
7943 since the results are used only for type determinations. Works on both
7944 structs and unions. Representation note: to save space, we memorize
27710edb 7945 the result of this function in the type::target_type of the
4c4b4cd2
PH
7946 template type. */
7947
7948static struct type *
7949template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
7950{
7951 struct type *type;
7952 int nfields;
7953 int f;
7954
9e195661 7955 /* No need no do anything if the input type is already fixed. */
22c4c60c 7956 if (type0->is_fixed_instance ())
9e195661
PMR
7957 return type0;
7958
7959 /* Likewise if we already have computed the static approximation. */
27710edb
SM
7960 if (type0->target_type () != NULL)
7961 return type0->target_type ();
4c4b4cd2 7962
9e195661 7963 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 7964 type = type0;
1f704f76 7965 nfields = type0->num_fields ();
9e195661
PMR
7966
7967 /* Whether or not we cloned TYPE0, cache the result so that we don't do
7968 recompute all over next time. */
8a50fdce 7969 type0->set_target_type (type);
14f9c5c9
AS
7970
7971 for (f = 0; f < nfields; f += 1)
7972 {
940da03e 7973 struct type *field_type = type0->field (f).type ();
4c4b4cd2 7974 struct type *new_type;
14f9c5c9 7975
4c4b4cd2 7976 if (is_dynamic_field (type0, f))
460efde1
JB
7977 {
7978 field_type = ada_check_typedef (field_type);
27710edb 7979 new_type = to_static_fixed_type (field_type->target_type ());
460efde1 7980 }
14f9c5c9 7981 else
dda83cd7 7982 new_type = static_unwrap_type (field_type);
9e195661
PMR
7983
7984 if (new_type != field_type)
7985 {
7986 /* Clone TYPE0 only the first time we get a new field type. */
7987 if (type == type0)
7988 {
9fa83a7a 7989 type = type_allocator (type0).new_type ();
8a50fdce 7990 type0->set_target_type (type);
78134374 7991 type->set_code (type0->code ());
8ecb59f8 7992 INIT_NONE_SPECIFIC (type);
3cabb6b0 7993
2774f2da 7994 type->copy_fields (type0);
3cabb6b0 7995
d0e39ea2 7996 type->set_name (ada_type_name (type0));
9cdd0d12 7997 type->set_is_fixed_instance (true);
b6cdbc9a 7998 type->set_length (0);
9e195661 7999 }
5d14b6e5 8000 type->field (f).set_type (new_type);
33d16dd9 8001 type->field (f).set_name (type0->field (f).name ());
9e195661 8002 }
14f9c5c9 8003 }
9e195661 8004
14f9c5c9
AS
8005 return type;
8006}
8007
4c4b4cd2 8008/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8009 whose address in memory is ADDRESS, returns a revision of TYPE,
8010 which should be a non-dynamic-sized record, in which the variant
8011 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8012 for discriminant values in DVAL0, which can be NULL if the record
8013 contains the necessary discriminant values. */
8014
d2e4a39e 8015static struct type *
fc1a4b47 8016to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8017 CORE_ADDR address, struct value *dval0)
14f9c5c9 8018{
4c4b4cd2 8019 struct value *dval;
d2e4a39e 8020 struct type *rtype;
14f9c5c9 8021 struct type *branch_type;
1f704f76 8022 int nfields = type->num_fields ();
4c4b4cd2 8023 int variant_field = variant_field_index (type);
14f9c5c9 8024
4c4b4cd2 8025 if (variant_field == -1)
14f9c5c9
AS
8026 return type;
8027
65558ca5 8028 scoped_value_mark mark;
4c4b4cd2 8029 if (dval0 == NULL)
9f1f738a
SA
8030 {
8031 dval = value_from_contents_and_address (type, valaddr, address);
d0c97917 8032 type = dval->type ();
9f1f738a 8033 }
4c4b4cd2
PH
8034 else
8035 dval = dval0;
8036
9fa83a7a 8037 rtype = type_allocator (type).new_type ();
67607e24 8038 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8039 INIT_NONE_SPECIFIC (rtype);
2774f2da 8040 rtype->copy_fields (type);
3cabb6b0 8041
d0e39ea2 8042 rtype->set_name (ada_type_name (type));
9cdd0d12 8043 rtype->set_is_fixed_instance (true);
df86565b 8044 rtype->set_length (type->length ());
14f9c5c9 8045
4c4b4cd2 8046 branch_type = to_fixed_variant_branch_type
940da03e 8047 (type->field (variant_field).type (),
d2e4a39e 8048 cond_offset_host (valaddr,
b610c045 8049 type->field (variant_field).loc_bitpos ()
dda83cd7 8050 / TARGET_CHAR_BIT),
d2e4a39e 8051 cond_offset_target (address,
b610c045 8052 type->field (variant_field).loc_bitpos ()
dda83cd7 8053 / TARGET_CHAR_BIT), dval);
d2e4a39e 8054 if (branch_type == NULL)
14f9c5c9 8055 {
4c4b4cd2 8056 int f;
5b4ee69b 8057
4c4b4cd2 8058 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8059 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8060 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8061 }
8062 else
8063 {
5d14b6e5 8064 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8065 rtype->field (variant_field).set_name ("S");
886176b8 8066 rtype->field (variant_field).set_bitsize (0);
df86565b 8067 rtype->set_length (rtype->length () + branch_type->length ());
14f9c5c9 8068 }
b6cdbc9a 8069
df86565b
SM
8070 rtype->set_length (rtype->length ()
8071 - type->field (variant_field).type ()->length ());
d2e4a39e 8072
14f9c5c9
AS
8073 return rtype;
8074}
8075
8076/* An ordinary record type (with fixed-length fields) that describes
8077 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8078 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8079 should be in DVAL, a record value; it may be NULL if the object
8080 at ADDR itself contains any necessary discriminant values.
8081 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8082 values from the record are needed. Except in the case that DVAL,
8083 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8084 unchecked) is replaced by a particular branch of the variant.
8085
8086 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8087 is questionable and may be removed. It can arise during the
8088 processing of an unconstrained-array-of-record type where all the
8089 variant branches have exactly the same size. This is because in
8090 such cases, the compiler does not bother to use the XVS convention
8091 when encoding the record. I am currently dubious of this
8092 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8093
d2e4a39e 8094static struct type *
fc1a4b47 8095to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8096 CORE_ADDR address, struct value *dval)
14f9c5c9 8097{
d2e4a39e 8098 struct type *templ_type;
14f9c5c9 8099
22c4c60c 8100 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8101 return type0;
8102
d2e4a39e 8103 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8104
8105 if (templ_type != NULL)
8106 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8107 else if (variant_field_index (type0) >= 0)
8108 {
8109 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8110 return type0;
4c4b4cd2 8111 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8112 dval);
4c4b4cd2 8113 }
14f9c5c9
AS
8114 else
8115 {
9cdd0d12 8116 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8117 return type0;
8118 }
8119
8120}
8121
8122/* An ordinary record type (with fixed-length fields) that describes
8123 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8124 union type. Any necessary discriminants' values should be in DVAL,
8125 a record value. That is, this routine selects the appropriate
8126 branch of the union at ADDR according to the discriminant value
b1f33ddd 8127 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8128 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8129
d2e4a39e 8130static struct type *
fc1a4b47 8131to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8132 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8133{
8134 int which;
d2e4a39e
AS
8135 struct type *templ_type;
8136 struct type *var_type;
14f9c5c9 8137
78134374 8138 if (var_type0->code () == TYPE_CODE_PTR)
27710edb 8139 var_type = var_type0->target_type ();
d2e4a39e 8140 else
14f9c5c9
AS
8141 var_type = var_type0;
8142
8143 templ_type = ada_find_parallel_type (var_type, "___XVU");
8144
8145 if (templ_type != NULL)
8146 var_type = templ_type;
8147
d0c97917 8148 if (is_unchecked_variant (var_type, dval->type ()))
b1f33ddd 8149 return var_type0;
d8af9068 8150 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8151
8152 if (which < 0)
e9bb382b 8153 return empty_record (var_type);
14f9c5c9 8154 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8155 return to_fixed_record_type
27710edb 8156 (var_type->field (which).type ()->target_type(), valaddr, address, dval);
940da03e 8157 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8158 return
8159 to_fixed_record_type
940da03e 8160 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8161 else
940da03e 8162 return var_type->field (which).type ();
14f9c5c9
AS
8163}
8164
8908fca5
JB
8165/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8166 ENCODING_TYPE, a type following the GNAT conventions for discrete
8167 type encodings, only carries redundant information. */
8168
8169static int
8170ada_is_redundant_range_encoding (struct type *range_type,
8171 struct type *encoding_type)
8172{
108d56a4 8173 const char *bounds_str;
8908fca5
JB
8174 int n;
8175 LONGEST lo, hi;
8176
78134374 8177 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8178
78134374
SM
8179 if (get_base_type (range_type)->code ()
8180 != get_base_type (encoding_type)->code ())
005e2509
JB
8181 {
8182 /* The compiler probably used a simple base type to describe
8183 the range type instead of the range's actual base type,
8184 expecting us to get the real base type from the encoding
8185 anyway. In this situation, the encoding cannot be ignored
8186 as redundant. */
8187 return 0;
8188 }
8189
8908fca5
JB
8190 if (is_dynamic_type (range_type))
8191 return 0;
8192
7d93a1e0 8193 if (encoding_type->name () == NULL)
8908fca5
JB
8194 return 0;
8195
7d93a1e0 8196 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8197 if (bounds_str == NULL)
8198 return 0;
8199
8200 n = 8; /* Skip "___XDLU_". */
8201 if (!ada_scan_number (bounds_str, n, &lo, &n))
8202 return 0;
5537ddd0 8203 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8204 return 0;
8205
8206 n += 2; /* Skip the "__" separator between the two bounds. */
8207 if (!ada_scan_number (bounds_str, n, &hi, &n))
8208 return 0;
5537ddd0 8209 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8210 return 0;
8211
8212 return 1;
8213}
8214
8215/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8216 a type following the GNAT encoding for describing array type
8217 indices, only carries redundant information. */
8218
8219static int
8220ada_is_redundant_index_type_desc (struct type *array_type,
8221 struct type *desc_type)
8222{
8223 struct type *this_layer = check_typedef (array_type);
8224 int i;
8225
1f704f76 8226 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8227 {
3d967001 8228 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8229 desc_type->field (i).type ()))
8908fca5 8230 return 0;
27710edb 8231 this_layer = check_typedef (this_layer->target_type ());
8908fca5
JB
8232 }
8233
8234 return 1;
8235}
8236
14f9c5c9
AS
8237/* Assuming that TYPE0 is an array type describing the type of a value
8238 at ADDR, and that DVAL describes a record containing any
8239 discriminants used in TYPE0, returns a type for the value that
8240 contains no dynamic components (that is, no components whose sizes
8241 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8242 true, gives an error message if the resulting type's size is over
4c4b4cd2 8243 varsize_limit. */
14f9c5c9 8244
d2e4a39e
AS
8245static struct type *
8246to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8247 int ignore_too_big)
14f9c5c9 8248{
d2e4a39e
AS
8249 struct type *index_type_desc;
8250 struct type *result;
ad82864c 8251 int constrained_packed_array_p;
931e5bc3 8252 static const char *xa_suffix = "___XA";
14f9c5c9 8253
b0dd7688 8254 type0 = ada_check_typedef (type0);
22c4c60c 8255 if (type0->is_fixed_instance ())
4c4b4cd2 8256 return type0;
14f9c5c9 8257
ad82864c
JB
8258 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8259 if (constrained_packed_array_p)
75fd6a26
TT
8260 {
8261 type0 = decode_constrained_packed_array_type (type0);
8262 if (type0 == nullptr)
8263 error (_("could not decode constrained packed array type"));
8264 }
284614f0 8265
931e5bc3
JG
8266 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8267
8268 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8269 encoding suffixed with 'P' may still be generated. If so,
8270 it should be used to find the XA type. */
8271
8272 if (index_type_desc == NULL)
8273 {
1da0522e 8274 const char *type_name = ada_type_name (type0);
931e5bc3 8275
1da0522e 8276 if (type_name != NULL)
931e5bc3 8277 {
1da0522e 8278 const int len = strlen (type_name);
931e5bc3
JG
8279 char *name = (char *) alloca (len + strlen (xa_suffix));
8280
1da0522e 8281 if (type_name[len - 1] == 'P')
931e5bc3 8282 {
1da0522e 8283 strcpy (name, type_name);
931e5bc3
JG
8284 strcpy (name + len - 1, xa_suffix);
8285 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8286 }
8287 }
8288 }
8289
28c85d6c 8290 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8291 if (index_type_desc != NULL
8292 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8293 {
8294 /* Ignore this ___XA parallel type, as it does not bring any
8295 useful information. This allows us to avoid creating fixed
8296 versions of the array's index types, which would be identical
8297 to the original ones. This, in turn, can also help avoid
8298 the creation of fixed versions of the array itself. */
8299 index_type_desc = NULL;
8300 }
8301
14f9c5c9
AS
8302 if (index_type_desc == NULL)
8303 {
27710edb 8304 struct type *elt_type0 = ada_check_typedef (type0->target_type ());
5b4ee69b 8305
14f9c5c9 8306 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8307 depend on the contents of the array in properly constructed
8308 debugging data. */
529cad9c 8309 /* Create a fixed version of the array element type.
dda83cd7
SM
8310 We're not providing the address of an element here,
8311 and thus the actual object value cannot be inspected to do
8312 the conversion. This should not be a problem, since arrays of
8313 unconstrained objects are not allowed. In particular, all
8314 the elements of an array of a tagged type should all be of
8315 the same type specified in the debugging info. No need to
8316 consult the object tag. */
1ed6ede0 8317 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8318
284614f0
JB
8319 /* Make sure we always create a new array type when dealing with
8320 packed array types, since we're going to fix-up the array
8321 type length and element bitsize a little further down. */
ad82864c 8322 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8323 result = type0;
14f9c5c9 8324 else
9e76b17a
TT
8325 {
8326 type_allocator alloc (type0);
8327 result = create_array_type (alloc, elt_type, type0->index_type ());
8328 }
14f9c5c9
AS
8329 }
8330 else
8331 {
8332 int i;
8333 struct type *elt_type0;
8334
8335 elt_type0 = type0;
1f704f76 8336 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
27710edb 8337 elt_type0 = elt_type0->target_type ();
14f9c5c9
AS
8338
8339 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8340 depend on the contents of the array in properly constructed
8341 debugging data. */
529cad9c 8342 /* Create a fixed version of the array element type.
dda83cd7
SM
8343 We're not providing the address of an element here,
8344 and thus the actual object value cannot be inspected to do
8345 the conversion. This should not be a problem, since arrays of
8346 unconstrained objects are not allowed. In particular, all
8347 the elements of an array of a tagged type should all be of
8348 the same type specified in the debugging info. No need to
8349 consult the object tag. */
1ed6ede0 8350 result =
dda83cd7 8351 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8352
8353 elt_type0 = type0;
1f704f76 8354 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8355 {
8356 struct type *range_type =
8357 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8358
9e76b17a
TT
8359 type_allocator alloc (elt_type0);
8360 result = create_array_type (alloc, result, range_type);
27710edb 8361 elt_type0 = elt_type0->target_type ();
dda83cd7 8362 }
14f9c5c9
AS
8363 }
8364
2e6fda7d
JB
8365 /* We want to preserve the type name. This can be useful when
8366 trying to get the type name of a value that has already been
8367 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8368 result->set_name (type0->name ());
2e6fda7d 8369
ad82864c 8370 if (constrained_packed_array_p)
284614f0
JB
8371 {
8372 /* So far, the resulting type has been created as if the original
8373 type was a regular (non-packed) array type. As a result, the
8374 bitsize of the array elements needs to be set again, and the array
8375 length needs to be recomputed based on that bitsize. */
df86565b 8376 int len = result->length () / result->target_type ()->length ();
3757d2d4 8377 int elt_bitsize = type0->field (0).bitsize ();
284614f0 8378
3757d2d4 8379 result->field (0).set_bitsize (elt_bitsize);
b6cdbc9a 8380 result->set_length (len * elt_bitsize / HOST_CHAR_BIT);
df86565b
SM
8381 if (result->length () * HOST_CHAR_BIT < len * elt_bitsize)
8382 result->set_length (result->length () + 1);
284614f0
JB
8383 }
8384
9cdd0d12 8385 result->set_is_fixed_instance (true);
14f9c5c9 8386 return result;
d2e4a39e 8387}
14f9c5c9
AS
8388
8389
8390/* A standard type (containing no dynamically sized components)
8391 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8392 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8393 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8394 ADDRESS or in VALADDR contains these discriminants.
8395
1ed6ede0
JB
8396 If CHECK_TAG is not null, in the case of tagged types, this function
8397 attempts to locate the object's tag and use it to compute the actual
8398 type. However, when ADDRESS is null, we cannot use it to determine the
8399 location of the tag, and therefore compute the tagged type's actual type.
8400 So we return the tagged type without consulting the tag. */
529cad9c 8401
f192137b
JB
8402static struct type *
8403ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8404 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8405{
61ee279c 8406 type = ada_check_typedef (type);
8ecb59f8
TT
8407
8408 /* Only un-fixed types need to be handled here. */
8409 if (!HAVE_GNAT_AUX_INFO (type))
8410 return type;
8411
78134374 8412 switch (type->code ())
d2e4a39e
AS
8413 {
8414 default:
14f9c5c9 8415 return type;
d2e4a39e 8416 case TYPE_CODE_STRUCT:
4c4b4cd2 8417 {
dda83cd7
SM
8418 struct type *static_type = to_static_fixed_type (type);
8419 struct type *fixed_record_type =
8420 to_fixed_record_type (type, valaddr, address, NULL);
8421
8422 /* If STATIC_TYPE is a tagged type and we know the object's address,
8423 then we can determine its tag, and compute the object's actual
8424 type from there. Note that we have to use the fixed record
8425 type (the parent part of the record may have dynamic fields
8426 and the way the location of _tag is expressed may depend on
8427 them). */
8428
8429 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8430 {
b50d69b5
JG
8431 struct value *tag =
8432 value_tag_from_contents_and_address
8433 (fixed_record_type,
8434 valaddr,
8435 address);
8436 struct type *real_type = type_from_tag (tag);
8437 struct value *obj =
8438 value_from_contents_and_address (fixed_record_type,
8439 valaddr,
8440 address);
d0c97917 8441 fixed_record_type = obj->type ();
dda83cd7
SM
8442 if (real_type != NULL)
8443 return to_fixed_record_type
b50d69b5 8444 (real_type, NULL,
9feb2d07 8445 ada_tag_value_at_base_address (obj)->address (), NULL);
dda83cd7
SM
8446 }
8447
8448 /* Check to see if there is a parallel ___XVZ variable.
8449 If there is, then it provides the actual size of our type. */
8450 else if (ada_type_name (fixed_record_type) != NULL)
8451 {
8452 const char *name = ada_type_name (fixed_record_type);
8453 char *xvz_name
224c3ddb 8454 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8455 bool xvz_found = false;
dda83cd7 8456 LONGEST size;
4af88198 8457
dda83cd7 8458 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8459 try
eccab96d
JB
8460 {
8461 xvz_found = get_int_var_value (xvz_name, size);
8462 }
230d2906 8463 catch (const gdb_exception_error &except)
eccab96d
JB
8464 {
8465 /* We found the variable, but somehow failed to read
8466 its value. Rethrow the same error, but with a little
8467 bit more information, to help the user understand
8468 what went wrong (Eg: the variable might have been
8469 optimized out). */
8470 throw_error (except.error,
8471 _("unable to read value of %s (%s)"),
3d6e9d23 8472 xvz_name, except.what ());
eccab96d 8473 }
eccab96d 8474
df86565b 8475 if (xvz_found && fixed_record_type->length () != size)
dda83cd7
SM
8476 {
8477 fixed_record_type = copy_type (fixed_record_type);
b6cdbc9a 8478 fixed_record_type->set_length (size);
dda83cd7
SM
8479
8480 /* The FIXED_RECORD_TYPE may have be a stub. We have
8481 observed this when the debugging info is STABS, and
8482 apparently it is something that is hard to fix.
8483
8484 In practice, we don't need the actual type definition
8485 at all, because the presence of the XVZ variable allows us
8486 to assume that there must be a XVS type as well, which we
8487 should be able to use later, when we need the actual type
8488 definition.
8489
8490 In the meantime, pretend that the "fixed" type we are
8491 returning is NOT a stub, because this can cause trouble
8492 when using this type to create new types targeting it.
8493 Indeed, the associated creation routines often check
8494 whether the target type is a stub and will try to replace
8495 it, thus using a type with the wrong size. This, in turn,
8496 might cause the new type to have the wrong size too.
8497 Consider the case of an array, for instance, where the size
8498 of the array is computed from the number of elements in
8499 our array multiplied by the size of its element. */
b4b73759 8500 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8501 }
8502 }
8503 return fixed_record_type;
4c4b4cd2 8504 }
d2e4a39e 8505 case TYPE_CODE_ARRAY:
4c4b4cd2 8506 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8507 case TYPE_CODE_UNION:
8508 if (dval == NULL)
dda83cd7 8509 return type;
d2e4a39e 8510 else
dda83cd7 8511 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8512 }
14f9c5c9
AS
8513}
8514
f192137b
JB
8515/* The same as ada_to_fixed_type_1, except that it preserves the type
8516 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8517
8518 The typedef layer needs be preserved in order to differentiate between
8519 arrays and array pointers when both types are implemented using the same
8520 fat pointer. In the array pointer case, the pointer is encoded as
8521 a typedef of the pointer type. For instance, considering:
8522
8523 type String_Access is access String;
8524 S1 : String_Access := null;
8525
8526 To the debugger, S1 is defined as a typedef of type String. But
8527 to the user, it is a pointer. So if the user tries to print S1,
8528 we should not dereference the array, but print the array address
8529 instead.
8530
8531 If we didn't preserve the typedef layer, we would lose the fact that
8532 the type is to be presented as a pointer (needs de-reference before
8533 being printed). And we would also use the source-level type name. */
f192137b
JB
8534
8535struct type *
8536ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8537 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8538
8539{
8540 struct type *fixed_type =
8541 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8542
96dbd2c1
JB
8543 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8544 then preserve the typedef layer.
8545
8546 Implementation note: We can only check the main-type portion of
8547 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8548 from TYPE now returns a type that has the same instance flags
8549 as TYPE. For instance, if TYPE is a "typedef const", and its
8550 target type is a "struct", then the typedef elimination will return
8551 a "const" version of the target type. See check_typedef for more
8552 details about how the typedef layer elimination is done.
8553
8554 brobecker/2010-11-19: It seems to me that the only case where it is
8555 useful to preserve the typedef layer is when dealing with fat pointers.
8556 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8557 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8558 because we call check_typedef/ada_check_typedef pretty much everywhere.
8559 */
78134374 8560 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8561 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8562 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8563 return type;
8564
8565 return fixed_type;
8566}
8567
14f9c5c9 8568/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8569 TYPE0, but based on no runtime data. */
14f9c5c9 8570
d2e4a39e
AS
8571static struct type *
8572to_static_fixed_type (struct type *type0)
14f9c5c9 8573{
d2e4a39e 8574 struct type *type;
14f9c5c9
AS
8575
8576 if (type0 == NULL)
8577 return NULL;
8578
22c4c60c 8579 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8580 return type0;
8581
61ee279c 8582 type0 = ada_check_typedef (type0);
d2e4a39e 8583
78134374 8584 switch (type0->code ())
14f9c5c9
AS
8585 {
8586 default:
8587 return type0;
8588 case TYPE_CODE_STRUCT:
8589 type = dynamic_template_type (type0);
d2e4a39e 8590 if (type != NULL)
dda83cd7 8591 return template_to_static_fixed_type (type);
4c4b4cd2 8592 else
dda83cd7 8593 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8594 case TYPE_CODE_UNION:
8595 type = ada_find_parallel_type (type0, "___XVU");
8596 if (type != NULL)
dda83cd7 8597 return template_to_static_fixed_type (type);
4c4b4cd2 8598 else
dda83cd7 8599 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8600 }
8601}
8602
4c4b4cd2
PH
8603/* A static approximation of TYPE with all type wrappers removed. */
8604
d2e4a39e
AS
8605static struct type *
8606static_unwrap_type (struct type *type)
14f9c5c9
AS
8607{
8608 if (ada_is_aligner_type (type))
8609 {
940da03e 8610 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8611 if (ada_type_name (type1) == NULL)
d0e39ea2 8612 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8613
8614 return static_unwrap_type (type1);
8615 }
d2e4a39e 8616 else
14f9c5c9 8617 {
d2e4a39e 8618 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8619
d2e4a39e 8620 if (raw_real_type == type)
dda83cd7 8621 return type;
14f9c5c9 8622 else
dda83cd7 8623 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8624 }
8625}
8626
8627/* In some cases, incomplete and private types require
4c4b4cd2 8628 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8629 type Foo;
8630 type FooP is access Foo;
8631 V: FooP;
8632 type Foo is array ...;
4c4b4cd2 8633 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8634 cross-references to such types, we instead substitute for FooP a
8635 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8636 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8637
8638/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8639 exists, otherwise TYPE. */
8640
d2e4a39e 8641struct type *
61ee279c 8642ada_check_typedef (struct type *type)
14f9c5c9 8643{
727e3d2e
JB
8644 if (type == NULL)
8645 return NULL;
8646
736ade86
XR
8647 /* If our type is an access to an unconstrained array, which is encoded
8648 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8649 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8650 what allows us to distinguish between fat pointers that represent
8651 array types, and fat pointers that represent array access types
8652 (in both cases, the compiler implements them as fat pointers). */
736ade86 8653 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8654 return type;
8655
f168693b 8656 type = check_typedef (type);
78134374 8657 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8658 || !type->is_stub ()
7d93a1e0 8659 || type->name () == NULL)
14f9c5c9 8660 return type;
d2e4a39e 8661 else
14f9c5c9 8662 {
7d93a1e0 8663 const char *name = type->name ();
d2e4a39e 8664 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8665
05e522ef 8666 if (type1 == NULL)
dda83cd7 8667 return type;
05e522ef
JB
8668
8669 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8670 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8671 types, only for the typedef-to-array types). If that's the case,
8672 strip the typedef layer. */
78134374 8673 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8674 type1 = ada_check_typedef (type1);
8675
8676 return type1;
14f9c5c9
AS
8677 }
8678}
8679
8680/* A value representing the data at VALADDR/ADDRESS as described by
8681 type TYPE0, but with a standard (static-sized) type that correctly
8682 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8683 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8684 creation of struct values]. */
14f9c5c9 8685
4c4b4cd2
PH
8686static struct value *
8687ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8688 struct value *val0)
14f9c5c9 8689{
1ed6ede0 8690 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8691
14f9c5c9
AS
8692 if (type == type0 && val0 != NULL)
8693 return val0;
cc0e770c 8694
736355f2 8695 if (val0->lval () != lval_memory)
cc0e770c
JB
8696 {
8697 /* Our value does not live in memory; it could be a convenience
8698 variable, for instance. Create a not_lval value using val0's
8699 contents. */
efaf1ae0 8700 return value_from_contents (type, val0->contents ().data ());
cc0e770c
JB
8701 }
8702
8703 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8704}
8705
8706/* A value representing VAL, but with a standard (static-sized) type
8707 that correctly describes it. Does not necessarily create a new
8708 value. */
8709
0c3acc09 8710struct value *
4c4b4cd2
PH
8711ada_to_fixed_value (struct value *val)
8712{
c48db5ca 8713 val = unwrap_value (val);
9feb2d07 8714 val = ada_to_fixed_value_create (val->type (), val->address (), val);
c48db5ca 8715 return val;
14f9c5c9 8716}
d2e4a39e 8717\f
14f9c5c9 8718
14f9c5c9
AS
8719/* Attributes */
8720
4c4b4cd2 8721/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8722
4c4b4cd2
PH
8723static LONGEST
8724pos_atr (struct value *arg)
14f9c5c9 8725{
24209737 8726 struct value *val = coerce_ref (arg);
d0c97917 8727 struct type *type = val->type ();
14f9c5c9 8728
d2e4a39e 8729 if (!discrete_type_p (type))
323e0a4a 8730 error (_("'POS only defined on discrete types"));
14f9c5c9 8731
6b09f134 8732 std::optional<LONGEST> result = discrete_position (type, value_as_long (val));
6244c119 8733 if (!result.has_value ())
aa715135 8734 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8735
6244c119 8736 return *result;
4c4b4cd2
PH
8737}
8738
7631cf6c 8739struct value *
7992accc
TT
8740ada_pos_atr (struct type *expect_type,
8741 struct expression *exp,
8742 enum noside noside, enum exp_opcode op,
8743 struct value *arg)
4c4b4cd2 8744{
7992accc
TT
8745 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8746 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8747 return value::zero (type, not_lval);
3cb382c9 8748 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8749}
8750
4c4b4cd2 8751/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8752
d2e4a39e 8753static struct value *
53a47a3e 8754val_atr (struct type *type, LONGEST val)
14f9c5c9 8755{
53a47a3e 8756 gdb_assert (discrete_type_p (type));
0bc2354b 8757 if (type->code () == TYPE_CODE_RANGE)
27710edb 8758 type = type->target_type ();
78134374 8759 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8760 {
53a47a3e 8761 if (val < 0 || val >= type->num_fields ())
dda83cd7 8762 error (_("argument to 'VAL out of range"));
970db518 8763 val = type->field (val).loc_enumval ();
14f9c5c9 8764 }
53a47a3e
TT
8765 return value_from_longest (type, val);
8766}
8767
9e99f48f 8768struct value *
22f6f797
TT
8769ada_val_atr (struct expression *exp, enum noside noside, struct type *type,
8770 struct value *arg)
53a47a3e 8771{
3848abd6 8772 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8773 return value::zero (type, not_lval);
3848abd6 8774
53a47a3e
TT
8775 if (!discrete_type_p (type))
8776 error (_("'VAL only defined on discrete types"));
d0c97917 8777 if (!integer_type_p (arg->type ()))
53a47a3e
TT
8778 error (_("'VAL requires integral argument"));
8779
8780 return val_atr (type, value_as_long (arg));
14f9c5c9 8781}
22f6f797
TT
8782
8783/* Implementation of the enum_rep attribute. */
8784struct value *
8785ada_atr_enum_rep (struct expression *exp, enum noside noside, struct type *type,
8786 struct value *arg)
8787{
8788 struct type *inttype = builtin_type (exp->gdbarch)->builtin_int;
8789 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8790 return value::zero (inttype, not_lval);
8791
8792 if (type->code () == TYPE_CODE_RANGE)
8793 type = type->target_type ();
8794 if (type->code () != TYPE_CODE_ENUM)
8795 error (_("'Enum_Rep only defined on enum types"));
8796 if (!types_equal (type, arg->type ()))
8797 error (_("'Enum_Rep requires argument to have same type as enum"));
8798
8799 return value_cast (inttype, arg);
8800}
8801
8802/* Implementation of the enum_val attribute. */
8803struct value *
8804ada_atr_enum_val (struct expression *exp, enum noside noside, struct type *type,
8805 struct value *arg)
8806{
8807 struct type *original_type = type;
8808 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8809 return value::zero (original_type, not_lval);
8810
8811 if (type->code () == TYPE_CODE_RANGE)
8812 type = type->target_type ();
8813 if (type->code () != TYPE_CODE_ENUM)
8814 error (_("'Enum_Val only defined on enum types"));
8815 if (!integer_type_p (arg->type ()))
8816 error (_("'Enum_Val requires integral argument"));
8817
8818 LONGEST value = value_as_long (arg);
8819 for (int i = 0; i < type->num_fields (); ++i)
8820 {
8821 if (type->field (i).loc_enumval () == value)
8822 return value_from_longest (original_type, value);
8823 }
8824
8825 error (_("value %s not found in enum"), plongest (value));
8826}
8827
14f9c5c9 8828\f
d2e4a39e 8829
dda83cd7 8830 /* Evaluation */
14f9c5c9 8831
4c4b4cd2
PH
8832/* True if TYPE appears to be an Ada character type.
8833 [At the moment, this is true only for Character and Wide_Character;
8834 It is a heuristic test that could stand improvement]. */
14f9c5c9 8835
fc913e53 8836bool
d2e4a39e 8837ada_is_character_type (struct type *type)
14f9c5c9 8838{
7b9f71f2
JB
8839 const char *name;
8840
8841 /* If the type code says it's a character, then assume it really is,
8842 and don't check any further. */
78134374 8843 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8844 return true;
7b9f71f2
JB
8845
8846 /* Otherwise, assume it's a character type iff it is a discrete type
8847 with a known character type name. */
8848 name = ada_type_name (type);
8849 return (name != NULL
dda83cd7
SM
8850 && (type->code () == TYPE_CODE_INT
8851 || type->code () == TYPE_CODE_RANGE)
8852 && (strcmp (name, "character") == 0
8853 || strcmp (name, "wide_character") == 0
8854 || strcmp (name, "wide_wide_character") == 0
8855 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8856}
8857
4c4b4cd2 8858/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8859
fc913e53 8860bool
ebf56fd3 8861ada_is_string_type (struct type *type)
14f9c5c9 8862{
61ee279c 8863 type = ada_check_typedef (type);
d2e4a39e 8864 if (type != NULL
78134374 8865 && type->code () != TYPE_CODE_PTR
76a01679 8866 && (ada_is_simple_array_type (type)
dda83cd7 8867 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8868 && ada_array_arity (type) == 1)
8869 {
8870 struct type *elttype = ada_array_element_type (type, 1);
8871
8872 return ada_is_character_type (elttype);
8873 }
d2e4a39e 8874 else
fc913e53 8875 return false;
14f9c5c9
AS
8876}
8877
5bf03f13
JB
8878/* The compiler sometimes provides a parallel XVS type for a given
8879 PAD type. Normally, it is safe to follow the PAD type directly,
8880 but older versions of the compiler have a bug that causes the offset
8881 of its "F" field to be wrong. Following that field in that case
8882 would lead to incorrect results, but this can be worked around
8883 by ignoring the PAD type and using the associated XVS type instead.
8884
8885 Set to True if the debugger should trust the contents of PAD types.
8886 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8887static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8888
8889/* True if TYPE is a struct type introduced by the compiler to force the
8890 alignment of a value. Such types have a single field with a
4c4b4cd2 8891 distinctive name. */
14f9c5c9
AS
8892
8893int
ebf56fd3 8894ada_is_aligner_type (struct type *type)
14f9c5c9 8895{
61ee279c 8896 type = ada_check_typedef (type);
714e53ab 8897
5bf03f13 8898 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
8899 return 0;
8900
78134374 8901 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 8902 && type->num_fields () == 1
33d16dd9 8903 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
8904}
8905
8906/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 8907 the parallel type. */
14f9c5c9 8908
d2e4a39e
AS
8909struct type *
8910ada_get_base_type (struct type *raw_type)
14f9c5c9 8911{
d2e4a39e
AS
8912 struct type *real_type_namer;
8913 struct type *raw_real_type;
14f9c5c9 8914
78134374 8915 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
8916 return raw_type;
8917
284614f0
JB
8918 if (ada_is_aligner_type (raw_type))
8919 /* The encoding specifies that we should always use the aligner type.
8920 So, even if this aligner type has an associated XVS type, we should
8921 simply ignore it.
8922
8923 According to the compiler gurus, an XVS type parallel to an aligner
8924 type may exist because of a stabs limitation. In stabs, aligner
8925 types are empty because the field has a variable-sized type, and
8926 thus cannot actually be used as an aligner type. As a result,
8927 we need the associated parallel XVS type to decode the type.
8928 Since the policy in the compiler is to not change the internal
8929 representation based on the debugging info format, we sometimes
8930 end up having a redundant XVS type parallel to the aligner type. */
8931 return raw_type;
8932
14f9c5c9 8933 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 8934 if (real_type_namer == NULL
78134374 8935 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 8936 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
8937 return raw_type;
8938
940da03e 8939 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
8940 {
8941 /* This is an older encoding form where the base type needs to be
85102364 8942 looked up by name. We prefer the newer encoding because it is
f80d3ff2 8943 more efficient. */
33d16dd9 8944 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
8945 if (raw_real_type == NULL)
8946 return raw_type;
8947 else
8948 return raw_real_type;
8949 }
8950
8951 /* The field in our XVS type is a reference to the base type. */
27710edb 8952 return real_type_namer->field (0).type ()->target_type ();
d2e4a39e 8953}
14f9c5c9 8954
4c4b4cd2 8955/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 8956
d2e4a39e
AS
8957struct type *
8958ada_aligned_type (struct type *type)
14f9c5c9
AS
8959{
8960 if (ada_is_aligner_type (type))
940da03e 8961 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
8962 else
8963 return ada_get_base_type (type);
8964}
8965
8966
8967/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 8968 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 8969
fc1a4b47
AC
8970const gdb_byte *
8971ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 8972{
d2e4a39e 8973 if (ada_is_aligner_type (type))
b610c045
SM
8974 return ada_aligned_value_addr
8975 (type->field (0).type (),
8976 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
8977 else
8978 return valaddr;
8979}
8980
4c4b4cd2
PH
8981
8982
14f9c5c9 8983/* The printed representation of an enumeration literal with encoded
4c4b4cd2 8984 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
8985const char *
8986ada_enum_name (const char *name)
14f9c5c9 8987{
5f9febe0 8988 static std::string storage;
e6a959d6 8989 const char *tmp;
14f9c5c9 8990
4c4b4cd2
PH
8991 /* First, unqualify the enumeration name:
8992 1. Search for the last '.' character. If we find one, then skip
177b42fe 8993 all the preceding characters, the unqualified name starts
76a01679 8994 right after that dot.
4c4b4cd2 8995 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
8996 translates dots into "__". Search forward for double underscores,
8997 but stop searching when we hit an overloading suffix, which is
8998 of the form "__" followed by digits. */
4c4b4cd2 8999
c3e5cd34
PH
9000 tmp = strrchr (name, '.');
9001 if (tmp != NULL)
4c4b4cd2
PH
9002 name = tmp + 1;
9003 else
14f9c5c9 9004 {
4c4b4cd2 9005 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9006 {
9007 if (isdigit (tmp[2]))
9008 break;
9009 else
9010 name = tmp + 2;
9011 }
14f9c5c9
AS
9012 }
9013
9014 if (name[0] == 'Q')
9015 {
14f9c5c9 9016 int v;
5b4ee69b 9017
14f9c5c9 9018 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9019 {
a7041de8
TT
9020 int offset = 2;
9021 if (name[1] == 'W' && name[2] == 'W')
9022 {
9023 /* Also handle the QWW case. */
9024 ++offset;
9025 }
9026 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9027 return name;
9028 }
272560b5
TT
9029 else if (((name[1] >= '0' && name[1] <= '9')
9030 || (name[1] >= 'a' && name[1] <= 'z'))
9031 && name[2] == '\0')
9032 {
5f9febe0
TT
9033 storage = string_printf ("'%c'", name[1]);
9034 return storage.c_str ();
272560b5 9035 }
14f9c5c9 9036 else
dda83cd7 9037 return name;
14f9c5c9
AS
9038
9039 if (isascii (v) && isprint (v))
5f9febe0 9040 storage = string_printf ("'%c'", v);
14f9c5c9 9041 else if (name[1] == 'U')
a7041de8
TT
9042 storage = string_printf ("'[\"%02x\"]'", v);
9043 else if (name[2] != 'W')
9044 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9045 else
a7041de8 9046 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9047
5f9febe0 9048 return storage.c_str ();
14f9c5c9 9049 }
d2e4a39e 9050 else
4c4b4cd2 9051 {
c3e5cd34
PH
9052 tmp = strstr (name, "__");
9053 if (tmp == NULL)
9054 tmp = strstr (name, "$");
9055 if (tmp != NULL)
dda83cd7 9056 {
5f9febe0
TT
9057 storage = std::string (name, tmp - name);
9058 return storage.c_str ();
dda83cd7 9059 }
4c4b4cd2
PH
9060
9061 return name;
9062 }
14f9c5c9
AS
9063}
9064
013a623f
TT
9065/* If TYPE is a dynamic type, return the base type. Otherwise, if
9066 there is no parallel type, return nullptr. */
9067
9068static struct type *
9069find_base_type (struct type *type)
9070{
9071 struct type *raw_real_type
9072 = ada_check_typedef (ada_get_base_type (type));
9073
9074 /* No parallel XVS or XVE type. */
9075 if (type == raw_real_type
9076 && ada_find_parallel_type (type, "___XVE") == nullptr)
9077 return nullptr;
9078
9079 return raw_real_type;
9080}
9081
14f9c5c9 9082/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9083 value it wraps. */
14f9c5c9 9084
d2e4a39e
AS
9085static struct value *
9086unwrap_value (struct value *val)
14f9c5c9 9087{
d0c97917 9088 struct type *type = ada_check_typedef (val->type ());
5b4ee69b 9089
14f9c5c9
AS
9090 if (ada_is_aligner_type (type))
9091 {
de4d072f 9092 struct value *v = ada_value_struct_elt (val, "F", 0);
d0c97917 9093 struct type *val_type = ada_check_typedef (v->type ());
5b4ee69b 9094
14f9c5c9 9095 if (ada_type_name (val_type) == NULL)
d0e39ea2 9096 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9097
9098 return unwrap_value (v);
9099 }
d2e4a39e 9100 else
14f9c5c9 9101 {
013a623f
TT
9102 struct type *raw_real_type = find_base_type (type);
9103 if (raw_real_type == nullptr)
5bf03f13 9104 return val;
14f9c5c9 9105
d2e4a39e 9106 return
dda83cd7
SM
9107 coerce_unspec_val_to_type
9108 (val, ada_to_fixed_type (raw_real_type, 0,
9feb2d07 9109 val->address (),
dda83cd7 9110 NULL, 1));
14f9c5c9
AS
9111 }
9112}
d2e4a39e 9113
d99dcf51
JB
9114/* Given two array types T1 and T2, return nonzero iff both arrays
9115 contain the same number of elements. */
9116
9117static int
9118ada_same_array_size_p (struct type *t1, struct type *t2)
9119{
9120 LONGEST lo1, hi1, lo2, hi2;
9121
9122 /* Get the array bounds in order to verify that the size of
9123 the two arrays match. */
9124 if (!get_array_bounds (t1, &lo1, &hi1)
9125 || !get_array_bounds (t2, &lo2, &hi2))
9126 error (_("unable to determine array bounds"));
9127
9128 /* To make things easier for size comparison, normalize a bit
9129 the case of empty arrays by making sure that the difference
9130 between upper bound and lower bound is always -1. */
9131 if (lo1 > hi1)
9132 hi1 = lo1 - 1;
9133 if (lo2 > hi2)
9134 hi2 = lo2 - 1;
9135
9136 return (hi1 - lo1 == hi2 - lo2);
9137}
9138
9139/* Assuming that VAL is an array of integrals, and TYPE represents
9140 an array with the same number of elements, but with wider integral
9141 elements, return an array "casted" to TYPE. In practice, this
9142 means that the returned array is built by casting each element
9143 of the original array into TYPE's (wider) element type. */
9144
9145static struct value *
9146ada_promote_array_of_integrals (struct type *type, struct value *val)
9147{
27710edb 9148 struct type *elt_type = type->target_type ();
d99dcf51 9149 LONGEST lo, hi;
d99dcf51
JB
9150 LONGEST i;
9151
9152 /* Verify that both val and type are arrays of scalars, and
9153 that the size of val's elements is smaller than the size
9154 of type's element. */
78134374 9155 gdb_assert (type->code () == TYPE_CODE_ARRAY);
27710edb 9156 gdb_assert (is_integral_type (type->target_type ()));
d0c97917
TT
9157 gdb_assert (val->type ()->code () == TYPE_CODE_ARRAY);
9158 gdb_assert (is_integral_type (val->type ()->target_type ()));
df86565b 9159 gdb_assert (type->target_type ()->length ()
d0c97917 9160 > val->type ()->target_type ()->length ());
d99dcf51
JB
9161
9162 if (!get_array_bounds (type, &lo, &hi))
9163 error (_("unable to determine array bounds"));
9164
317c3ed9 9165 value *res = value::allocate (type);
bbe912ba 9166 gdb::array_view<gdb_byte> res_contents = res->contents_writeable ();
d99dcf51
JB
9167
9168 /* Promote each array element. */
9169 for (i = 0; i < hi - lo + 1; i++)
9170 {
9171 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
df86565b 9172 int elt_len = elt_type->length ();
d99dcf51 9173
efaf1ae0 9174 copy (elt->contents_all (), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9175 }
9176
9177 return res;
9178}
9179
4c4b4cd2
PH
9180/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9181 return the converted value. */
9182
d2e4a39e
AS
9183static struct value *
9184coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9185{
d0c97917 9186 struct type *type2 = val->type ();
5b4ee69b 9187
14f9c5c9
AS
9188 if (type == type2)
9189 return val;
9190
61ee279c
PH
9191 type2 = ada_check_typedef (type2);
9192 type = ada_check_typedef (type);
14f9c5c9 9193
78134374
SM
9194 if (type2->code () == TYPE_CODE_PTR
9195 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9196 {
9197 val = ada_value_ind (val);
d0c97917 9198 type2 = val->type ();
14f9c5c9
AS
9199 }
9200
78134374
SM
9201 if (type2->code () == TYPE_CODE_ARRAY
9202 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9203 {
d99dcf51
JB
9204 if (!ada_same_array_size_p (type, type2))
9205 error (_("cannot assign arrays of different length"));
9206
27710edb
SM
9207 if (is_integral_type (type->target_type ())
9208 && is_integral_type (type2->target_type ())
df86565b 9209 && type2->target_type ()->length () < type->target_type ()->length ())
d99dcf51
JB
9210 {
9211 /* Allow implicit promotion of the array elements to
9212 a wider type. */
9213 return ada_promote_array_of_integrals (type, val);
9214 }
9215
df86565b 9216 if (type2->target_type ()->length () != type->target_type ()->length ())
dda83cd7 9217 error (_("Incompatible types in assignment"));
81ae560c 9218 val->deprecated_set_type (type);
14f9c5c9 9219 }
d2e4a39e 9220 return val;
14f9c5c9
AS
9221}
9222
4c4b4cd2
PH
9223static struct value *
9224ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9225{
4c4b4cd2 9226 struct type *type1, *type2;
4c4b4cd2 9227
994b9211
AC
9228 arg1 = coerce_ref (arg1);
9229 arg2 = coerce_ref (arg2);
d0c97917
TT
9230 type1 = get_base_type (ada_check_typedef (arg1->type ()));
9231 type2 = get_base_type (ada_check_typedef (arg2->type ()));
4c4b4cd2 9232
78134374
SM
9233 if (type1->code () != TYPE_CODE_INT
9234 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9235 return value_binop (arg1, arg2, op);
9236
76a01679 9237 switch (op)
4c4b4cd2
PH
9238 {
9239 case BINOP_MOD:
9240 case BINOP_DIV:
9241 case BINOP_REM:
9242 break;
9243 default:
9244 return value_binop (arg1, arg2, op);
9245 }
9246
70050808
TT
9247 gdb_mpz v2 = value_as_mpz (arg2);
9248 if (v2.sgn () == 0)
b0f9164c
TT
9249 {
9250 const char *name;
9251 if (op == BINOP_MOD)
9252 name = "mod";
9253 else if (op == BINOP_DIV)
9254 name = "/";
9255 else
9256 {
9257 gdb_assert (op == BINOP_REM);
9258 name = "rem";
9259 }
9260
9261 error (_("second operand of %s must not be zero."), name);
9262 }
4c4b4cd2 9263
c6d940a9 9264 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9265 return value_binop (arg1, arg2, op);
9266
70050808
TT
9267 gdb_mpz v1 = value_as_mpz (arg1);
9268 gdb_mpz v;
4c4b4cd2
PH
9269 switch (op)
9270 {
9271 case BINOP_DIV:
9272 v = v1 / v2;
4c4b4cd2
PH
9273 break;
9274 case BINOP_REM:
9275 v = v1 % v2;
76a01679 9276 if (v * v1 < 0)
dda83cd7 9277 v -= v2;
4c4b4cd2
PH
9278 break;
9279 default:
9280 /* Should not reach this point. */
70050808 9281 gdb_assert_not_reached ("invalid operator");
4c4b4cd2
PH
9282 }
9283
70050808 9284 return value_from_mpz (type1, v);
4c4b4cd2
PH
9285}
9286
9287static int
9288ada_value_equal (struct value *arg1, struct value *arg2)
9289{
d0c97917
TT
9290 if (ada_is_direct_array_type (arg1->type ())
9291 || ada_is_direct_array_type (arg2->type ()))
4c4b4cd2 9292 {
79e8fcaa
JB
9293 struct type *arg1_type, *arg2_type;
9294
f58b38bf 9295 /* Automatically dereference any array reference before
dda83cd7 9296 we attempt to perform the comparison. */
f58b38bf
JB
9297 arg1 = ada_coerce_ref (arg1);
9298 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9299
4c4b4cd2
PH
9300 arg1 = ada_coerce_to_simple_array (arg1);
9301 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa 9302
d0c97917
TT
9303 arg1_type = ada_check_typedef (arg1->type ());
9304 arg2_type = ada_check_typedef (arg2->type ());
79e8fcaa 9305
78134374 9306 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9307 || arg2_type->code () != TYPE_CODE_ARRAY)
9308 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9309 /* FIXME: The following works only for types whose
dda83cd7
SM
9310 representations use all bits (no padding or undefined bits)
9311 and do not have user-defined equality. */
df86565b 9312 return (arg1_type->length () == arg2_type->length ()
efaf1ae0
TT
9313 && memcmp (arg1->contents ().data (),
9314 arg2->contents ().data (),
df86565b 9315 arg1_type->length ()) == 0);
4c4b4cd2
PH
9316 }
9317 return value_equal (arg1, arg2);
9318}
9319
d3c54a1c
TT
9320namespace expr
9321{
9322
9323bool
9324check_objfile (const std::unique_ptr<ada_component> &comp,
9325 struct objfile *objfile)
9326{
9327 return comp->uses_objfile (objfile);
9328}
9329
9330/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9331 component of LHS (a simple array or a record). Does not modify the
9332 inferior's memory, nor does it modify LHS (unless LHS ==
9333 CONTAINER). */
52ce6436
PH
9334
9335static void
9336assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9337 struct expression *exp, operation_up &arg)
52ce6436 9338{
d3c54a1c
TT
9339 scoped_value_mark mark;
9340
52ce6436 9341 struct value *elt;
d0c97917 9342 struct type *lhs_type = check_typedef (lhs->type ());
5b4ee69b 9343
78134374 9344 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9345 {
22601c15
UW
9346 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9347 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9348
52ce6436
PH
9349 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9350 }
9351 else
9352 {
d0c97917 9353 elt = ada_index_struct_field (index, lhs, 0, lhs->type ());
c48db5ca 9354 elt = ada_to_fixed_value (elt);
52ce6436
PH
9355 }
9356
d3c54a1c
TT
9357 ada_aggregate_operation *ag_op
9358 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9359 if (ag_op != nullptr)
9360 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9361 else
d3c54a1c
TT
9362 value_assign_to_component (container, elt,
9363 arg->evaluate (nullptr, exp,
9364 EVAL_NORMAL));
9365}
52ce6436 9366
d3c54a1c
TT
9367bool
9368ada_aggregate_component::uses_objfile (struct objfile *objfile)
9369{
9370 for (const auto &item : m_components)
9371 if (item->uses_objfile (objfile))
9372 return true;
9373 return false;
9374}
9375
9376void
9377ada_aggregate_component::dump (ui_file *stream, int depth)
9378{
6cb06a8c 9379 gdb_printf (stream, _("%*sAggregate\n"), depth, "");
d3c54a1c
TT
9380 for (const auto &item : m_components)
9381 item->dump (stream, depth + 1);
9382}
9383
9384void
9385ada_aggregate_component::assign (struct value *container,
9386 struct value *lhs, struct expression *exp,
9387 std::vector<LONGEST> &indices,
9388 LONGEST low, LONGEST high)
9389{
9390 for (auto &item : m_components)
9391 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9392}
9393
207582c0 9394/* See ada-exp.h. */
52ce6436 9395
207582c0 9396value *
d3c54a1c
TT
9397ada_aggregate_operation::assign_aggregate (struct value *container,
9398 struct value *lhs,
9399 struct expression *exp)
52ce6436
PH
9400{
9401 struct type *lhs_type;
52ce6436 9402 LONGEST low_index, high_index;
52ce6436
PH
9403
9404 container = ada_coerce_ref (container);
d0c97917 9405 if (ada_is_direct_array_type (container->type ()))
52ce6436
PH
9406 container = ada_coerce_to_simple_array (container);
9407 lhs = ada_coerce_ref (lhs);
4b53ca88 9408 if (!lhs->deprecated_modifiable ())
52ce6436
PH
9409 error (_("Left operand of assignment is not a modifiable lvalue."));
9410
d0c97917 9411 lhs_type = check_typedef (lhs->type ());
52ce6436
PH
9412 if (ada_is_direct_array_type (lhs_type))
9413 {
9414 lhs = ada_coerce_to_simple_array (lhs);
d0c97917 9415 lhs_type = check_typedef (lhs->type ());
cf88be68
SM
9416 low_index = lhs_type->bounds ()->low.const_val ();
9417 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9418 }
78134374 9419 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9420 {
9421 low_index = 0;
9422 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9423 }
9424 else
9425 error (_("Left-hand side must be array or record."));
9426
cf608cc4 9427 std::vector<LONGEST> indices (4);
52ce6436
PH
9428 indices[0] = indices[1] = low_index - 1;
9429 indices[2] = indices[3] = high_index + 1;
52ce6436 9430
d3c54a1c
TT
9431 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9432 low_index, high_index);
207582c0
TT
9433
9434 return container;
d3c54a1c
TT
9435}
9436
9437bool
9438ada_positional_component::uses_objfile (struct objfile *objfile)
9439{
9440 return m_op->uses_objfile (objfile);
9441}
52ce6436 9442
d3c54a1c
TT
9443void
9444ada_positional_component::dump (ui_file *stream, int depth)
9445{
6cb06a8c
TT
9446 gdb_printf (stream, _("%*sPositional, index = %d\n"),
9447 depth, "", m_index);
d3c54a1c 9448 m_op->dump (stream, depth + 1);
52ce6436 9449}
d3c54a1c 9450
52ce6436 9451/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9452 construct, given that the positions are relative to lower bound
9453 LOW, where HIGH is the upper bound. Record the position in
9454 INDICES. CONTAINER is as for assign_aggregate. */
9455void
9456ada_positional_component::assign (struct value *container,
9457 struct value *lhs, struct expression *exp,
9458 std::vector<LONGEST> &indices,
9459 LONGEST low, LONGEST high)
52ce6436 9460{
d3c54a1c
TT
9461 LONGEST ind = m_index + low;
9462
52ce6436 9463 if (ind - 1 == high)
e1d5a0d2 9464 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9465 if (ind <= high)
9466 {
cf608cc4 9467 add_component_interval (ind, ind, indices);
d3c54a1c 9468 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9469 }
52ce6436
PH
9470}
9471
d3c54a1c
TT
9472bool
9473ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9474{
9475 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9476}
9477
9478void
9479ada_discrete_range_association::dump (ui_file *stream, int depth)
9480{
6cb06a8c 9481 gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
a88c4354
TT
9482 m_low->dump (stream, depth + 1);
9483 m_high->dump (stream, depth + 1);
9484}
9485
9486void
9487ada_discrete_range_association::assign (struct value *container,
9488 struct value *lhs,
9489 struct expression *exp,
9490 std::vector<LONGEST> &indices,
9491 LONGEST low, LONGEST high,
9492 operation_up &op)
9493{
9494 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9495 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9496
9497 if (lower <= upper && (lower < low || upper > high))
9498 error (_("Index in component association out of bounds."));
9499
9500 add_component_interval (lower, upper, indices);
9501 while (lower <= upper)
9502 {
9503 assign_component (container, lhs, lower, exp, op);
9504 lower += 1;
9505 }
9506}
9507
9508bool
9509ada_name_association::uses_objfile (struct objfile *objfile)
9510{
9511 return m_val->uses_objfile (objfile);
9512}
9513
9514void
9515ada_name_association::dump (ui_file *stream, int depth)
9516{
6cb06a8c 9517 gdb_printf (stream, _("%*sName:\n"), depth, "");
a88c4354
TT
9518 m_val->dump (stream, depth + 1);
9519}
9520
9521void
9522ada_name_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 int index;
9530
d0c97917 9531 if (ada_is_direct_array_type (lhs->type ()))
a88c4354
TT
9532 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9533 EVAL_NORMAL)));
9534 else
9535 {
9536 ada_string_operation *strop
9537 = dynamic_cast<ada_string_operation *> (m_val.get ());
9538
9539 const char *name;
9540 if (strop != nullptr)
9541 name = strop->get_name ();
9542 else
9543 {
9544 ada_var_value_operation *vvo
9545 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
94c5098e 9546 if (vvo == nullptr)
a88c4354
TT
9547 error (_("Invalid record component association."));
9548 name = vvo->get_symbol ()->natural_name ();
94c5098e
TT
9549 /* In this scenario, the user wrote (name => expr), but
9550 write_name_assoc found some fully-qualified name and
9551 substituted it. This happens because, at parse time, the
9552 meaning of the expression isn't known; but here we know
9553 that just the base name was supplied and it refers to the
9554 name of a field. */
9555 name = ada_unqualified_name (name);
a88c4354
TT
9556 }
9557
9558 index = 0;
d0c97917 9559 if (! find_struct_field (name, lhs->type (), 0,
a88c4354
TT
9560 NULL, NULL, NULL, NULL, &index))
9561 error (_("Unknown component name: %s."), name);
9562 }
9563
9564 add_component_interval (index, index, indices);
9565 assign_component (container, lhs, index, exp, op);
9566}
9567
9568bool
9569ada_choices_component::uses_objfile (struct objfile *objfile)
9570{
9571 if (m_op->uses_objfile (objfile))
9572 return true;
9573 for (const auto &item : m_assocs)
9574 if (item->uses_objfile (objfile))
9575 return true;
9576 return false;
9577}
9578
9579void
9580ada_choices_component::dump (ui_file *stream, int depth)
9581{
6cb06a8c 9582 gdb_printf (stream, _("%*sChoices:\n"), depth, "");
a88c4354
TT
9583 m_op->dump (stream, depth + 1);
9584 for (const auto &item : m_assocs)
9585 item->dump (stream, depth + 1);
9586}
9587
9588/* Assign into the components of LHS indexed by the OP_CHOICES
9589 construct at *POS, updating *POS past the construct, given that
9590 the allowable indices are LOW..HIGH. Record the indices assigned
9591 to in INDICES. CONTAINER is as for assign_aggregate. */
9592void
9593ada_choices_component::assign (struct value *container,
9594 struct value *lhs, struct expression *exp,
9595 std::vector<LONGEST> &indices,
9596 LONGEST low, LONGEST high)
9597{
9598 for (auto &item : m_assocs)
9599 item->assign (container, lhs, exp, indices, low, high, m_op);
9600}
9601
9602bool
9603ada_others_component::uses_objfile (struct objfile *objfile)
9604{
9605 return m_op->uses_objfile (objfile);
9606}
9607
9608void
9609ada_others_component::dump (ui_file *stream, int depth)
9610{
6cb06a8c 9611 gdb_printf (stream, _("%*sOthers:\n"), depth, "");
a88c4354
TT
9612 m_op->dump (stream, depth + 1);
9613}
9614
9615/* Assign the value of the expression in the OP_OTHERS construct in
9616 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9617 have not been previously assigned. The index intervals already assigned
9618 are in INDICES. CONTAINER is as for assign_aggregate. */
9619void
9620ada_others_component::assign (struct value *container,
9621 struct value *lhs, struct expression *exp,
9622 std::vector<LONGEST> &indices,
9623 LONGEST low, LONGEST high)
9624{
9625 int num_indices = indices.size ();
9626 for (int i = 0; i < num_indices - 2; i += 2)
9627 {
9628 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9629 assign_component (container, lhs, ind, exp, m_op);
9630 }
9631}
9632
9633struct value *
9634ada_assign_operation::evaluate (struct type *expect_type,
9635 struct expression *exp,
9636 enum noside noside)
9637{
9638 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
b3a27d2f 9639 scoped_restore save_lhs = make_scoped_restore (&m_current, arg1);
a88c4354
TT
9640
9641 ada_aggregate_operation *ag_op
9642 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9643 if (ag_op != nullptr)
9644 {
9645 if (noside != EVAL_NORMAL)
9646 return arg1;
9647
207582c0 9648 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9649 return ada_value_assign (arg1, arg1);
9650 }
9651 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9652 except if the lhs of our assignment is a convenience variable.
9653 In the case of assigning to a convenience variable, the lhs
9654 should be exactly the result of the evaluation of the rhs. */
d0c97917 9655 struct type *type = arg1->type ();
736355f2 9656 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9657 type = NULL;
9658 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9659 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354 9660 return arg1;
736355f2 9661 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9662 {
9663 /* Nothing. */
9664 }
9665 else
d0c97917 9666 arg2 = coerce_for_assign (arg1->type (), arg2);
a88c4354
TT
9667 return ada_value_assign (arg1, arg2);
9668}
9669
9670} /* namespace expr */
9671
cf608cc4
TT
9672/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9673 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9674 overlap. */
52ce6436
PH
9675static void
9676add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9677 std::vector<LONGEST> &indices)
52ce6436
PH
9678{
9679 int i, j;
5b4ee69b 9680
cf608cc4
TT
9681 int size = indices.size ();
9682 for (i = 0; i < size; i += 2) {
52ce6436
PH
9683 if (high >= indices[i] && low <= indices[i + 1])
9684 {
9685 int kh;
5b4ee69b 9686
cf608cc4 9687 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9688 if (high < indices[kh])
9689 break;
9690 if (low < indices[i])
9691 indices[i] = low;
9692 indices[i + 1] = indices[kh - 1];
9693 if (high > indices[i + 1])
9694 indices[i + 1] = high;
cf608cc4
TT
9695 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9696 indices.resize (kh - i - 2);
52ce6436
PH
9697 return;
9698 }
9699 else if (high < indices[i])
9700 break;
9701 }
9702
cf608cc4 9703 indices.resize (indices.size () + 2);
d4813f10 9704 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9705 indices[j] = indices[j - 2];
9706 indices[i] = low;
9707 indices[i + 1] = high;
9708}
9709
6e48bd2c
JB
9710/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9711 is different. */
9712
9713static struct value *
b7e22850 9714ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c 9715{
d0c97917 9716 if (type == ada_check_typedef (arg2->type ()))
6e48bd2c
JB
9717 return arg2;
9718
6e48bd2c
JB
9719 return value_cast (type, arg2);
9720}
9721
284614f0
JB
9722/* Evaluating Ada expressions, and printing their result.
9723 ------------------------------------------------------
9724
21649b50
JB
9725 1. Introduction:
9726 ----------------
9727
284614f0
JB
9728 We usually evaluate an Ada expression in order to print its value.
9729 We also evaluate an expression in order to print its type, which
9730 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9731 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9732 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9733 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9734 similar.
9735
9736 Evaluating expressions is a little more complicated for Ada entities
9737 than it is for entities in languages such as C. The main reason for
9738 this is that Ada provides types whose definition might be dynamic.
9739 One example of such types is variant records. Or another example
9740 would be an array whose bounds can only be known at run time.
9741
9742 The following description is a general guide as to what should be
9743 done (and what should NOT be done) in order to evaluate an expression
9744 involving such types, and when. This does not cover how the semantic
9745 information is encoded by GNAT as this is covered separatly. For the
9746 document used as the reference for the GNAT encoding, see exp_dbug.ads
9747 in the GNAT sources.
9748
9749 Ideally, we should embed each part of this description next to its
9750 associated code. Unfortunately, the amount of code is so vast right
9751 now that it's hard to see whether the code handling a particular
9752 situation might be duplicated or not. One day, when the code is
9753 cleaned up, this guide might become redundant with the comments
9754 inserted in the code, and we might want to remove it.
9755
21649b50
JB
9756 2. ``Fixing'' an Entity, the Simple Case:
9757 -----------------------------------------
9758
284614f0
JB
9759 When evaluating Ada expressions, the tricky issue is that they may
9760 reference entities whose type contents and size are not statically
9761 known. Consider for instance a variant record:
9762
9763 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9764 case Empty is
9765 when True => null;
9766 when False => Value : Integer;
9767 end case;
284614f0
JB
9768 end record;
9769 Yes : Rec := (Empty => False, Value => 1);
9770 No : Rec := (empty => True);
9771
9772 The size and contents of that record depends on the value of the
33b5899f 9773 discriminant (Rec.Empty). At this point, neither the debugging
284614f0
JB
9774 information nor the associated type structure in GDB are able to
9775 express such dynamic types. So what the debugger does is to create
9776 "fixed" versions of the type that applies to the specific object.
30baf67b 9777 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9778 which means creating its associated fixed type.
9779
9780 Example: when printing the value of variable "Yes" above, its fixed
9781 type would look like this:
9782
9783 type Rec is record
dda83cd7
SM
9784 Empty : Boolean;
9785 Value : Integer;
284614f0
JB
9786 end record;
9787
9788 On the other hand, if we printed the value of "No", its fixed type
9789 would become:
9790
9791 type Rec is record
dda83cd7 9792 Empty : Boolean;
284614f0
JB
9793 end record;
9794
9795 Things become a little more complicated when trying to fix an entity
9796 with a dynamic type that directly contains another dynamic type,
9797 such as an array of variant records, for instance. There are
9798 two possible cases: Arrays, and records.
9799
21649b50
JB
9800 3. ``Fixing'' Arrays:
9801 ---------------------
9802
9803 The type structure in GDB describes an array in terms of its bounds,
9804 and the type of its elements. By design, all elements in the array
9805 have the same type and we cannot represent an array of variant elements
9806 using the current type structure in GDB. When fixing an array,
9807 we cannot fix the array element, as we would potentially need one
9808 fixed type per element of the array. As a result, the best we can do
9809 when fixing an array is to produce an array whose bounds and size
9810 are correct (allowing us to read it from memory), but without having
9811 touched its element type. Fixing each element will be done later,
9812 when (if) necessary.
9813
9814 Arrays are a little simpler to handle than records, because the same
9815 amount of memory is allocated for each element of the array, even if
1b536f04 9816 the amount of space actually used by each element differs from element
21649b50 9817 to element. Consider for instance the following array of type Rec:
284614f0
JB
9818
9819 type Rec_Array is array (1 .. 2) of Rec;
9820
1b536f04
JB
9821 The actual amount of memory occupied by each element might be different
9822 from element to element, depending on the value of their discriminant.
21649b50 9823 But the amount of space reserved for each element in the array remains
1b536f04 9824 fixed regardless. So we simply need to compute that size using
21649b50
JB
9825 the debugging information available, from which we can then determine
9826 the array size (we multiply the number of elements of the array by
9827 the size of each element).
9828
9829 The simplest case is when we have an array of a constrained element
9830 type. For instance, consider the following type declarations:
9831
dda83cd7
SM
9832 type Bounded_String (Max_Size : Integer) is
9833 Length : Integer;
9834 Buffer : String (1 .. Max_Size);
9835 end record;
9836 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9837
9838 In this case, the compiler describes the array as an array of
9839 variable-size elements (identified by its XVS suffix) for which
9840 the size can be read in the parallel XVZ variable.
9841
9842 In the case of an array of an unconstrained element type, the compiler
9843 wraps the array element inside a private PAD type. This type should not
9844 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9845 that we also use the adjective "aligner" in our code to designate
9846 these wrapper types.
9847
1b536f04 9848 In some cases, the size allocated for each element is statically
21649b50
JB
9849 known. In that case, the PAD type already has the correct size,
9850 and the array element should remain unfixed.
9851
9852 But there are cases when this size is not statically known.
9853 For instance, assuming that "Five" is an integer variable:
284614f0 9854
dda83cd7
SM
9855 type Dynamic is array (1 .. Five) of Integer;
9856 type Wrapper (Has_Length : Boolean := False) is record
9857 Data : Dynamic;
9858 case Has_Length is
9859 when True => Length : Integer;
9860 when False => null;
9861 end case;
9862 end record;
9863 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9864
dda83cd7
SM
9865 Hello : Wrapper_Array := (others => (Has_Length => True,
9866 Data => (others => 17),
9867 Length => 1));
284614f0
JB
9868
9869
9870 The debugging info would describe variable Hello as being an
9871 array of a PAD type. The size of that PAD type is not statically
9872 known, but can be determined using a parallel XVZ variable.
9873 In that case, a copy of the PAD type with the correct size should
9874 be used for the fixed array.
9875
21649b50
JB
9876 3. ``Fixing'' record type objects:
9877 ----------------------------------
9878
9879 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9880 record types. In this case, in order to compute the associated
9881 fixed type, we need to determine the size and offset of each of
9882 its components. This, in turn, requires us to compute the fixed
9883 type of each of these components.
9884
9885 Consider for instance the example:
9886
dda83cd7
SM
9887 type Bounded_String (Max_Size : Natural) is record
9888 Str : String (1 .. Max_Size);
9889 Length : Natural;
9890 end record;
9891 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9892
9893 In that case, the position of field "Length" depends on the size
9894 of field Str, which itself depends on the value of the Max_Size
21649b50 9895 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9896 we need to fix the type of field Str. Therefore, fixing a variant
9897 record requires us to fix each of its components.
9898
9899 However, if a component does not have a dynamic size, the component
9900 should not be fixed. In particular, fields that use a PAD type
9901 should not fixed. Here is an example where this might happen
9902 (assuming type Rec above):
9903
9904 type Container (Big : Boolean) is record
dda83cd7
SM
9905 First : Rec;
9906 After : Integer;
9907 case Big is
9908 when True => Another : Integer;
9909 when False => null;
9910 end case;
284614f0
JB
9911 end record;
9912 My_Container : Container := (Big => False,
dda83cd7
SM
9913 First => (Empty => True),
9914 After => 42);
284614f0
JB
9915
9916 In that example, the compiler creates a PAD type for component First,
9917 whose size is constant, and then positions the component After just
9918 right after it. The offset of component After is therefore constant
9919 in this case.
9920
9921 The debugger computes the position of each field based on an algorithm
9922 that uses, among other things, the actual position and size of the field
21649b50
JB
9923 preceding it. Let's now imagine that the user is trying to print
9924 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
9925 end up computing the offset of field After based on the size of the
9926 fixed version of field First. And since in our example First has
9927 only one actual field, the size of the fixed type is actually smaller
9928 than the amount of space allocated to that field, and thus we would
9929 compute the wrong offset of field After.
9930
21649b50
JB
9931 To make things more complicated, we need to watch out for dynamic
9932 components of variant records (identified by the ___XVL suffix in
9933 the component name). Even if the target type is a PAD type, the size
9934 of that type might not be statically known. So the PAD type needs
9935 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9936 we might end up with the wrong size for our component. This can be
9937 observed with the following type declarations:
284614f0 9938
dda83cd7
SM
9939 type Octal is new Integer range 0 .. 7;
9940 type Octal_Array is array (Positive range <>) of Octal;
9941 pragma Pack (Octal_Array);
284614f0 9942
dda83cd7
SM
9943 type Octal_Buffer (Size : Positive) is record
9944 Buffer : Octal_Array (1 .. Size);
9945 Length : Integer;
9946 end record;
284614f0
JB
9947
9948 In that case, Buffer is a PAD type whose size is unset and needs
9949 to be computed by fixing the unwrapped type.
9950
21649b50
JB
9951 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9952 ----------------------------------------------------------
9953
9954 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
9955 thus far, be actually fixed?
9956
9957 The answer is: Only when referencing that element. For instance
9958 when selecting one component of a record, this specific component
9959 should be fixed at that point in time. Or when printing the value
9960 of a record, each component should be fixed before its value gets
9961 printed. Similarly for arrays, the element of the array should be
9962 fixed when printing each element of the array, or when extracting
9963 one element out of that array. On the other hand, fixing should
9964 not be performed on the elements when taking a slice of an array!
9965
31432a67 9966 Note that one of the side effects of miscomputing the offset and
284614f0
JB
9967 size of each field is that we end up also miscomputing the size
9968 of the containing type. This can have adverse results when computing
9969 the value of an entity. GDB fetches the value of an entity based
9970 on the size of its type, and thus a wrong size causes GDB to fetch
9971 the wrong amount of memory. In the case where the computed size is
9972 too small, GDB fetches too little data to print the value of our
31432a67 9973 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
9974 past the buffer containing the data =:-o. */
9975
62d4bd94
TT
9976/* A helper function for TERNOP_IN_RANGE. */
9977
9978static value *
9979eval_ternop_in_range (struct type *expect_type, struct expression *exp,
9980 enum noside noside,
9981 value *arg1, value *arg2, value *arg3)
9982{
62d4bd94
TT
9983 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9984 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9985 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
9986 return
9987 value_from_longest (type,
9988 (value_less (arg1, arg3)
9989 || value_equal (arg1, arg3))
9990 && (value_less (arg2, arg1)
9991 || value_equal (arg2, arg1)));
9992}
9993
82390ab8
TT
9994/* A helper function for UNOP_NEG. */
9995
7c15d377 9996value *
82390ab8
TT
9997ada_unop_neg (struct type *expect_type,
9998 struct expression *exp,
9999 enum noside noside, enum exp_opcode op,
10000 struct value *arg1)
10001{
82390ab8
TT
10002 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10003 return value_neg (arg1);
10004}
10005
7efc87ff
TT
10006/* A helper function for UNOP_IN_RANGE. */
10007
95d49dfb 10008value *
7efc87ff
TT
10009ada_unop_in_range (struct type *expect_type,
10010 struct expression *exp,
10011 enum noside noside, enum exp_opcode op,
10012 struct value *arg1, struct type *type)
10013{
7efc87ff
TT
10014 struct value *arg2, *arg3;
10015 switch (type->code ())
10016 {
10017 default:
10018 lim_warning (_("Membership test incompletely implemented; "
10019 "always returns true"));
10020 type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10021 return value_from_longest (type, 1);
7efc87ff
TT
10022
10023 case TYPE_CODE_RANGE:
10024 arg2 = value_from_longest (type,
10025 type->bounds ()->low.const_val ());
10026 arg3 = value_from_longest (type,
10027 type->bounds ()->high.const_val ());
10028 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10029 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10030 type = language_bool_type (exp->language_defn, exp->gdbarch);
10031 return
10032 value_from_longest (type,
10033 (value_less (arg1, arg3)
10034 || value_equal (arg1, arg3))
10035 && (value_less (arg2, arg1)
10036 || value_equal (arg2, arg1)));
10037 }
10038}
10039
020dbabe
TT
10040/* A helper function for OP_ATR_TAG. */
10041
7c15d377 10042value *
020dbabe
TT
10043ada_atr_tag (struct type *expect_type,
10044 struct expression *exp,
10045 enum noside noside, enum exp_opcode op,
10046 struct value *arg1)
10047{
10048 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10049 return value::zero (ada_tag_type (arg1), not_lval);
020dbabe
TT
10050
10051 return ada_value_tag (arg1);
10052}
10053
68c75735
TT
10054/* A helper function for OP_ATR_SIZE. */
10055
7c15d377 10056value *
68c75735
TT
10057ada_atr_size (struct type *expect_type,
10058 struct expression *exp,
10059 enum noside noside, enum exp_opcode op,
10060 struct value *arg1)
10061{
d0c97917 10062 struct type *type = arg1->type ();
68c75735
TT
10063
10064 /* If the argument is a reference, then dereference its type, since
10065 the user is really asking for the size of the actual object,
10066 not the size of the pointer. */
10067 if (type->code () == TYPE_CODE_REF)
27710edb 10068 type = type->target_type ();
68c75735 10069
0b2b0b82 10070 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10071 return value::zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
68c75735
TT
10072 else
10073 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
df86565b 10074 TARGET_CHAR_BIT * type->length ());
68c75735
TT
10075}
10076
d05e24e6
TT
10077/* A helper function for UNOP_ABS. */
10078
7c15d377 10079value *
d05e24e6
TT
10080ada_abs (struct type *expect_type,
10081 struct expression *exp,
10082 enum noside noside, enum exp_opcode op,
10083 struct value *arg1)
10084{
10085 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
ee7bb294 10086 if (value_less (arg1, value::zero (arg1->type (), not_lval)))
d05e24e6
TT
10087 return value_neg (arg1);
10088 else
10089 return arg1;
10090}
10091
faa1dfd7
TT
10092/* A helper function for BINOP_MUL. */
10093
d9e7db06 10094value *
faa1dfd7
TT
10095ada_mult_binop (struct type *expect_type,
10096 struct expression *exp,
10097 enum noside noside, enum exp_opcode op,
10098 struct value *arg1, struct value *arg2)
10099{
10100 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10101 {
10102 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
ee7bb294 10103 return value::zero (arg1->type (), not_lval);
faa1dfd7
TT
10104 }
10105 else
10106 {
10107 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10108 return ada_value_binop (arg1, arg2, op);
10109 }
10110}
10111
214b13ac
TT
10112/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10113
6e8fb7b7 10114value *
214b13ac
TT
10115ada_equal_binop (struct type *expect_type,
10116 struct expression *exp,
10117 enum noside noside, enum exp_opcode op,
10118 struct value *arg1, struct value *arg2)
10119{
10120 int tem;
10121 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10122 tem = 0;
10123 else
10124 {
10125 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10126 tem = ada_value_equal (arg1, arg2);
10127 }
10128 if (op == BINOP_NOTEQUAL)
10129 tem = !tem;
10130 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10131 return value_from_longest (type, tem);
214b13ac
TT
10132}
10133
5ce19db8
TT
10134/* A helper function for TERNOP_SLICE. */
10135
1b1ebfab 10136value *
5ce19db8
TT
10137ada_ternop_slice (struct expression *exp,
10138 enum noside noside,
10139 struct value *array, struct value *low_bound_val,
10140 struct value *high_bound_val)
10141{
10142 LONGEST low_bound;
10143 LONGEST high_bound;
10144
10145 low_bound_val = coerce_ref (low_bound_val);
10146 high_bound_val = coerce_ref (high_bound_val);
10147 low_bound = value_as_long (low_bound_val);
10148 high_bound = value_as_long (high_bound_val);
10149
10150 /* If this is a reference to an aligner type, then remove all
10151 the aligners. */
d0c97917
TT
10152 if (array->type ()->code () == TYPE_CODE_REF
10153 && ada_is_aligner_type (array->type ()->target_type ()))
10154 array->type ()->set_target_type
10155 (ada_aligned_type (array->type ()->target_type ()));
5ce19db8 10156
d0c97917 10157 if (ada_is_any_packed_array_type (array->type ()))
5ce19db8
TT
10158 error (_("cannot slice a packed array"));
10159
10160 /* If this is a reference to an array or an array lvalue,
10161 convert to a pointer. */
d0c97917
TT
10162 if (array->type ()->code () == TYPE_CODE_REF
10163 || (array->type ()->code () == TYPE_CODE_ARRAY
736355f2 10164 && array->lval () == lval_memory))
5ce19db8
TT
10165 array = value_addr (array);
10166
10167 if (noside == EVAL_AVOID_SIDE_EFFECTS
10168 && ada_is_array_descriptor_type (ada_check_typedef
d0c97917 10169 (array->type ())))
5ce19db8
TT
10170 return empty_array (ada_type_of_array (array, 0), low_bound,
10171 high_bound);
10172
10173 array = ada_coerce_to_simple_array_ptr (array);
10174
10175 /* If we have more than one level of pointer indirection,
10176 dereference the value until we get only one level. */
d0c97917
TT
10177 while (array->type ()->code () == TYPE_CODE_PTR
10178 && (array->type ()->target_type ()->code ()
5ce19db8
TT
10179 == TYPE_CODE_PTR))
10180 array = value_ind (array);
10181
10182 /* Make sure we really do have an array type before going further,
10183 to avoid a SEGV when trying to get the index type or the target
10184 type later down the road if the debug info generated by
10185 the compiler is incorrect or incomplete. */
d0c97917 10186 if (!ada_is_simple_array_type (array->type ()))
5ce19db8
TT
10187 error (_("cannot take slice of non-array"));
10188
d0c97917 10189 if (ada_check_typedef (array->type ())->code ()
5ce19db8
TT
10190 == TYPE_CODE_PTR)
10191 {
d0c97917 10192 struct type *type0 = ada_check_typedef (array->type ());
5ce19db8
TT
10193
10194 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
27710edb 10195 return empty_array (type0->target_type (), low_bound, high_bound);
5ce19db8
TT
10196 else
10197 {
10198 struct type *arr_type0 =
27710edb 10199 to_fixed_array_type (type0->target_type (), NULL, 1);
5ce19db8
TT
10200
10201 return ada_value_slice_from_ptr (array, arr_type0,
10202 longest_to_int (low_bound),
10203 longest_to_int (high_bound));
10204 }
10205 }
10206 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10207 return array;
10208 else if (high_bound < low_bound)
d0c97917 10209 return empty_array (array->type (), low_bound, high_bound);
5ce19db8
TT
10210 else
10211 return ada_value_slice (array, longest_to_int (low_bound),
10212 longest_to_int (high_bound));
10213}
10214
b467efaa
TT
10215/* A helper function for BINOP_IN_BOUNDS. */
10216
82c3886e 10217value *
b467efaa
TT
10218ada_binop_in_bounds (struct expression *exp, enum noside noside,
10219 struct value *arg1, struct value *arg2, int n)
10220{
10221 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10222 {
10223 struct type *type = language_bool_type (exp->language_defn,
10224 exp->gdbarch);
ee7bb294 10225 return value::zero (type, not_lval);
b467efaa
TT
10226 }
10227
d0c97917 10228 struct type *type = ada_index_type (arg2->type (), n, "range");
b467efaa 10229 if (!type)
d0c97917 10230 type = arg1->type ();
b467efaa
TT
10231
10232 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10233 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10234
10235 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10236 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10237 type = language_bool_type (exp->language_defn, exp->gdbarch);
10238 return value_from_longest (type,
10239 (value_less (arg1, arg3)
10240 || value_equal (arg1, arg3))
10241 && (value_less (arg2, arg1)
10242 || value_equal (arg2, arg1)));
10243}
10244
b84564fc
TT
10245/* A helper function for some attribute operations. */
10246
10247static value *
10248ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10249 struct value *arg1, struct type *type_arg, int tem)
10250{
1e5ae3d1
TT
10251 const char *attr_name = nullptr;
10252 if (op == OP_ATR_FIRST)
10253 attr_name = "first";
10254 else if (op == OP_ATR_LAST)
10255 attr_name = "last";
10256
b84564fc
TT
10257 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10258 {
10259 if (type_arg == NULL)
d0c97917 10260 type_arg = arg1->type ();
b84564fc
TT
10261
10262 if (ada_is_constrained_packed_array_type (type_arg))
10263 type_arg = decode_constrained_packed_array_type (type_arg);
10264
10265 if (!discrete_type_p (type_arg))
10266 {
10267 switch (op)
10268 {
10269 default: /* Should never happen. */
10270 error (_("unexpected attribute encountered"));
10271 case OP_ATR_FIRST:
10272 case OP_ATR_LAST:
10273 type_arg = ada_index_type (type_arg, tem,
1e5ae3d1 10274 attr_name);
b84564fc
TT
10275 break;
10276 case OP_ATR_LENGTH:
10277 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10278 break;
10279 }
10280 }
10281
ee7bb294 10282 return value::zero (type_arg, not_lval);
b84564fc
TT
10283 }
10284 else if (type_arg == NULL)
10285 {
10286 arg1 = ada_coerce_ref (arg1);
10287
d0c97917 10288 if (ada_is_constrained_packed_array_type (arg1->type ()))
b84564fc
TT
10289 arg1 = ada_coerce_to_simple_array (arg1);
10290
10291 struct type *type;
10292 if (op == OP_ATR_LENGTH)
10293 type = builtin_type (exp->gdbarch)->builtin_int;
10294 else
10295 {
d0c97917 10296 type = ada_index_type (arg1->type (), tem,
1e5ae3d1 10297 attr_name);
b84564fc
TT
10298 if (type == NULL)
10299 type = builtin_type (exp->gdbarch)->builtin_int;
10300 }
10301
10302 switch (op)
10303 {
10304 default: /* Should never happen. */
10305 error (_("unexpected attribute encountered"));
10306 case OP_ATR_FIRST:
10307 return value_from_longest
10308 (type, ada_array_bound (arg1, tem, 0));
10309 case OP_ATR_LAST:
10310 return value_from_longest
10311 (type, ada_array_bound (arg1, tem, 1));
10312 case OP_ATR_LENGTH:
10313 return value_from_longest
10314 (type, ada_array_length (arg1, tem));
10315 }
10316 }
10317 else if (discrete_type_p (type_arg))
10318 {
10319 struct type *range_type;
10320 const char *name = ada_type_name (type_arg);
10321
10322 range_type = NULL;
10323 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10324 range_type = to_fixed_range_type (type_arg, NULL);
10325 if (range_type == NULL)
10326 range_type = type_arg;
10327 switch (op)
10328 {
10329 default:
10330 error (_("unexpected attribute encountered"));
10331 case OP_ATR_FIRST:
10332 return value_from_longest
10333 (range_type, ada_discrete_type_low_bound (range_type));
10334 case OP_ATR_LAST:
10335 return value_from_longest
10336 (range_type, ada_discrete_type_high_bound (range_type));
10337 case OP_ATR_LENGTH:
10338 error (_("the 'length attribute applies only to array types"));
10339 }
10340 }
10341 else if (type_arg->code () == TYPE_CODE_FLT)
10342 error (_("unimplemented type attribute"));
10343 else
10344 {
10345 LONGEST low, high;
10346
10347 if (ada_is_constrained_packed_array_type (type_arg))
10348 type_arg = decode_constrained_packed_array_type (type_arg);
10349
10350 struct type *type;
10351 if (op == OP_ATR_LENGTH)
10352 type = builtin_type (exp->gdbarch)->builtin_int;
10353 else
10354 {
1e5ae3d1 10355 type = ada_index_type (type_arg, tem, attr_name);
b84564fc
TT
10356 if (type == NULL)
10357 type = builtin_type (exp->gdbarch)->builtin_int;
10358 }
10359
10360 switch (op)
10361 {
10362 default:
10363 error (_("unexpected attribute encountered"));
10364 case OP_ATR_FIRST:
10365 low = ada_array_bound_from_type (type_arg, tem, 0);
10366 return value_from_longest (type, low);
10367 case OP_ATR_LAST:
10368 high = ada_array_bound_from_type (type_arg, tem, 1);
10369 return value_from_longest (type, high);
10370 case OP_ATR_LENGTH:
10371 low = ada_array_bound_from_type (type_arg, tem, 0);
10372 high = ada_array_bound_from_type (type_arg, tem, 1);
10373 return value_from_longest (type, high - low + 1);
10374 }
10375 }
10376}
10377
38dc70cf
TT
10378/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10379
6ad3b8bf 10380struct value *
38dc70cf
TT
10381ada_binop_minmax (struct type *expect_type,
10382 struct expression *exp,
10383 enum noside noside, enum exp_opcode op,
10384 struct value *arg1, struct value *arg2)
10385{
10386 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10387 return value::zero (arg1->type (), not_lval);
38dc70cf
TT
10388 else
10389 {
10390 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10391 return value_binop (arg1, arg2, op);
38dc70cf
TT
10392 }
10393}
10394
dd5fd283
TT
10395/* A helper function for BINOP_EXP. */
10396
065ec826 10397struct value *
dd5fd283
TT
10398ada_binop_exp (struct type *expect_type,
10399 struct expression *exp,
10400 enum noside noside, enum exp_opcode op,
10401 struct value *arg1, struct value *arg2)
10402{
10403 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10404 return value::zero (arg1->type (), not_lval);
dd5fd283
TT
10405 else
10406 {
10407 /* For integer exponentiation operations,
10408 only promote the first argument. */
d0c97917 10409 if (is_integral_type (arg2->type ()))
dd5fd283
TT
10410 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10411 else
10412 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10413
10414 return value_binop (arg1, arg2, op);
10415 }
10416}
10417
03070ee9
TT
10418namespace expr
10419{
10420
8b12db26
TT
10421/* See ada-exp.h. */
10422
10423operation_up
10424ada_resolvable::replace (operation_up &&owner,
10425 struct expression *exp,
10426 bool deprocedure_p,
10427 bool parse_completion,
10428 innermost_block_tracker *tracker,
10429 struct type *context_type)
10430{
10431 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10432 return (make_operation<ada_funcall_operation>
10433 (std::move (owner),
10434 std::vector<operation_up> ()));
10435 return std::move (owner);
10436}
10437
c9f66f00 10438/* Convert the character literal whose value would be VAL to the
03adb248
TT
10439 appropriate value of type TYPE, if there is a translation.
10440 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10441 the literal 'A' (VAL == 65), returns 0. */
10442
10443static LONGEST
10444convert_char_literal (struct type *type, LONGEST val)
10445{
c9f66f00 10446 char name[12];
03adb248
TT
10447 int f;
10448
10449 if (type == NULL)
10450 return val;
10451 type = check_typedef (type);
10452 if (type->code () != TYPE_CODE_ENUM)
10453 return val;
10454
10455 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10456 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10457 else if (val >= 0 && val < 256)
10458 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10459 else if (val >= 0 && val < 0x10000)
10460 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10461 else
c9f66f00 10462 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10463 size_t len = strlen (name);
10464 for (f = 0; f < type->num_fields (); f += 1)
10465 {
10466 /* Check the suffix because an enum constant in a package will
10467 have a name like "pkg__QUxx". This is safe enough because we
10468 already have the correct type, and because mangling means
10469 there can't be clashes. */
33d16dd9 10470 const char *ename = type->field (f).name ();
03adb248
TT
10471 size_t elen = strlen (ename);
10472
10473 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10474 return type->field (f).loc_enumval ();
03adb248
TT
10475 }
10476 return val;
10477}
10478
b1b9c411
TT
10479value *
10480ada_char_operation::evaluate (struct type *expect_type,
10481 struct expression *exp,
10482 enum noside noside)
10483{
10484 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10485 if (expect_type != nullptr)
10486 result = ada_value_cast (expect_type, result);
10487 return result;
10488}
10489
03adb248
TT
10490/* See ada-exp.h. */
10491
10492operation_up
10493ada_char_operation::replace (operation_up &&owner,
10494 struct expression *exp,
10495 bool deprocedure_p,
10496 bool parse_completion,
10497 innermost_block_tracker *tracker,
10498 struct type *context_type)
10499{
10500 operation_up result = std::move (owner);
10501
10502 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10503 {
5309ce2f 10504 LONGEST val = as_longest ();
03adb248
TT
10505 gdb_assert (result.get () == this);
10506 std::get<0> (m_storage) = context_type;
5309ce2f 10507 std::get<1> (m_storage) = convert_char_literal (context_type, val);
03adb248
TT
10508 }
10509
b1b9c411 10510 return result;
03adb248
TT
10511}
10512
03070ee9
TT
10513value *
10514ada_wrapped_operation::evaluate (struct type *expect_type,
10515 struct expression *exp,
10516 enum noside noside)
10517{
10518 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10519 if (noside == EVAL_NORMAL)
10520 result = unwrap_value (result);
10521
10522 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10523 then we need to perform the conversion manually, because
10524 evaluate_subexp_standard doesn't do it. This conversion is
10525 necessary in Ada because the different kinds of float/fixed
10526 types in Ada have different representations.
10527
10528 Similarly, we need to perform the conversion from OP_LONG
10529 ourselves. */
10530 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10531 result = ada_value_cast (expect_type, result);
10532
10533 return result;
10534}
10535
013a623f
TT
10536void
10537ada_wrapped_operation::do_generate_ax (struct expression *exp,
10538 struct agent_expr *ax,
10539 struct axs_value *value,
10540 struct type *cast_type)
10541{
10542 std::get<0> (m_storage)->generate_ax (exp, ax, value, cast_type);
10543
10544 struct type *type = value->type;
10545 if (ada_is_aligner_type (type))
10546 error (_("Aligner types cannot be handled in agent expressions"));
10547 else if (find_base_type (type) != nullptr)
10548 error (_("Dynamic types cannot be handled in agent expressions"));
10549}
10550
42fecb61
TT
10551value *
10552ada_string_operation::evaluate (struct type *expect_type,
10553 struct expression *exp,
10554 enum noside noside)
10555{
fc18a21b
TT
10556 struct type *char_type;
10557 if (expect_type != nullptr && ada_is_string_type (expect_type))
10558 char_type = ada_array_element_type (expect_type, 1);
10559 else
10560 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10561
10562 const std::string &str = std::get<0> (m_storage);
10563 const char *encoding;
df86565b 10564 switch (char_type->length ())
fc18a21b
TT
10565 {
10566 case 1:
10567 {
10568 /* Simply copy over the data -- this isn't perhaps strictly
10569 correct according to the encodings, but it is gdb's
10570 historical behavior. */
10571 struct type *stringtype
10572 = lookup_array_range_type (char_type, 1, str.length ());
317c3ed9 10573 struct value *val = value::allocate (stringtype);
bbe912ba 10574 memcpy (val->contents_raw ().data (), str.c_str (),
fc18a21b
TT
10575 str.length ());
10576 return val;
10577 }
10578
10579 case 2:
10580 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10581 encoding = "UTF-16BE";
10582 else
10583 encoding = "UTF-16LE";
10584 break;
10585
10586 case 4:
10587 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10588 encoding = "UTF-32BE";
10589 else
10590 encoding = "UTF-32LE";
10591 break;
10592
10593 default:
10594 error (_("unexpected character type size %s"),
df86565b 10595 pulongest (char_type->length ()));
fc18a21b
TT
10596 }
10597
10598 auto_obstack converted;
10599 convert_between_encodings (host_charset (), encoding,
10600 (const gdb_byte *) str.c_str (),
10601 str.length (), 1,
10602 &converted, translit_none);
10603
10604 struct type *stringtype
10605 = lookup_array_range_type (char_type, 1,
10606 obstack_object_size (&converted)
df86565b 10607 / char_type->length ());
317c3ed9 10608 struct value *val = value::allocate (stringtype);
bbe912ba 10609 memcpy (val->contents_raw ().data (),
fc18a21b
TT
10610 obstack_base (&converted),
10611 obstack_object_size (&converted));
10612 return val;
42fecb61
TT
10613}
10614
b1b9c411
TT
10615value *
10616ada_concat_operation::evaluate (struct type *expect_type,
10617 struct expression *exp,
10618 enum noside noside)
10619{
10620 /* If one side is a literal, evaluate the other side first so that
10621 the expected type can be set properly. */
10622 const operation_up &lhs_expr = std::get<0> (m_storage);
10623 const operation_up &rhs_expr = std::get<1> (m_storage);
10624
10625 value *lhs, *rhs;
10626 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10627 {
10628 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10629 lhs = lhs_expr->evaluate (rhs->type (), exp, noside);
b1b9c411
TT
10630 }
10631 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10632 {
10633 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10634 struct type *rhs_type = check_typedef (rhs->type ());
b1b9c411
TT
10635 struct type *elt_type = nullptr;
10636 if (rhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10637 elt_type = rhs_type->target_type ();
b1b9c411
TT
10638 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10639 }
10640 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10641 {
10642 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10643 rhs = rhs_expr->evaluate (lhs->type (), exp, noside);
b1b9c411
TT
10644 }
10645 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10646 {
10647 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10648 struct type *lhs_type = check_typedef (lhs->type ());
b1b9c411
TT
10649 struct type *elt_type = nullptr;
10650 if (lhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10651 elt_type = lhs_type->target_type ();
b1b9c411
TT
10652 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10653 }
10654 else
10655 return concat_operation::evaluate (expect_type, exp, noside);
10656
10657 return value_concat (lhs, rhs);
10658}
10659
cc6bd32e
TT
10660value *
10661ada_qual_operation::evaluate (struct type *expect_type,
10662 struct expression *exp,
10663 enum noside noside)
10664{
10665 struct type *type = std::get<1> (m_storage);
10666 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10667}
10668
fc715eb2
TT
10669value *
10670ada_ternop_range_operation::evaluate (struct type *expect_type,
10671 struct expression *exp,
10672 enum noside noside)
10673{
10674 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10675 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10676 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10677 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10678}
10679
73796c73
TT
10680value *
10681ada_binop_addsub_operation::evaluate (struct type *expect_type,
10682 struct expression *exp,
10683 enum noside noside)
10684{
10685 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10686 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10687
5bd5fecd 10688 auto do_op = [this] (LONGEST x, LONGEST y)
73796c73
TT
10689 {
10690 if (std::get<0> (m_storage) == BINOP_ADD)
10691 return x + y;
10692 return x - y;
10693 };
10694
d0c97917 10695 if (arg1->type ()->code () == TYPE_CODE_PTR)
73796c73 10696 return (value_from_longest
d0c97917 10697 (arg1->type (),
73796c73 10698 do_op (value_as_long (arg1), value_as_long (arg2))));
d0c97917 10699 if (arg2->type ()->code () == TYPE_CODE_PTR)
73796c73 10700 return (value_from_longest
d0c97917 10701 (arg2->type (),
73796c73
TT
10702 do_op (value_as_long (arg1), value_as_long (arg2))));
10703 /* Preserve the original type for use by the range case below.
10704 We cannot cast the result to a reference type, so if ARG1 is
10705 a reference type, find its underlying type. */
d0c97917 10706 struct type *type = arg1->type ();
73796c73 10707 while (type->code () == TYPE_CODE_REF)
27710edb 10708 type = type->target_type ();
73796c73
TT
10709 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10710 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10711 /* We need to special-case the result with a range.
10712 This is done for the benefit of "ptype". gdb's Ada support
10713 historically used the LHS to set the result type here, so
10714 preserve this behavior. */
10715 if (type->code () == TYPE_CODE_RANGE)
10716 arg1 = value_cast (type, arg1);
10717 return arg1;
10718}
10719
60fa02ca
TT
10720value *
10721ada_unop_atr_operation::evaluate (struct type *expect_type,
10722 struct expression *exp,
10723 enum noside noside)
10724{
10725 struct type *type_arg = nullptr;
10726 value *val = nullptr;
10727
10728 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10729 {
10730 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10731 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 10732 type_arg = tem->type ();
60fa02ca
TT
10733 }
10734 else
10735 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10736
10737 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10738 val, type_arg, std::get<2> (m_storage));
10739}
10740
3f4a0053
TT
10741value *
10742ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10743 struct expression *exp,
10744 enum noside noside)
10745{
10746 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10747 return value::zero (expect_type, not_lval);
3f4a0053 10748
9c79936b
TT
10749 const bound_minimal_symbol &b = std::get<0> (m_storage);
10750 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10751
10752 val = ada_value_cast (expect_type, val);
10753
10754 /* Follow the Ada language semantics that do not allow taking
10755 an address of the result of a cast (view conversion in Ada). */
736355f2 10756 if (val->lval () == lval_memory)
3f4a0053 10757 {
3ee3b270 10758 if (val->lazy ())
78259c36 10759 val->fetch_lazy ();
6f9c9d71 10760 val->set_lval (not_lval);
3f4a0053
TT
10761 }
10762 return val;
10763}
10764
99a3b1e7
TT
10765value *
10766ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10767 struct expression *exp,
10768 enum noside noside)
10769{
10770 value *val = evaluate_var_value (noside,
9e5e03df
TT
10771 std::get<0> (m_storage).block,
10772 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10773
10774 val = ada_value_cast (expect_type, val);
10775
10776 /* Follow the Ada language semantics that do not allow taking
10777 an address of the result of a cast (view conversion in Ada). */
736355f2 10778 if (val->lval () == lval_memory)
99a3b1e7 10779 {
3ee3b270 10780 if (val->lazy ())
78259c36 10781 val->fetch_lazy ();
6f9c9d71 10782 val->set_lval (not_lval);
99a3b1e7
TT
10783 }
10784 return val;
10785}
10786
10787value *
10788ada_var_value_operation::evaluate (struct type *expect_type,
10789 struct expression *exp,
10790 enum noside noside)
10791{
9e5e03df 10792 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10793
6c9c307c 10794 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10795 /* Only encountered when an unresolved symbol occurs in a
10796 context other than a function call, in which case, it is
10797 invalid. */
10798 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10799 sym->print_name ());
10800
10801 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10802 {
5f9c5a63 10803 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10804 /* Check to see if this is a tagged type. We also need to handle
10805 the case where the type is a reference to a tagged type, but
10806 we have to be careful to exclude pointers to tagged types.
10807 The latter should be shown as usual (as a pointer), whereas
10808 a reference should mostly be transparent to the user. */
10809 if (ada_is_tagged_type (type, 0)
10810 || (type->code () == TYPE_CODE_REF
27710edb 10811 && ada_is_tagged_type (type->target_type (), 0)))
99a3b1e7
TT
10812 {
10813 /* Tagged types are a little special in the fact that the real
10814 type is dynamic and can only be determined by inspecting the
10815 object's tag. This means that we need to get the object's
10816 value first (EVAL_NORMAL) and then extract the actual object
10817 type from its tag.
10818
10819 Note that we cannot skip the final step where we extract
10820 the object type from its tag, because the EVAL_NORMAL phase
10821 results in dynamic components being resolved into fixed ones.
10822 This can cause problems when trying to print the type
10823 description of tagged types whose parent has a dynamic size:
10824 We use the type name of the "_parent" component in order
10825 to print the name of the ancestor type in the type description.
10826 If that component had a dynamic size, the resolution into
10827 a fixed type would result in the loss of that type name,
10828 thus preventing us from printing the name of the ancestor
10829 type in the type description. */
9863c3b5 10830 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10831
10832 if (type->code () != TYPE_CODE_REF)
10833 {
10834 struct type *actual_type;
10835
10836 actual_type = type_from_tag (ada_value_tag (arg1));
10837 if (actual_type == NULL)
10838 /* If, for some reason, we were unable to determine
10839 the actual type from the tag, then use the static
10840 approximation that we just computed as a fallback.
10841 This can happen if the debugging information is
10842 incomplete, for instance. */
10843 actual_type = type;
ee7bb294 10844 return value::zero (actual_type, not_lval);
99a3b1e7
TT
10845 }
10846 else
10847 {
10848 /* In the case of a ref, ada_coerce_ref takes care
10849 of determining the actual type. But the evaluation
10850 should return a ref as it should be valid to ask
10851 for its address; so rebuild a ref after coerce. */
10852 arg1 = ada_coerce_ref (arg1);
10853 return value_ref (arg1, TYPE_CODE_REF);
10854 }
10855 }
10856
10857 /* Records and unions for which GNAT encodings have been
10858 generated need to be statically fixed as well.
10859 Otherwise, non-static fixing produces a type where
10860 all dynamic properties are removed, which prevents "ptype"
10861 from being able to completely describe the type.
10862 For instance, a case statement in a variant record would be
10863 replaced by the relevant components based on the actual
10864 value of the discriminants. */
10865 if ((type->code () == TYPE_CODE_STRUCT
10866 && dynamic_template_type (type) != NULL)
10867 || (type->code () == TYPE_CODE_UNION
10868 && ada_find_parallel_type (type, "___XVU") != NULL))
ee7bb294 10869 return value::zero (to_static_fixed_type (type), not_lval);
99a3b1e7
TT
10870 }
10871
10872 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10873 return ada_to_fixed_value (arg1);
10874}
10875
d8a4ed8a
TT
10876bool
10877ada_var_value_operation::resolve (struct expression *exp,
10878 bool deprocedure_p,
10879 bool parse_completion,
10880 innermost_block_tracker *tracker,
10881 struct type *context_type)
10882{
9e5e03df 10883 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10884 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10885 {
10886 block_symbol resolved
9e5e03df 10887 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10888 context_type, parse_completion,
10889 deprocedure_p, tracker);
9e5e03df 10890 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10891 }
10892
10893 if (deprocedure_p
5f9c5a63 10894 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10895 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10896 return true;
10897
10898 return false;
10899}
10900
013a623f
TT
10901void
10902ada_var_value_operation::do_generate_ax (struct expression *exp,
10903 struct agent_expr *ax,
10904 struct axs_value *value,
10905 struct type *cast_type)
10906{
10907 symbol *sym = std::get<0> (m_storage).symbol;
10908
10909 if (sym->domain () == UNDEF_DOMAIN)
10910 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10911 sym->print_name ());
10912
10913 struct type *type = static_unwrap_type (sym->type ());
10914 if (ada_is_tagged_type (type, 0)
10915 || (type->code () == TYPE_CODE_REF
10916 && ada_is_tagged_type (type->target_type (), 0)))
10917 error (_("Tagged types cannot be handled in agent expressions"));
10918
10919 if ((type->code () == TYPE_CODE_STRUCT
10920 && dynamic_template_type (type) != NULL)
10921 || (type->code () == TYPE_CODE_UNION
10922 && ada_find_parallel_type (type, "___XVU") != NULL))
10923 error (_("Dynamic types cannot be handled in agent expressions"));
10924
10925 var_value_operation::do_generate_ax (exp, ax, value, cast_type);
10926}
10927
e8c33fa1
TT
10928value *
10929ada_unop_ind_operation::evaluate (struct type *expect_type,
10930 struct expression *exp,
10931 enum noside noside)
10932{
10933 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10934
d0c97917 10935 struct type *type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
10936 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10937 {
10938 if (ada_is_array_descriptor_type (type))
10939 /* GDB allows dereferencing GNAT array descriptors. */
10940 {
10941 struct type *arrType = ada_type_of_array (arg1, 0);
10942
10943 if (arrType == NULL)
10944 error (_("Attempt to dereference null array pointer."));
10945 return value_at_lazy (arrType, 0);
10946 }
10947 else if (type->code () == TYPE_CODE_PTR
10948 || type->code () == TYPE_CODE_REF
10949 /* In C you can dereference an array to get the 1st elt. */
10950 || type->code () == TYPE_CODE_ARRAY)
10951 {
10952 /* As mentioned in the OP_VAR_VALUE case, tagged types can
10953 only be determined by inspecting the object's tag.
10954 This means that we need to evaluate completely the
10955 expression in order to get its type. */
10956
10957 if ((type->code () == TYPE_CODE_REF
10958 || type->code () == TYPE_CODE_PTR)
27710edb 10959 && ada_is_tagged_type (type->target_type (), 0))
e8c33fa1
TT
10960 {
10961 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
10962 EVAL_NORMAL);
d0c97917 10963 type = ada_value_ind (arg1)->type ();
e8c33fa1
TT
10964 }
10965 else
10966 {
10967 type = to_static_fixed_type
10968 (ada_aligned_type
27710edb 10969 (ada_check_typedef (type->target_type ())));
e8c33fa1 10970 }
ee7bb294 10971 return value::zero (type, lval_memory);
e8c33fa1
TT
10972 }
10973 else if (type->code () == TYPE_CODE_INT)
10974 {
10975 /* GDB allows dereferencing an int. */
10976 if (expect_type == NULL)
ee7bb294 10977 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
e8c33fa1
TT
10978 lval_memory);
10979 else
10980 {
10981 expect_type =
10982 to_static_fixed_type (ada_aligned_type (expect_type));
ee7bb294 10983 return value::zero (expect_type, lval_memory);
e8c33fa1
TT
10984 }
10985 }
10986 else
10987 error (_("Attempt to take contents of a non-pointer value."));
10988 }
10989 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
d0c97917 10990 type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
10991
10992 if (type->code () == TYPE_CODE_INT)
10993 /* GDB allows dereferencing an int. If we were given
10994 the expect_type, then use that as the target type.
10995 Otherwise, assume that the target type is an int. */
10996 {
10997 if (expect_type != NULL)
10998 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
10999 arg1));
11000 else
11001 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11002 (CORE_ADDR) value_as_address (arg1));
11003 }
11004
11005 if (ada_is_array_descriptor_type (type))
11006 /* GDB allows dereferencing GNAT array descriptors. */
11007 return ada_coerce_to_simple_array (arg1);
11008 else
11009 return ada_value_ind (arg1);
11010}
11011
ebc06ad8
TT
11012value *
11013ada_structop_operation::evaluate (struct type *expect_type,
11014 struct expression *exp,
11015 enum noside noside)
11016{
11017 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11018 const char *str = std::get<1> (m_storage).c_str ();
11019 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11020 {
11021 struct type *type;
d0c97917 11022 struct type *type1 = arg1->type ();
ebc06ad8
TT
11023
11024 if (ada_is_tagged_type (type1, 1))
11025 {
11026 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11027
11028 /* If the field is not found, check if it exists in the
11029 extension of this object's type. This means that we
11030 need to evaluate completely the expression. */
11031
11032 if (type == NULL)
11033 {
11034 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11035 EVAL_NORMAL);
11036 arg1 = ada_value_struct_elt (arg1, str, 0);
11037 arg1 = unwrap_value (arg1);
d0c97917 11038 type = ada_to_fixed_value (arg1)->type ();
ebc06ad8
TT
11039 }
11040 }
11041 else
11042 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11043
ee7bb294 11044 return value::zero (ada_aligned_type (type), lval_memory);
ebc06ad8
TT
11045 }
11046 else
11047 {
11048 arg1 = ada_value_struct_elt (arg1, str, 0);
11049 arg1 = unwrap_value (arg1);
11050 return ada_to_fixed_value (arg1);
11051 }
11052}
11053
efe3af2f
TT
11054value *
11055ada_funcall_operation::evaluate (struct type *expect_type,
11056 struct expression *exp,
11057 enum noside noside)
11058{
11059 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11060 int nargs = args_up.size ();
11061 std::vector<value *> argvec (nargs);
11062 operation_up &callee_op = std::get<0> (m_storage);
11063
11064 ada_var_value_operation *avv
11065 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11066 if (avv != nullptr
6c9c307c 11067 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11068 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11069 avv->get_symbol ()->print_name ());
11070
11071 value *callee = callee_op->evaluate (nullptr, exp, noside);
11072 for (int i = 0; i < args_up.size (); ++i)
11073 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11074
11075 if (ada_is_constrained_packed_array_type
d0c97917 11076 (desc_base_type (callee->type ())))
efe3af2f 11077 callee = ada_coerce_to_simple_array (callee);
d0c97917 11078 else if (callee->type ()->code () == TYPE_CODE_ARRAY
3757d2d4 11079 && callee->type ()->field (0).bitsize () != 0)
efe3af2f
TT
11080 /* This is a packed array that has already been fixed, and
11081 therefore already coerced to a simple array. Nothing further
11082 to do. */
11083 ;
d0c97917 11084 else if (callee->type ()->code () == TYPE_CODE_REF)
efe3af2f
TT
11085 {
11086 /* Make sure we dereference references so that all the code below
11087 feels like it's really handling the referenced value. Wrapping
11088 types (for alignment) may be there, so make sure we strip them as
11089 well. */
11090 callee = ada_to_fixed_value (coerce_ref (callee));
11091 }
d0c97917 11092 else if (callee->type ()->code () == TYPE_CODE_ARRAY
736355f2 11093 && callee->lval () == lval_memory)
efe3af2f
TT
11094 callee = value_addr (callee);
11095
d0c97917 11096 struct type *type = ada_check_typedef (callee->type ());
efe3af2f
TT
11097
11098 /* Ada allows us to implicitly dereference arrays when subscripting
11099 them. So, if this is an array typedef (encoding use for array
11100 access types encoded as fat pointers), strip it now. */
11101 if (type->code () == TYPE_CODE_TYPEDEF)
11102 type = ada_typedef_target_type (type);
11103
11104 if (type->code () == TYPE_CODE_PTR)
11105 {
27710edb 11106 switch (ada_check_typedef (type->target_type ())->code ())
efe3af2f
TT
11107 {
11108 case TYPE_CODE_FUNC:
27710edb 11109 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11110 break;
11111 case TYPE_CODE_ARRAY:
11112 break;
11113 case TYPE_CODE_STRUCT:
11114 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11115 callee = ada_value_ind (callee);
27710edb 11116 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11117 break;
11118 default:
11119 error (_("cannot subscript or call something of type `%s'"),
d0c97917 11120 ada_type_name (callee->type ()));
efe3af2f
TT
11121 break;
11122 }
11123 }
11124
11125 switch (type->code ())
11126 {
11127 case TYPE_CODE_FUNC:
11128 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11129 {
27710edb 11130 if (type->target_type () == NULL)
efe3af2f 11131 error_call_unknown_return_type (NULL);
317c3ed9 11132 return value::allocate (type->target_type ());
efe3af2f 11133 }
61f9fb1e 11134 return call_function_by_hand (callee, expect_type, argvec);
efe3af2f
TT
11135 case TYPE_CODE_INTERNAL_FUNCTION:
11136 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11137 /* We don't know anything about what the internal
11138 function might return, but we have to return
11139 something. */
ee7bb294 11140 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
efe3af2f
TT
11141 not_lval);
11142 else
11143 return call_internal_function (exp->gdbarch, exp->language_defn,
11144 callee, nargs,
11145 argvec.data ());
11146
d3c54a1c
TT
11147 case TYPE_CODE_STRUCT:
11148 {
11149 int arity;
4c4b4cd2 11150
d3c54a1c
TT
11151 arity = ada_array_arity (type);
11152 type = ada_array_element_type (type, nargs);
11153 if (type == NULL)
11154 error (_("cannot subscript or call a record"));
11155 if (arity != nargs)
11156 error (_("wrong number of subscripts; expecting %d"), arity);
11157 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 11158 return value::zero (ada_aligned_type (type), lval_memory);
d3c54a1c
TT
11159 return
11160 unwrap_value (ada_value_subscript
11161 (callee, nargs, argvec.data ()));
11162 }
11163 case TYPE_CODE_ARRAY:
14f9c5c9 11164 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11165 {
d3c54a1c
TT
11166 type = ada_array_element_type (type, nargs);
11167 if (type == NULL)
11168 error (_("element type of array unknown"));
dda83cd7 11169 else
ee7bb294 11170 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11171 }
d3c54a1c
TT
11172 return
11173 unwrap_value (ada_value_subscript
11174 (ada_coerce_to_simple_array (callee),
11175 nargs, argvec.data ()));
11176 case TYPE_CODE_PTR: /* Pointer to array */
11177 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11178 {
27710edb 11179 type = to_fixed_array_type (type->target_type (), NULL, 1);
d3c54a1c
TT
11180 type = ada_array_element_type (type, nargs);
11181 if (type == NULL)
11182 error (_("element type of array unknown"));
96967637 11183 else
ee7bb294 11184 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11185 }
d3c54a1c
TT
11186 return
11187 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11188 argvec.data ()));
6b0d7253 11189
d3c54a1c
TT
11190 default:
11191 error (_("Attempt to index or call something other than an "
11192 "array or function"));
11193 }
11194}
5b4ee69b 11195
d3c54a1c
TT
11196bool
11197ada_funcall_operation::resolve (struct expression *exp,
11198 bool deprocedure_p,
11199 bool parse_completion,
11200 innermost_block_tracker *tracker,
11201 struct type *context_type)
11202{
11203 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11204
d3c54a1c
TT
11205 ada_var_value_operation *avv
11206 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11207 if (avv == nullptr)
11208 return false;
5ec18f2b 11209
d3c54a1c 11210 symbol *sym = avv->get_symbol ();
6c9c307c 11211 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11212 return false;
dda83cd7 11213
d3c54a1c
TT
11214 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11215 int nargs = args_up.size ();
11216 std::vector<value *> argvec (nargs);
284614f0 11217
d3c54a1c
TT
11218 for (int i = 0; i < args_up.size (); ++i)
11219 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11220
d3c54a1c
TT
11221 const block *block = avv->get_block ();
11222 block_symbol resolved
11223 = ada_resolve_funcall (sym, block,
11224 context_type, parse_completion,
11225 nargs, argvec.data (),
11226 tracker);
11227
11228 std::get<0> (m_storage)
9e5e03df 11229 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11230 return false;
11231}
11232
11233bool
11234ada_ternop_slice_operation::resolve (struct expression *exp,
11235 bool deprocedure_p,
11236 bool parse_completion,
11237 innermost_block_tracker *tracker,
11238 struct type *context_type)
11239{
11240 /* Historically this check was done during resolution, so we
11241 continue that here. */
11242 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11243 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 11244 if (ada_is_any_packed_array_type (v->type ()))
d3c54a1c
TT
11245 error (_("cannot slice a packed array"));
11246 return false;
11247}
14f9c5c9 11248
14f9c5c9 11249}
d3c54a1c 11250
14f9c5c9 11251\f
d2e4a39e 11252
4c4b4cd2
PH
11253/* Return non-zero iff TYPE represents a System.Address type. */
11254
11255int
11256ada_is_system_address_type (struct type *type)
11257{
7d93a1e0 11258 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11259}
11260
14f9c5c9 11261\f
d2e4a39e 11262
dda83cd7 11263 /* Range types */
14f9c5c9
AS
11264
11265/* Scan STR beginning at position K for a discriminant name, and
11266 return the value of that discriminant field of DVAL in *PX. If
11267 PNEW_K is not null, put the position of the character beyond the
11268 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11269 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11270
11271static int
108d56a4 11272scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11273 int *pnew_k)
14f9c5c9 11274{
5f9febe0 11275 static std::string storage;
5da1a4d3 11276 const char *pstart, *pend, *bound;
d2e4a39e 11277 struct value *bound_val;
14f9c5c9
AS
11278
11279 if (dval == NULL || str == NULL || str[k] == '\0')
11280 return 0;
11281
5da1a4d3
SM
11282 pstart = str + k;
11283 pend = strstr (pstart, "__");
14f9c5c9
AS
11284 if (pend == NULL)
11285 {
5da1a4d3 11286 bound = pstart;
14f9c5c9
AS
11287 k += strlen (bound);
11288 }
d2e4a39e 11289 else
14f9c5c9 11290 {
5da1a4d3
SM
11291 int len = pend - pstart;
11292
11293 /* Strip __ and beyond. */
5f9febe0
TT
11294 storage = std::string (pstart, len);
11295 bound = storage.c_str ();
d2e4a39e 11296 k = pend - str;
14f9c5c9 11297 }
d2e4a39e 11298
d0c97917 11299 bound_val = ada_search_struct_field (bound, dval, 0, dval->type ());
14f9c5c9
AS
11300 if (bound_val == NULL)
11301 return 0;
11302
11303 *px = value_as_long (bound_val);
11304 if (pnew_k != NULL)
11305 *pnew_k = k;
11306 return 1;
11307}
11308
25a1127b
TT
11309/* Value of variable named NAME. Only exact matches are considered.
11310 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11311 otherwise causes an error with message ERR_MSG. */
11312
d2e4a39e 11313static struct value *
edb0c9cb 11314get_var_value (const char *name, const char *err_msg)
14f9c5c9 11315{
25a1127b
TT
11316 std::string quoted_name = add_angle_brackets (name);
11317
11318 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11319
d1183b06
TT
11320 std::vector<struct block_symbol> syms
11321 = ada_lookup_symbol_list_worker (lookup_name,
11322 get_selected_block (0),
6c015214 11323 SEARCH_VFT, 1);
14f9c5c9 11324
d1183b06 11325 if (syms.size () != 1)
14f9c5c9
AS
11326 {
11327 if (err_msg == NULL)
dda83cd7 11328 return 0;
14f9c5c9 11329 else
dda83cd7 11330 error (("%s"), err_msg);
14f9c5c9
AS
11331 }
11332
54d343a2 11333 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11334}
d2e4a39e 11335
edb0c9cb
PA
11336/* Value of integer variable named NAME in the current environment.
11337 If no such variable is found, returns false. Otherwise, sets VALUE
11338 to the variable's value and returns true. */
4c4b4cd2 11339
edb0c9cb
PA
11340bool
11341get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11342{
4c4b4cd2 11343 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11344
14f9c5c9 11345 if (var_val == 0)
edb0c9cb
PA
11346 return false;
11347
11348 value = value_as_long (var_val);
11349 return true;
14f9c5c9 11350}
d2e4a39e 11351
14f9c5c9
AS
11352
11353/* Return a range type whose base type is that of the range type named
11354 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11355 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11356 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11357 corresponding range type from debug information; fall back to using it
11358 if symbol lookup fails. If a new type must be created, allocate it
11359 like ORIG_TYPE was. The bounds information, in general, is encoded
11360 in NAME, the base type given in the named range type. */
14f9c5c9 11361
d2e4a39e 11362static struct type *
28c85d6c 11363to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11364{
0d5cff50 11365 const char *name;
14f9c5c9 11366 struct type *base_type;
108d56a4 11367 const char *subtype_info;
14f9c5c9 11368
28c85d6c 11369 gdb_assert (raw_type != NULL);
7d93a1e0 11370 gdb_assert (raw_type->name () != NULL);
dddfab26 11371
78134374 11372 if (raw_type->code () == TYPE_CODE_RANGE)
27710edb 11373 base_type = raw_type->target_type ();
14f9c5c9
AS
11374 else
11375 base_type = raw_type;
11376
7d93a1e0 11377 name = raw_type->name ();
14f9c5c9
AS
11378 subtype_info = strstr (name, "___XD");
11379 if (subtype_info == NULL)
690cc4eb 11380 {
43bbcdc2
PH
11381 LONGEST L = ada_discrete_type_low_bound (raw_type);
11382 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11383
690cc4eb
PH
11384 if (L < INT_MIN || U > INT_MAX)
11385 return raw_type;
11386 else
e727c536
TT
11387 {
11388 type_allocator alloc (raw_type);
11389 return create_static_range_type (alloc, raw_type, L, U);
11390 }
690cc4eb 11391 }
14f9c5c9
AS
11392 else
11393 {
14f9c5c9
AS
11394 int prefix_len = subtype_info - name;
11395 LONGEST L, U;
11396 struct type *type;
108d56a4 11397 const char *bounds_str;
14f9c5c9
AS
11398 int n;
11399
14f9c5c9
AS
11400 subtype_info += 5;
11401 bounds_str = strchr (subtype_info, '_');
11402 n = 1;
11403
d2e4a39e 11404 if (*subtype_info == 'L')
dda83cd7
SM
11405 {
11406 if (!ada_scan_number (bounds_str, n, &L, &n)
11407 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11408 return raw_type;
11409 if (bounds_str[n] == '_')
11410 n += 2;
11411 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11412 n += 1;
11413 subtype_info += 1;
11414 }
d2e4a39e 11415 else
dda83cd7 11416 {
5f9febe0
TT
11417 std::string name_buf = std::string (name, prefix_len) + "___L";
11418 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11419 {
11420 lim_warning (_("Unknown lower bound, using 1."));
11421 L = 1;
11422 }
11423 }
14f9c5c9 11424
d2e4a39e 11425 if (*subtype_info == 'U')
dda83cd7
SM
11426 {
11427 if (!ada_scan_number (bounds_str, n, &U, &n)
11428 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11429 return raw_type;
11430 }
d2e4a39e 11431 else
dda83cd7 11432 {
5f9febe0
TT
11433 std::string name_buf = std::string (name, prefix_len) + "___U";
11434 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11435 {
11436 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11437 U = L;
11438 }
11439 }
14f9c5c9 11440
e727c536
TT
11441 type_allocator alloc (raw_type);
11442 type = create_static_range_type (alloc, base_type, L, U);
f5a91472 11443 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11444 to match the size of the base_type, which is not what we want.
11445 Set it back to the original range type's length. */
df86565b 11446 type->set_length (raw_type->length ());
d0e39ea2 11447 type->set_name (name);
14f9c5c9
AS
11448 return type;
11449 }
11450}
11451
4c4b4cd2
PH
11452/* True iff NAME is the name of a range type. */
11453
14f9c5c9 11454int
d2e4a39e 11455ada_is_range_type_name (const char *name)
14f9c5c9
AS
11456{
11457 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11458}
14f9c5c9 11459\f
d2e4a39e 11460
dda83cd7 11461 /* Modular types */
4c4b4cd2
PH
11462
11463/* True iff TYPE is an Ada modular type. */
14f9c5c9 11464
14f9c5c9 11465int
d2e4a39e 11466ada_is_modular_type (struct type *type)
14f9c5c9 11467{
18af8284 11468 struct type *subranged_type = get_base_type (type);
14f9c5c9 11469
78134374 11470 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11471 && subranged_type->code () == TYPE_CODE_INT
11472 && subranged_type->is_unsigned ());
14f9c5c9
AS
11473}
11474
4c4b4cd2
PH
11475/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11476
61ee279c 11477ULONGEST
0056e4d5 11478ada_modulus (struct type *type)
14f9c5c9 11479{
5e500d33
SM
11480 const dynamic_prop &high = type->bounds ()->high;
11481
9c0fb734 11482 if (high.is_constant ())
5e500d33
SM
11483 return (ULONGEST) high.const_val () + 1;
11484
11485 /* If TYPE is unresolved, the high bound might be a location list. Return
11486 0, for lack of a better value to return. */
11487 return 0;
14f9c5c9 11488}
d2e4a39e 11489\f
f7f9143b
JB
11490
11491/* Ada exception catchpoint support:
11492 ---------------------------------
11493
11494 We support 3 kinds of exception catchpoints:
11495 . catchpoints on Ada exceptions
11496 . catchpoints on unhandled Ada exceptions
11497 . catchpoints on failed assertions
11498
11499 Exceptions raised during failed assertions, or unhandled exceptions
11500 could perfectly be caught with the general catchpoint on Ada exceptions.
11501 However, we can easily differentiate these two special cases, and having
11502 the option to distinguish these two cases from the rest can be useful
11503 to zero-in on certain situations.
11504
11505 Exception catchpoints are a specialized form of breakpoint,
11506 since they rely on inserting breakpoints inside known routines
11507 of the GNAT runtime. The implementation therefore uses a standard
11508 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11509 of breakpoint_ops.
11510
0259addd
JB
11511 Support in the runtime for exception catchpoints have been changed
11512 a few times already, and these changes affect the implementation
11513 of these catchpoints. In order to be able to support several
11514 variants of the runtime, we use a sniffer that will determine
28010a5d 11515 the runtime variant used by the program being debugged. */
f7f9143b 11516
82eacd52
JB
11517/* Ada's standard exceptions.
11518
11519 The Ada 83 standard also defined Numeric_Error. But there so many
11520 situations where it was unclear from the Ada 83 Reference Manual
11521 (RM) whether Constraint_Error or Numeric_Error should be raised,
11522 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11523 Interpretation saying that anytime the RM says that Numeric_Error
11524 should be raised, the implementation may raise Constraint_Error.
11525 Ada 95 went one step further and pretty much removed Numeric_Error
11526 from the list of standard exceptions (it made it a renaming of
11527 Constraint_Error, to help preserve compatibility when compiling
11528 an Ada83 compiler). As such, we do not include Numeric_Error from
11529 this list of standard exceptions. */
3d0b0fa3 11530
27087b7f 11531static const char * const standard_exc[] = {
3d0b0fa3
JB
11532 "constraint_error",
11533 "program_error",
11534 "storage_error",
11535 "tasking_error"
11536};
11537
0259addd
JB
11538typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11539
11540/* A structure that describes how to support exception catchpoints
11541 for a given executable. */
11542
11543struct exception_support_info
11544{
11545 /* The name of the symbol to break on in order to insert
11546 a catchpoint on exceptions. */
11547 const char *catch_exception_sym;
11548
11549 /* The name of the symbol to break on in order to insert
11550 a catchpoint on unhandled exceptions. */
11551 const char *catch_exception_unhandled_sym;
11552
11553 /* The name of the symbol to break on in order to insert
11554 a catchpoint on failed assertions. */
11555 const char *catch_assert_sym;
11556
9f757bf7
XR
11557 /* The name of the symbol to break on in order to insert
11558 a catchpoint on exception handling. */
11559 const char *catch_handlers_sym;
11560
0259addd
JB
11561 /* Assuming that the inferior just triggered an unhandled exception
11562 catchpoint, this function is responsible for returning the address
11563 in inferior memory where the name of that exception is stored.
11564 Return zero if the address could not be computed. */
11565 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11566};
11567
11568static CORE_ADDR ada_unhandled_exception_name_addr (void);
11569static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11570
11571/* The following exception support info structure describes how to
11572 implement exception catchpoints with the latest version of the
ca683e3a 11573 Ada runtime (as of 2019-08-??). */
0259addd
JB
11574
11575static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11576{
11577 "__gnat_debug_raise_exception", /* catch_exception_sym */
11578 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11579 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11580 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11581 ada_unhandled_exception_name_addr
11582};
11583
11584/* The following exception support info structure describes how to
11585 implement exception catchpoints with an earlier version of the
11586 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11587
11588static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11589{
11590 "__gnat_debug_raise_exception", /* catch_exception_sym */
11591 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11592 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11593 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11594 ada_unhandled_exception_name_addr
11595};
11596
11597/* The following exception support info structure describes how to
11598 implement exception catchpoints with a slightly older version
11599 of the Ada runtime. */
11600
11601static const struct exception_support_info exception_support_info_fallback =
11602{
11603 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11604 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11605 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11606 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11607 ada_unhandled_exception_name_addr_from_raise
11608};
11609
f17011e0
JB
11610/* Return nonzero if we can detect the exception support routines
11611 described in EINFO.
11612
11613 This function errors out if an abnormal situation is detected
11614 (for instance, if we find the exception support routines, but
11615 that support is found to be incomplete). */
11616
11617static int
11618ada_has_this_exception_support (const struct exception_support_info *einfo)
11619{
11620 struct symbol *sym;
11621
11622 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11623 that should be compiled with debugging information. As a result, we
11624 expect to find that symbol in the symtabs. */
11625
6c015214 11626 sym = standard_lookup (einfo->catch_exception_sym, NULL, SEARCH_VFT);
f17011e0 11627 if (sym == NULL)
a6af7abe
JB
11628 {
11629 /* Perhaps we did not find our symbol because the Ada runtime was
11630 compiled without debugging info, or simply stripped of it.
11631 It happens on some GNU/Linux distributions for instance, where
11632 users have to install a separate debug package in order to get
11633 the runtime's debugging info. In that situation, let the user
11634 know why we cannot insert an Ada exception catchpoint.
11635
11636 Note: Just for the purpose of inserting our Ada exception
11637 catchpoint, we could rely purely on the associated minimal symbol.
11638 But we would be operating in degraded mode anyway, since we are
11639 still lacking the debugging info needed later on to extract
11640 the name of the exception being raised (this name is printed in
11641 the catchpoint message, and is also used when trying to catch
11642 a specific exception). We do not handle this case for now. */
3b7344d5 11643 struct bound_minimal_symbol msym
1c8e84b0
JB
11644 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11645
60f62e2b 11646 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
a6af7abe
JB
11647 error (_("Your Ada runtime appears to be missing some debugging "
11648 "information.\nCannot insert Ada exception catchpoint "
11649 "in this configuration."));
11650
11651 return 0;
11652 }
f17011e0
JB
11653
11654 /* Make sure that the symbol we found corresponds to a function. */
11655
66d7f48f 11656 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11657 error (_("Symbol \"%s\" is not a function (class = %d)"),
11658 sym->linkage_name (), sym->aclass ());
ca683e3a 11659
6c015214 11660 sym = standard_lookup (einfo->catch_handlers_sym, NULL, SEARCH_VFT);
ca683e3a
AO
11661 if (sym == NULL)
11662 {
11663 struct bound_minimal_symbol msym
11664 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11665
60f62e2b 11666 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
ca683e3a
AO
11667 error (_("Your Ada runtime appears to be missing some debugging "
11668 "information.\nCannot insert Ada exception catchpoint "
11669 "in this configuration."));
11670
11671 return 0;
11672 }
11673
11674 /* Make sure that the symbol we found corresponds to a function. */
11675
66d7f48f 11676 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11677 error (_("Symbol \"%s\" is not a function (class = %d)"),
11678 sym->linkage_name (), sym->aclass ());
f17011e0
JB
11679
11680 return 1;
11681}
11682
0259addd
JB
11683/* Inspect the Ada runtime and determine which exception info structure
11684 should be used to provide support for exception catchpoints.
11685
3eecfa55
JB
11686 This function will always set the per-inferior exception_info,
11687 or raise an error. */
0259addd
JB
11688
11689static void
11690ada_exception_support_info_sniffer (void)
11691{
3eecfa55 11692 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11693
11694 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11695 if (data->exception_info != NULL)
0259addd
JB
11696 return;
11697
11698 /* Check the latest (default) exception support info. */
f17011e0 11699 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11700 {
3eecfa55 11701 data->exception_info = &default_exception_support_info;
0259addd
JB
11702 return;
11703 }
11704
ca683e3a
AO
11705 /* Try the v0 exception suport info. */
11706 if (ada_has_this_exception_support (&exception_support_info_v0))
11707 {
11708 data->exception_info = &exception_support_info_v0;
11709 return;
11710 }
11711
0259addd 11712 /* Try our fallback exception suport info. */
f17011e0 11713 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11714 {
3eecfa55 11715 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11716 return;
11717 }
11718
2c4c710f
TT
11719 throw_error (NOT_FOUND_ERROR,
11720 _("Could not find Ada runtime exception support"));
0259addd
JB
11721}
11722
f7f9143b
JB
11723/* True iff FRAME is very likely to be that of a function that is
11724 part of the runtime system. This is all very heuristic, but is
11725 intended to be used as advice as to what frames are uninteresting
11726 to most users. */
11727
11728static int
bd2b40ac 11729is_known_support_routine (frame_info_ptr frame)
f7f9143b 11730{
692465f1 11731 enum language func_lang;
f7f9143b 11732 int i;
f35a17b5 11733 const char *fullname;
f7f9143b 11734
4ed6b5be
JB
11735 /* If this code does not have any debugging information (no symtab),
11736 This cannot be any user code. */
f7f9143b 11737
51abb421 11738 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11739 if (sal.symtab == NULL)
11740 return 1;
11741
4ed6b5be
JB
11742 /* If there is a symtab, but the associated source file cannot be
11743 located, then assume this is not user code: Selecting a frame
11744 for which we cannot display the code would not be very helpful
11745 for the user. This should also take care of case such as VxWorks
11746 where the kernel has some debugging info provided for a few units. */
f7f9143b 11747
f35a17b5
JK
11748 fullname = symtab_to_fullname (sal.symtab);
11749 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11750 return 1;
11751
85102364 11752 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11753 We also check the name of the objfile against the name of some
11754 known system libraries that sometimes come with debugging info
11755 too. */
11756
f7f9143b
JB
11757 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11758 {
11759 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11760 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11761 return 1;
3c86fae3
SM
11762 if (sal.symtab->compunit ()->objfile () != NULL
11763 && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
dda83cd7 11764 return 1;
f7f9143b
JB
11765 }
11766
4ed6b5be 11767 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11768
c6dc63a1
TT
11769 gdb::unique_xmalloc_ptr<char> func_name
11770 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11771 if (func_name == NULL)
11772 return 1;
11773
11774 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11775 {
11776 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11777 if (re_exec (func_name.get ()))
11778 return 1;
f7f9143b
JB
11779 }
11780
11781 return 0;
11782}
11783
11784/* Find the first frame that contains debugging information and that is not
11785 part of the Ada run-time, starting from FI and moving upward. */
11786
0ef643c8 11787void
bd2b40ac 11788ada_find_printable_frame (frame_info_ptr fi)
f7f9143b
JB
11789{
11790 for (; fi != NULL; fi = get_prev_frame (fi))
11791 {
11792 if (!is_known_support_routine (fi))
dda83cd7
SM
11793 {
11794 select_frame (fi);
11795 break;
11796 }
f7f9143b
JB
11797 }
11798
11799}
11800
11801/* Assuming that the inferior just triggered an unhandled exception
11802 catchpoint, return the address in inferior memory where the name
11803 of the exception is stored.
11804
11805 Return zero if the address could not be computed. */
11806
11807static CORE_ADDR
11808ada_unhandled_exception_name_addr (void)
0259addd
JB
11809{
11810 return parse_and_eval_address ("e.full_name");
11811}
11812
11813/* Same as ada_unhandled_exception_name_addr, except that this function
11814 should be used when the inferior uses an older version of the runtime,
11815 where the exception name needs to be extracted from a specific frame
11816 several frames up in the callstack. */
11817
11818static CORE_ADDR
11819ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11820{
11821 int frame_level;
bd2b40ac 11822 frame_info_ptr fi;
3eecfa55 11823 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11824
11825 /* To determine the name of this exception, we need to select
11826 the frame corresponding to RAISE_SYM_NAME. This frame is
11827 at least 3 levels up, so we simply skip the first 3 frames
11828 without checking the name of their associated function. */
11829 fi = get_current_frame ();
11830 for (frame_level = 0; frame_level < 3; frame_level += 1)
11831 if (fi != NULL)
11832 fi = get_prev_frame (fi);
11833
11834 while (fi != NULL)
11835 {
692465f1
JB
11836 enum language func_lang;
11837
c6dc63a1
TT
11838 gdb::unique_xmalloc_ptr<char> func_name
11839 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11840 if (func_name != NULL)
11841 {
dda83cd7 11842 if (strcmp (func_name.get (),
55b87a52
KS
11843 data->exception_info->catch_exception_sym) == 0)
11844 break; /* We found the frame we were looking for... */
55b87a52 11845 }
fb44b1a7 11846 fi = get_prev_frame (fi);
f7f9143b
JB
11847 }
11848
11849 if (fi == NULL)
11850 return 0;
11851
11852 select_frame (fi);
11853 return parse_and_eval_address ("id.full_name");
11854}
11855
11856/* Assuming the inferior just triggered an Ada exception catchpoint
11857 (of any type), return the address in inferior memory where the name
11858 of the exception is stored, if applicable.
11859
45db7c09
PA
11860 Assumes the selected frame is the current frame.
11861
f7f9143b
JB
11862 Return zero if the address could not be computed, or if not relevant. */
11863
11864static CORE_ADDR
7bd86313 11865ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex)
f7f9143b 11866{
3eecfa55
JB
11867 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11868
f7f9143b
JB
11869 switch (ex)
11870 {
761269c8 11871 case ada_catch_exception:
dda83cd7
SM
11872 return (parse_and_eval_address ("e.full_name"));
11873 break;
f7f9143b 11874
761269c8 11875 case ada_catch_exception_unhandled:
dda83cd7
SM
11876 return data->exception_info->unhandled_exception_name_addr ();
11877 break;
9f757bf7
XR
11878
11879 case ada_catch_handlers:
dda83cd7 11880 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11881 name. */
dda83cd7 11882 break;
9f757bf7 11883
761269c8 11884 case ada_catch_assert:
dda83cd7
SM
11885 return 0; /* Exception name is not relevant in this case. */
11886 break;
f7f9143b
JB
11887
11888 default:
f34652de 11889 internal_error (_("unexpected catchpoint type"));
dda83cd7 11890 break;
f7f9143b
JB
11891 }
11892
11893 return 0; /* Should never be reached. */
11894}
11895
e547c119
JB
11896/* Assuming the inferior is stopped at an exception catchpoint,
11897 return the message which was associated to the exception, if
11898 available. Return NULL if the message could not be retrieved.
11899
e547c119
JB
11900 Note: The exception message can be associated to an exception
11901 either through the use of the Raise_Exception function, or
11902 more simply (Ada 2005 and later), via:
11903
11904 raise Exception_Name with "exception message";
11905
11906 */
11907
6f46ac85 11908static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11909ada_exception_message_1 (void)
11910{
11911 struct value *e_msg_val;
e547c119 11912 int e_msg_len;
e547c119
JB
11913
11914 /* For runtimes that support this feature, the exception message
11915 is passed as an unbounded string argument called "message". */
11916 e_msg_val = parse_and_eval ("message");
11917 if (e_msg_val == NULL)
11918 return NULL; /* Exception message not supported. */
11919
11920 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
11921 gdb_assert (e_msg_val != NULL);
d0c97917 11922 e_msg_len = e_msg_val->type ()->length ();
e547c119
JB
11923
11924 /* If the message string is empty, then treat it as if there was
11925 no exception message. */
11926 if (e_msg_len <= 0)
11927 return NULL;
11928
15f3b077 11929 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
9feb2d07 11930 read_memory (e_msg_val->address (), (gdb_byte *) e_msg.get (),
15f3b077
TT
11931 e_msg_len);
11932 e_msg.get ()[e_msg_len] = '\0';
11933
11934 return e_msg;
e547c119
JB
11935}
11936
11937/* Same as ada_exception_message_1, except that all exceptions are
11938 contained here (returning NULL instead). */
11939
6f46ac85 11940static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11941ada_exception_message (void)
11942{
6f46ac85 11943 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 11944
a70b8144 11945 try
e547c119
JB
11946 {
11947 e_msg = ada_exception_message_1 ();
11948 }
230d2906 11949 catch (const gdb_exception_error &e)
e547c119 11950 {
6f46ac85 11951 e_msg.reset (nullptr);
e547c119 11952 }
e547c119
JB
11953
11954 return e_msg;
11955}
11956
f7f9143b
JB
11957/* Same as ada_exception_name_addr_1, except that it intercepts and contains
11958 any error that ada_exception_name_addr_1 might cause to be thrown.
11959 When an error is intercepted, a warning with the error message is printed,
11960 and zero is returned. */
11961
11962static CORE_ADDR
7bd86313 11963ada_exception_name_addr (enum ada_exception_catchpoint_kind ex)
f7f9143b 11964{
f7f9143b
JB
11965 CORE_ADDR result = 0;
11966
a70b8144 11967 try
f7f9143b 11968 {
7bd86313 11969 result = ada_exception_name_addr_1 (ex);
f7f9143b
JB
11970 }
11971
230d2906 11972 catch (const gdb_exception_error &e)
f7f9143b 11973 {
3d6e9d23 11974 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
11975 return 0;
11976 }
11977
11978 return result;
11979}
11980
cb7de75e 11981static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
11982 (const char *excep_string,
11983 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
11984
11985/* Ada catchpoints.
11986
11987 In the case of catchpoints on Ada exceptions, the catchpoint will
11988 stop the target on every exception the program throws. When a user
11989 specifies the name of a specific exception, we translate this
11990 request into a condition expression (in text form), and then parse
11991 it into an expression stored in each of the catchpoint's locations.
11992 We then use this condition to check whether the exception that was
11993 raised is the one the user is interested in. If not, then the
11994 target is resumed again. We store the name of the requested
11995 exception, in order to be able to re-set the condition expression
11996 when symbols change. */
11997
c1fc2657 11998/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 11999
74421c0b 12000struct ada_catchpoint : public code_breakpoint
28010a5d 12001{
73063f51 12002 ada_catchpoint (struct gdbarch *gdbarch_,
bd21b6c9 12003 enum ada_exception_catchpoint_kind kind,
2c4c710f 12004 const char *cond_string,
bd21b6c9
PA
12005 bool tempflag,
12006 bool enabled,
898db0f7
TT
12007 bool from_tty,
12008 std::string &&excep_string_)
2c4c710f 12009 : code_breakpoint (gdbarch_, bp_catchpoint, tempflag, cond_string),
03f531ea 12010 m_excep_string (std::move (excep_string_)),
73063f51 12011 m_kind (kind)
37f6a7f4 12012 {
74421c0b 12013 /* Unlike most code_breakpoint types, Ada catchpoints are
bd21b6c9 12014 pspace-specific. */
2c4c710f 12015 pspace = current_program_space;
bd21b6c9 12016 enable_state = enabled ? bp_enabled : bp_disabled;
bd21b6c9 12017 language = language_ada;
95f2fe27
TT
12018
12019 re_set ();
37f6a7f4
TT
12020 }
12021
ae72050b
TT
12022 struct bp_location *allocate_location () override;
12023 void re_set () override;
12024 void check_status (struct bpstat *bs) override;
7bd86313 12025 enum print_stop_action print_it (const bpstat *bs) const override;
5e632eca 12026 bool print_one (const bp_location **) const override;
b713485d 12027 void print_mention () const override;
4d1ae558 12028 void print_recreate (struct ui_file *fp) const override;
ae72050b 12029
03f531ea
TT
12030private:
12031
971149cb
TT
12032 /* A helper function for check_status. Returns true if we should
12033 stop for this breakpoint hit. If the user specified a specific
12034 exception, we only want to cause a stop if the program thrown
12035 that exception. */
12036 bool should_stop_exception (const struct bp_location *bl) const;
12037
28010a5d 12038 /* The name of the specific exception the user specified. */
03f531ea 12039 std::string m_excep_string;
37f6a7f4
TT
12040
12041 /* What kind of catchpoint this is. */
12042 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12043};
12044
8cd0bf5e
PA
12045/* An instance of this type is used to represent an Ada catchpoint
12046 breakpoint location. */
12047
12048class ada_catchpoint_location : public bp_location
12049{
12050public:
12051 explicit ada_catchpoint_location (ada_catchpoint *owner)
12052 : bp_location (owner, bp_loc_software_breakpoint)
12053 {}
12054
12055 /* The condition that checks whether the exception that was raised
12056 is the specific exception the user specified on catchpoint
12057 creation. */
12058 expression_up excep_cond_expr;
12059};
12060
2c4c710f
TT
12061static struct symtab_and_line ada_exception_sal
12062 (enum ada_exception_catchpoint_kind ex);
12063
95f2fe27
TT
12064/* Implement the RE_SET method in the structure for all exception
12065 catchpoint kinds. */
28010a5d 12066
95f2fe27
TT
12067void
12068ada_catchpoint::re_set ()
28010a5d 12069{
2c4c710f
TT
12070 std::vector<symtab_and_line> sals;
12071 try
12072 {
12073 struct symtab_and_line sal = ada_exception_sal (m_kind);
12074 sals.push_back (sal);
12075 }
12076 catch (const gdb_exception_error &ex)
12077 {
12078 /* For NOT_FOUND_ERROR, the breakpoint will be pending. */
12079 if (ex.error != NOT_FOUND_ERROR)
12080 throw;
12081 }
12082
12083 update_breakpoint_locations (this, pspace, sals, {});
95f2fe27
TT
12084
12085 /* Reparse the exception conditional expressions. One for each
12086 location. */
12087
28010a5d 12088 /* Nothing to do if there's no specific exception to catch. */
03f531ea 12089 if (m_excep_string.empty ())
28010a5d
PA
12090 return;
12091
12092 /* Same if there are no locations... */
95f2fe27 12093 if (!has_locations ())
28010a5d
PA
12094 return;
12095
fccf9de1 12096 /* Compute the condition expression in text form, from the specific
33b5899f 12097 exception we want to catch. */
fccf9de1 12098 std::string cond_string
03f531ea 12099 = ada_exception_catchpoint_cond_string (m_excep_string.c_str (), m_kind);
28010a5d 12100
fccf9de1
TT
12101 /* Iterate over all the catchpoint's locations, and parse an
12102 expression for each. */
95f2fe27 12103 for (bp_location &bl : locations ())
28010a5d 12104 {
b00b30b2
SM
12105 ada_catchpoint_location &ada_loc
12106 = static_cast<ada_catchpoint_location &> (bl);
4d01a485 12107 expression_up exp;
28010a5d 12108
b00b30b2 12109 if (!bl.shlib_disabled)
28010a5d 12110 {
bbc13ae3 12111 const char *s;
28010a5d 12112
cb7de75e 12113 s = cond_string.c_str ();
a70b8144 12114 try
28010a5d 12115 {
b00b30b2 12116 exp = parse_exp_1 (&s, bl.address, block_for_pc (bl.address), 0);
28010a5d 12117 }
230d2906 12118 catch (const gdb_exception_error &e)
849f2b52
JB
12119 {
12120 warning (_("failed to reevaluate internal exception condition "
12121 "for catchpoint %d: %s"),
95f2fe27 12122 number, e.what ());
849f2b52 12123 }
28010a5d
PA
12124 }
12125
b00b30b2 12126 ada_loc.excep_cond_expr = std::move (exp);
28010a5d 12127 }
28010a5d
PA
12128}
12129
ae72050b
TT
12130/* Implement the ALLOCATE_LOCATION method in the structure for all
12131 exception catchpoint kinds. */
28010a5d 12132
ae72050b
TT
12133struct bp_location *
12134ada_catchpoint::allocate_location ()
28010a5d 12135{
ae72050b 12136 return new ada_catchpoint_location (this);
28010a5d
PA
12137}
12138
971149cb 12139/* See declaration. */
28010a5d 12140
971149cb
TT
12141bool
12142ada_catchpoint::should_stop_exception (const struct bp_location *bl) const
28010a5d 12143{
8e032233 12144 ada_catchpoint *c = gdb::checked_static_cast<ada_catchpoint *> (bl->owner);
28010a5d
PA
12145 const struct ada_catchpoint_location *ada_loc
12146 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12147 bool stop;
28010a5d 12148
37f6a7f4
TT
12149 struct internalvar *var = lookup_internalvar ("_ada_exception");
12150 if (c->m_kind == ada_catch_assert)
12151 clear_internalvar (var);
12152 else
12153 {
12154 try
12155 {
12156 const char *expr;
12157
12158 if (c->m_kind == ada_catch_handlers)
12159 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12160 ".all.occurrence.id");
12161 else
12162 expr = "e";
12163
12164 struct value *exc = parse_and_eval (expr);
12165 set_internalvar (var, exc);
12166 }
12167 catch (const gdb_exception_error &ex)
12168 {
12169 clear_internalvar (var);
12170 }
12171 }
12172
28010a5d 12173 /* With no specific exception, should always stop. */
03f531ea 12174 if (c->m_excep_string.empty ())
7ebaa5f7 12175 return true;
28010a5d
PA
12176
12177 if (ada_loc->excep_cond_expr == NULL)
12178 {
12179 /* We will have a NULL expression if back when we were creating
12180 the expressions, this location's had failed to parse. */
7ebaa5f7 12181 return true;
28010a5d
PA
12182 }
12183
7ebaa5f7 12184 stop = true;
a70b8144 12185 try
28010a5d 12186 {
65558ca5 12187 scoped_value_mark mark;
43048e46 12188 stop = value_true (ada_loc->excep_cond_expr->evaluate ());
28010a5d 12189 }
b1ffd112 12190 catch (const gdb_exception_error &ex)
492d29ea
PA
12191 {
12192 exception_fprintf (gdb_stderr, ex,
12193 _("Error in testing exception condition:\n"));
12194 }
492d29ea 12195
28010a5d
PA
12196 return stop;
12197}
12198
ae72050b
TT
12199/* Implement the CHECK_STATUS method in the structure for all
12200 exception catchpoint kinds. */
28010a5d 12201
ae72050b
TT
12202void
12203ada_catchpoint::check_status (bpstat *bs)
28010a5d 12204{
b6433ede 12205 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12206}
12207
ae72050b
TT
12208/* Implement the PRINT_IT method in the structure for all exception
12209 catchpoint kinds. */
f7f9143b 12210
ae72050b 12211enum print_stop_action
7bd86313 12212ada_catchpoint::print_it (const bpstat *bs) const
f7f9143b 12213{
79a45e25 12214 struct ui_out *uiout = current_uiout;
348d480f 12215
ae72050b 12216 annotate_catchpoint (number);
f7f9143b 12217
112e8700 12218 if (uiout->is_mi_like_p ())
f7f9143b 12219 {
112e8700 12220 uiout->field_string ("reason",
956a9fb9 12221 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ae72050b 12222 uiout->field_string ("disp", bpdisp_text (disposition));
f7f9143b
JB
12223 }
12224
ae72050b 12225 uiout->text (disposition == disp_del
112e8700 12226 ? "\nTemporary catchpoint " : "\nCatchpoint ");
78805ff8 12227 print_num_locno (bs, uiout);
112e8700 12228 uiout->text (", ");
f7f9143b 12229
45db7c09
PA
12230 /* ada_exception_name_addr relies on the selected frame being the
12231 current frame. Need to do this here because this function may be
12232 called more than once when printing a stop, and below, we'll
12233 select the first frame past the Ada run-time (see
12234 ada_find_printable_frame). */
12235 select_frame (get_current_frame ());
12236
ae72050b 12237 switch (m_kind)
f7f9143b 12238 {
761269c8
JB
12239 case ada_catch_exception:
12240 case ada_catch_exception_unhandled:
9f757bf7 12241 case ada_catch_handlers:
956a9fb9 12242 {
7bd86313 12243 const CORE_ADDR addr = ada_exception_name_addr (m_kind);
956a9fb9
JB
12244 char exception_name[256];
12245
12246 if (addr != 0)
12247 {
c714b426
PA
12248 read_memory (addr, (gdb_byte *) exception_name,
12249 sizeof (exception_name) - 1);
956a9fb9
JB
12250 exception_name [sizeof (exception_name) - 1] = '\0';
12251 }
12252 else
12253 {
12254 /* For some reason, we were unable to read the exception
12255 name. This could happen if the Runtime was compiled
12256 without debugging info, for instance. In that case,
12257 just replace the exception name by the generic string
12258 "exception" - it will read as "an exception" in the
12259 notification we are about to print. */
967cff16 12260 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12261 }
12262 /* In the case of unhandled exception breakpoints, we print
12263 the exception name as "unhandled EXCEPTION_NAME", to make
12264 it clearer to the user which kind of catchpoint just got
12265 hit. We used ui_out_text to make sure that this extra
12266 info does not pollute the exception name in the MI case. */
ae72050b 12267 if (m_kind == ada_catch_exception_unhandled)
112e8700
SM
12268 uiout->text ("unhandled ");
12269 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12270 }
12271 break;
761269c8 12272 case ada_catch_assert:
956a9fb9
JB
12273 /* In this case, the name of the exception is not really
12274 important. Just print "failed assertion" to make it clearer
12275 that his program just hit an assertion-failure catchpoint.
12276 We used ui_out_text because this info does not belong in
12277 the MI output. */
112e8700 12278 uiout->text ("failed assertion");
956a9fb9 12279 break;
f7f9143b 12280 }
e547c119 12281
6f46ac85 12282 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12283 if (exception_message != NULL)
12284 {
e547c119 12285 uiout->text (" (");
6f46ac85 12286 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12287 uiout->text (")");
e547c119
JB
12288 }
12289
112e8700 12290 uiout->text (" at ");
956a9fb9 12291 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12292
12293 return PRINT_SRC_AND_LOC;
12294}
12295
ae72050b
TT
12296/* Implement the PRINT_ONE method in the structure for all exception
12297 catchpoint kinds. */
f7f9143b 12298
ae72050b 12299bool
5e632eca 12300ada_catchpoint::print_one (const bp_location **last_loc) const
f7f9143b 12301{
79a45e25 12302 struct ui_out *uiout = current_uiout;
79a45b7d
TT
12303 struct value_print_options opts;
12304
12305 get_user_print_options (&opts);
f06f1252 12306
79a45b7d 12307 if (opts.addressprint)
f06f1252 12308 uiout->field_skip ("addr");
f7f9143b
JB
12309
12310 annotate_field (5);
ae72050b 12311 switch (m_kind)
f7f9143b 12312 {
761269c8 12313 case ada_catch_exception:
03f531ea 12314 if (!m_excep_string.empty ())
dda83cd7 12315 {
bc18fbb5 12316 std::string msg = string_printf (_("`%s' Ada exception"),
03f531ea 12317 m_excep_string.c_str ());
28010a5d 12318
dda83cd7
SM
12319 uiout->field_string ("what", msg);
12320 }
12321 else
12322 uiout->field_string ("what", "all Ada exceptions");
12323
12324 break;
f7f9143b 12325
761269c8 12326 case ada_catch_exception_unhandled:
dda83cd7
SM
12327 uiout->field_string ("what", "unhandled Ada exceptions");
12328 break;
f7f9143b 12329
9f757bf7 12330 case ada_catch_handlers:
03f531ea 12331 if (!m_excep_string.empty ())
dda83cd7 12332 {
9f757bf7
XR
12333 uiout->field_fmt ("what",
12334 _("`%s' Ada exception handlers"),
03f531ea 12335 m_excep_string.c_str ());
dda83cd7
SM
12336 }
12337 else
9f757bf7 12338 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12339 break;
9f757bf7 12340
761269c8 12341 case ada_catch_assert:
dda83cd7
SM
12342 uiout->field_string ("what", "failed Ada assertions");
12343 break;
f7f9143b
JB
12344
12345 default:
f34652de 12346 internal_error (_("unexpected catchpoint type"));
dda83cd7 12347 break;
f7f9143b 12348 }
c01e038b
TT
12349
12350 return true;
f7f9143b
JB
12351}
12352
12353/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12354 for all exception catchpoint kinds. */
12355
ae72050b 12356void
b713485d 12357ada_catchpoint::print_mention () const
f7f9143b 12358{
79a45e25 12359 struct ui_out *uiout = current_uiout;
28010a5d 12360
ae72050b 12361 uiout->text (disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12362 : _("Catchpoint "));
ae72050b 12363 uiout->field_signed ("bkptno", number);
112e8700 12364 uiout->text (": ");
00eb2c4a 12365
ae72050b 12366 switch (m_kind)
f7f9143b 12367 {
761269c8 12368 case ada_catch_exception:
03f531ea 12369 if (!m_excep_string.empty ())
00eb2c4a 12370 {
862d101a 12371 std::string info = string_printf (_("`%s' Ada exception"),
03f531ea 12372 m_excep_string.c_str ());
4915bfdc 12373 uiout->text (info);
00eb2c4a 12374 }
dda83cd7
SM
12375 else
12376 uiout->text (_("all Ada exceptions"));
12377 break;
f7f9143b 12378
761269c8 12379 case ada_catch_exception_unhandled:
dda83cd7
SM
12380 uiout->text (_("unhandled Ada exceptions"));
12381 break;
9f757bf7
XR
12382
12383 case ada_catch_handlers:
03f531ea 12384 if (!m_excep_string.empty ())
9f757bf7
XR
12385 {
12386 std::string info
12387 = string_printf (_("`%s' Ada exception handlers"),
03f531ea 12388 m_excep_string.c_str ());
4915bfdc 12389 uiout->text (info);
9f757bf7 12390 }
dda83cd7
SM
12391 else
12392 uiout->text (_("all Ada exceptions handlers"));
12393 break;
9f757bf7 12394
761269c8 12395 case ada_catch_assert:
dda83cd7
SM
12396 uiout->text (_("failed Ada assertions"));
12397 break;
f7f9143b
JB
12398
12399 default:
f34652de 12400 internal_error (_("unexpected catchpoint type"));
dda83cd7 12401 break;
f7f9143b
JB
12402 }
12403}
12404
ae72050b
TT
12405/* Implement the PRINT_RECREATE method in the structure for all
12406 exception catchpoint kinds. */
6149aea9 12407
ae72050b 12408void
4d1ae558 12409ada_catchpoint::print_recreate (struct ui_file *fp) const
6149aea9 12410{
ae72050b 12411 switch (m_kind)
6149aea9 12412 {
761269c8 12413 case ada_catch_exception:
6cb06a8c 12414 gdb_printf (fp, "catch exception");
03f531ea
TT
12415 if (!m_excep_string.empty ())
12416 gdb_printf (fp, " %s", m_excep_string.c_str ());
6149aea9
PA
12417 break;
12418
761269c8 12419 case ada_catch_exception_unhandled:
6cb06a8c 12420 gdb_printf (fp, "catch exception unhandled");
6149aea9
PA
12421 break;
12422
9f757bf7 12423 case ada_catch_handlers:
6cb06a8c 12424 gdb_printf (fp, "catch handlers");
9f757bf7
XR
12425 break;
12426
761269c8 12427 case ada_catch_assert:
6cb06a8c 12428 gdb_printf (fp, "catch assert");
6149aea9
PA
12429 break;
12430
12431 default:
f34652de 12432 internal_error (_("unexpected catchpoint type"));
6149aea9 12433 }
04d0163c 12434 print_recreate_thread (fp);
6149aea9
PA
12435}
12436
f06f1252
TT
12437/* See ada-lang.h. */
12438
12439bool
12440is_ada_exception_catchpoint (breakpoint *bp)
12441{
ae72050b 12442 return dynamic_cast<ada_catchpoint *> (bp) != nullptr;
f06f1252
TT
12443}
12444
f7f9143b
JB
12445/* Split the arguments specified in a "catch exception" command.
12446 Set EX to the appropriate catchpoint type.
28010a5d 12447 Set EXCEP_STRING to the name of the specific exception if
5845583d 12448 specified by the user.
9f757bf7
XR
12449 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12450 "catch handlers" command. False otherwise.
5845583d
JB
12451 If a condition is found at the end of the arguments, the condition
12452 expression is stored in COND_STRING (memory must be deallocated
12453 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12454
12455static void
a121b7c1 12456catch_ada_exception_command_split (const char *args,
9f757bf7 12457 bool is_catch_handlers_cmd,
dda83cd7 12458 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12459 std::string *excep_string,
12460 std::string *cond_string)
f7f9143b 12461{
bc18fbb5 12462 std::string exception_name;
f7f9143b 12463
bc18fbb5
TT
12464 exception_name = extract_arg (&args);
12465 if (exception_name == "if")
5845583d
JB
12466 {
12467 /* This is not an exception name; this is the start of a condition
12468 expression for a catchpoint on all exceptions. So, "un-get"
12469 this token, and set exception_name to NULL. */
bc18fbb5 12470 exception_name.clear ();
5845583d
JB
12471 args -= 2;
12472 }
f7f9143b 12473
5845583d 12474 /* Check to see if we have a condition. */
f7f9143b 12475
f1735a53 12476 args = skip_spaces (args);
61012eef 12477 if (startswith (args, "if")
5845583d
JB
12478 && (isspace (args[2]) || args[2] == '\0'))
12479 {
12480 args += 2;
f1735a53 12481 args = skip_spaces (args);
5845583d
JB
12482
12483 if (args[0] == '\0')
dda83cd7 12484 error (_("Condition missing after `if' keyword"));
bc18fbb5 12485 *cond_string = args;
5845583d
JB
12486
12487 args += strlen (args);
12488 }
12489
12490 /* Check that we do not have any more arguments. Anything else
12491 is unexpected. */
f7f9143b
JB
12492
12493 if (args[0] != '\0')
12494 error (_("Junk at end of expression"));
12495
9f757bf7
XR
12496 if (is_catch_handlers_cmd)
12497 {
12498 /* Catch handling of exceptions. */
12499 *ex = ada_catch_handlers;
12500 *excep_string = exception_name;
12501 }
bc18fbb5 12502 else if (exception_name.empty ())
f7f9143b
JB
12503 {
12504 /* Catch all exceptions. */
761269c8 12505 *ex = ada_catch_exception;
bc18fbb5 12506 excep_string->clear ();
f7f9143b 12507 }
bc18fbb5 12508 else if (exception_name == "unhandled")
f7f9143b
JB
12509 {
12510 /* Catch unhandled exceptions. */
761269c8 12511 *ex = ada_catch_exception_unhandled;
bc18fbb5 12512 excep_string->clear ();
f7f9143b
JB
12513 }
12514 else
12515 {
12516 /* Catch a specific exception. */
761269c8 12517 *ex = ada_catch_exception;
28010a5d 12518 *excep_string = exception_name;
f7f9143b
JB
12519 }
12520}
12521
12522/* Return the name of the symbol on which we should break in order to
12523 implement a catchpoint of the EX kind. */
12524
12525static const char *
761269c8 12526ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12527{
3eecfa55
JB
12528 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12529
12530 gdb_assert (data->exception_info != NULL);
0259addd 12531
f7f9143b
JB
12532 switch (ex)
12533 {
761269c8 12534 case ada_catch_exception:
dda83cd7
SM
12535 return (data->exception_info->catch_exception_sym);
12536 break;
761269c8 12537 case ada_catch_exception_unhandled:
dda83cd7
SM
12538 return (data->exception_info->catch_exception_unhandled_sym);
12539 break;
761269c8 12540 case ada_catch_assert:
dda83cd7
SM
12541 return (data->exception_info->catch_assert_sym);
12542 break;
9f757bf7 12543 case ada_catch_handlers:
dda83cd7
SM
12544 return (data->exception_info->catch_handlers_sym);
12545 break;
f7f9143b 12546 default:
f34652de 12547 internal_error (_("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12548 }
12549}
12550
f7f9143b
JB
12551/* Return the condition that will be used to match the current exception
12552 being raised with the exception that the user wants to catch. This
12553 assumes that this condition is used when the inferior just triggered
12554 an exception catchpoint.
cb7de75e 12555 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12556
cb7de75e 12557static std::string
9f757bf7 12558ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12559 enum ada_exception_catchpoint_kind ex)
f7f9143b 12560{
fccf9de1 12561 bool is_standard_exc = false;
cb7de75e 12562 std::string result;
9f757bf7
XR
12563
12564 if (ex == ada_catch_handlers)
12565 {
12566 /* For exception handlers catchpoints, the condition string does
dda83cd7 12567 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12568 result = ("long_integer (GNAT_GCC_exception_Access"
12569 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12570 }
12571 else
fccf9de1 12572 result = "long_integer (e)";
3d0b0fa3 12573
0963b4bd 12574 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12575 runtime units that have been compiled without debugging info; if
28010a5d 12576 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12577 exception (e.g. "constraint_error") then, during the evaluation
12578 of the condition expression, the symbol lookup on this name would
0963b4bd 12579 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12580 may then be set only on user-defined exceptions which have the
12581 same not-fully-qualified name (e.g. my_package.constraint_error).
12582
12583 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12584 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12585 exception constraint_error" is rewritten into "catch exception
12586 standard.constraint_error".
12587
85102364 12588 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12589 the inferior program, then the only way to specify this exception as a
12590 breakpoint condition is to use its fully-qualified named:
fccf9de1 12591 e.g. my_package.constraint_error. */
3d0b0fa3 12592
696d6f4d 12593 for (const char *name : standard_exc)
3d0b0fa3 12594 {
696d6f4d 12595 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12596 {
fccf9de1 12597 is_standard_exc = true;
9f757bf7 12598 break;
3d0b0fa3
JB
12599 }
12600 }
9f757bf7 12601
fccf9de1
TT
12602 result += " = ";
12603
12604 if (is_standard_exc)
12605 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12606 else
12607 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12608
9f757bf7 12609 return result;
f7f9143b
JB
12610}
12611
2c4c710f
TT
12612/* Return the symtab_and_line that should be used to insert an
12613 exception catchpoint of the TYPE kind. */
f7f9143b
JB
12614
12615static struct symtab_and_line
2c4c710f 12616ada_exception_sal (enum ada_exception_catchpoint_kind ex)
f7f9143b
JB
12617{
12618 const char *sym_name;
12619 struct symbol *sym;
f7f9143b 12620
0259addd
JB
12621 /* First, find out which exception support info to use. */
12622 ada_exception_support_info_sniffer ();
12623
12624 /* Then lookup the function on which we will break in order to catch
f7f9143b 12625 the Ada exceptions requested by the user. */
f7f9143b 12626 sym_name = ada_exception_sym_name (ex);
6c015214 12627 sym = standard_lookup (sym_name, NULL, SEARCH_VFT);
f7f9143b 12628
57aff202 12629 if (sym == NULL)
2c4c710f
TT
12630 throw_error (NOT_FOUND_ERROR, _("Catchpoint symbol not found: %s"),
12631 sym_name);
57aff202 12632
66d7f48f 12633 if (sym->aclass () != LOC_BLOCK)
57aff202 12634 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b 12635
f17011e0 12636 return find_function_start_sal (sym, 1);
f7f9143b
JB
12637}
12638
b4a5b78b 12639/* Create an Ada exception catchpoint.
f7f9143b 12640
b4a5b78b 12641 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12642
bc18fbb5 12643 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12644 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12645 of the exception to which this catchpoint applies.
2df4d1d5 12646
bc18fbb5 12647 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12648
b4a5b78b
JB
12649 TEMPFLAG, if nonzero, means that the underlying breakpoint
12650 should be temporary.
28010a5d 12651
b4a5b78b 12652 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12653
349774ef 12654void
28010a5d 12655create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12656 enum ada_exception_catchpoint_kind ex_kind,
898db0f7 12657 std::string &&excep_string,
56ecd069 12658 const std::string &cond_string,
28010a5d 12659 int tempflag,
12d67b37 12660 int enabled,
28010a5d
PA
12661 int from_tty)
12662{
bd21b6c9 12663 std::unique_ptr<ada_catchpoint> c
2c4c710f
TT
12664 (new ada_catchpoint (gdbarch, ex_kind,
12665 cond_string.empty () ? nullptr : cond_string.c_str (),
898db0f7
TT
12666 tempflag, enabled, from_tty,
12667 std::move (excep_string)));
b270e6f9 12668 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12669}
12670
9ac4176b
PA
12671/* Implement the "catch exception" command. */
12672
12673static void
eb4c3f4a 12674catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12675 struct cmd_list_element *command)
12676{
a121b7c1 12677 const char *arg = arg_entry;
9ac4176b
PA
12678 struct gdbarch *gdbarch = get_current_arch ();
12679 int tempflag;
761269c8 12680 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12681 std::string excep_string;
56ecd069 12682 std::string cond_string;
9ac4176b 12683
0f8e2034 12684 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12685
12686 if (!arg)
12687 arg = "";
9f757bf7 12688 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12689 &cond_string);
9f757bf7 12690 create_ada_exception_catchpoint (gdbarch, ex_kind,
898db0f7 12691 std::move (excep_string), cond_string,
9f757bf7
XR
12692 tempflag, 1 /* enabled */,
12693 from_tty);
12694}
12695
12696/* Implement the "catch handlers" command. */
12697
12698static void
12699catch_ada_handlers_command (const char *arg_entry, int from_tty,
12700 struct cmd_list_element *command)
12701{
12702 const char *arg = arg_entry;
12703 struct gdbarch *gdbarch = get_current_arch ();
12704 int tempflag;
12705 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12706 std::string excep_string;
56ecd069 12707 std::string cond_string;
9f757bf7 12708
0f8e2034 12709 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12710
12711 if (!arg)
12712 arg = "";
12713 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12714 &cond_string);
b4a5b78b 12715 create_ada_exception_catchpoint (gdbarch, ex_kind,
898db0f7 12716 std::move (excep_string), cond_string,
349774ef
JB
12717 tempflag, 1 /* enabled */,
12718 from_tty);
9ac4176b
PA
12719}
12720
71bed2db
TT
12721/* Completion function for the Ada "catch" commands. */
12722
12723static void
12724catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12725 const char *text, const char *word)
12726{
12727 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12728
12729 for (const ada_exc_info &info : exceptions)
12730 {
12731 if (startswith (info.name, word))
b02f78f9 12732 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12733 }
12734}
12735
b4a5b78b 12736/* Split the arguments specified in a "catch assert" command.
5845583d 12737
b4a5b78b
JB
12738 ARGS contains the command's arguments (or the empty string if
12739 no arguments were passed).
5845583d
JB
12740
12741 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12742 (the memory needs to be deallocated after use). */
5845583d 12743
b4a5b78b 12744static void
56ecd069 12745catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12746{
f1735a53 12747 args = skip_spaces (args);
f7f9143b 12748
5845583d 12749 /* Check whether a condition was provided. */
61012eef 12750 if (startswith (args, "if")
5845583d 12751 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12752 {
5845583d 12753 args += 2;
f1735a53 12754 args = skip_spaces (args);
5845583d 12755 if (args[0] == '\0')
dda83cd7 12756 error (_("condition missing after `if' keyword"));
56ecd069 12757 cond_string.assign (args);
f7f9143b
JB
12758 }
12759
5845583d
JB
12760 /* Otherwise, there should be no other argument at the end of
12761 the command. */
12762 else if (args[0] != '\0')
12763 error (_("Junk at end of arguments."));
f7f9143b
JB
12764}
12765
9ac4176b
PA
12766/* Implement the "catch assert" command. */
12767
12768static void
eb4c3f4a 12769catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12770 struct cmd_list_element *command)
12771{
a121b7c1 12772 const char *arg = arg_entry;
9ac4176b
PA
12773 struct gdbarch *gdbarch = get_current_arch ();
12774 int tempflag;
56ecd069 12775 std::string cond_string;
9ac4176b 12776
0f8e2034 12777 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12778
12779 if (!arg)
12780 arg = "";
56ecd069 12781 catch_ada_assert_command_split (arg, cond_string);
761269c8 12782 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
898db0f7 12783 {}, cond_string,
349774ef
JB
12784 tempflag, 1 /* enabled */,
12785 from_tty);
9ac4176b 12786}
778865d3
JB
12787
12788/* Return non-zero if the symbol SYM is an Ada exception object. */
12789
12790static int
12791ada_is_exception_sym (struct symbol *sym)
12792{
5f9c5a63 12793 const char *type_name = sym->type ()->name ();
778865d3 12794
66d7f48f
SM
12795 return (sym->aclass () != LOC_TYPEDEF
12796 && sym->aclass () != LOC_BLOCK
12797 && sym->aclass () != LOC_CONST
12798 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12799 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12800}
12801
12802/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12803 Ada exception object. This matches all exceptions except the ones
12804 defined by the Ada language. */
12805
12806static int
12807ada_is_non_standard_exception_sym (struct symbol *sym)
12808{
778865d3
JB
12809 if (!ada_is_exception_sym (sym))
12810 return 0;
12811
696d6f4d
TT
12812 for (const char *name : standard_exc)
12813 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12814 return 0; /* A standard exception. */
12815
12816 /* Numeric_Error is also a standard exception, so exclude it.
12817 See the STANDARD_EXC description for more details as to why
12818 this exception is not listed in that array. */
987012b8 12819 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12820 return 0;
12821
12822 return 1;
12823}
12824
ab816a27 12825/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12826 objects.
12827
12828 The comparison is determined first by exception name, and then
12829 by exception address. */
12830
ab816a27 12831bool
cc536b21 12832ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12833{
778865d3
JB
12834 int result;
12835
ab816a27
TT
12836 result = strcmp (name, other.name);
12837 if (result < 0)
12838 return true;
12839 if (result == 0 && addr < other.addr)
12840 return true;
12841 return false;
12842}
778865d3 12843
ab816a27 12844bool
cc536b21 12845ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12846{
12847 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12848}
12849
12850/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12851 routine, but keeping the first SKIP elements untouched.
12852
12853 All duplicates are also removed. */
12854
12855static void
ab816a27 12856sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12857 int skip)
12858{
ab816a27
TT
12859 std::sort (exceptions->begin () + skip, exceptions->end ());
12860 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12861 exceptions->end ());
778865d3
JB
12862}
12863
778865d3
JB
12864/* Add all exceptions defined by the Ada standard whose name match
12865 a regular expression.
12866
12867 If PREG is not NULL, then this regexp_t object is used to
12868 perform the symbol name matching. Otherwise, no name-based
12869 filtering is performed.
12870
12871 EXCEPTIONS is a vector of exceptions to which matching exceptions
12872 gets pushed. */
12873
12874static void
2d7cc5c7 12875ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12876 std::vector<ada_exc_info> *exceptions)
778865d3 12877{
696d6f4d 12878 for (const char *name : standard_exc)
778865d3 12879 {
696d6f4d 12880 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3 12881 {
4326580d
MM
12882 symbol_name_match_type match_type = name_match_type_from_name (name);
12883 lookup_name_info lookup_name (name, match_type);
778865d3 12884
4326580d
MM
12885 symbol_name_matcher_ftype *match_name
12886 = ada_get_symbol_name_matcher (lookup_name);
778865d3 12887
4326580d
MM
12888 /* Iterate over all objfiles irrespective of scope or linker
12889 namespaces so we get all exceptions anywhere in the
12890 progspace. */
12891 for (objfile *objfile : current_program_space->objfiles ())
12892 {
12893 for (minimal_symbol *msymbol : objfile->msymbols ())
12894 {
12895 if (match_name (msymbol->linkage_name (), lookup_name,
12896 nullptr)
12897 && msymbol->type () != mst_solib_trampoline)
12898 {
12899 ada_exc_info info
12900 = {name, msymbol->value_address (objfile)};
12901
12902 exceptions->push_back (info);
12903 }
12904 }
778865d3
JB
12905 }
12906 }
12907 }
12908}
12909
12910/* Add all Ada exceptions defined locally and accessible from the given
12911 FRAME.
12912
12913 If PREG is not NULL, then this regexp_t object is used to
12914 perform the symbol name matching. Otherwise, no name-based
12915 filtering is performed.
12916
12917 EXCEPTIONS is a vector of exceptions to which matching exceptions
12918 gets pushed. */
12919
12920static void
2d7cc5c7 12921ada_add_exceptions_from_frame (compiled_regex *preg,
bd2b40ac 12922 frame_info_ptr frame,
ab816a27 12923 std::vector<ada_exc_info> *exceptions)
778865d3 12924{
3977b71f 12925 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
12926
12927 while (block != 0)
12928 {
548a89df 12929 for (struct symbol *sym : block_iterator_range (block))
778865d3 12930 {
66d7f48f 12931 switch (sym->aclass ())
778865d3
JB
12932 {
12933 case LOC_TYPEDEF:
12934 case LOC_BLOCK:
12935 case LOC_CONST:
12936 break;
12937 default:
12938 if (ada_is_exception_sym (sym))
12939 {
987012b8 12940 struct ada_exc_info info = {sym->print_name (),
4aeddc50 12941 sym->value_address ()};
778865d3 12942
ab816a27 12943 exceptions->push_back (info);
778865d3
JB
12944 }
12945 }
12946 }
6c00f721 12947 if (block->function () != NULL)
778865d3 12948 break;
f135fe72 12949 block = block->superblock ();
778865d3
JB
12950 }
12951}
12952
14bc53a8
PA
12953/* Return true if NAME matches PREG or if PREG is NULL. */
12954
12955static bool
2d7cc5c7 12956name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
12957{
12958 return (preg == NULL
f945dedf 12959 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
12960}
12961
778865d3
JB
12962/* Add all exceptions defined globally whose name name match
12963 a regular expression, excluding standard exceptions.
12964
12965 The reason we exclude standard exceptions is that they need
12966 to be handled separately: Standard exceptions are defined inside
12967 a runtime unit which is normally not compiled with debugging info,
12968 and thus usually do not show up in our symbol search. However,
12969 if the unit was in fact built with debugging info, we need to
12970 exclude them because they would duplicate the entry we found
12971 during the special loop that specifically searches for those
12972 standard exceptions.
12973
12974 If PREG is not NULL, then this regexp_t object is used to
12975 perform the symbol name matching. Otherwise, no name-based
12976 filtering is performed.
12977
12978 EXCEPTIONS is a vector of exceptions to which matching exceptions
12979 gets pushed. */
12980
12981static void
2d7cc5c7 12982ada_add_global_exceptions (compiled_regex *preg,
ab816a27 12983 std::vector<ada_exc_info> *exceptions)
778865d3 12984{
14bc53a8
PA
12985 /* In Ada, the symbol "search name" is a linkage name, whereas the
12986 regular expression used to do the matching refers to the natural
12987 name. So match against the decoded name. */
12988 expand_symtabs_matching (NULL,
b5ec771e 12989 lookup_name_info::match_any (),
14bc53a8
PA
12990 [&] (const char *search_name)
12991 {
f945dedf
CB
12992 std::string decoded = ada_decode (search_name);
12993 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
12994 },
12995 NULL,
03a8ea51 12996 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
c92d4de1 12997 SEARCH_VAR_DOMAIN);
778865d3 12998
4326580d
MM
12999 /* Iterate over all objfiles irrespective of scope or linker namespaces
13000 so we get all exceptions anywhere in the progspace. */
2030c079 13001 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13002 {
b669c953 13003 for (compunit_symtab *s : objfile->compunits ())
778865d3 13004 {
af39c5c8 13005 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13006 int i;
778865d3 13007
d8aeb77f
TT
13008 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13009 {
63d609de 13010 const struct block *b = bv->block (i);
778865d3 13011
548a89df 13012 for (struct symbol *sym : block_iterator_range (b))
d8aeb77f 13013 if (ada_is_non_standard_exception_sym (sym)
987012b8 13014 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13015 {
13016 struct ada_exc_info info
4aeddc50 13017 = {sym->print_name (), sym->value_address ()};
d8aeb77f
TT
13018
13019 exceptions->push_back (info);
13020 }
13021 }
778865d3
JB
13022 }
13023 }
13024}
13025
13026/* Implements ada_exceptions_list with the regular expression passed
13027 as a regex_t, rather than a string.
13028
13029 If not NULL, PREG is used to filter out exceptions whose names
13030 do not match. Otherwise, all exceptions are listed. */
13031
ab816a27 13032static std::vector<ada_exc_info>
2d7cc5c7 13033ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13034{
ab816a27 13035 std::vector<ada_exc_info> result;
778865d3
JB
13036 int prev_len;
13037
13038 /* First, list the known standard exceptions. These exceptions
13039 need to be handled separately, as they are usually defined in
13040 runtime units that have been compiled without debugging info. */
13041
13042 ada_add_standard_exceptions (preg, &result);
13043
13044 /* Next, find all exceptions whose scope is local and accessible
13045 from the currently selected frame. */
13046
13047 if (has_stack_frames ())
13048 {
ab816a27 13049 prev_len = result.size ();
778865d3
JB
13050 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13051 &result);
ab816a27 13052 if (result.size () > prev_len)
778865d3
JB
13053 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13054 }
13055
13056 /* Add all exceptions whose scope is global. */
13057
ab816a27 13058 prev_len = result.size ();
778865d3 13059 ada_add_global_exceptions (preg, &result);
ab816a27 13060 if (result.size () > prev_len)
778865d3
JB
13061 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13062
778865d3
JB
13063 return result;
13064}
13065
13066/* Return a vector of ada_exc_info.
13067
13068 If REGEXP is NULL, all exceptions are included in the result.
13069 Otherwise, it should contain a valid regular expression,
13070 and only the exceptions whose names match that regular expression
13071 are included in the result.
13072
13073 The exceptions are sorted in the following order:
13074 - Standard exceptions (defined by the Ada language), in
13075 alphabetical order;
13076 - Exceptions only visible from the current frame, in
13077 alphabetical order;
13078 - Exceptions whose scope is global, in alphabetical order. */
13079
ab816a27 13080std::vector<ada_exc_info>
778865d3
JB
13081ada_exceptions_list (const char *regexp)
13082{
2d7cc5c7
PA
13083 if (regexp == NULL)
13084 return ada_exceptions_list_1 (NULL);
778865d3 13085
2d7cc5c7
PA
13086 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13087 return ada_exceptions_list_1 (&reg);
778865d3
JB
13088}
13089
13090/* Implement the "info exceptions" command. */
13091
13092static void
1d12d88f 13093info_exceptions_command (const char *regexp, int from_tty)
778865d3 13094{
778865d3 13095 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13096
ab816a27 13097 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13098
13099 if (regexp != NULL)
6cb06a8c 13100 gdb_printf
778865d3
JB
13101 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13102 else
6cb06a8c 13103 gdb_printf (_("All defined Ada exceptions:\n"));
778865d3 13104
ab816a27 13105 for (const ada_exc_info &info : exceptions)
6cb06a8c 13106 gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13107}
13108
6c038f32
PH
13109\f
13110 /* Language vector */
13111
b5ec771e
PA
13112/* symbol_name_matcher_ftype adapter for wild_match. */
13113
13114static bool
13115do_wild_match (const char *symbol_search_name,
13116 const lookup_name_info &lookup_name,
a207cff2 13117 completion_match_result *comp_match_res)
b5ec771e
PA
13118{
13119 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13120}
13121
13122/* symbol_name_matcher_ftype adapter for full_match. */
13123
13124static bool
13125do_full_match (const char *symbol_search_name,
13126 const lookup_name_info &lookup_name,
a207cff2 13127 completion_match_result *comp_match_res)
b5ec771e 13128{
959d6a67
TT
13129 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13130
13131 /* If both symbols start with "_ada_", just let the loop below
13132 handle the comparison. However, if only the symbol name starts
13133 with "_ada_", skip the prefix and let the match proceed as
13134 usual. */
13135 if (startswith (symbol_search_name, "_ada_")
13136 && !startswith (lname, "_ada"))
86b44259 13137 symbol_search_name += 5;
81eaa506
TT
13138 /* Likewise for ghost entities. */
13139 if (startswith (symbol_search_name, "___ghost_")
13140 && !startswith (lname, "___ghost_"))
13141 symbol_search_name += 9;
86b44259 13142
86b44259
TT
13143 int uscore_count = 0;
13144 while (*lname != '\0')
13145 {
13146 if (*symbol_search_name != *lname)
13147 {
13148 if (*symbol_search_name == 'B' && uscore_count == 2
13149 && symbol_search_name[1] == '_')
13150 {
13151 symbol_search_name += 2;
13152 while (isdigit (*symbol_search_name))
13153 ++symbol_search_name;
13154 if (symbol_search_name[0] == '_'
13155 && symbol_search_name[1] == '_')
13156 {
13157 symbol_search_name += 2;
13158 continue;
13159 }
13160 }
13161 return false;
13162 }
13163
13164 if (*symbol_search_name == '_')
13165 ++uscore_count;
13166 else
13167 uscore_count = 0;
13168
13169 ++symbol_search_name;
13170 ++lname;
13171 }
13172
13173 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13174}
13175
a2cd4f14
JB
13176/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13177
13178static bool
13179do_exact_match (const char *symbol_search_name,
13180 const lookup_name_info &lookup_name,
13181 completion_match_result *comp_match_res)
13182{
13183 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13184}
13185
b5ec771e
PA
13186/* Build the Ada lookup name for LOOKUP_NAME. */
13187
13188ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13189{
8082468f 13190 std::string_view user_name = lookup_name.name ();
b5ec771e 13191
6a780b67 13192 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13193 {
13194 if (user_name.back () == '>')
882b0505 13195 m_encoded_name = user_name.substr (1, user_name.size () - 2);
b5ec771e 13196 else
882b0505 13197 m_encoded_name = user_name.substr (1, user_name.size () - 1);
b5ec771e
PA
13198 m_encoded_p = true;
13199 m_verbatim_p = true;
13200 m_wild_match_p = false;
13201 m_standard_p = false;
13202 }
13203 else
13204 {
13205 m_verbatim_p = false;
13206
8082468f 13207 m_encoded_p = user_name.find ("__") != std::string_view::npos;
b5ec771e
PA
13208
13209 if (!m_encoded_p)
13210 {
e0802d59 13211 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13212 m_encoded_name = ada_encode_1 (folded, false);
13213 if (m_encoded_name.empty ())
882b0505 13214 m_encoded_name = user_name;
b5ec771e
PA
13215 }
13216 else
882b0505 13217 m_encoded_name = user_name;
b5ec771e
PA
13218
13219 /* Handle the 'package Standard' special case. See description
13220 of m_standard_p. */
13221 if (startswith (m_encoded_name.c_str (), "standard__"))
13222 {
13223 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13224 m_standard_p = true;
13225 }
13226 else
13227 m_standard_p = false;
74ccd7f5 13228
957ce537
TT
13229 m_decoded_name = ada_decode (m_encoded_name.c_str (), true, false, false);
13230
b5ec771e
PA
13231 /* If the name contains a ".", then the user is entering a fully
13232 qualified entity name, and the match must not be done in wild
13233 mode. Similarly, if the user wants to complete what looks
13234 like an encoded name, the match must not be done in wild
13235 mode. Also, in the standard__ special case always do
13236 non-wild matching. */
13237 m_wild_match_p
13238 = (lookup_name.match_type () != symbol_name_match_type::FULL
13239 && !m_encoded_p
13240 && !m_standard_p
13241 && user_name.find ('.') == std::string::npos);
13242 }
13243}
13244
13245/* symbol_name_matcher_ftype method for Ada. This only handles
13246 completion mode. */
13247
13248static bool
13249ada_symbol_name_matches (const char *symbol_search_name,
13250 const lookup_name_info &lookup_name,
a207cff2 13251 completion_match_result *comp_match_res)
74ccd7f5 13252{
b5ec771e
PA
13253 return lookup_name.ada ().matches (symbol_search_name,
13254 lookup_name.match_type (),
a207cff2 13255 comp_match_res);
b5ec771e
PA
13256}
13257
de63c46b
PA
13258/* A name matcher that matches the symbol name exactly, with
13259 strcmp. */
13260
13261static bool
13262literal_symbol_name_matcher (const char *symbol_search_name,
13263 const lookup_name_info &lookup_name,
13264 completion_match_result *comp_match_res)
13265{
8082468f 13266 std::string_view name_view = lookup_name.name ();
de63c46b 13267
e0802d59
TT
13268 if (lookup_name.completion_mode ()
13269 ? (strncmp (symbol_search_name, name_view.data (),
13270 name_view.size ()) == 0)
13271 : symbol_search_name == name_view)
de63c46b
PA
13272 {
13273 if (comp_match_res != NULL)
13274 comp_match_res->set_match (symbol_search_name);
13275 return true;
13276 }
13277 else
13278 return false;
13279}
13280
c9debfb9 13281/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13282 Ada. */
13283
13284static symbol_name_matcher_ftype *
13285ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13286{
de63c46b
PA
13287 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13288 return literal_symbol_name_matcher;
13289
b5ec771e
PA
13290 if (lookup_name.completion_mode ())
13291 return ada_symbol_name_matches;
74ccd7f5 13292 else
b5ec771e
PA
13293 {
13294 if (lookup_name.ada ().wild_match_p ())
13295 return do_wild_match;
a2cd4f14
JB
13296 else if (lookup_name.ada ().verbatim_p ())
13297 return do_exact_match;
b5ec771e
PA
13298 else
13299 return do_full_match;
13300 }
74ccd7f5
JB
13301}
13302
0874fd07
AB
13303/* Class representing the Ada language. */
13304
13305class ada_language : public language_defn
13306{
13307public:
13308 ada_language ()
0e25e767 13309 : language_defn (language_ada)
0874fd07 13310 { /* Nothing. */ }
5bd40f2a 13311
6f7664a9
AB
13312 /* See language.h. */
13313
13314 const char *name () const override
13315 { return "ada"; }
13316
13317 /* See language.h. */
13318
13319 const char *natural_name () const override
13320 { return "Ada"; }
13321
e171d6f1
AB
13322 /* See language.h. */
13323
13324 const std::vector<const char *> &filename_extensions () const override
13325 {
13326 static const std::vector<const char *> extensions
13327 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13328 return extensions;
13329 }
13330
5bd40f2a
AB
13331 /* Print an array element index using the Ada syntax. */
13332
13333 void print_array_index (struct type *index_type,
13334 LONGEST index,
13335 struct ui_file *stream,
13336 const value_print_options *options) const override
13337 {
13338 struct value *index_value = val_atr (index_type, index);
13339
00c696a6 13340 value_print (index_value, stream, options);
6cb06a8c 13341 gdb_printf (stream, " => ");
5bd40f2a 13342 }
15e5fd35
AB
13343
13344 /* Implement the "read_var_value" language_defn method for Ada. */
13345
13346 struct value *read_var_value (struct symbol *var,
13347 const struct block *var_block,
bd2b40ac 13348 frame_info_ptr frame) const override
15e5fd35
AB
13349 {
13350 /* The only case where default_read_var_value is not sufficient
13351 is when VAR is a renaming... */
13352 if (frame != nullptr)
13353 {
13354 const struct block *frame_block = get_frame_block (frame, NULL);
13355 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13356 return ada_read_renaming_var_value (var, frame_block);
13357 }
13358
13359 /* This is a typical case where we expect the default_read_var_value
13360 function to work. */
13361 return language_defn::read_var_value (var, var_block, frame);
13362 }
1fb314aa 13363
2c71f639 13364 /* See language.h. */
496feb16 13365 bool symbol_printing_suppressed (struct symbol *symbol) const override
2c71f639 13366 {
496feb16 13367 return symbol->is_artificial ();
2c71f639
TV
13368 }
13369
baab3753
AB
13370 /* See language.h. */
13371 struct value *value_string (struct gdbarch *gdbarch,
13372 const char *ptr, ssize_t len) const override
13373 {
13374 struct type *type = language_string_char_type (this, gdbarch);
13375 value *val = ::value_string (ptr, len, type);
13376 /* VAL will be a TYPE_CODE_STRING, but Ada only knows how to print
13377 strings that are arrays of characters, so fix the type now. */
13378 gdb_assert (val->type ()->code () == TYPE_CODE_STRING);
13379 val->type ()->set_code (TYPE_CODE_ARRAY);
13380 return val;
13381 }
13382
1fb314aa
AB
13383 /* See language.h. */
13384 void language_arch_info (struct gdbarch *gdbarch,
13385 struct language_arch_info *lai) const override
13386 {
13387 const struct builtin_type *builtin = builtin_type (gdbarch);
13388
7bea47f0
AB
13389 /* Helper function to allow shorter lines below. */
13390 auto add = [&] (struct type *t)
13391 {
13392 lai->add_primitive_type (t);
13393 };
13394
cc495054 13395 type_allocator alloc (gdbarch);
2d39ccd3 13396 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13397 0, "integer"));
2d39ccd3 13398 add (init_integer_type (alloc, gdbarch_long_bit (gdbarch),
7bea47f0 13399 0, "long_integer"));
2d39ccd3 13400 add (init_integer_type (alloc, gdbarch_short_bit (gdbarch),
7bea47f0 13401 0, "short_integer"));
f50b437c 13402 struct type *char_type = init_character_type (alloc, TARGET_CHAR_BIT,
c9f66f00 13403 1, "character");
7bea47f0
AB
13404 lai->set_string_char_type (char_type);
13405 add (char_type);
f50b437c
TT
13406 add (init_character_type (alloc, 16, 1, "wide_character"));
13407 add (init_character_type (alloc, 32, 1, "wide_wide_character"));
77c5f496 13408 add (init_float_type (alloc, gdbarch_float_bit (gdbarch),
7bea47f0 13409 "float", gdbarch_float_format (gdbarch)));
77c5f496 13410 add (init_float_type (alloc, gdbarch_double_bit (gdbarch),
7bea47f0 13411 "long_float", gdbarch_double_format (gdbarch)));
2d39ccd3 13412 add (init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
7bea47f0 13413 0, "long_long_integer"));
e49831ba
TT
13414 add (init_integer_type (alloc, 128, 0, "long_long_long_integer"));
13415 add (init_integer_type (alloc, 128, 1, "unsigned_long_long_long_integer"));
77c5f496 13416 add (init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
7bea47f0
AB
13417 "long_long_float",
13418 gdbarch_long_double_format (gdbarch)));
2d39ccd3 13419 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13420 0, "natural"));
2d39ccd3 13421 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0
AB
13422 0, "positive"));
13423 add (builtin->builtin_void);
13424
13425 struct type *system_addr_ptr
cc495054
TT
13426 = lookup_pointer_type (alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT,
13427 "void"));
7bea47f0
AB
13428 system_addr_ptr->set_name ("system__address");
13429 add (system_addr_ptr);
1fb314aa
AB
13430
13431 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13432 type. This is a signed integral type whose size is the same as
13433 the size of addresses. */
df86565b 13434 unsigned int addr_length = system_addr_ptr->length ();
2d39ccd3 13435 add (init_integer_type (alloc, addr_length * HOST_CHAR_BIT, 0,
7bea47f0 13436 "storage_offset"));
1fb314aa 13437
7bea47f0 13438 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13439 }
4009ee92
AB
13440
13441 /* See language.h. */
13442
13443 bool iterate_over_symbols
13444 (const struct block *block, const lookup_name_info &name,
6c015214 13445 domain_search_flags domain,
4009ee92
AB
13446 gdb::function_view<symbol_found_callback_ftype> callback) const override
13447 {
d1183b06
TT
13448 std::vector<struct block_symbol> results
13449 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13450 for (block_symbol &sym : results)
13451 {
13452 if (!callback (&sym))
13453 return false;
13454 }
13455
13456 return true;
13457 }
6f827019
AB
13458
13459 /* See language.h. */
3456e70c
TT
13460 bool sniff_from_mangled_name
13461 (const char *mangled,
13462 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13463 {
13464 std::string demangled = ada_decode (mangled);
13465
13466 *out = NULL;
13467
13468 if (demangled != mangled && demangled[0] != '<')
13469 {
13470 /* Set the gsymbol language to Ada, but still return 0.
13471 Two reasons for that:
13472
13473 1. For Ada, we prefer computing the symbol's decoded name
13474 on the fly rather than pre-compute it, in order to save
13475 memory (Ada projects are typically very large).
13476
13477 2. There are some areas in the definition of the GNAT
13478 encoding where, with a bit of bad luck, we might be able
13479 to decode a non-Ada symbol, generating an incorrect
13480 demangled name (Eg: names ending with "TB" for instance
13481 are identified as task bodies and so stripped from
13482 the decoded name returned).
13483
13484 Returning true, here, but not setting *DEMANGLED, helps us get
13485 a little bit of the best of both worlds. Because we're last,
13486 we should not affect any of the other languages that were
13487 able to demangle the symbol before us; we get to correctly
13488 tag Ada symbols as such; and even if we incorrectly tagged a
13489 non-Ada symbol, which should be rare, any routing through the
13490 Ada language should be transparent (Ada tries to behave much
13491 like C/C++ with non-Ada symbols). */
13492 return true;
13493 }
13494
13495 return false;
13496 }
fbfb0a46
AB
13497
13498 /* See language.h. */
13499
3456e70c
TT
13500 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13501 int options) const override
0a50df5d 13502 {
3456e70c 13503 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13504 }
13505
13506 /* See language.h. */
13507
fbfb0a46
AB
13508 void print_type (struct type *type, const char *varstring,
13509 struct ui_file *stream, int show, int level,
13510 const struct type_print_options *flags) const override
13511 {
13512 ada_print_type (type, varstring, stream, show, level, flags);
13513 }
c9debfb9 13514
53fc67f8
AB
13515 /* See language.h. */
13516
13517 const char *word_break_characters (void) const override
13518 {
13519 return ada_completer_word_break_characters;
13520 }
13521
7e56227d
AB
13522 /* See language.h. */
13523
13524 void collect_symbol_completion_matches (completion_tracker &tracker,
13525 complete_symbol_mode mode,
13526 symbol_name_match_type name_match_type,
13527 const char *text, const char *word,
13528 enum type_code code) const override
13529 {
7e56227d 13530 const struct block *b, *surrounding_static_block = 0;
7e56227d
AB
13531
13532 gdb_assert (code == TYPE_CODE_UNDEF);
13533
13534 lookup_name_info lookup_name (text, name_match_type, true);
13535
13536 /* First, look at the partial symtab symbols. */
13537 expand_symtabs_matching (NULL,
13538 lookup_name,
13539 NULL,
13540 NULL,
03a8ea51 13541 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
c92d4de1 13542 SEARCH_ALL);
7e56227d
AB
13543
13544 /* At this point scan through the misc symbol vectors and add each
13545 symbol you find to the list. Eventually we want to ignore
13546 anything that isn't a text symbol (everything else will be
13547 handled by the psymtab code above). */
13548
13549 for (objfile *objfile : current_program_space->objfiles ())
13550 {
13551 for (minimal_symbol *msymbol : objfile->msymbols ())
13552 {
13553 QUIT;
13554
13555 if (completion_skip_symbol (mode, msymbol))
13556 continue;
13557
13558 language symbol_language = msymbol->language ();
13559
13560 /* Ada minimal symbols won't have their language set to Ada. If
13561 we let completion_list_add_name compare using the
13562 default/C-like matcher, then when completing e.g., symbols in a
13563 package named "pck", we'd match internal Ada symbols like
13564 "pckS", which are invalid in an Ada expression, unless you wrap
13565 them in '<' '>' to request a verbatim match.
13566
13567 Unfortunately, some Ada encoded names successfully demangle as
13568 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13569 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13570 with the wrong language set. Paper over that issue here. */
129bce36 13571 if (symbol_language == language_unknown
7e56227d
AB
13572 || symbol_language == language_cplus)
13573 symbol_language = language_ada;
13574
13575 completion_list_add_name (tracker,
13576 symbol_language,
13577 msymbol->linkage_name (),
13578 lookup_name, text, word);
13579 }
13580 }
13581
13582 /* Search upwards from currently selected frame (so that we can
13583 complete on local vars. */
13584
f135fe72 13585 for (b = get_selected_block (0); b != NULL; b = b->superblock ())
7e56227d 13586 {
f135fe72 13587 if (!b->superblock ())
7e56227d
AB
13588 surrounding_static_block = b; /* For elmin of dups */
13589
548a89df 13590 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13591 {
13592 if (completion_skip_symbol (mode, sym))
13593 continue;
13594
13595 completion_list_add_name (tracker,
13596 sym->language (),
13597 sym->linkage_name (),
13598 lookup_name, text, word);
13599 }
13600 }
13601
13602 /* Go through the symtabs and check the externs and statics for
13603 symbols which match. */
13604
13605 for (objfile *objfile : current_program_space->objfiles ())
13606 {
13607 for (compunit_symtab *s : objfile->compunits ())
13608 {
13609 QUIT;
63d609de 13610 b = s->blockvector ()->global_block ();
548a89df 13611 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13612 {
13613 if (completion_skip_symbol (mode, sym))
13614 continue;
13615
13616 completion_list_add_name (tracker,
13617 sym->language (),
13618 sym->linkage_name (),
13619 lookup_name, text, word);
13620 }
13621 }
13622 }
13623
13624 for (objfile *objfile : current_program_space->objfiles ())
13625 {
13626 for (compunit_symtab *s : objfile->compunits ())
13627 {
13628 QUIT;
63d609de 13629 b = s->blockvector ()->static_block ();
7e56227d
AB
13630 /* Don't do this block twice. */
13631 if (b == surrounding_static_block)
13632 continue;
548a89df 13633 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13634 {
13635 if (completion_skip_symbol (mode, sym))
13636 continue;
13637
13638 completion_list_add_name (tracker,
13639 sym->language (),
13640 sym->linkage_name (),
13641 lookup_name, text, word);
13642 }
13643 }
13644 }
13645 }
13646
f16a9f57
AB
13647 /* See language.h. */
13648
13649 gdb::unique_xmalloc_ptr<char> watch_location_expression
13650 (struct type *type, CORE_ADDR addr) const override
13651 {
27710edb 13652 type = check_typedef (check_typedef (type)->target_type ());
f16a9f57 13653 std::string name = type_to_string (type);
8579fd13 13654 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13655 }
13656
a1d1fa3e
AB
13657 /* See language.h. */
13658
13659 void value_print (struct value *val, struct ui_file *stream,
13660 const struct value_print_options *options) const override
13661 {
13662 return ada_value_print (val, stream, options);
13663 }
13664
ebe2334e
AB
13665 /* See language.h. */
13666
13667 void value_print_inner
13668 (struct value *val, struct ui_file *stream, int recurse,
13669 const struct value_print_options *options) const override
13670 {
13671 return ada_value_print_inner (val, stream, recurse, options);
13672 }
13673
a78a19b1
AB
13674 /* See language.h. */
13675
13676 struct block_symbol lookup_symbol_nonlocal
13677 (const char *name, const struct block *block,
ccf41c24 13678 const domain_search_flags domain) const override
a78a19b1
AB
13679 {
13680 struct block_symbol sym;
13681
78004096
TT
13682 sym = ada_lookup_symbol (name,
13683 (block == nullptr
13684 ? nullptr
d24e14a0 13685 : block->static_block ()),
ccf41c24 13686 domain);
a78a19b1
AB
13687 if (sym.symbol != NULL)
13688 return sym;
13689
13690 /* If we haven't found a match at this point, try the primitive
13691 types. In other languages, this search is performed before
13692 searching for global symbols in order to short-circuit that
13693 global-symbol search if it happens that the name corresponds
13694 to a primitive type. But we cannot do the same in Ada, because
13695 it is perfectly legitimate for a program to declare a type which
13696 has the same name as a standard type. If looking up a type in
13697 that situation, we have traditionally ignored the primitive type
13698 in favor of user-defined types. This is why, unlike most other
13699 languages, we search the primitive types this late and only after
13700 having searched the global symbols without success. */
13701
ccf41c24 13702 if ((domain & SEARCH_TYPE_DOMAIN) != 0)
a78a19b1
AB
13703 {
13704 struct gdbarch *gdbarch;
13705
13706 if (block == NULL)
99d9c3b9 13707 gdbarch = current_inferior ()->arch ();
a78a19b1 13708 else
7f5937df 13709 gdbarch = block->gdbarch ();
a78a19b1
AB
13710 sym.symbol
13711 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13712 if (sym.symbol != NULL)
13713 return sym;
13714 }
13715
13716 return {};
13717 }
13718
87afa652
AB
13719 /* See language.h. */
13720
13721 int parser (struct parser_state *ps) const override
13722 {
13723 warnings_issued = 0;
13724 return ada_parse (ps);
13725 }
13726
ec8cec5b
AB
13727 /* See language.h. */
13728
13729 void emitchar (int ch, struct type *chtype,
13730 struct ui_file *stream, int quoter) const override
13731 {
13732 ada_emit_char (ch, chtype, stream, quoter, 1);
13733 }
13734
52b50f2c
AB
13735 /* See language.h. */
13736
13737 void printchar (int ch, struct type *chtype,
13738 struct ui_file *stream) const override
13739 {
13740 ada_printchar (ch, chtype, stream);
13741 }
13742
d711ee67
AB
13743 /* See language.h. */
13744
13745 void printstr (struct ui_file *stream, struct type *elttype,
13746 const gdb_byte *string, unsigned int length,
13747 const char *encoding, int force_ellipses,
13748 const struct value_print_options *options) const override
13749 {
13750 ada_printstr (stream, elttype, string, length, encoding,
13751 force_ellipses, options);
13752 }
13753
4ffc13fb
AB
13754 /* See language.h. */
13755
13756 void print_typedef (struct type *type, struct symbol *new_symbol,
13757 struct ui_file *stream) const override
13758 {
13759 ada_print_typedef (type, new_symbol, stream);
13760 }
13761
39e7ecca
AB
13762 /* See language.h. */
13763
13764 bool is_string_type_p (struct type *type) const override
13765 {
13766 return ada_is_string_type (type);
13767 }
13768
22e3f3ed
AB
13769 /* See language.h. */
13770
26733fc7
TT
13771 bool is_array_like (struct type *type) const override
13772 {
13773 return (ada_is_constrained_packed_array_type (type)
13774 || ada_is_array_descriptor_type (type));
13775 }
13776
13777 /* See language.h. */
13778
13779 struct value *to_array (struct value *val) const override
13780 { return ada_coerce_to_simple_array (val); }
13781
13782 /* See language.h. */
13783
22e3f3ed
AB
13784 const char *struct_too_deep_ellipsis () const override
13785 { return "(...)"; }
39e7ecca 13786
67bd3fd5
AB
13787 /* See language.h. */
13788
13789 bool c_style_arrays_p () const override
13790 { return false; }
13791
d3355e4d
AB
13792 /* See language.h. */
13793
13794 bool store_sym_names_in_linkage_form_p () const override
13795 { return true; }
13796
b63a3f3f
AB
13797 /* See language.h. */
13798
13799 const struct lang_varobj_ops *varobj_ops () const override
13800 { return &ada_varobj_ops; }
13801
c9debfb9
AB
13802protected:
13803 /* See language.h. */
13804
13805 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13806 (const lookup_name_info &lookup_name) const override
13807 {
13808 return ada_get_symbol_name_matcher (lookup_name);
13809 }
0874fd07
AB
13810};
13811
13812/* Single instance of the Ada language class. */
13813
13814static ada_language ada_language_defn;
13815
5bf03f13
JB
13816/* Command-list for the "set/show ada" prefix command. */
13817static struct cmd_list_element *set_ada_list;
13818static struct cmd_list_element *show_ada_list;
13819
3d9434b5
JB
13820/* This module's 'new_objfile' observer. */
13821
13822static void
13823ada_new_objfile_observer (struct objfile *objfile)
13824{
74daa597 13825 ada_clear_symbol_cache (objfile->pspace);
3d9434b5
JB
13826}
13827
13828/* This module's 'free_objfile' observer. */
13829
13830static void
13831ada_free_objfile_observer (struct objfile *objfile)
13832{
74daa597 13833 ada_clear_symbol_cache (objfile->pspace);
3d9434b5
JB
13834}
13835
315e4ebb
TT
13836/* Charsets known to GNAT. */
13837static const char * const gnat_source_charsets[] =
13838{
13839 /* Note that code below assumes that the default comes first.
13840 Latin-1 is the default here, because that is also GNAT's
13841 default. */
13842 "ISO-8859-1",
13843 "ISO-8859-2",
13844 "ISO-8859-3",
13845 "ISO-8859-4",
13846 "ISO-8859-5",
13847 "ISO-8859-15",
13848 "CP437",
13849 "CP850",
13850 /* Note that this value is special-cased in the encoder and
13851 decoder. */
13852 ada_utf8,
13853 nullptr
13854};
13855
6c265988 13856void _initialize_ada_language ();
d2e4a39e 13857void
6c265988 13858_initialize_ada_language ()
14f9c5c9 13859{
f54bdb6d
SM
13860 add_setshow_prefix_cmd
13861 ("ada", no_class,
13862 _("Prefix command for changing Ada-specific settings."),
13863 _("Generic command for showing Ada-specific settings."),
13864 &set_ada_list, &show_ada_list,
13865 &setlist, &showlist);
5bf03f13
JB
13866
13867 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13868 &trust_pad_over_xvs, _("\
590042fc
PW
13869Enable or disable an optimization trusting PAD types over XVS types."), _("\
13870Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13871 _("\
5bf03f13
JB
13872This is related to the encoding used by the GNAT compiler. The debugger\n\
13873should normally trust the contents of PAD types, but certain older versions\n\
13874of GNAT have a bug that sometimes causes the information in the PAD type\n\
13875to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13876work around this bug. It is always safe to turn this option \"off\", but\n\
13877this incurs a slight performance penalty, so it is recommended to NOT change\n\
13878this option to \"off\" unless necessary."),
dda83cd7 13879 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13880
d72413e6
PMR
13881 add_setshow_boolean_cmd ("print-signatures", class_vars,
13882 &print_signatures, _("\
13883Enable or disable the output of formal and return types for functions in the \
590042fc 13884overloads selection menu."), _("\
d72413e6 13885Show whether the output of formal and return types for functions in the \
590042fc 13886overloads selection menu is activated."),
d72413e6
PMR
13887 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13888
315e4ebb
TT
13889 ada_source_charset = gnat_source_charsets[0];
13890 add_setshow_enum_cmd ("source-charset", class_files,
13891 gnat_source_charsets,
13892 &ada_source_charset, _("\
13893Set the Ada source character set."), _("\
13894Show the Ada source character set."), _("\
13895The character set used for Ada source files.\n\
13896This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13897 nullptr, nullptr,
13898 &set_ada_list, &show_ada_list);
13899
9ac4176b
PA
13900 add_catch_command ("exception", _("\
13901Catch Ada exceptions, when raised.\n\
9bf7038b 13902Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
13903Without any argument, stop when any Ada exception is raised.\n\
13904If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13905being raised does not have a handler (and will therefore lead to the task's\n\
13906termination).\n\
13907Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
13908raised is the same as ARG.\n\
13909CONDITION is a boolean expression that is evaluated to see whether the\n\
13910exception should cause a stop."),
9ac4176b 13911 catch_ada_exception_command,
71bed2db 13912 catch_ada_completer,
9ac4176b
PA
13913 CATCH_PERMANENT,
13914 CATCH_TEMPORARY);
9f757bf7
XR
13915
13916 add_catch_command ("handlers", _("\
13917Catch Ada exceptions, when handled.\n\
9bf7038b
TT
13918Usage: catch handlers [ARG] [if CONDITION]\n\
13919Without any argument, stop when any Ada exception is handled.\n\
13920With an argument, catch only exceptions with the given name.\n\
13921CONDITION is a boolean expression that is evaluated to see whether the\n\
13922exception should cause a stop."),
9f757bf7 13923 catch_ada_handlers_command,
dda83cd7 13924 catch_ada_completer,
9f757bf7
XR
13925 CATCH_PERMANENT,
13926 CATCH_TEMPORARY);
9ac4176b
PA
13927 add_catch_command ("assert", _("\
13928Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
13929Usage: catch assert [if CONDITION]\n\
13930CONDITION is a boolean expression that is evaluated to see whether the\n\
13931exception should cause a stop."),
9ac4176b 13932 catch_assert_command,
dda83cd7 13933 NULL,
9ac4176b
PA
13934 CATCH_PERMANENT,
13935 CATCH_TEMPORARY);
13936
778865d3
JB
13937 add_info ("exceptions", info_exceptions_command,
13938 _("\
13939List all Ada exception names.\n\
9bf7038b 13940Usage: info exceptions [REGEXP]\n\
778865d3
JB
13941If a regular expression is passed as an argument, only those matching\n\
13942the regular expression are listed."));
13943
f54bdb6d
SM
13944 add_setshow_prefix_cmd ("ada", class_maintenance,
13945 _("Set Ada maintenance-related variables."),
13946 _("Show Ada maintenance-related variables."),
13947 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
13948 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
13949
13950 add_setshow_boolean_cmd
13951 ("ignore-descriptive-types", class_maintenance,
13952 &ada_ignore_descriptive_types_p,
13953 _("Set whether descriptive types generated by GNAT should be ignored."),
13954 _("Show whether descriptive types generated by GNAT should be ignored."),
13955 _("\
13956When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
13957DWARF attribute."),
13958 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
13959
2698f5ea
TT
13960 decoded_names_store = htab_create_alloc (256, htab_hash_string,
13961 htab_eq_string,
459a2e4c 13962 NULL, xcalloc, xfree);
6b69afc4 13963
3d9434b5 13964 /* The ada-lang observers. */
c90e7d63 13965 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
74daa597
SM
13966 gdb::observers::all_objfiles_removed.attach (ada_clear_symbol_cache,
13967 "ada-lang");
c90e7d63
SM
13968 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
13969 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
033bc52b
TT
13970
13971#ifdef GDB_SELF_TEST
13972 selftests::register_test ("ada-decode", ada_decode_tests);
13973#endif
14f9c5c9 13974}