]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
gdb/python: implement DisassemblerResult.__str__ method
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
213516ef 3 Copyright (C) 1992-2023 Free Software Foundation, Inc.
14f9c5c9 4
a9762ec7 5 This file is part of GDB.
14f9c5c9 6
a9762ec7
JB
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
14f9c5c9 11
a9762ec7
JB
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
14f9c5c9 16
a9762ec7
JB
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 19
96d887e8 20
4c4b4cd2 21#include "defs.h"
14f9c5c9 22#include <ctype.h>
d322d6d6 23#include "gdbsupport/gdb_regex.h"
4de283e4
TT
24#include "frame.h"
25#include "symtab.h"
26#include "gdbtypes.h"
14f9c5c9 27#include "gdbcmd.h"
4de283e4
TT
28#include "expression.h"
29#include "parser-defs.h"
30#include "language.h"
31#include "varobj.h"
4de283e4
TT
32#include "inferior.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "breakpoint.h"
14f9c5c9 36#include "gdbcore.h"
4c4b4cd2 37#include "hashtab.h"
bf31fd38 38#include "gdbsupport/gdb_obstack.h"
4de283e4
TT
39#include "ada-lang.h"
40#include "completer.h"
4de283e4
TT
41#include "ui-out.h"
42#include "block.h"
04714b91 43#include "infcall.h"
4de283e4
TT
44#include "annotate.h"
45#include "valprint.h"
d55e5aa6 46#include "source.h"
4de283e4 47#include "observable.h"
692465f1 48#include "stack.h"
79d43c61 49#include "typeprint.h"
4de283e4 50#include "namespace.h"
7f6aba03 51#include "cli/cli-style.h"
0f8e2034 52#include "cli/cli-decode.h"
4de283e4 53
40bc484c 54#include "value.h"
4de283e4
TT
55#include "mi/mi-common.h"
56#include "arch-utils.h"
57#include "cli/cli-utils.h"
268a13a5
TT
58#include "gdbsupport/function-view.h"
59#include "gdbsupport/byte-vector.h"
4de283e4 60#include <algorithm>
03070ee9 61#include "ada-exp.h"
315e4ebb 62#include "charset.h"
013a623f 63#include "ax-gdb.h"
ccefe4c4 64
d2e4a39e 65static struct type *desc_base_type (struct type *);
14f9c5c9 66
d2e4a39e 67static struct type *desc_bounds_type (struct type *);
14f9c5c9 68
d2e4a39e 69static struct value *desc_bounds (struct value *);
14f9c5c9 70
d2e4a39e 71static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 72
d2e4a39e 73static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 74
556bdfd4 75static struct type *desc_data_target_type (struct type *);
14f9c5c9 76
d2e4a39e 77static struct value *desc_data (struct value *);
14f9c5c9 78
d2e4a39e 79static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 80
d2e4a39e 81static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 82
d2e4a39e 83static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 84
d2e4a39e 85static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 86
d2e4a39e 87static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 88
d2e4a39e 89static struct type *desc_index_type (struct type *, int);
14f9c5c9 90
d2e4a39e 91static int desc_arity (struct type *);
14f9c5c9 92
d2e4a39e 93static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 94
40bc484c 95static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 96
d1183b06 97static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
98 const struct block *,
99 const lookup_name_info &lookup_name,
100 domain_enum, struct objfile *);
14f9c5c9 101
d1183b06
TT
102static void ada_add_all_symbols (std::vector<struct block_symbol> &,
103 const struct block *,
b5ec771e
PA
104 const lookup_name_info &lookup_name,
105 domain_enum, int, int *);
22cee43f 106
d1183b06 107static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 108
d1183b06
TT
109static void add_defn_to_vec (std::vector<struct block_symbol> &,
110 struct symbol *,
dda83cd7 111 const struct block *);
14f9c5c9 112
d2e4a39e 113static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 114
4c4b4cd2 115static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 116
d2e4a39e 117static int numeric_type_p (struct type *);
14f9c5c9 118
d2e4a39e 119static int integer_type_p (struct type *);
14f9c5c9 120
d2e4a39e 121static int scalar_type_p (struct type *);
14f9c5c9 122
d2e4a39e 123static int discrete_type_p (struct type *);
14f9c5c9 124
a121b7c1 125static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 126 int, int);
4c4b4cd2 127
b4ba55a1 128static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 129 const char *);
b4ba55a1 130
d2e4a39e 131static int is_dynamic_field (struct type *, int);
14f9c5c9 132
10a2c479 133static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 134 const gdb_byte *,
dda83cd7 135 CORE_ADDR, struct value *);
4c4b4cd2
PH
136
137static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 138
28c85d6c 139static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 140
d2e4a39e 141static struct type *to_static_fixed_type (struct type *);
f192137b 142static struct type *static_unwrap_type (struct type *type);
14f9c5c9 143
d2e4a39e 144static struct value *unwrap_value (struct value *);
14f9c5c9 145
ad82864c 146static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 147
ad82864c 148static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 149
ad82864c
JB
150static long decode_packed_array_bitsize (struct type *);
151
152static struct value *decode_constrained_packed_array (struct value *);
153
ad82864c 154static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 155
d2e4a39e 156static struct value *value_subscript_packed (struct value *, int,
dda83cd7 157 struct value **);
14f9c5c9 158
4c4b4cd2 159static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 160 struct type *);
14f9c5c9 161
d2e4a39e 162static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 163
d2e4a39e 164static int equiv_types (struct type *, struct type *);
14f9c5c9 165
d2e4a39e 166static int is_name_suffix (const char *);
14f9c5c9 167
59c8a30b 168static int advance_wild_match (const char **, const char *, char);
73589123 169
b5ec771e 170static bool wild_match (const char *name, const char *patn);
14f9c5c9 171
d2e4a39e 172static struct value *ada_coerce_ref (struct value *);
14f9c5c9 173
4c4b4cd2
PH
174static LONGEST pos_atr (struct value *);
175
53a47a3e
TT
176static struct value *val_atr (struct type *, LONGEST);
177
4c4b4cd2 178static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 179 domain_enum);
14f9c5c9 180
108d56a4 181static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 182 struct type *);
4c4b4cd2 183
0d5cff50 184static int find_struct_field (const char *, struct type *, int,
dda83cd7 185 struct type **, int *, int *, int *, int *);
4c4b4cd2 186
d1183b06 187static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 188 struct value **, int, const char *,
7056f312 189 struct type *, bool);
4c4b4cd2 190
4c4b4cd2
PH
191static int ada_is_direct_array_type (struct type *);
192
52ce6436
PH
193static struct value *ada_index_struct_field (int, struct value *, int,
194 struct type *);
195
cf608cc4 196static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
197
198
852dff6c 199static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
200
201static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
202 (const lookup_name_info &lookup_name);
203
4c4b4cd2
PH
204\f
205
315e4ebb
TT
206/* The character set used for source files. */
207static const char *ada_source_charset;
208
209/* The string "UTF-8". This is here so we can check for the UTF-8
210 charset using == rather than strcmp. */
211static const char ada_utf8[] = "UTF-8";
212
213/* Each entry in the UTF-32 case-folding table is of this form. */
214struct utf8_entry
215{
216 /* The start and end, inclusive, of this range of codepoints. */
217 uint32_t start, end;
218 /* The delta to apply to get the upper-case form. 0 if this is
219 already upper-case. */
220 int upper_delta;
221 /* The delta to apply to get the lower-case form. 0 if this is
222 already lower-case. */
223 int lower_delta;
224
225 bool operator< (uint32_t val) const
226 {
227 return end < val;
228 }
229};
230
231static const utf8_entry ada_case_fold[] =
232{
233#include "ada-casefold.h"
234};
235
236\f
237
67cb5b2d 238static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
239#ifdef VMS
240 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
241#else
14f9c5c9 242 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 243#endif
14f9c5c9 244
4c4b4cd2 245/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 246static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 247 = "__gnat_ada_main_program_name";
14f9c5c9 248
4c4b4cd2
PH
249/* Limit on the number of warnings to raise per expression evaluation. */
250static int warning_limit = 2;
251
252/* Number of warning messages issued; reset to 0 by cleanups after
253 expression evaluation. */
254static int warnings_issued = 0;
255
27087b7f 256static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
257 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
258};
259
27087b7f 260static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
261 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
262};
263
c6044dd1
JB
264/* Maintenance-related settings for this module. */
265
266static struct cmd_list_element *maint_set_ada_cmdlist;
267static struct cmd_list_element *maint_show_ada_cmdlist;
268
c6044dd1
JB
269/* The "maintenance ada set/show ignore-descriptive-type" value. */
270
491144b5 271static bool ada_ignore_descriptive_types_p = false;
c6044dd1 272
e802dbe0
JB
273 /* Inferior-specific data. */
274
275/* Per-inferior data for this module. */
276
277struct ada_inferior_data
278{
279 /* The ada__tags__type_specific_data type, which is used when decoding
280 tagged types. With older versions of GNAT, this type was directly
281 accessible through a component ("tsd") in the object tag. But this
282 is no longer the case, so we cache it for each inferior. */
f37b313d 283 struct type *tsd_type = nullptr;
3eecfa55
JB
284
285 /* The exception_support_info data. This data is used to determine
286 how to implement support for Ada exception catchpoints in a given
287 inferior. */
f37b313d 288 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
289};
290
291/* Our key to this module's inferior data. */
08b8a139 292static const registry<inferior>::key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
293
294/* Return our inferior data for the given inferior (INF).
295
296 This function always returns a valid pointer to an allocated
297 ada_inferior_data structure. If INF's inferior data has not
298 been previously set, this functions creates a new one with all
299 fields set to zero, sets INF's inferior to it, and then returns
300 a pointer to that newly allocated ada_inferior_data. */
301
302static struct ada_inferior_data *
303get_ada_inferior_data (struct inferior *inf)
304{
305 struct ada_inferior_data *data;
306
f37b313d 307 data = ada_inferior_data.get (inf);
e802dbe0 308 if (data == NULL)
f37b313d 309 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
310
311 return data;
312}
313
314/* Perform all necessary cleanups regarding our module's inferior data
315 that is required after the inferior INF just exited. */
316
317static void
318ada_inferior_exit (struct inferior *inf)
319{
f37b313d 320 ada_inferior_data.clear (inf);
e802dbe0
JB
321}
322
ee01b665
JB
323
324 /* program-space-specific data. */
325
9d1c303d
TT
326/* The result of a symbol lookup to be stored in our symbol cache. */
327
328struct cache_entry
ee01b665 329{
9d1c303d
TT
330 /* The name used to perform the lookup. */
331 std::string name;
332 /* The namespace used during the lookup. */
333 domain_enum domain = UNDEF_DOMAIN;
334 /* The symbol returned by the lookup, or NULL if no matching symbol
335 was found. */
336 struct symbol *sym = nullptr;
337 /* The block where the symbol was found, or NULL if no matching
338 symbol was found. */
339 const struct block *block = nullptr;
ee01b665
JB
340};
341
9d1c303d
TT
342/* The symbol cache uses this type when searching. */
343
344struct cache_entry_search
345{
346 const char *name;
347 domain_enum domain;
348
349 hashval_t hash () const
350 {
351 /* This must agree with hash_cache_entry, below. */
352 return htab_hash_string (name);
353 }
354};
355
356/* Hash function for cache_entry. */
357
358static hashval_t
359hash_cache_entry (const void *v)
360{
361 const cache_entry *entry = (const cache_entry *) v;
362 return htab_hash_string (entry->name.c_str ());
363}
364
365/* Equality function for cache_entry. */
366
367static int
368eq_cache_entry (const void *a, const void *b)
369{
370 const cache_entry *entrya = (const cache_entry *) a;
371 const cache_entry_search *entryb = (const cache_entry_search *) b;
372
373 return entrya->domain == entryb->domain && entrya->name == entryb->name;
374}
375
ee01b665 376/* Key to our per-program-space data. */
9d1c303d 377static const registry<program_space>::key<htab, htab_deleter>
08b8a139 378 ada_pspace_data_handle;
ee01b665
JB
379
380/* Return this module's data for the given program space (PSPACE).
381 If not is found, add a zero'ed one now.
382
383 This function always returns a valid object. */
384
9d1c303d 385static htab_t
ee01b665
JB
386get_ada_pspace_data (struct program_space *pspace)
387{
9d1c303d
TT
388 htab_t data = ada_pspace_data_handle.get (pspace);
389 if (data == nullptr)
390 {
391 data = htab_create_alloc (10, hash_cache_entry, eq_cache_entry,
392 htab_delete_entry<cache_entry>,
393 xcalloc, xfree);
394 ada_pspace_data_handle.set (pspace, data);
395 }
ee01b665
JB
396
397 return data;
398}
399
dda83cd7 400 /* Utilities */
4c4b4cd2 401
720d1a40 402/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 403 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
404
405 Normally, we really expect a typedef type to only have 1 typedef layer.
406 In other words, we really expect the target type of a typedef type to be
407 a non-typedef type. This is particularly true for Ada units, because
408 the language does not have a typedef vs not-typedef distinction.
409 In that respect, the Ada compiler has been trying to eliminate as many
410 typedef definitions in the debugging information, since they generally
411 do not bring any extra information (we still use typedef under certain
412 circumstances related mostly to the GNAT encoding).
413
414 Unfortunately, we have seen situations where the debugging information
415 generated by the compiler leads to such multiple typedef layers. For
416 instance, consider the following example with stabs:
417
418 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
419 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
420
421 This is an error in the debugging information which causes type
422 pck__float_array___XUP to be defined twice, and the second time,
423 it is defined as a typedef of a typedef.
424
425 This is on the fringe of legality as far as debugging information is
426 concerned, and certainly unexpected. But it is easy to handle these
427 situations correctly, so we can afford to be lenient in this case. */
428
429static struct type *
430ada_typedef_target_type (struct type *type)
431{
78134374 432 while (type->code () == TYPE_CODE_TYPEDEF)
27710edb 433 type = type->target_type ();
720d1a40
JB
434 return type;
435}
436
41d27058
JB
437/* Given DECODED_NAME a string holding a symbol name in its
438 decoded form (ie using the Ada dotted notation), returns
439 its unqualified name. */
440
441static const char *
442ada_unqualified_name (const char *decoded_name)
443{
2b0f535a
JB
444 const char *result;
445
446 /* If the decoded name starts with '<', it means that the encoded
447 name does not follow standard naming conventions, and thus that
448 it is not your typical Ada symbol name. Trying to unqualify it
449 is therefore pointless and possibly erroneous. */
450 if (decoded_name[0] == '<')
451 return decoded_name;
452
453 result = strrchr (decoded_name, '.');
41d27058
JB
454 if (result != NULL)
455 result++; /* Skip the dot... */
456 else
457 result = decoded_name;
458
459 return result;
460}
461
39e7af3e 462/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 463
39e7af3e 464static std::string
41d27058
JB
465add_angle_brackets (const char *str)
466{
39e7af3e 467 return string_printf ("<%s>", str);
41d27058 468}
96d887e8 469
14f9c5c9 470/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 471 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
472
473static int
ebf56fd3 474field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
475{
476 int len = strlen (target);
5b4ee69b 477
d2e4a39e 478 return
4c4b4cd2
PH
479 (strncmp (field_name, target, len) == 0
480 && (field_name[len] == '\0'
dda83cd7
SM
481 || (startswith (field_name + len, "___")
482 && strcmp (field_name + strlen (field_name) - 6,
483 "___XVN") != 0)));
14f9c5c9
AS
484}
485
486
872c8b51
JB
487/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
488 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
489 and return its index. This function also handles fields whose name
490 have ___ suffixes because the compiler sometimes alters their name
491 by adding such a suffix to represent fields with certain constraints.
492 If the field could not be found, return a negative number if
493 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
494
495int
496ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 497 int maybe_missing)
4c4b4cd2
PH
498{
499 int fieldno;
872c8b51
JB
500 struct type *struct_type = check_typedef ((struct type *) type);
501
1f704f76 502 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 503 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
504 return fieldno;
505
506 if (!maybe_missing)
323e0a4a 507 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 508 field_name, struct_type->name ());
4c4b4cd2
PH
509
510 return -1;
511}
512
513/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
514
515int
d2e4a39e 516ada_name_prefix_len (const char *name)
14f9c5c9
AS
517{
518 if (name == NULL)
519 return 0;
d2e4a39e 520 else
14f9c5c9 521 {
d2e4a39e 522 const char *p = strstr (name, "___");
5b4ee69b 523
14f9c5c9 524 if (p == NULL)
dda83cd7 525 return strlen (name);
14f9c5c9 526 else
dda83cd7 527 return p - name;
14f9c5c9
AS
528 }
529}
530
4c4b4cd2
PH
531/* Return non-zero if SUFFIX is a suffix of STR.
532 Return zero if STR is null. */
533
14f9c5c9 534static int
d2e4a39e 535is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
536{
537 int len1, len2;
5b4ee69b 538
14f9c5c9
AS
539 if (str == NULL)
540 return 0;
541 len1 = strlen (str);
542 len2 = strlen (suffix);
4c4b4cd2 543 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
544}
545
4c4b4cd2
PH
546/* The contents of value VAL, treated as a value of type TYPE. The
547 result is an lval in memory if VAL is. */
14f9c5c9 548
d2e4a39e 549static struct value *
4c4b4cd2 550coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 551{
61ee279c 552 type = ada_check_typedef (type);
d0c97917 553 if (val->type () == type)
4c4b4cd2 554 return val;
d2e4a39e 555 else
14f9c5c9 556 {
4c4b4cd2
PH
557 struct value *result;
558
d00664db 559 if (val->optimized_out ())
b27556e3 560 result = value::allocate_optimized_out (type);
3ee3b270 561 else if (val->lazy ()
f73e424f 562 /* Be careful not to make a lazy not_lval value. */
736355f2 563 || (val->lval () != not_lval
d0c97917 564 && type->length () > val->type ()->length ()))
cbe793af 565 result = value::allocate_lazy (type);
41e8491f
JK
566 else
567 {
317c3ed9 568 result = value::allocate (type);
6c49729e 569 val->contents_copy (result, 0, 0, type->length ());
41e8491f 570 }
8181b7b6 571 result->set_component_location (val);
f49d5fa2 572 result->set_bitsize (val->bitsize ());
5011c493 573 result->set_bitpos (val->bitpos ());
736355f2 574 if (result->lval () == lval_memory)
9feb2d07 575 result->set_address (val->address ());
14f9c5c9
AS
576 return result;
577 }
578}
579
fc1a4b47
AC
580static const gdb_byte *
581cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
582{
583 if (valaddr == NULL)
584 return NULL;
585 else
586 return valaddr + offset;
587}
588
589static CORE_ADDR
ebf56fd3 590cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
591{
592 if (address == 0)
593 return 0;
d2e4a39e 594 else
14f9c5c9
AS
595 return address + offset;
596}
597
4c4b4cd2
PH
598/* Issue a warning (as for the definition of warning in utils.c, but
599 with exactly one argument rather than ...), unless the limit on the
600 number of warnings has passed during the evaluation of the current
601 expression. */
a2249542 602
77109804
AC
603/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
604 provided by "complaint". */
a0b31db1 605static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 606
14f9c5c9 607static void
a2249542 608lim_warning (const char *format, ...)
14f9c5c9 609{
a2249542 610 va_list args;
a2249542 611
5b4ee69b 612 va_start (args, format);
4c4b4cd2
PH
613 warnings_issued += 1;
614 if (warnings_issued <= warning_limit)
a2249542
MK
615 vwarning (format, args);
616
617 va_end (args);
4c4b4cd2
PH
618}
619
0963b4bd 620/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 621static LONGEST
c3e5cd34 622max_of_size (int size)
4c4b4cd2 623{
76a01679 624 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 625
76a01679 626 return top_bit | (top_bit - 1);
4c4b4cd2
PH
627}
628
0963b4bd 629/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 630static LONGEST
c3e5cd34 631min_of_size (int size)
4c4b4cd2 632{
c3e5cd34 633 return -max_of_size (size) - 1;
4c4b4cd2
PH
634}
635
0963b4bd 636/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 637static ULONGEST
c3e5cd34 638umax_of_size (int size)
4c4b4cd2 639{
76a01679 640 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 641
76a01679 642 return top_bit | (top_bit - 1);
4c4b4cd2
PH
643}
644
0963b4bd 645/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
646static LONGEST
647max_of_type (struct type *t)
4c4b4cd2 648{
c6d940a9 649 if (t->is_unsigned ())
df86565b 650 return (LONGEST) umax_of_size (t->length ());
c3e5cd34 651 else
df86565b 652 return max_of_size (t->length ());
c3e5cd34
PH
653}
654
0963b4bd 655/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
656static LONGEST
657min_of_type (struct type *t)
658{
c6d940a9 659 if (t->is_unsigned ())
c3e5cd34
PH
660 return 0;
661 else
df86565b 662 return min_of_size (t->length ());
4c4b4cd2
PH
663}
664
665/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
666LONGEST
667ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 668{
b249d2c2 669 type = resolve_dynamic_type (type, {}, 0);
78134374 670 switch (type->code ())
4c4b4cd2
PH
671 {
672 case TYPE_CODE_RANGE:
d1fd641e
SM
673 {
674 const dynamic_prop &high = type->bounds ()->high;
675
676 if (high.kind () == PROP_CONST)
677 return high.const_val ();
678 else
679 {
680 gdb_assert (high.kind () == PROP_UNDEFINED);
681
682 /* This happens when trying to evaluate a type's dynamic bound
683 without a live target. There is nothing relevant for us to
684 return here, so return 0. */
685 return 0;
686 }
687 }
4c4b4cd2 688 case TYPE_CODE_ENUM:
970db518 689 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
690 case TYPE_CODE_BOOL:
691 return 1;
692 case TYPE_CODE_CHAR:
76a01679 693 case TYPE_CODE_INT:
690cc4eb 694 return max_of_type (type);
4c4b4cd2 695 default:
43bbcdc2 696 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
697 }
698}
699
14e75d8e 700/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
701LONGEST
702ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 703{
b249d2c2 704 type = resolve_dynamic_type (type, {}, 0);
78134374 705 switch (type->code ())
4c4b4cd2
PH
706 {
707 case TYPE_CODE_RANGE:
d1fd641e
SM
708 {
709 const dynamic_prop &low = type->bounds ()->low;
710
711 if (low.kind () == PROP_CONST)
712 return low.const_val ();
713 else
714 {
715 gdb_assert (low.kind () == PROP_UNDEFINED);
716
717 /* This happens when trying to evaluate a type's dynamic bound
718 without a live target. There is nothing relevant for us to
719 return here, so return 0. */
720 return 0;
721 }
722 }
4c4b4cd2 723 case TYPE_CODE_ENUM:
970db518 724 return type->field (0).loc_enumval ();
690cc4eb
PH
725 case TYPE_CODE_BOOL:
726 return 0;
727 case TYPE_CODE_CHAR:
76a01679 728 case TYPE_CODE_INT:
690cc4eb 729 return min_of_type (type);
4c4b4cd2 730 default:
43bbcdc2 731 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
732 }
733}
734
735/* The identity on non-range types. For range types, the underlying
76a01679 736 non-range scalar type. */
4c4b4cd2
PH
737
738static struct type *
18af8284 739get_base_type (struct type *type)
4c4b4cd2 740{
78134374 741 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 742 {
27710edb 743 if (type == type->target_type () || type->target_type () == NULL)
dda83cd7 744 return type;
27710edb 745 type = type->target_type ();
4c4b4cd2
PH
746 }
747 return type;
14f9c5c9 748}
41246937
JB
749
750/* Return a decoded version of the given VALUE. This means returning
751 a value whose type is obtained by applying all the GNAT-specific
85102364 752 encodings, making the resulting type a static but standard description
41246937
JB
753 of the initial type. */
754
755struct value *
756ada_get_decoded_value (struct value *value)
757{
d0c97917 758 struct type *type = ada_check_typedef (value->type ());
41246937
JB
759
760 if (ada_is_array_descriptor_type (type)
761 || (ada_is_constrained_packed_array_type (type)
dda83cd7 762 && type->code () != TYPE_CODE_PTR))
41246937 763 {
78134374 764 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 765 value = ada_coerce_to_simple_array_ptr (value);
41246937 766 else
dda83cd7 767 value = ada_coerce_to_simple_array (value);
41246937
JB
768 }
769 else
770 value = ada_to_fixed_value (value);
771
772 return value;
773}
774
775/* Same as ada_get_decoded_value, but with the given TYPE.
776 Because there is no associated actual value for this type,
777 the resulting type might be a best-effort approximation in
778 the case of dynamic types. */
779
780struct type *
781ada_get_decoded_type (struct type *type)
782{
783 type = to_static_fixed_type (type);
784 if (ada_is_constrained_packed_array_type (type))
785 type = ada_coerce_to_simple_array_type (type);
786 return type;
787}
788
4c4b4cd2 789\f
76a01679 790
dda83cd7 791 /* Language Selection */
14f9c5c9
AS
792
793/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 794 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 795
de93309a 796static enum language
ccefe4c4 797ada_update_initial_language (enum language lang)
14f9c5c9 798{
cafb3438 799 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 800 return language_ada;
14f9c5c9
AS
801
802 return lang;
803}
96d887e8
PH
804
805/* If the main procedure is written in Ada, then return its name.
806 The result is good until the next call. Return NULL if the main
807 procedure doesn't appear to be in Ada. */
808
6f63b61d
TT
809const char *
810ada_main_name ()
96d887e8 811{
3b7344d5 812 struct bound_minimal_symbol msym;
e83e4e24 813 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 814
96d887e8
PH
815 /* For Ada, the name of the main procedure is stored in a specific
816 string constant, generated by the binder. Look for that symbol,
817 extract its address, and then read that string. If we didn't find
818 that string, then most probably the main procedure is not written
819 in Ada. */
820 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
821
3b7344d5 822 if (msym.minsym != NULL)
96d887e8 823 {
4aeddc50 824 CORE_ADDR main_program_name_addr = msym.value_address ();
96d887e8 825 if (main_program_name_addr == 0)
dda83cd7 826 error (_("Invalid address for Ada main program name."));
96d887e8 827
66920317 828 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 829 return main_program_name.get ();
96d887e8
PH
830 }
831
832 /* The main procedure doesn't seem to be in Ada. */
833 return NULL;
834}
14f9c5c9 835\f
dda83cd7 836 /* Symbols */
d2e4a39e 837
4c4b4cd2
PH
838/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
839 of NULLs. */
14f9c5c9 840
d2e4a39e
AS
841const struct ada_opname_map ada_opname_table[] = {
842 {"Oadd", "\"+\"", BINOP_ADD},
843 {"Osubtract", "\"-\"", BINOP_SUB},
844 {"Omultiply", "\"*\"", BINOP_MUL},
845 {"Odivide", "\"/\"", BINOP_DIV},
846 {"Omod", "\"mod\"", BINOP_MOD},
847 {"Orem", "\"rem\"", BINOP_REM},
848 {"Oexpon", "\"**\"", BINOP_EXP},
849 {"Olt", "\"<\"", BINOP_LESS},
850 {"Ole", "\"<=\"", BINOP_LEQ},
851 {"Ogt", "\">\"", BINOP_GTR},
852 {"Oge", "\">=\"", BINOP_GEQ},
853 {"Oeq", "\"=\"", BINOP_EQUAL},
854 {"One", "\"/=\"", BINOP_NOTEQUAL},
855 {"Oand", "\"and\"", BINOP_BITWISE_AND},
856 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
857 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
858 {"Oconcat", "\"&\"", BINOP_CONCAT},
859 {"Oabs", "\"abs\"", UNOP_ABS},
860 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
861 {"Oadd", "\"+\"", UNOP_PLUS},
862 {"Osubtract", "\"-\"", UNOP_NEG},
863 {NULL, NULL}
14f9c5c9
AS
864};
865
965bc1df
TT
866/* If STR is a decoded version of a compiler-provided suffix (like the
867 "[cold]" in "symbol[cold]"), return true. Otherwise, return
868 false. */
869
870static bool
871is_compiler_suffix (const char *str)
872{
873 gdb_assert (*str == '[');
874 ++str;
875 while (*str != '\0' && isalpha (*str))
876 ++str;
877 /* We accept a missing "]" in order to support completion. */
878 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
879}
880
315e4ebb
TT
881/* Append a non-ASCII character to RESULT. */
882static void
883append_hex_encoded (std::string &result, uint32_t one_char)
884{
885 if (one_char <= 0xff)
886 {
887 result.append ("U");
888 result.append (phex (one_char, 1));
889 }
890 else if (one_char <= 0xffff)
891 {
892 result.append ("W");
893 result.append (phex (one_char, 2));
894 }
895 else
896 {
897 result.append ("WW");
898 result.append (phex (one_char, 4));
899 }
900}
901
902/* Return a string that is a copy of the data in STORAGE, with
903 non-ASCII characters replaced by the appropriate hex encoding. A
904 template is used because, for UTF-8, we actually want to work with
905 UTF-32 codepoints. */
906template<typename T>
907std::string
908copy_and_hex_encode (struct obstack *storage)
909{
910 const T *chars = (T *) obstack_base (storage);
911 int num_chars = obstack_object_size (storage) / sizeof (T);
912 std::string result;
913 for (int i = 0; i < num_chars; ++i)
914 {
915 if (chars[i] <= 0x7f)
916 {
917 /* The host character set has to be a superset of ASCII, as
918 are all the other character sets we can use. */
919 result.push_back (chars[i]);
920 }
921 else
922 append_hex_encoded (result, chars[i]);
923 }
924 return result;
925}
926
5c4258f4 927/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 928 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 929 Otherwise, return the empty string in that case. */
4c4b4cd2 930
5c4258f4 931static std::string
b5ec771e 932ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 933{
4c4b4cd2 934 if (decoded == NULL)
5c4258f4 935 return {};
14f9c5c9 936
5c4258f4 937 std::string encoding_buffer;
315e4ebb 938 bool saw_non_ascii = false;
5c4258f4 939 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 940 {
315e4ebb
TT
941 if ((*p & 0x80) != 0)
942 saw_non_ascii = true;
943
cdc7bb92 944 if (*p == '.')
5c4258f4 945 encoding_buffer.append ("__");
965bc1df
TT
946 else if (*p == '[' && is_compiler_suffix (p))
947 {
948 encoding_buffer = encoding_buffer + "." + (p + 1);
949 if (encoding_buffer.back () == ']')
950 encoding_buffer.pop_back ();
951 break;
952 }
14f9c5c9 953 else if (*p == '"')
dda83cd7
SM
954 {
955 const struct ada_opname_map *mapping;
956
957 for (mapping = ada_opname_table;
958 mapping->encoded != NULL
959 && !startswith (p, mapping->decoded); mapping += 1)
960 ;
961 if (mapping->encoded == NULL)
b5ec771e
PA
962 {
963 if (throw_errors)
964 error (_("invalid Ada operator name: %s"), p);
965 else
5c4258f4 966 return {};
b5ec771e 967 }
5c4258f4 968 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
969 break;
970 }
d2e4a39e 971 else
5c4258f4 972 encoding_buffer.push_back (*p);
14f9c5c9
AS
973 }
974
315e4ebb
TT
975 /* If a non-ASCII character is seen, we must convert it to the
976 appropriate hex form. As this is more expensive, we keep track
977 of whether it is even necessary. */
978 if (saw_non_ascii)
979 {
980 auto_obstack storage;
981 bool is_utf8 = ada_source_charset == ada_utf8;
982 try
983 {
984 convert_between_encodings
985 (host_charset (),
986 is_utf8 ? HOST_UTF32 : ada_source_charset,
987 (const gdb_byte *) encoding_buffer.c_str (),
988 encoding_buffer.length (), 1,
989 &storage, translit_none);
990 }
991 catch (const gdb_exception &)
992 {
993 static bool warned = false;
994
995 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
996 might like to know why. */
997 if (!warned)
998 {
999 warned = true;
1000 warning (_("charset conversion failure for '%s'.\n"
1001 "You may have the wrong value for 'set ada source-charset'."),
1002 encoding_buffer.c_str ());
1003 }
1004
1005 /* We don't try to recover from errors. */
1006 return encoding_buffer;
1007 }
1008
1009 if (is_utf8)
1010 return copy_and_hex_encode<uint32_t> (&storage);
1011 return copy_and_hex_encode<gdb_byte> (&storage);
1012 }
1013
4c4b4cd2 1014 return encoding_buffer;
14f9c5c9
AS
1015}
1016
315e4ebb
TT
1017/* Find the entry for C in the case-folding table. Return nullptr if
1018 the entry does not cover C. */
1019static const utf8_entry *
1020find_case_fold_entry (uint32_t c)
b5ec771e 1021{
315e4ebb
TT
1022 auto iter = std::lower_bound (std::begin (ada_case_fold),
1023 std::end (ada_case_fold),
1024 c);
1025 if (iter == std::end (ada_case_fold)
1026 || c < iter->start
1027 || c > iter->end)
1028 return nullptr;
1029 return &*iter;
b5ec771e
PA
1030}
1031
14f9c5c9 1032/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1033 quotes, unfolded, but with the quotes stripped away. If
1034 THROW_ON_ERROR is true, encoding failures will throw an exception
1035 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1036
5f9febe0 1037static const char *
315e4ebb 1038ada_fold_name (gdb::string_view name, bool throw_on_error = false)
14f9c5c9 1039{
5f9febe0 1040 static std::string fold_storage;
14f9c5c9 1041
6a780b67 1042 if (!name.empty () && name[0] == '\'')
01573d73 1043 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
1044 else
1045 {
315e4ebb
TT
1046 /* Why convert to UTF-32 and implement our own case-folding,
1047 rather than convert to wchar_t and use the platform's
1048 functions? I'm glad you asked.
1049
1050 The main problem is that GNAT implements an unusual rule for
1051 case folding. For ASCII letters, letters in single-byte
1052 encodings (such as ISO-8859-*), and Unicode letters that fit
1053 in a single byte (i.e., code point is <= 0xff), the letter is
1054 folded to lower case. Other Unicode letters are folded to
1055 upper case.
1056
1057 This rule means that the code must be able to examine the
1058 value of the character. And, some hosts do not use Unicode
1059 for wchar_t, so examining the value of such characters is
1060 forbidden. */
1061 auto_obstack storage;
1062 try
1063 {
1064 convert_between_encodings
1065 (host_charset (), HOST_UTF32,
1066 (const gdb_byte *) name.data (),
1067 name.length (), 1,
1068 &storage, translit_none);
1069 }
1070 catch (const gdb_exception &)
1071 {
1072 if (throw_on_error)
1073 throw;
1074
1075 static bool warned = false;
1076
1077 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1078 might like to know why. */
1079 if (!warned)
1080 {
1081 warned = true;
1082 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1083 "This normally should not happen, please file a bug report."),
1084 gdb::to_string (name).c_str (), host_charset ());
1085 }
1086
1087 /* We don't try to recover from errors; just return the
1088 original string. */
1089 fold_storage = gdb::to_string (name);
1090 return fold_storage.c_str ();
1091 }
1092
1093 bool is_utf8 = ada_source_charset == ada_utf8;
1094 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1095 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1096 for (int i = 0; i < num_chars; ++i)
1097 {
1098 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1099 if (entry != nullptr)
1100 {
1101 uint32_t low = chars[i] + entry->lower_delta;
1102 if (!is_utf8 || low <= 0xff)
1103 chars[i] = low;
1104 else
1105 chars[i] = chars[i] + entry->upper_delta;
1106 }
1107 }
1108
1109 /* Now convert back to ordinary characters. */
1110 auto_obstack reconverted;
1111 try
1112 {
1113 convert_between_encodings (HOST_UTF32,
1114 host_charset (),
1115 (const gdb_byte *) chars,
1116 num_chars * sizeof (uint32_t),
1117 sizeof (uint32_t),
1118 &reconverted,
1119 translit_none);
1120 obstack_1grow (&reconverted, '\0');
1121 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1122 }
1123 catch (const gdb_exception &)
1124 {
1125 if (throw_on_error)
1126 throw;
1127
1128 static bool warned = false;
1129
1130 /* Converting back from UTF-32 shouldn't normally fail, but
1131 there are some host encodings without upper/lower
1132 equivalence. */
1133 if (!warned)
1134 {
1135 warned = true;
1136 warning (_("could not convert the lower-cased variant of '%s'\n"
1137 "from UTF-32 to the host encoding (%s)."),
1138 gdb::to_string (name).c_str (), host_charset ());
1139 }
1140
1141 /* We don't try to recover from errors; just return the
1142 original string. */
1143 fold_storage = gdb::to_string (name);
1144 }
14f9c5c9
AS
1145 }
1146
5f9febe0 1147 return fold_storage.c_str ();
14f9c5c9
AS
1148}
1149
5fea9794
TT
1150/* The "encoded" form of DECODED, according to GNAT conventions. If
1151 FOLD is true (the default), case-fold any ordinary symbol. Symbols
1152 with <...> quoting are not folded in any case. */
315e4ebb
TT
1153
1154std::string
5fea9794 1155ada_encode (const char *decoded, bool fold)
315e4ebb 1156{
5fea9794 1157 if (fold && decoded[0] != '<')
315e4ebb
TT
1158 decoded = ada_fold_name (decoded);
1159 return ada_encode_1 (decoded, true);
1160}
1161
529cad9c
PH
1162/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1163
1164static int
1165is_lower_alphanum (const char c)
1166{
1167 return (isdigit (c) || (isalpha (c) && islower (c)));
1168}
1169
c90092fe
JB
1170/* ENCODED is the linkage name of a symbol and LEN contains its length.
1171 This function saves in LEN the length of that same symbol name but
1172 without either of these suffixes:
29480c32
JB
1173 . .{DIGIT}+
1174 . ${DIGIT}+
1175 . ___{DIGIT}+
1176 . __{DIGIT}+.
c90092fe 1177
29480c32
JB
1178 These are suffixes introduced by the compiler for entities such as
1179 nested subprogram for instance, in order to avoid name clashes.
1180 They do not serve any purpose for the debugger. */
1181
1182static void
1183ada_remove_trailing_digits (const char *encoded, int *len)
1184{
1185 if (*len > 1 && isdigit (encoded[*len - 1]))
1186 {
1187 int i = *len - 2;
5b4ee69b 1188
29480c32 1189 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1190 i--;
29480c32 1191 if (i >= 0 && encoded[i] == '.')
dda83cd7 1192 *len = i;
29480c32 1193 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1194 *len = i;
61012eef 1195 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1196 *len = i - 2;
61012eef 1197 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1198 *len = i - 1;
29480c32
JB
1199 }
1200}
1201
1202/* Remove the suffix introduced by the compiler for protected object
1203 subprograms. */
1204
1205static void
1206ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1207{
1208 /* Remove trailing N. */
1209
1210 /* Protected entry subprograms are broken into two
1211 separate subprograms: The first one is unprotected, and has
1212 a 'N' suffix; the second is the protected version, and has
0963b4bd 1213 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1214 the protection. Since the P subprograms are internally generated,
1215 we leave these names undecoded, giving the user a clue that this
1216 entity is internal. */
1217
1218 if (*len > 1
1219 && encoded[*len - 1] == 'N'
1220 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1221 *len = *len - 1;
1222}
1223
965bc1df
TT
1224/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1225 then update *LEN to remove the suffix and return the offset of the
1226 character just past the ".". Otherwise, return -1. */
1227
1228static int
1229remove_compiler_suffix (const char *encoded, int *len)
1230{
1231 int offset = *len - 1;
1232 while (offset > 0 && isalpha (encoded[offset]))
1233 --offset;
1234 if (offset > 0 && encoded[offset] == '.')
1235 {
1236 *len = offset;
1237 return offset + 1;
1238 }
1239 return -1;
1240}
1241
315e4ebb
TT
1242/* Convert an ASCII hex string to a number. Reads exactly N
1243 characters from STR. Returns true on success, false if one of the
1244 digits was not a hex digit. */
1245static bool
1246convert_hex (const char *str, int n, uint32_t *out)
1247{
1248 uint32_t result = 0;
1249
1250 for (int i = 0; i < n; ++i)
1251 {
1252 if (!isxdigit (str[i]))
1253 return false;
1254 result <<= 4;
1255 result |= fromhex (str[i]);
1256 }
1257
1258 *out = result;
1259 return true;
1260}
1261
1262/* Convert a wide character from its ASCII hex representation in STR
1263 (consisting of exactly N characters) to the host encoding,
1264 appending the resulting bytes to OUT. If N==2 and the Ada source
1265 charset is not UTF-8, then hex refers to an encoding in the
1266 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1267 Return false and do not modify OUT on conversion failure. */
1268static bool
1269convert_from_hex_encoded (std::string &out, const char *str, int n)
1270{
1271 uint32_t value;
1272
1273 if (!convert_hex (str, n, &value))
1274 return false;
1275 try
1276 {
1277 auto_obstack bytes;
1278 /* In the 'U' case, the hex digits encode the character in the
1279 Ada source charset. However, if the source charset is UTF-8,
1280 this really means it is a single-byte UTF-32 character. */
1281 if (n == 2 && ada_source_charset != ada_utf8)
1282 {
1283 gdb_byte one_char = (gdb_byte) value;
1284
1285 convert_between_encodings (ada_source_charset, host_charset (),
1286 &one_char,
1287 sizeof (one_char), sizeof (one_char),
1288 &bytes, translit_none);
1289 }
1290 else
1291 convert_between_encodings (HOST_UTF32, host_charset (),
1292 (const gdb_byte *) &value,
1293 sizeof (value), sizeof (value),
1294 &bytes, translit_none);
1295 obstack_1grow (&bytes, '\0');
1296 out.append ((const char *) obstack_base (&bytes));
1297 }
1298 catch (const gdb_exception &)
1299 {
1300 /* On failure, the caller will just let the encoded form
1301 through, which seems basically reasonable. */
1302 return false;
1303 }
1304
1305 return true;
1306}
1307
8a3df5ac 1308/* See ada-lang.h. */
14f9c5c9 1309
f945dedf 1310std::string
5c94f938 1311ada_decode (const char *encoded, bool wrap, bool operators)
14f9c5c9 1312{
36f5ca53 1313 int i;
14f9c5c9 1314 int len0;
d2e4a39e 1315 const char *p;
14f9c5c9 1316 int at_start_name;
f945dedf 1317 std::string decoded;
965bc1df 1318 int suffix = -1;
d2e4a39e 1319
0d81f350
JG
1320 /* With function descriptors on PPC64, the value of a symbol named
1321 ".FN", if it exists, is the entry point of the function "FN". */
1322 if (encoded[0] == '.')
1323 encoded += 1;
1324
29480c32
JB
1325 /* The name of the Ada main procedure starts with "_ada_".
1326 This prefix is not part of the decoded name, so skip this part
1327 if we see this prefix. */
61012eef 1328 if (startswith (encoded, "_ada_"))
4c4b4cd2 1329 encoded += 5;
81eaa506
TT
1330 /* The "___ghost_" prefix is used for ghost entities. Normally
1331 these aren't preserved but when they are, it's useful to see
1332 them. */
1333 if (startswith (encoded, "___ghost_"))
1334 encoded += 9;
14f9c5c9 1335
29480c32
JB
1336 /* If the name starts with '_', then it is not a properly encoded
1337 name, so do not attempt to decode it. Similarly, if the name
1338 starts with '<', the name should not be decoded. */
4c4b4cd2 1339 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1340 goto Suppress;
1341
4c4b4cd2 1342 len0 = strlen (encoded);
4c4b4cd2 1343
965bc1df
TT
1344 suffix = remove_compiler_suffix (encoded, &len0);
1345
29480c32
JB
1346 ada_remove_trailing_digits (encoded, &len0);
1347 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1348
4c4b4cd2
PH
1349 /* Remove the ___X.* suffix if present. Do not forget to verify that
1350 the suffix is located before the current "end" of ENCODED. We want
1351 to avoid re-matching parts of ENCODED that have previously been
1352 marked as discarded (by decrementing LEN0). */
1353 p = strstr (encoded, "___");
1354 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1355 {
1356 if (p[3] == 'X')
dda83cd7 1357 len0 = p - encoded;
14f9c5c9 1358 else
dda83cd7 1359 goto Suppress;
14f9c5c9 1360 }
4c4b4cd2 1361
29480c32
JB
1362 /* Remove any trailing TKB suffix. It tells us that this symbol
1363 is for the body of a task, but that information does not actually
1364 appear in the decoded name. */
1365
61012eef 1366 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1367 len0 -= 3;
76a01679 1368
a10967fa
JB
1369 /* Remove any trailing TB suffix. The TB suffix is slightly different
1370 from the TKB suffix because it is used for non-anonymous task
1371 bodies. */
1372
61012eef 1373 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1374 len0 -= 2;
1375
29480c32
JB
1376 /* Remove trailing "B" suffixes. */
1377 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1378
61012eef 1379 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1380 len0 -= 1;
1381
29480c32
JB
1382 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1383
4c4b4cd2 1384 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1385 {
4c4b4cd2
PH
1386 i = len0 - 2;
1387 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1388 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1389 i -= 1;
4c4b4cd2 1390 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1391 len0 = i - 1;
4c4b4cd2 1392 else if (encoded[i] == '$')
dda83cd7 1393 len0 = i;
d2e4a39e 1394 }
14f9c5c9 1395
29480c32
JB
1396 /* The first few characters that are not alphabetic are not part
1397 of any encoding we use, so we can copy them over verbatim. */
1398
36f5ca53
TT
1399 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1400 decoded.push_back (encoded[i]);
14f9c5c9
AS
1401
1402 at_start_name = 1;
1403 while (i < len0)
1404 {
29480c32 1405 /* Is this a symbol function? */
5c94f938 1406 if (operators && at_start_name && encoded[i] == 'O')
dda83cd7
SM
1407 {
1408 int k;
1409
1410 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1411 {
1412 int op_len = strlen (ada_opname_table[k].encoded);
1413 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1414 op_len - 1) == 0)
1415 && !isalnum (encoded[i + op_len]))
1416 {
36f5ca53 1417 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1418 at_start_name = 0;
1419 i += op_len;
dda83cd7
SM
1420 break;
1421 }
1422 }
1423 if (ada_opname_table[k].encoded != NULL)
1424 continue;
1425 }
14f9c5c9
AS
1426 at_start_name = 0;
1427
529cad9c 1428 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1429 into "." (just below). */
529cad9c 1430
61012eef 1431 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1432 i += 2;
529cad9c 1433
29480c32 1434 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1435 be translated into "." (just below). These are internal names
1436 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1437
1438 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1439 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1440 && isdigit (encoded [i+4]))
1441 {
1442 int k = i + 5;
1443
1444 while (k < len0 && isdigit (encoded[k]))
1445 k++; /* Skip any extra digit. */
1446
1447 /* Double-check that the "__B_{DIGITS}+" sequence we found
1448 is indeed followed by "__". */
1449 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1450 i = k;
1451 }
29480c32 1452
529cad9c
PH
1453 /* Remove _E{DIGITS}+[sb] */
1454
1455 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1456 of subprograms created by the compiler for each entry. The first
1457 one implements the actual entry code, and has a suffix following
1458 the convention above; the second one implements the barrier and
1459 uses the same convention as above, except that the 'E' is replaced
1460 by a 'B'.
529cad9c 1461
dda83cd7
SM
1462 Just as above, we do not decode the name of barrier functions
1463 to give the user a clue that the code he is debugging has been
1464 internally generated. */
529cad9c
PH
1465
1466 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1467 && isdigit (encoded[i+2]))
1468 {
1469 int k = i + 3;
1470
1471 while (k < len0 && isdigit (encoded[k]))
1472 k++;
1473
1474 if (k < len0
1475 && (encoded[k] == 'b' || encoded[k] == 's'))
1476 {
1477 k++;
1478 /* Just as an extra precaution, make sure that if this
1479 suffix is followed by anything else, it is a '_'.
1480 Otherwise, we matched this sequence by accident. */
1481 if (k == len0
1482 || (k < len0 && encoded[k] == '_'))
1483 i = k;
1484 }
1485 }
529cad9c
PH
1486
1487 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1488 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1489
1490 if (i < len0 + 3
dda83cd7
SM
1491 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1492 {
1493 /* Backtrack a bit up until we reach either the begining of
1494 the encoded name, or "__". Make sure that we only find
1495 digits or lowercase characters. */
1496 const char *ptr = encoded + i - 1;
1497
1498 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1499 ptr--;
1500 if (ptr < encoded
1501 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1502 i++;
1503 }
529cad9c 1504
315e4ebb
TT
1505 if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
1506 {
1507 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1508 {
1509 i += 3;
1510 continue;
1511 }
1512 }
1513 else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
1514 {
1515 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1516 {
1517 i += 5;
1518 continue;
1519 }
1520 }
1521 else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
1522 && isxdigit (encoded[i + 2]))
1523 {
1524 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1525 {
1526 i += 10;
1527 continue;
1528 }
1529 }
1530
4c4b4cd2 1531 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1532 {
1533 /* This is a X[bn]* sequence not separated from the previous
1534 part of the name with a non-alpha-numeric character (in other
1535 words, immediately following an alpha-numeric character), then
1536 verify that it is placed at the end of the encoded name. If
1537 not, then the encoding is not valid and we should abort the
1538 decoding. Otherwise, just skip it, it is used in body-nested
1539 package names. */
1540 do
1541 i += 1;
1542 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1543 if (i < len0)
1544 goto Suppress;
1545 }
cdc7bb92 1546 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1547 {
1548 /* Replace '__' by '.'. */
36f5ca53 1549 decoded.push_back ('.');
dda83cd7
SM
1550 at_start_name = 1;
1551 i += 2;
dda83cd7 1552 }
14f9c5c9 1553 else
dda83cd7
SM
1554 {
1555 /* It's a character part of the decoded name, so just copy it
1556 over. */
36f5ca53 1557 decoded.push_back (encoded[i]);
dda83cd7 1558 i += 1;
dda83cd7 1559 }
14f9c5c9 1560 }
14f9c5c9 1561
29480c32
JB
1562 /* Decoded names should never contain any uppercase character.
1563 Double-check this, and abort the decoding if we find one. */
1564
5c94f938
TT
1565 if (operators)
1566 {
1567 for (i = 0; i < decoded.length(); ++i)
1568 if (isupper (decoded[i]) || decoded[i] == ' ')
1569 goto Suppress;
1570 }
14f9c5c9 1571
965bc1df
TT
1572 /* If the compiler added a suffix, append it now. */
1573 if (suffix >= 0)
1574 decoded = decoded + "[" + &encoded[suffix] + "]";
1575
f945dedf 1576 return decoded;
14f9c5c9
AS
1577
1578Suppress:
8a3df5ac
TT
1579 if (!wrap)
1580 return {};
1581
4c4b4cd2 1582 if (encoded[0] == '<')
f945dedf 1583 decoded = encoded;
14f9c5c9 1584 else
f945dedf 1585 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1586 return decoded;
4c4b4cd2
PH
1587}
1588
1589/* Table for keeping permanent unique copies of decoded names. Once
1590 allocated, names in this table are never released. While this is a
1591 storage leak, it should not be significant unless there are massive
1592 changes in the set of decoded names in successive versions of a
1593 symbol table loaded during a single session. */
1594static struct htab *decoded_names_store;
1595
1596/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1597 in the language-specific part of GSYMBOL, if it has not been
1598 previously computed. Tries to save the decoded name in the same
1599 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1600 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1601 GSYMBOL).
4c4b4cd2
PH
1602 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1603 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1604 when a decoded name is cached in it. */
4c4b4cd2 1605
45e6c716 1606const char *
f85f34ed 1607ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1608{
f85f34ed
TT
1609 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1610 const char **resultp =
615b3f62 1611 &gsymbol->language_specific.demangled_name;
5b4ee69b 1612
f85f34ed 1613 if (!gsymbol->ada_mangled)
4c4b4cd2 1614 {
4d4eaa30 1615 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1616 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1617
f85f34ed 1618 gsymbol->ada_mangled = 1;
5b4ee69b 1619
f85f34ed 1620 if (obstack != NULL)
f945dedf 1621 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1622 else
dda83cd7 1623 {
f85f34ed
TT
1624 /* Sometimes, we can't find a corresponding objfile, in
1625 which case, we put the result on the heap. Since we only
1626 decode when needed, we hope this usually does not cause a
1627 significant memory leak (FIXME). */
1628
dda83cd7
SM
1629 char **slot = (char **) htab_find_slot (decoded_names_store,
1630 decoded.c_str (), INSERT);
5b4ee69b 1631
dda83cd7
SM
1632 if (*slot == NULL)
1633 *slot = xstrdup (decoded.c_str ());
1634 *resultp = *slot;
1635 }
4c4b4cd2 1636 }
14f9c5c9 1637
4c4b4cd2
PH
1638 return *resultp;
1639}
76a01679 1640
14f9c5c9 1641\f
d2e4a39e 1642
dda83cd7 1643 /* Arrays */
14f9c5c9 1644
28c85d6c
JB
1645/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1646 generated by the GNAT compiler to describe the index type used
1647 for each dimension of an array, check whether it follows the latest
1648 known encoding. If not, fix it up to conform to the latest encoding.
1649 Otherwise, do nothing. This function also does nothing if
1650 INDEX_DESC_TYPE is NULL.
1651
85102364 1652 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1653 Initially, the information would be provided through the name of each
1654 field of the structure type only, while the type of these fields was
1655 described as unspecified and irrelevant. The debugger was then expected
1656 to perform a global type lookup using the name of that field in order
1657 to get access to the full index type description. Because these global
1658 lookups can be very expensive, the encoding was later enhanced to make
1659 the global lookup unnecessary by defining the field type as being
1660 the full index type description.
1661
1662 The purpose of this routine is to allow us to support older versions
1663 of the compiler by detecting the use of the older encoding, and by
1664 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1665 we essentially replace each field's meaningless type by the associated
1666 index subtype). */
1667
1668void
1669ada_fixup_array_indexes_type (struct type *index_desc_type)
1670{
1671 int i;
1672
1673 if (index_desc_type == NULL)
1674 return;
1f704f76 1675 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1676
1677 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1678 to check one field only, no need to check them all). If not, return
1679 now.
1680
1681 If our INDEX_DESC_TYPE was generated using the older encoding,
1682 the field type should be a meaningless integer type whose name
1683 is not equal to the field name. */
940da03e
SM
1684 if (index_desc_type->field (0).type ()->name () != NULL
1685 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1686 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1687 return;
1688
1689 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1690 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1691 {
33d16dd9 1692 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1693 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1694
1695 if (raw_type)
5d14b6e5 1696 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1697 }
1698}
1699
4c4b4cd2
PH
1700/* The desc_* routines return primitive portions of array descriptors
1701 (fat pointers). */
14f9c5c9
AS
1702
1703/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1704 level of indirection, if needed. */
1705
d2e4a39e
AS
1706static struct type *
1707desc_base_type (struct type *type)
14f9c5c9
AS
1708{
1709 if (type == NULL)
1710 return NULL;
61ee279c 1711 type = ada_check_typedef (type);
78134374 1712 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1713 type = ada_typedef_target_type (type);
1714
1265e4aa 1715 if (type != NULL
78134374 1716 && (type->code () == TYPE_CODE_PTR
dda83cd7 1717 || type->code () == TYPE_CODE_REF))
27710edb 1718 return ada_check_typedef (type->target_type ());
14f9c5c9
AS
1719 else
1720 return type;
1721}
1722
4c4b4cd2
PH
1723/* True iff TYPE indicates a "thin" array pointer type. */
1724
14f9c5c9 1725static int
d2e4a39e 1726is_thin_pntr (struct type *type)
14f9c5c9 1727{
d2e4a39e 1728 return
14f9c5c9
AS
1729 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1730 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1731}
1732
4c4b4cd2
PH
1733/* The descriptor type for thin pointer type TYPE. */
1734
d2e4a39e
AS
1735static struct type *
1736thin_descriptor_type (struct type *type)
14f9c5c9 1737{
d2e4a39e 1738 struct type *base_type = desc_base_type (type);
5b4ee69b 1739
14f9c5c9
AS
1740 if (base_type == NULL)
1741 return NULL;
1742 if (is_suffix (ada_type_name (base_type), "___XVE"))
1743 return base_type;
d2e4a39e 1744 else
14f9c5c9 1745 {
d2e4a39e 1746 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1747
14f9c5c9 1748 if (alt_type == NULL)
dda83cd7 1749 return base_type;
14f9c5c9 1750 else
dda83cd7 1751 return alt_type;
14f9c5c9
AS
1752 }
1753}
1754
4c4b4cd2
PH
1755/* A pointer to the array data for thin-pointer value VAL. */
1756
d2e4a39e
AS
1757static struct value *
1758thin_data_pntr (struct value *val)
14f9c5c9 1759{
d0c97917 1760 struct type *type = ada_check_typedef (val->type ());
556bdfd4 1761 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1762
556bdfd4
UW
1763 data_type = lookup_pointer_type (data_type);
1764
78134374 1765 if (type->code () == TYPE_CODE_PTR)
cda03344 1766 return value_cast (data_type, val->copy ());
d2e4a39e 1767 else
9feb2d07 1768 return value_from_longest (data_type, val->address ());
14f9c5c9
AS
1769}
1770
4c4b4cd2
PH
1771/* True iff TYPE indicates a "thick" array pointer type. */
1772
14f9c5c9 1773static int
d2e4a39e 1774is_thick_pntr (struct type *type)
14f9c5c9
AS
1775{
1776 type = desc_base_type (type);
78134374 1777 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1778 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1779}
1780
4c4b4cd2
PH
1781/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1782 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1783
d2e4a39e
AS
1784static struct type *
1785desc_bounds_type (struct type *type)
14f9c5c9 1786{
d2e4a39e 1787 struct type *r;
14f9c5c9
AS
1788
1789 type = desc_base_type (type);
1790
1791 if (type == NULL)
1792 return NULL;
1793 else if (is_thin_pntr (type))
1794 {
1795 type = thin_descriptor_type (type);
1796 if (type == NULL)
dda83cd7 1797 return NULL;
14f9c5c9
AS
1798 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1799 if (r != NULL)
dda83cd7 1800 return ada_check_typedef (r);
14f9c5c9 1801 }
78134374 1802 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1803 {
1804 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1805 if (r != NULL)
27710edb 1806 return ada_check_typedef (ada_check_typedef (r)->target_type ());
14f9c5c9
AS
1807 }
1808 return NULL;
1809}
1810
1811/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1812 one, a pointer to its bounds data. Otherwise NULL. */
1813
d2e4a39e
AS
1814static struct value *
1815desc_bounds (struct value *arr)
14f9c5c9 1816{
d0c97917 1817 struct type *type = ada_check_typedef (arr->type ());
5b4ee69b 1818
d2e4a39e 1819 if (is_thin_pntr (type))
14f9c5c9 1820 {
d2e4a39e 1821 struct type *bounds_type =
dda83cd7 1822 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1823 LONGEST addr;
1824
4cdfadb1 1825 if (bounds_type == NULL)
dda83cd7 1826 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1827
1828 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1829 since desc_type is an XVE-encoded type (and shouldn't be),
1830 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1831 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1832 addr = value_as_long (arr);
d2e4a39e 1833 else
9feb2d07 1834 addr = arr->address ();
14f9c5c9 1835
d2e4a39e 1836 return
dda83cd7 1837 value_from_longest (lookup_pointer_type (bounds_type),
df86565b 1838 addr - bounds_type->length ());
14f9c5c9
AS
1839 }
1840
1841 else if (is_thick_pntr (type))
05e522ef 1842 {
158cc4fe 1843 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef 1844 _("Bad GNAT array descriptor"));
d0c97917 1845 struct type *p_bounds_type = p_bounds->type ();
05e522ef
JB
1846
1847 if (p_bounds_type
78134374 1848 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef 1849 {
27710edb 1850 struct type *target_type = p_bounds_type->target_type ();
05e522ef 1851
e46d3488 1852 if (target_type->is_stub ())
05e522ef
JB
1853 p_bounds = value_cast (lookup_pointer_type
1854 (ada_check_typedef (target_type)),
1855 p_bounds);
1856 }
1857 else
1858 error (_("Bad GNAT array descriptor"));
1859
1860 return p_bounds;
1861 }
14f9c5c9
AS
1862 else
1863 return NULL;
1864}
1865
4c4b4cd2
PH
1866/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1867 position of the field containing the address of the bounds data. */
1868
14f9c5c9 1869static int
d2e4a39e 1870fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1871{
b610c045 1872 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1873}
1874
1875/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1876 size of the field containing the address of the bounds data. */
1877
14f9c5c9 1878static int
d2e4a39e 1879fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1880{
1881 type = desc_base_type (type);
1882
d2e4a39e 1883 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1884 return TYPE_FIELD_BITSIZE (type, 1);
1885 else
df86565b 1886 return 8 * ada_check_typedef (type->field (1).type ())->length ();
14f9c5c9
AS
1887}
1888
4c4b4cd2 1889/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1890 pointer to one, the type of its array data (a array-with-no-bounds type);
1891 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1892 data. */
4c4b4cd2 1893
d2e4a39e 1894static struct type *
556bdfd4 1895desc_data_target_type (struct type *type)
14f9c5c9
AS
1896{
1897 type = desc_base_type (type);
1898
4c4b4cd2 1899 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1900 if (is_thin_pntr (type))
940da03e 1901 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1902 else if (is_thick_pntr (type))
556bdfd4
UW
1903 {
1904 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1905
1906 if (data_type
78134374 1907 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
27710edb 1908 return ada_check_typedef (data_type->target_type ());
556bdfd4
UW
1909 }
1910
1911 return NULL;
14f9c5c9
AS
1912}
1913
1914/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1915 its array data. */
4c4b4cd2 1916
d2e4a39e
AS
1917static struct value *
1918desc_data (struct value *arr)
14f9c5c9 1919{
d0c97917 1920 struct type *type = arr->type ();
5b4ee69b 1921
14f9c5c9
AS
1922 if (is_thin_pntr (type))
1923 return thin_data_pntr (arr);
1924 else if (is_thick_pntr (type))
158cc4fe 1925 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1926 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1927 else
1928 return NULL;
1929}
1930
1931
1932/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1933 position of the field containing the address of the data. */
1934
14f9c5c9 1935static int
d2e4a39e 1936fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1937{
b610c045 1938 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1939}
1940
1941/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1942 size of the field containing the address of the data. */
1943
14f9c5c9 1944static int
d2e4a39e 1945fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1946{
1947 type = desc_base_type (type);
1948
1949 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1950 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1951 else
df86565b 1952 return TARGET_CHAR_BIT * type->field (0).type ()->length ();
14f9c5c9
AS
1953}
1954
4c4b4cd2 1955/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1956 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1957 bound, if WHICH is 1. The first bound is I=1. */
1958
d2e4a39e
AS
1959static struct value *
1960desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1961{
250106a7
TT
1962 char bound_name[20];
1963 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1964 which ? 'U' : 'L', i - 1);
158cc4fe 1965 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1966 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1967}
1968
1969/* If BOUNDS is an array-bounds structure type, return the bit position
1970 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1971 bound, if WHICH is 1. The first bound is I=1. */
1972
14f9c5c9 1973static int
d2e4a39e 1974desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1975{
b610c045 1976 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1977}
1978
1979/* If BOUNDS is an array-bounds structure type, return the bit field size
1980 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1981 bound, if WHICH is 1. The first bound is I=1. */
1982
76a01679 1983static int
d2e4a39e 1984desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1985{
1986 type = desc_base_type (type);
1987
d2e4a39e
AS
1988 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1989 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1990 else
df86565b 1991 return 8 * type->field (2 * i + which - 2).type ()->length ();
14f9c5c9
AS
1992}
1993
1994/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1995 Ith bound (numbering from 1). Otherwise, NULL. */
1996
d2e4a39e
AS
1997static struct type *
1998desc_index_type (struct type *type, int i)
14f9c5c9
AS
1999{
2000 type = desc_base_type (type);
2001
78134374 2002 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
2003 {
2004 char bound_name[20];
2005 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
2006 return lookup_struct_elt_type (type, bound_name, 1);
2007 }
d2e4a39e 2008 else
14f9c5c9
AS
2009 return NULL;
2010}
2011
4c4b4cd2
PH
2012/* The number of index positions in the array-bounds type TYPE.
2013 Return 0 if TYPE is NULL. */
2014
14f9c5c9 2015static int
d2e4a39e 2016desc_arity (struct type *type)
14f9c5c9
AS
2017{
2018 type = desc_base_type (type);
2019
2020 if (type != NULL)
1f704f76 2021 return type->num_fields () / 2;
14f9c5c9
AS
2022 return 0;
2023}
2024
4c4b4cd2
PH
2025/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2026 an array descriptor type (representing an unconstrained array
2027 type). */
2028
76a01679
JB
2029static int
2030ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2031{
2032 if (type == NULL)
2033 return 0;
61ee279c 2034 type = ada_check_typedef (type);
78134374 2035 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2036 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2037}
2038
52ce6436 2039/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2040 * to one. */
52ce6436 2041
2c0b251b 2042static int
52ce6436
PH
2043ada_is_array_type (struct type *type)
2044{
78134374
SM
2045 while (type != NULL
2046 && (type->code () == TYPE_CODE_PTR
2047 || type->code () == TYPE_CODE_REF))
27710edb 2048 type = type->target_type ();
52ce6436
PH
2049 return ada_is_direct_array_type (type);
2050}
2051
4c4b4cd2 2052/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2053
14f9c5c9 2054int
4c4b4cd2 2055ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2056{
2057 if (type == NULL)
2058 return 0;
61ee279c 2059 type = ada_check_typedef (type);
78134374
SM
2060 return (type->code () == TYPE_CODE_ARRAY
2061 || (type->code () == TYPE_CODE_PTR
27710edb 2062 && (ada_check_typedef (type->target_type ())->code ()
78134374 2063 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2064}
2065
4c4b4cd2
PH
2066/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2067
14f9c5c9 2068int
4c4b4cd2 2069ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2070{
556bdfd4 2071 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2072
2073 if (type == NULL)
2074 return 0;
61ee279c 2075 type = ada_check_typedef (type);
556bdfd4 2076 return (data_type != NULL
78134374 2077 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2078 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2079}
2080
2081/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 2082 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 2083 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
2084 is still needed. */
2085
14f9c5c9 2086int
ebf56fd3 2087ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 2088{
d2e4a39e 2089 return
14f9c5c9 2090 type != NULL
78134374 2091 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 2092 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 2093 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 2094 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
2095}
2096
2097
4c4b4cd2 2098/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2099 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2100 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2101 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2102 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2103 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2104 a descriptor. */
de93309a
SM
2105
2106static struct type *
d2e4a39e 2107ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2108{
d0c97917
TT
2109 if (ada_is_constrained_packed_array_type (arr->type ()))
2110 return decode_constrained_packed_array_type (arr->type ());
14f9c5c9 2111
d0c97917
TT
2112 if (!ada_is_array_descriptor_type (arr->type ()))
2113 return arr->type ();
d2e4a39e
AS
2114
2115 if (!bounds)
ad82864c
JB
2116 {
2117 struct type *array_type =
d0c97917 2118 ada_check_typedef (desc_data_target_type (arr->type ()));
ad82864c 2119
d0c97917 2120 if (ada_is_unconstrained_packed_array_type (arr->type ()))
ad82864c 2121 TYPE_FIELD_BITSIZE (array_type, 0) =
d0c97917 2122 decode_packed_array_bitsize (arr->type ());
ad82864c
JB
2123
2124 return array_type;
2125 }
14f9c5c9
AS
2126 else
2127 {
d2e4a39e 2128 struct type *elt_type;
14f9c5c9 2129 int arity;
d2e4a39e 2130 struct value *descriptor;
14f9c5c9 2131
d0c97917
TT
2132 elt_type = ada_array_element_type (arr->type (), -1);
2133 arity = ada_array_arity (arr->type ());
14f9c5c9 2134
d2e4a39e 2135 if (elt_type == NULL || arity == 0)
d0c97917 2136 return ada_check_typedef (arr->type ());
14f9c5c9
AS
2137
2138 descriptor = desc_bounds (arr);
d2e4a39e 2139 if (value_as_long (descriptor) == 0)
dda83cd7 2140 return NULL;
d2e4a39e 2141 while (arity > 0)
dda83cd7 2142 {
9fa83a7a 2143 type_allocator alloc (arr->type ());
dda83cd7
SM
2144 struct value *low = desc_one_bound (descriptor, arity, 0);
2145 struct value *high = desc_one_bound (descriptor, arity, 1);
2146
2147 arity -= 1;
e727c536
TT
2148 struct type *range_type
2149 = create_static_range_type (alloc, low->type (),
2150 longest_to_int (value_as_long (low)),
2151 longest_to_int (value_as_long (high)));
9e76b17a 2152 elt_type = create_array_type (alloc, elt_type, range_type);
ad82864c 2153
d0c97917 2154 if (ada_is_unconstrained_packed_array_type (arr->type ()))
e67ad678
JB
2155 {
2156 /* We need to store the element packed bitsize, as well as
dda83cd7 2157 recompute the array size, because it was previously
e67ad678
JB
2158 computed based on the unpacked element size. */
2159 LONGEST lo = value_as_long (low);
2160 LONGEST hi = value_as_long (high);
2161
2162 TYPE_FIELD_BITSIZE (elt_type, 0) =
d0c97917 2163 decode_packed_array_bitsize (arr->type ());
e67ad678 2164 /* If the array has no element, then the size is already
dda83cd7 2165 zero, and does not need to be recomputed. */
e67ad678
JB
2166 if (lo < hi)
2167 {
2168 int array_bitsize =
dda83cd7 2169 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678 2170
9e76b17a 2171 elt_type->set_length ((array_bitsize + 7) / 8);
e67ad678
JB
2172 }
2173 }
dda83cd7 2174 }
14f9c5c9
AS
2175
2176 return lookup_pointer_type (elt_type);
2177 }
2178}
2179
2180/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2181 Otherwise, returns either a standard GDB array with bounds set
2182 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2183 GDB array. Returns NULL if ARR is a null fat pointer. */
2184
d2e4a39e
AS
2185struct value *
2186ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2187{
d0c97917 2188 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2189 {
d2e4a39e 2190 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2191
14f9c5c9 2192 if (arrType == NULL)
dda83cd7 2193 return NULL;
cda03344 2194 return value_cast (arrType, desc_data (arr)->copy ());
14f9c5c9 2195 }
d0c97917 2196 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2197 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2198 else
2199 return arr;
2200}
2201
2202/* If ARR does not represent an array, returns ARR unchanged.
2203 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2204 be ARR itself if it already is in the proper form). */
2205
720d1a40 2206struct value *
d2e4a39e 2207ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2208{
d0c97917 2209 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2210 {
d2e4a39e 2211 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2212
14f9c5c9 2213 if (arrVal == NULL)
dda83cd7 2214 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2215 return value_ind (arrVal);
2216 }
d0c97917 2217 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2218 return decode_constrained_packed_array (arr);
d2e4a39e 2219 else
14f9c5c9
AS
2220 return arr;
2221}
2222
2223/* If TYPE represents a GNAT array type, return it translated to an
2224 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2225 packing). For other types, is the identity. */
2226
d2e4a39e
AS
2227struct type *
2228ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2229{
ad82864c
JB
2230 if (ada_is_constrained_packed_array_type (type))
2231 return decode_constrained_packed_array_type (type);
17280b9f
UW
2232
2233 if (ada_is_array_descriptor_type (type))
556bdfd4 2234 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2235
2236 return type;
14f9c5c9
AS
2237}
2238
4c4b4cd2
PH
2239/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2240
ad82864c 2241static int
57567375 2242ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2243{
2244 if (type == NULL)
2245 return 0;
4c4b4cd2 2246 type = desc_base_type (type);
61ee279c 2247 type = ada_check_typedef (type);
d2e4a39e 2248 return
14f9c5c9
AS
2249 ada_type_name (type) != NULL
2250 && strstr (ada_type_name (type), "___XP") != NULL;
2251}
2252
ad82864c
JB
2253/* Non-zero iff TYPE represents a standard GNAT constrained
2254 packed-array type. */
2255
2256int
2257ada_is_constrained_packed_array_type (struct type *type)
2258{
57567375 2259 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2260 && !ada_is_array_descriptor_type (type);
2261}
2262
2263/* Non-zero iff TYPE represents an array descriptor for a
2264 unconstrained packed-array type. */
2265
2266static int
2267ada_is_unconstrained_packed_array_type (struct type *type)
2268{
57567375
TT
2269 if (!ada_is_array_descriptor_type (type))
2270 return 0;
2271
2272 if (ada_is_gnat_encoded_packed_array_type (type))
2273 return 1;
2274
2275 /* If we saw GNAT encodings, then the above code is sufficient.
2276 However, with minimal encodings, we will just have a thick
2277 pointer instead. */
2278 if (is_thick_pntr (type))
2279 {
2280 type = desc_base_type (type);
2281 /* The structure's first field is a pointer to an array, so this
2282 fetches the array type. */
27710edb 2283 type = type->field (0).type ()->target_type ();
af5300fe
TV
2284 if (type->code () == TYPE_CODE_TYPEDEF)
2285 type = ada_typedef_target_type (type);
57567375
TT
2286 /* Now we can see if the array elements are packed. */
2287 return TYPE_FIELD_BITSIZE (type, 0) > 0;
2288 }
2289
2290 return 0;
ad82864c
JB
2291}
2292
c9a28cbe
TT
2293/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2294 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2295
2296static bool
2297ada_is_any_packed_array_type (struct type *type)
2298{
2299 return (ada_is_constrained_packed_array_type (type)
2300 || (type->code () == TYPE_CODE_ARRAY
2301 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
2302}
2303
ad82864c
JB
2304/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2305 return the size of its elements in bits. */
2306
2307static long
2308decode_packed_array_bitsize (struct type *type)
2309{
0d5cff50
DE
2310 const char *raw_name;
2311 const char *tail;
ad82864c
JB
2312 long bits;
2313
720d1a40
JB
2314 /* Access to arrays implemented as fat pointers are encoded as a typedef
2315 of the fat pointer type. We need the name of the fat pointer type
2316 to do the decoding, so strip the typedef layer. */
78134374 2317 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2318 type = ada_typedef_target_type (type);
2319
2320 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2321 if (!raw_name)
2322 raw_name = ada_type_name (desc_base_type (type));
2323
2324 if (!raw_name)
2325 return 0;
2326
2327 tail = strstr (raw_name, "___XP");
57567375
TT
2328 if (tail == nullptr)
2329 {
2330 gdb_assert (is_thick_pntr (type));
2331 /* The structure's first field is a pointer to an array, so this
2332 fetches the array type. */
27710edb 2333 type = type->field (0).type ()->target_type ();
57567375
TT
2334 /* Now we can see if the array elements are packed. */
2335 return TYPE_FIELD_BITSIZE (type, 0);
2336 }
ad82864c
JB
2337
2338 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2339 {
2340 lim_warning
2341 (_("could not understand bit size information on packed array"));
2342 return 0;
2343 }
2344
2345 return bits;
2346}
2347
14f9c5c9
AS
2348/* Given that TYPE is a standard GDB array type with all bounds filled
2349 in, and that the element size of its ultimate scalar constituents
2350 (that is, either its elements, or, if it is an array of arrays, its
2351 elements' elements, etc.) is *ELT_BITS, return an identical type,
2352 but with the bit sizes of its elements (and those of any
2353 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2354 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2355 in bits.
2356
2357 Note that, for arrays whose index type has an XA encoding where
2358 a bound references a record discriminant, getting that discriminant,
2359 and therefore the actual value of that bound, is not possible
2360 because none of the given parameters gives us access to the record.
2361 This function assumes that it is OK in the context where it is being
2362 used to return an array whose bounds are still dynamic and where
2363 the length is arbitrary. */
4c4b4cd2 2364
d2e4a39e 2365static struct type *
ad82864c 2366constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2367{
d2e4a39e
AS
2368 struct type *new_elt_type;
2369 struct type *new_type;
99b1c762
JB
2370 struct type *index_type_desc;
2371 struct type *index_type;
14f9c5c9
AS
2372 LONGEST low_bound, high_bound;
2373
61ee279c 2374 type = ada_check_typedef (type);
78134374 2375 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2376 return type;
2377
99b1c762
JB
2378 index_type_desc = ada_find_parallel_type (type, "___XA");
2379 if (index_type_desc)
940da03e 2380 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2381 NULL);
2382 else
3d967001 2383 index_type = type->index_type ();
99b1c762 2384
9e76b17a 2385 type_allocator alloc (type);
ad82864c 2386 new_elt_type =
27710edb 2387 constrained_packed_array_type (ada_check_typedef (type->target_type ()),
ad82864c 2388 elt_bits);
9e76b17a 2389 new_type = create_array_type (alloc, new_elt_type, index_type);
14f9c5c9 2390 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2391 new_type->set_name (ada_type_name (type));
14f9c5c9 2392
78134374 2393 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2394 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2395 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2396 low_bound = high_bound = 0;
2397 if (high_bound < low_bound)
b6cdbc9a
SM
2398 {
2399 *elt_bits = 0;
2400 new_type->set_length (0);
2401 }
d2e4a39e 2402 else
14f9c5c9
AS
2403 {
2404 *elt_bits *= (high_bound - low_bound + 1);
b6cdbc9a 2405 new_type->set_length ((*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
14f9c5c9
AS
2406 }
2407
9cdd0d12 2408 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2409 return new_type;
2410}
2411
ad82864c
JB
2412/* The array type encoded by TYPE, where
2413 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2414
d2e4a39e 2415static struct type *
ad82864c 2416decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2417{
0d5cff50 2418 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2419 char *name;
0d5cff50 2420 const char *tail;
d2e4a39e 2421 struct type *shadow_type;
14f9c5c9 2422 long bits;
14f9c5c9 2423
727e3d2e
JB
2424 if (!raw_name)
2425 raw_name = ada_type_name (desc_base_type (type));
2426
2427 if (!raw_name)
2428 return NULL;
2429
2430 name = (char *) alloca (strlen (raw_name) + 1);
2431 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2432 type = desc_base_type (type);
2433
14f9c5c9
AS
2434 memcpy (name, raw_name, tail - raw_name);
2435 name[tail - raw_name] = '\000';
2436
b4ba55a1
JB
2437 shadow_type = ada_find_parallel_type_with_name (type, name);
2438
2439 if (shadow_type == NULL)
14f9c5c9 2440 {
323e0a4a 2441 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2442 return NULL;
2443 }
f168693b 2444 shadow_type = check_typedef (shadow_type);
14f9c5c9 2445
78134374 2446 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2447 {
0963b4bd
MS
2448 lim_warning (_("could not understand bounds "
2449 "information on packed array"));
14f9c5c9
AS
2450 return NULL;
2451 }
d2e4a39e 2452
ad82864c
JB
2453 bits = decode_packed_array_bitsize (type);
2454 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2455}
2456
a7400e44
TT
2457/* Helper function for decode_constrained_packed_array. Set the field
2458 bitsize on a series of packed arrays. Returns the number of
2459 elements in TYPE. */
2460
2461static LONGEST
2462recursively_update_array_bitsize (struct type *type)
2463{
2464 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2465
2466 LONGEST low, high;
1f8d2881 2467 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2468 || low > high)
2469 return 0;
2470 LONGEST our_len = high - low + 1;
2471
27710edb 2472 struct type *elt_type = type->target_type ();
a7400e44
TT
2473 if (elt_type->code () == TYPE_CODE_ARRAY)
2474 {
2475 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2476 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2477 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2478
b6cdbc9a
SM
2479 type->set_length (((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2480 / HOST_CHAR_BIT));
a7400e44
TT
2481 }
2482
2483 return our_len;
2484}
2485
ad82864c
JB
2486/* Given that ARR is a struct value *indicating a GNAT constrained packed
2487 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2488 standard GDB array type except that the BITSIZEs of the array
2489 target types are set to the number of bits in each element, and the
4c4b4cd2 2490 type length is set appropriately. */
14f9c5c9 2491
d2e4a39e 2492static struct value *
ad82864c 2493decode_constrained_packed_array (struct value *arr)
14f9c5c9 2494{
4c4b4cd2 2495 struct type *type;
14f9c5c9 2496
11aa919a
PMR
2497 /* If our value is a pointer, then dereference it. Likewise if
2498 the value is a reference. Make sure that this operation does not
2499 cause the target type to be fixed, as this would indirectly cause
2500 this array to be decoded. The rest of the routine assumes that
2501 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2502 and "value_ind" routines to perform the dereferencing, as opposed
2503 to using "ada_coerce_ref" or "ada_value_ind". */
2504 arr = coerce_ref (arr);
d0c97917 2505 if (ada_check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
284614f0 2506 arr = value_ind (arr);
4c4b4cd2 2507
d0c97917 2508 type = decode_constrained_packed_array_type (arr->type ());
14f9c5c9
AS
2509 if (type == NULL)
2510 {
323e0a4a 2511 error (_("can't unpack array"));
14f9c5c9
AS
2512 return NULL;
2513 }
61ee279c 2514
a7400e44
TT
2515 /* Decoding the packed array type could not correctly set the field
2516 bitsizes for any dimension except the innermost, because the
2517 bounds may be variable and were not passed to that function. So,
2518 we further resolve the array bounds here and then update the
2519 sizes. */
efaf1ae0 2520 const gdb_byte *valaddr = arr->contents_for_printing ().data ();
9feb2d07 2521 CORE_ADDR address = arr->address ();
a7400e44 2522 gdb::array_view<const gdb_byte> view
df86565b 2523 = gdb::make_array_view (valaddr, type->length ());
a7400e44
TT
2524 type = resolve_dynamic_type (type, view, address);
2525 recursively_update_array_bitsize (type);
2526
d0c97917
TT
2527 if (type_byte_order (arr->type ()) == BFD_ENDIAN_BIG
2528 && ada_is_modular_type (arr->type ()))
61ee279c
PH
2529 {
2530 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2531 array with no wrapper. In order to interpret the value through
2532 the (left-justified) packed array type we just built, we must
2533 first left-justify it. */
61ee279c
PH
2534 int bit_size, bit_pos;
2535 ULONGEST mod;
2536
d0c97917 2537 mod = ada_modulus (arr->type ()) - 1;
61ee279c
PH
2538 bit_size = 0;
2539 while (mod > 0)
2540 {
2541 bit_size += 1;
2542 mod >>= 1;
2543 }
d0c97917 2544 bit_pos = HOST_CHAR_BIT * arr->type ()->length () - bit_size;
61ee279c
PH
2545 arr = ada_value_primitive_packed_val (arr, NULL,
2546 bit_pos / HOST_CHAR_BIT,
2547 bit_pos % HOST_CHAR_BIT,
2548 bit_size,
2549 type);
2550 }
2551
4c4b4cd2 2552 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2553}
2554
2555
2556/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2557 given in IND. ARR must be a simple array. */
14f9c5c9 2558
d2e4a39e
AS
2559static struct value *
2560value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2561{
2562 int i;
2563 int bits, elt_off, bit_off;
2564 long elt_total_bit_offset;
d2e4a39e
AS
2565 struct type *elt_type;
2566 struct value *v;
14f9c5c9
AS
2567
2568 bits = 0;
2569 elt_total_bit_offset = 0;
d0c97917 2570 elt_type = ada_check_typedef (arr->type ());
d2e4a39e 2571 for (i = 0; i < arity; i += 1)
14f9c5c9 2572 {
78134374 2573 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2574 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2575 error
2576 (_("attempt to do packed indexing of "
0963b4bd 2577 "something other than a packed array"));
14f9c5c9 2578 else
dda83cd7
SM
2579 {
2580 struct type *range_type = elt_type->index_type ();
2581 LONGEST lowerbound, upperbound;
2582 LONGEST idx;
2583
1f8d2881 2584 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2585 {
2586 lim_warning (_("don't know bounds of array"));
2587 lowerbound = upperbound = 0;
2588 }
2589
2590 idx = pos_atr (ind[i]);
2591 if (idx < lowerbound || idx > upperbound)
2592 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2593 (long) idx);
dda83cd7
SM
2594 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2595 elt_total_bit_offset += (idx - lowerbound) * bits;
27710edb 2596 elt_type = ada_check_typedef (elt_type->target_type ());
dda83cd7 2597 }
14f9c5c9
AS
2598 }
2599 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2600 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2601
2602 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2603 bits, elt_type);
14f9c5c9
AS
2604 return v;
2605}
2606
4c4b4cd2 2607/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2608
2609static int
d2e4a39e 2610has_negatives (struct type *type)
14f9c5c9 2611{
78134374 2612 switch (type->code ())
d2e4a39e
AS
2613 {
2614 default:
2615 return 0;
2616 case TYPE_CODE_INT:
c6d940a9 2617 return !type->is_unsigned ();
d2e4a39e 2618 case TYPE_CODE_RANGE:
5537ddd0 2619 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2620 }
14f9c5c9 2621}
d2e4a39e 2622
f93fca70 2623/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2624 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2625 the unpacked buffer.
14f9c5c9 2626
5b639dea
JB
2627 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2628 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2629
f93fca70
JB
2630 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2631 zero otherwise.
14f9c5c9 2632
f93fca70 2633 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2634
f93fca70
JB
2635 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2636
2637static void
2638ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2639 gdb_byte *unpacked, int unpacked_len,
2640 int is_big_endian, int is_signed_type,
2641 int is_scalar)
2642{
a1c95e6b
JB
2643 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2644 int src_idx; /* Index into the source area */
2645 int src_bytes_left; /* Number of source bytes left to process. */
2646 int srcBitsLeft; /* Number of source bits left to move */
2647 int unusedLS; /* Number of bits in next significant
dda83cd7 2648 byte of source that are unused */
a1c95e6b 2649
a1c95e6b
JB
2650 int unpacked_idx; /* Index into the unpacked buffer */
2651 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2652
4c4b4cd2 2653 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2654 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2655 unsigned char sign;
a1c95e6b 2656
4c4b4cd2
PH
2657 /* Transmit bytes from least to most significant; delta is the direction
2658 the indices move. */
f93fca70 2659 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2660
5b639dea
JB
2661 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2662 bits from SRC. .*/
2663 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2664 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2665 bit_size, unpacked_len);
2666
14f9c5c9 2667 srcBitsLeft = bit_size;
086ca51f 2668 src_bytes_left = src_len;
f93fca70 2669 unpacked_bytes_left = unpacked_len;
14f9c5c9 2670 sign = 0;
f93fca70
JB
2671
2672 if (is_big_endian)
14f9c5c9 2673 {
086ca51f 2674 src_idx = src_len - 1;
f93fca70
JB
2675 if (is_signed_type
2676 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2677 sign = ~0;
d2e4a39e
AS
2678
2679 unusedLS =
dda83cd7
SM
2680 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2681 % HOST_CHAR_BIT;
14f9c5c9 2682
f93fca70
JB
2683 if (is_scalar)
2684 {
dda83cd7
SM
2685 accumSize = 0;
2686 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2687 }
2688 else
2689 {
dda83cd7
SM
2690 /* Non-scalar values must be aligned at a byte boundary... */
2691 accumSize =
2692 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2693 /* ... And are placed at the beginning (most-significant) bytes
2694 of the target. */
2695 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2696 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2697 }
14f9c5c9 2698 }
d2e4a39e 2699 else
14f9c5c9
AS
2700 {
2701 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2702
086ca51f 2703 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2704 unusedLS = bit_offset;
2705 accumSize = 0;
2706
f93fca70 2707 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2708 sign = ~0;
14f9c5c9 2709 }
d2e4a39e 2710
14f9c5c9 2711 accum = 0;
086ca51f 2712 while (src_bytes_left > 0)
14f9c5c9
AS
2713 {
2714 /* Mask for removing bits of the next source byte that are not
dda83cd7 2715 part of the value. */
d2e4a39e 2716 unsigned int unusedMSMask =
dda83cd7
SM
2717 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2718 1;
4c4b4cd2 2719 /* Sign-extend bits for this byte. */
14f9c5c9 2720 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2721
d2e4a39e 2722 accum |=
dda83cd7 2723 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2724 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2725 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2726 {
2727 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2728 accumSize -= HOST_CHAR_BIT;
2729 accum >>= HOST_CHAR_BIT;
2730 unpacked_bytes_left -= 1;
2731 unpacked_idx += delta;
2732 }
14f9c5c9
AS
2733 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2734 unusedLS = 0;
086ca51f
JB
2735 src_bytes_left -= 1;
2736 src_idx += delta;
14f9c5c9 2737 }
086ca51f 2738 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2739 {
2740 accum |= sign << accumSize;
db297a65 2741 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2742 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2743 if (accumSize < 0)
2744 accumSize = 0;
14f9c5c9 2745 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2746 unpacked_bytes_left -= 1;
2747 unpacked_idx += delta;
14f9c5c9 2748 }
f93fca70
JB
2749}
2750
2751/* Create a new value of type TYPE from the contents of OBJ starting
2752 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2753 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2754 assigning through the result will set the field fetched from.
2755 VALADDR is ignored unless OBJ is NULL, in which case,
2756 VALADDR+OFFSET must address the start of storage containing the
2757 packed value. The value returned in this case is never an lval.
2758 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2759
2760struct value *
2761ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2762 long offset, int bit_offset, int bit_size,
dda83cd7 2763 struct type *type)
f93fca70
JB
2764{
2765 struct value *v;
bfb1c796 2766 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2767 gdb_byte *unpacked;
220475ed 2768 const int is_scalar = is_scalar_type (type);
d5a22e77 2769 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2770 gdb::byte_vector staging;
f93fca70
JB
2771
2772 type = ada_check_typedef (type);
2773
d0a9e810 2774 if (obj == NULL)
bfb1c796 2775 src = valaddr + offset;
d0a9e810 2776 else
efaf1ae0 2777 src = obj->contents ().data () + offset;
d0a9e810
JB
2778
2779 if (is_dynamic_type (type))
2780 {
2781 /* The length of TYPE might by dynamic, so we need to resolve
2782 TYPE in order to know its actual size, which we then use
2783 to create the contents buffer of the value we return.
2784 The difficulty is that the data containing our object is
2785 packed, and therefore maybe not at a byte boundary. So, what
2786 we do, is unpack the data into a byte-aligned buffer, and then
2787 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2788 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2789 staging.resize (staging_len);
d0a9e810
JB
2790
2791 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2792 staging.data (), staging.size (),
d0a9e810
JB
2793 is_big_endian, has_negatives (type),
2794 is_scalar);
b249d2c2 2795 type = resolve_dynamic_type (type, staging, 0);
df86565b 2796 if (type->length () < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
0cafa88c
JB
2797 {
2798 /* This happens when the length of the object is dynamic,
2799 and is actually smaller than the space reserved for it.
2800 For instance, in an array of variant records, the bit_size
2801 we're given is the array stride, which is constant and
2802 normally equal to the maximum size of its element.
2803 But, in reality, each element only actually spans a portion
2804 of that stride. */
df86565b 2805 bit_size = type->length () * HOST_CHAR_BIT;
0cafa88c 2806 }
d0a9e810
JB
2807 }
2808
f93fca70
JB
2809 if (obj == NULL)
2810 {
317c3ed9 2811 v = value::allocate (type);
bfb1c796 2812 src = valaddr + offset;
f93fca70 2813 }
736355f2 2814 else if (obj->lval () == lval_memory && obj->lazy ())
f93fca70 2815 {
0cafa88c 2816 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2817 gdb_byte *buf;
0cafa88c 2818
9feb2d07 2819 v = value_at (type, obj->address () + offset);
bfb1c796 2820 buf = (gdb_byte *) alloca (src_len);
9feb2d07 2821 read_memory (v->address (), buf, src_len);
bfb1c796 2822 src = buf;
f93fca70
JB
2823 }
2824 else
2825 {
317c3ed9 2826 v = value::allocate (type);
efaf1ae0 2827 src = obj->contents ().data () + offset;
f93fca70
JB
2828 }
2829
2830 if (obj != NULL)
2831 {
2832 long new_offset = offset;
2833
8181b7b6 2834 v->set_component_location (obj);
5011c493 2835 v->set_bitpos (bit_offset + obj->bitpos ());
f49d5fa2 2836 v->set_bitsize (bit_size);
5011c493 2837 if (v->bitpos () >= HOST_CHAR_BIT)
dda83cd7 2838 {
f93fca70 2839 ++new_offset;
5011c493 2840 v->set_bitpos (v->bitpos () - HOST_CHAR_BIT);
dda83cd7 2841 }
76675c4d 2842 v->set_offset (new_offset);
f93fca70
JB
2843
2844 /* Also set the parent value. This is needed when trying to
2845 assign a new value (in inferior memory). */
fac7bdaa 2846 v->set_parent (obj);
f93fca70
JB
2847 }
2848 else
f49d5fa2 2849 v->set_bitsize (bit_size);
bbe912ba 2850 unpacked = v->contents_writeable ().data ();
f93fca70
JB
2851
2852 if (bit_size == 0)
2853 {
df86565b 2854 memset (unpacked, 0, type->length ());
f93fca70
JB
2855 return v;
2856 }
2857
df86565b 2858 if (staging.size () == type->length ())
f93fca70 2859 {
d0a9e810
JB
2860 /* Small short-cut: If we've unpacked the data into a buffer
2861 of the same size as TYPE's length, then we can reuse that,
2862 instead of doing the unpacking again. */
d5722aa2 2863 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2864 }
d0a9e810
JB
2865 else
2866 ada_unpack_from_contents (src, bit_offset, bit_size,
df86565b 2867 unpacked, type->length (),
d0a9e810 2868 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2869
14f9c5c9
AS
2870 return v;
2871}
d2e4a39e 2872
14f9c5c9
AS
2873/* Store the contents of FROMVAL into the location of TOVAL.
2874 Return a new value with the location of TOVAL and contents of
2875 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2876 floating-point or non-scalar types. */
14f9c5c9 2877
d2e4a39e
AS
2878static struct value *
2879ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2880{
d0c97917 2881 struct type *type = toval->type ();
f49d5fa2 2882 int bits = toval->bitsize ();
14f9c5c9 2883
52ce6436
PH
2884 toval = ada_coerce_ref (toval);
2885 fromval = ada_coerce_ref (fromval);
2886
d0c97917 2887 if (ada_is_direct_array_type (toval->type ()))
52ce6436 2888 toval = ada_coerce_to_simple_array (toval);
d0c97917 2889 if (ada_is_direct_array_type (fromval->type ()))
52ce6436
PH
2890 fromval = ada_coerce_to_simple_array (fromval);
2891
4b53ca88 2892 if (!toval->deprecated_modifiable ())
323e0a4a 2893 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2894
736355f2 2895 if (toval->lval () == lval_memory
14f9c5c9 2896 && bits > 0
78134374 2897 && (type->code () == TYPE_CODE_FLT
dda83cd7 2898 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2899 {
5011c493 2900 int len = (toval->bitpos ()
df407dfe 2901 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2902 int from_size;
224c3ddb 2903 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2904 struct value *val;
9feb2d07 2905 CORE_ADDR to_addr = toval->address ();
14f9c5c9 2906
78134374 2907 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2908 fromval = value_cast (type, fromval);
14f9c5c9 2909
52ce6436 2910 read_memory (to_addr, buffer, len);
f49d5fa2 2911 from_size = fromval->bitsize ();
aced2898 2912 if (from_size == 0)
d0c97917 2913 from_size = fromval->type ()->length () * TARGET_CHAR_BIT;
d48e62f4 2914
d5a22e77 2915 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4 2916 ULONGEST from_offset = 0;
d0c97917 2917 if (is_big_endian && is_scalar_type (fromval->type ()))
d48e62f4 2918 from_offset = from_size - bits;
5011c493 2919 copy_bitwise (buffer, toval->bitpos (),
efaf1ae0 2920 fromval->contents ().data (), from_offset,
d48e62f4 2921 bits, is_big_endian);
972daa01 2922 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2923
cda03344 2924 val = toval->copy ();
bbe912ba 2925 memcpy (val->contents_raw ().data (),
efaf1ae0 2926 fromval->contents ().data (),
df86565b 2927 type->length ());
81ae560c 2928 val->deprecated_set_type (type);
d2e4a39e 2929
14f9c5c9
AS
2930 return val;
2931 }
2932
2933 return value_assign (toval, fromval);
2934}
2935
2936
7c512744
JB
2937/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2938 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2939 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2940 COMPONENT, and not the inferior's memory. The current contents
2941 of COMPONENT are ignored.
2942
2943 Although not part of the initial design, this function also works
2944 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2945 had a null address, and COMPONENT had an address which is equal to
2946 its offset inside CONTAINER. */
2947
52ce6436
PH
2948static void
2949value_assign_to_component (struct value *container, struct value *component,
2950 struct value *val)
2951{
2952 LONGEST offset_in_container =
9feb2d07 2953 (LONGEST) (component->address () - container->address ());
7c512744 2954 int bit_offset_in_container =
5011c493 2955 component->bitpos () - container->bitpos ();
52ce6436 2956 int bits;
7c512744 2957
d0c97917 2958 val = value_cast (component->type (), val);
52ce6436 2959
f49d5fa2 2960 if (component->bitsize () == 0)
d0c97917 2961 bits = TARGET_CHAR_BIT * component->type ()->length ();
52ce6436 2962 else
f49d5fa2 2963 bits = component->bitsize ();
52ce6436 2964
d0c97917 2965 if (type_byte_order (container->type ()) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2966 {
2967 int src_offset;
2968
d0c97917 2969 if (is_scalar_type (check_typedef (component->type ())))
dda83cd7 2970 src_offset
d0c97917 2971 = component->type ()->length () * TARGET_CHAR_BIT - bits;
2a62dfa9
JB
2972 else
2973 src_offset = 0;
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 (), src_offset, bits, 1);
2a62dfa9 2978 }
52ce6436 2979 else
bbe912ba 2980 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2981 + offset_in_container),
5011c493 2982 container->bitpos () + bit_offset_in_container,
efaf1ae0 2983 val->contents ().data (), 0, bits, 0);
7c512744
JB
2984}
2985
736ade86
XR
2986/* Determine if TYPE is an access to an unconstrained array. */
2987
d91e9ea8 2988bool
736ade86
XR
2989ada_is_access_to_unconstrained_array (struct type *type)
2990{
78134374 2991 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2992 && is_thick_pntr (ada_typedef_target_type (type)));
2993}
2994
4c4b4cd2
PH
2995/* The value of the element of array ARR at the ARITY indices given in IND.
2996 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2997 thereto. */
2998
d2e4a39e
AS
2999struct value *
3000ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3001{
3002 int k;
d2e4a39e
AS
3003 struct value *elt;
3004 struct type *elt_type;
14f9c5c9
AS
3005
3006 elt = ada_coerce_to_simple_array (arr);
3007
d0c97917 3008 elt_type = ada_check_typedef (elt->type ());
78134374 3009 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
3010 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
3011 return value_subscript_packed (elt, arity, ind);
3012
3013 for (k = 0; k < arity; k += 1)
3014 {
27710edb 3015 struct type *saved_elt_type = elt_type->target_type ();
b9c50e9a 3016
78134374 3017 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3018 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3019
2497b498 3020 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3021
3022 if (ada_is_access_to_unconstrained_array (saved_elt_type)
d0c97917 3023 && elt->type ()->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3024 {
3025 /* The element is a typedef to an unconstrained array,
3026 except that the value_subscript call stripped the
3027 typedef layer. The typedef layer is GNAT's way to
3028 specify that the element is, at the source level, an
3029 access to the unconstrained array, rather than the
3030 unconstrained array. So, we need to restore that
3031 typedef layer, which we can do by forcing the element's
3032 type back to its original type. Otherwise, the returned
3033 value is going to be printed as the array, rather
3034 than as an access. Another symptom of the same issue
3035 would be that an expression trying to dereference the
3036 element would also be improperly rejected. */
81ae560c 3037 elt->deprecated_set_type (saved_elt_type);
b9c50e9a
XR
3038 }
3039
d0c97917 3040 elt_type = ada_check_typedef (elt->type ());
14f9c5c9 3041 }
b9c50e9a 3042
14f9c5c9
AS
3043 return elt;
3044}
3045
deede10c
JB
3046/* Assuming ARR is a pointer to a GDB array, the value of the element
3047 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3048 Does not read the entire array into memory.
3049
3050 Note: Unlike what one would expect, this function is used instead of
3051 ada_value_subscript for basically all non-packed array types. The reason
3052 for this is that a side effect of doing our own pointer arithmetics instead
3053 of relying on value_subscript is that there is no implicit typedef peeling.
3054 This is important for arrays of array accesses, where it allows us to
3055 preserve the fact that the array's element is an array access, where the
3056 access part os encoded in a typedef layer. */
14f9c5c9 3057
2c0b251b 3058static struct value *
deede10c 3059ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3060{
3061 int k;
919e6dbe 3062 struct value *array_ind = ada_value_ind (arr);
deede10c 3063 struct type *type
463b870d 3064 = check_typedef (array_ind->enclosing_type ());
919e6dbe 3065
78134374 3066 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
3067 && TYPE_FIELD_BITSIZE (type, 0) > 0)
3068 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3069
3070 for (k = 0; k < arity; k += 1)
3071 {
3072 LONGEST lwb, upb;
14f9c5c9 3073
78134374 3074 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3075 error (_("too many subscripts (%d expected)"), k);
27710edb 3076 arr = value_cast (lookup_pointer_type (type->target_type ()),
cda03344 3077 arr->copy ());
3d967001 3078 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3079 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
27710edb 3080 type = type->target_type ();
14f9c5c9
AS
3081 }
3082
3083 return value_ind (arr);
3084}
3085
0b5d8877 3086/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3087 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3088 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3089 this array is LOW, as per Ada rules. */
0b5d8877 3090static struct value *
f5938064 3091ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3092 int low, int high)
0b5d8877 3093{
b0dd7688 3094 struct type *type0 = ada_check_typedef (type);
27710edb 3095 struct type *base_index_type = type0->index_type ()->target_type ();
e727c536 3096 type_allocator alloc (base_index_type);
0c9c3474 3097 struct type *index_type
e727c536 3098 = create_static_range_type (alloc, base_index_type, low, high);
9fe561ab 3099 struct type *slice_type = create_array_type_with_stride
9e76b17a 3100 (alloc, type0->target_type (), index_type,
24e99c6c 3101 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3102 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 3103 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 3104 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3105 CORE_ADDR base;
3106
6244c119
SM
3107 low_pos = discrete_position (base_index_type, low);
3108 base_low_pos = discrete_position (base_index_type, base_low);
3109
3110 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3111 {
3112 warning (_("unable to get positions in slice, use bounds instead"));
3113 low_pos = low;
3114 base_low_pos = base_low;
3115 }
5b4ee69b 3116
7ff5b937
TT
3117 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
3118 if (stride == 0)
df86565b 3119 stride = type0->target_type ()->length ();
7ff5b937 3120
6244c119 3121 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3122 return value_at_lazy (slice_type, base);
0b5d8877
PH
3123}
3124
3125
3126static struct value *
3127ada_value_slice (struct value *array, int low, int high)
3128{
d0c97917 3129 struct type *type = ada_check_typedef (array->type ());
27710edb 3130 struct type *base_index_type = type->index_type ()->target_type ();
e727c536 3131 type_allocator alloc (type->index_type ());
0c9c3474 3132 struct type *index_type
e727c536 3133 = create_static_range_type (alloc, type->index_type (), low, high);
9fe561ab 3134 struct type *slice_type = create_array_type_with_stride
9e76b17a 3135 (alloc, type->target_type (), index_type,
24e99c6c 3136 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3137 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
3138 gdb::optional<LONGEST> low_pos, high_pos;
3139
5b4ee69b 3140
6244c119
SM
3141 low_pos = discrete_position (base_index_type, low);
3142 high_pos = discrete_position (base_index_type, high);
3143
3144 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3145 {
3146 warning (_("unable to get positions in slice, use bounds instead"));
3147 low_pos = low;
3148 high_pos = high;
3149 }
3150
3151 return value_cast (slice_type,
6244c119 3152 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3153}
3154
14f9c5c9
AS
3155/* If type is a record type in the form of a standard GNAT array
3156 descriptor, returns the number of dimensions for type. If arr is a
3157 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3158 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3159
3160int
d2e4a39e 3161ada_array_arity (struct type *type)
14f9c5c9
AS
3162{
3163 int arity;
3164
3165 if (type == NULL)
3166 return 0;
3167
3168 type = desc_base_type (type);
3169
3170 arity = 0;
78134374 3171 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3172 return desc_arity (desc_bounds_type (type));
d2e4a39e 3173 else
78134374 3174 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3175 {
dda83cd7 3176 arity += 1;
27710edb 3177 type = ada_check_typedef (type->target_type ());
14f9c5c9 3178 }
d2e4a39e 3179
14f9c5c9
AS
3180 return arity;
3181}
3182
3183/* If TYPE is a record type in the form of a standard GNAT array
3184 descriptor or a simple array type, returns the element type for
3185 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3186 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3187
d2e4a39e
AS
3188struct type *
3189ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3190{
3191 type = desc_base_type (type);
3192
78134374 3193 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3194 {
3195 int k;
d2e4a39e 3196 struct type *p_array_type;
14f9c5c9 3197
556bdfd4 3198 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3199
3200 k = ada_array_arity (type);
3201 if (k == 0)
dda83cd7 3202 return NULL;
d2e4a39e 3203
4c4b4cd2 3204 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3205 if (nindices >= 0 && k > nindices)
dda83cd7 3206 k = nindices;
d2e4a39e 3207 while (k > 0 && p_array_type != NULL)
dda83cd7 3208 {
27710edb 3209 p_array_type = ada_check_typedef (p_array_type->target_type ());
dda83cd7
SM
3210 k -= 1;
3211 }
14f9c5c9
AS
3212 return p_array_type;
3213 }
78134374 3214 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3215 {
78134374 3216 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7 3217 {
27710edb 3218 type = type->target_type ();
6a40c6e4
TT
3219 /* A multi-dimensional array is represented using a sequence
3220 of array types. If one of these types has a name, then
3221 it is not another dimension of the outer array, but
3222 rather the element type of the outermost array. */
3223 if (type->name () != nullptr)
3224 break;
dda83cd7
SM
3225 nindices -= 1;
3226 }
14f9c5c9
AS
3227 return type;
3228 }
3229
3230 return NULL;
3231}
3232
08a057e6 3233/* See ada-lang.h. */
14f9c5c9 3234
08a057e6 3235struct type *
1eea4ebd 3236ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3237{
4c4b4cd2
PH
3238 struct type *result_type;
3239
14f9c5c9
AS
3240 type = desc_base_type (type);
3241
1eea4ebd
UW
3242 if (n < 0 || n > ada_array_arity (type))
3243 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3244
4c4b4cd2 3245 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3246 {
3247 int i;
3248
3249 for (i = 1; i < n; i += 1)
2869ac4b
TT
3250 {
3251 type = ada_check_typedef (type);
27710edb 3252 type = type->target_type ();
2869ac4b 3253 }
27710edb 3254 result_type = ada_check_typedef (type)->index_type ()->target_type ();
4c4b4cd2 3255 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3256 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3257 perhaps stabsread.c would make more sense. */
78134374 3258 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3259 result_type = NULL;
14f9c5c9 3260 }
d2e4a39e 3261 else
1eea4ebd
UW
3262 {
3263 result_type = desc_index_type (desc_bounds_type (type), n);
3264 if (result_type == NULL)
3265 error (_("attempt to take bound of something that is not an array"));
3266 }
3267
3268 return result_type;
14f9c5c9
AS
3269}
3270
3271/* Given that arr is an array type, returns the lower bound of the
3272 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3273 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3274 array-descriptor type. It works for other arrays with bounds supplied
3275 by run-time quantities other than discriminants. */
14f9c5c9 3276
abb68b3e 3277static LONGEST
fb5e3d5c 3278ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3279{
8a48ac95 3280 struct type *type, *index_type_desc, *index_type;
1ce677a4 3281 int i;
262452ec
JK
3282
3283 gdb_assert (which == 0 || which == 1);
14f9c5c9 3284
ad82864c
JB
3285 if (ada_is_constrained_packed_array_type (arr_type))
3286 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3287
4c4b4cd2 3288 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
66cf9350 3289 return - which;
14f9c5c9 3290
78134374 3291 if (arr_type->code () == TYPE_CODE_PTR)
27710edb 3292 type = arr_type->target_type ();
14f9c5c9
AS
3293 else
3294 type = arr_type;
3295
22c4c60c 3296 if (type->is_fixed_instance ())
bafffb51
JB
3297 {
3298 /* The array has already been fixed, so we do not need to
3299 check the parallel ___XA type again. That encoding has
3300 already been applied, so ignore it now. */
3301 index_type_desc = NULL;
3302 }
3303 else
3304 {
3305 index_type_desc = ada_find_parallel_type (type, "___XA");
3306 ada_fixup_array_indexes_type (index_type_desc);
3307 }
3308
262452ec 3309 if (index_type_desc != NULL)
940da03e 3310 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3311 NULL);
262452ec 3312 else
8a48ac95
JB
3313 {
3314 struct type *elt_type = check_typedef (type);
3315
3316 for (i = 1; i < n; i++)
27710edb 3317 elt_type = check_typedef (elt_type->target_type ());
8a48ac95 3318
3d967001 3319 index_type = elt_type->index_type ();
8a48ac95 3320 }
262452ec 3321
66cf9350
TT
3322 return (which == 0
3323 ? ada_discrete_type_low_bound (index_type)
3324 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3325}
3326
3327/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3328 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3329 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3330 supplied by run-time quantities other than discriminants. */
14f9c5c9 3331
1eea4ebd 3332static LONGEST
4dc81987 3333ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3334{
eb479039
JB
3335 struct type *arr_type;
3336
d0c97917 3337 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3338 arr = value_ind (arr);
463b870d 3339 arr_type = arr->enclosing_type ();
14f9c5c9 3340
ad82864c
JB
3341 if (ada_is_constrained_packed_array_type (arr_type))
3342 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3343 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3344 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3345 else
1eea4ebd 3346 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3347}
3348
3349/* Given that arr is an array value, returns the length of the
3350 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3351 supplied by run-time quantities other than discriminants.
3352 Does not work for arrays indexed by enumeration types with representation
3353 clauses at the moment. */
14f9c5c9 3354
1eea4ebd 3355static LONGEST
d2e4a39e 3356ada_array_length (struct value *arr, int n)
14f9c5c9 3357{
aa715135
JG
3358 struct type *arr_type, *index_type;
3359 int low, high;
eb479039 3360
d0c97917 3361 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3362 arr = value_ind (arr);
463b870d 3363 arr_type = arr->enclosing_type ();
14f9c5c9 3364
ad82864c
JB
3365 if (ada_is_constrained_packed_array_type (arr_type))
3366 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3367
4c4b4cd2 3368 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3369 {
3370 low = ada_array_bound_from_type (arr_type, n, 0);
3371 high = ada_array_bound_from_type (arr_type, n, 1);
3372 }
14f9c5c9 3373 else
aa715135
JG
3374 {
3375 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3376 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3377 }
3378
f168693b 3379 arr_type = check_typedef (arr_type);
7150d33c 3380 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3381 if (index_type != NULL)
3382 {
3383 struct type *base_type;
78134374 3384 if (index_type->code () == TYPE_CODE_RANGE)
27710edb 3385 base_type = index_type->target_type ();
aa715135
JG
3386 else
3387 base_type = index_type;
3388
3389 low = pos_atr (value_from_longest (base_type, low));
3390 high = pos_atr (value_from_longest (base_type, high));
3391 }
3392 return high - low + 1;
4c4b4cd2
PH
3393}
3394
bff8c71f
TT
3395/* An array whose type is that of ARR_TYPE (an array type), with
3396 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3397 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3398
3399static struct value *
bff8c71f 3400empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3401{
b0dd7688 3402 struct type *arr_type0 = ada_check_typedef (arr_type);
e727c536 3403 type_allocator alloc (arr_type0->index_type ()->target_type ());
0c9c3474
SA
3404 struct type *index_type
3405 = create_static_range_type
e727c536 3406 (alloc, arr_type0->index_type ()->target_type (), low,
bff8c71f 3407 high < low ? low - 1 : high);
b0dd7688 3408 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3409
9e76b17a 3410 return value::allocate (create_array_type (alloc, elt_type, index_type));
14f9c5c9 3411}
14f9c5c9 3412\f
d2e4a39e 3413
dda83cd7 3414 /* Name resolution */
14f9c5c9 3415
4c4b4cd2
PH
3416/* The "decoded" name for the user-definable Ada operator corresponding
3417 to OP. */
14f9c5c9 3418
d2e4a39e 3419static const char *
4c4b4cd2 3420ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3421{
3422 int i;
3423
4c4b4cd2 3424 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3425 {
3426 if (ada_opname_table[i].op == op)
dda83cd7 3427 return ada_opname_table[i].decoded;
14f9c5c9 3428 }
323e0a4a 3429 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3430}
3431
de93309a
SM
3432/* Returns true (non-zero) iff decoded name N0 should appear before N1
3433 in a listing of choices during disambiguation (see sort_choices, below).
3434 The idea is that overloadings of a subprogram name from the
3435 same package should sort in their source order. We settle for ordering
3436 such symbols by their trailing number (__N or $N). */
14f9c5c9 3437
de93309a
SM
3438static int
3439encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3440{
de93309a
SM
3441 if (N1 == NULL)
3442 return 0;
3443 else if (N0 == NULL)
3444 return 1;
3445 else
3446 {
3447 int k0, k1;
30b15541 3448
de93309a 3449 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3450 ;
de93309a 3451 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3452 ;
de93309a 3453 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3454 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3455 {
3456 int n0, n1;
3457
3458 n0 = k0;
3459 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3460 n0 -= 1;
3461 n1 = k1;
3462 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3463 n1 -= 1;
3464 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3465 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3466 }
de93309a
SM
3467 return (strcmp (N0, N1) < 0);
3468 }
14f9c5c9
AS
3469}
3470
de93309a
SM
3471/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3472 encoded names. */
14f9c5c9 3473
de93309a
SM
3474static void
3475sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3476{
14f9c5c9 3477 int i;
14f9c5c9 3478
de93309a 3479 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3480 {
de93309a
SM
3481 struct block_symbol sym = syms[i];
3482 int j;
3483
3484 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3485 {
3486 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3487 sym.symbol->linkage_name ()))
3488 break;
3489 syms[j + 1] = syms[j];
3490 }
de93309a
SM
3491 syms[j + 1] = sym;
3492 }
3493}
14f9c5c9 3494
de93309a
SM
3495/* Whether GDB should display formals and return types for functions in the
3496 overloads selection menu. */
3497static bool print_signatures = true;
4c4b4cd2 3498
de93309a
SM
3499/* Print the signature for SYM on STREAM according to the FLAGS options. For
3500 all but functions, the signature is just the name of the symbol. For
3501 functions, this is the name of the function, the list of types for formals
3502 and the return type (if any). */
4c4b4cd2 3503
de93309a
SM
3504static void
3505ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3506 const struct type_print_options *flags)
3507{
5f9c5a63 3508 struct type *type = sym->type ();
14f9c5c9 3509
6cb06a8c 3510 gdb_printf (stream, "%s", sym->print_name ());
de93309a
SM
3511 if (!print_signatures
3512 || type == NULL
78134374 3513 || type->code () != TYPE_CODE_FUNC)
de93309a 3514 return;
4c4b4cd2 3515
1f704f76 3516 if (type->num_fields () > 0)
de93309a
SM
3517 {
3518 int i;
14f9c5c9 3519
6cb06a8c 3520 gdb_printf (stream, " (");
1f704f76 3521 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3522 {
3523 if (i > 0)
6cb06a8c 3524 gdb_printf (stream, "; ");
940da03e 3525 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3526 flags);
3527 }
6cb06a8c 3528 gdb_printf (stream, ")");
de93309a 3529 }
27710edb
SM
3530 if (type->target_type () != NULL
3531 && type->target_type ()->code () != TYPE_CODE_VOID)
de93309a 3532 {
6cb06a8c 3533 gdb_printf (stream, " return ");
27710edb 3534 ada_print_type (type->target_type (), NULL, stream, -1, 0, flags);
de93309a
SM
3535 }
3536}
14f9c5c9 3537
de93309a
SM
3538/* Read and validate a set of numeric choices from the user in the
3539 range 0 .. N_CHOICES-1. Place the results in increasing
3540 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3541
de93309a
SM
3542 The user types choices as a sequence of numbers on one line
3543 separated by blanks, encoding them as follows:
14f9c5c9 3544
de93309a
SM
3545 + A choice of 0 means to cancel the selection, throwing an error.
3546 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3547 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3548
de93309a 3549 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3550
de93309a
SM
3551 ANNOTATION_SUFFIX, if present, is used to annotate the input
3552 prompts (for use with the -f switch). */
14f9c5c9 3553
de93309a
SM
3554static int
3555get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3556 int is_all_choice, const char *annotation_suffix)
de93309a 3557{
992a7040 3558 const char *args;
de93309a
SM
3559 const char *prompt;
3560 int n_chosen;
3561 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3562
de93309a
SM
3563 prompt = getenv ("PS2");
3564 if (prompt == NULL)
3565 prompt = "> ";
4c4b4cd2 3566
f8631e5e
SM
3567 std::string buffer;
3568 args = command_line_input (buffer, prompt, annotation_suffix);
4c4b4cd2 3569
de93309a
SM
3570 if (args == NULL)
3571 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3572
de93309a 3573 n_chosen = 0;
4c4b4cd2 3574
de93309a
SM
3575 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3576 order, as given in args. Choices are validated. */
3577 while (1)
14f9c5c9 3578 {
de93309a
SM
3579 char *args2;
3580 int choice, j;
76a01679 3581
de93309a
SM
3582 args = skip_spaces (args);
3583 if (*args == '\0' && n_chosen == 0)
dda83cd7 3584 error_no_arg (_("one or more choice numbers"));
de93309a 3585 else if (*args == '\0')
dda83cd7 3586 break;
76a01679 3587
de93309a
SM
3588 choice = strtol (args, &args2, 10);
3589 if (args == args2 || choice < 0
dda83cd7
SM
3590 || choice > n_choices + first_choice - 1)
3591 error (_("Argument must be choice number"));
de93309a 3592 args = args2;
76a01679 3593
de93309a 3594 if (choice == 0)
dda83cd7 3595 error (_("cancelled"));
76a01679 3596
de93309a 3597 if (choice < first_choice)
dda83cd7
SM
3598 {
3599 n_chosen = n_choices;
3600 for (j = 0; j < n_choices; j += 1)
3601 choices[j] = j;
3602 break;
3603 }
de93309a 3604 choice -= first_choice;
76a01679 3605
de93309a 3606 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3607 {
3608 }
4c4b4cd2 3609
de93309a 3610 if (j < 0 || choice != choices[j])
dda83cd7
SM
3611 {
3612 int k;
4c4b4cd2 3613
dda83cd7
SM
3614 for (k = n_chosen - 1; k > j; k -= 1)
3615 choices[k + 1] = choices[k];
3616 choices[j + 1] = choice;
3617 n_chosen += 1;
3618 }
14f9c5c9
AS
3619 }
3620
de93309a
SM
3621 if (n_chosen > max_results)
3622 error (_("Select no more than %d of the above"), max_results);
3623
3624 return n_chosen;
14f9c5c9
AS
3625}
3626
de93309a
SM
3627/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3628 by asking the user (if necessary), returning the number selected,
3629 and setting the first elements of SYMS items. Error if no symbols
3630 selected. */
3631
3632/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3633 to be re-integrated one of these days. */
14f9c5c9
AS
3634
3635static int
de93309a 3636user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3637{
de93309a
SM
3638 int i;
3639 int *chosen = XALLOCAVEC (int , nsyms);
3640 int n_chosen;
3641 int first_choice = (max_results == 1) ? 1 : 2;
3642 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3643
de93309a
SM
3644 if (max_results < 1)
3645 error (_("Request to select 0 symbols!"));
3646 if (nsyms <= 1)
3647 return nsyms;
14f9c5c9 3648
de93309a
SM
3649 if (select_mode == multiple_symbols_cancel)
3650 error (_("\
3651canceled because the command is ambiguous\n\
3652See set/show multiple-symbol."));
14f9c5c9 3653
de93309a
SM
3654 /* If select_mode is "all", then return all possible symbols.
3655 Only do that if more than one symbol can be selected, of course.
3656 Otherwise, display the menu as usual. */
3657 if (select_mode == multiple_symbols_all && max_results > 1)
3658 return nsyms;
14f9c5c9 3659
6cb06a8c 3660 gdb_printf (_("[0] cancel\n"));
de93309a 3661 if (max_results > 1)
6cb06a8c 3662 gdb_printf (_("[1] all\n"));
14f9c5c9 3663
de93309a 3664 sort_choices (syms, nsyms);
14f9c5c9 3665
de93309a
SM
3666 for (i = 0; i < nsyms; i += 1)
3667 {
3668 if (syms[i].symbol == NULL)
dda83cd7 3669 continue;
14f9c5c9 3670
66d7f48f 3671 if (syms[i].symbol->aclass () == LOC_BLOCK)
dda83cd7
SM
3672 {
3673 struct symtab_and_line sal =
3674 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3675
6cb06a8c 3676 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3677 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3678 &type_print_raw_options);
3679 if (sal.symtab == NULL)
6cb06a8c
TT
3680 gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
3681 metadata_style.style ().ptr (), nullptr, sal.line);
de93309a 3682 else
6cb06a8c 3683 gdb_printf
de93309a
SM
3684 (_(" at %ps:%d\n"),
3685 styled_string (file_name_style.style (),
3686 symtab_to_filename_for_display (sal.symtab)),
3687 sal.line);
dda83cd7
SM
3688 continue;
3689 }
76a01679 3690 else
dda83cd7
SM
3691 {
3692 int is_enumeral =
66d7f48f 3693 (syms[i].symbol->aclass () == LOC_CONST
5f9c5a63
SM
3694 && syms[i].symbol->type () != NULL
3695 && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
de93309a 3696 struct symtab *symtab = NULL;
4c4b4cd2 3697
7b3ecc75 3698 if (syms[i].symbol->is_objfile_owned ())
4206d69e 3699 symtab = syms[i].symbol->symtab ();
de93309a 3700
5d0027b9 3701 if (syms[i].symbol->line () != 0 && symtab != NULL)
de93309a 3702 {
6cb06a8c 3703 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3704 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3705 &type_print_raw_options);
6cb06a8c
TT
3706 gdb_printf (_(" at %s:%d\n"),
3707 symtab_to_filename_for_display (symtab),
3708 syms[i].symbol->line ());
de93309a 3709 }
dda83cd7 3710 else if (is_enumeral
5f9c5a63 3711 && syms[i].symbol->type ()->name () != NULL)
dda83cd7 3712 {
6cb06a8c 3713 gdb_printf (("[%d] "), i + first_choice);
5f9c5a63 3714 ada_print_type (syms[i].symbol->type (), NULL,
dda83cd7 3715 gdb_stdout, -1, 0, &type_print_raw_options);
6cb06a8c
TT
3716 gdb_printf (_("'(%s) (enumeral)\n"),
3717 syms[i].symbol->print_name ());
dda83cd7 3718 }
de93309a
SM
3719 else
3720 {
6cb06a8c 3721 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3722 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3723 &type_print_raw_options);
3724
3725 if (symtab != NULL)
6cb06a8c
TT
3726 gdb_printf (is_enumeral
3727 ? _(" in %s (enumeral)\n")
3728 : _(" at %s:?\n"),
3729 symtab_to_filename_for_display (symtab));
de93309a 3730 else
6cb06a8c
TT
3731 gdb_printf (is_enumeral
3732 ? _(" (enumeral)\n")
3733 : _(" at ?\n"));
de93309a 3734 }
dda83cd7 3735 }
14f9c5c9 3736 }
14f9c5c9 3737
de93309a 3738 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3739 "overload-choice");
14f9c5c9 3740
de93309a
SM
3741 for (i = 0; i < n_chosen; i += 1)
3742 syms[i] = syms[chosen[i]];
14f9c5c9 3743
de93309a
SM
3744 return n_chosen;
3745}
14f9c5c9 3746
cd9a3148
TT
3747/* See ada-lang.h. */
3748
3749block_symbol
7056f312 3750ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3751 int nargs, value *argvec[])
3752{
3753 if (possible_user_operator_p (op, argvec))
3754 {
3755 std::vector<struct block_symbol> candidates
3756 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3757 NULL, VAR_DOMAIN);
3758
3759 int i = ada_resolve_function (candidates, argvec,
3760 nargs, ada_decoded_op_name (op), NULL,
3761 parse_completion);
3762 if (i >= 0)
3763 return candidates[i];
3764 }
3765 return {};
3766}
3767
3768/* See ada-lang.h. */
3769
3770block_symbol
3771ada_resolve_funcall (struct symbol *sym, const struct block *block,
3772 struct type *context_type,
7056f312 3773 bool parse_completion,
cd9a3148
TT
3774 int nargs, value *argvec[],
3775 innermost_block_tracker *tracker)
3776{
3777 std::vector<struct block_symbol> candidates
3778 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3779
3780 int i;
3781 if (candidates.size () == 1)
3782 i = 0;
3783 else
3784 {
3785 i = ada_resolve_function
3786 (candidates,
3787 argvec, nargs,
3788 sym->linkage_name (),
3789 context_type, parse_completion);
3790 if (i < 0)
3791 error (_("Could not find a match for %s"), sym->print_name ());
3792 }
3793
3794 tracker->update (candidates[i]);
3795 return candidates[i];
3796}
3797
ba8694b6
TT
3798/* Resolve a mention of a name where the context type is an
3799 enumeration type. */
3800
3801static int
3802ada_resolve_enum (std::vector<struct block_symbol> &syms,
3803 const char *name, struct type *context_type,
3804 bool parse_completion)
3805{
3806 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3807 context_type = ada_check_typedef (context_type);
3808
3809 for (int i = 0; i < syms.size (); ++i)
3810 {
3811 /* We already know the name matches, so we're just looking for
3812 an element of the correct enum type. */
5f9c5a63 3813 if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
ba8694b6
TT
3814 return i;
3815 }
3816
3817 error (_("No name '%s' in enumeration type '%s'"), name,
3818 ada_type_name (context_type));
3819}
3820
cd9a3148
TT
3821/* See ada-lang.h. */
3822
3823block_symbol
3824ada_resolve_variable (struct symbol *sym, const struct block *block,
3825 struct type *context_type,
7056f312 3826 bool parse_completion,
cd9a3148
TT
3827 int deprocedure_p,
3828 innermost_block_tracker *tracker)
3829{
3830 std::vector<struct block_symbol> candidates
3831 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3832
3833 if (std::any_of (candidates.begin (),
3834 candidates.end (),
3835 [] (block_symbol &bsym)
3836 {
66d7f48f 3837 switch (bsym.symbol->aclass ())
cd9a3148
TT
3838 {
3839 case LOC_REGISTER:
3840 case LOC_ARG:
3841 case LOC_REF_ARG:
3842 case LOC_REGPARM_ADDR:
3843 case LOC_LOCAL:
3844 case LOC_COMPUTED:
3845 return true;
3846 default:
3847 return false;
3848 }
3849 }))
3850 {
3851 /* Types tend to get re-introduced locally, so if there
3852 are any local symbols that are not types, first filter
3853 out all types. */
3854 candidates.erase
3855 (std::remove_if
3856 (candidates.begin (),
3857 candidates.end (),
3858 [] (block_symbol &bsym)
3859 {
66d7f48f 3860 return bsym.symbol->aclass () == LOC_TYPEDEF;
cd9a3148
TT
3861 }),
3862 candidates.end ());
3863 }
3864
2c71f639
TV
3865 /* Filter out artificial symbols. */
3866 candidates.erase
3867 (std::remove_if
3868 (candidates.begin (),
3869 candidates.end (),
3870 [] (block_symbol &bsym)
3871 {
496feb16 3872 return bsym.symbol->is_artificial ();
2c71f639
TV
3873 }),
3874 candidates.end ());
3875
cd9a3148
TT
3876 int i;
3877 if (candidates.empty ())
3878 error (_("No definition found for %s"), sym->print_name ());
3879 else if (candidates.size () == 1)
3880 i = 0;
ba8694b6
TT
3881 else if (context_type != nullptr
3882 && context_type->code () == TYPE_CODE_ENUM)
3883 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3884 parse_completion);
cd9a3148
TT
3885 else if (deprocedure_p && !is_nonfunction (candidates))
3886 {
3887 i = ada_resolve_function
3888 (candidates, NULL, 0,
3889 sym->linkage_name (),
3890 context_type, parse_completion);
3891 if (i < 0)
3892 error (_("Could not find a match for %s"), sym->print_name ());
3893 }
3894 else
3895 {
6cb06a8c 3896 gdb_printf (_("Multiple matches for %s\n"), sym->print_name ());
cd9a3148
TT
3897 user_select_syms (candidates.data (), candidates.size (), 1);
3898 i = 0;
3899 }
3900
3901 tracker->update (candidates[i]);
3902 return candidates[i];
3903}
3904
db2534b7 3905/* Return non-zero if formal type FTYPE matches actual type ATYPE. */
de93309a
SM
3906/* The term "match" here is rather loose. The match is heuristic and
3907 liberal. */
14f9c5c9 3908
de93309a 3909static int
db2534b7 3910ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3911{
de93309a
SM
3912 ftype = ada_check_typedef (ftype);
3913 atype = ada_check_typedef (atype);
14f9c5c9 3914
78134374 3915 if (ftype->code () == TYPE_CODE_REF)
27710edb 3916 ftype = ftype->target_type ();
78134374 3917 if (atype->code () == TYPE_CODE_REF)
27710edb 3918 atype = atype->target_type ();
14f9c5c9 3919
78134374 3920 switch (ftype->code ())
14f9c5c9 3921 {
de93309a 3922 default:
78134374 3923 return ftype->code () == atype->code ();
de93309a 3924 case TYPE_CODE_PTR:
db2534b7
TT
3925 if (atype->code () != TYPE_CODE_PTR)
3926 return 0;
27710edb 3927 atype = atype->target_type ();
db2534b7 3928 /* This can only happen if the actual argument is 'null'. */
df86565b 3929 if (atype->code () == TYPE_CODE_INT && atype->length () == 0)
db2534b7 3930 return 1;
27710edb 3931 return ada_type_match (ftype->target_type (), atype);
de93309a
SM
3932 case TYPE_CODE_INT:
3933 case TYPE_CODE_ENUM:
3934 case TYPE_CODE_RANGE:
78134374 3935 switch (atype->code ())
dda83cd7
SM
3936 {
3937 case TYPE_CODE_INT:
3938 case TYPE_CODE_ENUM:
3939 case TYPE_CODE_RANGE:
3940 return 1;
3941 default:
3942 return 0;
3943 }
d2e4a39e 3944
de93309a 3945 case TYPE_CODE_ARRAY:
78134374 3946 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3947 || ada_is_array_descriptor_type (atype));
14f9c5c9 3948
de93309a
SM
3949 case TYPE_CODE_STRUCT:
3950 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3951 return (atype->code () == TYPE_CODE_ARRAY
3952 || ada_is_array_descriptor_type (atype));
de93309a 3953 else
dda83cd7
SM
3954 return (atype->code () == TYPE_CODE_STRUCT
3955 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3956
de93309a
SM
3957 case TYPE_CODE_UNION:
3958 case TYPE_CODE_FLT:
78134374 3959 return (atype->code () == ftype->code ());
de93309a 3960 }
14f9c5c9
AS
3961}
3962
de93309a
SM
3963/* Return non-zero if the formals of FUNC "sufficiently match" the
3964 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3965 may also be an enumeral, in which case it is treated as a 0-
3966 argument function. */
14f9c5c9 3967
de93309a
SM
3968static int
3969ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3970{
3971 int i;
5f9c5a63 3972 struct type *func_type = func->type ();
14f9c5c9 3973
66d7f48f 3974 if (func->aclass () == LOC_CONST
78134374 3975 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3976 return (n_actuals == 0);
78134374 3977 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3978 return 0;
14f9c5c9 3979
1f704f76 3980 if (func_type->num_fields () != n_actuals)
de93309a 3981 return 0;
14f9c5c9 3982
de93309a
SM
3983 for (i = 0; i < n_actuals; i += 1)
3984 {
3985 if (actuals[i] == NULL)
dda83cd7 3986 return 0;
de93309a 3987 else
dda83cd7
SM
3988 {
3989 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
d0c97917 3990 struct type *atype = ada_check_typedef (actuals[i]->type ());
14f9c5c9 3991
db2534b7 3992 if (!ada_type_match (ftype, atype))
dda83cd7
SM
3993 return 0;
3994 }
de93309a
SM
3995 }
3996 return 1;
3997}
d2e4a39e 3998
de93309a
SM
3999/* False iff function type FUNC_TYPE definitely does not produce a value
4000 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
4001 FUNC_TYPE is not a valid function type with a non-null return type
4002 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 4003
de93309a
SM
4004static int
4005return_match (struct type *func_type, struct type *context_type)
4006{
4007 struct type *return_type;
d2e4a39e 4008
de93309a
SM
4009 if (func_type == NULL)
4010 return 1;
14f9c5c9 4011
78134374 4012 if (func_type->code () == TYPE_CODE_FUNC)
27710edb 4013 return_type = get_base_type (func_type->target_type ());
de93309a
SM
4014 else
4015 return_type = get_base_type (func_type);
4016 if (return_type == NULL)
4017 return 1;
76a01679 4018
de93309a 4019 context_type = get_base_type (context_type);
14f9c5c9 4020
78134374 4021 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
4022 return context_type == NULL || return_type == context_type;
4023 else if (context_type == NULL)
78134374 4024 return return_type->code () != TYPE_CODE_VOID;
de93309a 4025 else
78134374 4026 return return_type->code () == context_type->code ();
de93309a 4027}
14f9c5c9 4028
14f9c5c9 4029
1bfa81ac 4030/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
4031 function (if any) that matches the types of the NARGS arguments in
4032 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4033 that returns that type, then eliminate matches that don't. If
4034 CONTEXT_TYPE is void and there is at least one match that does not
4035 return void, eliminate all matches that do.
14f9c5c9 4036
de93309a
SM
4037 Asks the user if there is more than one match remaining. Returns -1
4038 if there is no such symbol or none is selected. NAME is used
4039 solely for messages. May re-arrange and modify SYMS in
4040 the process; the index returned is for the modified vector. */
14f9c5c9 4041
de93309a 4042static int
d1183b06
TT
4043ada_resolve_function (std::vector<struct block_symbol> &syms,
4044 struct value **args, int nargs,
dda83cd7 4045 const char *name, struct type *context_type,
7056f312 4046 bool parse_completion)
de93309a
SM
4047{
4048 int fallback;
4049 int k;
4050 int m; /* Number of hits */
14f9c5c9 4051
de93309a
SM
4052 m = 0;
4053 /* In the first pass of the loop, we only accept functions matching
4054 context_type. If none are found, we add a second pass of the loop
4055 where every function is accepted. */
4056 for (fallback = 0; m == 0 && fallback < 2; fallback++)
4057 {
d1183b06 4058 for (k = 0; k < syms.size (); k += 1)
dda83cd7 4059 {
5f9c5a63 4060 struct type *type = ada_check_typedef (syms[k].symbol->type ());
5b4ee69b 4061
dda83cd7
SM
4062 if (ada_args_match (syms[k].symbol, args, nargs)
4063 && (fallback || return_match (type, context_type)))
4064 {
4065 syms[m] = syms[k];
4066 m += 1;
4067 }
4068 }
14f9c5c9
AS
4069 }
4070
de93309a
SM
4071 /* If we got multiple matches, ask the user which one to use. Don't do this
4072 interactive thing during completion, though, as the purpose of the
4073 completion is providing a list of all possible matches. Prompting the
4074 user to filter it down would be completely unexpected in this case. */
4075 if (m == 0)
4076 return -1;
4077 else if (m > 1 && !parse_completion)
4078 {
6cb06a8c 4079 gdb_printf (_("Multiple matches for %s\n"), name);
d1183b06 4080 user_select_syms (syms.data (), m, 1);
de93309a
SM
4081 return 0;
4082 }
4083 return 0;
14f9c5c9
AS
4084}
4085
14f9c5c9
AS
4086/* Type-class predicates */
4087
4c4b4cd2
PH
4088/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4089 or FLOAT). */
14f9c5c9
AS
4090
4091static int
d2e4a39e 4092numeric_type_p (struct type *type)
14f9c5c9
AS
4093{
4094 if (type == NULL)
4095 return 0;
d2e4a39e
AS
4096 else
4097 {
78134374 4098 switch (type->code ())
dda83cd7
SM
4099 {
4100 case TYPE_CODE_INT:
4101 case TYPE_CODE_FLT:
c04da66c 4102 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4103 return 1;
4104 case TYPE_CODE_RANGE:
27710edb
SM
4105 return (type == type->target_type ()
4106 || numeric_type_p (type->target_type ()));
dda83cd7
SM
4107 default:
4108 return 0;
4109 }
d2e4a39e 4110 }
14f9c5c9
AS
4111}
4112
4c4b4cd2 4113/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4114
4115static int
d2e4a39e 4116integer_type_p (struct type *type)
14f9c5c9
AS
4117{
4118 if (type == NULL)
4119 return 0;
d2e4a39e
AS
4120 else
4121 {
78134374 4122 switch (type->code ())
dda83cd7
SM
4123 {
4124 case TYPE_CODE_INT:
4125 return 1;
4126 case TYPE_CODE_RANGE:
27710edb
SM
4127 return (type == type->target_type ()
4128 || integer_type_p (type->target_type ()));
dda83cd7
SM
4129 default:
4130 return 0;
4131 }
d2e4a39e 4132 }
14f9c5c9
AS
4133}
4134
4c4b4cd2 4135/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4136
4137static int
d2e4a39e 4138scalar_type_p (struct type *type)
14f9c5c9
AS
4139{
4140 if (type == NULL)
4141 return 0;
d2e4a39e
AS
4142 else
4143 {
78134374 4144 switch (type->code ())
dda83cd7
SM
4145 {
4146 case TYPE_CODE_INT:
4147 case TYPE_CODE_RANGE:
4148 case TYPE_CODE_ENUM:
4149 case TYPE_CODE_FLT:
c04da66c 4150 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4151 return 1;
4152 default:
4153 return 0;
4154 }
d2e4a39e 4155 }
14f9c5c9
AS
4156}
4157
98847c1e
TT
4158/* True iff TYPE is discrete, as defined in the Ada Reference Manual.
4159 This essentially means one of (INT, RANGE, ENUM) -- but note that
4160 "enum" includes character and boolean as well. */
14f9c5c9
AS
4161
4162static int
d2e4a39e 4163discrete_type_p (struct type *type)
14f9c5c9
AS
4164{
4165 if (type == NULL)
4166 return 0;
d2e4a39e
AS
4167 else
4168 {
78134374 4169 switch (type->code ())
dda83cd7
SM
4170 {
4171 case TYPE_CODE_INT:
4172 case TYPE_CODE_RANGE:
4173 case TYPE_CODE_ENUM:
4174 case TYPE_CODE_BOOL:
98847c1e 4175 case TYPE_CODE_CHAR:
dda83cd7
SM
4176 return 1;
4177 default:
4178 return 0;
4179 }
d2e4a39e 4180 }
14f9c5c9
AS
4181}
4182
4c4b4cd2
PH
4183/* Returns non-zero if OP with operands in the vector ARGS could be
4184 a user-defined function. Errs on the side of pre-defined operators
4185 (i.e., result 0). */
14f9c5c9
AS
4186
4187static int
d2e4a39e 4188possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4189{
76a01679 4190 struct type *type0 =
d0c97917 4191 (args[0] == NULL) ? NULL : ada_check_typedef (args[0]->type ());
d2e4a39e 4192 struct type *type1 =
d0c97917 4193 (args[1] == NULL) ? NULL : ada_check_typedef (args[1]->type ());
d2e4a39e 4194
4c4b4cd2
PH
4195 if (type0 == NULL)
4196 return 0;
4197
14f9c5c9
AS
4198 switch (op)
4199 {
4200 default:
4201 return 0;
4202
4203 case BINOP_ADD:
4204 case BINOP_SUB:
4205 case BINOP_MUL:
4206 case BINOP_DIV:
d2e4a39e 4207 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4208
4209 case BINOP_REM:
4210 case BINOP_MOD:
4211 case BINOP_BITWISE_AND:
4212 case BINOP_BITWISE_IOR:
4213 case BINOP_BITWISE_XOR:
d2e4a39e 4214 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4215
4216 case BINOP_EQUAL:
4217 case BINOP_NOTEQUAL:
4218 case BINOP_LESS:
4219 case BINOP_GTR:
4220 case BINOP_LEQ:
4221 case BINOP_GEQ:
d2e4a39e 4222 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4223
4224 case BINOP_CONCAT:
ee90b9ab 4225 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4226
4227 case BINOP_EXP:
d2e4a39e 4228 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4229
4230 case UNOP_NEG:
4231 case UNOP_PLUS:
4232 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4233 case UNOP_ABS:
4234 return (!numeric_type_p (type0));
14f9c5c9
AS
4235
4236 }
4237}
4238\f
dda83cd7 4239 /* Renaming */
14f9c5c9 4240
aeb5907d
JB
4241/* NOTES:
4242
4243 1. In the following, we assume that a renaming type's name may
4244 have an ___XD suffix. It would be nice if this went away at some
4245 point.
4246 2. We handle both the (old) purely type-based representation of
4247 renamings and the (new) variable-based encoding. At some point,
4248 it is devoutly to be hoped that the former goes away
4249 (FIXME: hilfinger-2007-07-09).
4250 3. Subprogram renamings are not implemented, although the XRS
4251 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4252
4253/* If SYM encodes a renaming,
4254
4255 <renaming> renames <renamed entity>,
4256
4257 sets *LEN to the length of the renamed entity's name,
4258 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4259 the string describing the subcomponent selected from the renamed
0963b4bd 4260 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4261 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4262 are undefined). Otherwise, returns a value indicating the category
4263 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4264 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4265 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4266 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4267 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4268 may be NULL, in which case they are not assigned.
4269
4270 [Currently, however, GCC does not generate subprogram renamings.] */
4271
4272enum ada_renaming_category
4273ada_parse_renaming (struct symbol *sym,
4274 const char **renamed_entity, int *len,
4275 const char **renaming_expr)
4276{
4277 enum ada_renaming_category kind;
4278 const char *info;
4279 const char *suffix;
4280
4281 if (sym == NULL)
4282 return ADA_NOT_RENAMING;
66d7f48f 4283 switch (sym->aclass ())
14f9c5c9 4284 {
aeb5907d
JB
4285 default:
4286 return ADA_NOT_RENAMING;
aeb5907d
JB
4287 case LOC_LOCAL:
4288 case LOC_STATIC:
4289 case LOC_COMPUTED:
4290 case LOC_OPTIMIZED_OUT:
987012b8 4291 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4292 if (info == NULL)
4293 return ADA_NOT_RENAMING;
4294 switch (info[5])
4295 {
4296 case '_':
4297 kind = ADA_OBJECT_RENAMING;
4298 info += 6;
4299 break;
4300 case 'E':
4301 kind = ADA_EXCEPTION_RENAMING;
4302 info += 7;
4303 break;
4304 case 'P':
4305 kind = ADA_PACKAGE_RENAMING;
4306 info += 7;
4307 break;
4308 case 'S':
4309 kind = ADA_SUBPROGRAM_RENAMING;
4310 info += 7;
4311 break;
4312 default:
4313 return ADA_NOT_RENAMING;
4314 }
14f9c5c9 4315 }
4c4b4cd2 4316
de93309a
SM
4317 if (renamed_entity != NULL)
4318 *renamed_entity = info;
4319 suffix = strstr (info, "___XE");
4320 if (suffix == NULL || suffix == info)
4321 return ADA_NOT_RENAMING;
4322 if (len != NULL)
4323 *len = strlen (info) - strlen (suffix);
4324 suffix += 5;
4325 if (renaming_expr != NULL)
4326 *renaming_expr = suffix;
4327 return kind;
4328}
4329
4330/* Compute the value of the given RENAMING_SYM, which is expected to
4331 be a symbol encoding a renaming expression. BLOCK is the block
4332 used to evaluate the renaming. */
4333
4334static struct value *
4335ada_read_renaming_var_value (struct symbol *renaming_sym,
4336 const struct block *block)
4337{
4338 const char *sym_name;
4339
987012b8 4340 sym_name = renaming_sym->linkage_name ();
de93309a 4341 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
43048e46 4342 return expr->evaluate ();
de93309a
SM
4343}
4344\f
4345
dda83cd7 4346 /* Evaluation: Function Calls */
de93309a
SM
4347
4348/* Return an lvalue containing the value VAL. This is the identity on
4349 lvalues, and otherwise has the side-effect of allocating memory
4350 in the inferior where a copy of the value contents is copied. */
4351
4352static struct value *
4353ensure_lval (struct value *val)
4354{
736355f2
TT
4355 if (val->lval () == not_lval
4356 || val->lval () == lval_internalvar)
de93309a 4357 {
d0c97917 4358 int len = ada_check_typedef (val->type ())->length ();
de93309a 4359 const CORE_ADDR addr =
dda83cd7 4360 value_as_long (value_allocate_space_in_inferior (len));
de93309a 4361
6f9c9d71 4362 val->set_lval (lval_memory);
9feb2d07 4363 val->set_address (addr);
efaf1ae0 4364 write_memory (addr, val->contents ().data (), len);
de93309a
SM
4365 }
4366
4367 return val;
4368}
4369
4370/* Given ARG, a value of type (pointer or reference to a)*
4371 structure/union, extract the component named NAME from the ultimate
4372 target structure/union and return it as a value with its
4373 appropriate type.
4374
4375 The routine searches for NAME among all members of the structure itself
4376 and (recursively) among all members of any wrapper members
4377 (e.g., '_parent').
4378
4379 If NO_ERR, then simply return NULL in case of error, rather than
4380 calling error. */
4381
4382static struct value *
4383ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4384{
4385 struct type *t, *t1;
4386 struct value *v;
4387 int check_tag;
4388
4389 v = NULL;
d0c97917 4390 t1 = t = ada_check_typedef (arg->type ());
78134374 4391 if (t->code () == TYPE_CODE_REF)
de93309a 4392 {
27710edb 4393 t1 = t->target_type ();
de93309a
SM
4394 if (t1 == NULL)
4395 goto BadValue;
4396 t1 = ada_check_typedef (t1);
78134374 4397 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4398 {
4399 arg = coerce_ref (arg);
4400 t = t1;
4401 }
de93309a
SM
4402 }
4403
78134374 4404 while (t->code () == TYPE_CODE_PTR)
de93309a 4405 {
27710edb 4406 t1 = t->target_type ();
de93309a
SM
4407 if (t1 == NULL)
4408 goto BadValue;
4409 t1 = ada_check_typedef (t1);
78134374 4410 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4411 {
4412 arg = value_ind (arg);
4413 t = t1;
4414 }
de93309a 4415 else
dda83cd7 4416 break;
de93309a 4417 }
aeb5907d 4418
78134374 4419 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4420 goto BadValue;
52ce6436 4421
de93309a
SM
4422 if (t1 == t)
4423 v = ada_search_struct_field (name, arg, 0, t);
4424 else
4425 {
4426 int bit_offset, bit_size, byte_offset;
4427 struct type *field_type;
4428 CORE_ADDR address;
a5ee536b 4429
78134374 4430 if (t->code () == TYPE_CODE_PTR)
9feb2d07 4431 address = ada_value_ind (arg)->address ();
de93309a 4432 else
9feb2d07 4433 address = ada_coerce_ref (arg)->address ();
d2e4a39e 4434
de93309a 4435 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4436 the case where the type is a reference to a tagged type, but
4437 we have to be careful to exclude pointers to tagged types.
4438 The latter should be shown as usual (as a pointer), whereas
4439 a reference should mostly be transparent to the user. */
14f9c5c9 4440
de93309a 4441 if (ada_is_tagged_type (t1, 0)
dda83cd7 4442 || (t1->code () == TYPE_CODE_REF
27710edb 4443 && ada_is_tagged_type (t1->target_type (), 0)))
dda83cd7
SM
4444 {
4445 /* We first try to find the searched field in the current type.
de93309a 4446 If not found then let's look in the fixed type. */
14f9c5c9 4447
dda83cd7 4448 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4449 nullptr, nullptr, nullptr,
4450 nullptr, nullptr))
de93309a
SM
4451 check_tag = 1;
4452 else
4453 check_tag = 0;
dda83cd7 4454 }
de93309a
SM
4455 else
4456 check_tag = 0;
c3e5cd34 4457
de93309a
SM
4458 /* Convert to fixed type in all cases, so that we have proper
4459 offsets to each field in unconstrained record types. */
4460 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4461 address, NULL, check_tag);
4462
24aa1b02
TT
4463 /* Resolve the dynamic type as well. */
4464 arg = value_from_contents_and_address (t1, nullptr, address);
d0c97917 4465 t1 = arg->type ();
24aa1b02 4466
de93309a 4467 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4468 &field_type, &byte_offset, &bit_offset,
4469 &bit_size, NULL))
4470 {
4471 if (bit_size != 0)
4472 {
4473 if (t->code () == TYPE_CODE_REF)
4474 arg = ada_coerce_ref (arg);
4475 else
4476 arg = ada_value_ind (arg);
4477 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4478 bit_offset, bit_size,
4479 field_type);
4480 }
4481 else
4482 v = value_at_lazy (field_type, address + byte_offset);
4483 }
c3e5cd34 4484 }
14f9c5c9 4485
de93309a
SM
4486 if (v != NULL || no_err)
4487 return v;
4488 else
4489 error (_("There is no member named %s."), name);
4490
4491 BadValue:
4492 if (no_err)
4493 return NULL;
4494 else
4495 error (_("Attempt to extract a component of "
4496 "a value that is not a record."));
14f9c5c9
AS
4497}
4498
4499/* Return the value ACTUAL, converted to be an appropriate value for a
4500 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4501 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4502 values not residing in memory, updating it as needed. */
14f9c5c9 4503
a93c0eb6 4504struct value *
40bc484c 4505ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4506{
d0c97917 4507 struct type *actual_type = ada_check_typedef (actual->type ());
61ee279c 4508 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4509 struct type *formal_target =
78134374 4510 formal_type->code () == TYPE_CODE_PTR
27710edb 4511 ? ada_check_typedef (formal_type->target_type ()) : formal_type;
d2e4a39e 4512 struct type *actual_target =
78134374 4513 actual_type->code () == TYPE_CODE_PTR
27710edb 4514 ? ada_check_typedef (actual_type->target_type ()) : actual_type;
14f9c5c9 4515
4c4b4cd2 4516 if (ada_is_array_descriptor_type (formal_target)
78134374 4517 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4518 return make_array_descriptor (formal_type, actual);
78134374
SM
4519 else if (formal_type->code () == TYPE_CODE_PTR
4520 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4521 {
a84a8a0d 4522 struct value *result;
5b4ee69b 4523
78134374 4524 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4525 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4526 result = desc_data (actual);
78134374 4527 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7 4528 {
736355f2 4529 if (actual->lval () != lval_memory)
dda83cd7
SM
4530 {
4531 struct value *val;
4532
d0c97917 4533 actual_type = ada_check_typedef (actual->type ());
317c3ed9 4534 val = value::allocate (actual_type);
efaf1ae0 4535 copy (actual->contents (), val->contents_raw ());
dda83cd7
SM
4536 actual = ensure_lval (val);
4537 }
4538 result = value_addr (actual);
4539 }
a84a8a0d
JB
4540 else
4541 return actual;
b1af9e97 4542 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4543 }
78134374 4544 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4545 return ada_value_ind (actual);
8344af1e
JB
4546 else if (ada_is_aligner_type (formal_type))
4547 {
4548 /* We need to turn this parameter into an aligner type
4549 as well. */
317c3ed9 4550 struct value *aligner = value::allocate (formal_type);
8344af1e
JB
4551 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4552
4553 value_assign_to_component (aligner, component, actual);
4554 return aligner;
4555 }
14f9c5c9
AS
4556
4557 return actual;
4558}
4559
438c98a1
JB
4560/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4561 type TYPE. This is usually an inefficient no-op except on some targets
4562 (such as AVR) where the representation of a pointer and an address
4563 differs. */
4564
4565static CORE_ADDR
4566value_pointer (struct value *value, struct type *type)
4567{
df86565b 4568 unsigned len = type->length ();
224c3ddb 4569 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4570 CORE_ADDR addr;
4571
9feb2d07 4572 addr = value->address ();
8ee511af 4573 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4574 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4575 return addr;
4576}
4577
14f9c5c9 4578
4c4b4cd2
PH
4579/* Push a descriptor of type TYPE for array value ARR on the stack at
4580 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4581 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4582 to-descriptor type rather than a descriptor type), a struct value *
4583 representing a pointer to this descriptor. */
14f9c5c9 4584
d2e4a39e 4585static struct value *
40bc484c 4586make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4587{
d2e4a39e
AS
4588 struct type *bounds_type = desc_bounds_type (type);
4589 struct type *desc_type = desc_base_type (type);
317c3ed9
TT
4590 struct value *descriptor = value::allocate (desc_type);
4591 struct value *bounds = value::allocate (bounds_type);
14f9c5c9 4592 int i;
d2e4a39e 4593
d0c97917 4594 for (i = ada_array_arity (ada_check_typedef (arr->type ()));
0963b4bd 4595 i > 0; i -= 1)
14f9c5c9 4596 {
d0c97917 4597 modify_field (bounds->type (),
bbe912ba 4598 bounds->contents_writeable ().data (),
19f220c3
JK
4599 ada_array_bound (arr, i, 0),
4600 desc_bound_bitpos (bounds_type, i, 0),
4601 desc_bound_bitsize (bounds_type, i, 0));
d0c97917 4602 modify_field (bounds->type (),
bbe912ba 4603 bounds->contents_writeable ().data (),
19f220c3
JK
4604 ada_array_bound (arr, i, 1),
4605 desc_bound_bitpos (bounds_type, i, 1),
4606 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4607 }
d2e4a39e 4608
40bc484c 4609 bounds = ensure_lval (bounds);
d2e4a39e 4610
d0c97917 4611 modify_field (descriptor->type (),
bbe912ba 4612 descriptor->contents_writeable ().data (),
19f220c3 4613 value_pointer (ensure_lval (arr),
940da03e 4614 desc_type->field (0).type ()),
19f220c3
JK
4615 fat_pntr_data_bitpos (desc_type),
4616 fat_pntr_data_bitsize (desc_type));
4617
d0c97917 4618 modify_field (descriptor->type (),
bbe912ba 4619 descriptor->contents_writeable ().data (),
19f220c3 4620 value_pointer (bounds,
940da03e 4621 desc_type->field (1).type ()),
19f220c3
JK
4622 fat_pntr_bounds_bitpos (desc_type),
4623 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4624
40bc484c 4625 descriptor = ensure_lval (descriptor);
14f9c5c9 4626
78134374 4627 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4628 return value_addr (descriptor);
4629 else
4630 return descriptor;
4631}
14f9c5c9 4632\f
dda83cd7 4633 /* Symbol Cache Module */
3d9434b5 4634
3d9434b5 4635/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4636 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4637 on the type of entity being printed, the cache can make it as much
4638 as an order of magnitude faster than without it.
4639
4640 The descriptive type DWARF extension has significantly reduced
4641 the need for this cache, at least when DWARF is being used. However,
4642 even in this case, some expensive name-based symbol searches are still
4643 sometimes necessary - to find an XVZ variable, mostly. */
4644
3d9434b5
JB
4645/* Clear all entries from the symbol cache. */
4646
4647static void
bdcccc56 4648ada_clear_symbol_cache ()
3d9434b5 4649{
9d1c303d 4650 ada_pspace_data_handle.clear (current_program_space);
3d9434b5
JB
4651}
4652
fe978cb0 4653/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4654 Return 1 if found, 0 otherwise.
4655
4656 If an entry was found and SYM is not NULL, set *SYM to the entry's
4657 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4658
96d887e8 4659static int
fe978cb0 4660lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4661 struct symbol **sym, const struct block **block)
96d887e8 4662{
9d1c303d
TT
4663 htab_t tab = get_ada_pspace_data (current_program_space);
4664 cache_entry_search search;
4665 search.name = name;
4666 search.domain = domain;
3d9434b5 4667
9d1c303d
TT
4668 cache_entry *e = (cache_entry *) htab_find_with_hash (tab, &search,
4669 search.hash ());
4670 if (e == nullptr)
3d9434b5 4671 return 0;
9d1c303d
TT
4672 if (sym != nullptr)
4673 *sym = e->sym;
4674 if (block != nullptr)
4675 *block = e->block;
3d9434b5 4676 return 1;
96d887e8
PH
4677}
4678
3d9434b5 4679/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4680 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4681
96d887e8 4682static void
fe978cb0 4683cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4684 const struct block *block)
96d887e8 4685{
1994afbf
DE
4686 /* Symbols for builtin types don't have a block.
4687 For now don't cache such symbols. */
7b3ecc75 4688 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4689 return;
4690
3d9434b5
JB
4691 /* If the symbol is a local symbol, then do not cache it, as a search
4692 for that symbol depends on the context. To determine whether
4693 the symbol is local or not, we check the block where we found it
4694 against the global and static blocks of its associated symtab. */
63d609de
SM
4695 if (sym != nullptr)
4696 {
4697 const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
4698
4699 if (bv.global_block () != block && bv.static_block () != block)
4700 return;
4701 }
3d9434b5 4702
9d1c303d
TT
4703 htab_t tab = get_ada_pspace_data (current_program_space);
4704 cache_entry_search search;
4705 search.name = name;
4706 search.domain = domain;
4707
4708 void **slot = htab_find_slot_with_hash (tab, &search,
4709 search.hash (), INSERT);
4710
4711 cache_entry *e = new cache_entry;
4712 e->name = name;
fe978cb0 4713 e->domain = domain;
9d1c303d 4714 e->sym = sym;
3d9434b5 4715 e->block = block;
9d1c303d
TT
4716
4717 *slot = e;
96d887e8 4718}
4c4b4cd2 4719\f
dda83cd7 4720 /* Symbol Lookup */
4c4b4cd2 4721
b5ec771e
PA
4722/* Return the symbol name match type that should be used used when
4723 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4724
4725 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4726 for Ada lookups. */
c0431670 4727
b5ec771e
PA
4728static symbol_name_match_type
4729name_match_type_from_name (const char *lookup_name)
c0431670 4730{
b5ec771e
PA
4731 return (strstr (lookup_name, "__") == NULL
4732 ? symbol_name_match_type::WILD
4733 : symbol_name_match_type::FULL);
c0431670
JB
4734}
4735
4c4b4cd2
PH
4736/* Return the result of a standard (literal, C-like) lookup of NAME in
4737 given DOMAIN, visible from lexical block BLOCK. */
4738
4739static struct symbol *
4740standard_lookup (const char *name, const struct block *block,
dda83cd7 4741 domain_enum domain)
4c4b4cd2 4742{
acbd605d 4743 /* Initialize it just to avoid a GCC false warning. */
6640a367 4744 struct block_symbol sym = {};
4c4b4cd2 4745
d12307c1
PMR
4746 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4747 return sym.symbol;
a2cd4f14 4748 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4749 cache_symbol (name, domain, sym.symbol, sym.block);
4750 return sym.symbol;
4c4b4cd2
PH
4751}
4752
4753
4754/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4755 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4756 since they contend in overloading in the same way. */
4757static int
d1183b06 4758is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4759{
d1183b06 4760 for (const block_symbol &sym : syms)
5f9c5a63
SM
4761 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4762 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4763 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4764 return 1;
4765
4766 return 0;
4767}
4768
4769/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4770 struct types. Otherwise, they may not. */
14f9c5c9
AS
4771
4772static int
d2e4a39e 4773equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4774{
d2e4a39e 4775 if (type0 == type1)
14f9c5c9 4776 return 1;
d2e4a39e 4777 if (type0 == NULL || type1 == NULL
78134374 4778 || type0->code () != type1->code ())
14f9c5c9 4779 return 0;
78134374
SM
4780 if ((type0->code () == TYPE_CODE_STRUCT
4781 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4782 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4783 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4784 return 1;
d2e4a39e 4785
14f9c5c9
AS
4786 return 0;
4787}
4788
4789/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4790 no more defined than that of SYM1. */
14f9c5c9
AS
4791
4792static int
d2e4a39e 4793lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4794{
4795 if (sym0 == sym1)
4796 return 1;
6c9c307c 4797 if (sym0->domain () != sym1->domain ()
66d7f48f 4798 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4799 return 0;
4800
66d7f48f 4801 switch (sym0->aclass ())
14f9c5c9
AS
4802 {
4803 case LOC_UNDEF:
4804 return 1;
4805 case LOC_TYPEDEF:
4806 {
5f9c5a63
SM
4807 struct type *type0 = sym0->type ();
4808 struct type *type1 = sym1->type ();
dda83cd7
SM
4809 const char *name0 = sym0->linkage_name ();
4810 const char *name1 = sym1->linkage_name ();
4811 int len0 = strlen (name0);
4812
4813 return
4814 type0->code () == type1->code ()
4815 && (equiv_types (type0, type1)
4816 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4817 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4818 }
4819 case LOC_CONST:
4aeddc50 4820 return sym0->value_longest () == sym1->value_longest ()
5f9c5a63 4821 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4822
4823 case LOC_STATIC:
4824 {
dda83cd7
SM
4825 const char *name0 = sym0->linkage_name ();
4826 const char *name1 = sym1->linkage_name ();
4827 return (strcmp (name0, name1) == 0
4aeddc50 4828 && sym0->value_address () == sym1->value_address ());
4b610737
TT
4829 }
4830
d2e4a39e
AS
4831 default:
4832 return 0;
14f9c5c9
AS
4833 }
4834}
4835
d1183b06
TT
4836/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4837 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4838
4839static void
d1183b06 4840add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4841 struct symbol *sym,
4842 const struct block *block)
14f9c5c9 4843{
529cad9c
PH
4844 /* Do not try to complete stub types, as the debugger is probably
4845 already scanning all symbols matching a certain name at the
4846 time when this function is called. Trying to replace the stub
4847 type by its associated full type will cause us to restart a scan
4848 which may lead to an infinite recursion. Instead, the client
4849 collecting the matching symbols will end up collecting several
4850 matches, with at least one of them complete. It can then filter
4851 out the stub ones if needed. */
4852
d1183b06 4853 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4854 {
d1183b06 4855 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4856 return;
d1183b06 4857 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4858 {
d1183b06
TT
4859 result[i].symbol = sym;
4860 result[i].block = block;
dda83cd7
SM
4861 return;
4862 }
4c4b4cd2
PH
4863 }
4864
d1183b06
TT
4865 struct block_symbol info;
4866 info.symbol = sym;
4867 info.block = block;
4868 result.push_back (info);
4c4b4cd2
PH
4869}
4870
7c7b6655
TT
4871/* Return a bound minimal symbol matching NAME according to Ada
4872 decoding rules. Returns an invalid symbol if there is no such
4873 minimal symbol. Names prefixed with "standard__" are handled
4874 specially: "standard__" is first stripped off, and only static and
4875 global symbols are searched. */
4c4b4cd2 4876
7c7b6655 4877struct bound_minimal_symbol
06a670e2 4878ada_lookup_simple_minsym (const char *name, struct objfile *objfile)
4c4b4cd2 4879{
7c7b6655 4880 struct bound_minimal_symbol result;
4c4b4cd2 4881
b5ec771e
PA
4882 symbol_name_match_type match_type = name_match_type_from_name (name);
4883 lookup_name_info lookup_name (name, match_type);
4884
4885 symbol_name_matcher_ftype *match_name
4886 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4887
06a670e2
MM
4888 gdbarch_iterate_over_objfiles_in_search_order
4889 (objfile != NULL ? objfile->arch () : target_gdbarch (),
4890 [&result, lookup_name, match_name] (struct objfile *obj)
4891 {
4892 for (minimal_symbol *msymbol : obj->msymbols ())
4893 {
4894 if (match_name (msymbol->linkage_name (), lookup_name, nullptr)
4895 && msymbol->type () != mst_solib_trampoline)
4896 {
4897 result.minsym = msymbol;
4898 result.objfile = obj;
4899 return 1;
4900 }
4901 }
4902
4903 return 0;
4904 }, objfile);
4c4b4cd2 4905
7c7b6655 4906 return result;
96d887e8 4907}
4c4b4cd2 4908
96d887e8
PH
4909/* True if TYPE is definitely an artificial type supplied to a symbol
4910 for which no debugging information was given in the symbol file. */
14f9c5c9 4911
96d887e8
PH
4912static int
4913is_nondebugging_type (struct type *type)
4914{
0d5cff50 4915 const char *name = ada_type_name (type);
5b4ee69b 4916
96d887e8
PH
4917 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4918}
4c4b4cd2 4919
8f17729f
JB
4920/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4921 that are deemed "identical" for practical purposes.
4922
4923 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4924 types and that their number of enumerals is identical (in other
1f704f76 4925 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4926
4927static int
4928ada_identical_enum_types_p (struct type *type1, struct type *type2)
4929{
4930 int i;
4931
4932 /* The heuristic we use here is fairly conservative. We consider
4933 that 2 enumerate types are identical if they have the same
4934 number of enumerals and that all enumerals have the same
4935 underlying value and name. */
4936
4937 /* All enums in the type should have an identical underlying value. */
1f704f76 4938 for (i = 0; i < type1->num_fields (); i++)
970db518 4939 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4940 return 0;
4941
4942 /* All enumerals should also have the same name (modulo any numerical
4943 suffix). */
1f704f76 4944 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4945 {
33d16dd9
SM
4946 const char *name_1 = type1->field (i).name ();
4947 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4948 int len_1 = strlen (name_1);
4949 int len_2 = strlen (name_2);
4950
33d16dd9
SM
4951 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4952 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4953 if (len_1 != len_2
33d16dd9
SM
4954 || strncmp (type1->field (i).name (),
4955 type2->field (i).name (),
8f17729f
JB
4956 len_1) != 0)
4957 return 0;
4958 }
4959
4960 return 1;
4961}
4962
4963/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4964 that are deemed "identical" for practical purposes. Sometimes,
4965 enumerals are not strictly identical, but their types are so similar
4966 that they can be considered identical.
4967
4968 For instance, consider the following code:
4969
4970 type Color is (Black, Red, Green, Blue, White);
4971 type RGB_Color is new Color range Red .. Blue;
4972
4973 Type RGB_Color is a subrange of an implicit type which is a copy
4974 of type Color. If we call that implicit type RGB_ColorB ("B" is
4975 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4976 As a result, when an expression references any of the enumeral
4977 by name (Eg. "print green"), the expression is technically
4978 ambiguous and the user should be asked to disambiguate. But
4979 doing so would only hinder the user, since it wouldn't matter
4980 what choice he makes, the outcome would always be the same.
4981 So, for practical purposes, we consider them as the same. */
4982
4983static int
54d343a2 4984symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4985{
4986 int i;
4987
4988 /* Before performing a thorough comparison check of each type,
4989 we perform a series of inexpensive checks. We expect that these
4990 checks will quickly fail in the vast majority of cases, and thus
4991 help prevent the unnecessary use of a more expensive comparison.
4992 Said comparison also expects us to make some of these checks
4993 (see ada_identical_enum_types_p). */
4994
4995 /* Quick check: All symbols should have an enum type. */
54d343a2 4996 for (i = 0; i < syms.size (); i++)
5f9c5a63 4997 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
4998 return 0;
4999
5000 /* Quick check: They should all have the same value. */
54d343a2 5001 for (i = 1; i < syms.size (); i++)
4aeddc50 5002 if (syms[i].symbol->value_longest () != syms[0].symbol->value_longest ())
8f17729f
JB
5003 return 0;
5004
5005 /* Quick check: They should all have the same number of enumerals. */
54d343a2 5006 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5007 if (syms[i].symbol->type ()->num_fields ()
5008 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
5009 return 0;
5010
5011 /* All the sanity checks passed, so we might have a set of
5012 identical enumeration types. Perform a more complete
5013 comparison of the type of each symbol. */
54d343a2 5014 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5015 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5016 syms[0].symbol->type ()))
8f17729f
JB
5017 return 0;
5018
5019 return 1;
5020}
5021
54d343a2 5022/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5023 duplicate other symbols in the list (The only case I know of where
5024 this happens is when object files containing stabs-in-ecoff are
5025 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5026 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5027
d1183b06 5028static void
54d343a2 5029remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
5030{
5031 int i, j;
4c4b4cd2 5032
8f17729f
JB
5033 /* We should never be called with less than 2 symbols, as there
5034 cannot be any extra symbol in that case. But it's easy to
5035 handle, since we have nothing to do in that case. */
54d343a2 5036 if (syms->size () < 2)
d1183b06 5037 return;
8f17729f 5038
96d887e8 5039 i = 0;
54d343a2 5040 while (i < syms->size ())
96d887e8 5041 {
a35ddb44 5042 int remove_p = 0;
339c13b6
JB
5043
5044 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5045 the get rid of the stub. */
339c13b6 5046
5f9c5a63 5047 if ((*syms)[i].symbol->type ()->is_stub ()
dda83cd7
SM
5048 && (*syms)[i].symbol->linkage_name () != NULL)
5049 {
5050 for (j = 0; j < syms->size (); j++)
5051 {
5052 if (j != i
5f9c5a63 5053 && !(*syms)[j].symbol->type ()->is_stub ()
dda83cd7
SM
5054 && (*syms)[j].symbol->linkage_name () != NULL
5055 && strcmp ((*syms)[i].symbol->linkage_name (),
5056 (*syms)[j].symbol->linkage_name ()) == 0)
5057 remove_p = 1;
5058 }
5059 }
339c13b6
JB
5060
5061 /* Two symbols with the same name, same class and same address
dda83cd7 5062 should be identical. */
339c13b6 5063
987012b8 5064 else if ((*syms)[i].symbol->linkage_name () != NULL
66d7f48f 5065 && (*syms)[i].symbol->aclass () == LOC_STATIC
5f9c5a63 5066 && is_nondebugging_type ((*syms)[i].symbol->type ()))
dda83cd7
SM
5067 {
5068 for (j = 0; j < syms->size (); j += 1)
5069 {
5070 if (i != j
5071 && (*syms)[j].symbol->linkage_name () != NULL
5072 && strcmp ((*syms)[i].symbol->linkage_name (),
5073 (*syms)[j].symbol->linkage_name ()) == 0
66d7f48f
SM
5074 && ((*syms)[i].symbol->aclass ()
5075 == (*syms)[j].symbol->aclass ())
4aeddc50
SM
5076 && (*syms)[i].symbol->value_address ()
5077 == (*syms)[j].symbol->value_address ())
dda83cd7
SM
5078 remove_p = 1;
5079 }
5080 }
339c13b6 5081
a35ddb44 5082 if (remove_p)
54d343a2 5083 syms->erase (syms->begin () + i);
1b788fb6
TT
5084 else
5085 i += 1;
14f9c5c9 5086 }
8f17729f
JB
5087
5088 /* If all the remaining symbols are identical enumerals, then
5089 just keep the first one and discard the rest.
5090
5091 Unlike what we did previously, we do not discard any entry
5092 unless they are ALL identical. This is because the symbol
5093 comparison is not a strict comparison, but rather a practical
5094 comparison. If all symbols are considered identical, then
5095 we can just go ahead and use the first one and discard the rest.
5096 But if we cannot reduce the list to a single element, we have
5097 to ask the user to disambiguate anyways. And if we have to
5098 present a multiple-choice menu, it's less confusing if the list
5099 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
5100 if (symbols_are_identical_enums (*syms))
5101 syms->resize (1);
14f9c5c9
AS
5102}
5103
96d887e8
PH
5104/* Given a type that corresponds to a renaming entity, use the type name
5105 to extract the scope (package name or function name, fully qualified,
5106 and following the GNAT encoding convention) where this renaming has been
49d83361 5107 defined. */
4c4b4cd2 5108
49d83361 5109static std::string
96d887e8 5110xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5111{
96d887e8 5112 /* The renaming types adhere to the following convention:
0963b4bd 5113 <scope>__<rename>___<XR extension>.
96d887e8
PH
5114 So, to extract the scope, we search for the "___XR" extension,
5115 and then backtrack until we find the first "__". */
76a01679 5116
7d93a1e0 5117 const char *name = renaming_type->name ();
108d56a4
SM
5118 const char *suffix = strstr (name, "___XR");
5119 const char *last;
14f9c5c9 5120
96d887e8
PH
5121 /* Now, backtrack a bit until we find the first "__". Start looking
5122 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5123
96d887e8
PH
5124 for (last = suffix - 3; last > name; last--)
5125 if (last[0] == '_' && last[1] == '_')
5126 break;
76a01679 5127
96d887e8 5128 /* Make a copy of scope and return it. */
49d83361 5129 return std::string (name, last);
4c4b4cd2
PH
5130}
5131
96d887e8 5132/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5133
96d887e8
PH
5134static int
5135is_package_name (const char *name)
4c4b4cd2 5136{
96d887e8
PH
5137 /* Here, We take advantage of the fact that no symbols are generated
5138 for packages, while symbols are generated for each function.
5139 So the condition for NAME represent a package becomes equivalent
5140 to NAME not existing in our list of symbols. There is only one
5141 small complication with library-level functions (see below). */
4c4b4cd2 5142
96d887e8
PH
5143 /* If it is a function that has not been defined at library level,
5144 then we should be able to look it up in the symbols. */
5145 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5146 return 0;
14f9c5c9 5147
96d887e8
PH
5148 /* Library-level function names start with "_ada_". See if function
5149 "_ada_" followed by NAME can be found. */
14f9c5c9 5150
96d887e8 5151 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5152 functions names cannot contain "__" in them. */
96d887e8
PH
5153 if (strstr (name, "__") != NULL)
5154 return 0;
4c4b4cd2 5155
528e1572 5156 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5157
528e1572 5158 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5159}
14f9c5c9 5160
96d887e8 5161/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5162 not visible from FUNCTION_NAME. */
14f9c5c9 5163
96d887e8 5164static int
0d5cff50 5165old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5166{
66d7f48f 5167 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5168 return 0;
5169
5f9c5a63 5170 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5171
96d887e8 5172 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5173 if (is_package_name (scope.c_str ()))
5174 return 0;
14f9c5c9 5175
96d887e8
PH
5176 /* Check that the rename is in the current function scope by checking
5177 that its name starts with SCOPE. */
76a01679 5178
96d887e8
PH
5179 /* If the function name starts with "_ada_", it means that it is
5180 a library-level function. Strip this prefix before doing the
5181 comparison, as the encoding for the renaming does not contain
5182 this prefix. */
61012eef 5183 if (startswith (function_name, "_ada_"))
96d887e8 5184 function_name += 5;
f26caa11 5185
49d83361 5186 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5187}
5188
aeb5907d
JB
5189/* Remove entries from SYMS that corresponds to a renaming entity that
5190 is not visible from the function associated with CURRENT_BLOCK or
5191 that is superfluous due to the presence of more specific renaming
5192 information. Places surviving symbols in the initial entries of
d1183b06
TT
5193 SYMS.
5194
96d887e8 5195 Rationale:
aeb5907d
JB
5196 First, in cases where an object renaming is implemented as a
5197 reference variable, GNAT may produce both the actual reference
5198 variable and the renaming encoding. In this case, we discard the
5199 latter.
5200
5201 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5202 entity. Unfortunately, STABS currently does not support the definition
5203 of types that are local to a given lexical block, so all renamings types
5204 are emitted at library level. As a consequence, if an application
5205 contains two renaming entities using the same name, and a user tries to
5206 print the value of one of these entities, the result of the ada symbol
5207 lookup will also contain the wrong renaming type.
f26caa11 5208
96d887e8
PH
5209 This function partially covers for this limitation by attempting to
5210 remove from the SYMS list renaming symbols that should be visible
5211 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5212 method with the current information available. The implementation
5213 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5214
5215 - When the user tries to print a rename in a function while there
dda83cd7
SM
5216 is another rename entity defined in a package: Normally, the
5217 rename in the function has precedence over the rename in the
5218 package, so the latter should be removed from the list. This is
5219 currently not the case.
5220
96d887e8 5221 - This function will incorrectly remove valid renames if
dda83cd7
SM
5222 the CURRENT_BLOCK corresponds to a function which symbol name
5223 has been changed by an "Export" pragma. As a consequence,
5224 the user will be unable to print such rename entities. */
4c4b4cd2 5225
d1183b06 5226static void
54d343a2
TT
5227remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5228 const struct block *current_block)
4c4b4cd2
PH
5229{
5230 struct symbol *current_function;
0d5cff50 5231 const char *current_function_name;
4c4b4cd2 5232 int i;
aeb5907d
JB
5233 int is_new_style_renaming;
5234
5235 /* If there is both a renaming foo___XR... encoded as a variable and
5236 a simple variable foo in the same block, discard the latter.
0963b4bd 5237 First, zero out such symbols, then compress. */
aeb5907d 5238 is_new_style_renaming = 0;
54d343a2 5239 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5240 {
54d343a2
TT
5241 struct symbol *sym = (*syms)[i].symbol;
5242 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5243 const char *name;
5244 const char *suffix;
5245
66d7f48f 5246 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5247 continue;
987012b8 5248 name = sym->linkage_name ();
aeb5907d
JB
5249 suffix = strstr (name, "___XR");
5250
5251 if (suffix != NULL)
5252 {
5253 int name_len = suffix - name;
5254 int j;
5b4ee69b 5255
aeb5907d 5256 is_new_style_renaming = 1;
54d343a2
TT
5257 for (j = 0; j < syms->size (); j += 1)
5258 if (i != j && (*syms)[j].symbol != NULL
987012b8 5259 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5260 name_len) == 0
54d343a2
TT
5261 && block == (*syms)[j].block)
5262 (*syms)[j].symbol = NULL;
aeb5907d
JB
5263 }
5264 }
5265 if (is_new_style_renaming)
5266 {
5267 int j, k;
5268
54d343a2
TT
5269 for (j = k = 0; j < syms->size (); j += 1)
5270 if ((*syms)[j].symbol != NULL)
aeb5907d 5271 {
54d343a2 5272 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5273 k += 1;
5274 }
d1183b06
TT
5275 syms->resize (k);
5276 return;
aeb5907d 5277 }
4c4b4cd2
PH
5278
5279 /* Extract the function name associated to CURRENT_BLOCK.
5280 Abort if unable to do so. */
76a01679 5281
4c4b4cd2 5282 if (current_block == NULL)
d1183b06 5283 return;
76a01679 5284
3c9d0506 5285 current_function = current_block->linkage_function ();
4c4b4cd2 5286 if (current_function == NULL)
d1183b06 5287 return;
4c4b4cd2 5288
987012b8 5289 current_function_name = current_function->linkage_name ();
4c4b4cd2 5290 if (current_function_name == NULL)
d1183b06 5291 return;
4c4b4cd2
PH
5292
5293 /* Check each of the symbols, and remove it from the list if it is
5294 a type corresponding to a renaming that is out of the scope of
5295 the current block. */
5296
5297 i = 0;
54d343a2 5298 while (i < syms->size ())
4c4b4cd2 5299 {
54d343a2 5300 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5301 == ADA_OBJECT_RENAMING
5302 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5303 current_function_name))
5304 syms->erase (syms->begin () + i);
4c4b4cd2 5305 else
dda83cd7 5306 i += 1;
4c4b4cd2 5307 }
4c4b4cd2
PH
5308}
5309
d1183b06 5310/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5311 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5312
cd458349 5313 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5314
5315static void
d1183b06 5316ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5317 const lookup_name_info &lookup_name,
5318 const struct block *block, domain_enum domain)
339c13b6 5319{
339c13b6
JB
5320 while (block != NULL)
5321 {
d1183b06 5322 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5323
ba8694b6
TT
5324 /* If we found a non-function match, assume that's the one. We
5325 only check this when finding a function boundary, so that we
5326 can accumulate all results from intervening blocks first. */
6c00f721 5327 if (block->function () != nullptr && is_nonfunction (result))
dda83cd7 5328 return;
339c13b6 5329
f135fe72 5330 block = block->superblock ();
339c13b6 5331 }
339c13b6
JB
5332}
5333
2315bb2d 5334/* An object of this type is used as the callback argument when
40658b94 5335 calling the map_matching_symbols method. */
ccefe4c4 5336
40658b94 5337struct match_data
ccefe4c4 5338{
1bfa81ac
TT
5339 explicit match_data (std::vector<struct block_symbol> *rp)
5340 : resultp (rp)
5341 {
5342 }
5343 DISABLE_COPY_AND_ASSIGN (match_data);
5344
2315bb2d
TT
5345 bool operator() (struct block_symbol *bsym);
5346
1bfa81ac 5347 struct objfile *objfile = nullptr;
d1183b06 5348 std::vector<struct block_symbol> *resultp;
1bfa81ac 5349 struct symbol *arg_sym = nullptr;
1178743e 5350 bool found_sym = false;
ccefe4c4
TT
5351};
5352
2315bb2d
TT
5353/* A callback for add_nonlocal_symbols that adds symbol, found in
5354 BSYM, to a list of symbols. */
ccefe4c4 5355
2315bb2d
TT
5356bool
5357match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5358{
199b4314
TT
5359 const struct block *block = bsym->block;
5360 struct symbol *sym = bsym->symbol;
5361
40658b94
PH
5362 if (sym == NULL)
5363 {
2315bb2d 5364 if (!found_sym && arg_sym != NULL)
dae58e04 5365 add_defn_to_vec (*resultp, arg_sym, block);
2315bb2d
TT
5366 found_sym = false;
5367 arg_sym = NULL;
40658b94
PH
5368 }
5369 else
5370 {
66d7f48f 5371 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5372 return true;
d9743061 5373 else if (sym->is_argument ())
2315bb2d 5374 arg_sym = sym;
40658b94
PH
5375 else
5376 {
2315bb2d 5377 found_sym = true;
dae58e04 5378 add_defn_to_vec (*resultp, sym, block);
40658b94
PH
5379 }
5380 }
199b4314 5381 return true;
40658b94
PH
5382}
5383
b5ec771e
PA
5384/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5385 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5386 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5387
5388static int
d1183b06 5389ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5390 const struct block *block,
b5ec771e
PA
5391 const lookup_name_info &lookup_name,
5392 domain_enum domain)
22cee43f
PMR
5393{
5394 struct using_direct *renaming;
d1183b06 5395 int defns_mark = result.size ();
22cee43f 5396
b5ec771e
PA
5397 symbol_name_matcher_ftype *name_match
5398 = ada_get_symbol_name_matcher (lookup_name);
5399
3c45e9f9 5400 for (renaming = block->get_using ();
22cee43f
PMR
5401 renaming != NULL;
5402 renaming = renaming->next)
5403 {
5404 const char *r_name;
22cee43f
PMR
5405
5406 /* Avoid infinite recursions: skip this renaming if we are actually
5407 already traversing it.
5408
5409 Currently, symbol lookup in Ada don't use the namespace machinery from
5410 C++/Fortran support: skip namespace imports that use them. */
5411 if (renaming->searched
5412 || (renaming->import_src != NULL
5413 && renaming->import_src[0] != '\0')
5414 || (renaming->import_dest != NULL
5415 && renaming->import_dest[0] != '\0'))
5416 continue;
5417 renaming->searched = 1;
5418
5419 /* TODO: here, we perform another name-based symbol lookup, which can
5420 pull its own multiple overloads. In theory, we should be able to do
5421 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5422 not a simple name. But in order to do this, we would need to enhance
5423 the DWARF reader to associate a symbol to this renaming, instead of a
5424 name. So, for now, we do something simpler: re-use the C++/Fortran
5425 namespace machinery. */
5426 r_name = (renaming->alias != NULL
5427 ? renaming->alias
5428 : renaming->declaration);
b5ec771e
PA
5429 if (name_match (r_name, lookup_name, NULL))
5430 {
5431 lookup_name_info decl_lookup_name (renaming->declaration,
5432 lookup_name.match_type ());
d1183b06 5433 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5434 1, NULL);
5435 }
22cee43f
PMR
5436 renaming->searched = 0;
5437 }
d1183b06 5438 return result.size () != defns_mark;
22cee43f
PMR
5439}
5440
db230ce3
JB
5441/* Implements compare_names, but only applying the comparision using
5442 the given CASING. */
5b4ee69b 5443
40658b94 5444static int
db230ce3
JB
5445compare_names_with_case (const char *string1, const char *string2,
5446 enum case_sensitivity casing)
40658b94
PH
5447{
5448 while (*string1 != '\0' && *string2 != '\0')
5449 {
db230ce3
JB
5450 char c1, c2;
5451
40658b94
PH
5452 if (isspace (*string1) || isspace (*string2))
5453 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5454
5455 if (casing == case_sensitive_off)
5456 {
5457 c1 = tolower (*string1);
5458 c2 = tolower (*string2);
5459 }
5460 else
5461 {
5462 c1 = *string1;
5463 c2 = *string2;
5464 }
5465 if (c1 != c2)
40658b94 5466 break;
db230ce3 5467
40658b94
PH
5468 string1 += 1;
5469 string2 += 1;
5470 }
db230ce3 5471
40658b94
PH
5472 switch (*string1)
5473 {
5474 case '(':
5475 return strcmp_iw_ordered (string1, string2);
5476 case '_':
5477 if (*string2 == '\0')
5478 {
052874e8 5479 if (is_name_suffix (string1))
40658b94
PH
5480 return 0;
5481 else
1a1d5513 5482 return 1;
40658b94 5483 }
dbb8534f 5484 /* FALLTHROUGH */
40658b94
PH
5485 default:
5486 if (*string2 == '(')
5487 return strcmp_iw_ordered (string1, string2);
5488 else
db230ce3
JB
5489 {
5490 if (casing == case_sensitive_off)
5491 return tolower (*string1) - tolower (*string2);
5492 else
5493 return *string1 - *string2;
5494 }
40658b94 5495 }
ccefe4c4
TT
5496}
5497
db230ce3
JB
5498/* Compare STRING1 to STRING2, with results as for strcmp.
5499 Compatible with strcmp_iw_ordered in that...
5500
5501 strcmp_iw_ordered (STRING1, STRING2) <= 0
5502
5503 ... implies...
5504
5505 compare_names (STRING1, STRING2) <= 0
5506
5507 (they may differ as to what symbols compare equal). */
5508
5509static int
5510compare_names (const char *string1, const char *string2)
5511{
5512 int result;
5513
5514 /* Similar to what strcmp_iw_ordered does, we need to perform
5515 a case-insensitive comparison first, and only resort to
5516 a second, case-sensitive, comparison if the first one was
5517 not sufficient to differentiate the two strings. */
5518
5519 result = compare_names_with_case (string1, string2, case_sensitive_off);
5520 if (result == 0)
5521 result = compare_names_with_case (string1, string2, case_sensitive_on);
5522
5523 return result;
5524}
5525
b5ec771e
PA
5526/* Convenience function to get at the Ada encoded lookup name for
5527 LOOKUP_NAME, as a C string. */
5528
5529static const char *
5530ada_lookup_name (const lookup_name_info &lookup_name)
5531{
5532 return lookup_name.ada ().lookup_name ().c_str ();
5533}
5534
0b7b2c2a
TT
5535/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5536 for OBJFILE, then walk the objfile's symtabs and update the
5537 results. */
5538
5539static void
5540map_matching_symbols (struct objfile *objfile,
5541 const lookup_name_info &lookup_name,
5542 bool is_wild_match,
5543 domain_enum domain,
5544 int global,
5545 match_data &data)
5546{
5547 data.objfile = objfile;
5548 objfile->expand_matching_symbols (lookup_name, domain, global,
5549 is_wild_match ? nullptr : compare_names);
5550
5551 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5552 for (compunit_symtab *symtab : objfile->compunits ())
5553 {
5554 const struct block *block
63d609de 5555 = symtab->blockvector ()->block (block_kind);
0b7b2c2a
TT
5556 if (!iterate_over_symbols_terminated (block, lookup_name,
5557 domain, data))
5558 break;
5559 }
5560}
5561
1bfa81ac 5562/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5563 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5564 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5565 symbols otherwise. */
339c13b6
JB
5566
5567static void
d1183b06 5568add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5569 const lookup_name_info &lookup_name,
5570 domain_enum domain, int global)
339c13b6 5571{
1bfa81ac 5572 struct match_data data (&result);
339c13b6 5573
b5ec771e
PA
5574 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5575
2030c079 5576 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5577 {
0b7b2c2a
TT
5578 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5579 global, data);
22cee43f 5580
b669c953 5581 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5582 {
5583 const struct block *global_block
63d609de 5584 = cu->blockvector ()->global_block ();
22cee43f 5585
d1183b06 5586 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5587 domain))
1178743e 5588 data.found_sym = true;
22cee43f 5589 }
40658b94
PH
5590 }
5591
d1183b06 5592 if (result.empty () && global && !is_wild_match)
40658b94 5593 {
b5ec771e 5594 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5595 std::string bracket_name = std::string ("<_ada_") + name + '>';
5596 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5597
2030c079 5598 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5599 map_matching_symbols (objfile, name1, false, domain, global, data);
5600 }
339c13b6
JB
5601}
5602
b5ec771e
PA
5603/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5604 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5605 returning the number of matches. Add these to RESULT.
4eeaa230 5606
22cee43f
PMR
5607 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5608 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5609 is the one match returned (no other matches in that or
d9680e73 5610 enclosing blocks is returned). If there are any matches in or
22cee43f 5611 surrounding BLOCK, then these alone are returned.
4eeaa230 5612
b5ec771e
PA
5613 Names prefixed with "standard__" are handled specially:
5614 "standard__" is first stripped off (by the lookup_name
5615 constructor), and only static and global symbols are searched.
14f9c5c9 5616
22cee43f
PMR
5617 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5618 to lookup global symbols. */
5619
5620static void
d1183b06 5621ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5622 const struct block *block,
b5ec771e 5623 const lookup_name_info &lookup_name,
22cee43f
PMR
5624 domain_enum domain,
5625 int full_search,
5626 int *made_global_lookup_p)
14f9c5c9
AS
5627{
5628 struct symbol *sym;
14f9c5c9 5629
22cee43f
PMR
5630 if (made_global_lookup_p)
5631 *made_global_lookup_p = 0;
339c13b6
JB
5632
5633 /* Special case: If the user specifies a symbol name inside package
5634 Standard, do a non-wild matching of the symbol name without
5635 the "standard__" prefix. This was primarily introduced in order
5636 to allow the user to specifically access the standard exceptions
5637 using, for instance, Standard.Constraint_Error when Constraint_Error
5638 is ambiguous (due to the user defining its own Constraint_Error
5639 entity inside its program). */
b5ec771e
PA
5640 if (lookup_name.ada ().standard_p ())
5641 block = NULL;
4c4b4cd2 5642
339c13b6 5643 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5644
4eeaa230
DE
5645 if (block != NULL)
5646 {
5647 if (full_search)
d1183b06 5648 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5649 else
5650 {
5651 /* In the !full_search case we're are being called by
4009ee92 5652 iterate_over_symbols, and we don't want to search
4eeaa230 5653 superblocks. */
d1183b06 5654 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5655 }
d1183b06 5656 if (!result.empty () || !full_search)
22cee43f 5657 return;
4eeaa230 5658 }
d2e4a39e 5659
339c13b6
JB
5660 /* No non-global symbols found. Check our cache to see if we have
5661 already performed this search before. If we have, then return
5662 the same result. */
5663
b5ec771e
PA
5664 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5665 domain, &sym, &block))
4c4b4cd2
PH
5666 {
5667 if (sym != NULL)
d1183b06 5668 add_defn_to_vec (result, sym, block);
22cee43f 5669 return;
4c4b4cd2 5670 }
14f9c5c9 5671
22cee43f
PMR
5672 if (made_global_lookup_p)
5673 *made_global_lookup_p = 1;
b1eedac9 5674
339c13b6
JB
5675 /* Search symbols from all global blocks. */
5676
d1183b06 5677 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5678
4c4b4cd2 5679 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5680 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5681
d1183b06
TT
5682 if (result.empty ())
5683 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5684}
5685
b5ec771e 5686/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5687 is non-zero, enclosing scope and in global scopes.
5688
5689 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5690 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5691
5692 When full_search is non-zero, any non-function/non-enumeral
5693 symbol match within the nest of blocks whose innermost member is BLOCK,
5694 is the one match returned (no other matches in that or
5695 enclosing blocks is returned). If there are any matches in or
5696 surrounding BLOCK, then these alone are returned.
5697
5698 Names prefixed with "standard__" are handled specially: "standard__"
5699 is first stripped off, and only static and global symbols are searched. */
5700
d1183b06 5701static std::vector<struct block_symbol>
b5ec771e
PA
5702ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5703 const struct block *block,
22cee43f 5704 domain_enum domain,
22cee43f
PMR
5705 int full_search)
5706{
22cee43f 5707 int syms_from_global_search;
d1183b06 5708 std::vector<struct block_symbol> results;
22cee43f 5709
d1183b06 5710 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5711 domain, full_search, &syms_from_global_search);
14f9c5c9 5712
d1183b06 5713 remove_extra_symbols (&results);
4c4b4cd2 5714
d1183b06 5715 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5716 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5717
d1183b06 5718 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5719 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5720 results[0].symbol, results[0].block);
ec6a20c2 5721
d1183b06
TT
5722 remove_irrelevant_renamings (&results, block);
5723 return results;
14f9c5c9
AS
5724}
5725
b5ec771e 5726/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5727 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5728
4eeaa230
DE
5729 See ada_lookup_symbol_list_worker for further details. */
5730
d1183b06 5731std::vector<struct block_symbol>
b5ec771e 5732ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5733 domain_enum domain)
4eeaa230 5734{
b5ec771e
PA
5735 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5736 lookup_name_info lookup_name (name, name_match_type);
5737
d1183b06 5738 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5739}
5740
4e5c77fe
JB
5741/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5742 to 1, but choosing the first symbol found if there are multiple
5743 choices.
5744
5e2336be
JB
5745 The result is stored in *INFO, which must be non-NULL.
5746 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5747
5748void
5749ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5750 domain_enum domain,
d12307c1 5751 struct block_symbol *info)
14f9c5c9 5752{
b5ec771e
PA
5753 /* Since we already have an encoded name, wrap it in '<>' to force a
5754 verbatim match. Otherwise, if the name happens to not look like
5755 an encoded name (because it doesn't include a "__"),
5756 ada_lookup_name_info would re-encode/fold it again, and that
5757 would e.g., incorrectly lowercase object renaming names like
5758 "R28b" -> "r28b". */
12932e2c 5759 std::string verbatim = add_angle_brackets (name);
b5ec771e 5760
5e2336be 5761 gdb_assert (info != NULL);
65392b3e 5762 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5763}
aeb5907d
JB
5764
5765/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5766 scope and in global scopes, or NULL if none. NAME is folded and
5767 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5768 choosing the first symbol if there are multiple choices. */
4e5c77fe 5769
d12307c1 5770struct block_symbol
aeb5907d 5771ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5772 domain_enum domain)
aeb5907d 5773{
d1183b06
TT
5774 std::vector<struct block_symbol> candidates
5775 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5776
d1183b06 5777 if (candidates.empty ())
54d343a2 5778 return {};
f98fc17b 5779
dae58e04 5780 return candidates[0];
4c4b4cd2 5781}
14f9c5c9 5782
14f9c5c9 5783
4c4b4cd2
PH
5784/* True iff STR is a possible encoded suffix of a normal Ada name
5785 that is to be ignored for matching purposes. Suffixes of parallel
5786 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5787 are given by any of the regular expressions:
4c4b4cd2 5788
babe1480
JB
5789 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5790 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5791 TKB [subprogram suffix for task bodies]
babe1480 5792 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5793 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5794
5795 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5796 match is performed. This sequence is used to differentiate homonyms,
5797 is an optional part of a valid name suffix. */
4c4b4cd2 5798
14f9c5c9 5799static int
d2e4a39e 5800is_name_suffix (const char *str)
14f9c5c9
AS
5801{
5802 int k;
4c4b4cd2
PH
5803 const char *matching;
5804 const int len = strlen (str);
5805
babe1480
JB
5806 /* Skip optional leading __[0-9]+. */
5807
4c4b4cd2
PH
5808 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5809 {
babe1480
JB
5810 str += 3;
5811 while (isdigit (str[0]))
dda83cd7 5812 str += 1;
4c4b4cd2 5813 }
babe1480
JB
5814
5815 /* [.$][0-9]+ */
4c4b4cd2 5816
babe1480 5817 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5818 {
babe1480 5819 matching = str + 1;
4c4b4cd2 5820 while (isdigit (matching[0]))
dda83cd7 5821 matching += 1;
4c4b4cd2 5822 if (matching[0] == '\0')
dda83cd7 5823 return 1;
4c4b4cd2
PH
5824 }
5825
5826 /* ___[0-9]+ */
babe1480 5827
4c4b4cd2
PH
5828 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5829 {
5830 matching = str + 3;
5831 while (isdigit (matching[0]))
dda83cd7 5832 matching += 1;
4c4b4cd2 5833 if (matching[0] == '\0')
dda83cd7 5834 return 1;
4c4b4cd2
PH
5835 }
5836
9ac7f98e
JB
5837 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5838
5839 if (strcmp (str, "TKB") == 0)
5840 return 1;
5841
529cad9c
PH
5842#if 0
5843 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5844 with a N at the end. Unfortunately, the compiler uses the same
5845 convention for other internal types it creates. So treating
529cad9c 5846 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5847 some regressions. For instance, consider the case of an enumerated
5848 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5849 name ends with N.
5850 Having a single character like this as a suffix carrying some
0963b4bd 5851 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5852 to be something like "_N" instead. In the meantime, do not do
5853 the following check. */
5854 /* Protected Object Subprograms */
5855 if (len == 1 && str [0] == 'N')
5856 return 1;
5857#endif
5858
5859 /* _E[0-9]+[bs]$ */
5860 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5861 {
5862 matching = str + 3;
5863 while (isdigit (matching[0]))
dda83cd7 5864 matching += 1;
529cad9c 5865 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5866 && matching [1] == '\0')
5867 return 1;
529cad9c
PH
5868 }
5869
4c4b4cd2
PH
5870 /* ??? We should not modify STR directly, as we are doing below. This
5871 is fine in this case, but may become problematic later if we find
5872 that this alternative did not work, and want to try matching
5873 another one from the begining of STR. Since we modified it, we
5874 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5875 if (str[0] == 'X')
5876 {
5877 str += 1;
d2e4a39e 5878 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5879 {
5880 if (str[0] != 'n' && str[0] != 'b')
5881 return 0;
5882 str += 1;
5883 }
14f9c5c9 5884 }
babe1480 5885
14f9c5c9
AS
5886 if (str[0] == '\000')
5887 return 1;
babe1480 5888
d2e4a39e 5889 if (str[0] == '_')
14f9c5c9
AS
5890 {
5891 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5892 return 0;
d2e4a39e 5893 if (str[2] == '_')
dda83cd7
SM
5894 {
5895 if (strcmp (str + 3, "JM") == 0)
5896 return 1;
5897 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5898 the LJM suffix in favor of the JM one. But we will
5899 still accept LJM as a valid suffix for a reasonable
5900 amount of time, just to allow ourselves to debug programs
5901 compiled using an older version of GNAT. */
5902 if (strcmp (str + 3, "LJM") == 0)
5903 return 1;
5904 if (str[3] != 'X')
5905 return 0;
5906 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5907 || str[4] == 'U' || str[4] == 'P')
5908 return 1;
5909 if (str[4] == 'R' && str[5] != 'T')
5910 return 1;
5911 return 0;
5912 }
4c4b4cd2 5913 if (!isdigit (str[2]))
dda83cd7 5914 return 0;
4c4b4cd2 5915 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5916 if (!isdigit (str[k]) && str[k] != '_')
5917 return 0;
14f9c5c9
AS
5918 return 1;
5919 }
4c4b4cd2 5920 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5921 {
4c4b4cd2 5922 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5923 if (!isdigit (str[k]) && str[k] != '_')
5924 return 0;
14f9c5c9
AS
5925 return 1;
5926 }
5927 return 0;
5928}
d2e4a39e 5929
aeb5907d
JB
5930/* Return non-zero if the string starting at NAME and ending before
5931 NAME_END contains no capital letters. */
529cad9c
PH
5932
5933static int
5934is_valid_name_for_wild_match (const char *name0)
5935{
f945dedf 5936 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5937 int i;
5938
5823c3ef
JB
5939 /* If the decoded name starts with an angle bracket, it means that
5940 NAME0 does not follow the GNAT encoding format. It should then
5941 not be allowed as a possible wild match. */
5942 if (decoded_name[0] == '<')
5943 return 0;
5944
529cad9c
PH
5945 for (i=0; decoded_name[i] != '\0'; i++)
5946 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5947 return 0;
5948
5949 return 1;
5950}
5951
59c8a30b
JB
5952/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5953 character which could start a simple name. Assumes that *NAMEP points
5954 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5955
14f9c5c9 5956static int
59c8a30b 5957advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5958{
73589123 5959 const char *name = *namep;
5b4ee69b 5960
5823c3ef 5961 while (1)
14f9c5c9 5962 {
59c8a30b 5963 char t0, t1;
73589123
PH
5964
5965 t0 = *name;
5966 if (t0 == '_')
5967 {
5968 t1 = name[1];
5969 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5970 {
5971 name += 1;
61012eef 5972 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5973 break;
5974 else
5975 name += 1;
5976 }
aa27d0b3
JB
5977 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5978 || name[2] == target0))
73589123
PH
5979 {
5980 name += 2;
5981 break;
5982 }
86b44259
TT
5983 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5984 {
5985 /* Names like "pkg__B_N__name", where N is a number, are
5986 block-local. We can handle these by simply skipping
5987 the "B_" here. */
5988 name += 4;
5989 }
73589123
PH
5990 else
5991 return 0;
5992 }
5993 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5994 name += 1;
5995 else
5823c3ef 5996 return 0;
73589123
PH
5997 }
5998
5999 *namep = name;
6000 return 1;
6001}
6002
b5ec771e
PA
6003/* Return true iff NAME encodes a name of the form prefix.PATN.
6004 Ignores any informational suffixes of NAME (i.e., for which
6005 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6006 simple name. */
73589123 6007
b5ec771e 6008static bool
73589123
PH
6009wild_match (const char *name, const char *patn)
6010{
22e048c9 6011 const char *p;
73589123
PH
6012 const char *name0 = name;
6013
81eaa506
TT
6014 if (startswith (name, "___ghost_"))
6015 name += 9;
6016
73589123
PH
6017 while (1)
6018 {
6019 const char *match = name;
6020
6021 if (*name == *patn)
6022 {
6023 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6024 if (*p != *name)
6025 break;
6026 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6027 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6028
6029 if (name[-1] == '_')
6030 name -= 1;
6031 }
6032 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6033 return false;
96d887e8 6034 }
96d887e8
PH
6035}
6036
d1183b06 6037/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6038 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6039
6040static void
d1183b06 6041ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6042 const struct block *block,
6043 const lookup_name_info &lookup_name,
6044 domain_enum domain, struct objfile *objfile)
96d887e8 6045{
96d887e8
PH
6046 /* A matching argument symbol, if any. */
6047 struct symbol *arg_sym;
6048 /* Set true when we find a matching non-argument symbol. */
1178743e 6049 bool found_sym;
96d887e8
PH
6050
6051 arg_sym = NULL;
1178743e 6052 found_sym = false;
1c49bb45 6053 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
96d887e8 6054 {
6c9c307c 6055 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
b5ec771e 6056 {
66d7f48f 6057 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6058 {
d9743061 6059 if (sym->is_argument ())
b5ec771e
PA
6060 arg_sym = sym;
6061 else
6062 {
1178743e 6063 found_sym = true;
dae58e04 6064 add_defn_to_vec (result, sym, block);
b5ec771e
PA
6065 }
6066 }
6067 }
96d887e8
PH
6068 }
6069
22cee43f
PMR
6070 /* Handle renamings. */
6071
d1183b06 6072 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6073 found_sym = true;
22cee43f 6074
96d887e8
PH
6075 if (!found_sym && arg_sym != NULL)
6076 {
dae58e04 6077 add_defn_to_vec (result, arg_sym, block);
96d887e8
PH
6078 }
6079
b5ec771e 6080 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6081 {
6082 arg_sym = NULL;
1178743e 6083 found_sym = false;
b5ec771e
PA
6084 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6085 const char *name = ada_lookup_name.c_str ();
6086 size_t name_len = ada_lookup_name.size ();
96d887e8 6087
548a89df 6088 for (struct symbol *sym : block_iterator_range (block))
76a01679 6089 {
dda83cd7 6090 if (symbol_matches_domain (sym->language (),
6c9c307c 6091 sym->domain (), domain))
dda83cd7
SM
6092 {
6093 int cmp;
6094
6095 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6096 if (cmp == 0)
6097 {
6098 cmp = !startswith (sym->linkage_name (), "_ada_");
6099 if (cmp == 0)
6100 cmp = strncmp (name, sym->linkage_name () + 5,
6101 name_len);
6102 }
6103
6104 if (cmp == 0
6105 && is_name_suffix (sym->linkage_name () + name_len + 5))
6106 {
66d7f48f 6107 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6108 {
d9743061 6109 if (sym->is_argument ())
2a2d4dc3
AS
6110 arg_sym = sym;
6111 else
6112 {
1178743e 6113 found_sym = true;
dae58e04 6114 add_defn_to_vec (result, sym, block);
2a2d4dc3
AS
6115 }
6116 }
dda83cd7
SM
6117 }
6118 }
76a01679 6119 }
96d887e8
PH
6120
6121 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6122 They aren't parameters, right? */
96d887e8 6123 if (!found_sym && arg_sym != NULL)
dda83cd7 6124 {
dae58e04 6125 add_defn_to_vec (result, arg_sym, block);
dda83cd7 6126 }
96d887e8
PH
6127 }
6128}
6129\f
41d27058 6130
dda83cd7 6131 /* Symbol Completion */
41d27058 6132
b5ec771e 6133/* See symtab.h. */
41d27058 6134
b5ec771e
PA
6135bool
6136ada_lookup_name_info::matches
6137 (const char *sym_name,
6138 symbol_name_match_type match_type,
a207cff2 6139 completion_match_result *comp_match_res) const
41d27058 6140{
b5ec771e
PA
6141 bool match = false;
6142 const char *text = m_encoded_name.c_str ();
6143 size_t text_len = m_encoded_name.size ();
41d27058
JB
6144
6145 /* First, test against the fully qualified name of the symbol. */
6146
6147 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6148 match = true;
41d27058 6149
f945dedf 6150 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6151 if (match && !m_encoded_p)
41d27058
JB
6152 {
6153 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6154 that iff we are doing a verbatim match, the decoded version
6155 of the symbol name starts with '<'. Otherwise, this symbol name
6156 is not a suitable completion. */
41d27058 6157
f945dedf 6158 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6159 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6160 }
6161
b5ec771e 6162 if (match && !m_verbatim_p)
41d27058
JB
6163 {
6164 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6165 be done is to verify that the potentially matching symbol name
6166 does not include capital letters, because the ada-mode would
6167 not be able to understand these symbol names without the
6168 angle bracket notation. */
41d27058
JB
6169 const char *tmp;
6170
6171 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6172 if (*tmp != '\0')
b5ec771e 6173 match = false;
41d27058
JB
6174 }
6175
6176 /* Second: Try wild matching... */
6177
b5ec771e 6178 if (!match && m_wild_match_p)
41d27058
JB
6179 {
6180 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6181 may represent an unqualified symbol name. We therefore must
6182 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6183 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6184
6185 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6186 match = true;
41d27058
JB
6187 }
6188
b5ec771e 6189 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6190
6191 if (!match)
b5ec771e 6192 return false;
41d27058 6193
a207cff2 6194 if (comp_match_res != NULL)
b5ec771e 6195 {
a207cff2 6196 std::string &match_str = comp_match_res->match.storage ();
41d27058 6197
b5ec771e 6198 if (!m_encoded_p)
a207cff2 6199 match_str = ada_decode (sym_name);
b5ec771e
PA
6200 else
6201 {
6202 if (m_verbatim_p)
6203 match_str = add_angle_brackets (sym_name);
6204 else
6205 match_str = sym_name;
41d27058 6206
b5ec771e 6207 }
a207cff2
PA
6208
6209 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6210 }
6211
b5ec771e 6212 return true;
41d27058
JB
6213}
6214
dda83cd7 6215 /* Field Access */
96d887e8 6216
73fb9985
JB
6217/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6218 for tagged types. */
6219
6220static int
6221ada_is_dispatch_table_ptr_type (struct type *type)
6222{
0d5cff50 6223 const char *name;
73fb9985 6224
78134374 6225 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6226 return 0;
6227
27710edb 6228 name = type->target_type ()->name ();
73fb9985
JB
6229 if (name == NULL)
6230 return 0;
6231
6232 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6233}
6234
ac4a2da4
JG
6235/* Return non-zero if TYPE is an interface tag. */
6236
6237static int
6238ada_is_interface_tag (struct type *type)
6239{
7d93a1e0 6240 const char *name = type->name ();
ac4a2da4
JG
6241
6242 if (name == NULL)
6243 return 0;
6244
6245 return (strcmp (name, "ada__tags__interface_tag") == 0);
6246}
6247
963a6417
PH
6248/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6249 to be invisible to users. */
96d887e8 6250
963a6417
PH
6251int
6252ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6253{
1f704f76 6254 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6255 return 1;
ffde82bf 6256
73fb9985
JB
6257 /* Check the name of that field. */
6258 {
33d16dd9 6259 const char *name = type->field (field_num).name ();
73fb9985
JB
6260
6261 /* Anonymous field names should not be printed.
6262 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6263 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6264 if (name == NULL)
6265 return 1;
6266
ffde82bf
JB
6267 /* Normally, fields whose name start with an underscore ("_")
6268 are fields that have been internally generated by the compiler,
6269 and thus should not be printed. The "_parent" field is special,
6270 however: This is a field internally generated by the compiler
6271 for tagged types, and it contains the components inherited from
6272 the parent type. This field should not be printed as is, but
6273 should not be ignored either. */
61012eef 6274 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985 6275 return 1;
d537777d
TT
6276
6277 /* The compiler doesn't document this, but sometimes it emits
6278 a field whose name starts with a capital letter, like 'V148s'.
6279 These aren't marked as artificial in any way, but we know they
6280 should be ignored. However, wrapper fields should not be
6281 ignored. */
6282 if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
6283 {
6284 /* Wrapper field. */
6285 }
6286 else if (isupper (name[0]))
6287 return 1;
73fb9985
JB
6288 }
6289
ac4a2da4
JG
6290 /* If this is the dispatch table of a tagged type or an interface tag,
6291 then ignore. */
73fb9985 6292 if (ada_is_tagged_type (type, 1)
940da03e
SM
6293 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6294 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6295 return 1;
6296
6297 /* Not a special field, so it should not be ignored. */
6298 return 0;
963a6417 6299}
96d887e8 6300
963a6417 6301/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6302 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6303
963a6417
PH
6304int
6305ada_is_tagged_type (struct type *type, int refok)
6306{
988f6b3d 6307 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6308}
96d887e8 6309
963a6417 6310/* True iff TYPE represents the type of X'Tag */
96d887e8 6311
963a6417
PH
6312int
6313ada_is_tag_type (struct type *type)
6314{
460efde1
JB
6315 type = ada_check_typedef (type);
6316
78134374 6317 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6318 return 0;
6319 else
96d887e8 6320 {
27710edb 6321 const char *name = ada_type_name (type->target_type ());
5b4ee69b 6322
963a6417 6323 return (name != NULL
dda83cd7 6324 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6325 }
96d887e8
PH
6326}
6327
963a6417 6328/* The type of the tag on VAL. */
76a01679 6329
de93309a 6330static struct type *
963a6417 6331ada_tag_type (struct value *val)
96d887e8 6332{
d0c97917 6333 return ada_lookup_struct_elt_type (val->type (), "_tag", 1, 0);
963a6417 6334}
96d887e8 6335
b50d69b5
JG
6336/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6337 retired at Ada 05). */
6338
6339static int
6340is_ada95_tag (struct value *tag)
6341{
6342 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6343}
6344
963a6417 6345/* The value of the tag on VAL. */
96d887e8 6346
de93309a 6347static struct value *
963a6417
PH
6348ada_value_tag (struct value *val)
6349{
03ee6b2e 6350 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6351}
6352
963a6417
PH
6353/* The value of the tag on the object of type TYPE whose contents are
6354 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6355 ADDRESS. */
96d887e8 6356
963a6417 6357static struct value *
10a2c479 6358value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6359 const gdb_byte *valaddr,
dda83cd7 6360 CORE_ADDR address)
96d887e8 6361{
b5385fc0 6362 int tag_byte_offset;
963a6417 6363 struct type *tag_type;
5b4ee69b 6364
4d1795ac
TT
6365 gdb::array_view<const gdb_byte> contents;
6366 if (valaddr != nullptr)
df86565b 6367 contents = gdb::make_array_view (valaddr, type->length ());
4d1795ac
TT
6368 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6369 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6370 NULL, NULL, NULL))
96d887e8 6371 {
fc1a4b47 6372 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6373 ? NULL
6374 : valaddr + tag_byte_offset);
963a6417 6375 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6376
963a6417 6377 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6378 }
963a6417
PH
6379 return NULL;
6380}
96d887e8 6381
963a6417
PH
6382static struct type *
6383type_from_tag (struct value *tag)
6384{
f5272a3b 6385 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6386
963a6417 6387 if (type_name != NULL)
5c4258f4 6388 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6389 return NULL;
6390}
96d887e8 6391
b50d69b5
JG
6392/* Given a value OBJ of a tagged type, return a value of this
6393 type at the base address of the object. The base address, as
6394 defined in Ada.Tags, it is the address of the primary tag of
6395 the object, and therefore where the field values of its full
6396 view can be fetched. */
6397
6398struct value *
6399ada_tag_value_at_base_address (struct value *obj)
6400{
b50d69b5
JG
6401 struct value *val;
6402 LONGEST offset_to_top = 0;
6403 struct type *ptr_type, *obj_type;
6404 struct value *tag;
6405 CORE_ADDR base_address;
6406
d0c97917 6407 obj_type = obj->type ();
b50d69b5
JG
6408
6409 /* It is the responsability of the caller to deref pointers. */
6410
78134374 6411 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6412 return obj;
6413
6414 tag = ada_value_tag (obj);
6415 if (!tag)
6416 return obj;
6417
6418 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6419
6420 if (is_ada95_tag (tag))
6421 return obj;
6422
d537777d
TT
6423 struct type *offset_type
6424 = language_lookup_primitive_type (language_def (language_ada),
6425 target_gdbarch(), "storage_offset");
6426 ptr_type = lookup_pointer_type (offset_type);
b50d69b5
JG
6427 val = value_cast (ptr_type, tag);
6428 if (!val)
6429 return obj;
6430
6431 /* It is perfectly possible that an exception be raised while
6432 trying to determine the base address, just like for the tag;
6433 see ada_tag_name for more details. We do not print the error
6434 message for the same reason. */
6435
a70b8144 6436 try
b50d69b5
JG
6437 {
6438 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6439 }
6440
230d2906 6441 catch (const gdb_exception_error &e)
492d29ea
PA
6442 {
6443 return obj;
6444 }
b50d69b5
JG
6445
6446 /* If offset is null, nothing to do. */
6447
6448 if (offset_to_top == 0)
6449 return obj;
6450
6451 /* -1 is a special case in Ada.Tags; however, what should be done
6452 is not quite clear from the documentation. So do nothing for
6453 now. */
6454
6455 if (offset_to_top == -1)
6456 return obj;
6457
d537777d
TT
6458 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6459 top is used. In this situation the offset is stored just after
6460 the tag, in the object itself. */
df86565b 6461 ULONGEST last = (((ULONGEST) 1) << (8 * offset_type->length () - 1)) - 1;
d537777d
TT
6462 if (offset_to_top == last)
6463 {
6464 struct value *tem = value_addr (tag);
6465 tem = value_ptradd (tem, 1);
6466 tem = value_cast (ptr_type, tem);
6467 offset_to_top = value_as_long (value_ind (tem));
6468 }
05527d8c
TV
6469
6470 if (offset_to_top > 0)
d537777d
TT
6471 {
6472 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6473 from the base address. This was however incompatible with
6474 C++ dispatch table: C++ uses a *negative* value to *add*
6475 to the base address. Ada's convention has therefore been
6476 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6477 use the same convention. Here, we support both cases by
6478 checking the sign of OFFSET_TO_TOP. */
6479 offset_to_top = -offset_to_top;
6480 }
08f49010 6481
9feb2d07 6482 base_address = obj->address () + offset_to_top;
b50d69b5
JG
6483 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6484
6485 /* Make sure that we have a proper tag at the new address.
6486 Otherwise, offset_to_top is bogus (which can happen when
6487 the object is not initialized yet). */
6488
6489 if (!tag)
6490 return obj;
6491
6492 obj_type = type_from_tag (tag);
6493
6494 if (!obj_type)
6495 return obj;
6496
6497 return value_from_contents_and_address (obj_type, NULL, base_address);
6498}
6499
1b611343
JB
6500/* Return the "ada__tags__type_specific_data" type. */
6501
6502static struct type *
6503ada_get_tsd_type (struct inferior *inf)
963a6417 6504{
1b611343 6505 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6506
1b611343
JB
6507 if (data->tsd_type == 0)
6508 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6509 return data->tsd_type;
6510}
529cad9c 6511
1b611343
JB
6512/* Return the TSD (type-specific data) associated to the given TAG.
6513 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6514
1b611343 6515 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6516
1b611343
JB
6517static struct value *
6518ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6519{
4c4b4cd2 6520 struct value *val;
1b611343 6521 struct type *type;
5b4ee69b 6522
1b611343
JB
6523 /* First option: The TSD is simply stored as a field of our TAG.
6524 Only older versions of GNAT would use this format, but we have
6525 to test it first, because there are no visible markers for
6526 the current approach except the absence of that field. */
529cad9c 6527
1b611343
JB
6528 val = ada_value_struct_elt (tag, "tsd", 1);
6529 if (val)
6530 return val;
e802dbe0 6531
1b611343
JB
6532 /* Try the second representation for the dispatch table (in which
6533 there is no explicit 'tsd' field in the referent of the tag pointer,
6534 and instead the tsd pointer is stored just before the dispatch
6535 table. */
e802dbe0 6536
1b611343
JB
6537 type = ada_get_tsd_type (current_inferior());
6538 if (type == NULL)
6539 return NULL;
6540 type = lookup_pointer_type (lookup_pointer_type (type));
6541 val = value_cast (type, tag);
6542 if (val == NULL)
6543 return NULL;
6544 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6545}
6546
1b611343
JB
6547/* Given the TSD of a tag (type-specific data), return a string
6548 containing the name of the associated type.
6549
f5272a3b 6550 May return NULL if we are unable to determine the tag name. */
1b611343 6551
f5272a3b 6552static gdb::unique_xmalloc_ptr<char>
1b611343 6553ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6554{
1b611343 6555 struct value *val;
529cad9c 6556
1b611343 6557 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6558 if (val == NULL)
1b611343 6559 return NULL;
66920317
TT
6560 gdb::unique_xmalloc_ptr<char> buffer
6561 = target_read_string (value_as_address (val), INT_MAX);
6562 if (buffer == nullptr)
f5272a3b
TT
6563 return nullptr;
6564
315e4ebb 6565 try
f5272a3b 6566 {
315e4ebb
TT
6567 /* Let this throw an exception on error. If the data is
6568 uninitialized, we'd rather not have the user see a
6569 warning. */
6570 const char *folded = ada_fold_name (buffer.get (), true);
6571 return make_unique_xstrdup (folded);
6572 }
6573 catch (const gdb_exception &)
6574 {
6575 return nullptr;
f5272a3b 6576 }
4c4b4cd2
PH
6577}
6578
6579/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6580 a C string.
6581
6582 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6583 determine the name of that tag. */
4c4b4cd2 6584
f5272a3b 6585gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6586ada_tag_name (struct value *tag)
6587{
f5272a3b 6588 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6589
d0c97917 6590 if (!ada_is_tag_type (tag->type ()))
4c4b4cd2 6591 return NULL;
1b611343
JB
6592
6593 /* It is perfectly possible that an exception be raised while trying
6594 to determine the TAG's name, even under normal circumstances:
6595 The associated variable may be uninitialized or corrupted, for
6596 instance. We do not let any exception propagate past this point.
6597 instead we return NULL.
6598
6599 We also do not print the error message either (which often is very
6600 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6601 the caller print a more meaningful message if necessary. */
a70b8144 6602 try
1b611343
JB
6603 {
6604 struct value *tsd = ada_get_tsd_from_tag (tag);
6605
6606 if (tsd != NULL)
6607 name = ada_tag_name_from_tsd (tsd);
6608 }
230d2906 6609 catch (const gdb_exception_error &e)
492d29ea
PA
6610 {
6611 }
1b611343
JB
6612
6613 return name;
4c4b4cd2
PH
6614}
6615
6616/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6617
d2e4a39e 6618struct type *
ebf56fd3 6619ada_parent_type (struct type *type)
14f9c5c9
AS
6620{
6621 int i;
6622
61ee279c 6623 type = ada_check_typedef (type);
14f9c5c9 6624
78134374 6625 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6626 return NULL;
6627
1f704f76 6628 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6629 if (ada_is_parent_field (type, i))
0c1f74cf 6630 {
dda83cd7 6631 struct type *parent_type = type->field (i).type ();
0c1f74cf 6632
dda83cd7
SM
6633 /* If the _parent field is a pointer, then dereference it. */
6634 if (parent_type->code () == TYPE_CODE_PTR)
27710edb 6635 parent_type = parent_type->target_type ();
dda83cd7
SM
6636 /* If there is a parallel XVS type, get the actual base type. */
6637 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6638
dda83cd7 6639 return ada_check_typedef (parent_type);
0c1f74cf 6640 }
14f9c5c9
AS
6641
6642 return NULL;
6643}
6644
4c4b4cd2
PH
6645/* True iff field number FIELD_NUM of structure type TYPE contains the
6646 parent-type (inherited) fields of a derived type. Assumes TYPE is
6647 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6648
6649int
ebf56fd3 6650ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6651{
33d16dd9 6652 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6653
4c4b4cd2 6654 return (name != NULL
dda83cd7
SM
6655 && (startswith (name, "PARENT")
6656 || startswith (name, "_parent")));
14f9c5c9
AS
6657}
6658
4c4b4cd2 6659/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6660 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6661 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6662 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6663 structures. */
14f9c5c9
AS
6664
6665int
ebf56fd3 6666ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6667{
33d16dd9 6668 const char *name = type->field (field_num).name ();
5b4ee69b 6669
dddc0e16
JB
6670 if (name != NULL && strcmp (name, "RETVAL") == 0)
6671 {
6672 /* This happens in functions with "out" or "in out" parameters
6673 which are passed by copy. For such functions, GNAT describes
6674 the function's return type as being a struct where the return
6675 value is in a field called RETVAL, and where the other "out"
6676 or "in out" parameters are fields of that struct. This is not
6677 a wrapper. */
6678 return 0;
6679 }
6680
d2e4a39e 6681 return (name != NULL
dda83cd7
SM
6682 && (startswith (name, "PARENT")
6683 || strcmp (name, "REP") == 0
6684 || startswith (name, "_parent")
6685 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6686}
6687
4c4b4cd2
PH
6688/* True iff field number FIELD_NUM of structure or union type TYPE
6689 is a variant wrapper. Assumes TYPE is a structure type with at least
6690 FIELD_NUM+1 fields. */
14f9c5c9
AS
6691
6692int
ebf56fd3 6693ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6694{
8ecb59f8
TT
6695 /* Only Ada types are eligible. */
6696 if (!ADA_TYPE_P (type))
6697 return 0;
6698
940da03e 6699 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6700
78134374
SM
6701 return (field_type->code () == TYPE_CODE_UNION
6702 || (is_dynamic_field (type, field_num)
27710edb 6703 && (field_type->target_type ()->code ()
c3e5cd34 6704 == TYPE_CODE_UNION)));
14f9c5c9
AS
6705}
6706
6707/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6708 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6709 returns the type of the controlling discriminant for the variant.
6710 May return NULL if the type could not be found. */
14f9c5c9 6711
d2e4a39e 6712struct type *
ebf56fd3 6713ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6714{
a121b7c1 6715 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6716
988f6b3d 6717 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6718}
6719
4c4b4cd2 6720/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6721 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6722 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6723
de93309a 6724static int
ebf56fd3 6725ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6726{
33d16dd9 6727 const char *name = type->field (field_num).name ();
5b4ee69b 6728
14f9c5c9
AS
6729 return (name != NULL && name[0] == 'O');
6730}
6731
6732/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6733 returns the name of the discriminant controlling the variant.
6734 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6735
a121b7c1 6736const char *
ebf56fd3 6737ada_variant_discrim_name (struct type *type0)
14f9c5c9 6738{
5f9febe0 6739 static std::string result;
d2e4a39e
AS
6740 struct type *type;
6741 const char *name;
6742 const char *discrim_end;
6743 const char *discrim_start;
14f9c5c9 6744
78134374 6745 if (type0->code () == TYPE_CODE_PTR)
27710edb 6746 type = type0->target_type ();
14f9c5c9
AS
6747 else
6748 type = type0;
6749
6750 name = ada_type_name (type);
6751
6752 if (name == NULL || name[0] == '\000')
6753 return "";
6754
6755 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6756 discrim_end -= 1)
6757 {
61012eef 6758 if (startswith (discrim_end, "___XVN"))
dda83cd7 6759 break;
14f9c5c9
AS
6760 }
6761 if (discrim_end == name)
6762 return "";
6763
d2e4a39e 6764 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6765 discrim_start -= 1)
6766 {
d2e4a39e 6767 if (discrim_start == name + 1)
dda83cd7 6768 return "";
76a01679 6769 if ((discrim_start > name + 3
dda83cd7
SM
6770 && startswith (discrim_start - 3, "___"))
6771 || discrim_start[-1] == '.')
6772 break;
14f9c5c9
AS
6773 }
6774
5f9febe0
TT
6775 result = std::string (discrim_start, discrim_end - discrim_start);
6776 return result.c_str ();
14f9c5c9
AS
6777}
6778
4c4b4cd2
PH
6779/* Scan STR for a subtype-encoded number, beginning at position K.
6780 Put the position of the character just past the number scanned in
6781 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6782 Return 1 if there was a valid number at the given position, and 0
6783 otherwise. A "subtype-encoded" number consists of the absolute value
6784 in decimal, followed by the letter 'm' to indicate a negative number.
6785 Assumes 0m does not occur. */
14f9c5c9
AS
6786
6787int
d2e4a39e 6788ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6789{
6790 ULONGEST RU;
6791
d2e4a39e 6792 if (!isdigit (str[k]))
14f9c5c9
AS
6793 return 0;
6794
4c4b4cd2 6795 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6796 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6797 LONGEST. */
14f9c5c9
AS
6798 RU = 0;
6799 while (isdigit (str[k]))
6800 {
d2e4a39e 6801 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6802 k += 1;
6803 }
6804
d2e4a39e 6805 if (str[k] == 'm')
14f9c5c9
AS
6806 {
6807 if (R != NULL)
dda83cd7 6808 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6809 k += 1;
6810 }
6811 else if (R != NULL)
6812 *R = (LONGEST) RU;
6813
4c4b4cd2 6814 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6815 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6816 number representable as a LONGEST (although either would probably work
6817 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6818 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6819
6820 if (new_k != NULL)
6821 *new_k = k;
6822 return 1;
6823}
6824
4c4b4cd2
PH
6825/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6826 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6827 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6828
de93309a 6829static int
ebf56fd3 6830ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6831{
33d16dd9 6832 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6833 int p;
6834
6835 p = 0;
6836 while (1)
6837 {
d2e4a39e 6838 switch (name[p])
dda83cd7
SM
6839 {
6840 case '\0':
6841 return 0;
6842 case 'S':
6843 {
6844 LONGEST W;
6845
6846 if (!ada_scan_number (name, p + 1, &W, &p))
6847 return 0;
6848 if (val == W)
6849 return 1;
6850 break;
6851 }
6852 case 'R':
6853 {
6854 LONGEST L, U;
6855
6856 if (!ada_scan_number (name, p + 1, &L, &p)
6857 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6858 return 0;
6859 if (val >= L && val <= U)
6860 return 1;
6861 break;
6862 }
6863 case 'O':
6864 return 1;
6865 default:
6866 return 0;
6867 }
4c4b4cd2
PH
6868 }
6869}
6870
0963b4bd 6871/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6872
6873/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6874 ARG_TYPE, extract and return the value of one of its (non-static)
6875 fields. FIELDNO says which field. Differs from value_primitive_field
6876 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6877
5eb68a39 6878struct value *
d2e4a39e 6879ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6880 struct type *arg_type)
14f9c5c9 6881{
14f9c5c9
AS
6882 struct type *type;
6883
61ee279c 6884 arg_type = ada_check_typedef (arg_type);
940da03e 6885 type = arg_type->field (fieldno).type ();
14f9c5c9 6886
4504bbde
TT
6887 /* Handle packed fields. It might be that the field is not packed
6888 relative to its containing structure, but the structure itself is
6889 packed; in this case we must take the bit-field path. */
5011c493 6890 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || arg1->bitpos () != 0)
14f9c5c9 6891 {
b610c045 6892 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
14f9c5c9 6893 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6894
50888e42 6895 return ada_value_primitive_packed_val (arg1,
efaf1ae0 6896 arg1->contents ().data (),
dda83cd7
SM
6897 offset + bit_pos / 8,
6898 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6899 }
6900 else
6c49729e 6901 return arg1->primitive_field (offset, fieldno, arg_type);
14f9c5c9
AS
6902}
6903
52ce6436
PH
6904/* Find field with name NAME in object of type TYPE. If found,
6905 set the following for each argument that is non-null:
6906 - *FIELD_TYPE_P to the field's type;
6907 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6908 an object of that type;
6909 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6910 - *BIT_SIZE_P to its size in bits if the field is packed, and
6911 0 otherwise;
6912 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6913 fields up to but not including the desired field, or by the total
6914 number of fields if not found. A NULL value of NAME never
6915 matches; the function just counts visible fields in this case.
6916
828d5846
XR
6917 Notice that we need to handle when a tagged record hierarchy
6918 has some components with the same name, like in this scenario:
6919
6920 type Top_T is tagged record
dda83cd7
SM
6921 N : Integer := 1;
6922 U : Integer := 974;
6923 A : Integer := 48;
828d5846
XR
6924 end record;
6925
6926 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6927 N : Character := 'a';
6928 C : Integer := 3;
828d5846
XR
6929 end record;
6930
6931 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6932 N : Float := 4.0;
6933 C : Character := '5';
6934 X : Integer := 6;
6935 A : Character := 'J';
828d5846
XR
6936 end record;
6937
6938 Let's say we now have a variable declared and initialized as follow:
6939
6940 TC : Top_A := new Bottom_T;
6941
6942 And then we use this variable to call this function
6943
6944 procedure Assign (Obj: in out Top_T; TV : Integer);
6945
6946 as follow:
6947
6948 Assign (Top_T (B), 12);
6949
6950 Now, we're in the debugger, and we're inside that procedure
6951 then and we want to print the value of obj.c:
6952
6953 Usually, the tagged record or one of the parent type owns the
6954 component to print and there's no issue but in this particular
6955 case, what does it mean to ask for Obj.C? Since the actual
6956 type for object is type Bottom_T, it could mean two things: type
6957 component C from the Middle_T view, but also component C from
6958 Bottom_T. So in that "undefined" case, when the component is
6959 not found in the non-resolved type (which includes all the
6960 components of the parent type), then resolve it and see if we
6961 get better luck once expanded.
6962
6963 In the case of homonyms in the derived tagged type, we don't
6964 guaranty anything, and pick the one that's easiest for us
6965 to program.
6966
0963b4bd 6967 Returns 1 if found, 0 otherwise. */
52ce6436 6968
4c4b4cd2 6969static int
0d5cff50 6970find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6971 struct type **field_type_p,
6972 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6973 int *index_p)
4c4b4cd2
PH
6974{
6975 int i;
828d5846 6976 int parent_offset = -1;
4c4b4cd2 6977
61ee279c 6978 type = ada_check_typedef (type);
76a01679 6979
52ce6436
PH
6980 if (field_type_p != NULL)
6981 *field_type_p = NULL;
6982 if (byte_offset_p != NULL)
d5d6fca5 6983 *byte_offset_p = 0;
52ce6436
PH
6984 if (bit_offset_p != NULL)
6985 *bit_offset_p = 0;
6986 if (bit_size_p != NULL)
6987 *bit_size_p = 0;
6988
1f704f76 6989 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 6990 {
4d1795ac
TT
6991 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6992 type. However, we only need the values to be correct when
6993 the caller asks for them. */
6994 int bit_pos = 0, fld_offset = 0;
6995 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6996 {
b610c045 6997 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
6998 fld_offset = offset + bit_pos / 8;
6999 }
7000
33d16dd9 7001 const char *t_field_name = type->field (i).name ();
76a01679 7002
4c4b4cd2 7003 if (t_field_name == NULL)
dda83cd7 7004 continue;
4c4b4cd2 7005
828d5846 7006 else if (ada_is_parent_field (type, i))
dda83cd7 7007 {
828d5846
XR
7008 /* This is a field pointing us to the parent type of a tagged
7009 type. As hinted in this function's documentation, we give
7010 preference to fields in the current record first, so what
7011 we do here is just record the index of this field before
7012 we skip it. If it turns out we couldn't find our field
7013 in the current record, then we'll get back to it and search
7014 inside it whether the field might exist in the parent. */
7015
dda83cd7
SM
7016 parent_offset = i;
7017 continue;
7018 }
828d5846 7019
52ce6436 7020 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
7021 {
7022 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 7023
52ce6436 7024 if (field_type_p != NULL)
940da03e 7025 *field_type_p = type->field (i).type ();
52ce6436
PH
7026 if (byte_offset_p != NULL)
7027 *byte_offset_p = fld_offset;
7028 if (bit_offset_p != NULL)
7029 *bit_offset_p = bit_pos % 8;
7030 if (bit_size_p != NULL)
7031 *bit_size_p = bit_size;
dda83cd7
SM
7032 return 1;
7033 }
4c4b4cd2 7034 else if (ada_is_wrapper_field (type, i))
dda83cd7 7035 {
940da03e 7036 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7037 field_type_p, byte_offset_p, bit_offset_p,
7038 bit_size_p, index_p))
dda83cd7
SM
7039 return 1;
7040 }
4c4b4cd2 7041 else if (ada_is_variant_part (type, i))
dda83cd7 7042 {
52ce6436
PH
7043 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7044 fixed type?? */
dda83cd7
SM
7045 int j;
7046 struct type *field_type
940da03e 7047 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7048
dda83cd7
SM
7049 for (j = 0; j < field_type->num_fields (); j += 1)
7050 {
7051 if (find_struct_field (name, field_type->field (j).type (),
7052 fld_offset
b610c045 7053 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7054 field_type_p, byte_offset_p,
7055 bit_offset_p, bit_size_p, index_p))
7056 return 1;
7057 }
7058 }
52ce6436
PH
7059 else if (index_p != NULL)
7060 *index_p += 1;
4c4b4cd2 7061 }
828d5846
XR
7062
7063 /* Field not found so far. If this is a tagged type which
7064 has a parent, try finding that field in the parent now. */
7065
7066 if (parent_offset != -1)
7067 {
4d1795ac
TT
7068 /* As above, only compute the offset when truly needed. */
7069 int fld_offset = offset;
7070 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7071 {
b610c045 7072 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7073 fld_offset += bit_pos / 8;
7074 }
828d5846 7075
940da03e 7076 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7077 fld_offset, field_type_p, byte_offset_p,
7078 bit_offset_p, bit_size_p, index_p))
7079 return 1;
828d5846
XR
7080 }
7081
4c4b4cd2
PH
7082 return 0;
7083}
7084
0963b4bd 7085/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7086
52ce6436
PH
7087static int
7088num_visible_fields (struct type *type)
7089{
7090 int n;
5b4ee69b 7091
52ce6436
PH
7092 n = 0;
7093 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7094 return n;
7095}
14f9c5c9 7096
4c4b4cd2 7097/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7098 and search in it assuming it has (class) type TYPE.
7099 If found, return value, else return NULL.
7100
828d5846
XR
7101 Searches recursively through wrapper fields (e.g., '_parent').
7102
7103 In the case of homonyms in the tagged types, please refer to the
7104 long explanation in find_struct_field's function documentation. */
14f9c5c9 7105
4c4b4cd2 7106static struct value *
108d56a4 7107ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7108 struct type *type)
14f9c5c9
AS
7109{
7110 int i;
828d5846 7111 int parent_offset = -1;
14f9c5c9 7112
5b4ee69b 7113 type = ada_check_typedef (type);
1f704f76 7114 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7115 {
33d16dd9 7116 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7117
7118 if (t_field_name == NULL)
dda83cd7 7119 continue;
14f9c5c9 7120
828d5846 7121 else if (ada_is_parent_field (type, i))
dda83cd7 7122 {
828d5846
XR
7123 /* This is a field pointing us to the parent type of a tagged
7124 type. As hinted in this function's documentation, we give
7125 preference to fields in the current record first, so what
7126 we do here is just record the index of this field before
7127 we skip it. If it turns out we couldn't find our field
7128 in the current record, then we'll get back to it and search
7129 inside it whether the field might exist in the parent. */
7130
dda83cd7
SM
7131 parent_offset = i;
7132 continue;
7133 }
828d5846 7134
14f9c5c9 7135 else if (field_name_match (t_field_name, name))
dda83cd7 7136 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7137
7138 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7139 {
7140 struct value *v = /* Do not let indent join lines here. */
7141 ada_search_struct_field (name, arg,
b610c045 7142 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7143 type->field (i).type ());
5b4ee69b 7144
dda83cd7
SM
7145 if (v != NULL)
7146 return v;
7147 }
14f9c5c9
AS
7148
7149 else if (ada_is_variant_part (type, i))
dda83cd7 7150 {
0963b4bd 7151 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7152 int j;
7153 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7154 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7155
dda83cd7
SM
7156 for (j = 0; j < field_type->num_fields (); j += 1)
7157 {
7158 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7159 break. */
dda83cd7 7160 (name, arg,
b610c045 7161 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7162 field_type->field (j).type ());
5b4ee69b 7163
dda83cd7
SM
7164 if (v != NULL)
7165 return v;
7166 }
7167 }
14f9c5c9 7168 }
828d5846
XR
7169
7170 /* Field not found so far. If this is a tagged type which
7171 has a parent, try finding that field in the parent now. */
7172
7173 if (parent_offset != -1)
7174 {
7175 struct value *v = ada_search_struct_field (
b610c045 7176 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7177 type->field (parent_offset).type ());
828d5846
XR
7178
7179 if (v != NULL)
dda83cd7 7180 return v;
828d5846
XR
7181 }
7182
14f9c5c9
AS
7183 return NULL;
7184}
d2e4a39e 7185
52ce6436
PH
7186static struct value *ada_index_struct_field_1 (int *, struct value *,
7187 int, struct type *);
7188
7189
7190/* Return field #INDEX in ARG, where the index is that returned by
7191 * find_struct_field through its INDEX_P argument. Adjust the address
7192 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7193 * If found, return value, else return NULL. */
52ce6436
PH
7194
7195static struct value *
7196ada_index_struct_field (int index, struct value *arg, int offset,
7197 struct type *type)
7198{
7199 return ada_index_struct_field_1 (&index, arg, offset, type);
7200}
7201
7202
7203/* Auxiliary function for ada_index_struct_field. Like
7204 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7205 * *INDEX_P. */
52ce6436
PH
7206
7207static struct value *
7208ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7209 struct type *type)
7210{
7211 int i;
7212 type = ada_check_typedef (type);
7213
1f704f76 7214 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7215 {
33d16dd9 7216 if (type->field (i).name () == NULL)
dda83cd7 7217 continue;
52ce6436 7218 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7219 {
7220 struct value *v = /* Do not let indent join lines here. */
7221 ada_index_struct_field_1 (index_p, arg,
b610c045 7222 offset + type->field (i).loc_bitpos () / 8,
940da03e 7223 type->field (i).type ());
5b4ee69b 7224
dda83cd7
SM
7225 if (v != NULL)
7226 return v;
7227 }
52ce6436
PH
7228
7229 else if (ada_is_variant_part (type, i))
dda83cd7 7230 {
52ce6436 7231 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7232 find_struct_field. */
52ce6436 7233 error (_("Cannot assign this kind of variant record"));
dda83cd7 7234 }
52ce6436 7235 else if (*index_p == 0)
dda83cd7 7236 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7237 else
7238 *index_p -= 1;
7239 }
7240 return NULL;
7241}
7242
3b4de39c 7243/* Return a string representation of type TYPE. */
99bbb428 7244
3b4de39c 7245static std::string
99bbb428
PA
7246type_as_string (struct type *type)
7247{
d7e74731 7248 string_file tmp_stream;
99bbb428 7249
d7e74731 7250 type_print (type, "", &tmp_stream, -1);
99bbb428 7251
5d10a204 7252 return tmp_stream.release ();
99bbb428
PA
7253}
7254
14f9c5c9 7255/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7256 If DISPP is non-null, add its byte displacement from the beginning of a
7257 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7258 work for packed fields).
7259
7260 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7261 followed by "___".
14f9c5c9 7262
0963b4bd 7263 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7264 be a (pointer or reference)+ to a struct or union, and the
7265 ultimate target type will be searched.
14f9c5c9
AS
7266
7267 Looks recursively into variant clauses and parent types.
7268
828d5846
XR
7269 In the case of homonyms in the tagged types, please refer to the
7270 long explanation in find_struct_field's function documentation.
7271
4c4b4cd2
PH
7272 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7273 TYPE is not a type of the right kind. */
14f9c5c9 7274
4c4b4cd2 7275static struct type *
a121b7c1 7276ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7277 int noerr)
14f9c5c9
AS
7278{
7279 int i;
828d5846 7280 int parent_offset = -1;
14f9c5c9
AS
7281
7282 if (name == NULL)
7283 goto BadName;
7284
76a01679 7285 if (refok && type != NULL)
4c4b4cd2
PH
7286 while (1)
7287 {
dda83cd7
SM
7288 type = ada_check_typedef (type);
7289 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7290 break;
27710edb 7291 type = type->target_type ();
4c4b4cd2 7292 }
14f9c5c9 7293
76a01679 7294 if (type == NULL
78134374
SM
7295 || (type->code () != TYPE_CODE_STRUCT
7296 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7297 {
4c4b4cd2 7298 if (noerr)
dda83cd7 7299 return NULL;
99bbb428 7300
3b4de39c
PA
7301 error (_("Type %s is not a structure or union type"),
7302 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7303 }
7304
7305 type = to_static_fixed_type (type);
7306
1f704f76 7307 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7308 {
33d16dd9 7309 const char *t_field_name = type->field (i).name ();
14f9c5c9 7310 struct type *t;
d2e4a39e 7311
14f9c5c9 7312 if (t_field_name == NULL)
dda83cd7 7313 continue;
14f9c5c9 7314
828d5846 7315 else if (ada_is_parent_field (type, i))
dda83cd7 7316 {
828d5846
XR
7317 /* This is a field pointing us to the parent type of a tagged
7318 type. As hinted in this function's documentation, we give
7319 preference to fields in the current record first, so what
7320 we do here is just record the index of this field before
7321 we skip it. If it turns out we couldn't find our field
7322 in the current record, then we'll get back to it and search
7323 inside it whether the field might exist in the parent. */
7324
dda83cd7
SM
7325 parent_offset = i;
7326 continue;
7327 }
828d5846 7328
14f9c5c9 7329 else if (field_name_match (t_field_name, name))
940da03e 7330 return type->field (i).type ();
14f9c5c9
AS
7331
7332 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7333 {
7334 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7335 0, 1);
7336 if (t != NULL)
988f6b3d 7337 return t;
dda83cd7 7338 }
14f9c5c9
AS
7339
7340 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7341 {
7342 int j;
7343 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7344
dda83cd7
SM
7345 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7346 {
b1f33ddd 7347 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7348 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7349 generates these for unchecked variant types. Revisit
dda83cd7 7350 if the compiler changes this practice. */
33d16dd9 7351 const char *v_field_name = field_type->field (j).name ();
988f6b3d 7352
b1f33ddd
JB
7353 if (v_field_name != NULL
7354 && field_name_match (v_field_name, name))
940da03e 7355 t = field_type->field (j).type ();
b1f33ddd 7356 else
940da03e 7357 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7358 name, 0, 1);
b1f33ddd 7359
dda83cd7 7360 if (t != NULL)
988f6b3d 7361 return t;
dda83cd7
SM
7362 }
7363 }
14f9c5c9
AS
7364
7365 }
7366
828d5846
XR
7367 /* Field not found so far. If this is a tagged type which
7368 has a parent, try finding that field in the parent now. */
7369
7370 if (parent_offset != -1)
7371 {
dda83cd7 7372 struct type *t;
828d5846 7373
dda83cd7
SM
7374 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7375 name, 0, 1);
7376 if (t != NULL)
828d5846
XR
7377 return t;
7378 }
7379
14f9c5c9 7380BadName:
d2e4a39e 7381 if (!noerr)
14f9c5c9 7382 {
2b2798cc 7383 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7384
7385 error (_("Type %s has no component named %s"),
3b4de39c 7386 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7387 }
7388
7389 return NULL;
7390}
7391
b1f33ddd
JB
7392/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7393 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7394 represents an unchecked union (that is, the variant part of a
0963b4bd 7395 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7396
7397static int
7398is_unchecked_variant (struct type *var_type, struct type *outer_type)
7399{
a121b7c1 7400 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7401
988f6b3d 7402 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7403}
7404
7405
14f9c5c9 7406/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7407 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7408 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7409
d2e4a39e 7410int
d8af9068 7411ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7412{
7413 int others_clause;
7414 int i;
a121b7c1 7415 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7416 struct value *discrim;
14f9c5c9
AS
7417 LONGEST discrim_val;
7418
012370f6
TT
7419 /* Using plain value_from_contents_and_address here causes problems
7420 because we will end up trying to resolve a type that is currently
7421 being constructed. */
0c281816
JB
7422 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7423 if (discrim == NULL)
14f9c5c9 7424 return -1;
0c281816 7425 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7426
7427 others_clause = -1;
1f704f76 7428 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7429 {
7430 if (ada_is_others_clause (var_type, i))
dda83cd7 7431 others_clause = i;
14f9c5c9 7432 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7433 return i;
14f9c5c9
AS
7434 }
7435
7436 return others_clause;
7437}
d2e4a39e 7438\f
14f9c5c9
AS
7439
7440
dda83cd7 7441 /* Dynamic-Sized Records */
14f9c5c9
AS
7442
7443/* Strategy: The type ostensibly attached to a value with dynamic size
7444 (i.e., a size that is not statically recorded in the debugging
7445 data) does not accurately reflect the size or layout of the value.
7446 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7447 conventional types that are constructed on the fly. */
14f9c5c9
AS
7448
7449/* There is a subtle and tricky problem here. In general, we cannot
7450 determine the size of dynamic records without its data. However,
7451 the 'struct value' data structure, which GDB uses to represent
7452 quantities in the inferior process (the target), requires the size
7453 of the type at the time of its allocation in order to reserve space
7454 for GDB's internal copy of the data. That's why the
7455 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7456 rather than struct value*s.
14f9c5c9
AS
7457
7458 However, GDB's internal history variables ($1, $2, etc.) are
7459 struct value*s containing internal copies of the data that are not, in
7460 general, the same as the data at their corresponding addresses in
7461 the target. Fortunately, the types we give to these values are all
7462 conventional, fixed-size types (as per the strategy described
7463 above), so that we don't usually have to perform the
7464 'to_fixed_xxx_type' conversions to look at their values.
7465 Unfortunately, there is one exception: if one of the internal
7466 history variables is an array whose elements are unconstrained
7467 records, then we will need to create distinct fixed types for each
7468 element selected. */
7469
7470/* The upshot of all of this is that many routines take a (type, host
7471 address, target address) triple as arguments to represent a value.
7472 The host address, if non-null, is supposed to contain an internal
7473 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7474 target at the target address. */
14f9c5c9
AS
7475
7476/* Assuming that VAL0 represents a pointer value, the result of
7477 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7478 dynamic-sized types. */
14f9c5c9 7479
d2e4a39e
AS
7480struct value *
7481ada_value_ind (struct value *val0)
14f9c5c9 7482{
c48db5ca 7483 struct value *val = value_ind (val0);
5b4ee69b 7484
d0c97917 7485 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7486 val = ada_tag_value_at_base_address (val);
7487
4c4b4cd2 7488 return ada_to_fixed_value (val);
14f9c5c9
AS
7489}
7490
7491/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7492 qualifiers on VAL0. */
7493
d2e4a39e
AS
7494static struct value *
7495ada_coerce_ref (struct value *val0)
7496{
d0c97917 7497 if (val0->type ()->code () == TYPE_CODE_REF)
d2e4a39e
AS
7498 {
7499 struct value *val = val0;
5b4ee69b 7500
994b9211 7501 val = coerce_ref (val);
b50d69b5 7502
d0c97917 7503 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7504 val = ada_tag_value_at_base_address (val);
7505
4c4b4cd2 7506 return ada_to_fixed_value (val);
d2e4a39e
AS
7507 }
7508 else
14f9c5c9
AS
7509 return val0;
7510}
7511
4c4b4cd2 7512/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7513
7514static unsigned int
ebf56fd3 7515field_alignment (struct type *type, int f)
14f9c5c9 7516{
33d16dd9 7517 const char *name = type->field (f).name ();
64a1bf19 7518 int len;
14f9c5c9
AS
7519 int align_offset;
7520
64a1bf19
JB
7521 /* The field name should never be null, unless the debugging information
7522 is somehow malformed. In this case, we assume the field does not
7523 require any alignment. */
7524 if (name == NULL)
7525 return 1;
7526
7527 len = strlen (name);
7528
4c4b4cd2
PH
7529 if (!isdigit (name[len - 1]))
7530 return 1;
14f9c5c9 7531
d2e4a39e 7532 if (isdigit (name[len - 2]))
14f9c5c9
AS
7533 align_offset = len - 2;
7534 else
7535 align_offset = len - 1;
7536
61012eef 7537 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7538 return TARGET_CHAR_BIT;
7539
4c4b4cd2
PH
7540 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7541}
7542
852dff6c 7543/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7544
852dff6c
JB
7545static struct symbol *
7546ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7547{
7548 struct symbol *sym;
7549
7550 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
66d7f48f 7551 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7552 return sym;
7553
4186eb54
KS
7554 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7555 return sym;
14f9c5c9
AS
7556}
7557
dddfab26
UW
7558/* Find a type named NAME. Ignores ambiguity. This routine will look
7559 solely for types defined by debug info, it will not search the GDB
7560 primitive types. */
4c4b4cd2 7561
852dff6c 7562static struct type *
ebf56fd3 7563ada_find_any_type (const char *name)
14f9c5c9 7564{
852dff6c 7565 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7566
14f9c5c9 7567 if (sym != NULL)
5f9c5a63 7568 return sym->type ();
14f9c5c9 7569
dddfab26 7570 return NULL;
14f9c5c9
AS
7571}
7572
739593e0
JB
7573/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7574 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7575 symbol, in which case it is returned. Otherwise, this looks for
7576 symbols whose name is that of NAME_SYM suffixed with "___XR".
7577 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7578
c0e70c62
TT
7579static bool
7580ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7581{
987012b8 7582 const char *name = name_sym->linkage_name ();
c0e70c62 7583 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7584}
7585
14f9c5c9 7586/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7587 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7588 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7589 otherwise return 0. */
7590
14f9c5c9 7591int
d2e4a39e 7592ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7593{
7594 if (type1 == NULL)
7595 return 1;
7596 else if (type0 == NULL)
7597 return 0;
78134374 7598 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7599 return 1;
78134374 7600 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7601 return 0;
7d93a1e0 7602 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7603 return 1;
ad82864c 7604 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7605 return 1;
4c4b4cd2 7606 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7607 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7608 return 1;
aeb5907d
JB
7609 else
7610 {
7d93a1e0
SM
7611 const char *type0_name = type0->name ();
7612 const char *type1_name = type1->name ();
aeb5907d
JB
7613
7614 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7615 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7616 return 1;
7617 }
14f9c5c9
AS
7618 return 0;
7619}
7620
e86ca25f
TT
7621/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7622 null. */
4c4b4cd2 7623
0d5cff50 7624const char *
d2e4a39e 7625ada_type_name (struct type *type)
14f9c5c9 7626{
d2e4a39e 7627 if (type == NULL)
14f9c5c9 7628 return NULL;
7d93a1e0 7629 return type->name ();
14f9c5c9
AS
7630}
7631
b4ba55a1
JB
7632/* Search the list of "descriptive" types associated to TYPE for a type
7633 whose name is NAME. */
7634
7635static struct type *
7636find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7637{
931e5bc3 7638 struct type *result, *tmp;
b4ba55a1 7639
c6044dd1
JB
7640 if (ada_ignore_descriptive_types_p)
7641 return NULL;
7642
b4ba55a1
JB
7643 /* If there no descriptive-type info, then there is no parallel type
7644 to be found. */
7645 if (!HAVE_GNAT_AUX_INFO (type))
7646 return NULL;
7647
7648 result = TYPE_DESCRIPTIVE_TYPE (type);
7649 while (result != NULL)
7650 {
0d5cff50 7651 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7652
7653 if (result_name == NULL)
dda83cd7
SM
7654 {
7655 warning (_("unexpected null name on descriptive type"));
7656 return NULL;
7657 }
b4ba55a1
JB
7658
7659 /* If the names match, stop. */
7660 if (strcmp (result_name, name) == 0)
7661 break;
7662
7663 /* Otherwise, look at the next item on the list, if any. */
7664 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7665 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7666 else
7667 tmp = NULL;
7668
7669 /* If not found either, try after having resolved the typedef. */
7670 if (tmp != NULL)
7671 result = tmp;
b4ba55a1 7672 else
931e5bc3 7673 {
f168693b 7674 result = check_typedef (result);
931e5bc3
JG
7675 if (HAVE_GNAT_AUX_INFO (result))
7676 result = TYPE_DESCRIPTIVE_TYPE (result);
7677 else
7678 result = NULL;
7679 }
b4ba55a1
JB
7680 }
7681
7682 /* If we didn't find a match, see whether this is a packed array. With
7683 older compilers, the descriptive type information is either absent or
7684 irrelevant when it comes to packed arrays so the above lookup fails.
7685 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7686 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7687 return ada_find_any_type (name);
7688
7689 return result;
7690}
7691
7692/* Find a parallel type to TYPE with the specified NAME, using the
7693 descriptive type taken from the debugging information, if available,
7694 and otherwise using the (slower) name-based method. */
7695
7696static struct type *
7697ada_find_parallel_type_with_name (struct type *type, const char *name)
7698{
7699 struct type *result = NULL;
7700
7701 if (HAVE_GNAT_AUX_INFO (type))
7702 result = find_parallel_type_by_descriptive_type (type, name);
7703 else
7704 result = ada_find_any_type (name);
7705
7706 return result;
7707}
7708
7709/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7710 SUFFIX to the name of TYPE. */
14f9c5c9 7711
d2e4a39e 7712struct type *
ebf56fd3 7713ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7714{
0d5cff50 7715 char *name;
fe978cb0 7716 const char *type_name = ada_type_name (type);
14f9c5c9 7717 int len;
d2e4a39e 7718
fe978cb0 7719 if (type_name == NULL)
14f9c5c9
AS
7720 return NULL;
7721
fe978cb0 7722 len = strlen (type_name);
14f9c5c9 7723
b4ba55a1 7724 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7725
fe978cb0 7726 strcpy (name, type_name);
14f9c5c9
AS
7727 strcpy (name + len, suffix);
7728
b4ba55a1 7729 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7730}
7731
14f9c5c9 7732/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7733 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7734
d2e4a39e
AS
7735static struct type *
7736dynamic_template_type (struct type *type)
14f9c5c9 7737{
61ee279c 7738 type = ada_check_typedef (type);
14f9c5c9 7739
78134374 7740 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7741 || ada_type_name (type) == NULL)
14f9c5c9 7742 return NULL;
d2e4a39e 7743 else
14f9c5c9
AS
7744 {
7745 int len = strlen (ada_type_name (type));
5b4ee69b 7746
4c4b4cd2 7747 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7748 return type;
14f9c5c9 7749 else
dda83cd7 7750 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7751 }
7752}
7753
7754/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7755 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7756
d2e4a39e
AS
7757static int
7758is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7759{
33d16dd9 7760 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7761
d2e4a39e 7762 return name != NULL
940da03e 7763 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7764 && strstr (name, "___XVL") != NULL;
7765}
7766
4c4b4cd2
PH
7767/* The index of the variant field of TYPE, or -1 if TYPE does not
7768 represent a variant record type. */
14f9c5c9 7769
d2e4a39e 7770static int
4c4b4cd2 7771variant_field_index (struct type *type)
14f9c5c9
AS
7772{
7773 int f;
7774
78134374 7775 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7776 return -1;
7777
1f704f76 7778 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7779 {
7780 if (ada_is_variant_part (type, f))
dda83cd7 7781 return f;
4c4b4cd2
PH
7782 }
7783 return -1;
14f9c5c9
AS
7784}
7785
4c4b4cd2
PH
7786/* A record type with no fields. */
7787
d2e4a39e 7788static struct type *
fe978cb0 7789empty_record (struct type *templ)
14f9c5c9 7790{
9fa83a7a 7791 struct type *type = type_allocator (templ).new_type ();
5b4ee69b 7792
67607e24 7793 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7794 INIT_NONE_SPECIFIC (type);
d0e39ea2 7795 type->set_name ("<empty>");
b6cdbc9a 7796 type->set_length (0);
14f9c5c9
AS
7797 return type;
7798}
7799
7800/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7801 the value of type TYPE at VALADDR or ADDRESS (see comments at
7802 the beginning of this section) VAL according to GNAT conventions.
7803 DVAL0 should describe the (portion of a) record that contains any
d0c97917 7804 necessary discriminants. It should be NULL if VAL->type () is
14f9c5c9
AS
7805 an outer-level type (i.e., as opposed to a branch of a variant.) A
7806 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7807 of the variant.
14f9c5c9 7808
4c4b4cd2
PH
7809 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7810 length are not statically known are discarded. As a consequence,
7811 VALADDR, ADDRESS and DVAL0 are ignored.
7812
7813 NOTE: Limitations: For now, we assume that dynamic fields and
7814 variants occupy whole numbers of bytes. However, they need not be
7815 byte-aligned. */
7816
7817struct type *
10a2c479 7818ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7819 const gdb_byte *valaddr,
dda83cd7
SM
7820 CORE_ADDR address, struct value *dval0,
7821 int keep_dynamic_fields)
14f9c5c9 7822{
d2e4a39e
AS
7823 struct value *dval;
7824 struct type *rtype;
14f9c5c9 7825 int nfields, bit_len;
4c4b4cd2 7826 int variant_field;
14f9c5c9 7827 long off;
d94e4f4f 7828 int fld_bit_len;
14f9c5c9
AS
7829 int f;
7830
65558ca5
TT
7831 scoped_value_mark mark;
7832
4c4b4cd2
PH
7833 /* Compute the number of fields in this record type that are going
7834 to be processed: unless keep_dynamic_fields, this includes only
7835 fields whose position and length are static will be processed. */
7836 if (keep_dynamic_fields)
1f704f76 7837 nfields = type->num_fields ();
4c4b4cd2
PH
7838 else
7839 {
7840 nfields = 0;
1f704f76 7841 while (nfields < type->num_fields ()
dda83cd7
SM
7842 && !ada_is_variant_part (type, nfields)
7843 && !is_dynamic_field (type, nfields))
7844 nfields++;
4c4b4cd2
PH
7845 }
7846
9fa83a7a 7847 rtype = type_allocator (type).new_type ();
67607e24 7848 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7849 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7850 rtype->set_num_fields (nfields);
3cabb6b0
SM
7851 rtype->set_fields
7852 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7853 rtype->set_name (ada_type_name (type));
9cdd0d12 7854 rtype->set_is_fixed_instance (true);
14f9c5c9 7855
d2e4a39e
AS
7856 off = 0;
7857 bit_len = 0;
4c4b4cd2
PH
7858 variant_field = -1;
7859
14f9c5c9
AS
7860 for (f = 0; f < nfields; f += 1)
7861 {
a89febbd 7862 off = align_up (off, field_alignment (type, f))
b610c045 7863 + type->field (f).loc_bitpos ();
cd3f655c 7864 rtype->field (f).set_loc_bitpos (off);
d2e4a39e 7865 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7866
d2e4a39e 7867 if (ada_is_variant_part (type, f))
dda83cd7
SM
7868 {
7869 variant_field = f;
7870 fld_bit_len = 0;
7871 }
14f9c5c9 7872 else if (is_dynamic_field (type, f))
dda83cd7 7873 {
284614f0
JB
7874 const gdb_byte *field_valaddr = valaddr;
7875 CORE_ADDR field_address = address;
27710edb 7876 struct type *field_type = type->field (f).type ()->target_type ();
284614f0 7877
dda83cd7 7878 if (dval0 == NULL)
b5304971 7879 {
012370f6
TT
7880 /* Using plain value_from_contents_and_address here
7881 causes problems because we will end up trying to
7882 resolve a type that is currently being
7883 constructed. */
7884 dval = value_from_contents_and_address_unresolved (rtype,
7885 valaddr,
7886 address);
d0c97917 7887 rtype = dval->type ();
b5304971 7888 }
dda83cd7
SM
7889 else
7890 dval = dval0;
4c4b4cd2 7891
284614f0
JB
7892 /* If the type referenced by this field is an aligner type, we need
7893 to unwrap that aligner type, because its size might not be set.
7894 Keeping the aligner type would cause us to compute the wrong
7895 size for this field, impacting the offset of the all the fields
7896 that follow this one. */
7897 if (ada_is_aligner_type (field_type))
7898 {
b610c045 7899 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7900
7901 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7902 field_address = cond_offset_target (field_address, field_offset);
7903 field_type = ada_aligned_type (field_type);
7904 }
7905
7906 field_valaddr = cond_offset_host (field_valaddr,
7907 off / TARGET_CHAR_BIT);
7908 field_address = cond_offset_target (field_address,
7909 off / TARGET_CHAR_BIT);
7910
7911 /* Get the fixed type of the field. Note that, in this case,
7912 we do not want to get the real type out of the tag: if
7913 the current field is the parent part of a tagged record,
7914 we will get the tag of the object. Clearly wrong: the real
7915 type of the parent is not the real type of the child. We
7916 would end up in an infinite loop. */
7917 field_type = ada_get_base_type (field_type);
7918 field_type = ada_to_fixed_type (field_type, field_valaddr,
7919 field_address, dval, 0);
7920
5d14b6e5 7921 rtype->field (f).set_type (field_type);
33d16dd9 7922 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7923 /* The multiplication can potentially overflow. But because
7924 the field length has been size-checked just above, and
7925 assuming that the maximum size is a reasonable value,
7926 an overflow should not happen in practice. So rather than
7927 adding overflow recovery code to this already complex code,
7928 we just assume that it's not going to happen. */
df86565b 7929 fld_bit_len = rtype->field (f).type ()->length () * TARGET_CHAR_BIT;
dda83cd7 7930 }
14f9c5c9 7931 else
dda83cd7 7932 {
5ded5331
JB
7933 /* Note: If this field's type is a typedef, it is important
7934 to preserve the typedef layer.
7935
7936 Otherwise, we might be transforming a typedef to a fat
7937 pointer (encoding a pointer to an unconstrained array),
7938 into a basic fat pointer (encoding an unconstrained
7939 array). As both types are implemented using the same
7940 structure, the typedef is the only clue which allows us
7941 to distinguish between the two options. Stripping it
7942 would prevent us from printing this field appropriately. */
dda83cd7 7943 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7944 rtype->field (f).set_name (type->field (f).name ());
dda83cd7
SM
7945 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7946 fld_bit_len =
7947 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7948 else
5ded5331 7949 {
940da03e 7950 struct type *field_type = type->field (f).type ();
5ded5331
JB
7951
7952 /* We need to be careful of typedefs when computing
7953 the length of our field. If this is a typedef,
7954 get the length of the target type, not the length
7955 of the typedef. */
78134374 7956 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7957 field_type = ada_typedef_target_type (field_type);
7958
dda83cd7 7959 fld_bit_len =
df86565b 7960 ada_check_typedef (field_type)->length () * TARGET_CHAR_BIT;
5ded5331 7961 }
dda83cd7 7962 }
14f9c5c9 7963 if (off + fld_bit_len > bit_len)
dda83cd7 7964 bit_len = off + fld_bit_len;
d94e4f4f 7965 off += fld_bit_len;
b6cdbc9a 7966 rtype->set_length (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
14f9c5c9 7967 }
4c4b4cd2
PH
7968
7969 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7970 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7971 the record. This can happen in the presence of representation
7972 clauses. */
7973 if (variant_field >= 0)
7974 {
7975 struct type *branch_type;
7976
b610c045 7977 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
7978
7979 if (dval0 == NULL)
9f1f738a 7980 {
012370f6
TT
7981 /* Using plain value_from_contents_and_address here causes
7982 problems because we will end up trying to resolve a type
7983 that is currently being constructed. */
7984 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7985 address);
d0c97917 7986 rtype = dval->type ();
9f1f738a 7987 }
4c4b4cd2 7988 else
dda83cd7 7989 dval = dval0;
4c4b4cd2
PH
7990
7991 branch_type =
dda83cd7
SM
7992 to_fixed_variant_branch_type
7993 (type->field (variant_field).type (),
7994 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7995 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7996 if (branch_type == NULL)
dda83cd7
SM
7997 {
7998 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7999 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8000 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 8001 }
4c4b4cd2 8002 else
dda83cd7
SM
8003 {
8004 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8005 rtype->field (variant_field).set_name ("S");
dda83cd7 8006 fld_bit_len =
df86565b 8007 rtype->field (variant_field).type ()->length () * TARGET_CHAR_BIT;
dda83cd7
SM
8008 if (off + fld_bit_len > bit_len)
8009 bit_len = off + fld_bit_len;
b6cdbc9a
SM
8010
8011 rtype->set_length
8012 (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
dda83cd7 8013 }
4c4b4cd2
PH
8014 }
8015
714e53ab
PH
8016 /* According to exp_dbug.ads, the size of TYPE for variable-size records
8017 should contain the alignment of that record, which should be a strictly
8018 positive value. If null or negative, then something is wrong, most
8019 probably in the debug info. In that case, we don't round up the size
0963b4bd 8020 of the resulting type. If this record is not part of another structure,
714e53ab 8021 the current RTYPE length might be good enough for our purposes. */
df86565b 8022 if (type->length () <= 0)
714e53ab 8023 {
7d93a1e0 8024 if (rtype->name ())
cc1defb1 8025 warning (_("Invalid type size for `%s' detected: %s."),
df86565b 8026 rtype->name (), pulongest (type->length ()));
323e0a4a 8027 else
cc1defb1 8028 warning (_("Invalid type size for <unnamed> detected: %s."),
df86565b 8029 pulongest (type->length ()));
714e53ab
PH
8030 }
8031 else
df86565b 8032 rtype->set_length (align_up (rtype->length (), type->length ()));
14f9c5c9 8033
14f9c5c9
AS
8034 return rtype;
8035}
8036
4c4b4cd2
PH
8037/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8038 of 1. */
14f9c5c9 8039
d2e4a39e 8040static struct type *
fc1a4b47 8041template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8042 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
8043{
8044 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 8045 address, dval0, 1);
4c4b4cd2
PH
8046}
8047
8048/* An ordinary record type in which ___XVL-convention fields and
8049 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8050 static approximations, containing all possible fields. Uses
8051 no runtime values. Useless for use in values, but that's OK,
8052 since the results are used only for type determinations. Works on both
8053 structs and unions. Representation note: to save space, we memorize
27710edb 8054 the result of this function in the type::target_type of the
4c4b4cd2
PH
8055 template type. */
8056
8057static struct type *
8058template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8059{
8060 struct type *type;
8061 int nfields;
8062 int f;
8063
9e195661 8064 /* No need no do anything if the input type is already fixed. */
22c4c60c 8065 if (type0->is_fixed_instance ())
9e195661
PMR
8066 return type0;
8067
8068 /* Likewise if we already have computed the static approximation. */
27710edb
SM
8069 if (type0->target_type () != NULL)
8070 return type0->target_type ();
4c4b4cd2 8071
9e195661 8072 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8073 type = type0;
1f704f76 8074 nfields = type0->num_fields ();
9e195661
PMR
8075
8076 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8077 recompute all over next time. */
8a50fdce 8078 type0->set_target_type (type);
14f9c5c9
AS
8079
8080 for (f = 0; f < nfields; f += 1)
8081 {
940da03e 8082 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8083 struct type *new_type;
14f9c5c9 8084
4c4b4cd2 8085 if (is_dynamic_field (type0, f))
460efde1
JB
8086 {
8087 field_type = ada_check_typedef (field_type);
27710edb 8088 new_type = to_static_fixed_type (field_type->target_type ());
460efde1 8089 }
14f9c5c9 8090 else
dda83cd7 8091 new_type = static_unwrap_type (field_type);
9e195661
PMR
8092
8093 if (new_type != field_type)
8094 {
8095 /* Clone TYPE0 only the first time we get a new field type. */
8096 if (type == type0)
8097 {
9fa83a7a 8098 type = type_allocator (type0).new_type ();
8a50fdce 8099 type0->set_target_type (type);
78134374 8100 type->set_code (type0->code ());
8ecb59f8 8101 INIT_NONE_SPECIFIC (type);
5e33d5f4 8102 type->set_num_fields (nfields);
3cabb6b0
SM
8103
8104 field *fields =
8105 ((struct field *)
8106 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 8107 memcpy (fields, type0->fields (),
9e195661 8108 sizeof (struct field) * nfields);
3cabb6b0
SM
8109 type->set_fields (fields);
8110
d0e39ea2 8111 type->set_name (ada_type_name (type0));
9cdd0d12 8112 type->set_is_fixed_instance (true);
b6cdbc9a 8113 type->set_length (0);
9e195661 8114 }
5d14b6e5 8115 type->field (f).set_type (new_type);
33d16dd9 8116 type->field (f).set_name (type0->field (f).name ());
9e195661 8117 }
14f9c5c9 8118 }
9e195661 8119
14f9c5c9
AS
8120 return type;
8121}
8122
4c4b4cd2 8123/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8124 whose address in memory is ADDRESS, returns a revision of TYPE,
8125 which should be a non-dynamic-sized record, in which the variant
8126 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8127 for discriminant values in DVAL0, which can be NULL if the record
8128 contains the necessary discriminant values. */
8129
d2e4a39e 8130static struct type *
fc1a4b47 8131to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8132 CORE_ADDR address, struct value *dval0)
14f9c5c9 8133{
4c4b4cd2 8134 struct value *dval;
d2e4a39e 8135 struct type *rtype;
14f9c5c9 8136 struct type *branch_type;
1f704f76 8137 int nfields = type->num_fields ();
4c4b4cd2 8138 int variant_field = variant_field_index (type);
14f9c5c9 8139
4c4b4cd2 8140 if (variant_field == -1)
14f9c5c9
AS
8141 return type;
8142
65558ca5 8143 scoped_value_mark mark;
4c4b4cd2 8144 if (dval0 == NULL)
9f1f738a
SA
8145 {
8146 dval = value_from_contents_and_address (type, valaddr, address);
d0c97917 8147 type = dval->type ();
9f1f738a 8148 }
4c4b4cd2
PH
8149 else
8150 dval = dval0;
8151
9fa83a7a 8152 rtype = type_allocator (type).new_type ();
67607e24 8153 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8154 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 8155 rtype->set_num_fields (nfields);
3cabb6b0
SM
8156
8157 field *fields =
d2e4a39e 8158 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 8159 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
8160 rtype->set_fields (fields);
8161
d0e39ea2 8162 rtype->set_name (ada_type_name (type));
9cdd0d12 8163 rtype->set_is_fixed_instance (true);
df86565b 8164 rtype->set_length (type->length ());
14f9c5c9 8165
4c4b4cd2 8166 branch_type = to_fixed_variant_branch_type
940da03e 8167 (type->field (variant_field).type (),
d2e4a39e 8168 cond_offset_host (valaddr,
b610c045 8169 type->field (variant_field).loc_bitpos ()
dda83cd7 8170 / TARGET_CHAR_BIT),
d2e4a39e 8171 cond_offset_target (address,
b610c045 8172 type->field (variant_field).loc_bitpos ()
dda83cd7 8173 / TARGET_CHAR_BIT), dval);
d2e4a39e 8174 if (branch_type == NULL)
14f9c5c9 8175 {
4c4b4cd2 8176 int f;
5b4ee69b 8177
4c4b4cd2 8178 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8179 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8180 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8181 }
8182 else
8183 {
5d14b6e5 8184 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8185 rtype->field (variant_field).set_name ("S");
4c4b4cd2 8186 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
df86565b 8187 rtype->set_length (rtype->length () + branch_type->length ());
14f9c5c9 8188 }
b6cdbc9a 8189
df86565b
SM
8190 rtype->set_length (rtype->length ()
8191 - type->field (variant_field).type ()->length ());
d2e4a39e 8192
14f9c5c9
AS
8193 return rtype;
8194}
8195
8196/* An ordinary record type (with fixed-length fields) that describes
8197 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8198 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8199 should be in DVAL, a record value; it may be NULL if the object
8200 at ADDR itself contains any necessary discriminant values.
8201 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8202 values from the record are needed. Except in the case that DVAL,
8203 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8204 unchecked) is replaced by a particular branch of the variant.
8205
8206 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8207 is questionable and may be removed. It can arise during the
8208 processing of an unconstrained-array-of-record type where all the
8209 variant branches have exactly the same size. This is because in
8210 such cases, the compiler does not bother to use the XVS convention
8211 when encoding the record. I am currently dubious of this
8212 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8213
d2e4a39e 8214static struct type *
fc1a4b47 8215to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8216 CORE_ADDR address, struct value *dval)
14f9c5c9 8217{
d2e4a39e 8218 struct type *templ_type;
14f9c5c9 8219
22c4c60c 8220 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8221 return type0;
8222
d2e4a39e 8223 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8224
8225 if (templ_type != NULL)
8226 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8227 else if (variant_field_index (type0) >= 0)
8228 {
8229 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8230 return type0;
4c4b4cd2 8231 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8232 dval);
4c4b4cd2 8233 }
14f9c5c9
AS
8234 else
8235 {
9cdd0d12 8236 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8237 return type0;
8238 }
8239
8240}
8241
8242/* An ordinary record type (with fixed-length fields) that describes
8243 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8244 union type. Any necessary discriminants' values should be in DVAL,
8245 a record value. That is, this routine selects the appropriate
8246 branch of the union at ADDR according to the discriminant value
b1f33ddd 8247 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8248 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8249
d2e4a39e 8250static struct type *
fc1a4b47 8251to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8252 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8253{
8254 int which;
d2e4a39e
AS
8255 struct type *templ_type;
8256 struct type *var_type;
14f9c5c9 8257
78134374 8258 if (var_type0->code () == TYPE_CODE_PTR)
27710edb 8259 var_type = var_type0->target_type ();
d2e4a39e 8260 else
14f9c5c9
AS
8261 var_type = var_type0;
8262
8263 templ_type = ada_find_parallel_type (var_type, "___XVU");
8264
8265 if (templ_type != NULL)
8266 var_type = templ_type;
8267
d0c97917 8268 if (is_unchecked_variant (var_type, dval->type ()))
b1f33ddd 8269 return var_type0;
d8af9068 8270 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8271
8272 if (which < 0)
e9bb382b 8273 return empty_record (var_type);
14f9c5c9 8274 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8275 return to_fixed_record_type
27710edb 8276 (var_type->field (which).type ()->target_type(), valaddr, address, dval);
940da03e 8277 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8278 return
8279 to_fixed_record_type
940da03e 8280 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8281 else
940da03e 8282 return var_type->field (which).type ();
14f9c5c9
AS
8283}
8284
8908fca5
JB
8285/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8286 ENCODING_TYPE, a type following the GNAT conventions for discrete
8287 type encodings, only carries redundant information. */
8288
8289static int
8290ada_is_redundant_range_encoding (struct type *range_type,
8291 struct type *encoding_type)
8292{
108d56a4 8293 const char *bounds_str;
8908fca5
JB
8294 int n;
8295 LONGEST lo, hi;
8296
78134374 8297 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8298
78134374
SM
8299 if (get_base_type (range_type)->code ()
8300 != get_base_type (encoding_type)->code ())
005e2509
JB
8301 {
8302 /* The compiler probably used a simple base type to describe
8303 the range type instead of the range's actual base type,
8304 expecting us to get the real base type from the encoding
8305 anyway. In this situation, the encoding cannot be ignored
8306 as redundant. */
8307 return 0;
8308 }
8309
8908fca5
JB
8310 if (is_dynamic_type (range_type))
8311 return 0;
8312
7d93a1e0 8313 if (encoding_type->name () == NULL)
8908fca5
JB
8314 return 0;
8315
7d93a1e0 8316 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8317 if (bounds_str == NULL)
8318 return 0;
8319
8320 n = 8; /* Skip "___XDLU_". */
8321 if (!ada_scan_number (bounds_str, n, &lo, &n))
8322 return 0;
5537ddd0 8323 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8324 return 0;
8325
8326 n += 2; /* Skip the "__" separator between the two bounds. */
8327 if (!ada_scan_number (bounds_str, n, &hi, &n))
8328 return 0;
5537ddd0 8329 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8330 return 0;
8331
8332 return 1;
8333}
8334
8335/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8336 a type following the GNAT encoding for describing array type
8337 indices, only carries redundant information. */
8338
8339static int
8340ada_is_redundant_index_type_desc (struct type *array_type,
8341 struct type *desc_type)
8342{
8343 struct type *this_layer = check_typedef (array_type);
8344 int i;
8345
1f704f76 8346 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8347 {
3d967001 8348 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8349 desc_type->field (i).type ()))
8908fca5 8350 return 0;
27710edb 8351 this_layer = check_typedef (this_layer->target_type ());
8908fca5
JB
8352 }
8353
8354 return 1;
8355}
8356
14f9c5c9
AS
8357/* Assuming that TYPE0 is an array type describing the type of a value
8358 at ADDR, and that DVAL describes a record containing any
8359 discriminants used in TYPE0, returns a type for the value that
8360 contains no dynamic components (that is, no components whose sizes
8361 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8362 true, gives an error message if the resulting type's size is over
4c4b4cd2 8363 varsize_limit. */
14f9c5c9 8364
d2e4a39e
AS
8365static struct type *
8366to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8367 int ignore_too_big)
14f9c5c9 8368{
d2e4a39e
AS
8369 struct type *index_type_desc;
8370 struct type *result;
ad82864c 8371 int constrained_packed_array_p;
931e5bc3 8372 static const char *xa_suffix = "___XA";
14f9c5c9 8373
b0dd7688 8374 type0 = ada_check_typedef (type0);
22c4c60c 8375 if (type0->is_fixed_instance ())
4c4b4cd2 8376 return type0;
14f9c5c9 8377
ad82864c
JB
8378 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8379 if (constrained_packed_array_p)
75fd6a26
TT
8380 {
8381 type0 = decode_constrained_packed_array_type (type0);
8382 if (type0 == nullptr)
8383 error (_("could not decode constrained packed array type"));
8384 }
284614f0 8385
931e5bc3
JG
8386 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8387
8388 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8389 encoding suffixed with 'P' may still be generated. If so,
8390 it should be used to find the XA type. */
8391
8392 if (index_type_desc == NULL)
8393 {
1da0522e 8394 const char *type_name = ada_type_name (type0);
931e5bc3 8395
1da0522e 8396 if (type_name != NULL)
931e5bc3 8397 {
1da0522e 8398 const int len = strlen (type_name);
931e5bc3
JG
8399 char *name = (char *) alloca (len + strlen (xa_suffix));
8400
1da0522e 8401 if (type_name[len - 1] == 'P')
931e5bc3 8402 {
1da0522e 8403 strcpy (name, type_name);
931e5bc3
JG
8404 strcpy (name + len - 1, xa_suffix);
8405 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8406 }
8407 }
8408 }
8409
28c85d6c 8410 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8411 if (index_type_desc != NULL
8412 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8413 {
8414 /* Ignore this ___XA parallel type, as it does not bring any
8415 useful information. This allows us to avoid creating fixed
8416 versions of the array's index types, which would be identical
8417 to the original ones. This, in turn, can also help avoid
8418 the creation of fixed versions of the array itself. */
8419 index_type_desc = NULL;
8420 }
8421
14f9c5c9
AS
8422 if (index_type_desc == NULL)
8423 {
27710edb 8424 struct type *elt_type0 = ada_check_typedef (type0->target_type ());
5b4ee69b 8425
14f9c5c9 8426 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8427 depend on the contents of the array in properly constructed
8428 debugging data. */
529cad9c 8429 /* Create a fixed version of the array element type.
dda83cd7
SM
8430 We're not providing the address of an element here,
8431 and thus the actual object value cannot be inspected to do
8432 the conversion. This should not be a problem, since arrays of
8433 unconstrained objects are not allowed. In particular, all
8434 the elements of an array of a tagged type should all be of
8435 the same type specified in the debugging info. No need to
8436 consult the object tag. */
1ed6ede0 8437 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8438
284614f0
JB
8439 /* Make sure we always create a new array type when dealing with
8440 packed array types, since we're going to fix-up the array
8441 type length and element bitsize a little further down. */
ad82864c 8442 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8443 result = type0;
14f9c5c9 8444 else
9e76b17a
TT
8445 {
8446 type_allocator alloc (type0);
8447 result = create_array_type (alloc, elt_type, type0->index_type ());
8448 }
14f9c5c9
AS
8449 }
8450 else
8451 {
8452 int i;
8453 struct type *elt_type0;
8454
8455 elt_type0 = type0;
1f704f76 8456 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
27710edb 8457 elt_type0 = elt_type0->target_type ();
14f9c5c9
AS
8458
8459 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8460 depend on the contents of the array in properly constructed
8461 debugging data. */
529cad9c 8462 /* Create a fixed version of the array element type.
dda83cd7
SM
8463 We're not providing the address of an element here,
8464 and thus the actual object value cannot be inspected to do
8465 the conversion. This should not be a problem, since arrays of
8466 unconstrained objects are not allowed. In particular, all
8467 the elements of an array of a tagged type should all be of
8468 the same type specified in the debugging info. No need to
8469 consult the object tag. */
1ed6ede0 8470 result =
dda83cd7 8471 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8472
8473 elt_type0 = type0;
1f704f76 8474 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8475 {
8476 struct type *range_type =
8477 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8478
9e76b17a
TT
8479 type_allocator alloc (elt_type0);
8480 result = create_array_type (alloc, result, range_type);
27710edb 8481 elt_type0 = elt_type0->target_type ();
dda83cd7 8482 }
14f9c5c9
AS
8483 }
8484
2e6fda7d
JB
8485 /* We want to preserve the type name. This can be useful when
8486 trying to get the type name of a value that has already been
8487 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8488 result->set_name (type0->name ());
2e6fda7d 8489
ad82864c 8490 if (constrained_packed_array_p)
284614f0
JB
8491 {
8492 /* So far, the resulting type has been created as if the original
8493 type was a regular (non-packed) array type. As a result, the
8494 bitsize of the array elements needs to be set again, and the array
8495 length needs to be recomputed based on that bitsize. */
df86565b 8496 int len = result->length () / result->target_type ()->length ();
284614f0
JB
8497 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8498
8499 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
b6cdbc9a 8500 result->set_length (len * elt_bitsize / HOST_CHAR_BIT);
df86565b
SM
8501 if (result->length () * HOST_CHAR_BIT < len * elt_bitsize)
8502 result->set_length (result->length () + 1);
284614f0
JB
8503 }
8504
9cdd0d12 8505 result->set_is_fixed_instance (true);
14f9c5c9 8506 return result;
d2e4a39e 8507}
14f9c5c9
AS
8508
8509
8510/* A standard type (containing no dynamically sized components)
8511 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8512 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8513 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8514 ADDRESS or in VALADDR contains these discriminants.
8515
1ed6ede0
JB
8516 If CHECK_TAG is not null, in the case of tagged types, this function
8517 attempts to locate the object's tag and use it to compute the actual
8518 type. However, when ADDRESS is null, we cannot use it to determine the
8519 location of the tag, and therefore compute the tagged type's actual type.
8520 So we return the tagged type without consulting the tag. */
529cad9c 8521
f192137b
JB
8522static struct type *
8523ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8524 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8525{
61ee279c 8526 type = ada_check_typedef (type);
8ecb59f8
TT
8527
8528 /* Only un-fixed types need to be handled here. */
8529 if (!HAVE_GNAT_AUX_INFO (type))
8530 return type;
8531
78134374 8532 switch (type->code ())
d2e4a39e
AS
8533 {
8534 default:
14f9c5c9 8535 return type;
d2e4a39e 8536 case TYPE_CODE_STRUCT:
4c4b4cd2 8537 {
dda83cd7
SM
8538 struct type *static_type = to_static_fixed_type (type);
8539 struct type *fixed_record_type =
8540 to_fixed_record_type (type, valaddr, address, NULL);
8541
8542 /* If STATIC_TYPE is a tagged type and we know the object's address,
8543 then we can determine its tag, and compute the object's actual
8544 type from there. Note that we have to use the fixed record
8545 type (the parent part of the record may have dynamic fields
8546 and the way the location of _tag is expressed may depend on
8547 them). */
8548
8549 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8550 {
b50d69b5
JG
8551 struct value *tag =
8552 value_tag_from_contents_and_address
8553 (fixed_record_type,
8554 valaddr,
8555 address);
8556 struct type *real_type = type_from_tag (tag);
8557 struct value *obj =
8558 value_from_contents_and_address (fixed_record_type,
8559 valaddr,
8560 address);
d0c97917 8561 fixed_record_type = obj->type ();
dda83cd7
SM
8562 if (real_type != NULL)
8563 return to_fixed_record_type
b50d69b5 8564 (real_type, NULL,
9feb2d07 8565 ada_tag_value_at_base_address (obj)->address (), NULL);
dda83cd7
SM
8566 }
8567
8568 /* Check to see if there is a parallel ___XVZ variable.
8569 If there is, then it provides the actual size of our type. */
8570 else if (ada_type_name (fixed_record_type) != NULL)
8571 {
8572 const char *name = ada_type_name (fixed_record_type);
8573 char *xvz_name
224c3ddb 8574 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8575 bool xvz_found = false;
dda83cd7 8576 LONGEST size;
4af88198 8577
dda83cd7 8578 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8579 try
eccab96d
JB
8580 {
8581 xvz_found = get_int_var_value (xvz_name, size);
8582 }
230d2906 8583 catch (const gdb_exception_error &except)
eccab96d
JB
8584 {
8585 /* We found the variable, but somehow failed to read
8586 its value. Rethrow the same error, but with a little
8587 bit more information, to help the user understand
8588 what went wrong (Eg: the variable might have been
8589 optimized out). */
8590 throw_error (except.error,
8591 _("unable to read value of %s (%s)"),
3d6e9d23 8592 xvz_name, except.what ());
eccab96d 8593 }
eccab96d 8594
df86565b 8595 if (xvz_found && fixed_record_type->length () != size)
dda83cd7
SM
8596 {
8597 fixed_record_type = copy_type (fixed_record_type);
b6cdbc9a 8598 fixed_record_type->set_length (size);
dda83cd7
SM
8599
8600 /* The FIXED_RECORD_TYPE may have be a stub. We have
8601 observed this when the debugging info is STABS, and
8602 apparently it is something that is hard to fix.
8603
8604 In practice, we don't need the actual type definition
8605 at all, because the presence of the XVZ variable allows us
8606 to assume that there must be a XVS type as well, which we
8607 should be able to use later, when we need the actual type
8608 definition.
8609
8610 In the meantime, pretend that the "fixed" type we are
8611 returning is NOT a stub, because this can cause trouble
8612 when using this type to create new types targeting it.
8613 Indeed, the associated creation routines often check
8614 whether the target type is a stub and will try to replace
8615 it, thus using a type with the wrong size. This, in turn,
8616 might cause the new type to have the wrong size too.
8617 Consider the case of an array, for instance, where the size
8618 of the array is computed from the number of elements in
8619 our array multiplied by the size of its element. */
b4b73759 8620 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8621 }
8622 }
8623 return fixed_record_type;
4c4b4cd2 8624 }
d2e4a39e 8625 case TYPE_CODE_ARRAY:
4c4b4cd2 8626 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8627 case TYPE_CODE_UNION:
8628 if (dval == NULL)
dda83cd7 8629 return type;
d2e4a39e 8630 else
dda83cd7 8631 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8632 }
14f9c5c9
AS
8633}
8634
f192137b
JB
8635/* The same as ada_to_fixed_type_1, except that it preserves the type
8636 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8637
8638 The typedef layer needs be preserved in order to differentiate between
8639 arrays and array pointers when both types are implemented using the same
8640 fat pointer. In the array pointer case, the pointer is encoded as
8641 a typedef of the pointer type. For instance, considering:
8642
8643 type String_Access is access String;
8644 S1 : String_Access := null;
8645
8646 To the debugger, S1 is defined as a typedef of type String. But
8647 to the user, it is a pointer. So if the user tries to print S1,
8648 we should not dereference the array, but print the array address
8649 instead.
8650
8651 If we didn't preserve the typedef layer, we would lose the fact that
8652 the type is to be presented as a pointer (needs de-reference before
8653 being printed). And we would also use the source-level type name. */
f192137b
JB
8654
8655struct type *
8656ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8657 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8658
8659{
8660 struct type *fixed_type =
8661 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8662
96dbd2c1
JB
8663 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8664 then preserve the typedef layer.
8665
8666 Implementation note: We can only check the main-type portion of
8667 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8668 from TYPE now returns a type that has the same instance flags
8669 as TYPE. For instance, if TYPE is a "typedef const", and its
8670 target type is a "struct", then the typedef elimination will return
8671 a "const" version of the target type. See check_typedef for more
8672 details about how the typedef layer elimination is done.
8673
8674 brobecker/2010-11-19: It seems to me that the only case where it is
8675 useful to preserve the typedef layer is when dealing with fat pointers.
8676 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8677 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8678 because we call check_typedef/ada_check_typedef pretty much everywhere.
8679 */
78134374 8680 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8681 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8682 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8683 return type;
8684
8685 return fixed_type;
8686}
8687
14f9c5c9 8688/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8689 TYPE0, but based on no runtime data. */
14f9c5c9 8690
d2e4a39e
AS
8691static struct type *
8692to_static_fixed_type (struct type *type0)
14f9c5c9 8693{
d2e4a39e 8694 struct type *type;
14f9c5c9
AS
8695
8696 if (type0 == NULL)
8697 return NULL;
8698
22c4c60c 8699 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8700 return type0;
8701
61ee279c 8702 type0 = ada_check_typedef (type0);
d2e4a39e 8703
78134374 8704 switch (type0->code ())
14f9c5c9
AS
8705 {
8706 default:
8707 return type0;
8708 case TYPE_CODE_STRUCT:
8709 type = dynamic_template_type (type0);
d2e4a39e 8710 if (type != NULL)
dda83cd7 8711 return template_to_static_fixed_type (type);
4c4b4cd2 8712 else
dda83cd7 8713 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8714 case TYPE_CODE_UNION:
8715 type = ada_find_parallel_type (type0, "___XVU");
8716 if (type != NULL)
dda83cd7 8717 return template_to_static_fixed_type (type);
4c4b4cd2 8718 else
dda83cd7 8719 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8720 }
8721}
8722
4c4b4cd2
PH
8723/* A static approximation of TYPE with all type wrappers removed. */
8724
d2e4a39e
AS
8725static struct type *
8726static_unwrap_type (struct type *type)
14f9c5c9
AS
8727{
8728 if (ada_is_aligner_type (type))
8729 {
940da03e 8730 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8731 if (ada_type_name (type1) == NULL)
d0e39ea2 8732 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8733
8734 return static_unwrap_type (type1);
8735 }
d2e4a39e 8736 else
14f9c5c9 8737 {
d2e4a39e 8738 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8739
d2e4a39e 8740 if (raw_real_type == type)
dda83cd7 8741 return type;
14f9c5c9 8742 else
dda83cd7 8743 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8744 }
8745}
8746
8747/* In some cases, incomplete and private types require
4c4b4cd2 8748 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8749 type Foo;
8750 type FooP is access Foo;
8751 V: FooP;
8752 type Foo is array ...;
4c4b4cd2 8753 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8754 cross-references to such types, we instead substitute for FooP a
8755 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8756 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8757
8758/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8759 exists, otherwise TYPE. */
8760
d2e4a39e 8761struct type *
61ee279c 8762ada_check_typedef (struct type *type)
14f9c5c9 8763{
727e3d2e
JB
8764 if (type == NULL)
8765 return NULL;
8766
736ade86
XR
8767 /* If our type is an access to an unconstrained array, which is encoded
8768 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8769 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8770 what allows us to distinguish between fat pointers that represent
8771 array types, and fat pointers that represent array access types
8772 (in both cases, the compiler implements them as fat pointers). */
736ade86 8773 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8774 return type;
8775
f168693b 8776 type = check_typedef (type);
78134374 8777 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8778 || !type->is_stub ()
7d93a1e0 8779 || type->name () == NULL)
14f9c5c9 8780 return type;
d2e4a39e 8781 else
14f9c5c9 8782 {
7d93a1e0 8783 const char *name = type->name ();
d2e4a39e 8784 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8785
05e522ef 8786 if (type1 == NULL)
dda83cd7 8787 return type;
05e522ef
JB
8788
8789 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8790 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8791 types, only for the typedef-to-array types). If that's the case,
8792 strip the typedef layer. */
78134374 8793 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8794 type1 = ada_check_typedef (type1);
8795
8796 return type1;
14f9c5c9
AS
8797 }
8798}
8799
8800/* A value representing the data at VALADDR/ADDRESS as described by
8801 type TYPE0, but with a standard (static-sized) type that correctly
8802 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8803 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8804 creation of struct values]. */
14f9c5c9 8805
4c4b4cd2
PH
8806static struct value *
8807ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8808 struct value *val0)
14f9c5c9 8809{
1ed6ede0 8810 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8811
14f9c5c9
AS
8812 if (type == type0 && val0 != NULL)
8813 return val0;
cc0e770c 8814
736355f2 8815 if (val0->lval () != lval_memory)
cc0e770c
JB
8816 {
8817 /* Our value does not live in memory; it could be a convenience
8818 variable, for instance. Create a not_lval value using val0's
8819 contents. */
efaf1ae0 8820 return value_from_contents (type, val0->contents ().data ());
cc0e770c
JB
8821 }
8822
8823 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8824}
8825
8826/* A value representing VAL, but with a standard (static-sized) type
8827 that correctly describes it. Does not necessarily create a new
8828 value. */
8829
0c3acc09 8830struct value *
4c4b4cd2
PH
8831ada_to_fixed_value (struct value *val)
8832{
c48db5ca 8833 val = unwrap_value (val);
9feb2d07 8834 val = ada_to_fixed_value_create (val->type (), val->address (), val);
c48db5ca 8835 return val;
14f9c5c9 8836}
d2e4a39e 8837\f
14f9c5c9 8838
14f9c5c9
AS
8839/* Attributes */
8840
4c4b4cd2
PH
8841/* Table mapping attribute numbers to names.
8842 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8843
27087b7f 8844static const char * const attribute_names[] = {
14f9c5c9
AS
8845 "<?>",
8846
d2e4a39e 8847 "first",
14f9c5c9
AS
8848 "last",
8849 "length",
8850 "image",
14f9c5c9
AS
8851 "max",
8852 "min",
4c4b4cd2
PH
8853 "modulus",
8854 "pos",
8855 "size",
8856 "tag",
14f9c5c9 8857 "val",
14f9c5c9
AS
8858 0
8859};
8860
de93309a 8861static const char *
4c4b4cd2 8862ada_attribute_name (enum exp_opcode n)
14f9c5c9 8863{
4c4b4cd2
PH
8864 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8865 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8866 else
8867 return attribute_names[0];
8868}
8869
4c4b4cd2 8870/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8871
4c4b4cd2
PH
8872static LONGEST
8873pos_atr (struct value *arg)
14f9c5c9 8874{
24209737 8875 struct value *val = coerce_ref (arg);
d0c97917 8876 struct type *type = val->type ();
14f9c5c9 8877
d2e4a39e 8878 if (!discrete_type_p (type))
323e0a4a 8879 error (_("'POS only defined on discrete types"));
14f9c5c9 8880
6244c119
SM
8881 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8882 if (!result.has_value ())
aa715135 8883 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8884
6244c119 8885 return *result;
4c4b4cd2
PH
8886}
8887
7631cf6c 8888struct value *
7992accc
TT
8889ada_pos_atr (struct type *expect_type,
8890 struct expression *exp,
8891 enum noside noside, enum exp_opcode op,
8892 struct value *arg)
4c4b4cd2 8893{
7992accc
TT
8894 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8895 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8896 return value::zero (type, not_lval);
3cb382c9 8897 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8898}
8899
4c4b4cd2 8900/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8901
d2e4a39e 8902static struct value *
53a47a3e 8903val_atr (struct type *type, LONGEST val)
14f9c5c9 8904{
53a47a3e 8905 gdb_assert (discrete_type_p (type));
0bc2354b 8906 if (type->code () == TYPE_CODE_RANGE)
27710edb 8907 type = type->target_type ();
78134374 8908 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8909 {
53a47a3e 8910 if (val < 0 || val >= type->num_fields ())
dda83cd7 8911 error (_("argument to 'VAL out of range"));
970db518 8912 val = type->field (val).loc_enumval ();
14f9c5c9 8913 }
53a47a3e
TT
8914 return value_from_longest (type, val);
8915}
8916
9e99f48f 8917struct value *
3848abd6 8918ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8919{
3848abd6 8920 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8921 return value::zero (type, not_lval);
3848abd6 8922
53a47a3e
TT
8923 if (!discrete_type_p (type))
8924 error (_("'VAL only defined on discrete types"));
d0c97917 8925 if (!integer_type_p (arg->type ()))
53a47a3e
TT
8926 error (_("'VAL requires integral argument"));
8927
8928 return val_atr (type, value_as_long (arg));
14f9c5c9 8929}
14f9c5c9 8930\f
d2e4a39e 8931
dda83cd7 8932 /* Evaluation */
14f9c5c9 8933
4c4b4cd2
PH
8934/* True if TYPE appears to be an Ada character type.
8935 [At the moment, this is true only for Character and Wide_Character;
8936 It is a heuristic test that could stand improvement]. */
14f9c5c9 8937
fc913e53 8938bool
d2e4a39e 8939ada_is_character_type (struct type *type)
14f9c5c9 8940{
7b9f71f2
JB
8941 const char *name;
8942
8943 /* If the type code says it's a character, then assume it really is,
8944 and don't check any further. */
78134374 8945 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8946 return true;
7b9f71f2
JB
8947
8948 /* Otherwise, assume it's a character type iff it is a discrete type
8949 with a known character type name. */
8950 name = ada_type_name (type);
8951 return (name != NULL
dda83cd7
SM
8952 && (type->code () == TYPE_CODE_INT
8953 || type->code () == TYPE_CODE_RANGE)
8954 && (strcmp (name, "character") == 0
8955 || strcmp (name, "wide_character") == 0
8956 || strcmp (name, "wide_wide_character") == 0
8957 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8958}
8959
4c4b4cd2 8960/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8961
fc913e53 8962bool
ebf56fd3 8963ada_is_string_type (struct type *type)
14f9c5c9 8964{
61ee279c 8965 type = ada_check_typedef (type);
d2e4a39e 8966 if (type != NULL
78134374 8967 && type->code () != TYPE_CODE_PTR
76a01679 8968 && (ada_is_simple_array_type (type)
dda83cd7 8969 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8970 && ada_array_arity (type) == 1)
8971 {
8972 struct type *elttype = ada_array_element_type (type, 1);
8973
8974 return ada_is_character_type (elttype);
8975 }
d2e4a39e 8976 else
fc913e53 8977 return false;
14f9c5c9
AS
8978}
8979
5bf03f13
JB
8980/* The compiler sometimes provides a parallel XVS type for a given
8981 PAD type. Normally, it is safe to follow the PAD type directly,
8982 but older versions of the compiler have a bug that causes the offset
8983 of its "F" field to be wrong. Following that field in that case
8984 would lead to incorrect results, but this can be worked around
8985 by ignoring the PAD type and using the associated XVS type instead.
8986
8987 Set to True if the debugger should trust the contents of PAD types.
8988 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8989static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8990
8991/* True if TYPE is a struct type introduced by the compiler to force the
8992 alignment of a value. Such types have a single field with a
4c4b4cd2 8993 distinctive name. */
14f9c5c9
AS
8994
8995int
ebf56fd3 8996ada_is_aligner_type (struct type *type)
14f9c5c9 8997{
61ee279c 8998 type = ada_check_typedef (type);
714e53ab 8999
5bf03f13 9000 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
9001 return 0;
9002
78134374 9003 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 9004 && type->num_fields () == 1
33d16dd9 9005 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
9006}
9007
9008/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 9009 the parallel type. */
14f9c5c9 9010
d2e4a39e
AS
9011struct type *
9012ada_get_base_type (struct type *raw_type)
14f9c5c9 9013{
d2e4a39e
AS
9014 struct type *real_type_namer;
9015 struct type *raw_real_type;
14f9c5c9 9016
78134374 9017 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
9018 return raw_type;
9019
284614f0
JB
9020 if (ada_is_aligner_type (raw_type))
9021 /* The encoding specifies that we should always use the aligner type.
9022 So, even if this aligner type has an associated XVS type, we should
9023 simply ignore it.
9024
9025 According to the compiler gurus, an XVS type parallel to an aligner
9026 type may exist because of a stabs limitation. In stabs, aligner
9027 types are empty because the field has a variable-sized type, and
9028 thus cannot actually be used as an aligner type. As a result,
9029 we need the associated parallel XVS type to decode the type.
9030 Since the policy in the compiler is to not change the internal
9031 representation based on the debugging info format, we sometimes
9032 end up having a redundant XVS type parallel to the aligner type. */
9033 return raw_type;
9034
14f9c5c9 9035 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9036 if (real_type_namer == NULL
78134374 9037 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 9038 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
9039 return raw_type;
9040
940da03e 9041 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
9042 {
9043 /* This is an older encoding form where the base type needs to be
85102364 9044 looked up by name. We prefer the newer encoding because it is
f80d3ff2 9045 more efficient. */
33d16dd9 9046 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
9047 if (raw_real_type == NULL)
9048 return raw_type;
9049 else
9050 return raw_real_type;
9051 }
9052
9053 /* The field in our XVS type is a reference to the base type. */
27710edb 9054 return real_type_namer->field (0).type ()->target_type ();
d2e4a39e 9055}
14f9c5c9 9056
4c4b4cd2 9057/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9058
d2e4a39e
AS
9059struct type *
9060ada_aligned_type (struct type *type)
14f9c5c9
AS
9061{
9062 if (ada_is_aligner_type (type))
940da03e 9063 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
9064 else
9065 return ada_get_base_type (type);
9066}
9067
9068
9069/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9070 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9071
fc1a4b47
AC
9072const gdb_byte *
9073ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9074{
d2e4a39e 9075 if (ada_is_aligner_type (type))
b610c045
SM
9076 return ada_aligned_value_addr
9077 (type->field (0).type (),
9078 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
9079 else
9080 return valaddr;
9081}
9082
4c4b4cd2
PH
9083
9084
14f9c5c9 9085/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9086 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9087const char *
9088ada_enum_name (const char *name)
14f9c5c9 9089{
5f9febe0 9090 static std::string storage;
e6a959d6 9091 const char *tmp;
14f9c5c9 9092
4c4b4cd2
PH
9093 /* First, unqualify the enumeration name:
9094 1. Search for the last '.' character. If we find one, then skip
177b42fe 9095 all the preceding characters, the unqualified name starts
76a01679 9096 right after that dot.
4c4b4cd2 9097 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9098 translates dots into "__". Search forward for double underscores,
9099 but stop searching when we hit an overloading suffix, which is
9100 of the form "__" followed by digits. */
4c4b4cd2 9101
c3e5cd34
PH
9102 tmp = strrchr (name, '.');
9103 if (tmp != NULL)
4c4b4cd2
PH
9104 name = tmp + 1;
9105 else
14f9c5c9 9106 {
4c4b4cd2 9107 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9108 {
9109 if (isdigit (tmp[2]))
9110 break;
9111 else
9112 name = tmp + 2;
9113 }
14f9c5c9
AS
9114 }
9115
9116 if (name[0] == 'Q')
9117 {
14f9c5c9 9118 int v;
5b4ee69b 9119
14f9c5c9 9120 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9121 {
a7041de8
TT
9122 int offset = 2;
9123 if (name[1] == 'W' && name[2] == 'W')
9124 {
9125 /* Also handle the QWW case. */
9126 ++offset;
9127 }
9128 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9129 return name;
9130 }
272560b5
TT
9131 else if (((name[1] >= '0' && name[1] <= '9')
9132 || (name[1] >= 'a' && name[1] <= 'z'))
9133 && name[2] == '\0')
9134 {
5f9febe0
TT
9135 storage = string_printf ("'%c'", name[1]);
9136 return storage.c_str ();
272560b5 9137 }
14f9c5c9 9138 else
dda83cd7 9139 return name;
14f9c5c9
AS
9140
9141 if (isascii (v) && isprint (v))
5f9febe0 9142 storage = string_printf ("'%c'", v);
14f9c5c9 9143 else if (name[1] == 'U')
a7041de8
TT
9144 storage = string_printf ("'[\"%02x\"]'", v);
9145 else if (name[2] != 'W')
9146 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9147 else
a7041de8 9148 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9149
5f9febe0 9150 return storage.c_str ();
14f9c5c9 9151 }
d2e4a39e 9152 else
4c4b4cd2 9153 {
c3e5cd34
PH
9154 tmp = strstr (name, "__");
9155 if (tmp == NULL)
9156 tmp = strstr (name, "$");
9157 if (tmp != NULL)
dda83cd7 9158 {
5f9febe0
TT
9159 storage = std::string (name, tmp - name);
9160 return storage.c_str ();
dda83cd7 9161 }
4c4b4cd2
PH
9162
9163 return name;
9164 }
14f9c5c9
AS
9165}
9166
013a623f
TT
9167/* If TYPE is a dynamic type, return the base type. Otherwise, if
9168 there is no parallel type, return nullptr. */
9169
9170static struct type *
9171find_base_type (struct type *type)
9172{
9173 struct type *raw_real_type
9174 = ada_check_typedef (ada_get_base_type (type));
9175
9176 /* No parallel XVS or XVE type. */
9177 if (type == raw_real_type
9178 && ada_find_parallel_type (type, "___XVE") == nullptr)
9179 return nullptr;
9180
9181 return raw_real_type;
9182}
9183
14f9c5c9 9184/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9185 value it wraps. */
14f9c5c9 9186
d2e4a39e
AS
9187static struct value *
9188unwrap_value (struct value *val)
14f9c5c9 9189{
d0c97917 9190 struct type *type = ada_check_typedef (val->type ());
5b4ee69b 9191
14f9c5c9
AS
9192 if (ada_is_aligner_type (type))
9193 {
de4d072f 9194 struct value *v = ada_value_struct_elt (val, "F", 0);
d0c97917 9195 struct type *val_type = ada_check_typedef (v->type ());
5b4ee69b 9196
14f9c5c9 9197 if (ada_type_name (val_type) == NULL)
d0e39ea2 9198 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9199
9200 return unwrap_value (v);
9201 }
d2e4a39e 9202 else
14f9c5c9 9203 {
013a623f
TT
9204 struct type *raw_real_type = find_base_type (type);
9205 if (raw_real_type == nullptr)
5bf03f13 9206 return val;
14f9c5c9 9207
d2e4a39e 9208 return
dda83cd7
SM
9209 coerce_unspec_val_to_type
9210 (val, ada_to_fixed_type (raw_real_type, 0,
9feb2d07 9211 val->address (),
dda83cd7 9212 NULL, 1));
14f9c5c9
AS
9213 }
9214}
d2e4a39e 9215
d99dcf51
JB
9216/* Given two array types T1 and T2, return nonzero iff both arrays
9217 contain the same number of elements. */
9218
9219static int
9220ada_same_array_size_p (struct type *t1, struct type *t2)
9221{
9222 LONGEST lo1, hi1, lo2, hi2;
9223
9224 /* Get the array bounds in order to verify that the size of
9225 the two arrays match. */
9226 if (!get_array_bounds (t1, &lo1, &hi1)
9227 || !get_array_bounds (t2, &lo2, &hi2))
9228 error (_("unable to determine array bounds"));
9229
9230 /* To make things easier for size comparison, normalize a bit
9231 the case of empty arrays by making sure that the difference
9232 between upper bound and lower bound is always -1. */
9233 if (lo1 > hi1)
9234 hi1 = lo1 - 1;
9235 if (lo2 > hi2)
9236 hi2 = lo2 - 1;
9237
9238 return (hi1 - lo1 == hi2 - lo2);
9239}
9240
9241/* Assuming that VAL is an array of integrals, and TYPE represents
9242 an array with the same number of elements, but with wider integral
9243 elements, return an array "casted" to TYPE. In practice, this
9244 means that the returned array is built by casting each element
9245 of the original array into TYPE's (wider) element type. */
9246
9247static struct value *
9248ada_promote_array_of_integrals (struct type *type, struct value *val)
9249{
27710edb 9250 struct type *elt_type = type->target_type ();
d99dcf51 9251 LONGEST lo, hi;
d99dcf51
JB
9252 LONGEST i;
9253
9254 /* Verify that both val and type are arrays of scalars, and
9255 that the size of val's elements is smaller than the size
9256 of type's element. */
78134374 9257 gdb_assert (type->code () == TYPE_CODE_ARRAY);
27710edb 9258 gdb_assert (is_integral_type (type->target_type ()));
d0c97917
TT
9259 gdb_assert (val->type ()->code () == TYPE_CODE_ARRAY);
9260 gdb_assert (is_integral_type (val->type ()->target_type ()));
df86565b 9261 gdb_assert (type->target_type ()->length ()
d0c97917 9262 > val->type ()->target_type ()->length ());
d99dcf51
JB
9263
9264 if (!get_array_bounds (type, &lo, &hi))
9265 error (_("unable to determine array bounds"));
9266
317c3ed9 9267 value *res = value::allocate (type);
bbe912ba 9268 gdb::array_view<gdb_byte> res_contents = res->contents_writeable ();
d99dcf51
JB
9269
9270 /* Promote each array element. */
9271 for (i = 0; i < hi - lo + 1; i++)
9272 {
9273 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
df86565b 9274 int elt_len = elt_type->length ();
d99dcf51 9275
efaf1ae0 9276 copy (elt->contents_all (), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9277 }
9278
9279 return res;
9280}
9281
4c4b4cd2
PH
9282/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9283 return the converted value. */
9284
d2e4a39e
AS
9285static struct value *
9286coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9287{
d0c97917 9288 struct type *type2 = val->type ();
5b4ee69b 9289
14f9c5c9
AS
9290 if (type == type2)
9291 return val;
9292
61ee279c
PH
9293 type2 = ada_check_typedef (type2);
9294 type = ada_check_typedef (type);
14f9c5c9 9295
78134374
SM
9296 if (type2->code () == TYPE_CODE_PTR
9297 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9298 {
9299 val = ada_value_ind (val);
d0c97917 9300 type2 = val->type ();
14f9c5c9
AS
9301 }
9302
78134374
SM
9303 if (type2->code () == TYPE_CODE_ARRAY
9304 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9305 {
d99dcf51
JB
9306 if (!ada_same_array_size_p (type, type2))
9307 error (_("cannot assign arrays of different length"));
9308
27710edb
SM
9309 if (is_integral_type (type->target_type ())
9310 && is_integral_type (type2->target_type ())
df86565b 9311 && type2->target_type ()->length () < type->target_type ()->length ())
d99dcf51
JB
9312 {
9313 /* Allow implicit promotion of the array elements to
9314 a wider type. */
9315 return ada_promote_array_of_integrals (type, val);
9316 }
9317
df86565b 9318 if (type2->target_type ()->length () != type->target_type ()->length ())
dda83cd7 9319 error (_("Incompatible types in assignment"));
81ae560c 9320 val->deprecated_set_type (type);
14f9c5c9 9321 }
d2e4a39e 9322 return val;
14f9c5c9
AS
9323}
9324
4c4b4cd2
PH
9325static struct value *
9326ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9327{
4c4b4cd2 9328 struct type *type1, *type2;
4c4b4cd2 9329
994b9211
AC
9330 arg1 = coerce_ref (arg1);
9331 arg2 = coerce_ref (arg2);
d0c97917
TT
9332 type1 = get_base_type (ada_check_typedef (arg1->type ()));
9333 type2 = get_base_type (ada_check_typedef (arg2->type ()));
4c4b4cd2 9334
78134374
SM
9335 if (type1->code () != TYPE_CODE_INT
9336 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9337 return value_binop (arg1, arg2, op);
9338
76a01679 9339 switch (op)
4c4b4cd2
PH
9340 {
9341 case BINOP_MOD:
9342 case BINOP_DIV:
9343 case BINOP_REM:
9344 break;
9345 default:
9346 return value_binop (arg1, arg2, op);
9347 }
9348
70050808
TT
9349 gdb_mpz v2 = value_as_mpz (arg2);
9350 if (v2.sgn () == 0)
b0f9164c
TT
9351 {
9352 const char *name;
9353 if (op == BINOP_MOD)
9354 name = "mod";
9355 else if (op == BINOP_DIV)
9356 name = "/";
9357 else
9358 {
9359 gdb_assert (op == BINOP_REM);
9360 name = "rem";
9361 }
9362
9363 error (_("second operand of %s must not be zero."), name);
9364 }
4c4b4cd2 9365
c6d940a9 9366 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9367 return value_binop (arg1, arg2, op);
9368
70050808
TT
9369 gdb_mpz v1 = value_as_mpz (arg1);
9370 gdb_mpz v;
4c4b4cd2
PH
9371 switch (op)
9372 {
9373 case BINOP_DIV:
9374 v = v1 / v2;
4c4b4cd2
PH
9375 break;
9376 case BINOP_REM:
9377 v = v1 % v2;
76a01679 9378 if (v * v1 < 0)
dda83cd7 9379 v -= v2;
4c4b4cd2
PH
9380 break;
9381 default:
9382 /* Should not reach this point. */
70050808 9383 gdb_assert_not_reached ("invalid operator");
4c4b4cd2
PH
9384 }
9385
70050808 9386 return value_from_mpz (type1, v);
4c4b4cd2
PH
9387}
9388
9389static int
9390ada_value_equal (struct value *arg1, struct value *arg2)
9391{
d0c97917
TT
9392 if (ada_is_direct_array_type (arg1->type ())
9393 || ada_is_direct_array_type (arg2->type ()))
4c4b4cd2 9394 {
79e8fcaa
JB
9395 struct type *arg1_type, *arg2_type;
9396
f58b38bf 9397 /* Automatically dereference any array reference before
dda83cd7 9398 we attempt to perform the comparison. */
f58b38bf
JB
9399 arg1 = ada_coerce_ref (arg1);
9400 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9401
4c4b4cd2
PH
9402 arg1 = ada_coerce_to_simple_array (arg1);
9403 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa 9404
d0c97917
TT
9405 arg1_type = ada_check_typedef (arg1->type ());
9406 arg2_type = ada_check_typedef (arg2->type ());
79e8fcaa 9407
78134374 9408 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9409 || arg2_type->code () != TYPE_CODE_ARRAY)
9410 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9411 /* FIXME: The following works only for types whose
dda83cd7
SM
9412 representations use all bits (no padding or undefined bits)
9413 and do not have user-defined equality. */
df86565b 9414 return (arg1_type->length () == arg2_type->length ()
efaf1ae0
TT
9415 && memcmp (arg1->contents ().data (),
9416 arg2->contents ().data (),
df86565b 9417 arg1_type->length ()) == 0);
4c4b4cd2
PH
9418 }
9419 return value_equal (arg1, arg2);
9420}
9421
d3c54a1c
TT
9422namespace expr
9423{
9424
9425bool
9426check_objfile (const std::unique_ptr<ada_component> &comp,
9427 struct objfile *objfile)
9428{
9429 return comp->uses_objfile (objfile);
9430}
9431
9432/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9433 component of LHS (a simple array or a record). Does not modify the
9434 inferior's memory, nor does it modify LHS (unless LHS ==
9435 CONTAINER). */
52ce6436
PH
9436
9437static void
9438assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9439 struct expression *exp, operation_up &arg)
52ce6436 9440{
d3c54a1c
TT
9441 scoped_value_mark mark;
9442
52ce6436 9443 struct value *elt;
d0c97917 9444 struct type *lhs_type = check_typedef (lhs->type ());
5b4ee69b 9445
78134374 9446 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9447 {
22601c15
UW
9448 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9449 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9450
52ce6436
PH
9451 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9452 }
9453 else
9454 {
d0c97917 9455 elt = ada_index_struct_field (index, lhs, 0, lhs->type ());
c48db5ca 9456 elt = ada_to_fixed_value (elt);
52ce6436
PH
9457 }
9458
d3c54a1c
TT
9459 ada_aggregate_operation *ag_op
9460 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9461 if (ag_op != nullptr)
9462 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9463 else
d3c54a1c
TT
9464 value_assign_to_component (container, elt,
9465 arg->evaluate (nullptr, exp,
9466 EVAL_NORMAL));
9467}
52ce6436 9468
d3c54a1c
TT
9469bool
9470ada_aggregate_component::uses_objfile (struct objfile *objfile)
9471{
9472 for (const auto &item : m_components)
9473 if (item->uses_objfile (objfile))
9474 return true;
9475 return false;
9476}
9477
9478void
9479ada_aggregate_component::dump (ui_file *stream, int depth)
9480{
6cb06a8c 9481 gdb_printf (stream, _("%*sAggregate\n"), depth, "");
d3c54a1c
TT
9482 for (const auto &item : m_components)
9483 item->dump (stream, depth + 1);
9484}
9485
9486void
9487ada_aggregate_component::assign (struct value *container,
9488 struct value *lhs, struct expression *exp,
9489 std::vector<LONGEST> &indices,
9490 LONGEST low, LONGEST high)
9491{
9492 for (auto &item : m_components)
9493 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9494}
9495
207582c0 9496/* See ada-exp.h. */
52ce6436 9497
207582c0 9498value *
d3c54a1c
TT
9499ada_aggregate_operation::assign_aggregate (struct value *container,
9500 struct value *lhs,
9501 struct expression *exp)
52ce6436
PH
9502{
9503 struct type *lhs_type;
52ce6436 9504 LONGEST low_index, high_index;
52ce6436
PH
9505
9506 container = ada_coerce_ref (container);
d0c97917 9507 if (ada_is_direct_array_type (container->type ()))
52ce6436
PH
9508 container = ada_coerce_to_simple_array (container);
9509 lhs = ada_coerce_ref (lhs);
4b53ca88 9510 if (!lhs->deprecated_modifiable ())
52ce6436
PH
9511 error (_("Left operand of assignment is not a modifiable lvalue."));
9512
d0c97917 9513 lhs_type = check_typedef (lhs->type ());
52ce6436
PH
9514 if (ada_is_direct_array_type (lhs_type))
9515 {
9516 lhs = ada_coerce_to_simple_array (lhs);
d0c97917 9517 lhs_type = check_typedef (lhs->type ());
cf88be68
SM
9518 low_index = lhs_type->bounds ()->low.const_val ();
9519 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9520 }
78134374 9521 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9522 {
9523 low_index = 0;
9524 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9525 }
9526 else
9527 error (_("Left-hand side must be array or record."));
9528
cf608cc4 9529 std::vector<LONGEST> indices (4);
52ce6436
PH
9530 indices[0] = indices[1] = low_index - 1;
9531 indices[2] = indices[3] = high_index + 1;
52ce6436 9532
d3c54a1c
TT
9533 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9534 low_index, high_index);
207582c0
TT
9535
9536 return container;
d3c54a1c
TT
9537}
9538
9539bool
9540ada_positional_component::uses_objfile (struct objfile *objfile)
9541{
9542 return m_op->uses_objfile (objfile);
9543}
52ce6436 9544
d3c54a1c
TT
9545void
9546ada_positional_component::dump (ui_file *stream, int depth)
9547{
6cb06a8c
TT
9548 gdb_printf (stream, _("%*sPositional, index = %d\n"),
9549 depth, "", m_index);
d3c54a1c 9550 m_op->dump (stream, depth + 1);
52ce6436 9551}
d3c54a1c 9552
52ce6436 9553/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9554 construct, given that the positions are relative to lower bound
9555 LOW, where HIGH is the upper bound. Record the position in
9556 INDICES. CONTAINER is as for assign_aggregate. */
9557void
9558ada_positional_component::assign (struct value *container,
9559 struct value *lhs, struct expression *exp,
9560 std::vector<LONGEST> &indices,
9561 LONGEST low, LONGEST high)
52ce6436 9562{
d3c54a1c
TT
9563 LONGEST ind = m_index + low;
9564
52ce6436 9565 if (ind - 1 == high)
e1d5a0d2 9566 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9567 if (ind <= high)
9568 {
cf608cc4 9569 add_component_interval (ind, ind, indices);
d3c54a1c 9570 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9571 }
52ce6436
PH
9572}
9573
d3c54a1c
TT
9574bool
9575ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9576{
9577 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9578}
9579
9580void
9581ada_discrete_range_association::dump (ui_file *stream, int depth)
9582{
6cb06a8c 9583 gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
a88c4354
TT
9584 m_low->dump (stream, depth + 1);
9585 m_high->dump (stream, depth + 1);
9586}
9587
9588void
9589ada_discrete_range_association::assign (struct value *container,
9590 struct value *lhs,
9591 struct expression *exp,
9592 std::vector<LONGEST> &indices,
9593 LONGEST low, LONGEST high,
9594 operation_up &op)
9595{
9596 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9597 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9598
9599 if (lower <= upper && (lower < low || upper > high))
9600 error (_("Index in component association out of bounds."));
9601
9602 add_component_interval (lower, upper, indices);
9603 while (lower <= upper)
9604 {
9605 assign_component (container, lhs, lower, exp, op);
9606 lower += 1;
9607 }
9608}
9609
9610bool
9611ada_name_association::uses_objfile (struct objfile *objfile)
9612{
9613 return m_val->uses_objfile (objfile);
9614}
9615
9616void
9617ada_name_association::dump (ui_file *stream, int depth)
9618{
6cb06a8c 9619 gdb_printf (stream, _("%*sName:\n"), depth, "");
a88c4354
TT
9620 m_val->dump (stream, depth + 1);
9621}
9622
9623void
9624ada_name_association::assign (struct value *container,
9625 struct value *lhs,
9626 struct expression *exp,
9627 std::vector<LONGEST> &indices,
9628 LONGEST low, LONGEST high,
9629 operation_up &op)
9630{
9631 int index;
9632
d0c97917 9633 if (ada_is_direct_array_type (lhs->type ()))
a88c4354
TT
9634 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9635 EVAL_NORMAL)));
9636 else
9637 {
9638 ada_string_operation *strop
9639 = dynamic_cast<ada_string_operation *> (m_val.get ());
9640
9641 const char *name;
9642 if (strop != nullptr)
9643 name = strop->get_name ();
9644 else
9645 {
9646 ada_var_value_operation *vvo
9647 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
9648 if (vvo != nullptr)
9649 error (_("Invalid record component association."));
9650 name = vvo->get_symbol ()->natural_name ();
9651 }
9652
9653 index = 0;
d0c97917 9654 if (! find_struct_field (name, lhs->type (), 0,
a88c4354
TT
9655 NULL, NULL, NULL, NULL, &index))
9656 error (_("Unknown component name: %s."), name);
9657 }
9658
9659 add_component_interval (index, index, indices);
9660 assign_component (container, lhs, index, exp, op);
9661}
9662
9663bool
9664ada_choices_component::uses_objfile (struct objfile *objfile)
9665{
9666 if (m_op->uses_objfile (objfile))
9667 return true;
9668 for (const auto &item : m_assocs)
9669 if (item->uses_objfile (objfile))
9670 return true;
9671 return false;
9672}
9673
9674void
9675ada_choices_component::dump (ui_file *stream, int depth)
9676{
6cb06a8c 9677 gdb_printf (stream, _("%*sChoices:\n"), depth, "");
a88c4354
TT
9678 m_op->dump (stream, depth + 1);
9679 for (const auto &item : m_assocs)
9680 item->dump (stream, depth + 1);
9681}
9682
9683/* Assign into the components of LHS indexed by the OP_CHOICES
9684 construct at *POS, updating *POS past the construct, given that
9685 the allowable indices are LOW..HIGH. Record the indices assigned
9686 to in INDICES. CONTAINER is as for assign_aggregate. */
9687void
9688ada_choices_component::assign (struct value *container,
9689 struct value *lhs, struct expression *exp,
9690 std::vector<LONGEST> &indices,
9691 LONGEST low, LONGEST high)
9692{
9693 for (auto &item : m_assocs)
9694 item->assign (container, lhs, exp, indices, low, high, m_op);
9695}
9696
9697bool
9698ada_others_component::uses_objfile (struct objfile *objfile)
9699{
9700 return m_op->uses_objfile (objfile);
9701}
9702
9703void
9704ada_others_component::dump (ui_file *stream, int depth)
9705{
6cb06a8c 9706 gdb_printf (stream, _("%*sOthers:\n"), depth, "");
a88c4354
TT
9707 m_op->dump (stream, depth + 1);
9708}
9709
9710/* Assign the value of the expression in the OP_OTHERS construct in
9711 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9712 have not been previously assigned. The index intervals already assigned
9713 are in INDICES. CONTAINER is as for assign_aggregate. */
9714void
9715ada_others_component::assign (struct value *container,
9716 struct value *lhs, struct expression *exp,
9717 std::vector<LONGEST> &indices,
9718 LONGEST low, LONGEST high)
9719{
9720 int num_indices = indices.size ();
9721 for (int i = 0; i < num_indices - 2; i += 2)
9722 {
9723 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9724 assign_component (container, lhs, ind, exp, m_op);
9725 }
9726}
9727
9728struct value *
9729ada_assign_operation::evaluate (struct type *expect_type,
9730 struct expression *exp,
9731 enum noside noside)
9732{
9733 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
9734
9735 ada_aggregate_operation *ag_op
9736 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9737 if (ag_op != nullptr)
9738 {
9739 if (noside != EVAL_NORMAL)
9740 return arg1;
9741
207582c0 9742 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9743 return ada_value_assign (arg1, arg1);
9744 }
9745 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9746 except if the lhs of our assignment is a convenience variable.
9747 In the case of assigning to a convenience variable, the lhs
9748 should be exactly the result of the evaluation of the rhs. */
d0c97917 9749 struct type *type = arg1->type ();
736355f2 9750 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9751 type = NULL;
9752 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9753 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354 9754 return arg1;
736355f2 9755 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9756 {
9757 /* Nothing. */
9758 }
9759 else
d0c97917 9760 arg2 = coerce_for_assign (arg1->type (), arg2);
a88c4354
TT
9761 return ada_value_assign (arg1, arg2);
9762}
9763
9764} /* namespace expr */
9765
cf608cc4
TT
9766/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9767 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9768 overlap. */
52ce6436
PH
9769static void
9770add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9771 std::vector<LONGEST> &indices)
52ce6436
PH
9772{
9773 int i, j;
5b4ee69b 9774
cf608cc4
TT
9775 int size = indices.size ();
9776 for (i = 0; i < size; i += 2) {
52ce6436
PH
9777 if (high >= indices[i] && low <= indices[i + 1])
9778 {
9779 int kh;
5b4ee69b 9780
cf608cc4 9781 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9782 if (high < indices[kh])
9783 break;
9784 if (low < indices[i])
9785 indices[i] = low;
9786 indices[i + 1] = indices[kh - 1];
9787 if (high > indices[i + 1])
9788 indices[i + 1] = high;
cf608cc4
TT
9789 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9790 indices.resize (kh - i - 2);
52ce6436
PH
9791 return;
9792 }
9793 else if (high < indices[i])
9794 break;
9795 }
9796
cf608cc4 9797 indices.resize (indices.size () + 2);
d4813f10 9798 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9799 indices[j] = indices[j - 2];
9800 indices[i] = low;
9801 indices[i + 1] = high;
9802}
9803
6e48bd2c
JB
9804/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9805 is different. */
9806
9807static struct value *
b7e22850 9808ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c 9809{
d0c97917 9810 if (type == ada_check_typedef (arg2->type ()))
6e48bd2c
JB
9811 return arg2;
9812
6e48bd2c
JB
9813 return value_cast (type, arg2);
9814}
9815
284614f0
JB
9816/* Evaluating Ada expressions, and printing their result.
9817 ------------------------------------------------------
9818
21649b50
JB
9819 1. Introduction:
9820 ----------------
9821
284614f0
JB
9822 We usually evaluate an Ada expression in order to print its value.
9823 We also evaluate an expression in order to print its type, which
9824 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9825 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9826 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9827 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9828 similar.
9829
9830 Evaluating expressions is a little more complicated for Ada entities
9831 than it is for entities in languages such as C. The main reason for
9832 this is that Ada provides types whose definition might be dynamic.
9833 One example of such types is variant records. Or another example
9834 would be an array whose bounds can only be known at run time.
9835
9836 The following description is a general guide as to what should be
9837 done (and what should NOT be done) in order to evaluate an expression
9838 involving such types, and when. This does not cover how the semantic
9839 information is encoded by GNAT as this is covered separatly. For the
9840 document used as the reference for the GNAT encoding, see exp_dbug.ads
9841 in the GNAT sources.
9842
9843 Ideally, we should embed each part of this description next to its
9844 associated code. Unfortunately, the amount of code is so vast right
9845 now that it's hard to see whether the code handling a particular
9846 situation might be duplicated or not. One day, when the code is
9847 cleaned up, this guide might become redundant with the comments
9848 inserted in the code, and we might want to remove it.
9849
21649b50
JB
9850 2. ``Fixing'' an Entity, the Simple Case:
9851 -----------------------------------------
9852
284614f0
JB
9853 When evaluating Ada expressions, the tricky issue is that they may
9854 reference entities whose type contents and size are not statically
9855 known. Consider for instance a variant record:
9856
9857 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9858 case Empty is
9859 when True => null;
9860 when False => Value : Integer;
9861 end case;
284614f0
JB
9862 end record;
9863 Yes : Rec := (Empty => False, Value => 1);
9864 No : Rec := (empty => True);
9865
9866 The size and contents of that record depends on the value of the
9867 descriminant (Rec.Empty). At this point, neither the debugging
9868 information nor the associated type structure in GDB are able to
9869 express such dynamic types. So what the debugger does is to create
9870 "fixed" versions of the type that applies to the specific object.
30baf67b 9871 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9872 which means creating its associated fixed type.
9873
9874 Example: when printing the value of variable "Yes" above, its fixed
9875 type would look like this:
9876
9877 type Rec is record
dda83cd7
SM
9878 Empty : Boolean;
9879 Value : Integer;
284614f0
JB
9880 end record;
9881
9882 On the other hand, if we printed the value of "No", its fixed type
9883 would become:
9884
9885 type Rec is record
dda83cd7 9886 Empty : Boolean;
284614f0
JB
9887 end record;
9888
9889 Things become a little more complicated when trying to fix an entity
9890 with a dynamic type that directly contains another dynamic type,
9891 such as an array of variant records, for instance. There are
9892 two possible cases: Arrays, and records.
9893
21649b50
JB
9894 3. ``Fixing'' Arrays:
9895 ---------------------
9896
9897 The type structure in GDB describes an array in terms of its bounds,
9898 and the type of its elements. By design, all elements in the array
9899 have the same type and we cannot represent an array of variant elements
9900 using the current type structure in GDB. When fixing an array,
9901 we cannot fix the array element, as we would potentially need one
9902 fixed type per element of the array. As a result, the best we can do
9903 when fixing an array is to produce an array whose bounds and size
9904 are correct (allowing us to read it from memory), but without having
9905 touched its element type. Fixing each element will be done later,
9906 when (if) necessary.
9907
9908 Arrays are a little simpler to handle than records, because the same
9909 amount of memory is allocated for each element of the array, even if
1b536f04 9910 the amount of space actually used by each element differs from element
21649b50 9911 to element. Consider for instance the following array of type Rec:
284614f0
JB
9912
9913 type Rec_Array is array (1 .. 2) of Rec;
9914
1b536f04
JB
9915 The actual amount of memory occupied by each element might be different
9916 from element to element, depending on the value of their discriminant.
21649b50 9917 But the amount of space reserved for each element in the array remains
1b536f04 9918 fixed regardless. So we simply need to compute that size using
21649b50
JB
9919 the debugging information available, from which we can then determine
9920 the array size (we multiply the number of elements of the array by
9921 the size of each element).
9922
9923 The simplest case is when we have an array of a constrained element
9924 type. For instance, consider the following type declarations:
9925
dda83cd7
SM
9926 type Bounded_String (Max_Size : Integer) is
9927 Length : Integer;
9928 Buffer : String (1 .. Max_Size);
9929 end record;
9930 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9931
9932 In this case, the compiler describes the array as an array of
9933 variable-size elements (identified by its XVS suffix) for which
9934 the size can be read in the parallel XVZ variable.
9935
9936 In the case of an array of an unconstrained element type, the compiler
9937 wraps the array element inside a private PAD type. This type should not
9938 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9939 that we also use the adjective "aligner" in our code to designate
9940 these wrapper types.
9941
1b536f04 9942 In some cases, the size allocated for each element is statically
21649b50
JB
9943 known. In that case, the PAD type already has the correct size,
9944 and the array element should remain unfixed.
9945
9946 But there are cases when this size is not statically known.
9947 For instance, assuming that "Five" is an integer variable:
284614f0 9948
dda83cd7
SM
9949 type Dynamic is array (1 .. Five) of Integer;
9950 type Wrapper (Has_Length : Boolean := False) is record
9951 Data : Dynamic;
9952 case Has_Length is
9953 when True => Length : Integer;
9954 when False => null;
9955 end case;
9956 end record;
9957 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9958
dda83cd7
SM
9959 Hello : Wrapper_Array := (others => (Has_Length => True,
9960 Data => (others => 17),
9961 Length => 1));
284614f0
JB
9962
9963
9964 The debugging info would describe variable Hello as being an
9965 array of a PAD type. The size of that PAD type is not statically
9966 known, but can be determined using a parallel XVZ variable.
9967 In that case, a copy of the PAD type with the correct size should
9968 be used for the fixed array.
9969
21649b50
JB
9970 3. ``Fixing'' record type objects:
9971 ----------------------------------
9972
9973 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9974 record types. In this case, in order to compute the associated
9975 fixed type, we need to determine the size and offset of each of
9976 its components. This, in turn, requires us to compute the fixed
9977 type of each of these components.
9978
9979 Consider for instance the example:
9980
dda83cd7
SM
9981 type Bounded_String (Max_Size : Natural) is record
9982 Str : String (1 .. Max_Size);
9983 Length : Natural;
9984 end record;
9985 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9986
9987 In that case, the position of field "Length" depends on the size
9988 of field Str, which itself depends on the value of the Max_Size
21649b50 9989 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9990 we need to fix the type of field Str. Therefore, fixing a variant
9991 record requires us to fix each of its components.
9992
9993 However, if a component does not have a dynamic size, the component
9994 should not be fixed. In particular, fields that use a PAD type
9995 should not fixed. Here is an example where this might happen
9996 (assuming type Rec above):
9997
9998 type Container (Big : Boolean) is record
dda83cd7
SM
9999 First : Rec;
10000 After : Integer;
10001 case Big is
10002 when True => Another : Integer;
10003 when False => null;
10004 end case;
284614f0
JB
10005 end record;
10006 My_Container : Container := (Big => False,
dda83cd7
SM
10007 First => (Empty => True),
10008 After => 42);
284614f0
JB
10009
10010 In that example, the compiler creates a PAD type for component First,
10011 whose size is constant, and then positions the component After just
10012 right after it. The offset of component After is therefore constant
10013 in this case.
10014
10015 The debugger computes the position of each field based on an algorithm
10016 that uses, among other things, the actual position and size of the field
21649b50
JB
10017 preceding it. Let's now imagine that the user is trying to print
10018 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
10019 end up computing the offset of field After based on the size of the
10020 fixed version of field First. And since in our example First has
10021 only one actual field, the size of the fixed type is actually smaller
10022 than the amount of space allocated to that field, and thus we would
10023 compute the wrong offset of field After.
10024
21649b50
JB
10025 To make things more complicated, we need to watch out for dynamic
10026 components of variant records (identified by the ___XVL suffix in
10027 the component name). Even if the target type is a PAD type, the size
10028 of that type might not be statically known. So the PAD type needs
10029 to be unwrapped and the resulting type needs to be fixed. Otherwise,
10030 we might end up with the wrong size for our component. This can be
10031 observed with the following type declarations:
284614f0 10032
dda83cd7
SM
10033 type Octal is new Integer range 0 .. 7;
10034 type Octal_Array is array (Positive range <>) of Octal;
10035 pragma Pack (Octal_Array);
284614f0 10036
dda83cd7
SM
10037 type Octal_Buffer (Size : Positive) is record
10038 Buffer : Octal_Array (1 .. Size);
10039 Length : Integer;
10040 end record;
284614f0
JB
10041
10042 In that case, Buffer is a PAD type whose size is unset and needs
10043 to be computed by fixing the unwrapped type.
10044
21649b50
JB
10045 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
10046 ----------------------------------------------------------
10047
10048 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
10049 thus far, be actually fixed?
10050
10051 The answer is: Only when referencing that element. For instance
10052 when selecting one component of a record, this specific component
10053 should be fixed at that point in time. Or when printing the value
10054 of a record, each component should be fixed before its value gets
10055 printed. Similarly for arrays, the element of the array should be
10056 fixed when printing each element of the array, or when extracting
10057 one element out of that array. On the other hand, fixing should
10058 not be performed on the elements when taking a slice of an array!
10059
31432a67 10060 Note that one of the side effects of miscomputing the offset and
284614f0
JB
10061 size of each field is that we end up also miscomputing the size
10062 of the containing type. This can have adverse results when computing
10063 the value of an entity. GDB fetches the value of an entity based
10064 on the size of its type, and thus a wrong size causes GDB to fetch
10065 the wrong amount of memory. In the case where the computed size is
10066 too small, GDB fetches too little data to print the value of our
31432a67 10067 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
10068 past the buffer containing the data =:-o. */
10069
62d4bd94
TT
10070/* A helper function for TERNOP_IN_RANGE. */
10071
10072static value *
10073eval_ternop_in_range (struct type *expect_type, struct expression *exp,
10074 enum noside noside,
10075 value *arg1, value *arg2, value *arg3)
10076{
62d4bd94
TT
10077 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10078 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10079 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10080 return
10081 value_from_longest (type,
10082 (value_less (arg1, arg3)
10083 || value_equal (arg1, arg3))
10084 && (value_less (arg2, arg1)
10085 || value_equal (arg2, arg1)));
10086}
10087
82390ab8
TT
10088/* A helper function for UNOP_NEG. */
10089
7c15d377 10090value *
82390ab8
TT
10091ada_unop_neg (struct type *expect_type,
10092 struct expression *exp,
10093 enum noside noside, enum exp_opcode op,
10094 struct value *arg1)
10095{
82390ab8
TT
10096 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10097 return value_neg (arg1);
10098}
10099
7efc87ff
TT
10100/* A helper function for UNOP_IN_RANGE. */
10101
95d49dfb 10102value *
7efc87ff
TT
10103ada_unop_in_range (struct type *expect_type,
10104 struct expression *exp,
10105 enum noside noside, enum exp_opcode op,
10106 struct value *arg1, struct type *type)
10107{
7efc87ff
TT
10108 struct value *arg2, *arg3;
10109 switch (type->code ())
10110 {
10111 default:
10112 lim_warning (_("Membership test incompletely implemented; "
10113 "always returns true"));
10114 type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10115 return value_from_longest (type, 1);
7efc87ff
TT
10116
10117 case TYPE_CODE_RANGE:
10118 arg2 = value_from_longest (type,
10119 type->bounds ()->low.const_val ());
10120 arg3 = value_from_longest (type,
10121 type->bounds ()->high.const_val ());
10122 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10123 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10124 type = language_bool_type (exp->language_defn, exp->gdbarch);
10125 return
10126 value_from_longest (type,
10127 (value_less (arg1, arg3)
10128 || value_equal (arg1, arg3))
10129 && (value_less (arg2, arg1)
10130 || value_equal (arg2, arg1)));
10131 }
10132}
10133
020dbabe
TT
10134/* A helper function for OP_ATR_TAG. */
10135
7c15d377 10136value *
020dbabe
TT
10137ada_atr_tag (struct type *expect_type,
10138 struct expression *exp,
10139 enum noside noside, enum exp_opcode op,
10140 struct value *arg1)
10141{
10142 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10143 return value::zero (ada_tag_type (arg1), not_lval);
020dbabe
TT
10144
10145 return ada_value_tag (arg1);
10146}
10147
68c75735
TT
10148/* A helper function for OP_ATR_SIZE. */
10149
7c15d377 10150value *
68c75735
TT
10151ada_atr_size (struct type *expect_type,
10152 struct expression *exp,
10153 enum noside noside, enum exp_opcode op,
10154 struct value *arg1)
10155{
d0c97917 10156 struct type *type = arg1->type ();
68c75735
TT
10157
10158 /* If the argument is a reference, then dereference its type, since
10159 the user is really asking for the size of the actual object,
10160 not the size of the pointer. */
10161 if (type->code () == TYPE_CODE_REF)
27710edb 10162 type = type->target_type ();
68c75735 10163
0b2b0b82 10164 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10165 return value::zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
68c75735
TT
10166 else
10167 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
df86565b 10168 TARGET_CHAR_BIT * type->length ());
68c75735
TT
10169}
10170
d05e24e6
TT
10171/* A helper function for UNOP_ABS. */
10172
7c15d377 10173value *
d05e24e6
TT
10174ada_abs (struct type *expect_type,
10175 struct expression *exp,
10176 enum noside noside, enum exp_opcode op,
10177 struct value *arg1)
10178{
10179 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
ee7bb294 10180 if (value_less (arg1, value::zero (arg1->type (), not_lval)))
d05e24e6
TT
10181 return value_neg (arg1);
10182 else
10183 return arg1;
10184}
10185
faa1dfd7
TT
10186/* A helper function for BINOP_MUL. */
10187
d9e7db06 10188value *
faa1dfd7
TT
10189ada_mult_binop (struct type *expect_type,
10190 struct expression *exp,
10191 enum noside noside, enum exp_opcode op,
10192 struct value *arg1, struct value *arg2)
10193{
10194 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10195 {
10196 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
ee7bb294 10197 return value::zero (arg1->type (), not_lval);
faa1dfd7
TT
10198 }
10199 else
10200 {
10201 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10202 return ada_value_binop (arg1, arg2, op);
10203 }
10204}
10205
214b13ac
TT
10206/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10207
6e8fb7b7 10208value *
214b13ac
TT
10209ada_equal_binop (struct type *expect_type,
10210 struct expression *exp,
10211 enum noside noside, enum exp_opcode op,
10212 struct value *arg1, struct value *arg2)
10213{
10214 int tem;
10215 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10216 tem = 0;
10217 else
10218 {
10219 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10220 tem = ada_value_equal (arg1, arg2);
10221 }
10222 if (op == BINOP_NOTEQUAL)
10223 tem = !tem;
10224 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
66cf9350 10225 return value_from_longest (type, tem);
214b13ac
TT
10226}
10227
5ce19db8
TT
10228/* A helper function for TERNOP_SLICE. */
10229
1b1ebfab 10230value *
5ce19db8
TT
10231ada_ternop_slice (struct expression *exp,
10232 enum noside noside,
10233 struct value *array, struct value *low_bound_val,
10234 struct value *high_bound_val)
10235{
10236 LONGEST low_bound;
10237 LONGEST high_bound;
10238
10239 low_bound_val = coerce_ref (low_bound_val);
10240 high_bound_val = coerce_ref (high_bound_val);
10241 low_bound = value_as_long (low_bound_val);
10242 high_bound = value_as_long (high_bound_val);
10243
10244 /* If this is a reference to an aligner type, then remove all
10245 the aligners. */
d0c97917
TT
10246 if (array->type ()->code () == TYPE_CODE_REF
10247 && ada_is_aligner_type (array->type ()->target_type ()))
10248 array->type ()->set_target_type
10249 (ada_aligned_type (array->type ()->target_type ()));
5ce19db8 10250
d0c97917 10251 if (ada_is_any_packed_array_type (array->type ()))
5ce19db8
TT
10252 error (_("cannot slice a packed array"));
10253
10254 /* If this is a reference to an array or an array lvalue,
10255 convert to a pointer. */
d0c97917
TT
10256 if (array->type ()->code () == TYPE_CODE_REF
10257 || (array->type ()->code () == TYPE_CODE_ARRAY
736355f2 10258 && array->lval () == lval_memory))
5ce19db8
TT
10259 array = value_addr (array);
10260
10261 if (noside == EVAL_AVOID_SIDE_EFFECTS
10262 && ada_is_array_descriptor_type (ada_check_typedef
d0c97917 10263 (array->type ())))
5ce19db8
TT
10264 return empty_array (ada_type_of_array (array, 0), low_bound,
10265 high_bound);
10266
10267 array = ada_coerce_to_simple_array_ptr (array);
10268
10269 /* If we have more than one level of pointer indirection,
10270 dereference the value until we get only one level. */
d0c97917
TT
10271 while (array->type ()->code () == TYPE_CODE_PTR
10272 && (array->type ()->target_type ()->code ()
5ce19db8
TT
10273 == TYPE_CODE_PTR))
10274 array = value_ind (array);
10275
10276 /* Make sure we really do have an array type before going further,
10277 to avoid a SEGV when trying to get the index type or the target
10278 type later down the road if the debug info generated by
10279 the compiler is incorrect or incomplete. */
d0c97917 10280 if (!ada_is_simple_array_type (array->type ()))
5ce19db8
TT
10281 error (_("cannot take slice of non-array"));
10282
d0c97917 10283 if (ada_check_typedef (array->type ())->code ()
5ce19db8
TT
10284 == TYPE_CODE_PTR)
10285 {
d0c97917 10286 struct type *type0 = ada_check_typedef (array->type ());
5ce19db8
TT
10287
10288 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
27710edb 10289 return empty_array (type0->target_type (), low_bound, high_bound);
5ce19db8
TT
10290 else
10291 {
10292 struct type *arr_type0 =
27710edb 10293 to_fixed_array_type (type0->target_type (), NULL, 1);
5ce19db8
TT
10294
10295 return ada_value_slice_from_ptr (array, arr_type0,
10296 longest_to_int (low_bound),
10297 longest_to_int (high_bound));
10298 }
10299 }
10300 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10301 return array;
10302 else if (high_bound < low_bound)
d0c97917 10303 return empty_array (array->type (), low_bound, high_bound);
5ce19db8
TT
10304 else
10305 return ada_value_slice (array, longest_to_int (low_bound),
10306 longest_to_int (high_bound));
10307}
10308
b467efaa
TT
10309/* A helper function for BINOP_IN_BOUNDS. */
10310
82c3886e 10311value *
b467efaa
TT
10312ada_binop_in_bounds (struct expression *exp, enum noside noside,
10313 struct value *arg1, struct value *arg2, int n)
10314{
10315 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10316 {
10317 struct type *type = language_bool_type (exp->language_defn,
10318 exp->gdbarch);
ee7bb294 10319 return value::zero (type, not_lval);
b467efaa
TT
10320 }
10321
d0c97917 10322 struct type *type = ada_index_type (arg2->type (), n, "range");
b467efaa 10323 if (!type)
d0c97917 10324 type = arg1->type ();
b467efaa
TT
10325
10326 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10327 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10328
10329 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10330 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10331 type = language_bool_type (exp->language_defn, exp->gdbarch);
10332 return value_from_longest (type,
10333 (value_less (arg1, arg3)
10334 || value_equal (arg1, arg3))
10335 && (value_less (arg2, arg1)
10336 || value_equal (arg2, arg1)));
10337}
10338
b84564fc
TT
10339/* A helper function for some attribute operations. */
10340
10341static value *
10342ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10343 struct value *arg1, struct type *type_arg, int tem)
10344{
10345 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10346 {
10347 if (type_arg == NULL)
d0c97917 10348 type_arg = arg1->type ();
b84564fc
TT
10349
10350 if (ada_is_constrained_packed_array_type (type_arg))
10351 type_arg = decode_constrained_packed_array_type (type_arg);
10352
10353 if (!discrete_type_p (type_arg))
10354 {
10355 switch (op)
10356 {
10357 default: /* Should never happen. */
10358 error (_("unexpected attribute encountered"));
10359 case OP_ATR_FIRST:
10360 case OP_ATR_LAST:
10361 type_arg = ada_index_type (type_arg, tem,
10362 ada_attribute_name (op));
10363 break;
10364 case OP_ATR_LENGTH:
10365 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10366 break;
10367 }
10368 }
10369
ee7bb294 10370 return value::zero (type_arg, not_lval);
b84564fc
TT
10371 }
10372 else if (type_arg == NULL)
10373 {
10374 arg1 = ada_coerce_ref (arg1);
10375
d0c97917 10376 if (ada_is_constrained_packed_array_type (arg1->type ()))
b84564fc
TT
10377 arg1 = ada_coerce_to_simple_array (arg1);
10378
10379 struct type *type;
10380 if (op == OP_ATR_LENGTH)
10381 type = builtin_type (exp->gdbarch)->builtin_int;
10382 else
10383 {
d0c97917 10384 type = ada_index_type (arg1->type (), tem,
b84564fc
TT
10385 ada_attribute_name (op));
10386 if (type == NULL)
10387 type = builtin_type (exp->gdbarch)->builtin_int;
10388 }
10389
10390 switch (op)
10391 {
10392 default: /* Should never happen. */
10393 error (_("unexpected attribute encountered"));
10394 case OP_ATR_FIRST:
10395 return value_from_longest
10396 (type, ada_array_bound (arg1, tem, 0));
10397 case OP_ATR_LAST:
10398 return value_from_longest
10399 (type, ada_array_bound (arg1, tem, 1));
10400 case OP_ATR_LENGTH:
10401 return value_from_longest
10402 (type, ada_array_length (arg1, tem));
10403 }
10404 }
10405 else if (discrete_type_p (type_arg))
10406 {
10407 struct type *range_type;
10408 const char *name = ada_type_name (type_arg);
10409
10410 range_type = NULL;
10411 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10412 range_type = to_fixed_range_type (type_arg, NULL);
10413 if (range_type == NULL)
10414 range_type = type_arg;
10415 switch (op)
10416 {
10417 default:
10418 error (_("unexpected attribute encountered"));
10419 case OP_ATR_FIRST:
10420 return value_from_longest
10421 (range_type, ada_discrete_type_low_bound (range_type));
10422 case OP_ATR_LAST:
10423 return value_from_longest
10424 (range_type, ada_discrete_type_high_bound (range_type));
10425 case OP_ATR_LENGTH:
10426 error (_("the 'length attribute applies only to array types"));
10427 }
10428 }
10429 else if (type_arg->code () == TYPE_CODE_FLT)
10430 error (_("unimplemented type attribute"));
10431 else
10432 {
10433 LONGEST low, high;
10434
10435 if (ada_is_constrained_packed_array_type (type_arg))
10436 type_arg = decode_constrained_packed_array_type (type_arg);
10437
10438 struct type *type;
10439 if (op == OP_ATR_LENGTH)
10440 type = builtin_type (exp->gdbarch)->builtin_int;
10441 else
10442 {
10443 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10444 if (type == NULL)
10445 type = builtin_type (exp->gdbarch)->builtin_int;
10446 }
10447
10448 switch (op)
10449 {
10450 default:
10451 error (_("unexpected attribute encountered"));
10452 case OP_ATR_FIRST:
10453 low = ada_array_bound_from_type (type_arg, tem, 0);
10454 return value_from_longest (type, low);
10455 case OP_ATR_LAST:
10456 high = ada_array_bound_from_type (type_arg, tem, 1);
10457 return value_from_longest (type, high);
10458 case OP_ATR_LENGTH:
10459 low = ada_array_bound_from_type (type_arg, tem, 0);
10460 high = ada_array_bound_from_type (type_arg, tem, 1);
10461 return value_from_longest (type, high - low + 1);
10462 }
10463 }
10464}
10465
38dc70cf
TT
10466/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10467
6ad3b8bf 10468struct value *
38dc70cf
TT
10469ada_binop_minmax (struct type *expect_type,
10470 struct expression *exp,
10471 enum noside noside, enum exp_opcode op,
10472 struct value *arg1, struct value *arg2)
10473{
10474 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10475 return value::zero (arg1->type (), not_lval);
38dc70cf
TT
10476 else
10477 {
10478 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10479 return value_binop (arg1, arg2, op);
38dc70cf
TT
10480 }
10481}
10482
dd5fd283
TT
10483/* A helper function for BINOP_EXP. */
10484
065ec826 10485struct value *
dd5fd283
TT
10486ada_binop_exp (struct type *expect_type,
10487 struct expression *exp,
10488 enum noside noside, enum exp_opcode op,
10489 struct value *arg1, struct value *arg2)
10490{
10491 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10492 return value::zero (arg1->type (), not_lval);
dd5fd283
TT
10493 else
10494 {
10495 /* For integer exponentiation operations,
10496 only promote the first argument. */
d0c97917 10497 if (is_integral_type (arg2->type ()))
dd5fd283
TT
10498 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10499 else
10500 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10501
10502 return value_binop (arg1, arg2, op);
10503 }
10504}
10505
03070ee9
TT
10506namespace expr
10507{
10508
8b12db26
TT
10509/* See ada-exp.h. */
10510
10511operation_up
10512ada_resolvable::replace (operation_up &&owner,
10513 struct expression *exp,
10514 bool deprocedure_p,
10515 bool parse_completion,
10516 innermost_block_tracker *tracker,
10517 struct type *context_type)
10518{
10519 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10520 return (make_operation<ada_funcall_operation>
10521 (std::move (owner),
10522 std::vector<operation_up> ()));
10523 return std::move (owner);
10524}
10525
c9f66f00 10526/* Convert the character literal whose value would be VAL to the
03adb248
TT
10527 appropriate value of type TYPE, if there is a translation.
10528 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10529 the literal 'A' (VAL == 65), returns 0. */
10530
10531static LONGEST
10532convert_char_literal (struct type *type, LONGEST val)
10533{
c9f66f00 10534 char name[12];
03adb248
TT
10535 int f;
10536
10537 if (type == NULL)
10538 return val;
10539 type = check_typedef (type);
10540 if (type->code () != TYPE_CODE_ENUM)
10541 return val;
10542
10543 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10544 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10545 else if (val >= 0 && val < 256)
10546 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10547 else if (val >= 0 && val < 0x10000)
10548 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10549 else
c9f66f00 10550 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10551 size_t len = strlen (name);
10552 for (f = 0; f < type->num_fields (); f += 1)
10553 {
10554 /* Check the suffix because an enum constant in a package will
10555 have a name like "pkg__QUxx". This is safe enough because we
10556 already have the correct type, and because mangling means
10557 there can't be clashes. */
33d16dd9 10558 const char *ename = type->field (f).name ();
03adb248
TT
10559 size_t elen = strlen (ename);
10560
10561 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10562 return type->field (f).loc_enumval ();
03adb248
TT
10563 }
10564 return val;
10565}
10566
b1b9c411
TT
10567value *
10568ada_char_operation::evaluate (struct type *expect_type,
10569 struct expression *exp,
10570 enum noside noside)
10571{
10572 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10573 if (expect_type != nullptr)
10574 result = ada_value_cast (expect_type, result);
10575 return result;
10576}
10577
03adb248
TT
10578/* See ada-exp.h. */
10579
10580operation_up
10581ada_char_operation::replace (operation_up &&owner,
10582 struct expression *exp,
10583 bool deprocedure_p,
10584 bool parse_completion,
10585 innermost_block_tracker *tracker,
10586 struct type *context_type)
10587{
10588 operation_up result = std::move (owner);
10589
10590 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10591 {
5309ce2f 10592 LONGEST val = as_longest ();
03adb248
TT
10593 gdb_assert (result.get () == this);
10594 std::get<0> (m_storage) = context_type;
5309ce2f 10595 std::get<1> (m_storage) = convert_char_literal (context_type, val);
03adb248
TT
10596 }
10597
b1b9c411 10598 return result;
03adb248
TT
10599}
10600
03070ee9
TT
10601value *
10602ada_wrapped_operation::evaluate (struct type *expect_type,
10603 struct expression *exp,
10604 enum noside noside)
10605{
10606 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10607 if (noside == EVAL_NORMAL)
10608 result = unwrap_value (result);
10609
10610 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10611 then we need to perform the conversion manually, because
10612 evaluate_subexp_standard doesn't do it. This conversion is
10613 necessary in Ada because the different kinds of float/fixed
10614 types in Ada have different representations.
10615
10616 Similarly, we need to perform the conversion from OP_LONG
10617 ourselves. */
10618 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10619 result = ada_value_cast (expect_type, result);
10620
10621 return result;
10622}
10623
013a623f
TT
10624void
10625ada_wrapped_operation::do_generate_ax (struct expression *exp,
10626 struct agent_expr *ax,
10627 struct axs_value *value,
10628 struct type *cast_type)
10629{
10630 std::get<0> (m_storage)->generate_ax (exp, ax, value, cast_type);
10631
10632 struct type *type = value->type;
10633 if (ada_is_aligner_type (type))
10634 error (_("Aligner types cannot be handled in agent expressions"));
10635 else if (find_base_type (type) != nullptr)
10636 error (_("Dynamic types cannot be handled in agent expressions"));
10637}
10638
42fecb61
TT
10639value *
10640ada_string_operation::evaluate (struct type *expect_type,
10641 struct expression *exp,
10642 enum noside noside)
10643{
fc18a21b
TT
10644 struct type *char_type;
10645 if (expect_type != nullptr && ada_is_string_type (expect_type))
10646 char_type = ada_array_element_type (expect_type, 1);
10647 else
10648 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10649
10650 const std::string &str = std::get<0> (m_storage);
10651 const char *encoding;
df86565b 10652 switch (char_type->length ())
fc18a21b
TT
10653 {
10654 case 1:
10655 {
10656 /* Simply copy over the data -- this isn't perhaps strictly
10657 correct according to the encodings, but it is gdb's
10658 historical behavior. */
10659 struct type *stringtype
10660 = lookup_array_range_type (char_type, 1, str.length ());
317c3ed9 10661 struct value *val = value::allocate (stringtype);
bbe912ba 10662 memcpy (val->contents_raw ().data (), str.c_str (),
fc18a21b
TT
10663 str.length ());
10664 return val;
10665 }
10666
10667 case 2:
10668 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10669 encoding = "UTF-16BE";
10670 else
10671 encoding = "UTF-16LE";
10672 break;
10673
10674 case 4:
10675 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10676 encoding = "UTF-32BE";
10677 else
10678 encoding = "UTF-32LE";
10679 break;
10680
10681 default:
10682 error (_("unexpected character type size %s"),
df86565b 10683 pulongest (char_type->length ()));
fc18a21b
TT
10684 }
10685
10686 auto_obstack converted;
10687 convert_between_encodings (host_charset (), encoding,
10688 (const gdb_byte *) str.c_str (),
10689 str.length (), 1,
10690 &converted, translit_none);
10691
10692 struct type *stringtype
10693 = lookup_array_range_type (char_type, 1,
10694 obstack_object_size (&converted)
df86565b 10695 / char_type->length ());
317c3ed9 10696 struct value *val = value::allocate (stringtype);
bbe912ba 10697 memcpy (val->contents_raw ().data (),
fc18a21b
TT
10698 obstack_base (&converted),
10699 obstack_object_size (&converted));
10700 return val;
42fecb61
TT
10701}
10702
b1b9c411
TT
10703value *
10704ada_concat_operation::evaluate (struct type *expect_type,
10705 struct expression *exp,
10706 enum noside noside)
10707{
10708 /* If one side is a literal, evaluate the other side first so that
10709 the expected type can be set properly. */
10710 const operation_up &lhs_expr = std::get<0> (m_storage);
10711 const operation_up &rhs_expr = std::get<1> (m_storage);
10712
10713 value *lhs, *rhs;
10714 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10715 {
10716 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10717 lhs = lhs_expr->evaluate (rhs->type (), exp, noside);
b1b9c411
TT
10718 }
10719 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10720 {
10721 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10722 struct type *rhs_type = check_typedef (rhs->type ());
b1b9c411
TT
10723 struct type *elt_type = nullptr;
10724 if (rhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10725 elt_type = rhs_type->target_type ();
b1b9c411
TT
10726 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10727 }
10728 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10729 {
10730 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10731 rhs = rhs_expr->evaluate (lhs->type (), exp, noside);
b1b9c411
TT
10732 }
10733 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10734 {
10735 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10736 struct type *lhs_type = check_typedef (lhs->type ());
b1b9c411
TT
10737 struct type *elt_type = nullptr;
10738 if (lhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10739 elt_type = lhs_type->target_type ();
b1b9c411
TT
10740 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10741 }
10742 else
10743 return concat_operation::evaluate (expect_type, exp, noside);
10744
10745 return value_concat (lhs, rhs);
10746}
10747
cc6bd32e
TT
10748value *
10749ada_qual_operation::evaluate (struct type *expect_type,
10750 struct expression *exp,
10751 enum noside noside)
10752{
10753 struct type *type = std::get<1> (m_storage);
10754 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10755}
10756
fc715eb2
TT
10757value *
10758ada_ternop_range_operation::evaluate (struct type *expect_type,
10759 struct expression *exp,
10760 enum noside noside)
10761{
10762 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10763 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10764 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10765 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10766}
10767
73796c73
TT
10768value *
10769ada_binop_addsub_operation::evaluate (struct type *expect_type,
10770 struct expression *exp,
10771 enum noside noside)
10772{
10773 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10774 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10775
10776 auto do_op = [=] (LONGEST x, LONGEST y)
10777 {
10778 if (std::get<0> (m_storage) == BINOP_ADD)
10779 return x + y;
10780 return x - y;
10781 };
10782
d0c97917 10783 if (arg1->type ()->code () == TYPE_CODE_PTR)
73796c73 10784 return (value_from_longest
d0c97917 10785 (arg1->type (),
73796c73 10786 do_op (value_as_long (arg1), value_as_long (arg2))));
d0c97917 10787 if (arg2->type ()->code () == TYPE_CODE_PTR)
73796c73 10788 return (value_from_longest
d0c97917 10789 (arg2->type (),
73796c73
TT
10790 do_op (value_as_long (arg1), value_as_long (arg2))));
10791 /* Preserve the original type for use by the range case below.
10792 We cannot cast the result to a reference type, so if ARG1 is
10793 a reference type, find its underlying type. */
d0c97917 10794 struct type *type = arg1->type ();
73796c73 10795 while (type->code () == TYPE_CODE_REF)
27710edb 10796 type = type->target_type ();
73796c73
TT
10797 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10798 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10799 /* We need to special-case the result with a range.
10800 This is done for the benefit of "ptype". gdb's Ada support
10801 historically used the LHS to set the result type here, so
10802 preserve this behavior. */
10803 if (type->code () == TYPE_CODE_RANGE)
10804 arg1 = value_cast (type, arg1);
10805 return arg1;
10806}
10807
60fa02ca
TT
10808value *
10809ada_unop_atr_operation::evaluate (struct type *expect_type,
10810 struct expression *exp,
10811 enum noside noside)
10812{
10813 struct type *type_arg = nullptr;
10814 value *val = nullptr;
10815
10816 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10817 {
10818 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10819 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 10820 type_arg = tem->type ();
60fa02ca
TT
10821 }
10822 else
10823 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10824
10825 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10826 val, type_arg, std::get<2> (m_storage));
10827}
10828
3f4a0053
TT
10829value *
10830ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10831 struct expression *exp,
10832 enum noside noside)
10833{
10834 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10835 return value::zero (expect_type, not_lval);
3f4a0053 10836
9c79936b
TT
10837 const bound_minimal_symbol &b = std::get<0> (m_storage);
10838 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10839
10840 val = ada_value_cast (expect_type, val);
10841
10842 /* Follow the Ada language semantics that do not allow taking
10843 an address of the result of a cast (view conversion in Ada). */
736355f2 10844 if (val->lval () == lval_memory)
3f4a0053 10845 {
3ee3b270 10846 if (val->lazy ())
78259c36 10847 val->fetch_lazy ();
6f9c9d71 10848 val->set_lval (not_lval);
3f4a0053
TT
10849 }
10850 return val;
10851}
10852
99a3b1e7
TT
10853value *
10854ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10855 struct expression *exp,
10856 enum noside noside)
10857{
10858 value *val = evaluate_var_value (noside,
9e5e03df
TT
10859 std::get<0> (m_storage).block,
10860 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10861
10862 val = ada_value_cast (expect_type, val);
10863
10864 /* Follow the Ada language semantics that do not allow taking
10865 an address of the result of a cast (view conversion in Ada). */
736355f2 10866 if (val->lval () == lval_memory)
99a3b1e7 10867 {
3ee3b270 10868 if (val->lazy ())
78259c36 10869 val->fetch_lazy ();
6f9c9d71 10870 val->set_lval (not_lval);
99a3b1e7
TT
10871 }
10872 return val;
10873}
10874
10875value *
10876ada_var_value_operation::evaluate (struct type *expect_type,
10877 struct expression *exp,
10878 enum noside noside)
10879{
9e5e03df 10880 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10881
6c9c307c 10882 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10883 /* Only encountered when an unresolved symbol occurs in a
10884 context other than a function call, in which case, it is
10885 invalid. */
10886 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10887 sym->print_name ());
10888
10889 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10890 {
5f9c5a63 10891 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10892 /* Check to see if this is a tagged type. We also need to handle
10893 the case where the type is a reference to a tagged type, but
10894 we have to be careful to exclude pointers to tagged types.
10895 The latter should be shown as usual (as a pointer), whereas
10896 a reference should mostly be transparent to the user. */
10897 if (ada_is_tagged_type (type, 0)
10898 || (type->code () == TYPE_CODE_REF
27710edb 10899 && ada_is_tagged_type (type->target_type (), 0)))
99a3b1e7
TT
10900 {
10901 /* Tagged types are a little special in the fact that the real
10902 type is dynamic and can only be determined by inspecting the
10903 object's tag. This means that we need to get the object's
10904 value first (EVAL_NORMAL) and then extract the actual object
10905 type from its tag.
10906
10907 Note that we cannot skip the final step where we extract
10908 the object type from its tag, because the EVAL_NORMAL phase
10909 results in dynamic components being resolved into fixed ones.
10910 This can cause problems when trying to print the type
10911 description of tagged types whose parent has a dynamic size:
10912 We use the type name of the "_parent" component in order
10913 to print the name of the ancestor type in the type description.
10914 If that component had a dynamic size, the resolution into
10915 a fixed type would result in the loss of that type name,
10916 thus preventing us from printing the name of the ancestor
10917 type in the type description. */
9863c3b5 10918 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10919
10920 if (type->code () != TYPE_CODE_REF)
10921 {
10922 struct type *actual_type;
10923
10924 actual_type = type_from_tag (ada_value_tag (arg1));
10925 if (actual_type == NULL)
10926 /* If, for some reason, we were unable to determine
10927 the actual type from the tag, then use the static
10928 approximation that we just computed as a fallback.
10929 This can happen if the debugging information is
10930 incomplete, for instance. */
10931 actual_type = type;
ee7bb294 10932 return value::zero (actual_type, not_lval);
99a3b1e7
TT
10933 }
10934 else
10935 {
10936 /* In the case of a ref, ada_coerce_ref takes care
10937 of determining the actual type. But the evaluation
10938 should return a ref as it should be valid to ask
10939 for its address; so rebuild a ref after coerce. */
10940 arg1 = ada_coerce_ref (arg1);
10941 return value_ref (arg1, TYPE_CODE_REF);
10942 }
10943 }
10944
10945 /* Records and unions for which GNAT encodings have been
10946 generated need to be statically fixed as well.
10947 Otherwise, non-static fixing produces a type where
10948 all dynamic properties are removed, which prevents "ptype"
10949 from being able to completely describe the type.
10950 For instance, a case statement in a variant record would be
10951 replaced by the relevant components based on the actual
10952 value of the discriminants. */
10953 if ((type->code () == TYPE_CODE_STRUCT
10954 && dynamic_template_type (type) != NULL)
10955 || (type->code () == TYPE_CODE_UNION
10956 && ada_find_parallel_type (type, "___XVU") != NULL))
ee7bb294 10957 return value::zero (to_static_fixed_type (type), not_lval);
99a3b1e7
TT
10958 }
10959
10960 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10961 return ada_to_fixed_value (arg1);
10962}
10963
d8a4ed8a
TT
10964bool
10965ada_var_value_operation::resolve (struct expression *exp,
10966 bool deprocedure_p,
10967 bool parse_completion,
10968 innermost_block_tracker *tracker,
10969 struct type *context_type)
10970{
9e5e03df 10971 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10972 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10973 {
10974 block_symbol resolved
9e5e03df 10975 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10976 context_type, parse_completion,
10977 deprocedure_p, tracker);
9e5e03df 10978 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10979 }
10980
10981 if (deprocedure_p
5f9c5a63 10982 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10983 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10984 return true;
10985
10986 return false;
10987}
10988
013a623f
TT
10989void
10990ada_var_value_operation::do_generate_ax (struct expression *exp,
10991 struct agent_expr *ax,
10992 struct axs_value *value,
10993 struct type *cast_type)
10994{
10995 symbol *sym = std::get<0> (m_storage).symbol;
10996
10997 if (sym->domain () == UNDEF_DOMAIN)
10998 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10999 sym->print_name ());
11000
11001 struct type *type = static_unwrap_type (sym->type ());
11002 if (ada_is_tagged_type (type, 0)
11003 || (type->code () == TYPE_CODE_REF
11004 && ada_is_tagged_type (type->target_type (), 0)))
11005 error (_("Tagged types cannot be handled in agent expressions"));
11006
11007 if ((type->code () == TYPE_CODE_STRUCT
11008 && dynamic_template_type (type) != NULL)
11009 || (type->code () == TYPE_CODE_UNION
11010 && ada_find_parallel_type (type, "___XVU") != NULL))
11011 error (_("Dynamic types cannot be handled in agent expressions"));
11012
11013 var_value_operation::do_generate_ax (exp, ax, value, cast_type);
11014}
11015
9e99f48f
TT
11016value *
11017ada_atr_val_operation::evaluate (struct type *expect_type,
11018 struct expression *exp,
11019 enum noside noside)
11020{
11021 value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
11022 return ada_val_atr (noside, std::get<0> (m_storage), arg);
11023}
11024
e8c33fa1
TT
11025value *
11026ada_unop_ind_operation::evaluate (struct type *expect_type,
11027 struct expression *exp,
11028 enum noside noside)
11029{
11030 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
11031
d0c97917 11032 struct type *type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
11033 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11034 {
11035 if (ada_is_array_descriptor_type (type))
11036 /* GDB allows dereferencing GNAT array descriptors. */
11037 {
11038 struct type *arrType = ada_type_of_array (arg1, 0);
11039
11040 if (arrType == NULL)
11041 error (_("Attempt to dereference null array pointer."));
11042 return value_at_lazy (arrType, 0);
11043 }
11044 else if (type->code () == TYPE_CODE_PTR
11045 || type->code () == TYPE_CODE_REF
11046 /* In C you can dereference an array to get the 1st elt. */
11047 || type->code () == TYPE_CODE_ARRAY)
11048 {
11049 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11050 only be determined by inspecting the object's tag.
11051 This means that we need to evaluate completely the
11052 expression in order to get its type. */
11053
11054 if ((type->code () == TYPE_CODE_REF
11055 || type->code () == TYPE_CODE_PTR)
27710edb 11056 && ada_is_tagged_type (type->target_type (), 0))
e8c33fa1
TT
11057 {
11058 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11059 EVAL_NORMAL);
d0c97917 11060 type = ada_value_ind (arg1)->type ();
e8c33fa1
TT
11061 }
11062 else
11063 {
11064 type = to_static_fixed_type
11065 (ada_aligned_type
27710edb 11066 (ada_check_typedef (type->target_type ())));
e8c33fa1 11067 }
ee7bb294 11068 return value::zero (type, lval_memory);
e8c33fa1
TT
11069 }
11070 else if (type->code () == TYPE_CODE_INT)
11071 {
11072 /* GDB allows dereferencing an int. */
11073 if (expect_type == NULL)
ee7bb294 11074 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
e8c33fa1
TT
11075 lval_memory);
11076 else
11077 {
11078 expect_type =
11079 to_static_fixed_type (ada_aligned_type (expect_type));
ee7bb294 11080 return value::zero (expect_type, lval_memory);
e8c33fa1
TT
11081 }
11082 }
11083 else
11084 error (_("Attempt to take contents of a non-pointer value."));
11085 }
11086 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
d0c97917 11087 type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
11088
11089 if (type->code () == TYPE_CODE_INT)
11090 /* GDB allows dereferencing an int. If we were given
11091 the expect_type, then use that as the target type.
11092 Otherwise, assume that the target type is an int. */
11093 {
11094 if (expect_type != NULL)
11095 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11096 arg1));
11097 else
11098 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11099 (CORE_ADDR) value_as_address (arg1));
11100 }
11101
11102 if (ada_is_array_descriptor_type (type))
11103 /* GDB allows dereferencing GNAT array descriptors. */
11104 return ada_coerce_to_simple_array (arg1);
11105 else
11106 return ada_value_ind (arg1);
11107}
11108
ebc06ad8
TT
11109value *
11110ada_structop_operation::evaluate (struct type *expect_type,
11111 struct expression *exp,
11112 enum noside noside)
11113{
11114 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11115 const char *str = std::get<1> (m_storage).c_str ();
11116 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11117 {
11118 struct type *type;
d0c97917 11119 struct type *type1 = arg1->type ();
ebc06ad8
TT
11120
11121 if (ada_is_tagged_type (type1, 1))
11122 {
11123 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11124
11125 /* If the field is not found, check if it exists in the
11126 extension of this object's type. This means that we
11127 need to evaluate completely the expression. */
11128
11129 if (type == NULL)
11130 {
11131 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11132 EVAL_NORMAL);
11133 arg1 = ada_value_struct_elt (arg1, str, 0);
11134 arg1 = unwrap_value (arg1);
d0c97917 11135 type = ada_to_fixed_value (arg1)->type ();
ebc06ad8
TT
11136 }
11137 }
11138 else
11139 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11140
ee7bb294 11141 return value::zero (ada_aligned_type (type), lval_memory);
ebc06ad8
TT
11142 }
11143 else
11144 {
11145 arg1 = ada_value_struct_elt (arg1, str, 0);
11146 arg1 = unwrap_value (arg1);
11147 return ada_to_fixed_value (arg1);
11148 }
11149}
11150
efe3af2f
TT
11151value *
11152ada_funcall_operation::evaluate (struct type *expect_type,
11153 struct expression *exp,
11154 enum noside noside)
11155{
11156 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11157 int nargs = args_up.size ();
11158 std::vector<value *> argvec (nargs);
11159 operation_up &callee_op = std::get<0> (m_storage);
11160
11161 ada_var_value_operation *avv
11162 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11163 if (avv != nullptr
6c9c307c 11164 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11165 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11166 avv->get_symbol ()->print_name ());
11167
11168 value *callee = callee_op->evaluate (nullptr, exp, noside);
11169 for (int i = 0; i < args_up.size (); ++i)
11170 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11171
11172 if (ada_is_constrained_packed_array_type
d0c97917 11173 (desc_base_type (callee->type ())))
efe3af2f 11174 callee = ada_coerce_to_simple_array (callee);
d0c97917
TT
11175 else if (callee->type ()->code () == TYPE_CODE_ARRAY
11176 && TYPE_FIELD_BITSIZE (callee->type (), 0) != 0)
efe3af2f
TT
11177 /* This is a packed array that has already been fixed, and
11178 therefore already coerced to a simple array. Nothing further
11179 to do. */
11180 ;
d0c97917 11181 else if (callee->type ()->code () == TYPE_CODE_REF)
efe3af2f
TT
11182 {
11183 /* Make sure we dereference references so that all the code below
11184 feels like it's really handling the referenced value. Wrapping
11185 types (for alignment) may be there, so make sure we strip them as
11186 well. */
11187 callee = ada_to_fixed_value (coerce_ref (callee));
11188 }
d0c97917 11189 else if (callee->type ()->code () == TYPE_CODE_ARRAY
736355f2 11190 && callee->lval () == lval_memory)
efe3af2f
TT
11191 callee = value_addr (callee);
11192
d0c97917 11193 struct type *type = ada_check_typedef (callee->type ());
efe3af2f
TT
11194
11195 /* Ada allows us to implicitly dereference arrays when subscripting
11196 them. So, if this is an array typedef (encoding use for array
11197 access types encoded as fat pointers), strip it now. */
11198 if (type->code () == TYPE_CODE_TYPEDEF)
11199 type = ada_typedef_target_type (type);
11200
11201 if (type->code () == TYPE_CODE_PTR)
11202 {
27710edb 11203 switch (ada_check_typedef (type->target_type ())->code ())
efe3af2f
TT
11204 {
11205 case TYPE_CODE_FUNC:
27710edb 11206 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11207 break;
11208 case TYPE_CODE_ARRAY:
11209 break;
11210 case TYPE_CODE_STRUCT:
11211 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11212 callee = ada_value_ind (callee);
27710edb 11213 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11214 break;
11215 default:
11216 error (_("cannot subscript or call something of type `%s'"),
d0c97917 11217 ada_type_name (callee->type ()));
efe3af2f
TT
11218 break;
11219 }
11220 }
11221
11222 switch (type->code ())
11223 {
11224 case TYPE_CODE_FUNC:
11225 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11226 {
27710edb 11227 if (type->target_type () == NULL)
efe3af2f 11228 error_call_unknown_return_type (NULL);
317c3ed9 11229 return value::allocate (type->target_type ());
efe3af2f
TT
11230 }
11231 return call_function_by_hand (callee, NULL, argvec);
11232 case TYPE_CODE_INTERNAL_FUNCTION:
11233 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11234 /* We don't know anything about what the internal
11235 function might return, but we have to return
11236 something. */
ee7bb294 11237 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
efe3af2f
TT
11238 not_lval);
11239 else
11240 return call_internal_function (exp->gdbarch, exp->language_defn,
11241 callee, nargs,
11242 argvec.data ());
11243
d3c54a1c
TT
11244 case TYPE_CODE_STRUCT:
11245 {
11246 int arity;
4c4b4cd2 11247
d3c54a1c
TT
11248 arity = ada_array_arity (type);
11249 type = ada_array_element_type (type, nargs);
11250 if (type == NULL)
11251 error (_("cannot subscript or call a record"));
11252 if (arity != nargs)
11253 error (_("wrong number of subscripts; expecting %d"), arity);
11254 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 11255 return value::zero (ada_aligned_type (type), lval_memory);
d3c54a1c
TT
11256 return
11257 unwrap_value (ada_value_subscript
11258 (callee, nargs, argvec.data ()));
11259 }
11260 case TYPE_CODE_ARRAY:
14f9c5c9 11261 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11262 {
d3c54a1c
TT
11263 type = ada_array_element_type (type, nargs);
11264 if (type == NULL)
11265 error (_("element type of array unknown"));
dda83cd7 11266 else
ee7bb294 11267 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11268 }
d3c54a1c
TT
11269 return
11270 unwrap_value (ada_value_subscript
11271 (ada_coerce_to_simple_array (callee),
11272 nargs, argvec.data ()));
11273 case TYPE_CODE_PTR: /* Pointer to array */
11274 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11275 {
27710edb 11276 type = to_fixed_array_type (type->target_type (), NULL, 1);
d3c54a1c
TT
11277 type = ada_array_element_type (type, nargs);
11278 if (type == NULL)
11279 error (_("element type of array unknown"));
96967637 11280 else
ee7bb294 11281 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11282 }
d3c54a1c
TT
11283 return
11284 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11285 argvec.data ()));
6b0d7253 11286
d3c54a1c
TT
11287 default:
11288 error (_("Attempt to index or call something other than an "
11289 "array or function"));
11290 }
11291}
5b4ee69b 11292
d3c54a1c
TT
11293bool
11294ada_funcall_operation::resolve (struct expression *exp,
11295 bool deprocedure_p,
11296 bool parse_completion,
11297 innermost_block_tracker *tracker,
11298 struct type *context_type)
11299{
11300 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11301
d3c54a1c
TT
11302 ada_var_value_operation *avv
11303 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11304 if (avv == nullptr)
11305 return false;
5ec18f2b 11306
d3c54a1c 11307 symbol *sym = avv->get_symbol ();
6c9c307c 11308 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11309 return false;
dda83cd7 11310
d3c54a1c
TT
11311 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11312 int nargs = args_up.size ();
11313 std::vector<value *> argvec (nargs);
284614f0 11314
d3c54a1c
TT
11315 for (int i = 0; i < args_up.size (); ++i)
11316 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11317
d3c54a1c
TT
11318 const block *block = avv->get_block ();
11319 block_symbol resolved
11320 = ada_resolve_funcall (sym, block,
11321 context_type, parse_completion,
11322 nargs, argvec.data (),
11323 tracker);
11324
11325 std::get<0> (m_storage)
9e5e03df 11326 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11327 return false;
11328}
11329
11330bool
11331ada_ternop_slice_operation::resolve (struct expression *exp,
11332 bool deprocedure_p,
11333 bool parse_completion,
11334 innermost_block_tracker *tracker,
11335 struct type *context_type)
11336{
11337 /* Historically this check was done during resolution, so we
11338 continue that here. */
11339 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11340 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 11341 if (ada_is_any_packed_array_type (v->type ()))
d3c54a1c
TT
11342 error (_("cannot slice a packed array"));
11343 return false;
11344}
14f9c5c9 11345
14f9c5c9 11346}
d3c54a1c 11347
14f9c5c9 11348\f
d2e4a39e 11349
4c4b4cd2
PH
11350/* Return non-zero iff TYPE represents a System.Address type. */
11351
11352int
11353ada_is_system_address_type (struct type *type)
11354{
7d93a1e0 11355 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11356}
11357
14f9c5c9 11358\f
d2e4a39e 11359
dda83cd7 11360 /* Range types */
14f9c5c9
AS
11361
11362/* Scan STR beginning at position K for a discriminant name, and
11363 return the value of that discriminant field of DVAL in *PX. If
11364 PNEW_K is not null, put the position of the character beyond the
11365 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11366 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11367
11368static int
108d56a4 11369scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11370 int *pnew_k)
14f9c5c9 11371{
5f9febe0 11372 static std::string storage;
5da1a4d3 11373 const char *pstart, *pend, *bound;
d2e4a39e 11374 struct value *bound_val;
14f9c5c9
AS
11375
11376 if (dval == NULL || str == NULL || str[k] == '\0')
11377 return 0;
11378
5da1a4d3
SM
11379 pstart = str + k;
11380 pend = strstr (pstart, "__");
14f9c5c9
AS
11381 if (pend == NULL)
11382 {
5da1a4d3 11383 bound = pstart;
14f9c5c9
AS
11384 k += strlen (bound);
11385 }
d2e4a39e 11386 else
14f9c5c9 11387 {
5da1a4d3
SM
11388 int len = pend - pstart;
11389
11390 /* Strip __ and beyond. */
5f9febe0
TT
11391 storage = std::string (pstart, len);
11392 bound = storage.c_str ();
d2e4a39e 11393 k = pend - str;
14f9c5c9 11394 }
d2e4a39e 11395
d0c97917 11396 bound_val = ada_search_struct_field (bound, dval, 0, dval->type ());
14f9c5c9
AS
11397 if (bound_val == NULL)
11398 return 0;
11399
11400 *px = value_as_long (bound_val);
11401 if (pnew_k != NULL)
11402 *pnew_k = k;
11403 return 1;
11404}
11405
25a1127b
TT
11406/* Value of variable named NAME. Only exact matches are considered.
11407 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11408 otherwise causes an error with message ERR_MSG. */
11409
d2e4a39e 11410static struct value *
edb0c9cb 11411get_var_value (const char *name, const char *err_msg)
14f9c5c9 11412{
25a1127b
TT
11413 std::string quoted_name = add_angle_brackets (name);
11414
11415 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11416
d1183b06
TT
11417 std::vector<struct block_symbol> syms
11418 = ada_lookup_symbol_list_worker (lookup_name,
11419 get_selected_block (0),
11420 VAR_DOMAIN, 1);
14f9c5c9 11421
d1183b06 11422 if (syms.size () != 1)
14f9c5c9
AS
11423 {
11424 if (err_msg == NULL)
dda83cd7 11425 return 0;
14f9c5c9 11426 else
dda83cd7 11427 error (("%s"), err_msg);
14f9c5c9
AS
11428 }
11429
54d343a2 11430 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11431}
d2e4a39e 11432
edb0c9cb
PA
11433/* Value of integer variable named NAME in the current environment.
11434 If no such variable is found, returns false. Otherwise, sets VALUE
11435 to the variable's value and returns true. */
4c4b4cd2 11436
edb0c9cb
PA
11437bool
11438get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11439{
4c4b4cd2 11440 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11441
14f9c5c9 11442 if (var_val == 0)
edb0c9cb
PA
11443 return false;
11444
11445 value = value_as_long (var_val);
11446 return true;
14f9c5c9 11447}
d2e4a39e 11448
14f9c5c9
AS
11449
11450/* Return a range type whose base type is that of the range type named
11451 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11452 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11453 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11454 corresponding range type from debug information; fall back to using it
11455 if symbol lookup fails. If a new type must be created, allocate it
11456 like ORIG_TYPE was. The bounds information, in general, is encoded
11457 in NAME, the base type given in the named range type. */
14f9c5c9 11458
d2e4a39e 11459static struct type *
28c85d6c 11460to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11461{
0d5cff50 11462 const char *name;
14f9c5c9 11463 struct type *base_type;
108d56a4 11464 const char *subtype_info;
14f9c5c9 11465
28c85d6c 11466 gdb_assert (raw_type != NULL);
7d93a1e0 11467 gdb_assert (raw_type->name () != NULL);
dddfab26 11468
78134374 11469 if (raw_type->code () == TYPE_CODE_RANGE)
27710edb 11470 base_type = raw_type->target_type ();
14f9c5c9
AS
11471 else
11472 base_type = raw_type;
11473
7d93a1e0 11474 name = raw_type->name ();
14f9c5c9
AS
11475 subtype_info = strstr (name, "___XD");
11476 if (subtype_info == NULL)
690cc4eb 11477 {
43bbcdc2
PH
11478 LONGEST L = ada_discrete_type_low_bound (raw_type);
11479 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11480
690cc4eb
PH
11481 if (L < INT_MIN || U > INT_MAX)
11482 return raw_type;
11483 else
e727c536
TT
11484 {
11485 type_allocator alloc (raw_type);
11486 return create_static_range_type (alloc, raw_type, L, U);
11487 }
690cc4eb 11488 }
14f9c5c9
AS
11489 else
11490 {
14f9c5c9
AS
11491 int prefix_len = subtype_info - name;
11492 LONGEST L, U;
11493 struct type *type;
108d56a4 11494 const char *bounds_str;
14f9c5c9
AS
11495 int n;
11496
14f9c5c9
AS
11497 subtype_info += 5;
11498 bounds_str = strchr (subtype_info, '_');
11499 n = 1;
11500
d2e4a39e 11501 if (*subtype_info == 'L')
dda83cd7
SM
11502 {
11503 if (!ada_scan_number (bounds_str, n, &L, &n)
11504 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11505 return raw_type;
11506 if (bounds_str[n] == '_')
11507 n += 2;
11508 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11509 n += 1;
11510 subtype_info += 1;
11511 }
d2e4a39e 11512 else
dda83cd7 11513 {
5f9febe0
TT
11514 std::string name_buf = std::string (name, prefix_len) + "___L";
11515 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11516 {
11517 lim_warning (_("Unknown lower bound, using 1."));
11518 L = 1;
11519 }
11520 }
14f9c5c9 11521
d2e4a39e 11522 if (*subtype_info == 'U')
dda83cd7
SM
11523 {
11524 if (!ada_scan_number (bounds_str, n, &U, &n)
11525 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11526 return raw_type;
11527 }
d2e4a39e 11528 else
dda83cd7 11529 {
5f9febe0
TT
11530 std::string name_buf = std::string (name, prefix_len) + "___U";
11531 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11532 {
11533 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11534 U = L;
11535 }
11536 }
14f9c5c9 11537
e727c536
TT
11538 type_allocator alloc (raw_type);
11539 type = create_static_range_type (alloc, base_type, L, U);
f5a91472 11540 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11541 to match the size of the base_type, which is not what we want.
11542 Set it back to the original range type's length. */
df86565b 11543 type->set_length (raw_type->length ());
d0e39ea2 11544 type->set_name (name);
14f9c5c9
AS
11545 return type;
11546 }
11547}
11548
4c4b4cd2
PH
11549/* True iff NAME is the name of a range type. */
11550
14f9c5c9 11551int
d2e4a39e 11552ada_is_range_type_name (const char *name)
14f9c5c9
AS
11553{
11554 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11555}
14f9c5c9 11556\f
d2e4a39e 11557
dda83cd7 11558 /* Modular types */
4c4b4cd2
PH
11559
11560/* True iff TYPE is an Ada modular type. */
14f9c5c9 11561
14f9c5c9 11562int
d2e4a39e 11563ada_is_modular_type (struct type *type)
14f9c5c9 11564{
18af8284 11565 struct type *subranged_type = get_base_type (type);
14f9c5c9 11566
78134374 11567 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11568 && subranged_type->code () == TYPE_CODE_INT
11569 && subranged_type->is_unsigned ());
14f9c5c9
AS
11570}
11571
4c4b4cd2
PH
11572/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11573
61ee279c 11574ULONGEST
0056e4d5 11575ada_modulus (struct type *type)
14f9c5c9 11576{
5e500d33
SM
11577 const dynamic_prop &high = type->bounds ()->high;
11578
11579 if (high.kind () == PROP_CONST)
11580 return (ULONGEST) high.const_val () + 1;
11581
11582 /* If TYPE is unresolved, the high bound might be a location list. Return
11583 0, for lack of a better value to return. */
11584 return 0;
14f9c5c9 11585}
d2e4a39e 11586\f
f7f9143b
JB
11587
11588/* Ada exception catchpoint support:
11589 ---------------------------------
11590
11591 We support 3 kinds of exception catchpoints:
11592 . catchpoints on Ada exceptions
11593 . catchpoints on unhandled Ada exceptions
11594 . catchpoints on failed assertions
11595
11596 Exceptions raised during failed assertions, or unhandled exceptions
11597 could perfectly be caught with the general catchpoint on Ada exceptions.
11598 However, we can easily differentiate these two special cases, and having
11599 the option to distinguish these two cases from the rest can be useful
11600 to zero-in on certain situations.
11601
11602 Exception catchpoints are a specialized form of breakpoint,
11603 since they rely on inserting breakpoints inside known routines
11604 of the GNAT runtime. The implementation therefore uses a standard
11605 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11606 of breakpoint_ops.
11607
0259addd
JB
11608 Support in the runtime for exception catchpoints have been changed
11609 a few times already, and these changes affect the implementation
11610 of these catchpoints. In order to be able to support several
11611 variants of the runtime, we use a sniffer that will determine
28010a5d 11612 the runtime variant used by the program being debugged. */
f7f9143b 11613
82eacd52
JB
11614/* Ada's standard exceptions.
11615
11616 The Ada 83 standard also defined Numeric_Error. But there so many
11617 situations where it was unclear from the Ada 83 Reference Manual
11618 (RM) whether Constraint_Error or Numeric_Error should be raised,
11619 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11620 Interpretation saying that anytime the RM says that Numeric_Error
11621 should be raised, the implementation may raise Constraint_Error.
11622 Ada 95 went one step further and pretty much removed Numeric_Error
11623 from the list of standard exceptions (it made it a renaming of
11624 Constraint_Error, to help preserve compatibility when compiling
11625 an Ada83 compiler). As such, we do not include Numeric_Error from
11626 this list of standard exceptions. */
3d0b0fa3 11627
27087b7f 11628static const char * const standard_exc[] = {
3d0b0fa3
JB
11629 "constraint_error",
11630 "program_error",
11631 "storage_error",
11632 "tasking_error"
11633};
11634
0259addd
JB
11635typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11636
11637/* A structure that describes how to support exception catchpoints
11638 for a given executable. */
11639
11640struct exception_support_info
11641{
11642 /* The name of the symbol to break on in order to insert
11643 a catchpoint on exceptions. */
11644 const char *catch_exception_sym;
11645
11646 /* The name of the symbol to break on in order to insert
11647 a catchpoint on unhandled exceptions. */
11648 const char *catch_exception_unhandled_sym;
11649
11650 /* The name of the symbol to break on in order to insert
11651 a catchpoint on failed assertions. */
11652 const char *catch_assert_sym;
11653
9f757bf7
XR
11654 /* The name of the symbol to break on in order to insert
11655 a catchpoint on exception handling. */
11656 const char *catch_handlers_sym;
11657
0259addd
JB
11658 /* Assuming that the inferior just triggered an unhandled exception
11659 catchpoint, this function is responsible for returning the address
11660 in inferior memory where the name of that exception is stored.
11661 Return zero if the address could not be computed. */
11662 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11663};
11664
11665static CORE_ADDR ada_unhandled_exception_name_addr (void);
11666static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11667
11668/* The following exception support info structure describes how to
11669 implement exception catchpoints with the latest version of the
ca683e3a 11670 Ada runtime (as of 2019-08-??). */
0259addd
JB
11671
11672static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11673{
11674 "__gnat_debug_raise_exception", /* catch_exception_sym */
11675 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11676 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11677 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11678 ada_unhandled_exception_name_addr
11679};
11680
11681/* The following exception support info structure describes how to
11682 implement exception catchpoints with an earlier version of the
11683 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11684
11685static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11686{
11687 "__gnat_debug_raise_exception", /* catch_exception_sym */
11688 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11689 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11690 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11691 ada_unhandled_exception_name_addr
11692};
11693
11694/* The following exception support info structure describes how to
11695 implement exception catchpoints with a slightly older version
11696 of the Ada runtime. */
11697
11698static const struct exception_support_info exception_support_info_fallback =
11699{
11700 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11701 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11702 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11703 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11704 ada_unhandled_exception_name_addr_from_raise
11705};
11706
f17011e0
JB
11707/* Return nonzero if we can detect the exception support routines
11708 described in EINFO.
11709
11710 This function errors out if an abnormal situation is detected
11711 (for instance, if we find the exception support routines, but
11712 that support is found to be incomplete). */
11713
11714static int
11715ada_has_this_exception_support (const struct exception_support_info *einfo)
11716{
11717 struct symbol *sym;
11718
11719 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11720 that should be compiled with debugging information. As a result, we
11721 expect to find that symbol in the symtabs. */
11722
11723 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11724 if (sym == NULL)
a6af7abe
JB
11725 {
11726 /* Perhaps we did not find our symbol because the Ada runtime was
11727 compiled without debugging info, or simply stripped of it.
11728 It happens on some GNU/Linux distributions for instance, where
11729 users have to install a separate debug package in order to get
11730 the runtime's debugging info. In that situation, let the user
11731 know why we cannot insert an Ada exception catchpoint.
11732
11733 Note: Just for the purpose of inserting our Ada exception
11734 catchpoint, we could rely purely on the associated minimal symbol.
11735 But we would be operating in degraded mode anyway, since we are
11736 still lacking the debugging info needed later on to extract
11737 the name of the exception being raised (this name is printed in
11738 the catchpoint message, and is also used when trying to catch
11739 a specific exception). We do not handle this case for now. */
3b7344d5 11740 struct bound_minimal_symbol msym
1c8e84b0
JB
11741 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11742
60f62e2b 11743 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
a6af7abe
JB
11744 error (_("Your Ada runtime appears to be missing some debugging "
11745 "information.\nCannot insert Ada exception catchpoint "
11746 "in this configuration."));
11747
11748 return 0;
11749 }
f17011e0
JB
11750
11751 /* Make sure that the symbol we found corresponds to a function. */
11752
66d7f48f 11753 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11754 error (_("Symbol \"%s\" is not a function (class = %d)"),
11755 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11756
11757 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11758 if (sym == NULL)
11759 {
11760 struct bound_minimal_symbol msym
11761 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11762
60f62e2b 11763 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
ca683e3a
AO
11764 error (_("Your Ada runtime appears to be missing some debugging "
11765 "information.\nCannot insert Ada exception catchpoint "
11766 "in this configuration."));
11767
11768 return 0;
11769 }
11770
11771 /* Make sure that the symbol we found corresponds to a function. */
11772
66d7f48f 11773 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11774 error (_("Symbol \"%s\" is not a function (class = %d)"),
11775 sym->linkage_name (), sym->aclass ());
f17011e0
JB
11776
11777 return 1;
11778}
11779
0259addd
JB
11780/* Inspect the Ada runtime and determine which exception info structure
11781 should be used to provide support for exception catchpoints.
11782
3eecfa55
JB
11783 This function will always set the per-inferior exception_info,
11784 or raise an error. */
0259addd
JB
11785
11786static void
11787ada_exception_support_info_sniffer (void)
11788{
3eecfa55 11789 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11790
11791 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11792 if (data->exception_info != NULL)
0259addd
JB
11793 return;
11794
11795 /* Check the latest (default) exception support info. */
f17011e0 11796 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11797 {
3eecfa55 11798 data->exception_info = &default_exception_support_info;
0259addd
JB
11799 return;
11800 }
11801
ca683e3a
AO
11802 /* Try the v0 exception suport info. */
11803 if (ada_has_this_exception_support (&exception_support_info_v0))
11804 {
11805 data->exception_info = &exception_support_info_v0;
11806 return;
11807 }
11808
0259addd 11809 /* Try our fallback exception suport info. */
f17011e0 11810 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11811 {
3eecfa55 11812 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11813 return;
11814 }
11815
11816 /* Sometimes, it is normal for us to not be able to find the routine
11817 we are looking for. This happens when the program is linked with
11818 the shared version of the GNAT runtime, and the program has not been
11819 started yet. Inform the user of these two possible causes if
11820 applicable. */
11821
ccefe4c4 11822 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11823 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11824
11825 /* If the symbol does not exist, then check that the program is
11826 already started, to make sure that shared libraries have been
11827 loaded. If it is not started, this may mean that the symbol is
11828 in a shared library. */
11829
e99b03dc 11830 if (inferior_ptid.pid () == 0)
0259addd
JB
11831 error (_("Unable to insert catchpoint. Try to start the program first."));
11832
11833 /* At this point, we know that we are debugging an Ada program and
11834 that the inferior has been started, but we still are not able to
0963b4bd 11835 find the run-time symbols. That can mean that we are in
0259addd
JB
11836 configurable run time mode, or that a-except as been optimized
11837 out by the linker... In any case, at this point it is not worth
11838 supporting this feature. */
11839
7dda8cff 11840 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11841}
11842
f7f9143b
JB
11843/* True iff FRAME is very likely to be that of a function that is
11844 part of the runtime system. This is all very heuristic, but is
11845 intended to be used as advice as to what frames are uninteresting
11846 to most users. */
11847
11848static int
bd2b40ac 11849is_known_support_routine (frame_info_ptr frame)
f7f9143b 11850{
692465f1 11851 enum language func_lang;
f7f9143b 11852 int i;
f35a17b5 11853 const char *fullname;
f7f9143b 11854
4ed6b5be
JB
11855 /* If this code does not have any debugging information (no symtab),
11856 This cannot be any user code. */
f7f9143b 11857
51abb421 11858 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11859 if (sal.symtab == NULL)
11860 return 1;
11861
4ed6b5be
JB
11862 /* If there is a symtab, but the associated source file cannot be
11863 located, then assume this is not user code: Selecting a frame
11864 for which we cannot display the code would not be very helpful
11865 for the user. This should also take care of case such as VxWorks
11866 where the kernel has some debugging info provided for a few units. */
f7f9143b 11867
f35a17b5
JK
11868 fullname = symtab_to_fullname (sal.symtab);
11869 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11870 return 1;
11871
85102364 11872 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11873 We also check the name of the objfile against the name of some
11874 known system libraries that sometimes come with debugging info
11875 too. */
11876
f7f9143b
JB
11877 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11878 {
11879 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11880 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11881 return 1;
3c86fae3
SM
11882 if (sal.symtab->compunit ()->objfile () != NULL
11883 && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
dda83cd7 11884 return 1;
f7f9143b
JB
11885 }
11886
4ed6b5be 11887 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11888
c6dc63a1
TT
11889 gdb::unique_xmalloc_ptr<char> func_name
11890 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11891 if (func_name == NULL)
11892 return 1;
11893
11894 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11895 {
11896 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11897 if (re_exec (func_name.get ()))
11898 return 1;
f7f9143b
JB
11899 }
11900
11901 return 0;
11902}
11903
11904/* Find the first frame that contains debugging information and that is not
11905 part of the Ada run-time, starting from FI and moving upward. */
11906
0ef643c8 11907void
bd2b40ac 11908ada_find_printable_frame (frame_info_ptr fi)
f7f9143b
JB
11909{
11910 for (; fi != NULL; fi = get_prev_frame (fi))
11911 {
11912 if (!is_known_support_routine (fi))
dda83cd7
SM
11913 {
11914 select_frame (fi);
11915 break;
11916 }
f7f9143b
JB
11917 }
11918
11919}
11920
11921/* Assuming that the inferior just triggered an unhandled exception
11922 catchpoint, return the address in inferior memory where the name
11923 of the exception is stored.
11924
11925 Return zero if the address could not be computed. */
11926
11927static CORE_ADDR
11928ada_unhandled_exception_name_addr (void)
0259addd
JB
11929{
11930 return parse_and_eval_address ("e.full_name");
11931}
11932
11933/* Same as ada_unhandled_exception_name_addr, except that this function
11934 should be used when the inferior uses an older version of the runtime,
11935 where the exception name needs to be extracted from a specific frame
11936 several frames up in the callstack. */
11937
11938static CORE_ADDR
11939ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11940{
11941 int frame_level;
bd2b40ac 11942 frame_info_ptr fi;
3eecfa55 11943 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11944
11945 /* To determine the name of this exception, we need to select
11946 the frame corresponding to RAISE_SYM_NAME. This frame is
11947 at least 3 levels up, so we simply skip the first 3 frames
11948 without checking the name of their associated function. */
11949 fi = get_current_frame ();
11950 for (frame_level = 0; frame_level < 3; frame_level += 1)
11951 if (fi != NULL)
11952 fi = get_prev_frame (fi);
11953
11954 while (fi != NULL)
11955 {
692465f1
JB
11956 enum language func_lang;
11957
c6dc63a1
TT
11958 gdb::unique_xmalloc_ptr<char> func_name
11959 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11960 if (func_name != NULL)
11961 {
dda83cd7 11962 if (strcmp (func_name.get (),
55b87a52
KS
11963 data->exception_info->catch_exception_sym) == 0)
11964 break; /* We found the frame we were looking for... */
55b87a52 11965 }
fb44b1a7 11966 fi = get_prev_frame (fi);
f7f9143b
JB
11967 }
11968
11969 if (fi == NULL)
11970 return 0;
11971
11972 select_frame (fi);
11973 return parse_and_eval_address ("id.full_name");
11974}
11975
11976/* Assuming the inferior just triggered an Ada exception catchpoint
11977 (of any type), return the address in inferior memory where the name
11978 of the exception is stored, if applicable.
11979
45db7c09
PA
11980 Assumes the selected frame is the current frame.
11981
f7f9143b
JB
11982 Return zero if the address could not be computed, or if not relevant. */
11983
11984static CORE_ADDR
7bd86313 11985ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex)
f7f9143b 11986{
3eecfa55
JB
11987 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11988
f7f9143b
JB
11989 switch (ex)
11990 {
761269c8 11991 case ada_catch_exception:
dda83cd7
SM
11992 return (parse_and_eval_address ("e.full_name"));
11993 break;
f7f9143b 11994
761269c8 11995 case ada_catch_exception_unhandled:
dda83cd7
SM
11996 return data->exception_info->unhandled_exception_name_addr ();
11997 break;
9f757bf7
XR
11998
11999 case ada_catch_handlers:
dda83cd7 12000 return 0; /* The runtimes does not provide access to the exception
9f757bf7 12001 name. */
dda83cd7 12002 break;
9f757bf7 12003
761269c8 12004 case ada_catch_assert:
dda83cd7
SM
12005 return 0; /* Exception name is not relevant in this case. */
12006 break;
f7f9143b
JB
12007
12008 default:
f34652de 12009 internal_error (_("unexpected catchpoint type"));
dda83cd7 12010 break;
f7f9143b
JB
12011 }
12012
12013 return 0; /* Should never be reached. */
12014}
12015
e547c119
JB
12016/* Assuming the inferior is stopped at an exception catchpoint,
12017 return the message which was associated to the exception, if
12018 available. Return NULL if the message could not be retrieved.
12019
e547c119
JB
12020 Note: The exception message can be associated to an exception
12021 either through the use of the Raise_Exception function, or
12022 more simply (Ada 2005 and later), via:
12023
12024 raise Exception_Name with "exception message";
12025
12026 */
12027
6f46ac85 12028static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12029ada_exception_message_1 (void)
12030{
12031 struct value *e_msg_val;
e547c119 12032 int e_msg_len;
e547c119
JB
12033
12034 /* For runtimes that support this feature, the exception message
12035 is passed as an unbounded string argument called "message". */
12036 e_msg_val = parse_and_eval ("message");
12037 if (e_msg_val == NULL)
12038 return NULL; /* Exception message not supported. */
12039
12040 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
12041 gdb_assert (e_msg_val != NULL);
d0c97917 12042 e_msg_len = e_msg_val->type ()->length ();
e547c119
JB
12043
12044 /* If the message string is empty, then treat it as if there was
12045 no exception message. */
12046 if (e_msg_len <= 0)
12047 return NULL;
12048
15f3b077 12049 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
9feb2d07 12050 read_memory (e_msg_val->address (), (gdb_byte *) e_msg.get (),
15f3b077
TT
12051 e_msg_len);
12052 e_msg.get ()[e_msg_len] = '\0';
12053
12054 return e_msg;
e547c119
JB
12055}
12056
12057/* Same as ada_exception_message_1, except that all exceptions are
12058 contained here (returning NULL instead). */
12059
6f46ac85 12060static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12061ada_exception_message (void)
12062{
6f46ac85 12063 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 12064
a70b8144 12065 try
e547c119
JB
12066 {
12067 e_msg = ada_exception_message_1 ();
12068 }
230d2906 12069 catch (const gdb_exception_error &e)
e547c119 12070 {
6f46ac85 12071 e_msg.reset (nullptr);
e547c119 12072 }
e547c119
JB
12073
12074 return e_msg;
12075}
12076
f7f9143b
JB
12077/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12078 any error that ada_exception_name_addr_1 might cause to be thrown.
12079 When an error is intercepted, a warning with the error message is printed,
12080 and zero is returned. */
12081
12082static CORE_ADDR
7bd86313 12083ada_exception_name_addr (enum ada_exception_catchpoint_kind ex)
f7f9143b 12084{
f7f9143b
JB
12085 CORE_ADDR result = 0;
12086
a70b8144 12087 try
f7f9143b 12088 {
7bd86313 12089 result = ada_exception_name_addr_1 (ex);
f7f9143b
JB
12090 }
12091
230d2906 12092 catch (const gdb_exception_error &e)
f7f9143b 12093 {
3d6e9d23 12094 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12095 return 0;
12096 }
12097
12098 return result;
12099}
12100
cb7de75e 12101static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12102 (const char *excep_string,
12103 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12104
12105/* Ada catchpoints.
12106
12107 In the case of catchpoints on Ada exceptions, the catchpoint will
12108 stop the target on every exception the program throws. When a user
12109 specifies the name of a specific exception, we translate this
12110 request into a condition expression (in text form), and then parse
12111 it into an expression stored in each of the catchpoint's locations.
12112 We then use this condition to check whether the exception that was
12113 raised is the one the user is interested in. If not, then the
12114 target is resumed again. We store the name of the requested
12115 exception, in order to be able to re-set the condition expression
12116 when symbols change. */
12117
c1fc2657 12118/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12119
74421c0b 12120struct ada_catchpoint : public code_breakpoint
28010a5d 12121{
73063f51 12122 ada_catchpoint (struct gdbarch *gdbarch_,
bd21b6c9
PA
12123 enum ada_exception_catchpoint_kind kind,
12124 struct symtab_and_line sal,
12125 const char *addr_string_,
12126 bool tempflag,
12127 bool enabled,
12128 bool from_tty)
74421c0b 12129 : code_breakpoint (gdbarch_, bp_catchpoint),
73063f51 12130 m_kind (kind)
37f6a7f4 12131 {
bd21b6c9
PA
12132 add_location (sal);
12133
74421c0b 12134 /* Unlike most code_breakpoint types, Ada catchpoints are
bd21b6c9
PA
12135 pspace-specific. */
12136 gdb_assert (sal.pspace != nullptr);
12137 this->pspace = sal.pspace;
12138
12139 if (from_tty)
12140 {
12141 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12142 if (!loc_gdbarch)
12143 loc_gdbarch = gdbarch;
12144
12145 describe_other_breakpoints (loc_gdbarch,
12146 sal.pspace, sal.pc, sal.section, -1);
12147 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12148 version for exception catchpoints, because two catchpoints
12149 used for different exception names will use the same address.
12150 In this case, a "breakpoint ... also set at..." warning is
12151 unproductive. Besides, the warning phrasing is also a bit
12152 inappropriate, we should use the word catchpoint, and tell
12153 the user what type of catchpoint it is. The above is good
12154 enough for now, though. */
12155 }
12156
12157 enable_state = enabled ? bp_enabled : bp_disabled;
12158 disposition = tempflag ? disp_del : disp_donttouch;
264f9890
PA
12159 locspec = string_to_location_spec (&addr_string_,
12160 language_def (language_ada));
bd21b6c9 12161 language = language_ada;
37f6a7f4
TT
12162 }
12163
ae72050b
TT
12164 struct bp_location *allocate_location () override;
12165 void re_set () override;
12166 void check_status (struct bpstat *bs) override;
7bd86313 12167 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 12168 bool print_one (bp_location **) const override;
b713485d 12169 void print_mention () const override;
4d1ae558 12170 void print_recreate (struct ui_file *fp) const override;
ae72050b 12171
28010a5d 12172 /* The name of the specific exception the user specified. */
bc18fbb5 12173 std::string excep_string;
37f6a7f4
TT
12174
12175 /* What kind of catchpoint this is. */
12176 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12177};
12178
8cd0bf5e
PA
12179/* An instance of this type is used to represent an Ada catchpoint
12180 breakpoint location. */
12181
12182class ada_catchpoint_location : public bp_location
12183{
12184public:
12185 explicit ada_catchpoint_location (ada_catchpoint *owner)
12186 : bp_location (owner, bp_loc_software_breakpoint)
12187 {}
12188
12189 /* The condition that checks whether the exception that was raised
12190 is the specific exception the user specified on catchpoint
12191 creation. */
12192 expression_up excep_cond_expr;
12193};
12194
28010a5d
PA
12195/* Parse the exception condition string in the context of each of the
12196 catchpoint's locations, and store them for later evaluation. */
12197
12198static void
9f757bf7 12199create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 12200 enum ada_exception_catchpoint_kind ex)
28010a5d 12201{
28010a5d 12202 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 12203 if (c->excep_string.empty ())
28010a5d
PA
12204 return;
12205
12206 /* Same if there are no locations... */
c1fc2657 12207 if (c->loc == NULL)
28010a5d
PA
12208 return;
12209
fccf9de1
TT
12210 /* Compute the condition expression in text form, from the specific
12211 expection we want to catch. */
12212 std::string cond_string
12213 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 12214
fccf9de1
TT
12215 /* Iterate over all the catchpoint's locations, and parse an
12216 expression for each. */
40cb8ca5 12217 for (bp_location *bl : c->locations ())
28010a5d
PA
12218 {
12219 struct ada_catchpoint_location *ada_loc
fccf9de1 12220 = (struct ada_catchpoint_location *) bl;
4d01a485 12221 expression_up exp;
28010a5d 12222
fccf9de1 12223 if (!bl->shlib_disabled)
28010a5d 12224 {
bbc13ae3 12225 const char *s;
28010a5d 12226
cb7de75e 12227 s = cond_string.c_str ();
a70b8144 12228 try
28010a5d 12229 {
fccf9de1
TT
12230 exp = parse_exp_1 (&s, bl->address,
12231 block_for_pc (bl->address),
036e657b 12232 0);
28010a5d 12233 }
230d2906 12234 catch (const gdb_exception_error &e)
849f2b52
JB
12235 {
12236 warning (_("failed to reevaluate internal exception condition "
12237 "for catchpoint %d: %s"),
3d6e9d23 12238 c->number, e.what ());
849f2b52 12239 }
28010a5d
PA
12240 }
12241
b22e99fd 12242 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 12243 }
28010a5d
PA
12244}
12245
ae72050b
TT
12246/* Implement the ALLOCATE_LOCATION method in the structure for all
12247 exception catchpoint kinds. */
28010a5d 12248
ae72050b
TT
12249struct bp_location *
12250ada_catchpoint::allocate_location ()
28010a5d 12251{
ae72050b 12252 return new ada_catchpoint_location (this);
28010a5d
PA
12253}
12254
ae72050b
TT
12255/* Implement the RE_SET method in the structure for all exception
12256 catchpoint kinds. */
28010a5d 12257
ae72050b
TT
12258void
12259ada_catchpoint::re_set ()
28010a5d 12260{
28010a5d
PA
12261 /* Call the base class's method. This updates the catchpoint's
12262 locations. */
74421c0b 12263 this->code_breakpoint::re_set ();
28010a5d
PA
12264
12265 /* Reparse the exception conditional expressions. One for each
12266 location. */
ae72050b 12267 create_excep_cond_exprs (this, m_kind);
28010a5d
PA
12268}
12269
12270/* Returns true if we should stop for this breakpoint hit. If the
12271 user specified a specific exception, we only want to cause a stop
12272 if the program thrown that exception. */
12273
7ebaa5f7 12274static bool
28010a5d
PA
12275should_stop_exception (const struct bp_location *bl)
12276{
12277 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12278 const struct ada_catchpoint_location *ada_loc
12279 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12280 bool stop;
28010a5d 12281
37f6a7f4
TT
12282 struct internalvar *var = lookup_internalvar ("_ada_exception");
12283 if (c->m_kind == ada_catch_assert)
12284 clear_internalvar (var);
12285 else
12286 {
12287 try
12288 {
12289 const char *expr;
12290
12291 if (c->m_kind == ada_catch_handlers)
12292 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12293 ".all.occurrence.id");
12294 else
12295 expr = "e";
12296
12297 struct value *exc = parse_and_eval (expr);
12298 set_internalvar (var, exc);
12299 }
12300 catch (const gdb_exception_error &ex)
12301 {
12302 clear_internalvar (var);
12303 }
12304 }
12305
28010a5d 12306 /* With no specific exception, should always stop. */
bc18fbb5 12307 if (c->excep_string.empty ())
7ebaa5f7 12308 return true;
28010a5d
PA
12309
12310 if (ada_loc->excep_cond_expr == NULL)
12311 {
12312 /* We will have a NULL expression if back when we were creating
12313 the expressions, this location's had failed to parse. */
7ebaa5f7 12314 return true;
28010a5d
PA
12315 }
12316
7ebaa5f7 12317 stop = true;
a70b8144 12318 try
28010a5d 12319 {
65558ca5 12320 scoped_value_mark mark;
43048e46 12321 stop = value_true (ada_loc->excep_cond_expr->evaluate ());
28010a5d 12322 }
b1ffd112 12323 catch (const gdb_exception_error &ex)
492d29ea
PA
12324 {
12325 exception_fprintf (gdb_stderr, ex,
12326 _("Error in testing exception condition:\n"));
12327 }
492d29ea 12328
28010a5d
PA
12329 return stop;
12330}
12331
ae72050b
TT
12332/* Implement the CHECK_STATUS method in the structure for all
12333 exception catchpoint kinds. */
28010a5d 12334
ae72050b
TT
12335void
12336ada_catchpoint::check_status (bpstat *bs)
28010a5d 12337{
b6433ede 12338 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12339}
12340
ae72050b
TT
12341/* Implement the PRINT_IT method in the structure for all exception
12342 catchpoint kinds. */
f7f9143b 12343
ae72050b 12344enum print_stop_action
7bd86313 12345ada_catchpoint::print_it (const bpstat *bs) const
f7f9143b 12346{
79a45e25 12347 struct ui_out *uiout = current_uiout;
348d480f 12348
ae72050b 12349 annotate_catchpoint (number);
f7f9143b 12350
112e8700 12351 if (uiout->is_mi_like_p ())
f7f9143b 12352 {
112e8700 12353 uiout->field_string ("reason",
956a9fb9 12354 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ae72050b 12355 uiout->field_string ("disp", bpdisp_text (disposition));
f7f9143b
JB
12356 }
12357
ae72050b 12358 uiout->text (disposition == disp_del
112e8700 12359 ? "\nTemporary catchpoint " : "\nCatchpoint ");
78805ff8 12360 print_num_locno (bs, uiout);
112e8700 12361 uiout->text (", ");
f7f9143b 12362
45db7c09
PA
12363 /* ada_exception_name_addr relies on the selected frame being the
12364 current frame. Need to do this here because this function may be
12365 called more than once when printing a stop, and below, we'll
12366 select the first frame past the Ada run-time (see
12367 ada_find_printable_frame). */
12368 select_frame (get_current_frame ());
12369
ae72050b 12370 switch (m_kind)
f7f9143b 12371 {
761269c8
JB
12372 case ada_catch_exception:
12373 case ada_catch_exception_unhandled:
9f757bf7 12374 case ada_catch_handlers:
956a9fb9 12375 {
7bd86313 12376 const CORE_ADDR addr = ada_exception_name_addr (m_kind);
956a9fb9
JB
12377 char exception_name[256];
12378
12379 if (addr != 0)
12380 {
c714b426
PA
12381 read_memory (addr, (gdb_byte *) exception_name,
12382 sizeof (exception_name) - 1);
956a9fb9
JB
12383 exception_name [sizeof (exception_name) - 1] = '\0';
12384 }
12385 else
12386 {
12387 /* For some reason, we were unable to read the exception
12388 name. This could happen if the Runtime was compiled
12389 without debugging info, for instance. In that case,
12390 just replace the exception name by the generic string
12391 "exception" - it will read as "an exception" in the
12392 notification we are about to print. */
967cff16 12393 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12394 }
12395 /* In the case of unhandled exception breakpoints, we print
12396 the exception name as "unhandled EXCEPTION_NAME", to make
12397 it clearer to the user which kind of catchpoint just got
12398 hit. We used ui_out_text to make sure that this extra
12399 info does not pollute the exception name in the MI case. */
ae72050b 12400 if (m_kind == ada_catch_exception_unhandled)
112e8700
SM
12401 uiout->text ("unhandled ");
12402 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12403 }
12404 break;
761269c8 12405 case ada_catch_assert:
956a9fb9
JB
12406 /* In this case, the name of the exception is not really
12407 important. Just print "failed assertion" to make it clearer
12408 that his program just hit an assertion-failure catchpoint.
12409 We used ui_out_text because this info does not belong in
12410 the MI output. */
112e8700 12411 uiout->text ("failed assertion");
956a9fb9 12412 break;
f7f9143b 12413 }
e547c119 12414
6f46ac85 12415 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12416 if (exception_message != NULL)
12417 {
e547c119 12418 uiout->text (" (");
6f46ac85 12419 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12420 uiout->text (")");
e547c119
JB
12421 }
12422
112e8700 12423 uiout->text (" at ");
956a9fb9 12424 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12425
12426 return PRINT_SRC_AND_LOC;
12427}
12428
ae72050b
TT
12429/* Implement the PRINT_ONE method in the structure for all exception
12430 catchpoint kinds. */
f7f9143b 12431
ae72050b 12432bool
a67bcaba 12433ada_catchpoint::print_one (bp_location **last_loc) const
f7f9143b 12434{
79a45e25 12435 struct ui_out *uiout = current_uiout;
79a45b7d
TT
12436 struct value_print_options opts;
12437
12438 get_user_print_options (&opts);
f06f1252 12439
79a45b7d 12440 if (opts.addressprint)
f06f1252 12441 uiout->field_skip ("addr");
f7f9143b
JB
12442
12443 annotate_field (5);
ae72050b 12444 switch (m_kind)
f7f9143b 12445 {
761269c8 12446 case ada_catch_exception:
ae72050b 12447 if (!excep_string.empty ())
dda83cd7 12448 {
bc18fbb5 12449 std::string msg = string_printf (_("`%s' Ada exception"),
ae72050b 12450 excep_string.c_str ());
28010a5d 12451
dda83cd7
SM
12452 uiout->field_string ("what", msg);
12453 }
12454 else
12455 uiout->field_string ("what", "all Ada exceptions");
12456
12457 break;
f7f9143b 12458
761269c8 12459 case ada_catch_exception_unhandled:
dda83cd7
SM
12460 uiout->field_string ("what", "unhandled Ada exceptions");
12461 break;
f7f9143b 12462
9f757bf7 12463 case ada_catch_handlers:
ae72050b 12464 if (!excep_string.empty ())
dda83cd7 12465 {
9f757bf7
XR
12466 uiout->field_fmt ("what",
12467 _("`%s' Ada exception handlers"),
ae72050b 12468 excep_string.c_str ());
dda83cd7
SM
12469 }
12470 else
9f757bf7 12471 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12472 break;
9f757bf7 12473
761269c8 12474 case ada_catch_assert:
dda83cd7
SM
12475 uiout->field_string ("what", "failed Ada assertions");
12476 break;
f7f9143b
JB
12477
12478 default:
f34652de 12479 internal_error (_("unexpected catchpoint type"));
dda83cd7 12480 break;
f7f9143b 12481 }
c01e038b
TT
12482
12483 return true;
f7f9143b
JB
12484}
12485
12486/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12487 for all exception catchpoint kinds. */
12488
ae72050b 12489void
b713485d 12490ada_catchpoint::print_mention () const
f7f9143b 12491{
79a45e25 12492 struct ui_out *uiout = current_uiout;
28010a5d 12493
ae72050b 12494 uiout->text (disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12495 : _("Catchpoint "));
ae72050b 12496 uiout->field_signed ("bkptno", number);
112e8700 12497 uiout->text (": ");
00eb2c4a 12498
ae72050b 12499 switch (m_kind)
f7f9143b 12500 {
761269c8 12501 case ada_catch_exception:
ae72050b 12502 if (!excep_string.empty ())
00eb2c4a 12503 {
862d101a 12504 std::string info = string_printf (_("`%s' Ada exception"),
ae72050b 12505 excep_string.c_str ());
4915bfdc 12506 uiout->text (info);
00eb2c4a 12507 }
dda83cd7
SM
12508 else
12509 uiout->text (_("all Ada exceptions"));
12510 break;
f7f9143b 12511
761269c8 12512 case ada_catch_exception_unhandled:
dda83cd7
SM
12513 uiout->text (_("unhandled Ada exceptions"));
12514 break;
9f757bf7
XR
12515
12516 case ada_catch_handlers:
ae72050b 12517 if (!excep_string.empty ())
9f757bf7
XR
12518 {
12519 std::string info
12520 = string_printf (_("`%s' Ada exception handlers"),
ae72050b 12521 excep_string.c_str ());
4915bfdc 12522 uiout->text (info);
9f757bf7 12523 }
dda83cd7
SM
12524 else
12525 uiout->text (_("all Ada exceptions handlers"));
12526 break;
9f757bf7 12527
761269c8 12528 case ada_catch_assert:
dda83cd7
SM
12529 uiout->text (_("failed Ada assertions"));
12530 break;
f7f9143b
JB
12531
12532 default:
f34652de 12533 internal_error (_("unexpected catchpoint type"));
dda83cd7 12534 break;
f7f9143b
JB
12535 }
12536}
12537
ae72050b
TT
12538/* Implement the PRINT_RECREATE method in the structure for all
12539 exception catchpoint kinds. */
6149aea9 12540
ae72050b 12541void
4d1ae558 12542ada_catchpoint::print_recreate (struct ui_file *fp) const
6149aea9 12543{
ae72050b 12544 switch (m_kind)
6149aea9 12545 {
761269c8 12546 case ada_catch_exception:
6cb06a8c 12547 gdb_printf (fp, "catch exception");
ae72050b
TT
12548 if (!excep_string.empty ())
12549 gdb_printf (fp, " %s", excep_string.c_str ());
6149aea9
PA
12550 break;
12551
761269c8 12552 case ada_catch_exception_unhandled:
6cb06a8c 12553 gdb_printf (fp, "catch exception unhandled");
6149aea9
PA
12554 break;
12555
9f757bf7 12556 case ada_catch_handlers:
6cb06a8c 12557 gdb_printf (fp, "catch handlers");
9f757bf7
XR
12558 break;
12559
761269c8 12560 case ada_catch_assert:
6cb06a8c 12561 gdb_printf (fp, "catch assert");
6149aea9
PA
12562 break;
12563
12564 default:
f34652de 12565 internal_error (_("unexpected catchpoint type"));
6149aea9 12566 }
04d0163c 12567 print_recreate_thread (fp);
6149aea9
PA
12568}
12569
f06f1252
TT
12570/* See ada-lang.h. */
12571
12572bool
12573is_ada_exception_catchpoint (breakpoint *bp)
12574{
ae72050b 12575 return dynamic_cast<ada_catchpoint *> (bp) != nullptr;
f06f1252
TT
12576}
12577
f7f9143b
JB
12578/* Split the arguments specified in a "catch exception" command.
12579 Set EX to the appropriate catchpoint type.
28010a5d 12580 Set EXCEP_STRING to the name of the specific exception if
5845583d 12581 specified by the user.
9f757bf7
XR
12582 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12583 "catch handlers" command. False otherwise.
5845583d
JB
12584 If a condition is found at the end of the arguments, the condition
12585 expression is stored in COND_STRING (memory must be deallocated
12586 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12587
12588static void
a121b7c1 12589catch_ada_exception_command_split (const char *args,
9f757bf7 12590 bool is_catch_handlers_cmd,
dda83cd7 12591 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12592 std::string *excep_string,
12593 std::string *cond_string)
f7f9143b 12594{
bc18fbb5 12595 std::string exception_name;
f7f9143b 12596
bc18fbb5
TT
12597 exception_name = extract_arg (&args);
12598 if (exception_name == "if")
5845583d
JB
12599 {
12600 /* This is not an exception name; this is the start of a condition
12601 expression for a catchpoint on all exceptions. So, "un-get"
12602 this token, and set exception_name to NULL. */
bc18fbb5 12603 exception_name.clear ();
5845583d
JB
12604 args -= 2;
12605 }
f7f9143b 12606
5845583d 12607 /* Check to see if we have a condition. */
f7f9143b 12608
f1735a53 12609 args = skip_spaces (args);
61012eef 12610 if (startswith (args, "if")
5845583d
JB
12611 && (isspace (args[2]) || args[2] == '\0'))
12612 {
12613 args += 2;
f1735a53 12614 args = skip_spaces (args);
5845583d
JB
12615
12616 if (args[0] == '\0')
dda83cd7 12617 error (_("Condition missing after `if' keyword"));
bc18fbb5 12618 *cond_string = args;
5845583d
JB
12619
12620 args += strlen (args);
12621 }
12622
12623 /* Check that we do not have any more arguments. Anything else
12624 is unexpected. */
f7f9143b
JB
12625
12626 if (args[0] != '\0')
12627 error (_("Junk at end of expression"));
12628
9f757bf7
XR
12629 if (is_catch_handlers_cmd)
12630 {
12631 /* Catch handling of exceptions. */
12632 *ex = ada_catch_handlers;
12633 *excep_string = exception_name;
12634 }
bc18fbb5 12635 else if (exception_name.empty ())
f7f9143b
JB
12636 {
12637 /* Catch all exceptions. */
761269c8 12638 *ex = ada_catch_exception;
bc18fbb5 12639 excep_string->clear ();
f7f9143b 12640 }
bc18fbb5 12641 else if (exception_name == "unhandled")
f7f9143b
JB
12642 {
12643 /* Catch unhandled exceptions. */
761269c8 12644 *ex = ada_catch_exception_unhandled;
bc18fbb5 12645 excep_string->clear ();
f7f9143b
JB
12646 }
12647 else
12648 {
12649 /* Catch a specific exception. */
761269c8 12650 *ex = ada_catch_exception;
28010a5d 12651 *excep_string = exception_name;
f7f9143b
JB
12652 }
12653}
12654
12655/* Return the name of the symbol on which we should break in order to
12656 implement a catchpoint of the EX kind. */
12657
12658static const char *
761269c8 12659ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12660{
3eecfa55
JB
12661 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12662
12663 gdb_assert (data->exception_info != NULL);
0259addd 12664
f7f9143b
JB
12665 switch (ex)
12666 {
761269c8 12667 case ada_catch_exception:
dda83cd7
SM
12668 return (data->exception_info->catch_exception_sym);
12669 break;
761269c8 12670 case ada_catch_exception_unhandled:
dda83cd7
SM
12671 return (data->exception_info->catch_exception_unhandled_sym);
12672 break;
761269c8 12673 case ada_catch_assert:
dda83cd7
SM
12674 return (data->exception_info->catch_assert_sym);
12675 break;
9f757bf7 12676 case ada_catch_handlers:
dda83cd7
SM
12677 return (data->exception_info->catch_handlers_sym);
12678 break;
f7f9143b 12679 default:
f34652de 12680 internal_error (_("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12681 }
12682}
12683
f7f9143b
JB
12684/* Return the condition that will be used to match the current exception
12685 being raised with the exception that the user wants to catch. This
12686 assumes that this condition is used when the inferior just triggered
12687 an exception catchpoint.
cb7de75e 12688 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12689
cb7de75e 12690static std::string
9f757bf7 12691ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12692 enum ada_exception_catchpoint_kind ex)
f7f9143b 12693{
fccf9de1 12694 bool is_standard_exc = false;
cb7de75e 12695 std::string result;
9f757bf7
XR
12696
12697 if (ex == ada_catch_handlers)
12698 {
12699 /* For exception handlers catchpoints, the condition string does
dda83cd7 12700 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12701 result = ("long_integer (GNAT_GCC_exception_Access"
12702 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12703 }
12704 else
fccf9de1 12705 result = "long_integer (e)";
3d0b0fa3 12706
0963b4bd 12707 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12708 runtime units that have been compiled without debugging info; if
28010a5d 12709 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12710 exception (e.g. "constraint_error") then, during the evaluation
12711 of the condition expression, the symbol lookup on this name would
0963b4bd 12712 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12713 may then be set only on user-defined exceptions which have the
12714 same not-fully-qualified name (e.g. my_package.constraint_error).
12715
12716 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12717 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12718 exception constraint_error" is rewritten into "catch exception
12719 standard.constraint_error".
12720
85102364 12721 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12722 the inferior program, then the only way to specify this exception as a
12723 breakpoint condition is to use its fully-qualified named:
fccf9de1 12724 e.g. my_package.constraint_error. */
3d0b0fa3 12725
696d6f4d 12726 for (const char *name : standard_exc)
3d0b0fa3 12727 {
696d6f4d 12728 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12729 {
fccf9de1 12730 is_standard_exc = true;
9f757bf7 12731 break;
3d0b0fa3
JB
12732 }
12733 }
9f757bf7 12734
fccf9de1
TT
12735 result += " = ";
12736
12737 if (is_standard_exc)
12738 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12739 else
12740 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12741
9f757bf7 12742 return result;
f7f9143b
JB
12743}
12744
12745/* Return the symtab_and_line that should be used to insert an exception
12746 catchpoint of the TYPE kind.
12747
28010a5d
PA
12748 ADDR_STRING returns the name of the function where the real
12749 breakpoint that implements the catchpoints is set, depending on the
12750 type of catchpoint we need to create. */
f7f9143b
JB
12751
12752static struct symtab_and_line
bc18fbb5 12753ada_exception_sal (enum ada_exception_catchpoint_kind ex,
ae72050b 12754 std::string *addr_string)
f7f9143b
JB
12755{
12756 const char *sym_name;
12757 struct symbol *sym;
f7f9143b 12758
0259addd
JB
12759 /* First, find out which exception support info to use. */
12760 ada_exception_support_info_sniffer ();
12761
12762 /* Then lookup the function on which we will break in order to catch
f7f9143b 12763 the Ada exceptions requested by the user. */
f7f9143b
JB
12764 sym_name = ada_exception_sym_name (ex);
12765 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12766
57aff202
JB
12767 if (sym == NULL)
12768 error (_("Catchpoint symbol not found: %s"), sym_name);
12769
66d7f48f 12770 if (sym->aclass () != LOC_BLOCK)
57aff202 12771 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12772
12773 /* Set ADDR_STRING. */
cc12f4a8 12774 *addr_string = sym_name;
f7f9143b 12775
f17011e0 12776 return find_function_start_sal (sym, 1);
f7f9143b
JB
12777}
12778
b4a5b78b 12779/* Create an Ada exception catchpoint.
f7f9143b 12780
b4a5b78b 12781 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12782
bc18fbb5 12783 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12784 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12785 of the exception to which this catchpoint applies.
2df4d1d5 12786
bc18fbb5 12787 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12788
b4a5b78b
JB
12789 TEMPFLAG, if nonzero, means that the underlying breakpoint
12790 should be temporary.
28010a5d 12791
b4a5b78b 12792 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12793
349774ef 12794void
28010a5d 12795create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12796 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12797 const std::string &excep_string,
56ecd069 12798 const std::string &cond_string,
28010a5d 12799 int tempflag,
12d67b37 12800 int enabled,
28010a5d
PA
12801 int from_tty)
12802{
cc12f4a8 12803 std::string addr_string;
ae72050b 12804 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string);
28010a5d 12805
bd21b6c9
PA
12806 std::unique_ptr<ada_catchpoint> c
12807 (new ada_catchpoint (gdbarch, ex_kind, sal, addr_string.c_str (),
12d67b37 12808 tempflag, enabled, from_tty));
28010a5d 12809 c->excep_string = excep_string;
9f757bf7 12810 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12811 if (!cond_string.empty ())
733d554a 12812 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12813 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12814}
12815
9ac4176b
PA
12816/* Implement the "catch exception" command. */
12817
12818static void
eb4c3f4a 12819catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12820 struct cmd_list_element *command)
12821{
a121b7c1 12822 const char *arg = arg_entry;
9ac4176b
PA
12823 struct gdbarch *gdbarch = get_current_arch ();
12824 int tempflag;
761269c8 12825 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12826 std::string excep_string;
56ecd069 12827 std::string cond_string;
9ac4176b 12828
0f8e2034 12829 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12830
12831 if (!arg)
12832 arg = "";
9f757bf7 12833 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12834 &cond_string);
9f757bf7
XR
12835 create_ada_exception_catchpoint (gdbarch, ex_kind,
12836 excep_string, cond_string,
12837 tempflag, 1 /* enabled */,
12838 from_tty);
12839}
12840
12841/* Implement the "catch handlers" command. */
12842
12843static void
12844catch_ada_handlers_command (const char *arg_entry, int from_tty,
12845 struct cmd_list_element *command)
12846{
12847 const char *arg = arg_entry;
12848 struct gdbarch *gdbarch = get_current_arch ();
12849 int tempflag;
12850 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12851 std::string excep_string;
56ecd069 12852 std::string cond_string;
9f757bf7 12853
0f8e2034 12854 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12855
12856 if (!arg)
12857 arg = "";
12858 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12859 &cond_string);
b4a5b78b
JB
12860 create_ada_exception_catchpoint (gdbarch, ex_kind,
12861 excep_string, cond_string,
349774ef
JB
12862 tempflag, 1 /* enabled */,
12863 from_tty);
9ac4176b
PA
12864}
12865
71bed2db
TT
12866/* Completion function for the Ada "catch" commands. */
12867
12868static void
12869catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12870 const char *text, const char *word)
12871{
12872 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12873
12874 for (const ada_exc_info &info : exceptions)
12875 {
12876 if (startswith (info.name, word))
b02f78f9 12877 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12878 }
12879}
12880
b4a5b78b 12881/* Split the arguments specified in a "catch assert" command.
5845583d 12882
b4a5b78b
JB
12883 ARGS contains the command's arguments (or the empty string if
12884 no arguments were passed).
5845583d
JB
12885
12886 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12887 (the memory needs to be deallocated after use). */
5845583d 12888
b4a5b78b 12889static void
56ecd069 12890catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12891{
f1735a53 12892 args = skip_spaces (args);
f7f9143b 12893
5845583d 12894 /* Check whether a condition was provided. */
61012eef 12895 if (startswith (args, "if")
5845583d 12896 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12897 {
5845583d 12898 args += 2;
f1735a53 12899 args = skip_spaces (args);
5845583d 12900 if (args[0] == '\0')
dda83cd7 12901 error (_("condition missing after `if' keyword"));
56ecd069 12902 cond_string.assign (args);
f7f9143b
JB
12903 }
12904
5845583d
JB
12905 /* Otherwise, there should be no other argument at the end of
12906 the command. */
12907 else if (args[0] != '\0')
12908 error (_("Junk at end of arguments."));
f7f9143b
JB
12909}
12910
9ac4176b
PA
12911/* Implement the "catch assert" command. */
12912
12913static void
eb4c3f4a 12914catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12915 struct cmd_list_element *command)
12916{
a121b7c1 12917 const char *arg = arg_entry;
9ac4176b
PA
12918 struct gdbarch *gdbarch = get_current_arch ();
12919 int tempflag;
56ecd069 12920 std::string cond_string;
9ac4176b 12921
0f8e2034 12922 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12923
12924 if (!arg)
12925 arg = "";
56ecd069 12926 catch_ada_assert_command_split (arg, cond_string);
761269c8 12927 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 12928 "", cond_string,
349774ef
JB
12929 tempflag, 1 /* enabled */,
12930 from_tty);
9ac4176b 12931}
778865d3
JB
12932
12933/* Return non-zero if the symbol SYM is an Ada exception object. */
12934
12935static int
12936ada_is_exception_sym (struct symbol *sym)
12937{
5f9c5a63 12938 const char *type_name = sym->type ()->name ();
778865d3 12939
66d7f48f
SM
12940 return (sym->aclass () != LOC_TYPEDEF
12941 && sym->aclass () != LOC_BLOCK
12942 && sym->aclass () != LOC_CONST
12943 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12944 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12945}
12946
12947/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12948 Ada exception object. This matches all exceptions except the ones
12949 defined by the Ada language. */
12950
12951static int
12952ada_is_non_standard_exception_sym (struct symbol *sym)
12953{
778865d3
JB
12954 if (!ada_is_exception_sym (sym))
12955 return 0;
12956
696d6f4d
TT
12957 for (const char *name : standard_exc)
12958 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12959 return 0; /* A standard exception. */
12960
12961 /* Numeric_Error is also a standard exception, so exclude it.
12962 See the STANDARD_EXC description for more details as to why
12963 this exception is not listed in that array. */
987012b8 12964 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12965 return 0;
12966
12967 return 1;
12968}
12969
ab816a27 12970/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12971 objects.
12972
12973 The comparison is determined first by exception name, and then
12974 by exception address. */
12975
ab816a27 12976bool
cc536b21 12977ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12978{
778865d3
JB
12979 int result;
12980
ab816a27
TT
12981 result = strcmp (name, other.name);
12982 if (result < 0)
12983 return true;
12984 if (result == 0 && addr < other.addr)
12985 return true;
12986 return false;
12987}
778865d3 12988
ab816a27 12989bool
cc536b21 12990ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12991{
12992 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12993}
12994
12995/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12996 routine, but keeping the first SKIP elements untouched.
12997
12998 All duplicates are also removed. */
12999
13000static void
ab816a27 13001sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
13002 int skip)
13003{
ab816a27
TT
13004 std::sort (exceptions->begin () + skip, exceptions->end ());
13005 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
13006 exceptions->end ());
778865d3
JB
13007}
13008
778865d3
JB
13009/* Add all exceptions defined by the Ada standard whose name match
13010 a regular expression.
13011
13012 If PREG is not NULL, then this regexp_t object is used to
13013 perform the symbol name matching. Otherwise, no name-based
13014 filtering is performed.
13015
13016 EXCEPTIONS is a vector of exceptions to which matching exceptions
13017 gets pushed. */
13018
13019static void
2d7cc5c7 13020ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 13021 std::vector<ada_exc_info> *exceptions)
778865d3 13022{
696d6f4d 13023 for (const char *name : standard_exc)
778865d3 13024 {
696d6f4d 13025 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3 13026 {
4326580d
MM
13027 symbol_name_match_type match_type = name_match_type_from_name (name);
13028 lookup_name_info lookup_name (name, match_type);
778865d3 13029
4326580d
MM
13030 symbol_name_matcher_ftype *match_name
13031 = ada_get_symbol_name_matcher (lookup_name);
778865d3 13032
4326580d
MM
13033 /* Iterate over all objfiles irrespective of scope or linker
13034 namespaces so we get all exceptions anywhere in the
13035 progspace. */
13036 for (objfile *objfile : current_program_space->objfiles ())
13037 {
13038 for (minimal_symbol *msymbol : objfile->msymbols ())
13039 {
13040 if (match_name (msymbol->linkage_name (), lookup_name,
13041 nullptr)
13042 && msymbol->type () != mst_solib_trampoline)
13043 {
13044 ada_exc_info info
13045 = {name, msymbol->value_address (objfile)};
13046
13047 exceptions->push_back (info);
13048 }
13049 }
778865d3
JB
13050 }
13051 }
13052 }
13053}
13054
13055/* Add all Ada exceptions defined locally and accessible from the given
13056 FRAME.
13057
13058 If PREG is not NULL, then this regexp_t object is used to
13059 perform the symbol name matching. Otherwise, no name-based
13060 filtering is performed.
13061
13062 EXCEPTIONS is a vector of exceptions to which matching exceptions
13063 gets pushed. */
13064
13065static void
2d7cc5c7 13066ada_add_exceptions_from_frame (compiled_regex *preg,
bd2b40ac 13067 frame_info_ptr frame,
ab816a27 13068 std::vector<ada_exc_info> *exceptions)
778865d3 13069{
3977b71f 13070 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
13071
13072 while (block != 0)
13073 {
548a89df 13074 for (struct symbol *sym : block_iterator_range (block))
778865d3 13075 {
66d7f48f 13076 switch (sym->aclass ())
778865d3
JB
13077 {
13078 case LOC_TYPEDEF:
13079 case LOC_BLOCK:
13080 case LOC_CONST:
13081 break;
13082 default:
13083 if (ada_is_exception_sym (sym))
13084 {
987012b8 13085 struct ada_exc_info info = {sym->print_name (),
4aeddc50 13086 sym->value_address ()};
778865d3 13087
ab816a27 13088 exceptions->push_back (info);
778865d3
JB
13089 }
13090 }
13091 }
6c00f721 13092 if (block->function () != NULL)
778865d3 13093 break;
f135fe72 13094 block = block->superblock ();
778865d3
JB
13095 }
13096}
13097
14bc53a8
PA
13098/* Return true if NAME matches PREG or if PREG is NULL. */
13099
13100static bool
2d7cc5c7 13101name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
13102{
13103 return (preg == NULL
f945dedf 13104 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
13105}
13106
778865d3
JB
13107/* Add all exceptions defined globally whose name name match
13108 a regular expression, excluding standard exceptions.
13109
13110 The reason we exclude standard exceptions is that they need
13111 to be handled separately: Standard exceptions are defined inside
13112 a runtime unit which is normally not compiled with debugging info,
13113 and thus usually do not show up in our symbol search. However,
13114 if the unit was in fact built with debugging info, we need to
13115 exclude them because they would duplicate the entry we found
13116 during the special loop that specifically searches for those
13117 standard exceptions.
13118
13119 If PREG is not NULL, then this regexp_t object is used to
13120 perform the symbol name matching. Otherwise, no name-based
13121 filtering is performed.
13122
13123 EXCEPTIONS is a vector of exceptions to which matching exceptions
13124 gets pushed. */
13125
13126static void
2d7cc5c7 13127ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13128 std::vector<ada_exc_info> *exceptions)
778865d3 13129{
14bc53a8
PA
13130 /* In Ada, the symbol "search name" is a linkage name, whereas the
13131 regular expression used to do the matching refers to the natural
13132 name. So match against the decoded name. */
13133 expand_symtabs_matching (NULL,
b5ec771e 13134 lookup_name_info::match_any (),
14bc53a8
PA
13135 [&] (const char *search_name)
13136 {
f945dedf
CB
13137 std::string decoded = ada_decode (search_name);
13138 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13139 },
13140 NULL,
03a8ea51 13141 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 13142 VARIABLES_DOMAIN);
778865d3 13143
4326580d
MM
13144 /* Iterate over all objfiles irrespective of scope or linker namespaces
13145 so we get all exceptions anywhere in the progspace. */
2030c079 13146 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13147 {
b669c953 13148 for (compunit_symtab *s : objfile->compunits ())
778865d3 13149 {
af39c5c8 13150 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13151 int i;
778865d3 13152
d8aeb77f
TT
13153 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13154 {
63d609de 13155 const struct block *b = bv->block (i);
778865d3 13156
548a89df 13157 for (struct symbol *sym : block_iterator_range (b))
d8aeb77f 13158 if (ada_is_non_standard_exception_sym (sym)
987012b8 13159 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13160 {
13161 struct ada_exc_info info
4aeddc50 13162 = {sym->print_name (), sym->value_address ()};
d8aeb77f
TT
13163
13164 exceptions->push_back (info);
13165 }
13166 }
778865d3
JB
13167 }
13168 }
13169}
13170
13171/* Implements ada_exceptions_list with the regular expression passed
13172 as a regex_t, rather than a string.
13173
13174 If not NULL, PREG is used to filter out exceptions whose names
13175 do not match. Otherwise, all exceptions are listed. */
13176
ab816a27 13177static std::vector<ada_exc_info>
2d7cc5c7 13178ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13179{
ab816a27 13180 std::vector<ada_exc_info> result;
778865d3
JB
13181 int prev_len;
13182
13183 /* First, list the known standard exceptions. These exceptions
13184 need to be handled separately, as they are usually defined in
13185 runtime units that have been compiled without debugging info. */
13186
13187 ada_add_standard_exceptions (preg, &result);
13188
13189 /* Next, find all exceptions whose scope is local and accessible
13190 from the currently selected frame. */
13191
13192 if (has_stack_frames ())
13193 {
ab816a27 13194 prev_len = result.size ();
778865d3
JB
13195 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13196 &result);
ab816a27 13197 if (result.size () > prev_len)
778865d3
JB
13198 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13199 }
13200
13201 /* Add all exceptions whose scope is global. */
13202
ab816a27 13203 prev_len = result.size ();
778865d3 13204 ada_add_global_exceptions (preg, &result);
ab816a27 13205 if (result.size () > prev_len)
778865d3
JB
13206 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13207
778865d3
JB
13208 return result;
13209}
13210
13211/* Return a vector of ada_exc_info.
13212
13213 If REGEXP is NULL, all exceptions are included in the result.
13214 Otherwise, it should contain a valid regular expression,
13215 and only the exceptions whose names match that regular expression
13216 are included in the result.
13217
13218 The exceptions are sorted in the following order:
13219 - Standard exceptions (defined by the Ada language), in
13220 alphabetical order;
13221 - Exceptions only visible from the current frame, in
13222 alphabetical order;
13223 - Exceptions whose scope is global, in alphabetical order. */
13224
ab816a27 13225std::vector<ada_exc_info>
778865d3
JB
13226ada_exceptions_list (const char *regexp)
13227{
2d7cc5c7
PA
13228 if (regexp == NULL)
13229 return ada_exceptions_list_1 (NULL);
778865d3 13230
2d7cc5c7
PA
13231 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13232 return ada_exceptions_list_1 (&reg);
778865d3
JB
13233}
13234
13235/* Implement the "info exceptions" command. */
13236
13237static void
1d12d88f 13238info_exceptions_command (const char *regexp, int from_tty)
778865d3 13239{
778865d3 13240 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13241
ab816a27 13242 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13243
13244 if (regexp != NULL)
6cb06a8c 13245 gdb_printf
778865d3
JB
13246 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13247 else
6cb06a8c 13248 gdb_printf (_("All defined Ada exceptions:\n"));
778865d3 13249
ab816a27 13250 for (const ada_exc_info &info : exceptions)
6cb06a8c 13251 gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13252}
13253
6c038f32
PH
13254\f
13255 /* Language vector */
13256
b5ec771e
PA
13257/* symbol_name_matcher_ftype adapter for wild_match. */
13258
13259static bool
13260do_wild_match (const char *symbol_search_name,
13261 const lookup_name_info &lookup_name,
a207cff2 13262 completion_match_result *comp_match_res)
b5ec771e
PA
13263{
13264 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13265}
13266
13267/* symbol_name_matcher_ftype adapter for full_match. */
13268
13269static bool
13270do_full_match (const char *symbol_search_name,
13271 const lookup_name_info &lookup_name,
a207cff2 13272 completion_match_result *comp_match_res)
b5ec771e 13273{
959d6a67
TT
13274 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13275
13276 /* If both symbols start with "_ada_", just let the loop below
13277 handle the comparison. However, if only the symbol name starts
13278 with "_ada_", skip the prefix and let the match proceed as
13279 usual. */
13280 if (startswith (symbol_search_name, "_ada_")
13281 && !startswith (lname, "_ada"))
86b44259 13282 symbol_search_name += 5;
81eaa506
TT
13283 /* Likewise for ghost entities. */
13284 if (startswith (symbol_search_name, "___ghost_")
13285 && !startswith (lname, "___ghost_"))
13286 symbol_search_name += 9;
86b44259 13287
86b44259
TT
13288 int uscore_count = 0;
13289 while (*lname != '\0')
13290 {
13291 if (*symbol_search_name != *lname)
13292 {
13293 if (*symbol_search_name == 'B' && uscore_count == 2
13294 && symbol_search_name[1] == '_')
13295 {
13296 symbol_search_name += 2;
13297 while (isdigit (*symbol_search_name))
13298 ++symbol_search_name;
13299 if (symbol_search_name[0] == '_'
13300 && symbol_search_name[1] == '_')
13301 {
13302 symbol_search_name += 2;
13303 continue;
13304 }
13305 }
13306 return false;
13307 }
13308
13309 if (*symbol_search_name == '_')
13310 ++uscore_count;
13311 else
13312 uscore_count = 0;
13313
13314 ++symbol_search_name;
13315 ++lname;
13316 }
13317
13318 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13319}
13320
a2cd4f14
JB
13321/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13322
13323static bool
13324do_exact_match (const char *symbol_search_name,
13325 const lookup_name_info &lookup_name,
13326 completion_match_result *comp_match_res)
13327{
13328 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13329}
13330
b5ec771e
PA
13331/* Build the Ada lookup name for LOOKUP_NAME. */
13332
13333ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13334{
e0802d59 13335 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13336
6a780b67 13337 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13338 {
13339 if (user_name.back () == '>')
e0802d59 13340 m_encoded_name
5ac58899 13341 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13342 else
e0802d59 13343 m_encoded_name
5ac58899 13344 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13345 m_encoded_p = true;
13346 m_verbatim_p = true;
13347 m_wild_match_p = false;
13348 m_standard_p = false;
13349 }
13350 else
13351 {
13352 m_verbatim_p = false;
13353
e0802d59 13354 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13355
13356 if (!m_encoded_p)
13357 {
e0802d59 13358 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13359 m_encoded_name = ada_encode_1 (folded, false);
13360 if (m_encoded_name.empty ())
5ac58899 13361 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13362 }
13363 else
5ac58899 13364 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13365
13366 /* Handle the 'package Standard' special case. See description
13367 of m_standard_p. */
13368 if (startswith (m_encoded_name.c_str (), "standard__"))
13369 {
13370 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13371 m_standard_p = true;
13372 }
13373 else
13374 m_standard_p = false;
74ccd7f5 13375
b5ec771e
PA
13376 /* If the name contains a ".", then the user is entering a fully
13377 qualified entity name, and the match must not be done in wild
13378 mode. Similarly, if the user wants to complete what looks
13379 like an encoded name, the match must not be done in wild
13380 mode. Also, in the standard__ special case always do
13381 non-wild matching. */
13382 m_wild_match_p
13383 = (lookup_name.match_type () != symbol_name_match_type::FULL
13384 && !m_encoded_p
13385 && !m_standard_p
13386 && user_name.find ('.') == std::string::npos);
13387 }
13388}
13389
13390/* symbol_name_matcher_ftype method for Ada. This only handles
13391 completion mode. */
13392
13393static bool
13394ada_symbol_name_matches (const char *symbol_search_name,
13395 const lookup_name_info &lookup_name,
a207cff2 13396 completion_match_result *comp_match_res)
74ccd7f5 13397{
b5ec771e
PA
13398 return lookup_name.ada ().matches (symbol_search_name,
13399 lookup_name.match_type (),
a207cff2 13400 comp_match_res);
b5ec771e
PA
13401}
13402
de63c46b
PA
13403/* A name matcher that matches the symbol name exactly, with
13404 strcmp. */
13405
13406static bool
13407literal_symbol_name_matcher (const char *symbol_search_name,
13408 const lookup_name_info &lookup_name,
13409 completion_match_result *comp_match_res)
13410{
e0802d59 13411 gdb::string_view name_view = lookup_name.name ();
de63c46b 13412
e0802d59
TT
13413 if (lookup_name.completion_mode ()
13414 ? (strncmp (symbol_search_name, name_view.data (),
13415 name_view.size ()) == 0)
13416 : symbol_search_name == name_view)
de63c46b
PA
13417 {
13418 if (comp_match_res != NULL)
13419 comp_match_res->set_match (symbol_search_name);
13420 return true;
13421 }
13422 else
13423 return false;
13424}
13425
c9debfb9 13426/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13427 Ada. */
13428
13429static symbol_name_matcher_ftype *
13430ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13431{
de63c46b
PA
13432 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13433 return literal_symbol_name_matcher;
13434
b5ec771e
PA
13435 if (lookup_name.completion_mode ())
13436 return ada_symbol_name_matches;
74ccd7f5 13437 else
b5ec771e
PA
13438 {
13439 if (lookup_name.ada ().wild_match_p ())
13440 return do_wild_match;
a2cd4f14
JB
13441 else if (lookup_name.ada ().verbatim_p ())
13442 return do_exact_match;
b5ec771e
PA
13443 else
13444 return do_full_match;
13445 }
74ccd7f5
JB
13446}
13447
0874fd07
AB
13448/* Class representing the Ada language. */
13449
13450class ada_language : public language_defn
13451{
13452public:
13453 ada_language ()
0e25e767 13454 : language_defn (language_ada)
0874fd07 13455 { /* Nothing. */ }
5bd40f2a 13456
6f7664a9
AB
13457 /* See language.h. */
13458
13459 const char *name () const override
13460 { return "ada"; }
13461
13462 /* See language.h. */
13463
13464 const char *natural_name () const override
13465 { return "Ada"; }
13466
e171d6f1
AB
13467 /* See language.h. */
13468
13469 const std::vector<const char *> &filename_extensions () const override
13470 {
13471 static const std::vector<const char *> extensions
13472 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13473 return extensions;
13474 }
13475
5bd40f2a
AB
13476 /* Print an array element index using the Ada syntax. */
13477
13478 void print_array_index (struct type *index_type,
13479 LONGEST index,
13480 struct ui_file *stream,
13481 const value_print_options *options) const override
13482 {
13483 struct value *index_value = val_atr (index_type, index);
13484
00c696a6 13485 value_print (index_value, stream, options);
6cb06a8c 13486 gdb_printf (stream, " => ");
5bd40f2a 13487 }
15e5fd35
AB
13488
13489 /* Implement the "read_var_value" language_defn method for Ada. */
13490
13491 struct value *read_var_value (struct symbol *var,
13492 const struct block *var_block,
bd2b40ac 13493 frame_info_ptr frame) const override
15e5fd35
AB
13494 {
13495 /* The only case where default_read_var_value is not sufficient
13496 is when VAR is a renaming... */
13497 if (frame != nullptr)
13498 {
13499 const struct block *frame_block = get_frame_block (frame, NULL);
13500 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13501 return ada_read_renaming_var_value (var, frame_block);
13502 }
13503
13504 /* This is a typical case where we expect the default_read_var_value
13505 function to work. */
13506 return language_defn::read_var_value (var, var_block, frame);
13507 }
1fb314aa 13508
2c71f639 13509 /* See language.h. */
496feb16 13510 bool symbol_printing_suppressed (struct symbol *symbol) const override
2c71f639 13511 {
496feb16 13512 return symbol->is_artificial ();
2c71f639
TV
13513 }
13514
1fb314aa
AB
13515 /* See language.h. */
13516 void language_arch_info (struct gdbarch *gdbarch,
13517 struct language_arch_info *lai) const override
13518 {
13519 const struct builtin_type *builtin = builtin_type (gdbarch);
13520
7bea47f0
AB
13521 /* Helper function to allow shorter lines below. */
13522 auto add = [&] (struct type *t)
13523 {
13524 lai->add_primitive_type (t);
13525 };
13526
cc495054 13527 type_allocator alloc (gdbarch);
2d39ccd3 13528 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13529 0, "integer"));
2d39ccd3 13530 add (init_integer_type (alloc, gdbarch_long_bit (gdbarch),
7bea47f0 13531 0, "long_integer"));
2d39ccd3 13532 add (init_integer_type (alloc, gdbarch_short_bit (gdbarch),
7bea47f0 13533 0, "short_integer"));
f50b437c 13534 struct type *char_type = init_character_type (alloc, TARGET_CHAR_BIT,
c9f66f00 13535 1, "character");
7bea47f0
AB
13536 lai->set_string_char_type (char_type);
13537 add (char_type);
f50b437c
TT
13538 add (init_character_type (alloc, 16, 1, "wide_character"));
13539 add (init_character_type (alloc, 32, 1, "wide_wide_character"));
77c5f496 13540 add (init_float_type (alloc, gdbarch_float_bit (gdbarch),
7bea47f0 13541 "float", gdbarch_float_format (gdbarch)));
77c5f496 13542 add (init_float_type (alloc, gdbarch_double_bit (gdbarch),
7bea47f0 13543 "long_float", gdbarch_double_format (gdbarch)));
2d39ccd3 13544 add (init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
7bea47f0 13545 0, "long_long_integer"));
e49831ba
TT
13546 add (init_integer_type (alloc, 128, 0, "long_long_long_integer"));
13547 add (init_integer_type (alloc, 128, 1, "unsigned_long_long_long_integer"));
77c5f496 13548 add (init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
7bea47f0
AB
13549 "long_long_float",
13550 gdbarch_long_double_format (gdbarch)));
2d39ccd3 13551 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0 13552 0, "natural"));
2d39ccd3 13553 add (init_integer_type (alloc, gdbarch_int_bit (gdbarch),
7bea47f0
AB
13554 0, "positive"));
13555 add (builtin->builtin_void);
13556
13557 struct type *system_addr_ptr
cc495054
TT
13558 = lookup_pointer_type (alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT,
13559 "void"));
7bea47f0
AB
13560 system_addr_ptr->set_name ("system__address");
13561 add (system_addr_ptr);
1fb314aa
AB
13562
13563 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13564 type. This is a signed integral type whose size is the same as
13565 the size of addresses. */
df86565b 13566 unsigned int addr_length = system_addr_ptr->length ();
2d39ccd3 13567 add (init_integer_type (alloc, addr_length * HOST_CHAR_BIT, 0,
7bea47f0 13568 "storage_offset"));
1fb314aa 13569
7bea47f0 13570 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13571 }
4009ee92
AB
13572
13573 /* See language.h. */
13574
13575 bool iterate_over_symbols
13576 (const struct block *block, const lookup_name_info &name,
13577 domain_enum domain,
13578 gdb::function_view<symbol_found_callback_ftype> callback) const override
13579 {
d1183b06
TT
13580 std::vector<struct block_symbol> results
13581 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13582 for (block_symbol &sym : results)
13583 {
13584 if (!callback (&sym))
13585 return false;
13586 }
13587
13588 return true;
13589 }
6f827019
AB
13590
13591 /* See language.h. */
3456e70c
TT
13592 bool sniff_from_mangled_name
13593 (const char *mangled,
13594 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13595 {
13596 std::string demangled = ada_decode (mangled);
13597
13598 *out = NULL;
13599
13600 if (demangled != mangled && demangled[0] != '<')
13601 {
13602 /* Set the gsymbol language to Ada, but still return 0.
13603 Two reasons for that:
13604
13605 1. For Ada, we prefer computing the symbol's decoded name
13606 on the fly rather than pre-compute it, in order to save
13607 memory (Ada projects are typically very large).
13608
13609 2. There are some areas in the definition of the GNAT
13610 encoding where, with a bit of bad luck, we might be able
13611 to decode a non-Ada symbol, generating an incorrect
13612 demangled name (Eg: names ending with "TB" for instance
13613 are identified as task bodies and so stripped from
13614 the decoded name returned).
13615
13616 Returning true, here, but not setting *DEMANGLED, helps us get
13617 a little bit of the best of both worlds. Because we're last,
13618 we should not affect any of the other languages that were
13619 able to demangle the symbol before us; we get to correctly
13620 tag Ada symbols as such; and even if we incorrectly tagged a
13621 non-Ada symbol, which should be rare, any routing through the
13622 Ada language should be transparent (Ada tries to behave much
13623 like C/C++ with non-Ada symbols). */
13624 return true;
13625 }
13626
13627 return false;
13628 }
fbfb0a46
AB
13629
13630 /* See language.h. */
13631
3456e70c
TT
13632 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13633 int options) const override
0a50df5d 13634 {
3456e70c 13635 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13636 }
13637
13638 /* See language.h. */
13639
fbfb0a46
AB
13640 void print_type (struct type *type, const char *varstring,
13641 struct ui_file *stream, int show, int level,
13642 const struct type_print_options *flags) const override
13643 {
13644 ada_print_type (type, varstring, stream, show, level, flags);
13645 }
c9debfb9 13646
53fc67f8
AB
13647 /* See language.h. */
13648
13649 const char *word_break_characters (void) const override
13650 {
13651 return ada_completer_word_break_characters;
13652 }
13653
7e56227d
AB
13654 /* See language.h. */
13655
13656 void collect_symbol_completion_matches (completion_tracker &tracker,
13657 complete_symbol_mode mode,
13658 symbol_name_match_type name_match_type,
13659 const char *text, const char *word,
13660 enum type_code code) const override
13661 {
7e56227d 13662 const struct block *b, *surrounding_static_block = 0;
7e56227d
AB
13663
13664 gdb_assert (code == TYPE_CODE_UNDEF);
13665
13666 lookup_name_info lookup_name (text, name_match_type, true);
13667
13668 /* First, look at the partial symtab symbols. */
13669 expand_symtabs_matching (NULL,
13670 lookup_name,
13671 NULL,
13672 NULL,
03a8ea51 13673 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13674 ALL_DOMAIN);
13675
13676 /* At this point scan through the misc symbol vectors and add each
13677 symbol you find to the list. Eventually we want to ignore
13678 anything that isn't a text symbol (everything else will be
13679 handled by the psymtab code above). */
13680
13681 for (objfile *objfile : current_program_space->objfiles ())
13682 {
13683 for (minimal_symbol *msymbol : objfile->msymbols ())
13684 {
13685 QUIT;
13686
13687 if (completion_skip_symbol (mode, msymbol))
13688 continue;
13689
13690 language symbol_language = msymbol->language ();
13691
13692 /* Ada minimal symbols won't have their language set to Ada. If
13693 we let completion_list_add_name compare using the
13694 default/C-like matcher, then when completing e.g., symbols in a
13695 package named "pck", we'd match internal Ada symbols like
13696 "pckS", which are invalid in an Ada expression, unless you wrap
13697 them in '<' '>' to request a verbatim match.
13698
13699 Unfortunately, some Ada encoded names successfully demangle as
13700 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13701 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13702 with the wrong language set. Paper over that issue here. */
129bce36 13703 if (symbol_language == language_unknown
7e56227d
AB
13704 || symbol_language == language_cplus)
13705 symbol_language = language_ada;
13706
13707 completion_list_add_name (tracker,
13708 symbol_language,
13709 msymbol->linkage_name (),
13710 lookup_name, text, word);
13711 }
13712 }
13713
13714 /* Search upwards from currently selected frame (so that we can
13715 complete on local vars. */
13716
f135fe72 13717 for (b = get_selected_block (0); b != NULL; b = b->superblock ())
7e56227d 13718 {
f135fe72 13719 if (!b->superblock ())
7e56227d
AB
13720 surrounding_static_block = b; /* For elmin of dups */
13721
548a89df 13722 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13723 {
13724 if (completion_skip_symbol (mode, sym))
13725 continue;
13726
13727 completion_list_add_name (tracker,
13728 sym->language (),
13729 sym->linkage_name (),
13730 lookup_name, text, word);
13731 }
13732 }
13733
13734 /* Go through the symtabs and check the externs and statics for
13735 symbols which match. */
13736
13737 for (objfile *objfile : current_program_space->objfiles ())
13738 {
13739 for (compunit_symtab *s : objfile->compunits ())
13740 {
13741 QUIT;
63d609de 13742 b = s->blockvector ()->global_block ();
548a89df 13743 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13744 {
13745 if (completion_skip_symbol (mode, sym))
13746 continue;
13747
13748 completion_list_add_name (tracker,
13749 sym->language (),
13750 sym->linkage_name (),
13751 lookup_name, text, word);
13752 }
13753 }
13754 }
13755
13756 for (objfile *objfile : current_program_space->objfiles ())
13757 {
13758 for (compunit_symtab *s : objfile->compunits ())
13759 {
13760 QUIT;
63d609de 13761 b = s->blockvector ()->static_block ();
7e56227d
AB
13762 /* Don't do this block twice. */
13763 if (b == surrounding_static_block)
13764 continue;
548a89df 13765 for (struct symbol *sym : block_iterator_range (b))
7e56227d
AB
13766 {
13767 if (completion_skip_symbol (mode, sym))
13768 continue;
13769
13770 completion_list_add_name (tracker,
13771 sym->language (),
13772 sym->linkage_name (),
13773 lookup_name, text, word);
13774 }
13775 }
13776 }
13777 }
13778
f16a9f57
AB
13779 /* See language.h. */
13780
13781 gdb::unique_xmalloc_ptr<char> watch_location_expression
13782 (struct type *type, CORE_ADDR addr) const override
13783 {
27710edb 13784 type = check_typedef (check_typedef (type)->target_type ());
f16a9f57 13785 std::string name = type_to_string (type);
8579fd13 13786 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13787 }
13788
a1d1fa3e
AB
13789 /* See language.h. */
13790
13791 void value_print (struct value *val, struct ui_file *stream,
13792 const struct value_print_options *options) const override
13793 {
13794 return ada_value_print (val, stream, options);
13795 }
13796
ebe2334e
AB
13797 /* See language.h. */
13798
13799 void value_print_inner
13800 (struct value *val, struct ui_file *stream, int recurse,
13801 const struct value_print_options *options) const override
13802 {
13803 return ada_value_print_inner (val, stream, recurse, options);
13804 }
13805
a78a19b1
AB
13806 /* See language.h. */
13807
13808 struct block_symbol lookup_symbol_nonlocal
13809 (const char *name, const struct block *block,
13810 const domain_enum domain) const override
13811 {
13812 struct block_symbol sym;
13813
78004096
TT
13814 sym = ada_lookup_symbol (name,
13815 (block == nullptr
13816 ? nullptr
d24e14a0 13817 : block->static_block ()),
78004096 13818 domain);
a78a19b1
AB
13819 if (sym.symbol != NULL)
13820 return sym;
13821
13822 /* If we haven't found a match at this point, try the primitive
13823 types. In other languages, this search is performed before
13824 searching for global symbols in order to short-circuit that
13825 global-symbol search if it happens that the name corresponds
13826 to a primitive type. But we cannot do the same in Ada, because
13827 it is perfectly legitimate for a program to declare a type which
13828 has the same name as a standard type. If looking up a type in
13829 that situation, we have traditionally ignored the primitive type
13830 in favor of user-defined types. This is why, unlike most other
13831 languages, we search the primitive types this late and only after
13832 having searched the global symbols without success. */
13833
13834 if (domain == VAR_DOMAIN)
13835 {
13836 struct gdbarch *gdbarch;
13837
13838 if (block == NULL)
13839 gdbarch = target_gdbarch ();
13840 else
7f5937df 13841 gdbarch = block->gdbarch ();
a78a19b1
AB
13842 sym.symbol
13843 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13844 if (sym.symbol != NULL)
13845 return sym;
13846 }
13847
13848 return {};
13849 }
13850
87afa652
AB
13851 /* See language.h. */
13852
13853 int parser (struct parser_state *ps) const override
13854 {
13855 warnings_issued = 0;
13856 return ada_parse (ps);
13857 }
13858
ec8cec5b
AB
13859 /* See language.h. */
13860
13861 void emitchar (int ch, struct type *chtype,
13862 struct ui_file *stream, int quoter) const override
13863 {
13864 ada_emit_char (ch, chtype, stream, quoter, 1);
13865 }
13866
52b50f2c
AB
13867 /* See language.h. */
13868
13869 void printchar (int ch, struct type *chtype,
13870 struct ui_file *stream) const override
13871 {
13872 ada_printchar (ch, chtype, stream);
13873 }
13874
d711ee67
AB
13875 /* See language.h. */
13876
13877 void printstr (struct ui_file *stream, struct type *elttype,
13878 const gdb_byte *string, unsigned int length,
13879 const char *encoding, int force_ellipses,
13880 const struct value_print_options *options) const override
13881 {
13882 ada_printstr (stream, elttype, string, length, encoding,
13883 force_ellipses, options);
13884 }
13885
4ffc13fb
AB
13886 /* See language.h. */
13887
13888 void print_typedef (struct type *type, struct symbol *new_symbol,
13889 struct ui_file *stream) const override
13890 {
13891 ada_print_typedef (type, new_symbol, stream);
13892 }
13893
39e7ecca
AB
13894 /* See language.h. */
13895
13896 bool is_string_type_p (struct type *type) const override
13897 {
13898 return ada_is_string_type (type);
13899 }
13900
22e3f3ed
AB
13901 /* See language.h. */
13902
13903 const char *struct_too_deep_ellipsis () const override
13904 { return "(...)"; }
39e7ecca 13905
67bd3fd5
AB
13906 /* See language.h. */
13907
13908 bool c_style_arrays_p () const override
13909 { return false; }
13910
d3355e4d
AB
13911 /* See language.h. */
13912
13913 bool store_sym_names_in_linkage_form_p () const override
13914 { return true; }
13915
b63a3f3f
AB
13916 /* See language.h. */
13917
13918 const struct lang_varobj_ops *varobj_ops () const override
13919 { return &ada_varobj_ops; }
13920
c9debfb9
AB
13921protected:
13922 /* See language.h. */
13923
13924 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13925 (const lookup_name_info &lookup_name) const override
13926 {
13927 return ada_get_symbol_name_matcher (lookup_name);
13928 }
0874fd07
AB
13929};
13930
13931/* Single instance of the Ada language class. */
13932
13933static ada_language ada_language_defn;
13934
5bf03f13
JB
13935/* Command-list for the "set/show ada" prefix command. */
13936static struct cmd_list_element *set_ada_list;
13937static struct cmd_list_element *show_ada_list;
13938
3d9434b5
JB
13939/* This module's 'new_objfile' observer. */
13940
13941static void
13942ada_new_objfile_observer (struct objfile *objfile)
13943{
13944 ada_clear_symbol_cache ();
13945}
13946
13947/* This module's 'free_objfile' observer. */
13948
13949static void
13950ada_free_objfile_observer (struct objfile *objfile)
13951{
13952 ada_clear_symbol_cache ();
13953}
13954
315e4ebb
TT
13955/* Charsets known to GNAT. */
13956static const char * const gnat_source_charsets[] =
13957{
13958 /* Note that code below assumes that the default comes first.
13959 Latin-1 is the default here, because that is also GNAT's
13960 default. */
13961 "ISO-8859-1",
13962 "ISO-8859-2",
13963 "ISO-8859-3",
13964 "ISO-8859-4",
13965 "ISO-8859-5",
13966 "ISO-8859-15",
13967 "CP437",
13968 "CP850",
13969 /* Note that this value is special-cased in the encoder and
13970 decoder. */
13971 ada_utf8,
13972 nullptr
13973};
13974
6c265988 13975void _initialize_ada_language ();
d2e4a39e 13976void
6c265988 13977_initialize_ada_language ()
14f9c5c9 13978{
f54bdb6d
SM
13979 add_setshow_prefix_cmd
13980 ("ada", no_class,
13981 _("Prefix command for changing Ada-specific settings."),
13982 _("Generic command for showing Ada-specific settings."),
13983 &set_ada_list, &show_ada_list,
13984 &setlist, &showlist);
5bf03f13
JB
13985
13986 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13987 &trust_pad_over_xvs, _("\
590042fc
PW
13988Enable or disable an optimization trusting PAD types over XVS types."), _("\
13989Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13990 _("\
5bf03f13
JB
13991This is related to the encoding used by the GNAT compiler. The debugger\n\
13992should normally trust the contents of PAD types, but certain older versions\n\
13993of GNAT have a bug that sometimes causes the information in the PAD type\n\
13994to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13995work around this bug. It is always safe to turn this option \"off\", but\n\
13996this incurs a slight performance penalty, so it is recommended to NOT change\n\
13997this option to \"off\" unless necessary."),
dda83cd7 13998 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13999
d72413e6
PMR
14000 add_setshow_boolean_cmd ("print-signatures", class_vars,
14001 &print_signatures, _("\
14002Enable or disable the output of formal and return types for functions in the \
590042fc 14003overloads selection menu."), _("\
d72413e6 14004Show whether the output of formal and return types for functions in the \
590042fc 14005overloads selection menu is activated."),
d72413e6
PMR
14006 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
14007
315e4ebb
TT
14008 ada_source_charset = gnat_source_charsets[0];
14009 add_setshow_enum_cmd ("source-charset", class_files,
14010 gnat_source_charsets,
14011 &ada_source_charset, _("\
14012Set the Ada source character set."), _("\
14013Show the Ada source character set."), _("\
14014The character set used for Ada source files.\n\
14015This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
14016 nullptr, nullptr,
14017 &set_ada_list, &show_ada_list);
14018
9ac4176b
PA
14019 add_catch_command ("exception", _("\
14020Catch Ada exceptions, when raised.\n\
9bf7038b 14021Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
14022Without any argument, stop when any Ada exception is raised.\n\
14023If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
14024being raised does not have a handler (and will therefore lead to the task's\n\
14025termination).\n\
14026Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
14027raised is the same as ARG.\n\
14028CONDITION is a boolean expression that is evaluated to see whether the\n\
14029exception should cause a stop."),
9ac4176b 14030 catch_ada_exception_command,
71bed2db 14031 catch_ada_completer,
9ac4176b
PA
14032 CATCH_PERMANENT,
14033 CATCH_TEMPORARY);
9f757bf7
XR
14034
14035 add_catch_command ("handlers", _("\
14036Catch Ada exceptions, when handled.\n\
9bf7038b
TT
14037Usage: catch handlers [ARG] [if CONDITION]\n\
14038Without any argument, stop when any Ada exception is handled.\n\
14039With an argument, catch only exceptions with the given name.\n\
14040CONDITION is a boolean expression that is evaluated to see whether the\n\
14041exception should cause a stop."),
9f757bf7 14042 catch_ada_handlers_command,
dda83cd7 14043 catch_ada_completer,
9f757bf7
XR
14044 CATCH_PERMANENT,
14045 CATCH_TEMPORARY);
9ac4176b
PA
14046 add_catch_command ("assert", _("\
14047Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
14048Usage: catch assert [if CONDITION]\n\
14049CONDITION is a boolean expression that is evaluated to see whether the\n\
14050exception should cause a stop."),
9ac4176b 14051 catch_assert_command,
dda83cd7 14052 NULL,
9ac4176b
PA
14053 CATCH_PERMANENT,
14054 CATCH_TEMPORARY);
14055
778865d3
JB
14056 add_info ("exceptions", info_exceptions_command,
14057 _("\
14058List all Ada exception names.\n\
9bf7038b 14059Usage: info exceptions [REGEXP]\n\
778865d3
JB
14060If a regular expression is passed as an argument, only those matching\n\
14061the regular expression are listed."));
14062
f54bdb6d
SM
14063 add_setshow_prefix_cmd ("ada", class_maintenance,
14064 _("Set Ada maintenance-related variables."),
14065 _("Show Ada maintenance-related variables."),
14066 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
14067 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
14068
14069 add_setshow_boolean_cmd
14070 ("ignore-descriptive-types", class_maintenance,
14071 &ada_ignore_descriptive_types_p,
14072 _("Set whether descriptive types generated by GNAT should be ignored."),
14073 _("Show whether descriptive types generated by GNAT should be ignored."),
14074 _("\
14075When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14076DWARF attribute."),
14077 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14078
2698f5ea
TT
14079 decoded_names_store = htab_create_alloc (256, htab_hash_string,
14080 htab_eq_string,
459a2e4c 14081 NULL, xcalloc, xfree);
6b69afc4 14082
3d9434b5 14083 /* The ada-lang observers. */
c90e7d63
SM
14084 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
14085 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
14086 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
14f9c5c9 14087}