]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
Remove ALL_BLOCK_SYMBOLS_WITH_NAME
[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"
ccefe4c4 63
4c4b4cd2 64/* Define whether or not the C operator '/' truncates towards zero for
0963b4bd 65 differently signed operands (truncation direction is undefined in C).
4c4b4cd2
PH
66 Copied from valarith.c. */
67
68#ifndef TRUNCATION_TOWARDS_ZERO
69#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
70#endif
71
d2e4a39e 72static struct type *desc_base_type (struct type *);
14f9c5c9 73
d2e4a39e 74static struct type *desc_bounds_type (struct type *);
14f9c5c9 75
d2e4a39e 76static struct value *desc_bounds (struct value *);
14f9c5c9 77
d2e4a39e 78static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 79
d2e4a39e 80static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 81
556bdfd4 82static struct type *desc_data_target_type (struct type *);
14f9c5c9 83
d2e4a39e 84static struct value *desc_data (struct value *);
14f9c5c9 85
d2e4a39e 86static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 87
d2e4a39e 88static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 89
d2e4a39e 90static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 91
d2e4a39e 92static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 93
d2e4a39e 94static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 95
d2e4a39e 96static struct type *desc_index_type (struct type *, int);
14f9c5c9 97
d2e4a39e 98static int desc_arity (struct type *);
14f9c5c9 99
d2e4a39e 100static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 101
40bc484c 102static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 103
d1183b06 104static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
105 const struct block *,
106 const lookup_name_info &lookup_name,
107 domain_enum, struct objfile *);
14f9c5c9 108
d1183b06
TT
109static void ada_add_all_symbols (std::vector<struct block_symbol> &,
110 const struct block *,
b5ec771e
PA
111 const lookup_name_info &lookup_name,
112 domain_enum, int, int *);
22cee43f 113
d1183b06 114static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 115
d1183b06
TT
116static void add_defn_to_vec (std::vector<struct block_symbol> &,
117 struct symbol *,
dda83cd7 118 const struct block *);
14f9c5c9 119
d2e4a39e 120static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 121
4c4b4cd2 122static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 123
d2e4a39e 124static int numeric_type_p (struct type *);
14f9c5c9 125
d2e4a39e 126static int integer_type_p (struct type *);
14f9c5c9 127
d2e4a39e 128static int scalar_type_p (struct type *);
14f9c5c9 129
d2e4a39e 130static int discrete_type_p (struct type *);
14f9c5c9 131
a121b7c1 132static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 133 int, int);
4c4b4cd2 134
b4ba55a1 135static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 136 const char *);
b4ba55a1 137
d2e4a39e 138static int is_dynamic_field (struct type *, int);
14f9c5c9 139
10a2c479 140static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 141 const gdb_byte *,
dda83cd7 142 CORE_ADDR, struct value *);
4c4b4cd2
PH
143
144static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 145
28c85d6c 146static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 147
d2e4a39e 148static struct type *to_static_fixed_type (struct type *);
f192137b 149static struct type *static_unwrap_type (struct type *type);
14f9c5c9 150
d2e4a39e 151static struct value *unwrap_value (struct value *);
14f9c5c9 152
ad82864c 153static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 154
ad82864c 155static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 156
ad82864c
JB
157static long decode_packed_array_bitsize (struct type *);
158
159static struct value *decode_constrained_packed_array (struct value *);
160
ad82864c 161static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 162
d2e4a39e 163static struct value *value_subscript_packed (struct value *, int,
dda83cd7 164 struct value **);
14f9c5c9 165
4c4b4cd2 166static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 167 struct type *);
14f9c5c9 168
d2e4a39e 169static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 170
d2e4a39e 171static int equiv_types (struct type *, struct type *);
14f9c5c9 172
d2e4a39e 173static int is_name_suffix (const char *);
14f9c5c9 174
59c8a30b 175static int advance_wild_match (const char **, const char *, char);
73589123 176
b5ec771e 177static bool wild_match (const char *name, const char *patn);
14f9c5c9 178
d2e4a39e 179static struct value *ada_coerce_ref (struct value *);
14f9c5c9 180
4c4b4cd2
PH
181static LONGEST pos_atr (struct value *);
182
53a47a3e
TT
183static struct value *val_atr (struct type *, LONGEST);
184
4c4b4cd2 185static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 186 domain_enum);
14f9c5c9 187
108d56a4 188static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 189 struct type *);
4c4b4cd2 190
0d5cff50 191static int find_struct_field (const char *, struct type *, int,
dda83cd7 192 struct type **, int *, int *, int *, int *);
4c4b4cd2 193
d1183b06 194static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 195 struct value **, int, const char *,
7056f312 196 struct type *, bool);
4c4b4cd2 197
4c4b4cd2
PH
198static int ada_is_direct_array_type (struct type *);
199
52ce6436
PH
200static struct value *ada_index_struct_field (int, struct value *, int,
201 struct type *);
202
cf608cc4 203static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
204
205
852dff6c 206static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
207
208static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
209 (const lookup_name_info &lookup_name);
210
4c4b4cd2
PH
211\f
212
315e4ebb
TT
213/* The character set used for source files. */
214static const char *ada_source_charset;
215
216/* The string "UTF-8". This is here so we can check for the UTF-8
217 charset using == rather than strcmp. */
218static const char ada_utf8[] = "UTF-8";
219
220/* Each entry in the UTF-32 case-folding table is of this form. */
221struct utf8_entry
222{
223 /* The start and end, inclusive, of this range of codepoints. */
224 uint32_t start, end;
225 /* The delta to apply to get the upper-case form. 0 if this is
226 already upper-case. */
227 int upper_delta;
228 /* The delta to apply to get the lower-case form. 0 if this is
229 already lower-case. */
230 int lower_delta;
231
232 bool operator< (uint32_t val) const
233 {
234 return end < val;
235 }
236};
237
238static const utf8_entry ada_case_fold[] =
239{
240#include "ada-casefold.h"
241};
242
243\f
244
ee01b665
JB
245/* The result of a symbol lookup to be stored in our symbol cache. */
246
247struct cache_entry
248{
249 /* The name used to perform the lookup. */
250 const char *name;
251 /* The namespace used during the lookup. */
fe978cb0 252 domain_enum domain;
ee01b665
JB
253 /* The symbol returned by the lookup, or NULL if no matching symbol
254 was found. */
255 struct symbol *sym;
256 /* The block where the symbol was found, or NULL if no matching
257 symbol was found. */
258 const struct block *block;
259 /* A pointer to the next entry with the same hash. */
260 struct cache_entry *next;
261};
262
263/* The Ada symbol cache, used to store the result of Ada-mode symbol
264 lookups in the course of executing the user's commands.
265
266 The cache is implemented using a simple, fixed-sized hash.
267 The size is fixed on the grounds that there are not likely to be
268 all that many symbols looked up during any given session, regardless
269 of the size of the symbol table. If we decide to go to a resizable
270 table, let's just use the stuff from libiberty instead. */
271
272#define HASH_SIZE 1009
273
274struct ada_symbol_cache
275{
276 /* An obstack used to store the entries in our cache. */
bdcccc56 277 struct auto_obstack cache_space;
ee01b665
JB
278
279 /* The root of the hash table used to implement our symbol cache. */
bdcccc56 280 struct cache_entry *root[HASH_SIZE] {};
ee01b665
JB
281};
282
67cb5b2d 283static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
284#ifdef VMS
285 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
286#else
14f9c5c9 287 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 288#endif
14f9c5c9 289
4c4b4cd2 290/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 291static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 292 = "__gnat_ada_main_program_name";
14f9c5c9 293
4c4b4cd2
PH
294/* Limit on the number of warnings to raise per expression evaluation. */
295static int warning_limit = 2;
296
297/* Number of warning messages issued; reset to 0 by cleanups after
298 expression evaluation. */
299static int warnings_issued = 0;
300
27087b7f 301static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
302 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
303};
304
27087b7f 305static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
306 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
307};
308
c6044dd1
JB
309/* Maintenance-related settings for this module. */
310
311static struct cmd_list_element *maint_set_ada_cmdlist;
312static struct cmd_list_element *maint_show_ada_cmdlist;
313
c6044dd1
JB
314/* The "maintenance ada set/show ignore-descriptive-type" value. */
315
491144b5 316static bool ada_ignore_descriptive_types_p = false;
c6044dd1 317
e802dbe0
JB
318 /* Inferior-specific data. */
319
320/* Per-inferior data for this module. */
321
322struct ada_inferior_data
323{
324 /* The ada__tags__type_specific_data type, which is used when decoding
325 tagged types. With older versions of GNAT, this type was directly
326 accessible through a component ("tsd") in the object tag. But this
327 is no longer the case, so we cache it for each inferior. */
f37b313d 328 struct type *tsd_type = nullptr;
3eecfa55
JB
329
330 /* The exception_support_info data. This data is used to determine
331 how to implement support for Ada exception catchpoints in a given
332 inferior. */
f37b313d 333 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
334};
335
336/* Our key to this module's inferior data. */
08b8a139 337static const registry<inferior>::key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
338
339/* Return our inferior data for the given inferior (INF).
340
341 This function always returns a valid pointer to an allocated
342 ada_inferior_data structure. If INF's inferior data has not
343 been previously set, this functions creates a new one with all
344 fields set to zero, sets INF's inferior to it, and then returns
345 a pointer to that newly allocated ada_inferior_data. */
346
347static struct ada_inferior_data *
348get_ada_inferior_data (struct inferior *inf)
349{
350 struct ada_inferior_data *data;
351
f37b313d 352 data = ada_inferior_data.get (inf);
e802dbe0 353 if (data == NULL)
f37b313d 354 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
355
356 return data;
357}
358
359/* Perform all necessary cleanups regarding our module's inferior data
360 that is required after the inferior INF just exited. */
361
362static void
363ada_inferior_exit (struct inferior *inf)
364{
f37b313d 365 ada_inferior_data.clear (inf);
e802dbe0
JB
366}
367
ee01b665
JB
368
369 /* program-space-specific data. */
370
371/* This module's per-program-space data. */
372struct ada_pspace_data
373{
374 /* The Ada symbol cache. */
bdcccc56 375 std::unique_ptr<ada_symbol_cache> sym_cache;
ee01b665
JB
376};
377
378/* Key to our per-program-space data. */
08b8a139
TT
379static const registry<program_space>::key<ada_pspace_data>
380 ada_pspace_data_handle;
ee01b665
JB
381
382/* Return this module's data for the given program space (PSPACE).
383 If not is found, add a zero'ed one now.
384
385 This function always returns a valid object. */
386
387static struct ada_pspace_data *
388get_ada_pspace_data (struct program_space *pspace)
389{
390 struct ada_pspace_data *data;
391
f37b313d 392 data = ada_pspace_data_handle.get (pspace);
ee01b665 393 if (data == NULL)
f37b313d 394 data = ada_pspace_data_handle.emplace (pspace);
ee01b665
JB
395
396 return data;
397}
398
dda83cd7 399 /* Utilities */
4c4b4cd2 400
720d1a40 401/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 402 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
403
404 Normally, we really expect a typedef type to only have 1 typedef layer.
405 In other words, we really expect the target type of a typedef type to be
406 a non-typedef type. This is particularly true for Ada units, because
407 the language does not have a typedef vs not-typedef distinction.
408 In that respect, the Ada compiler has been trying to eliminate as many
409 typedef definitions in the debugging information, since they generally
410 do not bring any extra information (we still use typedef under certain
411 circumstances related mostly to the GNAT encoding).
412
413 Unfortunately, we have seen situations where the debugging information
414 generated by the compiler leads to such multiple typedef layers. For
415 instance, consider the following example with stabs:
416
417 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
418 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
419
420 This is an error in the debugging information which causes type
421 pck__float_array___XUP to be defined twice, and the second time,
422 it is defined as a typedef of a typedef.
423
424 This is on the fringe of legality as far as debugging information is
425 concerned, and certainly unexpected. But it is easy to handle these
426 situations correctly, so we can afford to be lenient in this case. */
427
428static struct type *
429ada_typedef_target_type (struct type *type)
430{
78134374 431 while (type->code () == TYPE_CODE_TYPEDEF)
27710edb 432 type = type->target_type ();
720d1a40
JB
433 return type;
434}
435
41d27058
JB
436/* Given DECODED_NAME a string holding a symbol name in its
437 decoded form (ie using the Ada dotted notation), returns
438 its unqualified name. */
439
440static const char *
441ada_unqualified_name (const char *decoded_name)
442{
2b0f535a
JB
443 const char *result;
444
445 /* If the decoded name starts with '<', it means that the encoded
446 name does not follow standard naming conventions, and thus that
447 it is not your typical Ada symbol name. Trying to unqualify it
448 is therefore pointless and possibly erroneous. */
449 if (decoded_name[0] == '<')
450 return decoded_name;
451
452 result = strrchr (decoded_name, '.');
41d27058
JB
453 if (result != NULL)
454 result++; /* Skip the dot... */
455 else
456 result = decoded_name;
457
458 return result;
459}
460
39e7af3e 461/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 462
39e7af3e 463static std::string
41d27058
JB
464add_angle_brackets (const char *str)
465{
39e7af3e 466 return string_printf ("<%s>", str);
41d27058 467}
96d887e8 468
14f9c5c9 469/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 470 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
471
472static int
ebf56fd3 473field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
474{
475 int len = strlen (target);
5b4ee69b 476
d2e4a39e 477 return
4c4b4cd2
PH
478 (strncmp (field_name, target, len) == 0
479 && (field_name[len] == '\0'
dda83cd7
SM
480 || (startswith (field_name + len, "___")
481 && strcmp (field_name + strlen (field_name) - 6,
482 "___XVN") != 0)));
14f9c5c9
AS
483}
484
485
872c8b51
JB
486/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
487 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
488 and return its index. This function also handles fields whose name
489 have ___ suffixes because the compiler sometimes alters their name
490 by adding such a suffix to represent fields with certain constraints.
491 If the field could not be found, return a negative number if
492 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
493
494int
495ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 496 int maybe_missing)
4c4b4cd2
PH
497{
498 int fieldno;
872c8b51
JB
499 struct type *struct_type = check_typedef ((struct type *) type);
500
1f704f76 501 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 502 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
503 return fieldno;
504
505 if (!maybe_missing)
323e0a4a 506 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 507 field_name, struct_type->name ());
4c4b4cd2
PH
508
509 return -1;
510}
511
512/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
513
514int
d2e4a39e 515ada_name_prefix_len (const char *name)
14f9c5c9
AS
516{
517 if (name == NULL)
518 return 0;
d2e4a39e 519 else
14f9c5c9 520 {
d2e4a39e 521 const char *p = strstr (name, "___");
5b4ee69b 522
14f9c5c9 523 if (p == NULL)
dda83cd7 524 return strlen (name);
14f9c5c9 525 else
dda83cd7 526 return p - name;
14f9c5c9
AS
527 }
528}
529
4c4b4cd2
PH
530/* Return non-zero if SUFFIX is a suffix of STR.
531 Return zero if STR is null. */
532
14f9c5c9 533static int
d2e4a39e 534is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
535{
536 int len1, len2;
5b4ee69b 537
14f9c5c9
AS
538 if (str == NULL)
539 return 0;
540 len1 = strlen (str);
541 len2 = strlen (suffix);
4c4b4cd2 542 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
543}
544
4c4b4cd2
PH
545/* The contents of value VAL, treated as a value of type TYPE. The
546 result is an lval in memory if VAL is. */
14f9c5c9 547
d2e4a39e 548static struct value *
4c4b4cd2 549coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 550{
61ee279c 551 type = ada_check_typedef (type);
d0c97917 552 if (val->type () == type)
4c4b4cd2 553 return val;
d2e4a39e 554 else
14f9c5c9 555 {
4c4b4cd2
PH
556 struct value *result;
557
d00664db 558 if (val->optimized_out ())
b27556e3 559 result = value::allocate_optimized_out (type);
3ee3b270 560 else if (val->lazy ()
f73e424f 561 /* Be careful not to make a lazy not_lval value. */
736355f2 562 || (val->lval () != not_lval
d0c97917 563 && type->length () > val->type ()->length ()))
cbe793af 564 result = value::allocate_lazy (type);
41e8491f
JK
565 else
566 {
317c3ed9 567 result = value::allocate (type);
6c49729e 568 val->contents_copy (result, 0, 0, type->length ());
41e8491f 569 }
8181b7b6 570 result->set_component_location (val);
f49d5fa2 571 result->set_bitsize (val->bitsize ());
5011c493 572 result->set_bitpos (val->bitpos ());
736355f2 573 if (result->lval () == lval_memory)
9feb2d07 574 result->set_address (val->address ());
14f9c5c9
AS
575 return result;
576 }
577}
578
fc1a4b47
AC
579static const gdb_byte *
580cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
581{
582 if (valaddr == NULL)
583 return NULL;
584 else
585 return valaddr + offset;
586}
587
588static CORE_ADDR
ebf56fd3 589cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
590{
591 if (address == 0)
592 return 0;
d2e4a39e 593 else
14f9c5c9
AS
594 return address + offset;
595}
596
4c4b4cd2
PH
597/* Issue a warning (as for the definition of warning in utils.c, but
598 with exactly one argument rather than ...), unless the limit on the
599 number of warnings has passed during the evaluation of the current
600 expression. */
a2249542 601
77109804
AC
602/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
603 provided by "complaint". */
a0b31db1 604static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 605
14f9c5c9 606static void
a2249542 607lim_warning (const char *format, ...)
14f9c5c9 608{
a2249542 609 va_list args;
a2249542 610
5b4ee69b 611 va_start (args, format);
4c4b4cd2
PH
612 warnings_issued += 1;
613 if (warnings_issued <= warning_limit)
a2249542
MK
614 vwarning (format, args);
615
616 va_end (args);
4c4b4cd2
PH
617}
618
0963b4bd 619/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 620static LONGEST
c3e5cd34 621max_of_size (int size)
4c4b4cd2 622{
76a01679 623 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 624
76a01679 625 return top_bit | (top_bit - 1);
4c4b4cd2
PH
626}
627
0963b4bd 628/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 629static LONGEST
c3e5cd34 630min_of_size (int size)
4c4b4cd2 631{
c3e5cd34 632 return -max_of_size (size) - 1;
4c4b4cd2
PH
633}
634
0963b4bd 635/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 636static ULONGEST
c3e5cd34 637umax_of_size (int size)
4c4b4cd2 638{
76a01679 639 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 640
76a01679 641 return top_bit | (top_bit - 1);
4c4b4cd2
PH
642}
643
0963b4bd 644/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
645static LONGEST
646max_of_type (struct type *t)
4c4b4cd2 647{
c6d940a9 648 if (t->is_unsigned ())
df86565b 649 return (LONGEST) umax_of_size (t->length ());
c3e5cd34 650 else
df86565b 651 return max_of_size (t->length ());
c3e5cd34
PH
652}
653
0963b4bd 654/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
655static LONGEST
656min_of_type (struct type *t)
657{
c6d940a9 658 if (t->is_unsigned ())
c3e5cd34
PH
659 return 0;
660 else
df86565b 661 return min_of_size (t->length ());
4c4b4cd2
PH
662}
663
664/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
665LONGEST
666ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 667{
b249d2c2 668 type = resolve_dynamic_type (type, {}, 0);
78134374 669 switch (type->code ())
4c4b4cd2
PH
670 {
671 case TYPE_CODE_RANGE:
d1fd641e
SM
672 {
673 const dynamic_prop &high = type->bounds ()->high;
674
675 if (high.kind () == PROP_CONST)
676 return high.const_val ();
677 else
678 {
679 gdb_assert (high.kind () == PROP_UNDEFINED);
680
681 /* This happens when trying to evaluate a type's dynamic bound
682 without a live target. There is nothing relevant for us to
683 return here, so return 0. */
684 return 0;
685 }
686 }
4c4b4cd2 687 case TYPE_CODE_ENUM:
970db518 688 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
689 case TYPE_CODE_BOOL:
690 return 1;
691 case TYPE_CODE_CHAR:
76a01679 692 case TYPE_CODE_INT:
690cc4eb 693 return max_of_type (type);
4c4b4cd2 694 default:
43bbcdc2 695 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
696 }
697}
698
14e75d8e 699/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
700LONGEST
701ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 702{
b249d2c2 703 type = resolve_dynamic_type (type, {}, 0);
78134374 704 switch (type->code ())
4c4b4cd2
PH
705 {
706 case TYPE_CODE_RANGE:
d1fd641e
SM
707 {
708 const dynamic_prop &low = type->bounds ()->low;
709
710 if (low.kind () == PROP_CONST)
711 return low.const_val ();
712 else
713 {
714 gdb_assert (low.kind () == PROP_UNDEFINED);
715
716 /* This happens when trying to evaluate a type's dynamic bound
717 without a live target. There is nothing relevant for us to
718 return here, so return 0. */
719 return 0;
720 }
721 }
4c4b4cd2 722 case TYPE_CODE_ENUM:
970db518 723 return type->field (0).loc_enumval ();
690cc4eb
PH
724 case TYPE_CODE_BOOL:
725 return 0;
726 case TYPE_CODE_CHAR:
76a01679 727 case TYPE_CODE_INT:
690cc4eb 728 return min_of_type (type);
4c4b4cd2 729 default:
43bbcdc2 730 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
731 }
732}
733
734/* The identity on non-range types. For range types, the underlying
76a01679 735 non-range scalar type. */
4c4b4cd2
PH
736
737static struct type *
18af8284 738get_base_type (struct type *type)
4c4b4cd2 739{
78134374 740 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 741 {
27710edb 742 if (type == type->target_type () || type->target_type () == NULL)
dda83cd7 743 return type;
27710edb 744 type = type->target_type ();
4c4b4cd2
PH
745 }
746 return type;
14f9c5c9 747}
41246937
JB
748
749/* Return a decoded version of the given VALUE. This means returning
750 a value whose type is obtained by applying all the GNAT-specific
85102364 751 encodings, making the resulting type a static but standard description
41246937
JB
752 of the initial type. */
753
754struct value *
755ada_get_decoded_value (struct value *value)
756{
d0c97917 757 struct type *type = ada_check_typedef (value->type ());
41246937
JB
758
759 if (ada_is_array_descriptor_type (type)
760 || (ada_is_constrained_packed_array_type (type)
dda83cd7 761 && type->code () != TYPE_CODE_PTR))
41246937 762 {
78134374 763 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 764 value = ada_coerce_to_simple_array_ptr (value);
41246937 765 else
dda83cd7 766 value = ada_coerce_to_simple_array (value);
41246937
JB
767 }
768 else
769 value = ada_to_fixed_value (value);
770
771 return value;
772}
773
774/* Same as ada_get_decoded_value, but with the given TYPE.
775 Because there is no associated actual value for this type,
776 the resulting type might be a best-effort approximation in
777 the case of dynamic types. */
778
779struct type *
780ada_get_decoded_type (struct type *type)
781{
782 type = to_static_fixed_type (type);
783 if (ada_is_constrained_packed_array_type (type))
784 type = ada_coerce_to_simple_array_type (type);
785 return type;
786}
787
4c4b4cd2 788\f
76a01679 789
dda83cd7 790 /* Language Selection */
14f9c5c9
AS
791
792/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 793 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 794
de93309a 795static enum language
ccefe4c4 796ada_update_initial_language (enum language lang)
14f9c5c9 797{
cafb3438 798 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 799 return language_ada;
14f9c5c9
AS
800
801 return lang;
802}
96d887e8
PH
803
804/* If the main procedure is written in Ada, then return its name.
805 The result is good until the next call. Return NULL if the main
806 procedure doesn't appear to be in Ada. */
807
6f63b61d
TT
808const char *
809ada_main_name ()
96d887e8 810{
3b7344d5 811 struct bound_minimal_symbol msym;
e83e4e24 812 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 813
96d887e8
PH
814 /* For Ada, the name of the main procedure is stored in a specific
815 string constant, generated by the binder. Look for that symbol,
816 extract its address, and then read that string. If we didn't find
817 that string, then most probably the main procedure is not written
818 in Ada. */
819 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
820
3b7344d5 821 if (msym.minsym != NULL)
96d887e8 822 {
4aeddc50 823 CORE_ADDR main_program_name_addr = msym.value_address ();
96d887e8 824 if (main_program_name_addr == 0)
dda83cd7 825 error (_("Invalid address for Ada main program name."));
96d887e8 826
66920317 827 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 828 return main_program_name.get ();
96d887e8
PH
829 }
830
831 /* The main procedure doesn't seem to be in Ada. */
832 return NULL;
833}
14f9c5c9 834\f
dda83cd7 835 /* Symbols */
d2e4a39e 836
4c4b4cd2
PH
837/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
838 of NULLs. */
14f9c5c9 839
d2e4a39e
AS
840const struct ada_opname_map ada_opname_table[] = {
841 {"Oadd", "\"+\"", BINOP_ADD},
842 {"Osubtract", "\"-\"", BINOP_SUB},
843 {"Omultiply", "\"*\"", BINOP_MUL},
844 {"Odivide", "\"/\"", BINOP_DIV},
845 {"Omod", "\"mod\"", BINOP_MOD},
846 {"Orem", "\"rem\"", BINOP_REM},
847 {"Oexpon", "\"**\"", BINOP_EXP},
848 {"Olt", "\"<\"", BINOP_LESS},
849 {"Ole", "\"<=\"", BINOP_LEQ},
850 {"Ogt", "\">\"", BINOP_GTR},
851 {"Oge", "\">=\"", BINOP_GEQ},
852 {"Oeq", "\"=\"", BINOP_EQUAL},
853 {"One", "\"/=\"", BINOP_NOTEQUAL},
854 {"Oand", "\"and\"", BINOP_BITWISE_AND},
855 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
856 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
857 {"Oconcat", "\"&\"", BINOP_CONCAT},
858 {"Oabs", "\"abs\"", UNOP_ABS},
859 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
860 {"Oadd", "\"+\"", UNOP_PLUS},
861 {"Osubtract", "\"-\"", UNOP_NEG},
862 {NULL, NULL}
14f9c5c9
AS
863};
864
965bc1df
TT
865/* If STR is a decoded version of a compiler-provided suffix (like the
866 "[cold]" in "symbol[cold]"), return true. Otherwise, return
867 false. */
868
869static bool
870is_compiler_suffix (const char *str)
871{
872 gdb_assert (*str == '[');
873 ++str;
874 while (*str != '\0' && isalpha (*str))
875 ++str;
876 /* We accept a missing "]" in order to support completion. */
877 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
878}
879
315e4ebb
TT
880/* Append a non-ASCII character to RESULT. */
881static void
882append_hex_encoded (std::string &result, uint32_t one_char)
883{
884 if (one_char <= 0xff)
885 {
886 result.append ("U");
887 result.append (phex (one_char, 1));
888 }
889 else if (one_char <= 0xffff)
890 {
891 result.append ("W");
892 result.append (phex (one_char, 2));
893 }
894 else
895 {
896 result.append ("WW");
897 result.append (phex (one_char, 4));
898 }
899}
900
901/* Return a string that is a copy of the data in STORAGE, with
902 non-ASCII characters replaced by the appropriate hex encoding. A
903 template is used because, for UTF-8, we actually want to work with
904 UTF-32 codepoints. */
905template<typename T>
906std::string
907copy_and_hex_encode (struct obstack *storage)
908{
909 const T *chars = (T *) obstack_base (storage);
910 int num_chars = obstack_object_size (storage) / sizeof (T);
911 std::string result;
912 for (int i = 0; i < num_chars; ++i)
913 {
914 if (chars[i] <= 0x7f)
915 {
916 /* The host character set has to be a superset of ASCII, as
917 are all the other character sets we can use. */
918 result.push_back (chars[i]);
919 }
920 else
921 append_hex_encoded (result, chars[i]);
922 }
923 return result;
924}
925
5c4258f4 926/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 927 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 928 Otherwise, return the empty string in that case. */
4c4b4cd2 929
5c4258f4 930static std::string
b5ec771e 931ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 932{
4c4b4cd2 933 if (decoded == NULL)
5c4258f4 934 return {};
14f9c5c9 935
5c4258f4 936 std::string encoding_buffer;
315e4ebb 937 bool saw_non_ascii = false;
5c4258f4 938 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 939 {
315e4ebb
TT
940 if ((*p & 0x80) != 0)
941 saw_non_ascii = true;
942
cdc7bb92 943 if (*p == '.')
5c4258f4 944 encoding_buffer.append ("__");
965bc1df
TT
945 else if (*p == '[' && is_compiler_suffix (p))
946 {
947 encoding_buffer = encoding_buffer + "." + (p + 1);
948 if (encoding_buffer.back () == ']')
949 encoding_buffer.pop_back ();
950 break;
951 }
14f9c5c9 952 else if (*p == '"')
dda83cd7
SM
953 {
954 const struct ada_opname_map *mapping;
955
956 for (mapping = ada_opname_table;
957 mapping->encoded != NULL
958 && !startswith (p, mapping->decoded); mapping += 1)
959 ;
960 if (mapping->encoded == NULL)
b5ec771e
PA
961 {
962 if (throw_errors)
963 error (_("invalid Ada operator name: %s"), p);
964 else
5c4258f4 965 return {};
b5ec771e 966 }
5c4258f4 967 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
968 break;
969 }
d2e4a39e 970 else
5c4258f4 971 encoding_buffer.push_back (*p);
14f9c5c9
AS
972 }
973
315e4ebb
TT
974 /* If a non-ASCII character is seen, we must convert it to the
975 appropriate hex form. As this is more expensive, we keep track
976 of whether it is even necessary. */
977 if (saw_non_ascii)
978 {
979 auto_obstack storage;
980 bool is_utf8 = ada_source_charset == ada_utf8;
981 try
982 {
983 convert_between_encodings
984 (host_charset (),
985 is_utf8 ? HOST_UTF32 : ada_source_charset,
986 (const gdb_byte *) encoding_buffer.c_str (),
987 encoding_buffer.length (), 1,
988 &storage, translit_none);
989 }
990 catch (const gdb_exception &)
991 {
992 static bool warned = false;
993
994 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
995 might like to know why. */
996 if (!warned)
997 {
998 warned = true;
999 warning (_("charset conversion failure for '%s'.\n"
1000 "You may have the wrong value for 'set ada source-charset'."),
1001 encoding_buffer.c_str ());
1002 }
1003
1004 /* We don't try to recover from errors. */
1005 return encoding_buffer;
1006 }
1007
1008 if (is_utf8)
1009 return copy_and_hex_encode<uint32_t> (&storage);
1010 return copy_and_hex_encode<gdb_byte> (&storage);
1011 }
1012
4c4b4cd2 1013 return encoding_buffer;
14f9c5c9
AS
1014}
1015
315e4ebb
TT
1016/* Find the entry for C in the case-folding table. Return nullptr if
1017 the entry does not cover C. */
1018static const utf8_entry *
1019find_case_fold_entry (uint32_t c)
b5ec771e 1020{
315e4ebb
TT
1021 auto iter = std::lower_bound (std::begin (ada_case_fold),
1022 std::end (ada_case_fold),
1023 c);
1024 if (iter == std::end (ada_case_fold)
1025 || c < iter->start
1026 || c > iter->end)
1027 return nullptr;
1028 return &*iter;
b5ec771e
PA
1029}
1030
14f9c5c9 1031/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1032 quotes, unfolded, but with the quotes stripped away. If
1033 THROW_ON_ERROR is true, encoding failures will throw an exception
1034 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1035
5f9febe0 1036static const char *
315e4ebb 1037ada_fold_name (gdb::string_view name, bool throw_on_error = false)
14f9c5c9 1038{
5f9febe0 1039 static std::string fold_storage;
14f9c5c9 1040
6a780b67 1041 if (!name.empty () && name[0] == '\'')
01573d73 1042 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
1043 else
1044 {
315e4ebb
TT
1045 /* Why convert to UTF-32 and implement our own case-folding,
1046 rather than convert to wchar_t and use the platform's
1047 functions? I'm glad you asked.
1048
1049 The main problem is that GNAT implements an unusual rule for
1050 case folding. For ASCII letters, letters in single-byte
1051 encodings (such as ISO-8859-*), and Unicode letters that fit
1052 in a single byte (i.e., code point is <= 0xff), the letter is
1053 folded to lower case. Other Unicode letters are folded to
1054 upper case.
1055
1056 This rule means that the code must be able to examine the
1057 value of the character. And, some hosts do not use Unicode
1058 for wchar_t, so examining the value of such characters is
1059 forbidden. */
1060 auto_obstack storage;
1061 try
1062 {
1063 convert_between_encodings
1064 (host_charset (), HOST_UTF32,
1065 (const gdb_byte *) name.data (),
1066 name.length (), 1,
1067 &storage, translit_none);
1068 }
1069 catch (const gdb_exception &)
1070 {
1071 if (throw_on_error)
1072 throw;
1073
1074 static bool warned = false;
1075
1076 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1077 might like to know why. */
1078 if (!warned)
1079 {
1080 warned = true;
1081 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1082 "This normally should not happen, please file a bug report."),
1083 gdb::to_string (name).c_str (), host_charset ());
1084 }
1085
1086 /* We don't try to recover from errors; just return the
1087 original string. */
1088 fold_storage = gdb::to_string (name);
1089 return fold_storage.c_str ();
1090 }
1091
1092 bool is_utf8 = ada_source_charset == ada_utf8;
1093 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1094 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1095 for (int i = 0; i < num_chars; ++i)
1096 {
1097 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1098 if (entry != nullptr)
1099 {
1100 uint32_t low = chars[i] + entry->lower_delta;
1101 if (!is_utf8 || low <= 0xff)
1102 chars[i] = low;
1103 else
1104 chars[i] = chars[i] + entry->upper_delta;
1105 }
1106 }
1107
1108 /* Now convert back to ordinary characters. */
1109 auto_obstack reconverted;
1110 try
1111 {
1112 convert_between_encodings (HOST_UTF32,
1113 host_charset (),
1114 (const gdb_byte *) chars,
1115 num_chars * sizeof (uint32_t),
1116 sizeof (uint32_t),
1117 &reconverted,
1118 translit_none);
1119 obstack_1grow (&reconverted, '\0');
1120 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1121 }
1122 catch (const gdb_exception &)
1123 {
1124 if (throw_on_error)
1125 throw;
1126
1127 static bool warned = false;
1128
1129 /* Converting back from UTF-32 shouldn't normally fail, but
1130 there are some host encodings without upper/lower
1131 equivalence. */
1132 if (!warned)
1133 {
1134 warned = true;
1135 warning (_("could not convert the lower-cased variant of '%s'\n"
1136 "from UTF-32 to the host encoding (%s)."),
1137 gdb::to_string (name).c_str (), host_charset ());
1138 }
1139
1140 /* We don't try to recover from errors; just return the
1141 original string. */
1142 fold_storage = gdb::to_string (name);
1143 }
14f9c5c9
AS
1144 }
1145
5f9febe0 1146 return fold_storage.c_str ();
14f9c5c9
AS
1147}
1148
5fea9794
TT
1149/* The "encoded" form of DECODED, according to GNAT conventions. If
1150 FOLD is true (the default), case-fold any ordinary symbol. Symbols
1151 with <...> quoting are not folded in any case. */
315e4ebb
TT
1152
1153std::string
5fea9794 1154ada_encode (const char *decoded, bool fold)
315e4ebb 1155{
5fea9794 1156 if (fold && decoded[0] != '<')
315e4ebb
TT
1157 decoded = ada_fold_name (decoded);
1158 return ada_encode_1 (decoded, true);
1159}
1160
529cad9c
PH
1161/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1162
1163static int
1164is_lower_alphanum (const char c)
1165{
1166 return (isdigit (c) || (isalpha (c) && islower (c)));
1167}
1168
c90092fe
JB
1169/* ENCODED is the linkage name of a symbol and LEN contains its length.
1170 This function saves in LEN the length of that same symbol name but
1171 without either of these suffixes:
29480c32
JB
1172 . .{DIGIT}+
1173 . ${DIGIT}+
1174 . ___{DIGIT}+
1175 . __{DIGIT}+.
c90092fe 1176
29480c32
JB
1177 These are suffixes introduced by the compiler for entities such as
1178 nested subprogram for instance, in order to avoid name clashes.
1179 They do not serve any purpose for the debugger. */
1180
1181static void
1182ada_remove_trailing_digits (const char *encoded, int *len)
1183{
1184 if (*len > 1 && isdigit (encoded[*len - 1]))
1185 {
1186 int i = *len - 2;
5b4ee69b 1187
29480c32 1188 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1189 i--;
29480c32 1190 if (i >= 0 && encoded[i] == '.')
dda83cd7 1191 *len = i;
29480c32 1192 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1193 *len = i;
61012eef 1194 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1195 *len = i - 2;
61012eef 1196 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1197 *len = i - 1;
29480c32
JB
1198 }
1199}
1200
1201/* Remove the suffix introduced by the compiler for protected object
1202 subprograms. */
1203
1204static void
1205ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1206{
1207 /* Remove trailing N. */
1208
1209 /* Protected entry subprograms are broken into two
1210 separate subprograms: The first one is unprotected, and has
1211 a 'N' suffix; the second is the protected version, and has
0963b4bd 1212 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1213 the protection. Since the P subprograms are internally generated,
1214 we leave these names undecoded, giving the user a clue that this
1215 entity is internal. */
1216
1217 if (*len > 1
1218 && encoded[*len - 1] == 'N'
1219 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1220 *len = *len - 1;
1221}
1222
965bc1df
TT
1223/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1224 then update *LEN to remove the suffix and return the offset of the
1225 character just past the ".". Otherwise, return -1. */
1226
1227static int
1228remove_compiler_suffix (const char *encoded, int *len)
1229{
1230 int offset = *len - 1;
1231 while (offset > 0 && isalpha (encoded[offset]))
1232 --offset;
1233 if (offset > 0 && encoded[offset] == '.')
1234 {
1235 *len = offset;
1236 return offset + 1;
1237 }
1238 return -1;
1239}
1240
315e4ebb
TT
1241/* Convert an ASCII hex string to a number. Reads exactly N
1242 characters from STR. Returns true on success, false if one of the
1243 digits was not a hex digit. */
1244static bool
1245convert_hex (const char *str, int n, uint32_t *out)
1246{
1247 uint32_t result = 0;
1248
1249 for (int i = 0; i < n; ++i)
1250 {
1251 if (!isxdigit (str[i]))
1252 return false;
1253 result <<= 4;
1254 result |= fromhex (str[i]);
1255 }
1256
1257 *out = result;
1258 return true;
1259}
1260
1261/* Convert a wide character from its ASCII hex representation in STR
1262 (consisting of exactly N characters) to the host encoding,
1263 appending the resulting bytes to OUT. If N==2 and the Ada source
1264 charset is not UTF-8, then hex refers to an encoding in the
1265 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1266 Return false and do not modify OUT on conversion failure. */
1267static bool
1268convert_from_hex_encoded (std::string &out, const char *str, int n)
1269{
1270 uint32_t value;
1271
1272 if (!convert_hex (str, n, &value))
1273 return false;
1274 try
1275 {
1276 auto_obstack bytes;
1277 /* In the 'U' case, the hex digits encode the character in the
1278 Ada source charset. However, if the source charset is UTF-8,
1279 this really means it is a single-byte UTF-32 character. */
1280 if (n == 2 && ada_source_charset != ada_utf8)
1281 {
1282 gdb_byte one_char = (gdb_byte) value;
1283
1284 convert_between_encodings (ada_source_charset, host_charset (),
1285 &one_char,
1286 sizeof (one_char), sizeof (one_char),
1287 &bytes, translit_none);
1288 }
1289 else
1290 convert_between_encodings (HOST_UTF32, host_charset (),
1291 (const gdb_byte *) &value,
1292 sizeof (value), sizeof (value),
1293 &bytes, translit_none);
1294 obstack_1grow (&bytes, '\0');
1295 out.append ((const char *) obstack_base (&bytes));
1296 }
1297 catch (const gdb_exception &)
1298 {
1299 /* On failure, the caller will just let the encoded form
1300 through, which seems basically reasonable. */
1301 return false;
1302 }
1303
1304 return true;
1305}
1306
8a3df5ac 1307/* See ada-lang.h. */
14f9c5c9 1308
f945dedf 1309std::string
5c94f938 1310ada_decode (const char *encoded, bool wrap, bool operators)
14f9c5c9 1311{
36f5ca53 1312 int i;
14f9c5c9 1313 int len0;
d2e4a39e 1314 const char *p;
14f9c5c9 1315 int at_start_name;
f945dedf 1316 std::string decoded;
965bc1df 1317 int suffix = -1;
d2e4a39e 1318
0d81f350
JG
1319 /* With function descriptors on PPC64, the value of a symbol named
1320 ".FN", if it exists, is the entry point of the function "FN". */
1321 if (encoded[0] == '.')
1322 encoded += 1;
1323
29480c32
JB
1324 /* The name of the Ada main procedure starts with "_ada_".
1325 This prefix is not part of the decoded name, so skip this part
1326 if we see this prefix. */
61012eef 1327 if (startswith (encoded, "_ada_"))
4c4b4cd2 1328 encoded += 5;
81eaa506
TT
1329 /* The "___ghost_" prefix is used for ghost entities. Normally
1330 these aren't preserved but when they are, it's useful to see
1331 them. */
1332 if (startswith (encoded, "___ghost_"))
1333 encoded += 9;
14f9c5c9 1334
29480c32
JB
1335 /* If the name starts with '_', then it is not a properly encoded
1336 name, so do not attempt to decode it. Similarly, if the name
1337 starts with '<', the name should not be decoded. */
4c4b4cd2 1338 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1339 goto Suppress;
1340
4c4b4cd2 1341 len0 = strlen (encoded);
4c4b4cd2 1342
965bc1df
TT
1343 suffix = remove_compiler_suffix (encoded, &len0);
1344
29480c32
JB
1345 ada_remove_trailing_digits (encoded, &len0);
1346 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1347
4c4b4cd2
PH
1348 /* Remove the ___X.* suffix if present. Do not forget to verify that
1349 the suffix is located before the current "end" of ENCODED. We want
1350 to avoid re-matching parts of ENCODED that have previously been
1351 marked as discarded (by decrementing LEN0). */
1352 p = strstr (encoded, "___");
1353 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1354 {
1355 if (p[3] == 'X')
dda83cd7 1356 len0 = p - encoded;
14f9c5c9 1357 else
dda83cd7 1358 goto Suppress;
14f9c5c9 1359 }
4c4b4cd2 1360
29480c32
JB
1361 /* Remove any trailing TKB suffix. It tells us that this symbol
1362 is for the body of a task, but that information does not actually
1363 appear in the decoded name. */
1364
61012eef 1365 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1366 len0 -= 3;
76a01679 1367
a10967fa
JB
1368 /* Remove any trailing TB suffix. The TB suffix is slightly different
1369 from the TKB suffix because it is used for non-anonymous task
1370 bodies. */
1371
61012eef 1372 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1373 len0 -= 2;
1374
29480c32
JB
1375 /* Remove trailing "B" suffixes. */
1376 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1377
61012eef 1378 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1379 len0 -= 1;
1380
29480c32
JB
1381 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1382
4c4b4cd2 1383 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1384 {
4c4b4cd2
PH
1385 i = len0 - 2;
1386 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1387 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1388 i -= 1;
4c4b4cd2 1389 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1390 len0 = i - 1;
4c4b4cd2 1391 else if (encoded[i] == '$')
dda83cd7 1392 len0 = i;
d2e4a39e 1393 }
14f9c5c9 1394
29480c32
JB
1395 /* The first few characters that are not alphabetic are not part
1396 of any encoding we use, so we can copy them over verbatim. */
1397
36f5ca53
TT
1398 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1399 decoded.push_back (encoded[i]);
14f9c5c9
AS
1400
1401 at_start_name = 1;
1402 while (i < len0)
1403 {
29480c32 1404 /* Is this a symbol function? */
5c94f938 1405 if (operators && at_start_name && encoded[i] == 'O')
dda83cd7
SM
1406 {
1407 int k;
1408
1409 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1410 {
1411 int op_len = strlen (ada_opname_table[k].encoded);
1412 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1413 op_len - 1) == 0)
1414 && !isalnum (encoded[i + op_len]))
1415 {
36f5ca53 1416 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1417 at_start_name = 0;
1418 i += op_len;
dda83cd7
SM
1419 break;
1420 }
1421 }
1422 if (ada_opname_table[k].encoded != NULL)
1423 continue;
1424 }
14f9c5c9
AS
1425 at_start_name = 0;
1426
529cad9c 1427 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1428 into "." (just below). */
529cad9c 1429
61012eef 1430 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1431 i += 2;
529cad9c 1432
29480c32 1433 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1434 be translated into "." (just below). These are internal names
1435 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1436
1437 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1438 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1439 && isdigit (encoded [i+4]))
1440 {
1441 int k = i + 5;
1442
1443 while (k < len0 && isdigit (encoded[k]))
1444 k++; /* Skip any extra digit. */
1445
1446 /* Double-check that the "__B_{DIGITS}+" sequence we found
1447 is indeed followed by "__". */
1448 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1449 i = k;
1450 }
29480c32 1451
529cad9c
PH
1452 /* Remove _E{DIGITS}+[sb] */
1453
1454 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1455 of subprograms created by the compiler for each entry. The first
1456 one implements the actual entry code, and has a suffix following
1457 the convention above; the second one implements the barrier and
1458 uses the same convention as above, except that the 'E' is replaced
1459 by a 'B'.
529cad9c 1460
dda83cd7
SM
1461 Just as above, we do not decode the name of barrier functions
1462 to give the user a clue that the code he is debugging has been
1463 internally generated. */
529cad9c
PH
1464
1465 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1466 && isdigit (encoded[i+2]))
1467 {
1468 int k = i + 3;
1469
1470 while (k < len0 && isdigit (encoded[k]))
1471 k++;
1472
1473 if (k < len0
1474 && (encoded[k] == 'b' || encoded[k] == 's'))
1475 {
1476 k++;
1477 /* Just as an extra precaution, make sure that if this
1478 suffix is followed by anything else, it is a '_'.
1479 Otherwise, we matched this sequence by accident. */
1480 if (k == len0
1481 || (k < len0 && encoded[k] == '_'))
1482 i = k;
1483 }
1484 }
529cad9c
PH
1485
1486 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1487 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1488
1489 if (i < len0 + 3
dda83cd7
SM
1490 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1491 {
1492 /* Backtrack a bit up until we reach either the begining of
1493 the encoded name, or "__". Make sure that we only find
1494 digits or lowercase characters. */
1495 const char *ptr = encoded + i - 1;
1496
1497 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1498 ptr--;
1499 if (ptr < encoded
1500 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1501 i++;
1502 }
529cad9c 1503
315e4ebb
TT
1504 if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
1505 {
1506 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1507 {
1508 i += 3;
1509 continue;
1510 }
1511 }
1512 else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
1513 {
1514 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1515 {
1516 i += 5;
1517 continue;
1518 }
1519 }
1520 else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
1521 && isxdigit (encoded[i + 2]))
1522 {
1523 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1524 {
1525 i += 10;
1526 continue;
1527 }
1528 }
1529
4c4b4cd2 1530 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1531 {
1532 /* This is a X[bn]* sequence not separated from the previous
1533 part of the name with a non-alpha-numeric character (in other
1534 words, immediately following an alpha-numeric character), then
1535 verify that it is placed at the end of the encoded name. If
1536 not, then the encoding is not valid and we should abort the
1537 decoding. Otherwise, just skip it, it is used in body-nested
1538 package names. */
1539 do
1540 i += 1;
1541 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1542 if (i < len0)
1543 goto Suppress;
1544 }
cdc7bb92 1545 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1546 {
1547 /* Replace '__' by '.'. */
36f5ca53 1548 decoded.push_back ('.');
dda83cd7
SM
1549 at_start_name = 1;
1550 i += 2;
dda83cd7 1551 }
14f9c5c9 1552 else
dda83cd7
SM
1553 {
1554 /* It's a character part of the decoded name, so just copy it
1555 over. */
36f5ca53 1556 decoded.push_back (encoded[i]);
dda83cd7 1557 i += 1;
dda83cd7 1558 }
14f9c5c9 1559 }
14f9c5c9 1560
29480c32
JB
1561 /* Decoded names should never contain any uppercase character.
1562 Double-check this, and abort the decoding if we find one. */
1563
5c94f938
TT
1564 if (operators)
1565 {
1566 for (i = 0; i < decoded.length(); ++i)
1567 if (isupper (decoded[i]) || decoded[i] == ' ')
1568 goto Suppress;
1569 }
14f9c5c9 1570
965bc1df
TT
1571 /* If the compiler added a suffix, append it now. */
1572 if (suffix >= 0)
1573 decoded = decoded + "[" + &encoded[suffix] + "]";
1574
f945dedf 1575 return decoded;
14f9c5c9
AS
1576
1577Suppress:
8a3df5ac
TT
1578 if (!wrap)
1579 return {};
1580
4c4b4cd2 1581 if (encoded[0] == '<')
f945dedf 1582 decoded = encoded;
14f9c5c9 1583 else
f945dedf 1584 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1585 return decoded;
4c4b4cd2
PH
1586}
1587
1588/* Table for keeping permanent unique copies of decoded names. Once
1589 allocated, names in this table are never released. While this is a
1590 storage leak, it should not be significant unless there are massive
1591 changes in the set of decoded names in successive versions of a
1592 symbol table loaded during a single session. */
1593static struct htab *decoded_names_store;
1594
1595/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1596 in the language-specific part of GSYMBOL, if it has not been
1597 previously computed. Tries to save the decoded name in the same
1598 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1599 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1600 GSYMBOL).
4c4b4cd2
PH
1601 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1602 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1603 when a decoded name is cached in it. */
4c4b4cd2 1604
45e6c716 1605const char *
f85f34ed 1606ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1607{
f85f34ed
TT
1608 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1609 const char **resultp =
615b3f62 1610 &gsymbol->language_specific.demangled_name;
5b4ee69b 1611
f85f34ed 1612 if (!gsymbol->ada_mangled)
4c4b4cd2 1613 {
4d4eaa30 1614 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1615 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1616
f85f34ed 1617 gsymbol->ada_mangled = 1;
5b4ee69b 1618
f85f34ed 1619 if (obstack != NULL)
f945dedf 1620 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1621 else
dda83cd7 1622 {
f85f34ed
TT
1623 /* Sometimes, we can't find a corresponding objfile, in
1624 which case, we put the result on the heap. Since we only
1625 decode when needed, we hope this usually does not cause a
1626 significant memory leak (FIXME). */
1627
dda83cd7
SM
1628 char **slot = (char **) htab_find_slot (decoded_names_store,
1629 decoded.c_str (), INSERT);
5b4ee69b 1630
dda83cd7
SM
1631 if (*slot == NULL)
1632 *slot = xstrdup (decoded.c_str ());
1633 *resultp = *slot;
1634 }
4c4b4cd2 1635 }
14f9c5c9 1636
4c4b4cd2
PH
1637 return *resultp;
1638}
76a01679 1639
14f9c5c9 1640\f
d2e4a39e 1641
dda83cd7 1642 /* Arrays */
14f9c5c9 1643
28c85d6c
JB
1644/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1645 generated by the GNAT compiler to describe the index type used
1646 for each dimension of an array, check whether it follows the latest
1647 known encoding. If not, fix it up to conform to the latest encoding.
1648 Otherwise, do nothing. This function also does nothing if
1649 INDEX_DESC_TYPE is NULL.
1650
85102364 1651 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1652 Initially, the information would be provided through the name of each
1653 field of the structure type only, while the type of these fields was
1654 described as unspecified and irrelevant. The debugger was then expected
1655 to perform a global type lookup using the name of that field in order
1656 to get access to the full index type description. Because these global
1657 lookups can be very expensive, the encoding was later enhanced to make
1658 the global lookup unnecessary by defining the field type as being
1659 the full index type description.
1660
1661 The purpose of this routine is to allow us to support older versions
1662 of the compiler by detecting the use of the older encoding, and by
1663 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1664 we essentially replace each field's meaningless type by the associated
1665 index subtype). */
1666
1667void
1668ada_fixup_array_indexes_type (struct type *index_desc_type)
1669{
1670 int i;
1671
1672 if (index_desc_type == NULL)
1673 return;
1f704f76 1674 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1675
1676 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1677 to check one field only, no need to check them all). If not, return
1678 now.
1679
1680 If our INDEX_DESC_TYPE was generated using the older encoding,
1681 the field type should be a meaningless integer type whose name
1682 is not equal to the field name. */
940da03e
SM
1683 if (index_desc_type->field (0).type ()->name () != NULL
1684 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1685 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1686 return;
1687
1688 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1689 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1690 {
33d16dd9 1691 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1692 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1693
1694 if (raw_type)
5d14b6e5 1695 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1696 }
1697}
1698
4c4b4cd2
PH
1699/* The desc_* routines return primitive portions of array descriptors
1700 (fat pointers). */
14f9c5c9
AS
1701
1702/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1703 level of indirection, if needed. */
1704
d2e4a39e
AS
1705static struct type *
1706desc_base_type (struct type *type)
14f9c5c9
AS
1707{
1708 if (type == NULL)
1709 return NULL;
61ee279c 1710 type = ada_check_typedef (type);
78134374 1711 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1712 type = ada_typedef_target_type (type);
1713
1265e4aa 1714 if (type != NULL
78134374 1715 && (type->code () == TYPE_CODE_PTR
dda83cd7 1716 || type->code () == TYPE_CODE_REF))
27710edb 1717 return ada_check_typedef (type->target_type ());
14f9c5c9
AS
1718 else
1719 return type;
1720}
1721
4c4b4cd2
PH
1722/* True iff TYPE indicates a "thin" array pointer type. */
1723
14f9c5c9 1724static int
d2e4a39e 1725is_thin_pntr (struct type *type)
14f9c5c9 1726{
d2e4a39e 1727 return
14f9c5c9
AS
1728 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1729 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1730}
1731
4c4b4cd2
PH
1732/* The descriptor type for thin pointer type TYPE. */
1733
d2e4a39e
AS
1734static struct type *
1735thin_descriptor_type (struct type *type)
14f9c5c9 1736{
d2e4a39e 1737 struct type *base_type = desc_base_type (type);
5b4ee69b 1738
14f9c5c9
AS
1739 if (base_type == NULL)
1740 return NULL;
1741 if (is_suffix (ada_type_name (base_type), "___XVE"))
1742 return base_type;
d2e4a39e 1743 else
14f9c5c9 1744 {
d2e4a39e 1745 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1746
14f9c5c9 1747 if (alt_type == NULL)
dda83cd7 1748 return base_type;
14f9c5c9 1749 else
dda83cd7 1750 return alt_type;
14f9c5c9
AS
1751 }
1752}
1753
4c4b4cd2
PH
1754/* A pointer to the array data for thin-pointer value VAL. */
1755
d2e4a39e
AS
1756static struct value *
1757thin_data_pntr (struct value *val)
14f9c5c9 1758{
d0c97917 1759 struct type *type = ada_check_typedef (val->type ());
556bdfd4 1760 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1761
556bdfd4
UW
1762 data_type = lookup_pointer_type (data_type);
1763
78134374 1764 if (type->code () == TYPE_CODE_PTR)
cda03344 1765 return value_cast (data_type, val->copy ());
d2e4a39e 1766 else
9feb2d07 1767 return value_from_longest (data_type, val->address ());
14f9c5c9
AS
1768}
1769
4c4b4cd2
PH
1770/* True iff TYPE indicates a "thick" array pointer type. */
1771
14f9c5c9 1772static int
d2e4a39e 1773is_thick_pntr (struct type *type)
14f9c5c9
AS
1774{
1775 type = desc_base_type (type);
78134374 1776 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1777 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1778}
1779
4c4b4cd2
PH
1780/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1781 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1782
d2e4a39e
AS
1783static struct type *
1784desc_bounds_type (struct type *type)
14f9c5c9 1785{
d2e4a39e 1786 struct type *r;
14f9c5c9
AS
1787
1788 type = desc_base_type (type);
1789
1790 if (type == NULL)
1791 return NULL;
1792 else if (is_thin_pntr (type))
1793 {
1794 type = thin_descriptor_type (type);
1795 if (type == NULL)
dda83cd7 1796 return NULL;
14f9c5c9
AS
1797 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1798 if (r != NULL)
dda83cd7 1799 return ada_check_typedef (r);
14f9c5c9 1800 }
78134374 1801 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1802 {
1803 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1804 if (r != NULL)
27710edb 1805 return ada_check_typedef (ada_check_typedef (r)->target_type ());
14f9c5c9
AS
1806 }
1807 return NULL;
1808}
1809
1810/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1811 one, a pointer to its bounds data. Otherwise NULL. */
1812
d2e4a39e
AS
1813static struct value *
1814desc_bounds (struct value *arr)
14f9c5c9 1815{
d0c97917 1816 struct type *type = ada_check_typedef (arr->type ());
5b4ee69b 1817
d2e4a39e 1818 if (is_thin_pntr (type))
14f9c5c9 1819 {
d2e4a39e 1820 struct type *bounds_type =
dda83cd7 1821 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1822 LONGEST addr;
1823
4cdfadb1 1824 if (bounds_type == NULL)
dda83cd7 1825 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1826
1827 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1828 since desc_type is an XVE-encoded type (and shouldn't be),
1829 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1830 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1831 addr = value_as_long (arr);
d2e4a39e 1832 else
9feb2d07 1833 addr = arr->address ();
14f9c5c9 1834
d2e4a39e 1835 return
dda83cd7 1836 value_from_longest (lookup_pointer_type (bounds_type),
df86565b 1837 addr - bounds_type->length ());
14f9c5c9
AS
1838 }
1839
1840 else if (is_thick_pntr (type))
05e522ef 1841 {
158cc4fe 1842 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef 1843 _("Bad GNAT array descriptor"));
d0c97917 1844 struct type *p_bounds_type = p_bounds->type ();
05e522ef
JB
1845
1846 if (p_bounds_type
78134374 1847 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef 1848 {
27710edb 1849 struct type *target_type = p_bounds_type->target_type ();
05e522ef 1850
e46d3488 1851 if (target_type->is_stub ())
05e522ef
JB
1852 p_bounds = value_cast (lookup_pointer_type
1853 (ada_check_typedef (target_type)),
1854 p_bounds);
1855 }
1856 else
1857 error (_("Bad GNAT array descriptor"));
1858
1859 return p_bounds;
1860 }
14f9c5c9
AS
1861 else
1862 return NULL;
1863}
1864
4c4b4cd2
PH
1865/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1866 position of the field containing the address of the bounds data. */
1867
14f9c5c9 1868static int
d2e4a39e 1869fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1870{
b610c045 1871 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1872}
1873
1874/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1875 size of the field containing the address of the bounds data. */
1876
14f9c5c9 1877static int
d2e4a39e 1878fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1879{
1880 type = desc_base_type (type);
1881
d2e4a39e 1882 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1883 return TYPE_FIELD_BITSIZE (type, 1);
1884 else
df86565b 1885 return 8 * ada_check_typedef (type->field (1).type ())->length ();
14f9c5c9
AS
1886}
1887
4c4b4cd2 1888/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1889 pointer to one, the type of its array data (a array-with-no-bounds type);
1890 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1891 data. */
4c4b4cd2 1892
d2e4a39e 1893static struct type *
556bdfd4 1894desc_data_target_type (struct type *type)
14f9c5c9
AS
1895{
1896 type = desc_base_type (type);
1897
4c4b4cd2 1898 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1899 if (is_thin_pntr (type))
940da03e 1900 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1901 else if (is_thick_pntr (type))
556bdfd4
UW
1902 {
1903 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1904
1905 if (data_type
78134374 1906 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
27710edb 1907 return ada_check_typedef (data_type->target_type ());
556bdfd4
UW
1908 }
1909
1910 return NULL;
14f9c5c9
AS
1911}
1912
1913/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1914 its array data. */
4c4b4cd2 1915
d2e4a39e
AS
1916static struct value *
1917desc_data (struct value *arr)
14f9c5c9 1918{
d0c97917 1919 struct type *type = arr->type ();
5b4ee69b 1920
14f9c5c9
AS
1921 if (is_thin_pntr (type))
1922 return thin_data_pntr (arr);
1923 else if (is_thick_pntr (type))
158cc4fe 1924 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1925 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1926 else
1927 return NULL;
1928}
1929
1930
1931/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1932 position of the field containing the address of the data. */
1933
14f9c5c9 1934static int
d2e4a39e 1935fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1936{
b610c045 1937 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1938}
1939
1940/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1941 size of the field containing the address of the data. */
1942
14f9c5c9 1943static int
d2e4a39e 1944fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1945{
1946 type = desc_base_type (type);
1947
1948 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1949 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1950 else
df86565b 1951 return TARGET_CHAR_BIT * type->field (0).type ()->length ();
14f9c5c9
AS
1952}
1953
4c4b4cd2 1954/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1955 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1956 bound, if WHICH is 1. The first bound is I=1. */
1957
d2e4a39e
AS
1958static struct value *
1959desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1960{
250106a7
TT
1961 char bound_name[20];
1962 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1963 which ? 'U' : 'L', i - 1);
158cc4fe 1964 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1965 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1966}
1967
1968/* If BOUNDS is an array-bounds structure type, return the bit position
1969 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1970 bound, if WHICH is 1. The first bound is I=1. */
1971
14f9c5c9 1972static int
d2e4a39e 1973desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1974{
b610c045 1975 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1976}
1977
1978/* If BOUNDS is an array-bounds structure type, return the bit field size
1979 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1980 bound, if WHICH is 1. The first bound is I=1. */
1981
76a01679 1982static int
d2e4a39e 1983desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1984{
1985 type = desc_base_type (type);
1986
d2e4a39e
AS
1987 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1988 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1989 else
df86565b 1990 return 8 * type->field (2 * i + which - 2).type ()->length ();
14f9c5c9
AS
1991}
1992
1993/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1994 Ith bound (numbering from 1). Otherwise, NULL. */
1995
d2e4a39e
AS
1996static struct type *
1997desc_index_type (struct type *type, int i)
14f9c5c9
AS
1998{
1999 type = desc_base_type (type);
2000
78134374 2001 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
2002 {
2003 char bound_name[20];
2004 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
2005 return lookup_struct_elt_type (type, bound_name, 1);
2006 }
d2e4a39e 2007 else
14f9c5c9
AS
2008 return NULL;
2009}
2010
4c4b4cd2
PH
2011/* The number of index positions in the array-bounds type TYPE.
2012 Return 0 if TYPE is NULL. */
2013
14f9c5c9 2014static int
d2e4a39e 2015desc_arity (struct type *type)
14f9c5c9
AS
2016{
2017 type = desc_base_type (type);
2018
2019 if (type != NULL)
1f704f76 2020 return type->num_fields () / 2;
14f9c5c9
AS
2021 return 0;
2022}
2023
4c4b4cd2
PH
2024/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2025 an array descriptor type (representing an unconstrained array
2026 type). */
2027
76a01679
JB
2028static int
2029ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2030{
2031 if (type == NULL)
2032 return 0;
61ee279c 2033 type = ada_check_typedef (type);
78134374 2034 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2035 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2036}
2037
52ce6436 2038/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2039 * to one. */
52ce6436 2040
2c0b251b 2041static int
52ce6436
PH
2042ada_is_array_type (struct type *type)
2043{
78134374
SM
2044 while (type != NULL
2045 && (type->code () == TYPE_CODE_PTR
2046 || type->code () == TYPE_CODE_REF))
27710edb 2047 type = type->target_type ();
52ce6436
PH
2048 return ada_is_direct_array_type (type);
2049}
2050
4c4b4cd2 2051/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2052
14f9c5c9 2053int
4c4b4cd2 2054ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2055{
2056 if (type == NULL)
2057 return 0;
61ee279c 2058 type = ada_check_typedef (type);
78134374
SM
2059 return (type->code () == TYPE_CODE_ARRAY
2060 || (type->code () == TYPE_CODE_PTR
27710edb 2061 && (ada_check_typedef (type->target_type ())->code ()
78134374 2062 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2063}
2064
4c4b4cd2
PH
2065/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2066
14f9c5c9 2067int
4c4b4cd2 2068ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2069{
556bdfd4 2070 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2071
2072 if (type == NULL)
2073 return 0;
61ee279c 2074 type = ada_check_typedef (type);
556bdfd4 2075 return (data_type != NULL
78134374 2076 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2077 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2078}
2079
2080/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 2081 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 2082 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
2083 is still needed. */
2084
14f9c5c9 2085int
ebf56fd3 2086ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 2087{
d2e4a39e 2088 return
14f9c5c9 2089 type != NULL
78134374 2090 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 2091 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 2092 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 2093 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
2094}
2095
2096
4c4b4cd2 2097/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2098 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2099 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2100 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2101 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2102 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2103 a descriptor. */
de93309a
SM
2104
2105static struct type *
d2e4a39e 2106ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2107{
d0c97917
TT
2108 if (ada_is_constrained_packed_array_type (arr->type ()))
2109 return decode_constrained_packed_array_type (arr->type ());
14f9c5c9 2110
d0c97917
TT
2111 if (!ada_is_array_descriptor_type (arr->type ()))
2112 return arr->type ();
d2e4a39e
AS
2113
2114 if (!bounds)
ad82864c
JB
2115 {
2116 struct type *array_type =
d0c97917 2117 ada_check_typedef (desc_data_target_type (arr->type ()));
ad82864c 2118
d0c97917 2119 if (ada_is_unconstrained_packed_array_type (arr->type ()))
ad82864c 2120 TYPE_FIELD_BITSIZE (array_type, 0) =
d0c97917 2121 decode_packed_array_bitsize (arr->type ());
ad82864c
JB
2122
2123 return array_type;
2124 }
14f9c5c9
AS
2125 else
2126 {
d2e4a39e 2127 struct type *elt_type;
14f9c5c9 2128 int arity;
d2e4a39e 2129 struct value *descriptor;
14f9c5c9 2130
d0c97917
TT
2131 elt_type = ada_array_element_type (arr->type (), -1);
2132 arity = ada_array_arity (arr->type ());
14f9c5c9 2133
d2e4a39e 2134 if (elt_type == NULL || arity == 0)
d0c97917 2135 return ada_check_typedef (arr->type ());
14f9c5c9
AS
2136
2137 descriptor = desc_bounds (arr);
d2e4a39e 2138 if (value_as_long (descriptor) == 0)
dda83cd7 2139 return NULL;
d2e4a39e 2140 while (arity > 0)
dda83cd7 2141 {
d0c97917
TT
2142 struct type *range_type = alloc_type_copy (arr->type ());
2143 struct type *array_type = alloc_type_copy (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;
d0c97917 2148 create_static_range_type (range_type, low->type (),
0c9c3474
SA
2149 longest_to_int (value_as_long (low)),
2150 longest_to_int (value_as_long (high)));
dda83cd7 2151 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c 2152
d0c97917 2153 if (ada_is_unconstrained_packed_array_type (arr->type ()))
e67ad678
JB
2154 {
2155 /* We need to store the element packed bitsize, as well as
dda83cd7 2156 recompute the array size, because it was previously
e67ad678
JB
2157 computed based on the unpacked element size. */
2158 LONGEST lo = value_as_long (low);
2159 LONGEST hi = value_as_long (high);
2160
2161 TYPE_FIELD_BITSIZE (elt_type, 0) =
d0c97917 2162 decode_packed_array_bitsize (arr->type ());
e67ad678 2163 /* If the array has no element, then the size is already
dda83cd7 2164 zero, and does not need to be recomputed. */
e67ad678
JB
2165 if (lo < hi)
2166 {
2167 int array_bitsize =
dda83cd7 2168 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678 2169
b6cdbc9a 2170 array_type->set_length ((array_bitsize + 7) / 8);
e67ad678
JB
2171 }
2172 }
dda83cd7 2173 }
14f9c5c9
AS
2174
2175 return lookup_pointer_type (elt_type);
2176 }
2177}
2178
2179/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2180 Otherwise, returns either a standard GDB array with bounds set
2181 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2182 GDB array. Returns NULL if ARR is a null fat pointer. */
2183
d2e4a39e
AS
2184struct value *
2185ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2186{
d0c97917 2187 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2188 {
d2e4a39e 2189 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2190
14f9c5c9 2191 if (arrType == NULL)
dda83cd7 2192 return NULL;
cda03344 2193 return value_cast (arrType, desc_data (arr)->copy ());
14f9c5c9 2194 }
d0c97917 2195 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2196 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2197 else
2198 return arr;
2199}
2200
2201/* If ARR does not represent an array, returns ARR unchanged.
2202 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2203 be ARR itself if it already is in the proper form). */
2204
720d1a40 2205struct value *
d2e4a39e 2206ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2207{
d0c97917 2208 if (ada_is_array_descriptor_type (arr->type ()))
14f9c5c9 2209 {
d2e4a39e 2210 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2211
14f9c5c9 2212 if (arrVal == NULL)
dda83cd7 2213 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2214 return value_ind (arrVal);
2215 }
d0c97917 2216 else if (ada_is_constrained_packed_array_type (arr->type ()))
ad82864c 2217 return decode_constrained_packed_array (arr);
d2e4a39e 2218 else
14f9c5c9
AS
2219 return arr;
2220}
2221
2222/* If TYPE represents a GNAT array type, return it translated to an
2223 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2224 packing). For other types, is the identity. */
2225
d2e4a39e
AS
2226struct type *
2227ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2228{
ad82864c
JB
2229 if (ada_is_constrained_packed_array_type (type))
2230 return decode_constrained_packed_array_type (type);
17280b9f
UW
2231
2232 if (ada_is_array_descriptor_type (type))
556bdfd4 2233 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2234
2235 return type;
14f9c5c9
AS
2236}
2237
4c4b4cd2
PH
2238/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2239
ad82864c 2240static int
57567375 2241ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2242{
2243 if (type == NULL)
2244 return 0;
4c4b4cd2 2245 type = desc_base_type (type);
61ee279c 2246 type = ada_check_typedef (type);
d2e4a39e 2247 return
14f9c5c9
AS
2248 ada_type_name (type) != NULL
2249 && strstr (ada_type_name (type), "___XP") != NULL;
2250}
2251
ad82864c
JB
2252/* Non-zero iff TYPE represents a standard GNAT constrained
2253 packed-array type. */
2254
2255int
2256ada_is_constrained_packed_array_type (struct type *type)
2257{
57567375 2258 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2259 && !ada_is_array_descriptor_type (type);
2260}
2261
2262/* Non-zero iff TYPE represents an array descriptor for a
2263 unconstrained packed-array type. */
2264
2265static int
2266ada_is_unconstrained_packed_array_type (struct type *type)
2267{
57567375
TT
2268 if (!ada_is_array_descriptor_type (type))
2269 return 0;
2270
2271 if (ada_is_gnat_encoded_packed_array_type (type))
2272 return 1;
2273
2274 /* If we saw GNAT encodings, then the above code is sufficient.
2275 However, with minimal encodings, we will just have a thick
2276 pointer instead. */
2277 if (is_thick_pntr (type))
2278 {
2279 type = desc_base_type (type);
2280 /* The structure's first field is a pointer to an array, so this
2281 fetches the array type. */
27710edb 2282 type = type->field (0).type ()->target_type ();
af5300fe
TV
2283 if (type->code () == TYPE_CODE_TYPEDEF)
2284 type = ada_typedef_target_type (type);
57567375
TT
2285 /* Now we can see if the array elements are packed. */
2286 return TYPE_FIELD_BITSIZE (type, 0) > 0;
2287 }
2288
2289 return 0;
ad82864c
JB
2290}
2291
c9a28cbe
TT
2292/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2293 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2294
2295static bool
2296ada_is_any_packed_array_type (struct type *type)
2297{
2298 return (ada_is_constrained_packed_array_type (type)
2299 || (type->code () == TYPE_CODE_ARRAY
2300 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
2301}
2302
ad82864c
JB
2303/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2304 return the size of its elements in bits. */
2305
2306static long
2307decode_packed_array_bitsize (struct type *type)
2308{
0d5cff50
DE
2309 const char *raw_name;
2310 const char *tail;
ad82864c
JB
2311 long bits;
2312
720d1a40
JB
2313 /* Access to arrays implemented as fat pointers are encoded as a typedef
2314 of the fat pointer type. We need the name of the fat pointer type
2315 to do the decoding, so strip the typedef layer. */
78134374 2316 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2317 type = ada_typedef_target_type (type);
2318
2319 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2320 if (!raw_name)
2321 raw_name = ada_type_name (desc_base_type (type));
2322
2323 if (!raw_name)
2324 return 0;
2325
2326 tail = strstr (raw_name, "___XP");
57567375
TT
2327 if (tail == nullptr)
2328 {
2329 gdb_assert (is_thick_pntr (type));
2330 /* The structure's first field is a pointer to an array, so this
2331 fetches the array type. */
27710edb 2332 type = type->field (0).type ()->target_type ();
57567375
TT
2333 /* Now we can see if the array elements are packed. */
2334 return TYPE_FIELD_BITSIZE (type, 0);
2335 }
ad82864c
JB
2336
2337 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2338 {
2339 lim_warning
2340 (_("could not understand bit size information on packed array"));
2341 return 0;
2342 }
2343
2344 return bits;
2345}
2346
14f9c5c9
AS
2347/* Given that TYPE is a standard GDB array type with all bounds filled
2348 in, and that the element size of its ultimate scalar constituents
2349 (that is, either its elements, or, if it is an array of arrays, its
2350 elements' elements, etc.) is *ELT_BITS, return an identical type,
2351 but with the bit sizes of its elements (and those of any
2352 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2353 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2354 in bits.
2355
2356 Note that, for arrays whose index type has an XA encoding where
2357 a bound references a record discriminant, getting that discriminant,
2358 and therefore the actual value of that bound, is not possible
2359 because none of the given parameters gives us access to the record.
2360 This function assumes that it is OK in the context where it is being
2361 used to return an array whose bounds are still dynamic and where
2362 the length is arbitrary. */
4c4b4cd2 2363
d2e4a39e 2364static struct type *
ad82864c 2365constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2366{
d2e4a39e
AS
2367 struct type *new_elt_type;
2368 struct type *new_type;
99b1c762
JB
2369 struct type *index_type_desc;
2370 struct type *index_type;
14f9c5c9
AS
2371 LONGEST low_bound, high_bound;
2372
61ee279c 2373 type = ada_check_typedef (type);
78134374 2374 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2375 return type;
2376
99b1c762
JB
2377 index_type_desc = ada_find_parallel_type (type, "___XA");
2378 if (index_type_desc)
940da03e 2379 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2380 NULL);
2381 else
3d967001 2382 index_type = type->index_type ();
99b1c762 2383
e9bb382b 2384 new_type = alloc_type_copy (type);
ad82864c 2385 new_elt_type =
27710edb 2386 constrained_packed_array_type (ada_check_typedef (type->target_type ()),
ad82864c 2387 elt_bits);
99b1c762 2388 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9 2389 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2390 new_type->set_name (ada_type_name (type));
14f9c5c9 2391
78134374 2392 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2393 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2394 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2395 low_bound = high_bound = 0;
2396 if (high_bound < low_bound)
b6cdbc9a
SM
2397 {
2398 *elt_bits = 0;
2399 new_type->set_length (0);
2400 }
d2e4a39e 2401 else
14f9c5c9
AS
2402 {
2403 *elt_bits *= (high_bound - low_bound + 1);
b6cdbc9a 2404 new_type->set_length ((*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
14f9c5c9
AS
2405 }
2406
9cdd0d12 2407 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2408 return new_type;
2409}
2410
ad82864c
JB
2411/* The array type encoded by TYPE, where
2412 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2413
d2e4a39e 2414static struct type *
ad82864c 2415decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2416{
0d5cff50 2417 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2418 char *name;
0d5cff50 2419 const char *tail;
d2e4a39e 2420 struct type *shadow_type;
14f9c5c9 2421 long bits;
14f9c5c9 2422
727e3d2e
JB
2423 if (!raw_name)
2424 raw_name = ada_type_name (desc_base_type (type));
2425
2426 if (!raw_name)
2427 return NULL;
2428
2429 name = (char *) alloca (strlen (raw_name) + 1);
2430 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2431 type = desc_base_type (type);
2432
14f9c5c9
AS
2433 memcpy (name, raw_name, tail - raw_name);
2434 name[tail - raw_name] = '\000';
2435
b4ba55a1
JB
2436 shadow_type = ada_find_parallel_type_with_name (type, name);
2437
2438 if (shadow_type == NULL)
14f9c5c9 2439 {
323e0a4a 2440 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2441 return NULL;
2442 }
f168693b 2443 shadow_type = check_typedef (shadow_type);
14f9c5c9 2444
78134374 2445 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2446 {
0963b4bd
MS
2447 lim_warning (_("could not understand bounds "
2448 "information on packed array"));
14f9c5c9
AS
2449 return NULL;
2450 }
d2e4a39e 2451
ad82864c
JB
2452 bits = decode_packed_array_bitsize (type);
2453 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2454}
2455
a7400e44
TT
2456/* Helper function for decode_constrained_packed_array. Set the field
2457 bitsize on a series of packed arrays. Returns the number of
2458 elements in TYPE. */
2459
2460static LONGEST
2461recursively_update_array_bitsize (struct type *type)
2462{
2463 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2464
2465 LONGEST low, high;
1f8d2881 2466 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2467 || low > high)
2468 return 0;
2469 LONGEST our_len = high - low + 1;
2470
27710edb 2471 struct type *elt_type = type->target_type ();
a7400e44
TT
2472 if (elt_type->code () == TYPE_CODE_ARRAY)
2473 {
2474 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2475 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2476 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2477
b6cdbc9a
SM
2478 type->set_length (((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2479 / HOST_CHAR_BIT));
a7400e44
TT
2480 }
2481
2482 return our_len;
2483}
2484
ad82864c
JB
2485/* Given that ARR is a struct value *indicating a GNAT constrained packed
2486 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2487 standard GDB array type except that the BITSIZEs of the array
2488 target types are set to the number of bits in each element, and the
4c4b4cd2 2489 type length is set appropriately. */
14f9c5c9 2490
d2e4a39e 2491static struct value *
ad82864c 2492decode_constrained_packed_array (struct value *arr)
14f9c5c9 2493{
4c4b4cd2 2494 struct type *type;
14f9c5c9 2495
11aa919a
PMR
2496 /* If our value is a pointer, then dereference it. Likewise if
2497 the value is a reference. Make sure that this operation does not
2498 cause the target type to be fixed, as this would indirectly cause
2499 this array to be decoded. The rest of the routine assumes that
2500 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2501 and "value_ind" routines to perform the dereferencing, as opposed
2502 to using "ada_coerce_ref" or "ada_value_ind". */
2503 arr = coerce_ref (arr);
d0c97917 2504 if (ada_check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
284614f0 2505 arr = value_ind (arr);
4c4b4cd2 2506
d0c97917 2507 type = decode_constrained_packed_array_type (arr->type ());
14f9c5c9
AS
2508 if (type == NULL)
2509 {
323e0a4a 2510 error (_("can't unpack array"));
14f9c5c9
AS
2511 return NULL;
2512 }
61ee279c 2513
a7400e44
TT
2514 /* Decoding the packed array type could not correctly set the field
2515 bitsizes for any dimension except the innermost, because the
2516 bounds may be variable and were not passed to that function. So,
2517 we further resolve the array bounds here and then update the
2518 sizes. */
efaf1ae0 2519 const gdb_byte *valaddr = arr->contents_for_printing ().data ();
9feb2d07 2520 CORE_ADDR address = arr->address ();
a7400e44 2521 gdb::array_view<const gdb_byte> view
df86565b 2522 = gdb::make_array_view (valaddr, type->length ());
a7400e44
TT
2523 type = resolve_dynamic_type (type, view, address);
2524 recursively_update_array_bitsize (type);
2525
d0c97917
TT
2526 if (type_byte_order (arr->type ()) == BFD_ENDIAN_BIG
2527 && ada_is_modular_type (arr->type ()))
61ee279c
PH
2528 {
2529 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2530 array with no wrapper. In order to interpret the value through
2531 the (left-justified) packed array type we just built, we must
2532 first left-justify it. */
61ee279c
PH
2533 int bit_size, bit_pos;
2534 ULONGEST mod;
2535
d0c97917 2536 mod = ada_modulus (arr->type ()) - 1;
61ee279c
PH
2537 bit_size = 0;
2538 while (mod > 0)
2539 {
2540 bit_size += 1;
2541 mod >>= 1;
2542 }
d0c97917 2543 bit_pos = HOST_CHAR_BIT * arr->type ()->length () - bit_size;
61ee279c
PH
2544 arr = ada_value_primitive_packed_val (arr, NULL,
2545 bit_pos / HOST_CHAR_BIT,
2546 bit_pos % HOST_CHAR_BIT,
2547 bit_size,
2548 type);
2549 }
2550
4c4b4cd2 2551 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2552}
2553
2554
2555/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2556 given in IND. ARR must be a simple array. */
14f9c5c9 2557
d2e4a39e
AS
2558static struct value *
2559value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2560{
2561 int i;
2562 int bits, elt_off, bit_off;
2563 long elt_total_bit_offset;
d2e4a39e
AS
2564 struct type *elt_type;
2565 struct value *v;
14f9c5c9
AS
2566
2567 bits = 0;
2568 elt_total_bit_offset = 0;
d0c97917 2569 elt_type = ada_check_typedef (arr->type ());
d2e4a39e 2570 for (i = 0; i < arity; i += 1)
14f9c5c9 2571 {
78134374 2572 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2573 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2574 error
2575 (_("attempt to do packed indexing of "
0963b4bd 2576 "something other than a packed array"));
14f9c5c9 2577 else
dda83cd7
SM
2578 {
2579 struct type *range_type = elt_type->index_type ();
2580 LONGEST lowerbound, upperbound;
2581 LONGEST idx;
2582
1f8d2881 2583 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2584 {
2585 lim_warning (_("don't know bounds of array"));
2586 lowerbound = upperbound = 0;
2587 }
2588
2589 idx = pos_atr (ind[i]);
2590 if (idx < lowerbound || idx > upperbound)
2591 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2592 (long) idx);
dda83cd7
SM
2593 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2594 elt_total_bit_offset += (idx - lowerbound) * bits;
27710edb 2595 elt_type = ada_check_typedef (elt_type->target_type ());
dda83cd7 2596 }
14f9c5c9
AS
2597 }
2598 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2599 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2600
2601 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2602 bits, elt_type);
14f9c5c9
AS
2603 return v;
2604}
2605
4c4b4cd2 2606/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2607
2608static int
d2e4a39e 2609has_negatives (struct type *type)
14f9c5c9 2610{
78134374 2611 switch (type->code ())
d2e4a39e
AS
2612 {
2613 default:
2614 return 0;
2615 case TYPE_CODE_INT:
c6d940a9 2616 return !type->is_unsigned ();
d2e4a39e 2617 case TYPE_CODE_RANGE:
5537ddd0 2618 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2619 }
14f9c5c9 2620}
d2e4a39e 2621
f93fca70 2622/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2623 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2624 the unpacked buffer.
14f9c5c9 2625
5b639dea
JB
2626 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2627 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2628
f93fca70
JB
2629 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2630 zero otherwise.
14f9c5c9 2631
f93fca70 2632 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2633
f93fca70
JB
2634 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2635
2636static void
2637ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2638 gdb_byte *unpacked, int unpacked_len,
2639 int is_big_endian, int is_signed_type,
2640 int is_scalar)
2641{
a1c95e6b
JB
2642 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2643 int src_idx; /* Index into the source area */
2644 int src_bytes_left; /* Number of source bytes left to process. */
2645 int srcBitsLeft; /* Number of source bits left to move */
2646 int unusedLS; /* Number of bits in next significant
dda83cd7 2647 byte of source that are unused */
a1c95e6b 2648
a1c95e6b
JB
2649 int unpacked_idx; /* Index into the unpacked buffer */
2650 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2651
4c4b4cd2 2652 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2653 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2654 unsigned char sign;
a1c95e6b 2655
4c4b4cd2
PH
2656 /* Transmit bytes from least to most significant; delta is the direction
2657 the indices move. */
f93fca70 2658 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2659
5b639dea
JB
2660 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2661 bits from SRC. .*/
2662 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2663 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2664 bit_size, unpacked_len);
2665
14f9c5c9 2666 srcBitsLeft = bit_size;
086ca51f 2667 src_bytes_left = src_len;
f93fca70 2668 unpacked_bytes_left = unpacked_len;
14f9c5c9 2669 sign = 0;
f93fca70
JB
2670
2671 if (is_big_endian)
14f9c5c9 2672 {
086ca51f 2673 src_idx = src_len - 1;
f93fca70
JB
2674 if (is_signed_type
2675 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2676 sign = ~0;
d2e4a39e
AS
2677
2678 unusedLS =
dda83cd7
SM
2679 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2680 % HOST_CHAR_BIT;
14f9c5c9 2681
f93fca70
JB
2682 if (is_scalar)
2683 {
dda83cd7
SM
2684 accumSize = 0;
2685 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2686 }
2687 else
2688 {
dda83cd7
SM
2689 /* Non-scalar values must be aligned at a byte boundary... */
2690 accumSize =
2691 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2692 /* ... And are placed at the beginning (most-significant) bytes
2693 of the target. */
2694 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2695 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2696 }
14f9c5c9 2697 }
d2e4a39e 2698 else
14f9c5c9
AS
2699 {
2700 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2701
086ca51f 2702 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2703 unusedLS = bit_offset;
2704 accumSize = 0;
2705
f93fca70 2706 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2707 sign = ~0;
14f9c5c9 2708 }
d2e4a39e 2709
14f9c5c9 2710 accum = 0;
086ca51f 2711 while (src_bytes_left > 0)
14f9c5c9
AS
2712 {
2713 /* Mask for removing bits of the next source byte that are not
dda83cd7 2714 part of the value. */
d2e4a39e 2715 unsigned int unusedMSMask =
dda83cd7
SM
2716 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2717 1;
4c4b4cd2 2718 /* Sign-extend bits for this byte. */
14f9c5c9 2719 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2720
d2e4a39e 2721 accum |=
dda83cd7 2722 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2723 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2724 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2725 {
2726 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2727 accumSize -= HOST_CHAR_BIT;
2728 accum >>= HOST_CHAR_BIT;
2729 unpacked_bytes_left -= 1;
2730 unpacked_idx += delta;
2731 }
14f9c5c9
AS
2732 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2733 unusedLS = 0;
086ca51f
JB
2734 src_bytes_left -= 1;
2735 src_idx += delta;
14f9c5c9 2736 }
086ca51f 2737 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2738 {
2739 accum |= sign << accumSize;
db297a65 2740 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2741 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2742 if (accumSize < 0)
2743 accumSize = 0;
14f9c5c9 2744 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2745 unpacked_bytes_left -= 1;
2746 unpacked_idx += delta;
14f9c5c9 2747 }
f93fca70
JB
2748}
2749
2750/* Create a new value of type TYPE from the contents of OBJ starting
2751 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2752 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2753 assigning through the result will set the field fetched from.
2754 VALADDR is ignored unless OBJ is NULL, in which case,
2755 VALADDR+OFFSET must address the start of storage containing the
2756 packed value. The value returned in this case is never an lval.
2757 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2758
2759struct value *
2760ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2761 long offset, int bit_offset, int bit_size,
dda83cd7 2762 struct type *type)
f93fca70
JB
2763{
2764 struct value *v;
bfb1c796 2765 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2766 gdb_byte *unpacked;
220475ed 2767 const int is_scalar = is_scalar_type (type);
d5a22e77 2768 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2769 gdb::byte_vector staging;
f93fca70
JB
2770
2771 type = ada_check_typedef (type);
2772
d0a9e810 2773 if (obj == NULL)
bfb1c796 2774 src = valaddr + offset;
d0a9e810 2775 else
efaf1ae0 2776 src = obj->contents ().data () + offset;
d0a9e810
JB
2777
2778 if (is_dynamic_type (type))
2779 {
2780 /* The length of TYPE might by dynamic, so we need to resolve
2781 TYPE in order to know its actual size, which we then use
2782 to create the contents buffer of the value we return.
2783 The difficulty is that the data containing our object is
2784 packed, and therefore maybe not at a byte boundary. So, what
2785 we do, is unpack the data into a byte-aligned buffer, and then
2786 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2787 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2788 staging.resize (staging_len);
d0a9e810
JB
2789
2790 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2791 staging.data (), staging.size (),
d0a9e810
JB
2792 is_big_endian, has_negatives (type),
2793 is_scalar);
b249d2c2 2794 type = resolve_dynamic_type (type, staging, 0);
df86565b 2795 if (type->length () < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
0cafa88c
JB
2796 {
2797 /* This happens when the length of the object is dynamic,
2798 and is actually smaller than the space reserved for it.
2799 For instance, in an array of variant records, the bit_size
2800 we're given is the array stride, which is constant and
2801 normally equal to the maximum size of its element.
2802 But, in reality, each element only actually spans a portion
2803 of that stride. */
df86565b 2804 bit_size = type->length () * HOST_CHAR_BIT;
0cafa88c 2805 }
d0a9e810
JB
2806 }
2807
f93fca70
JB
2808 if (obj == NULL)
2809 {
317c3ed9 2810 v = value::allocate (type);
bfb1c796 2811 src = valaddr + offset;
f93fca70 2812 }
736355f2 2813 else if (obj->lval () == lval_memory && obj->lazy ())
f93fca70 2814 {
0cafa88c 2815 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2816 gdb_byte *buf;
0cafa88c 2817
9feb2d07 2818 v = value_at (type, obj->address () + offset);
bfb1c796 2819 buf = (gdb_byte *) alloca (src_len);
9feb2d07 2820 read_memory (v->address (), buf, src_len);
bfb1c796 2821 src = buf;
f93fca70
JB
2822 }
2823 else
2824 {
317c3ed9 2825 v = value::allocate (type);
efaf1ae0 2826 src = obj->contents ().data () + offset;
f93fca70
JB
2827 }
2828
2829 if (obj != NULL)
2830 {
2831 long new_offset = offset;
2832
8181b7b6 2833 v->set_component_location (obj);
5011c493 2834 v->set_bitpos (bit_offset + obj->bitpos ());
f49d5fa2 2835 v->set_bitsize (bit_size);
5011c493 2836 if (v->bitpos () >= HOST_CHAR_BIT)
dda83cd7 2837 {
f93fca70 2838 ++new_offset;
5011c493 2839 v->set_bitpos (v->bitpos () - HOST_CHAR_BIT);
dda83cd7 2840 }
76675c4d 2841 v->set_offset (new_offset);
f93fca70
JB
2842
2843 /* Also set the parent value. This is needed when trying to
2844 assign a new value (in inferior memory). */
fac7bdaa 2845 v->set_parent (obj);
f93fca70
JB
2846 }
2847 else
f49d5fa2 2848 v->set_bitsize (bit_size);
bbe912ba 2849 unpacked = v->contents_writeable ().data ();
f93fca70
JB
2850
2851 if (bit_size == 0)
2852 {
df86565b 2853 memset (unpacked, 0, type->length ());
f93fca70
JB
2854 return v;
2855 }
2856
df86565b 2857 if (staging.size () == type->length ())
f93fca70 2858 {
d0a9e810
JB
2859 /* Small short-cut: If we've unpacked the data into a buffer
2860 of the same size as TYPE's length, then we can reuse that,
2861 instead of doing the unpacking again. */
d5722aa2 2862 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2863 }
d0a9e810
JB
2864 else
2865 ada_unpack_from_contents (src, bit_offset, bit_size,
df86565b 2866 unpacked, type->length (),
d0a9e810 2867 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2868
14f9c5c9
AS
2869 return v;
2870}
d2e4a39e 2871
14f9c5c9
AS
2872/* Store the contents of FROMVAL into the location of TOVAL.
2873 Return a new value with the location of TOVAL and contents of
2874 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2875 floating-point or non-scalar types. */
14f9c5c9 2876
d2e4a39e
AS
2877static struct value *
2878ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2879{
d0c97917 2880 struct type *type = toval->type ();
f49d5fa2 2881 int bits = toval->bitsize ();
14f9c5c9 2882
52ce6436
PH
2883 toval = ada_coerce_ref (toval);
2884 fromval = ada_coerce_ref (fromval);
2885
d0c97917 2886 if (ada_is_direct_array_type (toval->type ()))
52ce6436 2887 toval = ada_coerce_to_simple_array (toval);
d0c97917 2888 if (ada_is_direct_array_type (fromval->type ()))
52ce6436
PH
2889 fromval = ada_coerce_to_simple_array (fromval);
2890
4b53ca88 2891 if (!toval->deprecated_modifiable ())
323e0a4a 2892 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2893
736355f2 2894 if (toval->lval () == lval_memory
14f9c5c9 2895 && bits > 0
78134374 2896 && (type->code () == TYPE_CODE_FLT
dda83cd7 2897 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2898 {
5011c493 2899 int len = (toval->bitpos ()
df407dfe 2900 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2901 int from_size;
224c3ddb 2902 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2903 struct value *val;
9feb2d07 2904 CORE_ADDR to_addr = toval->address ();
14f9c5c9 2905
78134374 2906 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2907 fromval = value_cast (type, fromval);
14f9c5c9 2908
52ce6436 2909 read_memory (to_addr, buffer, len);
f49d5fa2 2910 from_size = fromval->bitsize ();
aced2898 2911 if (from_size == 0)
d0c97917 2912 from_size = fromval->type ()->length () * TARGET_CHAR_BIT;
d48e62f4 2913
d5a22e77 2914 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4 2915 ULONGEST from_offset = 0;
d0c97917 2916 if (is_big_endian && is_scalar_type (fromval->type ()))
d48e62f4 2917 from_offset = from_size - bits;
5011c493 2918 copy_bitwise (buffer, toval->bitpos (),
efaf1ae0 2919 fromval->contents ().data (), from_offset,
d48e62f4 2920 bits, is_big_endian);
972daa01 2921 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2922
cda03344 2923 val = toval->copy ();
bbe912ba 2924 memcpy (val->contents_raw ().data (),
efaf1ae0 2925 fromval->contents ().data (),
df86565b 2926 type->length ());
81ae560c 2927 val->deprecated_set_type (type);
d2e4a39e 2928
14f9c5c9
AS
2929 return val;
2930 }
2931
2932 return value_assign (toval, fromval);
2933}
2934
2935
7c512744
JB
2936/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2937 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2938 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2939 COMPONENT, and not the inferior's memory. The current contents
2940 of COMPONENT are ignored.
2941
2942 Although not part of the initial design, this function also works
2943 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2944 had a null address, and COMPONENT had an address which is equal to
2945 its offset inside CONTAINER. */
2946
52ce6436
PH
2947static void
2948value_assign_to_component (struct value *container, struct value *component,
2949 struct value *val)
2950{
2951 LONGEST offset_in_container =
9feb2d07 2952 (LONGEST) (component->address () - container->address ());
7c512744 2953 int bit_offset_in_container =
5011c493 2954 component->bitpos () - container->bitpos ();
52ce6436 2955 int bits;
7c512744 2956
d0c97917 2957 val = value_cast (component->type (), val);
52ce6436 2958
f49d5fa2 2959 if (component->bitsize () == 0)
d0c97917 2960 bits = TARGET_CHAR_BIT * component->type ()->length ();
52ce6436 2961 else
f49d5fa2 2962 bits = component->bitsize ();
52ce6436 2963
d0c97917 2964 if (type_byte_order (container->type ()) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2965 {
2966 int src_offset;
2967
d0c97917 2968 if (is_scalar_type (check_typedef (component->type ())))
dda83cd7 2969 src_offset
d0c97917 2970 = component->type ()->length () * TARGET_CHAR_BIT - bits;
2a62dfa9
JB
2971 else
2972 src_offset = 0;
bbe912ba 2973 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2974 + offset_in_container),
5011c493 2975 container->bitpos () + bit_offset_in_container,
efaf1ae0 2976 val->contents ().data (), src_offset, bits, 1);
2a62dfa9 2977 }
52ce6436 2978 else
bbe912ba 2979 copy_bitwise ((container->contents_writeable ().data ()
50888e42 2980 + offset_in_container),
5011c493 2981 container->bitpos () + bit_offset_in_container,
efaf1ae0 2982 val->contents ().data (), 0, bits, 0);
7c512744
JB
2983}
2984
736ade86
XR
2985/* Determine if TYPE is an access to an unconstrained array. */
2986
d91e9ea8 2987bool
736ade86
XR
2988ada_is_access_to_unconstrained_array (struct type *type)
2989{
78134374 2990 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2991 && is_thick_pntr (ada_typedef_target_type (type)));
2992}
2993
4c4b4cd2
PH
2994/* The value of the element of array ARR at the ARITY indices given in IND.
2995 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2996 thereto. */
2997
d2e4a39e
AS
2998struct value *
2999ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3000{
3001 int k;
d2e4a39e
AS
3002 struct value *elt;
3003 struct type *elt_type;
14f9c5c9
AS
3004
3005 elt = ada_coerce_to_simple_array (arr);
3006
d0c97917 3007 elt_type = ada_check_typedef (elt->type ());
78134374 3008 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
3009 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
3010 return value_subscript_packed (elt, arity, ind);
3011
3012 for (k = 0; k < arity; k += 1)
3013 {
27710edb 3014 struct type *saved_elt_type = elt_type->target_type ();
b9c50e9a 3015
78134374 3016 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3017 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3018
2497b498 3019 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3020
3021 if (ada_is_access_to_unconstrained_array (saved_elt_type)
d0c97917 3022 && elt->type ()->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3023 {
3024 /* The element is a typedef to an unconstrained array,
3025 except that the value_subscript call stripped the
3026 typedef layer. The typedef layer is GNAT's way to
3027 specify that the element is, at the source level, an
3028 access to the unconstrained array, rather than the
3029 unconstrained array. So, we need to restore that
3030 typedef layer, which we can do by forcing the element's
3031 type back to its original type. Otherwise, the returned
3032 value is going to be printed as the array, rather
3033 than as an access. Another symptom of the same issue
3034 would be that an expression trying to dereference the
3035 element would also be improperly rejected. */
81ae560c 3036 elt->deprecated_set_type (saved_elt_type);
b9c50e9a
XR
3037 }
3038
d0c97917 3039 elt_type = ada_check_typedef (elt->type ());
14f9c5c9 3040 }
b9c50e9a 3041
14f9c5c9
AS
3042 return elt;
3043}
3044
deede10c
JB
3045/* Assuming ARR is a pointer to a GDB array, the value of the element
3046 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3047 Does not read the entire array into memory.
3048
3049 Note: Unlike what one would expect, this function is used instead of
3050 ada_value_subscript for basically all non-packed array types. The reason
3051 for this is that a side effect of doing our own pointer arithmetics instead
3052 of relying on value_subscript is that there is no implicit typedef peeling.
3053 This is important for arrays of array accesses, where it allows us to
3054 preserve the fact that the array's element is an array access, where the
3055 access part os encoded in a typedef layer. */
14f9c5c9 3056
2c0b251b 3057static struct value *
deede10c 3058ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3059{
3060 int k;
919e6dbe 3061 struct value *array_ind = ada_value_ind (arr);
deede10c 3062 struct type *type
463b870d 3063 = check_typedef (array_ind->enclosing_type ());
919e6dbe 3064
78134374 3065 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
3066 && TYPE_FIELD_BITSIZE (type, 0) > 0)
3067 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3068
3069 for (k = 0; k < arity; k += 1)
3070 {
3071 LONGEST lwb, upb;
14f9c5c9 3072
78134374 3073 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3074 error (_("too many subscripts (%d expected)"), k);
27710edb 3075 arr = value_cast (lookup_pointer_type (type->target_type ()),
cda03344 3076 arr->copy ());
3d967001 3077 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3078 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
27710edb 3079 type = type->target_type ();
14f9c5c9
AS
3080 }
3081
3082 return value_ind (arr);
3083}
3084
0b5d8877 3085/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3086 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3087 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3088 this array is LOW, as per Ada rules. */
0b5d8877 3089static struct value *
f5938064 3090ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3091 int low, int high)
0b5d8877 3092{
b0dd7688 3093 struct type *type0 = ada_check_typedef (type);
27710edb 3094 struct type *base_index_type = type0->index_type ()->target_type ();
0c9c3474 3095 struct type *index_type
aa715135 3096 = create_static_range_type (NULL, base_index_type, low, high);
9fe561ab 3097 struct type *slice_type = create_array_type_with_stride
27710edb 3098 (NULL, type0->target_type (), index_type,
24e99c6c 3099 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3100 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 3101 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 3102 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3103 CORE_ADDR base;
3104
6244c119
SM
3105 low_pos = discrete_position (base_index_type, low);
3106 base_low_pos = discrete_position (base_index_type, base_low);
3107
3108 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3109 {
3110 warning (_("unable to get positions in slice, use bounds instead"));
3111 low_pos = low;
3112 base_low_pos = base_low;
3113 }
5b4ee69b 3114
7ff5b937
TT
3115 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
3116 if (stride == 0)
df86565b 3117 stride = type0->target_type ()->length ();
7ff5b937 3118
6244c119 3119 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3120 return value_at_lazy (slice_type, base);
0b5d8877
PH
3121}
3122
3123
3124static struct value *
3125ada_value_slice (struct value *array, int low, int high)
3126{
d0c97917 3127 struct type *type = ada_check_typedef (array->type ());
27710edb 3128 struct type *base_index_type = type->index_type ()->target_type ();
0c9c3474 3129 struct type *index_type
3d967001 3130 = create_static_range_type (NULL, type->index_type (), low, high);
9fe561ab 3131 struct type *slice_type = create_array_type_with_stride
27710edb 3132 (NULL, type->target_type (), index_type,
24e99c6c 3133 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3134 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
3135 gdb::optional<LONGEST> low_pos, high_pos;
3136
5b4ee69b 3137
6244c119
SM
3138 low_pos = discrete_position (base_index_type, low);
3139 high_pos = discrete_position (base_index_type, high);
3140
3141 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3142 {
3143 warning (_("unable to get positions in slice, use bounds instead"));
3144 low_pos = low;
3145 high_pos = high;
3146 }
3147
3148 return value_cast (slice_type,
6244c119 3149 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3150}
3151
14f9c5c9
AS
3152/* If type is a record type in the form of a standard GNAT array
3153 descriptor, returns the number of dimensions for type. If arr is a
3154 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3155 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3156
3157int
d2e4a39e 3158ada_array_arity (struct type *type)
14f9c5c9
AS
3159{
3160 int arity;
3161
3162 if (type == NULL)
3163 return 0;
3164
3165 type = desc_base_type (type);
3166
3167 arity = 0;
78134374 3168 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3169 return desc_arity (desc_bounds_type (type));
d2e4a39e 3170 else
78134374 3171 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3172 {
dda83cd7 3173 arity += 1;
27710edb 3174 type = ada_check_typedef (type->target_type ());
14f9c5c9 3175 }
d2e4a39e 3176
14f9c5c9
AS
3177 return arity;
3178}
3179
3180/* If TYPE is a record type in the form of a standard GNAT array
3181 descriptor or a simple array type, returns the element type for
3182 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3183 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3184
d2e4a39e
AS
3185struct type *
3186ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3187{
3188 type = desc_base_type (type);
3189
78134374 3190 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3191 {
3192 int k;
d2e4a39e 3193 struct type *p_array_type;
14f9c5c9 3194
556bdfd4 3195 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3196
3197 k = ada_array_arity (type);
3198 if (k == 0)
dda83cd7 3199 return NULL;
d2e4a39e 3200
4c4b4cd2 3201 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3202 if (nindices >= 0 && k > nindices)
dda83cd7 3203 k = nindices;
d2e4a39e 3204 while (k > 0 && p_array_type != NULL)
dda83cd7 3205 {
27710edb 3206 p_array_type = ada_check_typedef (p_array_type->target_type ());
dda83cd7
SM
3207 k -= 1;
3208 }
14f9c5c9
AS
3209 return p_array_type;
3210 }
78134374 3211 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3212 {
78134374 3213 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7 3214 {
27710edb 3215 type = type->target_type ();
6a40c6e4
TT
3216 /* A multi-dimensional array is represented using a sequence
3217 of array types. If one of these types has a name, then
3218 it is not another dimension of the outer array, but
3219 rather the element type of the outermost array. */
3220 if (type->name () != nullptr)
3221 break;
dda83cd7
SM
3222 nindices -= 1;
3223 }
14f9c5c9
AS
3224 return type;
3225 }
3226
3227 return NULL;
3228}
3229
08a057e6 3230/* See ada-lang.h. */
14f9c5c9 3231
08a057e6 3232struct type *
1eea4ebd 3233ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3234{
4c4b4cd2
PH
3235 struct type *result_type;
3236
14f9c5c9
AS
3237 type = desc_base_type (type);
3238
1eea4ebd
UW
3239 if (n < 0 || n > ada_array_arity (type))
3240 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3241
4c4b4cd2 3242 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3243 {
3244 int i;
3245
3246 for (i = 1; i < n; i += 1)
2869ac4b
TT
3247 {
3248 type = ada_check_typedef (type);
27710edb 3249 type = type->target_type ();
2869ac4b 3250 }
27710edb 3251 result_type = ada_check_typedef (type)->index_type ()->target_type ();
4c4b4cd2 3252 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3253 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3254 perhaps stabsread.c would make more sense. */
78134374 3255 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3256 result_type = NULL;
14f9c5c9 3257 }
d2e4a39e 3258 else
1eea4ebd
UW
3259 {
3260 result_type = desc_index_type (desc_bounds_type (type), n);
3261 if (result_type == NULL)
3262 error (_("attempt to take bound of something that is not an array"));
3263 }
3264
3265 return result_type;
14f9c5c9
AS
3266}
3267
3268/* Given that arr is an array type, returns the lower bound of the
3269 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3270 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3271 array-descriptor type. It works for other arrays with bounds supplied
3272 by run-time quantities other than discriminants. */
14f9c5c9 3273
abb68b3e 3274static LONGEST
fb5e3d5c 3275ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3276{
8a48ac95 3277 struct type *type, *index_type_desc, *index_type;
1ce677a4 3278 int i;
262452ec
JK
3279
3280 gdb_assert (which == 0 || which == 1);
14f9c5c9 3281
ad82864c
JB
3282 if (ada_is_constrained_packed_array_type (arr_type))
3283 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3284
4c4b4cd2 3285 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 3286 return (LONGEST) - which;
14f9c5c9 3287
78134374 3288 if (arr_type->code () == TYPE_CODE_PTR)
27710edb 3289 type = arr_type->target_type ();
14f9c5c9
AS
3290 else
3291 type = arr_type;
3292
22c4c60c 3293 if (type->is_fixed_instance ())
bafffb51
JB
3294 {
3295 /* The array has already been fixed, so we do not need to
3296 check the parallel ___XA type again. That encoding has
3297 already been applied, so ignore it now. */
3298 index_type_desc = NULL;
3299 }
3300 else
3301 {
3302 index_type_desc = ada_find_parallel_type (type, "___XA");
3303 ada_fixup_array_indexes_type (index_type_desc);
3304 }
3305
262452ec 3306 if (index_type_desc != NULL)
940da03e 3307 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3308 NULL);
262452ec 3309 else
8a48ac95
JB
3310 {
3311 struct type *elt_type = check_typedef (type);
3312
3313 for (i = 1; i < n; i++)
27710edb 3314 elt_type = check_typedef (elt_type->target_type ());
8a48ac95 3315
3d967001 3316 index_type = elt_type->index_type ();
8a48ac95 3317 }
262452ec 3318
43bbcdc2
PH
3319 return
3320 (LONGEST) (which == 0
dda83cd7
SM
3321 ? ada_discrete_type_low_bound (index_type)
3322 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3323}
3324
3325/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3326 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3327 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3328 supplied by run-time quantities other than discriminants. */
14f9c5c9 3329
1eea4ebd 3330static LONGEST
4dc81987 3331ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3332{
eb479039
JB
3333 struct type *arr_type;
3334
d0c97917 3335 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3336 arr = value_ind (arr);
463b870d 3337 arr_type = arr->enclosing_type ();
14f9c5c9 3338
ad82864c
JB
3339 if (ada_is_constrained_packed_array_type (arr_type))
3340 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3341 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3342 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3343 else
1eea4ebd 3344 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3345}
3346
3347/* Given that arr is an array value, returns the length of the
3348 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3349 supplied by run-time quantities other than discriminants.
3350 Does not work for arrays indexed by enumeration types with representation
3351 clauses at the moment. */
14f9c5c9 3352
1eea4ebd 3353static LONGEST
d2e4a39e 3354ada_array_length (struct value *arr, int n)
14f9c5c9 3355{
aa715135
JG
3356 struct type *arr_type, *index_type;
3357 int low, high;
eb479039 3358
d0c97917 3359 if (check_typedef (arr->type ())->code () == TYPE_CODE_PTR)
eb479039 3360 arr = value_ind (arr);
463b870d 3361 arr_type = arr->enclosing_type ();
14f9c5c9 3362
ad82864c
JB
3363 if (ada_is_constrained_packed_array_type (arr_type))
3364 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3365
4c4b4cd2 3366 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3367 {
3368 low = ada_array_bound_from_type (arr_type, n, 0);
3369 high = ada_array_bound_from_type (arr_type, n, 1);
3370 }
14f9c5c9 3371 else
aa715135
JG
3372 {
3373 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3374 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3375 }
3376
f168693b 3377 arr_type = check_typedef (arr_type);
7150d33c 3378 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3379 if (index_type != NULL)
3380 {
3381 struct type *base_type;
78134374 3382 if (index_type->code () == TYPE_CODE_RANGE)
27710edb 3383 base_type = index_type->target_type ();
aa715135
JG
3384 else
3385 base_type = index_type;
3386
3387 low = pos_atr (value_from_longest (base_type, low));
3388 high = pos_atr (value_from_longest (base_type, high));
3389 }
3390 return high - low + 1;
4c4b4cd2
PH
3391}
3392
bff8c71f
TT
3393/* An array whose type is that of ARR_TYPE (an array type), with
3394 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3395 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3396
3397static struct value *
bff8c71f 3398empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3399{
b0dd7688 3400 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3401 struct type *index_type
3402 = create_static_range_type
27710edb 3403 (NULL, arr_type0->index_type ()->target_type (), low,
bff8c71f 3404 high < low ? low - 1 : high);
b0dd7688 3405 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3406
317c3ed9 3407 return value::allocate (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3408}
14f9c5c9 3409\f
d2e4a39e 3410
dda83cd7 3411 /* Name resolution */
14f9c5c9 3412
4c4b4cd2
PH
3413/* The "decoded" name for the user-definable Ada operator corresponding
3414 to OP. */
14f9c5c9 3415
d2e4a39e 3416static const char *
4c4b4cd2 3417ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3418{
3419 int i;
3420
4c4b4cd2 3421 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3422 {
3423 if (ada_opname_table[i].op == op)
dda83cd7 3424 return ada_opname_table[i].decoded;
14f9c5c9 3425 }
323e0a4a 3426 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3427}
3428
de93309a
SM
3429/* Returns true (non-zero) iff decoded name N0 should appear before N1
3430 in a listing of choices during disambiguation (see sort_choices, below).
3431 The idea is that overloadings of a subprogram name from the
3432 same package should sort in their source order. We settle for ordering
3433 such symbols by their trailing number (__N or $N). */
14f9c5c9 3434
de93309a
SM
3435static int
3436encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3437{
de93309a
SM
3438 if (N1 == NULL)
3439 return 0;
3440 else if (N0 == NULL)
3441 return 1;
3442 else
3443 {
3444 int k0, k1;
30b15541 3445
de93309a 3446 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3447 ;
de93309a 3448 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3449 ;
de93309a 3450 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3451 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3452 {
3453 int n0, n1;
3454
3455 n0 = k0;
3456 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3457 n0 -= 1;
3458 n1 = k1;
3459 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3460 n1 -= 1;
3461 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3462 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3463 }
de93309a
SM
3464 return (strcmp (N0, N1) < 0);
3465 }
14f9c5c9
AS
3466}
3467
de93309a
SM
3468/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3469 encoded names. */
14f9c5c9 3470
de93309a
SM
3471static void
3472sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3473{
14f9c5c9 3474 int i;
14f9c5c9 3475
de93309a 3476 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3477 {
de93309a
SM
3478 struct block_symbol sym = syms[i];
3479 int j;
3480
3481 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3482 {
3483 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3484 sym.symbol->linkage_name ()))
3485 break;
3486 syms[j + 1] = syms[j];
3487 }
de93309a
SM
3488 syms[j + 1] = sym;
3489 }
3490}
14f9c5c9 3491
de93309a
SM
3492/* Whether GDB should display formals and return types for functions in the
3493 overloads selection menu. */
3494static bool print_signatures = true;
4c4b4cd2 3495
de93309a
SM
3496/* Print the signature for SYM on STREAM according to the FLAGS options. For
3497 all but functions, the signature is just the name of the symbol. For
3498 functions, this is the name of the function, the list of types for formals
3499 and the return type (if any). */
4c4b4cd2 3500
de93309a
SM
3501static void
3502ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3503 const struct type_print_options *flags)
3504{
5f9c5a63 3505 struct type *type = sym->type ();
14f9c5c9 3506
6cb06a8c 3507 gdb_printf (stream, "%s", sym->print_name ());
de93309a
SM
3508 if (!print_signatures
3509 || type == NULL
78134374 3510 || type->code () != TYPE_CODE_FUNC)
de93309a 3511 return;
4c4b4cd2 3512
1f704f76 3513 if (type->num_fields () > 0)
de93309a
SM
3514 {
3515 int i;
14f9c5c9 3516
6cb06a8c 3517 gdb_printf (stream, " (");
1f704f76 3518 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3519 {
3520 if (i > 0)
6cb06a8c 3521 gdb_printf (stream, "; ");
940da03e 3522 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3523 flags);
3524 }
6cb06a8c 3525 gdb_printf (stream, ")");
de93309a 3526 }
27710edb
SM
3527 if (type->target_type () != NULL
3528 && type->target_type ()->code () != TYPE_CODE_VOID)
de93309a 3529 {
6cb06a8c 3530 gdb_printf (stream, " return ");
27710edb 3531 ada_print_type (type->target_type (), NULL, stream, -1, 0, flags);
de93309a
SM
3532 }
3533}
14f9c5c9 3534
de93309a
SM
3535/* Read and validate a set of numeric choices from the user in the
3536 range 0 .. N_CHOICES-1. Place the results in increasing
3537 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3538
de93309a
SM
3539 The user types choices as a sequence of numbers on one line
3540 separated by blanks, encoding them as follows:
14f9c5c9 3541
de93309a
SM
3542 + A choice of 0 means to cancel the selection, throwing an error.
3543 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3544 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3545
de93309a 3546 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3547
de93309a
SM
3548 ANNOTATION_SUFFIX, if present, is used to annotate the input
3549 prompts (for use with the -f switch). */
14f9c5c9 3550
de93309a
SM
3551static int
3552get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3553 int is_all_choice, const char *annotation_suffix)
de93309a 3554{
992a7040 3555 const char *args;
de93309a
SM
3556 const char *prompt;
3557 int n_chosen;
3558 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3559
de93309a
SM
3560 prompt = getenv ("PS2");
3561 if (prompt == NULL)
3562 prompt = "> ";
4c4b4cd2 3563
f8631e5e
SM
3564 std::string buffer;
3565 args = command_line_input (buffer, prompt, annotation_suffix);
4c4b4cd2 3566
de93309a
SM
3567 if (args == NULL)
3568 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3569
de93309a 3570 n_chosen = 0;
4c4b4cd2 3571
de93309a
SM
3572 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3573 order, as given in args. Choices are validated. */
3574 while (1)
14f9c5c9 3575 {
de93309a
SM
3576 char *args2;
3577 int choice, j;
76a01679 3578
de93309a
SM
3579 args = skip_spaces (args);
3580 if (*args == '\0' && n_chosen == 0)
dda83cd7 3581 error_no_arg (_("one or more choice numbers"));
de93309a 3582 else if (*args == '\0')
dda83cd7 3583 break;
76a01679 3584
de93309a
SM
3585 choice = strtol (args, &args2, 10);
3586 if (args == args2 || choice < 0
dda83cd7
SM
3587 || choice > n_choices + first_choice - 1)
3588 error (_("Argument must be choice number"));
de93309a 3589 args = args2;
76a01679 3590
de93309a 3591 if (choice == 0)
dda83cd7 3592 error (_("cancelled"));
76a01679 3593
de93309a 3594 if (choice < first_choice)
dda83cd7
SM
3595 {
3596 n_chosen = n_choices;
3597 for (j = 0; j < n_choices; j += 1)
3598 choices[j] = j;
3599 break;
3600 }
de93309a 3601 choice -= first_choice;
76a01679 3602
de93309a 3603 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3604 {
3605 }
4c4b4cd2 3606
de93309a 3607 if (j < 0 || choice != choices[j])
dda83cd7
SM
3608 {
3609 int k;
4c4b4cd2 3610
dda83cd7
SM
3611 for (k = n_chosen - 1; k > j; k -= 1)
3612 choices[k + 1] = choices[k];
3613 choices[j + 1] = choice;
3614 n_chosen += 1;
3615 }
14f9c5c9
AS
3616 }
3617
de93309a
SM
3618 if (n_chosen > max_results)
3619 error (_("Select no more than %d of the above"), max_results);
3620
3621 return n_chosen;
14f9c5c9
AS
3622}
3623
de93309a
SM
3624/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3625 by asking the user (if necessary), returning the number selected,
3626 and setting the first elements of SYMS items. Error if no symbols
3627 selected. */
3628
3629/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3630 to be re-integrated one of these days. */
14f9c5c9
AS
3631
3632static int
de93309a 3633user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3634{
de93309a
SM
3635 int i;
3636 int *chosen = XALLOCAVEC (int , nsyms);
3637 int n_chosen;
3638 int first_choice = (max_results == 1) ? 1 : 2;
3639 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3640
de93309a
SM
3641 if (max_results < 1)
3642 error (_("Request to select 0 symbols!"));
3643 if (nsyms <= 1)
3644 return nsyms;
14f9c5c9 3645
de93309a
SM
3646 if (select_mode == multiple_symbols_cancel)
3647 error (_("\
3648canceled because the command is ambiguous\n\
3649See set/show multiple-symbol."));
14f9c5c9 3650
de93309a
SM
3651 /* If select_mode is "all", then return all possible symbols.
3652 Only do that if more than one symbol can be selected, of course.
3653 Otherwise, display the menu as usual. */
3654 if (select_mode == multiple_symbols_all && max_results > 1)
3655 return nsyms;
14f9c5c9 3656
6cb06a8c 3657 gdb_printf (_("[0] cancel\n"));
de93309a 3658 if (max_results > 1)
6cb06a8c 3659 gdb_printf (_("[1] all\n"));
14f9c5c9 3660
de93309a 3661 sort_choices (syms, nsyms);
14f9c5c9 3662
de93309a
SM
3663 for (i = 0; i < nsyms; i += 1)
3664 {
3665 if (syms[i].symbol == NULL)
dda83cd7 3666 continue;
14f9c5c9 3667
66d7f48f 3668 if (syms[i].symbol->aclass () == LOC_BLOCK)
dda83cd7
SM
3669 {
3670 struct symtab_and_line sal =
3671 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3672
6cb06a8c 3673 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3674 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3675 &type_print_raw_options);
3676 if (sal.symtab == NULL)
6cb06a8c
TT
3677 gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
3678 metadata_style.style ().ptr (), nullptr, sal.line);
de93309a 3679 else
6cb06a8c 3680 gdb_printf
de93309a
SM
3681 (_(" at %ps:%d\n"),
3682 styled_string (file_name_style.style (),
3683 symtab_to_filename_for_display (sal.symtab)),
3684 sal.line);
dda83cd7
SM
3685 continue;
3686 }
76a01679 3687 else
dda83cd7
SM
3688 {
3689 int is_enumeral =
66d7f48f 3690 (syms[i].symbol->aclass () == LOC_CONST
5f9c5a63
SM
3691 && syms[i].symbol->type () != NULL
3692 && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
de93309a 3693 struct symtab *symtab = NULL;
4c4b4cd2 3694
7b3ecc75 3695 if (syms[i].symbol->is_objfile_owned ())
4206d69e 3696 symtab = syms[i].symbol->symtab ();
de93309a 3697
5d0027b9 3698 if (syms[i].symbol->line () != 0 && symtab != NULL)
de93309a 3699 {
6cb06a8c 3700 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3701 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3702 &type_print_raw_options);
6cb06a8c
TT
3703 gdb_printf (_(" at %s:%d\n"),
3704 symtab_to_filename_for_display (symtab),
3705 syms[i].symbol->line ());
de93309a 3706 }
dda83cd7 3707 else if (is_enumeral
5f9c5a63 3708 && syms[i].symbol->type ()->name () != NULL)
dda83cd7 3709 {
6cb06a8c 3710 gdb_printf (("[%d] "), i + first_choice);
5f9c5a63 3711 ada_print_type (syms[i].symbol->type (), NULL,
dda83cd7 3712 gdb_stdout, -1, 0, &type_print_raw_options);
6cb06a8c
TT
3713 gdb_printf (_("'(%s) (enumeral)\n"),
3714 syms[i].symbol->print_name ());
dda83cd7 3715 }
de93309a
SM
3716 else
3717 {
6cb06a8c 3718 gdb_printf ("[%d] ", i + first_choice);
de93309a
SM
3719 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3720 &type_print_raw_options);
3721
3722 if (symtab != NULL)
6cb06a8c
TT
3723 gdb_printf (is_enumeral
3724 ? _(" in %s (enumeral)\n")
3725 : _(" at %s:?\n"),
3726 symtab_to_filename_for_display (symtab));
de93309a 3727 else
6cb06a8c
TT
3728 gdb_printf (is_enumeral
3729 ? _(" (enumeral)\n")
3730 : _(" at ?\n"));
de93309a 3731 }
dda83cd7 3732 }
14f9c5c9 3733 }
14f9c5c9 3734
de93309a 3735 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3736 "overload-choice");
14f9c5c9 3737
de93309a
SM
3738 for (i = 0; i < n_chosen; i += 1)
3739 syms[i] = syms[chosen[i]];
14f9c5c9 3740
de93309a
SM
3741 return n_chosen;
3742}
14f9c5c9 3743
cd9a3148
TT
3744/* See ada-lang.h. */
3745
3746block_symbol
7056f312 3747ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3748 int nargs, value *argvec[])
3749{
3750 if (possible_user_operator_p (op, argvec))
3751 {
3752 std::vector<struct block_symbol> candidates
3753 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3754 NULL, VAR_DOMAIN);
3755
3756 int i = ada_resolve_function (candidates, argvec,
3757 nargs, ada_decoded_op_name (op), NULL,
3758 parse_completion);
3759 if (i >= 0)
3760 return candidates[i];
3761 }
3762 return {};
3763}
3764
3765/* See ada-lang.h. */
3766
3767block_symbol
3768ada_resolve_funcall (struct symbol *sym, const struct block *block,
3769 struct type *context_type,
7056f312 3770 bool parse_completion,
cd9a3148
TT
3771 int nargs, value *argvec[],
3772 innermost_block_tracker *tracker)
3773{
3774 std::vector<struct block_symbol> candidates
3775 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3776
3777 int i;
3778 if (candidates.size () == 1)
3779 i = 0;
3780 else
3781 {
3782 i = ada_resolve_function
3783 (candidates,
3784 argvec, nargs,
3785 sym->linkage_name (),
3786 context_type, parse_completion);
3787 if (i < 0)
3788 error (_("Could not find a match for %s"), sym->print_name ());
3789 }
3790
3791 tracker->update (candidates[i]);
3792 return candidates[i];
3793}
3794
ba8694b6
TT
3795/* Resolve a mention of a name where the context type is an
3796 enumeration type. */
3797
3798static int
3799ada_resolve_enum (std::vector<struct block_symbol> &syms,
3800 const char *name, struct type *context_type,
3801 bool parse_completion)
3802{
3803 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3804 context_type = ada_check_typedef (context_type);
3805
3806 for (int i = 0; i < syms.size (); ++i)
3807 {
3808 /* We already know the name matches, so we're just looking for
3809 an element of the correct enum type. */
5f9c5a63 3810 if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
ba8694b6
TT
3811 return i;
3812 }
3813
3814 error (_("No name '%s' in enumeration type '%s'"), name,
3815 ada_type_name (context_type));
3816}
3817
cd9a3148
TT
3818/* See ada-lang.h. */
3819
3820block_symbol
3821ada_resolve_variable (struct symbol *sym, const struct block *block,
3822 struct type *context_type,
7056f312 3823 bool parse_completion,
cd9a3148
TT
3824 int deprocedure_p,
3825 innermost_block_tracker *tracker)
3826{
3827 std::vector<struct block_symbol> candidates
3828 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3829
3830 if (std::any_of (candidates.begin (),
3831 candidates.end (),
3832 [] (block_symbol &bsym)
3833 {
66d7f48f 3834 switch (bsym.symbol->aclass ())
cd9a3148
TT
3835 {
3836 case LOC_REGISTER:
3837 case LOC_ARG:
3838 case LOC_REF_ARG:
3839 case LOC_REGPARM_ADDR:
3840 case LOC_LOCAL:
3841 case LOC_COMPUTED:
3842 return true;
3843 default:
3844 return false;
3845 }
3846 }))
3847 {
3848 /* Types tend to get re-introduced locally, so if there
3849 are any local symbols that are not types, first filter
3850 out all types. */
3851 candidates.erase
3852 (std::remove_if
3853 (candidates.begin (),
3854 candidates.end (),
3855 [] (block_symbol &bsym)
3856 {
66d7f48f 3857 return bsym.symbol->aclass () == LOC_TYPEDEF;
cd9a3148
TT
3858 }),
3859 candidates.end ());
3860 }
3861
2c71f639
TV
3862 /* Filter out artificial symbols. */
3863 candidates.erase
3864 (std::remove_if
3865 (candidates.begin (),
3866 candidates.end (),
3867 [] (block_symbol &bsym)
3868 {
496feb16 3869 return bsym.symbol->is_artificial ();
2c71f639
TV
3870 }),
3871 candidates.end ());
3872
cd9a3148
TT
3873 int i;
3874 if (candidates.empty ())
3875 error (_("No definition found for %s"), sym->print_name ());
3876 else if (candidates.size () == 1)
3877 i = 0;
ba8694b6
TT
3878 else if (context_type != nullptr
3879 && context_type->code () == TYPE_CODE_ENUM)
3880 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3881 parse_completion);
cd9a3148
TT
3882 else if (deprocedure_p && !is_nonfunction (candidates))
3883 {
3884 i = ada_resolve_function
3885 (candidates, NULL, 0,
3886 sym->linkage_name (),
3887 context_type, parse_completion);
3888 if (i < 0)
3889 error (_("Could not find a match for %s"), sym->print_name ());
3890 }
3891 else
3892 {
6cb06a8c 3893 gdb_printf (_("Multiple matches for %s\n"), sym->print_name ());
cd9a3148
TT
3894 user_select_syms (candidates.data (), candidates.size (), 1);
3895 i = 0;
3896 }
3897
3898 tracker->update (candidates[i]);
3899 return candidates[i];
3900}
3901
db2534b7 3902/* Return non-zero if formal type FTYPE matches actual type ATYPE. */
de93309a
SM
3903/* The term "match" here is rather loose. The match is heuristic and
3904 liberal. */
14f9c5c9 3905
de93309a 3906static int
db2534b7 3907ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3908{
de93309a
SM
3909 ftype = ada_check_typedef (ftype);
3910 atype = ada_check_typedef (atype);
14f9c5c9 3911
78134374 3912 if (ftype->code () == TYPE_CODE_REF)
27710edb 3913 ftype = ftype->target_type ();
78134374 3914 if (atype->code () == TYPE_CODE_REF)
27710edb 3915 atype = atype->target_type ();
14f9c5c9 3916
78134374 3917 switch (ftype->code ())
14f9c5c9 3918 {
de93309a 3919 default:
78134374 3920 return ftype->code () == atype->code ();
de93309a 3921 case TYPE_CODE_PTR:
db2534b7
TT
3922 if (atype->code () != TYPE_CODE_PTR)
3923 return 0;
27710edb 3924 atype = atype->target_type ();
db2534b7 3925 /* This can only happen if the actual argument is 'null'. */
df86565b 3926 if (atype->code () == TYPE_CODE_INT && atype->length () == 0)
db2534b7 3927 return 1;
27710edb 3928 return ada_type_match (ftype->target_type (), atype);
de93309a
SM
3929 case TYPE_CODE_INT:
3930 case TYPE_CODE_ENUM:
3931 case TYPE_CODE_RANGE:
78134374 3932 switch (atype->code ())
dda83cd7
SM
3933 {
3934 case TYPE_CODE_INT:
3935 case TYPE_CODE_ENUM:
3936 case TYPE_CODE_RANGE:
3937 return 1;
3938 default:
3939 return 0;
3940 }
d2e4a39e 3941
de93309a 3942 case TYPE_CODE_ARRAY:
78134374 3943 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3944 || ada_is_array_descriptor_type (atype));
14f9c5c9 3945
de93309a
SM
3946 case TYPE_CODE_STRUCT:
3947 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3948 return (atype->code () == TYPE_CODE_ARRAY
3949 || ada_is_array_descriptor_type (atype));
de93309a 3950 else
dda83cd7
SM
3951 return (atype->code () == TYPE_CODE_STRUCT
3952 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3953
de93309a
SM
3954 case TYPE_CODE_UNION:
3955 case TYPE_CODE_FLT:
78134374 3956 return (atype->code () == ftype->code ());
de93309a 3957 }
14f9c5c9
AS
3958}
3959
de93309a
SM
3960/* Return non-zero if the formals of FUNC "sufficiently match" the
3961 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3962 may also be an enumeral, in which case it is treated as a 0-
3963 argument function. */
14f9c5c9 3964
de93309a
SM
3965static int
3966ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3967{
3968 int i;
5f9c5a63 3969 struct type *func_type = func->type ();
14f9c5c9 3970
66d7f48f 3971 if (func->aclass () == LOC_CONST
78134374 3972 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3973 return (n_actuals == 0);
78134374 3974 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3975 return 0;
14f9c5c9 3976
1f704f76 3977 if (func_type->num_fields () != n_actuals)
de93309a 3978 return 0;
14f9c5c9 3979
de93309a
SM
3980 for (i = 0; i < n_actuals; i += 1)
3981 {
3982 if (actuals[i] == NULL)
dda83cd7 3983 return 0;
de93309a 3984 else
dda83cd7
SM
3985 {
3986 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
d0c97917 3987 struct type *atype = ada_check_typedef (actuals[i]->type ());
14f9c5c9 3988
db2534b7 3989 if (!ada_type_match (ftype, atype))
dda83cd7
SM
3990 return 0;
3991 }
de93309a
SM
3992 }
3993 return 1;
3994}
d2e4a39e 3995
de93309a
SM
3996/* False iff function type FUNC_TYPE definitely does not produce a value
3997 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3998 FUNC_TYPE is not a valid function type with a non-null return type
3999 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 4000
de93309a
SM
4001static int
4002return_match (struct type *func_type, struct type *context_type)
4003{
4004 struct type *return_type;
d2e4a39e 4005
de93309a
SM
4006 if (func_type == NULL)
4007 return 1;
14f9c5c9 4008
78134374 4009 if (func_type->code () == TYPE_CODE_FUNC)
27710edb 4010 return_type = get_base_type (func_type->target_type ());
de93309a
SM
4011 else
4012 return_type = get_base_type (func_type);
4013 if (return_type == NULL)
4014 return 1;
76a01679 4015
de93309a 4016 context_type = get_base_type (context_type);
14f9c5c9 4017
78134374 4018 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
4019 return context_type == NULL || return_type == context_type;
4020 else if (context_type == NULL)
78134374 4021 return return_type->code () != TYPE_CODE_VOID;
de93309a 4022 else
78134374 4023 return return_type->code () == context_type->code ();
de93309a 4024}
14f9c5c9 4025
14f9c5c9 4026
1bfa81ac 4027/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
4028 function (if any) that matches the types of the NARGS arguments in
4029 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4030 that returns that type, then eliminate matches that don't. If
4031 CONTEXT_TYPE is void and there is at least one match that does not
4032 return void, eliminate all matches that do.
14f9c5c9 4033
de93309a
SM
4034 Asks the user if there is more than one match remaining. Returns -1
4035 if there is no such symbol or none is selected. NAME is used
4036 solely for messages. May re-arrange and modify SYMS in
4037 the process; the index returned is for the modified vector. */
14f9c5c9 4038
de93309a 4039static int
d1183b06
TT
4040ada_resolve_function (std::vector<struct block_symbol> &syms,
4041 struct value **args, int nargs,
dda83cd7 4042 const char *name, struct type *context_type,
7056f312 4043 bool parse_completion)
de93309a
SM
4044{
4045 int fallback;
4046 int k;
4047 int m; /* Number of hits */
14f9c5c9 4048
de93309a
SM
4049 m = 0;
4050 /* In the first pass of the loop, we only accept functions matching
4051 context_type. If none are found, we add a second pass of the loop
4052 where every function is accepted. */
4053 for (fallback = 0; m == 0 && fallback < 2; fallback++)
4054 {
d1183b06 4055 for (k = 0; k < syms.size (); k += 1)
dda83cd7 4056 {
5f9c5a63 4057 struct type *type = ada_check_typedef (syms[k].symbol->type ());
5b4ee69b 4058
dda83cd7
SM
4059 if (ada_args_match (syms[k].symbol, args, nargs)
4060 && (fallback || return_match (type, context_type)))
4061 {
4062 syms[m] = syms[k];
4063 m += 1;
4064 }
4065 }
14f9c5c9
AS
4066 }
4067
de93309a
SM
4068 /* If we got multiple matches, ask the user which one to use. Don't do this
4069 interactive thing during completion, though, as the purpose of the
4070 completion is providing a list of all possible matches. Prompting the
4071 user to filter it down would be completely unexpected in this case. */
4072 if (m == 0)
4073 return -1;
4074 else if (m > 1 && !parse_completion)
4075 {
6cb06a8c 4076 gdb_printf (_("Multiple matches for %s\n"), name);
d1183b06 4077 user_select_syms (syms.data (), m, 1);
de93309a
SM
4078 return 0;
4079 }
4080 return 0;
14f9c5c9
AS
4081}
4082
14f9c5c9
AS
4083/* Type-class predicates */
4084
4c4b4cd2
PH
4085/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4086 or FLOAT). */
14f9c5c9
AS
4087
4088static int
d2e4a39e 4089numeric_type_p (struct type *type)
14f9c5c9
AS
4090{
4091 if (type == NULL)
4092 return 0;
d2e4a39e
AS
4093 else
4094 {
78134374 4095 switch (type->code ())
dda83cd7
SM
4096 {
4097 case TYPE_CODE_INT:
4098 case TYPE_CODE_FLT:
c04da66c 4099 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4100 return 1;
4101 case TYPE_CODE_RANGE:
27710edb
SM
4102 return (type == type->target_type ()
4103 || numeric_type_p (type->target_type ()));
dda83cd7
SM
4104 default:
4105 return 0;
4106 }
d2e4a39e 4107 }
14f9c5c9
AS
4108}
4109
4c4b4cd2 4110/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4111
4112static int
d2e4a39e 4113integer_type_p (struct type *type)
14f9c5c9
AS
4114{
4115 if (type == NULL)
4116 return 0;
d2e4a39e
AS
4117 else
4118 {
78134374 4119 switch (type->code ())
dda83cd7
SM
4120 {
4121 case TYPE_CODE_INT:
4122 return 1;
4123 case TYPE_CODE_RANGE:
27710edb
SM
4124 return (type == type->target_type ()
4125 || integer_type_p (type->target_type ()));
dda83cd7
SM
4126 default:
4127 return 0;
4128 }
d2e4a39e 4129 }
14f9c5c9
AS
4130}
4131
4c4b4cd2 4132/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4133
4134static int
d2e4a39e 4135scalar_type_p (struct type *type)
14f9c5c9
AS
4136{
4137 if (type == NULL)
4138 return 0;
d2e4a39e
AS
4139 else
4140 {
78134374 4141 switch (type->code ())
dda83cd7
SM
4142 {
4143 case TYPE_CODE_INT:
4144 case TYPE_CODE_RANGE:
4145 case TYPE_CODE_ENUM:
4146 case TYPE_CODE_FLT:
c04da66c 4147 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4148 return 1;
4149 default:
4150 return 0;
4151 }
d2e4a39e 4152 }
14f9c5c9
AS
4153}
4154
98847c1e
TT
4155/* True iff TYPE is discrete, as defined in the Ada Reference Manual.
4156 This essentially means one of (INT, RANGE, ENUM) -- but note that
4157 "enum" includes character and boolean as well. */
14f9c5c9
AS
4158
4159static int
d2e4a39e 4160discrete_type_p (struct type *type)
14f9c5c9
AS
4161{
4162 if (type == NULL)
4163 return 0;
d2e4a39e
AS
4164 else
4165 {
78134374 4166 switch (type->code ())
dda83cd7
SM
4167 {
4168 case TYPE_CODE_INT:
4169 case TYPE_CODE_RANGE:
4170 case TYPE_CODE_ENUM:
4171 case TYPE_CODE_BOOL:
98847c1e 4172 case TYPE_CODE_CHAR:
dda83cd7
SM
4173 return 1;
4174 default:
4175 return 0;
4176 }
d2e4a39e 4177 }
14f9c5c9
AS
4178}
4179
4c4b4cd2
PH
4180/* Returns non-zero if OP with operands in the vector ARGS could be
4181 a user-defined function. Errs on the side of pre-defined operators
4182 (i.e., result 0). */
14f9c5c9
AS
4183
4184static int
d2e4a39e 4185possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4186{
76a01679 4187 struct type *type0 =
d0c97917 4188 (args[0] == NULL) ? NULL : ada_check_typedef (args[0]->type ());
d2e4a39e 4189 struct type *type1 =
d0c97917 4190 (args[1] == NULL) ? NULL : ada_check_typedef (args[1]->type ());
d2e4a39e 4191
4c4b4cd2
PH
4192 if (type0 == NULL)
4193 return 0;
4194
14f9c5c9
AS
4195 switch (op)
4196 {
4197 default:
4198 return 0;
4199
4200 case BINOP_ADD:
4201 case BINOP_SUB:
4202 case BINOP_MUL:
4203 case BINOP_DIV:
d2e4a39e 4204 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4205
4206 case BINOP_REM:
4207 case BINOP_MOD:
4208 case BINOP_BITWISE_AND:
4209 case BINOP_BITWISE_IOR:
4210 case BINOP_BITWISE_XOR:
d2e4a39e 4211 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4212
4213 case BINOP_EQUAL:
4214 case BINOP_NOTEQUAL:
4215 case BINOP_LESS:
4216 case BINOP_GTR:
4217 case BINOP_LEQ:
4218 case BINOP_GEQ:
d2e4a39e 4219 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4220
4221 case BINOP_CONCAT:
ee90b9ab 4222 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4223
4224 case BINOP_EXP:
d2e4a39e 4225 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4226
4227 case UNOP_NEG:
4228 case UNOP_PLUS:
4229 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4230 case UNOP_ABS:
4231 return (!numeric_type_p (type0));
14f9c5c9
AS
4232
4233 }
4234}
4235\f
dda83cd7 4236 /* Renaming */
14f9c5c9 4237
aeb5907d
JB
4238/* NOTES:
4239
4240 1. In the following, we assume that a renaming type's name may
4241 have an ___XD suffix. It would be nice if this went away at some
4242 point.
4243 2. We handle both the (old) purely type-based representation of
4244 renamings and the (new) variable-based encoding. At some point,
4245 it is devoutly to be hoped that the former goes away
4246 (FIXME: hilfinger-2007-07-09).
4247 3. Subprogram renamings are not implemented, although the XRS
4248 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4249
4250/* If SYM encodes a renaming,
4251
4252 <renaming> renames <renamed entity>,
4253
4254 sets *LEN to the length of the renamed entity's name,
4255 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4256 the string describing the subcomponent selected from the renamed
0963b4bd 4257 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4258 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4259 are undefined). Otherwise, returns a value indicating the category
4260 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4261 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4262 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4263 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4264 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4265 may be NULL, in which case they are not assigned.
4266
4267 [Currently, however, GCC does not generate subprogram renamings.] */
4268
4269enum ada_renaming_category
4270ada_parse_renaming (struct symbol *sym,
4271 const char **renamed_entity, int *len,
4272 const char **renaming_expr)
4273{
4274 enum ada_renaming_category kind;
4275 const char *info;
4276 const char *suffix;
4277
4278 if (sym == NULL)
4279 return ADA_NOT_RENAMING;
66d7f48f 4280 switch (sym->aclass ())
14f9c5c9 4281 {
aeb5907d
JB
4282 default:
4283 return ADA_NOT_RENAMING;
aeb5907d
JB
4284 case LOC_LOCAL:
4285 case LOC_STATIC:
4286 case LOC_COMPUTED:
4287 case LOC_OPTIMIZED_OUT:
987012b8 4288 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4289 if (info == NULL)
4290 return ADA_NOT_RENAMING;
4291 switch (info[5])
4292 {
4293 case '_':
4294 kind = ADA_OBJECT_RENAMING;
4295 info += 6;
4296 break;
4297 case 'E':
4298 kind = ADA_EXCEPTION_RENAMING;
4299 info += 7;
4300 break;
4301 case 'P':
4302 kind = ADA_PACKAGE_RENAMING;
4303 info += 7;
4304 break;
4305 case 'S':
4306 kind = ADA_SUBPROGRAM_RENAMING;
4307 info += 7;
4308 break;
4309 default:
4310 return ADA_NOT_RENAMING;
4311 }
14f9c5c9 4312 }
4c4b4cd2 4313
de93309a
SM
4314 if (renamed_entity != NULL)
4315 *renamed_entity = info;
4316 suffix = strstr (info, "___XE");
4317 if (suffix == NULL || suffix == info)
4318 return ADA_NOT_RENAMING;
4319 if (len != NULL)
4320 *len = strlen (info) - strlen (suffix);
4321 suffix += 5;
4322 if (renaming_expr != NULL)
4323 *renaming_expr = suffix;
4324 return kind;
4325}
4326
4327/* Compute the value of the given RENAMING_SYM, which is expected to
4328 be a symbol encoding a renaming expression. BLOCK is the block
4329 used to evaluate the renaming. */
4330
4331static struct value *
4332ada_read_renaming_var_value (struct symbol *renaming_sym,
4333 const struct block *block)
4334{
4335 const char *sym_name;
4336
987012b8 4337 sym_name = renaming_sym->linkage_name ();
de93309a
SM
4338 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
4339 return evaluate_expression (expr.get ());
4340}
4341\f
4342
dda83cd7 4343 /* Evaluation: Function Calls */
de93309a
SM
4344
4345/* Return an lvalue containing the value VAL. This is the identity on
4346 lvalues, and otherwise has the side-effect of allocating memory
4347 in the inferior where a copy of the value contents is copied. */
4348
4349static struct value *
4350ensure_lval (struct value *val)
4351{
736355f2
TT
4352 if (val->lval () == not_lval
4353 || val->lval () == lval_internalvar)
de93309a 4354 {
d0c97917 4355 int len = ada_check_typedef (val->type ())->length ();
de93309a 4356 const CORE_ADDR addr =
dda83cd7 4357 value_as_long (value_allocate_space_in_inferior (len));
de93309a 4358
6f9c9d71 4359 val->set_lval (lval_memory);
9feb2d07 4360 val->set_address (addr);
efaf1ae0 4361 write_memory (addr, val->contents ().data (), len);
de93309a
SM
4362 }
4363
4364 return val;
4365}
4366
4367/* Given ARG, a value of type (pointer or reference to a)*
4368 structure/union, extract the component named NAME from the ultimate
4369 target structure/union and return it as a value with its
4370 appropriate type.
4371
4372 The routine searches for NAME among all members of the structure itself
4373 and (recursively) among all members of any wrapper members
4374 (e.g., '_parent').
4375
4376 If NO_ERR, then simply return NULL in case of error, rather than
4377 calling error. */
4378
4379static struct value *
4380ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4381{
4382 struct type *t, *t1;
4383 struct value *v;
4384 int check_tag;
4385
4386 v = NULL;
d0c97917 4387 t1 = t = ada_check_typedef (arg->type ());
78134374 4388 if (t->code () == TYPE_CODE_REF)
de93309a 4389 {
27710edb 4390 t1 = t->target_type ();
de93309a
SM
4391 if (t1 == NULL)
4392 goto BadValue;
4393 t1 = ada_check_typedef (t1);
78134374 4394 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4395 {
4396 arg = coerce_ref (arg);
4397 t = t1;
4398 }
de93309a
SM
4399 }
4400
78134374 4401 while (t->code () == TYPE_CODE_PTR)
de93309a 4402 {
27710edb 4403 t1 = t->target_type ();
de93309a
SM
4404 if (t1 == NULL)
4405 goto BadValue;
4406 t1 = ada_check_typedef (t1);
78134374 4407 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4408 {
4409 arg = value_ind (arg);
4410 t = t1;
4411 }
de93309a 4412 else
dda83cd7 4413 break;
de93309a 4414 }
aeb5907d 4415
78134374 4416 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4417 goto BadValue;
52ce6436 4418
de93309a
SM
4419 if (t1 == t)
4420 v = ada_search_struct_field (name, arg, 0, t);
4421 else
4422 {
4423 int bit_offset, bit_size, byte_offset;
4424 struct type *field_type;
4425 CORE_ADDR address;
a5ee536b 4426
78134374 4427 if (t->code () == TYPE_CODE_PTR)
9feb2d07 4428 address = ada_value_ind (arg)->address ();
de93309a 4429 else
9feb2d07 4430 address = ada_coerce_ref (arg)->address ();
d2e4a39e 4431
de93309a 4432 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4433 the case where the type is a reference to a tagged type, but
4434 we have to be careful to exclude pointers to tagged types.
4435 The latter should be shown as usual (as a pointer), whereas
4436 a reference should mostly be transparent to the user. */
14f9c5c9 4437
de93309a 4438 if (ada_is_tagged_type (t1, 0)
dda83cd7 4439 || (t1->code () == TYPE_CODE_REF
27710edb 4440 && ada_is_tagged_type (t1->target_type (), 0)))
dda83cd7
SM
4441 {
4442 /* We first try to find the searched field in the current type.
de93309a 4443 If not found then let's look in the fixed type. */
14f9c5c9 4444
dda83cd7 4445 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4446 nullptr, nullptr, nullptr,
4447 nullptr, nullptr))
de93309a
SM
4448 check_tag = 1;
4449 else
4450 check_tag = 0;
dda83cd7 4451 }
de93309a
SM
4452 else
4453 check_tag = 0;
c3e5cd34 4454
de93309a
SM
4455 /* Convert to fixed type in all cases, so that we have proper
4456 offsets to each field in unconstrained record types. */
4457 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4458 address, NULL, check_tag);
4459
24aa1b02
TT
4460 /* Resolve the dynamic type as well. */
4461 arg = value_from_contents_and_address (t1, nullptr, address);
d0c97917 4462 t1 = arg->type ();
24aa1b02 4463
de93309a 4464 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4465 &field_type, &byte_offset, &bit_offset,
4466 &bit_size, NULL))
4467 {
4468 if (bit_size != 0)
4469 {
4470 if (t->code () == TYPE_CODE_REF)
4471 arg = ada_coerce_ref (arg);
4472 else
4473 arg = ada_value_ind (arg);
4474 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4475 bit_offset, bit_size,
4476 field_type);
4477 }
4478 else
4479 v = value_at_lazy (field_type, address + byte_offset);
4480 }
c3e5cd34 4481 }
14f9c5c9 4482
de93309a
SM
4483 if (v != NULL || no_err)
4484 return v;
4485 else
4486 error (_("There is no member named %s."), name);
4487
4488 BadValue:
4489 if (no_err)
4490 return NULL;
4491 else
4492 error (_("Attempt to extract a component of "
4493 "a value that is not a record."));
14f9c5c9
AS
4494}
4495
4496/* Return the value ACTUAL, converted to be an appropriate value for a
4497 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4498 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4499 values not residing in memory, updating it as needed. */
14f9c5c9 4500
a93c0eb6 4501struct value *
40bc484c 4502ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4503{
d0c97917 4504 struct type *actual_type = ada_check_typedef (actual->type ());
61ee279c 4505 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4506 struct type *formal_target =
78134374 4507 formal_type->code () == TYPE_CODE_PTR
27710edb 4508 ? ada_check_typedef (formal_type->target_type ()) : formal_type;
d2e4a39e 4509 struct type *actual_target =
78134374 4510 actual_type->code () == TYPE_CODE_PTR
27710edb 4511 ? ada_check_typedef (actual_type->target_type ()) : actual_type;
14f9c5c9 4512
4c4b4cd2 4513 if (ada_is_array_descriptor_type (formal_target)
78134374 4514 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4515 return make_array_descriptor (formal_type, actual);
78134374
SM
4516 else if (formal_type->code () == TYPE_CODE_PTR
4517 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4518 {
a84a8a0d 4519 struct value *result;
5b4ee69b 4520
78134374 4521 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4522 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4523 result = desc_data (actual);
78134374 4524 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7 4525 {
736355f2 4526 if (actual->lval () != lval_memory)
dda83cd7
SM
4527 {
4528 struct value *val;
4529
d0c97917 4530 actual_type = ada_check_typedef (actual->type ());
317c3ed9 4531 val = value::allocate (actual_type);
efaf1ae0 4532 copy (actual->contents (), val->contents_raw ());
dda83cd7
SM
4533 actual = ensure_lval (val);
4534 }
4535 result = value_addr (actual);
4536 }
a84a8a0d
JB
4537 else
4538 return actual;
b1af9e97 4539 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4540 }
78134374 4541 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4542 return ada_value_ind (actual);
8344af1e
JB
4543 else if (ada_is_aligner_type (formal_type))
4544 {
4545 /* We need to turn this parameter into an aligner type
4546 as well. */
317c3ed9 4547 struct value *aligner = value::allocate (formal_type);
8344af1e
JB
4548 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4549
4550 value_assign_to_component (aligner, component, actual);
4551 return aligner;
4552 }
14f9c5c9
AS
4553
4554 return actual;
4555}
4556
438c98a1
JB
4557/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4558 type TYPE. This is usually an inefficient no-op except on some targets
4559 (such as AVR) where the representation of a pointer and an address
4560 differs. */
4561
4562static CORE_ADDR
4563value_pointer (struct value *value, struct type *type)
4564{
df86565b 4565 unsigned len = type->length ();
224c3ddb 4566 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4567 CORE_ADDR addr;
4568
9feb2d07 4569 addr = value->address ();
8ee511af 4570 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4571 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4572 return addr;
4573}
4574
14f9c5c9 4575
4c4b4cd2
PH
4576/* Push a descriptor of type TYPE for array value ARR on the stack at
4577 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4578 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4579 to-descriptor type rather than a descriptor type), a struct value *
4580 representing a pointer to this descriptor. */
14f9c5c9 4581
d2e4a39e 4582static struct value *
40bc484c 4583make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4584{
d2e4a39e
AS
4585 struct type *bounds_type = desc_bounds_type (type);
4586 struct type *desc_type = desc_base_type (type);
317c3ed9
TT
4587 struct value *descriptor = value::allocate (desc_type);
4588 struct value *bounds = value::allocate (bounds_type);
14f9c5c9 4589 int i;
d2e4a39e 4590
d0c97917 4591 for (i = ada_array_arity (ada_check_typedef (arr->type ()));
0963b4bd 4592 i > 0; i -= 1)
14f9c5c9 4593 {
d0c97917 4594 modify_field (bounds->type (),
bbe912ba 4595 bounds->contents_writeable ().data (),
19f220c3
JK
4596 ada_array_bound (arr, i, 0),
4597 desc_bound_bitpos (bounds_type, i, 0),
4598 desc_bound_bitsize (bounds_type, i, 0));
d0c97917 4599 modify_field (bounds->type (),
bbe912ba 4600 bounds->contents_writeable ().data (),
19f220c3
JK
4601 ada_array_bound (arr, i, 1),
4602 desc_bound_bitpos (bounds_type, i, 1),
4603 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4604 }
d2e4a39e 4605
40bc484c 4606 bounds = ensure_lval (bounds);
d2e4a39e 4607
d0c97917 4608 modify_field (descriptor->type (),
bbe912ba 4609 descriptor->contents_writeable ().data (),
19f220c3 4610 value_pointer (ensure_lval (arr),
940da03e 4611 desc_type->field (0).type ()),
19f220c3
JK
4612 fat_pntr_data_bitpos (desc_type),
4613 fat_pntr_data_bitsize (desc_type));
4614
d0c97917 4615 modify_field (descriptor->type (),
bbe912ba 4616 descriptor->contents_writeable ().data (),
19f220c3 4617 value_pointer (bounds,
940da03e 4618 desc_type->field (1).type ()),
19f220c3
JK
4619 fat_pntr_bounds_bitpos (desc_type),
4620 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4621
40bc484c 4622 descriptor = ensure_lval (descriptor);
14f9c5c9 4623
78134374 4624 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4625 return value_addr (descriptor);
4626 else
4627 return descriptor;
4628}
14f9c5c9 4629\f
dda83cd7 4630 /* Symbol Cache Module */
3d9434b5 4631
3d9434b5 4632/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4633 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4634 on the type of entity being printed, the cache can make it as much
4635 as an order of magnitude faster than without it.
4636
4637 The descriptive type DWARF extension has significantly reduced
4638 the need for this cache, at least when DWARF is being used. However,
4639 even in this case, some expensive name-based symbol searches are still
4640 sometimes necessary - to find an XVZ variable, mostly. */
4641
ee01b665
JB
4642/* Return the symbol cache associated to the given program space PSPACE.
4643 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4644
ee01b665
JB
4645static struct ada_symbol_cache *
4646ada_get_symbol_cache (struct program_space *pspace)
4647{
4648 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4649
bdcccc56
TT
4650 if (pspace_data->sym_cache == nullptr)
4651 pspace_data->sym_cache.reset (new ada_symbol_cache);
ee01b665 4652
bdcccc56 4653 return pspace_data->sym_cache.get ();
ee01b665 4654}
3d9434b5
JB
4655
4656/* Clear all entries from the symbol cache. */
4657
4658static void
bdcccc56 4659ada_clear_symbol_cache ()
3d9434b5 4660{
bdcccc56
TT
4661 struct ada_pspace_data *pspace_data
4662 = get_ada_pspace_data (current_program_space);
ee01b665 4663
bdcccc56
TT
4664 if (pspace_data->sym_cache != nullptr)
4665 pspace_data->sym_cache.reset ();
3d9434b5
JB
4666}
4667
fe978cb0 4668/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4669 Return it if found, or NULL otherwise. */
4670
4671static struct cache_entry **
fe978cb0 4672find_entry (const char *name, domain_enum domain)
3d9434b5 4673{
ee01b665
JB
4674 struct ada_symbol_cache *sym_cache
4675 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4676 int h = msymbol_hash (name) % HASH_SIZE;
4677 struct cache_entry **e;
4678
ee01b665 4679 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4680 {
fe978cb0 4681 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
dda83cd7 4682 return e;
3d9434b5
JB
4683 }
4684 return NULL;
4685}
4686
fe978cb0 4687/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4688 Return 1 if found, 0 otherwise.
4689
4690 If an entry was found and SYM is not NULL, set *SYM to the entry's
4691 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4692
96d887e8 4693static int
fe978cb0 4694lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4695 struct symbol **sym, const struct block **block)
96d887e8 4696{
fe978cb0 4697 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4698
4699 if (e == NULL)
4700 return 0;
4701 if (sym != NULL)
4702 *sym = (*e)->sym;
4703 if (block != NULL)
4704 *block = (*e)->block;
4705 return 1;
96d887e8
PH
4706}
4707
3d9434b5 4708/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4709 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4710
96d887e8 4711static void
fe978cb0 4712cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4713 const struct block *block)
96d887e8 4714{
ee01b665
JB
4715 struct ada_symbol_cache *sym_cache
4716 = ada_get_symbol_cache (current_program_space);
3d9434b5 4717 int h;
3d9434b5
JB
4718 struct cache_entry *e;
4719
1994afbf
DE
4720 /* Symbols for builtin types don't have a block.
4721 For now don't cache such symbols. */
7b3ecc75 4722 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4723 return;
4724
3d9434b5
JB
4725 /* If the symbol is a local symbol, then do not cache it, as a search
4726 for that symbol depends on the context. To determine whether
4727 the symbol is local or not, we check the block where we found it
4728 against the global and static blocks of its associated symtab. */
63d609de
SM
4729 if (sym != nullptr)
4730 {
4731 const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
4732
4733 if (bv.global_block () != block && bv.static_block () != block)
4734 return;
4735 }
3d9434b5
JB
4736
4737 h = msymbol_hash (name) % HASH_SIZE;
e39db4db 4738 e = XOBNEW (&sym_cache->cache_space, cache_entry);
ee01b665
JB
4739 e->next = sym_cache->root[h];
4740 sym_cache->root[h] = e;
2ef5453b 4741 e->name = obstack_strdup (&sym_cache->cache_space, name);
3d9434b5 4742 e->sym = sym;
fe978cb0 4743 e->domain = domain;
3d9434b5 4744 e->block = block;
96d887e8 4745}
4c4b4cd2 4746\f
dda83cd7 4747 /* Symbol Lookup */
4c4b4cd2 4748
b5ec771e
PA
4749/* Return the symbol name match type that should be used used when
4750 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4751
4752 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4753 for Ada lookups. */
c0431670 4754
b5ec771e
PA
4755static symbol_name_match_type
4756name_match_type_from_name (const char *lookup_name)
c0431670 4757{
b5ec771e
PA
4758 return (strstr (lookup_name, "__") == NULL
4759 ? symbol_name_match_type::WILD
4760 : symbol_name_match_type::FULL);
c0431670
JB
4761}
4762
4c4b4cd2
PH
4763/* Return the result of a standard (literal, C-like) lookup of NAME in
4764 given DOMAIN, visible from lexical block BLOCK. */
4765
4766static struct symbol *
4767standard_lookup (const char *name, const struct block *block,
dda83cd7 4768 domain_enum domain)
4c4b4cd2 4769{
acbd605d 4770 /* Initialize it just to avoid a GCC false warning. */
6640a367 4771 struct block_symbol sym = {};
4c4b4cd2 4772
d12307c1
PMR
4773 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4774 return sym.symbol;
a2cd4f14 4775 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4776 cache_symbol (name, domain, sym.symbol, sym.block);
4777 return sym.symbol;
4c4b4cd2
PH
4778}
4779
4780
4781/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4782 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4783 since they contend in overloading in the same way. */
4784static int
d1183b06 4785is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4786{
d1183b06 4787 for (const block_symbol &sym : syms)
5f9c5a63
SM
4788 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4789 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4790 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4791 return 1;
4792
4793 return 0;
4794}
4795
4796/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4797 struct types. Otherwise, they may not. */
14f9c5c9
AS
4798
4799static int
d2e4a39e 4800equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4801{
d2e4a39e 4802 if (type0 == type1)
14f9c5c9 4803 return 1;
d2e4a39e 4804 if (type0 == NULL || type1 == NULL
78134374 4805 || type0->code () != type1->code ())
14f9c5c9 4806 return 0;
78134374
SM
4807 if ((type0->code () == TYPE_CODE_STRUCT
4808 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4809 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4810 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4811 return 1;
d2e4a39e 4812
14f9c5c9
AS
4813 return 0;
4814}
4815
4816/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4817 no more defined than that of SYM1. */
14f9c5c9
AS
4818
4819static int
d2e4a39e 4820lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4821{
4822 if (sym0 == sym1)
4823 return 1;
6c9c307c 4824 if (sym0->domain () != sym1->domain ()
66d7f48f 4825 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4826 return 0;
4827
66d7f48f 4828 switch (sym0->aclass ())
14f9c5c9
AS
4829 {
4830 case LOC_UNDEF:
4831 return 1;
4832 case LOC_TYPEDEF:
4833 {
5f9c5a63
SM
4834 struct type *type0 = sym0->type ();
4835 struct type *type1 = sym1->type ();
dda83cd7
SM
4836 const char *name0 = sym0->linkage_name ();
4837 const char *name1 = sym1->linkage_name ();
4838 int len0 = strlen (name0);
4839
4840 return
4841 type0->code () == type1->code ()
4842 && (equiv_types (type0, type1)
4843 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4844 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4845 }
4846 case LOC_CONST:
4aeddc50 4847 return sym0->value_longest () == sym1->value_longest ()
5f9c5a63 4848 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4849
4850 case LOC_STATIC:
4851 {
dda83cd7
SM
4852 const char *name0 = sym0->linkage_name ();
4853 const char *name1 = sym1->linkage_name ();
4854 return (strcmp (name0, name1) == 0
4aeddc50 4855 && sym0->value_address () == sym1->value_address ());
4b610737
TT
4856 }
4857
d2e4a39e
AS
4858 default:
4859 return 0;
14f9c5c9
AS
4860 }
4861}
4862
d1183b06
TT
4863/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4864 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4865
4866static void
d1183b06 4867add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4868 struct symbol *sym,
4869 const struct block *block)
14f9c5c9 4870{
529cad9c
PH
4871 /* Do not try to complete stub types, as the debugger is probably
4872 already scanning all symbols matching a certain name at the
4873 time when this function is called. Trying to replace the stub
4874 type by its associated full type will cause us to restart a scan
4875 which may lead to an infinite recursion. Instead, the client
4876 collecting the matching symbols will end up collecting several
4877 matches, with at least one of them complete. It can then filter
4878 out the stub ones if needed. */
4879
d1183b06 4880 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4881 {
d1183b06 4882 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4883 return;
d1183b06 4884 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4885 {
d1183b06
TT
4886 result[i].symbol = sym;
4887 result[i].block = block;
dda83cd7
SM
4888 return;
4889 }
4c4b4cd2
PH
4890 }
4891
d1183b06
TT
4892 struct block_symbol info;
4893 info.symbol = sym;
4894 info.block = block;
4895 result.push_back (info);
4c4b4cd2
PH
4896}
4897
7c7b6655
TT
4898/* Return a bound minimal symbol matching NAME according to Ada
4899 decoding rules. Returns an invalid symbol if there is no such
4900 minimal symbol. Names prefixed with "standard__" are handled
4901 specially: "standard__" is first stripped off, and only static and
4902 global symbols are searched. */
4c4b4cd2 4903
7c7b6655 4904struct bound_minimal_symbol
06a670e2 4905ada_lookup_simple_minsym (const char *name, struct objfile *objfile)
4c4b4cd2 4906{
7c7b6655 4907 struct bound_minimal_symbol result;
4c4b4cd2 4908
b5ec771e
PA
4909 symbol_name_match_type match_type = name_match_type_from_name (name);
4910 lookup_name_info lookup_name (name, match_type);
4911
4912 symbol_name_matcher_ftype *match_name
4913 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4914
06a670e2
MM
4915 gdbarch_iterate_over_objfiles_in_search_order
4916 (objfile != NULL ? objfile->arch () : target_gdbarch (),
4917 [&result, lookup_name, match_name] (struct objfile *obj)
4918 {
4919 for (minimal_symbol *msymbol : obj->msymbols ())
4920 {
4921 if (match_name (msymbol->linkage_name (), lookup_name, nullptr)
4922 && msymbol->type () != mst_solib_trampoline)
4923 {
4924 result.minsym = msymbol;
4925 result.objfile = obj;
4926 return 1;
4927 }
4928 }
4929
4930 return 0;
4931 }, objfile);
4c4b4cd2 4932
7c7b6655 4933 return result;
96d887e8 4934}
4c4b4cd2 4935
96d887e8
PH
4936/* True if TYPE is definitely an artificial type supplied to a symbol
4937 for which no debugging information was given in the symbol file. */
14f9c5c9 4938
96d887e8
PH
4939static int
4940is_nondebugging_type (struct type *type)
4941{
0d5cff50 4942 const char *name = ada_type_name (type);
5b4ee69b 4943
96d887e8
PH
4944 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4945}
4c4b4cd2 4946
8f17729f
JB
4947/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4948 that are deemed "identical" for practical purposes.
4949
4950 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4951 types and that their number of enumerals is identical (in other
1f704f76 4952 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4953
4954static int
4955ada_identical_enum_types_p (struct type *type1, struct type *type2)
4956{
4957 int i;
4958
4959 /* The heuristic we use here is fairly conservative. We consider
4960 that 2 enumerate types are identical if they have the same
4961 number of enumerals and that all enumerals have the same
4962 underlying value and name. */
4963
4964 /* All enums in the type should have an identical underlying value. */
1f704f76 4965 for (i = 0; i < type1->num_fields (); i++)
970db518 4966 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4967 return 0;
4968
4969 /* All enumerals should also have the same name (modulo any numerical
4970 suffix). */
1f704f76 4971 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4972 {
33d16dd9
SM
4973 const char *name_1 = type1->field (i).name ();
4974 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4975 int len_1 = strlen (name_1);
4976 int len_2 = strlen (name_2);
4977
33d16dd9
SM
4978 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4979 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4980 if (len_1 != len_2
33d16dd9
SM
4981 || strncmp (type1->field (i).name (),
4982 type2->field (i).name (),
8f17729f
JB
4983 len_1) != 0)
4984 return 0;
4985 }
4986
4987 return 1;
4988}
4989
4990/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4991 that are deemed "identical" for practical purposes. Sometimes,
4992 enumerals are not strictly identical, but their types are so similar
4993 that they can be considered identical.
4994
4995 For instance, consider the following code:
4996
4997 type Color is (Black, Red, Green, Blue, White);
4998 type RGB_Color is new Color range Red .. Blue;
4999
5000 Type RGB_Color is a subrange of an implicit type which is a copy
5001 of type Color. If we call that implicit type RGB_ColorB ("B" is
5002 for "Base Type"), then type RGB_ColorB is a copy of type Color.
5003 As a result, when an expression references any of the enumeral
5004 by name (Eg. "print green"), the expression is technically
5005 ambiguous and the user should be asked to disambiguate. But
5006 doing so would only hinder the user, since it wouldn't matter
5007 what choice he makes, the outcome would always be the same.
5008 So, for practical purposes, we consider them as the same. */
5009
5010static int
54d343a2 5011symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
5012{
5013 int i;
5014
5015 /* Before performing a thorough comparison check of each type,
5016 we perform a series of inexpensive checks. We expect that these
5017 checks will quickly fail in the vast majority of cases, and thus
5018 help prevent the unnecessary use of a more expensive comparison.
5019 Said comparison also expects us to make some of these checks
5020 (see ada_identical_enum_types_p). */
5021
5022 /* Quick check: All symbols should have an enum type. */
54d343a2 5023 for (i = 0; i < syms.size (); i++)
5f9c5a63 5024 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
5025 return 0;
5026
5027 /* Quick check: They should all have the same value. */
54d343a2 5028 for (i = 1; i < syms.size (); i++)
4aeddc50 5029 if (syms[i].symbol->value_longest () != syms[0].symbol->value_longest ())
8f17729f
JB
5030 return 0;
5031
5032 /* Quick check: They should all have the same number of enumerals. */
54d343a2 5033 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5034 if (syms[i].symbol->type ()->num_fields ()
5035 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
5036 return 0;
5037
5038 /* All the sanity checks passed, so we might have a set of
5039 identical enumeration types. Perform a more complete
5040 comparison of the type of each symbol. */
54d343a2 5041 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5042 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5043 syms[0].symbol->type ()))
8f17729f
JB
5044 return 0;
5045
5046 return 1;
5047}
5048
54d343a2 5049/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5050 duplicate other symbols in the list (The only case I know of where
5051 this happens is when object files containing stabs-in-ecoff are
5052 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5053 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5054
d1183b06 5055static void
54d343a2 5056remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
5057{
5058 int i, j;
4c4b4cd2 5059
8f17729f
JB
5060 /* We should never be called with less than 2 symbols, as there
5061 cannot be any extra symbol in that case. But it's easy to
5062 handle, since we have nothing to do in that case. */
54d343a2 5063 if (syms->size () < 2)
d1183b06 5064 return;
8f17729f 5065
96d887e8 5066 i = 0;
54d343a2 5067 while (i < syms->size ())
96d887e8 5068 {
a35ddb44 5069 int remove_p = 0;
339c13b6
JB
5070
5071 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5072 the get rid of the stub. */
339c13b6 5073
5f9c5a63 5074 if ((*syms)[i].symbol->type ()->is_stub ()
dda83cd7
SM
5075 && (*syms)[i].symbol->linkage_name () != NULL)
5076 {
5077 for (j = 0; j < syms->size (); j++)
5078 {
5079 if (j != i
5f9c5a63 5080 && !(*syms)[j].symbol->type ()->is_stub ()
dda83cd7
SM
5081 && (*syms)[j].symbol->linkage_name () != NULL
5082 && strcmp ((*syms)[i].symbol->linkage_name (),
5083 (*syms)[j].symbol->linkage_name ()) == 0)
5084 remove_p = 1;
5085 }
5086 }
339c13b6
JB
5087
5088 /* Two symbols with the same name, same class and same address
dda83cd7 5089 should be identical. */
339c13b6 5090
987012b8 5091 else if ((*syms)[i].symbol->linkage_name () != NULL
66d7f48f 5092 && (*syms)[i].symbol->aclass () == LOC_STATIC
5f9c5a63 5093 && is_nondebugging_type ((*syms)[i].symbol->type ()))
dda83cd7
SM
5094 {
5095 for (j = 0; j < syms->size (); j += 1)
5096 {
5097 if (i != j
5098 && (*syms)[j].symbol->linkage_name () != NULL
5099 && strcmp ((*syms)[i].symbol->linkage_name (),
5100 (*syms)[j].symbol->linkage_name ()) == 0
66d7f48f
SM
5101 && ((*syms)[i].symbol->aclass ()
5102 == (*syms)[j].symbol->aclass ())
4aeddc50
SM
5103 && (*syms)[i].symbol->value_address ()
5104 == (*syms)[j].symbol->value_address ())
dda83cd7
SM
5105 remove_p = 1;
5106 }
5107 }
339c13b6 5108
a35ddb44 5109 if (remove_p)
54d343a2 5110 syms->erase (syms->begin () + i);
1b788fb6
TT
5111 else
5112 i += 1;
14f9c5c9 5113 }
8f17729f
JB
5114
5115 /* If all the remaining symbols are identical enumerals, then
5116 just keep the first one and discard the rest.
5117
5118 Unlike what we did previously, we do not discard any entry
5119 unless they are ALL identical. This is because the symbol
5120 comparison is not a strict comparison, but rather a practical
5121 comparison. If all symbols are considered identical, then
5122 we can just go ahead and use the first one and discard the rest.
5123 But if we cannot reduce the list to a single element, we have
5124 to ask the user to disambiguate anyways. And if we have to
5125 present a multiple-choice menu, it's less confusing if the list
5126 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
5127 if (symbols_are_identical_enums (*syms))
5128 syms->resize (1);
14f9c5c9
AS
5129}
5130
96d887e8
PH
5131/* Given a type that corresponds to a renaming entity, use the type name
5132 to extract the scope (package name or function name, fully qualified,
5133 and following the GNAT encoding convention) where this renaming has been
49d83361 5134 defined. */
4c4b4cd2 5135
49d83361 5136static std::string
96d887e8 5137xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5138{
96d887e8 5139 /* The renaming types adhere to the following convention:
0963b4bd 5140 <scope>__<rename>___<XR extension>.
96d887e8
PH
5141 So, to extract the scope, we search for the "___XR" extension,
5142 and then backtrack until we find the first "__". */
76a01679 5143
7d93a1e0 5144 const char *name = renaming_type->name ();
108d56a4
SM
5145 const char *suffix = strstr (name, "___XR");
5146 const char *last;
14f9c5c9 5147
96d887e8
PH
5148 /* Now, backtrack a bit until we find the first "__". Start looking
5149 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5150
96d887e8
PH
5151 for (last = suffix - 3; last > name; last--)
5152 if (last[0] == '_' && last[1] == '_')
5153 break;
76a01679 5154
96d887e8 5155 /* Make a copy of scope and return it. */
49d83361 5156 return std::string (name, last);
4c4b4cd2
PH
5157}
5158
96d887e8 5159/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5160
96d887e8
PH
5161static int
5162is_package_name (const char *name)
4c4b4cd2 5163{
96d887e8
PH
5164 /* Here, We take advantage of the fact that no symbols are generated
5165 for packages, while symbols are generated for each function.
5166 So the condition for NAME represent a package becomes equivalent
5167 to NAME not existing in our list of symbols. There is only one
5168 small complication with library-level functions (see below). */
4c4b4cd2 5169
96d887e8
PH
5170 /* If it is a function that has not been defined at library level,
5171 then we should be able to look it up in the symbols. */
5172 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5173 return 0;
14f9c5c9 5174
96d887e8
PH
5175 /* Library-level function names start with "_ada_". See if function
5176 "_ada_" followed by NAME can be found. */
14f9c5c9 5177
96d887e8 5178 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5179 functions names cannot contain "__" in them. */
96d887e8
PH
5180 if (strstr (name, "__") != NULL)
5181 return 0;
4c4b4cd2 5182
528e1572 5183 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5184
528e1572 5185 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5186}
14f9c5c9 5187
96d887e8 5188/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5189 not visible from FUNCTION_NAME. */
14f9c5c9 5190
96d887e8 5191static int
0d5cff50 5192old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5193{
66d7f48f 5194 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5195 return 0;
5196
5f9c5a63 5197 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5198
96d887e8 5199 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5200 if (is_package_name (scope.c_str ()))
5201 return 0;
14f9c5c9 5202
96d887e8
PH
5203 /* Check that the rename is in the current function scope by checking
5204 that its name starts with SCOPE. */
76a01679 5205
96d887e8
PH
5206 /* If the function name starts with "_ada_", it means that it is
5207 a library-level function. Strip this prefix before doing the
5208 comparison, as the encoding for the renaming does not contain
5209 this prefix. */
61012eef 5210 if (startswith (function_name, "_ada_"))
96d887e8 5211 function_name += 5;
f26caa11 5212
49d83361 5213 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5214}
5215
aeb5907d
JB
5216/* Remove entries from SYMS that corresponds to a renaming entity that
5217 is not visible from the function associated with CURRENT_BLOCK or
5218 that is superfluous due to the presence of more specific renaming
5219 information. Places surviving symbols in the initial entries of
d1183b06
TT
5220 SYMS.
5221
96d887e8 5222 Rationale:
aeb5907d
JB
5223 First, in cases where an object renaming is implemented as a
5224 reference variable, GNAT may produce both the actual reference
5225 variable and the renaming encoding. In this case, we discard the
5226 latter.
5227
5228 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5229 entity. Unfortunately, STABS currently does not support the definition
5230 of types that are local to a given lexical block, so all renamings types
5231 are emitted at library level. As a consequence, if an application
5232 contains two renaming entities using the same name, and a user tries to
5233 print the value of one of these entities, the result of the ada symbol
5234 lookup will also contain the wrong renaming type.
f26caa11 5235
96d887e8
PH
5236 This function partially covers for this limitation by attempting to
5237 remove from the SYMS list renaming symbols that should be visible
5238 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5239 method with the current information available. The implementation
5240 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5241
5242 - When the user tries to print a rename in a function while there
dda83cd7
SM
5243 is another rename entity defined in a package: Normally, the
5244 rename in the function has precedence over the rename in the
5245 package, so the latter should be removed from the list. This is
5246 currently not the case.
5247
96d887e8 5248 - This function will incorrectly remove valid renames if
dda83cd7
SM
5249 the CURRENT_BLOCK corresponds to a function which symbol name
5250 has been changed by an "Export" pragma. As a consequence,
5251 the user will be unable to print such rename entities. */
4c4b4cd2 5252
d1183b06 5253static void
54d343a2
TT
5254remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5255 const struct block *current_block)
4c4b4cd2
PH
5256{
5257 struct symbol *current_function;
0d5cff50 5258 const char *current_function_name;
4c4b4cd2 5259 int i;
aeb5907d
JB
5260 int is_new_style_renaming;
5261
5262 /* If there is both a renaming foo___XR... encoded as a variable and
5263 a simple variable foo in the same block, discard the latter.
0963b4bd 5264 First, zero out such symbols, then compress. */
aeb5907d 5265 is_new_style_renaming = 0;
54d343a2 5266 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5267 {
54d343a2
TT
5268 struct symbol *sym = (*syms)[i].symbol;
5269 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5270 const char *name;
5271 const char *suffix;
5272
66d7f48f 5273 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5274 continue;
987012b8 5275 name = sym->linkage_name ();
aeb5907d
JB
5276 suffix = strstr (name, "___XR");
5277
5278 if (suffix != NULL)
5279 {
5280 int name_len = suffix - name;
5281 int j;
5b4ee69b 5282
aeb5907d 5283 is_new_style_renaming = 1;
54d343a2
TT
5284 for (j = 0; j < syms->size (); j += 1)
5285 if (i != j && (*syms)[j].symbol != NULL
987012b8 5286 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5287 name_len) == 0
54d343a2
TT
5288 && block == (*syms)[j].block)
5289 (*syms)[j].symbol = NULL;
aeb5907d
JB
5290 }
5291 }
5292 if (is_new_style_renaming)
5293 {
5294 int j, k;
5295
54d343a2
TT
5296 for (j = k = 0; j < syms->size (); j += 1)
5297 if ((*syms)[j].symbol != NULL)
aeb5907d 5298 {
54d343a2 5299 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5300 k += 1;
5301 }
d1183b06
TT
5302 syms->resize (k);
5303 return;
aeb5907d 5304 }
4c4b4cd2
PH
5305
5306 /* Extract the function name associated to CURRENT_BLOCK.
5307 Abort if unable to do so. */
76a01679 5308
4c4b4cd2 5309 if (current_block == NULL)
d1183b06 5310 return;
76a01679 5311
3c9d0506 5312 current_function = current_block->linkage_function ();
4c4b4cd2 5313 if (current_function == NULL)
d1183b06 5314 return;
4c4b4cd2 5315
987012b8 5316 current_function_name = current_function->linkage_name ();
4c4b4cd2 5317 if (current_function_name == NULL)
d1183b06 5318 return;
4c4b4cd2
PH
5319
5320 /* Check each of the symbols, and remove it from the list if it is
5321 a type corresponding to a renaming that is out of the scope of
5322 the current block. */
5323
5324 i = 0;
54d343a2 5325 while (i < syms->size ())
4c4b4cd2 5326 {
54d343a2 5327 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5328 == ADA_OBJECT_RENAMING
5329 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5330 current_function_name))
5331 syms->erase (syms->begin () + i);
4c4b4cd2 5332 else
dda83cd7 5333 i += 1;
4c4b4cd2 5334 }
4c4b4cd2
PH
5335}
5336
d1183b06 5337/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5338 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5339
cd458349 5340 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5341
5342static void
d1183b06 5343ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5344 const lookup_name_info &lookup_name,
5345 const struct block *block, domain_enum domain)
339c13b6 5346{
339c13b6
JB
5347 while (block != NULL)
5348 {
d1183b06 5349 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5350
ba8694b6
TT
5351 /* If we found a non-function match, assume that's the one. We
5352 only check this when finding a function boundary, so that we
5353 can accumulate all results from intervening blocks first. */
6c00f721 5354 if (block->function () != nullptr && is_nonfunction (result))
dda83cd7 5355 return;
339c13b6 5356
f135fe72 5357 block = block->superblock ();
339c13b6 5358 }
339c13b6
JB
5359}
5360
2315bb2d 5361/* An object of this type is used as the callback argument when
40658b94 5362 calling the map_matching_symbols method. */
ccefe4c4 5363
40658b94 5364struct match_data
ccefe4c4 5365{
1bfa81ac
TT
5366 explicit match_data (std::vector<struct block_symbol> *rp)
5367 : resultp (rp)
5368 {
5369 }
5370 DISABLE_COPY_AND_ASSIGN (match_data);
5371
2315bb2d
TT
5372 bool operator() (struct block_symbol *bsym);
5373
1bfa81ac 5374 struct objfile *objfile = nullptr;
d1183b06 5375 std::vector<struct block_symbol> *resultp;
1bfa81ac 5376 struct symbol *arg_sym = nullptr;
1178743e 5377 bool found_sym = false;
ccefe4c4
TT
5378};
5379
2315bb2d
TT
5380/* A callback for add_nonlocal_symbols that adds symbol, found in
5381 BSYM, to a list of symbols. */
ccefe4c4 5382
2315bb2d
TT
5383bool
5384match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5385{
199b4314
TT
5386 const struct block *block = bsym->block;
5387 struct symbol *sym = bsym->symbol;
5388
40658b94
PH
5389 if (sym == NULL)
5390 {
2315bb2d 5391 if (!found_sym && arg_sym != NULL)
dae58e04 5392 add_defn_to_vec (*resultp, arg_sym, block);
2315bb2d
TT
5393 found_sym = false;
5394 arg_sym = NULL;
40658b94
PH
5395 }
5396 else
5397 {
66d7f48f 5398 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5399 return true;
d9743061 5400 else if (sym->is_argument ())
2315bb2d 5401 arg_sym = sym;
40658b94
PH
5402 else
5403 {
2315bb2d 5404 found_sym = true;
dae58e04 5405 add_defn_to_vec (*resultp, sym, block);
40658b94
PH
5406 }
5407 }
199b4314 5408 return true;
40658b94
PH
5409}
5410
b5ec771e
PA
5411/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5412 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5413 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5414
5415static int
d1183b06 5416ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5417 const struct block *block,
b5ec771e
PA
5418 const lookup_name_info &lookup_name,
5419 domain_enum domain)
22cee43f
PMR
5420{
5421 struct using_direct *renaming;
d1183b06 5422 int defns_mark = result.size ();
22cee43f 5423
b5ec771e
PA
5424 symbol_name_matcher_ftype *name_match
5425 = ada_get_symbol_name_matcher (lookup_name);
5426
3c45e9f9 5427 for (renaming = block->get_using ();
22cee43f
PMR
5428 renaming != NULL;
5429 renaming = renaming->next)
5430 {
5431 const char *r_name;
22cee43f
PMR
5432
5433 /* Avoid infinite recursions: skip this renaming if we are actually
5434 already traversing it.
5435
5436 Currently, symbol lookup in Ada don't use the namespace machinery from
5437 C++/Fortran support: skip namespace imports that use them. */
5438 if (renaming->searched
5439 || (renaming->import_src != NULL
5440 && renaming->import_src[0] != '\0')
5441 || (renaming->import_dest != NULL
5442 && renaming->import_dest[0] != '\0'))
5443 continue;
5444 renaming->searched = 1;
5445
5446 /* TODO: here, we perform another name-based symbol lookup, which can
5447 pull its own multiple overloads. In theory, we should be able to do
5448 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5449 not a simple name. But in order to do this, we would need to enhance
5450 the DWARF reader to associate a symbol to this renaming, instead of a
5451 name. So, for now, we do something simpler: re-use the C++/Fortran
5452 namespace machinery. */
5453 r_name = (renaming->alias != NULL
5454 ? renaming->alias
5455 : renaming->declaration);
b5ec771e
PA
5456 if (name_match (r_name, lookup_name, NULL))
5457 {
5458 lookup_name_info decl_lookup_name (renaming->declaration,
5459 lookup_name.match_type ());
d1183b06 5460 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5461 1, NULL);
5462 }
22cee43f
PMR
5463 renaming->searched = 0;
5464 }
d1183b06 5465 return result.size () != defns_mark;
22cee43f
PMR
5466}
5467
db230ce3
JB
5468/* Implements compare_names, but only applying the comparision using
5469 the given CASING. */
5b4ee69b 5470
40658b94 5471static int
db230ce3
JB
5472compare_names_with_case (const char *string1, const char *string2,
5473 enum case_sensitivity casing)
40658b94
PH
5474{
5475 while (*string1 != '\0' && *string2 != '\0')
5476 {
db230ce3
JB
5477 char c1, c2;
5478
40658b94
PH
5479 if (isspace (*string1) || isspace (*string2))
5480 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5481
5482 if (casing == case_sensitive_off)
5483 {
5484 c1 = tolower (*string1);
5485 c2 = tolower (*string2);
5486 }
5487 else
5488 {
5489 c1 = *string1;
5490 c2 = *string2;
5491 }
5492 if (c1 != c2)
40658b94 5493 break;
db230ce3 5494
40658b94
PH
5495 string1 += 1;
5496 string2 += 1;
5497 }
db230ce3 5498
40658b94
PH
5499 switch (*string1)
5500 {
5501 case '(':
5502 return strcmp_iw_ordered (string1, string2);
5503 case '_':
5504 if (*string2 == '\0')
5505 {
052874e8 5506 if (is_name_suffix (string1))
40658b94
PH
5507 return 0;
5508 else
1a1d5513 5509 return 1;
40658b94 5510 }
dbb8534f 5511 /* FALLTHROUGH */
40658b94
PH
5512 default:
5513 if (*string2 == '(')
5514 return strcmp_iw_ordered (string1, string2);
5515 else
db230ce3
JB
5516 {
5517 if (casing == case_sensitive_off)
5518 return tolower (*string1) - tolower (*string2);
5519 else
5520 return *string1 - *string2;
5521 }
40658b94 5522 }
ccefe4c4
TT
5523}
5524
db230ce3
JB
5525/* Compare STRING1 to STRING2, with results as for strcmp.
5526 Compatible with strcmp_iw_ordered in that...
5527
5528 strcmp_iw_ordered (STRING1, STRING2) <= 0
5529
5530 ... implies...
5531
5532 compare_names (STRING1, STRING2) <= 0
5533
5534 (they may differ as to what symbols compare equal). */
5535
5536static int
5537compare_names (const char *string1, const char *string2)
5538{
5539 int result;
5540
5541 /* Similar to what strcmp_iw_ordered does, we need to perform
5542 a case-insensitive comparison first, and only resort to
5543 a second, case-sensitive, comparison if the first one was
5544 not sufficient to differentiate the two strings. */
5545
5546 result = compare_names_with_case (string1, string2, case_sensitive_off);
5547 if (result == 0)
5548 result = compare_names_with_case (string1, string2, case_sensitive_on);
5549
5550 return result;
5551}
5552
b5ec771e
PA
5553/* Convenience function to get at the Ada encoded lookup name for
5554 LOOKUP_NAME, as a C string. */
5555
5556static const char *
5557ada_lookup_name (const lookup_name_info &lookup_name)
5558{
5559 return lookup_name.ada ().lookup_name ().c_str ();
5560}
5561
0b7b2c2a
TT
5562/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5563 for OBJFILE, then walk the objfile's symtabs and update the
5564 results. */
5565
5566static void
5567map_matching_symbols (struct objfile *objfile,
5568 const lookup_name_info &lookup_name,
5569 bool is_wild_match,
5570 domain_enum domain,
5571 int global,
5572 match_data &data)
5573{
5574 data.objfile = objfile;
5575 objfile->expand_matching_symbols (lookup_name, domain, global,
5576 is_wild_match ? nullptr : compare_names);
5577
5578 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5579 for (compunit_symtab *symtab : objfile->compunits ())
5580 {
5581 const struct block *block
63d609de 5582 = symtab->blockvector ()->block (block_kind);
0b7b2c2a
TT
5583 if (!iterate_over_symbols_terminated (block, lookup_name,
5584 domain, data))
5585 break;
5586 }
5587}
5588
1bfa81ac 5589/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5590 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5591 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5592 symbols otherwise. */
339c13b6
JB
5593
5594static void
d1183b06 5595add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5596 const lookup_name_info &lookup_name,
5597 domain_enum domain, int global)
339c13b6 5598{
1bfa81ac 5599 struct match_data data (&result);
339c13b6 5600
b5ec771e
PA
5601 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5602
2030c079 5603 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5604 {
0b7b2c2a
TT
5605 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5606 global, data);
22cee43f 5607
b669c953 5608 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5609 {
5610 const struct block *global_block
63d609de 5611 = cu->blockvector ()->global_block ();
22cee43f 5612
d1183b06 5613 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5614 domain))
1178743e 5615 data.found_sym = true;
22cee43f 5616 }
40658b94
PH
5617 }
5618
d1183b06 5619 if (result.empty () && global && !is_wild_match)
40658b94 5620 {
b5ec771e 5621 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5622 std::string bracket_name = std::string ("<_ada_") + name + '>';
5623 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5624
2030c079 5625 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5626 map_matching_symbols (objfile, name1, false, domain, global, data);
5627 }
339c13b6
JB
5628}
5629
b5ec771e
PA
5630/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5631 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5632 returning the number of matches. Add these to RESULT.
4eeaa230 5633
22cee43f
PMR
5634 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5635 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5636 is the one match returned (no other matches in that or
d9680e73 5637 enclosing blocks is returned). If there are any matches in or
22cee43f 5638 surrounding BLOCK, then these alone are returned.
4eeaa230 5639
b5ec771e
PA
5640 Names prefixed with "standard__" are handled specially:
5641 "standard__" is first stripped off (by the lookup_name
5642 constructor), and only static and global symbols are searched.
14f9c5c9 5643
22cee43f
PMR
5644 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5645 to lookup global symbols. */
5646
5647static void
d1183b06 5648ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5649 const struct block *block,
b5ec771e 5650 const lookup_name_info &lookup_name,
22cee43f
PMR
5651 domain_enum domain,
5652 int full_search,
5653 int *made_global_lookup_p)
14f9c5c9
AS
5654{
5655 struct symbol *sym;
14f9c5c9 5656
22cee43f
PMR
5657 if (made_global_lookup_p)
5658 *made_global_lookup_p = 0;
339c13b6
JB
5659
5660 /* Special case: If the user specifies a symbol name inside package
5661 Standard, do a non-wild matching of the symbol name without
5662 the "standard__" prefix. This was primarily introduced in order
5663 to allow the user to specifically access the standard exceptions
5664 using, for instance, Standard.Constraint_Error when Constraint_Error
5665 is ambiguous (due to the user defining its own Constraint_Error
5666 entity inside its program). */
b5ec771e
PA
5667 if (lookup_name.ada ().standard_p ())
5668 block = NULL;
4c4b4cd2 5669
339c13b6 5670 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5671
4eeaa230
DE
5672 if (block != NULL)
5673 {
5674 if (full_search)
d1183b06 5675 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5676 else
5677 {
5678 /* In the !full_search case we're are being called by
4009ee92 5679 iterate_over_symbols, and we don't want to search
4eeaa230 5680 superblocks. */
d1183b06 5681 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5682 }
d1183b06 5683 if (!result.empty () || !full_search)
22cee43f 5684 return;
4eeaa230 5685 }
d2e4a39e 5686
339c13b6
JB
5687 /* No non-global symbols found. Check our cache to see if we have
5688 already performed this search before. If we have, then return
5689 the same result. */
5690
b5ec771e
PA
5691 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5692 domain, &sym, &block))
4c4b4cd2
PH
5693 {
5694 if (sym != NULL)
d1183b06 5695 add_defn_to_vec (result, sym, block);
22cee43f 5696 return;
4c4b4cd2 5697 }
14f9c5c9 5698
22cee43f
PMR
5699 if (made_global_lookup_p)
5700 *made_global_lookup_p = 1;
b1eedac9 5701
339c13b6
JB
5702 /* Search symbols from all global blocks. */
5703
d1183b06 5704 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5705
4c4b4cd2 5706 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5707 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5708
d1183b06
TT
5709 if (result.empty ())
5710 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5711}
5712
b5ec771e 5713/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5714 is non-zero, enclosing scope and in global scopes.
5715
5716 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5717 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5718
5719 When full_search is non-zero, any non-function/non-enumeral
5720 symbol match within the nest of blocks whose innermost member is BLOCK,
5721 is the one match returned (no other matches in that or
5722 enclosing blocks is returned). If there are any matches in or
5723 surrounding BLOCK, then these alone are returned.
5724
5725 Names prefixed with "standard__" are handled specially: "standard__"
5726 is first stripped off, and only static and global symbols are searched. */
5727
d1183b06 5728static std::vector<struct block_symbol>
b5ec771e
PA
5729ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5730 const struct block *block,
22cee43f 5731 domain_enum domain,
22cee43f
PMR
5732 int full_search)
5733{
22cee43f 5734 int syms_from_global_search;
d1183b06 5735 std::vector<struct block_symbol> results;
22cee43f 5736
d1183b06 5737 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5738 domain, full_search, &syms_from_global_search);
14f9c5c9 5739
d1183b06 5740 remove_extra_symbols (&results);
4c4b4cd2 5741
d1183b06 5742 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5743 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5744
d1183b06 5745 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5746 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5747 results[0].symbol, results[0].block);
ec6a20c2 5748
d1183b06
TT
5749 remove_irrelevant_renamings (&results, block);
5750 return results;
14f9c5c9
AS
5751}
5752
b5ec771e 5753/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5754 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5755
4eeaa230
DE
5756 See ada_lookup_symbol_list_worker for further details. */
5757
d1183b06 5758std::vector<struct block_symbol>
b5ec771e 5759ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5760 domain_enum domain)
4eeaa230 5761{
b5ec771e
PA
5762 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5763 lookup_name_info lookup_name (name, name_match_type);
5764
d1183b06 5765 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5766}
5767
4e5c77fe
JB
5768/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5769 to 1, but choosing the first symbol found if there are multiple
5770 choices.
5771
5e2336be
JB
5772 The result is stored in *INFO, which must be non-NULL.
5773 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5774
5775void
5776ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5777 domain_enum domain,
d12307c1 5778 struct block_symbol *info)
14f9c5c9 5779{
b5ec771e
PA
5780 /* Since we already have an encoded name, wrap it in '<>' to force a
5781 verbatim match. Otherwise, if the name happens to not look like
5782 an encoded name (because it doesn't include a "__"),
5783 ada_lookup_name_info would re-encode/fold it again, and that
5784 would e.g., incorrectly lowercase object renaming names like
5785 "R28b" -> "r28b". */
12932e2c 5786 std::string verbatim = add_angle_brackets (name);
b5ec771e 5787
5e2336be 5788 gdb_assert (info != NULL);
65392b3e 5789 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5790}
aeb5907d
JB
5791
5792/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5793 scope and in global scopes, or NULL if none. NAME is folded and
5794 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5795 choosing the first symbol if there are multiple choices. */
4e5c77fe 5796
d12307c1 5797struct block_symbol
aeb5907d 5798ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5799 domain_enum domain)
aeb5907d 5800{
d1183b06
TT
5801 std::vector<struct block_symbol> candidates
5802 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5803
d1183b06 5804 if (candidates.empty ())
54d343a2 5805 return {};
f98fc17b 5806
dae58e04 5807 return candidates[0];
4c4b4cd2 5808}
14f9c5c9 5809
14f9c5c9 5810
4c4b4cd2
PH
5811/* True iff STR is a possible encoded suffix of a normal Ada name
5812 that is to be ignored for matching purposes. Suffixes of parallel
5813 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5814 are given by any of the regular expressions:
4c4b4cd2 5815
babe1480
JB
5816 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5817 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5818 TKB [subprogram suffix for task bodies]
babe1480 5819 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5820 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5821
5822 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5823 match is performed. This sequence is used to differentiate homonyms,
5824 is an optional part of a valid name suffix. */
4c4b4cd2 5825
14f9c5c9 5826static int
d2e4a39e 5827is_name_suffix (const char *str)
14f9c5c9
AS
5828{
5829 int k;
4c4b4cd2
PH
5830 const char *matching;
5831 const int len = strlen (str);
5832
babe1480
JB
5833 /* Skip optional leading __[0-9]+. */
5834
4c4b4cd2
PH
5835 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5836 {
babe1480
JB
5837 str += 3;
5838 while (isdigit (str[0]))
dda83cd7 5839 str += 1;
4c4b4cd2 5840 }
babe1480
JB
5841
5842 /* [.$][0-9]+ */
4c4b4cd2 5843
babe1480 5844 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5845 {
babe1480 5846 matching = str + 1;
4c4b4cd2 5847 while (isdigit (matching[0]))
dda83cd7 5848 matching += 1;
4c4b4cd2 5849 if (matching[0] == '\0')
dda83cd7 5850 return 1;
4c4b4cd2
PH
5851 }
5852
5853 /* ___[0-9]+ */
babe1480 5854
4c4b4cd2
PH
5855 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5856 {
5857 matching = str + 3;
5858 while (isdigit (matching[0]))
dda83cd7 5859 matching += 1;
4c4b4cd2 5860 if (matching[0] == '\0')
dda83cd7 5861 return 1;
4c4b4cd2
PH
5862 }
5863
9ac7f98e
JB
5864 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5865
5866 if (strcmp (str, "TKB") == 0)
5867 return 1;
5868
529cad9c
PH
5869#if 0
5870 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5871 with a N at the end. Unfortunately, the compiler uses the same
5872 convention for other internal types it creates. So treating
529cad9c 5873 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5874 some regressions. For instance, consider the case of an enumerated
5875 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5876 name ends with N.
5877 Having a single character like this as a suffix carrying some
0963b4bd 5878 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5879 to be something like "_N" instead. In the meantime, do not do
5880 the following check. */
5881 /* Protected Object Subprograms */
5882 if (len == 1 && str [0] == 'N')
5883 return 1;
5884#endif
5885
5886 /* _E[0-9]+[bs]$ */
5887 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5888 {
5889 matching = str + 3;
5890 while (isdigit (matching[0]))
dda83cd7 5891 matching += 1;
529cad9c 5892 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5893 && matching [1] == '\0')
5894 return 1;
529cad9c
PH
5895 }
5896
4c4b4cd2
PH
5897 /* ??? We should not modify STR directly, as we are doing below. This
5898 is fine in this case, but may become problematic later if we find
5899 that this alternative did not work, and want to try matching
5900 another one from the begining of STR. Since we modified it, we
5901 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5902 if (str[0] == 'X')
5903 {
5904 str += 1;
d2e4a39e 5905 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5906 {
5907 if (str[0] != 'n' && str[0] != 'b')
5908 return 0;
5909 str += 1;
5910 }
14f9c5c9 5911 }
babe1480 5912
14f9c5c9
AS
5913 if (str[0] == '\000')
5914 return 1;
babe1480 5915
d2e4a39e 5916 if (str[0] == '_')
14f9c5c9
AS
5917 {
5918 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5919 return 0;
d2e4a39e 5920 if (str[2] == '_')
dda83cd7
SM
5921 {
5922 if (strcmp (str + 3, "JM") == 0)
5923 return 1;
5924 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5925 the LJM suffix in favor of the JM one. But we will
5926 still accept LJM as a valid suffix for a reasonable
5927 amount of time, just to allow ourselves to debug programs
5928 compiled using an older version of GNAT. */
5929 if (strcmp (str + 3, "LJM") == 0)
5930 return 1;
5931 if (str[3] != 'X')
5932 return 0;
5933 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5934 || str[4] == 'U' || str[4] == 'P')
5935 return 1;
5936 if (str[4] == 'R' && str[5] != 'T')
5937 return 1;
5938 return 0;
5939 }
4c4b4cd2 5940 if (!isdigit (str[2]))
dda83cd7 5941 return 0;
4c4b4cd2 5942 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5943 if (!isdigit (str[k]) && str[k] != '_')
5944 return 0;
14f9c5c9
AS
5945 return 1;
5946 }
4c4b4cd2 5947 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5948 {
4c4b4cd2 5949 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5950 if (!isdigit (str[k]) && str[k] != '_')
5951 return 0;
14f9c5c9
AS
5952 return 1;
5953 }
5954 return 0;
5955}
d2e4a39e 5956
aeb5907d
JB
5957/* Return non-zero if the string starting at NAME and ending before
5958 NAME_END contains no capital letters. */
529cad9c
PH
5959
5960static int
5961is_valid_name_for_wild_match (const char *name0)
5962{
f945dedf 5963 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5964 int i;
5965
5823c3ef
JB
5966 /* If the decoded name starts with an angle bracket, it means that
5967 NAME0 does not follow the GNAT encoding format. It should then
5968 not be allowed as a possible wild match. */
5969 if (decoded_name[0] == '<')
5970 return 0;
5971
529cad9c
PH
5972 for (i=0; decoded_name[i] != '\0'; i++)
5973 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5974 return 0;
5975
5976 return 1;
5977}
5978
59c8a30b
JB
5979/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5980 character which could start a simple name. Assumes that *NAMEP points
5981 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5982
14f9c5c9 5983static int
59c8a30b 5984advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5985{
73589123 5986 const char *name = *namep;
5b4ee69b 5987
5823c3ef 5988 while (1)
14f9c5c9 5989 {
59c8a30b 5990 char t0, t1;
73589123
PH
5991
5992 t0 = *name;
5993 if (t0 == '_')
5994 {
5995 t1 = name[1];
5996 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5997 {
5998 name += 1;
61012eef 5999 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
6000 break;
6001 else
6002 name += 1;
6003 }
aa27d0b3
JB
6004 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
6005 || name[2] == target0))
73589123
PH
6006 {
6007 name += 2;
6008 break;
6009 }
86b44259
TT
6010 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
6011 {
6012 /* Names like "pkg__B_N__name", where N is a number, are
6013 block-local. We can handle these by simply skipping
6014 the "B_" here. */
6015 name += 4;
6016 }
73589123
PH
6017 else
6018 return 0;
6019 }
6020 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
6021 name += 1;
6022 else
5823c3ef 6023 return 0;
73589123
PH
6024 }
6025
6026 *namep = name;
6027 return 1;
6028}
6029
b5ec771e
PA
6030/* Return true iff NAME encodes a name of the form prefix.PATN.
6031 Ignores any informational suffixes of NAME (i.e., for which
6032 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6033 simple name. */
73589123 6034
b5ec771e 6035static bool
73589123
PH
6036wild_match (const char *name, const char *patn)
6037{
22e048c9 6038 const char *p;
73589123
PH
6039 const char *name0 = name;
6040
81eaa506
TT
6041 if (startswith (name, "___ghost_"))
6042 name += 9;
6043
73589123
PH
6044 while (1)
6045 {
6046 const char *match = name;
6047
6048 if (*name == *patn)
6049 {
6050 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6051 if (*p != *name)
6052 break;
6053 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6054 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6055
6056 if (name[-1] == '_')
6057 name -= 1;
6058 }
6059 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6060 return false;
96d887e8 6061 }
96d887e8
PH
6062}
6063
d1183b06 6064/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6065 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6066
6067static void
d1183b06 6068ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6069 const struct block *block,
6070 const lookup_name_info &lookup_name,
6071 domain_enum domain, struct objfile *objfile)
96d887e8 6072{
96d887e8
PH
6073 /* A matching argument symbol, if any. */
6074 struct symbol *arg_sym;
6075 /* Set true when we find a matching non-argument symbol. */
1178743e 6076 bool found_sym;
96d887e8
PH
6077
6078 arg_sym = NULL;
1178743e 6079 found_sym = false;
1c49bb45 6080 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
96d887e8 6081 {
6c9c307c 6082 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
b5ec771e 6083 {
66d7f48f 6084 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6085 {
d9743061 6086 if (sym->is_argument ())
b5ec771e
PA
6087 arg_sym = sym;
6088 else
6089 {
1178743e 6090 found_sym = true;
dae58e04 6091 add_defn_to_vec (result, sym, block);
b5ec771e
PA
6092 }
6093 }
6094 }
96d887e8
PH
6095 }
6096
22cee43f
PMR
6097 /* Handle renamings. */
6098
d1183b06 6099 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6100 found_sym = true;
22cee43f 6101
96d887e8
PH
6102 if (!found_sym && arg_sym != NULL)
6103 {
dae58e04 6104 add_defn_to_vec (result, arg_sym, block);
96d887e8
PH
6105 }
6106
b5ec771e 6107 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6108 {
6109 arg_sym = NULL;
1178743e 6110 found_sym = false;
b5ec771e
PA
6111 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6112 const char *name = ada_lookup_name.c_str ();
6113 size_t name_len = ada_lookup_name.size ();
1c49bb45
TT
6114 struct symbol *sym;
6115 struct block_iterator iter;
96d887e8
PH
6116
6117 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 6118 {
dda83cd7 6119 if (symbol_matches_domain (sym->language (),
6c9c307c 6120 sym->domain (), domain))
dda83cd7
SM
6121 {
6122 int cmp;
6123
6124 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6125 if (cmp == 0)
6126 {
6127 cmp = !startswith (sym->linkage_name (), "_ada_");
6128 if (cmp == 0)
6129 cmp = strncmp (name, sym->linkage_name () + 5,
6130 name_len);
6131 }
6132
6133 if (cmp == 0
6134 && is_name_suffix (sym->linkage_name () + name_len + 5))
6135 {
66d7f48f 6136 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6137 {
d9743061 6138 if (sym->is_argument ())
2a2d4dc3
AS
6139 arg_sym = sym;
6140 else
6141 {
1178743e 6142 found_sym = true;
dae58e04 6143 add_defn_to_vec (result, sym, block);
2a2d4dc3
AS
6144 }
6145 }
dda83cd7
SM
6146 }
6147 }
76a01679 6148 }
96d887e8
PH
6149
6150 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6151 They aren't parameters, right? */
96d887e8 6152 if (!found_sym && arg_sym != NULL)
dda83cd7 6153 {
dae58e04 6154 add_defn_to_vec (result, arg_sym, block);
dda83cd7 6155 }
96d887e8
PH
6156 }
6157}
6158\f
41d27058 6159
dda83cd7 6160 /* Symbol Completion */
41d27058 6161
b5ec771e 6162/* See symtab.h. */
41d27058 6163
b5ec771e
PA
6164bool
6165ada_lookup_name_info::matches
6166 (const char *sym_name,
6167 symbol_name_match_type match_type,
a207cff2 6168 completion_match_result *comp_match_res) const
41d27058 6169{
b5ec771e
PA
6170 bool match = false;
6171 const char *text = m_encoded_name.c_str ();
6172 size_t text_len = m_encoded_name.size ();
41d27058
JB
6173
6174 /* First, test against the fully qualified name of the symbol. */
6175
6176 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6177 match = true;
41d27058 6178
f945dedf 6179 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6180 if (match && !m_encoded_p)
41d27058
JB
6181 {
6182 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6183 that iff we are doing a verbatim match, the decoded version
6184 of the symbol name starts with '<'. Otherwise, this symbol name
6185 is not a suitable completion. */
41d27058 6186
f945dedf 6187 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6188 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6189 }
6190
b5ec771e 6191 if (match && !m_verbatim_p)
41d27058
JB
6192 {
6193 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6194 be done is to verify that the potentially matching symbol name
6195 does not include capital letters, because the ada-mode would
6196 not be able to understand these symbol names without the
6197 angle bracket notation. */
41d27058
JB
6198 const char *tmp;
6199
6200 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6201 if (*tmp != '\0')
b5ec771e 6202 match = false;
41d27058
JB
6203 }
6204
6205 /* Second: Try wild matching... */
6206
b5ec771e 6207 if (!match && m_wild_match_p)
41d27058
JB
6208 {
6209 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6210 may represent an unqualified symbol name. We therefore must
6211 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6212 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6213
6214 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6215 match = true;
41d27058
JB
6216 }
6217
b5ec771e 6218 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6219
6220 if (!match)
b5ec771e 6221 return false;
41d27058 6222
a207cff2 6223 if (comp_match_res != NULL)
b5ec771e 6224 {
a207cff2 6225 std::string &match_str = comp_match_res->match.storage ();
41d27058 6226
b5ec771e 6227 if (!m_encoded_p)
a207cff2 6228 match_str = ada_decode (sym_name);
b5ec771e
PA
6229 else
6230 {
6231 if (m_verbatim_p)
6232 match_str = add_angle_brackets (sym_name);
6233 else
6234 match_str = sym_name;
41d27058 6235
b5ec771e 6236 }
a207cff2
PA
6237
6238 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6239 }
6240
b5ec771e 6241 return true;
41d27058
JB
6242}
6243
dda83cd7 6244 /* Field Access */
96d887e8 6245
73fb9985
JB
6246/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6247 for tagged types. */
6248
6249static int
6250ada_is_dispatch_table_ptr_type (struct type *type)
6251{
0d5cff50 6252 const char *name;
73fb9985 6253
78134374 6254 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6255 return 0;
6256
27710edb 6257 name = type->target_type ()->name ();
73fb9985
JB
6258 if (name == NULL)
6259 return 0;
6260
6261 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6262}
6263
ac4a2da4
JG
6264/* Return non-zero if TYPE is an interface tag. */
6265
6266static int
6267ada_is_interface_tag (struct type *type)
6268{
7d93a1e0 6269 const char *name = type->name ();
ac4a2da4
JG
6270
6271 if (name == NULL)
6272 return 0;
6273
6274 return (strcmp (name, "ada__tags__interface_tag") == 0);
6275}
6276
963a6417
PH
6277/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6278 to be invisible to users. */
96d887e8 6279
963a6417
PH
6280int
6281ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6282{
1f704f76 6283 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6284 return 1;
ffde82bf 6285
73fb9985
JB
6286 /* Check the name of that field. */
6287 {
33d16dd9 6288 const char *name = type->field (field_num).name ();
73fb9985
JB
6289
6290 /* Anonymous field names should not be printed.
6291 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6292 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6293 if (name == NULL)
6294 return 1;
6295
ffde82bf
JB
6296 /* Normally, fields whose name start with an underscore ("_")
6297 are fields that have been internally generated by the compiler,
6298 and thus should not be printed. The "_parent" field is special,
6299 however: This is a field internally generated by the compiler
6300 for tagged types, and it contains the components inherited from
6301 the parent type. This field should not be printed as is, but
6302 should not be ignored either. */
61012eef 6303 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985 6304 return 1;
d537777d
TT
6305
6306 /* The compiler doesn't document this, but sometimes it emits
6307 a field whose name starts with a capital letter, like 'V148s'.
6308 These aren't marked as artificial in any way, but we know they
6309 should be ignored. However, wrapper fields should not be
6310 ignored. */
6311 if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
6312 {
6313 /* Wrapper field. */
6314 }
6315 else if (isupper (name[0]))
6316 return 1;
73fb9985
JB
6317 }
6318
ac4a2da4
JG
6319 /* If this is the dispatch table of a tagged type or an interface tag,
6320 then ignore. */
73fb9985 6321 if (ada_is_tagged_type (type, 1)
940da03e
SM
6322 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6323 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6324 return 1;
6325
6326 /* Not a special field, so it should not be ignored. */
6327 return 0;
963a6417 6328}
96d887e8 6329
963a6417 6330/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6331 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6332
963a6417
PH
6333int
6334ada_is_tagged_type (struct type *type, int refok)
6335{
988f6b3d 6336 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6337}
96d887e8 6338
963a6417 6339/* True iff TYPE represents the type of X'Tag */
96d887e8 6340
963a6417
PH
6341int
6342ada_is_tag_type (struct type *type)
6343{
460efde1
JB
6344 type = ada_check_typedef (type);
6345
78134374 6346 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6347 return 0;
6348 else
96d887e8 6349 {
27710edb 6350 const char *name = ada_type_name (type->target_type ());
5b4ee69b 6351
963a6417 6352 return (name != NULL
dda83cd7 6353 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6354 }
96d887e8
PH
6355}
6356
963a6417 6357/* The type of the tag on VAL. */
76a01679 6358
de93309a 6359static struct type *
963a6417 6360ada_tag_type (struct value *val)
96d887e8 6361{
d0c97917 6362 return ada_lookup_struct_elt_type (val->type (), "_tag", 1, 0);
963a6417 6363}
96d887e8 6364
b50d69b5
JG
6365/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6366 retired at Ada 05). */
6367
6368static int
6369is_ada95_tag (struct value *tag)
6370{
6371 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6372}
6373
963a6417 6374/* The value of the tag on VAL. */
96d887e8 6375
de93309a 6376static struct value *
963a6417
PH
6377ada_value_tag (struct value *val)
6378{
03ee6b2e 6379 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6380}
6381
963a6417
PH
6382/* The value of the tag on the object of type TYPE whose contents are
6383 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6384 ADDRESS. */
96d887e8 6385
963a6417 6386static struct value *
10a2c479 6387value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6388 const gdb_byte *valaddr,
dda83cd7 6389 CORE_ADDR address)
96d887e8 6390{
b5385fc0 6391 int tag_byte_offset;
963a6417 6392 struct type *tag_type;
5b4ee69b 6393
4d1795ac
TT
6394 gdb::array_view<const gdb_byte> contents;
6395 if (valaddr != nullptr)
df86565b 6396 contents = gdb::make_array_view (valaddr, type->length ());
4d1795ac
TT
6397 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6398 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6399 NULL, NULL, NULL))
96d887e8 6400 {
fc1a4b47 6401 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6402 ? NULL
6403 : valaddr + tag_byte_offset);
963a6417 6404 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6405
963a6417 6406 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6407 }
963a6417
PH
6408 return NULL;
6409}
96d887e8 6410
963a6417
PH
6411static struct type *
6412type_from_tag (struct value *tag)
6413{
f5272a3b 6414 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6415
963a6417 6416 if (type_name != NULL)
5c4258f4 6417 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6418 return NULL;
6419}
96d887e8 6420
b50d69b5
JG
6421/* Given a value OBJ of a tagged type, return a value of this
6422 type at the base address of the object. The base address, as
6423 defined in Ada.Tags, it is the address of the primary tag of
6424 the object, and therefore where the field values of its full
6425 view can be fetched. */
6426
6427struct value *
6428ada_tag_value_at_base_address (struct value *obj)
6429{
b50d69b5
JG
6430 struct value *val;
6431 LONGEST offset_to_top = 0;
6432 struct type *ptr_type, *obj_type;
6433 struct value *tag;
6434 CORE_ADDR base_address;
6435
d0c97917 6436 obj_type = obj->type ();
b50d69b5
JG
6437
6438 /* It is the responsability of the caller to deref pointers. */
6439
78134374 6440 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6441 return obj;
6442
6443 tag = ada_value_tag (obj);
6444 if (!tag)
6445 return obj;
6446
6447 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6448
6449 if (is_ada95_tag (tag))
6450 return obj;
6451
d537777d
TT
6452 struct type *offset_type
6453 = language_lookup_primitive_type (language_def (language_ada),
6454 target_gdbarch(), "storage_offset");
6455 ptr_type = lookup_pointer_type (offset_type);
b50d69b5
JG
6456 val = value_cast (ptr_type, tag);
6457 if (!val)
6458 return obj;
6459
6460 /* It is perfectly possible that an exception be raised while
6461 trying to determine the base address, just like for the tag;
6462 see ada_tag_name for more details. We do not print the error
6463 message for the same reason. */
6464
a70b8144 6465 try
b50d69b5
JG
6466 {
6467 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6468 }
6469
230d2906 6470 catch (const gdb_exception_error &e)
492d29ea
PA
6471 {
6472 return obj;
6473 }
b50d69b5
JG
6474
6475 /* If offset is null, nothing to do. */
6476
6477 if (offset_to_top == 0)
6478 return obj;
6479
6480 /* -1 is a special case in Ada.Tags; however, what should be done
6481 is not quite clear from the documentation. So do nothing for
6482 now. */
6483
6484 if (offset_to_top == -1)
6485 return obj;
6486
d537777d
TT
6487 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6488 top is used. In this situation the offset is stored just after
6489 the tag, in the object itself. */
df86565b 6490 ULONGEST last = (((ULONGEST) 1) << (8 * offset_type->length () - 1)) - 1;
d537777d
TT
6491 if (offset_to_top == last)
6492 {
6493 struct value *tem = value_addr (tag);
6494 tem = value_ptradd (tem, 1);
6495 tem = value_cast (ptr_type, tem);
6496 offset_to_top = value_as_long (value_ind (tem));
6497 }
05527d8c
TV
6498
6499 if (offset_to_top > 0)
d537777d
TT
6500 {
6501 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6502 from the base address. This was however incompatible with
6503 C++ dispatch table: C++ uses a *negative* value to *add*
6504 to the base address. Ada's convention has therefore been
6505 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6506 use the same convention. Here, we support both cases by
6507 checking the sign of OFFSET_TO_TOP. */
6508 offset_to_top = -offset_to_top;
6509 }
08f49010 6510
9feb2d07 6511 base_address = obj->address () + offset_to_top;
b50d69b5
JG
6512 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6513
6514 /* Make sure that we have a proper tag at the new address.
6515 Otherwise, offset_to_top is bogus (which can happen when
6516 the object is not initialized yet). */
6517
6518 if (!tag)
6519 return obj;
6520
6521 obj_type = type_from_tag (tag);
6522
6523 if (!obj_type)
6524 return obj;
6525
6526 return value_from_contents_and_address (obj_type, NULL, base_address);
6527}
6528
1b611343
JB
6529/* Return the "ada__tags__type_specific_data" type. */
6530
6531static struct type *
6532ada_get_tsd_type (struct inferior *inf)
963a6417 6533{
1b611343 6534 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6535
1b611343
JB
6536 if (data->tsd_type == 0)
6537 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6538 return data->tsd_type;
6539}
529cad9c 6540
1b611343
JB
6541/* Return the TSD (type-specific data) associated to the given TAG.
6542 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6543
1b611343 6544 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6545
1b611343
JB
6546static struct value *
6547ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6548{
4c4b4cd2 6549 struct value *val;
1b611343 6550 struct type *type;
5b4ee69b 6551
1b611343
JB
6552 /* First option: The TSD is simply stored as a field of our TAG.
6553 Only older versions of GNAT would use this format, but we have
6554 to test it first, because there are no visible markers for
6555 the current approach except the absence of that field. */
529cad9c 6556
1b611343
JB
6557 val = ada_value_struct_elt (tag, "tsd", 1);
6558 if (val)
6559 return val;
e802dbe0 6560
1b611343
JB
6561 /* Try the second representation for the dispatch table (in which
6562 there is no explicit 'tsd' field in the referent of the tag pointer,
6563 and instead the tsd pointer is stored just before the dispatch
6564 table. */
e802dbe0 6565
1b611343
JB
6566 type = ada_get_tsd_type (current_inferior());
6567 if (type == NULL)
6568 return NULL;
6569 type = lookup_pointer_type (lookup_pointer_type (type));
6570 val = value_cast (type, tag);
6571 if (val == NULL)
6572 return NULL;
6573 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6574}
6575
1b611343
JB
6576/* Given the TSD of a tag (type-specific data), return a string
6577 containing the name of the associated type.
6578
f5272a3b 6579 May return NULL if we are unable to determine the tag name. */
1b611343 6580
f5272a3b 6581static gdb::unique_xmalloc_ptr<char>
1b611343 6582ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6583{
1b611343 6584 struct value *val;
529cad9c 6585
1b611343 6586 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6587 if (val == NULL)
1b611343 6588 return NULL;
66920317
TT
6589 gdb::unique_xmalloc_ptr<char> buffer
6590 = target_read_string (value_as_address (val), INT_MAX);
6591 if (buffer == nullptr)
f5272a3b
TT
6592 return nullptr;
6593
315e4ebb 6594 try
f5272a3b 6595 {
315e4ebb
TT
6596 /* Let this throw an exception on error. If the data is
6597 uninitialized, we'd rather not have the user see a
6598 warning. */
6599 const char *folded = ada_fold_name (buffer.get (), true);
6600 return make_unique_xstrdup (folded);
6601 }
6602 catch (const gdb_exception &)
6603 {
6604 return nullptr;
f5272a3b 6605 }
4c4b4cd2
PH
6606}
6607
6608/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6609 a C string.
6610
6611 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6612 determine the name of that tag. */
4c4b4cd2 6613
f5272a3b 6614gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6615ada_tag_name (struct value *tag)
6616{
f5272a3b 6617 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6618
d0c97917 6619 if (!ada_is_tag_type (tag->type ()))
4c4b4cd2 6620 return NULL;
1b611343
JB
6621
6622 /* It is perfectly possible that an exception be raised while trying
6623 to determine the TAG's name, even under normal circumstances:
6624 The associated variable may be uninitialized or corrupted, for
6625 instance. We do not let any exception propagate past this point.
6626 instead we return NULL.
6627
6628 We also do not print the error message either (which often is very
6629 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6630 the caller print a more meaningful message if necessary. */
a70b8144 6631 try
1b611343
JB
6632 {
6633 struct value *tsd = ada_get_tsd_from_tag (tag);
6634
6635 if (tsd != NULL)
6636 name = ada_tag_name_from_tsd (tsd);
6637 }
230d2906 6638 catch (const gdb_exception_error &e)
492d29ea
PA
6639 {
6640 }
1b611343
JB
6641
6642 return name;
4c4b4cd2
PH
6643}
6644
6645/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6646
d2e4a39e 6647struct type *
ebf56fd3 6648ada_parent_type (struct type *type)
14f9c5c9
AS
6649{
6650 int i;
6651
61ee279c 6652 type = ada_check_typedef (type);
14f9c5c9 6653
78134374 6654 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6655 return NULL;
6656
1f704f76 6657 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6658 if (ada_is_parent_field (type, i))
0c1f74cf 6659 {
dda83cd7 6660 struct type *parent_type = type->field (i).type ();
0c1f74cf 6661
dda83cd7
SM
6662 /* If the _parent field is a pointer, then dereference it. */
6663 if (parent_type->code () == TYPE_CODE_PTR)
27710edb 6664 parent_type = parent_type->target_type ();
dda83cd7
SM
6665 /* If there is a parallel XVS type, get the actual base type. */
6666 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6667
dda83cd7 6668 return ada_check_typedef (parent_type);
0c1f74cf 6669 }
14f9c5c9
AS
6670
6671 return NULL;
6672}
6673
4c4b4cd2
PH
6674/* True iff field number FIELD_NUM of structure type TYPE contains the
6675 parent-type (inherited) fields of a derived type. Assumes TYPE is
6676 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6677
6678int
ebf56fd3 6679ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6680{
33d16dd9 6681 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6682
4c4b4cd2 6683 return (name != NULL
dda83cd7
SM
6684 && (startswith (name, "PARENT")
6685 || startswith (name, "_parent")));
14f9c5c9
AS
6686}
6687
4c4b4cd2 6688/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6689 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6690 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6691 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6692 structures. */
14f9c5c9
AS
6693
6694int
ebf56fd3 6695ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6696{
33d16dd9 6697 const char *name = type->field (field_num).name ();
5b4ee69b 6698
dddc0e16
JB
6699 if (name != NULL && strcmp (name, "RETVAL") == 0)
6700 {
6701 /* This happens in functions with "out" or "in out" parameters
6702 which are passed by copy. For such functions, GNAT describes
6703 the function's return type as being a struct where the return
6704 value is in a field called RETVAL, and where the other "out"
6705 or "in out" parameters are fields of that struct. This is not
6706 a wrapper. */
6707 return 0;
6708 }
6709
d2e4a39e 6710 return (name != NULL
dda83cd7
SM
6711 && (startswith (name, "PARENT")
6712 || strcmp (name, "REP") == 0
6713 || startswith (name, "_parent")
6714 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6715}
6716
4c4b4cd2
PH
6717/* True iff field number FIELD_NUM of structure or union type TYPE
6718 is a variant wrapper. Assumes TYPE is a structure type with at least
6719 FIELD_NUM+1 fields. */
14f9c5c9
AS
6720
6721int
ebf56fd3 6722ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6723{
8ecb59f8
TT
6724 /* Only Ada types are eligible. */
6725 if (!ADA_TYPE_P (type))
6726 return 0;
6727
940da03e 6728 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6729
78134374
SM
6730 return (field_type->code () == TYPE_CODE_UNION
6731 || (is_dynamic_field (type, field_num)
27710edb 6732 && (field_type->target_type ()->code ()
c3e5cd34 6733 == TYPE_CODE_UNION)));
14f9c5c9
AS
6734}
6735
6736/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6737 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6738 returns the type of the controlling discriminant for the variant.
6739 May return NULL if the type could not be found. */
14f9c5c9 6740
d2e4a39e 6741struct type *
ebf56fd3 6742ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6743{
a121b7c1 6744 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6745
988f6b3d 6746 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6747}
6748
4c4b4cd2 6749/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6750 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6751 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6752
de93309a 6753static int
ebf56fd3 6754ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6755{
33d16dd9 6756 const char *name = type->field (field_num).name ();
5b4ee69b 6757
14f9c5c9
AS
6758 return (name != NULL && name[0] == 'O');
6759}
6760
6761/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6762 returns the name of the discriminant controlling the variant.
6763 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6764
a121b7c1 6765const char *
ebf56fd3 6766ada_variant_discrim_name (struct type *type0)
14f9c5c9 6767{
5f9febe0 6768 static std::string result;
d2e4a39e
AS
6769 struct type *type;
6770 const char *name;
6771 const char *discrim_end;
6772 const char *discrim_start;
14f9c5c9 6773
78134374 6774 if (type0->code () == TYPE_CODE_PTR)
27710edb 6775 type = type0->target_type ();
14f9c5c9
AS
6776 else
6777 type = type0;
6778
6779 name = ada_type_name (type);
6780
6781 if (name == NULL || name[0] == '\000')
6782 return "";
6783
6784 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6785 discrim_end -= 1)
6786 {
61012eef 6787 if (startswith (discrim_end, "___XVN"))
dda83cd7 6788 break;
14f9c5c9
AS
6789 }
6790 if (discrim_end == name)
6791 return "";
6792
d2e4a39e 6793 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6794 discrim_start -= 1)
6795 {
d2e4a39e 6796 if (discrim_start == name + 1)
dda83cd7 6797 return "";
76a01679 6798 if ((discrim_start > name + 3
dda83cd7
SM
6799 && startswith (discrim_start - 3, "___"))
6800 || discrim_start[-1] == '.')
6801 break;
14f9c5c9
AS
6802 }
6803
5f9febe0
TT
6804 result = std::string (discrim_start, discrim_end - discrim_start);
6805 return result.c_str ();
14f9c5c9
AS
6806}
6807
4c4b4cd2
PH
6808/* Scan STR for a subtype-encoded number, beginning at position K.
6809 Put the position of the character just past the number scanned in
6810 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6811 Return 1 if there was a valid number at the given position, and 0
6812 otherwise. A "subtype-encoded" number consists of the absolute value
6813 in decimal, followed by the letter 'm' to indicate a negative number.
6814 Assumes 0m does not occur. */
14f9c5c9
AS
6815
6816int
d2e4a39e 6817ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6818{
6819 ULONGEST RU;
6820
d2e4a39e 6821 if (!isdigit (str[k]))
14f9c5c9
AS
6822 return 0;
6823
4c4b4cd2 6824 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6825 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6826 LONGEST. */
14f9c5c9
AS
6827 RU = 0;
6828 while (isdigit (str[k]))
6829 {
d2e4a39e 6830 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6831 k += 1;
6832 }
6833
d2e4a39e 6834 if (str[k] == 'm')
14f9c5c9
AS
6835 {
6836 if (R != NULL)
dda83cd7 6837 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6838 k += 1;
6839 }
6840 else if (R != NULL)
6841 *R = (LONGEST) RU;
6842
4c4b4cd2 6843 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6844 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6845 number representable as a LONGEST (although either would probably work
6846 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6847 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6848
6849 if (new_k != NULL)
6850 *new_k = k;
6851 return 1;
6852}
6853
4c4b4cd2
PH
6854/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6855 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6856 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6857
de93309a 6858static int
ebf56fd3 6859ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6860{
33d16dd9 6861 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6862 int p;
6863
6864 p = 0;
6865 while (1)
6866 {
d2e4a39e 6867 switch (name[p])
dda83cd7
SM
6868 {
6869 case '\0':
6870 return 0;
6871 case 'S':
6872 {
6873 LONGEST W;
6874
6875 if (!ada_scan_number (name, p + 1, &W, &p))
6876 return 0;
6877 if (val == W)
6878 return 1;
6879 break;
6880 }
6881 case 'R':
6882 {
6883 LONGEST L, U;
6884
6885 if (!ada_scan_number (name, p + 1, &L, &p)
6886 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6887 return 0;
6888 if (val >= L && val <= U)
6889 return 1;
6890 break;
6891 }
6892 case 'O':
6893 return 1;
6894 default:
6895 return 0;
6896 }
4c4b4cd2
PH
6897 }
6898}
6899
0963b4bd 6900/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6901
6902/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6903 ARG_TYPE, extract and return the value of one of its (non-static)
6904 fields. FIELDNO says which field. Differs from value_primitive_field
6905 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6906
5eb68a39 6907struct value *
d2e4a39e 6908ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6909 struct type *arg_type)
14f9c5c9 6910{
14f9c5c9
AS
6911 struct type *type;
6912
61ee279c 6913 arg_type = ada_check_typedef (arg_type);
940da03e 6914 type = arg_type->field (fieldno).type ();
14f9c5c9 6915
4504bbde
TT
6916 /* Handle packed fields. It might be that the field is not packed
6917 relative to its containing structure, but the structure itself is
6918 packed; in this case we must take the bit-field path. */
5011c493 6919 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || arg1->bitpos () != 0)
14f9c5c9 6920 {
b610c045 6921 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
14f9c5c9 6922 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6923
50888e42 6924 return ada_value_primitive_packed_val (arg1,
efaf1ae0 6925 arg1->contents ().data (),
dda83cd7
SM
6926 offset + bit_pos / 8,
6927 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6928 }
6929 else
6c49729e 6930 return arg1->primitive_field (offset, fieldno, arg_type);
14f9c5c9
AS
6931}
6932
52ce6436
PH
6933/* Find field with name NAME in object of type TYPE. If found,
6934 set the following for each argument that is non-null:
6935 - *FIELD_TYPE_P to the field's type;
6936 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6937 an object of that type;
6938 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6939 - *BIT_SIZE_P to its size in bits if the field is packed, and
6940 0 otherwise;
6941 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6942 fields up to but not including the desired field, or by the total
6943 number of fields if not found. A NULL value of NAME never
6944 matches; the function just counts visible fields in this case.
6945
828d5846
XR
6946 Notice that we need to handle when a tagged record hierarchy
6947 has some components with the same name, like in this scenario:
6948
6949 type Top_T is tagged record
dda83cd7
SM
6950 N : Integer := 1;
6951 U : Integer := 974;
6952 A : Integer := 48;
828d5846
XR
6953 end record;
6954
6955 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6956 N : Character := 'a';
6957 C : Integer := 3;
828d5846
XR
6958 end record;
6959
6960 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6961 N : Float := 4.0;
6962 C : Character := '5';
6963 X : Integer := 6;
6964 A : Character := 'J';
828d5846
XR
6965 end record;
6966
6967 Let's say we now have a variable declared and initialized as follow:
6968
6969 TC : Top_A := new Bottom_T;
6970
6971 And then we use this variable to call this function
6972
6973 procedure Assign (Obj: in out Top_T; TV : Integer);
6974
6975 as follow:
6976
6977 Assign (Top_T (B), 12);
6978
6979 Now, we're in the debugger, and we're inside that procedure
6980 then and we want to print the value of obj.c:
6981
6982 Usually, the tagged record or one of the parent type owns the
6983 component to print and there's no issue but in this particular
6984 case, what does it mean to ask for Obj.C? Since the actual
6985 type for object is type Bottom_T, it could mean two things: type
6986 component C from the Middle_T view, but also component C from
6987 Bottom_T. So in that "undefined" case, when the component is
6988 not found in the non-resolved type (which includes all the
6989 components of the parent type), then resolve it and see if we
6990 get better luck once expanded.
6991
6992 In the case of homonyms in the derived tagged type, we don't
6993 guaranty anything, and pick the one that's easiest for us
6994 to program.
6995
0963b4bd 6996 Returns 1 if found, 0 otherwise. */
52ce6436 6997
4c4b4cd2 6998static int
0d5cff50 6999find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
7000 struct type **field_type_p,
7001 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 7002 int *index_p)
4c4b4cd2
PH
7003{
7004 int i;
828d5846 7005 int parent_offset = -1;
4c4b4cd2 7006
61ee279c 7007 type = ada_check_typedef (type);
76a01679 7008
52ce6436
PH
7009 if (field_type_p != NULL)
7010 *field_type_p = NULL;
7011 if (byte_offset_p != NULL)
d5d6fca5 7012 *byte_offset_p = 0;
52ce6436
PH
7013 if (bit_offset_p != NULL)
7014 *bit_offset_p = 0;
7015 if (bit_size_p != NULL)
7016 *bit_size_p = 0;
7017
1f704f76 7018 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 7019 {
4d1795ac
TT
7020 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
7021 type. However, we only need the values to be correct when
7022 the caller asks for them. */
7023 int bit_pos = 0, fld_offset = 0;
7024 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7025 {
b610c045 7026 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
7027 fld_offset = offset + bit_pos / 8;
7028 }
7029
33d16dd9 7030 const char *t_field_name = type->field (i).name ();
76a01679 7031
4c4b4cd2 7032 if (t_field_name == NULL)
dda83cd7 7033 continue;
4c4b4cd2 7034
828d5846 7035 else if (ada_is_parent_field (type, i))
dda83cd7 7036 {
828d5846
XR
7037 /* This is a field pointing us to the parent type of a tagged
7038 type. As hinted in this function's documentation, we give
7039 preference to fields in the current record first, so what
7040 we do here is just record the index of this field before
7041 we skip it. If it turns out we couldn't find our field
7042 in the current record, then we'll get back to it and search
7043 inside it whether the field might exist in the parent. */
7044
dda83cd7
SM
7045 parent_offset = i;
7046 continue;
7047 }
828d5846 7048
52ce6436 7049 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
7050 {
7051 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 7052
52ce6436 7053 if (field_type_p != NULL)
940da03e 7054 *field_type_p = type->field (i).type ();
52ce6436
PH
7055 if (byte_offset_p != NULL)
7056 *byte_offset_p = fld_offset;
7057 if (bit_offset_p != NULL)
7058 *bit_offset_p = bit_pos % 8;
7059 if (bit_size_p != NULL)
7060 *bit_size_p = bit_size;
dda83cd7
SM
7061 return 1;
7062 }
4c4b4cd2 7063 else if (ada_is_wrapper_field (type, i))
dda83cd7 7064 {
940da03e 7065 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7066 field_type_p, byte_offset_p, bit_offset_p,
7067 bit_size_p, index_p))
dda83cd7
SM
7068 return 1;
7069 }
4c4b4cd2 7070 else if (ada_is_variant_part (type, i))
dda83cd7 7071 {
52ce6436
PH
7072 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7073 fixed type?? */
dda83cd7
SM
7074 int j;
7075 struct type *field_type
940da03e 7076 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7077
dda83cd7
SM
7078 for (j = 0; j < field_type->num_fields (); j += 1)
7079 {
7080 if (find_struct_field (name, field_type->field (j).type (),
7081 fld_offset
b610c045 7082 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7083 field_type_p, byte_offset_p,
7084 bit_offset_p, bit_size_p, index_p))
7085 return 1;
7086 }
7087 }
52ce6436
PH
7088 else if (index_p != NULL)
7089 *index_p += 1;
4c4b4cd2 7090 }
828d5846
XR
7091
7092 /* Field not found so far. If this is a tagged type which
7093 has a parent, try finding that field in the parent now. */
7094
7095 if (parent_offset != -1)
7096 {
4d1795ac
TT
7097 /* As above, only compute the offset when truly needed. */
7098 int fld_offset = offset;
7099 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7100 {
b610c045 7101 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7102 fld_offset += bit_pos / 8;
7103 }
828d5846 7104
940da03e 7105 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7106 fld_offset, field_type_p, byte_offset_p,
7107 bit_offset_p, bit_size_p, index_p))
7108 return 1;
828d5846
XR
7109 }
7110
4c4b4cd2
PH
7111 return 0;
7112}
7113
0963b4bd 7114/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7115
52ce6436
PH
7116static int
7117num_visible_fields (struct type *type)
7118{
7119 int n;
5b4ee69b 7120
52ce6436
PH
7121 n = 0;
7122 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7123 return n;
7124}
14f9c5c9 7125
4c4b4cd2 7126/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7127 and search in it assuming it has (class) type TYPE.
7128 If found, return value, else return NULL.
7129
828d5846
XR
7130 Searches recursively through wrapper fields (e.g., '_parent').
7131
7132 In the case of homonyms in the tagged types, please refer to the
7133 long explanation in find_struct_field's function documentation. */
14f9c5c9 7134
4c4b4cd2 7135static struct value *
108d56a4 7136ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7137 struct type *type)
14f9c5c9
AS
7138{
7139 int i;
828d5846 7140 int parent_offset = -1;
14f9c5c9 7141
5b4ee69b 7142 type = ada_check_typedef (type);
1f704f76 7143 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7144 {
33d16dd9 7145 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7146
7147 if (t_field_name == NULL)
dda83cd7 7148 continue;
14f9c5c9 7149
828d5846 7150 else if (ada_is_parent_field (type, i))
dda83cd7 7151 {
828d5846
XR
7152 /* This is a field pointing us to the parent type of a tagged
7153 type. As hinted in this function's documentation, we give
7154 preference to fields in the current record first, so what
7155 we do here is just record the index of this field before
7156 we skip it. If it turns out we couldn't find our field
7157 in the current record, then we'll get back to it and search
7158 inside it whether the field might exist in the parent. */
7159
dda83cd7
SM
7160 parent_offset = i;
7161 continue;
7162 }
828d5846 7163
14f9c5c9 7164 else if (field_name_match (t_field_name, name))
dda83cd7 7165 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7166
7167 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7168 {
7169 struct value *v = /* Do not let indent join lines here. */
7170 ada_search_struct_field (name, arg,
b610c045 7171 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7172 type->field (i).type ());
5b4ee69b 7173
dda83cd7
SM
7174 if (v != NULL)
7175 return v;
7176 }
14f9c5c9
AS
7177
7178 else if (ada_is_variant_part (type, i))
dda83cd7 7179 {
0963b4bd 7180 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7181 int j;
7182 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7183 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7184
dda83cd7
SM
7185 for (j = 0; j < field_type->num_fields (); j += 1)
7186 {
7187 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7188 break. */
dda83cd7 7189 (name, arg,
b610c045 7190 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7191 field_type->field (j).type ());
5b4ee69b 7192
dda83cd7
SM
7193 if (v != NULL)
7194 return v;
7195 }
7196 }
14f9c5c9 7197 }
828d5846
XR
7198
7199 /* Field not found so far. If this is a tagged type which
7200 has a parent, try finding that field in the parent now. */
7201
7202 if (parent_offset != -1)
7203 {
7204 struct value *v = ada_search_struct_field (
b610c045 7205 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7206 type->field (parent_offset).type ());
828d5846
XR
7207
7208 if (v != NULL)
dda83cd7 7209 return v;
828d5846
XR
7210 }
7211
14f9c5c9
AS
7212 return NULL;
7213}
d2e4a39e 7214
52ce6436
PH
7215static struct value *ada_index_struct_field_1 (int *, struct value *,
7216 int, struct type *);
7217
7218
7219/* Return field #INDEX in ARG, where the index is that returned by
7220 * find_struct_field through its INDEX_P argument. Adjust the address
7221 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7222 * If found, return value, else return NULL. */
52ce6436
PH
7223
7224static struct value *
7225ada_index_struct_field (int index, struct value *arg, int offset,
7226 struct type *type)
7227{
7228 return ada_index_struct_field_1 (&index, arg, offset, type);
7229}
7230
7231
7232/* Auxiliary function for ada_index_struct_field. Like
7233 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7234 * *INDEX_P. */
52ce6436
PH
7235
7236static struct value *
7237ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7238 struct type *type)
7239{
7240 int i;
7241 type = ada_check_typedef (type);
7242
1f704f76 7243 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7244 {
33d16dd9 7245 if (type->field (i).name () == NULL)
dda83cd7 7246 continue;
52ce6436 7247 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7248 {
7249 struct value *v = /* Do not let indent join lines here. */
7250 ada_index_struct_field_1 (index_p, arg,
b610c045 7251 offset + type->field (i).loc_bitpos () / 8,
940da03e 7252 type->field (i).type ());
5b4ee69b 7253
dda83cd7
SM
7254 if (v != NULL)
7255 return v;
7256 }
52ce6436
PH
7257
7258 else if (ada_is_variant_part (type, i))
dda83cd7 7259 {
52ce6436 7260 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7261 find_struct_field. */
52ce6436 7262 error (_("Cannot assign this kind of variant record"));
dda83cd7 7263 }
52ce6436 7264 else if (*index_p == 0)
dda83cd7 7265 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7266 else
7267 *index_p -= 1;
7268 }
7269 return NULL;
7270}
7271
3b4de39c 7272/* Return a string representation of type TYPE. */
99bbb428 7273
3b4de39c 7274static std::string
99bbb428
PA
7275type_as_string (struct type *type)
7276{
d7e74731 7277 string_file tmp_stream;
99bbb428 7278
d7e74731 7279 type_print (type, "", &tmp_stream, -1);
99bbb428 7280
5d10a204 7281 return tmp_stream.release ();
99bbb428
PA
7282}
7283
14f9c5c9 7284/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7285 If DISPP is non-null, add its byte displacement from the beginning of a
7286 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7287 work for packed fields).
7288
7289 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7290 followed by "___".
14f9c5c9 7291
0963b4bd 7292 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7293 be a (pointer or reference)+ to a struct or union, and the
7294 ultimate target type will be searched.
14f9c5c9
AS
7295
7296 Looks recursively into variant clauses and parent types.
7297
828d5846
XR
7298 In the case of homonyms in the tagged types, please refer to the
7299 long explanation in find_struct_field's function documentation.
7300
4c4b4cd2
PH
7301 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7302 TYPE is not a type of the right kind. */
14f9c5c9 7303
4c4b4cd2 7304static struct type *
a121b7c1 7305ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7306 int noerr)
14f9c5c9
AS
7307{
7308 int i;
828d5846 7309 int parent_offset = -1;
14f9c5c9
AS
7310
7311 if (name == NULL)
7312 goto BadName;
7313
76a01679 7314 if (refok && type != NULL)
4c4b4cd2
PH
7315 while (1)
7316 {
dda83cd7
SM
7317 type = ada_check_typedef (type);
7318 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7319 break;
27710edb 7320 type = type->target_type ();
4c4b4cd2 7321 }
14f9c5c9 7322
76a01679 7323 if (type == NULL
78134374
SM
7324 || (type->code () != TYPE_CODE_STRUCT
7325 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7326 {
4c4b4cd2 7327 if (noerr)
dda83cd7 7328 return NULL;
99bbb428 7329
3b4de39c
PA
7330 error (_("Type %s is not a structure or union type"),
7331 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7332 }
7333
7334 type = to_static_fixed_type (type);
7335
1f704f76 7336 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7337 {
33d16dd9 7338 const char *t_field_name = type->field (i).name ();
14f9c5c9 7339 struct type *t;
d2e4a39e 7340
14f9c5c9 7341 if (t_field_name == NULL)
dda83cd7 7342 continue;
14f9c5c9 7343
828d5846 7344 else if (ada_is_parent_field (type, i))
dda83cd7 7345 {
828d5846
XR
7346 /* This is a field pointing us to the parent type of a tagged
7347 type. As hinted in this function's documentation, we give
7348 preference to fields in the current record first, so what
7349 we do here is just record the index of this field before
7350 we skip it. If it turns out we couldn't find our field
7351 in the current record, then we'll get back to it and search
7352 inside it whether the field might exist in the parent. */
7353
dda83cd7
SM
7354 parent_offset = i;
7355 continue;
7356 }
828d5846 7357
14f9c5c9 7358 else if (field_name_match (t_field_name, name))
940da03e 7359 return type->field (i).type ();
14f9c5c9
AS
7360
7361 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7362 {
7363 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7364 0, 1);
7365 if (t != NULL)
988f6b3d 7366 return t;
dda83cd7 7367 }
14f9c5c9
AS
7368
7369 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7370 {
7371 int j;
7372 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7373
dda83cd7
SM
7374 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7375 {
b1f33ddd 7376 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7377 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7378 generates these for unchecked variant types. Revisit
dda83cd7 7379 if the compiler changes this practice. */
33d16dd9 7380 const char *v_field_name = field_type->field (j).name ();
988f6b3d 7381
b1f33ddd
JB
7382 if (v_field_name != NULL
7383 && field_name_match (v_field_name, name))
940da03e 7384 t = field_type->field (j).type ();
b1f33ddd 7385 else
940da03e 7386 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7387 name, 0, 1);
b1f33ddd 7388
dda83cd7 7389 if (t != NULL)
988f6b3d 7390 return t;
dda83cd7
SM
7391 }
7392 }
14f9c5c9
AS
7393
7394 }
7395
828d5846
XR
7396 /* Field not found so far. If this is a tagged type which
7397 has a parent, try finding that field in the parent now. */
7398
7399 if (parent_offset != -1)
7400 {
dda83cd7 7401 struct type *t;
828d5846 7402
dda83cd7
SM
7403 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7404 name, 0, 1);
7405 if (t != NULL)
828d5846
XR
7406 return t;
7407 }
7408
14f9c5c9 7409BadName:
d2e4a39e 7410 if (!noerr)
14f9c5c9 7411 {
2b2798cc 7412 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7413
7414 error (_("Type %s has no component named %s"),
3b4de39c 7415 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7416 }
7417
7418 return NULL;
7419}
7420
b1f33ddd
JB
7421/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7422 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7423 represents an unchecked union (that is, the variant part of a
0963b4bd 7424 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7425
7426static int
7427is_unchecked_variant (struct type *var_type, struct type *outer_type)
7428{
a121b7c1 7429 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7430
988f6b3d 7431 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7432}
7433
7434
14f9c5c9 7435/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7436 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7437 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7438
d2e4a39e 7439int
d8af9068 7440ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7441{
7442 int others_clause;
7443 int i;
a121b7c1 7444 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7445 struct value *discrim;
14f9c5c9
AS
7446 LONGEST discrim_val;
7447
012370f6
TT
7448 /* Using plain value_from_contents_and_address here causes problems
7449 because we will end up trying to resolve a type that is currently
7450 being constructed. */
0c281816
JB
7451 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7452 if (discrim == NULL)
14f9c5c9 7453 return -1;
0c281816 7454 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7455
7456 others_clause = -1;
1f704f76 7457 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7458 {
7459 if (ada_is_others_clause (var_type, i))
dda83cd7 7460 others_clause = i;
14f9c5c9 7461 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7462 return i;
14f9c5c9
AS
7463 }
7464
7465 return others_clause;
7466}
d2e4a39e 7467\f
14f9c5c9
AS
7468
7469
dda83cd7 7470 /* Dynamic-Sized Records */
14f9c5c9
AS
7471
7472/* Strategy: The type ostensibly attached to a value with dynamic size
7473 (i.e., a size that is not statically recorded in the debugging
7474 data) does not accurately reflect the size or layout of the value.
7475 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7476 conventional types that are constructed on the fly. */
14f9c5c9
AS
7477
7478/* There is a subtle and tricky problem here. In general, we cannot
7479 determine the size of dynamic records without its data. However,
7480 the 'struct value' data structure, which GDB uses to represent
7481 quantities in the inferior process (the target), requires the size
7482 of the type at the time of its allocation in order to reserve space
7483 for GDB's internal copy of the data. That's why the
7484 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7485 rather than struct value*s.
14f9c5c9
AS
7486
7487 However, GDB's internal history variables ($1, $2, etc.) are
7488 struct value*s containing internal copies of the data that are not, in
7489 general, the same as the data at their corresponding addresses in
7490 the target. Fortunately, the types we give to these values are all
7491 conventional, fixed-size types (as per the strategy described
7492 above), so that we don't usually have to perform the
7493 'to_fixed_xxx_type' conversions to look at their values.
7494 Unfortunately, there is one exception: if one of the internal
7495 history variables is an array whose elements are unconstrained
7496 records, then we will need to create distinct fixed types for each
7497 element selected. */
7498
7499/* The upshot of all of this is that many routines take a (type, host
7500 address, target address) triple as arguments to represent a value.
7501 The host address, if non-null, is supposed to contain an internal
7502 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7503 target at the target address. */
14f9c5c9
AS
7504
7505/* Assuming that VAL0 represents a pointer value, the result of
7506 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7507 dynamic-sized types. */
14f9c5c9 7508
d2e4a39e
AS
7509struct value *
7510ada_value_ind (struct value *val0)
14f9c5c9 7511{
c48db5ca 7512 struct value *val = value_ind (val0);
5b4ee69b 7513
d0c97917 7514 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7515 val = ada_tag_value_at_base_address (val);
7516
4c4b4cd2 7517 return ada_to_fixed_value (val);
14f9c5c9
AS
7518}
7519
7520/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7521 qualifiers on VAL0. */
7522
d2e4a39e
AS
7523static struct value *
7524ada_coerce_ref (struct value *val0)
7525{
d0c97917 7526 if (val0->type ()->code () == TYPE_CODE_REF)
d2e4a39e
AS
7527 {
7528 struct value *val = val0;
5b4ee69b 7529
994b9211 7530 val = coerce_ref (val);
b50d69b5 7531
d0c97917 7532 if (ada_is_tagged_type (val->type (), 0))
b50d69b5
JG
7533 val = ada_tag_value_at_base_address (val);
7534
4c4b4cd2 7535 return ada_to_fixed_value (val);
d2e4a39e
AS
7536 }
7537 else
14f9c5c9
AS
7538 return val0;
7539}
7540
4c4b4cd2 7541/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7542
7543static unsigned int
ebf56fd3 7544field_alignment (struct type *type, int f)
14f9c5c9 7545{
33d16dd9 7546 const char *name = type->field (f).name ();
64a1bf19 7547 int len;
14f9c5c9
AS
7548 int align_offset;
7549
64a1bf19
JB
7550 /* The field name should never be null, unless the debugging information
7551 is somehow malformed. In this case, we assume the field does not
7552 require any alignment. */
7553 if (name == NULL)
7554 return 1;
7555
7556 len = strlen (name);
7557
4c4b4cd2
PH
7558 if (!isdigit (name[len - 1]))
7559 return 1;
14f9c5c9 7560
d2e4a39e 7561 if (isdigit (name[len - 2]))
14f9c5c9
AS
7562 align_offset = len - 2;
7563 else
7564 align_offset = len - 1;
7565
61012eef 7566 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7567 return TARGET_CHAR_BIT;
7568
4c4b4cd2
PH
7569 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7570}
7571
852dff6c 7572/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7573
852dff6c
JB
7574static struct symbol *
7575ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7576{
7577 struct symbol *sym;
7578
7579 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
66d7f48f 7580 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7581 return sym;
7582
4186eb54
KS
7583 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7584 return sym;
14f9c5c9
AS
7585}
7586
dddfab26
UW
7587/* Find a type named NAME. Ignores ambiguity. This routine will look
7588 solely for types defined by debug info, it will not search the GDB
7589 primitive types. */
4c4b4cd2 7590
852dff6c 7591static struct type *
ebf56fd3 7592ada_find_any_type (const char *name)
14f9c5c9 7593{
852dff6c 7594 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7595
14f9c5c9 7596 if (sym != NULL)
5f9c5a63 7597 return sym->type ();
14f9c5c9 7598
dddfab26 7599 return NULL;
14f9c5c9
AS
7600}
7601
739593e0
JB
7602/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7603 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7604 symbol, in which case it is returned. Otherwise, this looks for
7605 symbols whose name is that of NAME_SYM suffixed with "___XR".
7606 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7607
c0e70c62
TT
7608static bool
7609ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7610{
987012b8 7611 const char *name = name_sym->linkage_name ();
c0e70c62 7612 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7613}
7614
14f9c5c9 7615/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7616 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7617 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7618 otherwise return 0. */
7619
14f9c5c9 7620int
d2e4a39e 7621ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7622{
7623 if (type1 == NULL)
7624 return 1;
7625 else if (type0 == NULL)
7626 return 0;
78134374 7627 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7628 return 1;
78134374 7629 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7630 return 0;
7d93a1e0 7631 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7632 return 1;
ad82864c 7633 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7634 return 1;
4c4b4cd2 7635 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7636 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7637 return 1;
aeb5907d
JB
7638 else
7639 {
7d93a1e0
SM
7640 const char *type0_name = type0->name ();
7641 const char *type1_name = type1->name ();
aeb5907d
JB
7642
7643 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7644 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7645 return 1;
7646 }
14f9c5c9
AS
7647 return 0;
7648}
7649
e86ca25f
TT
7650/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7651 null. */
4c4b4cd2 7652
0d5cff50 7653const char *
d2e4a39e 7654ada_type_name (struct type *type)
14f9c5c9 7655{
d2e4a39e 7656 if (type == NULL)
14f9c5c9 7657 return NULL;
7d93a1e0 7658 return type->name ();
14f9c5c9
AS
7659}
7660
b4ba55a1
JB
7661/* Search the list of "descriptive" types associated to TYPE for a type
7662 whose name is NAME. */
7663
7664static struct type *
7665find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7666{
931e5bc3 7667 struct type *result, *tmp;
b4ba55a1 7668
c6044dd1
JB
7669 if (ada_ignore_descriptive_types_p)
7670 return NULL;
7671
b4ba55a1
JB
7672 /* If there no descriptive-type info, then there is no parallel type
7673 to be found. */
7674 if (!HAVE_GNAT_AUX_INFO (type))
7675 return NULL;
7676
7677 result = TYPE_DESCRIPTIVE_TYPE (type);
7678 while (result != NULL)
7679 {
0d5cff50 7680 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7681
7682 if (result_name == NULL)
dda83cd7
SM
7683 {
7684 warning (_("unexpected null name on descriptive type"));
7685 return NULL;
7686 }
b4ba55a1
JB
7687
7688 /* If the names match, stop. */
7689 if (strcmp (result_name, name) == 0)
7690 break;
7691
7692 /* Otherwise, look at the next item on the list, if any. */
7693 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7694 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7695 else
7696 tmp = NULL;
7697
7698 /* If not found either, try after having resolved the typedef. */
7699 if (tmp != NULL)
7700 result = tmp;
b4ba55a1 7701 else
931e5bc3 7702 {
f168693b 7703 result = check_typedef (result);
931e5bc3
JG
7704 if (HAVE_GNAT_AUX_INFO (result))
7705 result = TYPE_DESCRIPTIVE_TYPE (result);
7706 else
7707 result = NULL;
7708 }
b4ba55a1
JB
7709 }
7710
7711 /* If we didn't find a match, see whether this is a packed array. With
7712 older compilers, the descriptive type information is either absent or
7713 irrelevant when it comes to packed arrays so the above lookup fails.
7714 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7715 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7716 return ada_find_any_type (name);
7717
7718 return result;
7719}
7720
7721/* Find a parallel type to TYPE with the specified NAME, using the
7722 descriptive type taken from the debugging information, if available,
7723 and otherwise using the (slower) name-based method. */
7724
7725static struct type *
7726ada_find_parallel_type_with_name (struct type *type, const char *name)
7727{
7728 struct type *result = NULL;
7729
7730 if (HAVE_GNAT_AUX_INFO (type))
7731 result = find_parallel_type_by_descriptive_type (type, name);
7732 else
7733 result = ada_find_any_type (name);
7734
7735 return result;
7736}
7737
7738/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7739 SUFFIX to the name of TYPE. */
14f9c5c9 7740
d2e4a39e 7741struct type *
ebf56fd3 7742ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7743{
0d5cff50 7744 char *name;
fe978cb0 7745 const char *type_name = ada_type_name (type);
14f9c5c9 7746 int len;
d2e4a39e 7747
fe978cb0 7748 if (type_name == NULL)
14f9c5c9
AS
7749 return NULL;
7750
fe978cb0 7751 len = strlen (type_name);
14f9c5c9 7752
b4ba55a1 7753 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7754
fe978cb0 7755 strcpy (name, type_name);
14f9c5c9
AS
7756 strcpy (name + len, suffix);
7757
b4ba55a1 7758 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7759}
7760
14f9c5c9 7761/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7762 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7763
d2e4a39e
AS
7764static struct type *
7765dynamic_template_type (struct type *type)
14f9c5c9 7766{
61ee279c 7767 type = ada_check_typedef (type);
14f9c5c9 7768
78134374 7769 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7770 || ada_type_name (type) == NULL)
14f9c5c9 7771 return NULL;
d2e4a39e 7772 else
14f9c5c9
AS
7773 {
7774 int len = strlen (ada_type_name (type));
5b4ee69b 7775
4c4b4cd2 7776 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7777 return type;
14f9c5c9 7778 else
dda83cd7 7779 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7780 }
7781}
7782
7783/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7784 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7785
d2e4a39e
AS
7786static int
7787is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7788{
33d16dd9 7789 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7790
d2e4a39e 7791 return name != NULL
940da03e 7792 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7793 && strstr (name, "___XVL") != NULL;
7794}
7795
4c4b4cd2
PH
7796/* The index of the variant field of TYPE, or -1 if TYPE does not
7797 represent a variant record type. */
14f9c5c9 7798
d2e4a39e 7799static int
4c4b4cd2 7800variant_field_index (struct type *type)
14f9c5c9
AS
7801{
7802 int f;
7803
78134374 7804 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7805 return -1;
7806
1f704f76 7807 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7808 {
7809 if (ada_is_variant_part (type, f))
dda83cd7 7810 return f;
4c4b4cd2
PH
7811 }
7812 return -1;
14f9c5c9
AS
7813}
7814
4c4b4cd2
PH
7815/* A record type with no fields. */
7816
d2e4a39e 7817static struct type *
fe978cb0 7818empty_record (struct type *templ)
14f9c5c9 7819{
fe978cb0 7820 struct type *type = alloc_type_copy (templ);
5b4ee69b 7821
67607e24 7822 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7823 INIT_NONE_SPECIFIC (type);
d0e39ea2 7824 type->set_name ("<empty>");
b6cdbc9a 7825 type->set_length (0);
14f9c5c9
AS
7826 return type;
7827}
7828
7829/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7830 the value of type TYPE at VALADDR or ADDRESS (see comments at
7831 the beginning of this section) VAL according to GNAT conventions.
7832 DVAL0 should describe the (portion of a) record that contains any
d0c97917 7833 necessary discriminants. It should be NULL if VAL->type () is
14f9c5c9
AS
7834 an outer-level type (i.e., as opposed to a branch of a variant.) A
7835 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7836 of the variant.
14f9c5c9 7837
4c4b4cd2
PH
7838 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7839 length are not statically known are discarded. As a consequence,
7840 VALADDR, ADDRESS and DVAL0 are ignored.
7841
7842 NOTE: Limitations: For now, we assume that dynamic fields and
7843 variants occupy whole numbers of bytes. However, they need not be
7844 byte-aligned. */
7845
7846struct type *
10a2c479 7847ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7848 const gdb_byte *valaddr,
dda83cd7
SM
7849 CORE_ADDR address, struct value *dval0,
7850 int keep_dynamic_fields)
14f9c5c9 7851{
d2e4a39e
AS
7852 struct value *dval;
7853 struct type *rtype;
14f9c5c9 7854 int nfields, bit_len;
4c4b4cd2 7855 int variant_field;
14f9c5c9 7856 long off;
d94e4f4f 7857 int fld_bit_len;
14f9c5c9
AS
7858 int f;
7859
65558ca5
TT
7860 scoped_value_mark mark;
7861
4c4b4cd2
PH
7862 /* Compute the number of fields in this record type that are going
7863 to be processed: unless keep_dynamic_fields, this includes only
7864 fields whose position and length are static will be processed. */
7865 if (keep_dynamic_fields)
1f704f76 7866 nfields = type->num_fields ();
4c4b4cd2
PH
7867 else
7868 {
7869 nfields = 0;
1f704f76 7870 while (nfields < type->num_fields ()
dda83cd7
SM
7871 && !ada_is_variant_part (type, nfields)
7872 && !is_dynamic_field (type, nfields))
7873 nfields++;
4c4b4cd2
PH
7874 }
7875
e9bb382b 7876 rtype = alloc_type_copy (type);
67607e24 7877 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7878 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7879 rtype->set_num_fields (nfields);
3cabb6b0
SM
7880 rtype->set_fields
7881 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7882 rtype->set_name (ada_type_name (type));
9cdd0d12 7883 rtype->set_is_fixed_instance (true);
14f9c5c9 7884
d2e4a39e
AS
7885 off = 0;
7886 bit_len = 0;
4c4b4cd2
PH
7887 variant_field = -1;
7888
14f9c5c9
AS
7889 for (f = 0; f < nfields; f += 1)
7890 {
a89febbd 7891 off = align_up (off, field_alignment (type, f))
b610c045 7892 + type->field (f).loc_bitpos ();
cd3f655c 7893 rtype->field (f).set_loc_bitpos (off);
d2e4a39e 7894 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7895
d2e4a39e 7896 if (ada_is_variant_part (type, f))
dda83cd7
SM
7897 {
7898 variant_field = f;
7899 fld_bit_len = 0;
7900 }
14f9c5c9 7901 else if (is_dynamic_field (type, f))
dda83cd7 7902 {
284614f0
JB
7903 const gdb_byte *field_valaddr = valaddr;
7904 CORE_ADDR field_address = address;
27710edb 7905 struct type *field_type = type->field (f).type ()->target_type ();
284614f0 7906
dda83cd7 7907 if (dval0 == NULL)
b5304971 7908 {
012370f6
TT
7909 /* Using plain value_from_contents_and_address here
7910 causes problems because we will end up trying to
7911 resolve a type that is currently being
7912 constructed. */
7913 dval = value_from_contents_and_address_unresolved (rtype,
7914 valaddr,
7915 address);
d0c97917 7916 rtype = dval->type ();
b5304971 7917 }
dda83cd7
SM
7918 else
7919 dval = dval0;
4c4b4cd2 7920
284614f0
JB
7921 /* If the type referenced by this field is an aligner type, we need
7922 to unwrap that aligner type, because its size might not be set.
7923 Keeping the aligner type would cause us to compute the wrong
7924 size for this field, impacting the offset of the all the fields
7925 that follow this one. */
7926 if (ada_is_aligner_type (field_type))
7927 {
b610c045 7928 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7929
7930 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7931 field_address = cond_offset_target (field_address, field_offset);
7932 field_type = ada_aligned_type (field_type);
7933 }
7934
7935 field_valaddr = cond_offset_host (field_valaddr,
7936 off / TARGET_CHAR_BIT);
7937 field_address = cond_offset_target (field_address,
7938 off / TARGET_CHAR_BIT);
7939
7940 /* Get the fixed type of the field. Note that, in this case,
7941 we do not want to get the real type out of the tag: if
7942 the current field is the parent part of a tagged record,
7943 we will get the tag of the object. Clearly wrong: the real
7944 type of the parent is not the real type of the child. We
7945 would end up in an infinite loop. */
7946 field_type = ada_get_base_type (field_type);
7947 field_type = ada_to_fixed_type (field_type, field_valaddr,
7948 field_address, dval, 0);
7949
5d14b6e5 7950 rtype->field (f).set_type (field_type);
33d16dd9 7951 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7952 /* The multiplication can potentially overflow. But because
7953 the field length has been size-checked just above, and
7954 assuming that the maximum size is a reasonable value,
7955 an overflow should not happen in practice. So rather than
7956 adding overflow recovery code to this already complex code,
7957 we just assume that it's not going to happen. */
df86565b 7958 fld_bit_len = rtype->field (f).type ()->length () * TARGET_CHAR_BIT;
dda83cd7 7959 }
14f9c5c9 7960 else
dda83cd7 7961 {
5ded5331
JB
7962 /* Note: If this field's type is a typedef, it is important
7963 to preserve the typedef layer.
7964
7965 Otherwise, we might be transforming a typedef to a fat
7966 pointer (encoding a pointer to an unconstrained array),
7967 into a basic fat pointer (encoding an unconstrained
7968 array). As both types are implemented using the same
7969 structure, the typedef is the only clue which allows us
7970 to distinguish between the two options. Stripping it
7971 would prevent us from printing this field appropriately. */
dda83cd7 7972 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7973 rtype->field (f).set_name (type->field (f).name ());
dda83cd7
SM
7974 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7975 fld_bit_len =
7976 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7977 else
5ded5331 7978 {
940da03e 7979 struct type *field_type = type->field (f).type ();
5ded5331
JB
7980
7981 /* We need to be careful of typedefs when computing
7982 the length of our field. If this is a typedef,
7983 get the length of the target type, not the length
7984 of the typedef. */
78134374 7985 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7986 field_type = ada_typedef_target_type (field_type);
7987
dda83cd7 7988 fld_bit_len =
df86565b 7989 ada_check_typedef (field_type)->length () * TARGET_CHAR_BIT;
5ded5331 7990 }
dda83cd7 7991 }
14f9c5c9 7992 if (off + fld_bit_len > bit_len)
dda83cd7 7993 bit_len = off + fld_bit_len;
d94e4f4f 7994 off += fld_bit_len;
b6cdbc9a 7995 rtype->set_length (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
14f9c5c9 7996 }
4c4b4cd2
PH
7997
7998 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7999 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
8000 the record. This can happen in the presence of representation
8001 clauses. */
8002 if (variant_field >= 0)
8003 {
8004 struct type *branch_type;
8005
b610c045 8006 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
8007
8008 if (dval0 == NULL)
9f1f738a 8009 {
012370f6
TT
8010 /* Using plain value_from_contents_and_address here causes
8011 problems because we will end up trying to resolve a type
8012 that is currently being constructed. */
8013 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
8014 address);
d0c97917 8015 rtype = dval->type ();
9f1f738a 8016 }
4c4b4cd2 8017 else
dda83cd7 8018 dval = dval0;
4c4b4cd2
PH
8019
8020 branch_type =
dda83cd7
SM
8021 to_fixed_variant_branch_type
8022 (type->field (variant_field).type (),
8023 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
8024 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 8025 if (branch_type == NULL)
dda83cd7
SM
8026 {
8027 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
8028 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8029 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 8030 }
4c4b4cd2 8031 else
dda83cd7
SM
8032 {
8033 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8034 rtype->field (variant_field).set_name ("S");
dda83cd7 8035 fld_bit_len =
df86565b 8036 rtype->field (variant_field).type ()->length () * TARGET_CHAR_BIT;
dda83cd7
SM
8037 if (off + fld_bit_len > bit_len)
8038 bit_len = off + fld_bit_len;
b6cdbc9a
SM
8039
8040 rtype->set_length
8041 (align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT);
dda83cd7 8042 }
4c4b4cd2
PH
8043 }
8044
714e53ab
PH
8045 /* According to exp_dbug.ads, the size of TYPE for variable-size records
8046 should contain the alignment of that record, which should be a strictly
8047 positive value. If null or negative, then something is wrong, most
8048 probably in the debug info. In that case, we don't round up the size
0963b4bd 8049 of the resulting type. If this record is not part of another structure,
714e53ab 8050 the current RTYPE length might be good enough for our purposes. */
df86565b 8051 if (type->length () <= 0)
714e53ab 8052 {
7d93a1e0 8053 if (rtype->name ())
cc1defb1 8054 warning (_("Invalid type size for `%s' detected: %s."),
df86565b 8055 rtype->name (), pulongest (type->length ()));
323e0a4a 8056 else
cc1defb1 8057 warning (_("Invalid type size for <unnamed> detected: %s."),
df86565b 8058 pulongest (type->length ()));
714e53ab
PH
8059 }
8060 else
df86565b 8061 rtype->set_length (align_up (rtype->length (), type->length ()));
14f9c5c9 8062
14f9c5c9
AS
8063 return rtype;
8064}
8065
4c4b4cd2
PH
8066/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8067 of 1. */
14f9c5c9 8068
d2e4a39e 8069static struct type *
fc1a4b47 8070template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8071 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
8072{
8073 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 8074 address, dval0, 1);
4c4b4cd2
PH
8075}
8076
8077/* An ordinary record type in which ___XVL-convention fields and
8078 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8079 static approximations, containing all possible fields. Uses
8080 no runtime values. Useless for use in values, but that's OK,
8081 since the results are used only for type determinations. Works on both
8082 structs and unions. Representation note: to save space, we memorize
27710edb 8083 the result of this function in the type::target_type of the
4c4b4cd2
PH
8084 template type. */
8085
8086static struct type *
8087template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8088{
8089 struct type *type;
8090 int nfields;
8091 int f;
8092
9e195661 8093 /* No need no do anything if the input type is already fixed. */
22c4c60c 8094 if (type0->is_fixed_instance ())
9e195661
PMR
8095 return type0;
8096
8097 /* Likewise if we already have computed the static approximation. */
27710edb
SM
8098 if (type0->target_type () != NULL)
8099 return type0->target_type ();
4c4b4cd2 8100
9e195661 8101 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8102 type = type0;
1f704f76 8103 nfields = type0->num_fields ();
9e195661
PMR
8104
8105 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8106 recompute all over next time. */
8a50fdce 8107 type0->set_target_type (type);
14f9c5c9
AS
8108
8109 for (f = 0; f < nfields; f += 1)
8110 {
940da03e 8111 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8112 struct type *new_type;
14f9c5c9 8113
4c4b4cd2 8114 if (is_dynamic_field (type0, f))
460efde1
JB
8115 {
8116 field_type = ada_check_typedef (field_type);
27710edb 8117 new_type = to_static_fixed_type (field_type->target_type ());
460efde1 8118 }
14f9c5c9 8119 else
dda83cd7 8120 new_type = static_unwrap_type (field_type);
9e195661
PMR
8121
8122 if (new_type != field_type)
8123 {
8124 /* Clone TYPE0 only the first time we get a new field type. */
8125 if (type == type0)
8126 {
8a50fdce
SM
8127 type = alloc_type_copy (type0);
8128 type0->set_target_type (type);
78134374 8129 type->set_code (type0->code ());
8ecb59f8 8130 INIT_NONE_SPECIFIC (type);
5e33d5f4 8131 type->set_num_fields (nfields);
3cabb6b0
SM
8132
8133 field *fields =
8134 ((struct field *)
8135 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 8136 memcpy (fields, type0->fields (),
9e195661 8137 sizeof (struct field) * nfields);
3cabb6b0
SM
8138 type->set_fields (fields);
8139
d0e39ea2 8140 type->set_name (ada_type_name (type0));
9cdd0d12 8141 type->set_is_fixed_instance (true);
b6cdbc9a 8142 type->set_length (0);
9e195661 8143 }
5d14b6e5 8144 type->field (f).set_type (new_type);
33d16dd9 8145 type->field (f).set_name (type0->field (f).name ());
9e195661 8146 }
14f9c5c9 8147 }
9e195661 8148
14f9c5c9
AS
8149 return type;
8150}
8151
4c4b4cd2 8152/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8153 whose address in memory is ADDRESS, returns a revision of TYPE,
8154 which should be a non-dynamic-sized record, in which the variant
8155 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8156 for discriminant values in DVAL0, which can be NULL if the record
8157 contains the necessary discriminant values. */
8158
d2e4a39e 8159static struct type *
fc1a4b47 8160to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8161 CORE_ADDR address, struct value *dval0)
14f9c5c9 8162{
4c4b4cd2 8163 struct value *dval;
d2e4a39e 8164 struct type *rtype;
14f9c5c9 8165 struct type *branch_type;
1f704f76 8166 int nfields = type->num_fields ();
4c4b4cd2 8167 int variant_field = variant_field_index (type);
14f9c5c9 8168
4c4b4cd2 8169 if (variant_field == -1)
14f9c5c9
AS
8170 return type;
8171
65558ca5 8172 scoped_value_mark mark;
4c4b4cd2 8173 if (dval0 == NULL)
9f1f738a
SA
8174 {
8175 dval = value_from_contents_and_address (type, valaddr, address);
d0c97917 8176 type = dval->type ();
9f1f738a 8177 }
4c4b4cd2
PH
8178 else
8179 dval = dval0;
8180
e9bb382b 8181 rtype = alloc_type_copy (type);
67607e24 8182 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8183 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 8184 rtype->set_num_fields (nfields);
3cabb6b0
SM
8185
8186 field *fields =
d2e4a39e 8187 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 8188 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
8189 rtype->set_fields (fields);
8190
d0e39ea2 8191 rtype->set_name (ada_type_name (type));
9cdd0d12 8192 rtype->set_is_fixed_instance (true);
df86565b 8193 rtype->set_length (type->length ());
14f9c5c9 8194
4c4b4cd2 8195 branch_type = to_fixed_variant_branch_type
940da03e 8196 (type->field (variant_field).type (),
d2e4a39e 8197 cond_offset_host (valaddr,
b610c045 8198 type->field (variant_field).loc_bitpos ()
dda83cd7 8199 / TARGET_CHAR_BIT),
d2e4a39e 8200 cond_offset_target (address,
b610c045 8201 type->field (variant_field).loc_bitpos ()
dda83cd7 8202 / TARGET_CHAR_BIT), dval);
d2e4a39e 8203 if (branch_type == NULL)
14f9c5c9 8204 {
4c4b4cd2 8205 int f;
5b4ee69b 8206
4c4b4cd2 8207 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8208 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8209 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8210 }
8211 else
8212 {
5d14b6e5 8213 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8214 rtype->field (variant_field).set_name ("S");
4c4b4cd2 8215 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
df86565b 8216 rtype->set_length (rtype->length () + branch_type->length ());
14f9c5c9 8217 }
b6cdbc9a 8218
df86565b
SM
8219 rtype->set_length (rtype->length ()
8220 - type->field (variant_field).type ()->length ());
d2e4a39e 8221
14f9c5c9
AS
8222 return rtype;
8223}
8224
8225/* An ordinary record type (with fixed-length fields) that describes
8226 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8227 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8228 should be in DVAL, a record value; it may be NULL if the object
8229 at ADDR itself contains any necessary discriminant values.
8230 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8231 values from the record are needed. Except in the case that DVAL,
8232 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8233 unchecked) is replaced by a particular branch of the variant.
8234
8235 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8236 is questionable and may be removed. It can arise during the
8237 processing of an unconstrained-array-of-record type where all the
8238 variant branches have exactly the same size. This is because in
8239 such cases, the compiler does not bother to use the XVS convention
8240 when encoding the record. I am currently dubious of this
8241 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8242
d2e4a39e 8243static struct type *
fc1a4b47 8244to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8245 CORE_ADDR address, struct value *dval)
14f9c5c9 8246{
d2e4a39e 8247 struct type *templ_type;
14f9c5c9 8248
22c4c60c 8249 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8250 return type0;
8251
d2e4a39e 8252 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8253
8254 if (templ_type != NULL)
8255 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8256 else if (variant_field_index (type0) >= 0)
8257 {
8258 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8259 return type0;
4c4b4cd2 8260 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8261 dval);
4c4b4cd2 8262 }
14f9c5c9
AS
8263 else
8264 {
9cdd0d12 8265 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8266 return type0;
8267 }
8268
8269}
8270
8271/* An ordinary record type (with fixed-length fields) that describes
8272 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8273 union type. Any necessary discriminants' values should be in DVAL,
8274 a record value. That is, this routine selects the appropriate
8275 branch of the union at ADDR according to the discriminant value
b1f33ddd 8276 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8277 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8278
d2e4a39e 8279static struct type *
fc1a4b47 8280to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8281 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8282{
8283 int which;
d2e4a39e
AS
8284 struct type *templ_type;
8285 struct type *var_type;
14f9c5c9 8286
78134374 8287 if (var_type0->code () == TYPE_CODE_PTR)
27710edb 8288 var_type = var_type0->target_type ();
d2e4a39e 8289 else
14f9c5c9
AS
8290 var_type = var_type0;
8291
8292 templ_type = ada_find_parallel_type (var_type, "___XVU");
8293
8294 if (templ_type != NULL)
8295 var_type = templ_type;
8296
d0c97917 8297 if (is_unchecked_variant (var_type, dval->type ()))
b1f33ddd 8298 return var_type0;
d8af9068 8299 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8300
8301 if (which < 0)
e9bb382b 8302 return empty_record (var_type);
14f9c5c9 8303 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8304 return to_fixed_record_type
27710edb 8305 (var_type->field (which).type ()->target_type(), valaddr, address, dval);
940da03e 8306 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8307 return
8308 to_fixed_record_type
940da03e 8309 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8310 else
940da03e 8311 return var_type->field (which).type ();
14f9c5c9
AS
8312}
8313
8908fca5
JB
8314/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8315 ENCODING_TYPE, a type following the GNAT conventions for discrete
8316 type encodings, only carries redundant information. */
8317
8318static int
8319ada_is_redundant_range_encoding (struct type *range_type,
8320 struct type *encoding_type)
8321{
108d56a4 8322 const char *bounds_str;
8908fca5
JB
8323 int n;
8324 LONGEST lo, hi;
8325
78134374 8326 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8327
78134374
SM
8328 if (get_base_type (range_type)->code ()
8329 != get_base_type (encoding_type)->code ())
005e2509
JB
8330 {
8331 /* The compiler probably used a simple base type to describe
8332 the range type instead of the range's actual base type,
8333 expecting us to get the real base type from the encoding
8334 anyway. In this situation, the encoding cannot be ignored
8335 as redundant. */
8336 return 0;
8337 }
8338
8908fca5
JB
8339 if (is_dynamic_type (range_type))
8340 return 0;
8341
7d93a1e0 8342 if (encoding_type->name () == NULL)
8908fca5
JB
8343 return 0;
8344
7d93a1e0 8345 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8346 if (bounds_str == NULL)
8347 return 0;
8348
8349 n = 8; /* Skip "___XDLU_". */
8350 if (!ada_scan_number (bounds_str, n, &lo, &n))
8351 return 0;
5537ddd0 8352 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8353 return 0;
8354
8355 n += 2; /* Skip the "__" separator between the two bounds. */
8356 if (!ada_scan_number (bounds_str, n, &hi, &n))
8357 return 0;
5537ddd0 8358 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8359 return 0;
8360
8361 return 1;
8362}
8363
8364/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8365 a type following the GNAT encoding for describing array type
8366 indices, only carries redundant information. */
8367
8368static int
8369ada_is_redundant_index_type_desc (struct type *array_type,
8370 struct type *desc_type)
8371{
8372 struct type *this_layer = check_typedef (array_type);
8373 int i;
8374
1f704f76 8375 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8376 {
3d967001 8377 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8378 desc_type->field (i).type ()))
8908fca5 8379 return 0;
27710edb 8380 this_layer = check_typedef (this_layer->target_type ());
8908fca5
JB
8381 }
8382
8383 return 1;
8384}
8385
14f9c5c9
AS
8386/* Assuming that TYPE0 is an array type describing the type of a value
8387 at ADDR, and that DVAL describes a record containing any
8388 discriminants used in TYPE0, returns a type for the value that
8389 contains no dynamic components (that is, no components whose sizes
8390 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8391 true, gives an error message if the resulting type's size is over
4c4b4cd2 8392 varsize_limit. */
14f9c5c9 8393
d2e4a39e
AS
8394static struct type *
8395to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8396 int ignore_too_big)
14f9c5c9 8397{
d2e4a39e
AS
8398 struct type *index_type_desc;
8399 struct type *result;
ad82864c 8400 int constrained_packed_array_p;
931e5bc3 8401 static const char *xa_suffix = "___XA";
14f9c5c9 8402
b0dd7688 8403 type0 = ada_check_typedef (type0);
22c4c60c 8404 if (type0->is_fixed_instance ())
4c4b4cd2 8405 return type0;
14f9c5c9 8406
ad82864c
JB
8407 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8408 if (constrained_packed_array_p)
75fd6a26
TT
8409 {
8410 type0 = decode_constrained_packed_array_type (type0);
8411 if (type0 == nullptr)
8412 error (_("could not decode constrained packed array type"));
8413 }
284614f0 8414
931e5bc3
JG
8415 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8416
8417 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8418 encoding suffixed with 'P' may still be generated. If so,
8419 it should be used to find the XA type. */
8420
8421 if (index_type_desc == NULL)
8422 {
1da0522e 8423 const char *type_name = ada_type_name (type0);
931e5bc3 8424
1da0522e 8425 if (type_name != NULL)
931e5bc3 8426 {
1da0522e 8427 const int len = strlen (type_name);
931e5bc3
JG
8428 char *name = (char *) alloca (len + strlen (xa_suffix));
8429
1da0522e 8430 if (type_name[len - 1] == 'P')
931e5bc3 8431 {
1da0522e 8432 strcpy (name, type_name);
931e5bc3
JG
8433 strcpy (name + len - 1, xa_suffix);
8434 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8435 }
8436 }
8437 }
8438
28c85d6c 8439 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8440 if (index_type_desc != NULL
8441 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8442 {
8443 /* Ignore this ___XA parallel type, as it does not bring any
8444 useful information. This allows us to avoid creating fixed
8445 versions of the array's index types, which would be identical
8446 to the original ones. This, in turn, can also help avoid
8447 the creation of fixed versions of the array itself. */
8448 index_type_desc = NULL;
8449 }
8450
14f9c5c9
AS
8451 if (index_type_desc == NULL)
8452 {
27710edb 8453 struct type *elt_type0 = ada_check_typedef (type0->target_type ());
5b4ee69b 8454
14f9c5c9 8455 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8456 depend on the contents of the array in properly constructed
8457 debugging data. */
529cad9c 8458 /* Create a fixed version of the array element type.
dda83cd7
SM
8459 We're not providing the address of an element here,
8460 and thus the actual object value cannot be inspected to do
8461 the conversion. This should not be a problem, since arrays of
8462 unconstrained objects are not allowed. In particular, all
8463 the elements of an array of a tagged type should all be of
8464 the same type specified in the debugging info. No need to
8465 consult the object tag. */
1ed6ede0 8466 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8467
284614f0
JB
8468 /* Make sure we always create a new array type when dealing with
8469 packed array types, since we're going to fix-up the array
8470 type length and element bitsize a little further down. */
ad82864c 8471 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8472 result = type0;
14f9c5c9 8473 else
dda83cd7
SM
8474 result = create_array_type (alloc_type_copy (type0),
8475 elt_type, type0->index_type ());
14f9c5c9
AS
8476 }
8477 else
8478 {
8479 int i;
8480 struct type *elt_type0;
8481
8482 elt_type0 = type0;
1f704f76 8483 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
27710edb 8484 elt_type0 = elt_type0->target_type ();
14f9c5c9
AS
8485
8486 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8487 depend on the contents of the array in properly constructed
8488 debugging data. */
529cad9c 8489 /* Create a fixed version of the array element type.
dda83cd7
SM
8490 We're not providing the address of an element here,
8491 and thus the actual object value cannot be inspected to do
8492 the conversion. This should not be a problem, since arrays of
8493 unconstrained objects are not allowed. In particular, all
8494 the elements of an array of a tagged type should all be of
8495 the same type specified in the debugging info. No need to
8496 consult the object tag. */
1ed6ede0 8497 result =
dda83cd7 8498 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8499
8500 elt_type0 = type0;
1f704f76 8501 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8502 {
8503 struct type *range_type =
8504 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8505
dda83cd7
SM
8506 result = create_array_type (alloc_type_copy (elt_type0),
8507 result, range_type);
27710edb 8508 elt_type0 = elt_type0->target_type ();
dda83cd7 8509 }
14f9c5c9
AS
8510 }
8511
2e6fda7d
JB
8512 /* We want to preserve the type name. This can be useful when
8513 trying to get the type name of a value that has already been
8514 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8515 result->set_name (type0->name ());
2e6fda7d 8516
ad82864c 8517 if (constrained_packed_array_p)
284614f0
JB
8518 {
8519 /* So far, the resulting type has been created as if the original
8520 type was a regular (non-packed) array type. As a result, the
8521 bitsize of the array elements needs to be set again, and the array
8522 length needs to be recomputed based on that bitsize. */
df86565b 8523 int len = result->length () / result->target_type ()->length ();
284614f0
JB
8524 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8525
8526 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
b6cdbc9a 8527 result->set_length (len * elt_bitsize / HOST_CHAR_BIT);
df86565b
SM
8528 if (result->length () * HOST_CHAR_BIT < len * elt_bitsize)
8529 result->set_length (result->length () + 1);
284614f0
JB
8530 }
8531
9cdd0d12 8532 result->set_is_fixed_instance (true);
14f9c5c9 8533 return result;
d2e4a39e 8534}
14f9c5c9
AS
8535
8536
8537/* A standard type (containing no dynamically sized components)
8538 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8539 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8540 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8541 ADDRESS or in VALADDR contains these discriminants.
8542
1ed6ede0
JB
8543 If CHECK_TAG is not null, in the case of tagged types, this function
8544 attempts to locate the object's tag and use it to compute the actual
8545 type. However, when ADDRESS is null, we cannot use it to determine the
8546 location of the tag, and therefore compute the tagged type's actual type.
8547 So we return the tagged type without consulting the tag. */
529cad9c 8548
f192137b
JB
8549static struct type *
8550ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8551 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8552{
61ee279c 8553 type = ada_check_typedef (type);
8ecb59f8
TT
8554
8555 /* Only un-fixed types need to be handled here. */
8556 if (!HAVE_GNAT_AUX_INFO (type))
8557 return type;
8558
78134374 8559 switch (type->code ())
d2e4a39e
AS
8560 {
8561 default:
14f9c5c9 8562 return type;
d2e4a39e 8563 case TYPE_CODE_STRUCT:
4c4b4cd2 8564 {
dda83cd7
SM
8565 struct type *static_type = to_static_fixed_type (type);
8566 struct type *fixed_record_type =
8567 to_fixed_record_type (type, valaddr, address, NULL);
8568
8569 /* If STATIC_TYPE is a tagged type and we know the object's address,
8570 then we can determine its tag, and compute the object's actual
8571 type from there. Note that we have to use the fixed record
8572 type (the parent part of the record may have dynamic fields
8573 and the way the location of _tag is expressed may depend on
8574 them). */
8575
8576 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8577 {
b50d69b5
JG
8578 struct value *tag =
8579 value_tag_from_contents_and_address
8580 (fixed_record_type,
8581 valaddr,
8582 address);
8583 struct type *real_type = type_from_tag (tag);
8584 struct value *obj =
8585 value_from_contents_and_address (fixed_record_type,
8586 valaddr,
8587 address);
d0c97917 8588 fixed_record_type = obj->type ();
dda83cd7
SM
8589 if (real_type != NULL)
8590 return to_fixed_record_type
b50d69b5 8591 (real_type, NULL,
9feb2d07 8592 ada_tag_value_at_base_address (obj)->address (), NULL);
dda83cd7
SM
8593 }
8594
8595 /* Check to see if there is a parallel ___XVZ variable.
8596 If there is, then it provides the actual size of our type. */
8597 else if (ada_type_name (fixed_record_type) != NULL)
8598 {
8599 const char *name = ada_type_name (fixed_record_type);
8600 char *xvz_name
224c3ddb 8601 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8602 bool xvz_found = false;
dda83cd7 8603 LONGEST size;
4af88198 8604
dda83cd7 8605 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8606 try
eccab96d
JB
8607 {
8608 xvz_found = get_int_var_value (xvz_name, size);
8609 }
230d2906 8610 catch (const gdb_exception_error &except)
eccab96d
JB
8611 {
8612 /* We found the variable, but somehow failed to read
8613 its value. Rethrow the same error, but with a little
8614 bit more information, to help the user understand
8615 what went wrong (Eg: the variable might have been
8616 optimized out). */
8617 throw_error (except.error,
8618 _("unable to read value of %s (%s)"),
3d6e9d23 8619 xvz_name, except.what ());
eccab96d 8620 }
eccab96d 8621
df86565b 8622 if (xvz_found && fixed_record_type->length () != size)
dda83cd7
SM
8623 {
8624 fixed_record_type = copy_type (fixed_record_type);
b6cdbc9a 8625 fixed_record_type->set_length (size);
dda83cd7
SM
8626
8627 /* The FIXED_RECORD_TYPE may have be a stub. We have
8628 observed this when the debugging info is STABS, and
8629 apparently it is something that is hard to fix.
8630
8631 In practice, we don't need the actual type definition
8632 at all, because the presence of the XVZ variable allows us
8633 to assume that there must be a XVS type as well, which we
8634 should be able to use later, when we need the actual type
8635 definition.
8636
8637 In the meantime, pretend that the "fixed" type we are
8638 returning is NOT a stub, because this can cause trouble
8639 when using this type to create new types targeting it.
8640 Indeed, the associated creation routines often check
8641 whether the target type is a stub and will try to replace
8642 it, thus using a type with the wrong size. This, in turn,
8643 might cause the new type to have the wrong size too.
8644 Consider the case of an array, for instance, where the size
8645 of the array is computed from the number of elements in
8646 our array multiplied by the size of its element. */
b4b73759 8647 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8648 }
8649 }
8650 return fixed_record_type;
4c4b4cd2 8651 }
d2e4a39e 8652 case TYPE_CODE_ARRAY:
4c4b4cd2 8653 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8654 case TYPE_CODE_UNION:
8655 if (dval == NULL)
dda83cd7 8656 return type;
d2e4a39e 8657 else
dda83cd7 8658 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8659 }
14f9c5c9
AS
8660}
8661
f192137b
JB
8662/* The same as ada_to_fixed_type_1, except that it preserves the type
8663 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8664
8665 The typedef layer needs be preserved in order to differentiate between
8666 arrays and array pointers when both types are implemented using the same
8667 fat pointer. In the array pointer case, the pointer is encoded as
8668 a typedef of the pointer type. For instance, considering:
8669
8670 type String_Access is access String;
8671 S1 : String_Access := null;
8672
8673 To the debugger, S1 is defined as a typedef of type String. But
8674 to the user, it is a pointer. So if the user tries to print S1,
8675 we should not dereference the array, but print the array address
8676 instead.
8677
8678 If we didn't preserve the typedef layer, we would lose the fact that
8679 the type is to be presented as a pointer (needs de-reference before
8680 being printed). And we would also use the source-level type name. */
f192137b
JB
8681
8682struct type *
8683ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8684 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8685
8686{
8687 struct type *fixed_type =
8688 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8689
96dbd2c1
JB
8690 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8691 then preserve the typedef layer.
8692
8693 Implementation note: We can only check the main-type portion of
8694 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8695 from TYPE now returns a type that has the same instance flags
8696 as TYPE. For instance, if TYPE is a "typedef const", and its
8697 target type is a "struct", then the typedef elimination will return
8698 a "const" version of the target type. See check_typedef for more
8699 details about how the typedef layer elimination is done.
8700
8701 brobecker/2010-11-19: It seems to me that the only case where it is
8702 useful to preserve the typedef layer is when dealing with fat pointers.
8703 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8704 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8705 because we call check_typedef/ada_check_typedef pretty much everywhere.
8706 */
78134374 8707 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8708 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8709 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8710 return type;
8711
8712 return fixed_type;
8713}
8714
14f9c5c9 8715/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8716 TYPE0, but based on no runtime data. */
14f9c5c9 8717
d2e4a39e
AS
8718static struct type *
8719to_static_fixed_type (struct type *type0)
14f9c5c9 8720{
d2e4a39e 8721 struct type *type;
14f9c5c9
AS
8722
8723 if (type0 == NULL)
8724 return NULL;
8725
22c4c60c 8726 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8727 return type0;
8728
61ee279c 8729 type0 = ada_check_typedef (type0);
d2e4a39e 8730
78134374 8731 switch (type0->code ())
14f9c5c9
AS
8732 {
8733 default:
8734 return type0;
8735 case TYPE_CODE_STRUCT:
8736 type = dynamic_template_type (type0);
d2e4a39e 8737 if (type != NULL)
dda83cd7 8738 return template_to_static_fixed_type (type);
4c4b4cd2 8739 else
dda83cd7 8740 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8741 case TYPE_CODE_UNION:
8742 type = ada_find_parallel_type (type0, "___XVU");
8743 if (type != NULL)
dda83cd7 8744 return template_to_static_fixed_type (type);
4c4b4cd2 8745 else
dda83cd7 8746 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8747 }
8748}
8749
4c4b4cd2
PH
8750/* A static approximation of TYPE with all type wrappers removed. */
8751
d2e4a39e
AS
8752static struct type *
8753static_unwrap_type (struct type *type)
14f9c5c9
AS
8754{
8755 if (ada_is_aligner_type (type))
8756 {
940da03e 8757 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8758 if (ada_type_name (type1) == NULL)
d0e39ea2 8759 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8760
8761 return static_unwrap_type (type1);
8762 }
d2e4a39e 8763 else
14f9c5c9 8764 {
d2e4a39e 8765 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8766
d2e4a39e 8767 if (raw_real_type == type)
dda83cd7 8768 return type;
14f9c5c9 8769 else
dda83cd7 8770 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8771 }
8772}
8773
8774/* In some cases, incomplete and private types require
4c4b4cd2 8775 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8776 type Foo;
8777 type FooP is access Foo;
8778 V: FooP;
8779 type Foo is array ...;
4c4b4cd2 8780 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8781 cross-references to such types, we instead substitute for FooP a
8782 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8783 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8784
8785/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8786 exists, otherwise TYPE. */
8787
d2e4a39e 8788struct type *
61ee279c 8789ada_check_typedef (struct type *type)
14f9c5c9 8790{
727e3d2e
JB
8791 if (type == NULL)
8792 return NULL;
8793
736ade86
XR
8794 /* If our type is an access to an unconstrained array, which is encoded
8795 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8796 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8797 what allows us to distinguish between fat pointers that represent
8798 array types, and fat pointers that represent array access types
8799 (in both cases, the compiler implements them as fat pointers). */
736ade86 8800 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8801 return type;
8802
f168693b 8803 type = check_typedef (type);
78134374 8804 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8805 || !type->is_stub ()
7d93a1e0 8806 || type->name () == NULL)
14f9c5c9 8807 return type;
d2e4a39e 8808 else
14f9c5c9 8809 {
7d93a1e0 8810 const char *name = type->name ();
d2e4a39e 8811 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8812
05e522ef 8813 if (type1 == NULL)
dda83cd7 8814 return type;
05e522ef
JB
8815
8816 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8817 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8818 types, only for the typedef-to-array types). If that's the case,
8819 strip the typedef layer. */
78134374 8820 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8821 type1 = ada_check_typedef (type1);
8822
8823 return type1;
14f9c5c9
AS
8824 }
8825}
8826
8827/* A value representing the data at VALADDR/ADDRESS as described by
8828 type TYPE0, but with a standard (static-sized) type that correctly
8829 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8830 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8831 creation of struct values]. */
14f9c5c9 8832
4c4b4cd2
PH
8833static struct value *
8834ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8835 struct value *val0)
14f9c5c9 8836{
1ed6ede0 8837 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8838
14f9c5c9
AS
8839 if (type == type0 && val0 != NULL)
8840 return val0;
cc0e770c 8841
736355f2 8842 if (val0->lval () != lval_memory)
cc0e770c
JB
8843 {
8844 /* Our value does not live in memory; it could be a convenience
8845 variable, for instance. Create a not_lval value using val0's
8846 contents. */
efaf1ae0 8847 return value_from_contents (type, val0->contents ().data ());
cc0e770c
JB
8848 }
8849
8850 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8851}
8852
8853/* A value representing VAL, but with a standard (static-sized) type
8854 that correctly describes it. Does not necessarily create a new
8855 value. */
8856
0c3acc09 8857struct value *
4c4b4cd2
PH
8858ada_to_fixed_value (struct value *val)
8859{
c48db5ca 8860 val = unwrap_value (val);
9feb2d07 8861 val = ada_to_fixed_value_create (val->type (), val->address (), val);
c48db5ca 8862 return val;
14f9c5c9 8863}
d2e4a39e 8864\f
14f9c5c9 8865
14f9c5c9
AS
8866/* Attributes */
8867
4c4b4cd2
PH
8868/* Table mapping attribute numbers to names.
8869 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8870
27087b7f 8871static const char * const attribute_names[] = {
14f9c5c9
AS
8872 "<?>",
8873
d2e4a39e 8874 "first",
14f9c5c9
AS
8875 "last",
8876 "length",
8877 "image",
14f9c5c9
AS
8878 "max",
8879 "min",
4c4b4cd2
PH
8880 "modulus",
8881 "pos",
8882 "size",
8883 "tag",
14f9c5c9 8884 "val",
14f9c5c9
AS
8885 0
8886};
8887
de93309a 8888static const char *
4c4b4cd2 8889ada_attribute_name (enum exp_opcode n)
14f9c5c9 8890{
4c4b4cd2
PH
8891 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8892 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8893 else
8894 return attribute_names[0];
8895}
8896
4c4b4cd2 8897/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8898
4c4b4cd2
PH
8899static LONGEST
8900pos_atr (struct value *arg)
14f9c5c9 8901{
24209737 8902 struct value *val = coerce_ref (arg);
d0c97917 8903 struct type *type = val->type ();
14f9c5c9 8904
d2e4a39e 8905 if (!discrete_type_p (type))
323e0a4a 8906 error (_("'POS only defined on discrete types"));
14f9c5c9 8907
6244c119
SM
8908 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8909 if (!result.has_value ())
aa715135 8910 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8911
6244c119 8912 return *result;
4c4b4cd2
PH
8913}
8914
7631cf6c 8915struct value *
7992accc
TT
8916ada_pos_atr (struct type *expect_type,
8917 struct expression *exp,
8918 enum noside noside, enum exp_opcode op,
8919 struct value *arg)
4c4b4cd2 8920{
7992accc
TT
8921 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8922 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8923 return value::zero (type, not_lval);
3cb382c9 8924 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8925}
8926
4c4b4cd2 8927/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8928
d2e4a39e 8929static struct value *
53a47a3e 8930val_atr (struct type *type, LONGEST val)
14f9c5c9 8931{
53a47a3e 8932 gdb_assert (discrete_type_p (type));
0bc2354b 8933 if (type->code () == TYPE_CODE_RANGE)
27710edb 8934 type = type->target_type ();
78134374 8935 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8936 {
53a47a3e 8937 if (val < 0 || val >= type->num_fields ())
dda83cd7 8938 error (_("argument to 'VAL out of range"));
970db518 8939 val = type->field (val).loc_enumval ();
14f9c5c9 8940 }
53a47a3e
TT
8941 return value_from_longest (type, val);
8942}
8943
9e99f48f 8944struct value *
3848abd6 8945ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8946{
3848abd6 8947 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 8948 return value::zero (type, not_lval);
3848abd6 8949
53a47a3e
TT
8950 if (!discrete_type_p (type))
8951 error (_("'VAL only defined on discrete types"));
d0c97917 8952 if (!integer_type_p (arg->type ()))
53a47a3e
TT
8953 error (_("'VAL requires integral argument"));
8954
8955 return val_atr (type, value_as_long (arg));
14f9c5c9 8956}
14f9c5c9 8957\f
d2e4a39e 8958
dda83cd7 8959 /* Evaluation */
14f9c5c9 8960
4c4b4cd2
PH
8961/* True if TYPE appears to be an Ada character type.
8962 [At the moment, this is true only for Character and Wide_Character;
8963 It is a heuristic test that could stand improvement]. */
14f9c5c9 8964
fc913e53 8965bool
d2e4a39e 8966ada_is_character_type (struct type *type)
14f9c5c9 8967{
7b9f71f2
JB
8968 const char *name;
8969
8970 /* If the type code says it's a character, then assume it really is,
8971 and don't check any further. */
78134374 8972 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8973 return true;
7b9f71f2
JB
8974
8975 /* Otherwise, assume it's a character type iff it is a discrete type
8976 with a known character type name. */
8977 name = ada_type_name (type);
8978 return (name != NULL
dda83cd7
SM
8979 && (type->code () == TYPE_CODE_INT
8980 || type->code () == TYPE_CODE_RANGE)
8981 && (strcmp (name, "character") == 0
8982 || strcmp (name, "wide_character") == 0
8983 || strcmp (name, "wide_wide_character") == 0
8984 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8985}
8986
4c4b4cd2 8987/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8988
fc913e53 8989bool
ebf56fd3 8990ada_is_string_type (struct type *type)
14f9c5c9 8991{
61ee279c 8992 type = ada_check_typedef (type);
d2e4a39e 8993 if (type != NULL
78134374 8994 && type->code () != TYPE_CODE_PTR
76a01679 8995 && (ada_is_simple_array_type (type)
dda83cd7 8996 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8997 && ada_array_arity (type) == 1)
8998 {
8999 struct type *elttype = ada_array_element_type (type, 1);
9000
9001 return ada_is_character_type (elttype);
9002 }
d2e4a39e 9003 else
fc913e53 9004 return false;
14f9c5c9
AS
9005}
9006
5bf03f13
JB
9007/* The compiler sometimes provides a parallel XVS type for a given
9008 PAD type. Normally, it is safe to follow the PAD type directly,
9009 but older versions of the compiler have a bug that causes the offset
9010 of its "F" field to be wrong. Following that field in that case
9011 would lead to incorrect results, but this can be worked around
9012 by ignoring the PAD type and using the associated XVS type instead.
9013
9014 Set to True if the debugger should trust the contents of PAD types.
9015 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 9016static bool trust_pad_over_xvs = true;
14f9c5c9
AS
9017
9018/* True if TYPE is a struct type introduced by the compiler to force the
9019 alignment of a value. Such types have a single field with a
4c4b4cd2 9020 distinctive name. */
14f9c5c9
AS
9021
9022int
ebf56fd3 9023ada_is_aligner_type (struct type *type)
14f9c5c9 9024{
61ee279c 9025 type = ada_check_typedef (type);
714e53ab 9026
5bf03f13 9027 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
9028 return 0;
9029
78134374 9030 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 9031 && type->num_fields () == 1
33d16dd9 9032 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
9033}
9034
9035/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 9036 the parallel type. */
14f9c5c9 9037
d2e4a39e
AS
9038struct type *
9039ada_get_base_type (struct type *raw_type)
14f9c5c9 9040{
d2e4a39e
AS
9041 struct type *real_type_namer;
9042 struct type *raw_real_type;
14f9c5c9 9043
78134374 9044 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
9045 return raw_type;
9046
284614f0
JB
9047 if (ada_is_aligner_type (raw_type))
9048 /* The encoding specifies that we should always use the aligner type.
9049 So, even if this aligner type has an associated XVS type, we should
9050 simply ignore it.
9051
9052 According to the compiler gurus, an XVS type parallel to an aligner
9053 type may exist because of a stabs limitation. In stabs, aligner
9054 types are empty because the field has a variable-sized type, and
9055 thus cannot actually be used as an aligner type. As a result,
9056 we need the associated parallel XVS type to decode the type.
9057 Since the policy in the compiler is to not change the internal
9058 representation based on the debugging info format, we sometimes
9059 end up having a redundant XVS type parallel to the aligner type. */
9060 return raw_type;
9061
14f9c5c9 9062 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9063 if (real_type_namer == NULL
78134374 9064 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 9065 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
9066 return raw_type;
9067
940da03e 9068 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
9069 {
9070 /* This is an older encoding form where the base type needs to be
85102364 9071 looked up by name. We prefer the newer encoding because it is
f80d3ff2 9072 more efficient. */
33d16dd9 9073 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
9074 if (raw_real_type == NULL)
9075 return raw_type;
9076 else
9077 return raw_real_type;
9078 }
9079
9080 /* The field in our XVS type is a reference to the base type. */
27710edb 9081 return real_type_namer->field (0).type ()->target_type ();
d2e4a39e 9082}
14f9c5c9 9083
4c4b4cd2 9084/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9085
d2e4a39e
AS
9086struct type *
9087ada_aligned_type (struct type *type)
14f9c5c9
AS
9088{
9089 if (ada_is_aligner_type (type))
940da03e 9090 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
9091 else
9092 return ada_get_base_type (type);
9093}
9094
9095
9096/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9097 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9098
fc1a4b47
AC
9099const gdb_byte *
9100ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9101{
d2e4a39e 9102 if (ada_is_aligner_type (type))
b610c045
SM
9103 return ada_aligned_value_addr
9104 (type->field (0).type (),
9105 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
9106 else
9107 return valaddr;
9108}
9109
4c4b4cd2
PH
9110
9111
14f9c5c9 9112/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9113 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9114const char *
9115ada_enum_name (const char *name)
14f9c5c9 9116{
5f9febe0 9117 static std::string storage;
e6a959d6 9118 const char *tmp;
14f9c5c9 9119
4c4b4cd2
PH
9120 /* First, unqualify the enumeration name:
9121 1. Search for the last '.' character. If we find one, then skip
177b42fe 9122 all the preceding characters, the unqualified name starts
76a01679 9123 right after that dot.
4c4b4cd2 9124 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9125 translates dots into "__". Search forward for double underscores,
9126 but stop searching when we hit an overloading suffix, which is
9127 of the form "__" followed by digits. */
4c4b4cd2 9128
c3e5cd34
PH
9129 tmp = strrchr (name, '.');
9130 if (tmp != NULL)
4c4b4cd2
PH
9131 name = tmp + 1;
9132 else
14f9c5c9 9133 {
4c4b4cd2 9134 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9135 {
9136 if (isdigit (tmp[2]))
9137 break;
9138 else
9139 name = tmp + 2;
9140 }
14f9c5c9
AS
9141 }
9142
9143 if (name[0] == 'Q')
9144 {
14f9c5c9 9145 int v;
5b4ee69b 9146
14f9c5c9 9147 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9148 {
a7041de8
TT
9149 int offset = 2;
9150 if (name[1] == 'W' && name[2] == 'W')
9151 {
9152 /* Also handle the QWW case. */
9153 ++offset;
9154 }
9155 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9156 return name;
9157 }
272560b5
TT
9158 else if (((name[1] >= '0' && name[1] <= '9')
9159 || (name[1] >= 'a' && name[1] <= 'z'))
9160 && name[2] == '\0')
9161 {
5f9febe0
TT
9162 storage = string_printf ("'%c'", name[1]);
9163 return storage.c_str ();
272560b5 9164 }
14f9c5c9 9165 else
dda83cd7 9166 return name;
14f9c5c9
AS
9167
9168 if (isascii (v) && isprint (v))
5f9febe0 9169 storage = string_printf ("'%c'", v);
14f9c5c9 9170 else if (name[1] == 'U')
a7041de8
TT
9171 storage = string_printf ("'[\"%02x\"]'", v);
9172 else if (name[2] != 'W')
9173 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9174 else
a7041de8 9175 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9176
5f9febe0 9177 return storage.c_str ();
14f9c5c9 9178 }
d2e4a39e 9179 else
4c4b4cd2 9180 {
c3e5cd34
PH
9181 tmp = strstr (name, "__");
9182 if (tmp == NULL)
9183 tmp = strstr (name, "$");
9184 if (tmp != NULL)
dda83cd7 9185 {
5f9febe0
TT
9186 storage = std::string (name, tmp - name);
9187 return storage.c_str ();
dda83cd7 9188 }
4c4b4cd2
PH
9189
9190 return name;
9191 }
14f9c5c9
AS
9192}
9193
14f9c5c9 9194/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9195 value it wraps. */
14f9c5c9 9196
d2e4a39e
AS
9197static struct value *
9198unwrap_value (struct value *val)
14f9c5c9 9199{
d0c97917 9200 struct type *type = ada_check_typedef (val->type ());
5b4ee69b 9201
14f9c5c9
AS
9202 if (ada_is_aligner_type (type))
9203 {
de4d072f 9204 struct value *v = ada_value_struct_elt (val, "F", 0);
d0c97917 9205 struct type *val_type = ada_check_typedef (v->type ());
5b4ee69b 9206
14f9c5c9 9207 if (ada_type_name (val_type) == NULL)
d0e39ea2 9208 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9209
9210 return unwrap_value (v);
9211 }
d2e4a39e 9212 else
14f9c5c9 9213 {
d2e4a39e 9214 struct type *raw_real_type =
dda83cd7 9215 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 9216
5bf03f13
JB
9217 /* If there is no parallel XVS or XVE type, then the value is
9218 already unwrapped. Return it without further modification. */
9219 if ((type == raw_real_type)
9220 && ada_find_parallel_type (type, "___XVE") == NULL)
9221 return val;
14f9c5c9 9222
d2e4a39e 9223 return
dda83cd7
SM
9224 coerce_unspec_val_to_type
9225 (val, ada_to_fixed_type (raw_real_type, 0,
9feb2d07 9226 val->address (),
dda83cd7 9227 NULL, 1));
14f9c5c9
AS
9228 }
9229}
d2e4a39e 9230
d99dcf51
JB
9231/* Given two array types T1 and T2, return nonzero iff both arrays
9232 contain the same number of elements. */
9233
9234static int
9235ada_same_array_size_p (struct type *t1, struct type *t2)
9236{
9237 LONGEST lo1, hi1, lo2, hi2;
9238
9239 /* Get the array bounds in order to verify that the size of
9240 the two arrays match. */
9241 if (!get_array_bounds (t1, &lo1, &hi1)
9242 || !get_array_bounds (t2, &lo2, &hi2))
9243 error (_("unable to determine array bounds"));
9244
9245 /* To make things easier for size comparison, normalize a bit
9246 the case of empty arrays by making sure that the difference
9247 between upper bound and lower bound is always -1. */
9248 if (lo1 > hi1)
9249 hi1 = lo1 - 1;
9250 if (lo2 > hi2)
9251 hi2 = lo2 - 1;
9252
9253 return (hi1 - lo1 == hi2 - lo2);
9254}
9255
9256/* Assuming that VAL is an array of integrals, and TYPE represents
9257 an array with the same number of elements, but with wider integral
9258 elements, return an array "casted" to TYPE. In practice, this
9259 means that the returned array is built by casting each element
9260 of the original array into TYPE's (wider) element type. */
9261
9262static struct value *
9263ada_promote_array_of_integrals (struct type *type, struct value *val)
9264{
27710edb 9265 struct type *elt_type = type->target_type ();
d99dcf51 9266 LONGEST lo, hi;
d99dcf51
JB
9267 LONGEST i;
9268
9269 /* Verify that both val and type are arrays of scalars, and
9270 that the size of val's elements is smaller than the size
9271 of type's element. */
78134374 9272 gdb_assert (type->code () == TYPE_CODE_ARRAY);
27710edb 9273 gdb_assert (is_integral_type (type->target_type ()));
d0c97917
TT
9274 gdb_assert (val->type ()->code () == TYPE_CODE_ARRAY);
9275 gdb_assert (is_integral_type (val->type ()->target_type ()));
df86565b 9276 gdb_assert (type->target_type ()->length ()
d0c97917 9277 > val->type ()->target_type ()->length ());
d99dcf51
JB
9278
9279 if (!get_array_bounds (type, &lo, &hi))
9280 error (_("unable to determine array bounds"));
9281
317c3ed9 9282 value *res = value::allocate (type);
bbe912ba 9283 gdb::array_view<gdb_byte> res_contents = res->contents_writeable ();
d99dcf51
JB
9284
9285 /* Promote each array element. */
9286 for (i = 0; i < hi - lo + 1; i++)
9287 {
9288 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
df86565b 9289 int elt_len = elt_type->length ();
d99dcf51 9290
efaf1ae0 9291 copy (elt->contents_all (), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9292 }
9293
9294 return res;
9295}
9296
4c4b4cd2
PH
9297/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9298 return the converted value. */
9299
d2e4a39e
AS
9300static struct value *
9301coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9302{
d0c97917 9303 struct type *type2 = val->type ();
5b4ee69b 9304
14f9c5c9
AS
9305 if (type == type2)
9306 return val;
9307
61ee279c
PH
9308 type2 = ada_check_typedef (type2);
9309 type = ada_check_typedef (type);
14f9c5c9 9310
78134374
SM
9311 if (type2->code () == TYPE_CODE_PTR
9312 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9313 {
9314 val = ada_value_ind (val);
d0c97917 9315 type2 = val->type ();
14f9c5c9
AS
9316 }
9317
78134374
SM
9318 if (type2->code () == TYPE_CODE_ARRAY
9319 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9320 {
d99dcf51
JB
9321 if (!ada_same_array_size_p (type, type2))
9322 error (_("cannot assign arrays of different length"));
9323
27710edb
SM
9324 if (is_integral_type (type->target_type ())
9325 && is_integral_type (type2->target_type ())
df86565b 9326 && type2->target_type ()->length () < type->target_type ()->length ())
d99dcf51
JB
9327 {
9328 /* Allow implicit promotion of the array elements to
9329 a wider type. */
9330 return ada_promote_array_of_integrals (type, val);
9331 }
9332
df86565b 9333 if (type2->target_type ()->length () != type->target_type ()->length ())
dda83cd7 9334 error (_("Incompatible types in assignment"));
81ae560c 9335 val->deprecated_set_type (type);
14f9c5c9 9336 }
d2e4a39e 9337 return val;
14f9c5c9
AS
9338}
9339
4c4b4cd2
PH
9340static struct value *
9341ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9342{
9343 struct value *val;
9344 struct type *type1, *type2;
9345 LONGEST v, v1, v2;
9346
994b9211
AC
9347 arg1 = coerce_ref (arg1);
9348 arg2 = coerce_ref (arg2);
d0c97917
TT
9349 type1 = get_base_type (ada_check_typedef (arg1->type ()));
9350 type2 = get_base_type (ada_check_typedef (arg2->type ()));
4c4b4cd2 9351
78134374
SM
9352 if (type1->code () != TYPE_CODE_INT
9353 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9354 return value_binop (arg1, arg2, op);
9355
76a01679 9356 switch (op)
4c4b4cd2
PH
9357 {
9358 case BINOP_MOD:
9359 case BINOP_DIV:
9360 case BINOP_REM:
9361 break;
9362 default:
9363 return value_binop (arg1, arg2, op);
9364 }
9365
9366 v2 = value_as_long (arg2);
9367 if (v2 == 0)
b0f9164c
TT
9368 {
9369 const char *name;
9370 if (op == BINOP_MOD)
9371 name = "mod";
9372 else if (op == BINOP_DIV)
9373 name = "/";
9374 else
9375 {
9376 gdb_assert (op == BINOP_REM);
9377 name = "rem";
9378 }
9379
9380 error (_("second operand of %s must not be zero."), name);
9381 }
4c4b4cd2 9382
c6d940a9 9383 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9384 return value_binop (arg1, arg2, op);
9385
9386 v1 = value_as_long (arg1);
9387 switch (op)
9388 {
9389 case BINOP_DIV:
9390 v = v1 / v2;
76a01679 9391 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
dda83cd7 9392 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9393 break;
9394 case BINOP_REM:
9395 v = v1 % v2;
76a01679 9396 if (v * v1 < 0)
dda83cd7 9397 v -= v2;
4c4b4cd2
PH
9398 break;
9399 default:
9400 /* Should not reach this point. */
9401 v = 0;
9402 }
9403
317c3ed9 9404 val = value::allocate (type1);
bbe912ba 9405 store_unsigned_integer (val->contents_raw ().data (),
d0c97917 9406 val->type ()->length (),
34877895 9407 type_byte_order (type1), v);
4c4b4cd2
PH
9408 return val;
9409}
9410
9411static int
9412ada_value_equal (struct value *arg1, struct value *arg2)
9413{
d0c97917
TT
9414 if (ada_is_direct_array_type (arg1->type ())
9415 || ada_is_direct_array_type (arg2->type ()))
4c4b4cd2 9416 {
79e8fcaa
JB
9417 struct type *arg1_type, *arg2_type;
9418
f58b38bf 9419 /* Automatically dereference any array reference before
dda83cd7 9420 we attempt to perform the comparison. */
f58b38bf
JB
9421 arg1 = ada_coerce_ref (arg1);
9422 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9423
4c4b4cd2
PH
9424 arg1 = ada_coerce_to_simple_array (arg1);
9425 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa 9426
d0c97917
TT
9427 arg1_type = ada_check_typedef (arg1->type ());
9428 arg2_type = ada_check_typedef (arg2->type ());
79e8fcaa 9429
78134374 9430 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9431 || arg2_type->code () != TYPE_CODE_ARRAY)
9432 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9433 /* FIXME: The following works only for types whose
dda83cd7
SM
9434 representations use all bits (no padding or undefined bits)
9435 and do not have user-defined equality. */
df86565b 9436 return (arg1_type->length () == arg2_type->length ()
efaf1ae0
TT
9437 && memcmp (arg1->contents ().data (),
9438 arg2->contents ().data (),
df86565b 9439 arg1_type->length ()) == 0);
4c4b4cd2
PH
9440 }
9441 return value_equal (arg1, arg2);
9442}
9443
d3c54a1c
TT
9444namespace expr
9445{
9446
9447bool
9448check_objfile (const std::unique_ptr<ada_component> &comp,
9449 struct objfile *objfile)
9450{
9451 return comp->uses_objfile (objfile);
9452}
9453
9454/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9455 component of LHS (a simple array or a record). Does not modify the
9456 inferior's memory, nor does it modify LHS (unless LHS ==
9457 CONTAINER). */
52ce6436
PH
9458
9459static void
9460assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9461 struct expression *exp, operation_up &arg)
52ce6436 9462{
d3c54a1c
TT
9463 scoped_value_mark mark;
9464
52ce6436 9465 struct value *elt;
d0c97917 9466 struct type *lhs_type = check_typedef (lhs->type ());
5b4ee69b 9467
78134374 9468 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9469 {
22601c15
UW
9470 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9471 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9472
52ce6436
PH
9473 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9474 }
9475 else
9476 {
d0c97917 9477 elt = ada_index_struct_field (index, lhs, 0, lhs->type ());
c48db5ca 9478 elt = ada_to_fixed_value (elt);
52ce6436
PH
9479 }
9480
d3c54a1c
TT
9481 ada_aggregate_operation *ag_op
9482 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9483 if (ag_op != nullptr)
9484 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9485 else
d3c54a1c
TT
9486 value_assign_to_component (container, elt,
9487 arg->evaluate (nullptr, exp,
9488 EVAL_NORMAL));
9489}
52ce6436 9490
d3c54a1c
TT
9491bool
9492ada_aggregate_component::uses_objfile (struct objfile *objfile)
9493{
9494 for (const auto &item : m_components)
9495 if (item->uses_objfile (objfile))
9496 return true;
9497 return false;
9498}
9499
9500void
9501ada_aggregate_component::dump (ui_file *stream, int depth)
9502{
6cb06a8c 9503 gdb_printf (stream, _("%*sAggregate\n"), depth, "");
d3c54a1c
TT
9504 for (const auto &item : m_components)
9505 item->dump (stream, depth + 1);
9506}
9507
9508void
9509ada_aggregate_component::assign (struct value *container,
9510 struct value *lhs, struct expression *exp,
9511 std::vector<LONGEST> &indices,
9512 LONGEST low, LONGEST high)
9513{
9514 for (auto &item : m_components)
9515 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9516}
9517
207582c0 9518/* See ada-exp.h. */
52ce6436 9519
207582c0 9520value *
d3c54a1c
TT
9521ada_aggregate_operation::assign_aggregate (struct value *container,
9522 struct value *lhs,
9523 struct expression *exp)
52ce6436
PH
9524{
9525 struct type *lhs_type;
52ce6436 9526 LONGEST low_index, high_index;
52ce6436
PH
9527
9528 container = ada_coerce_ref (container);
d0c97917 9529 if (ada_is_direct_array_type (container->type ()))
52ce6436
PH
9530 container = ada_coerce_to_simple_array (container);
9531 lhs = ada_coerce_ref (lhs);
4b53ca88 9532 if (!lhs->deprecated_modifiable ())
52ce6436
PH
9533 error (_("Left operand of assignment is not a modifiable lvalue."));
9534
d0c97917 9535 lhs_type = check_typedef (lhs->type ());
52ce6436
PH
9536 if (ada_is_direct_array_type (lhs_type))
9537 {
9538 lhs = ada_coerce_to_simple_array (lhs);
d0c97917 9539 lhs_type = check_typedef (lhs->type ());
cf88be68
SM
9540 low_index = lhs_type->bounds ()->low.const_val ();
9541 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9542 }
78134374 9543 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9544 {
9545 low_index = 0;
9546 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9547 }
9548 else
9549 error (_("Left-hand side must be array or record."));
9550
cf608cc4 9551 std::vector<LONGEST> indices (4);
52ce6436
PH
9552 indices[0] = indices[1] = low_index - 1;
9553 indices[2] = indices[3] = high_index + 1;
52ce6436 9554
d3c54a1c
TT
9555 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9556 low_index, high_index);
207582c0
TT
9557
9558 return container;
d3c54a1c
TT
9559}
9560
9561bool
9562ada_positional_component::uses_objfile (struct objfile *objfile)
9563{
9564 return m_op->uses_objfile (objfile);
9565}
52ce6436 9566
d3c54a1c
TT
9567void
9568ada_positional_component::dump (ui_file *stream, int depth)
9569{
6cb06a8c
TT
9570 gdb_printf (stream, _("%*sPositional, index = %d\n"),
9571 depth, "", m_index);
d3c54a1c 9572 m_op->dump (stream, depth + 1);
52ce6436 9573}
d3c54a1c 9574
52ce6436 9575/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9576 construct, given that the positions are relative to lower bound
9577 LOW, where HIGH is the upper bound. Record the position in
9578 INDICES. CONTAINER is as for assign_aggregate. */
9579void
9580ada_positional_component::assign (struct value *container,
9581 struct value *lhs, struct expression *exp,
9582 std::vector<LONGEST> &indices,
9583 LONGEST low, LONGEST high)
52ce6436 9584{
d3c54a1c
TT
9585 LONGEST ind = m_index + low;
9586
52ce6436 9587 if (ind - 1 == high)
e1d5a0d2 9588 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9589 if (ind <= high)
9590 {
cf608cc4 9591 add_component_interval (ind, ind, indices);
d3c54a1c 9592 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9593 }
52ce6436
PH
9594}
9595
d3c54a1c
TT
9596bool
9597ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9598{
9599 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9600}
9601
9602void
9603ada_discrete_range_association::dump (ui_file *stream, int depth)
9604{
6cb06a8c 9605 gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
a88c4354
TT
9606 m_low->dump (stream, depth + 1);
9607 m_high->dump (stream, depth + 1);
9608}
9609
9610void
9611ada_discrete_range_association::assign (struct value *container,
9612 struct value *lhs,
9613 struct expression *exp,
9614 std::vector<LONGEST> &indices,
9615 LONGEST low, LONGEST high,
9616 operation_up &op)
9617{
9618 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9619 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9620
9621 if (lower <= upper && (lower < low || upper > high))
9622 error (_("Index in component association out of bounds."));
9623
9624 add_component_interval (lower, upper, indices);
9625 while (lower <= upper)
9626 {
9627 assign_component (container, lhs, lower, exp, op);
9628 lower += 1;
9629 }
9630}
9631
9632bool
9633ada_name_association::uses_objfile (struct objfile *objfile)
9634{
9635 return m_val->uses_objfile (objfile);
9636}
9637
9638void
9639ada_name_association::dump (ui_file *stream, int depth)
9640{
6cb06a8c 9641 gdb_printf (stream, _("%*sName:\n"), depth, "");
a88c4354
TT
9642 m_val->dump (stream, depth + 1);
9643}
9644
9645void
9646ada_name_association::assign (struct value *container,
9647 struct value *lhs,
9648 struct expression *exp,
9649 std::vector<LONGEST> &indices,
9650 LONGEST low, LONGEST high,
9651 operation_up &op)
9652{
9653 int index;
9654
d0c97917 9655 if (ada_is_direct_array_type (lhs->type ()))
a88c4354
TT
9656 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9657 EVAL_NORMAL)));
9658 else
9659 {
9660 ada_string_operation *strop
9661 = dynamic_cast<ada_string_operation *> (m_val.get ());
9662
9663 const char *name;
9664 if (strop != nullptr)
9665 name = strop->get_name ();
9666 else
9667 {
9668 ada_var_value_operation *vvo
9669 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
9670 if (vvo != nullptr)
9671 error (_("Invalid record component association."));
9672 name = vvo->get_symbol ()->natural_name ();
9673 }
9674
9675 index = 0;
d0c97917 9676 if (! find_struct_field (name, lhs->type (), 0,
a88c4354
TT
9677 NULL, NULL, NULL, NULL, &index))
9678 error (_("Unknown component name: %s."), name);
9679 }
9680
9681 add_component_interval (index, index, indices);
9682 assign_component (container, lhs, index, exp, op);
9683}
9684
9685bool
9686ada_choices_component::uses_objfile (struct objfile *objfile)
9687{
9688 if (m_op->uses_objfile (objfile))
9689 return true;
9690 for (const auto &item : m_assocs)
9691 if (item->uses_objfile (objfile))
9692 return true;
9693 return false;
9694}
9695
9696void
9697ada_choices_component::dump (ui_file *stream, int depth)
9698{
6cb06a8c 9699 gdb_printf (stream, _("%*sChoices:\n"), depth, "");
a88c4354
TT
9700 m_op->dump (stream, depth + 1);
9701 for (const auto &item : m_assocs)
9702 item->dump (stream, depth + 1);
9703}
9704
9705/* Assign into the components of LHS indexed by the OP_CHOICES
9706 construct at *POS, updating *POS past the construct, given that
9707 the allowable indices are LOW..HIGH. Record the indices assigned
9708 to in INDICES. CONTAINER is as for assign_aggregate. */
9709void
9710ada_choices_component::assign (struct value *container,
9711 struct value *lhs, struct expression *exp,
9712 std::vector<LONGEST> &indices,
9713 LONGEST low, LONGEST high)
9714{
9715 for (auto &item : m_assocs)
9716 item->assign (container, lhs, exp, indices, low, high, m_op);
9717}
9718
9719bool
9720ada_others_component::uses_objfile (struct objfile *objfile)
9721{
9722 return m_op->uses_objfile (objfile);
9723}
9724
9725void
9726ada_others_component::dump (ui_file *stream, int depth)
9727{
6cb06a8c 9728 gdb_printf (stream, _("%*sOthers:\n"), depth, "");
a88c4354
TT
9729 m_op->dump (stream, depth + 1);
9730}
9731
9732/* Assign the value of the expression in the OP_OTHERS construct in
9733 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9734 have not been previously assigned. The index intervals already assigned
9735 are in INDICES. CONTAINER is as for assign_aggregate. */
9736void
9737ada_others_component::assign (struct value *container,
9738 struct value *lhs, struct expression *exp,
9739 std::vector<LONGEST> &indices,
9740 LONGEST low, LONGEST high)
9741{
9742 int num_indices = indices.size ();
9743 for (int i = 0; i < num_indices - 2; i += 2)
9744 {
9745 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9746 assign_component (container, lhs, ind, exp, m_op);
9747 }
9748}
9749
9750struct value *
9751ada_assign_operation::evaluate (struct type *expect_type,
9752 struct expression *exp,
9753 enum noside noside)
9754{
9755 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
9756
9757 ada_aggregate_operation *ag_op
9758 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9759 if (ag_op != nullptr)
9760 {
9761 if (noside != EVAL_NORMAL)
9762 return arg1;
9763
207582c0 9764 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9765 return ada_value_assign (arg1, arg1);
9766 }
9767 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9768 except if the lhs of our assignment is a convenience variable.
9769 In the case of assigning to a convenience variable, the lhs
9770 should be exactly the result of the evaluation of the rhs. */
d0c97917 9771 struct type *type = arg1->type ();
736355f2 9772 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9773 type = NULL;
9774 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9775 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354 9776 return arg1;
736355f2 9777 if (arg1->lval () == lval_internalvar)
a88c4354
TT
9778 {
9779 /* Nothing. */
9780 }
9781 else
d0c97917 9782 arg2 = coerce_for_assign (arg1->type (), arg2);
a88c4354
TT
9783 return ada_value_assign (arg1, arg2);
9784}
9785
9786} /* namespace expr */
9787
cf608cc4
TT
9788/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9789 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9790 overlap. */
52ce6436
PH
9791static void
9792add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9793 std::vector<LONGEST> &indices)
52ce6436
PH
9794{
9795 int i, j;
5b4ee69b 9796
cf608cc4
TT
9797 int size = indices.size ();
9798 for (i = 0; i < size; i += 2) {
52ce6436
PH
9799 if (high >= indices[i] && low <= indices[i + 1])
9800 {
9801 int kh;
5b4ee69b 9802
cf608cc4 9803 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9804 if (high < indices[kh])
9805 break;
9806 if (low < indices[i])
9807 indices[i] = low;
9808 indices[i + 1] = indices[kh - 1];
9809 if (high > indices[i + 1])
9810 indices[i + 1] = high;
cf608cc4
TT
9811 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9812 indices.resize (kh - i - 2);
52ce6436
PH
9813 return;
9814 }
9815 else if (high < indices[i])
9816 break;
9817 }
9818
cf608cc4 9819 indices.resize (indices.size () + 2);
d4813f10 9820 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9821 indices[j] = indices[j - 2];
9822 indices[i] = low;
9823 indices[i + 1] = high;
9824}
9825
6e48bd2c
JB
9826/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9827 is different. */
9828
9829static struct value *
b7e22850 9830ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c 9831{
d0c97917 9832 if (type == ada_check_typedef (arg2->type ()))
6e48bd2c
JB
9833 return arg2;
9834
6e48bd2c
JB
9835 return value_cast (type, arg2);
9836}
9837
284614f0
JB
9838/* Evaluating Ada expressions, and printing their result.
9839 ------------------------------------------------------
9840
21649b50
JB
9841 1. Introduction:
9842 ----------------
9843
284614f0
JB
9844 We usually evaluate an Ada expression in order to print its value.
9845 We also evaluate an expression in order to print its type, which
9846 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9847 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9848 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9849 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9850 similar.
9851
9852 Evaluating expressions is a little more complicated for Ada entities
9853 than it is for entities in languages such as C. The main reason for
9854 this is that Ada provides types whose definition might be dynamic.
9855 One example of such types is variant records. Or another example
9856 would be an array whose bounds can only be known at run time.
9857
9858 The following description is a general guide as to what should be
9859 done (and what should NOT be done) in order to evaluate an expression
9860 involving such types, and when. This does not cover how the semantic
9861 information is encoded by GNAT as this is covered separatly. For the
9862 document used as the reference for the GNAT encoding, see exp_dbug.ads
9863 in the GNAT sources.
9864
9865 Ideally, we should embed each part of this description next to its
9866 associated code. Unfortunately, the amount of code is so vast right
9867 now that it's hard to see whether the code handling a particular
9868 situation might be duplicated or not. One day, when the code is
9869 cleaned up, this guide might become redundant with the comments
9870 inserted in the code, and we might want to remove it.
9871
21649b50
JB
9872 2. ``Fixing'' an Entity, the Simple Case:
9873 -----------------------------------------
9874
284614f0
JB
9875 When evaluating Ada expressions, the tricky issue is that they may
9876 reference entities whose type contents and size are not statically
9877 known. Consider for instance a variant record:
9878
9879 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9880 case Empty is
9881 when True => null;
9882 when False => Value : Integer;
9883 end case;
284614f0
JB
9884 end record;
9885 Yes : Rec := (Empty => False, Value => 1);
9886 No : Rec := (empty => True);
9887
9888 The size and contents of that record depends on the value of the
9889 descriminant (Rec.Empty). At this point, neither the debugging
9890 information nor the associated type structure in GDB are able to
9891 express such dynamic types. So what the debugger does is to create
9892 "fixed" versions of the type that applies to the specific object.
30baf67b 9893 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9894 which means creating its associated fixed type.
9895
9896 Example: when printing the value of variable "Yes" above, its fixed
9897 type would look like this:
9898
9899 type Rec is record
dda83cd7
SM
9900 Empty : Boolean;
9901 Value : Integer;
284614f0
JB
9902 end record;
9903
9904 On the other hand, if we printed the value of "No", its fixed type
9905 would become:
9906
9907 type Rec is record
dda83cd7 9908 Empty : Boolean;
284614f0
JB
9909 end record;
9910
9911 Things become a little more complicated when trying to fix an entity
9912 with a dynamic type that directly contains another dynamic type,
9913 such as an array of variant records, for instance. There are
9914 two possible cases: Arrays, and records.
9915
21649b50
JB
9916 3. ``Fixing'' Arrays:
9917 ---------------------
9918
9919 The type structure in GDB describes an array in terms of its bounds,
9920 and the type of its elements. By design, all elements in the array
9921 have the same type and we cannot represent an array of variant elements
9922 using the current type structure in GDB. When fixing an array,
9923 we cannot fix the array element, as we would potentially need one
9924 fixed type per element of the array. As a result, the best we can do
9925 when fixing an array is to produce an array whose bounds and size
9926 are correct (allowing us to read it from memory), but without having
9927 touched its element type. Fixing each element will be done later,
9928 when (if) necessary.
9929
9930 Arrays are a little simpler to handle than records, because the same
9931 amount of memory is allocated for each element of the array, even if
1b536f04 9932 the amount of space actually used by each element differs from element
21649b50 9933 to element. Consider for instance the following array of type Rec:
284614f0
JB
9934
9935 type Rec_Array is array (1 .. 2) of Rec;
9936
1b536f04
JB
9937 The actual amount of memory occupied by each element might be different
9938 from element to element, depending on the value of their discriminant.
21649b50 9939 But the amount of space reserved for each element in the array remains
1b536f04 9940 fixed regardless. So we simply need to compute that size using
21649b50
JB
9941 the debugging information available, from which we can then determine
9942 the array size (we multiply the number of elements of the array by
9943 the size of each element).
9944
9945 The simplest case is when we have an array of a constrained element
9946 type. For instance, consider the following type declarations:
9947
dda83cd7
SM
9948 type Bounded_String (Max_Size : Integer) is
9949 Length : Integer;
9950 Buffer : String (1 .. Max_Size);
9951 end record;
9952 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9953
9954 In this case, the compiler describes the array as an array of
9955 variable-size elements (identified by its XVS suffix) for which
9956 the size can be read in the parallel XVZ variable.
9957
9958 In the case of an array of an unconstrained element type, the compiler
9959 wraps the array element inside a private PAD type. This type should not
9960 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9961 that we also use the adjective "aligner" in our code to designate
9962 these wrapper types.
9963
1b536f04 9964 In some cases, the size allocated for each element is statically
21649b50
JB
9965 known. In that case, the PAD type already has the correct size,
9966 and the array element should remain unfixed.
9967
9968 But there are cases when this size is not statically known.
9969 For instance, assuming that "Five" is an integer variable:
284614f0 9970
dda83cd7
SM
9971 type Dynamic is array (1 .. Five) of Integer;
9972 type Wrapper (Has_Length : Boolean := False) is record
9973 Data : Dynamic;
9974 case Has_Length is
9975 when True => Length : Integer;
9976 when False => null;
9977 end case;
9978 end record;
9979 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9980
dda83cd7
SM
9981 Hello : Wrapper_Array := (others => (Has_Length => True,
9982 Data => (others => 17),
9983 Length => 1));
284614f0
JB
9984
9985
9986 The debugging info would describe variable Hello as being an
9987 array of a PAD type. The size of that PAD type is not statically
9988 known, but can be determined using a parallel XVZ variable.
9989 In that case, a copy of the PAD type with the correct size should
9990 be used for the fixed array.
9991
21649b50
JB
9992 3. ``Fixing'' record type objects:
9993 ----------------------------------
9994
9995 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9996 record types. In this case, in order to compute the associated
9997 fixed type, we need to determine the size and offset of each of
9998 its components. This, in turn, requires us to compute the fixed
9999 type of each of these components.
10000
10001 Consider for instance the example:
10002
dda83cd7
SM
10003 type Bounded_String (Max_Size : Natural) is record
10004 Str : String (1 .. Max_Size);
10005 Length : Natural;
10006 end record;
10007 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
10008
10009 In that case, the position of field "Length" depends on the size
10010 of field Str, which itself depends on the value of the Max_Size
21649b50 10011 discriminant. In order to fix the type of variable My_String,
284614f0
JB
10012 we need to fix the type of field Str. Therefore, fixing a variant
10013 record requires us to fix each of its components.
10014
10015 However, if a component does not have a dynamic size, the component
10016 should not be fixed. In particular, fields that use a PAD type
10017 should not fixed. Here is an example where this might happen
10018 (assuming type Rec above):
10019
10020 type Container (Big : Boolean) is record
dda83cd7
SM
10021 First : Rec;
10022 After : Integer;
10023 case Big is
10024 when True => Another : Integer;
10025 when False => null;
10026 end case;
284614f0
JB
10027 end record;
10028 My_Container : Container := (Big => False,
dda83cd7
SM
10029 First => (Empty => True),
10030 After => 42);
284614f0
JB
10031
10032 In that example, the compiler creates a PAD type for component First,
10033 whose size is constant, and then positions the component After just
10034 right after it. The offset of component After is therefore constant
10035 in this case.
10036
10037 The debugger computes the position of each field based on an algorithm
10038 that uses, among other things, the actual position and size of the field
21649b50
JB
10039 preceding it. Let's now imagine that the user is trying to print
10040 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
10041 end up computing the offset of field After based on the size of the
10042 fixed version of field First. And since in our example First has
10043 only one actual field, the size of the fixed type is actually smaller
10044 than the amount of space allocated to that field, and thus we would
10045 compute the wrong offset of field After.
10046
21649b50
JB
10047 To make things more complicated, we need to watch out for dynamic
10048 components of variant records (identified by the ___XVL suffix in
10049 the component name). Even if the target type is a PAD type, the size
10050 of that type might not be statically known. So the PAD type needs
10051 to be unwrapped and the resulting type needs to be fixed. Otherwise,
10052 we might end up with the wrong size for our component. This can be
10053 observed with the following type declarations:
284614f0 10054
dda83cd7
SM
10055 type Octal is new Integer range 0 .. 7;
10056 type Octal_Array is array (Positive range <>) of Octal;
10057 pragma Pack (Octal_Array);
284614f0 10058
dda83cd7
SM
10059 type Octal_Buffer (Size : Positive) is record
10060 Buffer : Octal_Array (1 .. Size);
10061 Length : Integer;
10062 end record;
284614f0
JB
10063
10064 In that case, Buffer is a PAD type whose size is unset and needs
10065 to be computed by fixing the unwrapped type.
10066
21649b50
JB
10067 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
10068 ----------------------------------------------------------
10069
10070 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
10071 thus far, be actually fixed?
10072
10073 The answer is: Only when referencing that element. For instance
10074 when selecting one component of a record, this specific component
10075 should be fixed at that point in time. Or when printing the value
10076 of a record, each component should be fixed before its value gets
10077 printed. Similarly for arrays, the element of the array should be
10078 fixed when printing each element of the array, or when extracting
10079 one element out of that array. On the other hand, fixing should
10080 not be performed on the elements when taking a slice of an array!
10081
31432a67 10082 Note that one of the side effects of miscomputing the offset and
284614f0
JB
10083 size of each field is that we end up also miscomputing the size
10084 of the containing type. This can have adverse results when computing
10085 the value of an entity. GDB fetches the value of an entity based
10086 on the size of its type, and thus a wrong size causes GDB to fetch
10087 the wrong amount of memory. In the case where the computed size is
10088 too small, GDB fetches too little data to print the value of our
31432a67 10089 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
10090 past the buffer containing the data =:-o. */
10091
62d4bd94
TT
10092/* A helper function for TERNOP_IN_RANGE. */
10093
10094static value *
10095eval_ternop_in_range (struct type *expect_type, struct expression *exp,
10096 enum noside noside,
10097 value *arg1, value *arg2, value *arg3)
10098{
62d4bd94
TT
10099 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10100 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10101 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10102 return
10103 value_from_longest (type,
10104 (value_less (arg1, arg3)
10105 || value_equal (arg1, arg3))
10106 && (value_less (arg2, arg1)
10107 || value_equal (arg2, arg1)));
10108}
10109
82390ab8
TT
10110/* A helper function for UNOP_NEG. */
10111
7c15d377 10112value *
82390ab8
TT
10113ada_unop_neg (struct type *expect_type,
10114 struct expression *exp,
10115 enum noside noside, enum exp_opcode op,
10116 struct value *arg1)
10117{
82390ab8
TT
10118 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10119 return value_neg (arg1);
10120}
10121
7efc87ff
TT
10122/* A helper function for UNOP_IN_RANGE. */
10123
95d49dfb 10124value *
7efc87ff
TT
10125ada_unop_in_range (struct type *expect_type,
10126 struct expression *exp,
10127 enum noside noside, enum exp_opcode op,
10128 struct value *arg1, struct type *type)
10129{
7efc87ff
TT
10130 struct value *arg2, *arg3;
10131 switch (type->code ())
10132 {
10133 default:
10134 lim_warning (_("Membership test incompletely implemented; "
10135 "always returns true"));
10136 type = language_bool_type (exp->language_defn, exp->gdbarch);
10137 return value_from_longest (type, (LONGEST) 1);
10138
10139 case TYPE_CODE_RANGE:
10140 arg2 = value_from_longest (type,
10141 type->bounds ()->low.const_val ());
10142 arg3 = value_from_longest (type,
10143 type->bounds ()->high.const_val ());
10144 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10145 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10146 type = language_bool_type (exp->language_defn, exp->gdbarch);
10147 return
10148 value_from_longest (type,
10149 (value_less (arg1, arg3)
10150 || value_equal (arg1, arg3))
10151 && (value_less (arg2, arg1)
10152 || value_equal (arg2, arg1)));
10153 }
10154}
10155
020dbabe
TT
10156/* A helper function for OP_ATR_TAG. */
10157
7c15d377 10158value *
020dbabe
TT
10159ada_atr_tag (struct type *expect_type,
10160 struct expression *exp,
10161 enum noside noside, enum exp_opcode op,
10162 struct value *arg1)
10163{
10164 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10165 return value::zero (ada_tag_type (arg1), not_lval);
020dbabe
TT
10166
10167 return ada_value_tag (arg1);
10168}
10169
68c75735
TT
10170/* A helper function for OP_ATR_SIZE. */
10171
7c15d377 10172value *
68c75735
TT
10173ada_atr_size (struct type *expect_type,
10174 struct expression *exp,
10175 enum noside noside, enum exp_opcode op,
10176 struct value *arg1)
10177{
d0c97917 10178 struct type *type = arg1->type ();
68c75735
TT
10179
10180 /* If the argument is a reference, then dereference its type, since
10181 the user is really asking for the size of the actual object,
10182 not the size of the pointer. */
10183 if (type->code () == TYPE_CODE_REF)
27710edb 10184 type = type->target_type ();
68c75735 10185
0b2b0b82 10186 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10187 return value::zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
68c75735
TT
10188 else
10189 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
df86565b 10190 TARGET_CHAR_BIT * type->length ());
68c75735
TT
10191}
10192
d05e24e6
TT
10193/* A helper function for UNOP_ABS. */
10194
7c15d377 10195value *
d05e24e6
TT
10196ada_abs (struct type *expect_type,
10197 struct expression *exp,
10198 enum noside noside, enum exp_opcode op,
10199 struct value *arg1)
10200{
10201 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
ee7bb294 10202 if (value_less (arg1, value::zero (arg1->type (), not_lval)))
d05e24e6
TT
10203 return value_neg (arg1);
10204 else
10205 return arg1;
10206}
10207
faa1dfd7
TT
10208/* A helper function for BINOP_MUL. */
10209
d9e7db06 10210value *
faa1dfd7
TT
10211ada_mult_binop (struct type *expect_type,
10212 struct expression *exp,
10213 enum noside noside, enum exp_opcode op,
10214 struct value *arg1, struct value *arg2)
10215{
10216 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10217 {
10218 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
ee7bb294 10219 return value::zero (arg1->type (), not_lval);
faa1dfd7
TT
10220 }
10221 else
10222 {
10223 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10224 return ada_value_binop (arg1, arg2, op);
10225 }
10226}
10227
214b13ac
TT
10228/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10229
6e8fb7b7 10230value *
214b13ac
TT
10231ada_equal_binop (struct type *expect_type,
10232 struct expression *exp,
10233 enum noside noside, enum exp_opcode op,
10234 struct value *arg1, struct value *arg2)
10235{
10236 int tem;
10237 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10238 tem = 0;
10239 else
10240 {
10241 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10242 tem = ada_value_equal (arg1, arg2);
10243 }
10244 if (op == BINOP_NOTEQUAL)
10245 tem = !tem;
10246 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10247 return value_from_longest (type, (LONGEST) tem);
10248}
10249
5ce19db8
TT
10250/* A helper function for TERNOP_SLICE. */
10251
1b1ebfab 10252value *
5ce19db8
TT
10253ada_ternop_slice (struct expression *exp,
10254 enum noside noside,
10255 struct value *array, struct value *low_bound_val,
10256 struct value *high_bound_val)
10257{
10258 LONGEST low_bound;
10259 LONGEST high_bound;
10260
10261 low_bound_val = coerce_ref (low_bound_val);
10262 high_bound_val = coerce_ref (high_bound_val);
10263 low_bound = value_as_long (low_bound_val);
10264 high_bound = value_as_long (high_bound_val);
10265
10266 /* If this is a reference to an aligner type, then remove all
10267 the aligners. */
d0c97917
TT
10268 if (array->type ()->code () == TYPE_CODE_REF
10269 && ada_is_aligner_type (array->type ()->target_type ()))
10270 array->type ()->set_target_type
10271 (ada_aligned_type (array->type ()->target_type ()));
5ce19db8 10272
d0c97917 10273 if (ada_is_any_packed_array_type (array->type ()))
5ce19db8
TT
10274 error (_("cannot slice a packed array"));
10275
10276 /* If this is a reference to an array or an array lvalue,
10277 convert to a pointer. */
d0c97917
TT
10278 if (array->type ()->code () == TYPE_CODE_REF
10279 || (array->type ()->code () == TYPE_CODE_ARRAY
736355f2 10280 && array->lval () == lval_memory))
5ce19db8
TT
10281 array = value_addr (array);
10282
10283 if (noside == EVAL_AVOID_SIDE_EFFECTS
10284 && ada_is_array_descriptor_type (ada_check_typedef
d0c97917 10285 (array->type ())))
5ce19db8
TT
10286 return empty_array (ada_type_of_array (array, 0), low_bound,
10287 high_bound);
10288
10289 array = ada_coerce_to_simple_array_ptr (array);
10290
10291 /* If we have more than one level of pointer indirection,
10292 dereference the value until we get only one level. */
d0c97917
TT
10293 while (array->type ()->code () == TYPE_CODE_PTR
10294 && (array->type ()->target_type ()->code ()
5ce19db8
TT
10295 == TYPE_CODE_PTR))
10296 array = value_ind (array);
10297
10298 /* Make sure we really do have an array type before going further,
10299 to avoid a SEGV when trying to get the index type or the target
10300 type later down the road if the debug info generated by
10301 the compiler is incorrect or incomplete. */
d0c97917 10302 if (!ada_is_simple_array_type (array->type ()))
5ce19db8
TT
10303 error (_("cannot take slice of non-array"));
10304
d0c97917 10305 if (ada_check_typedef (array->type ())->code ()
5ce19db8
TT
10306 == TYPE_CODE_PTR)
10307 {
d0c97917 10308 struct type *type0 = ada_check_typedef (array->type ());
5ce19db8
TT
10309
10310 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
27710edb 10311 return empty_array (type0->target_type (), low_bound, high_bound);
5ce19db8
TT
10312 else
10313 {
10314 struct type *arr_type0 =
27710edb 10315 to_fixed_array_type (type0->target_type (), NULL, 1);
5ce19db8
TT
10316
10317 return ada_value_slice_from_ptr (array, arr_type0,
10318 longest_to_int (low_bound),
10319 longest_to_int (high_bound));
10320 }
10321 }
10322 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10323 return array;
10324 else if (high_bound < low_bound)
d0c97917 10325 return empty_array (array->type (), low_bound, high_bound);
5ce19db8
TT
10326 else
10327 return ada_value_slice (array, longest_to_int (low_bound),
10328 longest_to_int (high_bound));
10329}
10330
b467efaa
TT
10331/* A helper function for BINOP_IN_BOUNDS. */
10332
82c3886e 10333value *
b467efaa
TT
10334ada_binop_in_bounds (struct expression *exp, enum noside noside,
10335 struct value *arg1, struct value *arg2, int n)
10336{
10337 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10338 {
10339 struct type *type = language_bool_type (exp->language_defn,
10340 exp->gdbarch);
ee7bb294 10341 return value::zero (type, not_lval);
b467efaa
TT
10342 }
10343
d0c97917 10344 struct type *type = ada_index_type (arg2->type (), n, "range");
b467efaa 10345 if (!type)
d0c97917 10346 type = arg1->type ();
b467efaa
TT
10347
10348 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10349 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10350
10351 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10352 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10353 type = language_bool_type (exp->language_defn, exp->gdbarch);
10354 return value_from_longest (type,
10355 (value_less (arg1, arg3)
10356 || value_equal (arg1, arg3))
10357 && (value_less (arg2, arg1)
10358 || value_equal (arg2, arg1)));
10359}
10360
b84564fc
TT
10361/* A helper function for some attribute operations. */
10362
10363static value *
10364ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10365 struct value *arg1, struct type *type_arg, int tem)
10366{
10367 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10368 {
10369 if (type_arg == NULL)
d0c97917 10370 type_arg = arg1->type ();
b84564fc
TT
10371
10372 if (ada_is_constrained_packed_array_type (type_arg))
10373 type_arg = decode_constrained_packed_array_type (type_arg);
10374
10375 if (!discrete_type_p (type_arg))
10376 {
10377 switch (op)
10378 {
10379 default: /* Should never happen. */
10380 error (_("unexpected attribute encountered"));
10381 case OP_ATR_FIRST:
10382 case OP_ATR_LAST:
10383 type_arg = ada_index_type (type_arg, tem,
10384 ada_attribute_name (op));
10385 break;
10386 case OP_ATR_LENGTH:
10387 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10388 break;
10389 }
10390 }
10391
ee7bb294 10392 return value::zero (type_arg, not_lval);
b84564fc
TT
10393 }
10394 else if (type_arg == NULL)
10395 {
10396 arg1 = ada_coerce_ref (arg1);
10397
d0c97917 10398 if (ada_is_constrained_packed_array_type (arg1->type ()))
b84564fc
TT
10399 arg1 = ada_coerce_to_simple_array (arg1);
10400
10401 struct type *type;
10402 if (op == OP_ATR_LENGTH)
10403 type = builtin_type (exp->gdbarch)->builtin_int;
10404 else
10405 {
d0c97917 10406 type = ada_index_type (arg1->type (), tem,
b84564fc
TT
10407 ada_attribute_name (op));
10408 if (type == NULL)
10409 type = builtin_type (exp->gdbarch)->builtin_int;
10410 }
10411
10412 switch (op)
10413 {
10414 default: /* Should never happen. */
10415 error (_("unexpected attribute encountered"));
10416 case OP_ATR_FIRST:
10417 return value_from_longest
10418 (type, ada_array_bound (arg1, tem, 0));
10419 case OP_ATR_LAST:
10420 return value_from_longest
10421 (type, ada_array_bound (arg1, tem, 1));
10422 case OP_ATR_LENGTH:
10423 return value_from_longest
10424 (type, ada_array_length (arg1, tem));
10425 }
10426 }
10427 else if (discrete_type_p (type_arg))
10428 {
10429 struct type *range_type;
10430 const char *name = ada_type_name (type_arg);
10431
10432 range_type = NULL;
10433 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10434 range_type = to_fixed_range_type (type_arg, NULL);
10435 if (range_type == NULL)
10436 range_type = type_arg;
10437 switch (op)
10438 {
10439 default:
10440 error (_("unexpected attribute encountered"));
10441 case OP_ATR_FIRST:
10442 return value_from_longest
10443 (range_type, ada_discrete_type_low_bound (range_type));
10444 case OP_ATR_LAST:
10445 return value_from_longest
10446 (range_type, ada_discrete_type_high_bound (range_type));
10447 case OP_ATR_LENGTH:
10448 error (_("the 'length attribute applies only to array types"));
10449 }
10450 }
10451 else if (type_arg->code () == TYPE_CODE_FLT)
10452 error (_("unimplemented type attribute"));
10453 else
10454 {
10455 LONGEST low, high;
10456
10457 if (ada_is_constrained_packed_array_type (type_arg))
10458 type_arg = decode_constrained_packed_array_type (type_arg);
10459
10460 struct type *type;
10461 if (op == OP_ATR_LENGTH)
10462 type = builtin_type (exp->gdbarch)->builtin_int;
10463 else
10464 {
10465 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10466 if (type == NULL)
10467 type = builtin_type (exp->gdbarch)->builtin_int;
10468 }
10469
10470 switch (op)
10471 {
10472 default:
10473 error (_("unexpected attribute encountered"));
10474 case OP_ATR_FIRST:
10475 low = ada_array_bound_from_type (type_arg, tem, 0);
10476 return value_from_longest (type, low);
10477 case OP_ATR_LAST:
10478 high = ada_array_bound_from_type (type_arg, tem, 1);
10479 return value_from_longest (type, high);
10480 case OP_ATR_LENGTH:
10481 low = ada_array_bound_from_type (type_arg, tem, 0);
10482 high = ada_array_bound_from_type (type_arg, tem, 1);
10483 return value_from_longest (type, high - low + 1);
10484 }
10485 }
10486}
10487
38dc70cf
TT
10488/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10489
6ad3b8bf 10490struct value *
38dc70cf
TT
10491ada_binop_minmax (struct type *expect_type,
10492 struct expression *exp,
10493 enum noside noside, enum exp_opcode op,
10494 struct value *arg1, struct value *arg2)
10495{
10496 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10497 return value::zero (arg1->type (), not_lval);
38dc70cf
TT
10498 else
10499 {
10500 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10501 return value_binop (arg1, arg2, op);
38dc70cf
TT
10502 }
10503}
10504
dd5fd283
TT
10505/* A helper function for BINOP_EXP. */
10506
065ec826 10507struct value *
dd5fd283
TT
10508ada_binop_exp (struct type *expect_type,
10509 struct expression *exp,
10510 enum noside noside, enum exp_opcode op,
10511 struct value *arg1, struct value *arg2)
10512{
10513 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10514 return value::zero (arg1->type (), not_lval);
dd5fd283
TT
10515 else
10516 {
10517 /* For integer exponentiation operations,
10518 only promote the first argument. */
d0c97917 10519 if (is_integral_type (arg2->type ()))
dd5fd283
TT
10520 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10521 else
10522 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10523
10524 return value_binop (arg1, arg2, op);
10525 }
10526}
10527
03070ee9
TT
10528namespace expr
10529{
10530
8b12db26
TT
10531/* See ada-exp.h. */
10532
10533operation_up
10534ada_resolvable::replace (operation_up &&owner,
10535 struct expression *exp,
10536 bool deprocedure_p,
10537 bool parse_completion,
10538 innermost_block_tracker *tracker,
10539 struct type *context_type)
10540{
10541 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10542 return (make_operation<ada_funcall_operation>
10543 (std::move (owner),
10544 std::vector<operation_up> ()));
10545 return std::move (owner);
10546}
10547
c9f66f00 10548/* Convert the character literal whose value would be VAL to the
03adb248
TT
10549 appropriate value of type TYPE, if there is a translation.
10550 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10551 the literal 'A' (VAL == 65), returns 0. */
10552
10553static LONGEST
10554convert_char_literal (struct type *type, LONGEST val)
10555{
c9f66f00 10556 char name[12];
03adb248
TT
10557 int f;
10558
10559 if (type == NULL)
10560 return val;
10561 type = check_typedef (type);
10562 if (type->code () != TYPE_CODE_ENUM)
10563 return val;
10564
10565 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10566 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10567 else if (val >= 0 && val < 256)
10568 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10569 else if (val >= 0 && val < 0x10000)
10570 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10571 else
c9f66f00 10572 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10573 size_t len = strlen (name);
10574 for (f = 0; f < type->num_fields (); f += 1)
10575 {
10576 /* Check the suffix because an enum constant in a package will
10577 have a name like "pkg__QUxx". This is safe enough because we
10578 already have the correct type, and because mangling means
10579 there can't be clashes. */
33d16dd9 10580 const char *ename = type->field (f).name ();
03adb248
TT
10581 size_t elen = strlen (ename);
10582
10583 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10584 return type->field (f).loc_enumval ();
03adb248
TT
10585 }
10586 return val;
10587}
10588
b1b9c411
TT
10589value *
10590ada_char_operation::evaluate (struct type *expect_type,
10591 struct expression *exp,
10592 enum noside noside)
10593{
10594 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10595 if (expect_type != nullptr)
10596 result = ada_value_cast (expect_type, result);
10597 return result;
10598}
10599
03adb248
TT
10600/* See ada-exp.h. */
10601
10602operation_up
10603ada_char_operation::replace (operation_up &&owner,
10604 struct expression *exp,
10605 bool deprocedure_p,
10606 bool parse_completion,
10607 innermost_block_tracker *tracker,
10608 struct type *context_type)
10609{
10610 operation_up result = std::move (owner);
10611
10612 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10613 {
10614 gdb_assert (result.get () == this);
10615 std::get<0> (m_storage) = context_type;
10616 std::get<1> (m_storage)
10617 = convert_char_literal (context_type, std::get<1> (m_storage));
10618 }
10619
b1b9c411 10620 return result;
03adb248
TT
10621}
10622
03070ee9
TT
10623value *
10624ada_wrapped_operation::evaluate (struct type *expect_type,
10625 struct expression *exp,
10626 enum noside noside)
10627{
10628 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10629 if (noside == EVAL_NORMAL)
10630 result = unwrap_value (result);
10631
10632 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10633 then we need to perform the conversion manually, because
10634 evaluate_subexp_standard doesn't do it. This conversion is
10635 necessary in Ada because the different kinds of float/fixed
10636 types in Ada have different representations.
10637
10638 Similarly, we need to perform the conversion from OP_LONG
10639 ourselves. */
10640 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10641 result = ada_value_cast (expect_type, result);
10642
10643 return result;
10644}
10645
42fecb61
TT
10646value *
10647ada_string_operation::evaluate (struct type *expect_type,
10648 struct expression *exp,
10649 enum noside noside)
10650{
fc18a21b
TT
10651 struct type *char_type;
10652 if (expect_type != nullptr && ada_is_string_type (expect_type))
10653 char_type = ada_array_element_type (expect_type, 1);
10654 else
10655 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10656
10657 const std::string &str = std::get<0> (m_storage);
10658 const char *encoding;
df86565b 10659 switch (char_type->length ())
fc18a21b
TT
10660 {
10661 case 1:
10662 {
10663 /* Simply copy over the data -- this isn't perhaps strictly
10664 correct according to the encodings, but it is gdb's
10665 historical behavior. */
10666 struct type *stringtype
10667 = lookup_array_range_type (char_type, 1, str.length ());
317c3ed9 10668 struct value *val = value::allocate (stringtype);
bbe912ba 10669 memcpy (val->contents_raw ().data (), str.c_str (),
fc18a21b
TT
10670 str.length ());
10671 return val;
10672 }
10673
10674 case 2:
10675 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10676 encoding = "UTF-16BE";
10677 else
10678 encoding = "UTF-16LE";
10679 break;
10680
10681 case 4:
10682 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10683 encoding = "UTF-32BE";
10684 else
10685 encoding = "UTF-32LE";
10686 break;
10687
10688 default:
10689 error (_("unexpected character type size %s"),
df86565b 10690 pulongest (char_type->length ()));
fc18a21b
TT
10691 }
10692
10693 auto_obstack converted;
10694 convert_between_encodings (host_charset (), encoding,
10695 (const gdb_byte *) str.c_str (),
10696 str.length (), 1,
10697 &converted, translit_none);
10698
10699 struct type *stringtype
10700 = lookup_array_range_type (char_type, 1,
10701 obstack_object_size (&converted)
df86565b 10702 / char_type->length ());
317c3ed9 10703 struct value *val = value::allocate (stringtype);
bbe912ba 10704 memcpy (val->contents_raw ().data (),
fc18a21b
TT
10705 obstack_base (&converted),
10706 obstack_object_size (&converted));
10707 return val;
42fecb61
TT
10708}
10709
b1b9c411
TT
10710value *
10711ada_concat_operation::evaluate (struct type *expect_type,
10712 struct expression *exp,
10713 enum noside noside)
10714{
10715 /* If one side is a literal, evaluate the other side first so that
10716 the expected type can be set properly. */
10717 const operation_up &lhs_expr = std::get<0> (m_storage);
10718 const operation_up &rhs_expr = std::get<1> (m_storage);
10719
10720 value *lhs, *rhs;
10721 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10722 {
10723 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10724 lhs = lhs_expr->evaluate (rhs->type (), exp, noside);
b1b9c411
TT
10725 }
10726 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10727 {
10728 rhs = rhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10729 struct type *rhs_type = check_typedef (rhs->type ());
b1b9c411
TT
10730 struct type *elt_type = nullptr;
10731 if (rhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10732 elt_type = rhs_type->target_type ();
b1b9c411
TT
10733 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10734 }
10735 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10736 {
10737 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10738 rhs = rhs_expr->evaluate (lhs->type (), exp, noside);
b1b9c411
TT
10739 }
10740 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10741 {
10742 lhs = lhs_expr->evaluate (nullptr, exp, noside);
d0c97917 10743 struct type *lhs_type = check_typedef (lhs->type ());
b1b9c411
TT
10744 struct type *elt_type = nullptr;
10745 if (lhs_type->code () == TYPE_CODE_ARRAY)
27710edb 10746 elt_type = lhs_type->target_type ();
b1b9c411
TT
10747 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10748 }
10749 else
10750 return concat_operation::evaluate (expect_type, exp, noside);
10751
10752 return value_concat (lhs, rhs);
10753}
10754
cc6bd32e
TT
10755value *
10756ada_qual_operation::evaluate (struct type *expect_type,
10757 struct expression *exp,
10758 enum noside noside)
10759{
10760 struct type *type = std::get<1> (m_storage);
10761 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10762}
10763
fc715eb2
TT
10764value *
10765ada_ternop_range_operation::evaluate (struct type *expect_type,
10766 struct expression *exp,
10767 enum noside noside)
10768{
10769 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10770 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10771 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10772 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10773}
10774
73796c73
TT
10775value *
10776ada_binop_addsub_operation::evaluate (struct type *expect_type,
10777 struct expression *exp,
10778 enum noside noside)
10779{
10780 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10781 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10782
10783 auto do_op = [=] (LONGEST x, LONGEST y)
10784 {
10785 if (std::get<0> (m_storage) == BINOP_ADD)
10786 return x + y;
10787 return x - y;
10788 };
10789
d0c97917 10790 if (arg1->type ()->code () == TYPE_CODE_PTR)
73796c73 10791 return (value_from_longest
d0c97917 10792 (arg1->type (),
73796c73 10793 do_op (value_as_long (arg1), value_as_long (arg2))));
d0c97917 10794 if (arg2->type ()->code () == TYPE_CODE_PTR)
73796c73 10795 return (value_from_longest
d0c97917 10796 (arg2->type (),
73796c73
TT
10797 do_op (value_as_long (arg1), value_as_long (arg2))));
10798 /* Preserve the original type for use by the range case below.
10799 We cannot cast the result to a reference type, so if ARG1 is
10800 a reference type, find its underlying type. */
d0c97917 10801 struct type *type = arg1->type ();
73796c73 10802 while (type->code () == TYPE_CODE_REF)
27710edb 10803 type = type->target_type ();
73796c73
TT
10804 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10805 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10806 /* We need to special-case the result with a range.
10807 This is done for the benefit of "ptype". gdb's Ada support
10808 historically used the LHS to set the result type here, so
10809 preserve this behavior. */
10810 if (type->code () == TYPE_CODE_RANGE)
10811 arg1 = value_cast (type, arg1);
10812 return arg1;
10813}
10814
60fa02ca
TT
10815value *
10816ada_unop_atr_operation::evaluate (struct type *expect_type,
10817 struct expression *exp,
10818 enum noside noside)
10819{
10820 struct type *type_arg = nullptr;
10821 value *val = nullptr;
10822
10823 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10824 {
10825 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10826 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 10827 type_arg = tem->type ();
60fa02ca
TT
10828 }
10829 else
10830 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10831
10832 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10833 val, type_arg, std::get<2> (m_storage));
10834}
10835
3f4a0053
TT
10836value *
10837ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10838 struct expression *exp,
10839 enum noside noside)
10840{
10841 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 10842 return value::zero (expect_type, not_lval);
3f4a0053 10843
9c79936b
TT
10844 const bound_minimal_symbol &b = std::get<0> (m_storage);
10845 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10846
10847 val = ada_value_cast (expect_type, val);
10848
10849 /* Follow the Ada language semantics that do not allow taking
10850 an address of the result of a cast (view conversion in Ada). */
736355f2 10851 if (val->lval () == lval_memory)
3f4a0053 10852 {
3ee3b270 10853 if (val->lazy ())
78259c36 10854 val->fetch_lazy ();
6f9c9d71 10855 val->set_lval (not_lval);
3f4a0053
TT
10856 }
10857 return val;
10858}
10859
99a3b1e7
TT
10860value *
10861ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10862 struct expression *exp,
10863 enum noside noside)
10864{
10865 value *val = evaluate_var_value (noside,
9e5e03df
TT
10866 std::get<0> (m_storage).block,
10867 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10868
10869 val = ada_value_cast (expect_type, val);
10870
10871 /* Follow the Ada language semantics that do not allow taking
10872 an address of the result of a cast (view conversion in Ada). */
736355f2 10873 if (val->lval () == lval_memory)
99a3b1e7 10874 {
3ee3b270 10875 if (val->lazy ())
78259c36 10876 val->fetch_lazy ();
6f9c9d71 10877 val->set_lval (not_lval);
99a3b1e7
TT
10878 }
10879 return val;
10880}
10881
10882value *
10883ada_var_value_operation::evaluate (struct type *expect_type,
10884 struct expression *exp,
10885 enum noside noside)
10886{
9e5e03df 10887 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10888
6c9c307c 10889 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10890 /* Only encountered when an unresolved symbol occurs in a
10891 context other than a function call, in which case, it is
10892 invalid. */
10893 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10894 sym->print_name ());
10895
10896 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10897 {
5f9c5a63 10898 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10899 /* Check to see if this is a tagged type. We also need to handle
10900 the case where the type is a reference to a tagged type, but
10901 we have to be careful to exclude pointers to tagged types.
10902 The latter should be shown as usual (as a pointer), whereas
10903 a reference should mostly be transparent to the user. */
10904 if (ada_is_tagged_type (type, 0)
10905 || (type->code () == TYPE_CODE_REF
27710edb 10906 && ada_is_tagged_type (type->target_type (), 0)))
99a3b1e7
TT
10907 {
10908 /* Tagged types are a little special in the fact that the real
10909 type is dynamic and can only be determined by inspecting the
10910 object's tag. This means that we need to get the object's
10911 value first (EVAL_NORMAL) and then extract the actual object
10912 type from its tag.
10913
10914 Note that we cannot skip the final step where we extract
10915 the object type from its tag, because the EVAL_NORMAL phase
10916 results in dynamic components being resolved into fixed ones.
10917 This can cause problems when trying to print the type
10918 description of tagged types whose parent has a dynamic size:
10919 We use the type name of the "_parent" component in order
10920 to print the name of the ancestor type in the type description.
10921 If that component had a dynamic size, the resolution into
10922 a fixed type would result in the loss of that type name,
10923 thus preventing us from printing the name of the ancestor
10924 type in the type description. */
9863c3b5 10925 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10926
10927 if (type->code () != TYPE_CODE_REF)
10928 {
10929 struct type *actual_type;
10930
10931 actual_type = type_from_tag (ada_value_tag (arg1));
10932 if (actual_type == NULL)
10933 /* If, for some reason, we were unable to determine
10934 the actual type from the tag, then use the static
10935 approximation that we just computed as a fallback.
10936 This can happen if the debugging information is
10937 incomplete, for instance. */
10938 actual_type = type;
ee7bb294 10939 return value::zero (actual_type, not_lval);
99a3b1e7
TT
10940 }
10941 else
10942 {
10943 /* In the case of a ref, ada_coerce_ref takes care
10944 of determining the actual type. But the evaluation
10945 should return a ref as it should be valid to ask
10946 for its address; so rebuild a ref after coerce. */
10947 arg1 = ada_coerce_ref (arg1);
10948 return value_ref (arg1, TYPE_CODE_REF);
10949 }
10950 }
10951
10952 /* Records and unions for which GNAT encodings have been
10953 generated need to be statically fixed as well.
10954 Otherwise, non-static fixing produces a type where
10955 all dynamic properties are removed, which prevents "ptype"
10956 from being able to completely describe the type.
10957 For instance, a case statement in a variant record would be
10958 replaced by the relevant components based on the actual
10959 value of the discriminants. */
10960 if ((type->code () == TYPE_CODE_STRUCT
10961 && dynamic_template_type (type) != NULL)
10962 || (type->code () == TYPE_CODE_UNION
10963 && ada_find_parallel_type (type, "___XVU") != NULL))
ee7bb294 10964 return value::zero (to_static_fixed_type (type), not_lval);
99a3b1e7
TT
10965 }
10966
10967 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10968 return ada_to_fixed_value (arg1);
10969}
10970
d8a4ed8a
TT
10971bool
10972ada_var_value_operation::resolve (struct expression *exp,
10973 bool deprocedure_p,
10974 bool parse_completion,
10975 innermost_block_tracker *tracker,
10976 struct type *context_type)
10977{
9e5e03df 10978 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10979 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10980 {
10981 block_symbol resolved
9e5e03df 10982 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10983 context_type, parse_completion,
10984 deprocedure_p, tracker);
9e5e03df 10985 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10986 }
10987
10988 if (deprocedure_p
5f9c5a63 10989 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10990 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10991 return true;
10992
10993 return false;
10994}
10995
9e99f48f
TT
10996value *
10997ada_atr_val_operation::evaluate (struct type *expect_type,
10998 struct expression *exp,
10999 enum noside noside)
11000{
11001 value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
11002 return ada_val_atr (noside, std::get<0> (m_storage), arg);
11003}
11004
e8c33fa1
TT
11005value *
11006ada_unop_ind_operation::evaluate (struct type *expect_type,
11007 struct expression *exp,
11008 enum noside noside)
11009{
11010 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
11011
d0c97917 11012 struct type *type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
11013 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11014 {
11015 if (ada_is_array_descriptor_type (type))
11016 /* GDB allows dereferencing GNAT array descriptors. */
11017 {
11018 struct type *arrType = ada_type_of_array (arg1, 0);
11019
11020 if (arrType == NULL)
11021 error (_("Attempt to dereference null array pointer."));
11022 return value_at_lazy (arrType, 0);
11023 }
11024 else if (type->code () == TYPE_CODE_PTR
11025 || type->code () == TYPE_CODE_REF
11026 /* In C you can dereference an array to get the 1st elt. */
11027 || type->code () == TYPE_CODE_ARRAY)
11028 {
11029 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11030 only be determined by inspecting the object's tag.
11031 This means that we need to evaluate completely the
11032 expression in order to get its type. */
11033
11034 if ((type->code () == TYPE_CODE_REF
11035 || type->code () == TYPE_CODE_PTR)
27710edb 11036 && ada_is_tagged_type (type->target_type (), 0))
e8c33fa1
TT
11037 {
11038 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11039 EVAL_NORMAL);
d0c97917 11040 type = ada_value_ind (arg1)->type ();
e8c33fa1
TT
11041 }
11042 else
11043 {
11044 type = to_static_fixed_type
11045 (ada_aligned_type
27710edb 11046 (ada_check_typedef (type->target_type ())));
e8c33fa1 11047 }
ee7bb294 11048 return value::zero (type, lval_memory);
e8c33fa1
TT
11049 }
11050 else if (type->code () == TYPE_CODE_INT)
11051 {
11052 /* GDB allows dereferencing an int. */
11053 if (expect_type == NULL)
ee7bb294 11054 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
e8c33fa1
TT
11055 lval_memory);
11056 else
11057 {
11058 expect_type =
11059 to_static_fixed_type (ada_aligned_type (expect_type));
ee7bb294 11060 return value::zero (expect_type, lval_memory);
e8c33fa1
TT
11061 }
11062 }
11063 else
11064 error (_("Attempt to take contents of a non-pointer value."));
11065 }
11066 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
d0c97917 11067 type = ada_check_typedef (arg1->type ());
e8c33fa1
TT
11068
11069 if (type->code () == TYPE_CODE_INT)
11070 /* GDB allows dereferencing an int. If we were given
11071 the expect_type, then use that as the target type.
11072 Otherwise, assume that the target type is an int. */
11073 {
11074 if (expect_type != NULL)
11075 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11076 arg1));
11077 else
11078 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11079 (CORE_ADDR) value_as_address (arg1));
11080 }
11081
11082 if (ada_is_array_descriptor_type (type))
11083 /* GDB allows dereferencing GNAT array descriptors. */
11084 return ada_coerce_to_simple_array (arg1);
11085 else
11086 return ada_value_ind (arg1);
11087}
11088
ebc06ad8
TT
11089value *
11090ada_structop_operation::evaluate (struct type *expect_type,
11091 struct expression *exp,
11092 enum noside noside)
11093{
11094 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11095 const char *str = std::get<1> (m_storage).c_str ();
11096 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11097 {
11098 struct type *type;
d0c97917 11099 struct type *type1 = arg1->type ();
ebc06ad8
TT
11100
11101 if (ada_is_tagged_type (type1, 1))
11102 {
11103 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11104
11105 /* If the field is not found, check if it exists in the
11106 extension of this object's type. This means that we
11107 need to evaluate completely the expression. */
11108
11109 if (type == NULL)
11110 {
11111 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11112 EVAL_NORMAL);
11113 arg1 = ada_value_struct_elt (arg1, str, 0);
11114 arg1 = unwrap_value (arg1);
d0c97917 11115 type = ada_to_fixed_value (arg1)->type ();
ebc06ad8
TT
11116 }
11117 }
11118 else
11119 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11120
ee7bb294 11121 return value::zero (ada_aligned_type (type), lval_memory);
ebc06ad8
TT
11122 }
11123 else
11124 {
11125 arg1 = ada_value_struct_elt (arg1, str, 0);
11126 arg1 = unwrap_value (arg1);
11127 return ada_to_fixed_value (arg1);
11128 }
11129}
11130
efe3af2f
TT
11131value *
11132ada_funcall_operation::evaluate (struct type *expect_type,
11133 struct expression *exp,
11134 enum noside noside)
11135{
11136 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11137 int nargs = args_up.size ();
11138 std::vector<value *> argvec (nargs);
11139 operation_up &callee_op = std::get<0> (m_storage);
11140
11141 ada_var_value_operation *avv
11142 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11143 if (avv != nullptr
6c9c307c 11144 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11145 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11146 avv->get_symbol ()->print_name ());
11147
11148 value *callee = callee_op->evaluate (nullptr, exp, noside);
11149 for (int i = 0; i < args_up.size (); ++i)
11150 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11151
11152 if (ada_is_constrained_packed_array_type
d0c97917 11153 (desc_base_type (callee->type ())))
efe3af2f 11154 callee = ada_coerce_to_simple_array (callee);
d0c97917
TT
11155 else if (callee->type ()->code () == TYPE_CODE_ARRAY
11156 && TYPE_FIELD_BITSIZE (callee->type (), 0) != 0)
efe3af2f
TT
11157 /* This is a packed array that has already been fixed, and
11158 therefore already coerced to a simple array. Nothing further
11159 to do. */
11160 ;
d0c97917 11161 else if (callee->type ()->code () == TYPE_CODE_REF)
efe3af2f
TT
11162 {
11163 /* Make sure we dereference references so that all the code below
11164 feels like it's really handling the referenced value. Wrapping
11165 types (for alignment) may be there, so make sure we strip them as
11166 well. */
11167 callee = ada_to_fixed_value (coerce_ref (callee));
11168 }
d0c97917 11169 else if (callee->type ()->code () == TYPE_CODE_ARRAY
736355f2 11170 && callee->lval () == lval_memory)
efe3af2f
TT
11171 callee = value_addr (callee);
11172
d0c97917 11173 struct type *type = ada_check_typedef (callee->type ());
efe3af2f
TT
11174
11175 /* Ada allows us to implicitly dereference arrays when subscripting
11176 them. So, if this is an array typedef (encoding use for array
11177 access types encoded as fat pointers), strip it now. */
11178 if (type->code () == TYPE_CODE_TYPEDEF)
11179 type = ada_typedef_target_type (type);
11180
11181 if (type->code () == TYPE_CODE_PTR)
11182 {
27710edb 11183 switch (ada_check_typedef (type->target_type ())->code ())
efe3af2f
TT
11184 {
11185 case TYPE_CODE_FUNC:
27710edb 11186 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11187 break;
11188 case TYPE_CODE_ARRAY:
11189 break;
11190 case TYPE_CODE_STRUCT:
11191 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11192 callee = ada_value_ind (callee);
27710edb 11193 type = ada_check_typedef (type->target_type ());
efe3af2f
TT
11194 break;
11195 default:
11196 error (_("cannot subscript or call something of type `%s'"),
d0c97917 11197 ada_type_name (callee->type ()));
efe3af2f
TT
11198 break;
11199 }
11200 }
11201
11202 switch (type->code ())
11203 {
11204 case TYPE_CODE_FUNC:
11205 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11206 {
27710edb 11207 if (type->target_type () == NULL)
efe3af2f 11208 error_call_unknown_return_type (NULL);
317c3ed9 11209 return value::allocate (type->target_type ());
efe3af2f
TT
11210 }
11211 return call_function_by_hand (callee, NULL, argvec);
11212 case TYPE_CODE_INTERNAL_FUNCTION:
11213 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11214 /* We don't know anything about what the internal
11215 function might return, but we have to return
11216 something. */
ee7bb294 11217 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
efe3af2f
TT
11218 not_lval);
11219 else
11220 return call_internal_function (exp->gdbarch, exp->language_defn,
11221 callee, nargs,
11222 argvec.data ());
11223
d3c54a1c
TT
11224 case TYPE_CODE_STRUCT:
11225 {
11226 int arity;
4c4b4cd2 11227
d3c54a1c
TT
11228 arity = ada_array_arity (type);
11229 type = ada_array_element_type (type, nargs);
11230 if (type == NULL)
11231 error (_("cannot subscript or call a record"));
11232 if (arity != nargs)
11233 error (_("wrong number of subscripts; expecting %d"), arity);
11234 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 11235 return value::zero (ada_aligned_type (type), lval_memory);
d3c54a1c
TT
11236 return
11237 unwrap_value (ada_value_subscript
11238 (callee, nargs, argvec.data ()));
11239 }
11240 case TYPE_CODE_ARRAY:
14f9c5c9 11241 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11242 {
d3c54a1c
TT
11243 type = ada_array_element_type (type, nargs);
11244 if (type == NULL)
11245 error (_("element type of array unknown"));
dda83cd7 11246 else
ee7bb294 11247 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11248 }
d3c54a1c
TT
11249 return
11250 unwrap_value (ada_value_subscript
11251 (ada_coerce_to_simple_array (callee),
11252 nargs, argvec.data ()));
11253 case TYPE_CODE_PTR: /* Pointer to array */
11254 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11255 {
27710edb 11256 type = to_fixed_array_type (type->target_type (), NULL, 1);
d3c54a1c
TT
11257 type = ada_array_element_type (type, nargs);
11258 if (type == NULL)
11259 error (_("element type of array unknown"));
96967637 11260 else
ee7bb294 11261 return value::zero (ada_aligned_type (type), lval_memory);
dda83cd7 11262 }
d3c54a1c
TT
11263 return
11264 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11265 argvec.data ()));
6b0d7253 11266
d3c54a1c
TT
11267 default:
11268 error (_("Attempt to index or call something other than an "
11269 "array or function"));
11270 }
11271}
5b4ee69b 11272
d3c54a1c
TT
11273bool
11274ada_funcall_operation::resolve (struct expression *exp,
11275 bool deprocedure_p,
11276 bool parse_completion,
11277 innermost_block_tracker *tracker,
11278 struct type *context_type)
11279{
11280 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11281
d3c54a1c
TT
11282 ada_var_value_operation *avv
11283 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11284 if (avv == nullptr)
11285 return false;
5ec18f2b 11286
d3c54a1c 11287 symbol *sym = avv->get_symbol ();
6c9c307c 11288 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11289 return false;
dda83cd7 11290
d3c54a1c
TT
11291 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11292 int nargs = args_up.size ();
11293 std::vector<value *> argvec (nargs);
284614f0 11294
d3c54a1c
TT
11295 for (int i = 0; i < args_up.size (); ++i)
11296 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11297
d3c54a1c
TT
11298 const block *block = avv->get_block ();
11299 block_symbol resolved
11300 = ada_resolve_funcall (sym, block,
11301 context_type, parse_completion,
11302 nargs, argvec.data (),
11303 tracker);
11304
11305 std::get<0> (m_storage)
9e5e03df 11306 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11307 return false;
11308}
11309
11310bool
11311ada_ternop_slice_operation::resolve (struct expression *exp,
11312 bool deprocedure_p,
11313 bool parse_completion,
11314 innermost_block_tracker *tracker,
11315 struct type *context_type)
11316{
11317 /* Historically this check was done during resolution, so we
11318 continue that here. */
11319 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11320 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 11321 if (ada_is_any_packed_array_type (v->type ()))
d3c54a1c
TT
11322 error (_("cannot slice a packed array"));
11323 return false;
11324}
14f9c5c9 11325
14f9c5c9 11326}
d3c54a1c 11327
14f9c5c9 11328\f
d2e4a39e 11329
4c4b4cd2
PH
11330/* Return non-zero iff TYPE represents a System.Address type. */
11331
11332int
11333ada_is_system_address_type (struct type *type)
11334{
7d93a1e0 11335 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11336}
11337
14f9c5c9 11338\f
d2e4a39e 11339
dda83cd7 11340 /* Range types */
14f9c5c9
AS
11341
11342/* Scan STR beginning at position K for a discriminant name, and
11343 return the value of that discriminant field of DVAL in *PX. If
11344 PNEW_K is not null, put the position of the character beyond the
11345 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11346 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11347
11348static int
108d56a4 11349scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11350 int *pnew_k)
14f9c5c9 11351{
5f9febe0 11352 static std::string storage;
5da1a4d3 11353 const char *pstart, *pend, *bound;
d2e4a39e 11354 struct value *bound_val;
14f9c5c9
AS
11355
11356 if (dval == NULL || str == NULL || str[k] == '\0')
11357 return 0;
11358
5da1a4d3
SM
11359 pstart = str + k;
11360 pend = strstr (pstart, "__");
14f9c5c9
AS
11361 if (pend == NULL)
11362 {
5da1a4d3 11363 bound = pstart;
14f9c5c9
AS
11364 k += strlen (bound);
11365 }
d2e4a39e 11366 else
14f9c5c9 11367 {
5da1a4d3
SM
11368 int len = pend - pstart;
11369
11370 /* Strip __ and beyond. */
5f9febe0
TT
11371 storage = std::string (pstart, len);
11372 bound = storage.c_str ();
d2e4a39e 11373 k = pend - str;
14f9c5c9 11374 }
d2e4a39e 11375
d0c97917 11376 bound_val = ada_search_struct_field (bound, dval, 0, dval->type ());
14f9c5c9
AS
11377 if (bound_val == NULL)
11378 return 0;
11379
11380 *px = value_as_long (bound_val);
11381 if (pnew_k != NULL)
11382 *pnew_k = k;
11383 return 1;
11384}
11385
25a1127b
TT
11386/* Value of variable named NAME. Only exact matches are considered.
11387 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11388 otherwise causes an error with message ERR_MSG. */
11389
d2e4a39e 11390static struct value *
edb0c9cb 11391get_var_value (const char *name, const char *err_msg)
14f9c5c9 11392{
25a1127b
TT
11393 std::string quoted_name = add_angle_brackets (name);
11394
11395 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11396
d1183b06
TT
11397 std::vector<struct block_symbol> syms
11398 = ada_lookup_symbol_list_worker (lookup_name,
11399 get_selected_block (0),
11400 VAR_DOMAIN, 1);
14f9c5c9 11401
d1183b06 11402 if (syms.size () != 1)
14f9c5c9
AS
11403 {
11404 if (err_msg == NULL)
dda83cd7 11405 return 0;
14f9c5c9 11406 else
dda83cd7 11407 error (("%s"), err_msg);
14f9c5c9
AS
11408 }
11409
54d343a2 11410 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11411}
d2e4a39e 11412
edb0c9cb
PA
11413/* Value of integer variable named NAME in the current environment.
11414 If no such variable is found, returns false. Otherwise, sets VALUE
11415 to the variable's value and returns true. */
4c4b4cd2 11416
edb0c9cb
PA
11417bool
11418get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11419{
4c4b4cd2 11420 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11421
14f9c5c9 11422 if (var_val == 0)
edb0c9cb
PA
11423 return false;
11424
11425 value = value_as_long (var_val);
11426 return true;
14f9c5c9 11427}
d2e4a39e 11428
14f9c5c9
AS
11429
11430/* Return a range type whose base type is that of the range type named
11431 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11432 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11433 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11434 corresponding range type from debug information; fall back to using it
11435 if symbol lookup fails. If a new type must be created, allocate it
11436 like ORIG_TYPE was. The bounds information, in general, is encoded
11437 in NAME, the base type given in the named range type. */
14f9c5c9 11438
d2e4a39e 11439static struct type *
28c85d6c 11440to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11441{
0d5cff50 11442 const char *name;
14f9c5c9 11443 struct type *base_type;
108d56a4 11444 const char *subtype_info;
14f9c5c9 11445
28c85d6c 11446 gdb_assert (raw_type != NULL);
7d93a1e0 11447 gdb_assert (raw_type->name () != NULL);
dddfab26 11448
78134374 11449 if (raw_type->code () == TYPE_CODE_RANGE)
27710edb 11450 base_type = raw_type->target_type ();
14f9c5c9
AS
11451 else
11452 base_type = raw_type;
11453
7d93a1e0 11454 name = raw_type->name ();
14f9c5c9
AS
11455 subtype_info = strstr (name, "___XD");
11456 if (subtype_info == NULL)
690cc4eb 11457 {
43bbcdc2
PH
11458 LONGEST L = ada_discrete_type_low_bound (raw_type);
11459 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11460
690cc4eb
PH
11461 if (L < INT_MIN || U > INT_MAX)
11462 return raw_type;
11463 else
0c9c3474
SA
11464 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
11465 L, U);
690cc4eb 11466 }
14f9c5c9
AS
11467 else
11468 {
14f9c5c9
AS
11469 int prefix_len = subtype_info - name;
11470 LONGEST L, U;
11471 struct type *type;
108d56a4 11472 const char *bounds_str;
14f9c5c9
AS
11473 int n;
11474
14f9c5c9
AS
11475 subtype_info += 5;
11476 bounds_str = strchr (subtype_info, '_');
11477 n = 1;
11478
d2e4a39e 11479 if (*subtype_info == 'L')
dda83cd7
SM
11480 {
11481 if (!ada_scan_number (bounds_str, n, &L, &n)
11482 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11483 return raw_type;
11484 if (bounds_str[n] == '_')
11485 n += 2;
11486 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11487 n += 1;
11488 subtype_info += 1;
11489 }
d2e4a39e 11490 else
dda83cd7 11491 {
5f9febe0
TT
11492 std::string name_buf = std::string (name, prefix_len) + "___L";
11493 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11494 {
11495 lim_warning (_("Unknown lower bound, using 1."));
11496 L = 1;
11497 }
11498 }
14f9c5c9 11499
d2e4a39e 11500 if (*subtype_info == 'U')
dda83cd7
SM
11501 {
11502 if (!ada_scan_number (bounds_str, n, &U, &n)
11503 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11504 return raw_type;
11505 }
d2e4a39e 11506 else
dda83cd7 11507 {
5f9febe0
TT
11508 std::string name_buf = std::string (name, prefix_len) + "___U";
11509 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11510 {
11511 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11512 U = L;
11513 }
11514 }
14f9c5c9 11515
0c9c3474
SA
11516 type = create_static_range_type (alloc_type_copy (raw_type),
11517 base_type, L, U);
f5a91472 11518 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11519 to match the size of the base_type, which is not what we want.
11520 Set it back to the original range type's length. */
df86565b 11521 type->set_length (raw_type->length ());
d0e39ea2 11522 type->set_name (name);
14f9c5c9
AS
11523 return type;
11524 }
11525}
11526
4c4b4cd2
PH
11527/* True iff NAME is the name of a range type. */
11528
14f9c5c9 11529int
d2e4a39e 11530ada_is_range_type_name (const char *name)
14f9c5c9
AS
11531{
11532 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11533}
14f9c5c9 11534\f
d2e4a39e 11535
dda83cd7 11536 /* Modular types */
4c4b4cd2
PH
11537
11538/* True iff TYPE is an Ada modular type. */
14f9c5c9 11539
14f9c5c9 11540int
d2e4a39e 11541ada_is_modular_type (struct type *type)
14f9c5c9 11542{
18af8284 11543 struct type *subranged_type = get_base_type (type);
14f9c5c9 11544
78134374 11545 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11546 && subranged_type->code () == TYPE_CODE_INT
11547 && subranged_type->is_unsigned ());
14f9c5c9
AS
11548}
11549
4c4b4cd2
PH
11550/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11551
61ee279c 11552ULONGEST
0056e4d5 11553ada_modulus (struct type *type)
14f9c5c9 11554{
5e500d33
SM
11555 const dynamic_prop &high = type->bounds ()->high;
11556
11557 if (high.kind () == PROP_CONST)
11558 return (ULONGEST) high.const_val () + 1;
11559
11560 /* If TYPE is unresolved, the high bound might be a location list. Return
11561 0, for lack of a better value to return. */
11562 return 0;
14f9c5c9 11563}
d2e4a39e 11564\f
f7f9143b
JB
11565
11566/* Ada exception catchpoint support:
11567 ---------------------------------
11568
11569 We support 3 kinds of exception catchpoints:
11570 . catchpoints on Ada exceptions
11571 . catchpoints on unhandled Ada exceptions
11572 . catchpoints on failed assertions
11573
11574 Exceptions raised during failed assertions, or unhandled exceptions
11575 could perfectly be caught with the general catchpoint on Ada exceptions.
11576 However, we can easily differentiate these two special cases, and having
11577 the option to distinguish these two cases from the rest can be useful
11578 to zero-in on certain situations.
11579
11580 Exception catchpoints are a specialized form of breakpoint,
11581 since they rely on inserting breakpoints inside known routines
11582 of the GNAT runtime. The implementation therefore uses a standard
11583 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11584 of breakpoint_ops.
11585
0259addd
JB
11586 Support in the runtime for exception catchpoints have been changed
11587 a few times already, and these changes affect the implementation
11588 of these catchpoints. In order to be able to support several
11589 variants of the runtime, we use a sniffer that will determine
28010a5d 11590 the runtime variant used by the program being debugged. */
f7f9143b 11591
82eacd52
JB
11592/* Ada's standard exceptions.
11593
11594 The Ada 83 standard also defined Numeric_Error. But there so many
11595 situations where it was unclear from the Ada 83 Reference Manual
11596 (RM) whether Constraint_Error or Numeric_Error should be raised,
11597 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11598 Interpretation saying that anytime the RM says that Numeric_Error
11599 should be raised, the implementation may raise Constraint_Error.
11600 Ada 95 went one step further and pretty much removed Numeric_Error
11601 from the list of standard exceptions (it made it a renaming of
11602 Constraint_Error, to help preserve compatibility when compiling
11603 an Ada83 compiler). As such, we do not include Numeric_Error from
11604 this list of standard exceptions. */
3d0b0fa3 11605
27087b7f 11606static const char * const standard_exc[] = {
3d0b0fa3
JB
11607 "constraint_error",
11608 "program_error",
11609 "storage_error",
11610 "tasking_error"
11611};
11612
0259addd
JB
11613typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11614
11615/* A structure that describes how to support exception catchpoints
11616 for a given executable. */
11617
11618struct exception_support_info
11619{
11620 /* The name of the symbol to break on in order to insert
11621 a catchpoint on exceptions. */
11622 const char *catch_exception_sym;
11623
11624 /* The name of the symbol to break on in order to insert
11625 a catchpoint on unhandled exceptions. */
11626 const char *catch_exception_unhandled_sym;
11627
11628 /* The name of the symbol to break on in order to insert
11629 a catchpoint on failed assertions. */
11630 const char *catch_assert_sym;
11631
9f757bf7
XR
11632 /* The name of the symbol to break on in order to insert
11633 a catchpoint on exception handling. */
11634 const char *catch_handlers_sym;
11635
0259addd
JB
11636 /* Assuming that the inferior just triggered an unhandled exception
11637 catchpoint, this function is responsible for returning the address
11638 in inferior memory where the name of that exception is stored.
11639 Return zero if the address could not be computed. */
11640 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11641};
11642
11643static CORE_ADDR ada_unhandled_exception_name_addr (void);
11644static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11645
11646/* The following exception support info structure describes how to
11647 implement exception catchpoints with the latest version of the
ca683e3a 11648 Ada runtime (as of 2019-08-??). */
0259addd
JB
11649
11650static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11651{
11652 "__gnat_debug_raise_exception", /* catch_exception_sym */
11653 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11654 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11655 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11656 ada_unhandled_exception_name_addr
11657};
11658
11659/* The following exception support info structure describes how to
11660 implement exception catchpoints with an earlier version of the
11661 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11662
11663static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11664{
11665 "__gnat_debug_raise_exception", /* catch_exception_sym */
11666 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11667 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11668 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11669 ada_unhandled_exception_name_addr
11670};
11671
11672/* The following exception support info structure describes how to
11673 implement exception catchpoints with a slightly older version
11674 of the Ada runtime. */
11675
11676static const struct exception_support_info exception_support_info_fallback =
11677{
11678 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11679 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11680 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11681 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11682 ada_unhandled_exception_name_addr_from_raise
11683};
11684
f17011e0
JB
11685/* Return nonzero if we can detect the exception support routines
11686 described in EINFO.
11687
11688 This function errors out if an abnormal situation is detected
11689 (for instance, if we find the exception support routines, but
11690 that support is found to be incomplete). */
11691
11692static int
11693ada_has_this_exception_support (const struct exception_support_info *einfo)
11694{
11695 struct symbol *sym;
11696
11697 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11698 that should be compiled with debugging information. As a result, we
11699 expect to find that symbol in the symtabs. */
11700
11701 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11702 if (sym == NULL)
a6af7abe
JB
11703 {
11704 /* Perhaps we did not find our symbol because the Ada runtime was
11705 compiled without debugging info, or simply stripped of it.
11706 It happens on some GNU/Linux distributions for instance, where
11707 users have to install a separate debug package in order to get
11708 the runtime's debugging info. In that situation, let the user
11709 know why we cannot insert an Ada exception catchpoint.
11710
11711 Note: Just for the purpose of inserting our Ada exception
11712 catchpoint, we could rely purely on the associated minimal symbol.
11713 But we would be operating in degraded mode anyway, since we are
11714 still lacking the debugging info needed later on to extract
11715 the name of the exception being raised (this name is printed in
11716 the catchpoint message, and is also used when trying to catch
11717 a specific exception). We do not handle this case for now. */
3b7344d5 11718 struct bound_minimal_symbol msym
1c8e84b0
JB
11719 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11720
60f62e2b 11721 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
a6af7abe
JB
11722 error (_("Your Ada runtime appears to be missing some debugging "
11723 "information.\nCannot insert Ada exception catchpoint "
11724 "in this configuration."));
11725
11726 return 0;
11727 }
f17011e0
JB
11728
11729 /* Make sure that the symbol we found corresponds to a function. */
11730
66d7f48f 11731 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11732 error (_("Symbol \"%s\" is not a function (class = %d)"),
11733 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11734
11735 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11736 if (sym == NULL)
11737 {
11738 struct bound_minimal_symbol msym
11739 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11740
60f62e2b 11741 if (msym.minsym && msym.minsym->type () != mst_solib_trampoline)
ca683e3a
AO
11742 error (_("Your Ada runtime appears to be missing some debugging "
11743 "information.\nCannot insert Ada exception catchpoint "
11744 "in this configuration."));
11745
11746 return 0;
11747 }
11748
11749 /* Make sure that the symbol we found corresponds to a function. */
11750
66d7f48f 11751 if (sym->aclass () != LOC_BLOCK)
fe043185
TT
11752 error (_("Symbol \"%s\" is not a function (class = %d)"),
11753 sym->linkage_name (), sym->aclass ());
f17011e0
JB
11754
11755 return 1;
11756}
11757
0259addd
JB
11758/* Inspect the Ada runtime and determine which exception info structure
11759 should be used to provide support for exception catchpoints.
11760
3eecfa55
JB
11761 This function will always set the per-inferior exception_info,
11762 or raise an error. */
0259addd
JB
11763
11764static void
11765ada_exception_support_info_sniffer (void)
11766{
3eecfa55 11767 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11768
11769 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11770 if (data->exception_info != NULL)
0259addd
JB
11771 return;
11772
11773 /* Check the latest (default) exception support info. */
f17011e0 11774 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11775 {
3eecfa55 11776 data->exception_info = &default_exception_support_info;
0259addd
JB
11777 return;
11778 }
11779
ca683e3a
AO
11780 /* Try the v0 exception suport info. */
11781 if (ada_has_this_exception_support (&exception_support_info_v0))
11782 {
11783 data->exception_info = &exception_support_info_v0;
11784 return;
11785 }
11786
0259addd 11787 /* Try our fallback exception suport info. */
f17011e0 11788 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11789 {
3eecfa55 11790 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11791 return;
11792 }
11793
11794 /* Sometimes, it is normal for us to not be able to find the routine
11795 we are looking for. This happens when the program is linked with
11796 the shared version of the GNAT runtime, and the program has not been
11797 started yet. Inform the user of these two possible causes if
11798 applicable. */
11799
ccefe4c4 11800 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11801 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11802
11803 /* If the symbol does not exist, then check that the program is
11804 already started, to make sure that shared libraries have been
11805 loaded. If it is not started, this may mean that the symbol is
11806 in a shared library. */
11807
e99b03dc 11808 if (inferior_ptid.pid () == 0)
0259addd
JB
11809 error (_("Unable to insert catchpoint. Try to start the program first."));
11810
11811 /* At this point, we know that we are debugging an Ada program and
11812 that the inferior has been started, but we still are not able to
0963b4bd 11813 find the run-time symbols. That can mean that we are in
0259addd
JB
11814 configurable run time mode, or that a-except as been optimized
11815 out by the linker... In any case, at this point it is not worth
11816 supporting this feature. */
11817
7dda8cff 11818 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11819}
11820
f7f9143b
JB
11821/* True iff FRAME is very likely to be that of a function that is
11822 part of the runtime system. This is all very heuristic, but is
11823 intended to be used as advice as to what frames are uninteresting
11824 to most users. */
11825
11826static int
bd2b40ac 11827is_known_support_routine (frame_info_ptr frame)
f7f9143b 11828{
692465f1 11829 enum language func_lang;
f7f9143b 11830 int i;
f35a17b5 11831 const char *fullname;
f7f9143b 11832
4ed6b5be
JB
11833 /* If this code does not have any debugging information (no symtab),
11834 This cannot be any user code. */
f7f9143b 11835
51abb421 11836 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11837 if (sal.symtab == NULL)
11838 return 1;
11839
4ed6b5be
JB
11840 /* If there is a symtab, but the associated source file cannot be
11841 located, then assume this is not user code: Selecting a frame
11842 for which we cannot display the code would not be very helpful
11843 for the user. This should also take care of case such as VxWorks
11844 where the kernel has some debugging info provided for a few units. */
f7f9143b 11845
f35a17b5
JK
11846 fullname = symtab_to_fullname (sal.symtab);
11847 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11848 return 1;
11849
85102364 11850 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11851 We also check the name of the objfile against the name of some
11852 known system libraries that sometimes come with debugging info
11853 too. */
11854
f7f9143b
JB
11855 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11856 {
11857 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11858 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11859 return 1;
3c86fae3
SM
11860 if (sal.symtab->compunit ()->objfile () != NULL
11861 && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
dda83cd7 11862 return 1;
f7f9143b
JB
11863 }
11864
4ed6b5be 11865 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11866
c6dc63a1
TT
11867 gdb::unique_xmalloc_ptr<char> func_name
11868 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11869 if (func_name == NULL)
11870 return 1;
11871
11872 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11873 {
11874 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11875 if (re_exec (func_name.get ()))
11876 return 1;
f7f9143b
JB
11877 }
11878
11879 return 0;
11880}
11881
11882/* Find the first frame that contains debugging information and that is not
11883 part of the Ada run-time, starting from FI and moving upward. */
11884
0ef643c8 11885void
bd2b40ac 11886ada_find_printable_frame (frame_info_ptr fi)
f7f9143b
JB
11887{
11888 for (; fi != NULL; fi = get_prev_frame (fi))
11889 {
11890 if (!is_known_support_routine (fi))
dda83cd7
SM
11891 {
11892 select_frame (fi);
11893 break;
11894 }
f7f9143b
JB
11895 }
11896
11897}
11898
11899/* Assuming that the inferior just triggered an unhandled exception
11900 catchpoint, return the address in inferior memory where the name
11901 of the exception is stored.
11902
11903 Return zero if the address could not be computed. */
11904
11905static CORE_ADDR
11906ada_unhandled_exception_name_addr (void)
0259addd
JB
11907{
11908 return parse_and_eval_address ("e.full_name");
11909}
11910
11911/* Same as ada_unhandled_exception_name_addr, except that this function
11912 should be used when the inferior uses an older version of the runtime,
11913 where the exception name needs to be extracted from a specific frame
11914 several frames up in the callstack. */
11915
11916static CORE_ADDR
11917ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11918{
11919 int frame_level;
bd2b40ac 11920 frame_info_ptr fi;
3eecfa55 11921 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11922
11923 /* To determine the name of this exception, we need to select
11924 the frame corresponding to RAISE_SYM_NAME. This frame is
11925 at least 3 levels up, so we simply skip the first 3 frames
11926 without checking the name of their associated function. */
11927 fi = get_current_frame ();
11928 for (frame_level = 0; frame_level < 3; frame_level += 1)
11929 if (fi != NULL)
11930 fi = get_prev_frame (fi);
11931
11932 while (fi != NULL)
11933 {
692465f1
JB
11934 enum language func_lang;
11935
c6dc63a1
TT
11936 gdb::unique_xmalloc_ptr<char> func_name
11937 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11938 if (func_name != NULL)
11939 {
dda83cd7 11940 if (strcmp (func_name.get (),
55b87a52
KS
11941 data->exception_info->catch_exception_sym) == 0)
11942 break; /* We found the frame we were looking for... */
55b87a52 11943 }
fb44b1a7 11944 fi = get_prev_frame (fi);
f7f9143b
JB
11945 }
11946
11947 if (fi == NULL)
11948 return 0;
11949
11950 select_frame (fi);
11951 return parse_and_eval_address ("id.full_name");
11952}
11953
11954/* Assuming the inferior just triggered an Ada exception catchpoint
11955 (of any type), return the address in inferior memory where the name
11956 of the exception is stored, if applicable.
11957
45db7c09
PA
11958 Assumes the selected frame is the current frame.
11959
f7f9143b
JB
11960 Return zero if the address could not be computed, or if not relevant. */
11961
11962static CORE_ADDR
7bd86313 11963ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex)
f7f9143b 11964{
3eecfa55
JB
11965 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11966
f7f9143b
JB
11967 switch (ex)
11968 {
761269c8 11969 case ada_catch_exception:
dda83cd7
SM
11970 return (parse_and_eval_address ("e.full_name"));
11971 break;
f7f9143b 11972
761269c8 11973 case ada_catch_exception_unhandled:
dda83cd7
SM
11974 return data->exception_info->unhandled_exception_name_addr ();
11975 break;
9f757bf7
XR
11976
11977 case ada_catch_handlers:
dda83cd7 11978 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11979 name. */
dda83cd7 11980 break;
9f757bf7 11981
761269c8 11982 case ada_catch_assert:
dda83cd7
SM
11983 return 0; /* Exception name is not relevant in this case. */
11984 break;
f7f9143b
JB
11985
11986 default:
f34652de 11987 internal_error (_("unexpected catchpoint type"));
dda83cd7 11988 break;
f7f9143b
JB
11989 }
11990
11991 return 0; /* Should never be reached. */
11992}
11993
e547c119
JB
11994/* Assuming the inferior is stopped at an exception catchpoint,
11995 return the message which was associated to the exception, if
11996 available. Return NULL if the message could not be retrieved.
11997
e547c119
JB
11998 Note: The exception message can be associated to an exception
11999 either through the use of the Raise_Exception function, or
12000 more simply (Ada 2005 and later), via:
12001
12002 raise Exception_Name with "exception message";
12003
12004 */
12005
6f46ac85 12006static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12007ada_exception_message_1 (void)
12008{
12009 struct value *e_msg_val;
e547c119 12010 int e_msg_len;
e547c119
JB
12011
12012 /* For runtimes that support this feature, the exception message
12013 is passed as an unbounded string argument called "message". */
12014 e_msg_val = parse_and_eval ("message");
12015 if (e_msg_val == NULL)
12016 return NULL; /* Exception message not supported. */
12017
12018 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
12019 gdb_assert (e_msg_val != NULL);
d0c97917 12020 e_msg_len = e_msg_val->type ()->length ();
e547c119
JB
12021
12022 /* If the message string is empty, then treat it as if there was
12023 no exception message. */
12024 if (e_msg_len <= 0)
12025 return NULL;
12026
15f3b077 12027 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
9feb2d07 12028 read_memory (e_msg_val->address (), (gdb_byte *) e_msg.get (),
15f3b077
TT
12029 e_msg_len);
12030 e_msg.get ()[e_msg_len] = '\0';
12031
12032 return e_msg;
e547c119
JB
12033}
12034
12035/* Same as ada_exception_message_1, except that all exceptions are
12036 contained here (returning NULL instead). */
12037
6f46ac85 12038static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12039ada_exception_message (void)
12040{
6f46ac85 12041 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 12042
a70b8144 12043 try
e547c119
JB
12044 {
12045 e_msg = ada_exception_message_1 ();
12046 }
230d2906 12047 catch (const gdb_exception_error &e)
e547c119 12048 {
6f46ac85 12049 e_msg.reset (nullptr);
e547c119 12050 }
e547c119
JB
12051
12052 return e_msg;
12053}
12054
f7f9143b
JB
12055/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12056 any error that ada_exception_name_addr_1 might cause to be thrown.
12057 When an error is intercepted, a warning with the error message is printed,
12058 and zero is returned. */
12059
12060static CORE_ADDR
7bd86313 12061ada_exception_name_addr (enum ada_exception_catchpoint_kind ex)
f7f9143b 12062{
f7f9143b
JB
12063 CORE_ADDR result = 0;
12064
a70b8144 12065 try
f7f9143b 12066 {
7bd86313 12067 result = ada_exception_name_addr_1 (ex);
f7f9143b
JB
12068 }
12069
230d2906 12070 catch (const gdb_exception_error &e)
f7f9143b 12071 {
3d6e9d23 12072 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12073 return 0;
12074 }
12075
12076 return result;
12077}
12078
cb7de75e 12079static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12080 (const char *excep_string,
12081 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12082
12083/* Ada catchpoints.
12084
12085 In the case of catchpoints on Ada exceptions, the catchpoint will
12086 stop the target on every exception the program throws. When a user
12087 specifies the name of a specific exception, we translate this
12088 request into a condition expression (in text form), and then parse
12089 it into an expression stored in each of the catchpoint's locations.
12090 We then use this condition to check whether the exception that was
12091 raised is the one the user is interested in. If not, then the
12092 target is resumed again. We store the name of the requested
12093 exception, in order to be able to re-set the condition expression
12094 when symbols change. */
12095
c1fc2657 12096/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12097
74421c0b 12098struct ada_catchpoint : public code_breakpoint
28010a5d 12099{
73063f51 12100 ada_catchpoint (struct gdbarch *gdbarch_,
bd21b6c9
PA
12101 enum ada_exception_catchpoint_kind kind,
12102 struct symtab_and_line sal,
12103 const char *addr_string_,
12104 bool tempflag,
12105 bool enabled,
12106 bool from_tty)
74421c0b 12107 : code_breakpoint (gdbarch_, bp_catchpoint),
73063f51 12108 m_kind (kind)
37f6a7f4 12109 {
bd21b6c9
PA
12110 add_location (sal);
12111
74421c0b 12112 /* Unlike most code_breakpoint types, Ada catchpoints are
bd21b6c9
PA
12113 pspace-specific. */
12114 gdb_assert (sal.pspace != nullptr);
12115 this->pspace = sal.pspace;
12116
12117 if (from_tty)
12118 {
12119 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12120 if (!loc_gdbarch)
12121 loc_gdbarch = gdbarch;
12122
12123 describe_other_breakpoints (loc_gdbarch,
12124 sal.pspace, sal.pc, sal.section, -1);
12125 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12126 version for exception catchpoints, because two catchpoints
12127 used for different exception names will use the same address.
12128 In this case, a "breakpoint ... also set at..." warning is
12129 unproductive. Besides, the warning phrasing is also a bit
12130 inappropriate, we should use the word catchpoint, and tell
12131 the user what type of catchpoint it is. The above is good
12132 enough for now, though. */
12133 }
12134
12135 enable_state = enabled ? bp_enabled : bp_disabled;
12136 disposition = tempflag ? disp_del : disp_donttouch;
264f9890
PA
12137 locspec = string_to_location_spec (&addr_string_,
12138 language_def (language_ada));
bd21b6c9 12139 language = language_ada;
37f6a7f4
TT
12140 }
12141
ae72050b
TT
12142 struct bp_location *allocate_location () override;
12143 void re_set () override;
12144 void check_status (struct bpstat *bs) override;
7bd86313 12145 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 12146 bool print_one (bp_location **) const override;
b713485d 12147 void print_mention () const override;
4d1ae558 12148 void print_recreate (struct ui_file *fp) const override;
ae72050b 12149
28010a5d 12150 /* The name of the specific exception the user specified. */
bc18fbb5 12151 std::string excep_string;
37f6a7f4
TT
12152
12153 /* What kind of catchpoint this is. */
12154 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12155};
12156
8cd0bf5e
PA
12157/* An instance of this type is used to represent an Ada catchpoint
12158 breakpoint location. */
12159
12160class ada_catchpoint_location : public bp_location
12161{
12162public:
12163 explicit ada_catchpoint_location (ada_catchpoint *owner)
12164 : bp_location (owner, bp_loc_software_breakpoint)
12165 {}
12166
12167 /* The condition that checks whether the exception that was raised
12168 is the specific exception the user specified on catchpoint
12169 creation. */
12170 expression_up excep_cond_expr;
12171};
12172
28010a5d
PA
12173/* Parse the exception condition string in the context of each of the
12174 catchpoint's locations, and store them for later evaluation. */
12175
12176static void
9f757bf7 12177create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 12178 enum ada_exception_catchpoint_kind ex)
28010a5d 12179{
28010a5d 12180 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 12181 if (c->excep_string.empty ())
28010a5d
PA
12182 return;
12183
12184 /* Same if there are no locations... */
c1fc2657 12185 if (c->loc == NULL)
28010a5d
PA
12186 return;
12187
fccf9de1
TT
12188 /* Compute the condition expression in text form, from the specific
12189 expection we want to catch. */
12190 std::string cond_string
12191 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 12192
fccf9de1
TT
12193 /* Iterate over all the catchpoint's locations, and parse an
12194 expression for each. */
40cb8ca5 12195 for (bp_location *bl : c->locations ())
28010a5d
PA
12196 {
12197 struct ada_catchpoint_location *ada_loc
fccf9de1 12198 = (struct ada_catchpoint_location *) bl;
4d01a485 12199 expression_up exp;
28010a5d 12200
fccf9de1 12201 if (!bl->shlib_disabled)
28010a5d 12202 {
bbc13ae3 12203 const char *s;
28010a5d 12204
cb7de75e 12205 s = cond_string.c_str ();
a70b8144 12206 try
28010a5d 12207 {
fccf9de1
TT
12208 exp = parse_exp_1 (&s, bl->address,
12209 block_for_pc (bl->address),
036e657b 12210 0);
28010a5d 12211 }
230d2906 12212 catch (const gdb_exception_error &e)
849f2b52
JB
12213 {
12214 warning (_("failed to reevaluate internal exception condition "
12215 "for catchpoint %d: %s"),
3d6e9d23 12216 c->number, e.what ());
849f2b52 12217 }
28010a5d
PA
12218 }
12219
b22e99fd 12220 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 12221 }
28010a5d
PA
12222}
12223
ae72050b
TT
12224/* Implement the ALLOCATE_LOCATION method in the structure for all
12225 exception catchpoint kinds. */
28010a5d 12226
ae72050b
TT
12227struct bp_location *
12228ada_catchpoint::allocate_location ()
28010a5d 12229{
ae72050b 12230 return new ada_catchpoint_location (this);
28010a5d
PA
12231}
12232
ae72050b
TT
12233/* Implement the RE_SET method in the structure for all exception
12234 catchpoint kinds. */
28010a5d 12235
ae72050b
TT
12236void
12237ada_catchpoint::re_set ()
28010a5d 12238{
28010a5d
PA
12239 /* Call the base class's method. This updates the catchpoint's
12240 locations. */
74421c0b 12241 this->code_breakpoint::re_set ();
28010a5d
PA
12242
12243 /* Reparse the exception conditional expressions. One for each
12244 location. */
ae72050b 12245 create_excep_cond_exprs (this, m_kind);
28010a5d
PA
12246}
12247
12248/* Returns true if we should stop for this breakpoint hit. If the
12249 user specified a specific exception, we only want to cause a stop
12250 if the program thrown that exception. */
12251
7ebaa5f7 12252static bool
28010a5d
PA
12253should_stop_exception (const struct bp_location *bl)
12254{
12255 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12256 const struct ada_catchpoint_location *ada_loc
12257 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12258 bool stop;
28010a5d 12259
37f6a7f4
TT
12260 struct internalvar *var = lookup_internalvar ("_ada_exception");
12261 if (c->m_kind == ada_catch_assert)
12262 clear_internalvar (var);
12263 else
12264 {
12265 try
12266 {
12267 const char *expr;
12268
12269 if (c->m_kind == ada_catch_handlers)
12270 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12271 ".all.occurrence.id");
12272 else
12273 expr = "e";
12274
12275 struct value *exc = parse_and_eval (expr);
12276 set_internalvar (var, exc);
12277 }
12278 catch (const gdb_exception_error &ex)
12279 {
12280 clear_internalvar (var);
12281 }
12282 }
12283
28010a5d 12284 /* With no specific exception, should always stop. */
bc18fbb5 12285 if (c->excep_string.empty ())
7ebaa5f7 12286 return true;
28010a5d
PA
12287
12288 if (ada_loc->excep_cond_expr == NULL)
12289 {
12290 /* We will have a NULL expression if back when we were creating
12291 the expressions, this location's had failed to parse. */
7ebaa5f7 12292 return true;
28010a5d
PA
12293 }
12294
7ebaa5f7 12295 stop = true;
a70b8144 12296 try
28010a5d 12297 {
65558ca5 12298 scoped_value_mark mark;
4d01a485 12299 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
28010a5d 12300 }
230d2906 12301 catch (const gdb_exception &ex)
492d29ea
PA
12302 {
12303 exception_fprintf (gdb_stderr, ex,
12304 _("Error in testing exception condition:\n"));
12305 }
492d29ea 12306
28010a5d
PA
12307 return stop;
12308}
12309
ae72050b
TT
12310/* Implement the CHECK_STATUS method in the structure for all
12311 exception catchpoint kinds. */
28010a5d 12312
ae72050b
TT
12313void
12314ada_catchpoint::check_status (bpstat *bs)
28010a5d 12315{
b6433ede 12316 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12317}
12318
ae72050b
TT
12319/* Implement the PRINT_IT method in the structure for all exception
12320 catchpoint kinds. */
f7f9143b 12321
ae72050b 12322enum print_stop_action
7bd86313 12323ada_catchpoint::print_it (const bpstat *bs) const
f7f9143b 12324{
79a45e25 12325 struct ui_out *uiout = current_uiout;
348d480f 12326
ae72050b 12327 annotate_catchpoint (number);
f7f9143b 12328
112e8700 12329 if (uiout->is_mi_like_p ())
f7f9143b 12330 {
112e8700 12331 uiout->field_string ("reason",
956a9fb9 12332 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ae72050b 12333 uiout->field_string ("disp", bpdisp_text (disposition));
f7f9143b
JB
12334 }
12335
ae72050b 12336 uiout->text (disposition == disp_del
112e8700 12337 ? "\nTemporary catchpoint " : "\nCatchpoint ");
78805ff8 12338 print_num_locno (bs, uiout);
112e8700 12339 uiout->text (", ");
f7f9143b 12340
45db7c09
PA
12341 /* ada_exception_name_addr relies on the selected frame being the
12342 current frame. Need to do this here because this function may be
12343 called more than once when printing a stop, and below, we'll
12344 select the first frame past the Ada run-time (see
12345 ada_find_printable_frame). */
12346 select_frame (get_current_frame ());
12347
ae72050b 12348 switch (m_kind)
f7f9143b 12349 {
761269c8
JB
12350 case ada_catch_exception:
12351 case ada_catch_exception_unhandled:
9f757bf7 12352 case ada_catch_handlers:
956a9fb9 12353 {
7bd86313 12354 const CORE_ADDR addr = ada_exception_name_addr (m_kind);
956a9fb9
JB
12355 char exception_name[256];
12356
12357 if (addr != 0)
12358 {
c714b426
PA
12359 read_memory (addr, (gdb_byte *) exception_name,
12360 sizeof (exception_name) - 1);
956a9fb9
JB
12361 exception_name [sizeof (exception_name) - 1] = '\0';
12362 }
12363 else
12364 {
12365 /* For some reason, we were unable to read the exception
12366 name. This could happen if the Runtime was compiled
12367 without debugging info, for instance. In that case,
12368 just replace the exception name by the generic string
12369 "exception" - it will read as "an exception" in the
12370 notification we are about to print. */
967cff16 12371 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12372 }
12373 /* In the case of unhandled exception breakpoints, we print
12374 the exception name as "unhandled EXCEPTION_NAME", to make
12375 it clearer to the user which kind of catchpoint just got
12376 hit. We used ui_out_text to make sure that this extra
12377 info does not pollute the exception name in the MI case. */
ae72050b 12378 if (m_kind == ada_catch_exception_unhandled)
112e8700
SM
12379 uiout->text ("unhandled ");
12380 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12381 }
12382 break;
761269c8 12383 case ada_catch_assert:
956a9fb9
JB
12384 /* In this case, the name of the exception is not really
12385 important. Just print "failed assertion" to make it clearer
12386 that his program just hit an assertion-failure catchpoint.
12387 We used ui_out_text because this info does not belong in
12388 the MI output. */
112e8700 12389 uiout->text ("failed assertion");
956a9fb9 12390 break;
f7f9143b 12391 }
e547c119 12392
6f46ac85 12393 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12394 if (exception_message != NULL)
12395 {
e547c119 12396 uiout->text (" (");
6f46ac85 12397 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12398 uiout->text (")");
e547c119
JB
12399 }
12400
112e8700 12401 uiout->text (" at ");
956a9fb9 12402 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12403
12404 return PRINT_SRC_AND_LOC;
12405}
12406
ae72050b
TT
12407/* Implement the PRINT_ONE method in the structure for all exception
12408 catchpoint kinds. */
f7f9143b 12409
ae72050b 12410bool
a67bcaba 12411ada_catchpoint::print_one (bp_location **last_loc) const
f7f9143b 12412{
79a45e25 12413 struct ui_out *uiout = current_uiout;
79a45b7d
TT
12414 struct value_print_options opts;
12415
12416 get_user_print_options (&opts);
f06f1252 12417
79a45b7d 12418 if (opts.addressprint)
f06f1252 12419 uiout->field_skip ("addr");
f7f9143b
JB
12420
12421 annotate_field (5);
ae72050b 12422 switch (m_kind)
f7f9143b 12423 {
761269c8 12424 case ada_catch_exception:
ae72050b 12425 if (!excep_string.empty ())
dda83cd7 12426 {
bc18fbb5 12427 std::string msg = string_printf (_("`%s' Ada exception"),
ae72050b 12428 excep_string.c_str ());
28010a5d 12429
dda83cd7
SM
12430 uiout->field_string ("what", msg);
12431 }
12432 else
12433 uiout->field_string ("what", "all Ada exceptions");
12434
12435 break;
f7f9143b 12436
761269c8 12437 case ada_catch_exception_unhandled:
dda83cd7
SM
12438 uiout->field_string ("what", "unhandled Ada exceptions");
12439 break;
f7f9143b 12440
9f757bf7 12441 case ada_catch_handlers:
ae72050b 12442 if (!excep_string.empty ())
dda83cd7 12443 {
9f757bf7
XR
12444 uiout->field_fmt ("what",
12445 _("`%s' Ada exception handlers"),
ae72050b 12446 excep_string.c_str ());
dda83cd7
SM
12447 }
12448 else
9f757bf7 12449 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12450 break;
9f757bf7 12451
761269c8 12452 case ada_catch_assert:
dda83cd7
SM
12453 uiout->field_string ("what", "failed Ada assertions");
12454 break;
f7f9143b
JB
12455
12456 default:
f34652de 12457 internal_error (_("unexpected catchpoint type"));
dda83cd7 12458 break;
f7f9143b 12459 }
c01e038b
TT
12460
12461 return true;
f7f9143b
JB
12462}
12463
12464/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12465 for all exception catchpoint kinds. */
12466
ae72050b 12467void
b713485d 12468ada_catchpoint::print_mention () const
f7f9143b 12469{
79a45e25 12470 struct ui_out *uiout = current_uiout;
28010a5d 12471
ae72050b 12472 uiout->text (disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12473 : _("Catchpoint "));
ae72050b 12474 uiout->field_signed ("bkptno", number);
112e8700 12475 uiout->text (": ");
00eb2c4a 12476
ae72050b 12477 switch (m_kind)
f7f9143b 12478 {
761269c8 12479 case ada_catch_exception:
ae72050b 12480 if (!excep_string.empty ())
00eb2c4a 12481 {
862d101a 12482 std::string info = string_printf (_("`%s' Ada exception"),
ae72050b 12483 excep_string.c_str ());
4915bfdc 12484 uiout->text (info);
00eb2c4a 12485 }
dda83cd7
SM
12486 else
12487 uiout->text (_("all Ada exceptions"));
12488 break;
f7f9143b 12489
761269c8 12490 case ada_catch_exception_unhandled:
dda83cd7
SM
12491 uiout->text (_("unhandled Ada exceptions"));
12492 break;
9f757bf7
XR
12493
12494 case ada_catch_handlers:
ae72050b 12495 if (!excep_string.empty ())
9f757bf7
XR
12496 {
12497 std::string info
12498 = string_printf (_("`%s' Ada exception handlers"),
ae72050b 12499 excep_string.c_str ());
4915bfdc 12500 uiout->text (info);
9f757bf7 12501 }
dda83cd7
SM
12502 else
12503 uiout->text (_("all Ada exceptions handlers"));
12504 break;
9f757bf7 12505
761269c8 12506 case ada_catch_assert:
dda83cd7
SM
12507 uiout->text (_("failed Ada assertions"));
12508 break;
f7f9143b
JB
12509
12510 default:
f34652de 12511 internal_error (_("unexpected catchpoint type"));
dda83cd7 12512 break;
f7f9143b
JB
12513 }
12514}
12515
ae72050b
TT
12516/* Implement the PRINT_RECREATE method in the structure for all
12517 exception catchpoint kinds. */
6149aea9 12518
ae72050b 12519void
4d1ae558 12520ada_catchpoint::print_recreate (struct ui_file *fp) const
6149aea9 12521{
ae72050b 12522 switch (m_kind)
6149aea9 12523 {
761269c8 12524 case ada_catch_exception:
6cb06a8c 12525 gdb_printf (fp, "catch exception");
ae72050b
TT
12526 if (!excep_string.empty ())
12527 gdb_printf (fp, " %s", excep_string.c_str ());
6149aea9
PA
12528 break;
12529
761269c8 12530 case ada_catch_exception_unhandled:
6cb06a8c 12531 gdb_printf (fp, "catch exception unhandled");
6149aea9
PA
12532 break;
12533
9f757bf7 12534 case ada_catch_handlers:
6cb06a8c 12535 gdb_printf (fp, "catch handlers");
9f757bf7
XR
12536 break;
12537
761269c8 12538 case ada_catch_assert:
6cb06a8c 12539 gdb_printf (fp, "catch assert");
6149aea9
PA
12540 break;
12541
12542 default:
f34652de 12543 internal_error (_("unexpected catchpoint type"));
6149aea9 12544 }
04d0163c 12545 print_recreate_thread (fp);
6149aea9
PA
12546}
12547
f06f1252
TT
12548/* See ada-lang.h. */
12549
12550bool
12551is_ada_exception_catchpoint (breakpoint *bp)
12552{
ae72050b 12553 return dynamic_cast<ada_catchpoint *> (bp) != nullptr;
f06f1252
TT
12554}
12555
f7f9143b
JB
12556/* Split the arguments specified in a "catch exception" command.
12557 Set EX to the appropriate catchpoint type.
28010a5d 12558 Set EXCEP_STRING to the name of the specific exception if
5845583d 12559 specified by the user.
9f757bf7
XR
12560 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12561 "catch handlers" command. False otherwise.
5845583d
JB
12562 If a condition is found at the end of the arguments, the condition
12563 expression is stored in COND_STRING (memory must be deallocated
12564 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12565
12566static void
a121b7c1 12567catch_ada_exception_command_split (const char *args,
9f757bf7 12568 bool is_catch_handlers_cmd,
dda83cd7 12569 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12570 std::string *excep_string,
12571 std::string *cond_string)
f7f9143b 12572{
bc18fbb5 12573 std::string exception_name;
f7f9143b 12574
bc18fbb5
TT
12575 exception_name = extract_arg (&args);
12576 if (exception_name == "if")
5845583d
JB
12577 {
12578 /* This is not an exception name; this is the start of a condition
12579 expression for a catchpoint on all exceptions. So, "un-get"
12580 this token, and set exception_name to NULL. */
bc18fbb5 12581 exception_name.clear ();
5845583d
JB
12582 args -= 2;
12583 }
f7f9143b 12584
5845583d 12585 /* Check to see if we have a condition. */
f7f9143b 12586
f1735a53 12587 args = skip_spaces (args);
61012eef 12588 if (startswith (args, "if")
5845583d
JB
12589 && (isspace (args[2]) || args[2] == '\0'))
12590 {
12591 args += 2;
f1735a53 12592 args = skip_spaces (args);
5845583d
JB
12593
12594 if (args[0] == '\0')
dda83cd7 12595 error (_("Condition missing after `if' keyword"));
bc18fbb5 12596 *cond_string = args;
5845583d
JB
12597
12598 args += strlen (args);
12599 }
12600
12601 /* Check that we do not have any more arguments. Anything else
12602 is unexpected. */
f7f9143b
JB
12603
12604 if (args[0] != '\0')
12605 error (_("Junk at end of expression"));
12606
9f757bf7
XR
12607 if (is_catch_handlers_cmd)
12608 {
12609 /* Catch handling of exceptions. */
12610 *ex = ada_catch_handlers;
12611 *excep_string = exception_name;
12612 }
bc18fbb5 12613 else if (exception_name.empty ())
f7f9143b
JB
12614 {
12615 /* Catch all exceptions. */
761269c8 12616 *ex = ada_catch_exception;
bc18fbb5 12617 excep_string->clear ();
f7f9143b 12618 }
bc18fbb5 12619 else if (exception_name == "unhandled")
f7f9143b
JB
12620 {
12621 /* Catch unhandled exceptions. */
761269c8 12622 *ex = ada_catch_exception_unhandled;
bc18fbb5 12623 excep_string->clear ();
f7f9143b
JB
12624 }
12625 else
12626 {
12627 /* Catch a specific exception. */
761269c8 12628 *ex = ada_catch_exception;
28010a5d 12629 *excep_string = exception_name;
f7f9143b
JB
12630 }
12631}
12632
12633/* Return the name of the symbol on which we should break in order to
12634 implement a catchpoint of the EX kind. */
12635
12636static const char *
761269c8 12637ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12638{
3eecfa55
JB
12639 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12640
12641 gdb_assert (data->exception_info != NULL);
0259addd 12642
f7f9143b
JB
12643 switch (ex)
12644 {
761269c8 12645 case ada_catch_exception:
dda83cd7
SM
12646 return (data->exception_info->catch_exception_sym);
12647 break;
761269c8 12648 case ada_catch_exception_unhandled:
dda83cd7
SM
12649 return (data->exception_info->catch_exception_unhandled_sym);
12650 break;
761269c8 12651 case ada_catch_assert:
dda83cd7
SM
12652 return (data->exception_info->catch_assert_sym);
12653 break;
9f757bf7 12654 case ada_catch_handlers:
dda83cd7
SM
12655 return (data->exception_info->catch_handlers_sym);
12656 break;
f7f9143b 12657 default:
f34652de 12658 internal_error (_("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12659 }
12660}
12661
f7f9143b
JB
12662/* Return the condition that will be used to match the current exception
12663 being raised with the exception that the user wants to catch. This
12664 assumes that this condition is used when the inferior just triggered
12665 an exception catchpoint.
cb7de75e 12666 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12667
cb7de75e 12668static std::string
9f757bf7 12669ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12670 enum ada_exception_catchpoint_kind ex)
f7f9143b 12671{
fccf9de1 12672 bool is_standard_exc = false;
cb7de75e 12673 std::string result;
9f757bf7
XR
12674
12675 if (ex == ada_catch_handlers)
12676 {
12677 /* For exception handlers catchpoints, the condition string does
dda83cd7 12678 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12679 result = ("long_integer (GNAT_GCC_exception_Access"
12680 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12681 }
12682 else
fccf9de1 12683 result = "long_integer (e)";
3d0b0fa3 12684
0963b4bd 12685 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12686 runtime units that have been compiled without debugging info; if
28010a5d 12687 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12688 exception (e.g. "constraint_error") then, during the evaluation
12689 of the condition expression, the symbol lookup on this name would
0963b4bd 12690 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12691 may then be set only on user-defined exceptions which have the
12692 same not-fully-qualified name (e.g. my_package.constraint_error).
12693
12694 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12695 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12696 exception constraint_error" is rewritten into "catch exception
12697 standard.constraint_error".
12698
85102364 12699 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12700 the inferior program, then the only way to specify this exception as a
12701 breakpoint condition is to use its fully-qualified named:
fccf9de1 12702 e.g. my_package.constraint_error. */
3d0b0fa3 12703
696d6f4d 12704 for (const char *name : standard_exc)
3d0b0fa3 12705 {
696d6f4d 12706 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12707 {
fccf9de1 12708 is_standard_exc = true;
9f757bf7 12709 break;
3d0b0fa3
JB
12710 }
12711 }
9f757bf7 12712
fccf9de1
TT
12713 result += " = ";
12714
12715 if (is_standard_exc)
12716 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12717 else
12718 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12719
9f757bf7 12720 return result;
f7f9143b
JB
12721}
12722
12723/* Return the symtab_and_line that should be used to insert an exception
12724 catchpoint of the TYPE kind.
12725
28010a5d
PA
12726 ADDR_STRING returns the name of the function where the real
12727 breakpoint that implements the catchpoints is set, depending on the
12728 type of catchpoint we need to create. */
f7f9143b
JB
12729
12730static struct symtab_and_line
bc18fbb5 12731ada_exception_sal (enum ada_exception_catchpoint_kind ex,
ae72050b 12732 std::string *addr_string)
f7f9143b
JB
12733{
12734 const char *sym_name;
12735 struct symbol *sym;
f7f9143b 12736
0259addd
JB
12737 /* First, find out which exception support info to use. */
12738 ada_exception_support_info_sniffer ();
12739
12740 /* Then lookup the function on which we will break in order to catch
f7f9143b 12741 the Ada exceptions requested by the user. */
f7f9143b
JB
12742 sym_name = ada_exception_sym_name (ex);
12743 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12744
57aff202
JB
12745 if (sym == NULL)
12746 error (_("Catchpoint symbol not found: %s"), sym_name);
12747
66d7f48f 12748 if (sym->aclass () != LOC_BLOCK)
57aff202 12749 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12750
12751 /* Set ADDR_STRING. */
cc12f4a8 12752 *addr_string = sym_name;
f7f9143b 12753
f17011e0 12754 return find_function_start_sal (sym, 1);
f7f9143b
JB
12755}
12756
b4a5b78b 12757/* Create an Ada exception catchpoint.
f7f9143b 12758
b4a5b78b 12759 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12760
bc18fbb5 12761 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12762 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12763 of the exception to which this catchpoint applies.
2df4d1d5 12764
bc18fbb5 12765 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12766
b4a5b78b
JB
12767 TEMPFLAG, if nonzero, means that the underlying breakpoint
12768 should be temporary.
28010a5d 12769
b4a5b78b 12770 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12771
349774ef 12772void
28010a5d 12773create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12774 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12775 const std::string &excep_string,
56ecd069 12776 const std::string &cond_string,
28010a5d 12777 int tempflag,
12d67b37 12778 int enabled,
28010a5d
PA
12779 int from_tty)
12780{
cc12f4a8 12781 std::string addr_string;
ae72050b 12782 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string);
28010a5d 12783
bd21b6c9
PA
12784 std::unique_ptr<ada_catchpoint> c
12785 (new ada_catchpoint (gdbarch, ex_kind, sal, addr_string.c_str (),
12d67b37 12786 tempflag, enabled, from_tty));
28010a5d 12787 c->excep_string = excep_string;
9f757bf7 12788 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12789 if (!cond_string.empty ())
733d554a 12790 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12791 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12792}
12793
9ac4176b
PA
12794/* Implement the "catch exception" command. */
12795
12796static void
eb4c3f4a 12797catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12798 struct cmd_list_element *command)
12799{
a121b7c1 12800 const char *arg = arg_entry;
9ac4176b
PA
12801 struct gdbarch *gdbarch = get_current_arch ();
12802 int tempflag;
761269c8 12803 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12804 std::string excep_string;
56ecd069 12805 std::string cond_string;
9ac4176b 12806
0f8e2034 12807 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12808
12809 if (!arg)
12810 arg = "";
9f757bf7 12811 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12812 &cond_string);
9f757bf7
XR
12813 create_ada_exception_catchpoint (gdbarch, ex_kind,
12814 excep_string, cond_string,
12815 tempflag, 1 /* enabled */,
12816 from_tty);
12817}
12818
12819/* Implement the "catch handlers" command. */
12820
12821static void
12822catch_ada_handlers_command (const char *arg_entry, int from_tty,
12823 struct cmd_list_element *command)
12824{
12825 const char *arg = arg_entry;
12826 struct gdbarch *gdbarch = get_current_arch ();
12827 int tempflag;
12828 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12829 std::string excep_string;
56ecd069 12830 std::string cond_string;
9f757bf7 12831
0f8e2034 12832 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12833
12834 if (!arg)
12835 arg = "";
12836 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12837 &cond_string);
b4a5b78b
JB
12838 create_ada_exception_catchpoint (gdbarch, ex_kind,
12839 excep_string, cond_string,
349774ef
JB
12840 tempflag, 1 /* enabled */,
12841 from_tty);
9ac4176b
PA
12842}
12843
71bed2db
TT
12844/* Completion function for the Ada "catch" commands. */
12845
12846static void
12847catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12848 const char *text, const char *word)
12849{
12850 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12851
12852 for (const ada_exc_info &info : exceptions)
12853 {
12854 if (startswith (info.name, word))
b02f78f9 12855 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12856 }
12857}
12858
b4a5b78b 12859/* Split the arguments specified in a "catch assert" command.
5845583d 12860
b4a5b78b
JB
12861 ARGS contains the command's arguments (or the empty string if
12862 no arguments were passed).
5845583d
JB
12863
12864 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12865 (the memory needs to be deallocated after use). */
5845583d 12866
b4a5b78b 12867static void
56ecd069 12868catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12869{
f1735a53 12870 args = skip_spaces (args);
f7f9143b 12871
5845583d 12872 /* Check whether a condition was provided. */
61012eef 12873 if (startswith (args, "if")
5845583d 12874 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12875 {
5845583d 12876 args += 2;
f1735a53 12877 args = skip_spaces (args);
5845583d 12878 if (args[0] == '\0')
dda83cd7 12879 error (_("condition missing after `if' keyword"));
56ecd069 12880 cond_string.assign (args);
f7f9143b
JB
12881 }
12882
5845583d
JB
12883 /* Otherwise, there should be no other argument at the end of
12884 the command. */
12885 else if (args[0] != '\0')
12886 error (_("Junk at end of arguments."));
f7f9143b
JB
12887}
12888
9ac4176b
PA
12889/* Implement the "catch assert" command. */
12890
12891static void
eb4c3f4a 12892catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12893 struct cmd_list_element *command)
12894{
a121b7c1 12895 const char *arg = arg_entry;
9ac4176b
PA
12896 struct gdbarch *gdbarch = get_current_arch ();
12897 int tempflag;
56ecd069 12898 std::string cond_string;
9ac4176b 12899
0f8e2034 12900 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12901
12902 if (!arg)
12903 arg = "";
56ecd069 12904 catch_ada_assert_command_split (arg, cond_string);
761269c8 12905 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 12906 "", cond_string,
349774ef
JB
12907 tempflag, 1 /* enabled */,
12908 from_tty);
9ac4176b 12909}
778865d3
JB
12910
12911/* Return non-zero if the symbol SYM is an Ada exception object. */
12912
12913static int
12914ada_is_exception_sym (struct symbol *sym)
12915{
5f9c5a63 12916 const char *type_name = sym->type ()->name ();
778865d3 12917
66d7f48f
SM
12918 return (sym->aclass () != LOC_TYPEDEF
12919 && sym->aclass () != LOC_BLOCK
12920 && sym->aclass () != LOC_CONST
12921 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12922 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12923}
12924
12925/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12926 Ada exception object. This matches all exceptions except the ones
12927 defined by the Ada language. */
12928
12929static int
12930ada_is_non_standard_exception_sym (struct symbol *sym)
12931{
778865d3
JB
12932 if (!ada_is_exception_sym (sym))
12933 return 0;
12934
696d6f4d
TT
12935 for (const char *name : standard_exc)
12936 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12937 return 0; /* A standard exception. */
12938
12939 /* Numeric_Error is also a standard exception, so exclude it.
12940 See the STANDARD_EXC description for more details as to why
12941 this exception is not listed in that array. */
987012b8 12942 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12943 return 0;
12944
12945 return 1;
12946}
12947
ab816a27 12948/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12949 objects.
12950
12951 The comparison is determined first by exception name, and then
12952 by exception address. */
12953
ab816a27 12954bool
cc536b21 12955ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12956{
778865d3
JB
12957 int result;
12958
ab816a27
TT
12959 result = strcmp (name, other.name);
12960 if (result < 0)
12961 return true;
12962 if (result == 0 && addr < other.addr)
12963 return true;
12964 return false;
12965}
778865d3 12966
ab816a27 12967bool
cc536b21 12968ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12969{
12970 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12971}
12972
12973/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12974 routine, but keeping the first SKIP elements untouched.
12975
12976 All duplicates are also removed. */
12977
12978static void
ab816a27 12979sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12980 int skip)
12981{
ab816a27
TT
12982 std::sort (exceptions->begin () + skip, exceptions->end ());
12983 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12984 exceptions->end ());
778865d3
JB
12985}
12986
778865d3
JB
12987/* Add all exceptions defined by the Ada standard whose name match
12988 a regular expression.
12989
12990 If PREG is not NULL, then this regexp_t object is used to
12991 perform the symbol name matching. Otherwise, no name-based
12992 filtering is performed.
12993
12994 EXCEPTIONS is a vector of exceptions to which matching exceptions
12995 gets pushed. */
12996
12997static void
2d7cc5c7 12998ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12999 std::vector<ada_exc_info> *exceptions)
778865d3 13000{
696d6f4d 13001 for (const char *name : standard_exc)
778865d3 13002 {
696d6f4d 13003 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3 13004 {
4326580d
MM
13005 symbol_name_match_type match_type = name_match_type_from_name (name);
13006 lookup_name_info lookup_name (name, match_type);
778865d3 13007
4326580d
MM
13008 symbol_name_matcher_ftype *match_name
13009 = ada_get_symbol_name_matcher (lookup_name);
778865d3 13010
4326580d
MM
13011 /* Iterate over all objfiles irrespective of scope or linker
13012 namespaces so we get all exceptions anywhere in the
13013 progspace. */
13014 for (objfile *objfile : current_program_space->objfiles ())
13015 {
13016 for (minimal_symbol *msymbol : objfile->msymbols ())
13017 {
13018 if (match_name (msymbol->linkage_name (), lookup_name,
13019 nullptr)
13020 && msymbol->type () != mst_solib_trampoline)
13021 {
13022 ada_exc_info info
13023 = {name, msymbol->value_address (objfile)};
13024
13025 exceptions->push_back (info);
13026 }
13027 }
778865d3
JB
13028 }
13029 }
13030 }
13031}
13032
13033/* Add all Ada exceptions defined locally and accessible from the given
13034 FRAME.
13035
13036 If PREG is not NULL, then this regexp_t object is used to
13037 perform the symbol name matching. Otherwise, no name-based
13038 filtering is performed.
13039
13040 EXCEPTIONS is a vector of exceptions to which matching exceptions
13041 gets pushed. */
13042
13043static void
2d7cc5c7 13044ada_add_exceptions_from_frame (compiled_regex *preg,
bd2b40ac 13045 frame_info_ptr frame,
ab816a27 13046 std::vector<ada_exc_info> *exceptions)
778865d3 13047{
3977b71f 13048 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
13049
13050 while (block != 0)
13051 {
13052 struct block_iterator iter;
13053 struct symbol *sym;
13054
13055 ALL_BLOCK_SYMBOLS (block, iter, sym)
13056 {
66d7f48f 13057 switch (sym->aclass ())
778865d3
JB
13058 {
13059 case LOC_TYPEDEF:
13060 case LOC_BLOCK:
13061 case LOC_CONST:
13062 break;
13063 default:
13064 if (ada_is_exception_sym (sym))
13065 {
987012b8 13066 struct ada_exc_info info = {sym->print_name (),
4aeddc50 13067 sym->value_address ()};
778865d3 13068
ab816a27 13069 exceptions->push_back (info);
778865d3
JB
13070 }
13071 }
13072 }
6c00f721 13073 if (block->function () != NULL)
778865d3 13074 break;
f135fe72 13075 block = block->superblock ();
778865d3
JB
13076 }
13077}
13078
14bc53a8
PA
13079/* Return true if NAME matches PREG or if PREG is NULL. */
13080
13081static bool
2d7cc5c7 13082name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
13083{
13084 return (preg == NULL
f945dedf 13085 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
13086}
13087
778865d3
JB
13088/* Add all exceptions defined globally whose name name match
13089 a regular expression, excluding standard exceptions.
13090
13091 The reason we exclude standard exceptions is that they need
13092 to be handled separately: Standard exceptions are defined inside
13093 a runtime unit which is normally not compiled with debugging info,
13094 and thus usually do not show up in our symbol search. However,
13095 if the unit was in fact built with debugging info, we need to
13096 exclude them because they would duplicate the entry we found
13097 during the special loop that specifically searches for those
13098 standard exceptions.
13099
13100 If PREG is not NULL, then this regexp_t object is used to
13101 perform the symbol name matching. Otherwise, no name-based
13102 filtering is performed.
13103
13104 EXCEPTIONS is a vector of exceptions to which matching exceptions
13105 gets pushed. */
13106
13107static void
2d7cc5c7 13108ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13109 std::vector<ada_exc_info> *exceptions)
778865d3 13110{
14bc53a8
PA
13111 /* In Ada, the symbol "search name" is a linkage name, whereas the
13112 regular expression used to do the matching refers to the natural
13113 name. So match against the decoded name. */
13114 expand_symtabs_matching (NULL,
b5ec771e 13115 lookup_name_info::match_any (),
14bc53a8
PA
13116 [&] (const char *search_name)
13117 {
f945dedf
CB
13118 std::string decoded = ada_decode (search_name);
13119 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13120 },
13121 NULL,
03a8ea51 13122 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 13123 VARIABLES_DOMAIN);
778865d3 13124
4326580d
MM
13125 /* Iterate over all objfiles irrespective of scope or linker namespaces
13126 so we get all exceptions anywhere in the progspace. */
2030c079 13127 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13128 {
b669c953 13129 for (compunit_symtab *s : objfile->compunits ())
778865d3 13130 {
af39c5c8 13131 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13132 int i;
778865d3 13133
d8aeb77f
TT
13134 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13135 {
63d609de 13136 const struct block *b = bv->block (i);
d8aeb77f
TT
13137 struct block_iterator iter;
13138 struct symbol *sym;
778865d3 13139
d8aeb77f
TT
13140 ALL_BLOCK_SYMBOLS (b, iter, sym)
13141 if (ada_is_non_standard_exception_sym (sym)
987012b8 13142 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13143 {
13144 struct ada_exc_info info
4aeddc50 13145 = {sym->print_name (), sym->value_address ()};
d8aeb77f
TT
13146
13147 exceptions->push_back (info);
13148 }
13149 }
778865d3
JB
13150 }
13151 }
13152}
13153
13154/* Implements ada_exceptions_list with the regular expression passed
13155 as a regex_t, rather than a string.
13156
13157 If not NULL, PREG is used to filter out exceptions whose names
13158 do not match. Otherwise, all exceptions are listed. */
13159
ab816a27 13160static std::vector<ada_exc_info>
2d7cc5c7 13161ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13162{
ab816a27 13163 std::vector<ada_exc_info> result;
778865d3
JB
13164 int prev_len;
13165
13166 /* First, list the known standard exceptions. These exceptions
13167 need to be handled separately, as they are usually defined in
13168 runtime units that have been compiled without debugging info. */
13169
13170 ada_add_standard_exceptions (preg, &result);
13171
13172 /* Next, find all exceptions whose scope is local and accessible
13173 from the currently selected frame. */
13174
13175 if (has_stack_frames ())
13176 {
ab816a27 13177 prev_len = result.size ();
778865d3
JB
13178 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13179 &result);
ab816a27 13180 if (result.size () > prev_len)
778865d3
JB
13181 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13182 }
13183
13184 /* Add all exceptions whose scope is global. */
13185
ab816a27 13186 prev_len = result.size ();
778865d3 13187 ada_add_global_exceptions (preg, &result);
ab816a27 13188 if (result.size () > prev_len)
778865d3
JB
13189 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13190
778865d3
JB
13191 return result;
13192}
13193
13194/* Return a vector of ada_exc_info.
13195
13196 If REGEXP is NULL, all exceptions are included in the result.
13197 Otherwise, it should contain a valid regular expression,
13198 and only the exceptions whose names match that regular expression
13199 are included in the result.
13200
13201 The exceptions are sorted in the following order:
13202 - Standard exceptions (defined by the Ada language), in
13203 alphabetical order;
13204 - Exceptions only visible from the current frame, in
13205 alphabetical order;
13206 - Exceptions whose scope is global, in alphabetical order. */
13207
ab816a27 13208std::vector<ada_exc_info>
778865d3
JB
13209ada_exceptions_list (const char *regexp)
13210{
2d7cc5c7
PA
13211 if (regexp == NULL)
13212 return ada_exceptions_list_1 (NULL);
778865d3 13213
2d7cc5c7
PA
13214 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13215 return ada_exceptions_list_1 (&reg);
778865d3
JB
13216}
13217
13218/* Implement the "info exceptions" command. */
13219
13220static void
1d12d88f 13221info_exceptions_command (const char *regexp, int from_tty)
778865d3 13222{
778865d3 13223 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13224
ab816a27 13225 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13226
13227 if (regexp != NULL)
6cb06a8c 13228 gdb_printf
778865d3
JB
13229 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13230 else
6cb06a8c 13231 gdb_printf (_("All defined Ada exceptions:\n"));
778865d3 13232
ab816a27 13233 for (const ada_exc_info &info : exceptions)
6cb06a8c 13234 gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13235}
13236
6c038f32
PH
13237\f
13238 /* Language vector */
13239
b5ec771e
PA
13240/* symbol_name_matcher_ftype adapter for wild_match. */
13241
13242static bool
13243do_wild_match (const char *symbol_search_name,
13244 const lookup_name_info &lookup_name,
a207cff2 13245 completion_match_result *comp_match_res)
b5ec771e
PA
13246{
13247 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13248}
13249
13250/* symbol_name_matcher_ftype adapter for full_match. */
13251
13252static bool
13253do_full_match (const char *symbol_search_name,
13254 const lookup_name_info &lookup_name,
a207cff2 13255 completion_match_result *comp_match_res)
b5ec771e 13256{
959d6a67
TT
13257 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13258
13259 /* If both symbols start with "_ada_", just let the loop below
13260 handle the comparison. However, if only the symbol name starts
13261 with "_ada_", skip the prefix and let the match proceed as
13262 usual. */
13263 if (startswith (symbol_search_name, "_ada_")
13264 && !startswith (lname, "_ada"))
86b44259 13265 symbol_search_name += 5;
81eaa506
TT
13266 /* Likewise for ghost entities. */
13267 if (startswith (symbol_search_name, "___ghost_")
13268 && !startswith (lname, "___ghost_"))
13269 symbol_search_name += 9;
86b44259 13270
86b44259
TT
13271 int uscore_count = 0;
13272 while (*lname != '\0')
13273 {
13274 if (*symbol_search_name != *lname)
13275 {
13276 if (*symbol_search_name == 'B' && uscore_count == 2
13277 && symbol_search_name[1] == '_')
13278 {
13279 symbol_search_name += 2;
13280 while (isdigit (*symbol_search_name))
13281 ++symbol_search_name;
13282 if (symbol_search_name[0] == '_'
13283 && symbol_search_name[1] == '_')
13284 {
13285 symbol_search_name += 2;
13286 continue;
13287 }
13288 }
13289 return false;
13290 }
13291
13292 if (*symbol_search_name == '_')
13293 ++uscore_count;
13294 else
13295 uscore_count = 0;
13296
13297 ++symbol_search_name;
13298 ++lname;
13299 }
13300
13301 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13302}
13303
a2cd4f14
JB
13304/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13305
13306static bool
13307do_exact_match (const char *symbol_search_name,
13308 const lookup_name_info &lookup_name,
13309 completion_match_result *comp_match_res)
13310{
13311 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13312}
13313
b5ec771e
PA
13314/* Build the Ada lookup name for LOOKUP_NAME. */
13315
13316ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13317{
e0802d59 13318 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13319
6a780b67 13320 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13321 {
13322 if (user_name.back () == '>')
e0802d59 13323 m_encoded_name
5ac58899 13324 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13325 else
e0802d59 13326 m_encoded_name
5ac58899 13327 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13328 m_encoded_p = true;
13329 m_verbatim_p = true;
13330 m_wild_match_p = false;
13331 m_standard_p = false;
13332 }
13333 else
13334 {
13335 m_verbatim_p = false;
13336
e0802d59 13337 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13338
13339 if (!m_encoded_p)
13340 {
e0802d59 13341 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13342 m_encoded_name = ada_encode_1 (folded, false);
13343 if (m_encoded_name.empty ())
5ac58899 13344 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13345 }
13346 else
5ac58899 13347 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13348
13349 /* Handle the 'package Standard' special case. See description
13350 of m_standard_p. */
13351 if (startswith (m_encoded_name.c_str (), "standard__"))
13352 {
13353 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13354 m_standard_p = true;
13355 }
13356 else
13357 m_standard_p = false;
74ccd7f5 13358
b5ec771e
PA
13359 /* If the name contains a ".", then the user is entering a fully
13360 qualified entity name, and the match must not be done in wild
13361 mode. Similarly, if the user wants to complete what looks
13362 like an encoded name, the match must not be done in wild
13363 mode. Also, in the standard__ special case always do
13364 non-wild matching. */
13365 m_wild_match_p
13366 = (lookup_name.match_type () != symbol_name_match_type::FULL
13367 && !m_encoded_p
13368 && !m_standard_p
13369 && user_name.find ('.') == std::string::npos);
13370 }
13371}
13372
13373/* symbol_name_matcher_ftype method for Ada. This only handles
13374 completion mode. */
13375
13376static bool
13377ada_symbol_name_matches (const char *symbol_search_name,
13378 const lookup_name_info &lookup_name,
a207cff2 13379 completion_match_result *comp_match_res)
74ccd7f5 13380{
b5ec771e
PA
13381 return lookup_name.ada ().matches (symbol_search_name,
13382 lookup_name.match_type (),
a207cff2 13383 comp_match_res);
b5ec771e
PA
13384}
13385
de63c46b
PA
13386/* A name matcher that matches the symbol name exactly, with
13387 strcmp. */
13388
13389static bool
13390literal_symbol_name_matcher (const char *symbol_search_name,
13391 const lookup_name_info &lookup_name,
13392 completion_match_result *comp_match_res)
13393{
e0802d59 13394 gdb::string_view name_view = lookup_name.name ();
de63c46b 13395
e0802d59
TT
13396 if (lookup_name.completion_mode ()
13397 ? (strncmp (symbol_search_name, name_view.data (),
13398 name_view.size ()) == 0)
13399 : symbol_search_name == name_view)
de63c46b
PA
13400 {
13401 if (comp_match_res != NULL)
13402 comp_match_res->set_match (symbol_search_name);
13403 return true;
13404 }
13405 else
13406 return false;
13407}
13408
c9debfb9 13409/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13410 Ada. */
13411
13412static symbol_name_matcher_ftype *
13413ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13414{
de63c46b
PA
13415 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13416 return literal_symbol_name_matcher;
13417
b5ec771e
PA
13418 if (lookup_name.completion_mode ())
13419 return ada_symbol_name_matches;
74ccd7f5 13420 else
b5ec771e
PA
13421 {
13422 if (lookup_name.ada ().wild_match_p ())
13423 return do_wild_match;
a2cd4f14
JB
13424 else if (lookup_name.ada ().verbatim_p ())
13425 return do_exact_match;
b5ec771e
PA
13426 else
13427 return do_full_match;
13428 }
74ccd7f5
JB
13429}
13430
0874fd07
AB
13431/* Class representing the Ada language. */
13432
13433class ada_language : public language_defn
13434{
13435public:
13436 ada_language ()
0e25e767 13437 : language_defn (language_ada)
0874fd07 13438 { /* Nothing. */ }
5bd40f2a 13439
6f7664a9
AB
13440 /* See language.h. */
13441
13442 const char *name () const override
13443 { return "ada"; }
13444
13445 /* See language.h. */
13446
13447 const char *natural_name () const override
13448 { return "Ada"; }
13449
e171d6f1
AB
13450 /* See language.h. */
13451
13452 const std::vector<const char *> &filename_extensions () const override
13453 {
13454 static const std::vector<const char *> extensions
13455 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13456 return extensions;
13457 }
13458
5bd40f2a
AB
13459 /* Print an array element index using the Ada syntax. */
13460
13461 void print_array_index (struct type *index_type,
13462 LONGEST index,
13463 struct ui_file *stream,
13464 const value_print_options *options) const override
13465 {
13466 struct value *index_value = val_atr (index_type, index);
13467
00c696a6 13468 value_print (index_value, stream, options);
6cb06a8c 13469 gdb_printf (stream, " => ");
5bd40f2a 13470 }
15e5fd35
AB
13471
13472 /* Implement the "read_var_value" language_defn method for Ada. */
13473
13474 struct value *read_var_value (struct symbol *var,
13475 const struct block *var_block,
bd2b40ac 13476 frame_info_ptr frame) const override
15e5fd35
AB
13477 {
13478 /* The only case where default_read_var_value is not sufficient
13479 is when VAR is a renaming... */
13480 if (frame != nullptr)
13481 {
13482 const struct block *frame_block = get_frame_block (frame, NULL);
13483 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13484 return ada_read_renaming_var_value (var, frame_block);
13485 }
13486
13487 /* This is a typical case where we expect the default_read_var_value
13488 function to work. */
13489 return language_defn::read_var_value (var, var_block, frame);
13490 }
1fb314aa 13491
2c71f639 13492 /* See language.h. */
496feb16 13493 bool symbol_printing_suppressed (struct symbol *symbol) const override
2c71f639 13494 {
496feb16 13495 return symbol->is_artificial ();
2c71f639
TV
13496 }
13497
1fb314aa
AB
13498 /* See language.h. */
13499 void language_arch_info (struct gdbarch *gdbarch,
13500 struct language_arch_info *lai) const override
13501 {
13502 const struct builtin_type *builtin = builtin_type (gdbarch);
13503
7bea47f0
AB
13504 /* Helper function to allow shorter lines below. */
13505 auto add = [&] (struct type *t)
13506 {
13507 lai->add_primitive_type (t);
13508 };
13509
13510 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13511 0, "integer"));
13512 add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13513 0, "long_integer"));
13514 add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13515 0, "short_integer"));
13516 struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
c9f66f00 13517 1, "character");
7bea47f0
AB
13518 lai->set_string_char_type (char_type);
13519 add (char_type);
c9f66f00
TT
13520 add (arch_character_type (gdbarch, 16, 1, "wide_character"));
13521 add (arch_character_type (gdbarch, 32, 1, "wide_wide_character"));
7bea47f0
AB
13522 add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
13523 "float", gdbarch_float_format (gdbarch)));
13524 add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13525 "long_float", gdbarch_double_format (gdbarch)));
13526 add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
13527 0, "long_long_integer"));
13528 add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
13529 "long_long_float",
13530 gdbarch_long_double_format (gdbarch)));
13531 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13532 0, "natural"));
13533 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13534 0, "positive"));
13535 add (builtin->builtin_void);
13536
13537 struct type *system_addr_ptr
1fb314aa
AB
13538 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
13539 "void"));
7bea47f0
AB
13540 system_addr_ptr->set_name ("system__address");
13541 add (system_addr_ptr);
1fb314aa
AB
13542
13543 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13544 type. This is a signed integral type whose size is the same as
13545 the size of addresses. */
df86565b 13546 unsigned int addr_length = system_addr_ptr->length ();
7bea47f0
AB
13547 add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
13548 "storage_offset"));
1fb314aa 13549
7bea47f0 13550 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13551 }
4009ee92
AB
13552
13553 /* See language.h. */
13554
13555 bool iterate_over_symbols
13556 (const struct block *block, const lookup_name_info &name,
13557 domain_enum domain,
13558 gdb::function_view<symbol_found_callback_ftype> callback) const override
13559 {
d1183b06
TT
13560 std::vector<struct block_symbol> results
13561 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13562 for (block_symbol &sym : results)
13563 {
13564 if (!callback (&sym))
13565 return false;
13566 }
13567
13568 return true;
13569 }
6f827019
AB
13570
13571 /* See language.h. */
3456e70c
TT
13572 bool sniff_from_mangled_name
13573 (const char *mangled,
13574 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13575 {
13576 std::string demangled = ada_decode (mangled);
13577
13578 *out = NULL;
13579
13580 if (demangled != mangled && demangled[0] != '<')
13581 {
13582 /* Set the gsymbol language to Ada, but still return 0.
13583 Two reasons for that:
13584
13585 1. For Ada, we prefer computing the symbol's decoded name
13586 on the fly rather than pre-compute it, in order to save
13587 memory (Ada projects are typically very large).
13588
13589 2. There are some areas in the definition of the GNAT
13590 encoding where, with a bit of bad luck, we might be able
13591 to decode a non-Ada symbol, generating an incorrect
13592 demangled name (Eg: names ending with "TB" for instance
13593 are identified as task bodies and so stripped from
13594 the decoded name returned).
13595
13596 Returning true, here, but not setting *DEMANGLED, helps us get
13597 a little bit of the best of both worlds. Because we're last,
13598 we should not affect any of the other languages that were
13599 able to demangle the symbol before us; we get to correctly
13600 tag Ada symbols as such; and even if we incorrectly tagged a
13601 non-Ada symbol, which should be rare, any routing through the
13602 Ada language should be transparent (Ada tries to behave much
13603 like C/C++ with non-Ada symbols). */
13604 return true;
13605 }
13606
13607 return false;
13608 }
fbfb0a46
AB
13609
13610 /* See language.h. */
13611
3456e70c
TT
13612 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13613 int options) const override
0a50df5d 13614 {
3456e70c 13615 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13616 }
13617
13618 /* See language.h. */
13619
fbfb0a46
AB
13620 void print_type (struct type *type, const char *varstring,
13621 struct ui_file *stream, int show, int level,
13622 const struct type_print_options *flags) const override
13623 {
13624 ada_print_type (type, varstring, stream, show, level, flags);
13625 }
c9debfb9 13626
53fc67f8
AB
13627 /* See language.h. */
13628
13629 const char *word_break_characters (void) const override
13630 {
13631 return ada_completer_word_break_characters;
13632 }
13633
7e56227d
AB
13634 /* See language.h. */
13635
13636 void collect_symbol_completion_matches (completion_tracker &tracker,
13637 complete_symbol_mode mode,
13638 symbol_name_match_type name_match_type,
13639 const char *text, const char *word,
13640 enum type_code code) const override
13641 {
13642 struct symbol *sym;
13643 const struct block *b, *surrounding_static_block = 0;
13644 struct block_iterator iter;
13645
13646 gdb_assert (code == TYPE_CODE_UNDEF);
13647
13648 lookup_name_info lookup_name (text, name_match_type, true);
13649
13650 /* First, look at the partial symtab symbols. */
13651 expand_symtabs_matching (NULL,
13652 lookup_name,
13653 NULL,
13654 NULL,
03a8ea51 13655 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13656 ALL_DOMAIN);
13657
13658 /* At this point scan through the misc symbol vectors and add each
13659 symbol you find to the list. Eventually we want to ignore
13660 anything that isn't a text symbol (everything else will be
13661 handled by the psymtab code above). */
13662
13663 for (objfile *objfile : current_program_space->objfiles ())
13664 {
13665 for (minimal_symbol *msymbol : objfile->msymbols ())
13666 {
13667 QUIT;
13668
13669 if (completion_skip_symbol (mode, msymbol))
13670 continue;
13671
13672 language symbol_language = msymbol->language ();
13673
13674 /* Ada minimal symbols won't have their language set to Ada. If
13675 we let completion_list_add_name compare using the
13676 default/C-like matcher, then when completing e.g., symbols in a
13677 package named "pck", we'd match internal Ada symbols like
13678 "pckS", which are invalid in an Ada expression, unless you wrap
13679 them in '<' '>' to request a verbatim match.
13680
13681 Unfortunately, some Ada encoded names successfully demangle as
13682 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13683 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13684 with the wrong language set. Paper over that issue here. */
13685 if (symbol_language == language_auto
13686 || symbol_language == language_cplus)
13687 symbol_language = language_ada;
13688
13689 completion_list_add_name (tracker,
13690 symbol_language,
13691 msymbol->linkage_name (),
13692 lookup_name, text, word);
13693 }
13694 }
13695
13696 /* Search upwards from currently selected frame (so that we can
13697 complete on local vars. */
13698
f135fe72 13699 for (b = get_selected_block (0); b != NULL; b = b->superblock ())
7e56227d 13700 {
f135fe72 13701 if (!b->superblock ())
7e56227d
AB
13702 surrounding_static_block = b; /* For elmin of dups */
13703
13704 ALL_BLOCK_SYMBOLS (b, iter, sym)
13705 {
13706 if (completion_skip_symbol (mode, sym))
13707 continue;
13708
13709 completion_list_add_name (tracker,
13710 sym->language (),
13711 sym->linkage_name (),
13712 lookup_name, text, word);
13713 }
13714 }
13715
13716 /* Go through the symtabs and check the externs and statics for
13717 symbols which match. */
13718
13719 for (objfile *objfile : current_program_space->objfiles ())
13720 {
13721 for (compunit_symtab *s : objfile->compunits ())
13722 {
13723 QUIT;
63d609de 13724 b = s->blockvector ()->global_block ();
7e56227d
AB
13725 ALL_BLOCK_SYMBOLS (b, iter, sym)
13726 {
13727 if (completion_skip_symbol (mode, sym))
13728 continue;
13729
13730 completion_list_add_name (tracker,
13731 sym->language (),
13732 sym->linkage_name (),
13733 lookup_name, text, word);
13734 }
13735 }
13736 }
13737
13738 for (objfile *objfile : current_program_space->objfiles ())
13739 {
13740 for (compunit_symtab *s : objfile->compunits ())
13741 {
13742 QUIT;
63d609de 13743 b = s->blockvector ()->static_block ();
7e56227d
AB
13744 /* Don't do this block twice. */
13745 if (b == surrounding_static_block)
13746 continue;
13747 ALL_BLOCK_SYMBOLS (b, iter, sym)
13748 {
13749 if (completion_skip_symbol (mode, sym))
13750 continue;
13751
13752 completion_list_add_name (tracker,
13753 sym->language (),
13754 sym->linkage_name (),
13755 lookup_name, text, word);
13756 }
13757 }
13758 }
13759 }
13760
f16a9f57
AB
13761 /* See language.h. */
13762
13763 gdb::unique_xmalloc_ptr<char> watch_location_expression
13764 (struct type *type, CORE_ADDR addr) const override
13765 {
27710edb 13766 type = check_typedef (check_typedef (type)->target_type ());
f16a9f57 13767 std::string name = type_to_string (type);
8579fd13 13768 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13769 }
13770
a1d1fa3e
AB
13771 /* See language.h. */
13772
13773 void value_print (struct value *val, struct ui_file *stream,
13774 const struct value_print_options *options) const override
13775 {
13776 return ada_value_print (val, stream, options);
13777 }
13778
ebe2334e
AB
13779 /* See language.h. */
13780
13781 void value_print_inner
13782 (struct value *val, struct ui_file *stream, int recurse,
13783 const struct value_print_options *options) const override
13784 {
13785 return ada_value_print_inner (val, stream, recurse, options);
13786 }
13787
a78a19b1
AB
13788 /* See language.h. */
13789
13790 struct block_symbol lookup_symbol_nonlocal
13791 (const char *name, const struct block *block,
13792 const domain_enum domain) const override
13793 {
13794 struct block_symbol sym;
13795
78004096
TT
13796 sym = ada_lookup_symbol (name,
13797 (block == nullptr
13798 ? nullptr
d24e14a0 13799 : block->static_block ()),
78004096 13800 domain);
a78a19b1
AB
13801 if (sym.symbol != NULL)
13802 return sym;
13803
13804 /* If we haven't found a match at this point, try the primitive
13805 types. In other languages, this search is performed before
13806 searching for global symbols in order to short-circuit that
13807 global-symbol search if it happens that the name corresponds
13808 to a primitive type. But we cannot do the same in Ada, because
13809 it is perfectly legitimate for a program to declare a type which
13810 has the same name as a standard type. If looking up a type in
13811 that situation, we have traditionally ignored the primitive type
13812 in favor of user-defined types. This is why, unlike most other
13813 languages, we search the primitive types this late and only after
13814 having searched the global symbols without success. */
13815
13816 if (domain == VAR_DOMAIN)
13817 {
13818 struct gdbarch *gdbarch;
13819
13820 if (block == NULL)
13821 gdbarch = target_gdbarch ();
13822 else
7f5937df 13823 gdbarch = block->gdbarch ();
a78a19b1
AB
13824 sym.symbol
13825 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13826 if (sym.symbol != NULL)
13827 return sym;
13828 }
13829
13830 return {};
13831 }
13832
87afa652
AB
13833 /* See language.h. */
13834
13835 int parser (struct parser_state *ps) const override
13836 {
13837 warnings_issued = 0;
13838 return ada_parse (ps);
13839 }
13840
ec8cec5b
AB
13841 /* See language.h. */
13842
13843 void emitchar (int ch, struct type *chtype,
13844 struct ui_file *stream, int quoter) const override
13845 {
13846 ada_emit_char (ch, chtype, stream, quoter, 1);
13847 }
13848
52b50f2c
AB
13849 /* See language.h. */
13850
13851 void printchar (int ch, struct type *chtype,
13852 struct ui_file *stream) const override
13853 {
13854 ada_printchar (ch, chtype, stream);
13855 }
13856
d711ee67
AB
13857 /* See language.h. */
13858
13859 void printstr (struct ui_file *stream, struct type *elttype,
13860 const gdb_byte *string, unsigned int length,
13861 const char *encoding, int force_ellipses,
13862 const struct value_print_options *options) const override
13863 {
13864 ada_printstr (stream, elttype, string, length, encoding,
13865 force_ellipses, options);
13866 }
13867
4ffc13fb
AB
13868 /* See language.h. */
13869
13870 void print_typedef (struct type *type, struct symbol *new_symbol,
13871 struct ui_file *stream) const override
13872 {
13873 ada_print_typedef (type, new_symbol, stream);
13874 }
13875
39e7ecca
AB
13876 /* See language.h. */
13877
13878 bool is_string_type_p (struct type *type) const override
13879 {
13880 return ada_is_string_type (type);
13881 }
13882
22e3f3ed
AB
13883 /* See language.h. */
13884
13885 const char *struct_too_deep_ellipsis () const override
13886 { return "(...)"; }
39e7ecca 13887
67bd3fd5
AB
13888 /* See language.h. */
13889
13890 bool c_style_arrays_p () const override
13891 { return false; }
13892
d3355e4d
AB
13893 /* See language.h. */
13894
13895 bool store_sym_names_in_linkage_form_p () const override
13896 { return true; }
13897
b63a3f3f
AB
13898 /* See language.h. */
13899
13900 const struct lang_varobj_ops *varobj_ops () const override
13901 { return &ada_varobj_ops; }
13902
c9debfb9
AB
13903protected:
13904 /* See language.h. */
13905
13906 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13907 (const lookup_name_info &lookup_name) const override
13908 {
13909 return ada_get_symbol_name_matcher (lookup_name);
13910 }
0874fd07
AB
13911};
13912
13913/* Single instance of the Ada language class. */
13914
13915static ada_language ada_language_defn;
13916
5bf03f13
JB
13917/* Command-list for the "set/show ada" prefix command. */
13918static struct cmd_list_element *set_ada_list;
13919static struct cmd_list_element *show_ada_list;
13920
3d9434b5
JB
13921/* This module's 'new_objfile' observer. */
13922
13923static void
13924ada_new_objfile_observer (struct objfile *objfile)
13925{
13926 ada_clear_symbol_cache ();
13927}
13928
13929/* This module's 'free_objfile' observer. */
13930
13931static void
13932ada_free_objfile_observer (struct objfile *objfile)
13933{
13934 ada_clear_symbol_cache ();
13935}
13936
315e4ebb
TT
13937/* Charsets known to GNAT. */
13938static const char * const gnat_source_charsets[] =
13939{
13940 /* Note that code below assumes that the default comes first.
13941 Latin-1 is the default here, because that is also GNAT's
13942 default. */
13943 "ISO-8859-1",
13944 "ISO-8859-2",
13945 "ISO-8859-3",
13946 "ISO-8859-4",
13947 "ISO-8859-5",
13948 "ISO-8859-15",
13949 "CP437",
13950 "CP850",
13951 /* Note that this value is special-cased in the encoder and
13952 decoder. */
13953 ada_utf8,
13954 nullptr
13955};
13956
6c265988 13957void _initialize_ada_language ();
d2e4a39e 13958void
6c265988 13959_initialize_ada_language ()
14f9c5c9 13960{
f54bdb6d
SM
13961 add_setshow_prefix_cmd
13962 ("ada", no_class,
13963 _("Prefix command for changing Ada-specific settings."),
13964 _("Generic command for showing Ada-specific settings."),
13965 &set_ada_list, &show_ada_list,
13966 &setlist, &showlist);
5bf03f13
JB
13967
13968 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13969 &trust_pad_over_xvs, _("\
590042fc
PW
13970Enable or disable an optimization trusting PAD types over XVS types."), _("\
13971Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13972 _("\
5bf03f13
JB
13973This is related to the encoding used by the GNAT compiler. The debugger\n\
13974should normally trust the contents of PAD types, but certain older versions\n\
13975of GNAT have a bug that sometimes causes the information in the PAD type\n\
13976to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13977work around this bug. It is always safe to turn this option \"off\", but\n\
13978this incurs a slight performance penalty, so it is recommended to NOT change\n\
13979this option to \"off\" unless necessary."),
dda83cd7 13980 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13981
d72413e6
PMR
13982 add_setshow_boolean_cmd ("print-signatures", class_vars,
13983 &print_signatures, _("\
13984Enable or disable the output of formal and return types for functions in the \
590042fc 13985overloads selection menu."), _("\
d72413e6 13986Show whether the output of formal and return types for functions in the \
590042fc 13987overloads selection menu is activated."),
d72413e6
PMR
13988 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13989
315e4ebb
TT
13990 ada_source_charset = gnat_source_charsets[0];
13991 add_setshow_enum_cmd ("source-charset", class_files,
13992 gnat_source_charsets,
13993 &ada_source_charset, _("\
13994Set the Ada source character set."), _("\
13995Show the Ada source character set."), _("\
13996The character set used for Ada source files.\n\
13997This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13998 nullptr, nullptr,
13999 &set_ada_list, &show_ada_list);
14000
9ac4176b
PA
14001 add_catch_command ("exception", _("\
14002Catch Ada exceptions, when raised.\n\
9bf7038b 14003Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
14004Without any argument, stop when any Ada exception is raised.\n\
14005If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
14006being raised does not have a handler (and will therefore lead to the task's\n\
14007termination).\n\
14008Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
14009raised is the same as ARG.\n\
14010CONDITION is a boolean expression that is evaluated to see whether the\n\
14011exception should cause a stop."),
9ac4176b 14012 catch_ada_exception_command,
71bed2db 14013 catch_ada_completer,
9ac4176b
PA
14014 CATCH_PERMANENT,
14015 CATCH_TEMPORARY);
9f757bf7
XR
14016
14017 add_catch_command ("handlers", _("\
14018Catch Ada exceptions, when handled.\n\
9bf7038b
TT
14019Usage: catch handlers [ARG] [if CONDITION]\n\
14020Without any argument, stop when any Ada exception is handled.\n\
14021With an argument, catch only exceptions with the given name.\n\
14022CONDITION is a boolean expression that is evaluated to see whether the\n\
14023exception should cause a stop."),
9f757bf7 14024 catch_ada_handlers_command,
dda83cd7 14025 catch_ada_completer,
9f757bf7
XR
14026 CATCH_PERMANENT,
14027 CATCH_TEMPORARY);
9ac4176b
PA
14028 add_catch_command ("assert", _("\
14029Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
14030Usage: catch assert [if CONDITION]\n\
14031CONDITION is a boolean expression that is evaluated to see whether the\n\
14032exception should cause a stop."),
9ac4176b 14033 catch_assert_command,
dda83cd7 14034 NULL,
9ac4176b
PA
14035 CATCH_PERMANENT,
14036 CATCH_TEMPORARY);
14037
778865d3
JB
14038 add_info ("exceptions", info_exceptions_command,
14039 _("\
14040List all Ada exception names.\n\
9bf7038b 14041Usage: info exceptions [REGEXP]\n\
778865d3
JB
14042If a regular expression is passed as an argument, only those matching\n\
14043the regular expression are listed."));
14044
f54bdb6d
SM
14045 add_setshow_prefix_cmd ("ada", class_maintenance,
14046 _("Set Ada maintenance-related variables."),
14047 _("Show Ada maintenance-related variables."),
14048 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
14049 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
14050
14051 add_setshow_boolean_cmd
14052 ("ignore-descriptive-types", class_maintenance,
14053 &ada_ignore_descriptive_types_p,
14054 _("Set whether descriptive types generated by GNAT should be ignored."),
14055 _("Show whether descriptive types generated by GNAT should be ignored."),
14056 _("\
14057When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14058DWARF attribute."),
14059 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14060
2698f5ea
TT
14061 decoded_names_store = htab_create_alloc (256, htab_hash_string,
14062 htab_eq_string,
459a2e4c 14063 NULL, xcalloc, xfree);
6b69afc4 14064
3d9434b5 14065 /* The ada-lang observers. */
c90e7d63
SM
14066 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
14067 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
14068 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
14f9c5c9 14069}