]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
Unify gdb putc functions
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
4a94e368 3 Copyright (C) 1992-2022 Free Software Foundation, Inc.
14f9c5c9 4
a9762ec7 5 This file is part of GDB.
14f9c5c9 6
a9762ec7
JB
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
14f9c5c9 11
a9762ec7
JB
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
14f9c5c9 16
a9762ec7
JB
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 19
96d887e8 20
4c4b4cd2 21#include "defs.h"
14f9c5c9 22#include <ctype.h>
d322d6d6 23#include "gdbsupport/gdb_regex.h"
4de283e4
TT
24#include "frame.h"
25#include "symtab.h"
26#include "gdbtypes.h"
14f9c5c9 27#include "gdbcmd.h"
4de283e4
TT
28#include "expression.h"
29#include "parser-defs.h"
30#include "language.h"
31#include "varobj.h"
4de283e4
TT
32#include "inferior.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "breakpoint.h"
14f9c5c9 36#include "gdbcore.h"
4c4b4cd2 37#include "hashtab.h"
bf31fd38 38#include "gdbsupport/gdb_obstack.h"
4de283e4
TT
39#include "ada-lang.h"
40#include "completer.h"
4de283e4
TT
41#include "ui-out.h"
42#include "block.h"
04714b91 43#include "infcall.h"
4de283e4
TT
44#include "annotate.h"
45#include "valprint.h"
d55e5aa6 46#include "source.h"
4de283e4 47#include "observable.h"
692465f1 48#include "stack.h"
79d43c61 49#include "typeprint.h"
4de283e4 50#include "namespace.h"
7f6aba03 51#include "cli/cli-style.h"
0f8e2034 52#include "cli/cli-decode.h"
4de283e4 53
40bc484c 54#include "value.h"
4de283e4
TT
55#include "mi/mi-common.h"
56#include "arch-utils.h"
57#include "cli/cli-utils.h"
268a13a5
TT
58#include "gdbsupport/function-view.h"
59#include "gdbsupport/byte-vector.h"
4de283e4 60#include <algorithm>
03070ee9 61#include "ada-exp.h"
315e4ebb 62#include "charset.h"
ccefe4c4 63
4c4b4cd2 64/* Define whether or not the C operator '/' truncates towards zero for
0963b4bd 65 differently signed operands (truncation direction is undefined in C).
4c4b4cd2
PH
66 Copied from valarith.c. */
67
68#ifndef TRUNCATION_TOWARDS_ZERO
69#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
70#endif
71
d2e4a39e 72static struct type *desc_base_type (struct type *);
14f9c5c9 73
d2e4a39e 74static struct type *desc_bounds_type (struct type *);
14f9c5c9 75
d2e4a39e 76static struct value *desc_bounds (struct value *);
14f9c5c9 77
d2e4a39e 78static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 79
d2e4a39e 80static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 81
556bdfd4 82static struct type *desc_data_target_type (struct type *);
14f9c5c9 83
d2e4a39e 84static struct value *desc_data (struct value *);
14f9c5c9 85
d2e4a39e 86static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 87
d2e4a39e 88static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 89
d2e4a39e 90static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 91
d2e4a39e 92static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 93
d2e4a39e 94static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 95
d2e4a39e 96static struct type *desc_index_type (struct type *, int);
14f9c5c9 97
d2e4a39e 98static int desc_arity (struct type *);
14f9c5c9 99
d2e4a39e 100static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 101
40bc484c 102static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 103
d1183b06 104static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
105 const struct block *,
106 const lookup_name_info &lookup_name,
107 domain_enum, struct objfile *);
14f9c5c9 108
d1183b06
TT
109static void ada_add_all_symbols (std::vector<struct block_symbol> &,
110 const struct block *,
b5ec771e
PA
111 const lookup_name_info &lookup_name,
112 domain_enum, int, int *);
22cee43f 113
d1183b06 114static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 115
d1183b06
TT
116static void add_defn_to_vec (std::vector<struct block_symbol> &,
117 struct symbol *,
dda83cd7 118 const struct block *);
14f9c5c9 119
d2e4a39e 120static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 121
4c4b4cd2 122static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 123
d2e4a39e 124static int numeric_type_p (struct type *);
14f9c5c9 125
d2e4a39e 126static int integer_type_p (struct type *);
14f9c5c9 127
d2e4a39e 128static int scalar_type_p (struct type *);
14f9c5c9 129
d2e4a39e 130static int discrete_type_p (struct type *);
14f9c5c9 131
a121b7c1 132static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 133 int, int);
4c4b4cd2 134
b4ba55a1 135static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 136 const char *);
b4ba55a1 137
d2e4a39e 138static int is_dynamic_field (struct type *, int);
14f9c5c9 139
10a2c479 140static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 141 const gdb_byte *,
dda83cd7 142 CORE_ADDR, struct value *);
4c4b4cd2
PH
143
144static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 145
28c85d6c 146static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 147
d2e4a39e 148static struct type *to_static_fixed_type (struct type *);
f192137b 149static struct type *static_unwrap_type (struct type *type);
14f9c5c9 150
d2e4a39e 151static struct value *unwrap_value (struct value *);
14f9c5c9 152
ad82864c 153static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 154
ad82864c 155static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 156
ad82864c
JB
157static long decode_packed_array_bitsize (struct type *);
158
159static struct value *decode_constrained_packed_array (struct value *);
160
ad82864c 161static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 162
d2e4a39e 163static struct value *value_subscript_packed (struct value *, int,
dda83cd7 164 struct value **);
14f9c5c9 165
4c4b4cd2 166static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 167 struct type *);
14f9c5c9 168
d2e4a39e 169static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 170
d2e4a39e 171static int equiv_types (struct type *, struct type *);
14f9c5c9 172
d2e4a39e 173static int is_name_suffix (const char *);
14f9c5c9 174
59c8a30b 175static int advance_wild_match (const char **, const char *, char);
73589123 176
b5ec771e 177static bool wild_match (const char *name, const char *patn);
14f9c5c9 178
d2e4a39e 179static struct value *ada_coerce_ref (struct value *);
14f9c5c9 180
4c4b4cd2
PH
181static LONGEST pos_atr (struct value *);
182
53a47a3e
TT
183static struct value *val_atr (struct type *, LONGEST);
184
4c4b4cd2 185static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 186 domain_enum);
14f9c5c9 187
108d56a4 188static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 189 struct type *);
4c4b4cd2 190
0d5cff50 191static int find_struct_field (const char *, struct type *, int,
dda83cd7 192 struct type **, int *, int *, int *, int *);
4c4b4cd2 193
d1183b06 194static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 195 struct value **, int, const char *,
7056f312 196 struct type *, bool);
4c4b4cd2 197
4c4b4cd2
PH
198static int ada_is_direct_array_type (struct type *);
199
52ce6436
PH
200static struct value *ada_index_struct_field (int, struct value *, int,
201 struct type *);
202
cf608cc4 203static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
204
205
852dff6c 206static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
207
208static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
209 (const lookup_name_info &lookup_name);
210
4c4b4cd2
PH
211\f
212
315e4ebb
TT
213/* The character set used for source files. */
214static const char *ada_source_charset;
215
216/* The string "UTF-8". This is here so we can check for the UTF-8
217 charset using == rather than strcmp. */
218static const char ada_utf8[] = "UTF-8";
219
220/* Each entry in the UTF-32 case-folding table is of this form. */
221struct utf8_entry
222{
223 /* The start and end, inclusive, of this range of codepoints. */
224 uint32_t start, end;
225 /* The delta to apply to get the upper-case form. 0 if this is
226 already upper-case. */
227 int upper_delta;
228 /* The delta to apply to get the lower-case form. 0 if this is
229 already lower-case. */
230 int lower_delta;
231
232 bool operator< (uint32_t val) const
233 {
234 return end < val;
235 }
236};
237
238static const utf8_entry ada_case_fold[] =
239{
240#include "ada-casefold.h"
241};
242
243\f
244
ee01b665
JB
245/* The result of a symbol lookup to be stored in our symbol cache. */
246
247struct cache_entry
248{
249 /* The name used to perform the lookup. */
250 const char *name;
251 /* The namespace used during the lookup. */
fe978cb0 252 domain_enum domain;
ee01b665
JB
253 /* The symbol returned by the lookup, or NULL if no matching symbol
254 was found. */
255 struct symbol *sym;
256 /* The block where the symbol was found, or NULL if no matching
257 symbol was found. */
258 const struct block *block;
259 /* A pointer to the next entry with the same hash. */
260 struct cache_entry *next;
261};
262
263/* The Ada symbol cache, used to store the result of Ada-mode symbol
264 lookups in the course of executing the user's commands.
265
266 The cache is implemented using a simple, fixed-sized hash.
267 The size is fixed on the grounds that there are not likely to be
268 all that many symbols looked up during any given session, regardless
269 of the size of the symbol table. If we decide to go to a resizable
270 table, let's just use the stuff from libiberty instead. */
271
272#define HASH_SIZE 1009
273
274struct ada_symbol_cache
275{
276 /* An obstack used to store the entries in our cache. */
bdcccc56 277 struct auto_obstack cache_space;
ee01b665
JB
278
279 /* The root of the hash table used to implement our symbol cache. */
bdcccc56 280 struct cache_entry *root[HASH_SIZE] {};
ee01b665
JB
281};
282
67cb5b2d 283static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
284#ifdef VMS
285 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
286#else
14f9c5c9 287 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 288#endif
14f9c5c9 289
4c4b4cd2 290/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 291static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 292 = "__gnat_ada_main_program_name";
14f9c5c9 293
4c4b4cd2
PH
294/* Limit on the number of warnings to raise per expression evaluation. */
295static int warning_limit = 2;
296
297/* Number of warning messages issued; reset to 0 by cleanups after
298 expression evaluation. */
299static int warnings_issued = 0;
300
27087b7f 301static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
302 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
303};
304
27087b7f 305static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
306 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
307};
308
c6044dd1
JB
309/* Maintenance-related settings for this module. */
310
311static struct cmd_list_element *maint_set_ada_cmdlist;
312static struct cmd_list_element *maint_show_ada_cmdlist;
313
c6044dd1
JB
314/* The "maintenance ada set/show ignore-descriptive-type" value. */
315
491144b5 316static bool ada_ignore_descriptive_types_p = false;
c6044dd1 317
e802dbe0
JB
318 /* Inferior-specific data. */
319
320/* Per-inferior data for this module. */
321
322struct ada_inferior_data
323{
324 /* The ada__tags__type_specific_data type, which is used when decoding
325 tagged types. With older versions of GNAT, this type was directly
326 accessible through a component ("tsd") in the object tag. But this
327 is no longer the case, so we cache it for each inferior. */
f37b313d 328 struct type *tsd_type = nullptr;
3eecfa55
JB
329
330 /* The exception_support_info data. This data is used to determine
331 how to implement support for Ada exception catchpoints in a given
332 inferior. */
f37b313d 333 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
334};
335
336/* Our key to this module's inferior data. */
f37b313d 337static const struct inferior_key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
338
339/* Return our inferior data for the given inferior (INF).
340
341 This function always returns a valid pointer to an allocated
342 ada_inferior_data structure. If INF's inferior data has not
343 been previously set, this functions creates a new one with all
344 fields set to zero, sets INF's inferior to it, and then returns
345 a pointer to that newly allocated ada_inferior_data. */
346
347static struct ada_inferior_data *
348get_ada_inferior_data (struct inferior *inf)
349{
350 struct ada_inferior_data *data;
351
f37b313d 352 data = ada_inferior_data.get (inf);
e802dbe0 353 if (data == NULL)
f37b313d 354 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
355
356 return data;
357}
358
359/* Perform all necessary cleanups regarding our module's inferior data
360 that is required after the inferior INF just exited. */
361
362static void
363ada_inferior_exit (struct inferior *inf)
364{
f37b313d 365 ada_inferior_data.clear (inf);
e802dbe0
JB
366}
367
ee01b665
JB
368
369 /* program-space-specific data. */
370
371/* This module's per-program-space data. */
372struct ada_pspace_data
373{
374 /* The Ada symbol cache. */
bdcccc56 375 std::unique_ptr<ada_symbol_cache> sym_cache;
ee01b665
JB
376};
377
378/* Key to our per-program-space data. */
f37b313d 379static const struct program_space_key<ada_pspace_data> ada_pspace_data_handle;
ee01b665
JB
380
381/* Return this module's data for the given program space (PSPACE).
382 If not is found, add a zero'ed one now.
383
384 This function always returns a valid object. */
385
386static struct ada_pspace_data *
387get_ada_pspace_data (struct program_space *pspace)
388{
389 struct ada_pspace_data *data;
390
f37b313d 391 data = ada_pspace_data_handle.get (pspace);
ee01b665 392 if (data == NULL)
f37b313d 393 data = ada_pspace_data_handle.emplace (pspace);
ee01b665
JB
394
395 return data;
396}
397
dda83cd7 398 /* Utilities */
4c4b4cd2 399
720d1a40 400/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 401 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
402
403 Normally, we really expect a typedef type to only have 1 typedef layer.
404 In other words, we really expect the target type of a typedef type to be
405 a non-typedef type. This is particularly true for Ada units, because
406 the language does not have a typedef vs not-typedef distinction.
407 In that respect, the Ada compiler has been trying to eliminate as many
408 typedef definitions in the debugging information, since they generally
409 do not bring any extra information (we still use typedef under certain
410 circumstances related mostly to the GNAT encoding).
411
412 Unfortunately, we have seen situations where the debugging information
413 generated by the compiler leads to such multiple typedef layers. For
414 instance, consider the following example with stabs:
415
416 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
417 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
418
419 This is an error in the debugging information which causes type
420 pck__float_array___XUP to be defined twice, and the second time,
421 it is defined as a typedef of a typedef.
422
423 This is on the fringe of legality as far as debugging information is
424 concerned, and certainly unexpected. But it is easy to handle these
425 situations correctly, so we can afford to be lenient in this case. */
426
427static struct type *
428ada_typedef_target_type (struct type *type)
429{
78134374 430 while (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
431 type = TYPE_TARGET_TYPE (type);
432 return type;
433}
434
41d27058
JB
435/* Given DECODED_NAME a string holding a symbol name in its
436 decoded form (ie using the Ada dotted notation), returns
437 its unqualified name. */
438
439static const char *
440ada_unqualified_name (const char *decoded_name)
441{
2b0f535a
JB
442 const char *result;
443
444 /* If the decoded name starts with '<', it means that the encoded
445 name does not follow standard naming conventions, and thus that
446 it is not your typical Ada symbol name. Trying to unqualify it
447 is therefore pointless and possibly erroneous. */
448 if (decoded_name[0] == '<')
449 return decoded_name;
450
451 result = strrchr (decoded_name, '.');
41d27058
JB
452 if (result != NULL)
453 result++; /* Skip the dot... */
454 else
455 result = decoded_name;
456
457 return result;
458}
459
39e7af3e 460/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 461
39e7af3e 462static std::string
41d27058
JB
463add_angle_brackets (const char *str)
464{
39e7af3e 465 return string_printf ("<%s>", str);
41d27058 466}
96d887e8 467
14f9c5c9 468/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 469 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
470
471static int
ebf56fd3 472field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
473{
474 int len = strlen (target);
5b4ee69b 475
d2e4a39e 476 return
4c4b4cd2
PH
477 (strncmp (field_name, target, len) == 0
478 && (field_name[len] == '\0'
dda83cd7
SM
479 || (startswith (field_name + len, "___")
480 && strcmp (field_name + strlen (field_name) - 6,
481 "___XVN") != 0)));
14f9c5c9
AS
482}
483
484
872c8b51
JB
485/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
486 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
487 and return its index. This function also handles fields whose name
488 have ___ suffixes because the compiler sometimes alters their name
489 by adding such a suffix to represent fields with certain constraints.
490 If the field could not be found, return a negative number if
491 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
492
493int
494ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 495 int maybe_missing)
4c4b4cd2
PH
496{
497 int fieldno;
872c8b51
JB
498 struct type *struct_type = check_typedef ((struct type *) type);
499
1f704f76 500 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 501 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
502 return fieldno;
503
504 if (!maybe_missing)
323e0a4a 505 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 506 field_name, struct_type->name ());
4c4b4cd2
PH
507
508 return -1;
509}
510
511/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
512
513int
d2e4a39e 514ada_name_prefix_len (const char *name)
14f9c5c9
AS
515{
516 if (name == NULL)
517 return 0;
d2e4a39e 518 else
14f9c5c9 519 {
d2e4a39e 520 const char *p = strstr (name, "___");
5b4ee69b 521
14f9c5c9 522 if (p == NULL)
dda83cd7 523 return strlen (name);
14f9c5c9 524 else
dda83cd7 525 return p - name;
14f9c5c9
AS
526 }
527}
528
4c4b4cd2
PH
529/* Return non-zero if SUFFIX is a suffix of STR.
530 Return zero if STR is null. */
531
14f9c5c9 532static int
d2e4a39e 533is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
534{
535 int len1, len2;
5b4ee69b 536
14f9c5c9
AS
537 if (str == NULL)
538 return 0;
539 len1 = strlen (str);
540 len2 = strlen (suffix);
4c4b4cd2 541 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
542}
543
4c4b4cd2
PH
544/* The contents of value VAL, treated as a value of type TYPE. The
545 result is an lval in memory if VAL is. */
14f9c5c9 546
d2e4a39e 547static struct value *
4c4b4cd2 548coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 549{
61ee279c 550 type = ada_check_typedef (type);
df407dfe 551 if (value_type (val) == type)
4c4b4cd2 552 return val;
d2e4a39e 553 else
14f9c5c9 554 {
4c4b4cd2
PH
555 struct value *result;
556
f73e424f
TT
557 if (value_optimized_out (val))
558 result = allocate_optimized_out_value (type);
559 else if (value_lazy (val)
560 /* Be careful not to make a lazy not_lval value. */
561 || (VALUE_LVAL (val) != not_lval
562 && TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val))))
41e8491f
JK
563 result = allocate_value_lazy (type);
564 else
565 {
566 result = allocate_value (type);
f73e424f 567 value_contents_copy (result, 0, val, 0, TYPE_LENGTH (type));
41e8491f 568 }
74bcbdf3 569 set_value_component_location (result, val);
9bbda503
AC
570 set_value_bitsize (result, value_bitsize (val));
571 set_value_bitpos (result, value_bitpos (val));
c408a94f
TT
572 if (VALUE_LVAL (result) == lval_memory)
573 set_value_address (result, value_address (val));
14f9c5c9
AS
574 return result;
575 }
576}
577
fc1a4b47
AC
578static const gdb_byte *
579cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
580{
581 if (valaddr == NULL)
582 return NULL;
583 else
584 return valaddr + offset;
585}
586
587static CORE_ADDR
ebf56fd3 588cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
589{
590 if (address == 0)
591 return 0;
d2e4a39e 592 else
14f9c5c9
AS
593 return address + offset;
594}
595
4c4b4cd2
PH
596/* Issue a warning (as for the definition of warning in utils.c, but
597 with exactly one argument rather than ...), unless the limit on the
598 number of warnings has passed during the evaluation of the current
599 expression. */
a2249542 600
77109804
AC
601/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
602 provided by "complaint". */
a0b31db1 603static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 604
14f9c5c9 605static void
a2249542 606lim_warning (const char *format, ...)
14f9c5c9 607{
a2249542 608 va_list args;
a2249542 609
5b4ee69b 610 va_start (args, format);
4c4b4cd2
PH
611 warnings_issued += 1;
612 if (warnings_issued <= warning_limit)
a2249542
MK
613 vwarning (format, args);
614
615 va_end (args);
4c4b4cd2
PH
616}
617
0963b4bd 618/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 619static LONGEST
c3e5cd34 620max_of_size (int size)
4c4b4cd2 621{
76a01679 622 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 623
76a01679 624 return top_bit | (top_bit - 1);
4c4b4cd2
PH
625}
626
0963b4bd 627/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 628static LONGEST
c3e5cd34 629min_of_size (int size)
4c4b4cd2 630{
c3e5cd34 631 return -max_of_size (size) - 1;
4c4b4cd2
PH
632}
633
0963b4bd 634/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 635static ULONGEST
c3e5cd34 636umax_of_size (int size)
4c4b4cd2 637{
76a01679 638 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 639
76a01679 640 return top_bit | (top_bit - 1);
4c4b4cd2
PH
641}
642
0963b4bd 643/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
644static LONGEST
645max_of_type (struct type *t)
4c4b4cd2 646{
c6d940a9 647 if (t->is_unsigned ())
c3e5cd34
PH
648 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
649 else
650 return max_of_size (TYPE_LENGTH (t));
651}
652
0963b4bd 653/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
654static LONGEST
655min_of_type (struct type *t)
656{
c6d940a9 657 if (t->is_unsigned ())
c3e5cd34
PH
658 return 0;
659 else
660 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
661}
662
663/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
664LONGEST
665ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 666{
b249d2c2 667 type = resolve_dynamic_type (type, {}, 0);
78134374 668 switch (type->code ())
4c4b4cd2
PH
669 {
670 case TYPE_CODE_RANGE:
d1fd641e
SM
671 {
672 const dynamic_prop &high = type->bounds ()->high;
673
674 if (high.kind () == PROP_CONST)
675 return high.const_val ();
676 else
677 {
678 gdb_assert (high.kind () == PROP_UNDEFINED);
679
680 /* This happens when trying to evaluate a type's dynamic bound
681 without a live target. There is nothing relevant for us to
682 return here, so return 0. */
683 return 0;
684 }
685 }
4c4b4cd2 686 case TYPE_CODE_ENUM:
970db518 687 return type->field (type->num_fields () - 1).loc_enumval ();
690cc4eb
PH
688 case TYPE_CODE_BOOL:
689 return 1;
690 case TYPE_CODE_CHAR:
76a01679 691 case TYPE_CODE_INT:
690cc4eb 692 return max_of_type (type);
4c4b4cd2 693 default:
43bbcdc2 694 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
695 }
696}
697
14e75d8e 698/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
699LONGEST
700ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 701{
b249d2c2 702 type = resolve_dynamic_type (type, {}, 0);
78134374 703 switch (type->code ())
4c4b4cd2
PH
704 {
705 case TYPE_CODE_RANGE:
d1fd641e
SM
706 {
707 const dynamic_prop &low = type->bounds ()->low;
708
709 if (low.kind () == PROP_CONST)
710 return low.const_val ();
711 else
712 {
713 gdb_assert (low.kind () == PROP_UNDEFINED);
714
715 /* This happens when trying to evaluate a type's dynamic bound
716 without a live target. There is nothing relevant for us to
717 return here, so return 0. */
718 return 0;
719 }
720 }
4c4b4cd2 721 case TYPE_CODE_ENUM:
970db518 722 return type->field (0).loc_enumval ();
690cc4eb
PH
723 case TYPE_CODE_BOOL:
724 return 0;
725 case TYPE_CODE_CHAR:
76a01679 726 case TYPE_CODE_INT:
690cc4eb 727 return min_of_type (type);
4c4b4cd2 728 default:
43bbcdc2 729 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
730 }
731}
732
733/* The identity on non-range types. For range types, the underlying
76a01679 734 non-range scalar type. */
4c4b4cd2
PH
735
736static struct type *
18af8284 737get_base_type (struct type *type)
4c4b4cd2 738{
78134374 739 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 740 {
76a01679 741 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
dda83cd7 742 return type;
4c4b4cd2
PH
743 type = TYPE_TARGET_TYPE (type);
744 }
745 return type;
14f9c5c9 746}
41246937
JB
747
748/* Return a decoded version of the given VALUE. This means returning
749 a value whose type is obtained by applying all the GNAT-specific
85102364 750 encodings, making the resulting type a static but standard description
41246937
JB
751 of the initial type. */
752
753struct value *
754ada_get_decoded_value (struct value *value)
755{
756 struct type *type = ada_check_typedef (value_type (value));
757
758 if (ada_is_array_descriptor_type (type)
759 || (ada_is_constrained_packed_array_type (type)
dda83cd7 760 && type->code () != TYPE_CODE_PTR))
41246937 761 {
78134374 762 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 763 value = ada_coerce_to_simple_array_ptr (value);
41246937 764 else
dda83cd7 765 value = ada_coerce_to_simple_array (value);
41246937
JB
766 }
767 else
768 value = ada_to_fixed_value (value);
769
770 return value;
771}
772
773/* Same as ada_get_decoded_value, but with the given TYPE.
774 Because there is no associated actual value for this type,
775 the resulting type might be a best-effort approximation in
776 the case of dynamic types. */
777
778struct type *
779ada_get_decoded_type (struct type *type)
780{
781 type = to_static_fixed_type (type);
782 if (ada_is_constrained_packed_array_type (type))
783 type = ada_coerce_to_simple_array_type (type);
784 return type;
785}
786
4c4b4cd2 787\f
76a01679 788
dda83cd7 789 /* Language Selection */
14f9c5c9
AS
790
791/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 792 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 793
de93309a 794static enum language
ccefe4c4 795ada_update_initial_language (enum language lang)
14f9c5c9 796{
cafb3438 797 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 798 return language_ada;
14f9c5c9
AS
799
800 return lang;
801}
96d887e8
PH
802
803/* If the main procedure is written in Ada, then return its name.
804 The result is good until the next call. Return NULL if the main
805 procedure doesn't appear to be in Ada. */
806
807char *
808ada_main_name (void)
809{
3b7344d5 810 struct bound_minimal_symbol msym;
e83e4e24 811 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 812
96d887e8
PH
813 /* For Ada, the name of the main procedure is stored in a specific
814 string constant, generated by the binder. Look for that symbol,
815 extract its address, and then read that string. If we didn't find
816 that string, then most probably the main procedure is not written
817 in Ada. */
818 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
819
3b7344d5 820 if (msym.minsym != NULL)
96d887e8 821 {
66920317 822 CORE_ADDR main_program_name_addr = BMSYMBOL_VALUE_ADDRESS (msym);
96d887e8 823 if (main_program_name_addr == 0)
dda83cd7 824 error (_("Invalid address for Ada main program name."));
96d887e8 825
66920317 826 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 827 return main_program_name.get ();
96d887e8
PH
828 }
829
830 /* The main procedure doesn't seem to be in Ada. */
831 return NULL;
832}
14f9c5c9 833\f
dda83cd7 834 /* Symbols */
d2e4a39e 835
4c4b4cd2
PH
836/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
837 of NULLs. */
14f9c5c9 838
d2e4a39e
AS
839const struct ada_opname_map ada_opname_table[] = {
840 {"Oadd", "\"+\"", BINOP_ADD},
841 {"Osubtract", "\"-\"", BINOP_SUB},
842 {"Omultiply", "\"*\"", BINOP_MUL},
843 {"Odivide", "\"/\"", BINOP_DIV},
844 {"Omod", "\"mod\"", BINOP_MOD},
845 {"Orem", "\"rem\"", BINOP_REM},
846 {"Oexpon", "\"**\"", BINOP_EXP},
847 {"Olt", "\"<\"", BINOP_LESS},
848 {"Ole", "\"<=\"", BINOP_LEQ},
849 {"Ogt", "\">\"", BINOP_GTR},
850 {"Oge", "\">=\"", BINOP_GEQ},
851 {"Oeq", "\"=\"", BINOP_EQUAL},
852 {"One", "\"/=\"", BINOP_NOTEQUAL},
853 {"Oand", "\"and\"", BINOP_BITWISE_AND},
854 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
855 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
856 {"Oconcat", "\"&\"", BINOP_CONCAT},
857 {"Oabs", "\"abs\"", UNOP_ABS},
858 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
859 {"Oadd", "\"+\"", UNOP_PLUS},
860 {"Osubtract", "\"-\"", UNOP_NEG},
861 {NULL, NULL}
14f9c5c9
AS
862};
863
965bc1df
TT
864/* If STR is a decoded version of a compiler-provided suffix (like the
865 "[cold]" in "symbol[cold]"), return true. Otherwise, return
866 false. */
867
868static bool
869is_compiler_suffix (const char *str)
870{
871 gdb_assert (*str == '[');
872 ++str;
873 while (*str != '\0' && isalpha (*str))
874 ++str;
875 /* We accept a missing "]" in order to support completion. */
876 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
877}
878
315e4ebb
TT
879/* Append a non-ASCII character to RESULT. */
880static void
881append_hex_encoded (std::string &result, uint32_t one_char)
882{
883 if (one_char <= 0xff)
884 {
885 result.append ("U");
886 result.append (phex (one_char, 1));
887 }
888 else if (one_char <= 0xffff)
889 {
890 result.append ("W");
891 result.append (phex (one_char, 2));
892 }
893 else
894 {
895 result.append ("WW");
896 result.append (phex (one_char, 4));
897 }
898}
899
900/* Return a string that is a copy of the data in STORAGE, with
901 non-ASCII characters replaced by the appropriate hex encoding. A
902 template is used because, for UTF-8, we actually want to work with
903 UTF-32 codepoints. */
904template<typename T>
905std::string
906copy_and_hex_encode (struct obstack *storage)
907{
908 const T *chars = (T *) obstack_base (storage);
909 int num_chars = obstack_object_size (storage) / sizeof (T);
910 std::string result;
911 for (int i = 0; i < num_chars; ++i)
912 {
913 if (chars[i] <= 0x7f)
914 {
915 /* The host character set has to be a superset of ASCII, as
916 are all the other character sets we can use. */
917 result.push_back (chars[i]);
918 }
919 else
920 append_hex_encoded (result, chars[i]);
921 }
922 return result;
923}
924
5c4258f4 925/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 926 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 927 Otherwise, return the empty string in that case. */
4c4b4cd2 928
5c4258f4 929static std::string
b5ec771e 930ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 931{
4c4b4cd2 932 if (decoded == NULL)
5c4258f4 933 return {};
14f9c5c9 934
5c4258f4 935 std::string encoding_buffer;
315e4ebb 936 bool saw_non_ascii = false;
5c4258f4 937 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 938 {
315e4ebb
TT
939 if ((*p & 0x80) != 0)
940 saw_non_ascii = true;
941
cdc7bb92 942 if (*p == '.')
5c4258f4 943 encoding_buffer.append ("__");
965bc1df
TT
944 else if (*p == '[' && is_compiler_suffix (p))
945 {
946 encoding_buffer = encoding_buffer + "." + (p + 1);
947 if (encoding_buffer.back () == ']')
948 encoding_buffer.pop_back ();
949 break;
950 }
14f9c5c9 951 else if (*p == '"')
dda83cd7
SM
952 {
953 const struct ada_opname_map *mapping;
954
955 for (mapping = ada_opname_table;
956 mapping->encoded != NULL
957 && !startswith (p, mapping->decoded); mapping += 1)
958 ;
959 if (mapping->encoded == NULL)
b5ec771e
PA
960 {
961 if (throw_errors)
962 error (_("invalid Ada operator name: %s"), p);
963 else
5c4258f4 964 return {};
b5ec771e 965 }
5c4258f4 966 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
967 break;
968 }
d2e4a39e 969 else
5c4258f4 970 encoding_buffer.push_back (*p);
14f9c5c9
AS
971 }
972
315e4ebb
TT
973 /* If a non-ASCII character is seen, we must convert it to the
974 appropriate hex form. As this is more expensive, we keep track
975 of whether it is even necessary. */
976 if (saw_non_ascii)
977 {
978 auto_obstack storage;
979 bool is_utf8 = ada_source_charset == ada_utf8;
980 try
981 {
982 convert_between_encodings
983 (host_charset (),
984 is_utf8 ? HOST_UTF32 : ada_source_charset,
985 (const gdb_byte *) encoding_buffer.c_str (),
986 encoding_buffer.length (), 1,
987 &storage, translit_none);
988 }
989 catch (const gdb_exception &)
990 {
991 static bool warned = false;
992
993 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
994 might like to know why. */
995 if (!warned)
996 {
997 warned = true;
998 warning (_("charset conversion failure for '%s'.\n"
999 "You may have the wrong value for 'set ada source-charset'."),
1000 encoding_buffer.c_str ());
1001 }
1002
1003 /* We don't try to recover from errors. */
1004 return encoding_buffer;
1005 }
1006
1007 if (is_utf8)
1008 return copy_and_hex_encode<uint32_t> (&storage);
1009 return copy_and_hex_encode<gdb_byte> (&storage);
1010 }
1011
4c4b4cd2 1012 return encoding_buffer;
14f9c5c9
AS
1013}
1014
315e4ebb
TT
1015/* Find the entry for C in the case-folding table. Return nullptr if
1016 the entry does not cover C. */
1017static const utf8_entry *
1018find_case_fold_entry (uint32_t c)
b5ec771e 1019{
315e4ebb
TT
1020 auto iter = std::lower_bound (std::begin (ada_case_fold),
1021 std::end (ada_case_fold),
1022 c);
1023 if (iter == std::end (ada_case_fold)
1024 || c < iter->start
1025 || c > iter->end)
1026 return nullptr;
1027 return &*iter;
b5ec771e
PA
1028}
1029
14f9c5c9 1030/* Return NAME folded to lower case, or, if surrounded by single
315e4ebb
TT
1031 quotes, unfolded, but with the quotes stripped away. If
1032 THROW_ON_ERROR is true, encoding failures will throw an exception
1033 rather than emitting a warning. Result good to next call. */
4c4b4cd2 1034
5f9febe0 1035static const char *
315e4ebb 1036ada_fold_name (gdb::string_view name, bool throw_on_error = false)
14f9c5c9 1037{
5f9febe0 1038 static std::string fold_storage;
14f9c5c9 1039
6a780b67 1040 if (!name.empty () && name[0] == '\'')
01573d73 1041 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
1042 else
1043 {
315e4ebb
TT
1044 /* Why convert to UTF-32 and implement our own case-folding,
1045 rather than convert to wchar_t and use the platform's
1046 functions? I'm glad you asked.
1047
1048 The main problem is that GNAT implements an unusual rule for
1049 case folding. For ASCII letters, letters in single-byte
1050 encodings (such as ISO-8859-*), and Unicode letters that fit
1051 in a single byte (i.e., code point is <= 0xff), the letter is
1052 folded to lower case. Other Unicode letters are folded to
1053 upper case.
1054
1055 This rule means that the code must be able to examine the
1056 value of the character. And, some hosts do not use Unicode
1057 for wchar_t, so examining the value of such characters is
1058 forbidden. */
1059 auto_obstack storage;
1060 try
1061 {
1062 convert_between_encodings
1063 (host_charset (), HOST_UTF32,
1064 (const gdb_byte *) name.data (),
1065 name.length (), 1,
1066 &storage, translit_none);
1067 }
1068 catch (const gdb_exception &)
1069 {
1070 if (throw_on_error)
1071 throw;
1072
1073 static bool warned = false;
1074
1075 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1076 might like to know why. */
1077 if (!warned)
1078 {
1079 warned = true;
1080 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1081 "This normally should not happen, please file a bug report."),
1082 gdb::to_string (name).c_str (), host_charset ());
1083 }
1084
1085 /* We don't try to recover from errors; just return the
1086 original string. */
1087 fold_storage = gdb::to_string (name);
1088 return fold_storage.c_str ();
1089 }
1090
1091 bool is_utf8 = ada_source_charset == ada_utf8;
1092 uint32_t *chars = (uint32_t *) obstack_base (&storage);
1093 int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
1094 for (int i = 0; i < num_chars; ++i)
1095 {
1096 const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
1097 if (entry != nullptr)
1098 {
1099 uint32_t low = chars[i] + entry->lower_delta;
1100 if (!is_utf8 || low <= 0xff)
1101 chars[i] = low;
1102 else
1103 chars[i] = chars[i] + entry->upper_delta;
1104 }
1105 }
1106
1107 /* Now convert back to ordinary characters. */
1108 auto_obstack reconverted;
1109 try
1110 {
1111 convert_between_encodings (HOST_UTF32,
1112 host_charset (),
1113 (const gdb_byte *) chars,
1114 num_chars * sizeof (uint32_t),
1115 sizeof (uint32_t),
1116 &reconverted,
1117 translit_none);
1118 obstack_1grow (&reconverted, '\0');
1119 fold_storage = std::string ((const char *) obstack_base (&reconverted));
1120 }
1121 catch (const gdb_exception &)
1122 {
1123 if (throw_on_error)
1124 throw;
1125
1126 static bool warned = false;
1127
1128 /* Converting back from UTF-32 shouldn't normally fail, but
1129 there are some host encodings without upper/lower
1130 equivalence. */
1131 if (!warned)
1132 {
1133 warned = true;
1134 warning (_("could not convert the lower-cased variant of '%s'\n"
1135 "from UTF-32 to the host encoding (%s)."),
1136 gdb::to_string (name).c_str (), host_charset ());
1137 }
1138
1139 /* We don't try to recover from errors; just return the
1140 original string. */
1141 fold_storage = gdb::to_string (name);
1142 }
14f9c5c9
AS
1143 }
1144
5f9febe0 1145 return fold_storage.c_str ();
14f9c5c9
AS
1146}
1147
315e4ebb
TT
1148/* The "encoded" form of DECODED, according to GNAT conventions. */
1149
1150std::string
1151ada_encode (const char *decoded)
1152{
1153 if (decoded[0] != '<')
1154 decoded = ada_fold_name (decoded);
1155 return ada_encode_1 (decoded, true);
1156}
1157
529cad9c
PH
1158/* Return nonzero if C is either a digit or a lowercase alphabet character. */
1159
1160static int
1161is_lower_alphanum (const char c)
1162{
1163 return (isdigit (c) || (isalpha (c) && islower (c)));
1164}
1165
c90092fe
JB
1166/* ENCODED is the linkage name of a symbol and LEN contains its length.
1167 This function saves in LEN the length of that same symbol name but
1168 without either of these suffixes:
29480c32
JB
1169 . .{DIGIT}+
1170 . ${DIGIT}+
1171 . ___{DIGIT}+
1172 . __{DIGIT}+.
c90092fe 1173
29480c32
JB
1174 These are suffixes introduced by the compiler for entities such as
1175 nested subprogram for instance, in order to avoid name clashes.
1176 They do not serve any purpose for the debugger. */
1177
1178static void
1179ada_remove_trailing_digits (const char *encoded, int *len)
1180{
1181 if (*len > 1 && isdigit (encoded[*len - 1]))
1182 {
1183 int i = *len - 2;
5b4ee69b 1184
29480c32 1185 while (i > 0 && isdigit (encoded[i]))
dda83cd7 1186 i--;
29480c32 1187 if (i >= 0 && encoded[i] == '.')
dda83cd7 1188 *len = i;
29480c32 1189 else if (i >= 0 && encoded[i] == '$')
dda83cd7 1190 *len = i;
61012eef 1191 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 1192 *len = i - 2;
61012eef 1193 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 1194 *len = i - 1;
29480c32
JB
1195 }
1196}
1197
1198/* Remove the suffix introduced by the compiler for protected object
1199 subprograms. */
1200
1201static void
1202ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1203{
1204 /* Remove trailing N. */
1205
1206 /* Protected entry subprograms are broken into two
1207 separate subprograms: The first one is unprotected, and has
1208 a 'N' suffix; the second is the protected version, and has
0963b4bd 1209 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1210 the protection. Since the P subprograms are internally generated,
1211 we leave these names undecoded, giving the user a clue that this
1212 entity is internal. */
1213
1214 if (*len > 1
1215 && encoded[*len - 1] == 'N'
1216 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1217 *len = *len - 1;
1218}
1219
965bc1df
TT
1220/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1221 then update *LEN to remove the suffix and return the offset of the
1222 character just past the ".". Otherwise, return -1. */
1223
1224static int
1225remove_compiler_suffix (const char *encoded, int *len)
1226{
1227 int offset = *len - 1;
1228 while (offset > 0 && isalpha (encoded[offset]))
1229 --offset;
1230 if (offset > 0 && encoded[offset] == '.')
1231 {
1232 *len = offset;
1233 return offset + 1;
1234 }
1235 return -1;
1236}
1237
315e4ebb
TT
1238/* Convert an ASCII hex string to a number. Reads exactly N
1239 characters from STR. Returns true on success, false if one of the
1240 digits was not a hex digit. */
1241static bool
1242convert_hex (const char *str, int n, uint32_t *out)
1243{
1244 uint32_t result = 0;
1245
1246 for (int i = 0; i < n; ++i)
1247 {
1248 if (!isxdigit (str[i]))
1249 return false;
1250 result <<= 4;
1251 result |= fromhex (str[i]);
1252 }
1253
1254 *out = result;
1255 return true;
1256}
1257
1258/* Convert a wide character from its ASCII hex representation in STR
1259 (consisting of exactly N characters) to the host encoding,
1260 appending the resulting bytes to OUT. If N==2 and the Ada source
1261 charset is not UTF-8, then hex refers to an encoding in the
1262 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1263 Return false and do not modify OUT on conversion failure. */
1264static bool
1265convert_from_hex_encoded (std::string &out, const char *str, int n)
1266{
1267 uint32_t value;
1268
1269 if (!convert_hex (str, n, &value))
1270 return false;
1271 try
1272 {
1273 auto_obstack bytes;
1274 /* In the 'U' case, the hex digits encode the character in the
1275 Ada source charset. However, if the source charset is UTF-8,
1276 this really means it is a single-byte UTF-32 character. */
1277 if (n == 2 && ada_source_charset != ada_utf8)
1278 {
1279 gdb_byte one_char = (gdb_byte) value;
1280
1281 convert_between_encodings (ada_source_charset, host_charset (),
1282 &one_char,
1283 sizeof (one_char), sizeof (one_char),
1284 &bytes, translit_none);
1285 }
1286 else
1287 convert_between_encodings (HOST_UTF32, host_charset (),
1288 (const gdb_byte *) &value,
1289 sizeof (value), sizeof (value),
1290 &bytes, translit_none);
1291 obstack_1grow (&bytes, '\0');
1292 out.append ((const char *) obstack_base (&bytes));
1293 }
1294 catch (const gdb_exception &)
1295 {
1296 /* On failure, the caller will just let the encoded form
1297 through, which seems basically reasonable. */
1298 return false;
1299 }
1300
1301 return true;
1302}
1303
8a3df5ac 1304/* See ada-lang.h. */
14f9c5c9 1305
f945dedf 1306std::string
8a3df5ac 1307ada_decode (const char *encoded, bool wrap)
14f9c5c9 1308{
36f5ca53 1309 int i;
14f9c5c9 1310 int len0;
d2e4a39e 1311 const char *p;
14f9c5c9 1312 int at_start_name;
f945dedf 1313 std::string decoded;
965bc1df 1314 int suffix = -1;
d2e4a39e 1315
0d81f350
JG
1316 /* With function descriptors on PPC64, the value of a symbol named
1317 ".FN", if it exists, is the entry point of the function "FN". */
1318 if (encoded[0] == '.')
1319 encoded += 1;
1320
29480c32
JB
1321 /* The name of the Ada main procedure starts with "_ada_".
1322 This prefix is not part of the decoded name, so skip this part
1323 if we see this prefix. */
61012eef 1324 if (startswith (encoded, "_ada_"))
4c4b4cd2 1325 encoded += 5;
14f9c5c9 1326
29480c32
JB
1327 /* If the name starts with '_', then it is not a properly encoded
1328 name, so do not attempt to decode it. Similarly, if the name
1329 starts with '<', the name should not be decoded. */
4c4b4cd2 1330 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1331 goto Suppress;
1332
4c4b4cd2 1333 len0 = strlen (encoded);
4c4b4cd2 1334
965bc1df
TT
1335 suffix = remove_compiler_suffix (encoded, &len0);
1336
29480c32
JB
1337 ada_remove_trailing_digits (encoded, &len0);
1338 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1339
4c4b4cd2
PH
1340 /* Remove the ___X.* suffix if present. Do not forget to verify that
1341 the suffix is located before the current "end" of ENCODED. We want
1342 to avoid re-matching parts of ENCODED that have previously been
1343 marked as discarded (by decrementing LEN0). */
1344 p = strstr (encoded, "___");
1345 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1346 {
1347 if (p[3] == 'X')
dda83cd7 1348 len0 = p - encoded;
14f9c5c9 1349 else
dda83cd7 1350 goto Suppress;
14f9c5c9 1351 }
4c4b4cd2 1352
29480c32
JB
1353 /* Remove any trailing TKB suffix. It tells us that this symbol
1354 is for the body of a task, but that information does not actually
1355 appear in the decoded name. */
1356
61012eef 1357 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1358 len0 -= 3;
76a01679 1359
a10967fa
JB
1360 /* Remove any trailing TB suffix. The TB suffix is slightly different
1361 from the TKB suffix because it is used for non-anonymous task
1362 bodies. */
1363
61012eef 1364 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1365 len0 -= 2;
1366
29480c32
JB
1367 /* Remove trailing "B" suffixes. */
1368 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1369
61012eef 1370 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1371 len0 -= 1;
1372
29480c32
JB
1373 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1374
4c4b4cd2 1375 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1376 {
4c4b4cd2
PH
1377 i = len0 - 2;
1378 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1379 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1380 i -= 1;
4c4b4cd2 1381 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1382 len0 = i - 1;
4c4b4cd2 1383 else if (encoded[i] == '$')
dda83cd7 1384 len0 = i;
d2e4a39e 1385 }
14f9c5c9 1386
29480c32
JB
1387 /* The first few characters that are not alphabetic are not part
1388 of any encoding we use, so we can copy them over verbatim. */
1389
36f5ca53
TT
1390 for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
1391 decoded.push_back (encoded[i]);
14f9c5c9
AS
1392
1393 at_start_name = 1;
1394 while (i < len0)
1395 {
29480c32 1396 /* Is this a symbol function? */
4c4b4cd2 1397 if (at_start_name && encoded[i] == 'O')
dda83cd7
SM
1398 {
1399 int k;
1400
1401 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1402 {
1403 int op_len = strlen (ada_opname_table[k].encoded);
1404 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1405 op_len - 1) == 0)
1406 && !isalnum (encoded[i + op_len]))
1407 {
36f5ca53 1408 decoded.append (ada_opname_table[k].decoded);
dda83cd7
SM
1409 at_start_name = 0;
1410 i += op_len;
dda83cd7
SM
1411 break;
1412 }
1413 }
1414 if (ada_opname_table[k].encoded != NULL)
1415 continue;
1416 }
14f9c5c9
AS
1417 at_start_name = 0;
1418
529cad9c 1419 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1420 into "." (just below). */
529cad9c 1421
61012eef 1422 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1423 i += 2;
529cad9c 1424
29480c32 1425 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1426 be translated into "." (just below). These are internal names
1427 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1428
1429 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1430 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1431 && isdigit (encoded [i+4]))
1432 {
1433 int k = i + 5;
1434
1435 while (k < len0 && isdigit (encoded[k]))
1436 k++; /* Skip any extra digit. */
1437
1438 /* Double-check that the "__B_{DIGITS}+" sequence we found
1439 is indeed followed by "__". */
1440 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1441 i = k;
1442 }
29480c32 1443
529cad9c
PH
1444 /* Remove _E{DIGITS}+[sb] */
1445
1446 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1447 of subprograms created by the compiler for each entry. The first
1448 one implements the actual entry code, and has a suffix following
1449 the convention above; the second one implements the barrier and
1450 uses the same convention as above, except that the 'E' is replaced
1451 by a 'B'.
529cad9c 1452
dda83cd7
SM
1453 Just as above, we do not decode the name of barrier functions
1454 to give the user a clue that the code he is debugging has been
1455 internally generated. */
529cad9c
PH
1456
1457 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1458 && isdigit (encoded[i+2]))
1459 {
1460 int k = i + 3;
1461
1462 while (k < len0 && isdigit (encoded[k]))
1463 k++;
1464
1465 if (k < len0
1466 && (encoded[k] == 'b' || encoded[k] == 's'))
1467 {
1468 k++;
1469 /* Just as an extra precaution, make sure that if this
1470 suffix is followed by anything else, it is a '_'.
1471 Otherwise, we matched this sequence by accident. */
1472 if (k == len0
1473 || (k < len0 && encoded[k] == '_'))
1474 i = k;
1475 }
1476 }
529cad9c
PH
1477
1478 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1479 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1480
1481 if (i < len0 + 3
dda83cd7
SM
1482 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1483 {
1484 /* Backtrack a bit up until we reach either the begining of
1485 the encoded name, or "__". Make sure that we only find
1486 digits or lowercase characters. */
1487 const char *ptr = encoded + i - 1;
1488
1489 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1490 ptr--;
1491 if (ptr < encoded
1492 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1493 i++;
1494 }
529cad9c 1495
315e4ebb
TT
1496 if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
1497 {
1498 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
1499 {
1500 i += 3;
1501 continue;
1502 }
1503 }
1504 else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
1505 {
1506 if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
1507 {
1508 i += 5;
1509 continue;
1510 }
1511 }
1512 else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
1513 && isxdigit (encoded[i + 2]))
1514 {
1515 if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
1516 {
1517 i += 10;
1518 continue;
1519 }
1520 }
1521
4c4b4cd2 1522 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1523 {
1524 /* This is a X[bn]* sequence not separated from the previous
1525 part of the name with a non-alpha-numeric character (in other
1526 words, immediately following an alpha-numeric character), then
1527 verify that it is placed at the end of the encoded name. If
1528 not, then the encoding is not valid and we should abort the
1529 decoding. Otherwise, just skip it, it is used in body-nested
1530 package names. */
1531 do
1532 i += 1;
1533 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1534 if (i < len0)
1535 goto Suppress;
1536 }
cdc7bb92 1537 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1538 {
1539 /* Replace '__' by '.'. */
36f5ca53 1540 decoded.push_back ('.');
dda83cd7
SM
1541 at_start_name = 1;
1542 i += 2;
dda83cd7 1543 }
14f9c5c9 1544 else
dda83cd7
SM
1545 {
1546 /* It's a character part of the decoded name, so just copy it
1547 over. */
36f5ca53 1548 decoded.push_back (encoded[i]);
dda83cd7 1549 i += 1;
dda83cd7 1550 }
14f9c5c9 1551 }
14f9c5c9 1552
29480c32
JB
1553 /* Decoded names should never contain any uppercase character.
1554 Double-check this, and abort the decoding if we find one. */
1555
f945dedf 1556 for (i = 0; i < decoded.length(); ++i)
4c4b4cd2 1557 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1558 goto Suppress;
1559
965bc1df
TT
1560 /* If the compiler added a suffix, append it now. */
1561 if (suffix >= 0)
1562 decoded = decoded + "[" + &encoded[suffix] + "]";
1563
f945dedf 1564 return decoded;
14f9c5c9
AS
1565
1566Suppress:
8a3df5ac
TT
1567 if (!wrap)
1568 return {};
1569
4c4b4cd2 1570 if (encoded[0] == '<')
f945dedf 1571 decoded = encoded;
14f9c5c9 1572 else
f945dedf 1573 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1574 return decoded;
4c4b4cd2
PH
1575}
1576
1577/* Table for keeping permanent unique copies of decoded names. Once
1578 allocated, names in this table are never released. While this is a
1579 storage leak, it should not be significant unless there are massive
1580 changes in the set of decoded names in successive versions of a
1581 symbol table loaded during a single session. */
1582static struct htab *decoded_names_store;
1583
1584/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1585 in the language-specific part of GSYMBOL, if it has not been
1586 previously computed. Tries to save the decoded name in the same
1587 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1588 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1589 GSYMBOL).
4c4b4cd2
PH
1590 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1591 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1592 when a decoded name is cached in it. */
4c4b4cd2 1593
45e6c716 1594const char *
f85f34ed 1595ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1596{
f85f34ed
TT
1597 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1598 const char **resultp =
615b3f62 1599 &gsymbol->language_specific.demangled_name;
5b4ee69b 1600
f85f34ed 1601 if (!gsymbol->ada_mangled)
4c4b4cd2 1602 {
4d4eaa30 1603 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1604 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1605
f85f34ed 1606 gsymbol->ada_mangled = 1;
5b4ee69b 1607
f85f34ed 1608 if (obstack != NULL)
f945dedf 1609 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1610 else
dda83cd7 1611 {
f85f34ed
TT
1612 /* Sometimes, we can't find a corresponding objfile, in
1613 which case, we put the result on the heap. Since we only
1614 decode when needed, we hope this usually does not cause a
1615 significant memory leak (FIXME). */
1616
dda83cd7
SM
1617 char **slot = (char **) htab_find_slot (decoded_names_store,
1618 decoded.c_str (), INSERT);
5b4ee69b 1619
dda83cd7
SM
1620 if (*slot == NULL)
1621 *slot = xstrdup (decoded.c_str ());
1622 *resultp = *slot;
1623 }
4c4b4cd2 1624 }
14f9c5c9 1625
4c4b4cd2
PH
1626 return *resultp;
1627}
76a01679 1628
14f9c5c9 1629\f
d2e4a39e 1630
dda83cd7 1631 /* Arrays */
14f9c5c9 1632
28c85d6c
JB
1633/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1634 generated by the GNAT compiler to describe the index type used
1635 for each dimension of an array, check whether it follows the latest
1636 known encoding. If not, fix it up to conform to the latest encoding.
1637 Otherwise, do nothing. This function also does nothing if
1638 INDEX_DESC_TYPE is NULL.
1639
85102364 1640 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1641 Initially, the information would be provided through the name of each
1642 field of the structure type only, while the type of these fields was
1643 described as unspecified and irrelevant. The debugger was then expected
1644 to perform a global type lookup using the name of that field in order
1645 to get access to the full index type description. Because these global
1646 lookups can be very expensive, the encoding was later enhanced to make
1647 the global lookup unnecessary by defining the field type as being
1648 the full index type description.
1649
1650 The purpose of this routine is to allow us to support older versions
1651 of the compiler by detecting the use of the older encoding, and by
1652 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1653 we essentially replace each field's meaningless type by the associated
1654 index subtype). */
1655
1656void
1657ada_fixup_array_indexes_type (struct type *index_desc_type)
1658{
1659 int i;
1660
1661 if (index_desc_type == NULL)
1662 return;
1f704f76 1663 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1664
1665 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1666 to check one field only, no need to check them all). If not, return
1667 now.
1668
1669 If our INDEX_DESC_TYPE was generated using the older encoding,
1670 the field type should be a meaningless integer type whose name
1671 is not equal to the field name. */
940da03e
SM
1672 if (index_desc_type->field (0).type ()->name () != NULL
1673 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1674 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1675 return;
1676
1677 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1678 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1679 {
33d16dd9 1680 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1681 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1682
1683 if (raw_type)
5d14b6e5 1684 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1685 }
1686}
1687
4c4b4cd2
PH
1688/* The desc_* routines return primitive portions of array descriptors
1689 (fat pointers). */
14f9c5c9
AS
1690
1691/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1692 level of indirection, if needed. */
1693
d2e4a39e
AS
1694static struct type *
1695desc_base_type (struct type *type)
14f9c5c9
AS
1696{
1697 if (type == NULL)
1698 return NULL;
61ee279c 1699 type = ada_check_typedef (type);
78134374 1700 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1701 type = ada_typedef_target_type (type);
1702
1265e4aa 1703 if (type != NULL
78134374 1704 && (type->code () == TYPE_CODE_PTR
dda83cd7 1705 || type->code () == TYPE_CODE_REF))
61ee279c 1706 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1707 else
1708 return type;
1709}
1710
4c4b4cd2
PH
1711/* True iff TYPE indicates a "thin" array pointer type. */
1712
14f9c5c9 1713static int
d2e4a39e 1714is_thin_pntr (struct type *type)
14f9c5c9 1715{
d2e4a39e 1716 return
14f9c5c9
AS
1717 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1718 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1719}
1720
4c4b4cd2
PH
1721/* The descriptor type for thin pointer type TYPE. */
1722
d2e4a39e
AS
1723static struct type *
1724thin_descriptor_type (struct type *type)
14f9c5c9 1725{
d2e4a39e 1726 struct type *base_type = desc_base_type (type);
5b4ee69b 1727
14f9c5c9
AS
1728 if (base_type == NULL)
1729 return NULL;
1730 if (is_suffix (ada_type_name (base_type), "___XVE"))
1731 return base_type;
d2e4a39e 1732 else
14f9c5c9 1733 {
d2e4a39e 1734 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1735
14f9c5c9 1736 if (alt_type == NULL)
dda83cd7 1737 return base_type;
14f9c5c9 1738 else
dda83cd7 1739 return alt_type;
14f9c5c9
AS
1740 }
1741}
1742
4c4b4cd2
PH
1743/* A pointer to the array data for thin-pointer value VAL. */
1744
d2e4a39e
AS
1745static struct value *
1746thin_data_pntr (struct value *val)
14f9c5c9 1747{
828292f2 1748 struct type *type = ada_check_typedef (value_type (val));
556bdfd4 1749 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1750
556bdfd4
UW
1751 data_type = lookup_pointer_type (data_type);
1752
78134374 1753 if (type->code () == TYPE_CODE_PTR)
556bdfd4 1754 return value_cast (data_type, value_copy (val));
d2e4a39e 1755 else
42ae5230 1756 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1757}
1758
4c4b4cd2
PH
1759/* True iff TYPE indicates a "thick" array pointer type. */
1760
14f9c5c9 1761static int
d2e4a39e 1762is_thick_pntr (struct type *type)
14f9c5c9
AS
1763{
1764 type = desc_base_type (type);
78134374 1765 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1766 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1767}
1768
4c4b4cd2
PH
1769/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1770 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1771
d2e4a39e
AS
1772static struct type *
1773desc_bounds_type (struct type *type)
14f9c5c9 1774{
d2e4a39e 1775 struct type *r;
14f9c5c9
AS
1776
1777 type = desc_base_type (type);
1778
1779 if (type == NULL)
1780 return NULL;
1781 else if (is_thin_pntr (type))
1782 {
1783 type = thin_descriptor_type (type);
1784 if (type == NULL)
dda83cd7 1785 return NULL;
14f9c5c9
AS
1786 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1787 if (r != NULL)
dda83cd7 1788 return ada_check_typedef (r);
14f9c5c9 1789 }
78134374 1790 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1791 {
1792 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1793 if (r != NULL)
dda83cd7 1794 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1795 }
1796 return NULL;
1797}
1798
1799/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1800 one, a pointer to its bounds data. Otherwise NULL. */
1801
d2e4a39e
AS
1802static struct value *
1803desc_bounds (struct value *arr)
14f9c5c9 1804{
df407dfe 1805 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1806
d2e4a39e 1807 if (is_thin_pntr (type))
14f9c5c9 1808 {
d2e4a39e 1809 struct type *bounds_type =
dda83cd7 1810 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1811 LONGEST addr;
1812
4cdfadb1 1813 if (bounds_type == NULL)
dda83cd7 1814 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1815
1816 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1817 since desc_type is an XVE-encoded type (and shouldn't be),
1818 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1819 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1820 addr = value_as_long (arr);
d2e4a39e 1821 else
dda83cd7 1822 addr = value_address (arr);
14f9c5c9 1823
d2e4a39e 1824 return
dda83cd7
SM
1825 value_from_longest (lookup_pointer_type (bounds_type),
1826 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1827 }
1828
1829 else if (is_thick_pntr (type))
05e522ef 1830 {
158cc4fe 1831 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef
JB
1832 _("Bad GNAT array descriptor"));
1833 struct type *p_bounds_type = value_type (p_bounds);
1834
1835 if (p_bounds_type
78134374 1836 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef
JB
1837 {
1838 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1839
e46d3488 1840 if (target_type->is_stub ())
05e522ef
JB
1841 p_bounds = value_cast (lookup_pointer_type
1842 (ada_check_typedef (target_type)),
1843 p_bounds);
1844 }
1845 else
1846 error (_("Bad GNAT array descriptor"));
1847
1848 return p_bounds;
1849 }
14f9c5c9
AS
1850 else
1851 return NULL;
1852}
1853
4c4b4cd2
PH
1854/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1855 position of the field containing the address of the bounds data. */
1856
14f9c5c9 1857static int
d2e4a39e 1858fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9 1859{
b610c045 1860 return desc_base_type (type)->field (1).loc_bitpos ();
14f9c5c9
AS
1861}
1862
1863/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1864 size of the field containing the address of the bounds data. */
1865
14f9c5c9 1866static int
d2e4a39e 1867fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1868{
1869 type = desc_base_type (type);
1870
d2e4a39e 1871 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1872 return TYPE_FIELD_BITSIZE (type, 1);
1873 else
940da03e 1874 return 8 * TYPE_LENGTH (ada_check_typedef (type->field (1).type ()));
14f9c5c9
AS
1875}
1876
4c4b4cd2 1877/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1878 pointer to one, the type of its array data (a array-with-no-bounds type);
1879 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1880 data. */
4c4b4cd2 1881
d2e4a39e 1882static struct type *
556bdfd4 1883desc_data_target_type (struct type *type)
14f9c5c9
AS
1884{
1885 type = desc_base_type (type);
1886
4c4b4cd2 1887 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1888 if (is_thin_pntr (type))
940da03e 1889 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1890 else if (is_thick_pntr (type))
556bdfd4
UW
1891 {
1892 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1893
1894 if (data_type
78134374 1895 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
05e522ef 1896 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1897 }
1898
1899 return NULL;
14f9c5c9
AS
1900}
1901
1902/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1903 its array data. */
4c4b4cd2 1904
d2e4a39e
AS
1905static struct value *
1906desc_data (struct value *arr)
14f9c5c9 1907{
df407dfe 1908 struct type *type = value_type (arr);
5b4ee69b 1909
14f9c5c9
AS
1910 if (is_thin_pntr (type))
1911 return thin_data_pntr (arr);
1912 else if (is_thick_pntr (type))
158cc4fe 1913 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1914 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1915 else
1916 return NULL;
1917}
1918
1919
1920/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1921 position of the field containing the address of the data. */
1922
14f9c5c9 1923static int
d2e4a39e 1924fat_pntr_data_bitpos (struct type *type)
14f9c5c9 1925{
b610c045 1926 return desc_base_type (type)->field (0).loc_bitpos ();
14f9c5c9
AS
1927}
1928
1929/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1930 size of the field containing the address of the data. */
1931
14f9c5c9 1932static int
d2e4a39e 1933fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1934{
1935 type = desc_base_type (type);
1936
1937 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1938 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1939 else
940da03e 1940 return TARGET_CHAR_BIT * TYPE_LENGTH (type->field (0).type ());
14f9c5c9
AS
1941}
1942
4c4b4cd2 1943/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1944 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1945 bound, if WHICH is 1. The first bound is I=1. */
1946
d2e4a39e
AS
1947static struct value *
1948desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1949{
250106a7
TT
1950 char bound_name[20];
1951 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1952 which ? 'U' : 'L', i - 1);
158cc4fe 1953 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1954 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1955}
1956
1957/* If BOUNDS is an array-bounds structure type, return the bit position
1958 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1959 bound, if WHICH is 1. The first bound is I=1. */
1960
14f9c5c9 1961static int
d2e4a39e 1962desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1963{
b610c045 1964 return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
14f9c5c9
AS
1965}
1966
1967/* If BOUNDS is an array-bounds structure type, return the bit field size
1968 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1969 bound, if WHICH is 1. The first bound is I=1. */
1970
76a01679 1971static int
d2e4a39e 1972desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1973{
1974 type = desc_base_type (type);
1975
d2e4a39e
AS
1976 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1977 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1978 else
940da03e 1979 return 8 * TYPE_LENGTH (type->field (2 * i + which - 2).type ());
14f9c5c9
AS
1980}
1981
1982/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1983 Ith bound (numbering from 1). Otherwise, NULL. */
1984
d2e4a39e
AS
1985static struct type *
1986desc_index_type (struct type *type, int i)
14f9c5c9
AS
1987{
1988 type = desc_base_type (type);
1989
78134374 1990 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
1991 {
1992 char bound_name[20];
1993 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
1994 return lookup_struct_elt_type (type, bound_name, 1);
1995 }
d2e4a39e 1996 else
14f9c5c9
AS
1997 return NULL;
1998}
1999
4c4b4cd2
PH
2000/* The number of index positions in the array-bounds type TYPE.
2001 Return 0 if TYPE is NULL. */
2002
14f9c5c9 2003static int
d2e4a39e 2004desc_arity (struct type *type)
14f9c5c9
AS
2005{
2006 type = desc_base_type (type);
2007
2008 if (type != NULL)
1f704f76 2009 return type->num_fields () / 2;
14f9c5c9
AS
2010 return 0;
2011}
2012
4c4b4cd2
PH
2013/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2014 an array descriptor type (representing an unconstrained array
2015 type). */
2016
76a01679
JB
2017static int
2018ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
2019{
2020 if (type == NULL)
2021 return 0;
61ee279c 2022 type = ada_check_typedef (type);
78134374 2023 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 2024 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
2025}
2026
52ce6436 2027/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 2028 * to one. */
52ce6436 2029
2c0b251b 2030static int
52ce6436
PH
2031ada_is_array_type (struct type *type)
2032{
78134374
SM
2033 while (type != NULL
2034 && (type->code () == TYPE_CODE_PTR
2035 || type->code () == TYPE_CODE_REF))
52ce6436
PH
2036 type = TYPE_TARGET_TYPE (type);
2037 return ada_is_direct_array_type (type);
2038}
2039
4c4b4cd2 2040/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 2041
14f9c5c9 2042int
4c4b4cd2 2043ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
2044{
2045 if (type == NULL)
2046 return 0;
61ee279c 2047 type = ada_check_typedef (type);
78134374
SM
2048 return (type->code () == TYPE_CODE_ARRAY
2049 || (type->code () == TYPE_CODE_PTR
2050 && (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ()
2051 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
2052}
2053
4c4b4cd2
PH
2054/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2055
14f9c5c9 2056int
4c4b4cd2 2057ada_is_array_descriptor_type (struct type *type)
14f9c5c9 2058{
556bdfd4 2059 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
2060
2061 if (type == NULL)
2062 return 0;
61ee279c 2063 type = ada_check_typedef (type);
556bdfd4 2064 return (data_type != NULL
78134374 2065 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 2066 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
2067}
2068
2069/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 2070 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 2071 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
2072 is still needed. */
2073
14f9c5c9 2074int
ebf56fd3 2075ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 2076{
d2e4a39e 2077 return
14f9c5c9 2078 type != NULL
78134374 2079 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 2080 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 2081 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 2082 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
2083}
2084
2085
4c4b4cd2 2086/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 2087 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 2088 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 2089 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
2090 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2091 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 2092 a descriptor. */
de93309a
SM
2093
2094static struct type *
d2e4a39e 2095ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 2096{
ad82864c
JB
2097 if (ada_is_constrained_packed_array_type (value_type (arr)))
2098 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 2099
df407dfe
AC
2100 if (!ada_is_array_descriptor_type (value_type (arr)))
2101 return value_type (arr);
d2e4a39e
AS
2102
2103 if (!bounds)
ad82864c
JB
2104 {
2105 struct type *array_type =
2106 ada_check_typedef (desc_data_target_type (value_type (arr)));
2107
2108 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
2109 TYPE_FIELD_BITSIZE (array_type, 0) =
2110 decode_packed_array_bitsize (value_type (arr));
2111
2112 return array_type;
2113 }
14f9c5c9
AS
2114 else
2115 {
d2e4a39e 2116 struct type *elt_type;
14f9c5c9 2117 int arity;
d2e4a39e 2118 struct value *descriptor;
14f9c5c9 2119
df407dfe
AC
2120 elt_type = ada_array_element_type (value_type (arr), -1);
2121 arity = ada_array_arity (value_type (arr));
14f9c5c9 2122
d2e4a39e 2123 if (elt_type == NULL || arity == 0)
dda83cd7 2124 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
2125
2126 descriptor = desc_bounds (arr);
d2e4a39e 2127 if (value_as_long (descriptor) == 0)
dda83cd7 2128 return NULL;
d2e4a39e 2129 while (arity > 0)
dda83cd7
SM
2130 {
2131 struct type *range_type = alloc_type_copy (value_type (arr));
2132 struct type *array_type = alloc_type_copy (value_type (arr));
2133 struct value *low = desc_one_bound (descriptor, arity, 0);
2134 struct value *high = desc_one_bound (descriptor, arity, 1);
2135
2136 arity -= 1;
2137 create_static_range_type (range_type, value_type (low),
0c9c3474
SA
2138 longest_to_int (value_as_long (low)),
2139 longest_to_int (value_as_long (high)));
dda83cd7 2140 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
2141
2142 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
e67ad678
JB
2143 {
2144 /* We need to store the element packed bitsize, as well as
dda83cd7 2145 recompute the array size, because it was previously
e67ad678
JB
2146 computed based on the unpacked element size. */
2147 LONGEST lo = value_as_long (low);
2148 LONGEST hi = value_as_long (high);
2149
2150 TYPE_FIELD_BITSIZE (elt_type, 0) =
2151 decode_packed_array_bitsize (value_type (arr));
2152 /* If the array has no element, then the size is already
dda83cd7 2153 zero, and does not need to be recomputed. */
e67ad678
JB
2154 if (lo < hi)
2155 {
2156 int array_bitsize =
dda83cd7 2157 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678
JB
2158
2159 TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
2160 }
2161 }
dda83cd7 2162 }
14f9c5c9
AS
2163
2164 return lookup_pointer_type (elt_type);
2165 }
2166}
2167
2168/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
2169 Otherwise, returns either a standard GDB array with bounds set
2170 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2171 GDB array. Returns NULL if ARR is a null fat pointer. */
2172
d2e4a39e
AS
2173struct value *
2174ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 2175{
df407dfe 2176 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2177 {
d2e4a39e 2178 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 2179
14f9c5c9 2180 if (arrType == NULL)
dda83cd7 2181 return NULL;
14f9c5c9
AS
2182 return value_cast (arrType, value_copy (desc_data (arr)));
2183 }
ad82864c
JB
2184 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2185 return decode_constrained_packed_array (arr);
14f9c5c9
AS
2186 else
2187 return arr;
2188}
2189
2190/* If ARR does not represent an array, returns ARR unchanged.
2191 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
2192 be ARR itself if it already is in the proper form). */
2193
720d1a40 2194struct value *
d2e4a39e 2195ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 2196{
df407dfe 2197 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 2198 {
d2e4a39e 2199 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 2200
14f9c5c9 2201 if (arrVal == NULL)
dda83cd7 2202 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
2203 return value_ind (arrVal);
2204 }
ad82864c
JB
2205 else if (ada_is_constrained_packed_array_type (value_type (arr)))
2206 return decode_constrained_packed_array (arr);
d2e4a39e 2207 else
14f9c5c9
AS
2208 return arr;
2209}
2210
2211/* If TYPE represents a GNAT array type, return it translated to an
2212 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
2213 packing). For other types, is the identity. */
2214
d2e4a39e
AS
2215struct type *
2216ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 2217{
ad82864c
JB
2218 if (ada_is_constrained_packed_array_type (type))
2219 return decode_constrained_packed_array_type (type);
17280b9f
UW
2220
2221 if (ada_is_array_descriptor_type (type))
556bdfd4 2222 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
2223
2224 return type;
14f9c5c9
AS
2225}
2226
4c4b4cd2
PH
2227/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2228
ad82864c 2229static int
57567375 2230ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
2231{
2232 if (type == NULL)
2233 return 0;
4c4b4cd2 2234 type = desc_base_type (type);
61ee279c 2235 type = ada_check_typedef (type);
d2e4a39e 2236 return
14f9c5c9
AS
2237 ada_type_name (type) != NULL
2238 && strstr (ada_type_name (type), "___XP") != NULL;
2239}
2240
ad82864c
JB
2241/* Non-zero iff TYPE represents a standard GNAT constrained
2242 packed-array type. */
2243
2244int
2245ada_is_constrained_packed_array_type (struct type *type)
2246{
57567375 2247 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
2248 && !ada_is_array_descriptor_type (type);
2249}
2250
2251/* Non-zero iff TYPE represents an array descriptor for a
2252 unconstrained packed-array type. */
2253
2254static int
2255ada_is_unconstrained_packed_array_type (struct type *type)
2256{
57567375
TT
2257 if (!ada_is_array_descriptor_type (type))
2258 return 0;
2259
2260 if (ada_is_gnat_encoded_packed_array_type (type))
2261 return 1;
2262
2263 /* If we saw GNAT encodings, then the above code is sufficient.
2264 However, with minimal encodings, we will just have a thick
2265 pointer instead. */
2266 if (is_thick_pntr (type))
2267 {
2268 type = desc_base_type (type);
2269 /* The structure's first field is a pointer to an array, so this
2270 fetches the array type. */
2271 type = TYPE_TARGET_TYPE (type->field (0).type ());
af5300fe
TV
2272 if (type->code () == TYPE_CODE_TYPEDEF)
2273 type = ada_typedef_target_type (type);
57567375
TT
2274 /* Now we can see if the array elements are packed. */
2275 return TYPE_FIELD_BITSIZE (type, 0) > 0;
2276 }
2277
2278 return 0;
ad82864c
JB
2279}
2280
c9a28cbe
TT
2281/* Return true if TYPE is a (Gnat-encoded) constrained packed array
2282 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2283
2284static bool
2285ada_is_any_packed_array_type (struct type *type)
2286{
2287 return (ada_is_constrained_packed_array_type (type)
2288 || (type->code () == TYPE_CODE_ARRAY
2289 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
2290}
2291
ad82864c
JB
2292/* Given that TYPE encodes a packed array type (constrained or unconstrained),
2293 return the size of its elements in bits. */
2294
2295static long
2296decode_packed_array_bitsize (struct type *type)
2297{
0d5cff50
DE
2298 const char *raw_name;
2299 const char *tail;
ad82864c
JB
2300 long bits;
2301
720d1a40
JB
2302 /* Access to arrays implemented as fat pointers are encoded as a typedef
2303 of the fat pointer type. We need the name of the fat pointer type
2304 to do the decoding, so strip the typedef layer. */
78134374 2305 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
2306 type = ada_typedef_target_type (type);
2307
2308 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2309 if (!raw_name)
2310 raw_name = ada_type_name (desc_base_type (type));
2311
2312 if (!raw_name)
2313 return 0;
2314
2315 tail = strstr (raw_name, "___XP");
57567375
TT
2316 if (tail == nullptr)
2317 {
2318 gdb_assert (is_thick_pntr (type));
2319 /* The structure's first field is a pointer to an array, so this
2320 fetches the array type. */
2321 type = TYPE_TARGET_TYPE (type->field (0).type ());
2322 /* Now we can see if the array elements are packed. */
2323 return TYPE_FIELD_BITSIZE (type, 0);
2324 }
ad82864c
JB
2325
2326 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2327 {
2328 lim_warning
2329 (_("could not understand bit size information on packed array"));
2330 return 0;
2331 }
2332
2333 return bits;
2334}
2335
14f9c5c9
AS
2336/* Given that TYPE is a standard GDB array type with all bounds filled
2337 in, and that the element size of its ultimate scalar constituents
2338 (that is, either its elements, or, if it is an array of arrays, its
2339 elements' elements, etc.) is *ELT_BITS, return an identical type,
2340 but with the bit sizes of its elements (and those of any
2341 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2342 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2343 in bits.
2344
2345 Note that, for arrays whose index type has an XA encoding where
2346 a bound references a record discriminant, getting that discriminant,
2347 and therefore the actual value of that bound, is not possible
2348 because none of the given parameters gives us access to the record.
2349 This function assumes that it is OK in the context where it is being
2350 used to return an array whose bounds are still dynamic and where
2351 the length is arbitrary. */
4c4b4cd2 2352
d2e4a39e 2353static struct type *
ad82864c 2354constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2355{
d2e4a39e
AS
2356 struct type *new_elt_type;
2357 struct type *new_type;
99b1c762
JB
2358 struct type *index_type_desc;
2359 struct type *index_type;
14f9c5c9
AS
2360 LONGEST low_bound, high_bound;
2361
61ee279c 2362 type = ada_check_typedef (type);
78134374 2363 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2364 return type;
2365
99b1c762
JB
2366 index_type_desc = ada_find_parallel_type (type, "___XA");
2367 if (index_type_desc)
940da03e 2368 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2369 NULL);
2370 else
3d967001 2371 index_type = type->index_type ();
99b1c762 2372
e9bb382b 2373 new_type = alloc_type_copy (type);
ad82864c
JB
2374 new_elt_type =
2375 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
2376 elt_bits);
99b1c762 2377 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9 2378 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2379 new_type->set_name (ada_type_name (type));
14f9c5c9 2380
78134374 2381 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2382 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2383 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2384 low_bound = high_bound = 0;
2385 if (high_bound < low_bound)
2386 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 2387 else
14f9c5c9
AS
2388 {
2389 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 2390 TYPE_LENGTH (new_type) =
dda83cd7 2391 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
2392 }
2393
9cdd0d12 2394 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2395 return new_type;
2396}
2397
ad82864c
JB
2398/* The array type encoded by TYPE, where
2399 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2400
d2e4a39e 2401static struct type *
ad82864c 2402decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2403{
0d5cff50 2404 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2405 char *name;
0d5cff50 2406 const char *tail;
d2e4a39e 2407 struct type *shadow_type;
14f9c5c9 2408 long bits;
14f9c5c9 2409
727e3d2e
JB
2410 if (!raw_name)
2411 raw_name = ada_type_name (desc_base_type (type));
2412
2413 if (!raw_name)
2414 return NULL;
2415
2416 name = (char *) alloca (strlen (raw_name) + 1);
2417 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2418 type = desc_base_type (type);
2419
14f9c5c9
AS
2420 memcpy (name, raw_name, tail - raw_name);
2421 name[tail - raw_name] = '\000';
2422
b4ba55a1
JB
2423 shadow_type = ada_find_parallel_type_with_name (type, name);
2424
2425 if (shadow_type == NULL)
14f9c5c9 2426 {
323e0a4a 2427 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2428 return NULL;
2429 }
f168693b 2430 shadow_type = check_typedef (shadow_type);
14f9c5c9 2431
78134374 2432 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2433 {
0963b4bd
MS
2434 lim_warning (_("could not understand bounds "
2435 "information on packed array"));
14f9c5c9
AS
2436 return NULL;
2437 }
d2e4a39e 2438
ad82864c
JB
2439 bits = decode_packed_array_bitsize (type);
2440 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2441}
2442
a7400e44
TT
2443/* Helper function for decode_constrained_packed_array. Set the field
2444 bitsize on a series of packed arrays. Returns the number of
2445 elements in TYPE. */
2446
2447static LONGEST
2448recursively_update_array_bitsize (struct type *type)
2449{
2450 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2451
2452 LONGEST low, high;
1f8d2881 2453 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2454 || low > high)
2455 return 0;
2456 LONGEST our_len = high - low + 1;
2457
2458 struct type *elt_type = TYPE_TARGET_TYPE (type);
2459 if (elt_type->code () == TYPE_CODE_ARRAY)
2460 {
2461 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2462 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2463 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2464
2465 TYPE_LENGTH (type) = ((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2466 / HOST_CHAR_BIT);
2467 }
2468
2469 return our_len;
2470}
2471
ad82864c
JB
2472/* Given that ARR is a struct value *indicating a GNAT constrained packed
2473 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2474 standard GDB array type except that the BITSIZEs of the array
2475 target types are set to the number of bits in each element, and the
4c4b4cd2 2476 type length is set appropriately. */
14f9c5c9 2477
d2e4a39e 2478static struct value *
ad82864c 2479decode_constrained_packed_array (struct value *arr)
14f9c5c9 2480{
4c4b4cd2 2481 struct type *type;
14f9c5c9 2482
11aa919a
PMR
2483 /* If our value is a pointer, then dereference it. Likewise if
2484 the value is a reference. Make sure that this operation does not
2485 cause the target type to be fixed, as this would indirectly cause
2486 this array to be decoded. The rest of the routine assumes that
2487 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2488 and "value_ind" routines to perform the dereferencing, as opposed
2489 to using "ada_coerce_ref" or "ada_value_ind". */
2490 arr = coerce_ref (arr);
78134374 2491 if (ada_check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
284614f0 2492 arr = value_ind (arr);
4c4b4cd2 2493
ad82864c 2494 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2495 if (type == NULL)
2496 {
323e0a4a 2497 error (_("can't unpack array"));
14f9c5c9
AS
2498 return NULL;
2499 }
61ee279c 2500
a7400e44
TT
2501 /* Decoding the packed array type could not correctly set the field
2502 bitsizes for any dimension except the innermost, because the
2503 bounds may be variable and were not passed to that function. So,
2504 we further resolve the array bounds here and then update the
2505 sizes. */
50888e42 2506 const gdb_byte *valaddr = value_contents_for_printing (arr).data ();
a7400e44
TT
2507 CORE_ADDR address = value_address (arr);
2508 gdb::array_view<const gdb_byte> view
2509 = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
2510 type = resolve_dynamic_type (type, view, address);
2511 recursively_update_array_bitsize (type);
2512
d5a22e77 2513 if (type_byte_order (value_type (arr)) == BFD_ENDIAN_BIG
32c9a795 2514 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2515 {
2516 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2517 array with no wrapper. In order to interpret the value through
2518 the (left-justified) packed array type we just built, we must
2519 first left-justify it. */
61ee279c
PH
2520 int bit_size, bit_pos;
2521 ULONGEST mod;
2522
df407dfe 2523 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2524 bit_size = 0;
2525 while (mod > 0)
2526 {
2527 bit_size += 1;
2528 mod >>= 1;
2529 }
df407dfe 2530 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2531 arr = ada_value_primitive_packed_val (arr, NULL,
2532 bit_pos / HOST_CHAR_BIT,
2533 bit_pos % HOST_CHAR_BIT,
2534 bit_size,
2535 type);
2536 }
2537
4c4b4cd2 2538 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2539}
2540
2541
2542/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2543 given in IND. ARR must be a simple array. */
14f9c5c9 2544
d2e4a39e
AS
2545static struct value *
2546value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2547{
2548 int i;
2549 int bits, elt_off, bit_off;
2550 long elt_total_bit_offset;
d2e4a39e
AS
2551 struct type *elt_type;
2552 struct value *v;
14f9c5c9
AS
2553
2554 bits = 0;
2555 elt_total_bit_offset = 0;
df407dfe 2556 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2557 for (i = 0; i < arity; i += 1)
14f9c5c9 2558 {
78134374 2559 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2560 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2561 error
2562 (_("attempt to do packed indexing of "
0963b4bd 2563 "something other than a packed array"));
14f9c5c9 2564 else
dda83cd7
SM
2565 {
2566 struct type *range_type = elt_type->index_type ();
2567 LONGEST lowerbound, upperbound;
2568 LONGEST idx;
2569
1f8d2881 2570 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2571 {
2572 lim_warning (_("don't know bounds of array"));
2573 lowerbound = upperbound = 0;
2574 }
2575
2576 idx = pos_atr (ind[i]);
2577 if (idx < lowerbound || idx > upperbound)
2578 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2579 (long) idx);
dda83cd7
SM
2580 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2581 elt_total_bit_offset += (idx - lowerbound) * bits;
2582 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2583 }
14f9c5c9
AS
2584 }
2585 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2586 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2587
2588 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2589 bits, elt_type);
14f9c5c9
AS
2590 return v;
2591}
2592
4c4b4cd2 2593/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2594
2595static int
d2e4a39e 2596has_negatives (struct type *type)
14f9c5c9 2597{
78134374 2598 switch (type->code ())
d2e4a39e
AS
2599 {
2600 default:
2601 return 0;
2602 case TYPE_CODE_INT:
c6d940a9 2603 return !type->is_unsigned ();
d2e4a39e 2604 case TYPE_CODE_RANGE:
5537ddd0 2605 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2606 }
14f9c5c9 2607}
d2e4a39e 2608
f93fca70 2609/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2610 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2611 the unpacked buffer.
14f9c5c9 2612
5b639dea
JB
2613 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2614 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2615
f93fca70
JB
2616 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2617 zero otherwise.
14f9c5c9 2618
f93fca70 2619 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2620
f93fca70
JB
2621 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2622
2623static void
2624ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2625 gdb_byte *unpacked, int unpacked_len,
2626 int is_big_endian, int is_signed_type,
2627 int is_scalar)
2628{
a1c95e6b
JB
2629 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2630 int src_idx; /* Index into the source area */
2631 int src_bytes_left; /* Number of source bytes left to process. */
2632 int srcBitsLeft; /* Number of source bits left to move */
2633 int unusedLS; /* Number of bits in next significant
dda83cd7 2634 byte of source that are unused */
a1c95e6b 2635
a1c95e6b
JB
2636 int unpacked_idx; /* Index into the unpacked buffer */
2637 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2638
4c4b4cd2 2639 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2640 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2641 unsigned char sign;
a1c95e6b 2642
4c4b4cd2
PH
2643 /* Transmit bytes from least to most significant; delta is the direction
2644 the indices move. */
f93fca70 2645 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2646
5b639dea
JB
2647 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2648 bits from SRC. .*/
2649 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2650 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2651 bit_size, unpacked_len);
2652
14f9c5c9 2653 srcBitsLeft = bit_size;
086ca51f 2654 src_bytes_left = src_len;
f93fca70 2655 unpacked_bytes_left = unpacked_len;
14f9c5c9 2656 sign = 0;
f93fca70
JB
2657
2658 if (is_big_endian)
14f9c5c9 2659 {
086ca51f 2660 src_idx = src_len - 1;
f93fca70
JB
2661 if (is_signed_type
2662 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2663 sign = ~0;
d2e4a39e
AS
2664
2665 unusedLS =
dda83cd7
SM
2666 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2667 % HOST_CHAR_BIT;
14f9c5c9 2668
f93fca70
JB
2669 if (is_scalar)
2670 {
dda83cd7
SM
2671 accumSize = 0;
2672 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2673 }
2674 else
2675 {
dda83cd7
SM
2676 /* Non-scalar values must be aligned at a byte boundary... */
2677 accumSize =
2678 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2679 /* ... And are placed at the beginning (most-significant) bytes
2680 of the target. */
2681 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2682 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2683 }
14f9c5c9 2684 }
d2e4a39e 2685 else
14f9c5c9
AS
2686 {
2687 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2688
086ca51f 2689 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2690 unusedLS = bit_offset;
2691 accumSize = 0;
2692
f93fca70 2693 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2694 sign = ~0;
14f9c5c9 2695 }
d2e4a39e 2696
14f9c5c9 2697 accum = 0;
086ca51f 2698 while (src_bytes_left > 0)
14f9c5c9
AS
2699 {
2700 /* Mask for removing bits of the next source byte that are not
dda83cd7 2701 part of the value. */
d2e4a39e 2702 unsigned int unusedMSMask =
dda83cd7
SM
2703 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2704 1;
4c4b4cd2 2705 /* Sign-extend bits for this byte. */
14f9c5c9 2706 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2707
d2e4a39e 2708 accum |=
dda83cd7 2709 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2710 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2711 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2712 {
2713 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2714 accumSize -= HOST_CHAR_BIT;
2715 accum >>= HOST_CHAR_BIT;
2716 unpacked_bytes_left -= 1;
2717 unpacked_idx += delta;
2718 }
14f9c5c9
AS
2719 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2720 unusedLS = 0;
086ca51f
JB
2721 src_bytes_left -= 1;
2722 src_idx += delta;
14f9c5c9 2723 }
086ca51f 2724 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2725 {
2726 accum |= sign << accumSize;
db297a65 2727 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2728 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2729 if (accumSize < 0)
2730 accumSize = 0;
14f9c5c9 2731 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2732 unpacked_bytes_left -= 1;
2733 unpacked_idx += delta;
14f9c5c9 2734 }
f93fca70
JB
2735}
2736
2737/* Create a new value of type TYPE from the contents of OBJ starting
2738 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2739 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2740 assigning through the result will set the field fetched from.
2741 VALADDR is ignored unless OBJ is NULL, in which case,
2742 VALADDR+OFFSET must address the start of storage containing the
2743 packed value. The value returned in this case is never an lval.
2744 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2745
2746struct value *
2747ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2748 long offset, int bit_offset, int bit_size,
dda83cd7 2749 struct type *type)
f93fca70
JB
2750{
2751 struct value *v;
bfb1c796 2752 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2753 gdb_byte *unpacked;
220475ed 2754 const int is_scalar = is_scalar_type (type);
d5a22e77 2755 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2756 gdb::byte_vector staging;
f93fca70
JB
2757
2758 type = ada_check_typedef (type);
2759
d0a9e810 2760 if (obj == NULL)
bfb1c796 2761 src = valaddr + offset;
d0a9e810 2762 else
50888e42 2763 src = value_contents (obj).data () + offset;
d0a9e810
JB
2764
2765 if (is_dynamic_type (type))
2766 {
2767 /* The length of TYPE might by dynamic, so we need to resolve
2768 TYPE in order to know its actual size, which we then use
2769 to create the contents buffer of the value we return.
2770 The difficulty is that the data containing our object is
2771 packed, and therefore maybe not at a byte boundary. So, what
2772 we do, is unpack the data into a byte-aligned buffer, and then
2773 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2774 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2775 staging.resize (staging_len);
d0a9e810
JB
2776
2777 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2778 staging.data (), staging.size (),
d0a9e810
JB
2779 is_big_endian, has_negatives (type),
2780 is_scalar);
b249d2c2 2781 type = resolve_dynamic_type (type, staging, 0);
0cafa88c
JB
2782 if (TYPE_LENGTH (type) < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
2783 {
2784 /* This happens when the length of the object is dynamic,
2785 and is actually smaller than the space reserved for it.
2786 For instance, in an array of variant records, the bit_size
2787 we're given is the array stride, which is constant and
2788 normally equal to the maximum size of its element.
2789 But, in reality, each element only actually spans a portion
2790 of that stride. */
2791 bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
2792 }
d0a9e810
JB
2793 }
2794
f93fca70
JB
2795 if (obj == NULL)
2796 {
2797 v = allocate_value (type);
bfb1c796 2798 src = valaddr + offset;
f93fca70
JB
2799 }
2800 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2801 {
0cafa88c 2802 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2803 gdb_byte *buf;
0cafa88c 2804
f93fca70 2805 v = value_at (type, value_address (obj) + offset);
bfb1c796
PA
2806 buf = (gdb_byte *) alloca (src_len);
2807 read_memory (value_address (v), buf, src_len);
2808 src = buf;
f93fca70
JB
2809 }
2810 else
2811 {
2812 v = allocate_value (type);
50888e42 2813 src = value_contents (obj).data () + offset;
f93fca70
JB
2814 }
2815
2816 if (obj != NULL)
2817 {
2818 long new_offset = offset;
2819
2820 set_value_component_location (v, obj);
2821 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2822 set_value_bitsize (v, bit_size);
2823 if (value_bitpos (v) >= HOST_CHAR_BIT)
dda83cd7 2824 {
f93fca70 2825 ++new_offset;
dda83cd7
SM
2826 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2827 }
f93fca70
JB
2828 set_value_offset (v, new_offset);
2829
2830 /* Also set the parent value. This is needed when trying to
2831 assign a new value (in inferior memory). */
2832 set_value_parent (v, obj);
2833 }
2834 else
2835 set_value_bitsize (v, bit_size);
50888e42 2836 unpacked = value_contents_writeable (v).data ();
f93fca70
JB
2837
2838 if (bit_size == 0)
2839 {
2840 memset (unpacked, 0, TYPE_LENGTH (type));
2841 return v;
2842 }
2843
d5722aa2 2844 if (staging.size () == TYPE_LENGTH (type))
f93fca70 2845 {
d0a9e810
JB
2846 /* Small short-cut: If we've unpacked the data into a buffer
2847 of the same size as TYPE's length, then we can reuse that,
2848 instead of doing the unpacking again. */
d5722aa2 2849 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2850 }
d0a9e810
JB
2851 else
2852 ada_unpack_from_contents (src, bit_offset, bit_size,
2853 unpacked, TYPE_LENGTH (type),
2854 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2855
14f9c5c9
AS
2856 return v;
2857}
d2e4a39e 2858
14f9c5c9
AS
2859/* Store the contents of FROMVAL into the location of TOVAL.
2860 Return a new value with the location of TOVAL and contents of
2861 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2862 floating-point or non-scalar types. */
14f9c5c9 2863
d2e4a39e
AS
2864static struct value *
2865ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2866{
df407dfe
AC
2867 struct type *type = value_type (toval);
2868 int bits = value_bitsize (toval);
14f9c5c9 2869
52ce6436
PH
2870 toval = ada_coerce_ref (toval);
2871 fromval = ada_coerce_ref (fromval);
2872
2873 if (ada_is_direct_array_type (value_type (toval)))
2874 toval = ada_coerce_to_simple_array (toval);
2875 if (ada_is_direct_array_type (value_type (fromval)))
2876 fromval = ada_coerce_to_simple_array (fromval);
2877
88e3b34b 2878 if (!deprecated_value_modifiable (toval))
323e0a4a 2879 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2880
d2e4a39e 2881 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2882 && bits > 0
78134374 2883 && (type->code () == TYPE_CODE_FLT
dda83cd7 2884 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2885 {
df407dfe
AC
2886 int len = (value_bitpos (toval)
2887 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2888 int from_size;
224c3ddb 2889 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2890 struct value *val;
42ae5230 2891 CORE_ADDR to_addr = value_address (toval);
14f9c5c9 2892
78134374 2893 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2894 fromval = value_cast (type, fromval);
14f9c5c9 2895
52ce6436 2896 read_memory (to_addr, buffer, len);
aced2898
PH
2897 from_size = value_bitsize (fromval);
2898 if (from_size == 0)
2899 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
d48e62f4 2900
d5a22e77 2901 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4
TT
2902 ULONGEST from_offset = 0;
2903 if (is_big_endian && is_scalar_type (value_type (fromval)))
2904 from_offset = from_size - bits;
2905 copy_bitwise (buffer, value_bitpos (toval),
50888e42 2906 value_contents (fromval).data (), from_offset,
d48e62f4 2907 bits, is_big_endian);
972daa01 2908 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2909
14f9c5c9 2910 val = value_copy (toval);
fb2a515f
SM
2911 memcpy (value_contents_raw (val).data (),
2912 value_contents (fromval).data (),
2913 TYPE_LENGTH (type));
04624583 2914 deprecated_set_value_type (val, type);
d2e4a39e 2915
14f9c5c9
AS
2916 return val;
2917 }
2918
2919 return value_assign (toval, fromval);
2920}
2921
2922
7c512744
JB
2923/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2924 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2925 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2926 COMPONENT, and not the inferior's memory. The current contents
2927 of COMPONENT are ignored.
2928
2929 Although not part of the initial design, this function also works
2930 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2931 had a null address, and COMPONENT had an address which is equal to
2932 its offset inside CONTAINER. */
2933
52ce6436
PH
2934static void
2935value_assign_to_component (struct value *container, struct value *component,
2936 struct value *val)
2937{
2938 LONGEST offset_in_container =
42ae5230 2939 (LONGEST) (value_address (component) - value_address (container));
7c512744 2940 int bit_offset_in_container =
52ce6436
PH
2941 value_bitpos (component) - value_bitpos (container);
2942 int bits;
7c512744 2943
52ce6436
PH
2944 val = value_cast (value_type (component), val);
2945
2946 if (value_bitsize (component) == 0)
2947 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2948 else
2949 bits = value_bitsize (component);
2950
d5a22e77 2951 if (type_byte_order (value_type (container)) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2952 {
2953 int src_offset;
2954
2955 if (is_scalar_type (check_typedef (value_type (component))))
dda83cd7 2956 src_offset
2a62dfa9
JB
2957 = TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits;
2958 else
2959 src_offset = 0;
50888e42
SM
2960 copy_bitwise ((value_contents_writeable (container).data ()
2961 + offset_in_container),
a99bc3d2 2962 value_bitpos (container) + bit_offset_in_container,
50888e42 2963 value_contents (val).data (), src_offset, bits, 1);
2a62dfa9 2964 }
52ce6436 2965 else
50888e42
SM
2966 copy_bitwise ((value_contents_writeable (container).data ()
2967 + offset_in_container),
a99bc3d2 2968 value_bitpos (container) + bit_offset_in_container,
50888e42 2969 value_contents (val).data (), 0, bits, 0);
7c512744
JB
2970}
2971
736ade86
XR
2972/* Determine if TYPE is an access to an unconstrained array. */
2973
d91e9ea8 2974bool
736ade86
XR
2975ada_is_access_to_unconstrained_array (struct type *type)
2976{
78134374 2977 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2978 && is_thick_pntr (ada_typedef_target_type (type)));
2979}
2980
4c4b4cd2
PH
2981/* The value of the element of array ARR at the ARITY indices given in IND.
2982 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2983 thereto. */
2984
d2e4a39e
AS
2985struct value *
2986ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2987{
2988 int k;
d2e4a39e
AS
2989 struct value *elt;
2990 struct type *elt_type;
14f9c5c9
AS
2991
2992 elt = ada_coerce_to_simple_array (arr);
2993
df407dfe 2994 elt_type = ada_check_typedef (value_type (elt));
78134374 2995 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
2996 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2997 return value_subscript_packed (elt, arity, ind);
2998
2999 for (k = 0; k < arity; k += 1)
3000 {
b9c50e9a
XR
3001 struct type *saved_elt_type = TYPE_TARGET_TYPE (elt_type);
3002
78134374 3003 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 3004 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 3005
2497b498 3006 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
3007
3008 if (ada_is_access_to_unconstrained_array (saved_elt_type)
78134374 3009 && value_type (elt)->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
3010 {
3011 /* The element is a typedef to an unconstrained array,
3012 except that the value_subscript call stripped the
3013 typedef layer. The typedef layer is GNAT's way to
3014 specify that the element is, at the source level, an
3015 access to the unconstrained array, rather than the
3016 unconstrained array. So, we need to restore that
3017 typedef layer, which we can do by forcing the element's
3018 type back to its original type. Otherwise, the returned
3019 value is going to be printed as the array, rather
3020 than as an access. Another symptom of the same issue
3021 would be that an expression trying to dereference the
3022 element would also be improperly rejected. */
3023 deprecated_set_value_type (elt, saved_elt_type);
3024 }
3025
3026 elt_type = ada_check_typedef (value_type (elt));
14f9c5c9 3027 }
b9c50e9a 3028
14f9c5c9
AS
3029 return elt;
3030}
3031
deede10c
JB
3032/* Assuming ARR is a pointer to a GDB array, the value of the element
3033 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
3034 Does not read the entire array into memory.
3035
3036 Note: Unlike what one would expect, this function is used instead of
3037 ada_value_subscript for basically all non-packed array types. The reason
3038 for this is that a side effect of doing our own pointer arithmetics instead
3039 of relying on value_subscript is that there is no implicit typedef peeling.
3040 This is important for arrays of array accesses, where it allows us to
3041 preserve the fact that the array's element is an array access, where the
3042 access part os encoded in a typedef layer. */
14f9c5c9 3043
2c0b251b 3044static struct value *
deede10c 3045ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
3046{
3047 int k;
919e6dbe 3048 struct value *array_ind = ada_value_ind (arr);
deede10c 3049 struct type *type
919e6dbe
PMR
3050 = check_typedef (value_enclosing_type (array_ind));
3051
78134374 3052 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
3053 && TYPE_FIELD_BITSIZE (type, 0) > 0)
3054 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
3055
3056 for (k = 0; k < arity; k += 1)
3057 {
3058 LONGEST lwb, upb;
14f9c5c9 3059
78134374 3060 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 3061 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 3062 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
dda83cd7 3063 value_copy (arr));
3d967001 3064 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 3065 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
14f9c5c9
AS
3066 type = TYPE_TARGET_TYPE (type);
3067 }
3068
3069 return value_ind (arr);
3070}
3071
0b5d8877 3072/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
3073 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3074 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3075 this array is LOW, as per Ada rules. */
0b5d8877 3076static struct value *
f5938064 3077ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 3078 int low, int high)
0b5d8877 3079{
b0dd7688 3080 struct type *type0 = ada_check_typedef (type);
3d967001 3081 struct type *base_index_type = TYPE_TARGET_TYPE (type0->index_type ());
0c9c3474 3082 struct type *index_type
aa715135 3083 = create_static_range_type (NULL, base_index_type, low, high);
9fe561ab
JB
3084 struct type *slice_type = create_array_type_with_stride
3085 (NULL, TYPE_TARGET_TYPE (type0), index_type,
24e99c6c 3086 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3087 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 3088 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 3089 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
3090 CORE_ADDR base;
3091
6244c119
SM
3092 low_pos = discrete_position (base_index_type, low);
3093 base_low_pos = discrete_position (base_index_type, base_low);
3094
3095 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
3096 {
3097 warning (_("unable to get positions in slice, use bounds instead"));
3098 low_pos = low;
3099 base_low_pos = base_low;
3100 }
5b4ee69b 3101
7ff5b937
TT
3102 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
3103 if (stride == 0)
3104 stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
3105
6244c119 3106 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 3107 return value_at_lazy (slice_type, base);
0b5d8877
PH
3108}
3109
3110
3111static struct value *
3112ada_value_slice (struct value *array, int low, int high)
3113{
b0dd7688 3114 struct type *type = ada_check_typedef (value_type (array));
3d967001 3115 struct type *base_index_type = TYPE_TARGET_TYPE (type->index_type ());
0c9c3474 3116 struct type *index_type
3d967001 3117 = create_static_range_type (NULL, type->index_type (), low, high);
9fe561ab
JB
3118 struct type *slice_type = create_array_type_with_stride
3119 (NULL, TYPE_TARGET_TYPE (type), index_type,
24e99c6c 3120 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 3121 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
3122 gdb::optional<LONGEST> low_pos, high_pos;
3123
5b4ee69b 3124
6244c119
SM
3125 low_pos = discrete_position (base_index_type, low);
3126 high_pos = discrete_position (base_index_type, high);
3127
3128 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
3129 {
3130 warning (_("unable to get positions in slice, use bounds instead"));
3131 low_pos = low;
3132 high_pos = high;
3133 }
3134
3135 return value_cast (slice_type,
6244c119 3136 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
3137}
3138
14f9c5c9
AS
3139/* If type is a record type in the form of a standard GNAT array
3140 descriptor, returns the number of dimensions for type. If arr is a
3141 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 3142 type designation. Otherwise, returns 0. */
14f9c5c9
AS
3143
3144int
d2e4a39e 3145ada_array_arity (struct type *type)
14f9c5c9
AS
3146{
3147 int arity;
3148
3149 if (type == NULL)
3150 return 0;
3151
3152 type = desc_base_type (type);
3153
3154 arity = 0;
78134374 3155 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 3156 return desc_arity (desc_bounds_type (type));
d2e4a39e 3157 else
78134374 3158 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3159 {
dda83cd7
SM
3160 arity += 1;
3161 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 3162 }
d2e4a39e 3163
14f9c5c9
AS
3164 return arity;
3165}
3166
3167/* If TYPE is a record type in the form of a standard GNAT array
3168 descriptor or a simple array type, returns the element type for
3169 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 3170 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 3171
d2e4a39e
AS
3172struct type *
3173ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
3174{
3175 type = desc_base_type (type);
3176
78134374 3177 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
3178 {
3179 int k;
d2e4a39e 3180 struct type *p_array_type;
14f9c5c9 3181
556bdfd4 3182 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
3183
3184 k = ada_array_arity (type);
3185 if (k == 0)
dda83cd7 3186 return NULL;
d2e4a39e 3187
4c4b4cd2 3188 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 3189 if (nindices >= 0 && k > nindices)
dda83cd7 3190 k = nindices;
d2e4a39e 3191 while (k > 0 && p_array_type != NULL)
dda83cd7
SM
3192 {
3193 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
3194 k -= 1;
3195 }
14f9c5c9
AS
3196 return p_array_type;
3197 }
78134374 3198 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 3199 {
78134374 3200 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7
SM
3201 {
3202 type = TYPE_TARGET_TYPE (type);
3203 nindices -= 1;
3204 }
14f9c5c9
AS
3205 return type;
3206 }
3207
3208 return NULL;
3209}
3210
08a057e6 3211/* See ada-lang.h. */
14f9c5c9 3212
08a057e6 3213struct type *
1eea4ebd 3214ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 3215{
4c4b4cd2
PH
3216 struct type *result_type;
3217
14f9c5c9
AS
3218 type = desc_base_type (type);
3219
1eea4ebd
UW
3220 if (n < 0 || n > ada_array_arity (type))
3221 error (_("invalid dimension number to '%s"), name);
14f9c5c9 3222
4c4b4cd2 3223 if (ada_is_simple_array_type (type))
14f9c5c9
AS
3224 {
3225 int i;
3226
3227 for (i = 1; i < n; i += 1)
2869ac4b
TT
3228 {
3229 type = ada_check_typedef (type);
3230 type = TYPE_TARGET_TYPE (type);
3231 }
3232 result_type = TYPE_TARGET_TYPE (ada_check_typedef (type)->index_type ());
4c4b4cd2 3233 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
3234 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3235 perhaps stabsread.c would make more sense. */
78134374 3236 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 3237 result_type = NULL;
14f9c5c9 3238 }
d2e4a39e 3239 else
1eea4ebd
UW
3240 {
3241 result_type = desc_index_type (desc_bounds_type (type), n);
3242 if (result_type == NULL)
3243 error (_("attempt to take bound of something that is not an array"));
3244 }
3245
3246 return result_type;
14f9c5c9
AS
3247}
3248
3249/* Given that arr is an array type, returns the lower bound of the
3250 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 3251 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
3252 array-descriptor type. It works for other arrays with bounds supplied
3253 by run-time quantities other than discriminants. */
14f9c5c9 3254
abb68b3e 3255static LONGEST
fb5e3d5c 3256ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 3257{
8a48ac95 3258 struct type *type, *index_type_desc, *index_type;
1ce677a4 3259 int i;
262452ec
JK
3260
3261 gdb_assert (which == 0 || which == 1);
14f9c5c9 3262
ad82864c
JB
3263 if (ada_is_constrained_packed_array_type (arr_type))
3264 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 3265
4c4b4cd2 3266 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 3267 return (LONGEST) - which;
14f9c5c9 3268
78134374 3269 if (arr_type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
3270 type = TYPE_TARGET_TYPE (arr_type);
3271 else
3272 type = arr_type;
3273
22c4c60c 3274 if (type->is_fixed_instance ())
bafffb51
JB
3275 {
3276 /* The array has already been fixed, so we do not need to
3277 check the parallel ___XA type again. That encoding has
3278 already been applied, so ignore it now. */
3279 index_type_desc = NULL;
3280 }
3281 else
3282 {
3283 index_type_desc = ada_find_parallel_type (type, "___XA");
3284 ada_fixup_array_indexes_type (index_type_desc);
3285 }
3286
262452ec 3287 if (index_type_desc != NULL)
940da03e 3288 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 3289 NULL);
262452ec 3290 else
8a48ac95
JB
3291 {
3292 struct type *elt_type = check_typedef (type);
3293
3294 for (i = 1; i < n; i++)
3295 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
3296
3d967001 3297 index_type = elt_type->index_type ();
8a48ac95 3298 }
262452ec 3299
43bbcdc2
PH
3300 return
3301 (LONGEST) (which == 0
dda83cd7
SM
3302 ? ada_discrete_type_low_bound (index_type)
3303 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
3304}
3305
3306/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
3307 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3308 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3309 supplied by run-time quantities other than discriminants. */
14f9c5c9 3310
1eea4ebd 3311static LONGEST
4dc81987 3312ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3313{
eb479039
JB
3314 struct type *arr_type;
3315
78134374 3316 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3317 arr = value_ind (arr);
3318 arr_type = value_enclosing_type (arr);
14f9c5c9 3319
ad82864c
JB
3320 if (ada_is_constrained_packed_array_type (arr_type))
3321 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3322 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3323 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3324 else
1eea4ebd 3325 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3326}
3327
3328/* Given that arr is an array value, returns the length of the
3329 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3330 supplied by run-time quantities other than discriminants.
3331 Does not work for arrays indexed by enumeration types with representation
3332 clauses at the moment. */
14f9c5c9 3333
1eea4ebd 3334static LONGEST
d2e4a39e 3335ada_array_length (struct value *arr, int n)
14f9c5c9 3336{
aa715135
JG
3337 struct type *arr_type, *index_type;
3338 int low, high;
eb479039 3339
78134374 3340 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3341 arr = value_ind (arr);
3342 arr_type = value_enclosing_type (arr);
14f9c5c9 3343
ad82864c
JB
3344 if (ada_is_constrained_packed_array_type (arr_type))
3345 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3346
4c4b4cd2 3347 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3348 {
3349 low = ada_array_bound_from_type (arr_type, n, 0);
3350 high = ada_array_bound_from_type (arr_type, n, 1);
3351 }
14f9c5c9 3352 else
aa715135
JG
3353 {
3354 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3355 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3356 }
3357
f168693b 3358 arr_type = check_typedef (arr_type);
7150d33c 3359 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3360 if (index_type != NULL)
3361 {
3362 struct type *base_type;
78134374 3363 if (index_type->code () == TYPE_CODE_RANGE)
aa715135
JG
3364 base_type = TYPE_TARGET_TYPE (index_type);
3365 else
3366 base_type = index_type;
3367
3368 low = pos_atr (value_from_longest (base_type, low));
3369 high = pos_atr (value_from_longest (base_type, high));
3370 }
3371 return high - low + 1;
4c4b4cd2
PH
3372}
3373
bff8c71f
TT
3374/* An array whose type is that of ARR_TYPE (an array type), with
3375 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3376 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3377
3378static struct value *
bff8c71f 3379empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3380{
b0dd7688 3381 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3382 struct type *index_type
3383 = create_static_range_type
dda83cd7 3384 (NULL, TYPE_TARGET_TYPE (arr_type0->index_type ()), low,
bff8c71f 3385 high < low ? low - 1 : high);
b0dd7688 3386 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3387
0b5d8877 3388 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3389}
14f9c5c9 3390\f
d2e4a39e 3391
dda83cd7 3392 /* Name resolution */
14f9c5c9 3393
4c4b4cd2
PH
3394/* The "decoded" name for the user-definable Ada operator corresponding
3395 to OP. */
14f9c5c9 3396
d2e4a39e 3397static const char *
4c4b4cd2 3398ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3399{
3400 int i;
3401
4c4b4cd2 3402 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3403 {
3404 if (ada_opname_table[i].op == op)
dda83cd7 3405 return ada_opname_table[i].decoded;
14f9c5c9 3406 }
323e0a4a 3407 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3408}
3409
de93309a
SM
3410/* Returns true (non-zero) iff decoded name N0 should appear before N1
3411 in a listing of choices during disambiguation (see sort_choices, below).
3412 The idea is that overloadings of a subprogram name from the
3413 same package should sort in their source order. We settle for ordering
3414 such symbols by their trailing number (__N or $N). */
14f9c5c9 3415
de93309a
SM
3416static int
3417encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3418{
de93309a
SM
3419 if (N1 == NULL)
3420 return 0;
3421 else if (N0 == NULL)
3422 return 1;
3423 else
3424 {
3425 int k0, k1;
30b15541 3426
de93309a 3427 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3428 ;
de93309a 3429 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3430 ;
de93309a 3431 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3432 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3433 {
3434 int n0, n1;
3435
3436 n0 = k0;
3437 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3438 n0 -= 1;
3439 n1 = k1;
3440 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3441 n1 -= 1;
3442 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3443 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3444 }
de93309a
SM
3445 return (strcmp (N0, N1) < 0);
3446 }
14f9c5c9
AS
3447}
3448
de93309a
SM
3449/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3450 encoded names. */
14f9c5c9 3451
de93309a
SM
3452static void
3453sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3454{
14f9c5c9 3455 int i;
14f9c5c9 3456
de93309a 3457 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3458 {
de93309a
SM
3459 struct block_symbol sym = syms[i];
3460 int j;
3461
3462 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3463 {
3464 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3465 sym.symbol->linkage_name ()))
3466 break;
3467 syms[j + 1] = syms[j];
3468 }
de93309a
SM
3469 syms[j + 1] = sym;
3470 }
3471}
14f9c5c9 3472
de93309a
SM
3473/* Whether GDB should display formals and return types for functions in the
3474 overloads selection menu. */
3475static bool print_signatures = true;
4c4b4cd2 3476
de93309a
SM
3477/* Print the signature for SYM on STREAM according to the FLAGS options. For
3478 all but functions, the signature is just the name of the symbol. For
3479 functions, this is the name of the function, the list of types for formals
3480 and the return type (if any). */
4c4b4cd2 3481
de93309a
SM
3482static void
3483ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3484 const struct type_print_options *flags)
3485{
5f9c5a63 3486 struct type *type = sym->type ();
14f9c5c9 3487
987012b8 3488 fprintf_filtered (stream, "%s", sym->print_name ());
de93309a
SM
3489 if (!print_signatures
3490 || type == NULL
78134374 3491 || type->code () != TYPE_CODE_FUNC)
de93309a 3492 return;
4c4b4cd2 3493
1f704f76 3494 if (type->num_fields () > 0)
de93309a
SM
3495 {
3496 int i;
14f9c5c9 3497
de93309a 3498 fprintf_filtered (stream, " (");
1f704f76 3499 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3500 {
3501 if (i > 0)
3502 fprintf_filtered (stream, "; ");
940da03e 3503 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3504 flags);
3505 }
3506 fprintf_filtered (stream, ")");
3507 }
3508 if (TYPE_TARGET_TYPE (type) != NULL
78134374 3509 && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
de93309a
SM
3510 {
3511 fprintf_filtered (stream, " return ");
3512 ada_print_type (TYPE_TARGET_TYPE (type), NULL, stream, -1, 0, flags);
3513 }
3514}
14f9c5c9 3515
de93309a
SM
3516/* Read and validate a set of numeric choices from the user in the
3517 range 0 .. N_CHOICES-1. Place the results in increasing
3518 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3519
de93309a
SM
3520 The user types choices as a sequence of numbers on one line
3521 separated by blanks, encoding them as follows:
14f9c5c9 3522
de93309a
SM
3523 + A choice of 0 means to cancel the selection, throwing an error.
3524 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3525 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3526
de93309a 3527 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3528
de93309a
SM
3529 ANNOTATION_SUFFIX, if present, is used to annotate the input
3530 prompts (for use with the -f switch). */
14f9c5c9 3531
de93309a
SM
3532static int
3533get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3534 int is_all_choice, const char *annotation_suffix)
de93309a 3535{
992a7040 3536 const char *args;
de93309a
SM
3537 const char *prompt;
3538 int n_chosen;
3539 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3540
de93309a
SM
3541 prompt = getenv ("PS2");
3542 if (prompt == NULL)
3543 prompt = "> ";
4c4b4cd2 3544
de93309a 3545 args = command_line_input (prompt, annotation_suffix);
4c4b4cd2 3546
de93309a
SM
3547 if (args == NULL)
3548 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3549
de93309a 3550 n_chosen = 0;
4c4b4cd2 3551
de93309a
SM
3552 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3553 order, as given in args. Choices are validated. */
3554 while (1)
14f9c5c9 3555 {
de93309a
SM
3556 char *args2;
3557 int choice, j;
76a01679 3558
de93309a
SM
3559 args = skip_spaces (args);
3560 if (*args == '\0' && n_chosen == 0)
dda83cd7 3561 error_no_arg (_("one or more choice numbers"));
de93309a 3562 else if (*args == '\0')
dda83cd7 3563 break;
76a01679 3564
de93309a
SM
3565 choice = strtol (args, &args2, 10);
3566 if (args == args2 || choice < 0
dda83cd7
SM
3567 || choice > n_choices + first_choice - 1)
3568 error (_("Argument must be choice number"));
de93309a 3569 args = args2;
76a01679 3570
de93309a 3571 if (choice == 0)
dda83cd7 3572 error (_("cancelled"));
76a01679 3573
de93309a 3574 if (choice < first_choice)
dda83cd7
SM
3575 {
3576 n_chosen = n_choices;
3577 for (j = 0; j < n_choices; j += 1)
3578 choices[j] = j;
3579 break;
3580 }
de93309a 3581 choice -= first_choice;
76a01679 3582
de93309a 3583 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3584 {
3585 }
4c4b4cd2 3586
de93309a 3587 if (j < 0 || choice != choices[j])
dda83cd7
SM
3588 {
3589 int k;
4c4b4cd2 3590
dda83cd7
SM
3591 for (k = n_chosen - 1; k > j; k -= 1)
3592 choices[k + 1] = choices[k];
3593 choices[j + 1] = choice;
3594 n_chosen += 1;
3595 }
14f9c5c9
AS
3596 }
3597
de93309a
SM
3598 if (n_chosen > max_results)
3599 error (_("Select no more than %d of the above"), max_results);
3600
3601 return n_chosen;
14f9c5c9
AS
3602}
3603
de93309a
SM
3604/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3605 by asking the user (if necessary), returning the number selected,
3606 and setting the first elements of SYMS items. Error if no symbols
3607 selected. */
3608
3609/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3610 to be re-integrated one of these days. */
14f9c5c9
AS
3611
3612static int
de93309a 3613user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3614{
de93309a
SM
3615 int i;
3616 int *chosen = XALLOCAVEC (int , nsyms);
3617 int n_chosen;
3618 int first_choice = (max_results == 1) ? 1 : 2;
3619 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3620
de93309a
SM
3621 if (max_results < 1)
3622 error (_("Request to select 0 symbols!"));
3623 if (nsyms <= 1)
3624 return nsyms;
14f9c5c9 3625
de93309a
SM
3626 if (select_mode == multiple_symbols_cancel)
3627 error (_("\
3628canceled because the command is ambiguous\n\
3629See set/show multiple-symbol."));
14f9c5c9 3630
de93309a
SM
3631 /* If select_mode is "all", then return all possible symbols.
3632 Only do that if more than one symbol can be selected, of course.
3633 Otherwise, display the menu as usual. */
3634 if (select_mode == multiple_symbols_all && max_results > 1)
3635 return nsyms;
14f9c5c9 3636
de93309a
SM
3637 printf_filtered (_("[0] cancel\n"));
3638 if (max_results > 1)
3639 printf_filtered (_("[1] all\n"));
14f9c5c9 3640
de93309a 3641 sort_choices (syms, nsyms);
14f9c5c9 3642
de93309a
SM
3643 for (i = 0; i < nsyms; i += 1)
3644 {
3645 if (syms[i].symbol == NULL)
dda83cd7 3646 continue;
14f9c5c9 3647
66d7f48f 3648 if (syms[i].symbol->aclass () == LOC_BLOCK)
dda83cd7
SM
3649 {
3650 struct symtab_and_line sal =
3651 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3652
de93309a
SM
3653 printf_filtered ("[%d] ", i + first_choice);
3654 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3655 &type_print_raw_options);
3656 if (sal.symtab == NULL)
3657 printf_filtered (_(" at %p[<no source file available>%p]:%d\n"),
3658 metadata_style.style ().ptr (), nullptr, sal.line);
3659 else
3660 printf_filtered
3661 (_(" at %ps:%d\n"),
3662 styled_string (file_name_style.style (),
3663 symtab_to_filename_for_display (sal.symtab)),
3664 sal.line);
dda83cd7
SM
3665 continue;
3666 }
76a01679 3667 else
dda83cd7
SM
3668 {
3669 int is_enumeral =
66d7f48f 3670 (syms[i].symbol->aclass () == LOC_CONST
5f9c5a63
SM
3671 && syms[i].symbol->type () != NULL
3672 && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
de93309a 3673 struct symtab *symtab = NULL;
4c4b4cd2 3674
7b3ecc75 3675 if (syms[i].symbol->is_objfile_owned ())
de93309a
SM
3676 symtab = symbol_symtab (syms[i].symbol);
3677
5d0027b9 3678 if (syms[i].symbol->line () != 0 && symtab != NULL)
de93309a
SM
3679 {
3680 printf_filtered ("[%d] ", i + first_choice);
3681 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3682 &type_print_raw_options);
3683 printf_filtered (_(" at %s:%d\n"),
3684 symtab_to_filename_for_display (symtab),
5d0027b9 3685 syms[i].symbol->line ());
de93309a 3686 }
dda83cd7 3687 else if (is_enumeral
5f9c5a63 3688 && syms[i].symbol->type ()->name () != NULL)
dda83cd7
SM
3689 {
3690 printf_filtered (("[%d] "), i + first_choice);
5f9c5a63 3691 ada_print_type (syms[i].symbol->type (), NULL,
dda83cd7
SM
3692 gdb_stdout, -1, 0, &type_print_raw_options);
3693 printf_filtered (_("'(%s) (enumeral)\n"),
987012b8 3694 syms[i].symbol->print_name ());
dda83cd7 3695 }
de93309a
SM
3696 else
3697 {
3698 printf_filtered ("[%d] ", i + first_choice);
3699 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3700 &type_print_raw_options);
3701
3702 if (symtab != NULL)
3703 printf_filtered (is_enumeral
3704 ? _(" in %s (enumeral)\n")
3705 : _(" at %s:?\n"),
3706 symtab_to_filename_for_display (symtab));
3707 else
3708 printf_filtered (is_enumeral
3709 ? _(" (enumeral)\n")
3710 : _(" at ?\n"));
3711 }
dda83cd7 3712 }
14f9c5c9 3713 }
14f9c5c9 3714
de93309a 3715 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3716 "overload-choice");
14f9c5c9 3717
de93309a
SM
3718 for (i = 0; i < n_chosen; i += 1)
3719 syms[i] = syms[chosen[i]];
14f9c5c9 3720
de93309a
SM
3721 return n_chosen;
3722}
14f9c5c9 3723
cd9a3148
TT
3724/* See ada-lang.h. */
3725
3726block_symbol
7056f312 3727ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3728 int nargs, value *argvec[])
3729{
3730 if (possible_user_operator_p (op, argvec))
3731 {
3732 std::vector<struct block_symbol> candidates
3733 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3734 NULL, VAR_DOMAIN);
3735
3736 int i = ada_resolve_function (candidates, argvec,
3737 nargs, ada_decoded_op_name (op), NULL,
3738 parse_completion);
3739 if (i >= 0)
3740 return candidates[i];
3741 }
3742 return {};
3743}
3744
3745/* See ada-lang.h. */
3746
3747block_symbol
3748ada_resolve_funcall (struct symbol *sym, const struct block *block,
3749 struct type *context_type,
7056f312 3750 bool parse_completion,
cd9a3148
TT
3751 int nargs, value *argvec[],
3752 innermost_block_tracker *tracker)
3753{
3754 std::vector<struct block_symbol> candidates
3755 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3756
3757 int i;
3758 if (candidates.size () == 1)
3759 i = 0;
3760 else
3761 {
3762 i = ada_resolve_function
3763 (candidates,
3764 argvec, nargs,
3765 sym->linkage_name (),
3766 context_type, parse_completion);
3767 if (i < 0)
3768 error (_("Could not find a match for %s"), sym->print_name ());
3769 }
3770
3771 tracker->update (candidates[i]);
3772 return candidates[i];
3773}
3774
ba8694b6
TT
3775/* Resolve a mention of a name where the context type is an
3776 enumeration type. */
3777
3778static int
3779ada_resolve_enum (std::vector<struct block_symbol> &syms,
3780 const char *name, struct type *context_type,
3781 bool parse_completion)
3782{
3783 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3784 context_type = ada_check_typedef (context_type);
3785
3786 for (int i = 0; i < syms.size (); ++i)
3787 {
3788 /* We already know the name matches, so we're just looking for
3789 an element of the correct enum type. */
5f9c5a63 3790 if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
ba8694b6
TT
3791 return i;
3792 }
3793
3794 error (_("No name '%s' in enumeration type '%s'"), name,
3795 ada_type_name (context_type));
3796}
3797
cd9a3148
TT
3798/* See ada-lang.h. */
3799
3800block_symbol
3801ada_resolve_variable (struct symbol *sym, const struct block *block,
3802 struct type *context_type,
7056f312 3803 bool parse_completion,
cd9a3148
TT
3804 int deprocedure_p,
3805 innermost_block_tracker *tracker)
3806{
3807 std::vector<struct block_symbol> candidates
3808 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3809
3810 if (std::any_of (candidates.begin (),
3811 candidates.end (),
3812 [] (block_symbol &bsym)
3813 {
66d7f48f 3814 switch (bsym.symbol->aclass ())
cd9a3148
TT
3815 {
3816 case LOC_REGISTER:
3817 case LOC_ARG:
3818 case LOC_REF_ARG:
3819 case LOC_REGPARM_ADDR:
3820 case LOC_LOCAL:
3821 case LOC_COMPUTED:
3822 return true;
3823 default:
3824 return false;
3825 }
3826 }))
3827 {
3828 /* Types tend to get re-introduced locally, so if there
3829 are any local symbols that are not types, first filter
3830 out all types. */
3831 candidates.erase
3832 (std::remove_if
3833 (candidates.begin (),
3834 candidates.end (),
3835 [] (block_symbol &bsym)
3836 {
66d7f48f 3837 return bsym.symbol->aclass () == LOC_TYPEDEF;
cd9a3148
TT
3838 }),
3839 candidates.end ());
3840 }
3841
2c71f639
TV
3842 /* Filter out artificial symbols. */
3843 candidates.erase
3844 (std::remove_if
3845 (candidates.begin (),
3846 candidates.end (),
3847 [] (block_symbol &bsym)
3848 {
3849 return bsym.symbol->artificial;
3850 }),
3851 candidates.end ());
3852
cd9a3148
TT
3853 int i;
3854 if (candidates.empty ())
3855 error (_("No definition found for %s"), sym->print_name ());
3856 else if (candidates.size () == 1)
3857 i = 0;
ba8694b6
TT
3858 else if (context_type != nullptr
3859 && context_type->code () == TYPE_CODE_ENUM)
3860 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3861 parse_completion);
cd9a3148
TT
3862 else if (deprocedure_p && !is_nonfunction (candidates))
3863 {
3864 i = ada_resolve_function
3865 (candidates, NULL, 0,
3866 sym->linkage_name (),
3867 context_type, parse_completion);
3868 if (i < 0)
3869 error (_("Could not find a match for %s"), sym->print_name ());
3870 }
3871 else
3872 {
3873 printf_filtered (_("Multiple matches for %s\n"), sym->print_name ());
3874 user_select_syms (candidates.data (), candidates.size (), 1);
3875 i = 0;
3876 }
3877
3878 tracker->update (candidates[i]);
3879 return candidates[i];
3880}
3881
db2534b7 3882/* Return non-zero if formal type FTYPE matches actual type ATYPE. */
de93309a
SM
3883/* The term "match" here is rather loose. The match is heuristic and
3884 liberal. */
14f9c5c9 3885
de93309a 3886static int
db2534b7 3887ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3888{
de93309a
SM
3889 ftype = ada_check_typedef (ftype);
3890 atype = ada_check_typedef (atype);
14f9c5c9 3891
78134374 3892 if (ftype->code () == TYPE_CODE_REF)
de93309a 3893 ftype = TYPE_TARGET_TYPE (ftype);
78134374 3894 if (atype->code () == TYPE_CODE_REF)
de93309a 3895 atype = TYPE_TARGET_TYPE (atype);
14f9c5c9 3896
78134374 3897 switch (ftype->code ())
14f9c5c9 3898 {
de93309a 3899 default:
78134374 3900 return ftype->code () == atype->code ();
de93309a 3901 case TYPE_CODE_PTR:
db2534b7
TT
3902 if (atype->code () != TYPE_CODE_PTR)
3903 return 0;
3904 atype = TYPE_TARGET_TYPE (atype);
3905 /* This can only happen if the actual argument is 'null'. */
3906 if (atype->code () == TYPE_CODE_INT && TYPE_LENGTH (atype) == 0)
3907 return 1;
3908 return ada_type_match (TYPE_TARGET_TYPE (ftype), atype);
de93309a
SM
3909 case TYPE_CODE_INT:
3910 case TYPE_CODE_ENUM:
3911 case TYPE_CODE_RANGE:
78134374 3912 switch (atype->code ())
dda83cd7
SM
3913 {
3914 case TYPE_CODE_INT:
3915 case TYPE_CODE_ENUM:
3916 case TYPE_CODE_RANGE:
3917 return 1;
3918 default:
3919 return 0;
3920 }
d2e4a39e 3921
de93309a 3922 case TYPE_CODE_ARRAY:
78134374 3923 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3924 || ada_is_array_descriptor_type (atype));
14f9c5c9 3925
de93309a
SM
3926 case TYPE_CODE_STRUCT:
3927 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3928 return (atype->code () == TYPE_CODE_ARRAY
3929 || ada_is_array_descriptor_type (atype));
de93309a 3930 else
dda83cd7
SM
3931 return (atype->code () == TYPE_CODE_STRUCT
3932 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3933
de93309a
SM
3934 case TYPE_CODE_UNION:
3935 case TYPE_CODE_FLT:
78134374 3936 return (atype->code () == ftype->code ());
de93309a 3937 }
14f9c5c9
AS
3938}
3939
de93309a
SM
3940/* Return non-zero if the formals of FUNC "sufficiently match" the
3941 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3942 may also be an enumeral, in which case it is treated as a 0-
3943 argument function. */
14f9c5c9 3944
de93309a
SM
3945static int
3946ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3947{
3948 int i;
5f9c5a63 3949 struct type *func_type = func->type ();
14f9c5c9 3950
66d7f48f 3951 if (func->aclass () == LOC_CONST
78134374 3952 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3953 return (n_actuals == 0);
78134374 3954 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3955 return 0;
14f9c5c9 3956
1f704f76 3957 if (func_type->num_fields () != n_actuals)
de93309a 3958 return 0;
14f9c5c9 3959
de93309a
SM
3960 for (i = 0; i < n_actuals; i += 1)
3961 {
3962 if (actuals[i] == NULL)
dda83cd7 3963 return 0;
de93309a 3964 else
dda83cd7
SM
3965 {
3966 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
3967 struct type *atype = ada_check_typedef (value_type (actuals[i]));
14f9c5c9 3968
db2534b7 3969 if (!ada_type_match (ftype, atype))
dda83cd7
SM
3970 return 0;
3971 }
de93309a
SM
3972 }
3973 return 1;
3974}
d2e4a39e 3975
de93309a
SM
3976/* False iff function type FUNC_TYPE definitely does not produce a value
3977 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3978 FUNC_TYPE is not a valid function type with a non-null return type
3979 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 3980
de93309a
SM
3981static int
3982return_match (struct type *func_type, struct type *context_type)
3983{
3984 struct type *return_type;
d2e4a39e 3985
de93309a
SM
3986 if (func_type == NULL)
3987 return 1;
14f9c5c9 3988
78134374 3989 if (func_type->code () == TYPE_CODE_FUNC)
de93309a
SM
3990 return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
3991 else
3992 return_type = get_base_type (func_type);
3993 if (return_type == NULL)
3994 return 1;
76a01679 3995
de93309a 3996 context_type = get_base_type (context_type);
14f9c5c9 3997
78134374 3998 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
3999 return context_type == NULL || return_type == context_type;
4000 else if (context_type == NULL)
78134374 4001 return return_type->code () != TYPE_CODE_VOID;
de93309a 4002 else
78134374 4003 return return_type->code () == context_type->code ();
de93309a 4004}
14f9c5c9 4005
14f9c5c9 4006
1bfa81ac 4007/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
4008 function (if any) that matches the types of the NARGS arguments in
4009 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4010 that returns that type, then eliminate matches that don't. If
4011 CONTEXT_TYPE is void and there is at least one match that does not
4012 return void, eliminate all matches that do.
14f9c5c9 4013
de93309a
SM
4014 Asks the user if there is more than one match remaining. Returns -1
4015 if there is no such symbol or none is selected. NAME is used
4016 solely for messages. May re-arrange and modify SYMS in
4017 the process; the index returned is for the modified vector. */
14f9c5c9 4018
de93309a 4019static int
d1183b06
TT
4020ada_resolve_function (std::vector<struct block_symbol> &syms,
4021 struct value **args, int nargs,
dda83cd7 4022 const char *name, struct type *context_type,
7056f312 4023 bool parse_completion)
de93309a
SM
4024{
4025 int fallback;
4026 int k;
4027 int m; /* Number of hits */
14f9c5c9 4028
de93309a
SM
4029 m = 0;
4030 /* In the first pass of the loop, we only accept functions matching
4031 context_type. If none are found, we add a second pass of the loop
4032 where every function is accepted. */
4033 for (fallback = 0; m == 0 && fallback < 2; fallback++)
4034 {
d1183b06 4035 for (k = 0; k < syms.size (); k += 1)
dda83cd7 4036 {
5f9c5a63 4037 struct type *type = ada_check_typedef (syms[k].symbol->type ());
5b4ee69b 4038
dda83cd7
SM
4039 if (ada_args_match (syms[k].symbol, args, nargs)
4040 && (fallback || return_match (type, context_type)))
4041 {
4042 syms[m] = syms[k];
4043 m += 1;
4044 }
4045 }
14f9c5c9
AS
4046 }
4047
de93309a
SM
4048 /* If we got multiple matches, ask the user which one to use. Don't do this
4049 interactive thing during completion, though, as the purpose of the
4050 completion is providing a list of all possible matches. Prompting the
4051 user to filter it down would be completely unexpected in this case. */
4052 if (m == 0)
4053 return -1;
4054 else if (m > 1 && !parse_completion)
4055 {
4056 printf_filtered (_("Multiple matches for %s\n"), name);
d1183b06 4057 user_select_syms (syms.data (), m, 1);
de93309a
SM
4058 return 0;
4059 }
4060 return 0;
14f9c5c9
AS
4061}
4062
14f9c5c9
AS
4063/* Type-class predicates */
4064
4c4b4cd2
PH
4065/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4066 or FLOAT). */
14f9c5c9
AS
4067
4068static int
d2e4a39e 4069numeric_type_p (struct type *type)
14f9c5c9
AS
4070{
4071 if (type == NULL)
4072 return 0;
d2e4a39e
AS
4073 else
4074 {
78134374 4075 switch (type->code ())
dda83cd7
SM
4076 {
4077 case TYPE_CODE_INT:
4078 case TYPE_CODE_FLT:
c04da66c 4079 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4080 return 1;
4081 case TYPE_CODE_RANGE:
4082 return (type == TYPE_TARGET_TYPE (type)
4083 || numeric_type_p (TYPE_TARGET_TYPE (type)));
4084 default:
4085 return 0;
4086 }
d2e4a39e 4087 }
14f9c5c9
AS
4088}
4089
4c4b4cd2 4090/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4091
4092static int
d2e4a39e 4093integer_type_p (struct type *type)
14f9c5c9
AS
4094{
4095 if (type == NULL)
4096 return 0;
d2e4a39e
AS
4097 else
4098 {
78134374 4099 switch (type->code ())
dda83cd7
SM
4100 {
4101 case TYPE_CODE_INT:
4102 return 1;
4103 case TYPE_CODE_RANGE:
4104 return (type == TYPE_TARGET_TYPE (type)
4105 || integer_type_p (TYPE_TARGET_TYPE (type)));
4106 default:
4107 return 0;
4108 }
d2e4a39e 4109 }
14f9c5c9
AS
4110}
4111
4c4b4cd2 4112/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4113
4114static int
d2e4a39e 4115scalar_type_p (struct type *type)
14f9c5c9
AS
4116{
4117 if (type == NULL)
4118 return 0;
d2e4a39e
AS
4119 else
4120 {
78134374 4121 switch (type->code ())
dda83cd7
SM
4122 {
4123 case TYPE_CODE_INT:
4124 case TYPE_CODE_RANGE:
4125 case TYPE_CODE_ENUM:
4126 case TYPE_CODE_FLT:
c04da66c 4127 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
4128 return 1;
4129 default:
4130 return 0;
4131 }
d2e4a39e 4132 }
14f9c5c9
AS
4133}
4134
4c4b4cd2 4135/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
4136
4137static int
d2e4a39e 4138discrete_type_p (struct type *type)
14f9c5c9
AS
4139{
4140 if (type == NULL)
4141 return 0;
d2e4a39e
AS
4142 else
4143 {
78134374 4144 switch (type->code ())
dda83cd7
SM
4145 {
4146 case TYPE_CODE_INT:
4147 case TYPE_CODE_RANGE:
4148 case TYPE_CODE_ENUM:
4149 case TYPE_CODE_BOOL:
4150 return 1;
4151 default:
4152 return 0;
4153 }
d2e4a39e 4154 }
14f9c5c9
AS
4155}
4156
4c4b4cd2
PH
4157/* Returns non-zero if OP with operands in the vector ARGS could be
4158 a user-defined function. Errs on the side of pre-defined operators
4159 (i.e., result 0). */
14f9c5c9
AS
4160
4161static int
d2e4a39e 4162possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4163{
76a01679 4164 struct type *type0 =
df407dfe 4165 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 4166 struct type *type1 =
df407dfe 4167 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 4168
4c4b4cd2
PH
4169 if (type0 == NULL)
4170 return 0;
4171
14f9c5c9
AS
4172 switch (op)
4173 {
4174 default:
4175 return 0;
4176
4177 case BINOP_ADD:
4178 case BINOP_SUB:
4179 case BINOP_MUL:
4180 case BINOP_DIV:
d2e4a39e 4181 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4182
4183 case BINOP_REM:
4184 case BINOP_MOD:
4185 case BINOP_BITWISE_AND:
4186 case BINOP_BITWISE_IOR:
4187 case BINOP_BITWISE_XOR:
d2e4a39e 4188 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4189
4190 case BINOP_EQUAL:
4191 case BINOP_NOTEQUAL:
4192 case BINOP_LESS:
4193 case BINOP_GTR:
4194 case BINOP_LEQ:
4195 case BINOP_GEQ:
d2e4a39e 4196 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4197
4198 case BINOP_CONCAT:
ee90b9ab 4199 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4200
4201 case BINOP_EXP:
d2e4a39e 4202 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4203
4204 case UNOP_NEG:
4205 case UNOP_PLUS:
4206 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4207 case UNOP_ABS:
4208 return (!numeric_type_p (type0));
14f9c5c9
AS
4209
4210 }
4211}
4212\f
dda83cd7 4213 /* Renaming */
14f9c5c9 4214
aeb5907d
JB
4215/* NOTES:
4216
4217 1. In the following, we assume that a renaming type's name may
4218 have an ___XD suffix. It would be nice if this went away at some
4219 point.
4220 2. We handle both the (old) purely type-based representation of
4221 renamings and the (new) variable-based encoding. At some point,
4222 it is devoutly to be hoped that the former goes away
4223 (FIXME: hilfinger-2007-07-09).
4224 3. Subprogram renamings are not implemented, although the XRS
4225 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4226
4227/* If SYM encodes a renaming,
4228
4229 <renaming> renames <renamed entity>,
4230
4231 sets *LEN to the length of the renamed entity's name,
4232 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4233 the string describing the subcomponent selected from the renamed
0963b4bd 4234 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4235 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4236 are undefined). Otherwise, returns a value indicating the category
4237 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4238 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4239 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4240 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4241 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4242 may be NULL, in which case they are not assigned.
4243
4244 [Currently, however, GCC does not generate subprogram renamings.] */
4245
4246enum ada_renaming_category
4247ada_parse_renaming (struct symbol *sym,
4248 const char **renamed_entity, int *len,
4249 const char **renaming_expr)
4250{
4251 enum ada_renaming_category kind;
4252 const char *info;
4253 const char *suffix;
4254
4255 if (sym == NULL)
4256 return ADA_NOT_RENAMING;
66d7f48f 4257 switch (sym->aclass ())
14f9c5c9 4258 {
aeb5907d
JB
4259 default:
4260 return ADA_NOT_RENAMING;
aeb5907d
JB
4261 case LOC_LOCAL:
4262 case LOC_STATIC:
4263 case LOC_COMPUTED:
4264 case LOC_OPTIMIZED_OUT:
987012b8 4265 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4266 if (info == NULL)
4267 return ADA_NOT_RENAMING;
4268 switch (info[5])
4269 {
4270 case '_':
4271 kind = ADA_OBJECT_RENAMING;
4272 info += 6;
4273 break;
4274 case 'E':
4275 kind = ADA_EXCEPTION_RENAMING;
4276 info += 7;
4277 break;
4278 case 'P':
4279 kind = ADA_PACKAGE_RENAMING;
4280 info += 7;
4281 break;
4282 case 'S':
4283 kind = ADA_SUBPROGRAM_RENAMING;
4284 info += 7;
4285 break;
4286 default:
4287 return ADA_NOT_RENAMING;
4288 }
14f9c5c9 4289 }
4c4b4cd2 4290
de93309a
SM
4291 if (renamed_entity != NULL)
4292 *renamed_entity = info;
4293 suffix = strstr (info, "___XE");
4294 if (suffix == NULL || suffix == info)
4295 return ADA_NOT_RENAMING;
4296 if (len != NULL)
4297 *len = strlen (info) - strlen (suffix);
4298 suffix += 5;
4299 if (renaming_expr != NULL)
4300 *renaming_expr = suffix;
4301 return kind;
4302}
4303
4304/* Compute the value of the given RENAMING_SYM, which is expected to
4305 be a symbol encoding a renaming expression. BLOCK is the block
4306 used to evaluate the renaming. */
4307
4308static struct value *
4309ada_read_renaming_var_value (struct symbol *renaming_sym,
4310 const struct block *block)
4311{
4312 const char *sym_name;
4313
987012b8 4314 sym_name = renaming_sym->linkage_name ();
de93309a
SM
4315 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
4316 return evaluate_expression (expr.get ());
4317}
4318\f
4319
dda83cd7 4320 /* Evaluation: Function Calls */
de93309a
SM
4321
4322/* Return an lvalue containing the value VAL. This is the identity on
4323 lvalues, and otherwise has the side-effect of allocating memory
4324 in the inferior where a copy of the value contents is copied. */
4325
4326static struct value *
4327ensure_lval (struct value *val)
4328{
4329 if (VALUE_LVAL (val) == not_lval
4330 || VALUE_LVAL (val) == lval_internalvar)
4331 {
4332 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
4333 const CORE_ADDR addr =
dda83cd7 4334 value_as_long (value_allocate_space_in_inferior (len));
de93309a
SM
4335
4336 VALUE_LVAL (val) = lval_memory;
4337 set_value_address (val, addr);
50888e42 4338 write_memory (addr, value_contents (val).data (), len);
de93309a
SM
4339 }
4340
4341 return val;
4342}
4343
4344/* Given ARG, a value of type (pointer or reference to a)*
4345 structure/union, extract the component named NAME from the ultimate
4346 target structure/union and return it as a value with its
4347 appropriate type.
4348
4349 The routine searches for NAME among all members of the structure itself
4350 and (recursively) among all members of any wrapper members
4351 (e.g., '_parent').
4352
4353 If NO_ERR, then simply return NULL in case of error, rather than
4354 calling error. */
4355
4356static struct value *
4357ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4358{
4359 struct type *t, *t1;
4360 struct value *v;
4361 int check_tag;
4362
4363 v = NULL;
4364 t1 = t = ada_check_typedef (value_type (arg));
78134374 4365 if (t->code () == TYPE_CODE_REF)
de93309a
SM
4366 {
4367 t1 = TYPE_TARGET_TYPE (t);
4368 if (t1 == NULL)
4369 goto BadValue;
4370 t1 = ada_check_typedef (t1);
78134374 4371 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4372 {
4373 arg = coerce_ref (arg);
4374 t = t1;
4375 }
de93309a
SM
4376 }
4377
78134374 4378 while (t->code () == TYPE_CODE_PTR)
de93309a
SM
4379 {
4380 t1 = TYPE_TARGET_TYPE (t);
4381 if (t1 == NULL)
4382 goto BadValue;
4383 t1 = ada_check_typedef (t1);
78134374 4384 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4385 {
4386 arg = value_ind (arg);
4387 t = t1;
4388 }
de93309a 4389 else
dda83cd7 4390 break;
de93309a 4391 }
aeb5907d 4392
78134374 4393 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4394 goto BadValue;
52ce6436 4395
de93309a
SM
4396 if (t1 == t)
4397 v = ada_search_struct_field (name, arg, 0, t);
4398 else
4399 {
4400 int bit_offset, bit_size, byte_offset;
4401 struct type *field_type;
4402 CORE_ADDR address;
a5ee536b 4403
78134374 4404 if (t->code () == TYPE_CODE_PTR)
de93309a
SM
4405 address = value_address (ada_value_ind (arg));
4406 else
4407 address = value_address (ada_coerce_ref (arg));
d2e4a39e 4408
de93309a 4409 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4410 the case where the type is a reference to a tagged type, but
4411 we have to be careful to exclude pointers to tagged types.
4412 The latter should be shown as usual (as a pointer), whereas
4413 a reference should mostly be transparent to the user. */
14f9c5c9 4414
de93309a 4415 if (ada_is_tagged_type (t1, 0)
dda83cd7
SM
4416 || (t1->code () == TYPE_CODE_REF
4417 && ada_is_tagged_type (TYPE_TARGET_TYPE (t1), 0)))
4418 {
4419 /* We first try to find the searched field in the current type.
de93309a 4420 If not found then let's look in the fixed type. */
14f9c5c9 4421
dda83cd7 4422 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4423 nullptr, nullptr, nullptr,
4424 nullptr, nullptr))
de93309a
SM
4425 check_tag = 1;
4426 else
4427 check_tag = 0;
dda83cd7 4428 }
de93309a
SM
4429 else
4430 check_tag = 0;
c3e5cd34 4431
de93309a
SM
4432 /* Convert to fixed type in all cases, so that we have proper
4433 offsets to each field in unconstrained record types. */
4434 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4435 address, NULL, check_tag);
4436
24aa1b02
TT
4437 /* Resolve the dynamic type as well. */
4438 arg = value_from_contents_and_address (t1, nullptr, address);
4439 t1 = value_type (arg);
4440
de93309a 4441 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4442 &field_type, &byte_offset, &bit_offset,
4443 &bit_size, NULL))
4444 {
4445 if (bit_size != 0)
4446 {
4447 if (t->code () == TYPE_CODE_REF)
4448 arg = ada_coerce_ref (arg);
4449 else
4450 arg = ada_value_ind (arg);
4451 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4452 bit_offset, bit_size,
4453 field_type);
4454 }
4455 else
4456 v = value_at_lazy (field_type, address + byte_offset);
4457 }
c3e5cd34 4458 }
14f9c5c9 4459
de93309a
SM
4460 if (v != NULL || no_err)
4461 return v;
4462 else
4463 error (_("There is no member named %s."), name);
4464
4465 BadValue:
4466 if (no_err)
4467 return NULL;
4468 else
4469 error (_("Attempt to extract a component of "
4470 "a value that is not a record."));
14f9c5c9
AS
4471}
4472
4473/* Return the value ACTUAL, converted to be an appropriate value for a
4474 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4475 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4476 values not residing in memory, updating it as needed. */
14f9c5c9 4477
a93c0eb6 4478struct value *
40bc484c 4479ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4480{
df407dfe 4481 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 4482 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4483 struct type *formal_target =
78134374 4484 formal_type->code () == TYPE_CODE_PTR
61ee279c 4485 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e 4486 struct type *actual_target =
78134374 4487 actual_type->code () == TYPE_CODE_PTR
61ee279c 4488 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 4489
4c4b4cd2 4490 if (ada_is_array_descriptor_type (formal_target)
78134374 4491 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4492 return make_array_descriptor (formal_type, actual);
78134374
SM
4493 else if (formal_type->code () == TYPE_CODE_PTR
4494 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4495 {
a84a8a0d 4496 struct value *result;
5b4ee69b 4497
78134374 4498 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4499 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4500 result = desc_data (actual);
78134374 4501 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7
SM
4502 {
4503 if (VALUE_LVAL (actual) != lval_memory)
4504 {
4505 struct value *val;
4506
4507 actual_type = ada_check_typedef (value_type (actual));
4508 val = allocate_value (actual_type);
4bce7cda 4509 copy (value_contents (actual), value_contents_raw (val));
dda83cd7
SM
4510 actual = ensure_lval (val);
4511 }
4512 result = value_addr (actual);
4513 }
a84a8a0d
JB
4514 else
4515 return actual;
b1af9e97 4516 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4517 }
78134374 4518 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4519 return ada_value_ind (actual);
8344af1e
JB
4520 else if (ada_is_aligner_type (formal_type))
4521 {
4522 /* We need to turn this parameter into an aligner type
4523 as well. */
4524 struct value *aligner = allocate_value (formal_type);
4525 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4526
4527 value_assign_to_component (aligner, component, actual);
4528 return aligner;
4529 }
14f9c5c9
AS
4530
4531 return actual;
4532}
4533
438c98a1
JB
4534/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4535 type TYPE. This is usually an inefficient no-op except on some targets
4536 (such as AVR) where the representation of a pointer and an address
4537 differs. */
4538
4539static CORE_ADDR
4540value_pointer (struct value *value, struct type *type)
4541{
438c98a1 4542 unsigned len = TYPE_LENGTH (type);
224c3ddb 4543 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4544 CORE_ADDR addr;
4545
4546 addr = value_address (value);
8ee511af 4547 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4548 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4549 return addr;
4550}
4551
14f9c5c9 4552
4c4b4cd2
PH
4553/* Push a descriptor of type TYPE for array value ARR on the stack at
4554 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4555 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4556 to-descriptor type rather than a descriptor type), a struct value *
4557 representing a pointer to this descriptor. */
14f9c5c9 4558
d2e4a39e 4559static struct value *
40bc484c 4560make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4561{
d2e4a39e
AS
4562 struct type *bounds_type = desc_bounds_type (type);
4563 struct type *desc_type = desc_base_type (type);
4564 struct value *descriptor = allocate_value (desc_type);
4565 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4566 int i;
d2e4a39e 4567
0963b4bd
MS
4568 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4569 i > 0; i -= 1)
14f9c5c9 4570 {
50888e42
SM
4571 modify_field (value_type (bounds),
4572 value_contents_writeable (bounds).data (),
19f220c3
JK
4573 ada_array_bound (arr, i, 0),
4574 desc_bound_bitpos (bounds_type, i, 0),
4575 desc_bound_bitsize (bounds_type, i, 0));
50888e42
SM
4576 modify_field (value_type (bounds),
4577 value_contents_writeable (bounds).data (),
19f220c3
JK
4578 ada_array_bound (arr, i, 1),
4579 desc_bound_bitpos (bounds_type, i, 1),
4580 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4581 }
d2e4a39e 4582
40bc484c 4583 bounds = ensure_lval (bounds);
d2e4a39e 4584
19f220c3 4585 modify_field (value_type (descriptor),
50888e42 4586 value_contents_writeable (descriptor).data (),
19f220c3 4587 value_pointer (ensure_lval (arr),
940da03e 4588 desc_type->field (0).type ()),
19f220c3
JK
4589 fat_pntr_data_bitpos (desc_type),
4590 fat_pntr_data_bitsize (desc_type));
4591
4592 modify_field (value_type (descriptor),
50888e42 4593 value_contents_writeable (descriptor).data (),
19f220c3 4594 value_pointer (bounds,
940da03e 4595 desc_type->field (1).type ()),
19f220c3
JK
4596 fat_pntr_bounds_bitpos (desc_type),
4597 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4598
40bc484c 4599 descriptor = ensure_lval (descriptor);
14f9c5c9 4600
78134374 4601 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4602 return value_addr (descriptor);
4603 else
4604 return descriptor;
4605}
14f9c5c9 4606\f
dda83cd7 4607 /* Symbol Cache Module */
3d9434b5 4608
3d9434b5 4609/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4610 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4611 on the type of entity being printed, the cache can make it as much
4612 as an order of magnitude faster than without it.
4613
4614 The descriptive type DWARF extension has significantly reduced
4615 the need for this cache, at least when DWARF is being used. However,
4616 even in this case, some expensive name-based symbol searches are still
4617 sometimes necessary - to find an XVZ variable, mostly. */
4618
ee01b665
JB
4619/* Return the symbol cache associated to the given program space PSPACE.
4620 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4621
ee01b665
JB
4622static struct ada_symbol_cache *
4623ada_get_symbol_cache (struct program_space *pspace)
4624{
4625 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4626
bdcccc56
TT
4627 if (pspace_data->sym_cache == nullptr)
4628 pspace_data->sym_cache.reset (new ada_symbol_cache);
ee01b665 4629
bdcccc56 4630 return pspace_data->sym_cache.get ();
ee01b665 4631}
3d9434b5
JB
4632
4633/* Clear all entries from the symbol cache. */
4634
4635static void
bdcccc56 4636ada_clear_symbol_cache ()
3d9434b5 4637{
bdcccc56
TT
4638 struct ada_pspace_data *pspace_data
4639 = get_ada_pspace_data (current_program_space);
ee01b665 4640
bdcccc56
TT
4641 if (pspace_data->sym_cache != nullptr)
4642 pspace_data->sym_cache.reset ();
3d9434b5
JB
4643}
4644
fe978cb0 4645/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4646 Return it if found, or NULL otherwise. */
4647
4648static struct cache_entry **
fe978cb0 4649find_entry (const char *name, domain_enum domain)
3d9434b5 4650{
ee01b665
JB
4651 struct ada_symbol_cache *sym_cache
4652 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4653 int h = msymbol_hash (name) % HASH_SIZE;
4654 struct cache_entry **e;
4655
ee01b665 4656 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4657 {
fe978cb0 4658 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
dda83cd7 4659 return e;
3d9434b5
JB
4660 }
4661 return NULL;
4662}
4663
fe978cb0 4664/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4665 Return 1 if found, 0 otherwise.
4666
4667 If an entry was found and SYM is not NULL, set *SYM to the entry's
4668 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4669
96d887e8 4670static int
fe978cb0 4671lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4672 struct symbol **sym, const struct block **block)
96d887e8 4673{
fe978cb0 4674 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4675
4676 if (e == NULL)
4677 return 0;
4678 if (sym != NULL)
4679 *sym = (*e)->sym;
4680 if (block != NULL)
4681 *block = (*e)->block;
4682 return 1;
96d887e8
PH
4683}
4684
3d9434b5 4685/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4686 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4687
96d887e8 4688static void
fe978cb0 4689cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4690 const struct block *block)
96d887e8 4691{
ee01b665
JB
4692 struct ada_symbol_cache *sym_cache
4693 = ada_get_symbol_cache (current_program_space);
3d9434b5 4694 int h;
3d9434b5
JB
4695 struct cache_entry *e;
4696
1994afbf
DE
4697 /* Symbols for builtin types don't have a block.
4698 For now don't cache such symbols. */
7b3ecc75 4699 if (sym != NULL && !sym->is_objfile_owned ())
1994afbf
DE
4700 return;
4701
3d9434b5
JB
4702 /* If the symbol is a local symbol, then do not cache it, as a search
4703 for that symbol depends on the context. To determine whether
4704 the symbol is local or not, we check the block where we found it
4705 against the global and static blocks of its associated symtab. */
4706 if (sym
012cfab9 4707 && BLOCKVECTOR_BLOCK (symbol_symtab (sym)->blockvector (),
439247b6 4708 GLOBAL_BLOCK) != block
012cfab9 4709 && BLOCKVECTOR_BLOCK (symbol_symtab (sym)->blockvector (),
439247b6 4710 STATIC_BLOCK) != block)
3d9434b5
JB
4711 return;
4712
4713 h = msymbol_hash (name) % HASH_SIZE;
e39db4db 4714 e = XOBNEW (&sym_cache->cache_space, cache_entry);
ee01b665
JB
4715 e->next = sym_cache->root[h];
4716 sym_cache->root[h] = e;
2ef5453b 4717 e->name = obstack_strdup (&sym_cache->cache_space, name);
3d9434b5 4718 e->sym = sym;
fe978cb0 4719 e->domain = domain;
3d9434b5 4720 e->block = block;
96d887e8 4721}
4c4b4cd2 4722\f
dda83cd7 4723 /* Symbol Lookup */
4c4b4cd2 4724
b5ec771e
PA
4725/* Return the symbol name match type that should be used used when
4726 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4727
4728 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4729 for Ada lookups. */
c0431670 4730
b5ec771e
PA
4731static symbol_name_match_type
4732name_match_type_from_name (const char *lookup_name)
c0431670 4733{
b5ec771e
PA
4734 return (strstr (lookup_name, "__") == NULL
4735 ? symbol_name_match_type::WILD
4736 : symbol_name_match_type::FULL);
c0431670
JB
4737}
4738
4c4b4cd2
PH
4739/* Return the result of a standard (literal, C-like) lookup of NAME in
4740 given DOMAIN, visible from lexical block BLOCK. */
4741
4742static struct symbol *
4743standard_lookup (const char *name, const struct block *block,
dda83cd7 4744 domain_enum domain)
4c4b4cd2 4745{
acbd605d 4746 /* Initialize it just to avoid a GCC false warning. */
6640a367 4747 struct block_symbol sym = {};
4c4b4cd2 4748
d12307c1
PMR
4749 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4750 return sym.symbol;
a2cd4f14 4751 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4752 cache_symbol (name, domain, sym.symbol, sym.block);
4753 return sym.symbol;
4c4b4cd2
PH
4754}
4755
4756
4757/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4758 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4759 since they contend in overloading in the same way. */
4760static int
d1183b06 4761is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4762{
d1183b06 4763 for (const block_symbol &sym : syms)
5f9c5a63
SM
4764 if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
4765 && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
66d7f48f 4766 || sym.symbol->aclass () != LOC_CONST))
14f9c5c9
AS
4767 return 1;
4768
4769 return 0;
4770}
4771
4772/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4773 struct types. Otherwise, they may not. */
14f9c5c9
AS
4774
4775static int
d2e4a39e 4776equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4777{
d2e4a39e 4778 if (type0 == type1)
14f9c5c9 4779 return 1;
d2e4a39e 4780 if (type0 == NULL || type1 == NULL
78134374 4781 || type0->code () != type1->code ())
14f9c5c9 4782 return 0;
78134374
SM
4783 if ((type0->code () == TYPE_CODE_STRUCT
4784 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4785 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4786 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4787 return 1;
d2e4a39e 4788
14f9c5c9
AS
4789 return 0;
4790}
4791
4792/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4793 no more defined than that of SYM1. */
14f9c5c9
AS
4794
4795static int
d2e4a39e 4796lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4797{
4798 if (sym0 == sym1)
4799 return 1;
6c9c307c 4800 if (sym0->domain () != sym1->domain ()
66d7f48f 4801 || sym0->aclass () != sym1->aclass ())
14f9c5c9
AS
4802 return 0;
4803
66d7f48f 4804 switch (sym0->aclass ())
14f9c5c9
AS
4805 {
4806 case LOC_UNDEF:
4807 return 1;
4808 case LOC_TYPEDEF:
4809 {
5f9c5a63
SM
4810 struct type *type0 = sym0->type ();
4811 struct type *type1 = sym1->type ();
dda83cd7
SM
4812 const char *name0 = sym0->linkage_name ();
4813 const char *name1 = sym1->linkage_name ();
4814 int len0 = strlen (name0);
4815
4816 return
4817 type0->code () == type1->code ()
4818 && (equiv_types (type0, type1)
4819 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4820 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4821 }
4822 case LOC_CONST:
4823 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
5f9c5a63 4824 && equiv_types (sym0->type (), sym1->type ());
4b610737
TT
4825
4826 case LOC_STATIC:
4827 {
dda83cd7
SM
4828 const char *name0 = sym0->linkage_name ();
4829 const char *name1 = sym1->linkage_name ();
4830 return (strcmp (name0, name1) == 0
4831 && SYMBOL_VALUE_ADDRESS (sym0) == SYMBOL_VALUE_ADDRESS (sym1));
4b610737
TT
4832 }
4833
d2e4a39e
AS
4834 default:
4835 return 0;
14f9c5c9
AS
4836 }
4837}
4838
d1183b06
TT
4839/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4840 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4841
4842static void
d1183b06 4843add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4844 struct symbol *sym,
4845 const struct block *block)
14f9c5c9 4846{
529cad9c
PH
4847 /* Do not try to complete stub types, as the debugger is probably
4848 already scanning all symbols matching a certain name at the
4849 time when this function is called. Trying to replace the stub
4850 type by its associated full type will cause us to restart a scan
4851 which may lead to an infinite recursion. Instead, the client
4852 collecting the matching symbols will end up collecting several
4853 matches, with at least one of them complete. It can then filter
4854 out the stub ones if needed. */
4855
d1183b06 4856 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4857 {
d1183b06 4858 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4859 return;
d1183b06 4860 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4861 {
d1183b06
TT
4862 result[i].symbol = sym;
4863 result[i].block = block;
dda83cd7
SM
4864 return;
4865 }
4c4b4cd2
PH
4866 }
4867
d1183b06
TT
4868 struct block_symbol info;
4869 info.symbol = sym;
4870 info.block = block;
4871 result.push_back (info);
4c4b4cd2
PH
4872}
4873
7c7b6655
TT
4874/* Return a bound minimal symbol matching NAME according to Ada
4875 decoding rules. Returns an invalid symbol if there is no such
4876 minimal symbol. Names prefixed with "standard__" are handled
4877 specially: "standard__" is first stripped off, and only static and
4878 global symbols are searched. */
4c4b4cd2 4879
7c7b6655 4880struct bound_minimal_symbol
96d887e8 4881ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4882{
7c7b6655 4883 struct bound_minimal_symbol result;
4c4b4cd2 4884
b5ec771e
PA
4885 symbol_name_match_type match_type = name_match_type_from_name (name);
4886 lookup_name_info lookup_name (name, match_type);
4887
4888 symbol_name_matcher_ftype *match_name
4889 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4890
2030c079 4891 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4892 {
7932255d 4893 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf 4894 {
c9d95fa3 4895 if (match_name (msymbol->linkage_name (), lookup_name, NULL)
5325b9bf
TT
4896 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4897 {
4898 result.minsym = msymbol;
4899 result.objfile = objfile;
4900 break;
4901 }
4902 }
4903 }
4c4b4cd2 4904
7c7b6655 4905 return result;
96d887e8 4906}
4c4b4cd2 4907
96d887e8
PH
4908/* True if TYPE is definitely an artificial type supplied to a symbol
4909 for which no debugging information was given in the symbol file. */
14f9c5c9 4910
96d887e8
PH
4911static int
4912is_nondebugging_type (struct type *type)
4913{
0d5cff50 4914 const char *name = ada_type_name (type);
5b4ee69b 4915
96d887e8
PH
4916 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4917}
4c4b4cd2 4918
8f17729f
JB
4919/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4920 that are deemed "identical" for practical purposes.
4921
4922 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4923 types and that their number of enumerals is identical (in other
1f704f76 4924 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4925
4926static int
4927ada_identical_enum_types_p (struct type *type1, struct type *type2)
4928{
4929 int i;
4930
4931 /* The heuristic we use here is fairly conservative. We consider
4932 that 2 enumerate types are identical if they have the same
4933 number of enumerals and that all enumerals have the same
4934 underlying value and name. */
4935
4936 /* All enums in the type should have an identical underlying value. */
1f704f76 4937 for (i = 0; i < type1->num_fields (); i++)
970db518 4938 if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
8f17729f
JB
4939 return 0;
4940
4941 /* All enumerals should also have the same name (modulo any numerical
4942 suffix). */
1f704f76 4943 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4944 {
33d16dd9
SM
4945 const char *name_1 = type1->field (i).name ();
4946 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4947 int len_1 = strlen (name_1);
4948 int len_2 = strlen (name_2);
4949
33d16dd9
SM
4950 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4951 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4952 if (len_1 != len_2
33d16dd9
SM
4953 || strncmp (type1->field (i).name (),
4954 type2->field (i).name (),
8f17729f
JB
4955 len_1) != 0)
4956 return 0;
4957 }
4958
4959 return 1;
4960}
4961
4962/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4963 that are deemed "identical" for practical purposes. Sometimes,
4964 enumerals are not strictly identical, but their types are so similar
4965 that they can be considered identical.
4966
4967 For instance, consider the following code:
4968
4969 type Color is (Black, Red, Green, Blue, White);
4970 type RGB_Color is new Color range Red .. Blue;
4971
4972 Type RGB_Color is a subrange of an implicit type which is a copy
4973 of type Color. If we call that implicit type RGB_ColorB ("B" is
4974 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4975 As a result, when an expression references any of the enumeral
4976 by name (Eg. "print green"), the expression is technically
4977 ambiguous and the user should be asked to disambiguate. But
4978 doing so would only hinder the user, since it wouldn't matter
4979 what choice he makes, the outcome would always be the same.
4980 So, for practical purposes, we consider them as the same. */
4981
4982static int
54d343a2 4983symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4984{
4985 int i;
4986
4987 /* Before performing a thorough comparison check of each type,
4988 we perform a series of inexpensive checks. We expect that these
4989 checks will quickly fail in the vast majority of cases, and thus
4990 help prevent the unnecessary use of a more expensive comparison.
4991 Said comparison also expects us to make some of these checks
4992 (see ada_identical_enum_types_p). */
4993
4994 /* Quick check: All symbols should have an enum type. */
54d343a2 4995 for (i = 0; i < syms.size (); i++)
5f9c5a63 4996 if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
8f17729f
JB
4997 return 0;
4998
4999 /* Quick check: They should all have the same value. */
54d343a2 5000 for (i = 1; i < syms.size (); i++)
d12307c1 5001 if (SYMBOL_VALUE (syms[i].symbol) != SYMBOL_VALUE (syms[0].symbol))
8f17729f
JB
5002 return 0;
5003
5004 /* Quick check: They should all have the same number of enumerals. */
54d343a2 5005 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5006 if (syms[i].symbol->type ()->num_fields ()
5007 != syms[0].symbol->type ()->num_fields ())
8f17729f
JB
5008 return 0;
5009
5010 /* All the sanity checks passed, so we might have a set of
5011 identical enumeration types. Perform a more complete
5012 comparison of the type of each symbol. */
54d343a2 5013 for (i = 1; i < syms.size (); i++)
5f9c5a63
SM
5014 if (!ada_identical_enum_types_p (syms[i].symbol->type (),
5015 syms[0].symbol->type ()))
8f17729f
JB
5016 return 0;
5017
5018 return 1;
5019}
5020
54d343a2 5021/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5022 duplicate other symbols in the list (The only case I know of where
5023 this happens is when object files containing stabs-in-ecoff are
5024 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5025 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5026
d1183b06 5027static void
54d343a2 5028remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
5029{
5030 int i, j;
4c4b4cd2 5031
8f17729f
JB
5032 /* We should never be called with less than 2 symbols, as there
5033 cannot be any extra symbol in that case. But it's easy to
5034 handle, since we have nothing to do in that case. */
54d343a2 5035 if (syms->size () < 2)
d1183b06 5036 return;
8f17729f 5037
96d887e8 5038 i = 0;
54d343a2 5039 while (i < syms->size ())
96d887e8 5040 {
a35ddb44 5041 int remove_p = 0;
339c13b6
JB
5042
5043 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5044 the get rid of the stub. */
339c13b6 5045
5f9c5a63 5046 if ((*syms)[i].symbol->type ()->is_stub ()
dda83cd7
SM
5047 && (*syms)[i].symbol->linkage_name () != NULL)
5048 {
5049 for (j = 0; j < syms->size (); j++)
5050 {
5051 if (j != i
5f9c5a63 5052 && !(*syms)[j].symbol->type ()->is_stub ()
dda83cd7
SM
5053 && (*syms)[j].symbol->linkage_name () != NULL
5054 && strcmp ((*syms)[i].symbol->linkage_name (),
5055 (*syms)[j].symbol->linkage_name ()) == 0)
5056 remove_p = 1;
5057 }
5058 }
339c13b6
JB
5059
5060 /* Two symbols with the same name, same class and same address
dda83cd7 5061 should be identical. */
339c13b6 5062
987012b8 5063 else if ((*syms)[i].symbol->linkage_name () != NULL
66d7f48f 5064 && (*syms)[i].symbol->aclass () == LOC_STATIC
5f9c5a63 5065 && is_nondebugging_type ((*syms)[i].symbol->type ()))
dda83cd7
SM
5066 {
5067 for (j = 0; j < syms->size (); j += 1)
5068 {
5069 if (i != j
5070 && (*syms)[j].symbol->linkage_name () != NULL
5071 && strcmp ((*syms)[i].symbol->linkage_name (),
5072 (*syms)[j].symbol->linkage_name ()) == 0
66d7f48f
SM
5073 && ((*syms)[i].symbol->aclass ()
5074 == (*syms)[j].symbol->aclass ())
dda83cd7
SM
5075 && SYMBOL_VALUE_ADDRESS ((*syms)[i].symbol)
5076 == SYMBOL_VALUE_ADDRESS ((*syms)[j].symbol))
5077 remove_p = 1;
5078 }
5079 }
339c13b6 5080
a35ddb44 5081 if (remove_p)
54d343a2 5082 syms->erase (syms->begin () + i);
1b788fb6
TT
5083 else
5084 i += 1;
14f9c5c9 5085 }
8f17729f
JB
5086
5087 /* If all the remaining symbols are identical enumerals, then
5088 just keep the first one and discard the rest.
5089
5090 Unlike what we did previously, we do not discard any entry
5091 unless they are ALL identical. This is because the symbol
5092 comparison is not a strict comparison, but rather a practical
5093 comparison. If all symbols are considered identical, then
5094 we can just go ahead and use the first one and discard the rest.
5095 But if we cannot reduce the list to a single element, we have
5096 to ask the user to disambiguate anyways. And if we have to
5097 present a multiple-choice menu, it's less confusing if the list
5098 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
5099 if (symbols_are_identical_enums (*syms))
5100 syms->resize (1);
14f9c5c9
AS
5101}
5102
96d887e8
PH
5103/* Given a type that corresponds to a renaming entity, use the type name
5104 to extract the scope (package name or function name, fully qualified,
5105 and following the GNAT encoding convention) where this renaming has been
49d83361 5106 defined. */
4c4b4cd2 5107
49d83361 5108static std::string
96d887e8 5109xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5110{
96d887e8 5111 /* The renaming types adhere to the following convention:
0963b4bd 5112 <scope>__<rename>___<XR extension>.
96d887e8
PH
5113 So, to extract the scope, we search for the "___XR" extension,
5114 and then backtrack until we find the first "__". */
76a01679 5115
7d93a1e0 5116 const char *name = renaming_type->name ();
108d56a4
SM
5117 const char *suffix = strstr (name, "___XR");
5118 const char *last;
14f9c5c9 5119
96d887e8
PH
5120 /* Now, backtrack a bit until we find the first "__". Start looking
5121 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5122
96d887e8
PH
5123 for (last = suffix - 3; last > name; last--)
5124 if (last[0] == '_' && last[1] == '_')
5125 break;
76a01679 5126
96d887e8 5127 /* Make a copy of scope and return it. */
49d83361 5128 return std::string (name, last);
4c4b4cd2
PH
5129}
5130
96d887e8 5131/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5132
96d887e8
PH
5133static int
5134is_package_name (const char *name)
4c4b4cd2 5135{
96d887e8
PH
5136 /* Here, We take advantage of the fact that no symbols are generated
5137 for packages, while symbols are generated for each function.
5138 So the condition for NAME represent a package becomes equivalent
5139 to NAME not existing in our list of symbols. There is only one
5140 small complication with library-level functions (see below). */
4c4b4cd2 5141
96d887e8
PH
5142 /* If it is a function that has not been defined at library level,
5143 then we should be able to look it up in the symbols. */
5144 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5145 return 0;
14f9c5c9 5146
96d887e8
PH
5147 /* Library-level function names start with "_ada_". See if function
5148 "_ada_" followed by NAME can be found. */
14f9c5c9 5149
96d887e8 5150 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5151 functions names cannot contain "__" in them. */
96d887e8
PH
5152 if (strstr (name, "__") != NULL)
5153 return 0;
4c4b4cd2 5154
528e1572 5155 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5156
528e1572 5157 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5158}
14f9c5c9 5159
96d887e8 5160/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5161 not visible from FUNCTION_NAME. */
14f9c5c9 5162
96d887e8 5163static int
0d5cff50 5164old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5165{
66d7f48f 5166 if (sym->aclass () != LOC_TYPEDEF)
aeb5907d
JB
5167 return 0;
5168
5f9c5a63 5169 std::string scope = xget_renaming_scope (sym->type ());
14f9c5c9 5170
96d887e8 5171 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5172 if (is_package_name (scope.c_str ()))
5173 return 0;
14f9c5c9 5174
96d887e8
PH
5175 /* Check that the rename is in the current function scope by checking
5176 that its name starts with SCOPE. */
76a01679 5177
96d887e8
PH
5178 /* If the function name starts with "_ada_", it means that it is
5179 a library-level function. Strip this prefix before doing the
5180 comparison, as the encoding for the renaming does not contain
5181 this prefix. */
61012eef 5182 if (startswith (function_name, "_ada_"))
96d887e8 5183 function_name += 5;
f26caa11 5184
49d83361 5185 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5186}
5187
aeb5907d
JB
5188/* Remove entries from SYMS that corresponds to a renaming entity that
5189 is not visible from the function associated with CURRENT_BLOCK or
5190 that is superfluous due to the presence of more specific renaming
5191 information. Places surviving symbols in the initial entries of
d1183b06
TT
5192 SYMS.
5193
96d887e8 5194 Rationale:
aeb5907d
JB
5195 First, in cases where an object renaming is implemented as a
5196 reference variable, GNAT may produce both the actual reference
5197 variable and the renaming encoding. In this case, we discard the
5198 latter.
5199
5200 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5201 entity. Unfortunately, STABS currently does not support the definition
5202 of types that are local to a given lexical block, so all renamings types
5203 are emitted at library level. As a consequence, if an application
5204 contains two renaming entities using the same name, and a user tries to
5205 print the value of one of these entities, the result of the ada symbol
5206 lookup will also contain the wrong renaming type.
f26caa11 5207
96d887e8
PH
5208 This function partially covers for this limitation by attempting to
5209 remove from the SYMS list renaming symbols that should be visible
5210 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5211 method with the current information available. The implementation
5212 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5213
5214 - When the user tries to print a rename in a function while there
dda83cd7
SM
5215 is another rename entity defined in a package: Normally, the
5216 rename in the function has precedence over the rename in the
5217 package, so the latter should be removed from the list. This is
5218 currently not the case.
5219
96d887e8 5220 - This function will incorrectly remove valid renames if
dda83cd7
SM
5221 the CURRENT_BLOCK corresponds to a function which symbol name
5222 has been changed by an "Export" pragma. As a consequence,
5223 the user will be unable to print such rename entities. */
4c4b4cd2 5224
d1183b06 5225static void
54d343a2
TT
5226remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5227 const struct block *current_block)
4c4b4cd2
PH
5228{
5229 struct symbol *current_function;
0d5cff50 5230 const char *current_function_name;
4c4b4cd2 5231 int i;
aeb5907d
JB
5232 int is_new_style_renaming;
5233
5234 /* If there is both a renaming foo___XR... encoded as a variable and
5235 a simple variable foo in the same block, discard the latter.
0963b4bd 5236 First, zero out such symbols, then compress. */
aeb5907d 5237 is_new_style_renaming = 0;
54d343a2 5238 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5239 {
54d343a2
TT
5240 struct symbol *sym = (*syms)[i].symbol;
5241 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5242 const char *name;
5243 const char *suffix;
5244
66d7f48f 5245 if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
aeb5907d 5246 continue;
987012b8 5247 name = sym->linkage_name ();
aeb5907d
JB
5248 suffix = strstr (name, "___XR");
5249
5250 if (suffix != NULL)
5251 {
5252 int name_len = suffix - name;
5253 int j;
5b4ee69b 5254
aeb5907d 5255 is_new_style_renaming = 1;
54d343a2
TT
5256 for (j = 0; j < syms->size (); j += 1)
5257 if (i != j && (*syms)[j].symbol != NULL
987012b8 5258 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5259 name_len) == 0
54d343a2
TT
5260 && block == (*syms)[j].block)
5261 (*syms)[j].symbol = NULL;
aeb5907d
JB
5262 }
5263 }
5264 if (is_new_style_renaming)
5265 {
5266 int j, k;
5267
54d343a2
TT
5268 for (j = k = 0; j < syms->size (); j += 1)
5269 if ((*syms)[j].symbol != NULL)
aeb5907d 5270 {
54d343a2 5271 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5272 k += 1;
5273 }
d1183b06
TT
5274 syms->resize (k);
5275 return;
aeb5907d 5276 }
4c4b4cd2
PH
5277
5278 /* Extract the function name associated to CURRENT_BLOCK.
5279 Abort if unable to do so. */
76a01679 5280
4c4b4cd2 5281 if (current_block == NULL)
d1183b06 5282 return;
76a01679 5283
7f0df278 5284 current_function = block_linkage_function (current_block);
4c4b4cd2 5285 if (current_function == NULL)
d1183b06 5286 return;
4c4b4cd2 5287
987012b8 5288 current_function_name = current_function->linkage_name ();
4c4b4cd2 5289 if (current_function_name == NULL)
d1183b06 5290 return;
4c4b4cd2
PH
5291
5292 /* Check each of the symbols, and remove it from the list if it is
5293 a type corresponding to a renaming that is out of the scope of
5294 the current block. */
5295
5296 i = 0;
54d343a2 5297 while (i < syms->size ())
4c4b4cd2 5298 {
54d343a2 5299 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5300 == ADA_OBJECT_RENAMING
5301 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5302 current_function_name))
5303 syms->erase (syms->begin () + i);
4c4b4cd2 5304 else
dda83cd7 5305 i += 1;
4c4b4cd2 5306 }
4c4b4cd2
PH
5307}
5308
d1183b06 5309/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 5310 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 5311
cd458349 5312 Note: This function assumes that RESULT is empty. */
339c13b6
JB
5313
5314static void
d1183b06 5315ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5316 const lookup_name_info &lookup_name,
5317 const struct block *block, domain_enum domain)
339c13b6 5318{
339c13b6
JB
5319 while (block != NULL)
5320 {
d1183b06 5321 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5322
ba8694b6
TT
5323 /* If we found a non-function match, assume that's the one. We
5324 only check this when finding a function boundary, so that we
5325 can accumulate all results from intervening blocks first. */
5326 if (BLOCK_FUNCTION (block) != nullptr && is_nonfunction (result))
dda83cd7 5327 return;
339c13b6
JB
5328
5329 block = BLOCK_SUPERBLOCK (block);
5330 }
339c13b6
JB
5331}
5332
2315bb2d 5333/* An object of this type is used as the callback argument when
40658b94 5334 calling the map_matching_symbols method. */
ccefe4c4 5335
40658b94 5336struct match_data
ccefe4c4 5337{
1bfa81ac
TT
5338 explicit match_data (std::vector<struct block_symbol> *rp)
5339 : resultp (rp)
5340 {
5341 }
5342 DISABLE_COPY_AND_ASSIGN (match_data);
5343
2315bb2d
TT
5344 bool operator() (struct block_symbol *bsym);
5345
1bfa81ac 5346 struct objfile *objfile = nullptr;
d1183b06 5347 std::vector<struct block_symbol> *resultp;
1bfa81ac 5348 struct symbol *arg_sym = nullptr;
1178743e 5349 bool found_sym = false;
ccefe4c4
TT
5350};
5351
2315bb2d
TT
5352/* A callback for add_nonlocal_symbols that adds symbol, found in
5353 BSYM, to a list of symbols. */
ccefe4c4 5354
2315bb2d
TT
5355bool
5356match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5357{
199b4314
TT
5358 const struct block *block = bsym->block;
5359 struct symbol *sym = bsym->symbol;
5360
40658b94
PH
5361 if (sym == NULL)
5362 {
2315bb2d
TT
5363 if (!found_sym && arg_sym != NULL)
5364 add_defn_to_vec (*resultp,
5365 fixup_symbol_section (arg_sym, objfile),
40658b94 5366 block);
2315bb2d
TT
5367 found_sym = false;
5368 arg_sym = NULL;
40658b94
PH
5369 }
5370 else
5371 {
66d7f48f 5372 if (sym->aclass () == LOC_UNRESOLVED)
199b4314 5373 return true;
d9743061 5374 else if (sym->is_argument ())
2315bb2d 5375 arg_sym = sym;
40658b94
PH
5376 else
5377 {
2315bb2d
TT
5378 found_sym = true;
5379 add_defn_to_vec (*resultp,
5380 fixup_symbol_section (sym, objfile),
40658b94
PH
5381 block);
5382 }
5383 }
199b4314 5384 return true;
40658b94
PH
5385}
5386
b5ec771e
PA
5387/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5388 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5389 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5390
5391static int
d1183b06 5392ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5393 const struct block *block,
b5ec771e
PA
5394 const lookup_name_info &lookup_name,
5395 domain_enum domain)
22cee43f
PMR
5396{
5397 struct using_direct *renaming;
d1183b06 5398 int defns_mark = result.size ();
22cee43f 5399
b5ec771e
PA
5400 symbol_name_matcher_ftype *name_match
5401 = ada_get_symbol_name_matcher (lookup_name);
5402
22cee43f
PMR
5403 for (renaming = block_using (block);
5404 renaming != NULL;
5405 renaming = renaming->next)
5406 {
5407 const char *r_name;
22cee43f
PMR
5408
5409 /* Avoid infinite recursions: skip this renaming if we are actually
5410 already traversing it.
5411
5412 Currently, symbol lookup in Ada don't use the namespace machinery from
5413 C++/Fortran support: skip namespace imports that use them. */
5414 if (renaming->searched
5415 || (renaming->import_src != NULL
5416 && renaming->import_src[0] != '\0')
5417 || (renaming->import_dest != NULL
5418 && renaming->import_dest[0] != '\0'))
5419 continue;
5420 renaming->searched = 1;
5421
5422 /* TODO: here, we perform another name-based symbol lookup, which can
5423 pull its own multiple overloads. In theory, we should be able to do
5424 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5425 not a simple name. But in order to do this, we would need to enhance
5426 the DWARF reader to associate a symbol to this renaming, instead of a
5427 name. So, for now, we do something simpler: re-use the C++/Fortran
5428 namespace machinery. */
5429 r_name = (renaming->alias != NULL
5430 ? renaming->alias
5431 : renaming->declaration);
b5ec771e
PA
5432 if (name_match (r_name, lookup_name, NULL))
5433 {
5434 lookup_name_info decl_lookup_name (renaming->declaration,
5435 lookup_name.match_type ());
d1183b06 5436 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5437 1, NULL);
5438 }
22cee43f
PMR
5439 renaming->searched = 0;
5440 }
d1183b06 5441 return result.size () != defns_mark;
22cee43f
PMR
5442}
5443
db230ce3
JB
5444/* Implements compare_names, but only applying the comparision using
5445 the given CASING. */
5b4ee69b 5446
40658b94 5447static int
db230ce3
JB
5448compare_names_with_case (const char *string1, const char *string2,
5449 enum case_sensitivity casing)
40658b94
PH
5450{
5451 while (*string1 != '\0' && *string2 != '\0')
5452 {
db230ce3
JB
5453 char c1, c2;
5454
40658b94
PH
5455 if (isspace (*string1) || isspace (*string2))
5456 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5457
5458 if (casing == case_sensitive_off)
5459 {
5460 c1 = tolower (*string1);
5461 c2 = tolower (*string2);
5462 }
5463 else
5464 {
5465 c1 = *string1;
5466 c2 = *string2;
5467 }
5468 if (c1 != c2)
40658b94 5469 break;
db230ce3 5470
40658b94
PH
5471 string1 += 1;
5472 string2 += 1;
5473 }
db230ce3 5474
40658b94
PH
5475 switch (*string1)
5476 {
5477 case '(':
5478 return strcmp_iw_ordered (string1, string2);
5479 case '_':
5480 if (*string2 == '\0')
5481 {
052874e8 5482 if (is_name_suffix (string1))
40658b94
PH
5483 return 0;
5484 else
1a1d5513 5485 return 1;
40658b94 5486 }
dbb8534f 5487 /* FALLTHROUGH */
40658b94
PH
5488 default:
5489 if (*string2 == '(')
5490 return strcmp_iw_ordered (string1, string2);
5491 else
db230ce3
JB
5492 {
5493 if (casing == case_sensitive_off)
5494 return tolower (*string1) - tolower (*string2);
5495 else
5496 return *string1 - *string2;
5497 }
40658b94 5498 }
ccefe4c4
TT
5499}
5500
db230ce3
JB
5501/* Compare STRING1 to STRING2, with results as for strcmp.
5502 Compatible with strcmp_iw_ordered in that...
5503
5504 strcmp_iw_ordered (STRING1, STRING2) <= 0
5505
5506 ... implies...
5507
5508 compare_names (STRING1, STRING2) <= 0
5509
5510 (they may differ as to what symbols compare equal). */
5511
5512static int
5513compare_names (const char *string1, const char *string2)
5514{
5515 int result;
5516
5517 /* Similar to what strcmp_iw_ordered does, we need to perform
5518 a case-insensitive comparison first, and only resort to
5519 a second, case-sensitive, comparison if the first one was
5520 not sufficient to differentiate the two strings. */
5521
5522 result = compare_names_with_case (string1, string2, case_sensitive_off);
5523 if (result == 0)
5524 result = compare_names_with_case (string1, string2, case_sensitive_on);
5525
5526 return result;
5527}
5528
b5ec771e
PA
5529/* Convenience function to get at the Ada encoded lookup name for
5530 LOOKUP_NAME, as a C string. */
5531
5532static const char *
5533ada_lookup_name (const lookup_name_info &lookup_name)
5534{
5535 return lookup_name.ada ().lookup_name ().c_str ();
5536}
5537
0b7b2c2a
TT
5538/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5539 for OBJFILE, then walk the objfile's symtabs and update the
5540 results. */
5541
5542static void
5543map_matching_symbols (struct objfile *objfile,
5544 const lookup_name_info &lookup_name,
5545 bool is_wild_match,
5546 domain_enum domain,
5547 int global,
5548 match_data &data)
5549{
5550 data.objfile = objfile;
5551 objfile->expand_matching_symbols (lookup_name, domain, global,
5552 is_wild_match ? nullptr : compare_names);
5553
5554 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5555 for (compunit_symtab *symtab : objfile->compunits ())
5556 {
5557 const struct block *block
af39c5c8 5558 = BLOCKVECTOR_BLOCK (symtab->blockvector (), block_kind);
0b7b2c2a
TT
5559 if (!iterate_over_symbols_terminated (block, lookup_name,
5560 domain, data))
5561 break;
5562 }
5563}
5564
1bfa81ac 5565/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5566 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5567 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5568 symbols otherwise. */
339c13b6
JB
5569
5570static void
d1183b06 5571add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5572 const lookup_name_info &lookup_name,
5573 domain_enum domain, int global)
339c13b6 5574{
1bfa81ac 5575 struct match_data data (&result);
339c13b6 5576
b5ec771e
PA
5577 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5578
2030c079 5579 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5580 {
0b7b2c2a
TT
5581 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5582 global, data);
22cee43f 5583
b669c953 5584 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5585 {
5586 const struct block *global_block
af39c5c8 5587 = BLOCKVECTOR_BLOCK (cu->blockvector (), GLOBAL_BLOCK);
22cee43f 5588
d1183b06 5589 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5590 domain))
1178743e 5591 data.found_sym = true;
22cee43f 5592 }
40658b94
PH
5593 }
5594
d1183b06 5595 if (result.empty () && global && !is_wild_match)
40658b94 5596 {
b5ec771e 5597 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5598 std::string bracket_name = std::string ("<_ada_") + name + '>';
5599 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5600
2030c079 5601 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5602 map_matching_symbols (objfile, name1, false, domain, global, data);
5603 }
339c13b6
JB
5604}
5605
b5ec771e
PA
5606/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5607 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5608 returning the number of matches. Add these to RESULT.
4eeaa230 5609
22cee43f
PMR
5610 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5611 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5612 is the one match returned (no other matches in that or
d9680e73 5613 enclosing blocks is returned). If there are any matches in or
22cee43f 5614 surrounding BLOCK, then these alone are returned.
4eeaa230 5615
b5ec771e
PA
5616 Names prefixed with "standard__" are handled specially:
5617 "standard__" is first stripped off (by the lookup_name
5618 constructor), and only static and global symbols are searched.
14f9c5c9 5619
22cee43f
PMR
5620 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5621 to lookup global symbols. */
5622
5623static void
d1183b06 5624ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5625 const struct block *block,
b5ec771e 5626 const lookup_name_info &lookup_name,
22cee43f
PMR
5627 domain_enum domain,
5628 int full_search,
5629 int *made_global_lookup_p)
14f9c5c9
AS
5630{
5631 struct symbol *sym;
14f9c5c9 5632
22cee43f
PMR
5633 if (made_global_lookup_p)
5634 *made_global_lookup_p = 0;
339c13b6
JB
5635
5636 /* Special case: If the user specifies a symbol name inside package
5637 Standard, do a non-wild matching of the symbol name without
5638 the "standard__" prefix. This was primarily introduced in order
5639 to allow the user to specifically access the standard exceptions
5640 using, for instance, Standard.Constraint_Error when Constraint_Error
5641 is ambiguous (due to the user defining its own Constraint_Error
5642 entity inside its program). */
b5ec771e
PA
5643 if (lookup_name.ada ().standard_p ())
5644 block = NULL;
4c4b4cd2 5645
339c13b6 5646 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5647
4eeaa230
DE
5648 if (block != NULL)
5649 {
5650 if (full_search)
d1183b06 5651 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5652 else
5653 {
5654 /* In the !full_search case we're are being called by
4009ee92 5655 iterate_over_symbols, and we don't want to search
4eeaa230 5656 superblocks. */
d1183b06 5657 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5658 }
d1183b06 5659 if (!result.empty () || !full_search)
22cee43f 5660 return;
4eeaa230 5661 }
d2e4a39e 5662
339c13b6
JB
5663 /* No non-global symbols found. Check our cache to see if we have
5664 already performed this search before. If we have, then return
5665 the same result. */
5666
b5ec771e
PA
5667 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5668 domain, &sym, &block))
4c4b4cd2
PH
5669 {
5670 if (sym != NULL)
d1183b06 5671 add_defn_to_vec (result, sym, block);
22cee43f 5672 return;
4c4b4cd2 5673 }
14f9c5c9 5674
22cee43f
PMR
5675 if (made_global_lookup_p)
5676 *made_global_lookup_p = 1;
b1eedac9 5677
339c13b6
JB
5678 /* Search symbols from all global blocks. */
5679
d1183b06 5680 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5681
4c4b4cd2 5682 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5683 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5684
d1183b06
TT
5685 if (result.empty ())
5686 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5687}
5688
b5ec771e 5689/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5690 is non-zero, enclosing scope and in global scopes.
5691
5692 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5693 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5694
5695 When full_search is non-zero, any non-function/non-enumeral
5696 symbol match within the nest of blocks whose innermost member is BLOCK,
5697 is the one match returned (no other matches in that or
5698 enclosing blocks is returned). If there are any matches in or
5699 surrounding BLOCK, then these alone are returned.
5700
5701 Names prefixed with "standard__" are handled specially: "standard__"
5702 is first stripped off, and only static and global symbols are searched. */
5703
d1183b06 5704static std::vector<struct block_symbol>
b5ec771e
PA
5705ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5706 const struct block *block,
22cee43f 5707 domain_enum domain,
22cee43f
PMR
5708 int full_search)
5709{
22cee43f 5710 int syms_from_global_search;
d1183b06 5711 std::vector<struct block_symbol> results;
22cee43f 5712
d1183b06 5713 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5714 domain, full_search, &syms_from_global_search);
14f9c5c9 5715
d1183b06 5716 remove_extra_symbols (&results);
4c4b4cd2 5717
d1183b06 5718 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5719 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5720
d1183b06 5721 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5722 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5723 results[0].symbol, results[0].block);
ec6a20c2 5724
d1183b06
TT
5725 remove_irrelevant_renamings (&results, block);
5726 return results;
14f9c5c9
AS
5727}
5728
b5ec771e 5729/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5730 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5731
4eeaa230
DE
5732 See ada_lookup_symbol_list_worker for further details. */
5733
d1183b06 5734std::vector<struct block_symbol>
b5ec771e 5735ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5736 domain_enum domain)
4eeaa230 5737{
b5ec771e
PA
5738 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5739 lookup_name_info lookup_name (name, name_match_type);
5740
d1183b06 5741 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5742}
5743
4e5c77fe
JB
5744/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5745 to 1, but choosing the first symbol found if there are multiple
5746 choices.
5747
5e2336be
JB
5748 The result is stored in *INFO, which must be non-NULL.
5749 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5750
5751void
5752ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5753 domain_enum domain,
d12307c1 5754 struct block_symbol *info)
14f9c5c9 5755{
b5ec771e
PA
5756 /* Since we already have an encoded name, wrap it in '<>' to force a
5757 verbatim match. Otherwise, if the name happens to not look like
5758 an encoded name (because it doesn't include a "__"),
5759 ada_lookup_name_info would re-encode/fold it again, and that
5760 would e.g., incorrectly lowercase object renaming names like
5761 "R28b" -> "r28b". */
12932e2c 5762 std::string verbatim = add_angle_brackets (name);
b5ec771e 5763
5e2336be 5764 gdb_assert (info != NULL);
65392b3e 5765 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5766}
aeb5907d
JB
5767
5768/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5769 scope and in global scopes, or NULL if none. NAME is folded and
5770 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5771 choosing the first symbol if there are multiple choices. */
4e5c77fe 5772
d12307c1 5773struct block_symbol
aeb5907d 5774ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5775 domain_enum domain)
aeb5907d 5776{
d1183b06
TT
5777 std::vector<struct block_symbol> candidates
5778 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5779
d1183b06 5780 if (candidates.empty ())
54d343a2 5781 return {};
f98fc17b
PA
5782
5783 block_symbol info = candidates[0];
5784 info.symbol = fixup_symbol_section (info.symbol, NULL);
d12307c1 5785 return info;
4c4b4cd2 5786}
14f9c5c9 5787
14f9c5c9 5788
4c4b4cd2
PH
5789/* True iff STR is a possible encoded suffix of a normal Ada name
5790 that is to be ignored for matching purposes. Suffixes of parallel
5791 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5792 are given by any of the regular expressions:
4c4b4cd2 5793
babe1480
JB
5794 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5795 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5796 TKB [subprogram suffix for task bodies]
babe1480 5797 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5798 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5799
5800 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5801 match is performed. This sequence is used to differentiate homonyms,
5802 is an optional part of a valid name suffix. */
4c4b4cd2 5803
14f9c5c9 5804static int
d2e4a39e 5805is_name_suffix (const char *str)
14f9c5c9
AS
5806{
5807 int k;
4c4b4cd2
PH
5808 const char *matching;
5809 const int len = strlen (str);
5810
babe1480
JB
5811 /* Skip optional leading __[0-9]+. */
5812
4c4b4cd2
PH
5813 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5814 {
babe1480
JB
5815 str += 3;
5816 while (isdigit (str[0]))
dda83cd7 5817 str += 1;
4c4b4cd2 5818 }
babe1480
JB
5819
5820 /* [.$][0-9]+ */
4c4b4cd2 5821
babe1480 5822 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5823 {
babe1480 5824 matching = str + 1;
4c4b4cd2 5825 while (isdigit (matching[0]))
dda83cd7 5826 matching += 1;
4c4b4cd2 5827 if (matching[0] == '\0')
dda83cd7 5828 return 1;
4c4b4cd2
PH
5829 }
5830
5831 /* ___[0-9]+ */
babe1480 5832
4c4b4cd2
PH
5833 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5834 {
5835 matching = str + 3;
5836 while (isdigit (matching[0]))
dda83cd7 5837 matching += 1;
4c4b4cd2 5838 if (matching[0] == '\0')
dda83cd7 5839 return 1;
4c4b4cd2
PH
5840 }
5841
9ac7f98e
JB
5842 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5843
5844 if (strcmp (str, "TKB") == 0)
5845 return 1;
5846
529cad9c
PH
5847#if 0
5848 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5849 with a N at the end. Unfortunately, the compiler uses the same
5850 convention for other internal types it creates. So treating
529cad9c 5851 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5852 some regressions. For instance, consider the case of an enumerated
5853 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5854 name ends with N.
5855 Having a single character like this as a suffix carrying some
0963b4bd 5856 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5857 to be something like "_N" instead. In the meantime, do not do
5858 the following check. */
5859 /* Protected Object Subprograms */
5860 if (len == 1 && str [0] == 'N')
5861 return 1;
5862#endif
5863
5864 /* _E[0-9]+[bs]$ */
5865 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5866 {
5867 matching = str + 3;
5868 while (isdigit (matching[0]))
dda83cd7 5869 matching += 1;
529cad9c 5870 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5871 && matching [1] == '\0')
5872 return 1;
529cad9c
PH
5873 }
5874
4c4b4cd2
PH
5875 /* ??? We should not modify STR directly, as we are doing below. This
5876 is fine in this case, but may become problematic later if we find
5877 that this alternative did not work, and want to try matching
5878 another one from the begining of STR. Since we modified it, we
5879 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5880 if (str[0] == 'X')
5881 {
5882 str += 1;
d2e4a39e 5883 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5884 {
5885 if (str[0] != 'n' && str[0] != 'b')
5886 return 0;
5887 str += 1;
5888 }
14f9c5c9 5889 }
babe1480 5890
14f9c5c9
AS
5891 if (str[0] == '\000')
5892 return 1;
babe1480 5893
d2e4a39e 5894 if (str[0] == '_')
14f9c5c9
AS
5895 {
5896 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5897 return 0;
d2e4a39e 5898 if (str[2] == '_')
dda83cd7
SM
5899 {
5900 if (strcmp (str + 3, "JM") == 0)
5901 return 1;
5902 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5903 the LJM suffix in favor of the JM one. But we will
5904 still accept LJM as a valid suffix for a reasonable
5905 amount of time, just to allow ourselves to debug programs
5906 compiled using an older version of GNAT. */
5907 if (strcmp (str + 3, "LJM") == 0)
5908 return 1;
5909 if (str[3] != 'X')
5910 return 0;
5911 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5912 || str[4] == 'U' || str[4] == 'P')
5913 return 1;
5914 if (str[4] == 'R' && str[5] != 'T')
5915 return 1;
5916 return 0;
5917 }
4c4b4cd2 5918 if (!isdigit (str[2]))
dda83cd7 5919 return 0;
4c4b4cd2 5920 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5921 if (!isdigit (str[k]) && str[k] != '_')
5922 return 0;
14f9c5c9
AS
5923 return 1;
5924 }
4c4b4cd2 5925 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5926 {
4c4b4cd2 5927 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5928 if (!isdigit (str[k]) && str[k] != '_')
5929 return 0;
14f9c5c9
AS
5930 return 1;
5931 }
5932 return 0;
5933}
d2e4a39e 5934
aeb5907d
JB
5935/* Return non-zero if the string starting at NAME and ending before
5936 NAME_END contains no capital letters. */
529cad9c
PH
5937
5938static int
5939is_valid_name_for_wild_match (const char *name0)
5940{
f945dedf 5941 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5942 int i;
5943
5823c3ef
JB
5944 /* If the decoded name starts with an angle bracket, it means that
5945 NAME0 does not follow the GNAT encoding format. It should then
5946 not be allowed as a possible wild match. */
5947 if (decoded_name[0] == '<')
5948 return 0;
5949
529cad9c
PH
5950 for (i=0; decoded_name[i] != '\0'; i++)
5951 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5952 return 0;
5953
5954 return 1;
5955}
5956
59c8a30b
JB
5957/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5958 character which could start a simple name. Assumes that *NAMEP points
5959 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5960
14f9c5c9 5961static int
59c8a30b 5962advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5963{
73589123 5964 const char *name = *namep;
5b4ee69b 5965
5823c3ef 5966 while (1)
14f9c5c9 5967 {
59c8a30b 5968 char t0, t1;
73589123
PH
5969
5970 t0 = *name;
5971 if (t0 == '_')
5972 {
5973 t1 = name[1];
5974 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5975 {
5976 name += 1;
61012eef 5977 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5978 break;
5979 else
5980 name += 1;
5981 }
aa27d0b3
JB
5982 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5983 || name[2] == target0))
73589123
PH
5984 {
5985 name += 2;
5986 break;
5987 }
86b44259
TT
5988 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5989 {
5990 /* Names like "pkg__B_N__name", where N is a number, are
5991 block-local. We can handle these by simply skipping
5992 the "B_" here. */
5993 name += 4;
5994 }
73589123
PH
5995 else
5996 return 0;
5997 }
5998 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5999 name += 1;
6000 else
5823c3ef 6001 return 0;
73589123
PH
6002 }
6003
6004 *namep = name;
6005 return 1;
6006}
6007
b5ec771e
PA
6008/* Return true iff NAME encodes a name of the form prefix.PATN.
6009 Ignores any informational suffixes of NAME (i.e., for which
6010 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6011 simple name. */
73589123 6012
b5ec771e 6013static bool
73589123
PH
6014wild_match (const char *name, const char *patn)
6015{
22e048c9 6016 const char *p;
73589123
PH
6017 const char *name0 = name;
6018
6019 while (1)
6020 {
6021 const char *match = name;
6022
6023 if (*name == *patn)
6024 {
6025 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6026 if (*p != *name)
6027 break;
6028 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6029 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6030
6031 if (name[-1] == '_')
6032 name -= 1;
6033 }
6034 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6035 return false;
96d887e8 6036 }
96d887e8
PH
6037}
6038
d1183b06 6039/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6040 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6041
6042static void
d1183b06 6043ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6044 const struct block *block,
6045 const lookup_name_info &lookup_name,
6046 domain_enum domain, struct objfile *objfile)
96d887e8 6047{
8157b174 6048 struct block_iterator iter;
96d887e8
PH
6049 /* A matching argument symbol, if any. */
6050 struct symbol *arg_sym;
6051 /* Set true when we find a matching non-argument symbol. */
1178743e 6052 bool found_sym;
96d887e8
PH
6053 struct symbol *sym;
6054
6055 arg_sym = NULL;
1178743e 6056 found_sym = false;
b5ec771e
PA
6057 for (sym = block_iter_match_first (block, lookup_name, &iter);
6058 sym != NULL;
6059 sym = block_iter_match_next (lookup_name, &iter))
96d887e8 6060 {
6c9c307c 6061 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
b5ec771e 6062 {
66d7f48f 6063 if (sym->aclass () != LOC_UNRESOLVED)
b5ec771e 6064 {
d9743061 6065 if (sym->is_argument ())
b5ec771e
PA
6066 arg_sym = sym;
6067 else
6068 {
1178743e 6069 found_sym = true;
d1183b06 6070 add_defn_to_vec (result,
b5ec771e
PA
6071 fixup_symbol_section (sym, objfile),
6072 block);
6073 }
6074 }
6075 }
96d887e8
PH
6076 }
6077
22cee43f
PMR
6078 /* Handle renamings. */
6079
d1183b06 6080 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6081 found_sym = true;
22cee43f 6082
96d887e8
PH
6083 if (!found_sym && arg_sym != NULL)
6084 {
d1183b06 6085 add_defn_to_vec (result,
dda83cd7
SM
6086 fixup_symbol_section (arg_sym, objfile),
6087 block);
96d887e8
PH
6088 }
6089
b5ec771e 6090 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6091 {
6092 arg_sym = NULL;
1178743e 6093 found_sym = false;
b5ec771e
PA
6094 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6095 const char *name = ada_lookup_name.c_str ();
6096 size_t name_len = ada_lookup_name.size ();
96d887e8
PH
6097
6098 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 6099 {
dda83cd7 6100 if (symbol_matches_domain (sym->language (),
6c9c307c 6101 sym->domain (), domain))
dda83cd7
SM
6102 {
6103 int cmp;
6104
6105 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6106 if (cmp == 0)
6107 {
6108 cmp = !startswith (sym->linkage_name (), "_ada_");
6109 if (cmp == 0)
6110 cmp = strncmp (name, sym->linkage_name () + 5,
6111 name_len);
6112 }
6113
6114 if (cmp == 0
6115 && is_name_suffix (sym->linkage_name () + name_len + 5))
6116 {
66d7f48f 6117 if (sym->aclass () != LOC_UNRESOLVED)
2a2d4dc3 6118 {
d9743061 6119 if (sym->is_argument ())
2a2d4dc3
AS
6120 arg_sym = sym;
6121 else
6122 {
1178743e 6123 found_sym = true;
d1183b06 6124 add_defn_to_vec (result,
2a2d4dc3
AS
6125 fixup_symbol_section (sym, objfile),
6126 block);
6127 }
6128 }
dda83cd7
SM
6129 }
6130 }
76a01679 6131 }
96d887e8
PH
6132
6133 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6134 They aren't parameters, right? */
96d887e8 6135 if (!found_sym && arg_sym != NULL)
dda83cd7 6136 {
d1183b06 6137 add_defn_to_vec (result,
dda83cd7
SM
6138 fixup_symbol_section (arg_sym, objfile),
6139 block);
6140 }
96d887e8
PH
6141 }
6142}
6143\f
41d27058 6144
dda83cd7 6145 /* Symbol Completion */
41d27058 6146
b5ec771e 6147/* See symtab.h. */
41d27058 6148
b5ec771e
PA
6149bool
6150ada_lookup_name_info::matches
6151 (const char *sym_name,
6152 symbol_name_match_type match_type,
a207cff2 6153 completion_match_result *comp_match_res) const
41d27058 6154{
b5ec771e
PA
6155 bool match = false;
6156 const char *text = m_encoded_name.c_str ();
6157 size_t text_len = m_encoded_name.size ();
41d27058
JB
6158
6159 /* First, test against the fully qualified name of the symbol. */
6160
6161 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6162 match = true;
41d27058 6163
f945dedf 6164 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6165 if (match && !m_encoded_p)
41d27058
JB
6166 {
6167 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6168 that iff we are doing a verbatim match, the decoded version
6169 of the symbol name starts with '<'. Otherwise, this symbol name
6170 is not a suitable completion. */
41d27058 6171
f945dedf 6172 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6173 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6174 }
6175
b5ec771e 6176 if (match && !m_verbatim_p)
41d27058
JB
6177 {
6178 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6179 be done is to verify that the potentially matching symbol name
6180 does not include capital letters, because the ada-mode would
6181 not be able to understand these symbol names without the
6182 angle bracket notation. */
41d27058
JB
6183 const char *tmp;
6184
6185 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6186 if (*tmp != '\0')
b5ec771e 6187 match = false;
41d27058
JB
6188 }
6189
6190 /* Second: Try wild matching... */
6191
b5ec771e 6192 if (!match && m_wild_match_p)
41d27058
JB
6193 {
6194 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6195 may represent an unqualified symbol name. We therefore must
6196 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6197 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6198
6199 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6200 match = true;
41d27058
JB
6201 }
6202
b5ec771e 6203 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6204
6205 if (!match)
b5ec771e 6206 return false;
41d27058 6207
a207cff2 6208 if (comp_match_res != NULL)
b5ec771e 6209 {
a207cff2 6210 std::string &match_str = comp_match_res->match.storage ();
41d27058 6211
b5ec771e 6212 if (!m_encoded_p)
a207cff2 6213 match_str = ada_decode (sym_name);
b5ec771e
PA
6214 else
6215 {
6216 if (m_verbatim_p)
6217 match_str = add_angle_brackets (sym_name);
6218 else
6219 match_str = sym_name;
41d27058 6220
b5ec771e 6221 }
a207cff2
PA
6222
6223 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6224 }
6225
b5ec771e 6226 return true;
41d27058
JB
6227}
6228
dda83cd7 6229 /* Field Access */
96d887e8 6230
73fb9985
JB
6231/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6232 for tagged types. */
6233
6234static int
6235ada_is_dispatch_table_ptr_type (struct type *type)
6236{
0d5cff50 6237 const char *name;
73fb9985 6238
78134374 6239 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6240 return 0;
6241
7d93a1e0 6242 name = TYPE_TARGET_TYPE (type)->name ();
73fb9985
JB
6243 if (name == NULL)
6244 return 0;
6245
6246 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6247}
6248
ac4a2da4
JG
6249/* Return non-zero if TYPE is an interface tag. */
6250
6251static int
6252ada_is_interface_tag (struct type *type)
6253{
7d93a1e0 6254 const char *name = type->name ();
ac4a2da4
JG
6255
6256 if (name == NULL)
6257 return 0;
6258
6259 return (strcmp (name, "ada__tags__interface_tag") == 0);
6260}
6261
963a6417
PH
6262/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6263 to be invisible to users. */
96d887e8 6264
963a6417
PH
6265int
6266ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6267{
1f704f76 6268 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6269 return 1;
ffde82bf 6270
73fb9985
JB
6271 /* Check the name of that field. */
6272 {
33d16dd9 6273 const char *name = type->field (field_num).name ();
73fb9985
JB
6274
6275 /* Anonymous field names should not be printed.
6276 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6277 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6278 if (name == NULL)
6279 return 1;
6280
ffde82bf
JB
6281 /* Normally, fields whose name start with an underscore ("_")
6282 are fields that have been internally generated by the compiler,
6283 and thus should not be printed. The "_parent" field is special,
6284 however: This is a field internally generated by the compiler
6285 for tagged types, and it contains the components inherited from
6286 the parent type. This field should not be printed as is, but
6287 should not be ignored either. */
61012eef 6288 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985
JB
6289 return 1;
6290 }
6291
ac4a2da4
JG
6292 /* If this is the dispatch table of a tagged type or an interface tag,
6293 then ignore. */
73fb9985 6294 if (ada_is_tagged_type (type, 1)
940da03e
SM
6295 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6296 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6297 return 1;
6298
6299 /* Not a special field, so it should not be ignored. */
6300 return 0;
963a6417 6301}
96d887e8 6302
963a6417 6303/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6304 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6305
963a6417
PH
6306int
6307ada_is_tagged_type (struct type *type, int refok)
6308{
988f6b3d 6309 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6310}
96d887e8 6311
963a6417 6312/* True iff TYPE represents the type of X'Tag */
96d887e8 6313
963a6417
PH
6314int
6315ada_is_tag_type (struct type *type)
6316{
460efde1
JB
6317 type = ada_check_typedef (type);
6318
78134374 6319 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6320 return 0;
6321 else
96d887e8 6322 {
963a6417 6323 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 6324
963a6417 6325 return (name != NULL
dda83cd7 6326 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6327 }
96d887e8
PH
6328}
6329
963a6417 6330/* The type of the tag on VAL. */
76a01679 6331
de93309a 6332static struct type *
963a6417 6333ada_tag_type (struct value *val)
96d887e8 6334{
988f6b3d 6335 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0);
963a6417 6336}
96d887e8 6337
b50d69b5
JG
6338/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6339 retired at Ada 05). */
6340
6341static int
6342is_ada95_tag (struct value *tag)
6343{
6344 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6345}
6346
963a6417 6347/* The value of the tag on VAL. */
96d887e8 6348
de93309a 6349static struct value *
963a6417
PH
6350ada_value_tag (struct value *val)
6351{
03ee6b2e 6352 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6353}
6354
963a6417
PH
6355/* The value of the tag on the object of type TYPE whose contents are
6356 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6357 ADDRESS. */
96d887e8 6358
963a6417 6359static struct value *
10a2c479 6360value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6361 const gdb_byte *valaddr,
dda83cd7 6362 CORE_ADDR address)
96d887e8 6363{
b5385fc0 6364 int tag_byte_offset;
963a6417 6365 struct type *tag_type;
5b4ee69b 6366
4d1795ac
TT
6367 gdb::array_view<const gdb_byte> contents;
6368 if (valaddr != nullptr)
6369 contents = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
6370 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6371 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6372 NULL, NULL, NULL))
96d887e8 6373 {
fc1a4b47 6374 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6375 ? NULL
6376 : valaddr + tag_byte_offset);
963a6417 6377 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6378
963a6417 6379 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6380 }
963a6417
PH
6381 return NULL;
6382}
96d887e8 6383
963a6417
PH
6384static struct type *
6385type_from_tag (struct value *tag)
6386{
f5272a3b 6387 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6388
963a6417 6389 if (type_name != NULL)
5c4258f4 6390 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6391 return NULL;
6392}
96d887e8 6393
b50d69b5
JG
6394/* Given a value OBJ of a tagged type, return a value of this
6395 type at the base address of the object. The base address, as
6396 defined in Ada.Tags, it is the address of the primary tag of
6397 the object, and therefore where the field values of its full
6398 view can be fetched. */
6399
6400struct value *
6401ada_tag_value_at_base_address (struct value *obj)
6402{
b50d69b5
JG
6403 struct value *val;
6404 LONGEST offset_to_top = 0;
6405 struct type *ptr_type, *obj_type;
6406 struct value *tag;
6407 CORE_ADDR base_address;
6408
6409 obj_type = value_type (obj);
6410
6411 /* It is the responsability of the caller to deref pointers. */
6412
78134374 6413 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6414 return obj;
6415
6416 tag = ada_value_tag (obj);
6417 if (!tag)
6418 return obj;
6419
6420 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6421
6422 if (is_ada95_tag (tag))
6423 return obj;
6424
08f49010
XR
6425 ptr_type = language_lookup_primitive_type
6426 (language_def (language_ada), target_gdbarch(), "storage_offset");
b50d69b5
JG
6427 ptr_type = lookup_pointer_type (ptr_type);
6428 val = value_cast (ptr_type, tag);
6429 if (!val)
6430 return obj;
6431
6432 /* It is perfectly possible that an exception be raised while
6433 trying to determine the base address, just like for the tag;
6434 see ada_tag_name for more details. We do not print the error
6435 message for the same reason. */
6436
a70b8144 6437 try
b50d69b5
JG
6438 {
6439 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6440 }
6441
230d2906 6442 catch (const gdb_exception_error &e)
492d29ea
PA
6443 {
6444 return obj;
6445 }
b50d69b5
JG
6446
6447 /* If offset is null, nothing to do. */
6448
6449 if (offset_to_top == 0)
6450 return obj;
6451
6452 /* -1 is a special case in Ada.Tags; however, what should be done
6453 is not quite clear from the documentation. So do nothing for
6454 now. */
6455
6456 if (offset_to_top == -1)
6457 return obj;
6458
08f49010
XR
6459 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6460 from the base address. This was however incompatible with
6461 C++ dispatch table: C++ uses a *negative* value to *add*
6462 to the base address. Ada's convention has therefore been
6463 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6464 use the same convention. Here, we support both cases by
6465 checking the sign of OFFSET_TO_TOP. */
6466
6467 if (offset_to_top > 0)
6468 offset_to_top = -offset_to_top;
6469
6470 base_address = value_address (obj) + offset_to_top;
b50d69b5
JG
6471 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6472
6473 /* Make sure that we have a proper tag at the new address.
6474 Otherwise, offset_to_top is bogus (which can happen when
6475 the object is not initialized yet). */
6476
6477 if (!tag)
6478 return obj;
6479
6480 obj_type = type_from_tag (tag);
6481
6482 if (!obj_type)
6483 return obj;
6484
6485 return value_from_contents_and_address (obj_type, NULL, base_address);
6486}
6487
1b611343
JB
6488/* Return the "ada__tags__type_specific_data" type. */
6489
6490static struct type *
6491ada_get_tsd_type (struct inferior *inf)
963a6417 6492{
1b611343 6493 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6494
1b611343
JB
6495 if (data->tsd_type == 0)
6496 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6497 return data->tsd_type;
6498}
529cad9c 6499
1b611343
JB
6500/* Return the TSD (type-specific data) associated to the given TAG.
6501 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6502
1b611343 6503 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6504
1b611343
JB
6505static struct value *
6506ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6507{
4c4b4cd2 6508 struct value *val;
1b611343 6509 struct type *type;
5b4ee69b 6510
1b611343
JB
6511 /* First option: The TSD is simply stored as a field of our TAG.
6512 Only older versions of GNAT would use this format, but we have
6513 to test it first, because there are no visible markers for
6514 the current approach except the absence of that field. */
529cad9c 6515
1b611343
JB
6516 val = ada_value_struct_elt (tag, "tsd", 1);
6517 if (val)
6518 return val;
e802dbe0 6519
1b611343
JB
6520 /* Try the second representation for the dispatch table (in which
6521 there is no explicit 'tsd' field in the referent of the tag pointer,
6522 and instead the tsd pointer is stored just before the dispatch
6523 table. */
e802dbe0 6524
1b611343
JB
6525 type = ada_get_tsd_type (current_inferior());
6526 if (type == NULL)
6527 return NULL;
6528 type = lookup_pointer_type (lookup_pointer_type (type));
6529 val = value_cast (type, tag);
6530 if (val == NULL)
6531 return NULL;
6532 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6533}
6534
1b611343
JB
6535/* Given the TSD of a tag (type-specific data), return a string
6536 containing the name of the associated type.
6537
f5272a3b 6538 May return NULL if we are unable to determine the tag name. */
1b611343 6539
f5272a3b 6540static gdb::unique_xmalloc_ptr<char>
1b611343 6541ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6542{
1b611343 6543 struct value *val;
529cad9c 6544
1b611343 6545 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6546 if (val == NULL)
1b611343 6547 return NULL;
66920317
TT
6548 gdb::unique_xmalloc_ptr<char> buffer
6549 = target_read_string (value_as_address (val), INT_MAX);
6550 if (buffer == nullptr)
f5272a3b
TT
6551 return nullptr;
6552
315e4ebb 6553 try
f5272a3b 6554 {
315e4ebb
TT
6555 /* Let this throw an exception on error. If the data is
6556 uninitialized, we'd rather not have the user see a
6557 warning. */
6558 const char *folded = ada_fold_name (buffer.get (), true);
6559 return make_unique_xstrdup (folded);
6560 }
6561 catch (const gdb_exception &)
6562 {
6563 return nullptr;
f5272a3b 6564 }
4c4b4cd2
PH
6565}
6566
6567/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6568 a C string.
6569
6570 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6571 determine the name of that tag. */
4c4b4cd2 6572
f5272a3b 6573gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6574ada_tag_name (struct value *tag)
6575{
f5272a3b 6576 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6577
df407dfe 6578 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 6579 return NULL;
1b611343
JB
6580
6581 /* It is perfectly possible that an exception be raised while trying
6582 to determine the TAG's name, even under normal circumstances:
6583 The associated variable may be uninitialized or corrupted, for
6584 instance. We do not let any exception propagate past this point.
6585 instead we return NULL.
6586
6587 We also do not print the error message either (which often is very
6588 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6589 the caller print a more meaningful message if necessary. */
a70b8144 6590 try
1b611343
JB
6591 {
6592 struct value *tsd = ada_get_tsd_from_tag (tag);
6593
6594 if (tsd != NULL)
6595 name = ada_tag_name_from_tsd (tsd);
6596 }
230d2906 6597 catch (const gdb_exception_error &e)
492d29ea
PA
6598 {
6599 }
1b611343
JB
6600
6601 return name;
4c4b4cd2
PH
6602}
6603
6604/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6605
d2e4a39e 6606struct type *
ebf56fd3 6607ada_parent_type (struct type *type)
14f9c5c9
AS
6608{
6609 int i;
6610
61ee279c 6611 type = ada_check_typedef (type);
14f9c5c9 6612
78134374 6613 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6614 return NULL;
6615
1f704f76 6616 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6617 if (ada_is_parent_field (type, i))
0c1f74cf 6618 {
dda83cd7 6619 struct type *parent_type = type->field (i).type ();
0c1f74cf 6620
dda83cd7
SM
6621 /* If the _parent field is a pointer, then dereference it. */
6622 if (parent_type->code () == TYPE_CODE_PTR)
6623 parent_type = TYPE_TARGET_TYPE (parent_type);
6624 /* If there is a parallel XVS type, get the actual base type. */
6625 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6626
dda83cd7 6627 return ada_check_typedef (parent_type);
0c1f74cf 6628 }
14f9c5c9
AS
6629
6630 return NULL;
6631}
6632
4c4b4cd2
PH
6633/* True iff field number FIELD_NUM of structure type TYPE contains the
6634 parent-type (inherited) fields of a derived type. Assumes TYPE is
6635 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6636
6637int
ebf56fd3 6638ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6639{
33d16dd9 6640 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6641
4c4b4cd2 6642 return (name != NULL
dda83cd7
SM
6643 && (startswith (name, "PARENT")
6644 || startswith (name, "_parent")));
14f9c5c9
AS
6645}
6646
4c4b4cd2 6647/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6648 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6649 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6650 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6651 structures. */
14f9c5c9
AS
6652
6653int
ebf56fd3 6654ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6655{
33d16dd9 6656 const char *name = type->field (field_num).name ();
5b4ee69b 6657
dddc0e16
JB
6658 if (name != NULL && strcmp (name, "RETVAL") == 0)
6659 {
6660 /* This happens in functions with "out" or "in out" parameters
6661 which are passed by copy. For such functions, GNAT describes
6662 the function's return type as being a struct where the return
6663 value is in a field called RETVAL, and where the other "out"
6664 or "in out" parameters are fields of that struct. This is not
6665 a wrapper. */
6666 return 0;
6667 }
6668
d2e4a39e 6669 return (name != NULL
dda83cd7
SM
6670 && (startswith (name, "PARENT")
6671 || strcmp (name, "REP") == 0
6672 || startswith (name, "_parent")
6673 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6674}
6675
4c4b4cd2
PH
6676/* True iff field number FIELD_NUM of structure or union type TYPE
6677 is a variant wrapper. Assumes TYPE is a structure type with at least
6678 FIELD_NUM+1 fields. */
14f9c5c9
AS
6679
6680int
ebf56fd3 6681ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6682{
8ecb59f8
TT
6683 /* Only Ada types are eligible. */
6684 if (!ADA_TYPE_P (type))
6685 return 0;
6686
940da03e 6687 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6688
78134374
SM
6689 return (field_type->code () == TYPE_CODE_UNION
6690 || (is_dynamic_field (type, field_num)
6691 && (TYPE_TARGET_TYPE (field_type)->code ()
c3e5cd34 6692 == TYPE_CODE_UNION)));
14f9c5c9
AS
6693}
6694
6695/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6696 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6697 returns the type of the controlling discriminant for the variant.
6698 May return NULL if the type could not be found. */
14f9c5c9 6699
d2e4a39e 6700struct type *
ebf56fd3 6701ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6702{
a121b7c1 6703 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6704
988f6b3d 6705 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6706}
6707
4c4b4cd2 6708/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6709 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6710 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6711
de93309a 6712static int
ebf56fd3 6713ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6714{
33d16dd9 6715 const char *name = type->field (field_num).name ();
5b4ee69b 6716
14f9c5c9
AS
6717 return (name != NULL && name[0] == 'O');
6718}
6719
6720/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6721 returns the name of the discriminant controlling the variant.
6722 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6723
a121b7c1 6724const char *
ebf56fd3 6725ada_variant_discrim_name (struct type *type0)
14f9c5c9 6726{
5f9febe0 6727 static std::string result;
d2e4a39e
AS
6728 struct type *type;
6729 const char *name;
6730 const char *discrim_end;
6731 const char *discrim_start;
14f9c5c9 6732
78134374 6733 if (type0->code () == TYPE_CODE_PTR)
14f9c5c9
AS
6734 type = TYPE_TARGET_TYPE (type0);
6735 else
6736 type = type0;
6737
6738 name = ada_type_name (type);
6739
6740 if (name == NULL || name[0] == '\000')
6741 return "";
6742
6743 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6744 discrim_end -= 1)
6745 {
61012eef 6746 if (startswith (discrim_end, "___XVN"))
dda83cd7 6747 break;
14f9c5c9
AS
6748 }
6749 if (discrim_end == name)
6750 return "";
6751
d2e4a39e 6752 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6753 discrim_start -= 1)
6754 {
d2e4a39e 6755 if (discrim_start == name + 1)
dda83cd7 6756 return "";
76a01679 6757 if ((discrim_start > name + 3
dda83cd7
SM
6758 && startswith (discrim_start - 3, "___"))
6759 || discrim_start[-1] == '.')
6760 break;
14f9c5c9
AS
6761 }
6762
5f9febe0
TT
6763 result = std::string (discrim_start, discrim_end - discrim_start);
6764 return result.c_str ();
14f9c5c9
AS
6765}
6766
4c4b4cd2
PH
6767/* Scan STR for a subtype-encoded number, beginning at position K.
6768 Put the position of the character just past the number scanned in
6769 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6770 Return 1 if there was a valid number at the given position, and 0
6771 otherwise. A "subtype-encoded" number consists of the absolute value
6772 in decimal, followed by the letter 'm' to indicate a negative number.
6773 Assumes 0m does not occur. */
14f9c5c9
AS
6774
6775int
d2e4a39e 6776ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6777{
6778 ULONGEST RU;
6779
d2e4a39e 6780 if (!isdigit (str[k]))
14f9c5c9
AS
6781 return 0;
6782
4c4b4cd2 6783 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6784 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6785 LONGEST. */
14f9c5c9
AS
6786 RU = 0;
6787 while (isdigit (str[k]))
6788 {
d2e4a39e 6789 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6790 k += 1;
6791 }
6792
d2e4a39e 6793 if (str[k] == 'm')
14f9c5c9
AS
6794 {
6795 if (R != NULL)
dda83cd7 6796 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6797 k += 1;
6798 }
6799 else if (R != NULL)
6800 *R = (LONGEST) RU;
6801
4c4b4cd2 6802 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6803 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6804 number representable as a LONGEST (although either would probably work
6805 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6806 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6807
6808 if (new_k != NULL)
6809 *new_k = k;
6810 return 1;
6811}
6812
4c4b4cd2
PH
6813/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6814 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6815 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6816
de93309a 6817static int
ebf56fd3 6818ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6819{
33d16dd9 6820 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6821 int p;
6822
6823 p = 0;
6824 while (1)
6825 {
d2e4a39e 6826 switch (name[p])
dda83cd7
SM
6827 {
6828 case '\0':
6829 return 0;
6830 case 'S':
6831 {
6832 LONGEST W;
6833
6834 if (!ada_scan_number (name, p + 1, &W, &p))
6835 return 0;
6836 if (val == W)
6837 return 1;
6838 break;
6839 }
6840 case 'R':
6841 {
6842 LONGEST L, U;
6843
6844 if (!ada_scan_number (name, p + 1, &L, &p)
6845 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6846 return 0;
6847 if (val >= L && val <= U)
6848 return 1;
6849 break;
6850 }
6851 case 'O':
6852 return 1;
6853 default:
6854 return 0;
6855 }
4c4b4cd2
PH
6856 }
6857}
6858
0963b4bd 6859/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6860
6861/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6862 ARG_TYPE, extract and return the value of one of its (non-static)
6863 fields. FIELDNO says which field. Differs from value_primitive_field
6864 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6865
5eb68a39 6866struct value *
d2e4a39e 6867ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6868 struct type *arg_type)
14f9c5c9 6869{
14f9c5c9
AS
6870 struct type *type;
6871
61ee279c 6872 arg_type = ada_check_typedef (arg_type);
940da03e 6873 type = arg_type->field (fieldno).type ();
14f9c5c9 6874
4504bbde
TT
6875 /* Handle packed fields. It might be that the field is not packed
6876 relative to its containing structure, but the structure itself is
6877 packed; in this case we must take the bit-field path. */
6878 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || value_bitpos (arg1) != 0)
14f9c5c9 6879 {
b610c045 6880 int bit_pos = arg_type->field (fieldno).loc_bitpos ();
14f9c5c9 6881 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6882
50888e42
SM
6883 return ada_value_primitive_packed_val (arg1,
6884 value_contents (arg1).data (),
dda83cd7
SM
6885 offset + bit_pos / 8,
6886 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6887 }
6888 else
6889 return value_primitive_field (arg1, offset, fieldno, arg_type);
6890}
6891
52ce6436
PH
6892/* Find field with name NAME in object of type TYPE. If found,
6893 set the following for each argument that is non-null:
6894 - *FIELD_TYPE_P to the field's type;
6895 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6896 an object of that type;
6897 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6898 - *BIT_SIZE_P to its size in bits if the field is packed, and
6899 0 otherwise;
6900 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6901 fields up to but not including the desired field, or by the total
6902 number of fields if not found. A NULL value of NAME never
6903 matches; the function just counts visible fields in this case.
6904
828d5846
XR
6905 Notice that we need to handle when a tagged record hierarchy
6906 has some components with the same name, like in this scenario:
6907
6908 type Top_T is tagged record
dda83cd7
SM
6909 N : Integer := 1;
6910 U : Integer := 974;
6911 A : Integer := 48;
828d5846
XR
6912 end record;
6913
6914 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6915 N : Character := 'a';
6916 C : Integer := 3;
828d5846
XR
6917 end record;
6918
6919 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6920 N : Float := 4.0;
6921 C : Character := '5';
6922 X : Integer := 6;
6923 A : Character := 'J';
828d5846
XR
6924 end record;
6925
6926 Let's say we now have a variable declared and initialized as follow:
6927
6928 TC : Top_A := new Bottom_T;
6929
6930 And then we use this variable to call this function
6931
6932 procedure Assign (Obj: in out Top_T; TV : Integer);
6933
6934 as follow:
6935
6936 Assign (Top_T (B), 12);
6937
6938 Now, we're in the debugger, and we're inside that procedure
6939 then and we want to print the value of obj.c:
6940
6941 Usually, the tagged record or one of the parent type owns the
6942 component to print and there's no issue but in this particular
6943 case, what does it mean to ask for Obj.C? Since the actual
6944 type for object is type Bottom_T, it could mean two things: type
6945 component C from the Middle_T view, but also component C from
6946 Bottom_T. So in that "undefined" case, when the component is
6947 not found in the non-resolved type (which includes all the
6948 components of the parent type), then resolve it and see if we
6949 get better luck once expanded.
6950
6951 In the case of homonyms in the derived tagged type, we don't
6952 guaranty anything, and pick the one that's easiest for us
6953 to program.
6954
0963b4bd 6955 Returns 1 if found, 0 otherwise. */
52ce6436 6956
4c4b4cd2 6957static int
0d5cff50 6958find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6959 struct type **field_type_p,
6960 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6961 int *index_p)
4c4b4cd2
PH
6962{
6963 int i;
828d5846 6964 int parent_offset = -1;
4c4b4cd2 6965
61ee279c 6966 type = ada_check_typedef (type);
76a01679 6967
52ce6436
PH
6968 if (field_type_p != NULL)
6969 *field_type_p = NULL;
6970 if (byte_offset_p != NULL)
d5d6fca5 6971 *byte_offset_p = 0;
52ce6436
PH
6972 if (bit_offset_p != NULL)
6973 *bit_offset_p = 0;
6974 if (bit_size_p != NULL)
6975 *bit_size_p = 0;
6976
1f704f76 6977 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 6978 {
4d1795ac
TT
6979 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6980 type. However, we only need the values to be correct when
6981 the caller asks for them. */
6982 int bit_pos = 0, fld_offset = 0;
6983 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6984 {
b610c045 6985 bit_pos = type->field (i).loc_bitpos ();
4d1795ac
TT
6986 fld_offset = offset + bit_pos / 8;
6987 }
6988
33d16dd9 6989 const char *t_field_name = type->field (i).name ();
76a01679 6990
4c4b4cd2 6991 if (t_field_name == NULL)
dda83cd7 6992 continue;
4c4b4cd2 6993
828d5846 6994 else if (ada_is_parent_field (type, i))
dda83cd7 6995 {
828d5846
XR
6996 /* This is a field pointing us to the parent type of a tagged
6997 type. As hinted in this function's documentation, we give
6998 preference to fields in the current record first, so what
6999 we do here is just record the index of this field before
7000 we skip it. If it turns out we couldn't find our field
7001 in the current record, then we'll get back to it and search
7002 inside it whether the field might exist in the parent. */
7003
dda83cd7
SM
7004 parent_offset = i;
7005 continue;
7006 }
828d5846 7007
52ce6436 7008 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
7009 {
7010 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 7011
52ce6436 7012 if (field_type_p != NULL)
940da03e 7013 *field_type_p = type->field (i).type ();
52ce6436
PH
7014 if (byte_offset_p != NULL)
7015 *byte_offset_p = fld_offset;
7016 if (bit_offset_p != NULL)
7017 *bit_offset_p = bit_pos % 8;
7018 if (bit_size_p != NULL)
7019 *bit_size_p = bit_size;
dda83cd7
SM
7020 return 1;
7021 }
4c4b4cd2 7022 else if (ada_is_wrapper_field (type, i))
dda83cd7 7023 {
940da03e 7024 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
7025 field_type_p, byte_offset_p, bit_offset_p,
7026 bit_size_p, index_p))
dda83cd7
SM
7027 return 1;
7028 }
4c4b4cd2 7029 else if (ada_is_variant_part (type, i))
dda83cd7 7030 {
52ce6436
PH
7031 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7032 fixed type?? */
dda83cd7
SM
7033 int j;
7034 struct type *field_type
940da03e 7035 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7036
dda83cd7
SM
7037 for (j = 0; j < field_type->num_fields (); j += 1)
7038 {
7039 if (find_struct_field (name, field_type->field (j).type (),
7040 fld_offset
b610c045 7041 + field_type->field (j).loc_bitpos () / 8,
dda83cd7
SM
7042 field_type_p, byte_offset_p,
7043 bit_offset_p, bit_size_p, index_p))
7044 return 1;
7045 }
7046 }
52ce6436
PH
7047 else if (index_p != NULL)
7048 *index_p += 1;
4c4b4cd2 7049 }
828d5846
XR
7050
7051 /* Field not found so far. If this is a tagged type which
7052 has a parent, try finding that field in the parent now. */
7053
7054 if (parent_offset != -1)
7055 {
4d1795ac
TT
7056 /* As above, only compute the offset when truly needed. */
7057 int fld_offset = offset;
7058 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
7059 {
b610c045 7060 int bit_pos = type->field (parent_offset).loc_bitpos ();
4d1795ac
TT
7061 fld_offset += bit_pos / 8;
7062 }
828d5846 7063
940da03e 7064 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7065 fld_offset, field_type_p, byte_offset_p,
7066 bit_offset_p, bit_size_p, index_p))
7067 return 1;
828d5846
XR
7068 }
7069
4c4b4cd2
PH
7070 return 0;
7071}
7072
0963b4bd 7073/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7074
52ce6436
PH
7075static int
7076num_visible_fields (struct type *type)
7077{
7078 int n;
5b4ee69b 7079
52ce6436
PH
7080 n = 0;
7081 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7082 return n;
7083}
14f9c5c9 7084
4c4b4cd2 7085/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7086 and search in it assuming it has (class) type TYPE.
7087 If found, return value, else return NULL.
7088
828d5846
XR
7089 Searches recursively through wrapper fields (e.g., '_parent').
7090
7091 In the case of homonyms in the tagged types, please refer to the
7092 long explanation in find_struct_field's function documentation. */
14f9c5c9 7093
4c4b4cd2 7094static struct value *
108d56a4 7095ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7096 struct type *type)
14f9c5c9
AS
7097{
7098 int i;
828d5846 7099 int parent_offset = -1;
14f9c5c9 7100
5b4ee69b 7101 type = ada_check_typedef (type);
1f704f76 7102 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7103 {
33d16dd9 7104 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
7105
7106 if (t_field_name == NULL)
dda83cd7 7107 continue;
14f9c5c9 7108
828d5846 7109 else if (ada_is_parent_field (type, i))
dda83cd7 7110 {
828d5846
XR
7111 /* This is a field pointing us to the parent type of a tagged
7112 type. As hinted in this function's documentation, we give
7113 preference to fields in the current record first, so what
7114 we do here is just record the index of this field before
7115 we skip it. If it turns out we couldn't find our field
7116 in the current record, then we'll get back to it and search
7117 inside it whether the field might exist in the parent. */
7118
dda83cd7
SM
7119 parent_offset = i;
7120 continue;
7121 }
828d5846 7122
14f9c5c9 7123 else if (field_name_match (t_field_name, name))
dda83cd7 7124 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7125
7126 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7127 {
7128 struct value *v = /* Do not let indent join lines here. */
7129 ada_search_struct_field (name, arg,
b610c045 7130 offset + type->field (i).loc_bitpos () / 8,
dda83cd7 7131 type->field (i).type ());
5b4ee69b 7132
dda83cd7
SM
7133 if (v != NULL)
7134 return v;
7135 }
14f9c5c9
AS
7136
7137 else if (ada_is_variant_part (type, i))
dda83cd7 7138 {
0963b4bd 7139 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7140 int j;
7141 struct type *field_type = ada_check_typedef (type->field (i).type ());
b610c045 7142 int var_offset = offset + type->field (i).loc_bitpos () / 8;
4c4b4cd2 7143
dda83cd7
SM
7144 for (j = 0; j < field_type->num_fields (); j += 1)
7145 {
7146 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7147 break. */
dda83cd7 7148 (name, arg,
b610c045 7149 var_offset + field_type->field (j).loc_bitpos () / 8,
dda83cd7 7150 field_type->field (j).type ());
5b4ee69b 7151
dda83cd7
SM
7152 if (v != NULL)
7153 return v;
7154 }
7155 }
14f9c5c9 7156 }
828d5846
XR
7157
7158 /* Field not found so far. If this is a tagged type which
7159 has a parent, try finding that field in the parent now. */
7160
7161 if (parent_offset != -1)
7162 {
7163 struct value *v = ada_search_struct_field (
b610c045 7164 name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
940da03e 7165 type->field (parent_offset).type ());
828d5846
XR
7166
7167 if (v != NULL)
dda83cd7 7168 return v;
828d5846
XR
7169 }
7170
14f9c5c9
AS
7171 return NULL;
7172}
d2e4a39e 7173
52ce6436
PH
7174static struct value *ada_index_struct_field_1 (int *, struct value *,
7175 int, struct type *);
7176
7177
7178/* Return field #INDEX in ARG, where the index is that returned by
7179 * find_struct_field through its INDEX_P argument. Adjust the address
7180 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7181 * If found, return value, else return NULL. */
52ce6436
PH
7182
7183static struct value *
7184ada_index_struct_field (int index, struct value *arg, int offset,
7185 struct type *type)
7186{
7187 return ada_index_struct_field_1 (&index, arg, offset, type);
7188}
7189
7190
7191/* Auxiliary function for ada_index_struct_field. Like
7192 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7193 * *INDEX_P. */
52ce6436
PH
7194
7195static struct value *
7196ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7197 struct type *type)
7198{
7199 int i;
7200 type = ada_check_typedef (type);
7201
1f704f76 7202 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 7203 {
33d16dd9 7204 if (type->field (i).name () == NULL)
dda83cd7 7205 continue;
52ce6436 7206 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7207 {
7208 struct value *v = /* Do not let indent join lines here. */
7209 ada_index_struct_field_1 (index_p, arg,
b610c045 7210 offset + type->field (i).loc_bitpos () / 8,
940da03e 7211 type->field (i).type ());
5b4ee69b 7212
dda83cd7
SM
7213 if (v != NULL)
7214 return v;
7215 }
52ce6436
PH
7216
7217 else if (ada_is_variant_part (type, i))
dda83cd7 7218 {
52ce6436 7219 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7220 find_struct_field. */
52ce6436 7221 error (_("Cannot assign this kind of variant record"));
dda83cd7 7222 }
52ce6436 7223 else if (*index_p == 0)
dda83cd7 7224 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7225 else
7226 *index_p -= 1;
7227 }
7228 return NULL;
7229}
7230
3b4de39c 7231/* Return a string representation of type TYPE. */
99bbb428 7232
3b4de39c 7233static std::string
99bbb428
PA
7234type_as_string (struct type *type)
7235{
d7e74731 7236 string_file tmp_stream;
99bbb428 7237
d7e74731 7238 type_print (type, "", &tmp_stream, -1);
99bbb428 7239
5d10a204 7240 return tmp_stream.release ();
99bbb428
PA
7241}
7242
14f9c5c9 7243/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7244 If DISPP is non-null, add its byte displacement from the beginning of a
7245 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7246 work for packed fields).
7247
7248 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7249 followed by "___".
14f9c5c9 7250
0963b4bd 7251 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7252 be a (pointer or reference)+ to a struct or union, and the
7253 ultimate target type will be searched.
14f9c5c9
AS
7254
7255 Looks recursively into variant clauses and parent types.
7256
828d5846
XR
7257 In the case of homonyms in the tagged types, please refer to the
7258 long explanation in find_struct_field's function documentation.
7259
4c4b4cd2
PH
7260 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7261 TYPE is not a type of the right kind. */
14f9c5c9 7262
4c4b4cd2 7263static struct type *
a121b7c1 7264ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7265 int noerr)
14f9c5c9
AS
7266{
7267 int i;
828d5846 7268 int parent_offset = -1;
14f9c5c9
AS
7269
7270 if (name == NULL)
7271 goto BadName;
7272
76a01679 7273 if (refok && type != NULL)
4c4b4cd2
PH
7274 while (1)
7275 {
dda83cd7
SM
7276 type = ada_check_typedef (type);
7277 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7278 break;
7279 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 7280 }
14f9c5c9 7281
76a01679 7282 if (type == NULL
78134374
SM
7283 || (type->code () != TYPE_CODE_STRUCT
7284 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7285 {
4c4b4cd2 7286 if (noerr)
dda83cd7 7287 return NULL;
99bbb428 7288
3b4de39c
PA
7289 error (_("Type %s is not a structure or union type"),
7290 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7291 }
7292
7293 type = to_static_fixed_type (type);
7294
1f704f76 7295 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7296 {
33d16dd9 7297 const char *t_field_name = type->field (i).name ();
14f9c5c9 7298 struct type *t;
d2e4a39e 7299
14f9c5c9 7300 if (t_field_name == NULL)
dda83cd7 7301 continue;
14f9c5c9 7302
828d5846 7303 else if (ada_is_parent_field (type, i))
dda83cd7 7304 {
828d5846
XR
7305 /* This is a field pointing us to the parent type of a tagged
7306 type. As hinted in this function's documentation, we give
7307 preference to fields in the current record first, so what
7308 we do here is just record the index of this field before
7309 we skip it. If it turns out we couldn't find our field
7310 in the current record, then we'll get back to it and search
7311 inside it whether the field might exist in the parent. */
7312
dda83cd7
SM
7313 parent_offset = i;
7314 continue;
7315 }
828d5846 7316
14f9c5c9 7317 else if (field_name_match (t_field_name, name))
940da03e 7318 return type->field (i).type ();
14f9c5c9
AS
7319
7320 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7321 {
7322 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7323 0, 1);
7324 if (t != NULL)
988f6b3d 7325 return t;
dda83cd7 7326 }
14f9c5c9
AS
7327
7328 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7329 {
7330 int j;
7331 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7332
dda83cd7
SM
7333 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7334 {
b1f33ddd 7335 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7336 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7337 generates these for unchecked variant types. Revisit
dda83cd7 7338 if the compiler changes this practice. */
33d16dd9 7339 const char *v_field_name = field_type->field (j).name ();
988f6b3d 7340
b1f33ddd
JB
7341 if (v_field_name != NULL
7342 && field_name_match (v_field_name, name))
940da03e 7343 t = field_type->field (j).type ();
b1f33ddd 7344 else
940da03e 7345 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7346 name, 0, 1);
b1f33ddd 7347
dda83cd7 7348 if (t != NULL)
988f6b3d 7349 return t;
dda83cd7
SM
7350 }
7351 }
14f9c5c9
AS
7352
7353 }
7354
828d5846
XR
7355 /* Field not found so far. If this is a tagged type which
7356 has a parent, try finding that field in the parent now. */
7357
7358 if (parent_offset != -1)
7359 {
dda83cd7 7360 struct type *t;
828d5846 7361
dda83cd7
SM
7362 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7363 name, 0, 1);
7364 if (t != NULL)
828d5846
XR
7365 return t;
7366 }
7367
14f9c5c9 7368BadName:
d2e4a39e 7369 if (!noerr)
14f9c5c9 7370 {
2b2798cc 7371 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7372
7373 error (_("Type %s has no component named %s"),
3b4de39c 7374 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7375 }
7376
7377 return NULL;
7378}
7379
b1f33ddd
JB
7380/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7381 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7382 represents an unchecked union (that is, the variant part of a
0963b4bd 7383 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7384
7385static int
7386is_unchecked_variant (struct type *var_type, struct type *outer_type)
7387{
a121b7c1 7388 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7389
988f6b3d 7390 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7391}
7392
7393
14f9c5c9 7394/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7395 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7396 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7397
d2e4a39e 7398int
d8af9068 7399ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7400{
7401 int others_clause;
7402 int i;
a121b7c1 7403 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7404 struct value *discrim;
14f9c5c9
AS
7405 LONGEST discrim_val;
7406
012370f6
TT
7407 /* Using plain value_from_contents_and_address here causes problems
7408 because we will end up trying to resolve a type that is currently
7409 being constructed. */
0c281816
JB
7410 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7411 if (discrim == NULL)
14f9c5c9 7412 return -1;
0c281816 7413 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7414
7415 others_clause = -1;
1f704f76 7416 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7417 {
7418 if (ada_is_others_clause (var_type, i))
dda83cd7 7419 others_clause = i;
14f9c5c9 7420 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7421 return i;
14f9c5c9
AS
7422 }
7423
7424 return others_clause;
7425}
d2e4a39e 7426\f
14f9c5c9
AS
7427
7428
dda83cd7 7429 /* Dynamic-Sized Records */
14f9c5c9
AS
7430
7431/* Strategy: The type ostensibly attached to a value with dynamic size
7432 (i.e., a size that is not statically recorded in the debugging
7433 data) does not accurately reflect the size or layout of the value.
7434 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7435 conventional types that are constructed on the fly. */
14f9c5c9
AS
7436
7437/* There is a subtle and tricky problem here. In general, we cannot
7438 determine the size of dynamic records without its data. However,
7439 the 'struct value' data structure, which GDB uses to represent
7440 quantities in the inferior process (the target), requires the size
7441 of the type at the time of its allocation in order to reserve space
7442 for GDB's internal copy of the data. That's why the
7443 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7444 rather than struct value*s.
14f9c5c9
AS
7445
7446 However, GDB's internal history variables ($1, $2, etc.) are
7447 struct value*s containing internal copies of the data that are not, in
7448 general, the same as the data at their corresponding addresses in
7449 the target. Fortunately, the types we give to these values are all
7450 conventional, fixed-size types (as per the strategy described
7451 above), so that we don't usually have to perform the
7452 'to_fixed_xxx_type' conversions to look at their values.
7453 Unfortunately, there is one exception: if one of the internal
7454 history variables is an array whose elements are unconstrained
7455 records, then we will need to create distinct fixed types for each
7456 element selected. */
7457
7458/* The upshot of all of this is that many routines take a (type, host
7459 address, target address) triple as arguments to represent a value.
7460 The host address, if non-null, is supposed to contain an internal
7461 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7462 target at the target address. */
14f9c5c9
AS
7463
7464/* Assuming that VAL0 represents a pointer value, the result of
7465 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7466 dynamic-sized types. */
14f9c5c9 7467
d2e4a39e
AS
7468struct value *
7469ada_value_ind (struct value *val0)
14f9c5c9 7470{
c48db5ca 7471 struct value *val = value_ind (val0);
5b4ee69b 7472
b50d69b5
JG
7473 if (ada_is_tagged_type (value_type (val), 0))
7474 val = ada_tag_value_at_base_address (val);
7475
4c4b4cd2 7476 return ada_to_fixed_value (val);
14f9c5c9
AS
7477}
7478
7479/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7480 qualifiers on VAL0. */
7481
d2e4a39e
AS
7482static struct value *
7483ada_coerce_ref (struct value *val0)
7484{
78134374 7485 if (value_type (val0)->code () == TYPE_CODE_REF)
d2e4a39e
AS
7486 {
7487 struct value *val = val0;
5b4ee69b 7488
994b9211 7489 val = coerce_ref (val);
b50d69b5
JG
7490
7491 if (ada_is_tagged_type (value_type (val), 0))
7492 val = ada_tag_value_at_base_address (val);
7493
4c4b4cd2 7494 return ada_to_fixed_value (val);
d2e4a39e
AS
7495 }
7496 else
14f9c5c9
AS
7497 return val0;
7498}
7499
4c4b4cd2 7500/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7501
7502static unsigned int
ebf56fd3 7503field_alignment (struct type *type, int f)
14f9c5c9 7504{
33d16dd9 7505 const char *name = type->field (f).name ();
64a1bf19 7506 int len;
14f9c5c9
AS
7507 int align_offset;
7508
64a1bf19
JB
7509 /* The field name should never be null, unless the debugging information
7510 is somehow malformed. In this case, we assume the field does not
7511 require any alignment. */
7512 if (name == NULL)
7513 return 1;
7514
7515 len = strlen (name);
7516
4c4b4cd2
PH
7517 if (!isdigit (name[len - 1]))
7518 return 1;
14f9c5c9 7519
d2e4a39e 7520 if (isdigit (name[len - 2]))
14f9c5c9
AS
7521 align_offset = len - 2;
7522 else
7523 align_offset = len - 1;
7524
61012eef 7525 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7526 return TARGET_CHAR_BIT;
7527
4c4b4cd2
PH
7528 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7529}
7530
852dff6c 7531/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7532
852dff6c
JB
7533static struct symbol *
7534ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7535{
7536 struct symbol *sym;
7537
7538 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
66d7f48f 7539 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
4c4b4cd2
PH
7540 return sym;
7541
4186eb54
KS
7542 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7543 return sym;
14f9c5c9
AS
7544}
7545
dddfab26
UW
7546/* Find a type named NAME. Ignores ambiguity. This routine will look
7547 solely for types defined by debug info, it will not search the GDB
7548 primitive types. */
4c4b4cd2 7549
852dff6c 7550static struct type *
ebf56fd3 7551ada_find_any_type (const char *name)
14f9c5c9 7552{
852dff6c 7553 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7554
14f9c5c9 7555 if (sym != NULL)
5f9c5a63 7556 return sym->type ();
14f9c5c9 7557
dddfab26 7558 return NULL;
14f9c5c9
AS
7559}
7560
739593e0
JB
7561/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7562 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7563 symbol, in which case it is returned. Otherwise, this looks for
7564 symbols whose name is that of NAME_SYM suffixed with "___XR".
7565 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7566
c0e70c62
TT
7567static bool
7568ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7569{
987012b8 7570 const char *name = name_sym->linkage_name ();
c0e70c62 7571 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7572}
7573
14f9c5c9 7574/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7575 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7576 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7577 otherwise return 0. */
7578
14f9c5c9 7579int
d2e4a39e 7580ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7581{
7582 if (type1 == NULL)
7583 return 1;
7584 else if (type0 == NULL)
7585 return 0;
78134374 7586 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7587 return 1;
78134374 7588 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7589 return 0;
7d93a1e0 7590 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7591 return 1;
ad82864c 7592 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7593 return 1;
4c4b4cd2 7594 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7595 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7596 return 1;
aeb5907d
JB
7597 else
7598 {
7d93a1e0
SM
7599 const char *type0_name = type0->name ();
7600 const char *type1_name = type1->name ();
aeb5907d
JB
7601
7602 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7603 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7604 return 1;
7605 }
14f9c5c9
AS
7606 return 0;
7607}
7608
e86ca25f
TT
7609/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7610 null. */
4c4b4cd2 7611
0d5cff50 7612const char *
d2e4a39e 7613ada_type_name (struct type *type)
14f9c5c9 7614{
d2e4a39e 7615 if (type == NULL)
14f9c5c9 7616 return NULL;
7d93a1e0 7617 return type->name ();
14f9c5c9
AS
7618}
7619
b4ba55a1
JB
7620/* Search the list of "descriptive" types associated to TYPE for a type
7621 whose name is NAME. */
7622
7623static struct type *
7624find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7625{
931e5bc3 7626 struct type *result, *tmp;
b4ba55a1 7627
c6044dd1
JB
7628 if (ada_ignore_descriptive_types_p)
7629 return NULL;
7630
b4ba55a1
JB
7631 /* If there no descriptive-type info, then there is no parallel type
7632 to be found. */
7633 if (!HAVE_GNAT_AUX_INFO (type))
7634 return NULL;
7635
7636 result = TYPE_DESCRIPTIVE_TYPE (type);
7637 while (result != NULL)
7638 {
0d5cff50 7639 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7640
7641 if (result_name == NULL)
dda83cd7
SM
7642 {
7643 warning (_("unexpected null name on descriptive type"));
7644 return NULL;
7645 }
b4ba55a1
JB
7646
7647 /* If the names match, stop. */
7648 if (strcmp (result_name, name) == 0)
7649 break;
7650
7651 /* Otherwise, look at the next item on the list, if any. */
7652 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7653 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7654 else
7655 tmp = NULL;
7656
7657 /* If not found either, try after having resolved the typedef. */
7658 if (tmp != NULL)
7659 result = tmp;
b4ba55a1 7660 else
931e5bc3 7661 {
f168693b 7662 result = check_typedef (result);
931e5bc3
JG
7663 if (HAVE_GNAT_AUX_INFO (result))
7664 result = TYPE_DESCRIPTIVE_TYPE (result);
7665 else
7666 result = NULL;
7667 }
b4ba55a1
JB
7668 }
7669
7670 /* If we didn't find a match, see whether this is a packed array. With
7671 older compilers, the descriptive type information is either absent or
7672 irrelevant when it comes to packed arrays so the above lookup fails.
7673 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7674 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7675 return ada_find_any_type (name);
7676
7677 return result;
7678}
7679
7680/* Find a parallel type to TYPE with the specified NAME, using the
7681 descriptive type taken from the debugging information, if available,
7682 and otherwise using the (slower) name-based method. */
7683
7684static struct type *
7685ada_find_parallel_type_with_name (struct type *type, const char *name)
7686{
7687 struct type *result = NULL;
7688
7689 if (HAVE_GNAT_AUX_INFO (type))
7690 result = find_parallel_type_by_descriptive_type (type, name);
7691 else
7692 result = ada_find_any_type (name);
7693
7694 return result;
7695}
7696
7697/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7698 SUFFIX to the name of TYPE. */
14f9c5c9 7699
d2e4a39e 7700struct type *
ebf56fd3 7701ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7702{
0d5cff50 7703 char *name;
fe978cb0 7704 const char *type_name = ada_type_name (type);
14f9c5c9 7705 int len;
d2e4a39e 7706
fe978cb0 7707 if (type_name == NULL)
14f9c5c9
AS
7708 return NULL;
7709
fe978cb0 7710 len = strlen (type_name);
14f9c5c9 7711
b4ba55a1 7712 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7713
fe978cb0 7714 strcpy (name, type_name);
14f9c5c9
AS
7715 strcpy (name + len, suffix);
7716
b4ba55a1 7717 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7718}
7719
14f9c5c9 7720/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7721 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7722
d2e4a39e
AS
7723static struct type *
7724dynamic_template_type (struct type *type)
14f9c5c9 7725{
61ee279c 7726 type = ada_check_typedef (type);
14f9c5c9 7727
78134374 7728 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7729 || ada_type_name (type) == NULL)
14f9c5c9 7730 return NULL;
d2e4a39e 7731 else
14f9c5c9
AS
7732 {
7733 int len = strlen (ada_type_name (type));
5b4ee69b 7734
4c4b4cd2 7735 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7736 return type;
14f9c5c9 7737 else
dda83cd7 7738 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7739 }
7740}
7741
7742/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7743 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7744
d2e4a39e
AS
7745static int
7746is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7747{
33d16dd9 7748 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7749
d2e4a39e 7750 return name != NULL
940da03e 7751 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7752 && strstr (name, "___XVL") != NULL;
7753}
7754
4c4b4cd2
PH
7755/* The index of the variant field of TYPE, or -1 if TYPE does not
7756 represent a variant record type. */
14f9c5c9 7757
d2e4a39e 7758static int
4c4b4cd2 7759variant_field_index (struct type *type)
14f9c5c9
AS
7760{
7761 int f;
7762
78134374 7763 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7764 return -1;
7765
1f704f76 7766 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7767 {
7768 if (ada_is_variant_part (type, f))
dda83cd7 7769 return f;
4c4b4cd2
PH
7770 }
7771 return -1;
14f9c5c9
AS
7772}
7773
4c4b4cd2
PH
7774/* A record type with no fields. */
7775
d2e4a39e 7776static struct type *
fe978cb0 7777empty_record (struct type *templ)
14f9c5c9 7778{
fe978cb0 7779 struct type *type = alloc_type_copy (templ);
5b4ee69b 7780
67607e24 7781 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7782 INIT_NONE_SPECIFIC (type);
d0e39ea2 7783 type->set_name ("<empty>");
14f9c5c9
AS
7784 TYPE_LENGTH (type) = 0;
7785 return type;
7786}
7787
7788/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7789 the value of type TYPE at VALADDR or ADDRESS (see comments at
7790 the beginning of this section) VAL according to GNAT conventions.
7791 DVAL0 should describe the (portion of a) record that contains any
df407dfe 7792 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
7793 an outer-level type (i.e., as opposed to a branch of a variant.) A
7794 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7795 of the variant.
14f9c5c9 7796
4c4b4cd2
PH
7797 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7798 length are not statically known are discarded. As a consequence,
7799 VALADDR, ADDRESS and DVAL0 are ignored.
7800
7801 NOTE: Limitations: For now, we assume that dynamic fields and
7802 variants occupy whole numbers of bytes. However, they need not be
7803 byte-aligned. */
7804
7805struct type *
10a2c479 7806ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7807 const gdb_byte *valaddr,
dda83cd7
SM
7808 CORE_ADDR address, struct value *dval0,
7809 int keep_dynamic_fields)
14f9c5c9 7810{
d2e4a39e
AS
7811 struct value *mark = value_mark ();
7812 struct value *dval;
7813 struct type *rtype;
14f9c5c9 7814 int nfields, bit_len;
4c4b4cd2 7815 int variant_field;
14f9c5c9 7816 long off;
d94e4f4f 7817 int fld_bit_len;
14f9c5c9
AS
7818 int f;
7819
4c4b4cd2
PH
7820 /* Compute the number of fields in this record type that are going
7821 to be processed: unless keep_dynamic_fields, this includes only
7822 fields whose position and length are static will be processed. */
7823 if (keep_dynamic_fields)
1f704f76 7824 nfields = type->num_fields ();
4c4b4cd2
PH
7825 else
7826 {
7827 nfields = 0;
1f704f76 7828 while (nfields < type->num_fields ()
dda83cd7
SM
7829 && !ada_is_variant_part (type, nfields)
7830 && !is_dynamic_field (type, nfields))
7831 nfields++;
4c4b4cd2
PH
7832 }
7833
e9bb382b 7834 rtype = alloc_type_copy (type);
67607e24 7835 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7836 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7837 rtype->set_num_fields (nfields);
3cabb6b0
SM
7838 rtype->set_fields
7839 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7840 rtype->set_name (ada_type_name (type));
9cdd0d12 7841 rtype->set_is_fixed_instance (true);
14f9c5c9 7842
d2e4a39e
AS
7843 off = 0;
7844 bit_len = 0;
4c4b4cd2
PH
7845 variant_field = -1;
7846
14f9c5c9
AS
7847 for (f = 0; f < nfields; f += 1)
7848 {
a89febbd 7849 off = align_up (off, field_alignment (type, f))
b610c045 7850 + type->field (f).loc_bitpos ();
cd3f655c 7851 rtype->field (f).set_loc_bitpos (off);
d2e4a39e 7852 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7853
d2e4a39e 7854 if (ada_is_variant_part (type, f))
dda83cd7
SM
7855 {
7856 variant_field = f;
7857 fld_bit_len = 0;
7858 }
14f9c5c9 7859 else if (is_dynamic_field (type, f))
dda83cd7 7860 {
284614f0
JB
7861 const gdb_byte *field_valaddr = valaddr;
7862 CORE_ADDR field_address = address;
7863 struct type *field_type =
940da03e 7864 TYPE_TARGET_TYPE (type->field (f).type ());
284614f0 7865
dda83cd7 7866 if (dval0 == NULL)
b5304971 7867 {
012370f6
TT
7868 /* Using plain value_from_contents_and_address here
7869 causes problems because we will end up trying to
7870 resolve a type that is currently being
7871 constructed. */
7872 dval = value_from_contents_and_address_unresolved (rtype,
7873 valaddr,
7874 address);
9f1f738a 7875 rtype = value_type (dval);
b5304971 7876 }
dda83cd7
SM
7877 else
7878 dval = dval0;
4c4b4cd2 7879
284614f0
JB
7880 /* If the type referenced by this field is an aligner type, we need
7881 to unwrap that aligner type, because its size might not be set.
7882 Keeping the aligner type would cause us to compute the wrong
7883 size for this field, impacting the offset of the all the fields
7884 that follow this one. */
7885 if (ada_is_aligner_type (field_type))
7886 {
b610c045 7887 long field_offset = type->field (f).loc_bitpos ();
284614f0
JB
7888
7889 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7890 field_address = cond_offset_target (field_address, field_offset);
7891 field_type = ada_aligned_type (field_type);
7892 }
7893
7894 field_valaddr = cond_offset_host (field_valaddr,
7895 off / TARGET_CHAR_BIT);
7896 field_address = cond_offset_target (field_address,
7897 off / TARGET_CHAR_BIT);
7898
7899 /* Get the fixed type of the field. Note that, in this case,
7900 we do not want to get the real type out of the tag: if
7901 the current field is the parent part of a tagged record,
7902 we will get the tag of the object. Clearly wrong: the real
7903 type of the parent is not the real type of the child. We
7904 would end up in an infinite loop. */
7905 field_type = ada_get_base_type (field_type);
7906 field_type = ada_to_fixed_type (field_type, field_valaddr,
7907 field_address, dval, 0);
7908
5d14b6e5 7909 rtype->field (f).set_type (field_type);
33d16dd9 7910 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7911 /* The multiplication can potentially overflow. But because
7912 the field length has been size-checked just above, and
7913 assuming that the maximum size is a reasonable value,
7914 an overflow should not happen in practice. So rather than
7915 adding overflow recovery code to this already complex code,
7916 we just assume that it's not going to happen. */
dda83cd7
SM
7917 fld_bit_len =
7918 TYPE_LENGTH (rtype->field (f).type ()) * TARGET_CHAR_BIT;
7919 }
14f9c5c9 7920 else
dda83cd7 7921 {
5ded5331
JB
7922 /* Note: If this field's type is a typedef, it is important
7923 to preserve the typedef layer.
7924
7925 Otherwise, we might be transforming a typedef to a fat
7926 pointer (encoding a pointer to an unconstrained array),
7927 into a basic fat pointer (encoding an unconstrained
7928 array). As both types are implemented using the same
7929 structure, the typedef is the only clue which allows us
7930 to distinguish between the two options. Stripping it
7931 would prevent us from printing this field appropriately. */
dda83cd7 7932 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7933 rtype->field (f).set_name (type->field (f).name ());
dda83cd7
SM
7934 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7935 fld_bit_len =
7936 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7937 else
5ded5331 7938 {
940da03e 7939 struct type *field_type = type->field (f).type ();
5ded5331
JB
7940
7941 /* We need to be careful of typedefs when computing
7942 the length of our field. If this is a typedef,
7943 get the length of the target type, not the length
7944 of the typedef. */
78134374 7945 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7946 field_type = ada_typedef_target_type (field_type);
7947
dda83cd7
SM
7948 fld_bit_len =
7949 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
5ded5331 7950 }
dda83cd7 7951 }
14f9c5c9 7952 if (off + fld_bit_len > bit_len)
dda83cd7 7953 bit_len = off + fld_bit_len;
d94e4f4f 7954 off += fld_bit_len;
4c4b4cd2 7955 TYPE_LENGTH (rtype) =
dda83cd7 7956 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7957 }
4c4b4cd2
PH
7958
7959 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7960 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7961 the record. This can happen in the presence of representation
7962 clauses. */
7963 if (variant_field >= 0)
7964 {
7965 struct type *branch_type;
7966
b610c045 7967 off = rtype->field (variant_field).loc_bitpos ();
4c4b4cd2
PH
7968
7969 if (dval0 == NULL)
9f1f738a 7970 {
012370f6
TT
7971 /* Using plain value_from_contents_and_address here causes
7972 problems because we will end up trying to resolve a type
7973 that is currently being constructed. */
7974 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7975 address);
9f1f738a
SA
7976 rtype = value_type (dval);
7977 }
4c4b4cd2 7978 else
dda83cd7 7979 dval = dval0;
4c4b4cd2
PH
7980
7981 branch_type =
dda83cd7
SM
7982 to_fixed_variant_branch_type
7983 (type->field (variant_field).type (),
7984 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7985 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7986 if (branch_type == NULL)
dda83cd7
SM
7987 {
7988 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7989 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7990 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 7991 }
4c4b4cd2 7992 else
dda83cd7
SM
7993 {
7994 rtype->field (variant_field).set_type (branch_type);
d3fd12df 7995 rtype->field (variant_field).set_name ("S");
dda83cd7
SM
7996 fld_bit_len =
7997 TYPE_LENGTH (rtype->field (variant_field).type ()) *
7998 TARGET_CHAR_BIT;
7999 if (off + fld_bit_len > bit_len)
8000 bit_len = off + fld_bit_len;
8001 TYPE_LENGTH (rtype) =
8002 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
8003 }
4c4b4cd2
PH
8004 }
8005
714e53ab
PH
8006 /* According to exp_dbug.ads, the size of TYPE for variable-size records
8007 should contain the alignment of that record, which should be a strictly
8008 positive value. If null or negative, then something is wrong, most
8009 probably in the debug info. In that case, we don't round up the size
0963b4bd 8010 of the resulting type. If this record is not part of another structure,
714e53ab
PH
8011 the current RTYPE length might be good enough for our purposes. */
8012 if (TYPE_LENGTH (type) <= 0)
8013 {
7d93a1e0 8014 if (rtype->name ())
cc1defb1 8015 warning (_("Invalid type size for `%s' detected: %s."),
7d93a1e0 8016 rtype->name (), pulongest (TYPE_LENGTH (type)));
323e0a4a 8017 else
cc1defb1
KS
8018 warning (_("Invalid type size for <unnamed> detected: %s."),
8019 pulongest (TYPE_LENGTH (type)));
714e53ab
PH
8020 }
8021 else
8022 {
a89febbd
TT
8023 TYPE_LENGTH (rtype) = align_up (TYPE_LENGTH (rtype),
8024 TYPE_LENGTH (type));
714e53ab 8025 }
14f9c5c9
AS
8026
8027 value_free_to_mark (mark);
14f9c5c9
AS
8028 return rtype;
8029}
8030
4c4b4cd2
PH
8031/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8032 of 1. */
14f9c5c9 8033
d2e4a39e 8034static struct type *
fc1a4b47 8035template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8036 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
8037{
8038 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 8039 address, dval0, 1);
4c4b4cd2
PH
8040}
8041
8042/* An ordinary record type in which ___XVL-convention fields and
8043 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8044 static approximations, containing all possible fields. Uses
8045 no runtime values. Useless for use in values, but that's OK,
8046 since the results are used only for type determinations. Works on both
8047 structs and unions. Representation note: to save space, we memorize
8048 the result of this function in the TYPE_TARGET_TYPE of the
8049 template type. */
8050
8051static struct type *
8052template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8053{
8054 struct type *type;
8055 int nfields;
8056 int f;
8057
9e195661 8058 /* No need no do anything if the input type is already fixed. */
22c4c60c 8059 if (type0->is_fixed_instance ())
9e195661
PMR
8060 return type0;
8061
8062 /* Likewise if we already have computed the static approximation. */
4c4b4cd2
PH
8063 if (TYPE_TARGET_TYPE (type0) != NULL)
8064 return TYPE_TARGET_TYPE (type0);
8065
9e195661 8066 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8067 type = type0;
1f704f76 8068 nfields = type0->num_fields ();
9e195661
PMR
8069
8070 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8071 recompute all over next time. */
8072 TYPE_TARGET_TYPE (type0) = type;
14f9c5c9
AS
8073
8074 for (f = 0; f < nfields; f += 1)
8075 {
940da03e 8076 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8077 struct type *new_type;
14f9c5c9 8078
4c4b4cd2 8079 if (is_dynamic_field (type0, f))
460efde1
JB
8080 {
8081 field_type = ada_check_typedef (field_type);
dda83cd7 8082 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
460efde1 8083 }
14f9c5c9 8084 else
dda83cd7 8085 new_type = static_unwrap_type (field_type);
9e195661
PMR
8086
8087 if (new_type != field_type)
8088 {
8089 /* Clone TYPE0 only the first time we get a new field type. */
8090 if (type == type0)
8091 {
8092 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
78134374 8093 type->set_code (type0->code ());
8ecb59f8 8094 INIT_NONE_SPECIFIC (type);
5e33d5f4 8095 type->set_num_fields (nfields);
3cabb6b0
SM
8096
8097 field *fields =
8098 ((struct field *)
8099 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 8100 memcpy (fields, type0->fields (),
9e195661 8101 sizeof (struct field) * nfields);
3cabb6b0
SM
8102 type->set_fields (fields);
8103
d0e39ea2 8104 type->set_name (ada_type_name (type0));
9cdd0d12 8105 type->set_is_fixed_instance (true);
9e195661
PMR
8106 TYPE_LENGTH (type) = 0;
8107 }
5d14b6e5 8108 type->field (f).set_type (new_type);
33d16dd9 8109 type->field (f).set_name (type0->field (f).name ());
9e195661 8110 }
14f9c5c9 8111 }
9e195661 8112
14f9c5c9
AS
8113 return type;
8114}
8115
4c4b4cd2 8116/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8117 whose address in memory is ADDRESS, returns a revision of TYPE,
8118 which should be a non-dynamic-sized record, in which the variant
8119 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8120 for discriminant values in DVAL0, which can be NULL if the record
8121 contains the necessary discriminant values. */
8122
d2e4a39e 8123static struct type *
fc1a4b47 8124to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8125 CORE_ADDR address, struct value *dval0)
14f9c5c9 8126{
d2e4a39e 8127 struct value *mark = value_mark ();
4c4b4cd2 8128 struct value *dval;
d2e4a39e 8129 struct type *rtype;
14f9c5c9 8130 struct type *branch_type;
1f704f76 8131 int nfields = type->num_fields ();
4c4b4cd2 8132 int variant_field = variant_field_index (type);
14f9c5c9 8133
4c4b4cd2 8134 if (variant_field == -1)
14f9c5c9
AS
8135 return type;
8136
4c4b4cd2 8137 if (dval0 == NULL)
9f1f738a
SA
8138 {
8139 dval = value_from_contents_and_address (type, valaddr, address);
8140 type = value_type (dval);
8141 }
4c4b4cd2
PH
8142 else
8143 dval = dval0;
8144
e9bb382b 8145 rtype = alloc_type_copy (type);
67607e24 8146 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8147 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 8148 rtype->set_num_fields (nfields);
3cabb6b0
SM
8149
8150 field *fields =
d2e4a39e 8151 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 8152 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
8153 rtype->set_fields (fields);
8154
d0e39ea2 8155 rtype->set_name (ada_type_name (type));
9cdd0d12 8156 rtype->set_is_fixed_instance (true);
14f9c5c9
AS
8157 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
8158
4c4b4cd2 8159 branch_type = to_fixed_variant_branch_type
940da03e 8160 (type->field (variant_field).type (),
d2e4a39e 8161 cond_offset_host (valaddr,
b610c045 8162 type->field (variant_field).loc_bitpos ()
dda83cd7 8163 / TARGET_CHAR_BIT),
d2e4a39e 8164 cond_offset_target (address,
b610c045 8165 type->field (variant_field).loc_bitpos ()
dda83cd7 8166 / TARGET_CHAR_BIT), dval);
d2e4a39e 8167 if (branch_type == NULL)
14f9c5c9 8168 {
4c4b4cd2 8169 int f;
5b4ee69b 8170
4c4b4cd2 8171 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8172 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8173 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8174 }
8175 else
8176 {
5d14b6e5 8177 rtype->field (variant_field).set_type (branch_type);
d3fd12df 8178 rtype->field (variant_field).set_name ("S");
4c4b4cd2 8179 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 8180 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 8181 }
940da03e 8182 TYPE_LENGTH (rtype) -= TYPE_LENGTH (type->field (variant_field).type ());
d2e4a39e 8183
4c4b4cd2 8184 value_free_to_mark (mark);
14f9c5c9
AS
8185 return rtype;
8186}
8187
8188/* An ordinary record type (with fixed-length fields) that describes
8189 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8190 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8191 should be in DVAL, a record value; it may be NULL if the object
8192 at ADDR itself contains any necessary discriminant values.
8193 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8194 values from the record are needed. Except in the case that DVAL,
8195 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8196 unchecked) is replaced by a particular branch of the variant.
8197
8198 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8199 is questionable and may be removed. It can arise during the
8200 processing of an unconstrained-array-of-record type where all the
8201 variant branches have exactly the same size. This is because in
8202 such cases, the compiler does not bother to use the XVS convention
8203 when encoding the record. I am currently dubious of this
8204 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8205
d2e4a39e 8206static struct type *
fc1a4b47 8207to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8208 CORE_ADDR address, struct value *dval)
14f9c5c9 8209{
d2e4a39e 8210 struct type *templ_type;
14f9c5c9 8211
22c4c60c 8212 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8213 return type0;
8214
d2e4a39e 8215 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8216
8217 if (templ_type != NULL)
8218 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8219 else if (variant_field_index (type0) >= 0)
8220 {
8221 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8222 return type0;
4c4b4cd2 8223 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8224 dval);
4c4b4cd2 8225 }
14f9c5c9
AS
8226 else
8227 {
9cdd0d12 8228 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8229 return type0;
8230 }
8231
8232}
8233
8234/* An ordinary record type (with fixed-length fields) that describes
8235 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8236 union type. Any necessary discriminants' values should be in DVAL,
8237 a record value. That is, this routine selects the appropriate
8238 branch of the union at ADDR according to the discriminant value
b1f33ddd 8239 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8240 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8241
d2e4a39e 8242static struct type *
fc1a4b47 8243to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8244 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8245{
8246 int which;
d2e4a39e
AS
8247 struct type *templ_type;
8248 struct type *var_type;
14f9c5c9 8249
78134374 8250 if (var_type0->code () == TYPE_CODE_PTR)
14f9c5c9 8251 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 8252 else
14f9c5c9
AS
8253 var_type = var_type0;
8254
8255 templ_type = ada_find_parallel_type (var_type, "___XVU");
8256
8257 if (templ_type != NULL)
8258 var_type = templ_type;
8259
b1f33ddd
JB
8260 if (is_unchecked_variant (var_type, value_type (dval)))
8261 return var_type0;
d8af9068 8262 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8263
8264 if (which < 0)
e9bb382b 8265 return empty_record (var_type);
14f9c5c9 8266 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8267 return to_fixed_record_type
940da03e 8268 (TYPE_TARGET_TYPE (var_type->field (which).type ()),
d2e4a39e 8269 valaddr, address, dval);
940da03e 8270 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8271 return
8272 to_fixed_record_type
940da03e 8273 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8274 else
940da03e 8275 return var_type->field (which).type ();
14f9c5c9
AS
8276}
8277
8908fca5
JB
8278/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8279 ENCODING_TYPE, a type following the GNAT conventions for discrete
8280 type encodings, only carries redundant information. */
8281
8282static int
8283ada_is_redundant_range_encoding (struct type *range_type,
8284 struct type *encoding_type)
8285{
108d56a4 8286 const char *bounds_str;
8908fca5
JB
8287 int n;
8288 LONGEST lo, hi;
8289
78134374 8290 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8291
78134374
SM
8292 if (get_base_type (range_type)->code ()
8293 != get_base_type (encoding_type)->code ())
005e2509
JB
8294 {
8295 /* The compiler probably used a simple base type to describe
8296 the range type instead of the range's actual base type,
8297 expecting us to get the real base type from the encoding
8298 anyway. In this situation, the encoding cannot be ignored
8299 as redundant. */
8300 return 0;
8301 }
8302
8908fca5
JB
8303 if (is_dynamic_type (range_type))
8304 return 0;
8305
7d93a1e0 8306 if (encoding_type->name () == NULL)
8908fca5
JB
8307 return 0;
8308
7d93a1e0 8309 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8310 if (bounds_str == NULL)
8311 return 0;
8312
8313 n = 8; /* Skip "___XDLU_". */
8314 if (!ada_scan_number (bounds_str, n, &lo, &n))
8315 return 0;
5537ddd0 8316 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8317 return 0;
8318
8319 n += 2; /* Skip the "__" separator between the two bounds. */
8320 if (!ada_scan_number (bounds_str, n, &hi, &n))
8321 return 0;
5537ddd0 8322 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8323 return 0;
8324
8325 return 1;
8326}
8327
8328/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8329 a type following the GNAT encoding for describing array type
8330 indices, only carries redundant information. */
8331
8332static int
8333ada_is_redundant_index_type_desc (struct type *array_type,
8334 struct type *desc_type)
8335{
8336 struct type *this_layer = check_typedef (array_type);
8337 int i;
8338
1f704f76 8339 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8340 {
3d967001 8341 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8342 desc_type->field (i).type ()))
8908fca5
JB
8343 return 0;
8344 this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
8345 }
8346
8347 return 1;
8348}
8349
14f9c5c9
AS
8350/* Assuming that TYPE0 is an array type describing the type of a value
8351 at ADDR, and that DVAL describes a record containing any
8352 discriminants used in TYPE0, returns a type for the value that
8353 contains no dynamic components (that is, no components whose sizes
8354 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8355 true, gives an error message if the resulting type's size is over
4c4b4cd2 8356 varsize_limit. */
14f9c5c9 8357
d2e4a39e
AS
8358static struct type *
8359to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8360 int ignore_too_big)
14f9c5c9 8361{
d2e4a39e
AS
8362 struct type *index_type_desc;
8363 struct type *result;
ad82864c 8364 int constrained_packed_array_p;
931e5bc3 8365 static const char *xa_suffix = "___XA";
14f9c5c9 8366
b0dd7688 8367 type0 = ada_check_typedef (type0);
22c4c60c 8368 if (type0->is_fixed_instance ())
4c4b4cd2 8369 return type0;
14f9c5c9 8370
ad82864c
JB
8371 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8372 if (constrained_packed_array_p)
75fd6a26
TT
8373 {
8374 type0 = decode_constrained_packed_array_type (type0);
8375 if (type0 == nullptr)
8376 error (_("could not decode constrained packed array type"));
8377 }
284614f0 8378
931e5bc3
JG
8379 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8380
8381 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8382 encoding suffixed with 'P' may still be generated. If so,
8383 it should be used to find the XA type. */
8384
8385 if (index_type_desc == NULL)
8386 {
1da0522e 8387 const char *type_name = ada_type_name (type0);
931e5bc3 8388
1da0522e 8389 if (type_name != NULL)
931e5bc3 8390 {
1da0522e 8391 const int len = strlen (type_name);
931e5bc3
JG
8392 char *name = (char *) alloca (len + strlen (xa_suffix));
8393
1da0522e 8394 if (type_name[len - 1] == 'P')
931e5bc3 8395 {
1da0522e 8396 strcpy (name, type_name);
931e5bc3
JG
8397 strcpy (name + len - 1, xa_suffix);
8398 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8399 }
8400 }
8401 }
8402
28c85d6c 8403 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8404 if (index_type_desc != NULL
8405 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8406 {
8407 /* Ignore this ___XA parallel type, as it does not bring any
8408 useful information. This allows us to avoid creating fixed
8409 versions of the array's index types, which would be identical
8410 to the original ones. This, in turn, can also help avoid
8411 the creation of fixed versions of the array itself. */
8412 index_type_desc = NULL;
8413 }
8414
14f9c5c9
AS
8415 if (index_type_desc == NULL)
8416 {
61ee279c 8417 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 8418
14f9c5c9 8419 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8420 depend on the contents of the array in properly constructed
8421 debugging data. */
529cad9c 8422 /* Create a fixed version of the array element type.
dda83cd7
SM
8423 We're not providing the address of an element here,
8424 and thus the actual object value cannot be inspected to do
8425 the conversion. This should not be a problem, since arrays of
8426 unconstrained objects are not allowed. In particular, all
8427 the elements of an array of a tagged type should all be of
8428 the same type specified in the debugging info. No need to
8429 consult the object tag. */
1ed6ede0 8430 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8431
284614f0
JB
8432 /* Make sure we always create a new array type when dealing with
8433 packed array types, since we're going to fix-up the array
8434 type length and element bitsize a little further down. */
ad82864c 8435 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8436 result = type0;
14f9c5c9 8437 else
dda83cd7
SM
8438 result = create_array_type (alloc_type_copy (type0),
8439 elt_type, type0->index_type ());
14f9c5c9
AS
8440 }
8441 else
8442 {
8443 int i;
8444 struct type *elt_type0;
8445
8446 elt_type0 = type0;
1f704f76 8447 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
dda83cd7 8448 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8449
8450 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8451 depend on the contents of the array in properly constructed
8452 debugging data. */
529cad9c 8453 /* Create a fixed version of the array element type.
dda83cd7
SM
8454 We're not providing the address of an element here,
8455 and thus the actual object value cannot be inspected to do
8456 the conversion. This should not be a problem, since arrays of
8457 unconstrained objects are not allowed. In particular, all
8458 the elements of an array of a tagged type should all be of
8459 the same type specified in the debugging info. No need to
8460 consult the object tag. */
1ed6ede0 8461 result =
dda83cd7 8462 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8463
8464 elt_type0 = type0;
1f704f76 8465 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8466 {
8467 struct type *range_type =
8468 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8469
dda83cd7
SM
8470 result = create_array_type (alloc_type_copy (elt_type0),
8471 result, range_type);
1ce677a4 8472 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
dda83cd7 8473 }
14f9c5c9
AS
8474 }
8475
2e6fda7d
JB
8476 /* We want to preserve the type name. This can be useful when
8477 trying to get the type name of a value that has already been
8478 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8479 result->set_name (type0->name ());
2e6fda7d 8480
ad82864c 8481 if (constrained_packed_array_p)
284614f0
JB
8482 {
8483 /* So far, the resulting type has been created as if the original
8484 type was a regular (non-packed) array type. As a result, the
8485 bitsize of the array elements needs to be set again, and the array
8486 length needs to be recomputed based on that bitsize. */
8487 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
8488 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8489
8490 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
8491 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
8492 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
dda83cd7 8493 TYPE_LENGTH (result)++;
284614f0
JB
8494 }
8495
9cdd0d12 8496 result->set_is_fixed_instance (true);
14f9c5c9 8497 return result;
d2e4a39e 8498}
14f9c5c9
AS
8499
8500
8501/* A standard type (containing no dynamically sized components)
8502 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8503 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8504 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8505 ADDRESS or in VALADDR contains these discriminants.
8506
1ed6ede0
JB
8507 If CHECK_TAG is not null, in the case of tagged types, this function
8508 attempts to locate the object's tag and use it to compute the actual
8509 type. However, when ADDRESS is null, we cannot use it to determine the
8510 location of the tag, and therefore compute the tagged type's actual type.
8511 So we return the tagged type without consulting the tag. */
529cad9c 8512
f192137b
JB
8513static struct type *
8514ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8515 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8516{
61ee279c 8517 type = ada_check_typedef (type);
8ecb59f8
TT
8518
8519 /* Only un-fixed types need to be handled here. */
8520 if (!HAVE_GNAT_AUX_INFO (type))
8521 return type;
8522
78134374 8523 switch (type->code ())
d2e4a39e
AS
8524 {
8525 default:
14f9c5c9 8526 return type;
d2e4a39e 8527 case TYPE_CODE_STRUCT:
4c4b4cd2 8528 {
dda83cd7
SM
8529 struct type *static_type = to_static_fixed_type (type);
8530 struct type *fixed_record_type =
8531 to_fixed_record_type (type, valaddr, address, NULL);
8532
8533 /* If STATIC_TYPE is a tagged type and we know the object's address,
8534 then we can determine its tag, and compute the object's actual
8535 type from there. Note that we have to use the fixed record
8536 type (the parent part of the record may have dynamic fields
8537 and the way the location of _tag is expressed may depend on
8538 them). */
8539
8540 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8541 {
b50d69b5
JG
8542 struct value *tag =
8543 value_tag_from_contents_and_address
8544 (fixed_record_type,
8545 valaddr,
8546 address);
8547 struct type *real_type = type_from_tag (tag);
8548 struct value *obj =
8549 value_from_contents_and_address (fixed_record_type,
8550 valaddr,
8551 address);
dda83cd7
SM
8552 fixed_record_type = value_type (obj);
8553 if (real_type != NULL)
8554 return to_fixed_record_type
b50d69b5
JG
8555 (real_type, NULL,
8556 value_address (ada_tag_value_at_base_address (obj)), NULL);
dda83cd7
SM
8557 }
8558
8559 /* Check to see if there is a parallel ___XVZ variable.
8560 If there is, then it provides the actual size of our type. */
8561 else if (ada_type_name (fixed_record_type) != NULL)
8562 {
8563 const char *name = ada_type_name (fixed_record_type);
8564 char *xvz_name
224c3ddb 8565 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8566 bool xvz_found = false;
dda83cd7 8567 LONGEST size;
4af88198 8568
dda83cd7 8569 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8570 try
eccab96d
JB
8571 {
8572 xvz_found = get_int_var_value (xvz_name, size);
8573 }
230d2906 8574 catch (const gdb_exception_error &except)
eccab96d
JB
8575 {
8576 /* We found the variable, but somehow failed to read
8577 its value. Rethrow the same error, but with a little
8578 bit more information, to help the user understand
8579 what went wrong (Eg: the variable might have been
8580 optimized out). */
8581 throw_error (except.error,
8582 _("unable to read value of %s (%s)"),
3d6e9d23 8583 xvz_name, except.what ());
eccab96d 8584 }
eccab96d 8585
dda83cd7
SM
8586 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
8587 {
8588 fixed_record_type = copy_type (fixed_record_type);
8589 TYPE_LENGTH (fixed_record_type) = size;
8590
8591 /* The FIXED_RECORD_TYPE may have be a stub. We have
8592 observed this when the debugging info is STABS, and
8593 apparently it is something that is hard to fix.
8594
8595 In practice, we don't need the actual type definition
8596 at all, because the presence of the XVZ variable allows us
8597 to assume that there must be a XVS type as well, which we
8598 should be able to use later, when we need the actual type
8599 definition.
8600
8601 In the meantime, pretend that the "fixed" type we are
8602 returning is NOT a stub, because this can cause trouble
8603 when using this type to create new types targeting it.
8604 Indeed, the associated creation routines often check
8605 whether the target type is a stub and will try to replace
8606 it, thus using a type with the wrong size. This, in turn,
8607 might cause the new type to have the wrong size too.
8608 Consider the case of an array, for instance, where the size
8609 of the array is computed from the number of elements in
8610 our array multiplied by the size of its element. */
b4b73759 8611 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8612 }
8613 }
8614 return fixed_record_type;
4c4b4cd2 8615 }
d2e4a39e 8616 case TYPE_CODE_ARRAY:
4c4b4cd2 8617 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8618 case TYPE_CODE_UNION:
8619 if (dval == NULL)
dda83cd7 8620 return type;
d2e4a39e 8621 else
dda83cd7 8622 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8623 }
14f9c5c9
AS
8624}
8625
f192137b
JB
8626/* The same as ada_to_fixed_type_1, except that it preserves the type
8627 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8628
8629 The typedef layer needs be preserved in order to differentiate between
8630 arrays and array pointers when both types are implemented using the same
8631 fat pointer. In the array pointer case, the pointer is encoded as
8632 a typedef of the pointer type. For instance, considering:
8633
8634 type String_Access is access String;
8635 S1 : String_Access := null;
8636
8637 To the debugger, S1 is defined as a typedef of type String. But
8638 to the user, it is a pointer. So if the user tries to print S1,
8639 we should not dereference the array, but print the array address
8640 instead.
8641
8642 If we didn't preserve the typedef layer, we would lose the fact that
8643 the type is to be presented as a pointer (needs de-reference before
8644 being printed). And we would also use the source-level type name. */
f192137b
JB
8645
8646struct type *
8647ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8648 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8649
8650{
8651 struct type *fixed_type =
8652 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8653
96dbd2c1
JB
8654 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8655 then preserve the typedef layer.
8656
8657 Implementation note: We can only check the main-type portion of
8658 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8659 from TYPE now returns a type that has the same instance flags
8660 as TYPE. For instance, if TYPE is a "typedef const", and its
8661 target type is a "struct", then the typedef elimination will return
8662 a "const" version of the target type. See check_typedef for more
8663 details about how the typedef layer elimination is done.
8664
8665 brobecker/2010-11-19: It seems to me that the only case where it is
8666 useful to preserve the typedef layer is when dealing with fat pointers.
8667 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8668 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8669 because we call check_typedef/ada_check_typedef pretty much everywhere.
8670 */
78134374 8671 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8672 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8673 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8674 return type;
8675
8676 return fixed_type;
8677}
8678
14f9c5c9 8679/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8680 TYPE0, but based on no runtime data. */
14f9c5c9 8681
d2e4a39e
AS
8682static struct type *
8683to_static_fixed_type (struct type *type0)
14f9c5c9 8684{
d2e4a39e 8685 struct type *type;
14f9c5c9
AS
8686
8687 if (type0 == NULL)
8688 return NULL;
8689
22c4c60c 8690 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8691 return type0;
8692
61ee279c 8693 type0 = ada_check_typedef (type0);
d2e4a39e 8694
78134374 8695 switch (type0->code ())
14f9c5c9
AS
8696 {
8697 default:
8698 return type0;
8699 case TYPE_CODE_STRUCT:
8700 type = dynamic_template_type (type0);
d2e4a39e 8701 if (type != NULL)
dda83cd7 8702 return template_to_static_fixed_type (type);
4c4b4cd2 8703 else
dda83cd7 8704 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8705 case TYPE_CODE_UNION:
8706 type = ada_find_parallel_type (type0, "___XVU");
8707 if (type != NULL)
dda83cd7 8708 return template_to_static_fixed_type (type);
4c4b4cd2 8709 else
dda83cd7 8710 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8711 }
8712}
8713
4c4b4cd2
PH
8714/* A static approximation of TYPE with all type wrappers removed. */
8715
d2e4a39e
AS
8716static struct type *
8717static_unwrap_type (struct type *type)
14f9c5c9
AS
8718{
8719 if (ada_is_aligner_type (type))
8720 {
940da03e 8721 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8722 if (ada_type_name (type1) == NULL)
d0e39ea2 8723 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8724
8725 return static_unwrap_type (type1);
8726 }
d2e4a39e 8727 else
14f9c5c9 8728 {
d2e4a39e 8729 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8730
d2e4a39e 8731 if (raw_real_type == type)
dda83cd7 8732 return type;
14f9c5c9 8733 else
dda83cd7 8734 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8735 }
8736}
8737
8738/* In some cases, incomplete and private types require
4c4b4cd2 8739 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8740 type Foo;
8741 type FooP is access Foo;
8742 V: FooP;
8743 type Foo is array ...;
4c4b4cd2 8744 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8745 cross-references to such types, we instead substitute for FooP a
8746 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8747 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8748
8749/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8750 exists, otherwise TYPE. */
8751
d2e4a39e 8752struct type *
61ee279c 8753ada_check_typedef (struct type *type)
14f9c5c9 8754{
727e3d2e
JB
8755 if (type == NULL)
8756 return NULL;
8757
736ade86
XR
8758 /* If our type is an access to an unconstrained array, which is encoded
8759 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8760 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8761 what allows us to distinguish between fat pointers that represent
8762 array types, and fat pointers that represent array access types
8763 (in both cases, the compiler implements them as fat pointers). */
736ade86 8764 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8765 return type;
8766
f168693b 8767 type = check_typedef (type);
78134374 8768 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8769 || !type->is_stub ()
7d93a1e0 8770 || type->name () == NULL)
14f9c5c9 8771 return type;
d2e4a39e 8772 else
14f9c5c9 8773 {
7d93a1e0 8774 const char *name = type->name ();
d2e4a39e 8775 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8776
05e522ef 8777 if (type1 == NULL)
dda83cd7 8778 return type;
05e522ef
JB
8779
8780 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8781 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8782 types, only for the typedef-to-array types). If that's the case,
8783 strip the typedef layer. */
78134374 8784 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8785 type1 = ada_check_typedef (type1);
8786
8787 return type1;
14f9c5c9
AS
8788 }
8789}
8790
8791/* A value representing the data at VALADDR/ADDRESS as described by
8792 type TYPE0, but with a standard (static-sized) type that correctly
8793 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8794 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8795 creation of struct values]. */
14f9c5c9 8796
4c4b4cd2
PH
8797static struct value *
8798ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8799 struct value *val0)
14f9c5c9 8800{
1ed6ede0 8801 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8802
14f9c5c9
AS
8803 if (type == type0 && val0 != NULL)
8804 return val0;
cc0e770c
JB
8805
8806 if (VALUE_LVAL (val0) != lval_memory)
8807 {
8808 /* Our value does not live in memory; it could be a convenience
8809 variable, for instance. Create a not_lval value using val0's
8810 contents. */
50888e42 8811 return value_from_contents (type, value_contents (val0).data ());
cc0e770c
JB
8812 }
8813
8814 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8815}
8816
8817/* A value representing VAL, but with a standard (static-sized) type
8818 that correctly describes it. Does not necessarily create a new
8819 value. */
8820
0c3acc09 8821struct value *
4c4b4cd2
PH
8822ada_to_fixed_value (struct value *val)
8823{
c48db5ca 8824 val = unwrap_value (val);
d8ce9127 8825 val = ada_to_fixed_value_create (value_type (val), value_address (val), val);
c48db5ca 8826 return val;
14f9c5c9 8827}
d2e4a39e 8828\f
14f9c5c9 8829
14f9c5c9
AS
8830/* Attributes */
8831
4c4b4cd2
PH
8832/* Table mapping attribute numbers to names.
8833 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8834
27087b7f 8835static const char * const attribute_names[] = {
14f9c5c9
AS
8836 "<?>",
8837
d2e4a39e 8838 "first",
14f9c5c9
AS
8839 "last",
8840 "length",
8841 "image",
14f9c5c9
AS
8842 "max",
8843 "min",
4c4b4cd2
PH
8844 "modulus",
8845 "pos",
8846 "size",
8847 "tag",
14f9c5c9 8848 "val",
14f9c5c9
AS
8849 0
8850};
8851
de93309a 8852static const char *
4c4b4cd2 8853ada_attribute_name (enum exp_opcode n)
14f9c5c9 8854{
4c4b4cd2
PH
8855 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8856 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8857 else
8858 return attribute_names[0];
8859}
8860
4c4b4cd2 8861/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8862
4c4b4cd2
PH
8863static LONGEST
8864pos_atr (struct value *arg)
14f9c5c9 8865{
24209737
PH
8866 struct value *val = coerce_ref (arg);
8867 struct type *type = value_type (val);
14f9c5c9 8868
d2e4a39e 8869 if (!discrete_type_p (type))
323e0a4a 8870 error (_("'POS only defined on discrete types"));
14f9c5c9 8871
6244c119
SM
8872 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8873 if (!result.has_value ())
aa715135 8874 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8875
6244c119 8876 return *result;
4c4b4cd2
PH
8877}
8878
7631cf6c 8879struct value *
7992accc
TT
8880ada_pos_atr (struct type *expect_type,
8881 struct expression *exp,
8882 enum noside noside, enum exp_opcode op,
8883 struct value *arg)
4c4b4cd2 8884{
7992accc
TT
8885 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8886 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8887 return value_zero (type, not_lval);
3cb382c9 8888 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8889}
8890
4c4b4cd2 8891/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8892
d2e4a39e 8893static struct value *
53a47a3e 8894val_atr (struct type *type, LONGEST val)
14f9c5c9 8895{
53a47a3e 8896 gdb_assert (discrete_type_p (type));
0bc2354b
TT
8897 if (type->code () == TYPE_CODE_RANGE)
8898 type = TYPE_TARGET_TYPE (type);
78134374 8899 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8900 {
53a47a3e 8901 if (val < 0 || val >= type->num_fields ())
dda83cd7 8902 error (_("argument to 'VAL out of range"));
970db518 8903 val = type->field (val).loc_enumval ();
14f9c5c9 8904 }
53a47a3e
TT
8905 return value_from_longest (type, val);
8906}
8907
9e99f48f 8908struct value *
3848abd6 8909ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8910{
3848abd6
TT
8911 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8912 return value_zero (type, not_lval);
8913
53a47a3e
TT
8914 if (!discrete_type_p (type))
8915 error (_("'VAL only defined on discrete types"));
8916 if (!integer_type_p (value_type (arg)))
8917 error (_("'VAL requires integral argument"));
8918
8919 return val_atr (type, value_as_long (arg));
14f9c5c9 8920}
14f9c5c9 8921\f
d2e4a39e 8922
dda83cd7 8923 /* Evaluation */
14f9c5c9 8924
4c4b4cd2
PH
8925/* True if TYPE appears to be an Ada character type.
8926 [At the moment, this is true only for Character and Wide_Character;
8927 It is a heuristic test that could stand improvement]. */
14f9c5c9 8928
fc913e53 8929bool
d2e4a39e 8930ada_is_character_type (struct type *type)
14f9c5c9 8931{
7b9f71f2
JB
8932 const char *name;
8933
8934 /* If the type code says it's a character, then assume it really is,
8935 and don't check any further. */
78134374 8936 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8937 return true;
7b9f71f2
JB
8938
8939 /* Otherwise, assume it's a character type iff it is a discrete type
8940 with a known character type name. */
8941 name = ada_type_name (type);
8942 return (name != NULL
dda83cd7
SM
8943 && (type->code () == TYPE_CODE_INT
8944 || type->code () == TYPE_CODE_RANGE)
8945 && (strcmp (name, "character") == 0
8946 || strcmp (name, "wide_character") == 0
8947 || strcmp (name, "wide_wide_character") == 0
8948 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8949}
8950
4c4b4cd2 8951/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8952
fc913e53 8953bool
ebf56fd3 8954ada_is_string_type (struct type *type)
14f9c5c9 8955{
61ee279c 8956 type = ada_check_typedef (type);
d2e4a39e 8957 if (type != NULL
78134374 8958 && type->code () != TYPE_CODE_PTR
76a01679 8959 && (ada_is_simple_array_type (type)
dda83cd7 8960 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8961 && ada_array_arity (type) == 1)
8962 {
8963 struct type *elttype = ada_array_element_type (type, 1);
8964
8965 return ada_is_character_type (elttype);
8966 }
d2e4a39e 8967 else
fc913e53 8968 return false;
14f9c5c9
AS
8969}
8970
5bf03f13
JB
8971/* The compiler sometimes provides a parallel XVS type for a given
8972 PAD type. Normally, it is safe to follow the PAD type directly,
8973 but older versions of the compiler have a bug that causes the offset
8974 of its "F" field to be wrong. Following that field in that case
8975 would lead to incorrect results, but this can be worked around
8976 by ignoring the PAD type and using the associated XVS type instead.
8977
8978 Set to True if the debugger should trust the contents of PAD types.
8979 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8980static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8981
8982/* True if TYPE is a struct type introduced by the compiler to force the
8983 alignment of a value. Such types have a single field with a
4c4b4cd2 8984 distinctive name. */
14f9c5c9
AS
8985
8986int
ebf56fd3 8987ada_is_aligner_type (struct type *type)
14f9c5c9 8988{
61ee279c 8989 type = ada_check_typedef (type);
714e53ab 8990
5bf03f13 8991 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
8992 return 0;
8993
78134374 8994 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 8995 && type->num_fields () == 1
33d16dd9 8996 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
8997}
8998
8999/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 9000 the parallel type. */
14f9c5c9 9001
d2e4a39e
AS
9002struct type *
9003ada_get_base_type (struct type *raw_type)
14f9c5c9 9004{
d2e4a39e
AS
9005 struct type *real_type_namer;
9006 struct type *raw_real_type;
14f9c5c9 9007
78134374 9008 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
9009 return raw_type;
9010
284614f0
JB
9011 if (ada_is_aligner_type (raw_type))
9012 /* The encoding specifies that we should always use the aligner type.
9013 So, even if this aligner type has an associated XVS type, we should
9014 simply ignore it.
9015
9016 According to the compiler gurus, an XVS type parallel to an aligner
9017 type may exist because of a stabs limitation. In stabs, aligner
9018 types are empty because the field has a variable-sized type, and
9019 thus cannot actually be used as an aligner type. As a result,
9020 we need the associated parallel XVS type to decode the type.
9021 Since the policy in the compiler is to not change the internal
9022 representation based on the debugging info format, we sometimes
9023 end up having a redundant XVS type parallel to the aligner type. */
9024 return raw_type;
9025
14f9c5c9 9026 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9027 if (real_type_namer == NULL
78134374 9028 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 9029 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
9030 return raw_type;
9031
940da03e 9032 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
9033 {
9034 /* This is an older encoding form where the base type needs to be
85102364 9035 looked up by name. We prefer the newer encoding because it is
f80d3ff2 9036 more efficient. */
33d16dd9 9037 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
9038 if (raw_real_type == NULL)
9039 return raw_type;
9040 else
9041 return raw_real_type;
9042 }
9043
9044 /* The field in our XVS type is a reference to the base type. */
940da03e 9045 return TYPE_TARGET_TYPE (real_type_namer->field (0).type ());
d2e4a39e 9046}
14f9c5c9 9047
4c4b4cd2 9048/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9049
d2e4a39e
AS
9050struct type *
9051ada_aligned_type (struct type *type)
14f9c5c9
AS
9052{
9053 if (ada_is_aligner_type (type))
940da03e 9054 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
9055 else
9056 return ada_get_base_type (type);
9057}
9058
9059
9060/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9061 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9062
fc1a4b47
AC
9063const gdb_byte *
9064ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9065{
d2e4a39e 9066 if (ada_is_aligner_type (type))
b610c045
SM
9067 return ada_aligned_value_addr
9068 (type->field (0).type (),
9069 valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
14f9c5c9
AS
9070 else
9071 return valaddr;
9072}
9073
4c4b4cd2
PH
9074
9075
14f9c5c9 9076/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9077 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9078const char *
9079ada_enum_name (const char *name)
14f9c5c9 9080{
5f9febe0 9081 static std::string storage;
e6a959d6 9082 const char *tmp;
14f9c5c9 9083
4c4b4cd2
PH
9084 /* First, unqualify the enumeration name:
9085 1. Search for the last '.' character. If we find one, then skip
177b42fe 9086 all the preceding characters, the unqualified name starts
76a01679 9087 right after that dot.
4c4b4cd2 9088 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9089 translates dots into "__". Search forward for double underscores,
9090 but stop searching when we hit an overloading suffix, which is
9091 of the form "__" followed by digits. */
4c4b4cd2 9092
c3e5cd34
PH
9093 tmp = strrchr (name, '.');
9094 if (tmp != NULL)
4c4b4cd2
PH
9095 name = tmp + 1;
9096 else
14f9c5c9 9097 {
4c4b4cd2 9098 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9099 {
9100 if (isdigit (tmp[2]))
9101 break;
9102 else
9103 name = tmp + 2;
9104 }
14f9c5c9
AS
9105 }
9106
9107 if (name[0] == 'Q')
9108 {
14f9c5c9 9109 int v;
5b4ee69b 9110
14f9c5c9 9111 if (name[1] == 'U' || name[1] == 'W')
dda83cd7 9112 {
a7041de8
TT
9113 int offset = 2;
9114 if (name[1] == 'W' && name[2] == 'W')
9115 {
9116 /* Also handle the QWW case. */
9117 ++offset;
9118 }
9119 if (sscanf (name + offset, "%x", &v) != 1)
dda83cd7
SM
9120 return name;
9121 }
272560b5
TT
9122 else if (((name[1] >= '0' && name[1] <= '9')
9123 || (name[1] >= 'a' && name[1] <= 'z'))
9124 && name[2] == '\0')
9125 {
5f9febe0
TT
9126 storage = string_printf ("'%c'", name[1]);
9127 return storage.c_str ();
272560b5 9128 }
14f9c5c9 9129 else
dda83cd7 9130 return name;
14f9c5c9
AS
9131
9132 if (isascii (v) && isprint (v))
5f9febe0 9133 storage = string_printf ("'%c'", v);
14f9c5c9 9134 else if (name[1] == 'U')
a7041de8
TT
9135 storage = string_printf ("'[\"%02x\"]'", v);
9136 else if (name[2] != 'W')
9137 storage = string_printf ("'[\"%04x\"]'", v);
14f9c5c9 9138 else
a7041de8 9139 storage = string_printf ("'[\"%06x\"]'", v);
14f9c5c9 9140
5f9febe0 9141 return storage.c_str ();
14f9c5c9 9142 }
d2e4a39e 9143 else
4c4b4cd2 9144 {
c3e5cd34
PH
9145 tmp = strstr (name, "__");
9146 if (tmp == NULL)
9147 tmp = strstr (name, "$");
9148 if (tmp != NULL)
dda83cd7 9149 {
5f9febe0
TT
9150 storage = std::string (name, tmp - name);
9151 return storage.c_str ();
dda83cd7 9152 }
4c4b4cd2
PH
9153
9154 return name;
9155 }
14f9c5c9
AS
9156}
9157
14f9c5c9 9158/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9159 value it wraps. */
14f9c5c9 9160
d2e4a39e
AS
9161static struct value *
9162unwrap_value (struct value *val)
14f9c5c9 9163{
df407dfe 9164 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 9165
14f9c5c9
AS
9166 if (ada_is_aligner_type (type))
9167 {
de4d072f 9168 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 9169 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 9170
14f9c5c9 9171 if (ada_type_name (val_type) == NULL)
d0e39ea2 9172 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9173
9174 return unwrap_value (v);
9175 }
d2e4a39e 9176 else
14f9c5c9 9177 {
d2e4a39e 9178 struct type *raw_real_type =
dda83cd7 9179 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 9180
5bf03f13
JB
9181 /* If there is no parallel XVS or XVE type, then the value is
9182 already unwrapped. Return it without further modification. */
9183 if ((type == raw_real_type)
9184 && ada_find_parallel_type (type, "___XVE") == NULL)
9185 return val;
14f9c5c9 9186
d2e4a39e 9187 return
dda83cd7
SM
9188 coerce_unspec_val_to_type
9189 (val, ada_to_fixed_type (raw_real_type, 0,
9190 value_address (val),
9191 NULL, 1));
14f9c5c9
AS
9192 }
9193}
d2e4a39e 9194
d99dcf51
JB
9195/* Given two array types T1 and T2, return nonzero iff both arrays
9196 contain the same number of elements. */
9197
9198static int
9199ada_same_array_size_p (struct type *t1, struct type *t2)
9200{
9201 LONGEST lo1, hi1, lo2, hi2;
9202
9203 /* Get the array bounds in order to verify that the size of
9204 the two arrays match. */
9205 if (!get_array_bounds (t1, &lo1, &hi1)
9206 || !get_array_bounds (t2, &lo2, &hi2))
9207 error (_("unable to determine array bounds"));
9208
9209 /* To make things easier for size comparison, normalize a bit
9210 the case of empty arrays by making sure that the difference
9211 between upper bound and lower bound is always -1. */
9212 if (lo1 > hi1)
9213 hi1 = lo1 - 1;
9214 if (lo2 > hi2)
9215 hi2 = lo2 - 1;
9216
9217 return (hi1 - lo1 == hi2 - lo2);
9218}
9219
9220/* Assuming that VAL is an array of integrals, and TYPE represents
9221 an array with the same number of elements, but with wider integral
9222 elements, return an array "casted" to TYPE. In practice, this
9223 means that the returned array is built by casting each element
9224 of the original array into TYPE's (wider) element type. */
9225
9226static struct value *
9227ada_promote_array_of_integrals (struct type *type, struct value *val)
9228{
9229 struct type *elt_type = TYPE_TARGET_TYPE (type);
9230 LONGEST lo, hi;
d99dcf51
JB
9231 LONGEST i;
9232
9233 /* Verify that both val and type are arrays of scalars, and
9234 that the size of val's elements is smaller than the size
9235 of type's element. */
78134374 9236 gdb_assert (type->code () == TYPE_CODE_ARRAY);
d99dcf51 9237 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
78134374 9238 gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY);
d99dcf51
JB
9239 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
9240 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
9241 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
9242
9243 if (!get_array_bounds (type, &lo, &hi))
9244 error (_("unable to determine array bounds"));
9245
4bce7cda
SM
9246 value *res = allocate_value (type);
9247 gdb::array_view<gdb_byte> res_contents = value_contents_writeable (res);
d99dcf51
JB
9248
9249 /* Promote each array element. */
9250 for (i = 0; i < hi - lo + 1; i++)
9251 {
9252 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
4bce7cda 9253 int elt_len = TYPE_LENGTH (elt_type);
d99dcf51 9254
4bce7cda 9255 copy (value_contents_all (elt), res_contents.slice (elt_len * i, elt_len));
d99dcf51
JB
9256 }
9257
9258 return res;
9259}
9260
4c4b4cd2
PH
9261/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9262 return the converted value. */
9263
d2e4a39e
AS
9264static struct value *
9265coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9266{
df407dfe 9267 struct type *type2 = value_type (val);
5b4ee69b 9268
14f9c5c9
AS
9269 if (type == type2)
9270 return val;
9271
61ee279c
PH
9272 type2 = ada_check_typedef (type2);
9273 type = ada_check_typedef (type);
14f9c5c9 9274
78134374
SM
9275 if (type2->code () == TYPE_CODE_PTR
9276 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9277 {
9278 val = ada_value_ind (val);
df407dfe 9279 type2 = value_type (val);
14f9c5c9
AS
9280 }
9281
78134374
SM
9282 if (type2->code () == TYPE_CODE_ARRAY
9283 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9284 {
d99dcf51
JB
9285 if (!ada_same_array_size_p (type, type2))
9286 error (_("cannot assign arrays of different length"));
9287
9288 if (is_integral_type (TYPE_TARGET_TYPE (type))
9289 && is_integral_type (TYPE_TARGET_TYPE (type2))
9290 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
9291 < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9292 {
9293 /* Allow implicit promotion of the array elements to
9294 a wider type. */
9295 return ada_promote_array_of_integrals (type, val);
9296 }
9297
9298 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
dda83cd7
SM
9299 != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9300 error (_("Incompatible types in assignment"));
04624583 9301 deprecated_set_value_type (val, type);
14f9c5c9 9302 }
d2e4a39e 9303 return val;
14f9c5c9
AS
9304}
9305
4c4b4cd2
PH
9306static struct value *
9307ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9308{
9309 struct value *val;
9310 struct type *type1, *type2;
9311 LONGEST v, v1, v2;
9312
994b9211
AC
9313 arg1 = coerce_ref (arg1);
9314 arg2 = coerce_ref (arg2);
18af8284
JB
9315 type1 = get_base_type (ada_check_typedef (value_type (arg1)));
9316 type2 = get_base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 9317
78134374
SM
9318 if (type1->code () != TYPE_CODE_INT
9319 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9320 return value_binop (arg1, arg2, op);
9321
76a01679 9322 switch (op)
4c4b4cd2
PH
9323 {
9324 case BINOP_MOD:
9325 case BINOP_DIV:
9326 case BINOP_REM:
9327 break;
9328 default:
9329 return value_binop (arg1, arg2, op);
9330 }
9331
9332 v2 = value_as_long (arg2);
9333 if (v2 == 0)
b0f9164c
TT
9334 {
9335 const char *name;
9336 if (op == BINOP_MOD)
9337 name = "mod";
9338 else if (op == BINOP_DIV)
9339 name = "/";
9340 else
9341 {
9342 gdb_assert (op == BINOP_REM);
9343 name = "rem";
9344 }
9345
9346 error (_("second operand of %s must not be zero."), name);
9347 }
4c4b4cd2 9348
c6d940a9 9349 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9350 return value_binop (arg1, arg2, op);
9351
9352 v1 = value_as_long (arg1);
9353 switch (op)
9354 {
9355 case BINOP_DIV:
9356 v = v1 / v2;
76a01679 9357 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
dda83cd7 9358 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9359 break;
9360 case BINOP_REM:
9361 v = v1 % v2;
76a01679 9362 if (v * v1 < 0)
dda83cd7 9363 v -= v2;
4c4b4cd2
PH
9364 break;
9365 default:
9366 /* Should not reach this point. */
9367 v = 0;
9368 }
9369
9370 val = allocate_value (type1);
50888e42 9371 store_unsigned_integer (value_contents_raw (val).data (),
dda83cd7 9372 TYPE_LENGTH (value_type (val)),
34877895 9373 type_byte_order (type1), v);
4c4b4cd2
PH
9374 return val;
9375}
9376
9377static int
9378ada_value_equal (struct value *arg1, struct value *arg2)
9379{
df407dfe
AC
9380 if (ada_is_direct_array_type (value_type (arg1))
9381 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 9382 {
79e8fcaa
JB
9383 struct type *arg1_type, *arg2_type;
9384
f58b38bf 9385 /* Automatically dereference any array reference before
dda83cd7 9386 we attempt to perform the comparison. */
f58b38bf
JB
9387 arg1 = ada_coerce_ref (arg1);
9388 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9389
4c4b4cd2
PH
9390 arg1 = ada_coerce_to_simple_array (arg1);
9391 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa
JB
9392
9393 arg1_type = ada_check_typedef (value_type (arg1));
9394 arg2_type = ada_check_typedef (value_type (arg2));
9395
78134374 9396 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9397 || arg2_type->code () != TYPE_CODE_ARRAY)
9398 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9399 /* FIXME: The following works only for types whose
dda83cd7
SM
9400 representations use all bits (no padding or undefined bits)
9401 and do not have user-defined equality. */
79e8fcaa 9402 return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
50888e42
SM
9403 && memcmp (value_contents (arg1).data (),
9404 value_contents (arg2).data (),
79e8fcaa 9405 TYPE_LENGTH (arg1_type)) == 0);
4c4b4cd2
PH
9406 }
9407 return value_equal (arg1, arg2);
9408}
9409
d3c54a1c
TT
9410namespace expr
9411{
9412
9413bool
9414check_objfile (const std::unique_ptr<ada_component> &comp,
9415 struct objfile *objfile)
9416{
9417 return comp->uses_objfile (objfile);
9418}
9419
9420/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9421 component of LHS (a simple array or a record). Does not modify the
9422 inferior's memory, nor does it modify LHS (unless LHS ==
9423 CONTAINER). */
52ce6436
PH
9424
9425static void
9426assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9427 struct expression *exp, operation_up &arg)
52ce6436 9428{
d3c54a1c
TT
9429 scoped_value_mark mark;
9430
52ce6436 9431 struct value *elt;
0e2da9f0 9432 struct type *lhs_type = check_typedef (value_type (lhs));
5b4ee69b 9433
78134374 9434 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9435 {
22601c15
UW
9436 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9437 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9438
52ce6436
PH
9439 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9440 }
9441 else
9442 {
9443 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
c48db5ca 9444 elt = ada_to_fixed_value (elt);
52ce6436
PH
9445 }
9446
d3c54a1c
TT
9447 ada_aggregate_operation *ag_op
9448 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9449 if (ag_op != nullptr)
9450 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9451 else
d3c54a1c
TT
9452 value_assign_to_component (container, elt,
9453 arg->evaluate (nullptr, exp,
9454 EVAL_NORMAL));
9455}
52ce6436 9456
d3c54a1c
TT
9457bool
9458ada_aggregate_component::uses_objfile (struct objfile *objfile)
9459{
9460 for (const auto &item : m_components)
9461 if (item->uses_objfile (objfile))
9462 return true;
9463 return false;
9464}
9465
9466void
9467ada_aggregate_component::dump (ui_file *stream, int depth)
9468{
9469 fprintf_filtered (stream, _("%*sAggregate\n"), depth, "");
9470 for (const auto &item : m_components)
9471 item->dump (stream, depth + 1);
9472}
9473
9474void
9475ada_aggregate_component::assign (struct value *container,
9476 struct value *lhs, struct expression *exp,
9477 std::vector<LONGEST> &indices,
9478 LONGEST low, LONGEST high)
9479{
9480 for (auto &item : m_components)
9481 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9482}
9483
207582c0 9484/* See ada-exp.h. */
52ce6436 9485
207582c0 9486value *
d3c54a1c
TT
9487ada_aggregate_operation::assign_aggregate (struct value *container,
9488 struct value *lhs,
9489 struct expression *exp)
52ce6436
PH
9490{
9491 struct type *lhs_type;
52ce6436 9492 LONGEST low_index, high_index;
52ce6436
PH
9493
9494 container = ada_coerce_ref (container);
9495 if (ada_is_direct_array_type (value_type (container)))
9496 container = ada_coerce_to_simple_array (container);
9497 lhs = ada_coerce_ref (lhs);
9498 if (!deprecated_value_modifiable (lhs))
9499 error (_("Left operand of assignment is not a modifiable lvalue."));
9500
0e2da9f0 9501 lhs_type = check_typedef (value_type (lhs));
52ce6436
PH
9502 if (ada_is_direct_array_type (lhs_type))
9503 {
9504 lhs = ada_coerce_to_simple_array (lhs);
0e2da9f0 9505 lhs_type = check_typedef (value_type (lhs));
cf88be68
SM
9506 low_index = lhs_type->bounds ()->low.const_val ();
9507 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9508 }
78134374 9509 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9510 {
9511 low_index = 0;
9512 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9513 }
9514 else
9515 error (_("Left-hand side must be array or record."));
9516
cf608cc4 9517 std::vector<LONGEST> indices (4);
52ce6436
PH
9518 indices[0] = indices[1] = low_index - 1;
9519 indices[2] = indices[3] = high_index + 1;
52ce6436 9520
d3c54a1c
TT
9521 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9522 low_index, high_index);
207582c0
TT
9523
9524 return container;
d3c54a1c
TT
9525}
9526
9527bool
9528ada_positional_component::uses_objfile (struct objfile *objfile)
9529{
9530 return m_op->uses_objfile (objfile);
9531}
52ce6436 9532
d3c54a1c
TT
9533void
9534ada_positional_component::dump (ui_file *stream, int depth)
9535{
9536 fprintf_filtered (stream, _("%*sPositional, index = %d\n"),
9537 depth, "", m_index);
9538 m_op->dump (stream, depth + 1);
52ce6436 9539}
d3c54a1c 9540
52ce6436 9541/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9542 construct, given that the positions are relative to lower bound
9543 LOW, where HIGH is the upper bound. Record the position in
9544 INDICES. CONTAINER is as for assign_aggregate. */
9545void
9546ada_positional_component::assign (struct value *container,
9547 struct value *lhs, struct expression *exp,
9548 std::vector<LONGEST> &indices,
9549 LONGEST low, LONGEST high)
52ce6436 9550{
d3c54a1c
TT
9551 LONGEST ind = m_index + low;
9552
52ce6436 9553 if (ind - 1 == high)
e1d5a0d2 9554 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9555 if (ind <= high)
9556 {
cf608cc4 9557 add_component_interval (ind, ind, indices);
d3c54a1c 9558 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9559 }
52ce6436
PH
9560}
9561
d3c54a1c
TT
9562bool
9563ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9564{
9565 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9566}
9567
9568void
9569ada_discrete_range_association::dump (ui_file *stream, int depth)
9570{
9571 fprintf_filtered (stream, _("%*sDiscrete range:\n"), depth, "");
9572 m_low->dump (stream, depth + 1);
9573 m_high->dump (stream, depth + 1);
9574}
9575
9576void
9577ada_discrete_range_association::assign (struct value *container,
9578 struct value *lhs,
9579 struct expression *exp,
9580 std::vector<LONGEST> &indices,
9581 LONGEST low, LONGEST high,
9582 operation_up &op)
9583{
9584 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9585 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9586
9587 if (lower <= upper && (lower < low || upper > high))
9588 error (_("Index in component association out of bounds."));
9589
9590 add_component_interval (lower, upper, indices);
9591 while (lower <= upper)
9592 {
9593 assign_component (container, lhs, lower, exp, op);
9594 lower += 1;
9595 }
9596}
9597
9598bool
9599ada_name_association::uses_objfile (struct objfile *objfile)
9600{
9601 return m_val->uses_objfile (objfile);
9602}
9603
9604void
9605ada_name_association::dump (ui_file *stream, int depth)
9606{
9607 fprintf_filtered (stream, _("%*sName:\n"), depth, "");
9608 m_val->dump (stream, depth + 1);
9609}
9610
9611void
9612ada_name_association::assign (struct value *container,
9613 struct value *lhs,
9614 struct expression *exp,
9615 std::vector<LONGEST> &indices,
9616 LONGEST low, LONGEST high,
9617 operation_up &op)
9618{
9619 int index;
9620
9621 if (ada_is_direct_array_type (value_type (lhs)))
9622 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9623 EVAL_NORMAL)));
9624 else
9625 {
9626 ada_string_operation *strop
9627 = dynamic_cast<ada_string_operation *> (m_val.get ());
9628
9629 const char *name;
9630 if (strop != nullptr)
9631 name = strop->get_name ();
9632 else
9633 {
9634 ada_var_value_operation *vvo
9635 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
9636 if (vvo != nullptr)
9637 error (_("Invalid record component association."));
9638 name = vvo->get_symbol ()->natural_name ();
9639 }
9640
9641 index = 0;
9642 if (! find_struct_field (name, value_type (lhs), 0,
9643 NULL, NULL, NULL, NULL, &index))
9644 error (_("Unknown component name: %s."), name);
9645 }
9646
9647 add_component_interval (index, index, indices);
9648 assign_component (container, lhs, index, exp, op);
9649}
9650
9651bool
9652ada_choices_component::uses_objfile (struct objfile *objfile)
9653{
9654 if (m_op->uses_objfile (objfile))
9655 return true;
9656 for (const auto &item : m_assocs)
9657 if (item->uses_objfile (objfile))
9658 return true;
9659 return false;
9660}
9661
9662void
9663ada_choices_component::dump (ui_file *stream, int depth)
9664{
9665 fprintf_filtered (stream, _("%*sChoices:\n"), depth, "");
9666 m_op->dump (stream, depth + 1);
9667 for (const auto &item : m_assocs)
9668 item->dump (stream, depth + 1);
9669}
9670
9671/* Assign into the components of LHS indexed by the OP_CHOICES
9672 construct at *POS, updating *POS past the construct, given that
9673 the allowable indices are LOW..HIGH. Record the indices assigned
9674 to in INDICES. CONTAINER is as for assign_aggregate. */
9675void
9676ada_choices_component::assign (struct value *container,
9677 struct value *lhs, struct expression *exp,
9678 std::vector<LONGEST> &indices,
9679 LONGEST low, LONGEST high)
9680{
9681 for (auto &item : m_assocs)
9682 item->assign (container, lhs, exp, indices, low, high, m_op);
9683}
9684
9685bool
9686ada_others_component::uses_objfile (struct objfile *objfile)
9687{
9688 return m_op->uses_objfile (objfile);
9689}
9690
9691void
9692ada_others_component::dump (ui_file *stream, int depth)
9693{
9694 fprintf_filtered (stream, _("%*sOthers:\n"), depth, "");
9695 m_op->dump (stream, depth + 1);
9696}
9697
9698/* Assign the value of the expression in the OP_OTHERS construct in
9699 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9700 have not been previously assigned. The index intervals already assigned
9701 are in INDICES. CONTAINER is as for assign_aggregate. */
9702void
9703ada_others_component::assign (struct value *container,
9704 struct value *lhs, struct expression *exp,
9705 std::vector<LONGEST> &indices,
9706 LONGEST low, LONGEST high)
9707{
9708 int num_indices = indices.size ();
9709 for (int i = 0; i < num_indices - 2; i += 2)
9710 {
9711 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9712 assign_component (container, lhs, ind, exp, m_op);
9713 }
9714}
9715
9716struct value *
9717ada_assign_operation::evaluate (struct type *expect_type,
9718 struct expression *exp,
9719 enum noside noside)
9720{
9721 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
9722
9723 ada_aggregate_operation *ag_op
9724 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9725 if (ag_op != nullptr)
9726 {
9727 if (noside != EVAL_NORMAL)
9728 return arg1;
9729
207582c0 9730 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9731 return ada_value_assign (arg1, arg1);
9732 }
9733 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9734 except if the lhs of our assignment is a convenience variable.
9735 In the case of assigning to a convenience variable, the lhs
9736 should be exactly the result of the evaluation of the rhs. */
9737 struct type *type = value_type (arg1);
9738 if (VALUE_LVAL (arg1) == lval_internalvar)
9739 type = NULL;
9740 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9741 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354
TT
9742 return arg1;
9743 if (VALUE_LVAL (arg1) == lval_internalvar)
9744 {
9745 /* Nothing. */
9746 }
9747 else
9748 arg2 = coerce_for_assign (value_type (arg1), arg2);
9749 return ada_value_assign (arg1, arg2);
9750}
9751
9752} /* namespace expr */
9753
cf608cc4
TT
9754/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9755 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9756 overlap. */
52ce6436
PH
9757static void
9758add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9759 std::vector<LONGEST> &indices)
52ce6436
PH
9760{
9761 int i, j;
5b4ee69b 9762
cf608cc4
TT
9763 int size = indices.size ();
9764 for (i = 0; i < size; i += 2) {
52ce6436
PH
9765 if (high >= indices[i] && low <= indices[i + 1])
9766 {
9767 int kh;
5b4ee69b 9768
cf608cc4 9769 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9770 if (high < indices[kh])
9771 break;
9772 if (low < indices[i])
9773 indices[i] = low;
9774 indices[i + 1] = indices[kh - 1];
9775 if (high > indices[i + 1])
9776 indices[i + 1] = high;
cf608cc4
TT
9777 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9778 indices.resize (kh - i - 2);
52ce6436
PH
9779 return;
9780 }
9781 else if (high < indices[i])
9782 break;
9783 }
9784
cf608cc4 9785 indices.resize (indices.size () + 2);
d4813f10 9786 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9787 indices[j] = indices[j - 2];
9788 indices[i] = low;
9789 indices[i + 1] = high;
9790}
9791
6e48bd2c
JB
9792/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9793 is different. */
9794
9795static struct value *
b7e22850 9796ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c
JB
9797{
9798 if (type == ada_check_typedef (value_type (arg2)))
9799 return arg2;
9800
6e48bd2c
JB
9801 return value_cast (type, arg2);
9802}
9803
284614f0
JB
9804/* Evaluating Ada expressions, and printing their result.
9805 ------------------------------------------------------
9806
21649b50
JB
9807 1. Introduction:
9808 ----------------
9809
284614f0
JB
9810 We usually evaluate an Ada expression in order to print its value.
9811 We also evaluate an expression in order to print its type, which
9812 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9813 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9814 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9815 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9816 similar.
9817
9818 Evaluating expressions is a little more complicated for Ada entities
9819 than it is for entities in languages such as C. The main reason for
9820 this is that Ada provides types whose definition might be dynamic.
9821 One example of such types is variant records. Or another example
9822 would be an array whose bounds can only be known at run time.
9823
9824 The following description is a general guide as to what should be
9825 done (and what should NOT be done) in order to evaluate an expression
9826 involving such types, and when. This does not cover how the semantic
9827 information is encoded by GNAT as this is covered separatly. For the
9828 document used as the reference for the GNAT encoding, see exp_dbug.ads
9829 in the GNAT sources.
9830
9831 Ideally, we should embed each part of this description next to its
9832 associated code. Unfortunately, the amount of code is so vast right
9833 now that it's hard to see whether the code handling a particular
9834 situation might be duplicated or not. One day, when the code is
9835 cleaned up, this guide might become redundant with the comments
9836 inserted in the code, and we might want to remove it.
9837
21649b50
JB
9838 2. ``Fixing'' an Entity, the Simple Case:
9839 -----------------------------------------
9840
284614f0
JB
9841 When evaluating Ada expressions, the tricky issue is that they may
9842 reference entities whose type contents and size are not statically
9843 known. Consider for instance a variant record:
9844
9845 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9846 case Empty is
9847 when True => null;
9848 when False => Value : Integer;
9849 end case;
284614f0
JB
9850 end record;
9851 Yes : Rec := (Empty => False, Value => 1);
9852 No : Rec := (empty => True);
9853
9854 The size and contents of that record depends on the value of the
9855 descriminant (Rec.Empty). At this point, neither the debugging
9856 information nor the associated type structure in GDB are able to
9857 express such dynamic types. So what the debugger does is to create
9858 "fixed" versions of the type that applies to the specific object.
30baf67b 9859 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9860 which means creating its associated fixed type.
9861
9862 Example: when printing the value of variable "Yes" above, its fixed
9863 type would look like this:
9864
9865 type Rec is record
dda83cd7
SM
9866 Empty : Boolean;
9867 Value : Integer;
284614f0
JB
9868 end record;
9869
9870 On the other hand, if we printed the value of "No", its fixed type
9871 would become:
9872
9873 type Rec is record
dda83cd7 9874 Empty : Boolean;
284614f0
JB
9875 end record;
9876
9877 Things become a little more complicated when trying to fix an entity
9878 with a dynamic type that directly contains another dynamic type,
9879 such as an array of variant records, for instance. There are
9880 two possible cases: Arrays, and records.
9881
21649b50
JB
9882 3. ``Fixing'' Arrays:
9883 ---------------------
9884
9885 The type structure in GDB describes an array in terms of its bounds,
9886 and the type of its elements. By design, all elements in the array
9887 have the same type and we cannot represent an array of variant elements
9888 using the current type structure in GDB. When fixing an array,
9889 we cannot fix the array element, as we would potentially need one
9890 fixed type per element of the array. As a result, the best we can do
9891 when fixing an array is to produce an array whose bounds and size
9892 are correct (allowing us to read it from memory), but without having
9893 touched its element type. Fixing each element will be done later,
9894 when (if) necessary.
9895
9896 Arrays are a little simpler to handle than records, because the same
9897 amount of memory is allocated for each element of the array, even if
1b536f04 9898 the amount of space actually used by each element differs from element
21649b50 9899 to element. Consider for instance the following array of type Rec:
284614f0
JB
9900
9901 type Rec_Array is array (1 .. 2) of Rec;
9902
1b536f04
JB
9903 The actual amount of memory occupied by each element might be different
9904 from element to element, depending on the value of their discriminant.
21649b50 9905 But the amount of space reserved for each element in the array remains
1b536f04 9906 fixed regardless. So we simply need to compute that size using
21649b50
JB
9907 the debugging information available, from which we can then determine
9908 the array size (we multiply the number of elements of the array by
9909 the size of each element).
9910
9911 The simplest case is when we have an array of a constrained element
9912 type. For instance, consider the following type declarations:
9913
dda83cd7
SM
9914 type Bounded_String (Max_Size : Integer) is
9915 Length : Integer;
9916 Buffer : String (1 .. Max_Size);
9917 end record;
9918 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9919
9920 In this case, the compiler describes the array as an array of
9921 variable-size elements (identified by its XVS suffix) for which
9922 the size can be read in the parallel XVZ variable.
9923
9924 In the case of an array of an unconstrained element type, the compiler
9925 wraps the array element inside a private PAD type. This type should not
9926 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9927 that we also use the adjective "aligner" in our code to designate
9928 these wrapper types.
9929
1b536f04 9930 In some cases, the size allocated for each element is statically
21649b50
JB
9931 known. In that case, the PAD type already has the correct size,
9932 and the array element should remain unfixed.
9933
9934 But there are cases when this size is not statically known.
9935 For instance, assuming that "Five" is an integer variable:
284614f0 9936
dda83cd7
SM
9937 type Dynamic is array (1 .. Five) of Integer;
9938 type Wrapper (Has_Length : Boolean := False) is record
9939 Data : Dynamic;
9940 case Has_Length is
9941 when True => Length : Integer;
9942 when False => null;
9943 end case;
9944 end record;
9945 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9946
dda83cd7
SM
9947 Hello : Wrapper_Array := (others => (Has_Length => True,
9948 Data => (others => 17),
9949 Length => 1));
284614f0
JB
9950
9951
9952 The debugging info would describe variable Hello as being an
9953 array of a PAD type. The size of that PAD type is not statically
9954 known, but can be determined using a parallel XVZ variable.
9955 In that case, a copy of the PAD type with the correct size should
9956 be used for the fixed array.
9957
21649b50
JB
9958 3. ``Fixing'' record type objects:
9959 ----------------------------------
9960
9961 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9962 record types. In this case, in order to compute the associated
9963 fixed type, we need to determine the size and offset of each of
9964 its components. This, in turn, requires us to compute the fixed
9965 type of each of these components.
9966
9967 Consider for instance the example:
9968
dda83cd7
SM
9969 type Bounded_String (Max_Size : Natural) is record
9970 Str : String (1 .. Max_Size);
9971 Length : Natural;
9972 end record;
9973 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9974
9975 In that case, the position of field "Length" depends on the size
9976 of field Str, which itself depends on the value of the Max_Size
21649b50 9977 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9978 we need to fix the type of field Str. Therefore, fixing a variant
9979 record requires us to fix each of its components.
9980
9981 However, if a component does not have a dynamic size, the component
9982 should not be fixed. In particular, fields that use a PAD type
9983 should not fixed. Here is an example where this might happen
9984 (assuming type Rec above):
9985
9986 type Container (Big : Boolean) is record
dda83cd7
SM
9987 First : Rec;
9988 After : Integer;
9989 case Big is
9990 when True => Another : Integer;
9991 when False => null;
9992 end case;
284614f0
JB
9993 end record;
9994 My_Container : Container := (Big => False,
dda83cd7
SM
9995 First => (Empty => True),
9996 After => 42);
284614f0
JB
9997
9998 In that example, the compiler creates a PAD type for component First,
9999 whose size is constant, and then positions the component After just
10000 right after it. The offset of component After is therefore constant
10001 in this case.
10002
10003 The debugger computes the position of each field based on an algorithm
10004 that uses, among other things, the actual position and size of the field
21649b50
JB
10005 preceding it. Let's now imagine that the user is trying to print
10006 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
10007 end up computing the offset of field After based on the size of the
10008 fixed version of field First. And since in our example First has
10009 only one actual field, the size of the fixed type is actually smaller
10010 than the amount of space allocated to that field, and thus we would
10011 compute the wrong offset of field After.
10012
21649b50
JB
10013 To make things more complicated, we need to watch out for dynamic
10014 components of variant records (identified by the ___XVL suffix in
10015 the component name). Even if the target type is a PAD type, the size
10016 of that type might not be statically known. So the PAD type needs
10017 to be unwrapped and the resulting type needs to be fixed. Otherwise,
10018 we might end up with the wrong size for our component. This can be
10019 observed with the following type declarations:
284614f0 10020
dda83cd7
SM
10021 type Octal is new Integer range 0 .. 7;
10022 type Octal_Array is array (Positive range <>) of Octal;
10023 pragma Pack (Octal_Array);
284614f0 10024
dda83cd7
SM
10025 type Octal_Buffer (Size : Positive) is record
10026 Buffer : Octal_Array (1 .. Size);
10027 Length : Integer;
10028 end record;
284614f0
JB
10029
10030 In that case, Buffer is a PAD type whose size is unset and needs
10031 to be computed by fixing the unwrapped type.
10032
21649b50
JB
10033 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
10034 ----------------------------------------------------------
10035
10036 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
10037 thus far, be actually fixed?
10038
10039 The answer is: Only when referencing that element. For instance
10040 when selecting one component of a record, this specific component
10041 should be fixed at that point in time. Or when printing the value
10042 of a record, each component should be fixed before its value gets
10043 printed. Similarly for arrays, the element of the array should be
10044 fixed when printing each element of the array, or when extracting
10045 one element out of that array. On the other hand, fixing should
10046 not be performed on the elements when taking a slice of an array!
10047
31432a67 10048 Note that one of the side effects of miscomputing the offset and
284614f0
JB
10049 size of each field is that we end up also miscomputing the size
10050 of the containing type. This can have adverse results when computing
10051 the value of an entity. GDB fetches the value of an entity based
10052 on the size of its type, and thus a wrong size causes GDB to fetch
10053 the wrong amount of memory. In the case where the computed size is
10054 too small, GDB fetches too little data to print the value of our
31432a67 10055 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
10056 past the buffer containing the data =:-o. */
10057
62d4bd94
TT
10058/* A helper function for TERNOP_IN_RANGE. */
10059
10060static value *
10061eval_ternop_in_range (struct type *expect_type, struct expression *exp,
10062 enum noside noside,
10063 value *arg1, value *arg2, value *arg3)
10064{
62d4bd94
TT
10065 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10066 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10067 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10068 return
10069 value_from_longest (type,
10070 (value_less (arg1, arg3)
10071 || value_equal (arg1, arg3))
10072 && (value_less (arg2, arg1)
10073 || value_equal (arg2, arg1)));
10074}
10075
82390ab8
TT
10076/* A helper function for UNOP_NEG. */
10077
7c15d377 10078value *
82390ab8
TT
10079ada_unop_neg (struct type *expect_type,
10080 struct expression *exp,
10081 enum noside noside, enum exp_opcode op,
10082 struct value *arg1)
10083{
82390ab8
TT
10084 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10085 return value_neg (arg1);
10086}
10087
7efc87ff
TT
10088/* A helper function for UNOP_IN_RANGE. */
10089
95d49dfb 10090value *
7efc87ff
TT
10091ada_unop_in_range (struct type *expect_type,
10092 struct expression *exp,
10093 enum noside noside, enum exp_opcode op,
10094 struct value *arg1, struct type *type)
10095{
7efc87ff
TT
10096 struct value *arg2, *arg3;
10097 switch (type->code ())
10098 {
10099 default:
10100 lim_warning (_("Membership test incompletely implemented; "
10101 "always returns true"));
10102 type = language_bool_type (exp->language_defn, exp->gdbarch);
10103 return value_from_longest (type, (LONGEST) 1);
10104
10105 case TYPE_CODE_RANGE:
10106 arg2 = value_from_longest (type,
10107 type->bounds ()->low.const_val ());
10108 arg3 = value_from_longest (type,
10109 type->bounds ()->high.const_val ());
10110 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10111 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10112 type = language_bool_type (exp->language_defn, exp->gdbarch);
10113 return
10114 value_from_longest (type,
10115 (value_less (arg1, arg3)
10116 || value_equal (arg1, arg3))
10117 && (value_less (arg2, arg1)
10118 || value_equal (arg2, arg1)));
10119 }
10120}
10121
020dbabe
TT
10122/* A helper function for OP_ATR_TAG. */
10123
7c15d377 10124value *
020dbabe
TT
10125ada_atr_tag (struct type *expect_type,
10126 struct expression *exp,
10127 enum noside noside, enum exp_opcode op,
10128 struct value *arg1)
10129{
10130 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10131 return value_zero (ada_tag_type (arg1), not_lval);
10132
10133 return ada_value_tag (arg1);
10134}
10135
68c75735
TT
10136/* A helper function for OP_ATR_SIZE. */
10137
7c15d377 10138value *
68c75735
TT
10139ada_atr_size (struct type *expect_type,
10140 struct expression *exp,
10141 enum noside noside, enum exp_opcode op,
10142 struct value *arg1)
10143{
10144 struct type *type = value_type (arg1);
10145
10146 /* If the argument is a reference, then dereference its type, since
10147 the user is really asking for the size of the actual object,
10148 not the size of the pointer. */
10149 if (type->code () == TYPE_CODE_REF)
10150 type = TYPE_TARGET_TYPE (type);
10151
0b2b0b82 10152 if (noside == EVAL_AVOID_SIDE_EFFECTS)
68c75735
TT
10153 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
10154 else
10155 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
10156 TARGET_CHAR_BIT * TYPE_LENGTH (type));
10157}
10158
d05e24e6
TT
10159/* A helper function for UNOP_ABS. */
10160
7c15d377 10161value *
d05e24e6
TT
10162ada_abs (struct type *expect_type,
10163 struct expression *exp,
10164 enum noside noside, enum exp_opcode op,
10165 struct value *arg1)
10166{
10167 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10168 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
10169 return value_neg (arg1);
10170 else
10171 return arg1;
10172}
10173
faa1dfd7
TT
10174/* A helper function for BINOP_MUL. */
10175
d9e7db06 10176value *
faa1dfd7
TT
10177ada_mult_binop (struct type *expect_type,
10178 struct expression *exp,
10179 enum noside noside, enum exp_opcode op,
10180 struct value *arg1, struct value *arg2)
10181{
10182 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10183 {
10184 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10185 return value_zero (value_type (arg1), not_lval);
10186 }
10187 else
10188 {
10189 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10190 return ada_value_binop (arg1, arg2, op);
10191 }
10192}
10193
214b13ac
TT
10194/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10195
6e8fb7b7 10196value *
214b13ac
TT
10197ada_equal_binop (struct type *expect_type,
10198 struct expression *exp,
10199 enum noside noside, enum exp_opcode op,
10200 struct value *arg1, struct value *arg2)
10201{
10202 int tem;
10203 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10204 tem = 0;
10205 else
10206 {
10207 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10208 tem = ada_value_equal (arg1, arg2);
10209 }
10210 if (op == BINOP_NOTEQUAL)
10211 tem = !tem;
10212 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10213 return value_from_longest (type, (LONGEST) tem);
10214}
10215
5ce19db8
TT
10216/* A helper function for TERNOP_SLICE. */
10217
1b1ebfab 10218value *
5ce19db8
TT
10219ada_ternop_slice (struct expression *exp,
10220 enum noside noside,
10221 struct value *array, struct value *low_bound_val,
10222 struct value *high_bound_val)
10223{
10224 LONGEST low_bound;
10225 LONGEST high_bound;
10226
10227 low_bound_val = coerce_ref (low_bound_val);
10228 high_bound_val = coerce_ref (high_bound_val);
10229 low_bound = value_as_long (low_bound_val);
10230 high_bound = value_as_long (high_bound_val);
10231
10232 /* If this is a reference to an aligner type, then remove all
10233 the aligners. */
10234 if (value_type (array)->code () == TYPE_CODE_REF
10235 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
10236 TYPE_TARGET_TYPE (value_type (array)) =
10237 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
10238
10239 if (ada_is_any_packed_array_type (value_type (array)))
10240 error (_("cannot slice a packed array"));
10241
10242 /* If this is a reference to an array or an array lvalue,
10243 convert to a pointer. */
10244 if (value_type (array)->code () == TYPE_CODE_REF
10245 || (value_type (array)->code () == TYPE_CODE_ARRAY
10246 && VALUE_LVAL (array) == lval_memory))
10247 array = value_addr (array);
10248
10249 if (noside == EVAL_AVOID_SIDE_EFFECTS
10250 && ada_is_array_descriptor_type (ada_check_typedef
10251 (value_type (array))))
10252 return empty_array (ada_type_of_array (array, 0), low_bound,
10253 high_bound);
10254
10255 array = ada_coerce_to_simple_array_ptr (array);
10256
10257 /* If we have more than one level of pointer indirection,
10258 dereference the value until we get only one level. */
10259 while (value_type (array)->code () == TYPE_CODE_PTR
10260 && (TYPE_TARGET_TYPE (value_type (array))->code ()
10261 == TYPE_CODE_PTR))
10262 array = value_ind (array);
10263
10264 /* Make sure we really do have an array type before going further,
10265 to avoid a SEGV when trying to get the index type or the target
10266 type later down the road if the debug info generated by
10267 the compiler is incorrect or incomplete. */
10268 if (!ada_is_simple_array_type (value_type (array)))
10269 error (_("cannot take slice of non-array"));
10270
10271 if (ada_check_typedef (value_type (array))->code ()
10272 == TYPE_CODE_PTR)
10273 {
10274 struct type *type0 = ada_check_typedef (value_type (array));
10275
10276 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
10277 return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
10278 else
10279 {
10280 struct type *arr_type0 =
10281 to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
10282
10283 return ada_value_slice_from_ptr (array, arr_type0,
10284 longest_to_int (low_bound),
10285 longest_to_int (high_bound));
10286 }
10287 }
10288 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10289 return array;
10290 else if (high_bound < low_bound)
10291 return empty_array (value_type (array), low_bound, high_bound);
10292 else
10293 return ada_value_slice (array, longest_to_int (low_bound),
10294 longest_to_int (high_bound));
10295}
10296
b467efaa
TT
10297/* A helper function for BINOP_IN_BOUNDS. */
10298
82c3886e 10299value *
b467efaa
TT
10300ada_binop_in_bounds (struct expression *exp, enum noside noside,
10301 struct value *arg1, struct value *arg2, int n)
10302{
10303 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10304 {
10305 struct type *type = language_bool_type (exp->language_defn,
10306 exp->gdbarch);
10307 return value_zero (type, not_lval);
10308 }
10309
10310 struct type *type = ada_index_type (value_type (arg2), n, "range");
10311 if (!type)
10312 type = value_type (arg1);
10313
10314 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10315 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10316
10317 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10318 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10319 type = language_bool_type (exp->language_defn, exp->gdbarch);
10320 return value_from_longest (type,
10321 (value_less (arg1, arg3)
10322 || value_equal (arg1, arg3))
10323 && (value_less (arg2, arg1)
10324 || value_equal (arg2, arg1)));
10325}
10326
b84564fc
TT
10327/* A helper function for some attribute operations. */
10328
10329static value *
10330ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10331 struct value *arg1, struct type *type_arg, int tem)
10332{
10333 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10334 {
10335 if (type_arg == NULL)
10336 type_arg = value_type (arg1);
10337
10338 if (ada_is_constrained_packed_array_type (type_arg))
10339 type_arg = decode_constrained_packed_array_type (type_arg);
10340
10341 if (!discrete_type_p (type_arg))
10342 {
10343 switch (op)
10344 {
10345 default: /* Should never happen. */
10346 error (_("unexpected attribute encountered"));
10347 case OP_ATR_FIRST:
10348 case OP_ATR_LAST:
10349 type_arg = ada_index_type (type_arg, tem,
10350 ada_attribute_name (op));
10351 break;
10352 case OP_ATR_LENGTH:
10353 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10354 break;
10355 }
10356 }
10357
10358 return value_zero (type_arg, not_lval);
10359 }
10360 else if (type_arg == NULL)
10361 {
10362 arg1 = ada_coerce_ref (arg1);
10363
10364 if (ada_is_constrained_packed_array_type (value_type (arg1)))
10365 arg1 = ada_coerce_to_simple_array (arg1);
10366
10367 struct type *type;
10368 if (op == OP_ATR_LENGTH)
10369 type = builtin_type (exp->gdbarch)->builtin_int;
10370 else
10371 {
10372 type = ada_index_type (value_type (arg1), tem,
10373 ada_attribute_name (op));
10374 if (type == NULL)
10375 type = builtin_type (exp->gdbarch)->builtin_int;
10376 }
10377
10378 switch (op)
10379 {
10380 default: /* Should never happen. */
10381 error (_("unexpected attribute encountered"));
10382 case OP_ATR_FIRST:
10383 return value_from_longest
10384 (type, ada_array_bound (arg1, tem, 0));
10385 case OP_ATR_LAST:
10386 return value_from_longest
10387 (type, ada_array_bound (arg1, tem, 1));
10388 case OP_ATR_LENGTH:
10389 return value_from_longest
10390 (type, ada_array_length (arg1, tem));
10391 }
10392 }
10393 else if (discrete_type_p (type_arg))
10394 {
10395 struct type *range_type;
10396 const char *name = ada_type_name (type_arg);
10397
10398 range_type = NULL;
10399 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10400 range_type = to_fixed_range_type (type_arg, NULL);
10401 if (range_type == NULL)
10402 range_type = type_arg;
10403 switch (op)
10404 {
10405 default:
10406 error (_("unexpected attribute encountered"));
10407 case OP_ATR_FIRST:
10408 return value_from_longest
10409 (range_type, ada_discrete_type_low_bound (range_type));
10410 case OP_ATR_LAST:
10411 return value_from_longest
10412 (range_type, ada_discrete_type_high_bound (range_type));
10413 case OP_ATR_LENGTH:
10414 error (_("the 'length attribute applies only to array types"));
10415 }
10416 }
10417 else if (type_arg->code () == TYPE_CODE_FLT)
10418 error (_("unimplemented type attribute"));
10419 else
10420 {
10421 LONGEST low, high;
10422
10423 if (ada_is_constrained_packed_array_type (type_arg))
10424 type_arg = decode_constrained_packed_array_type (type_arg);
10425
10426 struct type *type;
10427 if (op == OP_ATR_LENGTH)
10428 type = builtin_type (exp->gdbarch)->builtin_int;
10429 else
10430 {
10431 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10432 if (type == NULL)
10433 type = builtin_type (exp->gdbarch)->builtin_int;
10434 }
10435
10436 switch (op)
10437 {
10438 default:
10439 error (_("unexpected attribute encountered"));
10440 case OP_ATR_FIRST:
10441 low = ada_array_bound_from_type (type_arg, tem, 0);
10442 return value_from_longest (type, low);
10443 case OP_ATR_LAST:
10444 high = ada_array_bound_from_type (type_arg, tem, 1);
10445 return value_from_longest (type, high);
10446 case OP_ATR_LENGTH:
10447 low = ada_array_bound_from_type (type_arg, tem, 0);
10448 high = ada_array_bound_from_type (type_arg, tem, 1);
10449 return value_from_longest (type, high - low + 1);
10450 }
10451 }
10452}
10453
38dc70cf
TT
10454/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10455
6ad3b8bf 10456struct value *
38dc70cf
TT
10457ada_binop_minmax (struct type *expect_type,
10458 struct expression *exp,
10459 enum noside noside, enum exp_opcode op,
10460 struct value *arg1, struct value *arg2)
10461{
10462 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10463 return value_zero (value_type (arg1), not_lval);
10464 else
10465 {
10466 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10467 return value_binop (arg1, arg2, op);
38dc70cf
TT
10468 }
10469}
10470
dd5fd283
TT
10471/* A helper function for BINOP_EXP. */
10472
065ec826 10473struct value *
dd5fd283
TT
10474ada_binop_exp (struct type *expect_type,
10475 struct expression *exp,
10476 enum noside noside, enum exp_opcode op,
10477 struct value *arg1, struct value *arg2)
10478{
10479 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10480 return value_zero (value_type (arg1), not_lval);
10481 else
10482 {
10483 /* For integer exponentiation operations,
10484 only promote the first argument. */
10485 if (is_integral_type (value_type (arg2)))
10486 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10487 else
10488 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10489
10490 return value_binop (arg1, arg2, op);
10491 }
10492}
10493
03070ee9
TT
10494namespace expr
10495{
10496
8b12db26
TT
10497/* See ada-exp.h. */
10498
10499operation_up
10500ada_resolvable::replace (operation_up &&owner,
10501 struct expression *exp,
10502 bool deprocedure_p,
10503 bool parse_completion,
10504 innermost_block_tracker *tracker,
10505 struct type *context_type)
10506{
10507 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10508 return (make_operation<ada_funcall_operation>
10509 (std::move (owner),
10510 std::vector<operation_up> ()));
10511 return std::move (owner);
10512}
10513
c9f66f00 10514/* Convert the character literal whose value would be VAL to the
03adb248
TT
10515 appropriate value of type TYPE, if there is a translation.
10516 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10517 the literal 'A' (VAL == 65), returns 0. */
10518
10519static LONGEST
10520convert_char_literal (struct type *type, LONGEST val)
10521{
c9f66f00 10522 char name[12];
03adb248
TT
10523 int f;
10524
10525 if (type == NULL)
10526 return val;
10527 type = check_typedef (type);
10528 if (type->code () != TYPE_CODE_ENUM)
10529 return val;
10530
10531 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10532 xsnprintf (name, sizeof (name), "Q%c", (int) val);
c9f66f00
TT
10533 else if (val >= 0 && val < 256)
10534 xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
10535 else if (val >= 0 && val < 0x10000)
10536 xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
03adb248 10537 else
c9f66f00 10538 xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
03adb248
TT
10539 size_t len = strlen (name);
10540 for (f = 0; f < type->num_fields (); f += 1)
10541 {
10542 /* Check the suffix because an enum constant in a package will
10543 have a name like "pkg__QUxx". This is safe enough because we
10544 already have the correct type, and because mangling means
10545 there can't be clashes. */
33d16dd9 10546 const char *ename = type->field (f).name ();
03adb248
TT
10547 size_t elen = strlen (ename);
10548
10549 if (elen >= len && strcmp (name, ename + elen - len) == 0)
970db518 10550 return type->field (f).loc_enumval ();
03adb248
TT
10551 }
10552 return val;
10553}
10554
b1b9c411
TT
10555value *
10556ada_char_operation::evaluate (struct type *expect_type,
10557 struct expression *exp,
10558 enum noside noside)
10559{
10560 value *result = long_const_operation::evaluate (expect_type, exp, noside);
10561 if (expect_type != nullptr)
10562 result = ada_value_cast (expect_type, result);
10563 return result;
10564}
10565
03adb248
TT
10566/* See ada-exp.h. */
10567
10568operation_up
10569ada_char_operation::replace (operation_up &&owner,
10570 struct expression *exp,
10571 bool deprocedure_p,
10572 bool parse_completion,
10573 innermost_block_tracker *tracker,
10574 struct type *context_type)
10575{
10576 operation_up result = std::move (owner);
10577
10578 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10579 {
10580 gdb_assert (result.get () == this);
10581 std::get<0> (m_storage) = context_type;
10582 std::get<1> (m_storage)
10583 = convert_char_literal (context_type, std::get<1> (m_storage));
10584 }
10585
b1b9c411 10586 return result;
03adb248
TT
10587}
10588
03070ee9
TT
10589value *
10590ada_wrapped_operation::evaluate (struct type *expect_type,
10591 struct expression *exp,
10592 enum noside noside)
10593{
10594 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10595 if (noside == EVAL_NORMAL)
10596 result = unwrap_value (result);
10597
10598 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10599 then we need to perform the conversion manually, because
10600 evaluate_subexp_standard doesn't do it. This conversion is
10601 necessary in Ada because the different kinds of float/fixed
10602 types in Ada have different representations.
10603
10604 Similarly, we need to perform the conversion from OP_LONG
10605 ourselves. */
10606 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10607 result = ada_value_cast (expect_type, result);
10608
10609 return result;
10610}
10611
42fecb61
TT
10612value *
10613ada_string_operation::evaluate (struct type *expect_type,
10614 struct expression *exp,
10615 enum noside noside)
10616{
fc18a21b
TT
10617 struct type *char_type;
10618 if (expect_type != nullptr && ada_is_string_type (expect_type))
10619 char_type = ada_array_element_type (expect_type, 1);
10620 else
10621 char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
10622
10623 const std::string &str = std::get<0> (m_storage);
10624 const char *encoding;
10625 switch (TYPE_LENGTH (char_type))
10626 {
10627 case 1:
10628 {
10629 /* Simply copy over the data -- this isn't perhaps strictly
10630 correct according to the encodings, but it is gdb's
10631 historical behavior. */
10632 struct type *stringtype
10633 = lookup_array_range_type (char_type, 1, str.length ());
10634 struct value *val = allocate_value (stringtype);
10635 memcpy (value_contents_raw (val).data (), str.c_str (),
10636 str.length ());
10637 return val;
10638 }
10639
10640 case 2:
10641 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10642 encoding = "UTF-16BE";
10643 else
10644 encoding = "UTF-16LE";
10645 break;
10646
10647 case 4:
10648 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
10649 encoding = "UTF-32BE";
10650 else
10651 encoding = "UTF-32LE";
10652 break;
10653
10654 default:
10655 error (_("unexpected character type size %s"),
10656 pulongest (TYPE_LENGTH (char_type)));
10657 }
10658
10659 auto_obstack converted;
10660 convert_between_encodings (host_charset (), encoding,
10661 (const gdb_byte *) str.c_str (),
10662 str.length (), 1,
10663 &converted, translit_none);
10664
10665 struct type *stringtype
10666 = lookup_array_range_type (char_type, 1,
10667 obstack_object_size (&converted)
10668 / TYPE_LENGTH (char_type));
10669 struct value *val = allocate_value (stringtype);
10670 memcpy (value_contents_raw (val).data (),
10671 obstack_base (&converted),
10672 obstack_object_size (&converted));
10673 return val;
42fecb61
TT
10674}
10675
b1b9c411
TT
10676value *
10677ada_concat_operation::evaluate (struct type *expect_type,
10678 struct expression *exp,
10679 enum noside noside)
10680{
10681 /* If one side is a literal, evaluate the other side first so that
10682 the expected type can be set properly. */
10683 const operation_up &lhs_expr = std::get<0> (m_storage);
10684 const operation_up &rhs_expr = std::get<1> (m_storage);
10685
10686 value *lhs, *rhs;
10687 if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
10688 {
10689 rhs = rhs_expr->evaluate (nullptr, exp, noside);
10690 lhs = lhs_expr->evaluate (value_type (rhs), exp, noside);
10691 }
10692 else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
10693 {
10694 rhs = rhs_expr->evaluate (nullptr, exp, noside);
10695 struct type *rhs_type = check_typedef (value_type (rhs));
10696 struct type *elt_type = nullptr;
10697 if (rhs_type->code () == TYPE_CODE_ARRAY)
10698 elt_type = TYPE_TARGET_TYPE (rhs_type);
10699 lhs = lhs_expr->evaluate (elt_type, exp, noside);
10700 }
10701 else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
10702 {
10703 lhs = lhs_expr->evaluate (nullptr, exp, noside);
10704 rhs = rhs_expr->evaluate (value_type (lhs), exp, noside);
10705 }
10706 else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
10707 {
10708 lhs = lhs_expr->evaluate (nullptr, exp, noside);
10709 struct type *lhs_type = check_typedef (value_type (lhs));
10710 struct type *elt_type = nullptr;
10711 if (lhs_type->code () == TYPE_CODE_ARRAY)
10712 elt_type = TYPE_TARGET_TYPE (lhs_type);
10713 rhs = rhs_expr->evaluate (elt_type, exp, noside);
10714 }
10715 else
10716 return concat_operation::evaluate (expect_type, exp, noside);
10717
10718 return value_concat (lhs, rhs);
10719}
10720
cc6bd32e
TT
10721value *
10722ada_qual_operation::evaluate (struct type *expect_type,
10723 struct expression *exp,
10724 enum noside noside)
10725{
10726 struct type *type = std::get<1> (m_storage);
10727 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10728}
10729
fc715eb2
TT
10730value *
10731ada_ternop_range_operation::evaluate (struct type *expect_type,
10732 struct expression *exp,
10733 enum noside noside)
10734{
10735 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10736 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10737 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10738 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10739}
10740
73796c73
TT
10741value *
10742ada_binop_addsub_operation::evaluate (struct type *expect_type,
10743 struct expression *exp,
10744 enum noside noside)
10745{
10746 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10747 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10748
10749 auto do_op = [=] (LONGEST x, LONGEST y)
10750 {
10751 if (std::get<0> (m_storage) == BINOP_ADD)
10752 return x + y;
10753 return x - y;
10754 };
10755
10756 if (value_type (arg1)->code () == TYPE_CODE_PTR)
10757 return (value_from_longest
10758 (value_type (arg1),
10759 do_op (value_as_long (arg1), value_as_long (arg2))));
10760 if (value_type (arg2)->code () == TYPE_CODE_PTR)
10761 return (value_from_longest
10762 (value_type (arg2),
10763 do_op (value_as_long (arg1), value_as_long (arg2))));
10764 /* Preserve the original type for use by the range case below.
10765 We cannot cast the result to a reference type, so if ARG1 is
10766 a reference type, find its underlying type. */
10767 struct type *type = value_type (arg1);
10768 while (type->code () == TYPE_CODE_REF)
10769 type = TYPE_TARGET_TYPE (type);
10770 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10771 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10772 /* We need to special-case the result with a range.
10773 This is done for the benefit of "ptype". gdb's Ada support
10774 historically used the LHS to set the result type here, so
10775 preserve this behavior. */
10776 if (type->code () == TYPE_CODE_RANGE)
10777 arg1 = value_cast (type, arg1);
10778 return arg1;
10779}
10780
60fa02ca
TT
10781value *
10782ada_unop_atr_operation::evaluate (struct type *expect_type,
10783 struct expression *exp,
10784 enum noside noside)
10785{
10786 struct type *type_arg = nullptr;
10787 value *val = nullptr;
10788
10789 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10790 {
10791 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10792 EVAL_AVOID_SIDE_EFFECTS);
10793 type_arg = value_type (tem);
10794 }
10795 else
10796 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10797
10798 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10799 val, type_arg, std::get<2> (m_storage));
10800}
10801
3f4a0053
TT
10802value *
10803ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10804 struct expression *exp,
10805 enum noside noside)
10806{
10807 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10808 return value_zero (expect_type, not_lval);
10809
9c79936b
TT
10810 const bound_minimal_symbol &b = std::get<0> (m_storage);
10811 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10812
10813 val = ada_value_cast (expect_type, val);
10814
10815 /* Follow the Ada language semantics that do not allow taking
10816 an address of the result of a cast (view conversion in Ada). */
10817 if (VALUE_LVAL (val) == lval_memory)
10818 {
10819 if (value_lazy (val))
10820 value_fetch_lazy (val);
10821 VALUE_LVAL (val) = not_lval;
10822 }
10823 return val;
10824}
10825
99a3b1e7
TT
10826value *
10827ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10828 struct expression *exp,
10829 enum noside noside)
10830{
10831 value *val = evaluate_var_value (noside,
9e5e03df
TT
10832 std::get<0> (m_storage).block,
10833 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10834
10835 val = ada_value_cast (expect_type, val);
10836
10837 /* Follow the Ada language semantics that do not allow taking
10838 an address of the result of a cast (view conversion in Ada). */
10839 if (VALUE_LVAL (val) == lval_memory)
10840 {
10841 if (value_lazy (val))
10842 value_fetch_lazy (val);
10843 VALUE_LVAL (val) = not_lval;
10844 }
10845 return val;
10846}
10847
10848value *
10849ada_var_value_operation::evaluate (struct type *expect_type,
10850 struct expression *exp,
10851 enum noside noside)
10852{
9e5e03df 10853 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7 10854
6c9c307c 10855 if (sym->domain () == UNDEF_DOMAIN)
99a3b1e7
TT
10856 /* Only encountered when an unresolved symbol occurs in a
10857 context other than a function call, in which case, it is
10858 invalid. */
10859 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10860 sym->print_name ());
10861
10862 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10863 {
5f9c5a63 10864 struct type *type = static_unwrap_type (sym->type ());
99a3b1e7
TT
10865 /* Check to see if this is a tagged type. We also need to handle
10866 the case where the type is a reference to a tagged type, but
10867 we have to be careful to exclude pointers to tagged types.
10868 The latter should be shown as usual (as a pointer), whereas
10869 a reference should mostly be transparent to the user. */
10870 if (ada_is_tagged_type (type, 0)
10871 || (type->code () == TYPE_CODE_REF
10872 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
10873 {
10874 /* Tagged types are a little special in the fact that the real
10875 type is dynamic and can only be determined by inspecting the
10876 object's tag. This means that we need to get the object's
10877 value first (EVAL_NORMAL) and then extract the actual object
10878 type from its tag.
10879
10880 Note that we cannot skip the final step where we extract
10881 the object type from its tag, because the EVAL_NORMAL phase
10882 results in dynamic components being resolved into fixed ones.
10883 This can cause problems when trying to print the type
10884 description of tagged types whose parent has a dynamic size:
10885 We use the type name of the "_parent" component in order
10886 to print the name of the ancestor type in the type description.
10887 If that component had a dynamic size, the resolution into
10888 a fixed type would result in the loss of that type name,
10889 thus preventing us from printing the name of the ancestor
10890 type in the type description. */
9863c3b5 10891 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10892
10893 if (type->code () != TYPE_CODE_REF)
10894 {
10895 struct type *actual_type;
10896
10897 actual_type = type_from_tag (ada_value_tag (arg1));
10898 if (actual_type == NULL)
10899 /* If, for some reason, we were unable to determine
10900 the actual type from the tag, then use the static
10901 approximation that we just computed as a fallback.
10902 This can happen if the debugging information is
10903 incomplete, for instance. */
10904 actual_type = type;
10905 return value_zero (actual_type, not_lval);
10906 }
10907 else
10908 {
10909 /* In the case of a ref, ada_coerce_ref takes care
10910 of determining the actual type. But the evaluation
10911 should return a ref as it should be valid to ask
10912 for its address; so rebuild a ref after coerce. */
10913 arg1 = ada_coerce_ref (arg1);
10914 return value_ref (arg1, TYPE_CODE_REF);
10915 }
10916 }
10917
10918 /* Records and unions for which GNAT encodings have been
10919 generated need to be statically fixed as well.
10920 Otherwise, non-static fixing produces a type where
10921 all dynamic properties are removed, which prevents "ptype"
10922 from being able to completely describe the type.
10923 For instance, a case statement in a variant record would be
10924 replaced by the relevant components based on the actual
10925 value of the discriminants. */
10926 if ((type->code () == TYPE_CODE_STRUCT
10927 && dynamic_template_type (type) != NULL)
10928 || (type->code () == TYPE_CODE_UNION
10929 && ada_find_parallel_type (type, "___XVU") != NULL))
10930 return value_zero (to_static_fixed_type (type), not_lval);
10931 }
10932
10933 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10934 return ada_to_fixed_value (arg1);
10935}
10936
d8a4ed8a
TT
10937bool
10938ada_var_value_operation::resolve (struct expression *exp,
10939 bool deprocedure_p,
10940 bool parse_completion,
10941 innermost_block_tracker *tracker,
10942 struct type *context_type)
10943{
9e5e03df 10944 symbol *sym = std::get<0> (m_storage).symbol;
6c9c307c 10945 if (sym->domain () == UNDEF_DOMAIN)
d8a4ed8a
TT
10946 {
10947 block_symbol resolved
9e5e03df 10948 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10949 context_type, parse_completion,
10950 deprocedure_p, tracker);
9e5e03df 10951 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10952 }
10953
10954 if (deprocedure_p
5f9c5a63 10955 && (std::get<0> (m_storage).symbol->type ()->code ()
9e5e03df 10956 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10957 return true;
10958
10959 return false;
10960}
10961
9e99f48f
TT
10962value *
10963ada_atr_val_operation::evaluate (struct type *expect_type,
10964 struct expression *exp,
10965 enum noside noside)
10966{
10967 value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10968 return ada_val_atr (noside, std::get<0> (m_storage), arg);
10969}
10970
e8c33fa1
TT
10971value *
10972ada_unop_ind_operation::evaluate (struct type *expect_type,
10973 struct expression *exp,
10974 enum noside noside)
10975{
10976 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10977
10978 struct type *type = ada_check_typedef (value_type (arg1));
10979 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10980 {
10981 if (ada_is_array_descriptor_type (type))
10982 /* GDB allows dereferencing GNAT array descriptors. */
10983 {
10984 struct type *arrType = ada_type_of_array (arg1, 0);
10985
10986 if (arrType == NULL)
10987 error (_("Attempt to dereference null array pointer."));
10988 return value_at_lazy (arrType, 0);
10989 }
10990 else if (type->code () == TYPE_CODE_PTR
10991 || type->code () == TYPE_CODE_REF
10992 /* In C you can dereference an array to get the 1st elt. */
10993 || type->code () == TYPE_CODE_ARRAY)
10994 {
10995 /* As mentioned in the OP_VAR_VALUE case, tagged types can
10996 only be determined by inspecting the object's tag.
10997 This means that we need to evaluate completely the
10998 expression in order to get its type. */
10999
11000 if ((type->code () == TYPE_CODE_REF
11001 || type->code () == TYPE_CODE_PTR)
11002 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
11003 {
11004 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11005 EVAL_NORMAL);
11006 type = value_type (ada_value_ind (arg1));
11007 }
11008 else
11009 {
11010 type = to_static_fixed_type
11011 (ada_aligned_type
11012 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
11013 }
e8c33fa1
TT
11014 return value_zero (type, lval_memory);
11015 }
11016 else if (type->code () == TYPE_CODE_INT)
11017 {
11018 /* GDB allows dereferencing an int. */
11019 if (expect_type == NULL)
11020 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11021 lval_memory);
11022 else
11023 {
11024 expect_type =
11025 to_static_fixed_type (ada_aligned_type (expect_type));
11026 return value_zero (expect_type, lval_memory);
11027 }
11028 }
11029 else
11030 error (_("Attempt to take contents of a non-pointer value."));
11031 }
11032 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
11033 type = ada_check_typedef (value_type (arg1));
11034
11035 if (type->code () == TYPE_CODE_INT)
11036 /* GDB allows dereferencing an int. If we were given
11037 the expect_type, then use that as the target type.
11038 Otherwise, assume that the target type is an int. */
11039 {
11040 if (expect_type != NULL)
11041 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11042 arg1));
11043 else
11044 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11045 (CORE_ADDR) value_as_address (arg1));
11046 }
11047
11048 if (ada_is_array_descriptor_type (type))
11049 /* GDB allows dereferencing GNAT array descriptors. */
11050 return ada_coerce_to_simple_array (arg1);
11051 else
11052 return ada_value_ind (arg1);
11053}
11054
ebc06ad8
TT
11055value *
11056ada_structop_operation::evaluate (struct type *expect_type,
11057 struct expression *exp,
11058 enum noside noside)
11059{
11060 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
11061 const char *str = std::get<1> (m_storage).c_str ();
11062 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11063 {
11064 struct type *type;
11065 struct type *type1 = value_type (arg1);
11066
11067 if (ada_is_tagged_type (type1, 1))
11068 {
11069 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
11070
11071 /* If the field is not found, check if it exists in the
11072 extension of this object's type. This means that we
11073 need to evaluate completely the expression. */
11074
11075 if (type == NULL)
11076 {
11077 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
11078 EVAL_NORMAL);
11079 arg1 = ada_value_struct_elt (arg1, str, 0);
11080 arg1 = unwrap_value (arg1);
11081 type = value_type (ada_to_fixed_value (arg1));
11082 }
11083 }
11084 else
11085 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
11086
11087 return value_zero (ada_aligned_type (type), lval_memory);
11088 }
11089 else
11090 {
11091 arg1 = ada_value_struct_elt (arg1, str, 0);
11092 arg1 = unwrap_value (arg1);
11093 return ada_to_fixed_value (arg1);
11094 }
11095}
11096
efe3af2f
TT
11097value *
11098ada_funcall_operation::evaluate (struct type *expect_type,
11099 struct expression *exp,
11100 enum noside noside)
11101{
11102 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11103 int nargs = args_up.size ();
11104 std::vector<value *> argvec (nargs);
11105 operation_up &callee_op = std::get<0> (m_storage);
11106
11107 ada_var_value_operation *avv
11108 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11109 if (avv != nullptr
6c9c307c 11110 && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
efe3af2f
TT
11111 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11112 avv->get_symbol ()->print_name ());
11113
11114 value *callee = callee_op->evaluate (nullptr, exp, noside);
11115 for (int i = 0; i < args_up.size (); ++i)
11116 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
11117
11118 if (ada_is_constrained_packed_array_type
11119 (desc_base_type (value_type (callee))))
11120 callee = ada_coerce_to_simple_array (callee);
11121 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
11122 && TYPE_FIELD_BITSIZE (value_type (callee), 0) != 0)
11123 /* This is a packed array that has already been fixed, and
11124 therefore already coerced to a simple array. Nothing further
11125 to do. */
11126 ;
11127 else if (value_type (callee)->code () == TYPE_CODE_REF)
11128 {
11129 /* Make sure we dereference references so that all the code below
11130 feels like it's really handling the referenced value. Wrapping
11131 types (for alignment) may be there, so make sure we strip them as
11132 well. */
11133 callee = ada_to_fixed_value (coerce_ref (callee));
11134 }
11135 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
11136 && VALUE_LVAL (callee) == lval_memory)
11137 callee = value_addr (callee);
11138
11139 struct type *type = ada_check_typedef (value_type (callee));
11140
11141 /* Ada allows us to implicitly dereference arrays when subscripting
11142 them. So, if this is an array typedef (encoding use for array
11143 access types encoded as fat pointers), strip it now. */
11144 if (type->code () == TYPE_CODE_TYPEDEF)
11145 type = ada_typedef_target_type (type);
11146
11147 if (type->code () == TYPE_CODE_PTR)
11148 {
11149 switch (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ())
11150 {
11151 case TYPE_CODE_FUNC:
11152 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11153 break;
11154 case TYPE_CODE_ARRAY:
11155 break;
11156 case TYPE_CODE_STRUCT:
11157 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11158 callee = ada_value_ind (callee);
11159 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11160 break;
11161 default:
11162 error (_("cannot subscript or call something of type `%s'"),
11163 ada_type_name (value_type (callee)));
11164 break;
11165 }
11166 }
11167
11168 switch (type->code ())
11169 {
11170 case TYPE_CODE_FUNC:
11171 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11172 {
11173 if (TYPE_TARGET_TYPE (type) == NULL)
11174 error_call_unknown_return_type (NULL);
11175 return allocate_value (TYPE_TARGET_TYPE (type));
11176 }
11177 return call_function_by_hand (callee, NULL, argvec);
11178 case TYPE_CODE_INTERNAL_FUNCTION:
11179 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11180 /* We don't know anything about what the internal
11181 function might return, but we have to return
11182 something. */
11183 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11184 not_lval);
11185 else
11186 return call_internal_function (exp->gdbarch, exp->language_defn,
11187 callee, nargs,
11188 argvec.data ());
11189
d3c54a1c
TT
11190 case TYPE_CODE_STRUCT:
11191 {
11192 int arity;
4c4b4cd2 11193
d3c54a1c
TT
11194 arity = ada_array_arity (type);
11195 type = ada_array_element_type (type, nargs);
11196 if (type == NULL)
11197 error (_("cannot subscript or call a record"));
11198 if (arity != nargs)
11199 error (_("wrong number of subscripts; expecting %d"), arity);
11200 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11201 return value_zero (ada_aligned_type (type), lval_memory);
11202 return
11203 unwrap_value (ada_value_subscript
11204 (callee, nargs, argvec.data ()));
11205 }
11206 case TYPE_CODE_ARRAY:
14f9c5c9 11207 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11208 {
d3c54a1c
TT
11209 type = ada_array_element_type (type, nargs);
11210 if (type == NULL)
11211 error (_("element type of array unknown"));
dda83cd7 11212 else
d3c54a1c 11213 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 11214 }
d3c54a1c
TT
11215 return
11216 unwrap_value (ada_value_subscript
11217 (ada_coerce_to_simple_array (callee),
11218 nargs, argvec.data ()));
11219 case TYPE_CODE_PTR: /* Pointer to array */
11220 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11221 {
d3c54a1c
TT
11222 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
11223 type = ada_array_element_type (type, nargs);
11224 if (type == NULL)
11225 error (_("element type of array unknown"));
96967637 11226 else
d3c54a1c 11227 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 11228 }
d3c54a1c
TT
11229 return
11230 unwrap_value (ada_value_ptr_subscript (callee, nargs,
11231 argvec.data ()));
6b0d7253 11232
d3c54a1c
TT
11233 default:
11234 error (_("Attempt to index or call something other than an "
11235 "array or function"));
11236 }
11237}
5b4ee69b 11238
d3c54a1c
TT
11239bool
11240ada_funcall_operation::resolve (struct expression *exp,
11241 bool deprocedure_p,
11242 bool parse_completion,
11243 innermost_block_tracker *tracker,
11244 struct type *context_type)
11245{
11246 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 11247
d3c54a1c
TT
11248 ada_var_value_operation *avv
11249 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
11250 if (avv == nullptr)
11251 return false;
5ec18f2b 11252
d3c54a1c 11253 symbol *sym = avv->get_symbol ();
6c9c307c 11254 if (sym->domain () != UNDEF_DOMAIN)
d3c54a1c 11255 return false;
dda83cd7 11256
d3c54a1c
TT
11257 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
11258 int nargs = args_up.size ();
11259 std::vector<value *> argvec (nargs);
284614f0 11260
d3c54a1c
TT
11261 for (int i = 0; i < args_up.size (); ++i)
11262 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 11263
d3c54a1c
TT
11264 const block *block = avv->get_block ();
11265 block_symbol resolved
11266 = ada_resolve_funcall (sym, block,
11267 context_type, parse_completion,
11268 nargs, argvec.data (),
11269 tracker);
11270
11271 std::get<0> (m_storage)
9e5e03df 11272 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
11273 return false;
11274}
11275
11276bool
11277ada_ternop_slice_operation::resolve (struct expression *exp,
11278 bool deprocedure_p,
11279 bool parse_completion,
11280 innermost_block_tracker *tracker,
11281 struct type *context_type)
11282{
11283 /* Historically this check was done during resolution, so we
11284 continue that here. */
11285 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
11286 EVAL_AVOID_SIDE_EFFECTS);
11287 if (ada_is_any_packed_array_type (value_type (v)))
11288 error (_("cannot slice a packed array"));
11289 return false;
11290}
14f9c5c9 11291
14f9c5c9 11292}
d3c54a1c 11293
14f9c5c9 11294\f
d2e4a39e 11295
4c4b4cd2
PH
11296/* Return non-zero iff TYPE represents a System.Address type. */
11297
11298int
11299ada_is_system_address_type (struct type *type)
11300{
7d93a1e0 11301 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11302}
11303
14f9c5c9 11304\f
d2e4a39e 11305
dda83cd7 11306 /* Range types */
14f9c5c9
AS
11307
11308/* Scan STR beginning at position K for a discriminant name, and
11309 return the value of that discriminant field of DVAL in *PX. If
11310 PNEW_K is not null, put the position of the character beyond the
11311 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11312 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11313
11314static int
108d56a4 11315scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11316 int *pnew_k)
14f9c5c9 11317{
5f9febe0 11318 static std::string storage;
5da1a4d3 11319 const char *pstart, *pend, *bound;
d2e4a39e 11320 struct value *bound_val;
14f9c5c9
AS
11321
11322 if (dval == NULL || str == NULL || str[k] == '\0')
11323 return 0;
11324
5da1a4d3
SM
11325 pstart = str + k;
11326 pend = strstr (pstart, "__");
14f9c5c9
AS
11327 if (pend == NULL)
11328 {
5da1a4d3 11329 bound = pstart;
14f9c5c9
AS
11330 k += strlen (bound);
11331 }
d2e4a39e 11332 else
14f9c5c9 11333 {
5da1a4d3
SM
11334 int len = pend - pstart;
11335
11336 /* Strip __ and beyond. */
5f9febe0
TT
11337 storage = std::string (pstart, len);
11338 bound = storage.c_str ();
d2e4a39e 11339 k = pend - str;
14f9c5c9 11340 }
d2e4a39e 11341
df407dfe 11342 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
11343 if (bound_val == NULL)
11344 return 0;
11345
11346 *px = value_as_long (bound_val);
11347 if (pnew_k != NULL)
11348 *pnew_k = k;
11349 return 1;
11350}
11351
25a1127b
TT
11352/* Value of variable named NAME. Only exact matches are considered.
11353 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11354 otherwise causes an error with message ERR_MSG. */
11355
d2e4a39e 11356static struct value *
edb0c9cb 11357get_var_value (const char *name, const char *err_msg)
14f9c5c9 11358{
25a1127b
TT
11359 std::string quoted_name = add_angle_brackets (name);
11360
11361 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11362
d1183b06
TT
11363 std::vector<struct block_symbol> syms
11364 = ada_lookup_symbol_list_worker (lookup_name,
11365 get_selected_block (0),
11366 VAR_DOMAIN, 1);
14f9c5c9 11367
d1183b06 11368 if (syms.size () != 1)
14f9c5c9
AS
11369 {
11370 if (err_msg == NULL)
dda83cd7 11371 return 0;
14f9c5c9 11372 else
dda83cd7 11373 error (("%s"), err_msg);
14f9c5c9
AS
11374 }
11375
54d343a2 11376 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11377}
d2e4a39e 11378
edb0c9cb
PA
11379/* Value of integer variable named NAME in the current environment.
11380 If no such variable is found, returns false. Otherwise, sets VALUE
11381 to the variable's value and returns true. */
4c4b4cd2 11382
edb0c9cb
PA
11383bool
11384get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11385{
4c4b4cd2 11386 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11387
14f9c5c9 11388 if (var_val == 0)
edb0c9cb
PA
11389 return false;
11390
11391 value = value_as_long (var_val);
11392 return true;
14f9c5c9 11393}
d2e4a39e 11394
14f9c5c9
AS
11395
11396/* Return a range type whose base type is that of the range type named
11397 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11398 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11399 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11400 corresponding range type from debug information; fall back to using it
11401 if symbol lookup fails. If a new type must be created, allocate it
11402 like ORIG_TYPE was. The bounds information, in general, is encoded
11403 in NAME, the base type given in the named range type. */
14f9c5c9 11404
d2e4a39e 11405static struct type *
28c85d6c 11406to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11407{
0d5cff50 11408 const char *name;
14f9c5c9 11409 struct type *base_type;
108d56a4 11410 const char *subtype_info;
14f9c5c9 11411
28c85d6c 11412 gdb_assert (raw_type != NULL);
7d93a1e0 11413 gdb_assert (raw_type->name () != NULL);
dddfab26 11414
78134374 11415 if (raw_type->code () == TYPE_CODE_RANGE)
14f9c5c9
AS
11416 base_type = TYPE_TARGET_TYPE (raw_type);
11417 else
11418 base_type = raw_type;
11419
7d93a1e0 11420 name = raw_type->name ();
14f9c5c9
AS
11421 subtype_info = strstr (name, "___XD");
11422 if (subtype_info == NULL)
690cc4eb 11423 {
43bbcdc2
PH
11424 LONGEST L = ada_discrete_type_low_bound (raw_type);
11425 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11426
690cc4eb
PH
11427 if (L < INT_MIN || U > INT_MAX)
11428 return raw_type;
11429 else
0c9c3474
SA
11430 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
11431 L, U);
690cc4eb 11432 }
14f9c5c9
AS
11433 else
11434 {
14f9c5c9
AS
11435 int prefix_len = subtype_info - name;
11436 LONGEST L, U;
11437 struct type *type;
108d56a4 11438 const char *bounds_str;
14f9c5c9
AS
11439 int n;
11440
14f9c5c9
AS
11441 subtype_info += 5;
11442 bounds_str = strchr (subtype_info, '_');
11443 n = 1;
11444
d2e4a39e 11445 if (*subtype_info == 'L')
dda83cd7
SM
11446 {
11447 if (!ada_scan_number (bounds_str, n, &L, &n)
11448 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11449 return raw_type;
11450 if (bounds_str[n] == '_')
11451 n += 2;
11452 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11453 n += 1;
11454 subtype_info += 1;
11455 }
d2e4a39e 11456 else
dda83cd7 11457 {
5f9febe0
TT
11458 std::string name_buf = std::string (name, prefix_len) + "___L";
11459 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11460 {
11461 lim_warning (_("Unknown lower bound, using 1."));
11462 L = 1;
11463 }
11464 }
14f9c5c9 11465
d2e4a39e 11466 if (*subtype_info == 'U')
dda83cd7
SM
11467 {
11468 if (!ada_scan_number (bounds_str, n, &U, &n)
11469 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11470 return raw_type;
11471 }
d2e4a39e 11472 else
dda83cd7 11473 {
5f9febe0
TT
11474 std::string name_buf = std::string (name, prefix_len) + "___U";
11475 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11476 {
11477 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11478 U = L;
11479 }
11480 }
14f9c5c9 11481
0c9c3474
SA
11482 type = create_static_range_type (alloc_type_copy (raw_type),
11483 base_type, L, U);
f5a91472 11484 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11485 to match the size of the base_type, which is not what we want.
11486 Set it back to the original range type's length. */
f5a91472 11487 TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
d0e39ea2 11488 type->set_name (name);
14f9c5c9
AS
11489 return type;
11490 }
11491}
11492
4c4b4cd2
PH
11493/* True iff NAME is the name of a range type. */
11494
14f9c5c9 11495int
d2e4a39e 11496ada_is_range_type_name (const char *name)
14f9c5c9
AS
11497{
11498 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11499}
14f9c5c9 11500\f
d2e4a39e 11501
dda83cd7 11502 /* Modular types */
4c4b4cd2
PH
11503
11504/* True iff TYPE is an Ada modular type. */
14f9c5c9 11505
14f9c5c9 11506int
d2e4a39e 11507ada_is_modular_type (struct type *type)
14f9c5c9 11508{
18af8284 11509 struct type *subranged_type = get_base_type (type);
14f9c5c9 11510
78134374 11511 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11512 && subranged_type->code () == TYPE_CODE_INT
11513 && subranged_type->is_unsigned ());
14f9c5c9
AS
11514}
11515
4c4b4cd2
PH
11516/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11517
61ee279c 11518ULONGEST
0056e4d5 11519ada_modulus (struct type *type)
14f9c5c9 11520{
5e500d33
SM
11521 const dynamic_prop &high = type->bounds ()->high;
11522
11523 if (high.kind () == PROP_CONST)
11524 return (ULONGEST) high.const_val () + 1;
11525
11526 /* If TYPE is unresolved, the high bound might be a location list. Return
11527 0, for lack of a better value to return. */
11528 return 0;
14f9c5c9 11529}
d2e4a39e 11530\f
f7f9143b
JB
11531
11532/* Ada exception catchpoint support:
11533 ---------------------------------
11534
11535 We support 3 kinds of exception catchpoints:
11536 . catchpoints on Ada exceptions
11537 . catchpoints on unhandled Ada exceptions
11538 . catchpoints on failed assertions
11539
11540 Exceptions raised during failed assertions, or unhandled exceptions
11541 could perfectly be caught with the general catchpoint on Ada exceptions.
11542 However, we can easily differentiate these two special cases, and having
11543 the option to distinguish these two cases from the rest can be useful
11544 to zero-in on certain situations.
11545
11546 Exception catchpoints are a specialized form of breakpoint,
11547 since they rely on inserting breakpoints inside known routines
11548 of the GNAT runtime. The implementation therefore uses a standard
11549 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11550 of breakpoint_ops.
11551
0259addd
JB
11552 Support in the runtime for exception catchpoints have been changed
11553 a few times already, and these changes affect the implementation
11554 of these catchpoints. In order to be able to support several
11555 variants of the runtime, we use a sniffer that will determine
28010a5d 11556 the runtime variant used by the program being debugged. */
f7f9143b 11557
82eacd52
JB
11558/* Ada's standard exceptions.
11559
11560 The Ada 83 standard also defined Numeric_Error. But there so many
11561 situations where it was unclear from the Ada 83 Reference Manual
11562 (RM) whether Constraint_Error or Numeric_Error should be raised,
11563 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11564 Interpretation saying that anytime the RM says that Numeric_Error
11565 should be raised, the implementation may raise Constraint_Error.
11566 Ada 95 went one step further and pretty much removed Numeric_Error
11567 from the list of standard exceptions (it made it a renaming of
11568 Constraint_Error, to help preserve compatibility when compiling
11569 an Ada83 compiler). As such, we do not include Numeric_Error from
11570 this list of standard exceptions. */
3d0b0fa3 11571
27087b7f 11572static const char * const standard_exc[] = {
3d0b0fa3
JB
11573 "constraint_error",
11574 "program_error",
11575 "storage_error",
11576 "tasking_error"
11577};
11578
0259addd
JB
11579typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11580
11581/* A structure that describes how to support exception catchpoints
11582 for a given executable. */
11583
11584struct exception_support_info
11585{
11586 /* The name of the symbol to break on in order to insert
11587 a catchpoint on exceptions. */
11588 const char *catch_exception_sym;
11589
11590 /* The name of the symbol to break on in order to insert
11591 a catchpoint on unhandled exceptions. */
11592 const char *catch_exception_unhandled_sym;
11593
11594 /* The name of the symbol to break on in order to insert
11595 a catchpoint on failed assertions. */
11596 const char *catch_assert_sym;
11597
9f757bf7
XR
11598 /* The name of the symbol to break on in order to insert
11599 a catchpoint on exception handling. */
11600 const char *catch_handlers_sym;
11601
0259addd
JB
11602 /* Assuming that the inferior just triggered an unhandled exception
11603 catchpoint, this function is responsible for returning the address
11604 in inferior memory where the name of that exception is stored.
11605 Return zero if the address could not be computed. */
11606 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11607};
11608
11609static CORE_ADDR ada_unhandled_exception_name_addr (void);
11610static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11611
11612/* The following exception support info structure describes how to
11613 implement exception catchpoints with the latest version of the
ca683e3a 11614 Ada runtime (as of 2019-08-??). */
0259addd
JB
11615
11616static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11617{
11618 "__gnat_debug_raise_exception", /* catch_exception_sym */
11619 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11620 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11621 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11622 ada_unhandled_exception_name_addr
11623};
11624
11625/* The following exception support info structure describes how to
11626 implement exception catchpoints with an earlier version of the
11627 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11628
11629static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11630{
11631 "__gnat_debug_raise_exception", /* catch_exception_sym */
11632 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11633 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11634 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11635 ada_unhandled_exception_name_addr
11636};
11637
11638/* The following exception support info structure describes how to
11639 implement exception catchpoints with a slightly older version
11640 of the Ada runtime. */
11641
11642static const struct exception_support_info exception_support_info_fallback =
11643{
11644 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11645 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11646 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11647 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11648 ada_unhandled_exception_name_addr_from_raise
11649};
11650
f17011e0
JB
11651/* Return nonzero if we can detect the exception support routines
11652 described in EINFO.
11653
11654 This function errors out if an abnormal situation is detected
11655 (for instance, if we find the exception support routines, but
11656 that support is found to be incomplete). */
11657
11658static int
11659ada_has_this_exception_support (const struct exception_support_info *einfo)
11660{
11661 struct symbol *sym;
11662
11663 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11664 that should be compiled with debugging information. As a result, we
11665 expect to find that symbol in the symtabs. */
11666
11667 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11668 if (sym == NULL)
a6af7abe
JB
11669 {
11670 /* Perhaps we did not find our symbol because the Ada runtime was
11671 compiled without debugging info, or simply stripped of it.
11672 It happens on some GNU/Linux distributions for instance, where
11673 users have to install a separate debug package in order to get
11674 the runtime's debugging info. In that situation, let the user
11675 know why we cannot insert an Ada exception catchpoint.
11676
11677 Note: Just for the purpose of inserting our Ada exception
11678 catchpoint, we could rely purely on the associated minimal symbol.
11679 But we would be operating in degraded mode anyway, since we are
11680 still lacking the debugging info needed later on to extract
11681 the name of the exception being raised (this name is printed in
11682 the catchpoint message, and is also used when trying to catch
11683 a specific exception). We do not handle this case for now. */
3b7344d5 11684 struct bound_minimal_symbol msym
1c8e84b0
JB
11685 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11686
3b7344d5 11687 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
a6af7abe
JB
11688 error (_("Your Ada runtime appears to be missing some debugging "
11689 "information.\nCannot insert Ada exception catchpoint "
11690 "in this configuration."));
11691
11692 return 0;
11693 }
f17011e0
JB
11694
11695 /* Make sure that the symbol we found corresponds to a function. */
11696
66d7f48f 11697 if (sym->aclass () != LOC_BLOCK)
ca683e3a
AO
11698 {
11699 error (_("Symbol \"%s\" is not a function (class = %d)"),
66d7f48f 11700 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11701 return 0;
11702 }
11703
11704 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11705 if (sym == NULL)
11706 {
11707 struct bound_minimal_symbol msym
11708 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11709
11710 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
11711 error (_("Your Ada runtime appears to be missing some debugging "
11712 "information.\nCannot insert Ada exception catchpoint "
11713 "in this configuration."));
11714
11715 return 0;
11716 }
11717
11718 /* Make sure that the symbol we found corresponds to a function. */
11719
66d7f48f 11720 if (sym->aclass () != LOC_BLOCK)
ca683e3a
AO
11721 {
11722 error (_("Symbol \"%s\" is not a function (class = %d)"),
66d7f48f 11723 sym->linkage_name (), sym->aclass ());
ca683e3a
AO
11724 return 0;
11725 }
f17011e0
JB
11726
11727 return 1;
11728}
11729
0259addd
JB
11730/* Inspect the Ada runtime and determine which exception info structure
11731 should be used to provide support for exception catchpoints.
11732
3eecfa55
JB
11733 This function will always set the per-inferior exception_info,
11734 or raise an error. */
0259addd
JB
11735
11736static void
11737ada_exception_support_info_sniffer (void)
11738{
3eecfa55 11739 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11740
11741 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11742 if (data->exception_info != NULL)
0259addd
JB
11743 return;
11744
11745 /* Check the latest (default) exception support info. */
f17011e0 11746 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11747 {
3eecfa55 11748 data->exception_info = &default_exception_support_info;
0259addd
JB
11749 return;
11750 }
11751
ca683e3a
AO
11752 /* Try the v0 exception suport info. */
11753 if (ada_has_this_exception_support (&exception_support_info_v0))
11754 {
11755 data->exception_info = &exception_support_info_v0;
11756 return;
11757 }
11758
0259addd 11759 /* Try our fallback exception suport info. */
f17011e0 11760 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11761 {
3eecfa55 11762 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11763 return;
11764 }
11765
11766 /* Sometimes, it is normal for us to not be able to find the routine
11767 we are looking for. This happens when the program is linked with
11768 the shared version of the GNAT runtime, and the program has not been
11769 started yet. Inform the user of these two possible causes if
11770 applicable. */
11771
ccefe4c4 11772 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11773 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11774
11775 /* If the symbol does not exist, then check that the program is
11776 already started, to make sure that shared libraries have been
11777 loaded. If it is not started, this may mean that the symbol is
11778 in a shared library. */
11779
e99b03dc 11780 if (inferior_ptid.pid () == 0)
0259addd
JB
11781 error (_("Unable to insert catchpoint. Try to start the program first."));
11782
11783 /* At this point, we know that we are debugging an Ada program and
11784 that the inferior has been started, but we still are not able to
0963b4bd 11785 find the run-time symbols. That can mean that we are in
0259addd
JB
11786 configurable run time mode, or that a-except as been optimized
11787 out by the linker... In any case, at this point it is not worth
11788 supporting this feature. */
11789
7dda8cff 11790 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11791}
11792
f7f9143b
JB
11793/* True iff FRAME is very likely to be that of a function that is
11794 part of the runtime system. This is all very heuristic, but is
11795 intended to be used as advice as to what frames are uninteresting
11796 to most users. */
11797
11798static int
11799is_known_support_routine (struct frame_info *frame)
11800{
692465f1 11801 enum language func_lang;
f7f9143b 11802 int i;
f35a17b5 11803 const char *fullname;
f7f9143b 11804
4ed6b5be
JB
11805 /* If this code does not have any debugging information (no symtab),
11806 This cannot be any user code. */
f7f9143b 11807
51abb421 11808 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11809 if (sal.symtab == NULL)
11810 return 1;
11811
4ed6b5be
JB
11812 /* If there is a symtab, but the associated source file cannot be
11813 located, then assume this is not user code: Selecting a frame
11814 for which we cannot display the code would not be very helpful
11815 for the user. This should also take care of case such as VxWorks
11816 where the kernel has some debugging info provided for a few units. */
f7f9143b 11817
f35a17b5
JK
11818 fullname = symtab_to_fullname (sal.symtab);
11819 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11820 return 1;
11821
85102364 11822 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11823 We also check the name of the objfile against the name of some
11824 known system libraries that sometimes come with debugging info
11825 too. */
11826
f7f9143b
JB
11827 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11828 {
11829 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11830 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11831 return 1;
65209971
SM
11832 if (sal.symtab->objfile () != NULL
11833 && re_exec (objfile_name (sal.symtab->objfile ())))
dda83cd7 11834 return 1;
f7f9143b
JB
11835 }
11836
4ed6b5be 11837 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11838
c6dc63a1
TT
11839 gdb::unique_xmalloc_ptr<char> func_name
11840 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11841 if (func_name == NULL)
11842 return 1;
11843
11844 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11845 {
11846 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11847 if (re_exec (func_name.get ()))
11848 return 1;
f7f9143b
JB
11849 }
11850
11851 return 0;
11852}
11853
11854/* Find the first frame that contains debugging information and that is not
11855 part of the Ada run-time, starting from FI and moving upward. */
11856
0ef643c8 11857void
f7f9143b
JB
11858ada_find_printable_frame (struct frame_info *fi)
11859{
11860 for (; fi != NULL; fi = get_prev_frame (fi))
11861 {
11862 if (!is_known_support_routine (fi))
dda83cd7
SM
11863 {
11864 select_frame (fi);
11865 break;
11866 }
f7f9143b
JB
11867 }
11868
11869}
11870
11871/* Assuming that the inferior just triggered an unhandled exception
11872 catchpoint, return the address in inferior memory where the name
11873 of the exception is stored.
11874
11875 Return zero if the address could not be computed. */
11876
11877static CORE_ADDR
11878ada_unhandled_exception_name_addr (void)
0259addd
JB
11879{
11880 return parse_and_eval_address ("e.full_name");
11881}
11882
11883/* Same as ada_unhandled_exception_name_addr, except that this function
11884 should be used when the inferior uses an older version of the runtime,
11885 where the exception name needs to be extracted from a specific frame
11886 several frames up in the callstack. */
11887
11888static CORE_ADDR
11889ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11890{
11891 int frame_level;
11892 struct frame_info *fi;
3eecfa55 11893 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11894
11895 /* To determine the name of this exception, we need to select
11896 the frame corresponding to RAISE_SYM_NAME. This frame is
11897 at least 3 levels up, so we simply skip the first 3 frames
11898 without checking the name of their associated function. */
11899 fi = get_current_frame ();
11900 for (frame_level = 0; frame_level < 3; frame_level += 1)
11901 if (fi != NULL)
11902 fi = get_prev_frame (fi);
11903
11904 while (fi != NULL)
11905 {
692465f1
JB
11906 enum language func_lang;
11907
c6dc63a1
TT
11908 gdb::unique_xmalloc_ptr<char> func_name
11909 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11910 if (func_name != NULL)
11911 {
dda83cd7 11912 if (strcmp (func_name.get (),
55b87a52
KS
11913 data->exception_info->catch_exception_sym) == 0)
11914 break; /* We found the frame we were looking for... */
55b87a52 11915 }
fb44b1a7 11916 fi = get_prev_frame (fi);
f7f9143b
JB
11917 }
11918
11919 if (fi == NULL)
11920 return 0;
11921
11922 select_frame (fi);
11923 return parse_and_eval_address ("id.full_name");
11924}
11925
11926/* Assuming the inferior just triggered an Ada exception catchpoint
11927 (of any type), return the address in inferior memory where the name
11928 of the exception is stored, if applicable.
11929
45db7c09
PA
11930 Assumes the selected frame is the current frame.
11931
f7f9143b
JB
11932 Return zero if the address could not be computed, or if not relevant. */
11933
11934static CORE_ADDR
761269c8 11935ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex,
dda83cd7 11936 struct breakpoint *b)
f7f9143b 11937{
3eecfa55
JB
11938 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11939
f7f9143b
JB
11940 switch (ex)
11941 {
761269c8 11942 case ada_catch_exception:
dda83cd7
SM
11943 return (parse_and_eval_address ("e.full_name"));
11944 break;
f7f9143b 11945
761269c8 11946 case ada_catch_exception_unhandled:
dda83cd7
SM
11947 return data->exception_info->unhandled_exception_name_addr ();
11948 break;
9f757bf7
XR
11949
11950 case ada_catch_handlers:
dda83cd7 11951 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11952 name. */
dda83cd7 11953 break;
9f757bf7 11954
761269c8 11955 case ada_catch_assert:
dda83cd7
SM
11956 return 0; /* Exception name is not relevant in this case. */
11957 break;
f7f9143b
JB
11958
11959 default:
dda83cd7
SM
11960 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
11961 break;
f7f9143b
JB
11962 }
11963
11964 return 0; /* Should never be reached. */
11965}
11966
e547c119
JB
11967/* Assuming the inferior is stopped at an exception catchpoint,
11968 return the message which was associated to the exception, if
11969 available. Return NULL if the message could not be retrieved.
11970
e547c119
JB
11971 Note: The exception message can be associated to an exception
11972 either through the use of the Raise_Exception function, or
11973 more simply (Ada 2005 and later), via:
11974
11975 raise Exception_Name with "exception message";
11976
11977 */
11978
6f46ac85 11979static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11980ada_exception_message_1 (void)
11981{
11982 struct value *e_msg_val;
e547c119 11983 int e_msg_len;
e547c119
JB
11984
11985 /* For runtimes that support this feature, the exception message
11986 is passed as an unbounded string argument called "message". */
11987 e_msg_val = parse_and_eval ("message");
11988 if (e_msg_val == NULL)
11989 return NULL; /* Exception message not supported. */
11990
11991 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
11992 gdb_assert (e_msg_val != NULL);
11993 e_msg_len = TYPE_LENGTH (value_type (e_msg_val));
11994
11995 /* If the message string is empty, then treat it as if there was
11996 no exception message. */
11997 if (e_msg_len <= 0)
11998 return NULL;
11999
15f3b077
TT
12000 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
12001 read_memory (value_address (e_msg_val), (gdb_byte *) e_msg.get (),
12002 e_msg_len);
12003 e_msg.get ()[e_msg_len] = '\0';
12004
12005 return e_msg;
e547c119
JB
12006}
12007
12008/* Same as ada_exception_message_1, except that all exceptions are
12009 contained here (returning NULL instead). */
12010
6f46ac85 12011static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12012ada_exception_message (void)
12013{
6f46ac85 12014 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 12015
a70b8144 12016 try
e547c119
JB
12017 {
12018 e_msg = ada_exception_message_1 ();
12019 }
230d2906 12020 catch (const gdb_exception_error &e)
e547c119 12021 {
6f46ac85 12022 e_msg.reset (nullptr);
e547c119 12023 }
e547c119
JB
12024
12025 return e_msg;
12026}
12027
f7f9143b
JB
12028/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12029 any error that ada_exception_name_addr_1 might cause to be thrown.
12030 When an error is intercepted, a warning with the error message is printed,
12031 and zero is returned. */
12032
12033static CORE_ADDR
761269c8 12034ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
dda83cd7 12035 struct breakpoint *b)
f7f9143b 12036{
f7f9143b
JB
12037 CORE_ADDR result = 0;
12038
a70b8144 12039 try
f7f9143b
JB
12040 {
12041 result = ada_exception_name_addr_1 (ex, b);
12042 }
12043
230d2906 12044 catch (const gdb_exception_error &e)
f7f9143b 12045 {
3d6e9d23 12046 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12047 return 0;
12048 }
12049
12050 return result;
12051}
12052
cb7de75e 12053static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12054 (const char *excep_string,
12055 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12056
12057/* Ada catchpoints.
12058
12059 In the case of catchpoints on Ada exceptions, the catchpoint will
12060 stop the target on every exception the program throws. When a user
12061 specifies the name of a specific exception, we translate this
12062 request into a condition expression (in text form), and then parse
12063 it into an expression stored in each of the catchpoint's locations.
12064 We then use this condition to check whether the exception that was
12065 raised is the one the user is interested in. If not, then the
12066 target is resumed again. We store the name of the requested
12067 exception, in order to be able to re-set the condition expression
12068 when symbols change. */
12069
12070/* An instance of this type is used to represent an Ada catchpoint
5625a286 12071 breakpoint location. */
28010a5d 12072
5625a286 12073class ada_catchpoint_location : public bp_location
28010a5d 12074{
5625a286 12075public:
5f486660 12076 ada_catchpoint_location (breakpoint *owner)
f06f1252 12077 : bp_location (owner, bp_loc_software_breakpoint)
5625a286 12078 {}
28010a5d
PA
12079
12080 /* The condition that checks whether the exception that was raised
12081 is the specific exception the user specified on catchpoint
12082 creation. */
4d01a485 12083 expression_up excep_cond_expr;
28010a5d
PA
12084};
12085
c1fc2657 12086/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12087
c1fc2657 12088struct ada_catchpoint : public breakpoint
28010a5d 12089{
37f6a7f4
TT
12090 explicit ada_catchpoint (enum ada_exception_catchpoint_kind kind)
12091 : m_kind (kind)
12092 {
12093 }
12094
28010a5d 12095 /* The name of the specific exception the user specified. */
bc18fbb5 12096 std::string excep_string;
37f6a7f4
TT
12097
12098 /* What kind of catchpoint this is. */
12099 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12100};
12101
12102/* Parse the exception condition string in the context of each of the
12103 catchpoint's locations, and store them for later evaluation. */
12104
12105static void
9f757bf7 12106create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 12107 enum ada_exception_catchpoint_kind ex)
28010a5d 12108{
28010a5d 12109 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 12110 if (c->excep_string.empty ())
28010a5d
PA
12111 return;
12112
12113 /* Same if there are no locations... */
c1fc2657 12114 if (c->loc == NULL)
28010a5d
PA
12115 return;
12116
fccf9de1
TT
12117 /* Compute the condition expression in text form, from the specific
12118 expection we want to catch. */
12119 std::string cond_string
12120 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 12121
fccf9de1
TT
12122 /* Iterate over all the catchpoint's locations, and parse an
12123 expression for each. */
40cb8ca5 12124 for (bp_location *bl : c->locations ())
28010a5d
PA
12125 {
12126 struct ada_catchpoint_location *ada_loc
fccf9de1 12127 = (struct ada_catchpoint_location *) bl;
4d01a485 12128 expression_up exp;
28010a5d 12129
fccf9de1 12130 if (!bl->shlib_disabled)
28010a5d 12131 {
bbc13ae3 12132 const char *s;
28010a5d 12133
cb7de75e 12134 s = cond_string.c_str ();
a70b8144 12135 try
28010a5d 12136 {
fccf9de1
TT
12137 exp = parse_exp_1 (&s, bl->address,
12138 block_for_pc (bl->address),
036e657b 12139 0);
28010a5d 12140 }
230d2906 12141 catch (const gdb_exception_error &e)
849f2b52
JB
12142 {
12143 warning (_("failed to reevaluate internal exception condition "
12144 "for catchpoint %d: %s"),
3d6e9d23 12145 c->number, e.what ());
849f2b52 12146 }
28010a5d
PA
12147 }
12148
b22e99fd 12149 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 12150 }
28010a5d
PA
12151}
12152
28010a5d
PA
12153/* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
12154 structure for all exception catchpoint kinds. */
12155
12156static struct bp_location *
37f6a7f4 12157allocate_location_exception (struct breakpoint *self)
28010a5d 12158{
5f486660 12159 return new ada_catchpoint_location (self);
28010a5d
PA
12160}
12161
12162/* Implement the RE_SET method in the breakpoint_ops structure for all
12163 exception catchpoint kinds. */
12164
12165static void
37f6a7f4 12166re_set_exception (struct breakpoint *b)
28010a5d
PA
12167{
12168 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12169
12170 /* Call the base class's method. This updates the catchpoint's
12171 locations. */
2060206e 12172 bkpt_breakpoint_ops.re_set (b);
28010a5d
PA
12173
12174 /* Reparse the exception conditional expressions. One for each
12175 location. */
37f6a7f4 12176 create_excep_cond_exprs (c, c->m_kind);
28010a5d
PA
12177}
12178
12179/* Returns true if we should stop for this breakpoint hit. If the
12180 user specified a specific exception, we only want to cause a stop
12181 if the program thrown that exception. */
12182
7ebaa5f7 12183static bool
28010a5d
PA
12184should_stop_exception (const struct bp_location *bl)
12185{
12186 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12187 const struct ada_catchpoint_location *ada_loc
12188 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 12189 bool stop;
28010a5d 12190
37f6a7f4
TT
12191 struct internalvar *var = lookup_internalvar ("_ada_exception");
12192 if (c->m_kind == ada_catch_assert)
12193 clear_internalvar (var);
12194 else
12195 {
12196 try
12197 {
12198 const char *expr;
12199
12200 if (c->m_kind == ada_catch_handlers)
12201 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12202 ".all.occurrence.id");
12203 else
12204 expr = "e";
12205
12206 struct value *exc = parse_and_eval (expr);
12207 set_internalvar (var, exc);
12208 }
12209 catch (const gdb_exception_error &ex)
12210 {
12211 clear_internalvar (var);
12212 }
12213 }
12214
28010a5d 12215 /* With no specific exception, should always stop. */
bc18fbb5 12216 if (c->excep_string.empty ())
7ebaa5f7 12217 return true;
28010a5d
PA
12218
12219 if (ada_loc->excep_cond_expr == NULL)
12220 {
12221 /* We will have a NULL expression if back when we were creating
12222 the expressions, this location's had failed to parse. */
7ebaa5f7 12223 return true;
28010a5d
PA
12224 }
12225
7ebaa5f7 12226 stop = true;
a70b8144 12227 try
28010a5d
PA
12228 {
12229 struct value *mark;
12230
12231 mark = value_mark ();
4d01a485 12232 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
28010a5d
PA
12233 value_free_to_mark (mark);
12234 }
230d2906 12235 catch (const gdb_exception &ex)
492d29ea
PA
12236 {
12237 exception_fprintf (gdb_stderr, ex,
12238 _("Error in testing exception condition:\n"));
12239 }
492d29ea 12240
28010a5d
PA
12241 return stop;
12242}
12243
12244/* Implement the CHECK_STATUS method in the breakpoint_ops structure
12245 for all exception catchpoint kinds. */
12246
12247static void
313f3b21 12248check_status_exception (bpstat *bs)
28010a5d 12249{
b6433ede 12250 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12251}
12252
f7f9143b
JB
12253/* Implement the PRINT_IT method in the breakpoint_ops structure
12254 for all exception catchpoint kinds. */
12255
12256static enum print_stop_action
313f3b21 12257print_it_exception (bpstat *bs)
f7f9143b 12258{
79a45e25 12259 struct ui_out *uiout = current_uiout;
348d480f
PA
12260 struct breakpoint *b = bs->breakpoint_at;
12261
956a9fb9 12262 annotate_catchpoint (b->number);
f7f9143b 12263
112e8700 12264 if (uiout->is_mi_like_p ())
f7f9143b 12265 {
112e8700 12266 uiout->field_string ("reason",
956a9fb9 12267 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12268 uiout->field_string ("disp", bpdisp_text (b->disposition));
f7f9143b
JB
12269 }
12270
112e8700
SM
12271 uiout->text (b->disposition == disp_del
12272 ? "\nTemporary catchpoint " : "\nCatchpoint ");
381befee 12273 uiout->field_signed ("bkptno", b->number);
112e8700 12274 uiout->text (", ");
f7f9143b 12275
45db7c09
PA
12276 /* ada_exception_name_addr relies on the selected frame being the
12277 current frame. Need to do this here because this function may be
12278 called more than once when printing a stop, and below, we'll
12279 select the first frame past the Ada run-time (see
12280 ada_find_printable_frame). */
12281 select_frame (get_current_frame ());
12282
37f6a7f4
TT
12283 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12284 switch (c->m_kind)
f7f9143b 12285 {
761269c8
JB
12286 case ada_catch_exception:
12287 case ada_catch_exception_unhandled:
9f757bf7 12288 case ada_catch_handlers:
956a9fb9 12289 {
37f6a7f4 12290 const CORE_ADDR addr = ada_exception_name_addr (c->m_kind, b);
956a9fb9
JB
12291 char exception_name[256];
12292
12293 if (addr != 0)
12294 {
c714b426
PA
12295 read_memory (addr, (gdb_byte *) exception_name,
12296 sizeof (exception_name) - 1);
956a9fb9
JB
12297 exception_name [sizeof (exception_name) - 1] = '\0';
12298 }
12299 else
12300 {
12301 /* For some reason, we were unable to read the exception
12302 name. This could happen if the Runtime was compiled
12303 without debugging info, for instance. In that case,
12304 just replace the exception name by the generic string
12305 "exception" - it will read as "an exception" in the
12306 notification we are about to print. */
967cff16 12307 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12308 }
12309 /* In the case of unhandled exception breakpoints, we print
12310 the exception name as "unhandled EXCEPTION_NAME", to make
12311 it clearer to the user which kind of catchpoint just got
12312 hit. We used ui_out_text to make sure that this extra
12313 info does not pollute the exception name in the MI case. */
37f6a7f4 12314 if (c->m_kind == ada_catch_exception_unhandled)
112e8700
SM
12315 uiout->text ("unhandled ");
12316 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12317 }
12318 break;
761269c8 12319 case ada_catch_assert:
956a9fb9
JB
12320 /* In this case, the name of the exception is not really
12321 important. Just print "failed assertion" to make it clearer
12322 that his program just hit an assertion-failure catchpoint.
12323 We used ui_out_text because this info does not belong in
12324 the MI output. */
112e8700 12325 uiout->text ("failed assertion");
956a9fb9 12326 break;
f7f9143b 12327 }
e547c119 12328
6f46ac85 12329 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12330 if (exception_message != NULL)
12331 {
e547c119 12332 uiout->text (" (");
6f46ac85 12333 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12334 uiout->text (")");
e547c119
JB
12335 }
12336
112e8700 12337 uiout->text (" at ");
956a9fb9 12338 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12339
12340 return PRINT_SRC_AND_LOC;
12341}
12342
12343/* Implement the PRINT_ONE method in the breakpoint_ops structure
12344 for all exception catchpoint kinds. */
12345
12346static void
37f6a7f4 12347print_one_exception (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 12348{
79a45e25 12349 struct ui_out *uiout = current_uiout;
28010a5d 12350 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45b7d
TT
12351 struct value_print_options opts;
12352
12353 get_user_print_options (&opts);
f06f1252 12354
79a45b7d 12355 if (opts.addressprint)
f06f1252 12356 uiout->field_skip ("addr");
f7f9143b
JB
12357
12358 annotate_field (5);
37f6a7f4 12359 switch (c->m_kind)
f7f9143b 12360 {
761269c8 12361 case ada_catch_exception:
dda83cd7
SM
12362 if (!c->excep_string.empty ())
12363 {
bc18fbb5
TT
12364 std::string msg = string_printf (_("`%s' Ada exception"),
12365 c->excep_string.c_str ());
28010a5d 12366
dda83cd7
SM
12367 uiout->field_string ("what", msg);
12368 }
12369 else
12370 uiout->field_string ("what", "all Ada exceptions");
12371
12372 break;
f7f9143b 12373
761269c8 12374 case ada_catch_exception_unhandled:
dda83cd7
SM
12375 uiout->field_string ("what", "unhandled Ada exceptions");
12376 break;
f7f9143b 12377
9f757bf7 12378 case ada_catch_handlers:
dda83cd7
SM
12379 if (!c->excep_string.empty ())
12380 {
9f757bf7
XR
12381 uiout->field_fmt ("what",
12382 _("`%s' Ada exception handlers"),
bc18fbb5 12383 c->excep_string.c_str ());
dda83cd7
SM
12384 }
12385 else
9f757bf7 12386 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12387 break;
9f757bf7 12388
761269c8 12389 case ada_catch_assert:
dda83cd7
SM
12390 uiout->field_string ("what", "failed Ada assertions");
12391 break;
f7f9143b
JB
12392
12393 default:
dda83cd7
SM
12394 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12395 break;
f7f9143b
JB
12396 }
12397}
12398
12399/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12400 for all exception catchpoint kinds. */
12401
12402static void
37f6a7f4 12403print_mention_exception (struct breakpoint *b)
f7f9143b 12404{
28010a5d 12405 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45e25 12406 struct ui_out *uiout = current_uiout;
28010a5d 12407
112e8700 12408 uiout->text (b->disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12409 : _("Catchpoint "));
381befee 12410 uiout->field_signed ("bkptno", b->number);
112e8700 12411 uiout->text (": ");
00eb2c4a 12412
37f6a7f4 12413 switch (c->m_kind)
f7f9143b 12414 {
761269c8 12415 case ada_catch_exception:
dda83cd7 12416 if (!c->excep_string.empty ())
00eb2c4a 12417 {
862d101a 12418 std::string info = string_printf (_("`%s' Ada exception"),
bc18fbb5 12419 c->excep_string.c_str ());
4915bfdc 12420 uiout->text (info);
00eb2c4a 12421 }
dda83cd7
SM
12422 else
12423 uiout->text (_("all Ada exceptions"));
12424 break;
f7f9143b 12425
761269c8 12426 case ada_catch_exception_unhandled:
dda83cd7
SM
12427 uiout->text (_("unhandled Ada exceptions"));
12428 break;
9f757bf7
XR
12429
12430 case ada_catch_handlers:
dda83cd7 12431 if (!c->excep_string.empty ())
9f757bf7
XR
12432 {
12433 std::string info
12434 = string_printf (_("`%s' Ada exception handlers"),
bc18fbb5 12435 c->excep_string.c_str ());
4915bfdc 12436 uiout->text (info);
9f757bf7 12437 }
dda83cd7
SM
12438 else
12439 uiout->text (_("all Ada exceptions handlers"));
12440 break;
9f757bf7 12441
761269c8 12442 case ada_catch_assert:
dda83cd7
SM
12443 uiout->text (_("failed Ada assertions"));
12444 break;
f7f9143b
JB
12445
12446 default:
dda83cd7
SM
12447 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12448 break;
f7f9143b
JB
12449 }
12450}
12451
6149aea9
PA
12452/* Implement the PRINT_RECREATE method in the breakpoint_ops structure
12453 for all exception catchpoint kinds. */
12454
12455static void
37f6a7f4 12456print_recreate_exception (struct breakpoint *b, struct ui_file *fp)
6149aea9 12457{
28010a5d
PA
12458 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12459
37f6a7f4 12460 switch (c->m_kind)
6149aea9 12461 {
761269c8 12462 case ada_catch_exception:
6149aea9 12463 fprintf_filtered (fp, "catch exception");
bc18fbb5
TT
12464 if (!c->excep_string.empty ())
12465 fprintf_filtered (fp, " %s", c->excep_string.c_str ());
6149aea9
PA
12466 break;
12467
761269c8 12468 case ada_catch_exception_unhandled:
78076abc 12469 fprintf_filtered (fp, "catch exception unhandled");
6149aea9
PA
12470 break;
12471
9f757bf7
XR
12472 case ada_catch_handlers:
12473 fprintf_filtered (fp, "catch handlers");
12474 break;
12475
761269c8 12476 case ada_catch_assert:
6149aea9
PA
12477 fprintf_filtered (fp, "catch assert");
12478 break;
12479
12480 default:
12481 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12482 }
d9b3f62e 12483 print_recreate_thread (b, fp);
6149aea9
PA
12484}
12485
d66beefa 12486/* Virtual table for breakpoint type. */
2060206e 12487static struct breakpoint_ops catch_exception_breakpoint_ops;
9f757bf7 12488
f06f1252
TT
12489/* See ada-lang.h. */
12490
12491bool
12492is_ada_exception_catchpoint (breakpoint *bp)
12493{
d66beefa 12494 return bp->ops == &catch_exception_breakpoint_ops;
f06f1252
TT
12495}
12496
f7f9143b
JB
12497/* Split the arguments specified in a "catch exception" command.
12498 Set EX to the appropriate catchpoint type.
28010a5d 12499 Set EXCEP_STRING to the name of the specific exception if
5845583d 12500 specified by the user.
9f757bf7
XR
12501 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12502 "catch handlers" command. False otherwise.
5845583d
JB
12503 If a condition is found at the end of the arguments, the condition
12504 expression is stored in COND_STRING (memory must be deallocated
12505 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12506
12507static void
a121b7c1 12508catch_ada_exception_command_split (const char *args,
9f757bf7 12509 bool is_catch_handlers_cmd,
dda83cd7 12510 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12511 std::string *excep_string,
12512 std::string *cond_string)
f7f9143b 12513{
bc18fbb5 12514 std::string exception_name;
f7f9143b 12515
bc18fbb5
TT
12516 exception_name = extract_arg (&args);
12517 if (exception_name == "if")
5845583d
JB
12518 {
12519 /* This is not an exception name; this is the start of a condition
12520 expression for a catchpoint on all exceptions. So, "un-get"
12521 this token, and set exception_name to NULL. */
bc18fbb5 12522 exception_name.clear ();
5845583d
JB
12523 args -= 2;
12524 }
f7f9143b 12525
5845583d 12526 /* Check to see if we have a condition. */
f7f9143b 12527
f1735a53 12528 args = skip_spaces (args);
61012eef 12529 if (startswith (args, "if")
5845583d
JB
12530 && (isspace (args[2]) || args[2] == '\0'))
12531 {
12532 args += 2;
f1735a53 12533 args = skip_spaces (args);
5845583d
JB
12534
12535 if (args[0] == '\0')
dda83cd7 12536 error (_("Condition missing after `if' keyword"));
bc18fbb5 12537 *cond_string = args;
5845583d
JB
12538
12539 args += strlen (args);
12540 }
12541
12542 /* Check that we do not have any more arguments. Anything else
12543 is unexpected. */
f7f9143b
JB
12544
12545 if (args[0] != '\0')
12546 error (_("Junk at end of expression"));
12547
9f757bf7
XR
12548 if (is_catch_handlers_cmd)
12549 {
12550 /* Catch handling of exceptions. */
12551 *ex = ada_catch_handlers;
12552 *excep_string = exception_name;
12553 }
bc18fbb5 12554 else if (exception_name.empty ())
f7f9143b
JB
12555 {
12556 /* Catch all exceptions. */
761269c8 12557 *ex = ada_catch_exception;
bc18fbb5 12558 excep_string->clear ();
f7f9143b 12559 }
bc18fbb5 12560 else if (exception_name == "unhandled")
f7f9143b
JB
12561 {
12562 /* Catch unhandled exceptions. */
761269c8 12563 *ex = ada_catch_exception_unhandled;
bc18fbb5 12564 excep_string->clear ();
f7f9143b
JB
12565 }
12566 else
12567 {
12568 /* Catch a specific exception. */
761269c8 12569 *ex = ada_catch_exception;
28010a5d 12570 *excep_string = exception_name;
f7f9143b
JB
12571 }
12572}
12573
12574/* Return the name of the symbol on which we should break in order to
12575 implement a catchpoint of the EX kind. */
12576
12577static const char *
761269c8 12578ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12579{
3eecfa55
JB
12580 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12581
12582 gdb_assert (data->exception_info != NULL);
0259addd 12583
f7f9143b
JB
12584 switch (ex)
12585 {
761269c8 12586 case ada_catch_exception:
dda83cd7
SM
12587 return (data->exception_info->catch_exception_sym);
12588 break;
761269c8 12589 case ada_catch_exception_unhandled:
dda83cd7
SM
12590 return (data->exception_info->catch_exception_unhandled_sym);
12591 break;
761269c8 12592 case ada_catch_assert:
dda83cd7
SM
12593 return (data->exception_info->catch_assert_sym);
12594 break;
9f757bf7 12595 case ada_catch_handlers:
dda83cd7
SM
12596 return (data->exception_info->catch_handlers_sym);
12597 break;
f7f9143b 12598 default:
dda83cd7
SM
12599 internal_error (__FILE__, __LINE__,
12600 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12601 }
12602}
12603
f7f9143b
JB
12604/* Return the condition that will be used to match the current exception
12605 being raised with the exception that the user wants to catch. This
12606 assumes that this condition is used when the inferior just triggered
12607 an exception catchpoint.
cb7de75e 12608 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12609
cb7de75e 12610static std::string
9f757bf7 12611ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12612 enum ada_exception_catchpoint_kind ex)
f7f9143b 12613{
fccf9de1 12614 bool is_standard_exc = false;
cb7de75e 12615 std::string result;
9f757bf7
XR
12616
12617 if (ex == ada_catch_handlers)
12618 {
12619 /* For exception handlers catchpoints, the condition string does
dda83cd7 12620 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12621 result = ("long_integer (GNAT_GCC_exception_Access"
12622 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12623 }
12624 else
fccf9de1 12625 result = "long_integer (e)";
3d0b0fa3 12626
0963b4bd 12627 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12628 runtime units that have been compiled without debugging info; if
28010a5d 12629 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12630 exception (e.g. "constraint_error") then, during the evaluation
12631 of the condition expression, the symbol lookup on this name would
0963b4bd 12632 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12633 may then be set only on user-defined exceptions which have the
12634 same not-fully-qualified name (e.g. my_package.constraint_error).
12635
12636 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12637 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12638 exception constraint_error" is rewritten into "catch exception
12639 standard.constraint_error".
12640
85102364 12641 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12642 the inferior program, then the only way to specify this exception as a
12643 breakpoint condition is to use its fully-qualified named:
fccf9de1 12644 e.g. my_package.constraint_error. */
3d0b0fa3 12645
696d6f4d 12646 for (const char *name : standard_exc)
3d0b0fa3 12647 {
696d6f4d 12648 if (strcmp (name, excep_string) == 0)
3d0b0fa3 12649 {
fccf9de1 12650 is_standard_exc = true;
9f757bf7 12651 break;
3d0b0fa3
JB
12652 }
12653 }
9f757bf7 12654
fccf9de1
TT
12655 result += " = ";
12656
12657 if (is_standard_exc)
12658 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12659 else
12660 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12661
9f757bf7 12662 return result;
f7f9143b
JB
12663}
12664
12665/* Return the symtab_and_line that should be used to insert an exception
12666 catchpoint of the TYPE kind.
12667
28010a5d
PA
12668 ADDR_STRING returns the name of the function where the real
12669 breakpoint that implements the catchpoints is set, depending on the
12670 type of catchpoint we need to create. */
f7f9143b
JB
12671
12672static struct symtab_and_line
bc18fbb5 12673ada_exception_sal (enum ada_exception_catchpoint_kind ex,
cc12f4a8 12674 std::string *addr_string, const struct breakpoint_ops **ops)
f7f9143b
JB
12675{
12676 const char *sym_name;
12677 struct symbol *sym;
f7f9143b 12678
0259addd
JB
12679 /* First, find out which exception support info to use. */
12680 ada_exception_support_info_sniffer ();
12681
12682 /* Then lookup the function on which we will break in order to catch
f7f9143b 12683 the Ada exceptions requested by the user. */
f7f9143b
JB
12684 sym_name = ada_exception_sym_name (ex);
12685 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12686
57aff202
JB
12687 if (sym == NULL)
12688 error (_("Catchpoint symbol not found: %s"), sym_name);
12689
66d7f48f 12690 if (sym->aclass () != LOC_BLOCK)
57aff202 12691 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12692
12693 /* Set ADDR_STRING. */
cc12f4a8 12694 *addr_string = sym_name;
f7f9143b 12695
f7f9143b 12696 /* Set OPS. */
d66beefa 12697 *ops = &catch_exception_breakpoint_ops;
f7f9143b 12698
f17011e0 12699 return find_function_start_sal (sym, 1);
f7f9143b
JB
12700}
12701
b4a5b78b 12702/* Create an Ada exception catchpoint.
f7f9143b 12703
b4a5b78b 12704 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12705
bc18fbb5 12706 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12707 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12708 of the exception to which this catchpoint applies.
2df4d1d5 12709
bc18fbb5 12710 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12711
b4a5b78b
JB
12712 TEMPFLAG, if nonzero, means that the underlying breakpoint
12713 should be temporary.
28010a5d 12714
b4a5b78b 12715 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12716
349774ef 12717void
28010a5d 12718create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12719 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12720 const std::string &excep_string,
56ecd069 12721 const std::string &cond_string,
28010a5d 12722 int tempflag,
349774ef 12723 int disabled,
28010a5d
PA
12724 int from_tty)
12725{
cc12f4a8 12726 std::string addr_string;
b4a5b78b 12727 const struct breakpoint_ops *ops = NULL;
bc18fbb5 12728 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string, &ops);
28010a5d 12729
37f6a7f4 12730 std::unique_ptr<ada_catchpoint> c (new ada_catchpoint (ex_kind));
cc12f4a8 12731 init_ada_exception_breakpoint (c.get (), gdbarch, sal, addr_string.c_str (),
349774ef 12732 ops, tempflag, disabled, from_tty);
28010a5d 12733 c->excep_string = excep_string;
9f757bf7 12734 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12735 if (!cond_string.empty ())
733d554a 12736 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12737 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12738}
12739
9ac4176b
PA
12740/* Implement the "catch exception" command. */
12741
12742static void
eb4c3f4a 12743catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12744 struct cmd_list_element *command)
12745{
a121b7c1 12746 const char *arg = arg_entry;
9ac4176b
PA
12747 struct gdbarch *gdbarch = get_current_arch ();
12748 int tempflag;
761269c8 12749 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12750 std::string excep_string;
56ecd069 12751 std::string cond_string;
9ac4176b 12752
0f8e2034 12753 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12754
12755 if (!arg)
12756 arg = "";
9f757bf7 12757 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12758 &cond_string);
9f757bf7
XR
12759 create_ada_exception_catchpoint (gdbarch, ex_kind,
12760 excep_string, cond_string,
12761 tempflag, 1 /* enabled */,
12762 from_tty);
12763}
12764
12765/* Implement the "catch handlers" command. */
12766
12767static void
12768catch_ada_handlers_command (const char *arg_entry, int from_tty,
12769 struct cmd_list_element *command)
12770{
12771 const char *arg = arg_entry;
12772 struct gdbarch *gdbarch = get_current_arch ();
12773 int tempflag;
12774 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12775 std::string excep_string;
56ecd069 12776 std::string cond_string;
9f757bf7 12777
0f8e2034 12778 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12779
12780 if (!arg)
12781 arg = "";
12782 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12783 &cond_string);
b4a5b78b
JB
12784 create_ada_exception_catchpoint (gdbarch, ex_kind,
12785 excep_string, cond_string,
349774ef
JB
12786 tempflag, 1 /* enabled */,
12787 from_tty);
9ac4176b
PA
12788}
12789
71bed2db
TT
12790/* Completion function for the Ada "catch" commands. */
12791
12792static void
12793catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12794 const char *text, const char *word)
12795{
12796 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12797
12798 for (const ada_exc_info &info : exceptions)
12799 {
12800 if (startswith (info.name, word))
b02f78f9 12801 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12802 }
12803}
12804
b4a5b78b 12805/* Split the arguments specified in a "catch assert" command.
5845583d 12806
b4a5b78b
JB
12807 ARGS contains the command's arguments (or the empty string if
12808 no arguments were passed).
5845583d
JB
12809
12810 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12811 (the memory needs to be deallocated after use). */
5845583d 12812
b4a5b78b 12813static void
56ecd069 12814catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12815{
f1735a53 12816 args = skip_spaces (args);
f7f9143b 12817
5845583d 12818 /* Check whether a condition was provided. */
61012eef 12819 if (startswith (args, "if")
5845583d 12820 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12821 {
5845583d 12822 args += 2;
f1735a53 12823 args = skip_spaces (args);
5845583d 12824 if (args[0] == '\0')
dda83cd7 12825 error (_("condition missing after `if' keyword"));
56ecd069 12826 cond_string.assign (args);
f7f9143b
JB
12827 }
12828
5845583d
JB
12829 /* Otherwise, there should be no other argument at the end of
12830 the command. */
12831 else if (args[0] != '\0')
12832 error (_("Junk at end of arguments."));
f7f9143b
JB
12833}
12834
9ac4176b
PA
12835/* Implement the "catch assert" command. */
12836
12837static void
eb4c3f4a 12838catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12839 struct cmd_list_element *command)
12840{
a121b7c1 12841 const char *arg = arg_entry;
9ac4176b
PA
12842 struct gdbarch *gdbarch = get_current_arch ();
12843 int tempflag;
56ecd069 12844 std::string cond_string;
9ac4176b 12845
0f8e2034 12846 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12847
12848 if (!arg)
12849 arg = "";
56ecd069 12850 catch_ada_assert_command_split (arg, cond_string);
761269c8 12851 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 12852 "", cond_string,
349774ef
JB
12853 tempflag, 1 /* enabled */,
12854 from_tty);
9ac4176b 12855}
778865d3
JB
12856
12857/* Return non-zero if the symbol SYM is an Ada exception object. */
12858
12859static int
12860ada_is_exception_sym (struct symbol *sym)
12861{
5f9c5a63 12862 const char *type_name = sym->type ()->name ();
778865d3 12863
66d7f48f
SM
12864 return (sym->aclass () != LOC_TYPEDEF
12865 && sym->aclass () != LOC_BLOCK
12866 && sym->aclass () != LOC_CONST
12867 && sym->aclass () != LOC_UNRESOLVED
dda83cd7 12868 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12869}
12870
12871/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12872 Ada exception object. This matches all exceptions except the ones
12873 defined by the Ada language. */
12874
12875static int
12876ada_is_non_standard_exception_sym (struct symbol *sym)
12877{
778865d3
JB
12878 if (!ada_is_exception_sym (sym))
12879 return 0;
12880
696d6f4d
TT
12881 for (const char *name : standard_exc)
12882 if (strcmp (sym->linkage_name (), name) == 0)
778865d3
JB
12883 return 0; /* A standard exception. */
12884
12885 /* Numeric_Error is also a standard exception, so exclude it.
12886 See the STANDARD_EXC description for more details as to why
12887 this exception is not listed in that array. */
987012b8 12888 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12889 return 0;
12890
12891 return 1;
12892}
12893
ab816a27 12894/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12895 objects.
12896
12897 The comparison is determined first by exception name, and then
12898 by exception address. */
12899
ab816a27 12900bool
cc536b21 12901ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12902{
778865d3
JB
12903 int result;
12904
ab816a27
TT
12905 result = strcmp (name, other.name);
12906 if (result < 0)
12907 return true;
12908 if (result == 0 && addr < other.addr)
12909 return true;
12910 return false;
12911}
778865d3 12912
ab816a27 12913bool
cc536b21 12914ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12915{
12916 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12917}
12918
12919/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12920 routine, but keeping the first SKIP elements untouched.
12921
12922 All duplicates are also removed. */
12923
12924static void
ab816a27 12925sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12926 int skip)
12927{
ab816a27
TT
12928 std::sort (exceptions->begin () + skip, exceptions->end ());
12929 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12930 exceptions->end ());
778865d3
JB
12931}
12932
778865d3
JB
12933/* Add all exceptions defined by the Ada standard whose name match
12934 a regular expression.
12935
12936 If PREG is not NULL, then this regexp_t object is used to
12937 perform the symbol name matching. Otherwise, no name-based
12938 filtering is performed.
12939
12940 EXCEPTIONS is a vector of exceptions to which matching exceptions
12941 gets pushed. */
12942
12943static void
2d7cc5c7 12944ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12945 std::vector<ada_exc_info> *exceptions)
778865d3 12946{
696d6f4d 12947 for (const char *name : standard_exc)
778865d3 12948 {
696d6f4d 12949 if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
778865d3
JB
12950 {
12951 struct bound_minimal_symbol msymbol
696d6f4d 12952 = ada_lookup_simple_minsym (name);
778865d3
JB
12953
12954 if (msymbol.minsym != NULL)
12955 {
12956 struct ada_exc_info info
696d6f4d 12957 = {name, BMSYMBOL_VALUE_ADDRESS (msymbol)};
778865d3 12958
ab816a27 12959 exceptions->push_back (info);
778865d3
JB
12960 }
12961 }
12962 }
12963}
12964
12965/* Add all Ada exceptions defined locally and accessible from the given
12966 FRAME.
12967
12968 If PREG is not NULL, then this regexp_t object is used to
12969 perform the symbol name matching. Otherwise, no name-based
12970 filtering is performed.
12971
12972 EXCEPTIONS is a vector of exceptions to which matching exceptions
12973 gets pushed. */
12974
12975static void
2d7cc5c7
PA
12976ada_add_exceptions_from_frame (compiled_regex *preg,
12977 struct frame_info *frame,
ab816a27 12978 std::vector<ada_exc_info> *exceptions)
778865d3 12979{
3977b71f 12980 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
12981
12982 while (block != 0)
12983 {
12984 struct block_iterator iter;
12985 struct symbol *sym;
12986
12987 ALL_BLOCK_SYMBOLS (block, iter, sym)
12988 {
66d7f48f 12989 switch (sym->aclass ())
778865d3
JB
12990 {
12991 case LOC_TYPEDEF:
12992 case LOC_BLOCK:
12993 case LOC_CONST:
12994 break;
12995 default:
12996 if (ada_is_exception_sym (sym))
12997 {
987012b8 12998 struct ada_exc_info info = {sym->print_name (),
778865d3
JB
12999 SYMBOL_VALUE_ADDRESS (sym)};
13000
ab816a27 13001 exceptions->push_back (info);
778865d3
JB
13002 }
13003 }
13004 }
13005 if (BLOCK_FUNCTION (block) != NULL)
13006 break;
13007 block = BLOCK_SUPERBLOCK (block);
13008 }
13009}
13010
14bc53a8
PA
13011/* Return true if NAME matches PREG or if PREG is NULL. */
13012
13013static bool
2d7cc5c7 13014name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
13015{
13016 return (preg == NULL
f945dedf 13017 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
13018}
13019
778865d3
JB
13020/* Add all exceptions defined globally whose name name match
13021 a regular expression, excluding standard exceptions.
13022
13023 The reason we exclude standard exceptions is that they need
13024 to be handled separately: Standard exceptions are defined inside
13025 a runtime unit which is normally not compiled with debugging info,
13026 and thus usually do not show up in our symbol search. However,
13027 if the unit was in fact built with debugging info, we need to
13028 exclude them because they would duplicate the entry we found
13029 during the special loop that specifically searches for those
13030 standard exceptions.
13031
13032 If PREG is not NULL, then this regexp_t object is used to
13033 perform the symbol name matching. Otherwise, no name-based
13034 filtering is performed.
13035
13036 EXCEPTIONS is a vector of exceptions to which matching exceptions
13037 gets pushed. */
13038
13039static void
2d7cc5c7 13040ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13041 std::vector<ada_exc_info> *exceptions)
778865d3 13042{
14bc53a8
PA
13043 /* In Ada, the symbol "search name" is a linkage name, whereas the
13044 regular expression used to do the matching refers to the natural
13045 name. So match against the decoded name. */
13046 expand_symtabs_matching (NULL,
b5ec771e 13047 lookup_name_info::match_any (),
14bc53a8
PA
13048 [&] (const char *search_name)
13049 {
f945dedf
CB
13050 std::string decoded = ada_decode (search_name);
13051 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13052 },
13053 NULL,
03a8ea51 13054 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 13055 VARIABLES_DOMAIN);
778865d3 13056
2030c079 13057 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13058 {
b669c953 13059 for (compunit_symtab *s : objfile->compunits ())
778865d3 13060 {
af39c5c8 13061 const struct blockvector *bv = s->blockvector ();
d8aeb77f 13062 int i;
778865d3 13063
d8aeb77f
TT
13064 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13065 {
582942f4 13066 const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
d8aeb77f
TT
13067 struct block_iterator iter;
13068 struct symbol *sym;
778865d3 13069
d8aeb77f
TT
13070 ALL_BLOCK_SYMBOLS (b, iter, sym)
13071 if (ada_is_non_standard_exception_sym (sym)
987012b8 13072 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13073 {
13074 struct ada_exc_info info
987012b8 13075 = {sym->print_name (), SYMBOL_VALUE_ADDRESS (sym)};
d8aeb77f
TT
13076
13077 exceptions->push_back (info);
13078 }
13079 }
778865d3
JB
13080 }
13081 }
13082}
13083
13084/* Implements ada_exceptions_list with the regular expression passed
13085 as a regex_t, rather than a string.
13086
13087 If not NULL, PREG is used to filter out exceptions whose names
13088 do not match. Otherwise, all exceptions are listed. */
13089
ab816a27 13090static std::vector<ada_exc_info>
2d7cc5c7 13091ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13092{
ab816a27 13093 std::vector<ada_exc_info> result;
778865d3
JB
13094 int prev_len;
13095
13096 /* First, list the known standard exceptions. These exceptions
13097 need to be handled separately, as they are usually defined in
13098 runtime units that have been compiled without debugging info. */
13099
13100 ada_add_standard_exceptions (preg, &result);
13101
13102 /* Next, find all exceptions whose scope is local and accessible
13103 from the currently selected frame. */
13104
13105 if (has_stack_frames ())
13106 {
ab816a27 13107 prev_len = result.size ();
778865d3
JB
13108 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13109 &result);
ab816a27 13110 if (result.size () > prev_len)
778865d3
JB
13111 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13112 }
13113
13114 /* Add all exceptions whose scope is global. */
13115
ab816a27 13116 prev_len = result.size ();
778865d3 13117 ada_add_global_exceptions (preg, &result);
ab816a27 13118 if (result.size () > prev_len)
778865d3
JB
13119 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13120
778865d3
JB
13121 return result;
13122}
13123
13124/* Return a vector of ada_exc_info.
13125
13126 If REGEXP is NULL, all exceptions are included in the result.
13127 Otherwise, it should contain a valid regular expression,
13128 and only the exceptions whose names match that regular expression
13129 are included in the result.
13130
13131 The exceptions are sorted in the following order:
13132 - Standard exceptions (defined by the Ada language), in
13133 alphabetical order;
13134 - Exceptions only visible from the current frame, in
13135 alphabetical order;
13136 - Exceptions whose scope is global, in alphabetical order. */
13137
ab816a27 13138std::vector<ada_exc_info>
778865d3
JB
13139ada_exceptions_list (const char *regexp)
13140{
2d7cc5c7
PA
13141 if (regexp == NULL)
13142 return ada_exceptions_list_1 (NULL);
778865d3 13143
2d7cc5c7
PA
13144 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13145 return ada_exceptions_list_1 (&reg);
778865d3
JB
13146}
13147
13148/* Implement the "info exceptions" command. */
13149
13150static void
1d12d88f 13151info_exceptions_command (const char *regexp, int from_tty)
778865d3 13152{
778865d3 13153 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13154
ab816a27 13155 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13156
13157 if (regexp != NULL)
13158 printf_filtered
13159 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13160 else
13161 printf_filtered (_("All defined Ada exceptions:\n"));
13162
ab816a27
TT
13163 for (const ada_exc_info &info : exceptions)
13164 printf_filtered ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13165}
13166
6c038f32
PH
13167\f
13168 /* Language vector */
13169
b5ec771e
PA
13170/* symbol_name_matcher_ftype adapter for wild_match. */
13171
13172static bool
13173do_wild_match (const char *symbol_search_name,
13174 const lookup_name_info &lookup_name,
a207cff2 13175 completion_match_result *comp_match_res)
b5ec771e
PA
13176{
13177 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13178}
13179
13180/* symbol_name_matcher_ftype adapter for full_match. */
13181
13182static bool
13183do_full_match (const char *symbol_search_name,
13184 const lookup_name_info &lookup_name,
a207cff2 13185 completion_match_result *comp_match_res)
b5ec771e 13186{
959d6a67
TT
13187 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13188
13189 /* If both symbols start with "_ada_", just let the loop below
13190 handle the comparison. However, if only the symbol name starts
13191 with "_ada_", skip the prefix and let the match proceed as
13192 usual. */
13193 if (startswith (symbol_search_name, "_ada_")
13194 && !startswith (lname, "_ada"))
86b44259
TT
13195 symbol_search_name += 5;
13196
86b44259
TT
13197 int uscore_count = 0;
13198 while (*lname != '\0')
13199 {
13200 if (*symbol_search_name != *lname)
13201 {
13202 if (*symbol_search_name == 'B' && uscore_count == 2
13203 && symbol_search_name[1] == '_')
13204 {
13205 symbol_search_name += 2;
13206 while (isdigit (*symbol_search_name))
13207 ++symbol_search_name;
13208 if (symbol_search_name[0] == '_'
13209 && symbol_search_name[1] == '_')
13210 {
13211 symbol_search_name += 2;
13212 continue;
13213 }
13214 }
13215 return false;
13216 }
13217
13218 if (*symbol_search_name == '_')
13219 ++uscore_count;
13220 else
13221 uscore_count = 0;
13222
13223 ++symbol_search_name;
13224 ++lname;
13225 }
13226
13227 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13228}
13229
a2cd4f14
JB
13230/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13231
13232static bool
13233do_exact_match (const char *symbol_search_name,
13234 const lookup_name_info &lookup_name,
13235 completion_match_result *comp_match_res)
13236{
13237 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13238}
13239
b5ec771e
PA
13240/* Build the Ada lookup name for LOOKUP_NAME. */
13241
13242ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13243{
e0802d59 13244 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13245
6a780b67 13246 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13247 {
13248 if (user_name.back () == '>')
e0802d59 13249 m_encoded_name
5ac58899 13250 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13251 else
e0802d59 13252 m_encoded_name
5ac58899 13253 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13254 m_encoded_p = true;
13255 m_verbatim_p = true;
13256 m_wild_match_p = false;
13257 m_standard_p = false;
13258 }
13259 else
13260 {
13261 m_verbatim_p = false;
13262
e0802d59 13263 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13264
13265 if (!m_encoded_p)
13266 {
e0802d59 13267 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13268 m_encoded_name = ada_encode_1 (folded, false);
13269 if (m_encoded_name.empty ())
5ac58899 13270 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13271 }
13272 else
5ac58899 13273 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13274
13275 /* Handle the 'package Standard' special case. See description
13276 of m_standard_p. */
13277 if (startswith (m_encoded_name.c_str (), "standard__"))
13278 {
13279 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13280 m_standard_p = true;
13281 }
13282 else
13283 m_standard_p = false;
74ccd7f5 13284
b5ec771e
PA
13285 /* If the name contains a ".", then the user is entering a fully
13286 qualified entity name, and the match must not be done in wild
13287 mode. Similarly, if the user wants to complete what looks
13288 like an encoded name, the match must not be done in wild
13289 mode. Also, in the standard__ special case always do
13290 non-wild matching. */
13291 m_wild_match_p
13292 = (lookup_name.match_type () != symbol_name_match_type::FULL
13293 && !m_encoded_p
13294 && !m_standard_p
13295 && user_name.find ('.') == std::string::npos);
13296 }
13297}
13298
13299/* symbol_name_matcher_ftype method for Ada. This only handles
13300 completion mode. */
13301
13302static bool
13303ada_symbol_name_matches (const char *symbol_search_name,
13304 const lookup_name_info &lookup_name,
a207cff2 13305 completion_match_result *comp_match_res)
74ccd7f5 13306{
b5ec771e
PA
13307 return lookup_name.ada ().matches (symbol_search_name,
13308 lookup_name.match_type (),
a207cff2 13309 comp_match_res);
b5ec771e
PA
13310}
13311
de63c46b
PA
13312/* A name matcher that matches the symbol name exactly, with
13313 strcmp. */
13314
13315static bool
13316literal_symbol_name_matcher (const char *symbol_search_name,
13317 const lookup_name_info &lookup_name,
13318 completion_match_result *comp_match_res)
13319{
e0802d59 13320 gdb::string_view name_view = lookup_name.name ();
de63c46b 13321
e0802d59
TT
13322 if (lookup_name.completion_mode ()
13323 ? (strncmp (symbol_search_name, name_view.data (),
13324 name_view.size ()) == 0)
13325 : symbol_search_name == name_view)
de63c46b
PA
13326 {
13327 if (comp_match_res != NULL)
13328 comp_match_res->set_match (symbol_search_name);
13329 return true;
13330 }
13331 else
13332 return false;
13333}
13334
c9debfb9 13335/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13336 Ada. */
13337
13338static symbol_name_matcher_ftype *
13339ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13340{
de63c46b
PA
13341 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13342 return literal_symbol_name_matcher;
13343
b5ec771e
PA
13344 if (lookup_name.completion_mode ())
13345 return ada_symbol_name_matches;
74ccd7f5 13346 else
b5ec771e
PA
13347 {
13348 if (lookup_name.ada ().wild_match_p ())
13349 return do_wild_match;
a2cd4f14
JB
13350 else if (lookup_name.ada ().verbatim_p ())
13351 return do_exact_match;
b5ec771e
PA
13352 else
13353 return do_full_match;
13354 }
74ccd7f5
JB
13355}
13356
0874fd07
AB
13357/* Class representing the Ada language. */
13358
13359class ada_language : public language_defn
13360{
13361public:
13362 ada_language ()
0e25e767 13363 : language_defn (language_ada)
0874fd07 13364 { /* Nothing. */ }
5bd40f2a 13365
6f7664a9
AB
13366 /* See language.h. */
13367
13368 const char *name () const override
13369 { return "ada"; }
13370
13371 /* See language.h. */
13372
13373 const char *natural_name () const override
13374 { return "Ada"; }
13375
e171d6f1
AB
13376 /* See language.h. */
13377
13378 const std::vector<const char *> &filename_extensions () const override
13379 {
13380 static const std::vector<const char *> extensions
13381 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13382 return extensions;
13383 }
13384
5bd40f2a
AB
13385 /* Print an array element index using the Ada syntax. */
13386
13387 void print_array_index (struct type *index_type,
13388 LONGEST index,
13389 struct ui_file *stream,
13390 const value_print_options *options) const override
13391 {
13392 struct value *index_value = val_atr (index_type, index);
13393
00c696a6 13394 value_print (index_value, stream, options);
5bd40f2a
AB
13395 fprintf_filtered (stream, " => ");
13396 }
15e5fd35
AB
13397
13398 /* Implement the "read_var_value" language_defn method for Ada. */
13399
13400 struct value *read_var_value (struct symbol *var,
13401 const struct block *var_block,
13402 struct frame_info *frame) const override
13403 {
13404 /* The only case where default_read_var_value is not sufficient
13405 is when VAR is a renaming... */
13406 if (frame != nullptr)
13407 {
13408 const struct block *frame_block = get_frame_block (frame, NULL);
13409 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13410 return ada_read_renaming_var_value (var, frame_block);
13411 }
13412
13413 /* This is a typical case where we expect the default_read_var_value
13414 function to work. */
13415 return language_defn::read_var_value (var, var_block, frame);
13416 }
1fb314aa 13417
2c71f639
TV
13418 /* See language.h. */
13419 virtual bool symbol_printing_suppressed (struct symbol *symbol) const override
13420 {
13421 return symbol->artificial;
13422 }
13423
1fb314aa
AB
13424 /* See language.h. */
13425 void language_arch_info (struct gdbarch *gdbarch,
13426 struct language_arch_info *lai) const override
13427 {
13428 const struct builtin_type *builtin = builtin_type (gdbarch);
13429
7bea47f0
AB
13430 /* Helper function to allow shorter lines below. */
13431 auto add = [&] (struct type *t)
13432 {
13433 lai->add_primitive_type (t);
13434 };
13435
13436 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13437 0, "integer"));
13438 add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13439 0, "long_integer"));
13440 add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13441 0, "short_integer"));
13442 struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
c9f66f00 13443 1, "character");
7bea47f0
AB
13444 lai->set_string_char_type (char_type);
13445 add (char_type);
c9f66f00
TT
13446 add (arch_character_type (gdbarch, 16, 1, "wide_character"));
13447 add (arch_character_type (gdbarch, 32, 1, "wide_wide_character"));
7bea47f0
AB
13448 add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
13449 "float", gdbarch_float_format (gdbarch)));
13450 add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13451 "long_float", gdbarch_double_format (gdbarch)));
13452 add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
13453 0, "long_long_integer"));
13454 add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
13455 "long_long_float",
13456 gdbarch_long_double_format (gdbarch)));
13457 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13458 0, "natural"));
13459 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13460 0, "positive"));
13461 add (builtin->builtin_void);
13462
13463 struct type *system_addr_ptr
1fb314aa
AB
13464 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
13465 "void"));
7bea47f0
AB
13466 system_addr_ptr->set_name ("system__address");
13467 add (system_addr_ptr);
1fb314aa
AB
13468
13469 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13470 type. This is a signed integral type whose size is the same as
13471 the size of addresses. */
7bea47f0
AB
13472 unsigned int addr_length = TYPE_LENGTH (system_addr_ptr);
13473 add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
13474 "storage_offset"));
1fb314aa 13475
7bea47f0 13476 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13477 }
4009ee92
AB
13478
13479 /* See language.h. */
13480
13481 bool iterate_over_symbols
13482 (const struct block *block, const lookup_name_info &name,
13483 domain_enum domain,
13484 gdb::function_view<symbol_found_callback_ftype> callback) const override
13485 {
d1183b06
TT
13486 std::vector<struct block_symbol> results
13487 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13488 for (block_symbol &sym : results)
13489 {
13490 if (!callback (&sym))
13491 return false;
13492 }
13493
13494 return true;
13495 }
6f827019
AB
13496
13497 /* See language.h. */
3456e70c
TT
13498 bool sniff_from_mangled_name
13499 (const char *mangled,
13500 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13501 {
13502 std::string demangled = ada_decode (mangled);
13503
13504 *out = NULL;
13505
13506 if (demangled != mangled && demangled[0] != '<')
13507 {
13508 /* Set the gsymbol language to Ada, but still return 0.
13509 Two reasons for that:
13510
13511 1. For Ada, we prefer computing the symbol's decoded name
13512 on the fly rather than pre-compute it, in order to save
13513 memory (Ada projects are typically very large).
13514
13515 2. There are some areas in the definition of the GNAT
13516 encoding where, with a bit of bad luck, we might be able
13517 to decode a non-Ada symbol, generating an incorrect
13518 demangled name (Eg: names ending with "TB" for instance
13519 are identified as task bodies and so stripped from
13520 the decoded name returned).
13521
13522 Returning true, here, but not setting *DEMANGLED, helps us get
13523 a little bit of the best of both worlds. Because we're last,
13524 we should not affect any of the other languages that were
13525 able to demangle the symbol before us; we get to correctly
13526 tag Ada symbols as such; and even if we incorrectly tagged a
13527 non-Ada symbol, which should be rare, any routing through the
13528 Ada language should be transparent (Ada tries to behave much
13529 like C/C++ with non-Ada symbols). */
13530 return true;
13531 }
13532
13533 return false;
13534 }
fbfb0a46
AB
13535
13536 /* See language.h. */
13537
3456e70c
TT
13538 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13539 int options) const override
0a50df5d 13540 {
3456e70c 13541 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13542 }
13543
13544 /* See language.h. */
13545
fbfb0a46
AB
13546 void print_type (struct type *type, const char *varstring,
13547 struct ui_file *stream, int show, int level,
13548 const struct type_print_options *flags) const override
13549 {
13550 ada_print_type (type, varstring, stream, show, level, flags);
13551 }
c9debfb9 13552
53fc67f8
AB
13553 /* See language.h. */
13554
13555 const char *word_break_characters (void) const override
13556 {
13557 return ada_completer_word_break_characters;
13558 }
13559
7e56227d
AB
13560 /* See language.h. */
13561
13562 void collect_symbol_completion_matches (completion_tracker &tracker,
13563 complete_symbol_mode mode,
13564 symbol_name_match_type name_match_type,
13565 const char *text, const char *word,
13566 enum type_code code) const override
13567 {
13568 struct symbol *sym;
13569 const struct block *b, *surrounding_static_block = 0;
13570 struct block_iterator iter;
13571
13572 gdb_assert (code == TYPE_CODE_UNDEF);
13573
13574 lookup_name_info lookup_name (text, name_match_type, true);
13575
13576 /* First, look at the partial symtab symbols. */
13577 expand_symtabs_matching (NULL,
13578 lookup_name,
13579 NULL,
13580 NULL,
03a8ea51 13581 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13582 ALL_DOMAIN);
13583
13584 /* At this point scan through the misc symbol vectors and add each
13585 symbol you find to the list. Eventually we want to ignore
13586 anything that isn't a text symbol (everything else will be
13587 handled by the psymtab code above). */
13588
13589 for (objfile *objfile : current_program_space->objfiles ())
13590 {
13591 for (minimal_symbol *msymbol : objfile->msymbols ())
13592 {
13593 QUIT;
13594
13595 if (completion_skip_symbol (mode, msymbol))
13596 continue;
13597
13598 language symbol_language = msymbol->language ();
13599
13600 /* Ada minimal symbols won't have their language set to Ada. If
13601 we let completion_list_add_name compare using the
13602 default/C-like matcher, then when completing e.g., symbols in a
13603 package named "pck", we'd match internal Ada symbols like
13604 "pckS", which are invalid in an Ada expression, unless you wrap
13605 them in '<' '>' to request a verbatim match.
13606
13607 Unfortunately, some Ada encoded names successfully demangle as
13608 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13609 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13610 with the wrong language set. Paper over that issue here. */
13611 if (symbol_language == language_auto
13612 || symbol_language == language_cplus)
13613 symbol_language = language_ada;
13614
13615 completion_list_add_name (tracker,
13616 symbol_language,
13617 msymbol->linkage_name (),
13618 lookup_name, text, word);
13619 }
13620 }
13621
13622 /* Search upwards from currently selected frame (so that we can
13623 complete on local vars. */
13624
13625 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
13626 {
13627 if (!BLOCK_SUPERBLOCK (b))
13628 surrounding_static_block = b; /* For elmin of dups */
13629
13630 ALL_BLOCK_SYMBOLS (b, iter, sym)
13631 {
13632 if (completion_skip_symbol (mode, sym))
13633 continue;
13634
13635 completion_list_add_name (tracker,
13636 sym->language (),
13637 sym->linkage_name (),
13638 lookup_name, text, word);
13639 }
13640 }
13641
13642 /* Go through the symtabs and check the externs and statics for
13643 symbols which match. */
13644
13645 for (objfile *objfile : current_program_space->objfiles ())
13646 {
13647 for (compunit_symtab *s : objfile->compunits ())
13648 {
13649 QUIT;
af39c5c8 13650 b = BLOCKVECTOR_BLOCK (s->blockvector (), GLOBAL_BLOCK);
7e56227d
AB
13651 ALL_BLOCK_SYMBOLS (b, iter, sym)
13652 {
13653 if (completion_skip_symbol (mode, sym))
13654 continue;
13655
13656 completion_list_add_name (tracker,
13657 sym->language (),
13658 sym->linkage_name (),
13659 lookup_name, text, word);
13660 }
13661 }
13662 }
13663
13664 for (objfile *objfile : current_program_space->objfiles ())
13665 {
13666 for (compunit_symtab *s : objfile->compunits ())
13667 {
13668 QUIT;
af39c5c8 13669 b = BLOCKVECTOR_BLOCK (s->blockvector (), STATIC_BLOCK);
7e56227d
AB
13670 /* Don't do this block twice. */
13671 if (b == surrounding_static_block)
13672 continue;
13673 ALL_BLOCK_SYMBOLS (b, iter, sym)
13674 {
13675 if (completion_skip_symbol (mode, sym))
13676 continue;
13677
13678 completion_list_add_name (tracker,
13679 sym->language (),
13680 sym->linkage_name (),
13681 lookup_name, text, word);
13682 }
13683 }
13684 }
13685 }
13686
f16a9f57
AB
13687 /* See language.h. */
13688
13689 gdb::unique_xmalloc_ptr<char> watch_location_expression
13690 (struct type *type, CORE_ADDR addr) const override
13691 {
13692 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
13693 std::string name = type_to_string (type);
8579fd13 13694 return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
f16a9f57
AB
13695 }
13696
a1d1fa3e
AB
13697 /* See language.h. */
13698
13699 void value_print (struct value *val, struct ui_file *stream,
13700 const struct value_print_options *options) const override
13701 {
13702 return ada_value_print (val, stream, options);
13703 }
13704
ebe2334e
AB
13705 /* See language.h. */
13706
13707 void value_print_inner
13708 (struct value *val, struct ui_file *stream, int recurse,
13709 const struct value_print_options *options) const override
13710 {
13711 return ada_value_print_inner (val, stream, recurse, options);
13712 }
13713
a78a19b1
AB
13714 /* See language.h. */
13715
13716 struct block_symbol lookup_symbol_nonlocal
13717 (const char *name, const struct block *block,
13718 const domain_enum domain) const override
13719 {
13720 struct block_symbol sym;
13721
13722 sym = ada_lookup_symbol (name, block_static_block (block), domain);
13723 if (sym.symbol != NULL)
13724 return sym;
13725
13726 /* If we haven't found a match at this point, try the primitive
13727 types. In other languages, this search is performed before
13728 searching for global symbols in order to short-circuit that
13729 global-symbol search if it happens that the name corresponds
13730 to a primitive type. But we cannot do the same in Ada, because
13731 it is perfectly legitimate for a program to declare a type which
13732 has the same name as a standard type. If looking up a type in
13733 that situation, we have traditionally ignored the primitive type
13734 in favor of user-defined types. This is why, unlike most other
13735 languages, we search the primitive types this late and only after
13736 having searched the global symbols without success. */
13737
13738 if (domain == VAR_DOMAIN)
13739 {
13740 struct gdbarch *gdbarch;
13741
13742 if (block == NULL)
13743 gdbarch = target_gdbarch ();
13744 else
13745 gdbarch = block_gdbarch (block);
13746 sym.symbol
13747 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13748 if (sym.symbol != NULL)
13749 return sym;
13750 }
13751
13752 return {};
13753 }
13754
87afa652
AB
13755 /* See language.h. */
13756
13757 int parser (struct parser_state *ps) const override
13758 {
13759 warnings_issued = 0;
13760 return ada_parse (ps);
13761 }
13762
ec8cec5b
AB
13763 /* See language.h. */
13764
13765 void emitchar (int ch, struct type *chtype,
13766 struct ui_file *stream, int quoter) const override
13767 {
13768 ada_emit_char (ch, chtype, stream, quoter, 1);
13769 }
13770
52b50f2c
AB
13771 /* See language.h. */
13772
13773 void printchar (int ch, struct type *chtype,
13774 struct ui_file *stream) const override
13775 {
13776 ada_printchar (ch, chtype, stream);
13777 }
13778
d711ee67
AB
13779 /* See language.h. */
13780
13781 void printstr (struct ui_file *stream, struct type *elttype,
13782 const gdb_byte *string, unsigned int length,
13783 const char *encoding, int force_ellipses,
13784 const struct value_print_options *options) const override
13785 {
13786 ada_printstr (stream, elttype, string, length, encoding,
13787 force_ellipses, options);
13788 }
13789
4ffc13fb
AB
13790 /* See language.h. */
13791
13792 void print_typedef (struct type *type, struct symbol *new_symbol,
13793 struct ui_file *stream) const override
13794 {
13795 ada_print_typedef (type, new_symbol, stream);
13796 }
13797
39e7ecca
AB
13798 /* See language.h. */
13799
13800 bool is_string_type_p (struct type *type) const override
13801 {
13802 return ada_is_string_type (type);
13803 }
13804
22e3f3ed
AB
13805 /* See language.h. */
13806
13807 const char *struct_too_deep_ellipsis () const override
13808 { return "(...)"; }
39e7ecca 13809
67bd3fd5
AB
13810 /* See language.h. */
13811
13812 bool c_style_arrays_p () const override
13813 { return false; }
13814
d3355e4d
AB
13815 /* See language.h. */
13816
13817 bool store_sym_names_in_linkage_form_p () const override
13818 { return true; }
13819
b63a3f3f
AB
13820 /* See language.h. */
13821
13822 const struct lang_varobj_ops *varobj_ops () const override
13823 { return &ada_varobj_ops; }
13824
c9debfb9
AB
13825protected:
13826 /* See language.h. */
13827
13828 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13829 (const lookup_name_info &lookup_name) const override
13830 {
13831 return ada_get_symbol_name_matcher (lookup_name);
13832 }
0874fd07
AB
13833};
13834
13835/* Single instance of the Ada language class. */
13836
13837static ada_language ada_language_defn;
13838
5bf03f13
JB
13839/* Command-list for the "set/show ada" prefix command. */
13840static struct cmd_list_element *set_ada_list;
13841static struct cmd_list_element *show_ada_list;
13842
2060206e
PA
13843static void
13844initialize_ada_catchpoint_ops (void)
13845{
13846 struct breakpoint_ops *ops;
13847
13848 initialize_breakpoint_ops ();
13849
13850 ops = &catch_exception_breakpoint_ops;
13851 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
13852 ops->allocate_location = allocate_location_exception;
13853 ops->re_set = re_set_exception;
13854 ops->check_status = check_status_exception;
13855 ops->print_it = print_it_exception;
13856 ops->print_one = print_one_exception;
13857 ops->print_mention = print_mention_exception;
13858 ops->print_recreate = print_recreate_exception;
2060206e
PA
13859}
13860
3d9434b5
JB
13861/* This module's 'new_objfile' observer. */
13862
13863static void
13864ada_new_objfile_observer (struct objfile *objfile)
13865{
13866 ada_clear_symbol_cache ();
13867}
13868
13869/* This module's 'free_objfile' observer. */
13870
13871static void
13872ada_free_objfile_observer (struct objfile *objfile)
13873{
13874 ada_clear_symbol_cache ();
13875}
13876
315e4ebb
TT
13877/* Charsets known to GNAT. */
13878static const char * const gnat_source_charsets[] =
13879{
13880 /* Note that code below assumes that the default comes first.
13881 Latin-1 is the default here, because that is also GNAT's
13882 default. */
13883 "ISO-8859-1",
13884 "ISO-8859-2",
13885 "ISO-8859-3",
13886 "ISO-8859-4",
13887 "ISO-8859-5",
13888 "ISO-8859-15",
13889 "CP437",
13890 "CP850",
13891 /* Note that this value is special-cased in the encoder and
13892 decoder. */
13893 ada_utf8,
13894 nullptr
13895};
13896
6c265988 13897void _initialize_ada_language ();
d2e4a39e 13898void
6c265988 13899_initialize_ada_language ()
14f9c5c9 13900{
2060206e
PA
13901 initialize_ada_catchpoint_ops ();
13902
f54bdb6d
SM
13903 add_setshow_prefix_cmd
13904 ("ada", no_class,
13905 _("Prefix command for changing Ada-specific settings."),
13906 _("Generic command for showing Ada-specific settings."),
13907 &set_ada_list, &show_ada_list,
13908 &setlist, &showlist);
5bf03f13
JB
13909
13910 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13911 &trust_pad_over_xvs, _("\
590042fc
PW
13912Enable or disable an optimization trusting PAD types over XVS types."), _("\
13913Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13914 _("\
5bf03f13
JB
13915This is related to the encoding used by the GNAT compiler. The debugger\n\
13916should normally trust the contents of PAD types, but certain older versions\n\
13917of GNAT have a bug that sometimes causes the information in the PAD type\n\
13918to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13919work around this bug. It is always safe to turn this option \"off\", but\n\
13920this incurs a slight performance penalty, so it is recommended to NOT change\n\
13921this option to \"off\" unless necessary."),
dda83cd7 13922 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13923
d72413e6
PMR
13924 add_setshow_boolean_cmd ("print-signatures", class_vars,
13925 &print_signatures, _("\
13926Enable or disable the output of formal and return types for functions in the \
590042fc 13927overloads selection menu."), _("\
d72413e6 13928Show whether the output of formal and return types for functions in the \
590042fc 13929overloads selection menu is activated."),
d72413e6
PMR
13930 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13931
315e4ebb
TT
13932 ada_source_charset = gnat_source_charsets[0];
13933 add_setshow_enum_cmd ("source-charset", class_files,
13934 gnat_source_charsets,
13935 &ada_source_charset, _("\
13936Set the Ada source character set."), _("\
13937Show the Ada source character set."), _("\
13938The character set used for Ada source files.\n\
13939This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13940 nullptr, nullptr,
13941 &set_ada_list, &show_ada_list);
13942
9ac4176b
PA
13943 add_catch_command ("exception", _("\
13944Catch Ada exceptions, when raised.\n\
9bf7038b 13945Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
13946Without any argument, stop when any Ada exception is raised.\n\
13947If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13948being raised does not have a handler (and will therefore lead to the task's\n\
13949termination).\n\
13950Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
13951raised is the same as ARG.\n\
13952CONDITION is a boolean expression that is evaluated to see whether the\n\
13953exception should cause a stop."),
9ac4176b 13954 catch_ada_exception_command,
71bed2db 13955 catch_ada_completer,
9ac4176b
PA
13956 CATCH_PERMANENT,
13957 CATCH_TEMPORARY);
9f757bf7
XR
13958
13959 add_catch_command ("handlers", _("\
13960Catch Ada exceptions, when handled.\n\
9bf7038b
TT
13961Usage: catch handlers [ARG] [if CONDITION]\n\
13962Without any argument, stop when any Ada exception is handled.\n\
13963With an argument, catch only exceptions with the given name.\n\
13964CONDITION is a boolean expression that is evaluated to see whether the\n\
13965exception should cause a stop."),
9f757bf7 13966 catch_ada_handlers_command,
dda83cd7 13967 catch_ada_completer,
9f757bf7
XR
13968 CATCH_PERMANENT,
13969 CATCH_TEMPORARY);
9ac4176b
PA
13970 add_catch_command ("assert", _("\
13971Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
13972Usage: catch assert [if CONDITION]\n\
13973CONDITION is a boolean expression that is evaluated to see whether the\n\
13974exception should cause a stop."),
9ac4176b 13975 catch_assert_command,
dda83cd7 13976 NULL,
9ac4176b
PA
13977 CATCH_PERMANENT,
13978 CATCH_TEMPORARY);
13979
778865d3
JB
13980 add_info ("exceptions", info_exceptions_command,
13981 _("\
13982List all Ada exception names.\n\
9bf7038b 13983Usage: info exceptions [REGEXP]\n\
778865d3
JB
13984If a regular expression is passed as an argument, only those matching\n\
13985the regular expression are listed."));
13986
f54bdb6d
SM
13987 add_setshow_prefix_cmd ("ada", class_maintenance,
13988 _("Set Ada maintenance-related variables."),
13989 _("Show Ada maintenance-related variables."),
13990 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
13991 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
13992
13993 add_setshow_boolean_cmd
13994 ("ignore-descriptive-types", class_maintenance,
13995 &ada_ignore_descriptive_types_p,
13996 _("Set whether descriptive types generated by GNAT should be ignored."),
13997 _("Show whether descriptive types generated by GNAT should be ignored."),
13998 _("\
13999When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14000DWARF attribute."),
14001 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14002
2698f5ea
TT
14003 decoded_names_store = htab_create_alloc (256, htab_hash_string,
14004 htab_eq_string,
459a2e4c 14005 NULL, xcalloc, xfree);
6b69afc4 14006
3d9434b5 14007 /* The ada-lang observers. */
c90e7d63
SM
14008 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
14009 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
14010 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
14f9c5c9 14011}