]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
gdb: remove TYPE_FIELD_LOC_KIND
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
6e681866 1/* Ada language support routines for GDB, the GNU debugger.
10a2c479 2
3666a048 3 Copyright (C) 1992-2021 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>
d55e5aa6 23#include "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"
4de283e4
TT
38#include "gdb_obstack.h"
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"
ccefe4c4 62
4c4b4cd2 63/* Define whether or not the C operator '/' truncates towards zero for
0963b4bd 64 differently signed operands (truncation direction is undefined in C).
4c4b4cd2
PH
65 Copied from valarith.c. */
66
67#ifndef TRUNCATION_TOWARDS_ZERO
68#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
69#endif
70
d2e4a39e 71static struct type *desc_base_type (struct type *);
14f9c5c9 72
d2e4a39e 73static struct type *desc_bounds_type (struct type *);
14f9c5c9 74
d2e4a39e 75static struct value *desc_bounds (struct value *);
14f9c5c9 76
d2e4a39e 77static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 78
d2e4a39e 79static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 80
556bdfd4 81static struct type *desc_data_target_type (struct type *);
14f9c5c9 82
d2e4a39e 83static struct value *desc_data (struct value *);
14f9c5c9 84
d2e4a39e 85static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 86
d2e4a39e 87static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 88
d2e4a39e 89static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 90
d2e4a39e 91static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 92
d2e4a39e 93static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 94
d2e4a39e 95static struct type *desc_index_type (struct type *, int);
14f9c5c9 96
d2e4a39e 97static int desc_arity (struct type *);
14f9c5c9 98
d2e4a39e 99static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 100
40bc484c 101static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 102
d1183b06 103static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
104 const struct block *,
105 const lookup_name_info &lookup_name,
106 domain_enum, struct objfile *);
14f9c5c9 107
d1183b06
TT
108static void ada_add_all_symbols (std::vector<struct block_symbol> &,
109 const struct block *,
b5ec771e
PA
110 const lookup_name_info &lookup_name,
111 domain_enum, int, int *);
22cee43f 112
d1183b06 113static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 114
d1183b06
TT
115static void add_defn_to_vec (std::vector<struct block_symbol> &,
116 struct symbol *,
dda83cd7 117 const struct block *);
14f9c5c9 118
d2e4a39e 119static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 120
4c4b4cd2 121static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 122
d2e4a39e 123static int numeric_type_p (struct type *);
14f9c5c9 124
d2e4a39e 125static int integer_type_p (struct type *);
14f9c5c9 126
d2e4a39e 127static int scalar_type_p (struct type *);
14f9c5c9 128
d2e4a39e 129static int discrete_type_p (struct type *);
14f9c5c9 130
a121b7c1 131static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 132 int, int);
4c4b4cd2 133
b4ba55a1 134static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 135 const char *);
b4ba55a1 136
d2e4a39e 137static int is_dynamic_field (struct type *, int);
14f9c5c9 138
10a2c479 139static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 140 const gdb_byte *,
dda83cd7 141 CORE_ADDR, struct value *);
4c4b4cd2
PH
142
143static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 144
28c85d6c 145static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 146
d2e4a39e 147static struct type *to_static_fixed_type (struct type *);
f192137b 148static struct type *static_unwrap_type (struct type *type);
14f9c5c9 149
d2e4a39e 150static struct value *unwrap_value (struct value *);
14f9c5c9 151
ad82864c 152static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 153
ad82864c 154static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 155
ad82864c
JB
156static long decode_packed_array_bitsize (struct type *);
157
158static struct value *decode_constrained_packed_array (struct value *);
159
ad82864c 160static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 161
d2e4a39e 162static struct value *value_subscript_packed (struct value *, int,
dda83cd7 163 struct value **);
14f9c5c9 164
4c4b4cd2 165static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 166 struct type *);
14f9c5c9 167
d2e4a39e 168static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 169
d2e4a39e 170static int equiv_types (struct type *, struct type *);
14f9c5c9 171
d2e4a39e 172static int is_name_suffix (const char *);
14f9c5c9 173
59c8a30b 174static int advance_wild_match (const char **, const char *, char);
73589123 175
b5ec771e 176static bool wild_match (const char *name, const char *patn);
14f9c5c9 177
d2e4a39e 178static struct value *ada_coerce_ref (struct value *);
14f9c5c9 179
4c4b4cd2
PH
180static LONGEST pos_atr (struct value *);
181
53a47a3e
TT
182static struct value *val_atr (struct type *, LONGEST);
183
4c4b4cd2 184static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 185 domain_enum);
14f9c5c9 186
108d56a4 187static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 188 struct type *);
4c4b4cd2 189
0d5cff50 190static int find_struct_field (const char *, struct type *, int,
dda83cd7 191 struct type **, int *, int *, int *, int *);
4c4b4cd2 192
d1183b06 193static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7 194 struct value **, int, const char *,
7056f312 195 struct type *, bool);
4c4b4cd2 196
4c4b4cd2
PH
197static int ada_is_direct_array_type (struct type *);
198
52ce6436
PH
199static struct value *ada_index_struct_field (int, struct value *, int,
200 struct type *);
201
cf608cc4 202static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
203
204
852dff6c 205static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
206
207static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
208 (const lookup_name_info &lookup_name);
209
4c4b4cd2
PH
210\f
211
ee01b665
JB
212/* The result of a symbol lookup to be stored in our symbol cache. */
213
214struct cache_entry
215{
216 /* The name used to perform the lookup. */
217 const char *name;
218 /* The namespace used during the lookup. */
fe978cb0 219 domain_enum domain;
ee01b665
JB
220 /* The symbol returned by the lookup, or NULL if no matching symbol
221 was found. */
222 struct symbol *sym;
223 /* The block where the symbol was found, or NULL if no matching
224 symbol was found. */
225 const struct block *block;
226 /* A pointer to the next entry with the same hash. */
227 struct cache_entry *next;
228};
229
230/* The Ada symbol cache, used to store the result of Ada-mode symbol
231 lookups in the course of executing the user's commands.
232
233 The cache is implemented using a simple, fixed-sized hash.
234 The size is fixed on the grounds that there are not likely to be
235 all that many symbols looked up during any given session, regardless
236 of the size of the symbol table. If we decide to go to a resizable
237 table, let's just use the stuff from libiberty instead. */
238
239#define HASH_SIZE 1009
240
241struct ada_symbol_cache
242{
243 /* An obstack used to store the entries in our cache. */
bdcccc56 244 struct auto_obstack cache_space;
ee01b665
JB
245
246 /* The root of the hash table used to implement our symbol cache. */
bdcccc56 247 struct cache_entry *root[HASH_SIZE] {};
ee01b665
JB
248};
249
67cb5b2d 250static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
251#ifdef VMS
252 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
253#else
14f9c5c9 254 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 255#endif
14f9c5c9 256
4c4b4cd2 257/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 258static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 259 = "__gnat_ada_main_program_name";
14f9c5c9 260
4c4b4cd2
PH
261/* Limit on the number of warnings to raise per expression evaluation. */
262static int warning_limit = 2;
263
264/* Number of warning messages issued; reset to 0 by cleanups after
265 expression evaluation. */
266static int warnings_issued = 0;
267
27087b7f 268static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
269 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
270};
271
27087b7f 272static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
273 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
274};
275
c6044dd1
JB
276/* Maintenance-related settings for this module. */
277
278static struct cmd_list_element *maint_set_ada_cmdlist;
279static struct cmd_list_element *maint_show_ada_cmdlist;
280
c6044dd1
JB
281/* The "maintenance ada set/show ignore-descriptive-type" value. */
282
491144b5 283static bool ada_ignore_descriptive_types_p = false;
c6044dd1 284
e802dbe0
JB
285 /* Inferior-specific data. */
286
287/* Per-inferior data for this module. */
288
289struct ada_inferior_data
290{
291 /* The ada__tags__type_specific_data type, which is used when decoding
292 tagged types. With older versions of GNAT, this type was directly
293 accessible through a component ("tsd") in the object tag. But this
294 is no longer the case, so we cache it for each inferior. */
f37b313d 295 struct type *tsd_type = nullptr;
3eecfa55
JB
296
297 /* The exception_support_info data. This data is used to determine
298 how to implement support for Ada exception catchpoints in a given
299 inferior. */
f37b313d 300 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
301};
302
303/* Our key to this module's inferior data. */
f37b313d 304static const struct inferior_key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
305
306/* Return our inferior data for the given inferior (INF).
307
308 This function always returns a valid pointer to an allocated
309 ada_inferior_data structure. If INF's inferior data has not
310 been previously set, this functions creates a new one with all
311 fields set to zero, sets INF's inferior to it, and then returns
312 a pointer to that newly allocated ada_inferior_data. */
313
314static struct ada_inferior_data *
315get_ada_inferior_data (struct inferior *inf)
316{
317 struct ada_inferior_data *data;
318
f37b313d 319 data = ada_inferior_data.get (inf);
e802dbe0 320 if (data == NULL)
f37b313d 321 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
322
323 return data;
324}
325
326/* Perform all necessary cleanups regarding our module's inferior data
327 that is required after the inferior INF just exited. */
328
329static void
330ada_inferior_exit (struct inferior *inf)
331{
f37b313d 332 ada_inferior_data.clear (inf);
e802dbe0
JB
333}
334
ee01b665
JB
335
336 /* program-space-specific data. */
337
338/* This module's per-program-space data. */
339struct ada_pspace_data
340{
341 /* The Ada symbol cache. */
bdcccc56 342 std::unique_ptr<ada_symbol_cache> sym_cache;
ee01b665
JB
343};
344
345/* Key to our per-program-space data. */
f37b313d 346static const struct program_space_key<ada_pspace_data> ada_pspace_data_handle;
ee01b665
JB
347
348/* Return this module's data for the given program space (PSPACE).
349 If not is found, add a zero'ed one now.
350
351 This function always returns a valid object. */
352
353static struct ada_pspace_data *
354get_ada_pspace_data (struct program_space *pspace)
355{
356 struct ada_pspace_data *data;
357
f37b313d 358 data = ada_pspace_data_handle.get (pspace);
ee01b665 359 if (data == NULL)
f37b313d 360 data = ada_pspace_data_handle.emplace (pspace);
ee01b665
JB
361
362 return data;
363}
364
dda83cd7 365 /* Utilities */
4c4b4cd2 366
720d1a40 367/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 368 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
369
370 Normally, we really expect a typedef type to only have 1 typedef layer.
371 In other words, we really expect the target type of a typedef type to be
372 a non-typedef type. This is particularly true for Ada units, because
373 the language does not have a typedef vs not-typedef distinction.
374 In that respect, the Ada compiler has been trying to eliminate as many
375 typedef definitions in the debugging information, since they generally
376 do not bring any extra information (we still use typedef under certain
377 circumstances related mostly to the GNAT encoding).
378
379 Unfortunately, we have seen situations where the debugging information
380 generated by the compiler leads to such multiple typedef layers. For
381 instance, consider the following example with stabs:
382
383 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
384 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
385
386 This is an error in the debugging information which causes type
387 pck__float_array___XUP to be defined twice, and the second time,
388 it is defined as a typedef of a typedef.
389
390 This is on the fringe of legality as far as debugging information is
391 concerned, and certainly unexpected. But it is easy to handle these
392 situations correctly, so we can afford to be lenient in this case. */
393
394static struct type *
395ada_typedef_target_type (struct type *type)
396{
78134374 397 while (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
398 type = TYPE_TARGET_TYPE (type);
399 return type;
400}
401
41d27058
JB
402/* Given DECODED_NAME a string holding a symbol name in its
403 decoded form (ie using the Ada dotted notation), returns
404 its unqualified name. */
405
406static const char *
407ada_unqualified_name (const char *decoded_name)
408{
2b0f535a
JB
409 const char *result;
410
411 /* If the decoded name starts with '<', it means that the encoded
412 name does not follow standard naming conventions, and thus that
413 it is not your typical Ada symbol name. Trying to unqualify it
414 is therefore pointless and possibly erroneous. */
415 if (decoded_name[0] == '<')
416 return decoded_name;
417
418 result = strrchr (decoded_name, '.');
41d27058
JB
419 if (result != NULL)
420 result++; /* Skip the dot... */
421 else
422 result = decoded_name;
423
424 return result;
425}
426
39e7af3e 427/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 428
39e7af3e 429static std::string
41d27058
JB
430add_angle_brackets (const char *str)
431{
39e7af3e 432 return string_printf ("<%s>", str);
41d27058 433}
96d887e8 434
14f9c5c9 435/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 436 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
437
438static int
ebf56fd3 439field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
440{
441 int len = strlen (target);
5b4ee69b 442
d2e4a39e 443 return
4c4b4cd2
PH
444 (strncmp (field_name, target, len) == 0
445 && (field_name[len] == '\0'
dda83cd7
SM
446 || (startswith (field_name + len, "___")
447 && strcmp (field_name + strlen (field_name) - 6,
448 "___XVN") != 0)));
14f9c5c9
AS
449}
450
451
872c8b51
JB
452/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
453 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
454 and return its index. This function also handles fields whose name
455 have ___ suffixes because the compiler sometimes alters their name
456 by adding such a suffix to represent fields with certain constraints.
457 If the field could not be found, return a negative number if
458 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
459
460int
461ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 462 int maybe_missing)
4c4b4cd2
PH
463{
464 int fieldno;
872c8b51
JB
465 struct type *struct_type = check_typedef ((struct type *) type);
466
1f704f76 467 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
33d16dd9 468 if (field_name_match (struct_type->field (fieldno).name (), field_name))
4c4b4cd2
PH
469 return fieldno;
470
471 if (!maybe_missing)
323e0a4a 472 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 473 field_name, struct_type->name ());
4c4b4cd2
PH
474
475 return -1;
476}
477
478/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
479
480int
d2e4a39e 481ada_name_prefix_len (const char *name)
14f9c5c9
AS
482{
483 if (name == NULL)
484 return 0;
d2e4a39e 485 else
14f9c5c9 486 {
d2e4a39e 487 const char *p = strstr (name, "___");
5b4ee69b 488
14f9c5c9 489 if (p == NULL)
dda83cd7 490 return strlen (name);
14f9c5c9 491 else
dda83cd7 492 return p - name;
14f9c5c9
AS
493 }
494}
495
4c4b4cd2
PH
496/* Return non-zero if SUFFIX is a suffix of STR.
497 Return zero if STR is null. */
498
14f9c5c9 499static int
d2e4a39e 500is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
501{
502 int len1, len2;
5b4ee69b 503
14f9c5c9
AS
504 if (str == NULL)
505 return 0;
506 len1 = strlen (str);
507 len2 = strlen (suffix);
4c4b4cd2 508 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
509}
510
4c4b4cd2
PH
511/* The contents of value VAL, treated as a value of type TYPE. The
512 result is an lval in memory if VAL is. */
14f9c5c9 513
d2e4a39e 514static struct value *
4c4b4cd2 515coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 516{
61ee279c 517 type = ada_check_typedef (type);
df407dfe 518 if (value_type (val) == type)
4c4b4cd2 519 return val;
d2e4a39e 520 else
14f9c5c9 521 {
4c4b4cd2
PH
522 struct value *result;
523
f73e424f
TT
524 if (value_optimized_out (val))
525 result = allocate_optimized_out_value (type);
526 else if (value_lazy (val)
527 /* Be careful not to make a lazy not_lval value. */
528 || (VALUE_LVAL (val) != not_lval
529 && TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val))))
41e8491f
JK
530 result = allocate_value_lazy (type);
531 else
532 {
533 result = allocate_value (type);
f73e424f 534 value_contents_copy (result, 0, val, 0, TYPE_LENGTH (type));
41e8491f 535 }
74bcbdf3 536 set_value_component_location (result, val);
9bbda503
AC
537 set_value_bitsize (result, value_bitsize (val));
538 set_value_bitpos (result, value_bitpos (val));
c408a94f
TT
539 if (VALUE_LVAL (result) == lval_memory)
540 set_value_address (result, value_address (val));
14f9c5c9
AS
541 return result;
542 }
543}
544
fc1a4b47
AC
545static const gdb_byte *
546cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
547{
548 if (valaddr == NULL)
549 return NULL;
550 else
551 return valaddr + offset;
552}
553
554static CORE_ADDR
ebf56fd3 555cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
556{
557 if (address == 0)
558 return 0;
d2e4a39e 559 else
14f9c5c9
AS
560 return address + offset;
561}
562
4c4b4cd2
PH
563/* Issue a warning (as for the definition of warning in utils.c, but
564 with exactly one argument rather than ...), unless the limit on the
565 number of warnings has passed during the evaluation of the current
566 expression. */
a2249542 567
77109804
AC
568/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
569 provided by "complaint". */
a0b31db1 570static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 571
14f9c5c9 572static void
a2249542 573lim_warning (const char *format, ...)
14f9c5c9 574{
a2249542 575 va_list args;
a2249542 576
5b4ee69b 577 va_start (args, format);
4c4b4cd2
PH
578 warnings_issued += 1;
579 if (warnings_issued <= warning_limit)
a2249542
MK
580 vwarning (format, args);
581
582 va_end (args);
4c4b4cd2
PH
583}
584
0963b4bd 585/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 586static LONGEST
c3e5cd34 587max_of_size (int size)
4c4b4cd2 588{
76a01679 589 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 590
76a01679 591 return top_bit | (top_bit - 1);
4c4b4cd2
PH
592}
593
0963b4bd 594/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 595static LONGEST
c3e5cd34 596min_of_size (int size)
4c4b4cd2 597{
c3e5cd34 598 return -max_of_size (size) - 1;
4c4b4cd2
PH
599}
600
0963b4bd 601/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 602static ULONGEST
c3e5cd34 603umax_of_size (int size)
4c4b4cd2 604{
76a01679 605 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 606
76a01679 607 return top_bit | (top_bit - 1);
4c4b4cd2
PH
608}
609
0963b4bd 610/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
611static LONGEST
612max_of_type (struct type *t)
4c4b4cd2 613{
c6d940a9 614 if (t->is_unsigned ())
c3e5cd34
PH
615 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
616 else
617 return max_of_size (TYPE_LENGTH (t));
618}
619
0963b4bd 620/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
621static LONGEST
622min_of_type (struct type *t)
623{
c6d940a9 624 if (t->is_unsigned ())
c3e5cd34
PH
625 return 0;
626 else
627 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
628}
629
630/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
631LONGEST
632ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 633{
b249d2c2 634 type = resolve_dynamic_type (type, {}, 0);
78134374 635 switch (type->code ())
4c4b4cd2
PH
636 {
637 case TYPE_CODE_RANGE:
d1fd641e
SM
638 {
639 const dynamic_prop &high = type->bounds ()->high;
640
641 if (high.kind () == PROP_CONST)
642 return high.const_val ();
643 else
644 {
645 gdb_assert (high.kind () == PROP_UNDEFINED);
646
647 /* This happens when trying to evaluate a type's dynamic bound
648 without a live target. There is nothing relevant for us to
649 return here, so return 0. */
650 return 0;
651 }
652 }
4c4b4cd2 653 case TYPE_CODE_ENUM:
1f704f76 654 return TYPE_FIELD_ENUMVAL (type, type->num_fields () - 1);
690cc4eb
PH
655 case TYPE_CODE_BOOL:
656 return 1;
657 case TYPE_CODE_CHAR:
76a01679 658 case TYPE_CODE_INT:
690cc4eb 659 return max_of_type (type);
4c4b4cd2 660 default:
43bbcdc2 661 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
662 }
663}
664
14e75d8e 665/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
666LONGEST
667ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 668{
b249d2c2 669 type = resolve_dynamic_type (type, {}, 0);
78134374 670 switch (type->code ())
4c4b4cd2
PH
671 {
672 case TYPE_CODE_RANGE:
d1fd641e
SM
673 {
674 const dynamic_prop &low = type->bounds ()->low;
675
676 if (low.kind () == PROP_CONST)
677 return low.const_val ();
678 else
679 {
680 gdb_assert (low.kind () == PROP_UNDEFINED);
681
682 /* This happens when trying to evaluate a type's dynamic bound
683 without a live target. There is nothing relevant for us to
684 return here, so return 0. */
685 return 0;
686 }
687 }
4c4b4cd2 688 case TYPE_CODE_ENUM:
14e75d8e 689 return TYPE_FIELD_ENUMVAL (type, 0);
690cc4eb
PH
690 case TYPE_CODE_BOOL:
691 return 0;
692 case TYPE_CODE_CHAR:
76a01679 693 case TYPE_CODE_INT:
690cc4eb 694 return min_of_type (type);
4c4b4cd2 695 default:
43bbcdc2 696 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
697 }
698}
699
700/* The identity on non-range types. For range types, the underlying
76a01679 701 non-range scalar type. */
4c4b4cd2
PH
702
703static struct type *
18af8284 704get_base_type (struct type *type)
4c4b4cd2 705{
78134374 706 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 707 {
76a01679 708 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
dda83cd7 709 return type;
4c4b4cd2
PH
710 type = TYPE_TARGET_TYPE (type);
711 }
712 return type;
14f9c5c9 713}
41246937
JB
714
715/* Return a decoded version of the given VALUE. This means returning
716 a value whose type is obtained by applying all the GNAT-specific
85102364 717 encodings, making the resulting type a static but standard description
41246937
JB
718 of the initial type. */
719
720struct value *
721ada_get_decoded_value (struct value *value)
722{
723 struct type *type = ada_check_typedef (value_type (value));
724
725 if (ada_is_array_descriptor_type (type)
726 || (ada_is_constrained_packed_array_type (type)
dda83cd7 727 && type->code () != TYPE_CODE_PTR))
41246937 728 {
78134374 729 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 730 value = ada_coerce_to_simple_array_ptr (value);
41246937 731 else
dda83cd7 732 value = ada_coerce_to_simple_array (value);
41246937
JB
733 }
734 else
735 value = ada_to_fixed_value (value);
736
737 return value;
738}
739
740/* Same as ada_get_decoded_value, but with the given TYPE.
741 Because there is no associated actual value for this type,
742 the resulting type might be a best-effort approximation in
743 the case of dynamic types. */
744
745struct type *
746ada_get_decoded_type (struct type *type)
747{
748 type = to_static_fixed_type (type);
749 if (ada_is_constrained_packed_array_type (type))
750 type = ada_coerce_to_simple_array_type (type);
751 return type;
752}
753
4c4b4cd2 754\f
76a01679 755
dda83cd7 756 /* Language Selection */
14f9c5c9
AS
757
758/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 759 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 760
de93309a 761static enum language
ccefe4c4 762ada_update_initial_language (enum language lang)
14f9c5c9 763{
cafb3438 764 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 765 return language_ada;
14f9c5c9
AS
766
767 return lang;
768}
96d887e8
PH
769
770/* If the main procedure is written in Ada, then return its name.
771 The result is good until the next call. Return NULL if the main
772 procedure doesn't appear to be in Ada. */
773
774char *
775ada_main_name (void)
776{
3b7344d5 777 struct bound_minimal_symbol msym;
e83e4e24 778 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 779
96d887e8
PH
780 /* For Ada, the name of the main procedure is stored in a specific
781 string constant, generated by the binder. Look for that symbol,
782 extract its address, and then read that string. If we didn't find
783 that string, then most probably the main procedure is not written
784 in Ada. */
785 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
786
3b7344d5 787 if (msym.minsym != NULL)
96d887e8 788 {
66920317 789 CORE_ADDR main_program_name_addr = BMSYMBOL_VALUE_ADDRESS (msym);
96d887e8 790 if (main_program_name_addr == 0)
dda83cd7 791 error (_("Invalid address for Ada main program name."));
96d887e8 792
66920317 793 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 794 return main_program_name.get ();
96d887e8
PH
795 }
796
797 /* The main procedure doesn't seem to be in Ada. */
798 return NULL;
799}
14f9c5c9 800\f
dda83cd7 801 /* Symbols */
d2e4a39e 802
4c4b4cd2
PH
803/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
804 of NULLs. */
14f9c5c9 805
d2e4a39e
AS
806const struct ada_opname_map ada_opname_table[] = {
807 {"Oadd", "\"+\"", BINOP_ADD},
808 {"Osubtract", "\"-\"", BINOP_SUB},
809 {"Omultiply", "\"*\"", BINOP_MUL},
810 {"Odivide", "\"/\"", BINOP_DIV},
811 {"Omod", "\"mod\"", BINOP_MOD},
812 {"Orem", "\"rem\"", BINOP_REM},
813 {"Oexpon", "\"**\"", BINOP_EXP},
814 {"Olt", "\"<\"", BINOP_LESS},
815 {"Ole", "\"<=\"", BINOP_LEQ},
816 {"Ogt", "\">\"", BINOP_GTR},
817 {"Oge", "\">=\"", BINOP_GEQ},
818 {"Oeq", "\"=\"", BINOP_EQUAL},
819 {"One", "\"/=\"", BINOP_NOTEQUAL},
820 {"Oand", "\"and\"", BINOP_BITWISE_AND},
821 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
822 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
823 {"Oconcat", "\"&\"", BINOP_CONCAT},
824 {"Oabs", "\"abs\"", UNOP_ABS},
825 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
826 {"Oadd", "\"+\"", UNOP_PLUS},
827 {"Osubtract", "\"-\"", UNOP_NEG},
828 {NULL, NULL}
14f9c5c9
AS
829};
830
965bc1df
TT
831/* If STR is a decoded version of a compiler-provided suffix (like the
832 "[cold]" in "symbol[cold]"), return true. Otherwise, return
833 false. */
834
835static bool
836is_compiler_suffix (const char *str)
837{
838 gdb_assert (*str == '[');
839 ++str;
840 while (*str != '\0' && isalpha (*str))
841 ++str;
842 /* We accept a missing "]" in order to support completion. */
843 return *str == '\0' || (str[0] == ']' && str[1] == '\0');
844}
845
5c4258f4 846/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 847 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 848 Otherwise, return the empty string in that case. */
4c4b4cd2 849
5c4258f4 850static std::string
b5ec771e 851ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 852{
4c4b4cd2 853 if (decoded == NULL)
5c4258f4 854 return {};
14f9c5c9 855
5c4258f4
TT
856 std::string encoding_buffer;
857 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 858 {
cdc7bb92 859 if (*p == '.')
5c4258f4 860 encoding_buffer.append ("__");
965bc1df
TT
861 else if (*p == '[' && is_compiler_suffix (p))
862 {
863 encoding_buffer = encoding_buffer + "." + (p + 1);
864 if (encoding_buffer.back () == ']')
865 encoding_buffer.pop_back ();
866 break;
867 }
14f9c5c9 868 else if (*p == '"')
dda83cd7
SM
869 {
870 const struct ada_opname_map *mapping;
871
872 for (mapping = ada_opname_table;
873 mapping->encoded != NULL
874 && !startswith (p, mapping->decoded); mapping += 1)
875 ;
876 if (mapping->encoded == NULL)
b5ec771e
PA
877 {
878 if (throw_errors)
879 error (_("invalid Ada operator name: %s"), p);
880 else
5c4258f4 881 return {};
b5ec771e 882 }
5c4258f4 883 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
884 break;
885 }
d2e4a39e 886 else
5c4258f4 887 encoding_buffer.push_back (*p);
14f9c5c9
AS
888 }
889
4c4b4cd2 890 return encoding_buffer;
14f9c5c9
AS
891}
892
5c4258f4 893/* The "encoded" form of DECODED, according to GNAT conventions. */
b5ec771e 894
5c4258f4 895std::string
b5ec771e
PA
896ada_encode (const char *decoded)
897{
898 return ada_encode_1 (decoded, true);
899}
900
14f9c5c9 901/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
902 quotes, unfolded, but with the quotes stripped away. Result good
903 to next call. */
904
5f9febe0 905static const char *
e0802d59 906ada_fold_name (gdb::string_view name)
14f9c5c9 907{
5f9febe0 908 static std::string fold_storage;
14f9c5c9 909
6a780b67 910 if (!name.empty () && name[0] == '\'')
01573d73 911 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
912 else
913 {
01573d73 914 fold_storage = gdb::to_string (name);
5f9febe0
TT
915 for (int i = 0; i < name.size (); i += 1)
916 fold_storage[i] = tolower (fold_storage[i]);
14f9c5c9
AS
917 }
918
5f9febe0 919 return fold_storage.c_str ();
14f9c5c9
AS
920}
921
529cad9c
PH
922/* Return nonzero if C is either a digit or a lowercase alphabet character. */
923
924static int
925is_lower_alphanum (const char c)
926{
927 return (isdigit (c) || (isalpha (c) && islower (c)));
928}
929
c90092fe
JB
930/* ENCODED is the linkage name of a symbol and LEN contains its length.
931 This function saves in LEN the length of that same symbol name but
932 without either of these suffixes:
29480c32
JB
933 . .{DIGIT}+
934 . ${DIGIT}+
935 . ___{DIGIT}+
936 . __{DIGIT}+.
c90092fe 937
29480c32
JB
938 These are suffixes introduced by the compiler for entities such as
939 nested subprogram for instance, in order to avoid name clashes.
940 They do not serve any purpose for the debugger. */
941
942static void
943ada_remove_trailing_digits (const char *encoded, int *len)
944{
945 if (*len > 1 && isdigit (encoded[*len - 1]))
946 {
947 int i = *len - 2;
5b4ee69b 948
29480c32 949 while (i > 0 && isdigit (encoded[i]))
dda83cd7 950 i--;
29480c32 951 if (i >= 0 && encoded[i] == '.')
dda83cd7 952 *len = i;
29480c32 953 else if (i >= 0 && encoded[i] == '$')
dda83cd7 954 *len = i;
61012eef 955 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 956 *len = i - 2;
61012eef 957 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 958 *len = i - 1;
29480c32
JB
959 }
960}
961
962/* Remove the suffix introduced by the compiler for protected object
963 subprograms. */
964
965static void
966ada_remove_po_subprogram_suffix (const char *encoded, int *len)
967{
968 /* Remove trailing N. */
969
970 /* Protected entry subprograms are broken into two
971 separate subprograms: The first one is unprotected, and has
972 a 'N' suffix; the second is the protected version, and has
0963b4bd 973 the 'P' suffix. The second calls the first one after handling
29480c32
JB
974 the protection. Since the P subprograms are internally generated,
975 we leave these names undecoded, giving the user a clue that this
976 entity is internal. */
977
978 if (*len > 1
979 && encoded[*len - 1] == 'N'
980 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
981 *len = *len - 1;
982}
983
965bc1df
TT
984/* If ENCODED ends with a compiler-provided suffix (like ".cold"),
985 then update *LEN to remove the suffix and return the offset of the
986 character just past the ".". Otherwise, return -1. */
987
988static int
989remove_compiler_suffix (const char *encoded, int *len)
990{
991 int offset = *len - 1;
992 while (offset > 0 && isalpha (encoded[offset]))
993 --offset;
994 if (offset > 0 && encoded[offset] == '.')
995 {
996 *len = offset;
997 return offset + 1;
998 }
999 return -1;
1000}
1001
8a3df5ac 1002/* See ada-lang.h. */
14f9c5c9 1003
f945dedf 1004std::string
8a3df5ac 1005ada_decode (const char *encoded, bool wrap)
14f9c5c9
AS
1006{
1007 int i, j;
1008 int len0;
d2e4a39e 1009 const char *p;
14f9c5c9 1010 int at_start_name;
f945dedf 1011 std::string decoded;
965bc1df 1012 int suffix = -1;
d2e4a39e 1013
0d81f350
JG
1014 /* With function descriptors on PPC64, the value of a symbol named
1015 ".FN", if it exists, is the entry point of the function "FN". */
1016 if (encoded[0] == '.')
1017 encoded += 1;
1018
29480c32
JB
1019 /* The name of the Ada main procedure starts with "_ada_".
1020 This prefix is not part of the decoded name, so skip this part
1021 if we see this prefix. */
61012eef 1022 if (startswith (encoded, "_ada_"))
4c4b4cd2 1023 encoded += 5;
14f9c5c9 1024
29480c32
JB
1025 /* If the name starts with '_', then it is not a properly encoded
1026 name, so do not attempt to decode it. Similarly, if the name
1027 starts with '<', the name should not be decoded. */
4c4b4cd2 1028 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1029 goto Suppress;
1030
4c4b4cd2 1031 len0 = strlen (encoded);
4c4b4cd2 1032
965bc1df
TT
1033 suffix = remove_compiler_suffix (encoded, &len0);
1034
29480c32
JB
1035 ada_remove_trailing_digits (encoded, &len0);
1036 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1037
4c4b4cd2
PH
1038 /* Remove the ___X.* suffix if present. Do not forget to verify that
1039 the suffix is located before the current "end" of ENCODED. We want
1040 to avoid re-matching parts of ENCODED that have previously been
1041 marked as discarded (by decrementing LEN0). */
1042 p = strstr (encoded, "___");
1043 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1044 {
1045 if (p[3] == 'X')
dda83cd7 1046 len0 = p - encoded;
14f9c5c9 1047 else
dda83cd7 1048 goto Suppress;
14f9c5c9 1049 }
4c4b4cd2 1050
29480c32
JB
1051 /* Remove any trailing TKB suffix. It tells us that this symbol
1052 is for the body of a task, but that information does not actually
1053 appear in the decoded name. */
1054
61012eef 1055 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1056 len0 -= 3;
76a01679 1057
a10967fa
JB
1058 /* Remove any trailing TB suffix. The TB suffix is slightly different
1059 from the TKB suffix because it is used for non-anonymous task
1060 bodies. */
1061
61012eef 1062 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1063 len0 -= 2;
1064
29480c32
JB
1065 /* Remove trailing "B" suffixes. */
1066 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1067
61012eef 1068 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1069 len0 -= 1;
1070
4c4b4cd2 1071 /* Make decoded big enough for possible expansion by operator name. */
29480c32 1072
f945dedf 1073 decoded.resize (2 * len0 + 1, 'X');
14f9c5c9 1074
29480c32
JB
1075 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1076
4c4b4cd2 1077 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1078 {
4c4b4cd2
PH
1079 i = len0 - 2;
1080 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1081 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1082 i -= 1;
4c4b4cd2 1083 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1084 len0 = i - 1;
4c4b4cd2 1085 else if (encoded[i] == '$')
dda83cd7 1086 len0 = i;
d2e4a39e 1087 }
14f9c5c9 1088
29480c32
JB
1089 /* The first few characters that are not alphabetic are not part
1090 of any encoding we use, so we can copy them over verbatim. */
1091
4c4b4cd2
PH
1092 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1093 decoded[j] = encoded[i];
14f9c5c9
AS
1094
1095 at_start_name = 1;
1096 while (i < len0)
1097 {
29480c32 1098 /* Is this a symbol function? */
4c4b4cd2 1099 if (at_start_name && encoded[i] == 'O')
dda83cd7
SM
1100 {
1101 int k;
1102
1103 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1104 {
1105 int op_len = strlen (ada_opname_table[k].encoded);
1106 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1107 op_len - 1) == 0)
1108 && !isalnum (encoded[i + op_len]))
1109 {
1110 strcpy (&decoded.front() + j, ada_opname_table[k].decoded);
1111 at_start_name = 0;
1112 i += op_len;
1113 j += strlen (ada_opname_table[k].decoded);
1114 break;
1115 }
1116 }
1117 if (ada_opname_table[k].encoded != NULL)
1118 continue;
1119 }
14f9c5c9
AS
1120 at_start_name = 0;
1121
529cad9c 1122 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1123 into "." (just below). */
529cad9c 1124
61012eef 1125 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1126 i += 2;
529cad9c 1127
29480c32 1128 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1129 be translated into "." (just below). These are internal names
1130 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1131
1132 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1133 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1134 && isdigit (encoded [i+4]))
1135 {
1136 int k = i + 5;
1137
1138 while (k < len0 && isdigit (encoded[k]))
1139 k++; /* Skip any extra digit. */
1140
1141 /* Double-check that the "__B_{DIGITS}+" sequence we found
1142 is indeed followed by "__". */
1143 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1144 i = k;
1145 }
29480c32 1146
529cad9c
PH
1147 /* Remove _E{DIGITS}+[sb] */
1148
1149 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1150 of subprograms created by the compiler for each entry. The first
1151 one implements the actual entry code, and has a suffix following
1152 the convention above; the second one implements the barrier and
1153 uses the same convention as above, except that the 'E' is replaced
1154 by a 'B'.
529cad9c 1155
dda83cd7
SM
1156 Just as above, we do not decode the name of barrier functions
1157 to give the user a clue that the code he is debugging has been
1158 internally generated. */
529cad9c
PH
1159
1160 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1161 && isdigit (encoded[i+2]))
1162 {
1163 int k = i + 3;
1164
1165 while (k < len0 && isdigit (encoded[k]))
1166 k++;
1167
1168 if (k < len0
1169 && (encoded[k] == 'b' || encoded[k] == 's'))
1170 {
1171 k++;
1172 /* Just as an extra precaution, make sure that if this
1173 suffix is followed by anything else, it is a '_'.
1174 Otherwise, we matched this sequence by accident. */
1175 if (k == len0
1176 || (k < len0 && encoded[k] == '_'))
1177 i = k;
1178 }
1179 }
529cad9c
PH
1180
1181 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1182 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1183
1184 if (i < len0 + 3
dda83cd7
SM
1185 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1186 {
1187 /* Backtrack a bit up until we reach either the begining of
1188 the encoded name, or "__". Make sure that we only find
1189 digits or lowercase characters. */
1190 const char *ptr = encoded + i - 1;
1191
1192 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1193 ptr--;
1194 if (ptr < encoded
1195 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1196 i++;
1197 }
529cad9c 1198
4c4b4cd2 1199 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1200 {
1201 /* This is a X[bn]* sequence not separated from the previous
1202 part of the name with a non-alpha-numeric character (in other
1203 words, immediately following an alpha-numeric character), then
1204 verify that it is placed at the end of the encoded name. If
1205 not, then the encoding is not valid and we should abort the
1206 decoding. Otherwise, just skip it, it is used in body-nested
1207 package names. */
1208 do
1209 i += 1;
1210 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1211 if (i < len0)
1212 goto Suppress;
1213 }
cdc7bb92 1214 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1215 {
1216 /* Replace '__' by '.'. */
1217 decoded[j] = '.';
1218 at_start_name = 1;
1219 i += 2;
1220 j += 1;
1221 }
14f9c5c9 1222 else
dda83cd7
SM
1223 {
1224 /* It's a character part of the decoded name, so just copy it
1225 over. */
1226 decoded[j] = encoded[i];
1227 i += 1;
1228 j += 1;
1229 }
14f9c5c9 1230 }
f945dedf 1231 decoded.resize (j);
14f9c5c9 1232
29480c32
JB
1233 /* Decoded names should never contain any uppercase character.
1234 Double-check this, and abort the decoding if we find one. */
1235
f945dedf 1236 for (i = 0; i < decoded.length(); ++i)
4c4b4cd2 1237 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1238 goto Suppress;
1239
965bc1df
TT
1240 /* If the compiler added a suffix, append it now. */
1241 if (suffix >= 0)
1242 decoded = decoded + "[" + &encoded[suffix] + "]";
1243
f945dedf 1244 return decoded;
14f9c5c9
AS
1245
1246Suppress:
8a3df5ac
TT
1247 if (!wrap)
1248 return {};
1249
4c4b4cd2 1250 if (encoded[0] == '<')
f945dedf 1251 decoded = encoded;
14f9c5c9 1252 else
f945dedf 1253 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2 1254 return decoded;
4c4b4cd2
PH
1255}
1256
1257/* Table for keeping permanent unique copies of decoded names. Once
1258 allocated, names in this table are never released. While this is a
1259 storage leak, it should not be significant unless there are massive
1260 changes in the set of decoded names in successive versions of a
1261 symbol table loaded during a single session. */
1262static struct htab *decoded_names_store;
1263
1264/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1265 in the language-specific part of GSYMBOL, if it has not been
1266 previously computed. Tries to save the decoded name in the same
1267 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1268 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1269 GSYMBOL).
4c4b4cd2
PH
1270 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1271 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1272 when a decoded name is cached in it. */
4c4b4cd2 1273
45e6c716 1274const char *
f85f34ed 1275ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1276{
f85f34ed
TT
1277 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1278 const char **resultp =
615b3f62 1279 &gsymbol->language_specific.demangled_name;
5b4ee69b 1280
f85f34ed 1281 if (!gsymbol->ada_mangled)
4c4b4cd2 1282 {
4d4eaa30 1283 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1284 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1285
f85f34ed 1286 gsymbol->ada_mangled = 1;
5b4ee69b 1287
f85f34ed 1288 if (obstack != NULL)
f945dedf 1289 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1290 else
dda83cd7 1291 {
f85f34ed
TT
1292 /* Sometimes, we can't find a corresponding objfile, in
1293 which case, we put the result on the heap. Since we only
1294 decode when needed, we hope this usually does not cause a
1295 significant memory leak (FIXME). */
1296
dda83cd7
SM
1297 char **slot = (char **) htab_find_slot (decoded_names_store,
1298 decoded.c_str (), INSERT);
5b4ee69b 1299
dda83cd7
SM
1300 if (*slot == NULL)
1301 *slot = xstrdup (decoded.c_str ());
1302 *resultp = *slot;
1303 }
4c4b4cd2 1304 }
14f9c5c9 1305
4c4b4cd2
PH
1306 return *resultp;
1307}
76a01679 1308
14f9c5c9 1309\f
d2e4a39e 1310
dda83cd7 1311 /* Arrays */
14f9c5c9 1312
28c85d6c
JB
1313/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1314 generated by the GNAT compiler to describe the index type used
1315 for each dimension of an array, check whether it follows the latest
1316 known encoding. If not, fix it up to conform to the latest encoding.
1317 Otherwise, do nothing. This function also does nothing if
1318 INDEX_DESC_TYPE is NULL.
1319
85102364 1320 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1321 Initially, the information would be provided through the name of each
1322 field of the structure type only, while the type of these fields was
1323 described as unspecified and irrelevant. The debugger was then expected
1324 to perform a global type lookup using the name of that field in order
1325 to get access to the full index type description. Because these global
1326 lookups can be very expensive, the encoding was later enhanced to make
1327 the global lookup unnecessary by defining the field type as being
1328 the full index type description.
1329
1330 The purpose of this routine is to allow us to support older versions
1331 of the compiler by detecting the use of the older encoding, and by
1332 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1333 we essentially replace each field's meaningless type by the associated
1334 index subtype). */
1335
1336void
1337ada_fixup_array_indexes_type (struct type *index_desc_type)
1338{
1339 int i;
1340
1341 if (index_desc_type == NULL)
1342 return;
1f704f76 1343 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1344
1345 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1346 to check one field only, no need to check them all). If not, return
1347 now.
1348
1349 If our INDEX_DESC_TYPE was generated using the older encoding,
1350 the field type should be a meaningless integer type whose name
1351 is not equal to the field name. */
940da03e
SM
1352 if (index_desc_type->field (0).type ()->name () != NULL
1353 && strcmp (index_desc_type->field (0).type ()->name (),
33d16dd9 1354 index_desc_type->field (0).name ()) == 0)
28c85d6c
JB
1355 return;
1356
1357 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1358 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1359 {
33d16dd9 1360 const char *name = index_desc_type->field (i).name ();
28c85d6c
JB
1361 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1362
1363 if (raw_type)
5d14b6e5 1364 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1365 }
1366}
1367
4c4b4cd2
PH
1368/* The desc_* routines return primitive portions of array descriptors
1369 (fat pointers). */
14f9c5c9
AS
1370
1371/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1372 level of indirection, if needed. */
1373
d2e4a39e
AS
1374static struct type *
1375desc_base_type (struct type *type)
14f9c5c9
AS
1376{
1377 if (type == NULL)
1378 return NULL;
61ee279c 1379 type = ada_check_typedef (type);
78134374 1380 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1381 type = ada_typedef_target_type (type);
1382
1265e4aa 1383 if (type != NULL
78134374 1384 && (type->code () == TYPE_CODE_PTR
dda83cd7 1385 || type->code () == TYPE_CODE_REF))
61ee279c 1386 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1387 else
1388 return type;
1389}
1390
4c4b4cd2
PH
1391/* True iff TYPE indicates a "thin" array pointer type. */
1392
14f9c5c9 1393static int
d2e4a39e 1394is_thin_pntr (struct type *type)
14f9c5c9 1395{
d2e4a39e 1396 return
14f9c5c9
AS
1397 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1398 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1399}
1400
4c4b4cd2
PH
1401/* The descriptor type for thin pointer type TYPE. */
1402
d2e4a39e
AS
1403static struct type *
1404thin_descriptor_type (struct type *type)
14f9c5c9 1405{
d2e4a39e 1406 struct type *base_type = desc_base_type (type);
5b4ee69b 1407
14f9c5c9
AS
1408 if (base_type == NULL)
1409 return NULL;
1410 if (is_suffix (ada_type_name (base_type), "___XVE"))
1411 return base_type;
d2e4a39e 1412 else
14f9c5c9 1413 {
d2e4a39e 1414 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1415
14f9c5c9 1416 if (alt_type == NULL)
dda83cd7 1417 return base_type;
14f9c5c9 1418 else
dda83cd7 1419 return alt_type;
14f9c5c9
AS
1420 }
1421}
1422
4c4b4cd2
PH
1423/* A pointer to the array data for thin-pointer value VAL. */
1424
d2e4a39e
AS
1425static struct value *
1426thin_data_pntr (struct value *val)
14f9c5c9 1427{
828292f2 1428 struct type *type = ada_check_typedef (value_type (val));
556bdfd4 1429 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1430
556bdfd4
UW
1431 data_type = lookup_pointer_type (data_type);
1432
78134374 1433 if (type->code () == TYPE_CODE_PTR)
556bdfd4 1434 return value_cast (data_type, value_copy (val));
d2e4a39e 1435 else
42ae5230 1436 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1437}
1438
4c4b4cd2
PH
1439/* True iff TYPE indicates a "thick" array pointer type. */
1440
14f9c5c9 1441static int
d2e4a39e 1442is_thick_pntr (struct type *type)
14f9c5c9
AS
1443{
1444 type = desc_base_type (type);
78134374 1445 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1446 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1447}
1448
4c4b4cd2
PH
1449/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1450 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1451
d2e4a39e
AS
1452static struct type *
1453desc_bounds_type (struct type *type)
14f9c5c9 1454{
d2e4a39e 1455 struct type *r;
14f9c5c9
AS
1456
1457 type = desc_base_type (type);
1458
1459 if (type == NULL)
1460 return NULL;
1461 else if (is_thin_pntr (type))
1462 {
1463 type = thin_descriptor_type (type);
1464 if (type == NULL)
dda83cd7 1465 return NULL;
14f9c5c9
AS
1466 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1467 if (r != NULL)
dda83cd7 1468 return ada_check_typedef (r);
14f9c5c9 1469 }
78134374 1470 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1471 {
1472 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1473 if (r != NULL)
dda83cd7 1474 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1475 }
1476 return NULL;
1477}
1478
1479/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1480 one, a pointer to its bounds data. Otherwise NULL. */
1481
d2e4a39e
AS
1482static struct value *
1483desc_bounds (struct value *arr)
14f9c5c9 1484{
df407dfe 1485 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1486
d2e4a39e 1487 if (is_thin_pntr (type))
14f9c5c9 1488 {
d2e4a39e 1489 struct type *bounds_type =
dda83cd7 1490 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1491 LONGEST addr;
1492
4cdfadb1 1493 if (bounds_type == NULL)
dda83cd7 1494 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1495
1496 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1497 since desc_type is an XVE-encoded type (and shouldn't be),
1498 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1499 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1500 addr = value_as_long (arr);
d2e4a39e 1501 else
dda83cd7 1502 addr = value_address (arr);
14f9c5c9 1503
d2e4a39e 1504 return
dda83cd7
SM
1505 value_from_longest (lookup_pointer_type (bounds_type),
1506 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1507 }
1508
1509 else if (is_thick_pntr (type))
05e522ef 1510 {
158cc4fe 1511 struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
05e522ef
JB
1512 _("Bad GNAT array descriptor"));
1513 struct type *p_bounds_type = value_type (p_bounds);
1514
1515 if (p_bounds_type
78134374 1516 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef
JB
1517 {
1518 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1519
e46d3488 1520 if (target_type->is_stub ())
05e522ef
JB
1521 p_bounds = value_cast (lookup_pointer_type
1522 (ada_check_typedef (target_type)),
1523 p_bounds);
1524 }
1525 else
1526 error (_("Bad GNAT array descriptor"));
1527
1528 return p_bounds;
1529 }
14f9c5c9
AS
1530 else
1531 return NULL;
1532}
1533
4c4b4cd2
PH
1534/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1535 position of the field containing the address of the bounds data. */
1536
14f9c5c9 1537static int
d2e4a39e 1538fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1539{
1540 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1541}
1542
1543/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1544 size of the field containing the address of the bounds data. */
1545
14f9c5c9 1546static int
d2e4a39e 1547fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1548{
1549 type = desc_base_type (type);
1550
d2e4a39e 1551 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1552 return TYPE_FIELD_BITSIZE (type, 1);
1553 else
940da03e 1554 return 8 * TYPE_LENGTH (ada_check_typedef (type->field (1).type ()));
14f9c5c9
AS
1555}
1556
4c4b4cd2 1557/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1558 pointer to one, the type of its array data (a array-with-no-bounds type);
1559 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1560 data. */
4c4b4cd2 1561
d2e4a39e 1562static struct type *
556bdfd4 1563desc_data_target_type (struct type *type)
14f9c5c9
AS
1564{
1565 type = desc_base_type (type);
1566
4c4b4cd2 1567 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1568 if (is_thin_pntr (type))
940da03e 1569 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1570 else if (is_thick_pntr (type))
556bdfd4
UW
1571 {
1572 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1573
1574 if (data_type
78134374 1575 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
05e522ef 1576 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1577 }
1578
1579 return NULL;
14f9c5c9
AS
1580}
1581
1582/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1583 its array data. */
4c4b4cd2 1584
d2e4a39e
AS
1585static struct value *
1586desc_data (struct value *arr)
14f9c5c9 1587{
df407dfe 1588 struct type *type = value_type (arr);
5b4ee69b 1589
14f9c5c9
AS
1590 if (is_thin_pntr (type))
1591 return thin_data_pntr (arr);
1592 else if (is_thick_pntr (type))
158cc4fe 1593 return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
dda83cd7 1594 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1595 else
1596 return NULL;
1597}
1598
1599
1600/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1601 position of the field containing the address of the data. */
1602
14f9c5c9 1603static int
d2e4a39e 1604fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1605{
1606 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1607}
1608
1609/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1610 size of the field containing the address of the data. */
1611
14f9c5c9 1612static int
d2e4a39e 1613fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1614{
1615 type = desc_base_type (type);
1616
1617 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1618 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1619 else
940da03e 1620 return TARGET_CHAR_BIT * TYPE_LENGTH (type->field (0).type ());
14f9c5c9
AS
1621}
1622
4c4b4cd2 1623/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1624 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1625 bound, if WHICH is 1. The first bound is I=1. */
1626
d2e4a39e
AS
1627static struct value *
1628desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1629{
250106a7
TT
1630 char bound_name[20];
1631 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1632 which ? 'U' : 'L', i - 1);
158cc4fe 1633 return value_struct_elt (&bounds, {}, bound_name, NULL,
dda83cd7 1634 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1635}
1636
1637/* If BOUNDS is an array-bounds structure type, return the bit position
1638 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1639 bound, if WHICH is 1. The first bound is I=1. */
1640
14f9c5c9 1641static int
d2e4a39e 1642desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1643{
d2e4a39e 1644 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1645}
1646
1647/* If BOUNDS is an array-bounds structure type, return the bit field size
1648 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1649 bound, if WHICH is 1. The first bound is I=1. */
1650
76a01679 1651static int
d2e4a39e 1652desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1653{
1654 type = desc_base_type (type);
1655
d2e4a39e
AS
1656 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1657 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1658 else
940da03e 1659 return 8 * TYPE_LENGTH (type->field (2 * i + which - 2).type ());
14f9c5c9
AS
1660}
1661
1662/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1663 Ith bound (numbering from 1). Otherwise, NULL. */
1664
d2e4a39e
AS
1665static struct type *
1666desc_index_type (struct type *type, int i)
14f9c5c9
AS
1667{
1668 type = desc_base_type (type);
1669
78134374 1670 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
1671 {
1672 char bound_name[20];
1673 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
1674 return lookup_struct_elt_type (type, bound_name, 1);
1675 }
d2e4a39e 1676 else
14f9c5c9
AS
1677 return NULL;
1678}
1679
4c4b4cd2
PH
1680/* The number of index positions in the array-bounds type TYPE.
1681 Return 0 if TYPE is NULL. */
1682
14f9c5c9 1683static int
d2e4a39e 1684desc_arity (struct type *type)
14f9c5c9
AS
1685{
1686 type = desc_base_type (type);
1687
1688 if (type != NULL)
1f704f76 1689 return type->num_fields () / 2;
14f9c5c9
AS
1690 return 0;
1691}
1692
4c4b4cd2
PH
1693/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1694 an array descriptor type (representing an unconstrained array
1695 type). */
1696
76a01679
JB
1697static int
1698ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1699{
1700 if (type == NULL)
1701 return 0;
61ee279c 1702 type = ada_check_typedef (type);
78134374 1703 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 1704 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1705}
1706
52ce6436 1707/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 1708 * to one. */
52ce6436 1709
2c0b251b 1710static int
52ce6436
PH
1711ada_is_array_type (struct type *type)
1712{
78134374
SM
1713 while (type != NULL
1714 && (type->code () == TYPE_CODE_PTR
1715 || type->code () == TYPE_CODE_REF))
52ce6436
PH
1716 type = TYPE_TARGET_TYPE (type);
1717 return ada_is_direct_array_type (type);
1718}
1719
4c4b4cd2 1720/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1721
14f9c5c9 1722int
4c4b4cd2 1723ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1724{
1725 if (type == NULL)
1726 return 0;
61ee279c 1727 type = ada_check_typedef (type);
78134374
SM
1728 return (type->code () == TYPE_CODE_ARRAY
1729 || (type->code () == TYPE_CODE_PTR
1730 && (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ()
1731 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
1732}
1733
4c4b4cd2
PH
1734/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1735
14f9c5c9 1736int
4c4b4cd2 1737ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1738{
556bdfd4 1739 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
1740
1741 if (type == NULL)
1742 return 0;
61ee279c 1743 type = ada_check_typedef (type);
556bdfd4 1744 return (data_type != NULL
78134374 1745 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 1746 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
1747}
1748
1749/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1750 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1751 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1752 is still needed. */
1753
14f9c5c9 1754int
ebf56fd3 1755ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1756{
d2e4a39e 1757 return
14f9c5c9 1758 type != NULL
78134374 1759 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 1760 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 1761 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 1762 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1763}
1764
1765
4c4b4cd2 1766/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1767 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1768 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1769 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1770 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1771 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1772 a descriptor. */
de93309a
SM
1773
1774static struct type *
d2e4a39e 1775ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 1776{
ad82864c
JB
1777 if (ada_is_constrained_packed_array_type (value_type (arr)))
1778 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 1779
df407dfe
AC
1780 if (!ada_is_array_descriptor_type (value_type (arr)))
1781 return value_type (arr);
d2e4a39e
AS
1782
1783 if (!bounds)
ad82864c
JB
1784 {
1785 struct type *array_type =
1786 ada_check_typedef (desc_data_target_type (value_type (arr)));
1787
1788 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1789 TYPE_FIELD_BITSIZE (array_type, 0) =
1790 decode_packed_array_bitsize (value_type (arr));
1791
1792 return array_type;
1793 }
14f9c5c9
AS
1794 else
1795 {
d2e4a39e 1796 struct type *elt_type;
14f9c5c9 1797 int arity;
d2e4a39e 1798 struct value *descriptor;
14f9c5c9 1799
df407dfe
AC
1800 elt_type = ada_array_element_type (value_type (arr), -1);
1801 arity = ada_array_arity (value_type (arr));
14f9c5c9 1802
d2e4a39e 1803 if (elt_type == NULL || arity == 0)
dda83cd7 1804 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
1805
1806 descriptor = desc_bounds (arr);
d2e4a39e 1807 if (value_as_long (descriptor) == 0)
dda83cd7 1808 return NULL;
d2e4a39e 1809 while (arity > 0)
dda83cd7
SM
1810 {
1811 struct type *range_type = alloc_type_copy (value_type (arr));
1812 struct type *array_type = alloc_type_copy (value_type (arr));
1813 struct value *low = desc_one_bound (descriptor, arity, 0);
1814 struct value *high = desc_one_bound (descriptor, arity, 1);
1815
1816 arity -= 1;
1817 create_static_range_type (range_type, value_type (low),
0c9c3474
SA
1818 longest_to_int (value_as_long (low)),
1819 longest_to_int (value_as_long (high)));
dda83cd7 1820 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
1821
1822 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
e67ad678
JB
1823 {
1824 /* We need to store the element packed bitsize, as well as
dda83cd7 1825 recompute the array size, because it was previously
e67ad678
JB
1826 computed based on the unpacked element size. */
1827 LONGEST lo = value_as_long (low);
1828 LONGEST hi = value_as_long (high);
1829
1830 TYPE_FIELD_BITSIZE (elt_type, 0) =
1831 decode_packed_array_bitsize (value_type (arr));
1832 /* If the array has no element, then the size is already
dda83cd7 1833 zero, and does not need to be recomputed. */
e67ad678
JB
1834 if (lo < hi)
1835 {
1836 int array_bitsize =
dda83cd7 1837 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678
JB
1838
1839 TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
1840 }
1841 }
dda83cd7 1842 }
14f9c5c9
AS
1843
1844 return lookup_pointer_type (elt_type);
1845 }
1846}
1847
1848/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
1849 Otherwise, returns either a standard GDB array with bounds set
1850 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1851 GDB array. Returns NULL if ARR is a null fat pointer. */
1852
d2e4a39e
AS
1853struct value *
1854ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 1855{
df407dfe 1856 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1857 {
d2e4a39e 1858 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 1859
14f9c5c9 1860 if (arrType == NULL)
dda83cd7 1861 return NULL;
14f9c5c9
AS
1862 return value_cast (arrType, value_copy (desc_data (arr)));
1863 }
ad82864c
JB
1864 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1865 return decode_constrained_packed_array (arr);
14f9c5c9
AS
1866 else
1867 return arr;
1868}
1869
1870/* If ARR does not represent an array, returns ARR unchanged.
1871 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
1872 be ARR itself if it already is in the proper form). */
1873
720d1a40 1874struct value *
d2e4a39e 1875ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 1876{
df407dfe 1877 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1878 {
d2e4a39e 1879 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 1880
14f9c5c9 1881 if (arrVal == NULL)
dda83cd7 1882 error (_("Bounds unavailable for null array pointer."));
14f9c5c9
AS
1883 return value_ind (arrVal);
1884 }
ad82864c
JB
1885 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1886 return decode_constrained_packed_array (arr);
d2e4a39e 1887 else
14f9c5c9
AS
1888 return arr;
1889}
1890
1891/* If TYPE represents a GNAT array type, return it translated to an
1892 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
1893 packing). For other types, is the identity. */
1894
d2e4a39e
AS
1895struct type *
1896ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1897{
ad82864c
JB
1898 if (ada_is_constrained_packed_array_type (type))
1899 return decode_constrained_packed_array_type (type);
17280b9f
UW
1900
1901 if (ada_is_array_descriptor_type (type))
556bdfd4 1902 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
1903
1904 return type;
14f9c5c9
AS
1905}
1906
4c4b4cd2
PH
1907/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1908
ad82864c 1909static int
57567375 1910ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
1911{
1912 if (type == NULL)
1913 return 0;
4c4b4cd2 1914 type = desc_base_type (type);
61ee279c 1915 type = ada_check_typedef (type);
d2e4a39e 1916 return
14f9c5c9
AS
1917 ada_type_name (type) != NULL
1918 && strstr (ada_type_name (type), "___XP") != NULL;
1919}
1920
ad82864c
JB
1921/* Non-zero iff TYPE represents a standard GNAT constrained
1922 packed-array type. */
1923
1924int
1925ada_is_constrained_packed_array_type (struct type *type)
1926{
57567375 1927 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
1928 && !ada_is_array_descriptor_type (type);
1929}
1930
1931/* Non-zero iff TYPE represents an array descriptor for a
1932 unconstrained packed-array type. */
1933
1934static int
1935ada_is_unconstrained_packed_array_type (struct type *type)
1936{
57567375
TT
1937 if (!ada_is_array_descriptor_type (type))
1938 return 0;
1939
1940 if (ada_is_gnat_encoded_packed_array_type (type))
1941 return 1;
1942
1943 /* If we saw GNAT encodings, then the above code is sufficient.
1944 However, with minimal encodings, we will just have a thick
1945 pointer instead. */
1946 if (is_thick_pntr (type))
1947 {
1948 type = desc_base_type (type);
1949 /* The structure's first field is a pointer to an array, so this
1950 fetches the array type. */
1951 type = TYPE_TARGET_TYPE (type->field (0).type ());
1952 /* Now we can see if the array elements are packed. */
1953 return TYPE_FIELD_BITSIZE (type, 0) > 0;
1954 }
1955
1956 return 0;
ad82864c
JB
1957}
1958
c9a28cbe
TT
1959/* Return true if TYPE is a (Gnat-encoded) constrained packed array
1960 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
1961
1962static bool
1963ada_is_any_packed_array_type (struct type *type)
1964{
1965 return (ada_is_constrained_packed_array_type (type)
1966 || (type->code () == TYPE_CODE_ARRAY
1967 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
1968}
1969
ad82864c
JB
1970/* Given that TYPE encodes a packed array type (constrained or unconstrained),
1971 return the size of its elements in bits. */
1972
1973static long
1974decode_packed_array_bitsize (struct type *type)
1975{
0d5cff50
DE
1976 const char *raw_name;
1977 const char *tail;
ad82864c
JB
1978 long bits;
1979
720d1a40
JB
1980 /* Access to arrays implemented as fat pointers are encoded as a typedef
1981 of the fat pointer type. We need the name of the fat pointer type
1982 to do the decoding, so strip the typedef layer. */
78134374 1983 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1984 type = ada_typedef_target_type (type);
1985
1986 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
1987 if (!raw_name)
1988 raw_name = ada_type_name (desc_base_type (type));
1989
1990 if (!raw_name)
1991 return 0;
1992
1993 tail = strstr (raw_name, "___XP");
57567375
TT
1994 if (tail == nullptr)
1995 {
1996 gdb_assert (is_thick_pntr (type));
1997 /* The structure's first field is a pointer to an array, so this
1998 fetches the array type. */
1999 type = TYPE_TARGET_TYPE (type->field (0).type ());
2000 /* Now we can see if the array elements are packed. */
2001 return TYPE_FIELD_BITSIZE (type, 0);
2002 }
ad82864c
JB
2003
2004 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2005 {
2006 lim_warning
2007 (_("could not understand bit size information on packed array"));
2008 return 0;
2009 }
2010
2011 return bits;
2012}
2013
14f9c5c9
AS
2014/* Given that TYPE is a standard GDB array type with all bounds filled
2015 in, and that the element size of its ultimate scalar constituents
2016 (that is, either its elements, or, if it is an array of arrays, its
2017 elements' elements, etc.) is *ELT_BITS, return an identical type,
2018 but with the bit sizes of its elements (and those of any
2019 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2020 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2021 in bits.
2022
2023 Note that, for arrays whose index type has an XA encoding where
2024 a bound references a record discriminant, getting that discriminant,
2025 and therefore the actual value of that bound, is not possible
2026 because none of the given parameters gives us access to the record.
2027 This function assumes that it is OK in the context where it is being
2028 used to return an array whose bounds are still dynamic and where
2029 the length is arbitrary. */
4c4b4cd2 2030
d2e4a39e 2031static struct type *
ad82864c 2032constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2033{
d2e4a39e
AS
2034 struct type *new_elt_type;
2035 struct type *new_type;
99b1c762
JB
2036 struct type *index_type_desc;
2037 struct type *index_type;
14f9c5c9
AS
2038 LONGEST low_bound, high_bound;
2039
61ee279c 2040 type = ada_check_typedef (type);
78134374 2041 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2042 return type;
2043
99b1c762
JB
2044 index_type_desc = ada_find_parallel_type (type, "___XA");
2045 if (index_type_desc)
940da03e 2046 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2047 NULL);
2048 else
3d967001 2049 index_type = type->index_type ();
99b1c762 2050
e9bb382b 2051 new_type = alloc_type_copy (type);
ad82864c
JB
2052 new_elt_type =
2053 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
2054 elt_bits);
99b1c762 2055 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9 2056 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2057 new_type->set_name (ada_type_name (type));
14f9c5c9 2058
78134374 2059 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2060 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2061 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2062 low_bound = high_bound = 0;
2063 if (high_bound < low_bound)
2064 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 2065 else
14f9c5c9
AS
2066 {
2067 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 2068 TYPE_LENGTH (new_type) =
dda83cd7 2069 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
2070 }
2071
9cdd0d12 2072 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2073 return new_type;
2074}
2075
ad82864c
JB
2076/* The array type encoded by TYPE, where
2077 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2078
d2e4a39e 2079static struct type *
ad82864c 2080decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2081{
0d5cff50 2082 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2083 char *name;
0d5cff50 2084 const char *tail;
d2e4a39e 2085 struct type *shadow_type;
14f9c5c9 2086 long bits;
14f9c5c9 2087
727e3d2e
JB
2088 if (!raw_name)
2089 raw_name = ada_type_name (desc_base_type (type));
2090
2091 if (!raw_name)
2092 return NULL;
2093
2094 name = (char *) alloca (strlen (raw_name) + 1);
2095 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2096 type = desc_base_type (type);
2097
14f9c5c9
AS
2098 memcpy (name, raw_name, tail - raw_name);
2099 name[tail - raw_name] = '\000';
2100
b4ba55a1
JB
2101 shadow_type = ada_find_parallel_type_with_name (type, name);
2102
2103 if (shadow_type == NULL)
14f9c5c9 2104 {
323e0a4a 2105 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2106 return NULL;
2107 }
f168693b 2108 shadow_type = check_typedef (shadow_type);
14f9c5c9 2109
78134374 2110 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2111 {
0963b4bd
MS
2112 lim_warning (_("could not understand bounds "
2113 "information on packed array"));
14f9c5c9
AS
2114 return NULL;
2115 }
d2e4a39e 2116
ad82864c
JB
2117 bits = decode_packed_array_bitsize (type);
2118 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2119}
2120
a7400e44
TT
2121/* Helper function for decode_constrained_packed_array. Set the field
2122 bitsize on a series of packed arrays. Returns the number of
2123 elements in TYPE. */
2124
2125static LONGEST
2126recursively_update_array_bitsize (struct type *type)
2127{
2128 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2129
2130 LONGEST low, high;
1f8d2881 2131 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2132 || low > high)
2133 return 0;
2134 LONGEST our_len = high - low + 1;
2135
2136 struct type *elt_type = TYPE_TARGET_TYPE (type);
2137 if (elt_type->code () == TYPE_CODE_ARRAY)
2138 {
2139 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2140 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2141 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2142
2143 TYPE_LENGTH (type) = ((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2144 / HOST_CHAR_BIT);
2145 }
2146
2147 return our_len;
2148}
2149
ad82864c
JB
2150/* Given that ARR is a struct value *indicating a GNAT constrained packed
2151 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2152 standard GDB array type except that the BITSIZEs of the array
2153 target types are set to the number of bits in each element, and the
4c4b4cd2 2154 type length is set appropriately. */
14f9c5c9 2155
d2e4a39e 2156static struct value *
ad82864c 2157decode_constrained_packed_array (struct value *arr)
14f9c5c9 2158{
4c4b4cd2 2159 struct type *type;
14f9c5c9 2160
11aa919a
PMR
2161 /* If our value is a pointer, then dereference it. Likewise if
2162 the value is a reference. Make sure that this operation does not
2163 cause the target type to be fixed, as this would indirectly cause
2164 this array to be decoded. The rest of the routine assumes that
2165 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2166 and "value_ind" routines to perform the dereferencing, as opposed
2167 to using "ada_coerce_ref" or "ada_value_ind". */
2168 arr = coerce_ref (arr);
78134374 2169 if (ada_check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
284614f0 2170 arr = value_ind (arr);
4c4b4cd2 2171
ad82864c 2172 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2173 if (type == NULL)
2174 {
323e0a4a 2175 error (_("can't unpack array"));
14f9c5c9
AS
2176 return NULL;
2177 }
61ee279c 2178
a7400e44
TT
2179 /* Decoding the packed array type could not correctly set the field
2180 bitsizes for any dimension except the innermost, because the
2181 bounds may be variable and were not passed to that function. So,
2182 we further resolve the array bounds here and then update the
2183 sizes. */
50888e42 2184 const gdb_byte *valaddr = value_contents_for_printing (arr).data ();
a7400e44
TT
2185 CORE_ADDR address = value_address (arr);
2186 gdb::array_view<const gdb_byte> view
2187 = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
2188 type = resolve_dynamic_type (type, view, address);
2189 recursively_update_array_bitsize (type);
2190
d5a22e77 2191 if (type_byte_order (value_type (arr)) == BFD_ENDIAN_BIG
32c9a795 2192 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2193 {
2194 /* This is a (right-justified) modular type representing a packed
24b21115
SM
2195 array with no wrapper. In order to interpret the value through
2196 the (left-justified) packed array type we just built, we must
2197 first left-justify it. */
61ee279c
PH
2198 int bit_size, bit_pos;
2199 ULONGEST mod;
2200
df407dfe 2201 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2202 bit_size = 0;
2203 while (mod > 0)
2204 {
2205 bit_size += 1;
2206 mod >>= 1;
2207 }
df407dfe 2208 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2209 arr = ada_value_primitive_packed_val (arr, NULL,
2210 bit_pos / HOST_CHAR_BIT,
2211 bit_pos % HOST_CHAR_BIT,
2212 bit_size,
2213 type);
2214 }
2215
4c4b4cd2 2216 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2217}
2218
2219
2220/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2221 given in IND. ARR must be a simple array. */
14f9c5c9 2222
d2e4a39e
AS
2223static struct value *
2224value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2225{
2226 int i;
2227 int bits, elt_off, bit_off;
2228 long elt_total_bit_offset;
d2e4a39e
AS
2229 struct type *elt_type;
2230 struct value *v;
14f9c5c9
AS
2231
2232 bits = 0;
2233 elt_total_bit_offset = 0;
df407dfe 2234 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2235 for (i = 0; i < arity; i += 1)
14f9c5c9 2236 {
78134374 2237 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2238 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2239 error
2240 (_("attempt to do packed indexing of "
0963b4bd 2241 "something other than a packed array"));
14f9c5c9 2242 else
dda83cd7
SM
2243 {
2244 struct type *range_type = elt_type->index_type ();
2245 LONGEST lowerbound, upperbound;
2246 LONGEST idx;
2247
1f8d2881 2248 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2249 {
2250 lim_warning (_("don't know bounds of array"));
2251 lowerbound = upperbound = 0;
2252 }
2253
2254 idx = pos_atr (ind[i]);
2255 if (idx < lowerbound || idx > upperbound)
2256 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2257 (long) idx);
dda83cd7
SM
2258 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2259 elt_total_bit_offset += (idx - lowerbound) * bits;
2260 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2261 }
14f9c5c9
AS
2262 }
2263 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2264 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2265
2266 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2267 bits, elt_type);
14f9c5c9
AS
2268 return v;
2269}
2270
4c4b4cd2 2271/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2272
2273static int
d2e4a39e 2274has_negatives (struct type *type)
14f9c5c9 2275{
78134374 2276 switch (type->code ())
d2e4a39e
AS
2277 {
2278 default:
2279 return 0;
2280 case TYPE_CODE_INT:
c6d940a9 2281 return !type->is_unsigned ();
d2e4a39e 2282 case TYPE_CODE_RANGE:
5537ddd0 2283 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2284 }
14f9c5c9 2285}
d2e4a39e 2286
f93fca70 2287/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2288 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2289 the unpacked buffer.
14f9c5c9 2290
5b639dea
JB
2291 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2292 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2293
f93fca70
JB
2294 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2295 zero otherwise.
14f9c5c9 2296
f93fca70 2297 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2298
f93fca70
JB
2299 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2300
2301static void
2302ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2303 gdb_byte *unpacked, int unpacked_len,
2304 int is_big_endian, int is_signed_type,
2305 int is_scalar)
2306{
a1c95e6b
JB
2307 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2308 int src_idx; /* Index into the source area */
2309 int src_bytes_left; /* Number of source bytes left to process. */
2310 int srcBitsLeft; /* Number of source bits left to move */
2311 int unusedLS; /* Number of bits in next significant
dda83cd7 2312 byte of source that are unused */
a1c95e6b 2313
a1c95e6b
JB
2314 int unpacked_idx; /* Index into the unpacked buffer */
2315 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2316
4c4b4cd2 2317 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2318 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2319 unsigned char sign;
a1c95e6b 2320
4c4b4cd2
PH
2321 /* Transmit bytes from least to most significant; delta is the direction
2322 the indices move. */
f93fca70 2323 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2324
5b639dea
JB
2325 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2326 bits from SRC. .*/
2327 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2328 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2329 bit_size, unpacked_len);
2330
14f9c5c9 2331 srcBitsLeft = bit_size;
086ca51f 2332 src_bytes_left = src_len;
f93fca70 2333 unpacked_bytes_left = unpacked_len;
14f9c5c9 2334 sign = 0;
f93fca70
JB
2335
2336 if (is_big_endian)
14f9c5c9 2337 {
086ca51f 2338 src_idx = src_len - 1;
f93fca70
JB
2339 if (is_signed_type
2340 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2341 sign = ~0;
d2e4a39e
AS
2342
2343 unusedLS =
dda83cd7
SM
2344 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2345 % HOST_CHAR_BIT;
14f9c5c9 2346
f93fca70
JB
2347 if (is_scalar)
2348 {
dda83cd7
SM
2349 accumSize = 0;
2350 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2351 }
2352 else
2353 {
dda83cd7
SM
2354 /* Non-scalar values must be aligned at a byte boundary... */
2355 accumSize =
2356 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2357 /* ... And are placed at the beginning (most-significant) bytes
2358 of the target. */
2359 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2360 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2361 }
14f9c5c9 2362 }
d2e4a39e 2363 else
14f9c5c9
AS
2364 {
2365 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2366
086ca51f 2367 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2368 unusedLS = bit_offset;
2369 accumSize = 0;
2370
f93fca70 2371 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2372 sign = ~0;
14f9c5c9 2373 }
d2e4a39e 2374
14f9c5c9 2375 accum = 0;
086ca51f 2376 while (src_bytes_left > 0)
14f9c5c9
AS
2377 {
2378 /* Mask for removing bits of the next source byte that are not
dda83cd7 2379 part of the value. */
d2e4a39e 2380 unsigned int unusedMSMask =
dda83cd7
SM
2381 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2382 1;
4c4b4cd2 2383 /* Sign-extend bits for this byte. */
14f9c5c9 2384 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2385
d2e4a39e 2386 accum |=
dda83cd7 2387 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2388 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2389 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2390 {
2391 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2392 accumSize -= HOST_CHAR_BIT;
2393 accum >>= HOST_CHAR_BIT;
2394 unpacked_bytes_left -= 1;
2395 unpacked_idx += delta;
2396 }
14f9c5c9
AS
2397 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2398 unusedLS = 0;
086ca51f
JB
2399 src_bytes_left -= 1;
2400 src_idx += delta;
14f9c5c9 2401 }
086ca51f 2402 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2403 {
2404 accum |= sign << accumSize;
db297a65 2405 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2406 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2407 if (accumSize < 0)
2408 accumSize = 0;
14f9c5c9 2409 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2410 unpacked_bytes_left -= 1;
2411 unpacked_idx += delta;
14f9c5c9 2412 }
f93fca70
JB
2413}
2414
2415/* Create a new value of type TYPE from the contents of OBJ starting
2416 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2417 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2418 assigning through the result will set the field fetched from.
2419 VALADDR is ignored unless OBJ is NULL, in which case,
2420 VALADDR+OFFSET must address the start of storage containing the
2421 packed value. The value returned in this case is never an lval.
2422 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2423
2424struct value *
2425ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2426 long offset, int bit_offset, int bit_size,
dda83cd7 2427 struct type *type)
f93fca70
JB
2428{
2429 struct value *v;
bfb1c796 2430 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2431 gdb_byte *unpacked;
220475ed 2432 const int is_scalar = is_scalar_type (type);
d5a22e77 2433 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2434 gdb::byte_vector staging;
f93fca70
JB
2435
2436 type = ada_check_typedef (type);
2437
d0a9e810 2438 if (obj == NULL)
bfb1c796 2439 src = valaddr + offset;
d0a9e810 2440 else
50888e42 2441 src = value_contents (obj).data () + offset;
d0a9e810
JB
2442
2443 if (is_dynamic_type (type))
2444 {
2445 /* The length of TYPE might by dynamic, so we need to resolve
2446 TYPE in order to know its actual size, which we then use
2447 to create the contents buffer of the value we return.
2448 The difficulty is that the data containing our object is
2449 packed, and therefore maybe not at a byte boundary. So, what
2450 we do, is unpack the data into a byte-aligned buffer, and then
2451 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2452 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2453 staging.resize (staging_len);
d0a9e810
JB
2454
2455 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2456 staging.data (), staging.size (),
d0a9e810
JB
2457 is_big_endian, has_negatives (type),
2458 is_scalar);
b249d2c2 2459 type = resolve_dynamic_type (type, staging, 0);
0cafa88c
JB
2460 if (TYPE_LENGTH (type) < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
2461 {
2462 /* This happens when the length of the object is dynamic,
2463 and is actually smaller than the space reserved for it.
2464 For instance, in an array of variant records, the bit_size
2465 we're given is the array stride, which is constant and
2466 normally equal to the maximum size of its element.
2467 But, in reality, each element only actually spans a portion
2468 of that stride. */
2469 bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
2470 }
d0a9e810
JB
2471 }
2472
f93fca70
JB
2473 if (obj == NULL)
2474 {
2475 v = allocate_value (type);
bfb1c796 2476 src = valaddr + offset;
f93fca70
JB
2477 }
2478 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2479 {
0cafa88c 2480 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2481 gdb_byte *buf;
0cafa88c 2482
f93fca70 2483 v = value_at (type, value_address (obj) + offset);
bfb1c796
PA
2484 buf = (gdb_byte *) alloca (src_len);
2485 read_memory (value_address (v), buf, src_len);
2486 src = buf;
f93fca70
JB
2487 }
2488 else
2489 {
2490 v = allocate_value (type);
50888e42 2491 src = value_contents (obj).data () + offset;
f93fca70
JB
2492 }
2493
2494 if (obj != NULL)
2495 {
2496 long new_offset = offset;
2497
2498 set_value_component_location (v, obj);
2499 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2500 set_value_bitsize (v, bit_size);
2501 if (value_bitpos (v) >= HOST_CHAR_BIT)
dda83cd7 2502 {
f93fca70 2503 ++new_offset;
dda83cd7
SM
2504 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2505 }
f93fca70
JB
2506 set_value_offset (v, new_offset);
2507
2508 /* Also set the parent value. This is needed when trying to
2509 assign a new value (in inferior memory). */
2510 set_value_parent (v, obj);
2511 }
2512 else
2513 set_value_bitsize (v, bit_size);
50888e42 2514 unpacked = value_contents_writeable (v).data ();
f93fca70
JB
2515
2516 if (bit_size == 0)
2517 {
2518 memset (unpacked, 0, TYPE_LENGTH (type));
2519 return v;
2520 }
2521
d5722aa2 2522 if (staging.size () == TYPE_LENGTH (type))
f93fca70 2523 {
d0a9e810
JB
2524 /* Small short-cut: If we've unpacked the data into a buffer
2525 of the same size as TYPE's length, then we can reuse that,
2526 instead of doing the unpacking again. */
d5722aa2 2527 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2528 }
d0a9e810
JB
2529 else
2530 ada_unpack_from_contents (src, bit_offset, bit_size,
2531 unpacked, TYPE_LENGTH (type),
2532 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2533
14f9c5c9
AS
2534 return v;
2535}
d2e4a39e 2536
14f9c5c9
AS
2537/* Store the contents of FROMVAL into the location of TOVAL.
2538 Return a new value with the location of TOVAL and contents of
2539 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2540 floating-point or non-scalar types. */
14f9c5c9 2541
d2e4a39e
AS
2542static struct value *
2543ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2544{
df407dfe
AC
2545 struct type *type = value_type (toval);
2546 int bits = value_bitsize (toval);
14f9c5c9 2547
52ce6436
PH
2548 toval = ada_coerce_ref (toval);
2549 fromval = ada_coerce_ref (fromval);
2550
2551 if (ada_is_direct_array_type (value_type (toval)))
2552 toval = ada_coerce_to_simple_array (toval);
2553 if (ada_is_direct_array_type (value_type (fromval)))
2554 fromval = ada_coerce_to_simple_array (fromval);
2555
88e3b34b 2556 if (!deprecated_value_modifiable (toval))
323e0a4a 2557 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2558
d2e4a39e 2559 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2560 && bits > 0
78134374 2561 && (type->code () == TYPE_CODE_FLT
dda83cd7 2562 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2563 {
df407dfe
AC
2564 int len = (value_bitpos (toval)
2565 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2566 int from_size;
224c3ddb 2567 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2568 struct value *val;
42ae5230 2569 CORE_ADDR to_addr = value_address (toval);
14f9c5c9 2570
78134374 2571 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2572 fromval = value_cast (type, fromval);
14f9c5c9 2573
52ce6436 2574 read_memory (to_addr, buffer, len);
aced2898
PH
2575 from_size = value_bitsize (fromval);
2576 if (from_size == 0)
2577 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
d48e62f4 2578
d5a22e77 2579 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4
TT
2580 ULONGEST from_offset = 0;
2581 if (is_big_endian && is_scalar_type (value_type (fromval)))
2582 from_offset = from_size - bits;
2583 copy_bitwise (buffer, value_bitpos (toval),
50888e42 2584 value_contents (fromval).data (), from_offset,
d48e62f4 2585 bits, is_big_endian);
972daa01 2586 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2587
14f9c5c9 2588 val = value_copy (toval);
50888e42
SM
2589 memcpy (value_contents_raw (val).data (),
2590 value_contents (fromval).data (),
dda83cd7 2591 TYPE_LENGTH (type));
04624583 2592 deprecated_set_value_type (val, type);
d2e4a39e 2593
14f9c5c9
AS
2594 return val;
2595 }
2596
2597 return value_assign (toval, fromval);
2598}
2599
2600
7c512744
JB
2601/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2602 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2603 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2604 COMPONENT, and not the inferior's memory. The current contents
2605 of COMPONENT are ignored.
2606
2607 Although not part of the initial design, this function also works
2608 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2609 had a null address, and COMPONENT had an address which is equal to
2610 its offset inside CONTAINER. */
2611
52ce6436
PH
2612static void
2613value_assign_to_component (struct value *container, struct value *component,
2614 struct value *val)
2615{
2616 LONGEST offset_in_container =
42ae5230 2617 (LONGEST) (value_address (component) - value_address (container));
7c512744 2618 int bit_offset_in_container =
52ce6436
PH
2619 value_bitpos (component) - value_bitpos (container);
2620 int bits;
7c512744 2621
52ce6436
PH
2622 val = value_cast (value_type (component), val);
2623
2624 if (value_bitsize (component) == 0)
2625 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2626 else
2627 bits = value_bitsize (component);
2628
d5a22e77 2629 if (type_byte_order (value_type (container)) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2630 {
2631 int src_offset;
2632
2633 if (is_scalar_type (check_typedef (value_type (component))))
dda83cd7 2634 src_offset
2a62dfa9
JB
2635 = TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits;
2636 else
2637 src_offset = 0;
50888e42
SM
2638 copy_bitwise ((value_contents_writeable (container).data ()
2639 + offset_in_container),
a99bc3d2 2640 value_bitpos (container) + bit_offset_in_container,
50888e42 2641 value_contents (val).data (), src_offset, bits, 1);
2a62dfa9 2642 }
52ce6436 2643 else
50888e42
SM
2644 copy_bitwise ((value_contents_writeable (container).data ()
2645 + offset_in_container),
a99bc3d2 2646 value_bitpos (container) + bit_offset_in_container,
50888e42 2647 value_contents (val).data (), 0, bits, 0);
7c512744
JB
2648}
2649
736ade86
XR
2650/* Determine if TYPE is an access to an unconstrained array. */
2651
d91e9ea8 2652bool
736ade86
XR
2653ada_is_access_to_unconstrained_array (struct type *type)
2654{
78134374 2655 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2656 && is_thick_pntr (ada_typedef_target_type (type)));
2657}
2658
4c4b4cd2
PH
2659/* The value of the element of array ARR at the ARITY indices given in IND.
2660 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2661 thereto. */
2662
d2e4a39e
AS
2663struct value *
2664ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2665{
2666 int k;
d2e4a39e
AS
2667 struct value *elt;
2668 struct type *elt_type;
14f9c5c9
AS
2669
2670 elt = ada_coerce_to_simple_array (arr);
2671
df407dfe 2672 elt_type = ada_check_typedef (value_type (elt));
78134374 2673 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
2674 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2675 return value_subscript_packed (elt, arity, ind);
2676
2677 for (k = 0; k < arity; k += 1)
2678 {
b9c50e9a
XR
2679 struct type *saved_elt_type = TYPE_TARGET_TYPE (elt_type);
2680
78134374 2681 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 2682 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 2683
2497b498 2684 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
2685
2686 if (ada_is_access_to_unconstrained_array (saved_elt_type)
78134374 2687 && value_type (elt)->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
2688 {
2689 /* The element is a typedef to an unconstrained array,
2690 except that the value_subscript call stripped the
2691 typedef layer. The typedef layer is GNAT's way to
2692 specify that the element is, at the source level, an
2693 access to the unconstrained array, rather than the
2694 unconstrained array. So, we need to restore that
2695 typedef layer, which we can do by forcing the element's
2696 type back to its original type. Otherwise, the returned
2697 value is going to be printed as the array, rather
2698 than as an access. Another symptom of the same issue
2699 would be that an expression trying to dereference the
2700 element would also be improperly rejected. */
2701 deprecated_set_value_type (elt, saved_elt_type);
2702 }
2703
2704 elt_type = ada_check_typedef (value_type (elt));
14f9c5c9 2705 }
b9c50e9a 2706
14f9c5c9
AS
2707 return elt;
2708}
2709
deede10c
JB
2710/* Assuming ARR is a pointer to a GDB array, the value of the element
2711 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
2712 Does not read the entire array into memory.
2713
2714 Note: Unlike what one would expect, this function is used instead of
2715 ada_value_subscript for basically all non-packed array types. The reason
2716 for this is that a side effect of doing our own pointer arithmetics instead
2717 of relying on value_subscript is that there is no implicit typedef peeling.
2718 This is important for arrays of array accesses, where it allows us to
2719 preserve the fact that the array's element is an array access, where the
2720 access part os encoded in a typedef layer. */
14f9c5c9 2721
2c0b251b 2722static struct value *
deede10c 2723ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2724{
2725 int k;
919e6dbe 2726 struct value *array_ind = ada_value_ind (arr);
deede10c 2727 struct type *type
919e6dbe
PMR
2728 = check_typedef (value_enclosing_type (array_ind));
2729
78134374 2730 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
2731 && TYPE_FIELD_BITSIZE (type, 0) > 0)
2732 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
2733
2734 for (k = 0; k < arity; k += 1)
2735 {
2736 LONGEST lwb, upb;
14f9c5c9 2737
78134374 2738 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 2739 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 2740 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
dda83cd7 2741 value_copy (arr));
3d967001 2742 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 2743 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
14f9c5c9
AS
2744 type = TYPE_TARGET_TYPE (type);
2745 }
2746
2747 return value_ind (arr);
2748}
2749
0b5d8877 2750/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
2751 actual type of ARRAY_PTR is ignored), returns the Ada slice of
2752 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
2753 this array is LOW, as per Ada rules. */
0b5d8877 2754static struct value *
f5938064 2755ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 2756 int low, int high)
0b5d8877 2757{
b0dd7688 2758 struct type *type0 = ada_check_typedef (type);
3d967001 2759 struct type *base_index_type = TYPE_TARGET_TYPE (type0->index_type ());
0c9c3474 2760 struct type *index_type
aa715135 2761 = create_static_range_type (NULL, base_index_type, low, high);
9fe561ab
JB
2762 struct type *slice_type = create_array_type_with_stride
2763 (NULL, TYPE_TARGET_TYPE (type0), index_type,
24e99c6c 2764 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 2765 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 2766 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 2767 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
2768 CORE_ADDR base;
2769
6244c119
SM
2770 low_pos = discrete_position (base_index_type, low);
2771 base_low_pos = discrete_position (base_index_type, base_low);
2772
2773 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
2774 {
2775 warning (_("unable to get positions in slice, use bounds instead"));
2776 low_pos = low;
2777 base_low_pos = base_low;
2778 }
5b4ee69b 2779
7ff5b937
TT
2780 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
2781 if (stride == 0)
2782 stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
2783
6244c119 2784 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 2785 return value_at_lazy (slice_type, base);
0b5d8877
PH
2786}
2787
2788
2789static struct value *
2790ada_value_slice (struct value *array, int low, int high)
2791{
b0dd7688 2792 struct type *type = ada_check_typedef (value_type (array));
3d967001 2793 struct type *base_index_type = TYPE_TARGET_TYPE (type->index_type ());
0c9c3474 2794 struct type *index_type
3d967001 2795 = create_static_range_type (NULL, type->index_type (), low, high);
9fe561ab
JB
2796 struct type *slice_type = create_array_type_with_stride
2797 (NULL, TYPE_TARGET_TYPE (type), index_type,
24e99c6c 2798 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 2799 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
2800 gdb::optional<LONGEST> low_pos, high_pos;
2801
5b4ee69b 2802
6244c119
SM
2803 low_pos = discrete_position (base_index_type, low);
2804 high_pos = discrete_position (base_index_type, high);
2805
2806 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
2807 {
2808 warning (_("unable to get positions in slice, use bounds instead"));
2809 low_pos = low;
2810 high_pos = high;
2811 }
2812
2813 return value_cast (slice_type,
6244c119 2814 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
2815}
2816
14f9c5c9
AS
2817/* If type is a record type in the form of a standard GNAT array
2818 descriptor, returns the number of dimensions for type. If arr is a
2819 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2820 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2821
2822int
d2e4a39e 2823ada_array_arity (struct type *type)
14f9c5c9
AS
2824{
2825 int arity;
2826
2827 if (type == NULL)
2828 return 0;
2829
2830 type = desc_base_type (type);
2831
2832 arity = 0;
78134374 2833 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 2834 return desc_arity (desc_bounds_type (type));
d2e4a39e 2835 else
78134374 2836 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 2837 {
dda83cd7
SM
2838 arity += 1;
2839 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2840 }
d2e4a39e 2841
14f9c5c9
AS
2842 return arity;
2843}
2844
2845/* If TYPE is a record type in the form of a standard GNAT array
2846 descriptor or a simple array type, returns the element type for
2847 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2848 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2849
d2e4a39e
AS
2850struct type *
2851ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2852{
2853 type = desc_base_type (type);
2854
78134374 2855 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
2856 {
2857 int k;
d2e4a39e 2858 struct type *p_array_type;
14f9c5c9 2859
556bdfd4 2860 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
2861
2862 k = ada_array_arity (type);
2863 if (k == 0)
dda83cd7 2864 return NULL;
d2e4a39e 2865
4c4b4cd2 2866 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2867 if (nindices >= 0 && k > nindices)
dda83cd7 2868 k = nindices;
d2e4a39e 2869 while (k > 0 && p_array_type != NULL)
dda83cd7
SM
2870 {
2871 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
2872 k -= 1;
2873 }
14f9c5c9
AS
2874 return p_array_type;
2875 }
78134374 2876 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 2877 {
78134374 2878 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7
SM
2879 {
2880 type = TYPE_TARGET_TYPE (type);
2881 nindices -= 1;
2882 }
14f9c5c9
AS
2883 return type;
2884 }
2885
2886 return NULL;
2887}
2888
08a057e6 2889/* See ada-lang.h. */
14f9c5c9 2890
08a057e6 2891struct type *
1eea4ebd 2892ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 2893{
4c4b4cd2
PH
2894 struct type *result_type;
2895
14f9c5c9
AS
2896 type = desc_base_type (type);
2897
1eea4ebd
UW
2898 if (n < 0 || n > ada_array_arity (type))
2899 error (_("invalid dimension number to '%s"), name);
14f9c5c9 2900
4c4b4cd2 2901 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2902 {
2903 int i;
2904
2905 for (i = 1; i < n; i += 1)
2869ac4b
TT
2906 {
2907 type = ada_check_typedef (type);
2908 type = TYPE_TARGET_TYPE (type);
2909 }
2910 result_type = TYPE_TARGET_TYPE (ada_check_typedef (type)->index_type ());
4c4b4cd2 2911 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
2912 has a target type of TYPE_CODE_UNDEF. We compensate here, but
2913 perhaps stabsread.c would make more sense. */
78134374 2914 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 2915 result_type = NULL;
14f9c5c9 2916 }
d2e4a39e 2917 else
1eea4ebd
UW
2918 {
2919 result_type = desc_index_type (desc_bounds_type (type), n);
2920 if (result_type == NULL)
2921 error (_("attempt to take bound of something that is not an array"));
2922 }
2923
2924 return result_type;
14f9c5c9
AS
2925}
2926
2927/* Given that arr is an array type, returns the lower bound of the
2928 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 2929 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
2930 array-descriptor type. It works for other arrays with bounds supplied
2931 by run-time quantities other than discriminants. */
14f9c5c9 2932
abb68b3e 2933static LONGEST
fb5e3d5c 2934ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 2935{
8a48ac95 2936 struct type *type, *index_type_desc, *index_type;
1ce677a4 2937 int i;
262452ec
JK
2938
2939 gdb_assert (which == 0 || which == 1);
14f9c5c9 2940
ad82864c
JB
2941 if (ada_is_constrained_packed_array_type (arr_type))
2942 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 2943
4c4b4cd2 2944 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 2945 return (LONGEST) - which;
14f9c5c9 2946
78134374 2947 if (arr_type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
2948 type = TYPE_TARGET_TYPE (arr_type);
2949 else
2950 type = arr_type;
2951
22c4c60c 2952 if (type->is_fixed_instance ())
bafffb51
JB
2953 {
2954 /* The array has already been fixed, so we do not need to
2955 check the parallel ___XA type again. That encoding has
2956 already been applied, so ignore it now. */
2957 index_type_desc = NULL;
2958 }
2959 else
2960 {
2961 index_type_desc = ada_find_parallel_type (type, "___XA");
2962 ada_fixup_array_indexes_type (index_type_desc);
2963 }
2964
262452ec 2965 if (index_type_desc != NULL)
940da03e 2966 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 2967 NULL);
262452ec 2968 else
8a48ac95
JB
2969 {
2970 struct type *elt_type = check_typedef (type);
2971
2972 for (i = 1; i < n; i++)
2973 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
2974
3d967001 2975 index_type = elt_type->index_type ();
8a48ac95 2976 }
262452ec 2977
43bbcdc2
PH
2978 return
2979 (LONGEST) (which == 0
dda83cd7
SM
2980 ? ada_discrete_type_low_bound (index_type)
2981 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
2982}
2983
2984/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
2985 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2986 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 2987 supplied by run-time quantities other than discriminants. */
14f9c5c9 2988
1eea4ebd 2989static LONGEST
4dc81987 2990ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 2991{
eb479039
JB
2992 struct type *arr_type;
2993
78134374 2994 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
2995 arr = value_ind (arr);
2996 arr_type = value_enclosing_type (arr);
14f9c5c9 2997
ad82864c
JB
2998 if (ada_is_constrained_packed_array_type (arr_type))
2999 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3000 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3001 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3002 else
1eea4ebd 3003 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3004}
3005
3006/* Given that arr is an array value, returns the length of the
3007 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3008 supplied by run-time quantities other than discriminants.
3009 Does not work for arrays indexed by enumeration types with representation
3010 clauses at the moment. */
14f9c5c9 3011
1eea4ebd 3012static LONGEST
d2e4a39e 3013ada_array_length (struct value *arr, int n)
14f9c5c9 3014{
aa715135
JG
3015 struct type *arr_type, *index_type;
3016 int low, high;
eb479039 3017
78134374 3018 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3019 arr = value_ind (arr);
3020 arr_type = value_enclosing_type (arr);
14f9c5c9 3021
ad82864c
JB
3022 if (ada_is_constrained_packed_array_type (arr_type))
3023 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3024
4c4b4cd2 3025 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3026 {
3027 low = ada_array_bound_from_type (arr_type, n, 0);
3028 high = ada_array_bound_from_type (arr_type, n, 1);
3029 }
14f9c5c9 3030 else
aa715135
JG
3031 {
3032 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3033 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3034 }
3035
f168693b 3036 arr_type = check_typedef (arr_type);
7150d33c 3037 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3038 if (index_type != NULL)
3039 {
3040 struct type *base_type;
78134374 3041 if (index_type->code () == TYPE_CODE_RANGE)
aa715135
JG
3042 base_type = TYPE_TARGET_TYPE (index_type);
3043 else
3044 base_type = index_type;
3045
3046 low = pos_atr (value_from_longest (base_type, low));
3047 high = pos_atr (value_from_longest (base_type, high));
3048 }
3049 return high - low + 1;
4c4b4cd2
PH
3050}
3051
bff8c71f
TT
3052/* An array whose type is that of ARR_TYPE (an array type), with
3053 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3054 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3055
3056static struct value *
bff8c71f 3057empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3058{
b0dd7688 3059 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3060 struct type *index_type
3061 = create_static_range_type
dda83cd7 3062 (NULL, TYPE_TARGET_TYPE (arr_type0->index_type ()), low,
bff8c71f 3063 high < low ? low - 1 : high);
b0dd7688 3064 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3065
0b5d8877 3066 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3067}
14f9c5c9 3068\f
d2e4a39e 3069
dda83cd7 3070 /* Name resolution */
14f9c5c9 3071
4c4b4cd2
PH
3072/* The "decoded" name for the user-definable Ada operator corresponding
3073 to OP. */
14f9c5c9 3074
d2e4a39e 3075static const char *
4c4b4cd2 3076ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3077{
3078 int i;
3079
4c4b4cd2 3080 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3081 {
3082 if (ada_opname_table[i].op == op)
dda83cd7 3083 return ada_opname_table[i].decoded;
14f9c5c9 3084 }
323e0a4a 3085 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3086}
3087
de93309a
SM
3088/* Returns true (non-zero) iff decoded name N0 should appear before N1
3089 in a listing of choices during disambiguation (see sort_choices, below).
3090 The idea is that overloadings of a subprogram name from the
3091 same package should sort in their source order. We settle for ordering
3092 such symbols by their trailing number (__N or $N). */
14f9c5c9 3093
de93309a
SM
3094static int
3095encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3096{
de93309a
SM
3097 if (N1 == NULL)
3098 return 0;
3099 else if (N0 == NULL)
3100 return 1;
3101 else
3102 {
3103 int k0, k1;
30b15541 3104
de93309a 3105 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3106 ;
de93309a 3107 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3108 ;
de93309a 3109 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3110 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3111 {
3112 int n0, n1;
3113
3114 n0 = k0;
3115 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3116 n0 -= 1;
3117 n1 = k1;
3118 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3119 n1 -= 1;
3120 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3121 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3122 }
de93309a
SM
3123 return (strcmp (N0, N1) < 0);
3124 }
14f9c5c9
AS
3125}
3126
de93309a
SM
3127/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3128 encoded names. */
14f9c5c9 3129
de93309a
SM
3130static void
3131sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3132{
14f9c5c9 3133 int i;
14f9c5c9 3134
de93309a 3135 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3136 {
de93309a
SM
3137 struct block_symbol sym = syms[i];
3138 int j;
3139
3140 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3141 {
3142 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3143 sym.symbol->linkage_name ()))
3144 break;
3145 syms[j + 1] = syms[j];
3146 }
de93309a
SM
3147 syms[j + 1] = sym;
3148 }
3149}
14f9c5c9 3150
de93309a
SM
3151/* Whether GDB should display formals and return types for functions in the
3152 overloads selection menu. */
3153static bool print_signatures = true;
4c4b4cd2 3154
de93309a
SM
3155/* Print the signature for SYM on STREAM according to the FLAGS options. For
3156 all but functions, the signature is just the name of the symbol. For
3157 functions, this is the name of the function, the list of types for formals
3158 and the return type (if any). */
4c4b4cd2 3159
de93309a
SM
3160static void
3161ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3162 const struct type_print_options *flags)
3163{
3164 struct type *type = SYMBOL_TYPE (sym);
14f9c5c9 3165
987012b8 3166 fprintf_filtered (stream, "%s", sym->print_name ());
de93309a
SM
3167 if (!print_signatures
3168 || type == NULL
78134374 3169 || type->code () != TYPE_CODE_FUNC)
de93309a 3170 return;
4c4b4cd2 3171
1f704f76 3172 if (type->num_fields () > 0)
de93309a
SM
3173 {
3174 int i;
14f9c5c9 3175
de93309a 3176 fprintf_filtered (stream, " (");
1f704f76 3177 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3178 {
3179 if (i > 0)
3180 fprintf_filtered (stream, "; ");
940da03e 3181 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3182 flags);
3183 }
3184 fprintf_filtered (stream, ")");
3185 }
3186 if (TYPE_TARGET_TYPE (type) != NULL
78134374 3187 && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
de93309a
SM
3188 {
3189 fprintf_filtered (stream, " return ");
3190 ada_print_type (TYPE_TARGET_TYPE (type), NULL, stream, -1, 0, flags);
3191 }
3192}
14f9c5c9 3193
de93309a
SM
3194/* Read and validate a set of numeric choices from the user in the
3195 range 0 .. N_CHOICES-1. Place the results in increasing
3196 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3197
de93309a
SM
3198 The user types choices as a sequence of numbers on one line
3199 separated by blanks, encoding them as follows:
14f9c5c9 3200
de93309a
SM
3201 + A choice of 0 means to cancel the selection, throwing an error.
3202 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3203 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3204
de93309a 3205 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3206
de93309a
SM
3207 ANNOTATION_SUFFIX, if present, is used to annotate the input
3208 prompts (for use with the -f switch). */
14f9c5c9 3209
de93309a
SM
3210static int
3211get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3212 int is_all_choice, const char *annotation_suffix)
de93309a 3213{
992a7040 3214 const char *args;
de93309a
SM
3215 const char *prompt;
3216 int n_chosen;
3217 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3218
de93309a
SM
3219 prompt = getenv ("PS2");
3220 if (prompt == NULL)
3221 prompt = "> ";
4c4b4cd2 3222
de93309a 3223 args = command_line_input (prompt, annotation_suffix);
4c4b4cd2 3224
de93309a
SM
3225 if (args == NULL)
3226 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3227
de93309a 3228 n_chosen = 0;
4c4b4cd2 3229
de93309a
SM
3230 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3231 order, as given in args. Choices are validated. */
3232 while (1)
14f9c5c9 3233 {
de93309a
SM
3234 char *args2;
3235 int choice, j;
76a01679 3236
de93309a
SM
3237 args = skip_spaces (args);
3238 if (*args == '\0' && n_chosen == 0)
dda83cd7 3239 error_no_arg (_("one or more choice numbers"));
de93309a 3240 else if (*args == '\0')
dda83cd7 3241 break;
76a01679 3242
de93309a
SM
3243 choice = strtol (args, &args2, 10);
3244 if (args == args2 || choice < 0
dda83cd7
SM
3245 || choice > n_choices + first_choice - 1)
3246 error (_("Argument must be choice number"));
de93309a 3247 args = args2;
76a01679 3248
de93309a 3249 if (choice == 0)
dda83cd7 3250 error (_("cancelled"));
76a01679 3251
de93309a 3252 if (choice < first_choice)
dda83cd7
SM
3253 {
3254 n_chosen = n_choices;
3255 for (j = 0; j < n_choices; j += 1)
3256 choices[j] = j;
3257 break;
3258 }
de93309a 3259 choice -= first_choice;
76a01679 3260
de93309a 3261 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3262 {
3263 }
4c4b4cd2 3264
de93309a 3265 if (j < 0 || choice != choices[j])
dda83cd7
SM
3266 {
3267 int k;
4c4b4cd2 3268
dda83cd7
SM
3269 for (k = n_chosen - 1; k > j; k -= 1)
3270 choices[k + 1] = choices[k];
3271 choices[j + 1] = choice;
3272 n_chosen += 1;
3273 }
14f9c5c9
AS
3274 }
3275
de93309a
SM
3276 if (n_chosen > max_results)
3277 error (_("Select no more than %d of the above"), max_results);
3278
3279 return n_chosen;
14f9c5c9
AS
3280}
3281
de93309a
SM
3282/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3283 by asking the user (if necessary), returning the number selected,
3284 and setting the first elements of SYMS items. Error if no symbols
3285 selected. */
3286
3287/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3288 to be re-integrated one of these days. */
14f9c5c9
AS
3289
3290static int
de93309a 3291user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3292{
de93309a
SM
3293 int i;
3294 int *chosen = XALLOCAVEC (int , nsyms);
3295 int n_chosen;
3296 int first_choice = (max_results == 1) ? 1 : 2;
3297 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3298
de93309a
SM
3299 if (max_results < 1)
3300 error (_("Request to select 0 symbols!"));
3301 if (nsyms <= 1)
3302 return nsyms;
14f9c5c9 3303
de93309a
SM
3304 if (select_mode == multiple_symbols_cancel)
3305 error (_("\
3306canceled because the command is ambiguous\n\
3307See set/show multiple-symbol."));
14f9c5c9 3308
de93309a
SM
3309 /* If select_mode is "all", then return all possible symbols.
3310 Only do that if more than one symbol can be selected, of course.
3311 Otherwise, display the menu as usual. */
3312 if (select_mode == multiple_symbols_all && max_results > 1)
3313 return nsyms;
14f9c5c9 3314
de93309a
SM
3315 printf_filtered (_("[0] cancel\n"));
3316 if (max_results > 1)
3317 printf_filtered (_("[1] all\n"));
14f9c5c9 3318
de93309a 3319 sort_choices (syms, nsyms);
14f9c5c9 3320
de93309a
SM
3321 for (i = 0; i < nsyms; i += 1)
3322 {
3323 if (syms[i].symbol == NULL)
dda83cd7 3324 continue;
14f9c5c9 3325
de93309a 3326 if (SYMBOL_CLASS (syms[i].symbol) == LOC_BLOCK)
dda83cd7
SM
3327 {
3328 struct symtab_and_line sal =
3329 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3330
de93309a
SM
3331 printf_filtered ("[%d] ", i + first_choice);
3332 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3333 &type_print_raw_options);
3334 if (sal.symtab == NULL)
3335 printf_filtered (_(" at %p[<no source file available>%p]:%d\n"),
3336 metadata_style.style ().ptr (), nullptr, sal.line);
3337 else
3338 printf_filtered
3339 (_(" at %ps:%d\n"),
3340 styled_string (file_name_style.style (),
3341 symtab_to_filename_for_display (sal.symtab)),
3342 sal.line);
dda83cd7
SM
3343 continue;
3344 }
76a01679 3345 else
dda83cd7
SM
3346 {
3347 int is_enumeral =
3348 (SYMBOL_CLASS (syms[i].symbol) == LOC_CONST
3349 && SYMBOL_TYPE (syms[i].symbol) != NULL
3350 && SYMBOL_TYPE (syms[i].symbol)->code () == TYPE_CODE_ENUM);
de93309a 3351 struct symtab *symtab = NULL;
4c4b4cd2 3352
de93309a
SM
3353 if (SYMBOL_OBJFILE_OWNED (syms[i].symbol))
3354 symtab = symbol_symtab (syms[i].symbol);
3355
dda83cd7 3356 if (SYMBOL_LINE (syms[i].symbol) != 0 && symtab != NULL)
de93309a
SM
3357 {
3358 printf_filtered ("[%d] ", i + first_choice);
3359 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3360 &type_print_raw_options);
3361 printf_filtered (_(" at %s:%d\n"),
3362 symtab_to_filename_for_display (symtab),
3363 SYMBOL_LINE (syms[i].symbol));
3364 }
dda83cd7
SM
3365 else if (is_enumeral
3366 && SYMBOL_TYPE (syms[i].symbol)->name () != NULL)
3367 {
3368 printf_filtered (("[%d] "), i + first_choice);
3369 ada_print_type (SYMBOL_TYPE (syms[i].symbol), NULL,
3370 gdb_stdout, -1, 0, &type_print_raw_options);
3371 printf_filtered (_("'(%s) (enumeral)\n"),
987012b8 3372 syms[i].symbol->print_name ());
dda83cd7 3373 }
de93309a
SM
3374 else
3375 {
3376 printf_filtered ("[%d] ", i + first_choice);
3377 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3378 &type_print_raw_options);
3379
3380 if (symtab != NULL)
3381 printf_filtered (is_enumeral
3382 ? _(" in %s (enumeral)\n")
3383 : _(" at %s:?\n"),
3384 symtab_to_filename_for_display (symtab));
3385 else
3386 printf_filtered (is_enumeral
3387 ? _(" (enumeral)\n")
3388 : _(" at ?\n"));
3389 }
dda83cd7 3390 }
14f9c5c9 3391 }
14f9c5c9 3392
de93309a 3393 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3394 "overload-choice");
14f9c5c9 3395
de93309a
SM
3396 for (i = 0; i < n_chosen; i += 1)
3397 syms[i] = syms[chosen[i]];
14f9c5c9 3398
de93309a
SM
3399 return n_chosen;
3400}
14f9c5c9 3401
cd9a3148
TT
3402/* See ada-lang.h. */
3403
3404block_symbol
7056f312 3405ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
cd9a3148
TT
3406 int nargs, value *argvec[])
3407{
3408 if (possible_user_operator_p (op, argvec))
3409 {
3410 std::vector<struct block_symbol> candidates
3411 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3412 NULL, VAR_DOMAIN);
3413
3414 int i = ada_resolve_function (candidates, argvec,
3415 nargs, ada_decoded_op_name (op), NULL,
3416 parse_completion);
3417 if (i >= 0)
3418 return candidates[i];
3419 }
3420 return {};
3421}
3422
3423/* See ada-lang.h. */
3424
3425block_symbol
3426ada_resolve_funcall (struct symbol *sym, const struct block *block,
3427 struct type *context_type,
7056f312 3428 bool parse_completion,
cd9a3148
TT
3429 int nargs, value *argvec[],
3430 innermost_block_tracker *tracker)
3431{
3432 std::vector<struct block_symbol> candidates
3433 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3434
3435 int i;
3436 if (candidates.size () == 1)
3437 i = 0;
3438 else
3439 {
3440 i = ada_resolve_function
3441 (candidates,
3442 argvec, nargs,
3443 sym->linkage_name (),
3444 context_type, parse_completion);
3445 if (i < 0)
3446 error (_("Could not find a match for %s"), sym->print_name ());
3447 }
3448
3449 tracker->update (candidates[i]);
3450 return candidates[i];
3451}
3452
ba8694b6
TT
3453/* Resolve a mention of a name where the context type is an
3454 enumeration type. */
3455
3456static int
3457ada_resolve_enum (std::vector<struct block_symbol> &syms,
3458 const char *name, struct type *context_type,
3459 bool parse_completion)
3460{
3461 gdb_assert (context_type->code () == TYPE_CODE_ENUM);
3462 context_type = ada_check_typedef (context_type);
3463
3464 for (int i = 0; i < syms.size (); ++i)
3465 {
3466 /* We already know the name matches, so we're just looking for
3467 an element of the correct enum type. */
3468 if (ada_check_typedef (SYMBOL_TYPE (syms[i].symbol)) == context_type)
3469 return i;
3470 }
3471
3472 error (_("No name '%s' in enumeration type '%s'"), name,
3473 ada_type_name (context_type));
3474}
3475
cd9a3148
TT
3476/* See ada-lang.h. */
3477
3478block_symbol
3479ada_resolve_variable (struct symbol *sym, const struct block *block,
3480 struct type *context_type,
7056f312 3481 bool parse_completion,
cd9a3148
TT
3482 int deprocedure_p,
3483 innermost_block_tracker *tracker)
3484{
3485 std::vector<struct block_symbol> candidates
3486 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3487
3488 if (std::any_of (candidates.begin (),
3489 candidates.end (),
3490 [] (block_symbol &bsym)
3491 {
3492 switch (SYMBOL_CLASS (bsym.symbol))
3493 {
3494 case LOC_REGISTER:
3495 case LOC_ARG:
3496 case LOC_REF_ARG:
3497 case LOC_REGPARM_ADDR:
3498 case LOC_LOCAL:
3499 case LOC_COMPUTED:
3500 return true;
3501 default:
3502 return false;
3503 }
3504 }))
3505 {
3506 /* Types tend to get re-introduced locally, so if there
3507 are any local symbols that are not types, first filter
3508 out all types. */
3509 candidates.erase
3510 (std::remove_if
3511 (candidates.begin (),
3512 candidates.end (),
3513 [] (block_symbol &bsym)
3514 {
3515 return SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF;
3516 }),
3517 candidates.end ());
3518 }
3519
2c71f639
TV
3520 /* Filter out artificial symbols. */
3521 candidates.erase
3522 (std::remove_if
3523 (candidates.begin (),
3524 candidates.end (),
3525 [] (block_symbol &bsym)
3526 {
3527 return bsym.symbol->artificial;
3528 }),
3529 candidates.end ());
3530
cd9a3148
TT
3531 int i;
3532 if (candidates.empty ())
3533 error (_("No definition found for %s"), sym->print_name ());
3534 else if (candidates.size () == 1)
3535 i = 0;
ba8694b6
TT
3536 else if (context_type != nullptr
3537 && context_type->code () == TYPE_CODE_ENUM)
3538 i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
3539 parse_completion);
cd9a3148
TT
3540 else if (deprocedure_p && !is_nonfunction (candidates))
3541 {
3542 i = ada_resolve_function
3543 (candidates, NULL, 0,
3544 sym->linkage_name (),
3545 context_type, parse_completion);
3546 if (i < 0)
3547 error (_("Could not find a match for %s"), sym->print_name ());
3548 }
3549 else
3550 {
3551 printf_filtered (_("Multiple matches for %s\n"), sym->print_name ());
3552 user_select_syms (candidates.data (), candidates.size (), 1);
3553 i = 0;
3554 }
3555
3556 tracker->update (candidates[i]);
3557 return candidates[i];
3558}
3559
db2534b7 3560/* Return non-zero if formal type FTYPE matches actual type ATYPE. */
de93309a
SM
3561/* The term "match" here is rather loose. The match is heuristic and
3562 liberal. */
14f9c5c9 3563
de93309a 3564static int
db2534b7 3565ada_type_match (struct type *ftype, struct type *atype)
14f9c5c9 3566{
de93309a
SM
3567 ftype = ada_check_typedef (ftype);
3568 atype = ada_check_typedef (atype);
14f9c5c9 3569
78134374 3570 if (ftype->code () == TYPE_CODE_REF)
de93309a 3571 ftype = TYPE_TARGET_TYPE (ftype);
78134374 3572 if (atype->code () == TYPE_CODE_REF)
de93309a 3573 atype = TYPE_TARGET_TYPE (atype);
14f9c5c9 3574
78134374 3575 switch (ftype->code ())
14f9c5c9 3576 {
de93309a 3577 default:
78134374 3578 return ftype->code () == atype->code ();
de93309a 3579 case TYPE_CODE_PTR:
db2534b7
TT
3580 if (atype->code () != TYPE_CODE_PTR)
3581 return 0;
3582 atype = TYPE_TARGET_TYPE (atype);
3583 /* This can only happen if the actual argument is 'null'. */
3584 if (atype->code () == TYPE_CODE_INT && TYPE_LENGTH (atype) == 0)
3585 return 1;
3586 return ada_type_match (TYPE_TARGET_TYPE (ftype), atype);
de93309a
SM
3587 case TYPE_CODE_INT:
3588 case TYPE_CODE_ENUM:
3589 case TYPE_CODE_RANGE:
78134374 3590 switch (atype->code ())
dda83cd7
SM
3591 {
3592 case TYPE_CODE_INT:
3593 case TYPE_CODE_ENUM:
3594 case TYPE_CODE_RANGE:
3595 return 1;
3596 default:
3597 return 0;
3598 }
d2e4a39e 3599
de93309a 3600 case TYPE_CODE_ARRAY:
78134374 3601 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3602 || ada_is_array_descriptor_type (atype));
14f9c5c9 3603
de93309a
SM
3604 case TYPE_CODE_STRUCT:
3605 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3606 return (atype->code () == TYPE_CODE_ARRAY
3607 || ada_is_array_descriptor_type (atype));
de93309a 3608 else
dda83cd7
SM
3609 return (atype->code () == TYPE_CODE_STRUCT
3610 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3611
de93309a
SM
3612 case TYPE_CODE_UNION:
3613 case TYPE_CODE_FLT:
78134374 3614 return (atype->code () == ftype->code ());
de93309a 3615 }
14f9c5c9
AS
3616}
3617
de93309a
SM
3618/* Return non-zero if the formals of FUNC "sufficiently match" the
3619 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3620 may also be an enumeral, in which case it is treated as a 0-
3621 argument function. */
14f9c5c9 3622
de93309a
SM
3623static int
3624ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3625{
3626 int i;
3627 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 3628
de93309a 3629 if (SYMBOL_CLASS (func) == LOC_CONST
78134374 3630 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3631 return (n_actuals == 0);
78134374 3632 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3633 return 0;
14f9c5c9 3634
1f704f76 3635 if (func_type->num_fields () != n_actuals)
de93309a 3636 return 0;
14f9c5c9 3637
de93309a
SM
3638 for (i = 0; i < n_actuals; i += 1)
3639 {
3640 if (actuals[i] == NULL)
dda83cd7 3641 return 0;
de93309a 3642 else
dda83cd7
SM
3643 {
3644 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
3645 struct type *atype = ada_check_typedef (value_type (actuals[i]));
14f9c5c9 3646
db2534b7 3647 if (!ada_type_match (ftype, atype))
dda83cd7
SM
3648 return 0;
3649 }
de93309a
SM
3650 }
3651 return 1;
3652}
d2e4a39e 3653
de93309a
SM
3654/* False iff function type FUNC_TYPE definitely does not produce a value
3655 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3656 FUNC_TYPE is not a valid function type with a non-null return type
3657 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 3658
de93309a
SM
3659static int
3660return_match (struct type *func_type, struct type *context_type)
3661{
3662 struct type *return_type;
d2e4a39e 3663
de93309a
SM
3664 if (func_type == NULL)
3665 return 1;
14f9c5c9 3666
78134374 3667 if (func_type->code () == TYPE_CODE_FUNC)
de93309a
SM
3668 return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
3669 else
3670 return_type = get_base_type (func_type);
3671 if (return_type == NULL)
3672 return 1;
76a01679 3673
de93309a 3674 context_type = get_base_type (context_type);
14f9c5c9 3675
78134374 3676 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
3677 return context_type == NULL || return_type == context_type;
3678 else if (context_type == NULL)
78134374 3679 return return_type->code () != TYPE_CODE_VOID;
de93309a 3680 else
78134374 3681 return return_type->code () == context_type->code ();
de93309a 3682}
14f9c5c9 3683
14f9c5c9 3684
1bfa81ac 3685/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
3686 function (if any) that matches the types of the NARGS arguments in
3687 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3688 that returns that type, then eliminate matches that don't. If
3689 CONTEXT_TYPE is void and there is at least one match that does not
3690 return void, eliminate all matches that do.
14f9c5c9 3691
de93309a
SM
3692 Asks the user if there is more than one match remaining. Returns -1
3693 if there is no such symbol or none is selected. NAME is used
3694 solely for messages. May re-arrange and modify SYMS in
3695 the process; the index returned is for the modified vector. */
14f9c5c9 3696
de93309a 3697static int
d1183b06
TT
3698ada_resolve_function (std::vector<struct block_symbol> &syms,
3699 struct value **args, int nargs,
dda83cd7 3700 const char *name, struct type *context_type,
7056f312 3701 bool parse_completion)
de93309a
SM
3702{
3703 int fallback;
3704 int k;
3705 int m; /* Number of hits */
14f9c5c9 3706
de93309a
SM
3707 m = 0;
3708 /* In the first pass of the loop, we only accept functions matching
3709 context_type. If none are found, we add a second pass of the loop
3710 where every function is accepted. */
3711 for (fallback = 0; m == 0 && fallback < 2; fallback++)
3712 {
d1183b06 3713 for (k = 0; k < syms.size (); k += 1)
dda83cd7
SM
3714 {
3715 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].symbol));
5b4ee69b 3716
dda83cd7
SM
3717 if (ada_args_match (syms[k].symbol, args, nargs)
3718 && (fallback || return_match (type, context_type)))
3719 {
3720 syms[m] = syms[k];
3721 m += 1;
3722 }
3723 }
14f9c5c9
AS
3724 }
3725
de93309a
SM
3726 /* If we got multiple matches, ask the user which one to use. Don't do this
3727 interactive thing during completion, though, as the purpose of the
3728 completion is providing a list of all possible matches. Prompting the
3729 user to filter it down would be completely unexpected in this case. */
3730 if (m == 0)
3731 return -1;
3732 else if (m > 1 && !parse_completion)
3733 {
3734 printf_filtered (_("Multiple matches for %s\n"), name);
d1183b06 3735 user_select_syms (syms.data (), m, 1);
de93309a
SM
3736 return 0;
3737 }
3738 return 0;
14f9c5c9
AS
3739}
3740
14f9c5c9
AS
3741/* Type-class predicates */
3742
4c4b4cd2
PH
3743/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3744 or FLOAT). */
14f9c5c9
AS
3745
3746static int
d2e4a39e 3747numeric_type_p (struct type *type)
14f9c5c9
AS
3748{
3749 if (type == NULL)
3750 return 0;
d2e4a39e
AS
3751 else
3752 {
78134374 3753 switch (type->code ())
dda83cd7
SM
3754 {
3755 case TYPE_CODE_INT:
3756 case TYPE_CODE_FLT:
c04da66c 3757 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
3758 return 1;
3759 case TYPE_CODE_RANGE:
3760 return (type == TYPE_TARGET_TYPE (type)
3761 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3762 default:
3763 return 0;
3764 }
d2e4a39e 3765 }
14f9c5c9
AS
3766}
3767
4c4b4cd2 3768/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
3769
3770static int
d2e4a39e 3771integer_type_p (struct type *type)
14f9c5c9
AS
3772{
3773 if (type == NULL)
3774 return 0;
d2e4a39e
AS
3775 else
3776 {
78134374 3777 switch (type->code ())
dda83cd7
SM
3778 {
3779 case TYPE_CODE_INT:
3780 return 1;
3781 case TYPE_CODE_RANGE:
3782 return (type == TYPE_TARGET_TYPE (type)
3783 || integer_type_p (TYPE_TARGET_TYPE (type)));
3784 default:
3785 return 0;
3786 }
d2e4a39e 3787 }
14f9c5c9
AS
3788}
3789
4c4b4cd2 3790/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
3791
3792static int
d2e4a39e 3793scalar_type_p (struct type *type)
14f9c5c9
AS
3794{
3795 if (type == NULL)
3796 return 0;
d2e4a39e
AS
3797 else
3798 {
78134374 3799 switch (type->code ())
dda83cd7
SM
3800 {
3801 case TYPE_CODE_INT:
3802 case TYPE_CODE_RANGE:
3803 case TYPE_CODE_ENUM:
3804 case TYPE_CODE_FLT:
c04da66c 3805 case TYPE_CODE_FIXED_POINT:
dda83cd7
SM
3806 return 1;
3807 default:
3808 return 0;
3809 }
d2e4a39e 3810 }
14f9c5c9
AS
3811}
3812
4c4b4cd2 3813/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
3814
3815static int
d2e4a39e 3816discrete_type_p (struct type *type)
14f9c5c9
AS
3817{
3818 if (type == NULL)
3819 return 0;
d2e4a39e
AS
3820 else
3821 {
78134374 3822 switch (type->code ())
dda83cd7
SM
3823 {
3824 case TYPE_CODE_INT:
3825 case TYPE_CODE_RANGE:
3826 case TYPE_CODE_ENUM:
3827 case TYPE_CODE_BOOL:
3828 return 1;
3829 default:
3830 return 0;
3831 }
d2e4a39e 3832 }
14f9c5c9
AS
3833}
3834
4c4b4cd2
PH
3835/* Returns non-zero if OP with operands in the vector ARGS could be
3836 a user-defined function. Errs on the side of pre-defined operators
3837 (i.e., result 0). */
14f9c5c9
AS
3838
3839static int
d2e4a39e 3840possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 3841{
76a01679 3842 struct type *type0 =
df407dfe 3843 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 3844 struct type *type1 =
df407dfe 3845 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 3846
4c4b4cd2
PH
3847 if (type0 == NULL)
3848 return 0;
3849
14f9c5c9
AS
3850 switch (op)
3851 {
3852 default:
3853 return 0;
3854
3855 case BINOP_ADD:
3856 case BINOP_SUB:
3857 case BINOP_MUL:
3858 case BINOP_DIV:
d2e4a39e 3859 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
3860
3861 case BINOP_REM:
3862 case BINOP_MOD:
3863 case BINOP_BITWISE_AND:
3864 case BINOP_BITWISE_IOR:
3865 case BINOP_BITWISE_XOR:
d2e4a39e 3866 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3867
3868 case BINOP_EQUAL:
3869 case BINOP_NOTEQUAL:
3870 case BINOP_LESS:
3871 case BINOP_GTR:
3872 case BINOP_LEQ:
3873 case BINOP_GEQ:
d2e4a39e 3874 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
3875
3876 case BINOP_CONCAT:
ee90b9ab 3877 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
3878
3879 case BINOP_EXP:
d2e4a39e 3880 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3881
3882 case UNOP_NEG:
3883 case UNOP_PLUS:
3884 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
3885 case UNOP_ABS:
3886 return (!numeric_type_p (type0));
14f9c5c9
AS
3887
3888 }
3889}
3890\f
dda83cd7 3891 /* Renaming */
14f9c5c9 3892
aeb5907d
JB
3893/* NOTES:
3894
3895 1. In the following, we assume that a renaming type's name may
3896 have an ___XD suffix. It would be nice if this went away at some
3897 point.
3898 2. We handle both the (old) purely type-based representation of
3899 renamings and the (new) variable-based encoding. At some point,
3900 it is devoutly to be hoped that the former goes away
3901 (FIXME: hilfinger-2007-07-09).
3902 3. Subprogram renamings are not implemented, although the XRS
3903 suffix is recognized (FIXME: hilfinger-2007-07-09). */
3904
3905/* If SYM encodes a renaming,
3906
3907 <renaming> renames <renamed entity>,
3908
3909 sets *LEN to the length of the renamed entity's name,
3910 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
3911 the string describing the subcomponent selected from the renamed
0963b4bd 3912 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
3913 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
3914 are undefined). Otherwise, returns a value indicating the category
3915 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
3916 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
3917 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
3918 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
3919 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
3920 may be NULL, in which case they are not assigned.
3921
3922 [Currently, however, GCC does not generate subprogram renamings.] */
3923
3924enum ada_renaming_category
3925ada_parse_renaming (struct symbol *sym,
3926 const char **renamed_entity, int *len,
3927 const char **renaming_expr)
3928{
3929 enum ada_renaming_category kind;
3930 const char *info;
3931 const char *suffix;
3932
3933 if (sym == NULL)
3934 return ADA_NOT_RENAMING;
3935 switch (SYMBOL_CLASS (sym))
14f9c5c9 3936 {
aeb5907d
JB
3937 default:
3938 return ADA_NOT_RENAMING;
aeb5907d
JB
3939 case LOC_LOCAL:
3940 case LOC_STATIC:
3941 case LOC_COMPUTED:
3942 case LOC_OPTIMIZED_OUT:
987012b8 3943 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
3944 if (info == NULL)
3945 return ADA_NOT_RENAMING;
3946 switch (info[5])
3947 {
3948 case '_':
3949 kind = ADA_OBJECT_RENAMING;
3950 info += 6;
3951 break;
3952 case 'E':
3953 kind = ADA_EXCEPTION_RENAMING;
3954 info += 7;
3955 break;
3956 case 'P':
3957 kind = ADA_PACKAGE_RENAMING;
3958 info += 7;
3959 break;
3960 case 'S':
3961 kind = ADA_SUBPROGRAM_RENAMING;
3962 info += 7;
3963 break;
3964 default:
3965 return ADA_NOT_RENAMING;
3966 }
14f9c5c9 3967 }
4c4b4cd2 3968
de93309a
SM
3969 if (renamed_entity != NULL)
3970 *renamed_entity = info;
3971 suffix = strstr (info, "___XE");
3972 if (suffix == NULL || suffix == info)
3973 return ADA_NOT_RENAMING;
3974 if (len != NULL)
3975 *len = strlen (info) - strlen (suffix);
3976 suffix += 5;
3977 if (renaming_expr != NULL)
3978 *renaming_expr = suffix;
3979 return kind;
3980}
3981
3982/* Compute the value of the given RENAMING_SYM, which is expected to
3983 be a symbol encoding a renaming expression. BLOCK is the block
3984 used to evaluate the renaming. */
3985
3986static struct value *
3987ada_read_renaming_var_value (struct symbol *renaming_sym,
3988 const struct block *block)
3989{
3990 const char *sym_name;
3991
987012b8 3992 sym_name = renaming_sym->linkage_name ();
de93309a
SM
3993 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
3994 return evaluate_expression (expr.get ());
3995}
3996\f
3997
dda83cd7 3998 /* Evaluation: Function Calls */
de93309a
SM
3999
4000/* Return an lvalue containing the value VAL. This is the identity on
4001 lvalues, and otherwise has the side-effect of allocating memory
4002 in the inferior where a copy of the value contents is copied. */
4003
4004static struct value *
4005ensure_lval (struct value *val)
4006{
4007 if (VALUE_LVAL (val) == not_lval
4008 || VALUE_LVAL (val) == lval_internalvar)
4009 {
4010 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
4011 const CORE_ADDR addr =
dda83cd7 4012 value_as_long (value_allocate_space_in_inferior (len));
de93309a
SM
4013
4014 VALUE_LVAL (val) = lval_memory;
4015 set_value_address (val, addr);
50888e42 4016 write_memory (addr, value_contents (val).data (), len);
de93309a
SM
4017 }
4018
4019 return val;
4020}
4021
4022/* Given ARG, a value of type (pointer or reference to a)*
4023 structure/union, extract the component named NAME from the ultimate
4024 target structure/union and return it as a value with its
4025 appropriate type.
4026
4027 The routine searches for NAME among all members of the structure itself
4028 and (recursively) among all members of any wrapper members
4029 (e.g., '_parent').
4030
4031 If NO_ERR, then simply return NULL in case of error, rather than
4032 calling error. */
4033
4034static struct value *
4035ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4036{
4037 struct type *t, *t1;
4038 struct value *v;
4039 int check_tag;
4040
4041 v = NULL;
4042 t1 = t = ada_check_typedef (value_type (arg));
78134374 4043 if (t->code () == TYPE_CODE_REF)
de93309a
SM
4044 {
4045 t1 = TYPE_TARGET_TYPE (t);
4046 if (t1 == NULL)
4047 goto BadValue;
4048 t1 = ada_check_typedef (t1);
78134374 4049 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4050 {
4051 arg = coerce_ref (arg);
4052 t = t1;
4053 }
de93309a
SM
4054 }
4055
78134374 4056 while (t->code () == TYPE_CODE_PTR)
de93309a
SM
4057 {
4058 t1 = TYPE_TARGET_TYPE (t);
4059 if (t1 == NULL)
4060 goto BadValue;
4061 t1 = ada_check_typedef (t1);
78134374 4062 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4063 {
4064 arg = value_ind (arg);
4065 t = t1;
4066 }
de93309a 4067 else
dda83cd7 4068 break;
de93309a 4069 }
aeb5907d 4070
78134374 4071 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4072 goto BadValue;
52ce6436 4073
de93309a
SM
4074 if (t1 == t)
4075 v = ada_search_struct_field (name, arg, 0, t);
4076 else
4077 {
4078 int bit_offset, bit_size, byte_offset;
4079 struct type *field_type;
4080 CORE_ADDR address;
a5ee536b 4081
78134374 4082 if (t->code () == TYPE_CODE_PTR)
de93309a
SM
4083 address = value_address (ada_value_ind (arg));
4084 else
4085 address = value_address (ada_coerce_ref (arg));
d2e4a39e 4086
de93309a 4087 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4088 the case where the type is a reference to a tagged type, but
4089 we have to be careful to exclude pointers to tagged types.
4090 The latter should be shown as usual (as a pointer), whereas
4091 a reference should mostly be transparent to the user. */
14f9c5c9 4092
de93309a 4093 if (ada_is_tagged_type (t1, 0)
dda83cd7
SM
4094 || (t1->code () == TYPE_CODE_REF
4095 && ada_is_tagged_type (TYPE_TARGET_TYPE (t1), 0)))
4096 {
4097 /* We first try to find the searched field in the current type.
de93309a 4098 If not found then let's look in the fixed type. */
14f9c5c9 4099
dda83cd7 4100 if (!find_struct_field (name, t1, 0,
4d1795ac
TT
4101 nullptr, nullptr, nullptr,
4102 nullptr, nullptr))
de93309a
SM
4103 check_tag = 1;
4104 else
4105 check_tag = 0;
dda83cd7 4106 }
de93309a
SM
4107 else
4108 check_tag = 0;
c3e5cd34 4109
de93309a
SM
4110 /* Convert to fixed type in all cases, so that we have proper
4111 offsets to each field in unconstrained record types. */
4112 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4113 address, NULL, check_tag);
4114
24aa1b02
TT
4115 /* Resolve the dynamic type as well. */
4116 arg = value_from_contents_and_address (t1, nullptr, address);
4117 t1 = value_type (arg);
4118
de93309a 4119 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4120 &field_type, &byte_offset, &bit_offset,
4121 &bit_size, NULL))
4122 {
4123 if (bit_size != 0)
4124 {
4125 if (t->code () == TYPE_CODE_REF)
4126 arg = ada_coerce_ref (arg);
4127 else
4128 arg = ada_value_ind (arg);
4129 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4130 bit_offset, bit_size,
4131 field_type);
4132 }
4133 else
4134 v = value_at_lazy (field_type, address + byte_offset);
4135 }
c3e5cd34 4136 }
14f9c5c9 4137
de93309a
SM
4138 if (v != NULL || no_err)
4139 return v;
4140 else
4141 error (_("There is no member named %s."), name);
4142
4143 BadValue:
4144 if (no_err)
4145 return NULL;
4146 else
4147 error (_("Attempt to extract a component of "
4148 "a value that is not a record."));
14f9c5c9
AS
4149}
4150
4151/* Return the value ACTUAL, converted to be an appropriate value for a
4152 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4153 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4154 values not residing in memory, updating it as needed. */
14f9c5c9 4155
a93c0eb6 4156struct value *
40bc484c 4157ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4158{
df407dfe 4159 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 4160 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4161 struct type *formal_target =
78134374 4162 formal_type->code () == TYPE_CODE_PTR
61ee279c 4163 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e 4164 struct type *actual_target =
78134374 4165 actual_type->code () == TYPE_CODE_PTR
61ee279c 4166 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 4167
4c4b4cd2 4168 if (ada_is_array_descriptor_type (formal_target)
78134374 4169 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4170 return make_array_descriptor (formal_type, actual);
78134374
SM
4171 else if (formal_type->code () == TYPE_CODE_PTR
4172 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4173 {
a84a8a0d 4174 struct value *result;
5b4ee69b 4175
78134374 4176 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4177 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4178 result = desc_data (actual);
78134374 4179 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7
SM
4180 {
4181 if (VALUE_LVAL (actual) != lval_memory)
4182 {
4183 struct value *val;
4184
4185 actual_type = ada_check_typedef (value_type (actual));
4186 val = allocate_value (actual_type);
50888e42
SM
4187 memcpy ((char *) value_contents_raw (val).data (),
4188 (char *) value_contents (actual).data (),
dda83cd7
SM
4189 TYPE_LENGTH (actual_type));
4190 actual = ensure_lval (val);
4191 }
4192 result = value_addr (actual);
4193 }
a84a8a0d
JB
4194 else
4195 return actual;
b1af9e97 4196 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4197 }
78134374 4198 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4199 return ada_value_ind (actual);
8344af1e
JB
4200 else if (ada_is_aligner_type (formal_type))
4201 {
4202 /* We need to turn this parameter into an aligner type
4203 as well. */
4204 struct value *aligner = allocate_value (formal_type);
4205 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4206
4207 value_assign_to_component (aligner, component, actual);
4208 return aligner;
4209 }
14f9c5c9
AS
4210
4211 return actual;
4212}
4213
438c98a1
JB
4214/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4215 type TYPE. This is usually an inefficient no-op except on some targets
4216 (such as AVR) where the representation of a pointer and an address
4217 differs. */
4218
4219static CORE_ADDR
4220value_pointer (struct value *value, struct type *type)
4221{
438c98a1 4222 unsigned len = TYPE_LENGTH (type);
224c3ddb 4223 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4224 CORE_ADDR addr;
4225
4226 addr = value_address (value);
8ee511af 4227 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4228 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4229 return addr;
4230}
4231
14f9c5c9 4232
4c4b4cd2
PH
4233/* Push a descriptor of type TYPE for array value ARR on the stack at
4234 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4235 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4236 to-descriptor type rather than a descriptor type), a struct value *
4237 representing a pointer to this descriptor. */
14f9c5c9 4238
d2e4a39e 4239static struct value *
40bc484c 4240make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4241{
d2e4a39e
AS
4242 struct type *bounds_type = desc_bounds_type (type);
4243 struct type *desc_type = desc_base_type (type);
4244 struct value *descriptor = allocate_value (desc_type);
4245 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4246 int i;
d2e4a39e 4247
0963b4bd
MS
4248 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4249 i > 0; i -= 1)
14f9c5c9 4250 {
50888e42
SM
4251 modify_field (value_type (bounds),
4252 value_contents_writeable (bounds).data (),
19f220c3
JK
4253 ada_array_bound (arr, i, 0),
4254 desc_bound_bitpos (bounds_type, i, 0),
4255 desc_bound_bitsize (bounds_type, i, 0));
50888e42
SM
4256 modify_field (value_type (bounds),
4257 value_contents_writeable (bounds).data (),
19f220c3
JK
4258 ada_array_bound (arr, i, 1),
4259 desc_bound_bitpos (bounds_type, i, 1),
4260 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4261 }
d2e4a39e 4262
40bc484c 4263 bounds = ensure_lval (bounds);
d2e4a39e 4264
19f220c3 4265 modify_field (value_type (descriptor),
50888e42 4266 value_contents_writeable (descriptor).data (),
19f220c3 4267 value_pointer (ensure_lval (arr),
940da03e 4268 desc_type->field (0).type ()),
19f220c3
JK
4269 fat_pntr_data_bitpos (desc_type),
4270 fat_pntr_data_bitsize (desc_type));
4271
4272 modify_field (value_type (descriptor),
50888e42 4273 value_contents_writeable (descriptor).data (),
19f220c3 4274 value_pointer (bounds,
940da03e 4275 desc_type->field (1).type ()),
19f220c3
JK
4276 fat_pntr_bounds_bitpos (desc_type),
4277 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4278
40bc484c 4279 descriptor = ensure_lval (descriptor);
14f9c5c9 4280
78134374 4281 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4282 return value_addr (descriptor);
4283 else
4284 return descriptor;
4285}
14f9c5c9 4286\f
dda83cd7 4287 /* Symbol Cache Module */
3d9434b5 4288
3d9434b5 4289/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4290 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4291 on the type of entity being printed, the cache can make it as much
4292 as an order of magnitude faster than without it.
4293
4294 The descriptive type DWARF extension has significantly reduced
4295 the need for this cache, at least when DWARF is being used. However,
4296 even in this case, some expensive name-based symbol searches are still
4297 sometimes necessary - to find an XVZ variable, mostly. */
4298
ee01b665
JB
4299/* Return the symbol cache associated to the given program space PSPACE.
4300 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4301
ee01b665
JB
4302static struct ada_symbol_cache *
4303ada_get_symbol_cache (struct program_space *pspace)
4304{
4305 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4306
bdcccc56
TT
4307 if (pspace_data->sym_cache == nullptr)
4308 pspace_data->sym_cache.reset (new ada_symbol_cache);
ee01b665 4309
bdcccc56 4310 return pspace_data->sym_cache.get ();
ee01b665 4311}
3d9434b5
JB
4312
4313/* Clear all entries from the symbol cache. */
4314
4315static void
bdcccc56 4316ada_clear_symbol_cache ()
3d9434b5 4317{
bdcccc56
TT
4318 struct ada_pspace_data *pspace_data
4319 = get_ada_pspace_data (current_program_space);
ee01b665 4320
bdcccc56
TT
4321 if (pspace_data->sym_cache != nullptr)
4322 pspace_data->sym_cache.reset ();
3d9434b5
JB
4323}
4324
fe978cb0 4325/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4326 Return it if found, or NULL otherwise. */
4327
4328static struct cache_entry **
fe978cb0 4329find_entry (const char *name, domain_enum domain)
3d9434b5 4330{
ee01b665
JB
4331 struct ada_symbol_cache *sym_cache
4332 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4333 int h = msymbol_hash (name) % HASH_SIZE;
4334 struct cache_entry **e;
4335
ee01b665 4336 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4337 {
fe978cb0 4338 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
dda83cd7 4339 return e;
3d9434b5
JB
4340 }
4341 return NULL;
4342}
4343
fe978cb0 4344/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4345 Return 1 if found, 0 otherwise.
4346
4347 If an entry was found and SYM is not NULL, set *SYM to the entry's
4348 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4349
96d887e8 4350static int
fe978cb0 4351lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4352 struct symbol **sym, const struct block **block)
96d887e8 4353{
fe978cb0 4354 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4355
4356 if (e == NULL)
4357 return 0;
4358 if (sym != NULL)
4359 *sym = (*e)->sym;
4360 if (block != NULL)
4361 *block = (*e)->block;
4362 return 1;
96d887e8
PH
4363}
4364
3d9434b5 4365/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4366 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4367
96d887e8 4368static void
fe978cb0 4369cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4370 const struct block *block)
96d887e8 4371{
ee01b665
JB
4372 struct ada_symbol_cache *sym_cache
4373 = ada_get_symbol_cache (current_program_space);
3d9434b5 4374 int h;
3d9434b5
JB
4375 struct cache_entry *e;
4376
1994afbf
DE
4377 /* Symbols for builtin types don't have a block.
4378 For now don't cache such symbols. */
4379 if (sym != NULL && !SYMBOL_OBJFILE_OWNED (sym))
4380 return;
4381
3d9434b5
JB
4382 /* If the symbol is a local symbol, then do not cache it, as a search
4383 for that symbol depends on the context. To determine whether
4384 the symbol is local or not, we check the block where we found it
4385 against the global and static blocks of its associated symtab. */
4386 if (sym
08be3fe3 4387 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4388 GLOBAL_BLOCK) != block
08be3fe3 4389 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4390 STATIC_BLOCK) != block)
3d9434b5
JB
4391 return;
4392
4393 h = msymbol_hash (name) % HASH_SIZE;
e39db4db 4394 e = XOBNEW (&sym_cache->cache_space, cache_entry);
ee01b665
JB
4395 e->next = sym_cache->root[h];
4396 sym_cache->root[h] = e;
2ef5453b 4397 e->name = obstack_strdup (&sym_cache->cache_space, name);
3d9434b5 4398 e->sym = sym;
fe978cb0 4399 e->domain = domain;
3d9434b5 4400 e->block = block;
96d887e8 4401}
4c4b4cd2 4402\f
dda83cd7 4403 /* Symbol Lookup */
4c4b4cd2 4404
b5ec771e
PA
4405/* Return the symbol name match type that should be used used when
4406 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4407
4408 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4409 for Ada lookups. */
c0431670 4410
b5ec771e
PA
4411static symbol_name_match_type
4412name_match_type_from_name (const char *lookup_name)
c0431670 4413{
b5ec771e
PA
4414 return (strstr (lookup_name, "__") == NULL
4415 ? symbol_name_match_type::WILD
4416 : symbol_name_match_type::FULL);
c0431670
JB
4417}
4418
4c4b4cd2
PH
4419/* Return the result of a standard (literal, C-like) lookup of NAME in
4420 given DOMAIN, visible from lexical block BLOCK. */
4421
4422static struct symbol *
4423standard_lookup (const char *name, const struct block *block,
dda83cd7 4424 domain_enum domain)
4c4b4cd2 4425{
acbd605d 4426 /* Initialize it just to avoid a GCC false warning. */
6640a367 4427 struct block_symbol sym = {};
4c4b4cd2 4428
d12307c1
PMR
4429 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4430 return sym.symbol;
a2cd4f14 4431 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4432 cache_symbol (name, domain, sym.symbol, sym.block);
4433 return sym.symbol;
4c4b4cd2
PH
4434}
4435
4436
4437/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4438 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4439 since they contend in overloading in the same way. */
4440static int
d1183b06 4441is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4442{
d1183b06
TT
4443 for (const block_symbol &sym : syms)
4444 if (SYMBOL_TYPE (sym.symbol)->code () != TYPE_CODE_FUNC
4445 && (SYMBOL_TYPE (sym.symbol)->code () != TYPE_CODE_ENUM
4446 || SYMBOL_CLASS (sym.symbol) != LOC_CONST))
14f9c5c9
AS
4447 return 1;
4448
4449 return 0;
4450}
4451
4452/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4453 struct types. Otherwise, they may not. */
14f9c5c9
AS
4454
4455static int
d2e4a39e 4456equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4457{
d2e4a39e 4458 if (type0 == type1)
14f9c5c9 4459 return 1;
d2e4a39e 4460 if (type0 == NULL || type1 == NULL
78134374 4461 || type0->code () != type1->code ())
14f9c5c9 4462 return 0;
78134374
SM
4463 if ((type0->code () == TYPE_CODE_STRUCT
4464 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4465 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4466 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4467 return 1;
d2e4a39e 4468
14f9c5c9
AS
4469 return 0;
4470}
4471
4472/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4473 no more defined than that of SYM1. */
14f9c5c9
AS
4474
4475static int
d2e4a39e 4476lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4477{
4478 if (sym0 == sym1)
4479 return 1;
176620f1 4480 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
4481 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4482 return 0;
4483
d2e4a39e 4484 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
4485 {
4486 case LOC_UNDEF:
4487 return 1;
4488 case LOC_TYPEDEF:
4489 {
dda83cd7
SM
4490 struct type *type0 = SYMBOL_TYPE (sym0);
4491 struct type *type1 = SYMBOL_TYPE (sym1);
4492 const char *name0 = sym0->linkage_name ();
4493 const char *name1 = sym1->linkage_name ();
4494 int len0 = strlen (name0);
4495
4496 return
4497 type0->code () == type1->code ()
4498 && (equiv_types (type0, type1)
4499 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4500 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4501 }
4502 case LOC_CONST:
4503 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
dda83cd7 4504 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
4b610737
TT
4505
4506 case LOC_STATIC:
4507 {
dda83cd7
SM
4508 const char *name0 = sym0->linkage_name ();
4509 const char *name1 = sym1->linkage_name ();
4510 return (strcmp (name0, name1) == 0
4511 && SYMBOL_VALUE_ADDRESS (sym0) == SYMBOL_VALUE_ADDRESS (sym1));
4b610737
TT
4512 }
4513
d2e4a39e
AS
4514 default:
4515 return 0;
14f9c5c9
AS
4516 }
4517}
4518
d1183b06
TT
4519/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4520 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4521
4522static void
d1183b06 4523add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4524 struct symbol *sym,
4525 const struct block *block)
14f9c5c9 4526{
529cad9c
PH
4527 /* Do not try to complete stub types, as the debugger is probably
4528 already scanning all symbols matching a certain name at the
4529 time when this function is called. Trying to replace the stub
4530 type by its associated full type will cause us to restart a scan
4531 which may lead to an infinite recursion. Instead, the client
4532 collecting the matching symbols will end up collecting several
4533 matches, with at least one of them complete. It can then filter
4534 out the stub ones if needed. */
4535
d1183b06 4536 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4537 {
d1183b06 4538 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4539 return;
d1183b06 4540 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4541 {
d1183b06
TT
4542 result[i].symbol = sym;
4543 result[i].block = block;
dda83cd7
SM
4544 return;
4545 }
4c4b4cd2
PH
4546 }
4547
d1183b06
TT
4548 struct block_symbol info;
4549 info.symbol = sym;
4550 info.block = block;
4551 result.push_back (info);
4c4b4cd2
PH
4552}
4553
7c7b6655
TT
4554/* Return a bound minimal symbol matching NAME according to Ada
4555 decoding rules. Returns an invalid symbol if there is no such
4556 minimal symbol. Names prefixed with "standard__" are handled
4557 specially: "standard__" is first stripped off, and only static and
4558 global symbols are searched. */
4c4b4cd2 4559
7c7b6655 4560struct bound_minimal_symbol
96d887e8 4561ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4562{
7c7b6655 4563 struct bound_minimal_symbol result;
4c4b4cd2 4564
7c7b6655
TT
4565 memset (&result, 0, sizeof (result));
4566
b5ec771e
PA
4567 symbol_name_match_type match_type = name_match_type_from_name (name);
4568 lookup_name_info lookup_name (name, match_type);
4569
4570 symbol_name_matcher_ftype *match_name
4571 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4572
2030c079 4573 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4574 {
7932255d 4575 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf 4576 {
c9d95fa3 4577 if (match_name (msymbol->linkage_name (), lookup_name, NULL)
5325b9bf
TT
4578 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4579 {
4580 result.minsym = msymbol;
4581 result.objfile = objfile;
4582 break;
4583 }
4584 }
4585 }
4c4b4cd2 4586
7c7b6655 4587 return result;
96d887e8 4588}
4c4b4cd2 4589
96d887e8
PH
4590/* True if TYPE is definitely an artificial type supplied to a symbol
4591 for which no debugging information was given in the symbol file. */
14f9c5c9 4592
96d887e8
PH
4593static int
4594is_nondebugging_type (struct type *type)
4595{
0d5cff50 4596 const char *name = ada_type_name (type);
5b4ee69b 4597
96d887e8
PH
4598 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4599}
4c4b4cd2 4600
8f17729f
JB
4601/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4602 that are deemed "identical" for practical purposes.
4603
4604 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4605 types and that their number of enumerals is identical (in other
1f704f76 4606 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4607
4608static int
4609ada_identical_enum_types_p (struct type *type1, struct type *type2)
4610{
4611 int i;
4612
4613 /* The heuristic we use here is fairly conservative. We consider
4614 that 2 enumerate types are identical if they have the same
4615 number of enumerals and that all enumerals have the same
4616 underlying value and name. */
4617
4618 /* All enums in the type should have an identical underlying value. */
1f704f76 4619 for (i = 0; i < type1->num_fields (); i++)
14e75d8e 4620 if (TYPE_FIELD_ENUMVAL (type1, i) != TYPE_FIELD_ENUMVAL (type2, i))
8f17729f
JB
4621 return 0;
4622
4623 /* All enumerals should also have the same name (modulo any numerical
4624 suffix). */
1f704f76 4625 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4626 {
33d16dd9
SM
4627 const char *name_1 = type1->field (i).name ();
4628 const char *name_2 = type2->field (i).name ();
8f17729f
JB
4629 int len_1 = strlen (name_1);
4630 int len_2 = strlen (name_2);
4631
33d16dd9
SM
4632 ada_remove_trailing_digits (type1->field (i).name (), &len_1);
4633 ada_remove_trailing_digits (type2->field (i).name (), &len_2);
8f17729f 4634 if (len_1 != len_2
33d16dd9
SM
4635 || strncmp (type1->field (i).name (),
4636 type2->field (i).name (),
8f17729f
JB
4637 len_1) != 0)
4638 return 0;
4639 }
4640
4641 return 1;
4642}
4643
4644/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4645 that are deemed "identical" for practical purposes. Sometimes,
4646 enumerals are not strictly identical, but their types are so similar
4647 that they can be considered identical.
4648
4649 For instance, consider the following code:
4650
4651 type Color is (Black, Red, Green, Blue, White);
4652 type RGB_Color is new Color range Red .. Blue;
4653
4654 Type RGB_Color is a subrange of an implicit type which is a copy
4655 of type Color. If we call that implicit type RGB_ColorB ("B" is
4656 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4657 As a result, when an expression references any of the enumeral
4658 by name (Eg. "print green"), the expression is technically
4659 ambiguous and the user should be asked to disambiguate. But
4660 doing so would only hinder the user, since it wouldn't matter
4661 what choice he makes, the outcome would always be the same.
4662 So, for practical purposes, we consider them as the same. */
4663
4664static int
54d343a2 4665symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4666{
4667 int i;
4668
4669 /* Before performing a thorough comparison check of each type,
4670 we perform a series of inexpensive checks. We expect that these
4671 checks will quickly fail in the vast majority of cases, and thus
4672 help prevent the unnecessary use of a more expensive comparison.
4673 Said comparison also expects us to make some of these checks
4674 (see ada_identical_enum_types_p). */
4675
4676 /* Quick check: All symbols should have an enum type. */
54d343a2 4677 for (i = 0; i < syms.size (); i++)
78134374 4678 if (SYMBOL_TYPE (syms[i].symbol)->code () != TYPE_CODE_ENUM)
8f17729f
JB
4679 return 0;
4680
4681 /* Quick check: They should all have the same value. */
54d343a2 4682 for (i = 1; i < syms.size (); i++)
d12307c1 4683 if (SYMBOL_VALUE (syms[i].symbol) != SYMBOL_VALUE (syms[0].symbol))
8f17729f
JB
4684 return 0;
4685
4686 /* Quick check: They should all have the same number of enumerals. */
54d343a2 4687 for (i = 1; i < syms.size (); i++)
1f704f76 4688 if (SYMBOL_TYPE (syms[i].symbol)->num_fields ()
dda83cd7 4689 != SYMBOL_TYPE (syms[0].symbol)->num_fields ())
8f17729f
JB
4690 return 0;
4691
4692 /* All the sanity checks passed, so we might have a set of
4693 identical enumeration types. Perform a more complete
4694 comparison of the type of each symbol. */
54d343a2 4695 for (i = 1; i < syms.size (); i++)
d12307c1 4696 if (!ada_identical_enum_types_p (SYMBOL_TYPE (syms[i].symbol),
dda83cd7 4697 SYMBOL_TYPE (syms[0].symbol)))
8f17729f
JB
4698 return 0;
4699
4700 return 1;
4701}
4702
54d343a2 4703/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
4704 duplicate other symbols in the list (The only case I know of where
4705 this happens is when object files containing stabs-in-ecoff are
4706 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 4707 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 4708
d1183b06 4709static void
54d343a2 4710remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
4711{
4712 int i, j;
4c4b4cd2 4713
8f17729f
JB
4714 /* We should never be called with less than 2 symbols, as there
4715 cannot be any extra symbol in that case. But it's easy to
4716 handle, since we have nothing to do in that case. */
54d343a2 4717 if (syms->size () < 2)
d1183b06 4718 return;
8f17729f 4719
96d887e8 4720 i = 0;
54d343a2 4721 while (i < syms->size ())
96d887e8 4722 {
a35ddb44 4723 int remove_p = 0;
339c13b6
JB
4724
4725 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 4726 the get rid of the stub. */
339c13b6 4727
e46d3488 4728 if (SYMBOL_TYPE ((*syms)[i].symbol)->is_stub ()
dda83cd7
SM
4729 && (*syms)[i].symbol->linkage_name () != NULL)
4730 {
4731 for (j = 0; j < syms->size (); j++)
4732 {
4733 if (j != i
4734 && !SYMBOL_TYPE ((*syms)[j].symbol)->is_stub ()
4735 && (*syms)[j].symbol->linkage_name () != NULL
4736 && strcmp ((*syms)[i].symbol->linkage_name (),
4737 (*syms)[j].symbol->linkage_name ()) == 0)
4738 remove_p = 1;
4739 }
4740 }
339c13b6
JB
4741
4742 /* Two symbols with the same name, same class and same address
dda83cd7 4743 should be identical. */
339c13b6 4744
987012b8 4745 else if ((*syms)[i].symbol->linkage_name () != NULL
dda83cd7
SM
4746 && SYMBOL_CLASS ((*syms)[i].symbol) == LOC_STATIC
4747 && is_nondebugging_type (SYMBOL_TYPE ((*syms)[i].symbol)))
4748 {
4749 for (j = 0; j < syms->size (); j += 1)
4750 {
4751 if (i != j
4752 && (*syms)[j].symbol->linkage_name () != NULL
4753 && strcmp ((*syms)[i].symbol->linkage_name (),
4754 (*syms)[j].symbol->linkage_name ()) == 0
4755 && SYMBOL_CLASS ((*syms)[i].symbol)
54d343a2 4756 == SYMBOL_CLASS ((*syms)[j].symbol)
dda83cd7
SM
4757 && SYMBOL_VALUE_ADDRESS ((*syms)[i].symbol)
4758 == SYMBOL_VALUE_ADDRESS ((*syms)[j].symbol))
4759 remove_p = 1;
4760 }
4761 }
339c13b6 4762
a35ddb44 4763 if (remove_p)
54d343a2 4764 syms->erase (syms->begin () + i);
1b788fb6
TT
4765 else
4766 i += 1;
14f9c5c9 4767 }
8f17729f
JB
4768
4769 /* If all the remaining symbols are identical enumerals, then
4770 just keep the first one and discard the rest.
4771
4772 Unlike what we did previously, we do not discard any entry
4773 unless they are ALL identical. This is because the symbol
4774 comparison is not a strict comparison, but rather a practical
4775 comparison. If all symbols are considered identical, then
4776 we can just go ahead and use the first one and discard the rest.
4777 But if we cannot reduce the list to a single element, we have
4778 to ask the user to disambiguate anyways. And if we have to
4779 present a multiple-choice menu, it's less confusing if the list
4780 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
4781 if (symbols_are_identical_enums (*syms))
4782 syms->resize (1);
14f9c5c9
AS
4783}
4784
96d887e8
PH
4785/* Given a type that corresponds to a renaming entity, use the type name
4786 to extract the scope (package name or function name, fully qualified,
4787 and following the GNAT encoding convention) where this renaming has been
49d83361 4788 defined. */
4c4b4cd2 4789
49d83361 4790static std::string
96d887e8 4791xget_renaming_scope (struct type *renaming_type)
14f9c5c9 4792{
96d887e8 4793 /* The renaming types adhere to the following convention:
0963b4bd 4794 <scope>__<rename>___<XR extension>.
96d887e8
PH
4795 So, to extract the scope, we search for the "___XR" extension,
4796 and then backtrack until we find the first "__". */
76a01679 4797
7d93a1e0 4798 const char *name = renaming_type->name ();
108d56a4
SM
4799 const char *suffix = strstr (name, "___XR");
4800 const char *last;
14f9c5c9 4801
96d887e8
PH
4802 /* Now, backtrack a bit until we find the first "__". Start looking
4803 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 4804
96d887e8
PH
4805 for (last = suffix - 3; last > name; last--)
4806 if (last[0] == '_' && last[1] == '_')
4807 break;
76a01679 4808
96d887e8 4809 /* Make a copy of scope and return it. */
49d83361 4810 return std::string (name, last);
4c4b4cd2
PH
4811}
4812
96d887e8 4813/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 4814
96d887e8
PH
4815static int
4816is_package_name (const char *name)
4c4b4cd2 4817{
96d887e8
PH
4818 /* Here, We take advantage of the fact that no symbols are generated
4819 for packages, while symbols are generated for each function.
4820 So the condition for NAME represent a package becomes equivalent
4821 to NAME not existing in our list of symbols. There is only one
4822 small complication with library-level functions (see below). */
4c4b4cd2 4823
96d887e8
PH
4824 /* If it is a function that has not been defined at library level,
4825 then we should be able to look it up in the symbols. */
4826 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4827 return 0;
14f9c5c9 4828
96d887e8
PH
4829 /* Library-level function names start with "_ada_". See if function
4830 "_ada_" followed by NAME can be found. */
14f9c5c9 4831
96d887e8 4832 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 4833 functions names cannot contain "__" in them. */
96d887e8
PH
4834 if (strstr (name, "__") != NULL)
4835 return 0;
4c4b4cd2 4836
528e1572 4837 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 4838
528e1572 4839 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 4840}
14f9c5c9 4841
96d887e8 4842/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 4843 not visible from FUNCTION_NAME. */
14f9c5c9 4844
96d887e8 4845static int
0d5cff50 4846old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 4847{
aeb5907d
JB
4848 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
4849 return 0;
4850
49d83361 4851 std::string scope = xget_renaming_scope (SYMBOL_TYPE (sym));
14f9c5c9 4852
96d887e8 4853 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
4854 if (is_package_name (scope.c_str ()))
4855 return 0;
14f9c5c9 4856
96d887e8
PH
4857 /* Check that the rename is in the current function scope by checking
4858 that its name starts with SCOPE. */
76a01679 4859
96d887e8
PH
4860 /* If the function name starts with "_ada_", it means that it is
4861 a library-level function. Strip this prefix before doing the
4862 comparison, as the encoding for the renaming does not contain
4863 this prefix. */
61012eef 4864 if (startswith (function_name, "_ada_"))
96d887e8 4865 function_name += 5;
f26caa11 4866
49d83361 4867 return !startswith (function_name, scope.c_str ());
f26caa11
PH
4868}
4869
aeb5907d
JB
4870/* Remove entries from SYMS that corresponds to a renaming entity that
4871 is not visible from the function associated with CURRENT_BLOCK or
4872 that is superfluous due to the presence of more specific renaming
4873 information. Places surviving symbols in the initial entries of
d1183b06
TT
4874 SYMS.
4875
96d887e8 4876 Rationale:
aeb5907d
JB
4877 First, in cases where an object renaming is implemented as a
4878 reference variable, GNAT may produce both the actual reference
4879 variable and the renaming encoding. In this case, we discard the
4880 latter.
4881
4882 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
4883 entity. Unfortunately, STABS currently does not support the definition
4884 of types that are local to a given lexical block, so all renamings types
4885 are emitted at library level. As a consequence, if an application
4886 contains two renaming entities using the same name, and a user tries to
4887 print the value of one of these entities, the result of the ada symbol
4888 lookup will also contain the wrong renaming type.
f26caa11 4889
96d887e8
PH
4890 This function partially covers for this limitation by attempting to
4891 remove from the SYMS list renaming symbols that should be visible
4892 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4893 method with the current information available. The implementation
4894 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4895
4896 - When the user tries to print a rename in a function while there
dda83cd7
SM
4897 is another rename entity defined in a package: Normally, the
4898 rename in the function has precedence over the rename in the
4899 package, so the latter should be removed from the list. This is
4900 currently not the case.
4901
96d887e8 4902 - This function will incorrectly remove valid renames if
dda83cd7
SM
4903 the CURRENT_BLOCK corresponds to a function which symbol name
4904 has been changed by an "Export" pragma. As a consequence,
4905 the user will be unable to print such rename entities. */
4c4b4cd2 4906
d1183b06 4907static void
54d343a2
TT
4908remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
4909 const struct block *current_block)
4c4b4cd2
PH
4910{
4911 struct symbol *current_function;
0d5cff50 4912 const char *current_function_name;
4c4b4cd2 4913 int i;
aeb5907d
JB
4914 int is_new_style_renaming;
4915
4916 /* If there is both a renaming foo___XR... encoded as a variable and
4917 a simple variable foo in the same block, discard the latter.
0963b4bd 4918 First, zero out such symbols, then compress. */
aeb5907d 4919 is_new_style_renaming = 0;
54d343a2 4920 for (i = 0; i < syms->size (); i += 1)
aeb5907d 4921 {
54d343a2
TT
4922 struct symbol *sym = (*syms)[i].symbol;
4923 const struct block *block = (*syms)[i].block;
aeb5907d
JB
4924 const char *name;
4925 const char *suffix;
4926
4927 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4928 continue;
987012b8 4929 name = sym->linkage_name ();
aeb5907d
JB
4930 suffix = strstr (name, "___XR");
4931
4932 if (suffix != NULL)
4933 {
4934 int name_len = suffix - name;
4935 int j;
5b4ee69b 4936
aeb5907d 4937 is_new_style_renaming = 1;
54d343a2
TT
4938 for (j = 0; j < syms->size (); j += 1)
4939 if (i != j && (*syms)[j].symbol != NULL
987012b8 4940 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 4941 name_len) == 0
54d343a2
TT
4942 && block == (*syms)[j].block)
4943 (*syms)[j].symbol = NULL;
aeb5907d
JB
4944 }
4945 }
4946 if (is_new_style_renaming)
4947 {
4948 int j, k;
4949
54d343a2
TT
4950 for (j = k = 0; j < syms->size (); j += 1)
4951 if ((*syms)[j].symbol != NULL)
aeb5907d 4952 {
54d343a2 4953 (*syms)[k] = (*syms)[j];
aeb5907d
JB
4954 k += 1;
4955 }
d1183b06
TT
4956 syms->resize (k);
4957 return;
aeb5907d 4958 }
4c4b4cd2
PH
4959
4960 /* Extract the function name associated to CURRENT_BLOCK.
4961 Abort if unable to do so. */
76a01679 4962
4c4b4cd2 4963 if (current_block == NULL)
d1183b06 4964 return;
76a01679 4965
7f0df278 4966 current_function = block_linkage_function (current_block);
4c4b4cd2 4967 if (current_function == NULL)
d1183b06 4968 return;
4c4b4cd2 4969
987012b8 4970 current_function_name = current_function->linkage_name ();
4c4b4cd2 4971 if (current_function_name == NULL)
d1183b06 4972 return;
4c4b4cd2
PH
4973
4974 /* Check each of the symbols, and remove it from the list if it is
4975 a type corresponding to a renaming that is out of the scope of
4976 the current block. */
4977
4978 i = 0;
54d343a2 4979 while (i < syms->size ())
4c4b4cd2 4980 {
54d343a2 4981 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
4982 == ADA_OBJECT_RENAMING
4983 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
4984 current_function_name))
4985 syms->erase (syms->begin () + i);
4c4b4cd2 4986 else
dda83cd7 4987 i += 1;
4c4b4cd2 4988 }
4c4b4cd2
PH
4989}
4990
d1183b06 4991/* Add to RESULT all symbols from BLOCK (and its super-blocks)
cd458349 4992 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
339c13b6 4993
cd458349 4994 Note: This function assumes that RESULT is empty. */
339c13b6
JB
4995
4996static void
d1183b06 4997ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
4998 const lookup_name_info &lookup_name,
4999 const struct block *block, domain_enum domain)
339c13b6 5000{
339c13b6
JB
5001 while (block != NULL)
5002 {
d1183b06 5003 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6 5004
ba8694b6
TT
5005 /* If we found a non-function match, assume that's the one. We
5006 only check this when finding a function boundary, so that we
5007 can accumulate all results from intervening blocks first. */
5008 if (BLOCK_FUNCTION (block) != nullptr && is_nonfunction (result))
dda83cd7 5009 return;
339c13b6
JB
5010
5011 block = BLOCK_SUPERBLOCK (block);
5012 }
339c13b6
JB
5013}
5014
2315bb2d 5015/* An object of this type is used as the callback argument when
40658b94 5016 calling the map_matching_symbols method. */
ccefe4c4 5017
40658b94 5018struct match_data
ccefe4c4 5019{
1bfa81ac
TT
5020 explicit match_data (std::vector<struct block_symbol> *rp)
5021 : resultp (rp)
5022 {
5023 }
5024 DISABLE_COPY_AND_ASSIGN (match_data);
5025
2315bb2d
TT
5026 bool operator() (struct block_symbol *bsym);
5027
1bfa81ac 5028 struct objfile *objfile = nullptr;
d1183b06 5029 std::vector<struct block_symbol> *resultp;
1bfa81ac 5030 struct symbol *arg_sym = nullptr;
1178743e 5031 bool found_sym = false;
ccefe4c4
TT
5032};
5033
2315bb2d
TT
5034/* A callback for add_nonlocal_symbols that adds symbol, found in
5035 BSYM, to a list of symbols. */
ccefe4c4 5036
2315bb2d
TT
5037bool
5038match_data::operator() (struct block_symbol *bsym)
ccefe4c4 5039{
199b4314
TT
5040 const struct block *block = bsym->block;
5041 struct symbol *sym = bsym->symbol;
5042
40658b94
PH
5043 if (sym == NULL)
5044 {
2315bb2d
TT
5045 if (!found_sym && arg_sym != NULL)
5046 add_defn_to_vec (*resultp,
5047 fixup_symbol_section (arg_sym, objfile),
40658b94 5048 block);
2315bb2d
TT
5049 found_sym = false;
5050 arg_sym = NULL;
40658b94
PH
5051 }
5052 else
5053 {
5054 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
199b4314 5055 return true;
40658b94 5056 else if (SYMBOL_IS_ARGUMENT (sym))
2315bb2d 5057 arg_sym = sym;
40658b94
PH
5058 else
5059 {
2315bb2d
TT
5060 found_sym = true;
5061 add_defn_to_vec (*resultp,
5062 fixup_symbol_section (sym, objfile),
40658b94
PH
5063 block);
5064 }
5065 }
199b4314 5066 return true;
40658b94
PH
5067}
5068
b5ec771e
PA
5069/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5070 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5071 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5072
5073static int
d1183b06 5074ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5075 const struct block *block,
b5ec771e
PA
5076 const lookup_name_info &lookup_name,
5077 domain_enum domain)
22cee43f
PMR
5078{
5079 struct using_direct *renaming;
d1183b06 5080 int defns_mark = result.size ();
22cee43f 5081
b5ec771e
PA
5082 symbol_name_matcher_ftype *name_match
5083 = ada_get_symbol_name_matcher (lookup_name);
5084
22cee43f
PMR
5085 for (renaming = block_using (block);
5086 renaming != NULL;
5087 renaming = renaming->next)
5088 {
5089 const char *r_name;
22cee43f
PMR
5090
5091 /* Avoid infinite recursions: skip this renaming if we are actually
5092 already traversing it.
5093
5094 Currently, symbol lookup in Ada don't use the namespace machinery from
5095 C++/Fortran support: skip namespace imports that use them. */
5096 if (renaming->searched
5097 || (renaming->import_src != NULL
5098 && renaming->import_src[0] != '\0')
5099 || (renaming->import_dest != NULL
5100 && renaming->import_dest[0] != '\0'))
5101 continue;
5102 renaming->searched = 1;
5103
5104 /* TODO: here, we perform another name-based symbol lookup, which can
5105 pull its own multiple overloads. In theory, we should be able to do
5106 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5107 not a simple name. But in order to do this, we would need to enhance
5108 the DWARF reader to associate a symbol to this renaming, instead of a
5109 name. So, for now, we do something simpler: re-use the C++/Fortran
5110 namespace machinery. */
5111 r_name = (renaming->alias != NULL
5112 ? renaming->alias
5113 : renaming->declaration);
b5ec771e
PA
5114 if (name_match (r_name, lookup_name, NULL))
5115 {
5116 lookup_name_info decl_lookup_name (renaming->declaration,
5117 lookup_name.match_type ());
d1183b06 5118 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5119 1, NULL);
5120 }
22cee43f
PMR
5121 renaming->searched = 0;
5122 }
d1183b06 5123 return result.size () != defns_mark;
22cee43f
PMR
5124}
5125
db230ce3
JB
5126/* Implements compare_names, but only applying the comparision using
5127 the given CASING. */
5b4ee69b 5128
40658b94 5129static int
db230ce3
JB
5130compare_names_with_case (const char *string1, const char *string2,
5131 enum case_sensitivity casing)
40658b94
PH
5132{
5133 while (*string1 != '\0' && *string2 != '\0')
5134 {
db230ce3
JB
5135 char c1, c2;
5136
40658b94
PH
5137 if (isspace (*string1) || isspace (*string2))
5138 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5139
5140 if (casing == case_sensitive_off)
5141 {
5142 c1 = tolower (*string1);
5143 c2 = tolower (*string2);
5144 }
5145 else
5146 {
5147 c1 = *string1;
5148 c2 = *string2;
5149 }
5150 if (c1 != c2)
40658b94 5151 break;
db230ce3 5152
40658b94
PH
5153 string1 += 1;
5154 string2 += 1;
5155 }
db230ce3 5156
40658b94
PH
5157 switch (*string1)
5158 {
5159 case '(':
5160 return strcmp_iw_ordered (string1, string2);
5161 case '_':
5162 if (*string2 == '\0')
5163 {
052874e8 5164 if (is_name_suffix (string1))
40658b94
PH
5165 return 0;
5166 else
1a1d5513 5167 return 1;
40658b94 5168 }
dbb8534f 5169 /* FALLTHROUGH */
40658b94
PH
5170 default:
5171 if (*string2 == '(')
5172 return strcmp_iw_ordered (string1, string2);
5173 else
db230ce3
JB
5174 {
5175 if (casing == case_sensitive_off)
5176 return tolower (*string1) - tolower (*string2);
5177 else
5178 return *string1 - *string2;
5179 }
40658b94 5180 }
ccefe4c4
TT
5181}
5182
db230ce3
JB
5183/* Compare STRING1 to STRING2, with results as for strcmp.
5184 Compatible with strcmp_iw_ordered in that...
5185
5186 strcmp_iw_ordered (STRING1, STRING2) <= 0
5187
5188 ... implies...
5189
5190 compare_names (STRING1, STRING2) <= 0
5191
5192 (they may differ as to what symbols compare equal). */
5193
5194static int
5195compare_names (const char *string1, const char *string2)
5196{
5197 int result;
5198
5199 /* Similar to what strcmp_iw_ordered does, we need to perform
5200 a case-insensitive comparison first, and only resort to
5201 a second, case-sensitive, comparison if the first one was
5202 not sufficient to differentiate the two strings. */
5203
5204 result = compare_names_with_case (string1, string2, case_sensitive_off);
5205 if (result == 0)
5206 result = compare_names_with_case (string1, string2, case_sensitive_on);
5207
5208 return result;
5209}
5210
b5ec771e
PA
5211/* Convenience function to get at the Ada encoded lookup name for
5212 LOOKUP_NAME, as a C string. */
5213
5214static const char *
5215ada_lookup_name (const lookup_name_info &lookup_name)
5216{
5217 return lookup_name.ada ().lookup_name ().c_str ();
5218}
5219
0b7b2c2a
TT
5220/* A helper for add_nonlocal_symbols. Call expand_matching_symbols
5221 for OBJFILE, then walk the objfile's symtabs and update the
5222 results. */
5223
5224static void
5225map_matching_symbols (struct objfile *objfile,
5226 const lookup_name_info &lookup_name,
5227 bool is_wild_match,
5228 domain_enum domain,
5229 int global,
5230 match_data &data)
5231{
5232 data.objfile = objfile;
5233 objfile->expand_matching_symbols (lookup_name, domain, global,
5234 is_wild_match ? nullptr : compare_names);
5235
5236 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5237 for (compunit_symtab *symtab : objfile->compunits ())
5238 {
5239 const struct block *block
5240 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5241 if (!iterate_over_symbols_terminated (block, lookup_name,
5242 domain, data))
5243 break;
5244 }
5245}
5246
1bfa81ac 5247/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5248 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5249 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5250 symbols otherwise. */
339c13b6
JB
5251
5252static void
d1183b06 5253add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5254 const lookup_name_info &lookup_name,
5255 domain_enum domain, int global)
339c13b6 5256{
1bfa81ac 5257 struct match_data data (&result);
339c13b6 5258
b5ec771e
PA
5259 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5260
2030c079 5261 for (objfile *objfile : current_program_space->objfiles ())
40658b94 5262 {
0b7b2c2a
TT
5263 map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
5264 global, data);
22cee43f 5265
b669c953 5266 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5267 {
5268 const struct block *global_block
5269 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cu), GLOBAL_BLOCK);
5270
d1183b06 5271 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5272 domain))
1178743e 5273 data.found_sym = true;
22cee43f 5274 }
40658b94
PH
5275 }
5276
d1183b06 5277 if (result.empty () && global && !is_wild_match)
40658b94 5278 {
b5ec771e 5279 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5280 std::string bracket_name = std::string ("<_ada_") + name + '>';
5281 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5282
2030c079 5283 for (objfile *objfile : current_program_space->objfiles ())
0b7b2c2a
TT
5284 map_matching_symbols (objfile, name1, false, domain, global, data);
5285 }
339c13b6
JB
5286}
5287
b5ec771e
PA
5288/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5289 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5290 returning the number of matches. Add these to RESULT.
4eeaa230 5291
22cee43f
PMR
5292 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5293 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5294 is the one match returned (no other matches in that or
d9680e73 5295 enclosing blocks is returned). If there are any matches in or
22cee43f 5296 surrounding BLOCK, then these alone are returned.
4eeaa230 5297
b5ec771e
PA
5298 Names prefixed with "standard__" are handled specially:
5299 "standard__" is first stripped off (by the lookup_name
5300 constructor), and only static and global symbols are searched.
14f9c5c9 5301
22cee43f
PMR
5302 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5303 to lookup global symbols. */
5304
5305static void
d1183b06 5306ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5307 const struct block *block,
b5ec771e 5308 const lookup_name_info &lookup_name,
22cee43f
PMR
5309 domain_enum domain,
5310 int full_search,
5311 int *made_global_lookup_p)
14f9c5c9
AS
5312{
5313 struct symbol *sym;
14f9c5c9 5314
22cee43f
PMR
5315 if (made_global_lookup_p)
5316 *made_global_lookup_p = 0;
339c13b6
JB
5317
5318 /* Special case: If the user specifies a symbol name inside package
5319 Standard, do a non-wild matching of the symbol name without
5320 the "standard__" prefix. This was primarily introduced in order
5321 to allow the user to specifically access the standard exceptions
5322 using, for instance, Standard.Constraint_Error when Constraint_Error
5323 is ambiguous (due to the user defining its own Constraint_Error
5324 entity inside its program). */
b5ec771e
PA
5325 if (lookup_name.ada ().standard_p ())
5326 block = NULL;
4c4b4cd2 5327
339c13b6 5328 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5329
4eeaa230
DE
5330 if (block != NULL)
5331 {
5332 if (full_search)
d1183b06 5333 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5334 else
5335 {
5336 /* In the !full_search case we're are being called by
4009ee92 5337 iterate_over_symbols, and we don't want to search
4eeaa230 5338 superblocks. */
d1183b06 5339 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5340 }
d1183b06 5341 if (!result.empty () || !full_search)
22cee43f 5342 return;
4eeaa230 5343 }
d2e4a39e 5344
339c13b6
JB
5345 /* No non-global symbols found. Check our cache to see if we have
5346 already performed this search before. If we have, then return
5347 the same result. */
5348
b5ec771e
PA
5349 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5350 domain, &sym, &block))
4c4b4cd2
PH
5351 {
5352 if (sym != NULL)
d1183b06 5353 add_defn_to_vec (result, sym, block);
22cee43f 5354 return;
4c4b4cd2 5355 }
14f9c5c9 5356
22cee43f
PMR
5357 if (made_global_lookup_p)
5358 *made_global_lookup_p = 1;
b1eedac9 5359
339c13b6
JB
5360 /* Search symbols from all global blocks. */
5361
d1183b06 5362 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5363
4c4b4cd2 5364 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5365 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5366
d1183b06
TT
5367 if (result.empty ())
5368 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5369}
5370
b5ec771e 5371/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5372 is non-zero, enclosing scope and in global scopes.
5373
5374 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5375 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5376
5377 When full_search is non-zero, any non-function/non-enumeral
5378 symbol match within the nest of blocks whose innermost member is BLOCK,
5379 is the one match returned (no other matches in that or
5380 enclosing blocks is returned). If there are any matches in or
5381 surrounding BLOCK, then these alone are returned.
5382
5383 Names prefixed with "standard__" are handled specially: "standard__"
5384 is first stripped off, and only static and global symbols are searched. */
5385
d1183b06 5386static std::vector<struct block_symbol>
b5ec771e
PA
5387ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5388 const struct block *block,
22cee43f 5389 domain_enum domain,
22cee43f
PMR
5390 int full_search)
5391{
22cee43f 5392 int syms_from_global_search;
d1183b06 5393 std::vector<struct block_symbol> results;
22cee43f 5394
d1183b06 5395 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5396 domain, full_search, &syms_from_global_search);
14f9c5c9 5397
d1183b06 5398 remove_extra_symbols (&results);
4c4b4cd2 5399
d1183b06 5400 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5401 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5402
d1183b06 5403 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5404 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5405 results[0].symbol, results[0].block);
ec6a20c2 5406
d1183b06
TT
5407 remove_irrelevant_renamings (&results, block);
5408 return results;
14f9c5c9
AS
5409}
5410
b5ec771e 5411/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5412 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5413
4eeaa230
DE
5414 See ada_lookup_symbol_list_worker for further details. */
5415
d1183b06 5416std::vector<struct block_symbol>
b5ec771e 5417ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5418 domain_enum domain)
4eeaa230 5419{
b5ec771e
PA
5420 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5421 lookup_name_info lookup_name (name, name_match_type);
5422
d1183b06 5423 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5424}
5425
4e5c77fe
JB
5426/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5427 to 1, but choosing the first symbol found if there are multiple
5428 choices.
5429
5e2336be
JB
5430 The result is stored in *INFO, which must be non-NULL.
5431 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5432
5433void
5434ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5435 domain_enum domain,
d12307c1 5436 struct block_symbol *info)
14f9c5c9 5437{
b5ec771e
PA
5438 /* Since we already have an encoded name, wrap it in '<>' to force a
5439 verbatim match. Otherwise, if the name happens to not look like
5440 an encoded name (because it doesn't include a "__"),
5441 ada_lookup_name_info would re-encode/fold it again, and that
5442 would e.g., incorrectly lowercase object renaming names like
5443 "R28b" -> "r28b". */
12932e2c 5444 std::string verbatim = add_angle_brackets (name);
b5ec771e 5445
5e2336be 5446 gdb_assert (info != NULL);
65392b3e 5447 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5448}
aeb5907d
JB
5449
5450/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5451 scope and in global scopes, or NULL if none. NAME is folded and
5452 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5453 choosing the first symbol if there are multiple choices. */
4e5c77fe 5454
d12307c1 5455struct block_symbol
aeb5907d 5456ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5457 domain_enum domain)
aeb5907d 5458{
d1183b06
TT
5459 std::vector<struct block_symbol> candidates
5460 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5461
d1183b06 5462 if (candidates.empty ())
54d343a2 5463 return {};
f98fc17b
PA
5464
5465 block_symbol info = candidates[0];
5466 info.symbol = fixup_symbol_section (info.symbol, NULL);
d12307c1 5467 return info;
4c4b4cd2 5468}
14f9c5c9 5469
14f9c5c9 5470
4c4b4cd2
PH
5471/* True iff STR is a possible encoded suffix of a normal Ada name
5472 that is to be ignored for matching purposes. Suffixes of parallel
5473 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5474 are given by any of the regular expressions:
4c4b4cd2 5475
babe1480
JB
5476 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5477 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5478 TKB [subprogram suffix for task bodies]
babe1480 5479 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5480 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5481
5482 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5483 match is performed. This sequence is used to differentiate homonyms,
5484 is an optional part of a valid name suffix. */
4c4b4cd2 5485
14f9c5c9 5486static int
d2e4a39e 5487is_name_suffix (const char *str)
14f9c5c9
AS
5488{
5489 int k;
4c4b4cd2
PH
5490 const char *matching;
5491 const int len = strlen (str);
5492
babe1480
JB
5493 /* Skip optional leading __[0-9]+. */
5494
4c4b4cd2
PH
5495 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5496 {
babe1480
JB
5497 str += 3;
5498 while (isdigit (str[0]))
dda83cd7 5499 str += 1;
4c4b4cd2 5500 }
babe1480
JB
5501
5502 /* [.$][0-9]+ */
4c4b4cd2 5503
babe1480 5504 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5505 {
babe1480 5506 matching = str + 1;
4c4b4cd2 5507 while (isdigit (matching[0]))
dda83cd7 5508 matching += 1;
4c4b4cd2 5509 if (matching[0] == '\0')
dda83cd7 5510 return 1;
4c4b4cd2
PH
5511 }
5512
5513 /* ___[0-9]+ */
babe1480 5514
4c4b4cd2
PH
5515 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5516 {
5517 matching = str + 3;
5518 while (isdigit (matching[0]))
dda83cd7 5519 matching += 1;
4c4b4cd2 5520 if (matching[0] == '\0')
dda83cd7 5521 return 1;
4c4b4cd2
PH
5522 }
5523
9ac7f98e
JB
5524 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5525
5526 if (strcmp (str, "TKB") == 0)
5527 return 1;
5528
529cad9c
PH
5529#if 0
5530 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5531 with a N at the end. Unfortunately, the compiler uses the same
5532 convention for other internal types it creates. So treating
529cad9c 5533 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5534 some regressions. For instance, consider the case of an enumerated
5535 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5536 name ends with N.
5537 Having a single character like this as a suffix carrying some
0963b4bd 5538 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5539 to be something like "_N" instead. In the meantime, do not do
5540 the following check. */
5541 /* Protected Object Subprograms */
5542 if (len == 1 && str [0] == 'N')
5543 return 1;
5544#endif
5545
5546 /* _E[0-9]+[bs]$ */
5547 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5548 {
5549 matching = str + 3;
5550 while (isdigit (matching[0]))
dda83cd7 5551 matching += 1;
529cad9c 5552 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5553 && matching [1] == '\0')
5554 return 1;
529cad9c
PH
5555 }
5556
4c4b4cd2
PH
5557 /* ??? We should not modify STR directly, as we are doing below. This
5558 is fine in this case, but may become problematic later if we find
5559 that this alternative did not work, and want to try matching
5560 another one from the begining of STR. Since we modified it, we
5561 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5562 if (str[0] == 'X')
5563 {
5564 str += 1;
d2e4a39e 5565 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5566 {
5567 if (str[0] != 'n' && str[0] != 'b')
5568 return 0;
5569 str += 1;
5570 }
14f9c5c9 5571 }
babe1480 5572
14f9c5c9
AS
5573 if (str[0] == '\000')
5574 return 1;
babe1480 5575
d2e4a39e 5576 if (str[0] == '_')
14f9c5c9
AS
5577 {
5578 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5579 return 0;
d2e4a39e 5580 if (str[2] == '_')
dda83cd7
SM
5581 {
5582 if (strcmp (str + 3, "JM") == 0)
5583 return 1;
5584 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5585 the LJM suffix in favor of the JM one. But we will
5586 still accept LJM as a valid suffix for a reasonable
5587 amount of time, just to allow ourselves to debug programs
5588 compiled using an older version of GNAT. */
5589 if (strcmp (str + 3, "LJM") == 0)
5590 return 1;
5591 if (str[3] != 'X')
5592 return 0;
5593 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5594 || str[4] == 'U' || str[4] == 'P')
5595 return 1;
5596 if (str[4] == 'R' && str[5] != 'T')
5597 return 1;
5598 return 0;
5599 }
4c4b4cd2 5600 if (!isdigit (str[2]))
dda83cd7 5601 return 0;
4c4b4cd2 5602 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5603 if (!isdigit (str[k]) && str[k] != '_')
5604 return 0;
14f9c5c9
AS
5605 return 1;
5606 }
4c4b4cd2 5607 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5608 {
4c4b4cd2 5609 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5610 if (!isdigit (str[k]) && str[k] != '_')
5611 return 0;
14f9c5c9
AS
5612 return 1;
5613 }
5614 return 0;
5615}
d2e4a39e 5616
aeb5907d
JB
5617/* Return non-zero if the string starting at NAME and ending before
5618 NAME_END contains no capital letters. */
529cad9c
PH
5619
5620static int
5621is_valid_name_for_wild_match (const char *name0)
5622{
f945dedf 5623 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5624 int i;
5625
5823c3ef
JB
5626 /* If the decoded name starts with an angle bracket, it means that
5627 NAME0 does not follow the GNAT encoding format. It should then
5628 not be allowed as a possible wild match. */
5629 if (decoded_name[0] == '<')
5630 return 0;
5631
529cad9c
PH
5632 for (i=0; decoded_name[i] != '\0'; i++)
5633 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5634 return 0;
5635
5636 return 1;
5637}
5638
59c8a30b
JB
5639/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5640 character which could start a simple name. Assumes that *NAMEP points
5641 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5642
14f9c5c9 5643static int
59c8a30b 5644advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5645{
73589123 5646 const char *name = *namep;
5b4ee69b 5647
5823c3ef 5648 while (1)
14f9c5c9 5649 {
59c8a30b 5650 char t0, t1;
73589123
PH
5651
5652 t0 = *name;
5653 if (t0 == '_')
5654 {
5655 t1 = name[1];
5656 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5657 {
5658 name += 1;
61012eef 5659 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5660 break;
5661 else
5662 name += 1;
5663 }
aa27d0b3
JB
5664 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5665 || name[2] == target0))
73589123
PH
5666 {
5667 name += 2;
5668 break;
5669 }
86b44259
TT
5670 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5671 {
5672 /* Names like "pkg__B_N__name", where N is a number, are
5673 block-local. We can handle these by simply skipping
5674 the "B_" here. */
5675 name += 4;
5676 }
73589123
PH
5677 else
5678 return 0;
5679 }
5680 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5681 name += 1;
5682 else
5823c3ef 5683 return 0;
73589123
PH
5684 }
5685
5686 *namep = name;
5687 return 1;
5688}
5689
b5ec771e
PA
5690/* Return true iff NAME encodes a name of the form prefix.PATN.
5691 Ignores any informational suffixes of NAME (i.e., for which
5692 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
5693 simple name. */
73589123 5694
b5ec771e 5695static bool
73589123
PH
5696wild_match (const char *name, const char *patn)
5697{
22e048c9 5698 const char *p;
73589123
PH
5699 const char *name0 = name;
5700
5701 while (1)
5702 {
5703 const char *match = name;
5704
5705 if (*name == *patn)
5706 {
5707 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
5708 if (*p != *name)
5709 break;
5710 if (*p == '\0' && is_name_suffix (name))
b5ec771e 5711 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
5712
5713 if (name[-1] == '_')
5714 name -= 1;
5715 }
5716 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 5717 return false;
96d887e8 5718 }
96d887e8
PH
5719}
5720
d1183b06 5721/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 5722 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
5723
5724static void
d1183b06 5725ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5726 const struct block *block,
5727 const lookup_name_info &lookup_name,
5728 domain_enum domain, struct objfile *objfile)
96d887e8 5729{
8157b174 5730 struct block_iterator iter;
96d887e8
PH
5731 /* A matching argument symbol, if any. */
5732 struct symbol *arg_sym;
5733 /* Set true when we find a matching non-argument symbol. */
1178743e 5734 bool found_sym;
96d887e8
PH
5735 struct symbol *sym;
5736
5737 arg_sym = NULL;
1178743e 5738 found_sym = false;
b5ec771e
PA
5739 for (sym = block_iter_match_first (block, lookup_name, &iter);
5740 sym != NULL;
5741 sym = block_iter_match_next (lookup_name, &iter))
96d887e8 5742 {
c1b5c1eb 5743 if (symbol_matches_domain (sym->language (), SYMBOL_DOMAIN (sym), domain))
b5ec771e
PA
5744 {
5745 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5746 {
5747 if (SYMBOL_IS_ARGUMENT (sym))
5748 arg_sym = sym;
5749 else
5750 {
1178743e 5751 found_sym = true;
d1183b06 5752 add_defn_to_vec (result,
b5ec771e
PA
5753 fixup_symbol_section (sym, objfile),
5754 block);
5755 }
5756 }
5757 }
96d887e8
PH
5758 }
5759
22cee43f
PMR
5760 /* Handle renamings. */
5761
d1183b06 5762 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 5763 found_sym = true;
22cee43f 5764
96d887e8
PH
5765 if (!found_sym && arg_sym != NULL)
5766 {
d1183b06 5767 add_defn_to_vec (result,
dda83cd7
SM
5768 fixup_symbol_section (arg_sym, objfile),
5769 block);
96d887e8
PH
5770 }
5771
b5ec771e 5772 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
5773 {
5774 arg_sym = NULL;
1178743e 5775 found_sym = false;
b5ec771e
PA
5776 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
5777 const char *name = ada_lookup_name.c_str ();
5778 size_t name_len = ada_lookup_name.size ();
96d887e8
PH
5779
5780 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 5781 {
dda83cd7
SM
5782 if (symbol_matches_domain (sym->language (),
5783 SYMBOL_DOMAIN (sym), domain))
5784 {
5785 int cmp;
5786
5787 cmp = (int) '_' - (int) sym->linkage_name ()[0];
5788 if (cmp == 0)
5789 {
5790 cmp = !startswith (sym->linkage_name (), "_ada_");
5791 if (cmp == 0)
5792 cmp = strncmp (name, sym->linkage_name () + 5,
5793 name_len);
5794 }
5795
5796 if (cmp == 0
5797 && is_name_suffix (sym->linkage_name () + name_len + 5))
5798 {
2a2d4dc3
AS
5799 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5800 {
5801 if (SYMBOL_IS_ARGUMENT (sym))
5802 arg_sym = sym;
5803 else
5804 {
1178743e 5805 found_sym = true;
d1183b06 5806 add_defn_to_vec (result,
2a2d4dc3
AS
5807 fixup_symbol_section (sym, objfile),
5808 block);
5809 }
5810 }
dda83cd7
SM
5811 }
5812 }
76a01679 5813 }
96d887e8
PH
5814
5815 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 5816 They aren't parameters, right? */
96d887e8 5817 if (!found_sym && arg_sym != NULL)
dda83cd7 5818 {
d1183b06 5819 add_defn_to_vec (result,
dda83cd7
SM
5820 fixup_symbol_section (arg_sym, objfile),
5821 block);
5822 }
96d887e8
PH
5823 }
5824}
5825\f
41d27058 5826
dda83cd7 5827 /* Symbol Completion */
41d27058 5828
b5ec771e 5829/* See symtab.h. */
41d27058 5830
b5ec771e
PA
5831bool
5832ada_lookup_name_info::matches
5833 (const char *sym_name,
5834 symbol_name_match_type match_type,
a207cff2 5835 completion_match_result *comp_match_res) const
41d27058 5836{
b5ec771e
PA
5837 bool match = false;
5838 const char *text = m_encoded_name.c_str ();
5839 size_t text_len = m_encoded_name.size ();
41d27058
JB
5840
5841 /* First, test against the fully qualified name of the symbol. */
5842
5843 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 5844 match = true;
41d27058 5845
f945dedf 5846 std::string decoded_name = ada_decode (sym_name);
b5ec771e 5847 if (match && !m_encoded_p)
41d27058
JB
5848 {
5849 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
5850 that iff we are doing a verbatim match, the decoded version
5851 of the symbol name starts with '<'. Otherwise, this symbol name
5852 is not a suitable completion. */
41d27058 5853
f945dedf 5854 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 5855 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
5856 }
5857
b5ec771e 5858 if (match && !m_verbatim_p)
41d27058
JB
5859 {
5860 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
5861 be done is to verify that the potentially matching symbol name
5862 does not include capital letters, because the ada-mode would
5863 not be able to understand these symbol names without the
5864 angle bracket notation. */
41d27058
JB
5865 const char *tmp;
5866
5867 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
5868 if (*tmp != '\0')
b5ec771e 5869 match = false;
41d27058
JB
5870 }
5871
5872 /* Second: Try wild matching... */
5873
b5ec771e 5874 if (!match && m_wild_match_p)
41d27058
JB
5875 {
5876 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
5877 may represent an unqualified symbol name. We therefore must
5878 also compare TEXT against the unqualified name of the symbol. */
f945dedf 5879 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
5880
5881 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 5882 match = true;
41d27058
JB
5883 }
5884
b5ec771e 5885 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
5886
5887 if (!match)
b5ec771e 5888 return false;
41d27058 5889
a207cff2 5890 if (comp_match_res != NULL)
b5ec771e 5891 {
a207cff2 5892 std::string &match_str = comp_match_res->match.storage ();
41d27058 5893
b5ec771e 5894 if (!m_encoded_p)
a207cff2 5895 match_str = ada_decode (sym_name);
b5ec771e
PA
5896 else
5897 {
5898 if (m_verbatim_p)
5899 match_str = add_angle_brackets (sym_name);
5900 else
5901 match_str = sym_name;
41d27058 5902
b5ec771e 5903 }
a207cff2
PA
5904
5905 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
5906 }
5907
b5ec771e 5908 return true;
41d27058
JB
5909}
5910
dda83cd7 5911 /* Field Access */
96d887e8 5912
73fb9985
JB
5913/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
5914 for tagged types. */
5915
5916static int
5917ada_is_dispatch_table_ptr_type (struct type *type)
5918{
0d5cff50 5919 const char *name;
73fb9985 5920
78134374 5921 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
5922 return 0;
5923
7d93a1e0 5924 name = TYPE_TARGET_TYPE (type)->name ();
73fb9985
JB
5925 if (name == NULL)
5926 return 0;
5927
5928 return (strcmp (name, "ada__tags__dispatch_table") == 0);
5929}
5930
ac4a2da4
JG
5931/* Return non-zero if TYPE is an interface tag. */
5932
5933static int
5934ada_is_interface_tag (struct type *type)
5935{
7d93a1e0 5936 const char *name = type->name ();
ac4a2da4
JG
5937
5938 if (name == NULL)
5939 return 0;
5940
5941 return (strcmp (name, "ada__tags__interface_tag") == 0);
5942}
5943
963a6417
PH
5944/* True if field number FIELD_NUM in struct or union type TYPE is supposed
5945 to be invisible to users. */
96d887e8 5946
963a6417
PH
5947int
5948ada_is_ignored_field (struct type *type, int field_num)
96d887e8 5949{
1f704f76 5950 if (field_num < 0 || field_num > type->num_fields ())
963a6417 5951 return 1;
ffde82bf 5952
73fb9985
JB
5953 /* Check the name of that field. */
5954 {
33d16dd9 5955 const char *name = type->field (field_num).name ();
73fb9985
JB
5956
5957 /* Anonymous field names should not be printed.
5958 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 5959 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
5960 if (name == NULL)
5961 return 1;
5962
ffde82bf
JB
5963 /* Normally, fields whose name start with an underscore ("_")
5964 are fields that have been internally generated by the compiler,
5965 and thus should not be printed. The "_parent" field is special,
5966 however: This is a field internally generated by the compiler
5967 for tagged types, and it contains the components inherited from
5968 the parent type. This field should not be printed as is, but
5969 should not be ignored either. */
61012eef 5970 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985
JB
5971 return 1;
5972 }
5973
ac4a2da4
JG
5974 /* If this is the dispatch table of a tagged type or an interface tag,
5975 then ignore. */
73fb9985 5976 if (ada_is_tagged_type (type, 1)
940da03e
SM
5977 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
5978 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
5979 return 1;
5980
5981 /* Not a special field, so it should not be ignored. */
5982 return 0;
963a6417 5983}
96d887e8 5984
963a6417 5985/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 5986 pointer or reference type whose ultimate target has a tag field. */
96d887e8 5987
963a6417
PH
5988int
5989ada_is_tagged_type (struct type *type, int refok)
5990{
988f6b3d 5991 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 5992}
96d887e8 5993
963a6417 5994/* True iff TYPE represents the type of X'Tag */
96d887e8 5995
963a6417
PH
5996int
5997ada_is_tag_type (struct type *type)
5998{
460efde1
JB
5999 type = ada_check_typedef (type);
6000
78134374 6001 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6002 return 0;
6003 else
96d887e8 6004 {
963a6417 6005 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 6006
963a6417 6007 return (name != NULL
dda83cd7 6008 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6009 }
96d887e8
PH
6010}
6011
963a6417 6012/* The type of the tag on VAL. */
76a01679 6013
de93309a 6014static struct type *
963a6417 6015ada_tag_type (struct value *val)
96d887e8 6016{
988f6b3d 6017 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0);
963a6417 6018}
96d887e8 6019
b50d69b5
JG
6020/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6021 retired at Ada 05). */
6022
6023static int
6024is_ada95_tag (struct value *tag)
6025{
6026 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6027}
6028
963a6417 6029/* The value of the tag on VAL. */
96d887e8 6030
de93309a 6031static struct value *
963a6417
PH
6032ada_value_tag (struct value *val)
6033{
03ee6b2e 6034 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6035}
6036
963a6417
PH
6037/* The value of the tag on the object of type TYPE whose contents are
6038 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6039 ADDRESS. */
96d887e8 6040
963a6417 6041static struct value *
10a2c479 6042value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6043 const gdb_byte *valaddr,
dda83cd7 6044 CORE_ADDR address)
96d887e8 6045{
b5385fc0 6046 int tag_byte_offset;
963a6417 6047 struct type *tag_type;
5b4ee69b 6048
4d1795ac
TT
6049 gdb::array_view<const gdb_byte> contents;
6050 if (valaddr != nullptr)
6051 contents = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
6052 struct type *resolved_type = resolve_dynamic_type (type, contents, address);
6053 if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6054 NULL, NULL, NULL))
96d887e8 6055 {
fc1a4b47 6056 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6057 ? NULL
6058 : valaddr + tag_byte_offset);
963a6417 6059 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6060
963a6417 6061 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6062 }
963a6417
PH
6063 return NULL;
6064}
96d887e8 6065
963a6417
PH
6066static struct type *
6067type_from_tag (struct value *tag)
6068{
f5272a3b 6069 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6070
963a6417 6071 if (type_name != NULL)
5c4258f4 6072 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6073 return NULL;
6074}
96d887e8 6075
b50d69b5
JG
6076/* Given a value OBJ of a tagged type, return a value of this
6077 type at the base address of the object. The base address, as
6078 defined in Ada.Tags, it is the address of the primary tag of
6079 the object, and therefore where the field values of its full
6080 view can be fetched. */
6081
6082struct value *
6083ada_tag_value_at_base_address (struct value *obj)
6084{
b50d69b5
JG
6085 struct value *val;
6086 LONGEST offset_to_top = 0;
6087 struct type *ptr_type, *obj_type;
6088 struct value *tag;
6089 CORE_ADDR base_address;
6090
6091 obj_type = value_type (obj);
6092
6093 /* It is the responsability of the caller to deref pointers. */
6094
78134374 6095 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6096 return obj;
6097
6098 tag = ada_value_tag (obj);
6099 if (!tag)
6100 return obj;
6101
6102 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6103
6104 if (is_ada95_tag (tag))
6105 return obj;
6106
08f49010
XR
6107 ptr_type = language_lookup_primitive_type
6108 (language_def (language_ada), target_gdbarch(), "storage_offset");
b50d69b5
JG
6109 ptr_type = lookup_pointer_type (ptr_type);
6110 val = value_cast (ptr_type, tag);
6111 if (!val)
6112 return obj;
6113
6114 /* It is perfectly possible that an exception be raised while
6115 trying to determine the base address, just like for the tag;
6116 see ada_tag_name for more details. We do not print the error
6117 message for the same reason. */
6118
a70b8144 6119 try
b50d69b5
JG
6120 {
6121 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6122 }
6123
230d2906 6124 catch (const gdb_exception_error &e)
492d29ea
PA
6125 {
6126 return obj;
6127 }
b50d69b5
JG
6128
6129 /* If offset is null, nothing to do. */
6130
6131 if (offset_to_top == 0)
6132 return obj;
6133
6134 /* -1 is a special case in Ada.Tags; however, what should be done
6135 is not quite clear from the documentation. So do nothing for
6136 now. */
6137
6138 if (offset_to_top == -1)
6139 return obj;
6140
08f49010
XR
6141 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6142 from the base address. This was however incompatible with
6143 C++ dispatch table: C++ uses a *negative* value to *add*
6144 to the base address. Ada's convention has therefore been
6145 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6146 use the same convention. Here, we support both cases by
6147 checking the sign of OFFSET_TO_TOP. */
6148
6149 if (offset_to_top > 0)
6150 offset_to_top = -offset_to_top;
6151
6152 base_address = value_address (obj) + offset_to_top;
b50d69b5
JG
6153 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6154
6155 /* Make sure that we have a proper tag at the new address.
6156 Otherwise, offset_to_top is bogus (which can happen when
6157 the object is not initialized yet). */
6158
6159 if (!tag)
6160 return obj;
6161
6162 obj_type = type_from_tag (tag);
6163
6164 if (!obj_type)
6165 return obj;
6166
6167 return value_from_contents_and_address (obj_type, NULL, base_address);
6168}
6169
1b611343
JB
6170/* Return the "ada__tags__type_specific_data" type. */
6171
6172static struct type *
6173ada_get_tsd_type (struct inferior *inf)
963a6417 6174{
1b611343 6175 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6176
1b611343
JB
6177 if (data->tsd_type == 0)
6178 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6179 return data->tsd_type;
6180}
529cad9c 6181
1b611343
JB
6182/* Return the TSD (type-specific data) associated to the given TAG.
6183 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6184
1b611343 6185 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6186
1b611343
JB
6187static struct value *
6188ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6189{
4c4b4cd2 6190 struct value *val;
1b611343 6191 struct type *type;
5b4ee69b 6192
1b611343
JB
6193 /* First option: The TSD is simply stored as a field of our TAG.
6194 Only older versions of GNAT would use this format, but we have
6195 to test it first, because there are no visible markers for
6196 the current approach except the absence of that field. */
529cad9c 6197
1b611343
JB
6198 val = ada_value_struct_elt (tag, "tsd", 1);
6199 if (val)
6200 return val;
e802dbe0 6201
1b611343
JB
6202 /* Try the second representation for the dispatch table (in which
6203 there is no explicit 'tsd' field in the referent of the tag pointer,
6204 and instead the tsd pointer is stored just before the dispatch
6205 table. */
e802dbe0 6206
1b611343
JB
6207 type = ada_get_tsd_type (current_inferior());
6208 if (type == NULL)
6209 return NULL;
6210 type = lookup_pointer_type (lookup_pointer_type (type));
6211 val = value_cast (type, tag);
6212 if (val == NULL)
6213 return NULL;
6214 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6215}
6216
1b611343
JB
6217/* Given the TSD of a tag (type-specific data), return a string
6218 containing the name of the associated type.
6219
f5272a3b 6220 May return NULL if we are unable to determine the tag name. */
1b611343 6221
f5272a3b 6222static gdb::unique_xmalloc_ptr<char>
1b611343 6223ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6224{
529cad9c 6225 char *p;
1b611343 6226 struct value *val;
529cad9c 6227
1b611343 6228 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6229 if (val == NULL)
1b611343 6230 return NULL;
66920317
TT
6231 gdb::unique_xmalloc_ptr<char> buffer
6232 = target_read_string (value_as_address (val), INT_MAX);
6233 if (buffer == nullptr)
f5272a3b
TT
6234 return nullptr;
6235
6236 for (p = buffer.get (); *p != '\0'; ++p)
6237 {
6238 if (isalpha (*p))
6239 *p = tolower (*p);
6240 }
6241
6242 return buffer;
4c4b4cd2
PH
6243}
6244
6245/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6246 a C string.
6247
6248 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6249 determine the name of that tag. */
4c4b4cd2 6250
f5272a3b 6251gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6252ada_tag_name (struct value *tag)
6253{
f5272a3b 6254 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6255
df407dfe 6256 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 6257 return NULL;
1b611343
JB
6258
6259 /* It is perfectly possible that an exception be raised while trying
6260 to determine the TAG's name, even under normal circumstances:
6261 The associated variable may be uninitialized or corrupted, for
6262 instance. We do not let any exception propagate past this point.
6263 instead we return NULL.
6264
6265 We also do not print the error message either (which often is very
6266 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6267 the caller print a more meaningful message if necessary. */
a70b8144 6268 try
1b611343
JB
6269 {
6270 struct value *tsd = ada_get_tsd_from_tag (tag);
6271
6272 if (tsd != NULL)
6273 name = ada_tag_name_from_tsd (tsd);
6274 }
230d2906 6275 catch (const gdb_exception_error &e)
492d29ea
PA
6276 {
6277 }
1b611343
JB
6278
6279 return name;
4c4b4cd2
PH
6280}
6281
6282/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6283
d2e4a39e 6284struct type *
ebf56fd3 6285ada_parent_type (struct type *type)
14f9c5c9
AS
6286{
6287 int i;
6288
61ee279c 6289 type = ada_check_typedef (type);
14f9c5c9 6290
78134374 6291 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6292 return NULL;
6293
1f704f76 6294 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6295 if (ada_is_parent_field (type, i))
0c1f74cf 6296 {
dda83cd7 6297 struct type *parent_type = type->field (i).type ();
0c1f74cf 6298
dda83cd7
SM
6299 /* If the _parent field is a pointer, then dereference it. */
6300 if (parent_type->code () == TYPE_CODE_PTR)
6301 parent_type = TYPE_TARGET_TYPE (parent_type);
6302 /* If there is a parallel XVS type, get the actual base type. */
6303 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6304
dda83cd7 6305 return ada_check_typedef (parent_type);
0c1f74cf 6306 }
14f9c5c9
AS
6307
6308 return NULL;
6309}
6310
4c4b4cd2
PH
6311/* True iff field number FIELD_NUM of structure type TYPE contains the
6312 parent-type (inherited) fields of a derived type. Assumes TYPE is
6313 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6314
6315int
ebf56fd3 6316ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6317{
33d16dd9 6318 const char *name = ada_check_typedef (type)->field (field_num).name ();
5b4ee69b 6319
4c4b4cd2 6320 return (name != NULL
dda83cd7
SM
6321 && (startswith (name, "PARENT")
6322 || startswith (name, "_parent")));
14f9c5c9
AS
6323}
6324
4c4b4cd2 6325/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6326 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6327 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6328 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6329 structures. */
14f9c5c9
AS
6330
6331int
ebf56fd3 6332ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6333{
33d16dd9 6334 const char *name = type->field (field_num).name ();
5b4ee69b 6335
dddc0e16
JB
6336 if (name != NULL && strcmp (name, "RETVAL") == 0)
6337 {
6338 /* This happens in functions with "out" or "in out" parameters
6339 which are passed by copy. For such functions, GNAT describes
6340 the function's return type as being a struct where the return
6341 value is in a field called RETVAL, and where the other "out"
6342 or "in out" parameters are fields of that struct. This is not
6343 a wrapper. */
6344 return 0;
6345 }
6346
d2e4a39e 6347 return (name != NULL
dda83cd7
SM
6348 && (startswith (name, "PARENT")
6349 || strcmp (name, "REP") == 0
6350 || startswith (name, "_parent")
6351 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6352}
6353
4c4b4cd2
PH
6354/* True iff field number FIELD_NUM of structure or union type TYPE
6355 is a variant wrapper. Assumes TYPE is a structure type with at least
6356 FIELD_NUM+1 fields. */
14f9c5c9
AS
6357
6358int
ebf56fd3 6359ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6360{
8ecb59f8
TT
6361 /* Only Ada types are eligible. */
6362 if (!ADA_TYPE_P (type))
6363 return 0;
6364
940da03e 6365 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6366
78134374
SM
6367 return (field_type->code () == TYPE_CODE_UNION
6368 || (is_dynamic_field (type, field_num)
6369 && (TYPE_TARGET_TYPE (field_type)->code ()
c3e5cd34 6370 == TYPE_CODE_UNION)));
14f9c5c9
AS
6371}
6372
6373/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6374 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6375 returns the type of the controlling discriminant for the variant.
6376 May return NULL if the type could not be found. */
14f9c5c9 6377
d2e4a39e 6378struct type *
ebf56fd3 6379ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6380{
a121b7c1 6381 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6382
988f6b3d 6383 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6384}
6385
4c4b4cd2 6386/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6387 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6388 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6389
de93309a 6390static int
ebf56fd3 6391ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6392{
33d16dd9 6393 const char *name = type->field (field_num).name ();
5b4ee69b 6394
14f9c5c9
AS
6395 return (name != NULL && name[0] == 'O');
6396}
6397
6398/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6399 returns the name of the discriminant controlling the variant.
6400 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6401
a121b7c1 6402const char *
ebf56fd3 6403ada_variant_discrim_name (struct type *type0)
14f9c5c9 6404{
5f9febe0 6405 static std::string result;
d2e4a39e
AS
6406 struct type *type;
6407 const char *name;
6408 const char *discrim_end;
6409 const char *discrim_start;
14f9c5c9 6410
78134374 6411 if (type0->code () == TYPE_CODE_PTR)
14f9c5c9
AS
6412 type = TYPE_TARGET_TYPE (type0);
6413 else
6414 type = type0;
6415
6416 name = ada_type_name (type);
6417
6418 if (name == NULL || name[0] == '\000')
6419 return "";
6420
6421 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6422 discrim_end -= 1)
6423 {
61012eef 6424 if (startswith (discrim_end, "___XVN"))
dda83cd7 6425 break;
14f9c5c9
AS
6426 }
6427 if (discrim_end == name)
6428 return "";
6429
d2e4a39e 6430 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6431 discrim_start -= 1)
6432 {
d2e4a39e 6433 if (discrim_start == name + 1)
dda83cd7 6434 return "";
76a01679 6435 if ((discrim_start > name + 3
dda83cd7
SM
6436 && startswith (discrim_start - 3, "___"))
6437 || discrim_start[-1] == '.')
6438 break;
14f9c5c9
AS
6439 }
6440
5f9febe0
TT
6441 result = std::string (discrim_start, discrim_end - discrim_start);
6442 return result.c_str ();
14f9c5c9
AS
6443}
6444
4c4b4cd2
PH
6445/* Scan STR for a subtype-encoded number, beginning at position K.
6446 Put the position of the character just past the number scanned in
6447 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6448 Return 1 if there was a valid number at the given position, and 0
6449 otherwise. A "subtype-encoded" number consists of the absolute value
6450 in decimal, followed by the letter 'm' to indicate a negative number.
6451 Assumes 0m does not occur. */
14f9c5c9
AS
6452
6453int
d2e4a39e 6454ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6455{
6456 ULONGEST RU;
6457
d2e4a39e 6458 if (!isdigit (str[k]))
14f9c5c9
AS
6459 return 0;
6460
4c4b4cd2 6461 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6462 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6463 LONGEST. */
14f9c5c9
AS
6464 RU = 0;
6465 while (isdigit (str[k]))
6466 {
d2e4a39e 6467 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6468 k += 1;
6469 }
6470
d2e4a39e 6471 if (str[k] == 'm')
14f9c5c9
AS
6472 {
6473 if (R != NULL)
dda83cd7 6474 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6475 k += 1;
6476 }
6477 else if (R != NULL)
6478 *R = (LONGEST) RU;
6479
4c4b4cd2 6480 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6481 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6482 number representable as a LONGEST (although either would probably work
6483 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6484 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6485
6486 if (new_k != NULL)
6487 *new_k = k;
6488 return 1;
6489}
6490
4c4b4cd2
PH
6491/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6492 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6493 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6494
de93309a 6495static int
ebf56fd3 6496ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6497{
33d16dd9 6498 const char *name = type->field (field_num).name ();
14f9c5c9
AS
6499 int p;
6500
6501 p = 0;
6502 while (1)
6503 {
d2e4a39e 6504 switch (name[p])
dda83cd7
SM
6505 {
6506 case '\0':
6507 return 0;
6508 case 'S':
6509 {
6510 LONGEST W;
6511
6512 if (!ada_scan_number (name, p + 1, &W, &p))
6513 return 0;
6514 if (val == W)
6515 return 1;
6516 break;
6517 }
6518 case 'R':
6519 {
6520 LONGEST L, U;
6521
6522 if (!ada_scan_number (name, p + 1, &L, &p)
6523 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6524 return 0;
6525 if (val >= L && val <= U)
6526 return 1;
6527 break;
6528 }
6529 case 'O':
6530 return 1;
6531 default:
6532 return 0;
6533 }
4c4b4cd2
PH
6534 }
6535}
6536
0963b4bd 6537/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6538
6539/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6540 ARG_TYPE, extract and return the value of one of its (non-static)
6541 fields. FIELDNO says which field. Differs from value_primitive_field
6542 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6543
5eb68a39 6544struct value *
d2e4a39e 6545ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6546 struct type *arg_type)
14f9c5c9 6547{
14f9c5c9
AS
6548 struct type *type;
6549
61ee279c 6550 arg_type = ada_check_typedef (arg_type);
940da03e 6551 type = arg_type->field (fieldno).type ();
14f9c5c9 6552
4504bbde
TT
6553 /* Handle packed fields. It might be that the field is not packed
6554 relative to its containing structure, but the structure itself is
6555 packed; in this case we must take the bit-field path. */
6556 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || value_bitpos (arg1) != 0)
14f9c5c9
AS
6557 {
6558 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6559 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6560
50888e42
SM
6561 return ada_value_primitive_packed_val (arg1,
6562 value_contents (arg1).data (),
dda83cd7
SM
6563 offset + bit_pos / 8,
6564 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6565 }
6566 else
6567 return value_primitive_field (arg1, offset, fieldno, arg_type);
6568}
6569
52ce6436
PH
6570/* Find field with name NAME in object of type TYPE. If found,
6571 set the following for each argument that is non-null:
6572 - *FIELD_TYPE_P to the field's type;
6573 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6574 an object of that type;
6575 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6576 - *BIT_SIZE_P to its size in bits if the field is packed, and
6577 0 otherwise;
6578 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6579 fields up to but not including the desired field, or by the total
6580 number of fields if not found. A NULL value of NAME never
6581 matches; the function just counts visible fields in this case.
6582
828d5846
XR
6583 Notice that we need to handle when a tagged record hierarchy
6584 has some components with the same name, like in this scenario:
6585
6586 type Top_T is tagged record
dda83cd7
SM
6587 N : Integer := 1;
6588 U : Integer := 974;
6589 A : Integer := 48;
828d5846
XR
6590 end record;
6591
6592 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6593 N : Character := 'a';
6594 C : Integer := 3;
828d5846
XR
6595 end record;
6596
6597 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6598 N : Float := 4.0;
6599 C : Character := '5';
6600 X : Integer := 6;
6601 A : Character := 'J';
828d5846
XR
6602 end record;
6603
6604 Let's say we now have a variable declared and initialized as follow:
6605
6606 TC : Top_A := new Bottom_T;
6607
6608 And then we use this variable to call this function
6609
6610 procedure Assign (Obj: in out Top_T; TV : Integer);
6611
6612 as follow:
6613
6614 Assign (Top_T (B), 12);
6615
6616 Now, we're in the debugger, and we're inside that procedure
6617 then and we want to print the value of obj.c:
6618
6619 Usually, the tagged record or one of the parent type owns the
6620 component to print and there's no issue but in this particular
6621 case, what does it mean to ask for Obj.C? Since the actual
6622 type for object is type Bottom_T, it could mean two things: type
6623 component C from the Middle_T view, but also component C from
6624 Bottom_T. So in that "undefined" case, when the component is
6625 not found in the non-resolved type (which includes all the
6626 components of the parent type), then resolve it and see if we
6627 get better luck once expanded.
6628
6629 In the case of homonyms in the derived tagged type, we don't
6630 guaranty anything, and pick the one that's easiest for us
6631 to program.
6632
0963b4bd 6633 Returns 1 if found, 0 otherwise. */
52ce6436 6634
4c4b4cd2 6635static int
0d5cff50 6636find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6637 struct type **field_type_p,
6638 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6639 int *index_p)
4c4b4cd2
PH
6640{
6641 int i;
828d5846 6642 int parent_offset = -1;
4c4b4cd2 6643
61ee279c 6644 type = ada_check_typedef (type);
76a01679 6645
52ce6436
PH
6646 if (field_type_p != NULL)
6647 *field_type_p = NULL;
6648 if (byte_offset_p != NULL)
d5d6fca5 6649 *byte_offset_p = 0;
52ce6436
PH
6650 if (bit_offset_p != NULL)
6651 *bit_offset_p = 0;
6652 if (bit_size_p != NULL)
6653 *bit_size_p = 0;
6654
1f704f76 6655 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2 6656 {
4d1795ac
TT
6657 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6658 type. However, we only need the values to be correct when
6659 the caller asks for them. */
6660 int bit_pos = 0, fld_offset = 0;
6661 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6662 {
6663 bit_pos = TYPE_FIELD_BITPOS (type, i);
6664 fld_offset = offset + bit_pos / 8;
6665 }
6666
33d16dd9 6667 const char *t_field_name = type->field (i).name ();
76a01679 6668
4c4b4cd2 6669 if (t_field_name == NULL)
dda83cd7 6670 continue;
4c4b4cd2 6671
828d5846 6672 else if (ada_is_parent_field (type, i))
dda83cd7 6673 {
828d5846
XR
6674 /* This is a field pointing us to the parent type of a tagged
6675 type. As hinted in this function's documentation, we give
6676 preference to fields in the current record first, so what
6677 we do here is just record the index of this field before
6678 we skip it. If it turns out we couldn't find our field
6679 in the current record, then we'll get back to it and search
6680 inside it whether the field might exist in the parent. */
6681
dda83cd7
SM
6682 parent_offset = i;
6683 continue;
6684 }
828d5846 6685
52ce6436 6686 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
6687 {
6688 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 6689
52ce6436 6690 if (field_type_p != NULL)
940da03e 6691 *field_type_p = type->field (i).type ();
52ce6436
PH
6692 if (byte_offset_p != NULL)
6693 *byte_offset_p = fld_offset;
6694 if (bit_offset_p != NULL)
6695 *bit_offset_p = bit_pos % 8;
6696 if (bit_size_p != NULL)
6697 *bit_size_p = bit_size;
dda83cd7
SM
6698 return 1;
6699 }
4c4b4cd2 6700 else if (ada_is_wrapper_field (type, i))
dda83cd7 6701 {
940da03e 6702 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
6703 field_type_p, byte_offset_p, bit_offset_p,
6704 bit_size_p, index_p))
dda83cd7
SM
6705 return 1;
6706 }
4c4b4cd2 6707 else if (ada_is_variant_part (type, i))
dda83cd7 6708 {
52ce6436
PH
6709 /* PNH: Wait. Do we ever execute this section, or is ARG always of
6710 fixed type?? */
dda83cd7
SM
6711 int j;
6712 struct type *field_type
940da03e 6713 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 6714
dda83cd7
SM
6715 for (j = 0; j < field_type->num_fields (); j += 1)
6716 {
6717 if (find_struct_field (name, field_type->field (j).type (),
6718 fld_offset
6719 + TYPE_FIELD_BITPOS (field_type, j) / 8,
6720 field_type_p, byte_offset_p,
6721 bit_offset_p, bit_size_p, index_p))
6722 return 1;
6723 }
6724 }
52ce6436
PH
6725 else if (index_p != NULL)
6726 *index_p += 1;
4c4b4cd2 6727 }
828d5846
XR
6728
6729 /* Field not found so far. If this is a tagged type which
6730 has a parent, try finding that field in the parent now. */
6731
6732 if (parent_offset != -1)
6733 {
4d1795ac
TT
6734 /* As above, only compute the offset when truly needed. */
6735 int fld_offset = offset;
6736 if (byte_offset_p != nullptr || bit_offset_p != nullptr)
6737 {
6738 int bit_pos = TYPE_FIELD_BITPOS (type, parent_offset);
6739 fld_offset += bit_pos / 8;
6740 }
828d5846 6741
940da03e 6742 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
6743 fld_offset, field_type_p, byte_offset_p,
6744 bit_offset_p, bit_size_p, index_p))
6745 return 1;
828d5846
XR
6746 }
6747
4c4b4cd2
PH
6748 return 0;
6749}
6750
0963b4bd 6751/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 6752
52ce6436
PH
6753static int
6754num_visible_fields (struct type *type)
6755{
6756 int n;
5b4ee69b 6757
52ce6436
PH
6758 n = 0;
6759 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
6760 return n;
6761}
14f9c5c9 6762
4c4b4cd2 6763/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
6764 and search in it assuming it has (class) type TYPE.
6765 If found, return value, else return NULL.
6766
828d5846
XR
6767 Searches recursively through wrapper fields (e.g., '_parent').
6768
6769 In the case of homonyms in the tagged types, please refer to the
6770 long explanation in find_struct_field's function documentation. */
14f9c5c9 6771
4c4b4cd2 6772static struct value *
108d56a4 6773ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 6774 struct type *type)
14f9c5c9
AS
6775{
6776 int i;
828d5846 6777 int parent_offset = -1;
14f9c5c9 6778
5b4ee69b 6779 type = ada_check_typedef (type);
1f704f76 6780 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6781 {
33d16dd9 6782 const char *t_field_name = type->field (i).name ();
14f9c5c9
AS
6783
6784 if (t_field_name == NULL)
dda83cd7 6785 continue;
14f9c5c9 6786
828d5846 6787 else if (ada_is_parent_field (type, i))
dda83cd7 6788 {
828d5846
XR
6789 /* This is a field pointing us to the parent type of a tagged
6790 type. As hinted in this function's documentation, we give
6791 preference to fields in the current record first, so what
6792 we do here is just record the index of this field before
6793 we skip it. If it turns out we couldn't find our field
6794 in the current record, then we'll get back to it and search
6795 inside it whether the field might exist in the parent. */
6796
dda83cd7
SM
6797 parent_offset = i;
6798 continue;
6799 }
828d5846 6800
14f9c5c9 6801 else if (field_name_match (t_field_name, name))
dda83cd7 6802 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
6803
6804 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
6805 {
6806 struct value *v = /* Do not let indent join lines here. */
6807 ada_search_struct_field (name, arg,
6808 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6809 type->field (i).type ());
5b4ee69b 6810
dda83cd7
SM
6811 if (v != NULL)
6812 return v;
6813 }
14f9c5c9
AS
6814
6815 else if (ada_is_variant_part (type, i))
dda83cd7 6816 {
0963b4bd 6817 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
6818 int j;
6819 struct type *field_type = ada_check_typedef (type->field (i).type ());
6820 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
4c4b4cd2 6821
dda83cd7
SM
6822 for (j = 0; j < field_type->num_fields (); j += 1)
6823 {
6824 struct value *v = ada_search_struct_field /* Force line
0963b4bd 6825 break. */
dda83cd7
SM
6826 (name, arg,
6827 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
6828 field_type->field (j).type ());
5b4ee69b 6829
dda83cd7
SM
6830 if (v != NULL)
6831 return v;
6832 }
6833 }
14f9c5c9 6834 }
828d5846
XR
6835
6836 /* Field not found so far. If this is a tagged type which
6837 has a parent, try finding that field in the parent now. */
6838
6839 if (parent_offset != -1)
6840 {
6841 struct value *v = ada_search_struct_field (
6842 name, arg, offset + TYPE_FIELD_BITPOS (type, parent_offset) / 8,
940da03e 6843 type->field (parent_offset).type ());
828d5846
XR
6844
6845 if (v != NULL)
dda83cd7 6846 return v;
828d5846
XR
6847 }
6848
14f9c5c9
AS
6849 return NULL;
6850}
d2e4a39e 6851
52ce6436
PH
6852static struct value *ada_index_struct_field_1 (int *, struct value *,
6853 int, struct type *);
6854
6855
6856/* Return field #INDEX in ARG, where the index is that returned by
6857 * find_struct_field through its INDEX_P argument. Adjust the address
6858 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 6859 * If found, return value, else return NULL. */
52ce6436
PH
6860
6861static struct value *
6862ada_index_struct_field (int index, struct value *arg, int offset,
6863 struct type *type)
6864{
6865 return ada_index_struct_field_1 (&index, arg, offset, type);
6866}
6867
6868
6869/* Auxiliary function for ada_index_struct_field. Like
6870 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 6871 * *INDEX_P. */
52ce6436
PH
6872
6873static struct value *
6874ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
6875 struct type *type)
6876{
6877 int i;
6878 type = ada_check_typedef (type);
6879
1f704f76 6880 for (i = 0; i < type->num_fields (); i += 1)
52ce6436 6881 {
33d16dd9 6882 if (type->field (i).name () == NULL)
dda83cd7 6883 continue;
52ce6436 6884 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
6885 {
6886 struct value *v = /* Do not let indent join lines here. */
6887 ada_index_struct_field_1 (index_p, arg,
52ce6436 6888 offset + TYPE_FIELD_BITPOS (type, i) / 8,
940da03e 6889 type->field (i).type ());
5b4ee69b 6890
dda83cd7
SM
6891 if (v != NULL)
6892 return v;
6893 }
52ce6436
PH
6894
6895 else if (ada_is_variant_part (type, i))
dda83cd7 6896 {
52ce6436 6897 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 6898 find_struct_field. */
52ce6436 6899 error (_("Cannot assign this kind of variant record"));
dda83cd7 6900 }
52ce6436 6901 else if (*index_p == 0)
dda83cd7 6902 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
6903 else
6904 *index_p -= 1;
6905 }
6906 return NULL;
6907}
6908
3b4de39c 6909/* Return a string representation of type TYPE. */
99bbb428 6910
3b4de39c 6911static std::string
99bbb428
PA
6912type_as_string (struct type *type)
6913{
d7e74731 6914 string_file tmp_stream;
99bbb428 6915
d7e74731 6916 type_print (type, "", &tmp_stream, -1);
99bbb428 6917
d7e74731 6918 return std::move (tmp_stream.string ());
99bbb428
PA
6919}
6920
14f9c5c9 6921/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
6922 If DISPP is non-null, add its byte displacement from the beginning of a
6923 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
6924 work for packed fields).
6925
6926 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 6927 followed by "___".
14f9c5c9 6928
0963b4bd 6929 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
6930 be a (pointer or reference)+ to a struct or union, and the
6931 ultimate target type will be searched.
14f9c5c9
AS
6932
6933 Looks recursively into variant clauses and parent types.
6934
828d5846
XR
6935 In the case of homonyms in the tagged types, please refer to the
6936 long explanation in find_struct_field's function documentation.
6937
4c4b4cd2
PH
6938 If NOERR is nonzero, return NULL if NAME is not suitably defined or
6939 TYPE is not a type of the right kind. */
14f9c5c9 6940
4c4b4cd2 6941static struct type *
a121b7c1 6942ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 6943 int noerr)
14f9c5c9
AS
6944{
6945 int i;
828d5846 6946 int parent_offset = -1;
14f9c5c9
AS
6947
6948 if (name == NULL)
6949 goto BadName;
6950
76a01679 6951 if (refok && type != NULL)
4c4b4cd2
PH
6952 while (1)
6953 {
dda83cd7
SM
6954 type = ada_check_typedef (type);
6955 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
6956 break;
6957 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 6958 }
14f9c5c9 6959
76a01679 6960 if (type == NULL
78134374
SM
6961 || (type->code () != TYPE_CODE_STRUCT
6962 && type->code () != TYPE_CODE_UNION))
14f9c5c9 6963 {
4c4b4cd2 6964 if (noerr)
dda83cd7 6965 return NULL;
99bbb428 6966
3b4de39c
PA
6967 error (_("Type %s is not a structure or union type"),
6968 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
6969 }
6970
6971 type = to_static_fixed_type (type);
6972
1f704f76 6973 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6974 {
33d16dd9 6975 const char *t_field_name = type->field (i).name ();
14f9c5c9 6976 struct type *t;
d2e4a39e 6977
14f9c5c9 6978 if (t_field_name == NULL)
dda83cd7 6979 continue;
14f9c5c9 6980
828d5846 6981 else if (ada_is_parent_field (type, i))
dda83cd7 6982 {
828d5846
XR
6983 /* This is a field pointing us to the parent type of a tagged
6984 type. As hinted in this function's documentation, we give
6985 preference to fields in the current record first, so what
6986 we do here is just record the index of this field before
6987 we skip it. If it turns out we couldn't find our field
6988 in the current record, then we'll get back to it and search
6989 inside it whether the field might exist in the parent. */
6990
dda83cd7
SM
6991 parent_offset = i;
6992 continue;
6993 }
828d5846 6994
14f9c5c9 6995 else if (field_name_match (t_field_name, name))
940da03e 6996 return type->field (i).type ();
14f9c5c9
AS
6997
6998 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
6999 {
7000 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7001 0, 1);
7002 if (t != NULL)
988f6b3d 7003 return t;
dda83cd7 7004 }
14f9c5c9
AS
7005
7006 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7007 {
7008 int j;
7009 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7010
dda83cd7
SM
7011 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7012 {
b1f33ddd 7013 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7014 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7015 generates these for unchecked variant types. Revisit
dda83cd7 7016 if the compiler changes this practice. */
33d16dd9 7017 const char *v_field_name = field_type->field (j).name ();
988f6b3d 7018
b1f33ddd
JB
7019 if (v_field_name != NULL
7020 && field_name_match (v_field_name, name))
940da03e 7021 t = field_type->field (j).type ();
b1f33ddd 7022 else
940da03e 7023 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7024 name, 0, 1);
b1f33ddd 7025
dda83cd7 7026 if (t != NULL)
988f6b3d 7027 return t;
dda83cd7
SM
7028 }
7029 }
14f9c5c9
AS
7030
7031 }
7032
828d5846
XR
7033 /* Field not found so far. If this is a tagged type which
7034 has a parent, try finding that field in the parent now. */
7035
7036 if (parent_offset != -1)
7037 {
dda83cd7 7038 struct type *t;
828d5846 7039
dda83cd7
SM
7040 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7041 name, 0, 1);
7042 if (t != NULL)
828d5846
XR
7043 return t;
7044 }
7045
14f9c5c9 7046BadName:
d2e4a39e 7047 if (!noerr)
14f9c5c9 7048 {
2b2798cc 7049 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7050
7051 error (_("Type %s has no component named %s"),
3b4de39c 7052 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7053 }
7054
7055 return NULL;
7056}
7057
b1f33ddd
JB
7058/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7059 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7060 represents an unchecked union (that is, the variant part of a
0963b4bd 7061 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7062
7063static int
7064is_unchecked_variant (struct type *var_type, struct type *outer_type)
7065{
a121b7c1 7066 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7067
988f6b3d 7068 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7069}
7070
7071
14f9c5c9 7072/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7073 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7074 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7075
d2e4a39e 7076int
d8af9068 7077ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7078{
7079 int others_clause;
7080 int i;
a121b7c1 7081 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7082 struct value *discrim;
14f9c5c9
AS
7083 LONGEST discrim_val;
7084
012370f6
TT
7085 /* Using plain value_from_contents_and_address here causes problems
7086 because we will end up trying to resolve a type that is currently
7087 being constructed. */
0c281816
JB
7088 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7089 if (discrim == NULL)
14f9c5c9 7090 return -1;
0c281816 7091 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7092
7093 others_clause = -1;
1f704f76 7094 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7095 {
7096 if (ada_is_others_clause (var_type, i))
dda83cd7 7097 others_clause = i;
14f9c5c9 7098 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7099 return i;
14f9c5c9
AS
7100 }
7101
7102 return others_clause;
7103}
d2e4a39e 7104\f
14f9c5c9
AS
7105
7106
dda83cd7 7107 /* Dynamic-Sized Records */
14f9c5c9
AS
7108
7109/* Strategy: The type ostensibly attached to a value with dynamic size
7110 (i.e., a size that is not statically recorded in the debugging
7111 data) does not accurately reflect the size or layout of the value.
7112 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7113 conventional types that are constructed on the fly. */
14f9c5c9
AS
7114
7115/* There is a subtle and tricky problem here. In general, we cannot
7116 determine the size of dynamic records without its data. However,
7117 the 'struct value' data structure, which GDB uses to represent
7118 quantities in the inferior process (the target), requires the size
7119 of the type at the time of its allocation in order to reserve space
7120 for GDB's internal copy of the data. That's why the
7121 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7122 rather than struct value*s.
14f9c5c9
AS
7123
7124 However, GDB's internal history variables ($1, $2, etc.) are
7125 struct value*s containing internal copies of the data that are not, in
7126 general, the same as the data at their corresponding addresses in
7127 the target. Fortunately, the types we give to these values are all
7128 conventional, fixed-size types (as per the strategy described
7129 above), so that we don't usually have to perform the
7130 'to_fixed_xxx_type' conversions to look at their values.
7131 Unfortunately, there is one exception: if one of the internal
7132 history variables is an array whose elements are unconstrained
7133 records, then we will need to create distinct fixed types for each
7134 element selected. */
7135
7136/* The upshot of all of this is that many routines take a (type, host
7137 address, target address) triple as arguments to represent a value.
7138 The host address, if non-null, is supposed to contain an internal
7139 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7140 target at the target address. */
14f9c5c9
AS
7141
7142/* Assuming that VAL0 represents a pointer value, the result of
7143 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7144 dynamic-sized types. */
14f9c5c9 7145
d2e4a39e
AS
7146struct value *
7147ada_value_ind (struct value *val0)
14f9c5c9 7148{
c48db5ca 7149 struct value *val = value_ind (val0);
5b4ee69b 7150
b50d69b5
JG
7151 if (ada_is_tagged_type (value_type (val), 0))
7152 val = ada_tag_value_at_base_address (val);
7153
4c4b4cd2 7154 return ada_to_fixed_value (val);
14f9c5c9
AS
7155}
7156
7157/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7158 qualifiers on VAL0. */
7159
d2e4a39e
AS
7160static struct value *
7161ada_coerce_ref (struct value *val0)
7162{
78134374 7163 if (value_type (val0)->code () == TYPE_CODE_REF)
d2e4a39e
AS
7164 {
7165 struct value *val = val0;
5b4ee69b 7166
994b9211 7167 val = coerce_ref (val);
b50d69b5
JG
7168
7169 if (ada_is_tagged_type (value_type (val), 0))
7170 val = ada_tag_value_at_base_address (val);
7171
4c4b4cd2 7172 return ada_to_fixed_value (val);
d2e4a39e
AS
7173 }
7174 else
14f9c5c9
AS
7175 return val0;
7176}
7177
4c4b4cd2 7178/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7179
7180static unsigned int
ebf56fd3 7181field_alignment (struct type *type, int f)
14f9c5c9 7182{
33d16dd9 7183 const char *name = type->field (f).name ();
64a1bf19 7184 int len;
14f9c5c9
AS
7185 int align_offset;
7186
64a1bf19
JB
7187 /* The field name should never be null, unless the debugging information
7188 is somehow malformed. In this case, we assume the field does not
7189 require any alignment. */
7190 if (name == NULL)
7191 return 1;
7192
7193 len = strlen (name);
7194
4c4b4cd2
PH
7195 if (!isdigit (name[len - 1]))
7196 return 1;
14f9c5c9 7197
d2e4a39e 7198 if (isdigit (name[len - 2]))
14f9c5c9
AS
7199 align_offset = len - 2;
7200 else
7201 align_offset = len - 1;
7202
61012eef 7203 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7204 return TARGET_CHAR_BIT;
7205
4c4b4cd2
PH
7206 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7207}
7208
852dff6c 7209/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7210
852dff6c
JB
7211static struct symbol *
7212ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7213{
7214 struct symbol *sym;
7215
7216 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
4186eb54 7217 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4c4b4cd2
PH
7218 return sym;
7219
4186eb54
KS
7220 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7221 return sym;
14f9c5c9
AS
7222}
7223
dddfab26
UW
7224/* Find a type named NAME. Ignores ambiguity. This routine will look
7225 solely for types defined by debug info, it will not search the GDB
7226 primitive types. */
4c4b4cd2 7227
852dff6c 7228static struct type *
ebf56fd3 7229ada_find_any_type (const char *name)
14f9c5c9 7230{
852dff6c 7231 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7232
14f9c5c9 7233 if (sym != NULL)
dddfab26 7234 return SYMBOL_TYPE (sym);
14f9c5c9 7235
dddfab26 7236 return NULL;
14f9c5c9
AS
7237}
7238
739593e0
JB
7239/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7240 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7241 symbol, in which case it is returned. Otherwise, this looks for
7242 symbols whose name is that of NAME_SYM suffixed with "___XR".
7243 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7244
c0e70c62
TT
7245static bool
7246ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7247{
987012b8 7248 const char *name = name_sym->linkage_name ();
c0e70c62 7249 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7250}
7251
14f9c5c9 7252/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7253 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7254 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7255 otherwise return 0. */
7256
14f9c5c9 7257int
d2e4a39e 7258ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7259{
7260 if (type1 == NULL)
7261 return 1;
7262 else if (type0 == NULL)
7263 return 0;
78134374 7264 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7265 return 1;
78134374 7266 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7267 return 0;
7d93a1e0 7268 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7269 return 1;
ad82864c 7270 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7271 return 1;
4c4b4cd2 7272 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7273 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7274 return 1;
aeb5907d
JB
7275 else
7276 {
7d93a1e0
SM
7277 const char *type0_name = type0->name ();
7278 const char *type1_name = type1->name ();
aeb5907d
JB
7279
7280 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7281 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7282 return 1;
7283 }
14f9c5c9
AS
7284 return 0;
7285}
7286
e86ca25f
TT
7287/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7288 null. */
4c4b4cd2 7289
0d5cff50 7290const char *
d2e4a39e 7291ada_type_name (struct type *type)
14f9c5c9 7292{
d2e4a39e 7293 if (type == NULL)
14f9c5c9 7294 return NULL;
7d93a1e0 7295 return type->name ();
14f9c5c9
AS
7296}
7297
b4ba55a1
JB
7298/* Search the list of "descriptive" types associated to TYPE for a type
7299 whose name is NAME. */
7300
7301static struct type *
7302find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7303{
931e5bc3 7304 struct type *result, *tmp;
b4ba55a1 7305
c6044dd1
JB
7306 if (ada_ignore_descriptive_types_p)
7307 return NULL;
7308
b4ba55a1
JB
7309 /* If there no descriptive-type info, then there is no parallel type
7310 to be found. */
7311 if (!HAVE_GNAT_AUX_INFO (type))
7312 return NULL;
7313
7314 result = TYPE_DESCRIPTIVE_TYPE (type);
7315 while (result != NULL)
7316 {
0d5cff50 7317 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7318
7319 if (result_name == NULL)
dda83cd7
SM
7320 {
7321 warning (_("unexpected null name on descriptive type"));
7322 return NULL;
7323 }
b4ba55a1
JB
7324
7325 /* If the names match, stop. */
7326 if (strcmp (result_name, name) == 0)
7327 break;
7328
7329 /* Otherwise, look at the next item on the list, if any. */
7330 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7331 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7332 else
7333 tmp = NULL;
7334
7335 /* If not found either, try after having resolved the typedef. */
7336 if (tmp != NULL)
7337 result = tmp;
b4ba55a1 7338 else
931e5bc3 7339 {
f168693b 7340 result = check_typedef (result);
931e5bc3
JG
7341 if (HAVE_GNAT_AUX_INFO (result))
7342 result = TYPE_DESCRIPTIVE_TYPE (result);
7343 else
7344 result = NULL;
7345 }
b4ba55a1
JB
7346 }
7347
7348 /* If we didn't find a match, see whether this is a packed array. With
7349 older compilers, the descriptive type information is either absent or
7350 irrelevant when it comes to packed arrays so the above lookup fails.
7351 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7352 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7353 return ada_find_any_type (name);
7354
7355 return result;
7356}
7357
7358/* Find a parallel type to TYPE with the specified NAME, using the
7359 descriptive type taken from the debugging information, if available,
7360 and otherwise using the (slower) name-based method. */
7361
7362static struct type *
7363ada_find_parallel_type_with_name (struct type *type, const char *name)
7364{
7365 struct type *result = NULL;
7366
7367 if (HAVE_GNAT_AUX_INFO (type))
7368 result = find_parallel_type_by_descriptive_type (type, name);
7369 else
7370 result = ada_find_any_type (name);
7371
7372 return result;
7373}
7374
7375/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7376 SUFFIX to the name of TYPE. */
14f9c5c9 7377
d2e4a39e 7378struct type *
ebf56fd3 7379ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7380{
0d5cff50 7381 char *name;
fe978cb0 7382 const char *type_name = ada_type_name (type);
14f9c5c9 7383 int len;
d2e4a39e 7384
fe978cb0 7385 if (type_name == NULL)
14f9c5c9
AS
7386 return NULL;
7387
fe978cb0 7388 len = strlen (type_name);
14f9c5c9 7389
b4ba55a1 7390 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7391
fe978cb0 7392 strcpy (name, type_name);
14f9c5c9
AS
7393 strcpy (name + len, suffix);
7394
b4ba55a1 7395 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7396}
7397
14f9c5c9 7398/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7399 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7400
d2e4a39e
AS
7401static struct type *
7402dynamic_template_type (struct type *type)
14f9c5c9 7403{
61ee279c 7404 type = ada_check_typedef (type);
14f9c5c9 7405
78134374 7406 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7407 || ada_type_name (type) == NULL)
14f9c5c9 7408 return NULL;
d2e4a39e 7409 else
14f9c5c9
AS
7410 {
7411 int len = strlen (ada_type_name (type));
5b4ee69b 7412
4c4b4cd2 7413 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7414 return type;
14f9c5c9 7415 else
dda83cd7 7416 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7417 }
7418}
7419
7420/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7421 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7422
d2e4a39e
AS
7423static int
7424is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9 7425{
33d16dd9 7426 const char *name = templ_type->field (field_num).name ();
5b4ee69b 7427
d2e4a39e 7428 return name != NULL
940da03e 7429 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7430 && strstr (name, "___XVL") != NULL;
7431}
7432
4c4b4cd2
PH
7433/* The index of the variant field of TYPE, or -1 if TYPE does not
7434 represent a variant record type. */
14f9c5c9 7435
d2e4a39e 7436static int
4c4b4cd2 7437variant_field_index (struct type *type)
14f9c5c9
AS
7438{
7439 int f;
7440
78134374 7441 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7442 return -1;
7443
1f704f76 7444 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7445 {
7446 if (ada_is_variant_part (type, f))
dda83cd7 7447 return f;
4c4b4cd2
PH
7448 }
7449 return -1;
14f9c5c9
AS
7450}
7451
4c4b4cd2
PH
7452/* A record type with no fields. */
7453
d2e4a39e 7454static struct type *
fe978cb0 7455empty_record (struct type *templ)
14f9c5c9 7456{
fe978cb0 7457 struct type *type = alloc_type_copy (templ);
5b4ee69b 7458
67607e24 7459 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7460 INIT_NONE_SPECIFIC (type);
d0e39ea2 7461 type->set_name ("<empty>");
14f9c5c9
AS
7462 TYPE_LENGTH (type) = 0;
7463 return type;
7464}
7465
7466/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7467 the value of type TYPE at VALADDR or ADDRESS (see comments at
7468 the beginning of this section) VAL according to GNAT conventions.
7469 DVAL0 should describe the (portion of a) record that contains any
df407dfe 7470 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
7471 an outer-level type (i.e., as opposed to a branch of a variant.) A
7472 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7473 of the variant.
14f9c5c9 7474
4c4b4cd2
PH
7475 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7476 length are not statically known are discarded. As a consequence,
7477 VALADDR, ADDRESS and DVAL0 are ignored.
7478
7479 NOTE: Limitations: For now, we assume that dynamic fields and
7480 variants occupy whole numbers of bytes. However, they need not be
7481 byte-aligned. */
7482
7483struct type *
10a2c479 7484ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7485 const gdb_byte *valaddr,
dda83cd7
SM
7486 CORE_ADDR address, struct value *dval0,
7487 int keep_dynamic_fields)
14f9c5c9 7488{
d2e4a39e
AS
7489 struct value *mark = value_mark ();
7490 struct value *dval;
7491 struct type *rtype;
14f9c5c9 7492 int nfields, bit_len;
4c4b4cd2 7493 int variant_field;
14f9c5c9 7494 long off;
d94e4f4f 7495 int fld_bit_len;
14f9c5c9
AS
7496 int f;
7497
4c4b4cd2
PH
7498 /* Compute the number of fields in this record type that are going
7499 to be processed: unless keep_dynamic_fields, this includes only
7500 fields whose position and length are static will be processed. */
7501 if (keep_dynamic_fields)
1f704f76 7502 nfields = type->num_fields ();
4c4b4cd2
PH
7503 else
7504 {
7505 nfields = 0;
1f704f76 7506 while (nfields < type->num_fields ()
dda83cd7
SM
7507 && !ada_is_variant_part (type, nfields)
7508 && !is_dynamic_field (type, nfields))
7509 nfields++;
4c4b4cd2
PH
7510 }
7511
e9bb382b 7512 rtype = alloc_type_copy (type);
67607e24 7513 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7514 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7515 rtype->set_num_fields (nfields);
3cabb6b0
SM
7516 rtype->set_fields
7517 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7518 rtype->set_name (ada_type_name (type));
9cdd0d12 7519 rtype->set_is_fixed_instance (true);
14f9c5c9 7520
d2e4a39e
AS
7521 off = 0;
7522 bit_len = 0;
4c4b4cd2
PH
7523 variant_field = -1;
7524
14f9c5c9
AS
7525 for (f = 0; f < nfields; f += 1)
7526 {
a89febbd 7527 off = align_up (off, field_alignment (type, f))
6c038f32 7528 + TYPE_FIELD_BITPOS (type, f);
cd3f655c 7529 rtype->field (f).set_loc_bitpos (off);
d2e4a39e 7530 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7531
d2e4a39e 7532 if (ada_is_variant_part (type, f))
dda83cd7
SM
7533 {
7534 variant_field = f;
7535 fld_bit_len = 0;
7536 }
14f9c5c9 7537 else if (is_dynamic_field (type, f))
dda83cd7 7538 {
284614f0
JB
7539 const gdb_byte *field_valaddr = valaddr;
7540 CORE_ADDR field_address = address;
7541 struct type *field_type =
940da03e 7542 TYPE_TARGET_TYPE (type->field (f).type ());
284614f0 7543
dda83cd7 7544 if (dval0 == NULL)
b5304971 7545 {
012370f6
TT
7546 /* Using plain value_from_contents_and_address here
7547 causes problems because we will end up trying to
7548 resolve a type that is currently being
7549 constructed. */
7550 dval = value_from_contents_and_address_unresolved (rtype,
7551 valaddr,
7552 address);
9f1f738a 7553 rtype = value_type (dval);
b5304971 7554 }
dda83cd7
SM
7555 else
7556 dval = dval0;
4c4b4cd2 7557
284614f0
JB
7558 /* If the type referenced by this field is an aligner type, we need
7559 to unwrap that aligner type, because its size might not be set.
7560 Keeping the aligner type would cause us to compute the wrong
7561 size for this field, impacting the offset of the all the fields
7562 that follow this one. */
7563 if (ada_is_aligner_type (field_type))
7564 {
5a8edb75 7565 long field_offset = TYPE_FIELD_BITPOS (type, f);
284614f0
JB
7566
7567 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7568 field_address = cond_offset_target (field_address, field_offset);
7569 field_type = ada_aligned_type (field_type);
7570 }
7571
7572 field_valaddr = cond_offset_host (field_valaddr,
7573 off / TARGET_CHAR_BIT);
7574 field_address = cond_offset_target (field_address,
7575 off / TARGET_CHAR_BIT);
7576
7577 /* Get the fixed type of the field. Note that, in this case,
7578 we do not want to get the real type out of the tag: if
7579 the current field is the parent part of a tagged record,
7580 we will get the tag of the object. Clearly wrong: the real
7581 type of the parent is not the real type of the child. We
7582 would end up in an infinite loop. */
7583 field_type = ada_get_base_type (field_type);
7584 field_type = ada_to_fixed_type (field_type, field_valaddr,
7585 field_address, dval, 0);
7586
5d14b6e5 7587 rtype->field (f).set_type (field_type);
33d16dd9 7588 rtype->field (f).set_name (type->field (f).name ());
27f2a97b
JB
7589 /* The multiplication can potentially overflow. But because
7590 the field length has been size-checked just above, and
7591 assuming that the maximum size is a reasonable value,
7592 an overflow should not happen in practice. So rather than
7593 adding overflow recovery code to this already complex code,
7594 we just assume that it's not going to happen. */
dda83cd7
SM
7595 fld_bit_len =
7596 TYPE_LENGTH (rtype->field (f).type ()) * TARGET_CHAR_BIT;
7597 }
14f9c5c9 7598 else
dda83cd7 7599 {
5ded5331
JB
7600 /* Note: If this field's type is a typedef, it is important
7601 to preserve the typedef layer.
7602
7603 Otherwise, we might be transforming a typedef to a fat
7604 pointer (encoding a pointer to an unconstrained array),
7605 into a basic fat pointer (encoding an unconstrained
7606 array). As both types are implemented using the same
7607 structure, the typedef is the only clue which allows us
7608 to distinguish between the two options. Stripping it
7609 would prevent us from printing this field appropriately. */
dda83cd7 7610 rtype->field (f).set_type (type->field (f).type ());
33d16dd9 7611 rtype->field (f).set_name (type->field (f).name ());
dda83cd7
SM
7612 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7613 fld_bit_len =
7614 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7615 else
5ded5331 7616 {
940da03e 7617 struct type *field_type = type->field (f).type ();
5ded5331
JB
7618
7619 /* We need to be careful of typedefs when computing
7620 the length of our field. If this is a typedef,
7621 get the length of the target type, not the length
7622 of the typedef. */
78134374 7623 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7624 field_type = ada_typedef_target_type (field_type);
7625
dda83cd7
SM
7626 fld_bit_len =
7627 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
5ded5331 7628 }
dda83cd7 7629 }
14f9c5c9 7630 if (off + fld_bit_len > bit_len)
dda83cd7 7631 bit_len = off + fld_bit_len;
d94e4f4f 7632 off += fld_bit_len;
4c4b4cd2 7633 TYPE_LENGTH (rtype) =
dda83cd7 7634 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7635 }
4c4b4cd2
PH
7636
7637 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7638 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7639 the record. This can happen in the presence of representation
7640 clauses. */
7641 if (variant_field >= 0)
7642 {
7643 struct type *branch_type;
7644
7645 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7646
7647 if (dval0 == NULL)
9f1f738a 7648 {
012370f6
TT
7649 /* Using plain value_from_contents_and_address here causes
7650 problems because we will end up trying to resolve a type
7651 that is currently being constructed. */
7652 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7653 address);
9f1f738a
SA
7654 rtype = value_type (dval);
7655 }
4c4b4cd2 7656 else
dda83cd7 7657 dval = dval0;
4c4b4cd2
PH
7658
7659 branch_type =
dda83cd7
SM
7660 to_fixed_variant_branch_type
7661 (type->field (variant_field).type (),
7662 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7663 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7664 if (branch_type == NULL)
dda83cd7
SM
7665 {
7666 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7667 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7668 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 7669 }
4c4b4cd2 7670 else
dda83cd7
SM
7671 {
7672 rtype->field (variant_field).set_type (branch_type);
d3fd12df 7673 rtype->field (variant_field).set_name ("S");
dda83cd7
SM
7674 fld_bit_len =
7675 TYPE_LENGTH (rtype->field (variant_field).type ()) *
7676 TARGET_CHAR_BIT;
7677 if (off + fld_bit_len > bit_len)
7678 bit_len = off + fld_bit_len;
7679 TYPE_LENGTH (rtype) =
7680 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7681 }
4c4b4cd2
PH
7682 }
7683
714e53ab
PH
7684 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7685 should contain the alignment of that record, which should be a strictly
7686 positive value. If null or negative, then something is wrong, most
7687 probably in the debug info. In that case, we don't round up the size
0963b4bd 7688 of the resulting type. If this record is not part of another structure,
714e53ab
PH
7689 the current RTYPE length might be good enough for our purposes. */
7690 if (TYPE_LENGTH (type) <= 0)
7691 {
7d93a1e0 7692 if (rtype->name ())
cc1defb1 7693 warning (_("Invalid type size for `%s' detected: %s."),
7d93a1e0 7694 rtype->name (), pulongest (TYPE_LENGTH (type)));
323e0a4a 7695 else
cc1defb1
KS
7696 warning (_("Invalid type size for <unnamed> detected: %s."),
7697 pulongest (TYPE_LENGTH (type)));
714e53ab
PH
7698 }
7699 else
7700 {
a89febbd
TT
7701 TYPE_LENGTH (rtype) = align_up (TYPE_LENGTH (rtype),
7702 TYPE_LENGTH (type));
714e53ab 7703 }
14f9c5c9
AS
7704
7705 value_free_to_mark (mark);
14f9c5c9
AS
7706 return rtype;
7707}
7708
4c4b4cd2
PH
7709/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7710 of 1. */
14f9c5c9 7711
d2e4a39e 7712static struct type *
fc1a4b47 7713template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 7714 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
7715{
7716 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 7717 address, dval0, 1);
4c4b4cd2
PH
7718}
7719
7720/* An ordinary record type in which ___XVL-convention fields and
7721 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7722 static approximations, containing all possible fields. Uses
7723 no runtime values. Useless for use in values, but that's OK,
7724 since the results are used only for type determinations. Works on both
7725 structs and unions. Representation note: to save space, we memorize
7726 the result of this function in the TYPE_TARGET_TYPE of the
7727 template type. */
7728
7729static struct type *
7730template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
7731{
7732 struct type *type;
7733 int nfields;
7734 int f;
7735
9e195661 7736 /* No need no do anything if the input type is already fixed. */
22c4c60c 7737 if (type0->is_fixed_instance ())
9e195661
PMR
7738 return type0;
7739
7740 /* Likewise if we already have computed the static approximation. */
4c4b4cd2
PH
7741 if (TYPE_TARGET_TYPE (type0) != NULL)
7742 return TYPE_TARGET_TYPE (type0);
7743
9e195661 7744 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 7745 type = type0;
1f704f76 7746 nfields = type0->num_fields ();
9e195661
PMR
7747
7748 /* Whether or not we cloned TYPE0, cache the result so that we don't do
7749 recompute all over next time. */
7750 TYPE_TARGET_TYPE (type0) = type;
14f9c5c9
AS
7751
7752 for (f = 0; f < nfields; f += 1)
7753 {
940da03e 7754 struct type *field_type = type0->field (f).type ();
4c4b4cd2 7755 struct type *new_type;
14f9c5c9 7756
4c4b4cd2 7757 if (is_dynamic_field (type0, f))
460efde1
JB
7758 {
7759 field_type = ada_check_typedef (field_type);
dda83cd7 7760 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
460efde1 7761 }
14f9c5c9 7762 else
dda83cd7 7763 new_type = static_unwrap_type (field_type);
9e195661
PMR
7764
7765 if (new_type != field_type)
7766 {
7767 /* Clone TYPE0 only the first time we get a new field type. */
7768 if (type == type0)
7769 {
7770 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
78134374 7771 type->set_code (type0->code ());
8ecb59f8 7772 INIT_NONE_SPECIFIC (type);
5e33d5f4 7773 type->set_num_fields (nfields);
3cabb6b0
SM
7774
7775 field *fields =
7776 ((struct field *)
7777 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 7778 memcpy (fields, type0->fields (),
9e195661 7779 sizeof (struct field) * nfields);
3cabb6b0
SM
7780 type->set_fields (fields);
7781
d0e39ea2 7782 type->set_name (ada_type_name (type0));
9cdd0d12 7783 type->set_is_fixed_instance (true);
9e195661
PMR
7784 TYPE_LENGTH (type) = 0;
7785 }
5d14b6e5 7786 type->field (f).set_type (new_type);
33d16dd9 7787 type->field (f).set_name (type0->field (f).name ());
9e195661 7788 }
14f9c5c9 7789 }
9e195661 7790
14f9c5c9
AS
7791 return type;
7792}
7793
4c4b4cd2 7794/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
7795 whose address in memory is ADDRESS, returns a revision of TYPE,
7796 which should be a non-dynamic-sized record, in which the variant
7797 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
7798 for discriminant values in DVAL0, which can be NULL if the record
7799 contains the necessary discriminant values. */
7800
d2e4a39e 7801static struct type *
fc1a4b47 7802to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 7803 CORE_ADDR address, struct value *dval0)
14f9c5c9 7804{
d2e4a39e 7805 struct value *mark = value_mark ();
4c4b4cd2 7806 struct value *dval;
d2e4a39e 7807 struct type *rtype;
14f9c5c9 7808 struct type *branch_type;
1f704f76 7809 int nfields = type->num_fields ();
4c4b4cd2 7810 int variant_field = variant_field_index (type);
14f9c5c9 7811
4c4b4cd2 7812 if (variant_field == -1)
14f9c5c9
AS
7813 return type;
7814
4c4b4cd2 7815 if (dval0 == NULL)
9f1f738a
SA
7816 {
7817 dval = value_from_contents_and_address (type, valaddr, address);
7818 type = value_type (dval);
7819 }
4c4b4cd2
PH
7820 else
7821 dval = dval0;
7822
e9bb382b 7823 rtype = alloc_type_copy (type);
67607e24 7824 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7825 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7826 rtype->set_num_fields (nfields);
3cabb6b0
SM
7827
7828 field *fields =
d2e4a39e 7829 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 7830 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
7831 rtype->set_fields (fields);
7832
d0e39ea2 7833 rtype->set_name (ada_type_name (type));
9cdd0d12 7834 rtype->set_is_fixed_instance (true);
14f9c5c9
AS
7835 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
7836
4c4b4cd2 7837 branch_type = to_fixed_variant_branch_type
940da03e 7838 (type->field (variant_field).type (),
d2e4a39e 7839 cond_offset_host (valaddr,
dda83cd7
SM
7840 TYPE_FIELD_BITPOS (type, variant_field)
7841 / TARGET_CHAR_BIT),
d2e4a39e 7842 cond_offset_target (address,
dda83cd7
SM
7843 TYPE_FIELD_BITPOS (type, variant_field)
7844 / TARGET_CHAR_BIT), dval);
d2e4a39e 7845 if (branch_type == NULL)
14f9c5c9 7846 {
4c4b4cd2 7847 int f;
5b4ee69b 7848
4c4b4cd2 7849 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 7850 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7851 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
7852 }
7853 else
7854 {
5d14b6e5 7855 rtype->field (variant_field).set_type (branch_type);
d3fd12df 7856 rtype->field (variant_field).set_name ("S");
4c4b4cd2 7857 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 7858 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 7859 }
940da03e 7860 TYPE_LENGTH (rtype) -= TYPE_LENGTH (type->field (variant_field).type ());
d2e4a39e 7861
4c4b4cd2 7862 value_free_to_mark (mark);
14f9c5c9
AS
7863 return rtype;
7864}
7865
7866/* An ordinary record type (with fixed-length fields) that describes
7867 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
7868 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
7869 should be in DVAL, a record value; it may be NULL if the object
7870 at ADDR itself contains any necessary discriminant values.
7871 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
7872 values from the record are needed. Except in the case that DVAL,
7873 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
7874 unchecked) is replaced by a particular branch of the variant.
7875
7876 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
7877 is questionable and may be removed. It can arise during the
7878 processing of an unconstrained-array-of-record type where all the
7879 variant branches have exactly the same size. This is because in
7880 such cases, the compiler does not bother to use the XVS convention
7881 when encoding the record. I am currently dubious of this
7882 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 7883
d2e4a39e 7884static struct type *
fc1a4b47 7885to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 7886 CORE_ADDR address, struct value *dval)
14f9c5c9 7887{
d2e4a39e 7888 struct type *templ_type;
14f9c5c9 7889
22c4c60c 7890 if (type0->is_fixed_instance ())
4c4b4cd2
PH
7891 return type0;
7892
d2e4a39e 7893 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
7894
7895 if (templ_type != NULL)
7896 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
7897 else if (variant_field_index (type0) >= 0)
7898 {
7899 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 7900 return type0;
4c4b4cd2 7901 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 7902 dval);
4c4b4cd2 7903 }
14f9c5c9
AS
7904 else
7905 {
9cdd0d12 7906 type0->set_is_fixed_instance (true);
14f9c5c9
AS
7907 return type0;
7908 }
7909
7910}
7911
7912/* An ordinary record type (with fixed-length fields) that describes
7913 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
7914 union type. Any necessary discriminants' values should be in DVAL,
7915 a record value. That is, this routine selects the appropriate
7916 branch of the union at ADDR according to the discriminant value
b1f33ddd 7917 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 7918 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 7919
d2e4a39e 7920static struct type *
fc1a4b47 7921to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 7922 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
7923{
7924 int which;
d2e4a39e
AS
7925 struct type *templ_type;
7926 struct type *var_type;
14f9c5c9 7927
78134374 7928 if (var_type0->code () == TYPE_CODE_PTR)
14f9c5c9 7929 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 7930 else
14f9c5c9
AS
7931 var_type = var_type0;
7932
7933 templ_type = ada_find_parallel_type (var_type, "___XVU");
7934
7935 if (templ_type != NULL)
7936 var_type = templ_type;
7937
b1f33ddd
JB
7938 if (is_unchecked_variant (var_type, value_type (dval)))
7939 return var_type0;
d8af9068 7940 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
7941
7942 if (which < 0)
e9bb382b 7943 return empty_record (var_type);
14f9c5c9 7944 else if (is_dynamic_field (var_type, which))
4c4b4cd2 7945 return to_fixed_record_type
940da03e 7946 (TYPE_TARGET_TYPE (var_type->field (which).type ()),
d2e4a39e 7947 valaddr, address, dval);
940da03e 7948 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
7949 return
7950 to_fixed_record_type
940da03e 7951 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 7952 else
940da03e 7953 return var_type->field (which).type ();
14f9c5c9
AS
7954}
7955
8908fca5
JB
7956/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
7957 ENCODING_TYPE, a type following the GNAT conventions for discrete
7958 type encodings, only carries redundant information. */
7959
7960static int
7961ada_is_redundant_range_encoding (struct type *range_type,
7962 struct type *encoding_type)
7963{
108d56a4 7964 const char *bounds_str;
8908fca5
JB
7965 int n;
7966 LONGEST lo, hi;
7967
78134374 7968 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 7969
78134374
SM
7970 if (get_base_type (range_type)->code ()
7971 != get_base_type (encoding_type)->code ())
005e2509
JB
7972 {
7973 /* The compiler probably used a simple base type to describe
7974 the range type instead of the range's actual base type,
7975 expecting us to get the real base type from the encoding
7976 anyway. In this situation, the encoding cannot be ignored
7977 as redundant. */
7978 return 0;
7979 }
7980
8908fca5
JB
7981 if (is_dynamic_type (range_type))
7982 return 0;
7983
7d93a1e0 7984 if (encoding_type->name () == NULL)
8908fca5
JB
7985 return 0;
7986
7d93a1e0 7987 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
7988 if (bounds_str == NULL)
7989 return 0;
7990
7991 n = 8; /* Skip "___XDLU_". */
7992 if (!ada_scan_number (bounds_str, n, &lo, &n))
7993 return 0;
5537ddd0 7994 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
7995 return 0;
7996
7997 n += 2; /* Skip the "__" separator between the two bounds. */
7998 if (!ada_scan_number (bounds_str, n, &hi, &n))
7999 return 0;
5537ddd0 8000 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8001 return 0;
8002
8003 return 1;
8004}
8005
8006/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8007 a type following the GNAT encoding for describing array type
8008 indices, only carries redundant information. */
8009
8010static int
8011ada_is_redundant_index_type_desc (struct type *array_type,
8012 struct type *desc_type)
8013{
8014 struct type *this_layer = check_typedef (array_type);
8015 int i;
8016
1f704f76 8017 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8018 {
3d967001 8019 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8020 desc_type->field (i).type ()))
8908fca5
JB
8021 return 0;
8022 this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
8023 }
8024
8025 return 1;
8026}
8027
14f9c5c9
AS
8028/* Assuming that TYPE0 is an array type describing the type of a value
8029 at ADDR, and that DVAL describes a record containing any
8030 discriminants used in TYPE0, returns a type for the value that
8031 contains no dynamic components (that is, no components whose sizes
8032 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8033 true, gives an error message if the resulting type's size is over
4c4b4cd2 8034 varsize_limit. */
14f9c5c9 8035
d2e4a39e
AS
8036static struct type *
8037to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8038 int ignore_too_big)
14f9c5c9 8039{
d2e4a39e
AS
8040 struct type *index_type_desc;
8041 struct type *result;
ad82864c 8042 int constrained_packed_array_p;
931e5bc3 8043 static const char *xa_suffix = "___XA";
14f9c5c9 8044
b0dd7688 8045 type0 = ada_check_typedef (type0);
22c4c60c 8046 if (type0->is_fixed_instance ())
4c4b4cd2 8047 return type0;
14f9c5c9 8048
ad82864c
JB
8049 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8050 if (constrained_packed_array_p)
75fd6a26
TT
8051 {
8052 type0 = decode_constrained_packed_array_type (type0);
8053 if (type0 == nullptr)
8054 error (_("could not decode constrained packed array type"));
8055 }
284614f0 8056
931e5bc3
JG
8057 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8058
8059 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8060 encoding suffixed with 'P' may still be generated. If so,
8061 it should be used to find the XA type. */
8062
8063 if (index_type_desc == NULL)
8064 {
1da0522e 8065 const char *type_name = ada_type_name (type0);
931e5bc3 8066
1da0522e 8067 if (type_name != NULL)
931e5bc3 8068 {
1da0522e 8069 const int len = strlen (type_name);
931e5bc3
JG
8070 char *name = (char *) alloca (len + strlen (xa_suffix));
8071
1da0522e 8072 if (type_name[len - 1] == 'P')
931e5bc3 8073 {
1da0522e 8074 strcpy (name, type_name);
931e5bc3
JG
8075 strcpy (name + len - 1, xa_suffix);
8076 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8077 }
8078 }
8079 }
8080
28c85d6c 8081 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8082 if (index_type_desc != NULL
8083 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8084 {
8085 /* Ignore this ___XA parallel type, as it does not bring any
8086 useful information. This allows us to avoid creating fixed
8087 versions of the array's index types, which would be identical
8088 to the original ones. This, in turn, can also help avoid
8089 the creation of fixed versions of the array itself. */
8090 index_type_desc = NULL;
8091 }
8092
14f9c5c9
AS
8093 if (index_type_desc == NULL)
8094 {
61ee279c 8095 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 8096
14f9c5c9 8097 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8098 depend on the contents of the array in properly constructed
8099 debugging data. */
529cad9c 8100 /* Create a fixed version of the array element type.
dda83cd7
SM
8101 We're not providing the address of an element here,
8102 and thus the actual object value cannot be inspected to do
8103 the conversion. This should not be a problem, since arrays of
8104 unconstrained objects are not allowed. In particular, all
8105 the elements of an array of a tagged type should all be of
8106 the same type specified in the debugging info. No need to
8107 consult the object tag. */
1ed6ede0 8108 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8109
284614f0
JB
8110 /* Make sure we always create a new array type when dealing with
8111 packed array types, since we're going to fix-up the array
8112 type length and element bitsize a little further down. */
ad82864c 8113 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8114 result = type0;
14f9c5c9 8115 else
dda83cd7
SM
8116 result = create_array_type (alloc_type_copy (type0),
8117 elt_type, type0->index_type ());
14f9c5c9
AS
8118 }
8119 else
8120 {
8121 int i;
8122 struct type *elt_type0;
8123
8124 elt_type0 = type0;
1f704f76 8125 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
dda83cd7 8126 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8127
8128 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8129 depend on the contents of the array in properly constructed
8130 debugging data. */
529cad9c 8131 /* Create a fixed version of the array element type.
dda83cd7
SM
8132 We're not providing the address of an element here,
8133 and thus the actual object value cannot be inspected to do
8134 the conversion. This should not be a problem, since arrays of
8135 unconstrained objects are not allowed. In particular, all
8136 the elements of an array of a tagged type should all be of
8137 the same type specified in the debugging info. No need to
8138 consult the object tag. */
1ed6ede0 8139 result =
dda83cd7 8140 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8141
8142 elt_type0 = type0;
1f704f76 8143 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8144 {
8145 struct type *range_type =
8146 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8147
dda83cd7
SM
8148 result = create_array_type (alloc_type_copy (elt_type0),
8149 result, range_type);
1ce677a4 8150 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
dda83cd7 8151 }
14f9c5c9
AS
8152 }
8153
2e6fda7d
JB
8154 /* We want to preserve the type name. This can be useful when
8155 trying to get the type name of a value that has already been
8156 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8157 result->set_name (type0->name ());
2e6fda7d 8158
ad82864c 8159 if (constrained_packed_array_p)
284614f0
JB
8160 {
8161 /* So far, the resulting type has been created as if the original
8162 type was a regular (non-packed) array type. As a result, the
8163 bitsize of the array elements needs to be set again, and the array
8164 length needs to be recomputed based on that bitsize. */
8165 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
8166 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8167
8168 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
8169 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
8170 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
dda83cd7 8171 TYPE_LENGTH (result)++;
284614f0
JB
8172 }
8173
9cdd0d12 8174 result->set_is_fixed_instance (true);
14f9c5c9 8175 return result;
d2e4a39e 8176}
14f9c5c9
AS
8177
8178
8179/* A standard type (containing no dynamically sized components)
8180 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8181 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8182 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8183 ADDRESS or in VALADDR contains these discriminants.
8184
1ed6ede0
JB
8185 If CHECK_TAG is not null, in the case of tagged types, this function
8186 attempts to locate the object's tag and use it to compute the actual
8187 type. However, when ADDRESS is null, we cannot use it to determine the
8188 location of the tag, and therefore compute the tagged type's actual type.
8189 So we return the tagged type without consulting the tag. */
529cad9c 8190
f192137b
JB
8191static struct type *
8192ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8193 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8194{
61ee279c 8195 type = ada_check_typedef (type);
8ecb59f8
TT
8196
8197 /* Only un-fixed types need to be handled here. */
8198 if (!HAVE_GNAT_AUX_INFO (type))
8199 return type;
8200
78134374 8201 switch (type->code ())
d2e4a39e
AS
8202 {
8203 default:
14f9c5c9 8204 return type;
d2e4a39e 8205 case TYPE_CODE_STRUCT:
4c4b4cd2 8206 {
dda83cd7
SM
8207 struct type *static_type = to_static_fixed_type (type);
8208 struct type *fixed_record_type =
8209 to_fixed_record_type (type, valaddr, address, NULL);
8210
8211 /* If STATIC_TYPE is a tagged type and we know the object's address,
8212 then we can determine its tag, and compute the object's actual
8213 type from there. Note that we have to use the fixed record
8214 type (the parent part of the record may have dynamic fields
8215 and the way the location of _tag is expressed may depend on
8216 them). */
8217
8218 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8219 {
b50d69b5
JG
8220 struct value *tag =
8221 value_tag_from_contents_and_address
8222 (fixed_record_type,
8223 valaddr,
8224 address);
8225 struct type *real_type = type_from_tag (tag);
8226 struct value *obj =
8227 value_from_contents_and_address (fixed_record_type,
8228 valaddr,
8229 address);
dda83cd7
SM
8230 fixed_record_type = value_type (obj);
8231 if (real_type != NULL)
8232 return to_fixed_record_type
b50d69b5
JG
8233 (real_type, NULL,
8234 value_address (ada_tag_value_at_base_address (obj)), NULL);
dda83cd7
SM
8235 }
8236
8237 /* Check to see if there is a parallel ___XVZ variable.
8238 If there is, then it provides the actual size of our type. */
8239 else if (ada_type_name (fixed_record_type) != NULL)
8240 {
8241 const char *name = ada_type_name (fixed_record_type);
8242 char *xvz_name
224c3ddb 8243 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8244 bool xvz_found = false;
dda83cd7 8245 LONGEST size;
4af88198 8246
dda83cd7 8247 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8248 try
eccab96d
JB
8249 {
8250 xvz_found = get_int_var_value (xvz_name, size);
8251 }
230d2906 8252 catch (const gdb_exception_error &except)
eccab96d
JB
8253 {
8254 /* We found the variable, but somehow failed to read
8255 its value. Rethrow the same error, but with a little
8256 bit more information, to help the user understand
8257 what went wrong (Eg: the variable might have been
8258 optimized out). */
8259 throw_error (except.error,
8260 _("unable to read value of %s (%s)"),
3d6e9d23 8261 xvz_name, except.what ());
eccab96d 8262 }
eccab96d 8263
dda83cd7
SM
8264 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
8265 {
8266 fixed_record_type = copy_type (fixed_record_type);
8267 TYPE_LENGTH (fixed_record_type) = size;
8268
8269 /* The FIXED_RECORD_TYPE may have be a stub. We have
8270 observed this when the debugging info is STABS, and
8271 apparently it is something that is hard to fix.
8272
8273 In practice, we don't need the actual type definition
8274 at all, because the presence of the XVZ variable allows us
8275 to assume that there must be a XVS type as well, which we
8276 should be able to use later, when we need the actual type
8277 definition.
8278
8279 In the meantime, pretend that the "fixed" type we are
8280 returning is NOT a stub, because this can cause trouble
8281 when using this type to create new types targeting it.
8282 Indeed, the associated creation routines often check
8283 whether the target type is a stub and will try to replace
8284 it, thus using a type with the wrong size. This, in turn,
8285 might cause the new type to have the wrong size too.
8286 Consider the case of an array, for instance, where the size
8287 of the array is computed from the number of elements in
8288 our array multiplied by the size of its element. */
b4b73759 8289 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8290 }
8291 }
8292 return fixed_record_type;
4c4b4cd2 8293 }
d2e4a39e 8294 case TYPE_CODE_ARRAY:
4c4b4cd2 8295 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8296 case TYPE_CODE_UNION:
8297 if (dval == NULL)
dda83cd7 8298 return type;
d2e4a39e 8299 else
dda83cd7 8300 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8301 }
14f9c5c9
AS
8302}
8303
f192137b
JB
8304/* The same as ada_to_fixed_type_1, except that it preserves the type
8305 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8306
8307 The typedef layer needs be preserved in order to differentiate between
8308 arrays and array pointers when both types are implemented using the same
8309 fat pointer. In the array pointer case, the pointer is encoded as
8310 a typedef of the pointer type. For instance, considering:
8311
8312 type String_Access is access String;
8313 S1 : String_Access := null;
8314
8315 To the debugger, S1 is defined as a typedef of type String. But
8316 to the user, it is a pointer. So if the user tries to print S1,
8317 we should not dereference the array, but print the array address
8318 instead.
8319
8320 If we didn't preserve the typedef layer, we would lose the fact that
8321 the type is to be presented as a pointer (needs de-reference before
8322 being printed). And we would also use the source-level type name. */
f192137b
JB
8323
8324struct type *
8325ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8326 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8327
8328{
8329 struct type *fixed_type =
8330 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8331
96dbd2c1
JB
8332 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8333 then preserve the typedef layer.
8334
8335 Implementation note: We can only check the main-type portion of
8336 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8337 from TYPE now returns a type that has the same instance flags
8338 as TYPE. For instance, if TYPE is a "typedef const", and its
8339 target type is a "struct", then the typedef elimination will return
8340 a "const" version of the target type. See check_typedef for more
8341 details about how the typedef layer elimination is done.
8342
8343 brobecker/2010-11-19: It seems to me that the only case where it is
8344 useful to preserve the typedef layer is when dealing with fat pointers.
8345 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8346 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8347 because we call check_typedef/ada_check_typedef pretty much everywhere.
8348 */
78134374 8349 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8350 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8351 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8352 return type;
8353
8354 return fixed_type;
8355}
8356
14f9c5c9 8357/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8358 TYPE0, but based on no runtime data. */
14f9c5c9 8359
d2e4a39e
AS
8360static struct type *
8361to_static_fixed_type (struct type *type0)
14f9c5c9 8362{
d2e4a39e 8363 struct type *type;
14f9c5c9
AS
8364
8365 if (type0 == NULL)
8366 return NULL;
8367
22c4c60c 8368 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8369 return type0;
8370
61ee279c 8371 type0 = ada_check_typedef (type0);
d2e4a39e 8372
78134374 8373 switch (type0->code ())
14f9c5c9
AS
8374 {
8375 default:
8376 return type0;
8377 case TYPE_CODE_STRUCT:
8378 type = dynamic_template_type (type0);
d2e4a39e 8379 if (type != NULL)
dda83cd7 8380 return template_to_static_fixed_type (type);
4c4b4cd2 8381 else
dda83cd7 8382 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8383 case TYPE_CODE_UNION:
8384 type = ada_find_parallel_type (type0, "___XVU");
8385 if (type != NULL)
dda83cd7 8386 return template_to_static_fixed_type (type);
4c4b4cd2 8387 else
dda83cd7 8388 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8389 }
8390}
8391
4c4b4cd2
PH
8392/* A static approximation of TYPE with all type wrappers removed. */
8393
d2e4a39e
AS
8394static struct type *
8395static_unwrap_type (struct type *type)
14f9c5c9
AS
8396{
8397 if (ada_is_aligner_type (type))
8398 {
940da03e 8399 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8400 if (ada_type_name (type1) == NULL)
d0e39ea2 8401 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8402
8403 return static_unwrap_type (type1);
8404 }
d2e4a39e 8405 else
14f9c5c9 8406 {
d2e4a39e 8407 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8408
d2e4a39e 8409 if (raw_real_type == type)
dda83cd7 8410 return type;
14f9c5c9 8411 else
dda83cd7 8412 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8413 }
8414}
8415
8416/* In some cases, incomplete and private types require
4c4b4cd2 8417 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8418 type Foo;
8419 type FooP is access Foo;
8420 V: FooP;
8421 type Foo is array ...;
4c4b4cd2 8422 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8423 cross-references to such types, we instead substitute for FooP a
8424 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8425 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8426
8427/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8428 exists, otherwise TYPE. */
8429
d2e4a39e 8430struct type *
61ee279c 8431ada_check_typedef (struct type *type)
14f9c5c9 8432{
727e3d2e
JB
8433 if (type == NULL)
8434 return NULL;
8435
736ade86
XR
8436 /* If our type is an access to an unconstrained array, which is encoded
8437 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8438 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8439 what allows us to distinguish between fat pointers that represent
8440 array types, and fat pointers that represent array access types
8441 (in both cases, the compiler implements them as fat pointers). */
736ade86 8442 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8443 return type;
8444
f168693b 8445 type = check_typedef (type);
78134374 8446 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8447 || !type->is_stub ()
7d93a1e0 8448 || type->name () == NULL)
14f9c5c9 8449 return type;
d2e4a39e 8450 else
14f9c5c9 8451 {
7d93a1e0 8452 const char *name = type->name ();
d2e4a39e 8453 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8454
05e522ef 8455 if (type1 == NULL)
dda83cd7 8456 return type;
05e522ef
JB
8457
8458 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8459 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8460 types, only for the typedef-to-array types). If that's the case,
8461 strip the typedef layer. */
78134374 8462 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8463 type1 = ada_check_typedef (type1);
8464
8465 return type1;
14f9c5c9
AS
8466 }
8467}
8468
8469/* A value representing the data at VALADDR/ADDRESS as described by
8470 type TYPE0, but with a standard (static-sized) type that correctly
8471 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8472 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8473 creation of struct values]. */
14f9c5c9 8474
4c4b4cd2
PH
8475static struct value *
8476ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8477 struct value *val0)
14f9c5c9 8478{
1ed6ede0 8479 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8480
14f9c5c9
AS
8481 if (type == type0 && val0 != NULL)
8482 return val0;
cc0e770c
JB
8483
8484 if (VALUE_LVAL (val0) != lval_memory)
8485 {
8486 /* Our value does not live in memory; it could be a convenience
8487 variable, for instance. Create a not_lval value using val0's
8488 contents. */
50888e42 8489 return value_from_contents (type, value_contents (val0).data ());
cc0e770c
JB
8490 }
8491
8492 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8493}
8494
8495/* A value representing VAL, but with a standard (static-sized) type
8496 that correctly describes it. Does not necessarily create a new
8497 value. */
8498
0c3acc09 8499struct value *
4c4b4cd2
PH
8500ada_to_fixed_value (struct value *val)
8501{
c48db5ca 8502 val = unwrap_value (val);
d8ce9127 8503 val = ada_to_fixed_value_create (value_type (val), value_address (val), val);
c48db5ca 8504 return val;
14f9c5c9 8505}
d2e4a39e 8506\f
14f9c5c9 8507
14f9c5c9
AS
8508/* Attributes */
8509
4c4b4cd2
PH
8510/* Table mapping attribute numbers to names.
8511 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8512
27087b7f 8513static const char * const attribute_names[] = {
14f9c5c9
AS
8514 "<?>",
8515
d2e4a39e 8516 "first",
14f9c5c9
AS
8517 "last",
8518 "length",
8519 "image",
14f9c5c9
AS
8520 "max",
8521 "min",
4c4b4cd2
PH
8522 "modulus",
8523 "pos",
8524 "size",
8525 "tag",
14f9c5c9 8526 "val",
14f9c5c9
AS
8527 0
8528};
8529
de93309a 8530static const char *
4c4b4cd2 8531ada_attribute_name (enum exp_opcode n)
14f9c5c9 8532{
4c4b4cd2
PH
8533 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8534 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8535 else
8536 return attribute_names[0];
8537}
8538
4c4b4cd2 8539/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8540
4c4b4cd2
PH
8541static LONGEST
8542pos_atr (struct value *arg)
14f9c5c9 8543{
24209737
PH
8544 struct value *val = coerce_ref (arg);
8545 struct type *type = value_type (val);
14f9c5c9 8546
d2e4a39e 8547 if (!discrete_type_p (type))
323e0a4a 8548 error (_("'POS only defined on discrete types"));
14f9c5c9 8549
6244c119
SM
8550 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8551 if (!result.has_value ())
aa715135 8552 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8553
6244c119 8554 return *result;
4c4b4cd2
PH
8555}
8556
7631cf6c 8557struct value *
7992accc
TT
8558ada_pos_atr (struct type *expect_type,
8559 struct expression *exp,
8560 enum noside noside, enum exp_opcode op,
8561 struct value *arg)
4c4b4cd2 8562{
7992accc
TT
8563 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8564 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8565 return value_zero (type, not_lval);
3cb382c9 8566 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8567}
8568
4c4b4cd2 8569/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8570
d2e4a39e 8571static struct value *
53a47a3e 8572val_atr (struct type *type, LONGEST val)
14f9c5c9 8573{
53a47a3e 8574 gdb_assert (discrete_type_p (type));
0bc2354b
TT
8575 if (type->code () == TYPE_CODE_RANGE)
8576 type = TYPE_TARGET_TYPE (type);
78134374 8577 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8578 {
53a47a3e 8579 if (val < 0 || val >= type->num_fields ())
dda83cd7 8580 error (_("argument to 'VAL out of range"));
53a47a3e 8581 val = TYPE_FIELD_ENUMVAL (type, val);
14f9c5c9 8582 }
53a47a3e
TT
8583 return value_from_longest (type, val);
8584}
8585
9e99f48f 8586struct value *
3848abd6 8587ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8588{
3848abd6
TT
8589 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8590 return value_zero (type, not_lval);
8591
53a47a3e
TT
8592 if (!discrete_type_p (type))
8593 error (_("'VAL only defined on discrete types"));
8594 if (!integer_type_p (value_type (arg)))
8595 error (_("'VAL requires integral argument"));
8596
8597 return val_atr (type, value_as_long (arg));
14f9c5c9 8598}
14f9c5c9 8599\f
d2e4a39e 8600
dda83cd7 8601 /* Evaluation */
14f9c5c9 8602
4c4b4cd2
PH
8603/* True if TYPE appears to be an Ada character type.
8604 [At the moment, this is true only for Character and Wide_Character;
8605 It is a heuristic test that could stand improvement]. */
14f9c5c9 8606
fc913e53 8607bool
d2e4a39e 8608ada_is_character_type (struct type *type)
14f9c5c9 8609{
7b9f71f2
JB
8610 const char *name;
8611
8612 /* If the type code says it's a character, then assume it really is,
8613 and don't check any further. */
78134374 8614 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8615 return true;
7b9f71f2
JB
8616
8617 /* Otherwise, assume it's a character type iff it is a discrete type
8618 with a known character type name. */
8619 name = ada_type_name (type);
8620 return (name != NULL
dda83cd7
SM
8621 && (type->code () == TYPE_CODE_INT
8622 || type->code () == TYPE_CODE_RANGE)
8623 && (strcmp (name, "character") == 0
8624 || strcmp (name, "wide_character") == 0
8625 || strcmp (name, "wide_wide_character") == 0
8626 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8627}
8628
4c4b4cd2 8629/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8630
fc913e53 8631bool
ebf56fd3 8632ada_is_string_type (struct type *type)
14f9c5c9 8633{
61ee279c 8634 type = ada_check_typedef (type);
d2e4a39e 8635 if (type != NULL
78134374 8636 && type->code () != TYPE_CODE_PTR
76a01679 8637 && (ada_is_simple_array_type (type)
dda83cd7 8638 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8639 && ada_array_arity (type) == 1)
8640 {
8641 struct type *elttype = ada_array_element_type (type, 1);
8642
8643 return ada_is_character_type (elttype);
8644 }
d2e4a39e 8645 else
fc913e53 8646 return false;
14f9c5c9
AS
8647}
8648
5bf03f13
JB
8649/* The compiler sometimes provides a parallel XVS type for a given
8650 PAD type. Normally, it is safe to follow the PAD type directly,
8651 but older versions of the compiler have a bug that causes the offset
8652 of its "F" field to be wrong. Following that field in that case
8653 would lead to incorrect results, but this can be worked around
8654 by ignoring the PAD type and using the associated XVS type instead.
8655
8656 Set to True if the debugger should trust the contents of PAD types.
8657 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8658static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8659
8660/* True if TYPE is a struct type introduced by the compiler to force the
8661 alignment of a value. Such types have a single field with a
4c4b4cd2 8662 distinctive name. */
14f9c5c9
AS
8663
8664int
ebf56fd3 8665ada_is_aligner_type (struct type *type)
14f9c5c9 8666{
61ee279c 8667 type = ada_check_typedef (type);
714e53ab 8668
5bf03f13 8669 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
8670 return 0;
8671
78134374 8672 return (type->code () == TYPE_CODE_STRUCT
dda83cd7 8673 && type->num_fields () == 1
33d16dd9 8674 && strcmp (type->field (0).name (), "F") == 0);
14f9c5c9
AS
8675}
8676
8677/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 8678 the parallel type. */
14f9c5c9 8679
d2e4a39e
AS
8680struct type *
8681ada_get_base_type (struct type *raw_type)
14f9c5c9 8682{
d2e4a39e
AS
8683 struct type *real_type_namer;
8684 struct type *raw_real_type;
14f9c5c9 8685
78134374 8686 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
8687 return raw_type;
8688
284614f0
JB
8689 if (ada_is_aligner_type (raw_type))
8690 /* The encoding specifies that we should always use the aligner type.
8691 So, even if this aligner type has an associated XVS type, we should
8692 simply ignore it.
8693
8694 According to the compiler gurus, an XVS type parallel to an aligner
8695 type may exist because of a stabs limitation. In stabs, aligner
8696 types are empty because the field has a variable-sized type, and
8697 thus cannot actually be used as an aligner type. As a result,
8698 we need the associated parallel XVS type to decode the type.
8699 Since the policy in the compiler is to not change the internal
8700 representation based on the debugging info format, we sometimes
8701 end up having a redundant XVS type parallel to the aligner type. */
8702 return raw_type;
8703
14f9c5c9 8704 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 8705 if (real_type_namer == NULL
78134374 8706 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 8707 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
8708 return raw_type;
8709
940da03e 8710 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
8711 {
8712 /* This is an older encoding form where the base type needs to be
85102364 8713 looked up by name. We prefer the newer encoding because it is
f80d3ff2 8714 more efficient. */
33d16dd9 8715 raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
f80d3ff2
JB
8716 if (raw_real_type == NULL)
8717 return raw_type;
8718 else
8719 return raw_real_type;
8720 }
8721
8722 /* The field in our XVS type is a reference to the base type. */
940da03e 8723 return TYPE_TARGET_TYPE (real_type_namer->field (0).type ());
d2e4a39e 8724}
14f9c5c9 8725
4c4b4cd2 8726/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 8727
d2e4a39e
AS
8728struct type *
8729ada_aligned_type (struct type *type)
14f9c5c9
AS
8730{
8731 if (ada_is_aligner_type (type))
940da03e 8732 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
8733 else
8734 return ada_get_base_type (type);
8735}
8736
8737
8738/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 8739 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 8740
fc1a4b47
AC
8741const gdb_byte *
8742ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 8743{
d2e4a39e 8744 if (ada_is_aligner_type (type))
940da03e 8745 return ada_aligned_value_addr (type->field (0).type (),
dda83cd7
SM
8746 valaddr +
8747 TYPE_FIELD_BITPOS (type,
8748 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
8749 else
8750 return valaddr;
8751}
8752
4c4b4cd2
PH
8753
8754
14f9c5c9 8755/* The printed representation of an enumeration literal with encoded
4c4b4cd2 8756 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
8757const char *
8758ada_enum_name (const char *name)
14f9c5c9 8759{
5f9febe0 8760 static std::string storage;
e6a959d6 8761 const char *tmp;
14f9c5c9 8762
4c4b4cd2
PH
8763 /* First, unqualify the enumeration name:
8764 1. Search for the last '.' character. If we find one, then skip
177b42fe 8765 all the preceding characters, the unqualified name starts
76a01679 8766 right after that dot.
4c4b4cd2 8767 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
8768 translates dots into "__". Search forward for double underscores,
8769 but stop searching when we hit an overloading suffix, which is
8770 of the form "__" followed by digits. */
4c4b4cd2 8771
c3e5cd34
PH
8772 tmp = strrchr (name, '.');
8773 if (tmp != NULL)
4c4b4cd2
PH
8774 name = tmp + 1;
8775 else
14f9c5c9 8776 {
4c4b4cd2 8777 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
8778 {
8779 if (isdigit (tmp[2]))
8780 break;
8781 else
8782 name = tmp + 2;
8783 }
14f9c5c9
AS
8784 }
8785
8786 if (name[0] == 'Q')
8787 {
14f9c5c9 8788 int v;
5b4ee69b 8789
14f9c5c9 8790 if (name[1] == 'U' || name[1] == 'W')
dda83cd7
SM
8791 {
8792 if (sscanf (name + 2, "%x", &v) != 1)
8793 return name;
8794 }
272560b5
TT
8795 else if (((name[1] >= '0' && name[1] <= '9')
8796 || (name[1] >= 'a' && name[1] <= 'z'))
8797 && name[2] == '\0')
8798 {
5f9febe0
TT
8799 storage = string_printf ("'%c'", name[1]);
8800 return storage.c_str ();
272560b5 8801 }
14f9c5c9 8802 else
dda83cd7 8803 return name;
14f9c5c9
AS
8804
8805 if (isascii (v) && isprint (v))
5f9febe0 8806 storage = string_printf ("'%c'", v);
14f9c5c9 8807 else if (name[1] == 'U')
5f9febe0 8808 storage = string_printf ("[\"%02x\"]", v);
14f9c5c9 8809 else
5f9febe0 8810 storage = string_printf ("[\"%04x\"]", v);
14f9c5c9 8811
5f9febe0 8812 return storage.c_str ();
14f9c5c9 8813 }
d2e4a39e 8814 else
4c4b4cd2 8815 {
c3e5cd34
PH
8816 tmp = strstr (name, "__");
8817 if (tmp == NULL)
8818 tmp = strstr (name, "$");
8819 if (tmp != NULL)
dda83cd7 8820 {
5f9febe0
TT
8821 storage = std::string (name, tmp - name);
8822 return storage.c_str ();
dda83cd7 8823 }
4c4b4cd2
PH
8824
8825 return name;
8826 }
14f9c5c9
AS
8827}
8828
14f9c5c9 8829/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 8830 value it wraps. */
14f9c5c9 8831
d2e4a39e
AS
8832static struct value *
8833unwrap_value (struct value *val)
14f9c5c9 8834{
df407dfe 8835 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 8836
14f9c5c9
AS
8837 if (ada_is_aligner_type (type))
8838 {
de4d072f 8839 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 8840 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 8841
14f9c5c9 8842 if (ada_type_name (val_type) == NULL)
d0e39ea2 8843 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
8844
8845 return unwrap_value (v);
8846 }
d2e4a39e 8847 else
14f9c5c9 8848 {
d2e4a39e 8849 struct type *raw_real_type =
dda83cd7 8850 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 8851
5bf03f13
JB
8852 /* If there is no parallel XVS or XVE type, then the value is
8853 already unwrapped. Return it without further modification. */
8854 if ((type == raw_real_type)
8855 && ada_find_parallel_type (type, "___XVE") == NULL)
8856 return val;
14f9c5c9 8857
d2e4a39e 8858 return
dda83cd7
SM
8859 coerce_unspec_val_to_type
8860 (val, ada_to_fixed_type (raw_real_type, 0,
8861 value_address (val),
8862 NULL, 1));
14f9c5c9
AS
8863 }
8864}
d2e4a39e 8865
d99dcf51
JB
8866/* Given two array types T1 and T2, return nonzero iff both arrays
8867 contain the same number of elements. */
8868
8869static int
8870ada_same_array_size_p (struct type *t1, struct type *t2)
8871{
8872 LONGEST lo1, hi1, lo2, hi2;
8873
8874 /* Get the array bounds in order to verify that the size of
8875 the two arrays match. */
8876 if (!get_array_bounds (t1, &lo1, &hi1)
8877 || !get_array_bounds (t2, &lo2, &hi2))
8878 error (_("unable to determine array bounds"));
8879
8880 /* To make things easier for size comparison, normalize a bit
8881 the case of empty arrays by making sure that the difference
8882 between upper bound and lower bound is always -1. */
8883 if (lo1 > hi1)
8884 hi1 = lo1 - 1;
8885 if (lo2 > hi2)
8886 hi2 = lo2 - 1;
8887
8888 return (hi1 - lo1 == hi2 - lo2);
8889}
8890
8891/* Assuming that VAL is an array of integrals, and TYPE represents
8892 an array with the same number of elements, but with wider integral
8893 elements, return an array "casted" to TYPE. In practice, this
8894 means that the returned array is built by casting each element
8895 of the original array into TYPE's (wider) element type. */
8896
8897static struct value *
8898ada_promote_array_of_integrals (struct type *type, struct value *val)
8899{
8900 struct type *elt_type = TYPE_TARGET_TYPE (type);
8901 LONGEST lo, hi;
8902 struct value *res;
8903 LONGEST i;
8904
8905 /* Verify that both val and type are arrays of scalars, and
8906 that the size of val's elements is smaller than the size
8907 of type's element. */
78134374 8908 gdb_assert (type->code () == TYPE_CODE_ARRAY);
d99dcf51 8909 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
78134374 8910 gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY);
d99dcf51
JB
8911 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
8912 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
8913 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
8914
8915 if (!get_array_bounds (type, &lo, &hi))
8916 error (_("unable to determine array bounds"));
8917
8918 res = allocate_value (type);
8919
8920 /* Promote each array element. */
8921 for (i = 0; i < hi - lo + 1; i++)
8922 {
8923 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
8924
50888e42
SM
8925 memcpy ((value_contents_writeable (res).data ()
8926 + (i * TYPE_LENGTH (elt_type))),
8927 value_contents_all (elt).data (), TYPE_LENGTH (elt_type));
d99dcf51
JB
8928 }
8929
8930 return res;
8931}
8932
4c4b4cd2
PH
8933/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
8934 return the converted value. */
8935
d2e4a39e
AS
8936static struct value *
8937coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 8938{
df407dfe 8939 struct type *type2 = value_type (val);
5b4ee69b 8940
14f9c5c9
AS
8941 if (type == type2)
8942 return val;
8943
61ee279c
PH
8944 type2 = ada_check_typedef (type2);
8945 type = ada_check_typedef (type);
14f9c5c9 8946
78134374
SM
8947 if (type2->code () == TYPE_CODE_PTR
8948 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
8949 {
8950 val = ada_value_ind (val);
df407dfe 8951 type2 = value_type (val);
14f9c5c9
AS
8952 }
8953
78134374
SM
8954 if (type2->code () == TYPE_CODE_ARRAY
8955 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 8956 {
d99dcf51
JB
8957 if (!ada_same_array_size_p (type, type2))
8958 error (_("cannot assign arrays of different length"));
8959
8960 if (is_integral_type (TYPE_TARGET_TYPE (type))
8961 && is_integral_type (TYPE_TARGET_TYPE (type2))
8962 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
8963 < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
8964 {
8965 /* Allow implicit promotion of the array elements to
8966 a wider type. */
8967 return ada_promote_array_of_integrals (type, val);
8968 }
8969
8970 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
dda83cd7
SM
8971 != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
8972 error (_("Incompatible types in assignment"));
04624583 8973 deprecated_set_value_type (val, type);
14f9c5c9 8974 }
d2e4a39e 8975 return val;
14f9c5c9
AS
8976}
8977
4c4b4cd2
PH
8978static struct value *
8979ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8980{
8981 struct value *val;
8982 struct type *type1, *type2;
8983 LONGEST v, v1, v2;
8984
994b9211
AC
8985 arg1 = coerce_ref (arg1);
8986 arg2 = coerce_ref (arg2);
18af8284
JB
8987 type1 = get_base_type (ada_check_typedef (value_type (arg1)));
8988 type2 = get_base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 8989
78134374
SM
8990 if (type1->code () != TYPE_CODE_INT
8991 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
8992 return value_binop (arg1, arg2, op);
8993
76a01679 8994 switch (op)
4c4b4cd2
PH
8995 {
8996 case BINOP_MOD:
8997 case BINOP_DIV:
8998 case BINOP_REM:
8999 break;
9000 default:
9001 return value_binop (arg1, arg2, op);
9002 }
9003
9004 v2 = value_as_long (arg2);
9005 if (v2 == 0)
b0f9164c
TT
9006 {
9007 const char *name;
9008 if (op == BINOP_MOD)
9009 name = "mod";
9010 else if (op == BINOP_DIV)
9011 name = "/";
9012 else
9013 {
9014 gdb_assert (op == BINOP_REM);
9015 name = "rem";
9016 }
9017
9018 error (_("second operand of %s must not be zero."), name);
9019 }
4c4b4cd2 9020
c6d940a9 9021 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9022 return value_binop (arg1, arg2, op);
9023
9024 v1 = value_as_long (arg1);
9025 switch (op)
9026 {
9027 case BINOP_DIV:
9028 v = v1 / v2;
76a01679 9029 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
dda83cd7 9030 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9031 break;
9032 case BINOP_REM:
9033 v = v1 % v2;
76a01679 9034 if (v * v1 < 0)
dda83cd7 9035 v -= v2;
4c4b4cd2
PH
9036 break;
9037 default:
9038 /* Should not reach this point. */
9039 v = 0;
9040 }
9041
9042 val = allocate_value (type1);
50888e42 9043 store_unsigned_integer (value_contents_raw (val).data (),
dda83cd7 9044 TYPE_LENGTH (value_type (val)),
34877895 9045 type_byte_order (type1), v);
4c4b4cd2
PH
9046 return val;
9047}
9048
9049static int
9050ada_value_equal (struct value *arg1, struct value *arg2)
9051{
df407dfe
AC
9052 if (ada_is_direct_array_type (value_type (arg1))
9053 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 9054 {
79e8fcaa
JB
9055 struct type *arg1_type, *arg2_type;
9056
f58b38bf 9057 /* Automatically dereference any array reference before
dda83cd7 9058 we attempt to perform the comparison. */
f58b38bf
JB
9059 arg1 = ada_coerce_ref (arg1);
9060 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9061
4c4b4cd2
PH
9062 arg1 = ada_coerce_to_simple_array (arg1);
9063 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa
JB
9064
9065 arg1_type = ada_check_typedef (value_type (arg1));
9066 arg2_type = ada_check_typedef (value_type (arg2));
9067
78134374 9068 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9069 || arg2_type->code () != TYPE_CODE_ARRAY)
9070 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9071 /* FIXME: The following works only for types whose
dda83cd7
SM
9072 representations use all bits (no padding or undefined bits)
9073 and do not have user-defined equality. */
79e8fcaa 9074 return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
50888e42
SM
9075 && memcmp (value_contents (arg1).data (),
9076 value_contents (arg2).data (),
79e8fcaa 9077 TYPE_LENGTH (arg1_type)) == 0);
4c4b4cd2
PH
9078 }
9079 return value_equal (arg1, arg2);
9080}
9081
d3c54a1c
TT
9082namespace expr
9083{
9084
9085bool
9086check_objfile (const std::unique_ptr<ada_component> &comp,
9087 struct objfile *objfile)
9088{
9089 return comp->uses_objfile (objfile);
9090}
9091
9092/* Assign the result of evaluating ARG starting at *POS to the INDEXth
9093 component of LHS (a simple array or a record). Does not modify the
9094 inferior's memory, nor does it modify LHS (unless LHS ==
9095 CONTAINER). */
52ce6436
PH
9096
9097static void
9098assign_component (struct value *container, struct value *lhs, LONGEST index,
d3c54a1c 9099 struct expression *exp, operation_up &arg)
52ce6436 9100{
d3c54a1c
TT
9101 scoped_value_mark mark;
9102
52ce6436 9103 struct value *elt;
0e2da9f0 9104 struct type *lhs_type = check_typedef (value_type (lhs));
5b4ee69b 9105
78134374 9106 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9107 {
22601c15
UW
9108 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9109 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9110
52ce6436
PH
9111 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9112 }
9113 else
9114 {
9115 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
c48db5ca 9116 elt = ada_to_fixed_value (elt);
52ce6436
PH
9117 }
9118
d3c54a1c
TT
9119 ada_aggregate_operation *ag_op
9120 = dynamic_cast<ada_aggregate_operation *> (arg.get ());
9121 if (ag_op != nullptr)
9122 ag_op->assign_aggregate (container, elt, exp);
52ce6436 9123 else
d3c54a1c
TT
9124 value_assign_to_component (container, elt,
9125 arg->evaluate (nullptr, exp,
9126 EVAL_NORMAL));
9127}
52ce6436 9128
d3c54a1c
TT
9129bool
9130ada_aggregate_component::uses_objfile (struct objfile *objfile)
9131{
9132 for (const auto &item : m_components)
9133 if (item->uses_objfile (objfile))
9134 return true;
9135 return false;
9136}
9137
9138void
9139ada_aggregate_component::dump (ui_file *stream, int depth)
9140{
9141 fprintf_filtered (stream, _("%*sAggregate\n"), depth, "");
9142 for (const auto &item : m_components)
9143 item->dump (stream, depth + 1);
9144}
9145
9146void
9147ada_aggregate_component::assign (struct value *container,
9148 struct value *lhs, struct expression *exp,
9149 std::vector<LONGEST> &indices,
9150 LONGEST low, LONGEST high)
9151{
9152 for (auto &item : m_components)
9153 item->assign (container, lhs, exp, indices, low, high);
52ce6436
PH
9154}
9155
207582c0 9156/* See ada-exp.h. */
52ce6436 9157
207582c0 9158value *
d3c54a1c
TT
9159ada_aggregate_operation::assign_aggregate (struct value *container,
9160 struct value *lhs,
9161 struct expression *exp)
52ce6436
PH
9162{
9163 struct type *lhs_type;
52ce6436 9164 LONGEST low_index, high_index;
52ce6436
PH
9165
9166 container = ada_coerce_ref (container);
9167 if (ada_is_direct_array_type (value_type (container)))
9168 container = ada_coerce_to_simple_array (container);
9169 lhs = ada_coerce_ref (lhs);
9170 if (!deprecated_value_modifiable (lhs))
9171 error (_("Left operand of assignment is not a modifiable lvalue."));
9172
0e2da9f0 9173 lhs_type = check_typedef (value_type (lhs));
52ce6436
PH
9174 if (ada_is_direct_array_type (lhs_type))
9175 {
9176 lhs = ada_coerce_to_simple_array (lhs);
0e2da9f0 9177 lhs_type = check_typedef (value_type (lhs));
cf88be68
SM
9178 low_index = lhs_type->bounds ()->low.const_val ();
9179 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9180 }
78134374 9181 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9182 {
9183 low_index = 0;
9184 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9185 }
9186 else
9187 error (_("Left-hand side must be array or record."));
9188
cf608cc4 9189 std::vector<LONGEST> indices (4);
52ce6436
PH
9190 indices[0] = indices[1] = low_index - 1;
9191 indices[2] = indices[3] = high_index + 1;
52ce6436 9192
d3c54a1c
TT
9193 std::get<0> (m_storage)->assign (container, lhs, exp, indices,
9194 low_index, high_index);
207582c0
TT
9195
9196 return container;
d3c54a1c
TT
9197}
9198
9199bool
9200ada_positional_component::uses_objfile (struct objfile *objfile)
9201{
9202 return m_op->uses_objfile (objfile);
9203}
52ce6436 9204
d3c54a1c
TT
9205void
9206ada_positional_component::dump (ui_file *stream, int depth)
9207{
9208 fprintf_filtered (stream, _("%*sPositional, index = %d\n"),
9209 depth, "", m_index);
9210 m_op->dump (stream, depth + 1);
52ce6436 9211}
d3c54a1c 9212
52ce6436 9213/* Assign into the component of LHS indexed by the OP_POSITIONAL
d3c54a1c
TT
9214 construct, given that the positions are relative to lower bound
9215 LOW, where HIGH is the upper bound. Record the position in
9216 INDICES. CONTAINER is as for assign_aggregate. */
9217void
9218ada_positional_component::assign (struct value *container,
9219 struct value *lhs, struct expression *exp,
9220 std::vector<LONGEST> &indices,
9221 LONGEST low, LONGEST high)
52ce6436 9222{
d3c54a1c
TT
9223 LONGEST ind = m_index + low;
9224
52ce6436 9225 if (ind - 1 == high)
e1d5a0d2 9226 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9227 if (ind <= high)
9228 {
cf608cc4 9229 add_component_interval (ind, ind, indices);
d3c54a1c 9230 assign_component (container, lhs, ind, exp, m_op);
52ce6436 9231 }
52ce6436
PH
9232}
9233
d3c54a1c
TT
9234bool
9235ada_discrete_range_association::uses_objfile (struct objfile *objfile)
a88c4354
TT
9236{
9237 return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
9238}
9239
9240void
9241ada_discrete_range_association::dump (ui_file *stream, int depth)
9242{
9243 fprintf_filtered (stream, _("%*sDiscrete range:\n"), depth, "");
9244 m_low->dump (stream, depth + 1);
9245 m_high->dump (stream, depth + 1);
9246}
9247
9248void
9249ada_discrete_range_association::assign (struct value *container,
9250 struct value *lhs,
9251 struct expression *exp,
9252 std::vector<LONGEST> &indices,
9253 LONGEST low, LONGEST high,
9254 operation_up &op)
9255{
9256 LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
9257 LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
9258
9259 if (lower <= upper && (lower < low || upper > high))
9260 error (_("Index in component association out of bounds."));
9261
9262 add_component_interval (lower, upper, indices);
9263 while (lower <= upper)
9264 {
9265 assign_component (container, lhs, lower, exp, op);
9266 lower += 1;
9267 }
9268}
9269
9270bool
9271ada_name_association::uses_objfile (struct objfile *objfile)
9272{
9273 return m_val->uses_objfile (objfile);
9274}
9275
9276void
9277ada_name_association::dump (ui_file *stream, int depth)
9278{
9279 fprintf_filtered (stream, _("%*sName:\n"), depth, "");
9280 m_val->dump (stream, depth + 1);
9281}
9282
9283void
9284ada_name_association::assign (struct value *container,
9285 struct value *lhs,
9286 struct expression *exp,
9287 std::vector<LONGEST> &indices,
9288 LONGEST low, LONGEST high,
9289 operation_up &op)
9290{
9291 int index;
9292
9293 if (ada_is_direct_array_type (value_type (lhs)))
9294 index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
9295 EVAL_NORMAL)));
9296 else
9297 {
9298 ada_string_operation *strop
9299 = dynamic_cast<ada_string_operation *> (m_val.get ());
9300
9301 const char *name;
9302 if (strop != nullptr)
9303 name = strop->get_name ();
9304 else
9305 {
9306 ada_var_value_operation *vvo
9307 = dynamic_cast<ada_var_value_operation *> (m_val.get ());
9308 if (vvo != nullptr)
9309 error (_("Invalid record component association."));
9310 name = vvo->get_symbol ()->natural_name ();
9311 }
9312
9313 index = 0;
9314 if (! find_struct_field (name, value_type (lhs), 0,
9315 NULL, NULL, NULL, NULL, &index))
9316 error (_("Unknown component name: %s."), name);
9317 }
9318
9319 add_component_interval (index, index, indices);
9320 assign_component (container, lhs, index, exp, op);
9321}
9322
9323bool
9324ada_choices_component::uses_objfile (struct objfile *objfile)
9325{
9326 if (m_op->uses_objfile (objfile))
9327 return true;
9328 for (const auto &item : m_assocs)
9329 if (item->uses_objfile (objfile))
9330 return true;
9331 return false;
9332}
9333
9334void
9335ada_choices_component::dump (ui_file *stream, int depth)
9336{
9337 fprintf_filtered (stream, _("%*sChoices:\n"), depth, "");
9338 m_op->dump (stream, depth + 1);
9339 for (const auto &item : m_assocs)
9340 item->dump (stream, depth + 1);
9341}
9342
9343/* Assign into the components of LHS indexed by the OP_CHOICES
9344 construct at *POS, updating *POS past the construct, given that
9345 the allowable indices are LOW..HIGH. Record the indices assigned
9346 to in INDICES. CONTAINER is as for assign_aggregate. */
9347void
9348ada_choices_component::assign (struct value *container,
9349 struct value *lhs, struct expression *exp,
9350 std::vector<LONGEST> &indices,
9351 LONGEST low, LONGEST high)
9352{
9353 for (auto &item : m_assocs)
9354 item->assign (container, lhs, exp, indices, low, high, m_op);
9355}
9356
9357bool
9358ada_others_component::uses_objfile (struct objfile *objfile)
9359{
9360 return m_op->uses_objfile (objfile);
9361}
9362
9363void
9364ada_others_component::dump (ui_file *stream, int depth)
9365{
9366 fprintf_filtered (stream, _("%*sOthers:\n"), depth, "");
9367 m_op->dump (stream, depth + 1);
9368}
9369
9370/* Assign the value of the expression in the OP_OTHERS construct in
9371 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9372 have not been previously assigned. The index intervals already assigned
9373 are in INDICES. CONTAINER is as for assign_aggregate. */
9374void
9375ada_others_component::assign (struct value *container,
9376 struct value *lhs, struct expression *exp,
9377 std::vector<LONGEST> &indices,
9378 LONGEST low, LONGEST high)
9379{
9380 int num_indices = indices.size ();
9381 for (int i = 0; i < num_indices - 2; i += 2)
9382 {
9383 for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9384 assign_component (container, lhs, ind, exp, m_op);
9385 }
9386}
9387
9388struct value *
9389ada_assign_operation::evaluate (struct type *expect_type,
9390 struct expression *exp,
9391 enum noside noside)
9392{
9393 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
9394
9395 ada_aggregate_operation *ag_op
9396 = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
9397 if (ag_op != nullptr)
9398 {
9399 if (noside != EVAL_NORMAL)
9400 return arg1;
9401
207582c0 9402 arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
a88c4354
TT
9403 return ada_value_assign (arg1, arg1);
9404 }
9405 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9406 except if the lhs of our assignment is a convenience variable.
9407 In the case of assigning to a convenience variable, the lhs
9408 should be exactly the result of the evaluation of the rhs. */
9409 struct type *type = value_type (arg1);
9410 if (VALUE_LVAL (arg1) == lval_internalvar)
9411 type = NULL;
9412 value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
0b2b0b82 9413 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a88c4354
TT
9414 return arg1;
9415 if (VALUE_LVAL (arg1) == lval_internalvar)
9416 {
9417 /* Nothing. */
9418 }
9419 else
9420 arg2 = coerce_for_assign (value_type (arg1), arg2);
9421 return ada_value_assign (arg1, arg2);
9422}
9423
9424} /* namespace expr */
9425
cf608cc4
TT
9426/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9427 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9428 overlap. */
52ce6436
PH
9429static void
9430add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9431 std::vector<LONGEST> &indices)
52ce6436
PH
9432{
9433 int i, j;
5b4ee69b 9434
cf608cc4
TT
9435 int size = indices.size ();
9436 for (i = 0; i < size; i += 2) {
52ce6436
PH
9437 if (high >= indices[i] && low <= indices[i + 1])
9438 {
9439 int kh;
5b4ee69b 9440
cf608cc4 9441 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9442 if (high < indices[kh])
9443 break;
9444 if (low < indices[i])
9445 indices[i] = low;
9446 indices[i + 1] = indices[kh - 1];
9447 if (high > indices[i + 1])
9448 indices[i + 1] = high;
cf608cc4
TT
9449 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9450 indices.resize (kh - i - 2);
52ce6436
PH
9451 return;
9452 }
9453 else if (high < indices[i])
9454 break;
9455 }
9456
cf608cc4 9457 indices.resize (indices.size () + 2);
d4813f10 9458 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9459 indices[j] = indices[j - 2];
9460 indices[i] = low;
9461 indices[i + 1] = high;
9462}
9463
6e48bd2c
JB
9464/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9465 is different. */
9466
9467static struct value *
b7e22850 9468ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c
JB
9469{
9470 if (type == ada_check_typedef (value_type (arg2)))
9471 return arg2;
9472
6e48bd2c
JB
9473 return value_cast (type, arg2);
9474}
9475
284614f0
JB
9476/* Evaluating Ada expressions, and printing their result.
9477 ------------------------------------------------------
9478
21649b50
JB
9479 1. Introduction:
9480 ----------------
9481
284614f0
JB
9482 We usually evaluate an Ada expression in order to print its value.
9483 We also evaluate an expression in order to print its type, which
9484 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9485 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9486 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9487 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9488 similar.
9489
9490 Evaluating expressions is a little more complicated for Ada entities
9491 than it is for entities in languages such as C. The main reason for
9492 this is that Ada provides types whose definition might be dynamic.
9493 One example of such types is variant records. Or another example
9494 would be an array whose bounds can only be known at run time.
9495
9496 The following description is a general guide as to what should be
9497 done (and what should NOT be done) in order to evaluate an expression
9498 involving such types, and when. This does not cover how the semantic
9499 information is encoded by GNAT as this is covered separatly. For the
9500 document used as the reference for the GNAT encoding, see exp_dbug.ads
9501 in the GNAT sources.
9502
9503 Ideally, we should embed each part of this description next to its
9504 associated code. Unfortunately, the amount of code is so vast right
9505 now that it's hard to see whether the code handling a particular
9506 situation might be duplicated or not. One day, when the code is
9507 cleaned up, this guide might become redundant with the comments
9508 inserted in the code, and we might want to remove it.
9509
21649b50
JB
9510 2. ``Fixing'' an Entity, the Simple Case:
9511 -----------------------------------------
9512
284614f0
JB
9513 When evaluating Ada expressions, the tricky issue is that they may
9514 reference entities whose type contents and size are not statically
9515 known. Consider for instance a variant record:
9516
9517 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9518 case Empty is
9519 when True => null;
9520 when False => Value : Integer;
9521 end case;
284614f0
JB
9522 end record;
9523 Yes : Rec := (Empty => False, Value => 1);
9524 No : Rec := (empty => True);
9525
9526 The size and contents of that record depends on the value of the
9527 descriminant (Rec.Empty). At this point, neither the debugging
9528 information nor the associated type structure in GDB are able to
9529 express such dynamic types. So what the debugger does is to create
9530 "fixed" versions of the type that applies to the specific object.
30baf67b 9531 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9532 which means creating its associated fixed type.
9533
9534 Example: when printing the value of variable "Yes" above, its fixed
9535 type would look like this:
9536
9537 type Rec is record
dda83cd7
SM
9538 Empty : Boolean;
9539 Value : Integer;
284614f0
JB
9540 end record;
9541
9542 On the other hand, if we printed the value of "No", its fixed type
9543 would become:
9544
9545 type Rec is record
dda83cd7 9546 Empty : Boolean;
284614f0
JB
9547 end record;
9548
9549 Things become a little more complicated when trying to fix an entity
9550 with a dynamic type that directly contains another dynamic type,
9551 such as an array of variant records, for instance. There are
9552 two possible cases: Arrays, and records.
9553
21649b50
JB
9554 3. ``Fixing'' Arrays:
9555 ---------------------
9556
9557 The type structure in GDB describes an array in terms of its bounds,
9558 and the type of its elements. By design, all elements in the array
9559 have the same type and we cannot represent an array of variant elements
9560 using the current type structure in GDB. When fixing an array,
9561 we cannot fix the array element, as we would potentially need one
9562 fixed type per element of the array. As a result, the best we can do
9563 when fixing an array is to produce an array whose bounds and size
9564 are correct (allowing us to read it from memory), but without having
9565 touched its element type. Fixing each element will be done later,
9566 when (if) necessary.
9567
9568 Arrays are a little simpler to handle than records, because the same
9569 amount of memory is allocated for each element of the array, even if
1b536f04 9570 the amount of space actually used by each element differs from element
21649b50 9571 to element. Consider for instance the following array of type Rec:
284614f0
JB
9572
9573 type Rec_Array is array (1 .. 2) of Rec;
9574
1b536f04
JB
9575 The actual amount of memory occupied by each element might be different
9576 from element to element, depending on the value of their discriminant.
21649b50 9577 But the amount of space reserved for each element in the array remains
1b536f04 9578 fixed regardless. So we simply need to compute that size using
21649b50
JB
9579 the debugging information available, from which we can then determine
9580 the array size (we multiply the number of elements of the array by
9581 the size of each element).
9582
9583 The simplest case is when we have an array of a constrained element
9584 type. For instance, consider the following type declarations:
9585
dda83cd7
SM
9586 type Bounded_String (Max_Size : Integer) is
9587 Length : Integer;
9588 Buffer : String (1 .. Max_Size);
9589 end record;
9590 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9591
9592 In this case, the compiler describes the array as an array of
9593 variable-size elements (identified by its XVS suffix) for which
9594 the size can be read in the parallel XVZ variable.
9595
9596 In the case of an array of an unconstrained element type, the compiler
9597 wraps the array element inside a private PAD type. This type should not
9598 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9599 that we also use the adjective "aligner" in our code to designate
9600 these wrapper types.
9601
1b536f04 9602 In some cases, the size allocated for each element is statically
21649b50
JB
9603 known. In that case, the PAD type already has the correct size,
9604 and the array element should remain unfixed.
9605
9606 But there are cases when this size is not statically known.
9607 For instance, assuming that "Five" is an integer variable:
284614f0 9608
dda83cd7
SM
9609 type Dynamic is array (1 .. Five) of Integer;
9610 type Wrapper (Has_Length : Boolean := False) is record
9611 Data : Dynamic;
9612 case Has_Length is
9613 when True => Length : Integer;
9614 when False => null;
9615 end case;
9616 end record;
9617 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9618
dda83cd7
SM
9619 Hello : Wrapper_Array := (others => (Has_Length => True,
9620 Data => (others => 17),
9621 Length => 1));
284614f0
JB
9622
9623
9624 The debugging info would describe variable Hello as being an
9625 array of a PAD type. The size of that PAD type is not statically
9626 known, but can be determined using a parallel XVZ variable.
9627 In that case, a copy of the PAD type with the correct size should
9628 be used for the fixed array.
9629
21649b50
JB
9630 3. ``Fixing'' record type objects:
9631 ----------------------------------
9632
9633 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9634 record types. In this case, in order to compute the associated
9635 fixed type, we need to determine the size and offset of each of
9636 its components. This, in turn, requires us to compute the fixed
9637 type of each of these components.
9638
9639 Consider for instance the example:
9640
dda83cd7
SM
9641 type Bounded_String (Max_Size : Natural) is record
9642 Str : String (1 .. Max_Size);
9643 Length : Natural;
9644 end record;
9645 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9646
9647 In that case, the position of field "Length" depends on the size
9648 of field Str, which itself depends on the value of the Max_Size
21649b50 9649 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9650 we need to fix the type of field Str. Therefore, fixing a variant
9651 record requires us to fix each of its components.
9652
9653 However, if a component does not have a dynamic size, the component
9654 should not be fixed. In particular, fields that use a PAD type
9655 should not fixed. Here is an example where this might happen
9656 (assuming type Rec above):
9657
9658 type Container (Big : Boolean) is record
dda83cd7
SM
9659 First : Rec;
9660 After : Integer;
9661 case Big is
9662 when True => Another : Integer;
9663 when False => null;
9664 end case;
284614f0
JB
9665 end record;
9666 My_Container : Container := (Big => False,
dda83cd7
SM
9667 First => (Empty => True),
9668 After => 42);
284614f0
JB
9669
9670 In that example, the compiler creates a PAD type for component First,
9671 whose size is constant, and then positions the component After just
9672 right after it. The offset of component After is therefore constant
9673 in this case.
9674
9675 The debugger computes the position of each field based on an algorithm
9676 that uses, among other things, the actual position and size of the field
21649b50
JB
9677 preceding it. Let's now imagine that the user is trying to print
9678 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
9679 end up computing the offset of field After based on the size of the
9680 fixed version of field First. And since in our example First has
9681 only one actual field, the size of the fixed type is actually smaller
9682 than the amount of space allocated to that field, and thus we would
9683 compute the wrong offset of field After.
9684
21649b50
JB
9685 To make things more complicated, we need to watch out for dynamic
9686 components of variant records (identified by the ___XVL suffix in
9687 the component name). Even if the target type is a PAD type, the size
9688 of that type might not be statically known. So the PAD type needs
9689 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9690 we might end up with the wrong size for our component. This can be
9691 observed with the following type declarations:
284614f0 9692
dda83cd7
SM
9693 type Octal is new Integer range 0 .. 7;
9694 type Octal_Array is array (Positive range <>) of Octal;
9695 pragma Pack (Octal_Array);
284614f0 9696
dda83cd7
SM
9697 type Octal_Buffer (Size : Positive) is record
9698 Buffer : Octal_Array (1 .. Size);
9699 Length : Integer;
9700 end record;
284614f0
JB
9701
9702 In that case, Buffer is a PAD type whose size is unset and needs
9703 to be computed by fixing the unwrapped type.
9704
21649b50
JB
9705 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9706 ----------------------------------------------------------
9707
9708 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
9709 thus far, be actually fixed?
9710
9711 The answer is: Only when referencing that element. For instance
9712 when selecting one component of a record, this specific component
9713 should be fixed at that point in time. Or when printing the value
9714 of a record, each component should be fixed before its value gets
9715 printed. Similarly for arrays, the element of the array should be
9716 fixed when printing each element of the array, or when extracting
9717 one element out of that array. On the other hand, fixing should
9718 not be performed on the elements when taking a slice of an array!
9719
31432a67 9720 Note that one of the side effects of miscomputing the offset and
284614f0
JB
9721 size of each field is that we end up also miscomputing the size
9722 of the containing type. This can have adverse results when computing
9723 the value of an entity. GDB fetches the value of an entity based
9724 on the size of its type, and thus a wrong size causes GDB to fetch
9725 the wrong amount of memory. In the case where the computed size is
9726 too small, GDB fetches too little data to print the value of our
31432a67 9727 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
9728 past the buffer containing the data =:-o. */
9729
62d4bd94
TT
9730/* A helper function for TERNOP_IN_RANGE. */
9731
9732static value *
9733eval_ternop_in_range (struct type *expect_type, struct expression *exp,
9734 enum noside noside,
9735 value *arg1, value *arg2, value *arg3)
9736{
62d4bd94
TT
9737 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9738 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9739 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
9740 return
9741 value_from_longest (type,
9742 (value_less (arg1, arg3)
9743 || value_equal (arg1, arg3))
9744 && (value_less (arg2, arg1)
9745 || value_equal (arg2, arg1)));
9746}
9747
82390ab8
TT
9748/* A helper function for UNOP_NEG. */
9749
7c15d377 9750value *
82390ab8
TT
9751ada_unop_neg (struct type *expect_type,
9752 struct expression *exp,
9753 enum noside noside, enum exp_opcode op,
9754 struct value *arg1)
9755{
82390ab8
TT
9756 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9757 return value_neg (arg1);
9758}
9759
7efc87ff
TT
9760/* A helper function for UNOP_IN_RANGE. */
9761
95d49dfb 9762value *
7efc87ff
TT
9763ada_unop_in_range (struct type *expect_type,
9764 struct expression *exp,
9765 enum noside noside, enum exp_opcode op,
9766 struct value *arg1, struct type *type)
9767{
7efc87ff
TT
9768 struct value *arg2, *arg3;
9769 switch (type->code ())
9770 {
9771 default:
9772 lim_warning (_("Membership test incompletely implemented; "
9773 "always returns true"));
9774 type = language_bool_type (exp->language_defn, exp->gdbarch);
9775 return value_from_longest (type, (LONGEST) 1);
9776
9777 case TYPE_CODE_RANGE:
9778 arg2 = value_from_longest (type,
9779 type->bounds ()->low.const_val ());
9780 arg3 = value_from_longest (type,
9781 type->bounds ()->high.const_val ());
9782 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9783 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9784 type = language_bool_type (exp->language_defn, exp->gdbarch);
9785 return
9786 value_from_longest (type,
9787 (value_less (arg1, arg3)
9788 || value_equal (arg1, arg3))
9789 && (value_less (arg2, arg1)
9790 || value_equal (arg2, arg1)));
9791 }
9792}
9793
020dbabe
TT
9794/* A helper function for OP_ATR_TAG. */
9795
7c15d377 9796value *
020dbabe
TT
9797ada_atr_tag (struct type *expect_type,
9798 struct expression *exp,
9799 enum noside noside, enum exp_opcode op,
9800 struct value *arg1)
9801{
9802 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9803 return value_zero (ada_tag_type (arg1), not_lval);
9804
9805 return ada_value_tag (arg1);
9806}
9807
68c75735
TT
9808/* A helper function for OP_ATR_SIZE. */
9809
7c15d377 9810value *
68c75735
TT
9811ada_atr_size (struct type *expect_type,
9812 struct expression *exp,
9813 enum noside noside, enum exp_opcode op,
9814 struct value *arg1)
9815{
9816 struct type *type = value_type (arg1);
9817
9818 /* If the argument is a reference, then dereference its type, since
9819 the user is really asking for the size of the actual object,
9820 not the size of the pointer. */
9821 if (type->code () == TYPE_CODE_REF)
9822 type = TYPE_TARGET_TYPE (type);
9823
0b2b0b82 9824 if (noside == EVAL_AVOID_SIDE_EFFECTS)
68c75735
TT
9825 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
9826 else
9827 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
9828 TARGET_CHAR_BIT * TYPE_LENGTH (type));
9829}
9830
d05e24e6
TT
9831/* A helper function for UNOP_ABS. */
9832
7c15d377 9833value *
d05e24e6
TT
9834ada_abs (struct type *expect_type,
9835 struct expression *exp,
9836 enum noside noside, enum exp_opcode op,
9837 struct value *arg1)
9838{
9839 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9840 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
9841 return value_neg (arg1);
9842 else
9843 return arg1;
9844}
9845
faa1dfd7
TT
9846/* A helper function for BINOP_MUL. */
9847
d9e7db06 9848value *
faa1dfd7
TT
9849ada_mult_binop (struct type *expect_type,
9850 struct expression *exp,
9851 enum noside noside, enum exp_opcode op,
9852 struct value *arg1, struct value *arg2)
9853{
9854 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9855 {
9856 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9857 return value_zero (value_type (arg1), not_lval);
9858 }
9859 else
9860 {
9861 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9862 return ada_value_binop (arg1, arg2, op);
9863 }
9864}
9865
214b13ac
TT
9866/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
9867
6e8fb7b7 9868value *
214b13ac
TT
9869ada_equal_binop (struct type *expect_type,
9870 struct expression *exp,
9871 enum noside noside, enum exp_opcode op,
9872 struct value *arg1, struct value *arg2)
9873{
9874 int tem;
9875 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9876 tem = 0;
9877 else
9878 {
9879 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9880 tem = ada_value_equal (arg1, arg2);
9881 }
9882 if (op == BINOP_NOTEQUAL)
9883 tem = !tem;
9884 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
9885 return value_from_longest (type, (LONGEST) tem);
9886}
9887
5ce19db8
TT
9888/* A helper function for TERNOP_SLICE. */
9889
1b1ebfab 9890value *
5ce19db8
TT
9891ada_ternop_slice (struct expression *exp,
9892 enum noside noside,
9893 struct value *array, struct value *low_bound_val,
9894 struct value *high_bound_val)
9895{
9896 LONGEST low_bound;
9897 LONGEST high_bound;
9898
9899 low_bound_val = coerce_ref (low_bound_val);
9900 high_bound_val = coerce_ref (high_bound_val);
9901 low_bound = value_as_long (low_bound_val);
9902 high_bound = value_as_long (high_bound_val);
9903
9904 /* If this is a reference to an aligner type, then remove all
9905 the aligners. */
9906 if (value_type (array)->code () == TYPE_CODE_REF
9907 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
9908 TYPE_TARGET_TYPE (value_type (array)) =
9909 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
9910
9911 if (ada_is_any_packed_array_type (value_type (array)))
9912 error (_("cannot slice a packed array"));
9913
9914 /* If this is a reference to an array or an array lvalue,
9915 convert to a pointer. */
9916 if (value_type (array)->code () == TYPE_CODE_REF
9917 || (value_type (array)->code () == TYPE_CODE_ARRAY
9918 && VALUE_LVAL (array) == lval_memory))
9919 array = value_addr (array);
9920
9921 if (noside == EVAL_AVOID_SIDE_EFFECTS
9922 && ada_is_array_descriptor_type (ada_check_typedef
9923 (value_type (array))))
9924 return empty_array (ada_type_of_array (array, 0), low_bound,
9925 high_bound);
9926
9927 array = ada_coerce_to_simple_array_ptr (array);
9928
9929 /* If we have more than one level of pointer indirection,
9930 dereference the value until we get only one level. */
9931 while (value_type (array)->code () == TYPE_CODE_PTR
9932 && (TYPE_TARGET_TYPE (value_type (array))->code ()
9933 == TYPE_CODE_PTR))
9934 array = value_ind (array);
9935
9936 /* Make sure we really do have an array type before going further,
9937 to avoid a SEGV when trying to get the index type or the target
9938 type later down the road if the debug info generated by
9939 the compiler is incorrect or incomplete. */
9940 if (!ada_is_simple_array_type (value_type (array)))
9941 error (_("cannot take slice of non-array"));
9942
9943 if (ada_check_typedef (value_type (array))->code ()
9944 == TYPE_CODE_PTR)
9945 {
9946 struct type *type0 = ada_check_typedef (value_type (array));
9947
9948 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
9949 return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
9950 else
9951 {
9952 struct type *arr_type0 =
9953 to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
9954
9955 return ada_value_slice_from_ptr (array, arr_type0,
9956 longest_to_int (low_bound),
9957 longest_to_int (high_bound));
9958 }
9959 }
9960 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9961 return array;
9962 else if (high_bound < low_bound)
9963 return empty_array (value_type (array), low_bound, high_bound);
9964 else
9965 return ada_value_slice (array, longest_to_int (low_bound),
9966 longest_to_int (high_bound));
9967}
9968
b467efaa
TT
9969/* A helper function for BINOP_IN_BOUNDS. */
9970
82c3886e 9971value *
b467efaa
TT
9972ada_binop_in_bounds (struct expression *exp, enum noside noside,
9973 struct value *arg1, struct value *arg2, int n)
9974{
9975 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9976 {
9977 struct type *type = language_bool_type (exp->language_defn,
9978 exp->gdbarch);
9979 return value_zero (type, not_lval);
9980 }
9981
9982 struct type *type = ada_index_type (value_type (arg2), n, "range");
9983 if (!type)
9984 type = value_type (arg1);
9985
9986 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
9987 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
9988
9989 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9990 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9991 type = language_bool_type (exp->language_defn, exp->gdbarch);
9992 return value_from_longest (type,
9993 (value_less (arg1, arg3)
9994 || value_equal (arg1, arg3))
9995 && (value_less (arg2, arg1)
9996 || value_equal (arg2, arg1)));
9997}
9998
b84564fc
TT
9999/* A helper function for some attribute operations. */
10000
10001static value *
10002ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10003 struct value *arg1, struct type *type_arg, int tem)
10004{
10005 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10006 {
10007 if (type_arg == NULL)
10008 type_arg = value_type (arg1);
10009
10010 if (ada_is_constrained_packed_array_type (type_arg))
10011 type_arg = decode_constrained_packed_array_type (type_arg);
10012
10013 if (!discrete_type_p (type_arg))
10014 {
10015 switch (op)
10016 {
10017 default: /* Should never happen. */
10018 error (_("unexpected attribute encountered"));
10019 case OP_ATR_FIRST:
10020 case OP_ATR_LAST:
10021 type_arg = ada_index_type (type_arg, tem,
10022 ada_attribute_name (op));
10023 break;
10024 case OP_ATR_LENGTH:
10025 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10026 break;
10027 }
10028 }
10029
10030 return value_zero (type_arg, not_lval);
10031 }
10032 else if (type_arg == NULL)
10033 {
10034 arg1 = ada_coerce_ref (arg1);
10035
10036 if (ada_is_constrained_packed_array_type (value_type (arg1)))
10037 arg1 = ada_coerce_to_simple_array (arg1);
10038
10039 struct type *type;
10040 if (op == OP_ATR_LENGTH)
10041 type = builtin_type (exp->gdbarch)->builtin_int;
10042 else
10043 {
10044 type = ada_index_type (value_type (arg1), tem,
10045 ada_attribute_name (op));
10046 if (type == NULL)
10047 type = builtin_type (exp->gdbarch)->builtin_int;
10048 }
10049
10050 switch (op)
10051 {
10052 default: /* Should never happen. */
10053 error (_("unexpected attribute encountered"));
10054 case OP_ATR_FIRST:
10055 return value_from_longest
10056 (type, ada_array_bound (arg1, tem, 0));
10057 case OP_ATR_LAST:
10058 return value_from_longest
10059 (type, ada_array_bound (arg1, tem, 1));
10060 case OP_ATR_LENGTH:
10061 return value_from_longest
10062 (type, ada_array_length (arg1, tem));
10063 }
10064 }
10065 else if (discrete_type_p (type_arg))
10066 {
10067 struct type *range_type;
10068 const char *name = ada_type_name (type_arg);
10069
10070 range_type = NULL;
10071 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10072 range_type = to_fixed_range_type (type_arg, NULL);
10073 if (range_type == NULL)
10074 range_type = type_arg;
10075 switch (op)
10076 {
10077 default:
10078 error (_("unexpected attribute encountered"));
10079 case OP_ATR_FIRST:
10080 return value_from_longest
10081 (range_type, ada_discrete_type_low_bound (range_type));
10082 case OP_ATR_LAST:
10083 return value_from_longest
10084 (range_type, ada_discrete_type_high_bound (range_type));
10085 case OP_ATR_LENGTH:
10086 error (_("the 'length attribute applies only to array types"));
10087 }
10088 }
10089 else if (type_arg->code () == TYPE_CODE_FLT)
10090 error (_("unimplemented type attribute"));
10091 else
10092 {
10093 LONGEST low, high;
10094
10095 if (ada_is_constrained_packed_array_type (type_arg))
10096 type_arg = decode_constrained_packed_array_type (type_arg);
10097
10098 struct type *type;
10099 if (op == OP_ATR_LENGTH)
10100 type = builtin_type (exp->gdbarch)->builtin_int;
10101 else
10102 {
10103 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10104 if (type == NULL)
10105 type = builtin_type (exp->gdbarch)->builtin_int;
10106 }
10107
10108 switch (op)
10109 {
10110 default:
10111 error (_("unexpected attribute encountered"));
10112 case OP_ATR_FIRST:
10113 low = ada_array_bound_from_type (type_arg, tem, 0);
10114 return value_from_longest (type, low);
10115 case OP_ATR_LAST:
10116 high = ada_array_bound_from_type (type_arg, tem, 1);
10117 return value_from_longest (type, high);
10118 case OP_ATR_LENGTH:
10119 low = ada_array_bound_from_type (type_arg, tem, 0);
10120 high = ada_array_bound_from_type (type_arg, tem, 1);
10121 return value_from_longest (type, high - low + 1);
10122 }
10123 }
10124}
10125
38dc70cf
TT
10126/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10127
6ad3b8bf 10128struct value *
38dc70cf
TT
10129ada_binop_minmax (struct type *expect_type,
10130 struct expression *exp,
10131 enum noside noside, enum exp_opcode op,
10132 struct value *arg1, struct value *arg2)
10133{
10134 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10135 return value_zero (value_type (arg1), not_lval);
10136 else
10137 {
10138 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
0922dc84 10139 return value_binop (arg1, arg2, op);
38dc70cf
TT
10140 }
10141}
10142
dd5fd283
TT
10143/* A helper function for BINOP_EXP. */
10144
065ec826 10145struct value *
dd5fd283
TT
10146ada_binop_exp (struct type *expect_type,
10147 struct expression *exp,
10148 enum noside noside, enum exp_opcode op,
10149 struct value *arg1, struct value *arg2)
10150{
10151 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10152 return value_zero (value_type (arg1), not_lval);
10153 else
10154 {
10155 /* For integer exponentiation operations,
10156 only promote the first argument. */
10157 if (is_integral_type (value_type (arg2)))
10158 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10159 else
10160 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10161
10162 return value_binop (arg1, arg2, op);
10163 }
10164}
10165
03070ee9
TT
10166namespace expr
10167{
10168
8b12db26
TT
10169/* See ada-exp.h. */
10170
10171operation_up
10172ada_resolvable::replace (operation_up &&owner,
10173 struct expression *exp,
10174 bool deprocedure_p,
10175 bool parse_completion,
10176 innermost_block_tracker *tracker,
10177 struct type *context_type)
10178{
10179 if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
10180 return (make_operation<ada_funcall_operation>
10181 (std::move (owner),
10182 std::vector<operation_up> ()));
10183 return std::move (owner);
10184}
10185
03adb248
TT
10186/* Convert the character literal whose ASCII value would be VAL to the
10187 appropriate value of type TYPE, if there is a translation.
10188 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10189 the literal 'A' (VAL == 65), returns 0. */
10190
10191static LONGEST
10192convert_char_literal (struct type *type, LONGEST val)
10193{
10194 char name[7];
10195 int f;
10196
10197 if (type == NULL)
10198 return val;
10199 type = check_typedef (type);
10200 if (type->code () != TYPE_CODE_ENUM)
10201 return val;
10202
10203 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
10204 xsnprintf (name, sizeof (name), "Q%c", (int) val);
10205 else
10206 xsnprintf (name, sizeof (name), "QU%02x", (int) val);
10207 size_t len = strlen (name);
10208 for (f = 0; f < type->num_fields (); f += 1)
10209 {
10210 /* Check the suffix because an enum constant in a package will
10211 have a name like "pkg__QUxx". This is safe enough because we
10212 already have the correct type, and because mangling means
10213 there can't be clashes. */
33d16dd9 10214 const char *ename = type->field (f).name ();
03adb248
TT
10215 size_t elen = strlen (ename);
10216
10217 if (elen >= len && strcmp (name, ename + elen - len) == 0)
10218 return TYPE_FIELD_ENUMVAL (type, f);
10219 }
10220 return val;
10221}
10222
10223/* See ada-exp.h. */
10224
10225operation_up
10226ada_char_operation::replace (operation_up &&owner,
10227 struct expression *exp,
10228 bool deprocedure_p,
10229 bool parse_completion,
10230 innermost_block_tracker *tracker,
10231 struct type *context_type)
10232{
10233 operation_up result = std::move (owner);
10234
10235 if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
10236 {
10237 gdb_assert (result.get () == this);
10238 std::get<0> (m_storage) = context_type;
10239 std::get<1> (m_storage)
10240 = convert_char_literal (context_type, std::get<1> (m_storage));
10241 }
10242
10243 return make_operation<ada_wrapped_operation> (std::move (result));
10244}
10245
03070ee9
TT
10246value *
10247ada_wrapped_operation::evaluate (struct type *expect_type,
10248 struct expression *exp,
10249 enum noside noside)
10250{
10251 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10252 if (noside == EVAL_NORMAL)
10253 result = unwrap_value (result);
10254
10255 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10256 then we need to perform the conversion manually, because
10257 evaluate_subexp_standard doesn't do it. This conversion is
10258 necessary in Ada because the different kinds of float/fixed
10259 types in Ada have different representations.
10260
10261 Similarly, we need to perform the conversion from OP_LONG
10262 ourselves. */
10263 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10264 result = ada_value_cast (expect_type, result);
10265
10266 return result;
10267}
10268
42fecb61
TT
10269value *
10270ada_string_operation::evaluate (struct type *expect_type,
10271 struct expression *exp,
10272 enum noside noside)
10273{
10274 value *result = string_operation::evaluate (expect_type, exp, noside);
10275 /* The result type will have code OP_STRING, bashed there from
10276 OP_ARRAY. Bash it back. */
10277 if (value_type (result)->code () == TYPE_CODE_STRING)
10278 value_type (result)->set_code (TYPE_CODE_ARRAY);
10279 return result;
10280}
10281
cc6bd32e
TT
10282value *
10283ada_qual_operation::evaluate (struct type *expect_type,
10284 struct expression *exp,
10285 enum noside noside)
10286{
10287 struct type *type = std::get<1> (m_storage);
10288 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10289}
10290
fc715eb2
TT
10291value *
10292ada_ternop_range_operation::evaluate (struct type *expect_type,
10293 struct expression *exp,
10294 enum noside noside)
10295{
10296 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10297 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10298 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10299 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10300}
10301
73796c73
TT
10302value *
10303ada_binop_addsub_operation::evaluate (struct type *expect_type,
10304 struct expression *exp,
10305 enum noside noside)
10306{
10307 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10308 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10309
10310 auto do_op = [=] (LONGEST x, LONGEST y)
10311 {
10312 if (std::get<0> (m_storage) == BINOP_ADD)
10313 return x + y;
10314 return x - y;
10315 };
10316
10317 if (value_type (arg1)->code () == TYPE_CODE_PTR)
10318 return (value_from_longest
10319 (value_type (arg1),
10320 do_op (value_as_long (arg1), value_as_long (arg2))));
10321 if (value_type (arg2)->code () == TYPE_CODE_PTR)
10322 return (value_from_longest
10323 (value_type (arg2),
10324 do_op (value_as_long (arg1), value_as_long (arg2))));
10325 /* Preserve the original type for use by the range case below.
10326 We cannot cast the result to a reference type, so if ARG1 is
10327 a reference type, find its underlying type. */
10328 struct type *type = value_type (arg1);
10329 while (type->code () == TYPE_CODE_REF)
10330 type = TYPE_TARGET_TYPE (type);
10331 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10332 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10333 /* We need to special-case the result with a range.
10334 This is done for the benefit of "ptype". gdb's Ada support
10335 historically used the LHS to set the result type here, so
10336 preserve this behavior. */
10337 if (type->code () == TYPE_CODE_RANGE)
10338 arg1 = value_cast (type, arg1);
10339 return arg1;
10340}
10341
60fa02ca
TT
10342value *
10343ada_unop_atr_operation::evaluate (struct type *expect_type,
10344 struct expression *exp,
10345 enum noside noside)
10346{
10347 struct type *type_arg = nullptr;
10348 value *val = nullptr;
10349
10350 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10351 {
10352 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10353 EVAL_AVOID_SIDE_EFFECTS);
10354 type_arg = value_type (tem);
10355 }
10356 else
10357 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10358
10359 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10360 val, type_arg, std::get<2> (m_storage));
10361}
10362
3f4a0053
TT
10363value *
10364ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10365 struct expression *exp,
10366 enum noside noside)
10367{
10368 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10369 return value_zero (expect_type, not_lval);
10370
9c79936b
TT
10371 const bound_minimal_symbol &b = std::get<0> (m_storage);
10372 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
3f4a0053
TT
10373
10374 val = ada_value_cast (expect_type, val);
10375
10376 /* Follow the Ada language semantics that do not allow taking
10377 an address of the result of a cast (view conversion in Ada). */
10378 if (VALUE_LVAL (val) == lval_memory)
10379 {
10380 if (value_lazy (val))
10381 value_fetch_lazy (val);
10382 VALUE_LVAL (val) = not_lval;
10383 }
10384 return val;
10385}
10386
99a3b1e7
TT
10387value *
10388ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10389 struct expression *exp,
10390 enum noside noside)
10391{
10392 value *val = evaluate_var_value (noside,
9e5e03df
TT
10393 std::get<0> (m_storage).block,
10394 std::get<0> (m_storage).symbol);
99a3b1e7
TT
10395
10396 val = ada_value_cast (expect_type, val);
10397
10398 /* Follow the Ada language semantics that do not allow taking
10399 an address of the result of a cast (view conversion in Ada). */
10400 if (VALUE_LVAL (val) == lval_memory)
10401 {
10402 if (value_lazy (val))
10403 value_fetch_lazy (val);
10404 VALUE_LVAL (val) = not_lval;
10405 }
10406 return val;
10407}
10408
10409value *
10410ada_var_value_operation::evaluate (struct type *expect_type,
10411 struct expression *exp,
10412 enum noside noside)
10413{
9e5e03df 10414 symbol *sym = std::get<0> (m_storage).symbol;
99a3b1e7
TT
10415
10416 if (SYMBOL_DOMAIN (sym) == UNDEF_DOMAIN)
10417 /* Only encountered when an unresolved symbol occurs in a
10418 context other than a function call, in which case, it is
10419 invalid. */
10420 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10421 sym->print_name ());
10422
10423 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10424 {
10425 struct type *type = static_unwrap_type (SYMBOL_TYPE (sym));
10426 /* Check to see if this is a tagged type. We also need to handle
10427 the case where the type is a reference to a tagged type, but
10428 we have to be careful to exclude pointers to tagged types.
10429 The latter should be shown as usual (as a pointer), whereas
10430 a reference should mostly be transparent to the user. */
10431 if (ada_is_tagged_type (type, 0)
10432 || (type->code () == TYPE_CODE_REF
10433 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
10434 {
10435 /* Tagged types are a little special in the fact that the real
10436 type is dynamic and can only be determined by inspecting the
10437 object's tag. This means that we need to get the object's
10438 value first (EVAL_NORMAL) and then extract the actual object
10439 type from its tag.
10440
10441 Note that we cannot skip the final step where we extract
10442 the object type from its tag, because the EVAL_NORMAL phase
10443 results in dynamic components being resolved into fixed ones.
10444 This can cause problems when trying to print the type
10445 description of tagged types whose parent has a dynamic size:
10446 We use the type name of the "_parent" component in order
10447 to print the name of the ancestor type in the type description.
10448 If that component had a dynamic size, the resolution into
10449 a fixed type would result in the loss of that type name,
10450 thus preventing us from printing the name of the ancestor
10451 type in the type description. */
9863c3b5 10452 value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
99a3b1e7
TT
10453
10454 if (type->code () != TYPE_CODE_REF)
10455 {
10456 struct type *actual_type;
10457
10458 actual_type = type_from_tag (ada_value_tag (arg1));
10459 if (actual_type == NULL)
10460 /* If, for some reason, we were unable to determine
10461 the actual type from the tag, then use the static
10462 approximation that we just computed as a fallback.
10463 This can happen if the debugging information is
10464 incomplete, for instance. */
10465 actual_type = type;
10466 return value_zero (actual_type, not_lval);
10467 }
10468 else
10469 {
10470 /* In the case of a ref, ada_coerce_ref takes care
10471 of determining the actual type. But the evaluation
10472 should return a ref as it should be valid to ask
10473 for its address; so rebuild a ref after coerce. */
10474 arg1 = ada_coerce_ref (arg1);
10475 return value_ref (arg1, TYPE_CODE_REF);
10476 }
10477 }
10478
10479 /* Records and unions for which GNAT encodings have been
10480 generated need to be statically fixed as well.
10481 Otherwise, non-static fixing produces a type where
10482 all dynamic properties are removed, which prevents "ptype"
10483 from being able to completely describe the type.
10484 For instance, a case statement in a variant record would be
10485 replaced by the relevant components based on the actual
10486 value of the discriminants. */
10487 if ((type->code () == TYPE_CODE_STRUCT
10488 && dynamic_template_type (type) != NULL)
10489 || (type->code () == TYPE_CODE_UNION
10490 && ada_find_parallel_type (type, "___XVU") != NULL))
10491 return value_zero (to_static_fixed_type (type), not_lval);
10492 }
10493
10494 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10495 return ada_to_fixed_value (arg1);
10496}
10497
d8a4ed8a
TT
10498bool
10499ada_var_value_operation::resolve (struct expression *exp,
10500 bool deprocedure_p,
10501 bool parse_completion,
10502 innermost_block_tracker *tracker,
10503 struct type *context_type)
10504{
9e5e03df 10505 symbol *sym = std::get<0> (m_storage).symbol;
d8a4ed8a
TT
10506 if (SYMBOL_DOMAIN (sym) == UNDEF_DOMAIN)
10507 {
10508 block_symbol resolved
9e5e03df 10509 = ada_resolve_variable (sym, std::get<0> (m_storage).block,
d8a4ed8a
TT
10510 context_type, parse_completion,
10511 deprocedure_p, tracker);
9e5e03df 10512 std::get<0> (m_storage) = resolved;
d8a4ed8a
TT
10513 }
10514
10515 if (deprocedure_p
9e5e03df
TT
10516 && (SYMBOL_TYPE (std::get<0> (m_storage).symbol)->code ()
10517 == TYPE_CODE_FUNC))
d8a4ed8a
TT
10518 return true;
10519
10520 return false;
10521}
10522
9e99f48f
TT
10523value *
10524ada_atr_val_operation::evaluate (struct type *expect_type,
10525 struct expression *exp,
10526 enum noside noside)
10527{
10528 value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10529 return ada_val_atr (noside, std::get<0> (m_storage), arg);
10530}
10531
e8c33fa1
TT
10532value *
10533ada_unop_ind_operation::evaluate (struct type *expect_type,
10534 struct expression *exp,
10535 enum noside noside)
10536{
10537 value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10538
10539 struct type *type = ada_check_typedef (value_type (arg1));
10540 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10541 {
10542 if (ada_is_array_descriptor_type (type))
10543 /* GDB allows dereferencing GNAT array descriptors. */
10544 {
10545 struct type *arrType = ada_type_of_array (arg1, 0);
10546
10547 if (arrType == NULL)
10548 error (_("Attempt to dereference null array pointer."));
10549 return value_at_lazy (arrType, 0);
10550 }
10551 else if (type->code () == TYPE_CODE_PTR
10552 || type->code () == TYPE_CODE_REF
10553 /* In C you can dereference an array to get the 1st elt. */
10554 || type->code () == TYPE_CODE_ARRAY)
10555 {
10556 /* As mentioned in the OP_VAR_VALUE case, tagged types can
10557 only be determined by inspecting the object's tag.
10558 This means that we need to evaluate completely the
10559 expression in order to get its type. */
10560
10561 if ((type->code () == TYPE_CODE_REF
10562 || type->code () == TYPE_CODE_PTR)
10563 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
10564 {
10565 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
10566 EVAL_NORMAL);
10567 type = value_type (ada_value_ind (arg1));
10568 }
10569 else
10570 {
10571 type = to_static_fixed_type
10572 (ada_aligned_type
10573 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
10574 }
e8c33fa1
TT
10575 return value_zero (type, lval_memory);
10576 }
10577 else if (type->code () == TYPE_CODE_INT)
10578 {
10579 /* GDB allows dereferencing an int. */
10580 if (expect_type == NULL)
10581 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
10582 lval_memory);
10583 else
10584 {
10585 expect_type =
10586 to_static_fixed_type (ada_aligned_type (expect_type));
10587 return value_zero (expect_type, lval_memory);
10588 }
10589 }
10590 else
10591 error (_("Attempt to take contents of a non-pointer value."));
10592 }
10593 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
10594 type = ada_check_typedef (value_type (arg1));
10595
10596 if (type->code () == TYPE_CODE_INT)
10597 /* GDB allows dereferencing an int. If we were given
10598 the expect_type, then use that as the target type.
10599 Otherwise, assume that the target type is an int. */
10600 {
10601 if (expect_type != NULL)
10602 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
10603 arg1));
10604 else
10605 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
10606 (CORE_ADDR) value_as_address (arg1));
10607 }
10608
10609 if (ada_is_array_descriptor_type (type))
10610 /* GDB allows dereferencing GNAT array descriptors. */
10611 return ada_coerce_to_simple_array (arg1);
10612 else
10613 return ada_value_ind (arg1);
10614}
10615
ebc06ad8
TT
10616value *
10617ada_structop_operation::evaluate (struct type *expect_type,
10618 struct expression *exp,
10619 enum noside noside)
10620{
10621 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10622 const char *str = std::get<1> (m_storage).c_str ();
10623 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10624 {
10625 struct type *type;
10626 struct type *type1 = value_type (arg1);
10627
10628 if (ada_is_tagged_type (type1, 1))
10629 {
10630 type = ada_lookup_struct_elt_type (type1, str, 1, 1);
10631
10632 /* If the field is not found, check if it exists in the
10633 extension of this object's type. This means that we
10634 need to evaluate completely the expression. */
10635
10636 if (type == NULL)
10637 {
10638 arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
10639 EVAL_NORMAL);
10640 arg1 = ada_value_struct_elt (arg1, str, 0);
10641 arg1 = unwrap_value (arg1);
10642 type = value_type (ada_to_fixed_value (arg1));
10643 }
10644 }
10645 else
10646 type = ada_lookup_struct_elt_type (type1, str, 1, 0);
10647
10648 return value_zero (ada_aligned_type (type), lval_memory);
10649 }
10650 else
10651 {
10652 arg1 = ada_value_struct_elt (arg1, str, 0);
10653 arg1 = unwrap_value (arg1);
10654 return ada_to_fixed_value (arg1);
10655 }
10656}
10657
efe3af2f
TT
10658value *
10659ada_funcall_operation::evaluate (struct type *expect_type,
10660 struct expression *exp,
10661 enum noside noside)
10662{
10663 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
10664 int nargs = args_up.size ();
10665 std::vector<value *> argvec (nargs);
10666 operation_up &callee_op = std::get<0> (m_storage);
10667
10668 ada_var_value_operation *avv
10669 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
10670 if (avv != nullptr
10671 && SYMBOL_DOMAIN (avv->get_symbol ()) == UNDEF_DOMAIN)
10672 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10673 avv->get_symbol ()->print_name ());
10674
10675 value *callee = callee_op->evaluate (nullptr, exp, noside);
10676 for (int i = 0; i < args_up.size (); ++i)
10677 argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
10678
10679 if (ada_is_constrained_packed_array_type
10680 (desc_base_type (value_type (callee))))
10681 callee = ada_coerce_to_simple_array (callee);
10682 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
10683 && TYPE_FIELD_BITSIZE (value_type (callee), 0) != 0)
10684 /* This is a packed array that has already been fixed, and
10685 therefore already coerced to a simple array. Nothing further
10686 to do. */
10687 ;
10688 else if (value_type (callee)->code () == TYPE_CODE_REF)
10689 {
10690 /* Make sure we dereference references so that all the code below
10691 feels like it's really handling the referenced value. Wrapping
10692 types (for alignment) may be there, so make sure we strip them as
10693 well. */
10694 callee = ada_to_fixed_value (coerce_ref (callee));
10695 }
10696 else if (value_type (callee)->code () == TYPE_CODE_ARRAY
10697 && VALUE_LVAL (callee) == lval_memory)
10698 callee = value_addr (callee);
10699
10700 struct type *type = ada_check_typedef (value_type (callee));
10701
10702 /* Ada allows us to implicitly dereference arrays when subscripting
10703 them. So, if this is an array typedef (encoding use for array
10704 access types encoded as fat pointers), strip it now. */
10705 if (type->code () == TYPE_CODE_TYPEDEF)
10706 type = ada_typedef_target_type (type);
10707
10708 if (type->code () == TYPE_CODE_PTR)
10709 {
10710 switch (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ())
10711 {
10712 case TYPE_CODE_FUNC:
10713 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
10714 break;
10715 case TYPE_CODE_ARRAY:
10716 break;
10717 case TYPE_CODE_STRUCT:
10718 if (noside != EVAL_AVOID_SIDE_EFFECTS)
10719 callee = ada_value_ind (callee);
10720 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
10721 break;
10722 default:
10723 error (_("cannot subscript or call something of type `%s'"),
10724 ada_type_name (value_type (callee)));
10725 break;
10726 }
10727 }
10728
10729 switch (type->code ())
10730 {
10731 case TYPE_CODE_FUNC:
10732 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10733 {
10734 if (TYPE_TARGET_TYPE (type) == NULL)
10735 error_call_unknown_return_type (NULL);
10736 return allocate_value (TYPE_TARGET_TYPE (type));
10737 }
10738 return call_function_by_hand (callee, NULL, argvec);
10739 case TYPE_CODE_INTERNAL_FUNCTION:
10740 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10741 /* We don't know anything about what the internal
10742 function might return, but we have to return
10743 something. */
10744 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
10745 not_lval);
10746 else
10747 return call_internal_function (exp->gdbarch, exp->language_defn,
10748 callee, nargs,
10749 argvec.data ());
10750
d3c54a1c
TT
10751 case TYPE_CODE_STRUCT:
10752 {
10753 int arity;
4c4b4cd2 10754
d3c54a1c
TT
10755 arity = ada_array_arity (type);
10756 type = ada_array_element_type (type, nargs);
10757 if (type == NULL)
10758 error (_("cannot subscript or call a record"));
10759 if (arity != nargs)
10760 error (_("wrong number of subscripts; expecting %d"), arity);
10761 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10762 return value_zero (ada_aligned_type (type), lval_memory);
10763 return
10764 unwrap_value (ada_value_subscript
10765 (callee, nargs, argvec.data ()));
10766 }
10767 case TYPE_CODE_ARRAY:
14f9c5c9 10768 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 10769 {
d3c54a1c
TT
10770 type = ada_array_element_type (type, nargs);
10771 if (type == NULL)
10772 error (_("element type of array unknown"));
dda83cd7 10773 else
d3c54a1c 10774 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 10775 }
d3c54a1c
TT
10776 return
10777 unwrap_value (ada_value_subscript
10778 (ada_coerce_to_simple_array (callee),
10779 nargs, argvec.data ()));
10780 case TYPE_CODE_PTR: /* Pointer to array */
10781 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 10782 {
d3c54a1c
TT
10783 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
10784 type = ada_array_element_type (type, nargs);
10785 if (type == NULL)
10786 error (_("element type of array unknown"));
96967637 10787 else
d3c54a1c 10788 return value_zero (ada_aligned_type (type), lval_memory);
dda83cd7 10789 }
d3c54a1c
TT
10790 return
10791 unwrap_value (ada_value_ptr_subscript (callee, nargs,
10792 argvec.data ()));
6b0d7253 10793
d3c54a1c
TT
10794 default:
10795 error (_("Attempt to index or call something other than an "
10796 "array or function"));
10797 }
10798}
5b4ee69b 10799
d3c54a1c
TT
10800bool
10801ada_funcall_operation::resolve (struct expression *exp,
10802 bool deprocedure_p,
10803 bool parse_completion,
10804 innermost_block_tracker *tracker,
10805 struct type *context_type)
10806{
10807 operation_up &callee_op = std::get<0> (m_storage);
5ec18f2b 10808
d3c54a1c
TT
10809 ada_var_value_operation *avv
10810 = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
10811 if (avv == nullptr)
10812 return false;
5ec18f2b 10813
d3c54a1c
TT
10814 symbol *sym = avv->get_symbol ();
10815 if (SYMBOL_DOMAIN (sym) != UNDEF_DOMAIN)
10816 return false;
dda83cd7 10817
d3c54a1c
TT
10818 const std::vector<operation_up> &args_up = std::get<1> (m_storage);
10819 int nargs = args_up.size ();
10820 std::vector<value *> argvec (nargs);
284614f0 10821
d3c54a1c
TT
10822 for (int i = 0; i < args_up.size (); ++i)
10823 argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
52ce6436 10824
d3c54a1c
TT
10825 const block *block = avv->get_block ();
10826 block_symbol resolved
10827 = ada_resolve_funcall (sym, block,
10828 context_type, parse_completion,
10829 nargs, argvec.data (),
10830 tracker);
10831
10832 std::get<0> (m_storage)
9e5e03df 10833 = make_operation<ada_var_value_operation> (resolved);
d3c54a1c
TT
10834 return false;
10835}
10836
10837bool
10838ada_ternop_slice_operation::resolve (struct expression *exp,
10839 bool deprocedure_p,
10840 bool parse_completion,
10841 innermost_block_tracker *tracker,
10842 struct type *context_type)
10843{
10844 /* Historically this check was done during resolution, so we
10845 continue that here. */
10846 value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
10847 EVAL_AVOID_SIDE_EFFECTS);
10848 if (ada_is_any_packed_array_type (value_type (v)))
10849 error (_("cannot slice a packed array"));
10850 return false;
10851}
14f9c5c9 10852
14f9c5c9 10853}
d3c54a1c 10854
14f9c5c9 10855\f
d2e4a39e 10856
4c4b4cd2
PH
10857/* Return non-zero iff TYPE represents a System.Address type. */
10858
10859int
10860ada_is_system_address_type (struct type *type)
10861{
7d93a1e0 10862 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
10863}
10864
14f9c5c9 10865\f
d2e4a39e 10866
dda83cd7 10867 /* Range types */
14f9c5c9
AS
10868
10869/* Scan STR beginning at position K for a discriminant name, and
10870 return the value of that discriminant field of DVAL in *PX. If
10871 PNEW_K is not null, put the position of the character beyond the
10872 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 10873 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
10874
10875static int
108d56a4 10876scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 10877 int *pnew_k)
14f9c5c9 10878{
5f9febe0 10879 static std::string storage;
5da1a4d3 10880 const char *pstart, *pend, *bound;
d2e4a39e 10881 struct value *bound_val;
14f9c5c9
AS
10882
10883 if (dval == NULL || str == NULL || str[k] == '\0')
10884 return 0;
10885
5da1a4d3
SM
10886 pstart = str + k;
10887 pend = strstr (pstart, "__");
14f9c5c9
AS
10888 if (pend == NULL)
10889 {
5da1a4d3 10890 bound = pstart;
14f9c5c9
AS
10891 k += strlen (bound);
10892 }
d2e4a39e 10893 else
14f9c5c9 10894 {
5da1a4d3
SM
10895 int len = pend - pstart;
10896
10897 /* Strip __ and beyond. */
5f9febe0
TT
10898 storage = std::string (pstart, len);
10899 bound = storage.c_str ();
d2e4a39e 10900 k = pend - str;
14f9c5c9 10901 }
d2e4a39e 10902
df407dfe 10903 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
10904 if (bound_val == NULL)
10905 return 0;
10906
10907 *px = value_as_long (bound_val);
10908 if (pnew_k != NULL)
10909 *pnew_k = k;
10910 return 1;
10911}
10912
25a1127b
TT
10913/* Value of variable named NAME. Only exact matches are considered.
10914 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
10915 otherwise causes an error with message ERR_MSG. */
10916
d2e4a39e 10917static struct value *
edb0c9cb 10918get_var_value (const char *name, const char *err_msg)
14f9c5c9 10919{
25a1127b
TT
10920 std::string quoted_name = add_angle_brackets (name);
10921
10922 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 10923
d1183b06
TT
10924 std::vector<struct block_symbol> syms
10925 = ada_lookup_symbol_list_worker (lookup_name,
10926 get_selected_block (0),
10927 VAR_DOMAIN, 1);
14f9c5c9 10928
d1183b06 10929 if (syms.size () != 1)
14f9c5c9
AS
10930 {
10931 if (err_msg == NULL)
dda83cd7 10932 return 0;
14f9c5c9 10933 else
dda83cd7 10934 error (("%s"), err_msg);
14f9c5c9
AS
10935 }
10936
54d343a2 10937 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 10938}
d2e4a39e 10939
edb0c9cb
PA
10940/* Value of integer variable named NAME in the current environment.
10941 If no such variable is found, returns false. Otherwise, sets VALUE
10942 to the variable's value and returns true. */
4c4b4cd2 10943
edb0c9cb
PA
10944bool
10945get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 10946{
4c4b4cd2 10947 struct value *var_val = get_var_value (name, 0);
d2e4a39e 10948
14f9c5c9 10949 if (var_val == 0)
edb0c9cb
PA
10950 return false;
10951
10952 value = value_as_long (var_val);
10953 return true;
14f9c5c9 10954}
d2e4a39e 10955
14f9c5c9
AS
10956
10957/* Return a range type whose base type is that of the range type named
10958 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 10959 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
10960 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
10961 corresponding range type from debug information; fall back to using it
10962 if symbol lookup fails. If a new type must be created, allocate it
10963 like ORIG_TYPE was. The bounds information, in general, is encoded
10964 in NAME, the base type given in the named range type. */
14f9c5c9 10965
d2e4a39e 10966static struct type *
28c85d6c 10967to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 10968{
0d5cff50 10969 const char *name;
14f9c5c9 10970 struct type *base_type;
108d56a4 10971 const char *subtype_info;
14f9c5c9 10972
28c85d6c 10973 gdb_assert (raw_type != NULL);
7d93a1e0 10974 gdb_assert (raw_type->name () != NULL);
dddfab26 10975
78134374 10976 if (raw_type->code () == TYPE_CODE_RANGE)
14f9c5c9
AS
10977 base_type = TYPE_TARGET_TYPE (raw_type);
10978 else
10979 base_type = raw_type;
10980
7d93a1e0 10981 name = raw_type->name ();
14f9c5c9
AS
10982 subtype_info = strstr (name, "___XD");
10983 if (subtype_info == NULL)
690cc4eb 10984 {
43bbcdc2
PH
10985 LONGEST L = ada_discrete_type_low_bound (raw_type);
10986 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 10987
690cc4eb
PH
10988 if (L < INT_MIN || U > INT_MAX)
10989 return raw_type;
10990 else
0c9c3474
SA
10991 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
10992 L, U);
690cc4eb 10993 }
14f9c5c9
AS
10994 else
10995 {
14f9c5c9
AS
10996 int prefix_len = subtype_info - name;
10997 LONGEST L, U;
10998 struct type *type;
108d56a4 10999 const char *bounds_str;
14f9c5c9
AS
11000 int n;
11001
14f9c5c9
AS
11002 subtype_info += 5;
11003 bounds_str = strchr (subtype_info, '_');
11004 n = 1;
11005
d2e4a39e 11006 if (*subtype_info == 'L')
dda83cd7
SM
11007 {
11008 if (!ada_scan_number (bounds_str, n, &L, &n)
11009 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11010 return raw_type;
11011 if (bounds_str[n] == '_')
11012 n += 2;
11013 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11014 n += 1;
11015 subtype_info += 1;
11016 }
d2e4a39e 11017 else
dda83cd7 11018 {
5f9febe0
TT
11019 std::string name_buf = std::string (name, prefix_len) + "___L";
11020 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11021 {
11022 lim_warning (_("Unknown lower bound, using 1."));
11023 L = 1;
11024 }
11025 }
14f9c5c9 11026
d2e4a39e 11027 if (*subtype_info == 'U')
dda83cd7
SM
11028 {
11029 if (!ada_scan_number (bounds_str, n, &U, &n)
11030 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11031 return raw_type;
11032 }
d2e4a39e 11033 else
dda83cd7 11034 {
5f9febe0
TT
11035 std::string name_buf = std::string (name, prefix_len) + "___U";
11036 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11037 {
11038 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11039 U = L;
11040 }
11041 }
14f9c5c9 11042
0c9c3474
SA
11043 type = create_static_range_type (alloc_type_copy (raw_type),
11044 base_type, L, U);
f5a91472 11045 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11046 to match the size of the base_type, which is not what we want.
11047 Set it back to the original range type's length. */
f5a91472 11048 TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
d0e39ea2 11049 type->set_name (name);
14f9c5c9
AS
11050 return type;
11051 }
11052}
11053
4c4b4cd2
PH
11054/* True iff NAME is the name of a range type. */
11055
14f9c5c9 11056int
d2e4a39e 11057ada_is_range_type_name (const char *name)
14f9c5c9
AS
11058{
11059 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11060}
14f9c5c9 11061\f
d2e4a39e 11062
dda83cd7 11063 /* Modular types */
4c4b4cd2
PH
11064
11065/* True iff TYPE is an Ada modular type. */
14f9c5c9 11066
14f9c5c9 11067int
d2e4a39e 11068ada_is_modular_type (struct type *type)
14f9c5c9 11069{
18af8284 11070 struct type *subranged_type = get_base_type (type);
14f9c5c9 11071
78134374 11072 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11073 && subranged_type->code () == TYPE_CODE_INT
11074 && subranged_type->is_unsigned ());
14f9c5c9
AS
11075}
11076
4c4b4cd2
PH
11077/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11078
61ee279c 11079ULONGEST
0056e4d5 11080ada_modulus (struct type *type)
14f9c5c9 11081{
5e500d33
SM
11082 const dynamic_prop &high = type->bounds ()->high;
11083
11084 if (high.kind () == PROP_CONST)
11085 return (ULONGEST) high.const_val () + 1;
11086
11087 /* If TYPE is unresolved, the high bound might be a location list. Return
11088 0, for lack of a better value to return. */
11089 return 0;
14f9c5c9 11090}
d2e4a39e 11091\f
f7f9143b
JB
11092
11093/* Ada exception catchpoint support:
11094 ---------------------------------
11095
11096 We support 3 kinds of exception catchpoints:
11097 . catchpoints on Ada exceptions
11098 . catchpoints on unhandled Ada exceptions
11099 . catchpoints on failed assertions
11100
11101 Exceptions raised during failed assertions, or unhandled exceptions
11102 could perfectly be caught with the general catchpoint on Ada exceptions.
11103 However, we can easily differentiate these two special cases, and having
11104 the option to distinguish these two cases from the rest can be useful
11105 to zero-in on certain situations.
11106
11107 Exception catchpoints are a specialized form of breakpoint,
11108 since they rely on inserting breakpoints inside known routines
11109 of the GNAT runtime. The implementation therefore uses a standard
11110 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11111 of breakpoint_ops.
11112
0259addd
JB
11113 Support in the runtime for exception catchpoints have been changed
11114 a few times already, and these changes affect the implementation
11115 of these catchpoints. In order to be able to support several
11116 variants of the runtime, we use a sniffer that will determine
28010a5d 11117 the runtime variant used by the program being debugged. */
f7f9143b 11118
82eacd52
JB
11119/* Ada's standard exceptions.
11120
11121 The Ada 83 standard also defined Numeric_Error. But there so many
11122 situations where it was unclear from the Ada 83 Reference Manual
11123 (RM) whether Constraint_Error or Numeric_Error should be raised,
11124 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11125 Interpretation saying that anytime the RM says that Numeric_Error
11126 should be raised, the implementation may raise Constraint_Error.
11127 Ada 95 went one step further and pretty much removed Numeric_Error
11128 from the list of standard exceptions (it made it a renaming of
11129 Constraint_Error, to help preserve compatibility when compiling
11130 an Ada83 compiler). As such, we do not include Numeric_Error from
11131 this list of standard exceptions. */
3d0b0fa3 11132
27087b7f 11133static const char * const standard_exc[] = {
3d0b0fa3
JB
11134 "constraint_error",
11135 "program_error",
11136 "storage_error",
11137 "tasking_error"
11138};
11139
0259addd
JB
11140typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11141
11142/* A structure that describes how to support exception catchpoints
11143 for a given executable. */
11144
11145struct exception_support_info
11146{
11147 /* The name of the symbol to break on in order to insert
11148 a catchpoint on exceptions. */
11149 const char *catch_exception_sym;
11150
11151 /* The name of the symbol to break on in order to insert
11152 a catchpoint on unhandled exceptions. */
11153 const char *catch_exception_unhandled_sym;
11154
11155 /* The name of the symbol to break on in order to insert
11156 a catchpoint on failed assertions. */
11157 const char *catch_assert_sym;
11158
9f757bf7
XR
11159 /* The name of the symbol to break on in order to insert
11160 a catchpoint on exception handling. */
11161 const char *catch_handlers_sym;
11162
0259addd
JB
11163 /* Assuming that the inferior just triggered an unhandled exception
11164 catchpoint, this function is responsible for returning the address
11165 in inferior memory where the name of that exception is stored.
11166 Return zero if the address could not be computed. */
11167 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11168};
11169
11170static CORE_ADDR ada_unhandled_exception_name_addr (void);
11171static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11172
11173/* The following exception support info structure describes how to
11174 implement exception catchpoints with the latest version of the
ca683e3a 11175 Ada runtime (as of 2019-08-??). */
0259addd
JB
11176
11177static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11178{
11179 "__gnat_debug_raise_exception", /* catch_exception_sym */
11180 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11181 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11182 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11183 ada_unhandled_exception_name_addr
11184};
11185
11186/* The following exception support info structure describes how to
11187 implement exception catchpoints with an earlier version of the
11188 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11189
11190static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11191{
11192 "__gnat_debug_raise_exception", /* catch_exception_sym */
11193 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11194 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11195 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11196 ada_unhandled_exception_name_addr
11197};
11198
11199/* The following exception support info structure describes how to
11200 implement exception catchpoints with a slightly older version
11201 of the Ada runtime. */
11202
11203static const struct exception_support_info exception_support_info_fallback =
11204{
11205 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11206 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11207 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11208 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11209 ada_unhandled_exception_name_addr_from_raise
11210};
11211
f17011e0
JB
11212/* Return nonzero if we can detect the exception support routines
11213 described in EINFO.
11214
11215 This function errors out if an abnormal situation is detected
11216 (for instance, if we find the exception support routines, but
11217 that support is found to be incomplete). */
11218
11219static int
11220ada_has_this_exception_support (const struct exception_support_info *einfo)
11221{
11222 struct symbol *sym;
11223
11224 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11225 that should be compiled with debugging information. As a result, we
11226 expect to find that symbol in the symtabs. */
11227
11228 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11229 if (sym == NULL)
a6af7abe
JB
11230 {
11231 /* Perhaps we did not find our symbol because the Ada runtime was
11232 compiled without debugging info, or simply stripped of it.
11233 It happens on some GNU/Linux distributions for instance, where
11234 users have to install a separate debug package in order to get
11235 the runtime's debugging info. In that situation, let the user
11236 know why we cannot insert an Ada exception catchpoint.
11237
11238 Note: Just for the purpose of inserting our Ada exception
11239 catchpoint, we could rely purely on the associated minimal symbol.
11240 But we would be operating in degraded mode anyway, since we are
11241 still lacking the debugging info needed later on to extract
11242 the name of the exception being raised (this name is printed in
11243 the catchpoint message, and is also used when trying to catch
11244 a specific exception). We do not handle this case for now. */
3b7344d5 11245 struct bound_minimal_symbol msym
1c8e84b0
JB
11246 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11247
3b7344d5 11248 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
a6af7abe
JB
11249 error (_("Your Ada runtime appears to be missing some debugging "
11250 "information.\nCannot insert Ada exception catchpoint "
11251 "in this configuration."));
11252
11253 return 0;
11254 }
f17011e0
JB
11255
11256 /* Make sure that the symbol we found corresponds to a function. */
11257
11258 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
ca683e3a
AO
11259 {
11260 error (_("Symbol \"%s\" is not a function (class = %d)"),
987012b8 11261 sym->linkage_name (), SYMBOL_CLASS (sym));
ca683e3a
AO
11262 return 0;
11263 }
11264
11265 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11266 if (sym == NULL)
11267 {
11268 struct bound_minimal_symbol msym
11269 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11270
11271 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
11272 error (_("Your Ada runtime appears to be missing some debugging "
11273 "information.\nCannot insert Ada exception catchpoint "
11274 "in this configuration."));
11275
11276 return 0;
11277 }
11278
11279 /* Make sure that the symbol we found corresponds to a function. */
11280
11281 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11282 {
11283 error (_("Symbol \"%s\" is not a function (class = %d)"),
987012b8 11284 sym->linkage_name (), SYMBOL_CLASS (sym));
ca683e3a
AO
11285 return 0;
11286 }
f17011e0
JB
11287
11288 return 1;
11289}
11290
0259addd
JB
11291/* Inspect the Ada runtime and determine which exception info structure
11292 should be used to provide support for exception catchpoints.
11293
3eecfa55
JB
11294 This function will always set the per-inferior exception_info,
11295 or raise an error. */
0259addd
JB
11296
11297static void
11298ada_exception_support_info_sniffer (void)
11299{
3eecfa55 11300 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11301
11302 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11303 if (data->exception_info != NULL)
0259addd
JB
11304 return;
11305
11306 /* Check the latest (default) exception support info. */
f17011e0 11307 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11308 {
3eecfa55 11309 data->exception_info = &default_exception_support_info;
0259addd
JB
11310 return;
11311 }
11312
ca683e3a
AO
11313 /* Try the v0 exception suport info. */
11314 if (ada_has_this_exception_support (&exception_support_info_v0))
11315 {
11316 data->exception_info = &exception_support_info_v0;
11317 return;
11318 }
11319
0259addd 11320 /* Try our fallback exception suport info. */
f17011e0 11321 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11322 {
3eecfa55 11323 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11324 return;
11325 }
11326
11327 /* Sometimes, it is normal for us to not be able to find the routine
11328 we are looking for. This happens when the program is linked with
11329 the shared version of the GNAT runtime, and the program has not been
11330 started yet. Inform the user of these two possible causes if
11331 applicable. */
11332
ccefe4c4 11333 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11334 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11335
11336 /* If the symbol does not exist, then check that the program is
11337 already started, to make sure that shared libraries have been
11338 loaded. If it is not started, this may mean that the symbol is
11339 in a shared library. */
11340
e99b03dc 11341 if (inferior_ptid.pid () == 0)
0259addd
JB
11342 error (_("Unable to insert catchpoint. Try to start the program first."));
11343
11344 /* At this point, we know that we are debugging an Ada program and
11345 that the inferior has been started, but we still are not able to
0963b4bd 11346 find the run-time symbols. That can mean that we are in
0259addd
JB
11347 configurable run time mode, or that a-except as been optimized
11348 out by the linker... In any case, at this point it is not worth
11349 supporting this feature. */
11350
7dda8cff 11351 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11352}
11353
f7f9143b
JB
11354/* True iff FRAME is very likely to be that of a function that is
11355 part of the runtime system. This is all very heuristic, but is
11356 intended to be used as advice as to what frames are uninteresting
11357 to most users. */
11358
11359static int
11360is_known_support_routine (struct frame_info *frame)
11361{
692465f1 11362 enum language func_lang;
f7f9143b 11363 int i;
f35a17b5 11364 const char *fullname;
f7f9143b 11365
4ed6b5be
JB
11366 /* If this code does not have any debugging information (no symtab),
11367 This cannot be any user code. */
f7f9143b 11368
51abb421 11369 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11370 if (sal.symtab == NULL)
11371 return 1;
11372
4ed6b5be
JB
11373 /* If there is a symtab, but the associated source file cannot be
11374 located, then assume this is not user code: Selecting a frame
11375 for which we cannot display the code would not be very helpful
11376 for the user. This should also take care of case such as VxWorks
11377 where the kernel has some debugging info provided for a few units. */
f7f9143b 11378
f35a17b5
JK
11379 fullname = symtab_to_fullname (sal.symtab);
11380 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11381 return 1;
11382
85102364 11383 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11384 We also check the name of the objfile against the name of some
11385 known system libraries that sometimes come with debugging info
11386 too. */
11387
f7f9143b
JB
11388 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11389 {
11390 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11391 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11392 return 1;
eb822aa6 11393 if (SYMTAB_OBJFILE (sal.symtab) != NULL
dda83cd7
SM
11394 && re_exec (objfile_name (SYMTAB_OBJFILE (sal.symtab))))
11395 return 1;
f7f9143b
JB
11396 }
11397
4ed6b5be 11398 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11399
c6dc63a1
TT
11400 gdb::unique_xmalloc_ptr<char> func_name
11401 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11402 if (func_name == NULL)
11403 return 1;
11404
11405 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11406 {
11407 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11408 if (re_exec (func_name.get ()))
11409 return 1;
f7f9143b
JB
11410 }
11411
11412 return 0;
11413}
11414
11415/* Find the first frame that contains debugging information and that is not
11416 part of the Ada run-time, starting from FI and moving upward. */
11417
0ef643c8 11418void
f7f9143b
JB
11419ada_find_printable_frame (struct frame_info *fi)
11420{
11421 for (; fi != NULL; fi = get_prev_frame (fi))
11422 {
11423 if (!is_known_support_routine (fi))
dda83cd7
SM
11424 {
11425 select_frame (fi);
11426 break;
11427 }
f7f9143b
JB
11428 }
11429
11430}
11431
11432/* Assuming that the inferior just triggered an unhandled exception
11433 catchpoint, return the address in inferior memory where the name
11434 of the exception is stored.
11435
11436 Return zero if the address could not be computed. */
11437
11438static CORE_ADDR
11439ada_unhandled_exception_name_addr (void)
0259addd
JB
11440{
11441 return parse_and_eval_address ("e.full_name");
11442}
11443
11444/* Same as ada_unhandled_exception_name_addr, except that this function
11445 should be used when the inferior uses an older version of the runtime,
11446 where the exception name needs to be extracted from a specific frame
11447 several frames up in the callstack. */
11448
11449static CORE_ADDR
11450ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
11451{
11452 int frame_level;
11453 struct frame_info *fi;
3eecfa55 11454 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
11455
11456 /* To determine the name of this exception, we need to select
11457 the frame corresponding to RAISE_SYM_NAME. This frame is
11458 at least 3 levels up, so we simply skip the first 3 frames
11459 without checking the name of their associated function. */
11460 fi = get_current_frame ();
11461 for (frame_level = 0; frame_level < 3; frame_level += 1)
11462 if (fi != NULL)
11463 fi = get_prev_frame (fi);
11464
11465 while (fi != NULL)
11466 {
692465f1
JB
11467 enum language func_lang;
11468
c6dc63a1
TT
11469 gdb::unique_xmalloc_ptr<char> func_name
11470 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
11471 if (func_name != NULL)
11472 {
dda83cd7 11473 if (strcmp (func_name.get (),
55b87a52
KS
11474 data->exception_info->catch_exception_sym) == 0)
11475 break; /* We found the frame we were looking for... */
55b87a52 11476 }
fb44b1a7 11477 fi = get_prev_frame (fi);
f7f9143b
JB
11478 }
11479
11480 if (fi == NULL)
11481 return 0;
11482
11483 select_frame (fi);
11484 return parse_and_eval_address ("id.full_name");
11485}
11486
11487/* Assuming the inferior just triggered an Ada exception catchpoint
11488 (of any type), return the address in inferior memory where the name
11489 of the exception is stored, if applicable.
11490
45db7c09
PA
11491 Assumes the selected frame is the current frame.
11492
f7f9143b
JB
11493 Return zero if the address could not be computed, or if not relevant. */
11494
11495static CORE_ADDR
761269c8 11496ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex,
dda83cd7 11497 struct breakpoint *b)
f7f9143b 11498{
3eecfa55
JB
11499 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
11500
f7f9143b
JB
11501 switch (ex)
11502 {
761269c8 11503 case ada_catch_exception:
dda83cd7
SM
11504 return (parse_and_eval_address ("e.full_name"));
11505 break;
f7f9143b 11506
761269c8 11507 case ada_catch_exception_unhandled:
dda83cd7
SM
11508 return data->exception_info->unhandled_exception_name_addr ();
11509 break;
9f757bf7
XR
11510
11511 case ada_catch_handlers:
dda83cd7 11512 return 0; /* The runtimes does not provide access to the exception
9f757bf7 11513 name. */
dda83cd7 11514 break;
9f757bf7 11515
761269c8 11516 case ada_catch_assert:
dda83cd7
SM
11517 return 0; /* Exception name is not relevant in this case. */
11518 break;
f7f9143b
JB
11519
11520 default:
dda83cd7
SM
11521 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
11522 break;
f7f9143b
JB
11523 }
11524
11525 return 0; /* Should never be reached. */
11526}
11527
e547c119
JB
11528/* Assuming the inferior is stopped at an exception catchpoint,
11529 return the message which was associated to the exception, if
11530 available. Return NULL if the message could not be retrieved.
11531
e547c119
JB
11532 Note: The exception message can be associated to an exception
11533 either through the use of the Raise_Exception function, or
11534 more simply (Ada 2005 and later), via:
11535
11536 raise Exception_Name with "exception message";
11537
11538 */
11539
6f46ac85 11540static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11541ada_exception_message_1 (void)
11542{
11543 struct value *e_msg_val;
e547c119 11544 int e_msg_len;
e547c119
JB
11545
11546 /* For runtimes that support this feature, the exception message
11547 is passed as an unbounded string argument called "message". */
11548 e_msg_val = parse_and_eval ("message");
11549 if (e_msg_val == NULL)
11550 return NULL; /* Exception message not supported. */
11551
11552 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
11553 gdb_assert (e_msg_val != NULL);
11554 e_msg_len = TYPE_LENGTH (value_type (e_msg_val));
11555
11556 /* If the message string is empty, then treat it as if there was
11557 no exception message. */
11558 if (e_msg_len <= 0)
11559 return NULL;
11560
15f3b077
TT
11561 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
11562 read_memory (value_address (e_msg_val), (gdb_byte *) e_msg.get (),
11563 e_msg_len);
11564 e_msg.get ()[e_msg_len] = '\0';
11565
11566 return e_msg;
e547c119
JB
11567}
11568
11569/* Same as ada_exception_message_1, except that all exceptions are
11570 contained here (returning NULL instead). */
11571
6f46ac85 11572static gdb::unique_xmalloc_ptr<char>
e547c119
JB
11573ada_exception_message (void)
11574{
6f46ac85 11575 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 11576
a70b8144 11577 try
e547c119
JB
11578 {
11579 e_msg = ada_exception_message_1 ();
11580 }
230d2906 11581 catch (const gdb_exception_error &e)
e547c119 11582 {
6f46ac85 11583 e_msg.reset (nullptr);
e547c119 11584 }
e547c119
JB
11585
11586 return e_msg;
11587}
11588
f7f9143b
JB
11589/* Same as ada_exception_name_addr_1, except that it intercepts and contains
11590 any error that ada_exception_name_addr_1 might cause to be thrown.
11591 When an error is intercepted, a warning with the error message is printed,
11592 and zero is returned. */
11593
11594static CORE_ADDR
761269c8 11595ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
dda83cd7 11596 struct breakpoint *b)
f7f9143b 11597{
f7f9143b
JB
11598 CORE_ADDR result = 0;
11599
a70b8144 11600 try
f7f9143b
JB
11601 {
11602 result = ada_exception_name_addr_1 (ex, b);
11603 }
11604
230d2906 11605 catch (const gdb_exception_error &e)
f7f9143b 11606 {
3d6e9d23 11607 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
11608 return 0;
11609 }
11610
11611 return result;
11612}
11613
cb7de75e 11614static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
11615 (const char *excep_string,
11616 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
11617
11618/* Ada catchpoints.
11619
11620 In the case of catchpoints on Ada exceptions, the catchpoint will
11621 stop the target on every exception the program throws. When a user
11622 specifies the name of a specific exception, we translate this
11623 request into a condition expression (in text form), and then parse
11624 it into an expression stored in each of the catchpoint's locations.
11625 We then use this condition to check whether the exception that was
11626 raised is the one the user is interested in. If not, then the
11627 target is resumed again. We store the name of the requested
11628 exception, in order to be able to re-set the condition expression
11629 when symbols change. */
11630
11631/* An instance of this type is used to represent an Ada catchpoint
5625a286 11632 breakpoint location. */
28010a5d 11633
5625a286 11634class ada_catchpoint_location : public bp_location
28010a5d 11635{
5625a286 11636public:
5f486660 11637 ada_catchpoint_location (breakpoint *owner)
f06f1252 11638 : bp_location (owner, bp_loc_software_breakpoint)
5625a286 11639 {}
28010a5d
PA
11640
11641 /* The condition that checks whether the exception that was raised
11642 is the specific exception the user specified on catchpoint
11643 creation. */
4d01a485 11644 expression_up excep_cond_expr;
28010a5d
PA
11645};
11646
c1fc2657 11647/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 11648
c1fc2657 11649struct ada_catchpoint : public breakpoint
28010a5d 11650{
37f6a7f4
TT
11651 explicit ada_catchpoint (enum ada_exception_catchpoint_kind kind)
11652 : m_kind (kind)
11653 {
11654 }
11655
28010a5d 11656 /* The name of the specific exception the user specified. */
bc18fbb5 11657 std::string excep_string;
37f6a7f4
TT
11658
11659 /* What kind of catchpoint this is. */
11660 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
11661};
11662
11663/* Parse the exception condition string in the context of each of the
11664 catchpoint's locations, and store them for later evaluation. */
11665
11666static void
9f757bf7 11667create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 11668 enum ada_exception_catchpoint_kind ex)
28010a5d 11669{
28010a5d 11670 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 11671 if (c->excep_string.empty ())
28010a5d
PA
11672 return;
11673
11674 /* Same if there are no locations... */
c1fc2657 11675 if (c->loc == NULL)
28010a5d
PA
11676 return;
11677
fccf9de1
TT
11678 /* Compute the condition expression in text form, from the specific
11679 expection we want to catch. */
11680 std::string cond_string
11681 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 11682
fccf9de1
TT
11683 /* Iterate over all the catchpoint's locations, and parse an
11684 expression for each. */
40cb8ca5 11685 for (bp_location *bl : c->locations ())
28010a5d
PA
11686 {
11687 struct ada_catchpoint_location *ada_loc
fccf9de1 11688 = (struct ada_catchpoint_location *) bl;
4d01a485 11689 expression_up exp;
28010a5d 11690
fccf9de1 11691 if (!bl->shlib_disabled)
28010a5d 11692 {
bbc13ae3 11693 const char *s;
28010a5d 11694
cb7de75e 11695 s = cond_string.c_str ();
a70b8144 11696 try
28010a5d 11697 {
fccf9de1
TT
11698 exp = parse_exp_1 (&s, bl->address,
11699 block_for_pc (bl->address),
036e657b 11700 0);
28010a5d 11701 }
230d2906 11702 catch (const gdb_exception_error &e)
849f2b52
JB
11703 {
11704 warning (_("failed to reevaluate internal exception condition "
11705 "for catchpoint %d: %s"),
3d6e9d23 11706 c->number, e.what ());
849f2b52 11707 }
28010a5d
PA
11708 }
11709
b22e99fd 11710 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 11711 }
28010a5d
PA
11712}
11713
28010a5d
PA
11714/* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
11715 structure for all exception catchpoint kinds. */
11716
11717static struct bp_location *
37f6a7f4 11718allocate_location_exception (struct breakpoint *self)
28010a5d 11719{
5f486660 11720 return new ada_catchpoint_location (self);
28010a5d
PA
11721}
11722
11723/* Implement the RE_SET method in the breakpoint_ops structure for all
11724 exception catchpoint kinds. */
11725
11726static void
37f6a7f4 11727re_set_exception (struct breakpoint *b)
28010a5d
PA
11728{
11729 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
11730
11731 /* Call the base class's method. This updates the catchpoint's
11732 locations. */
2060206e 11733 bkpt_breakpoint_ops.re_set (b);
28010a5d
PA
11734
11735 /* Reparse the exception conditional expressions. One for each
11736 location. */
37f6a7f4 11737 create_excep_cond_exprs (c, c->m_kind);
28010a5d
PA
11738}
11739
11740/* Returns true if we should stop for this breakpoint hit. If the
11741 user specified a specific exception, we only want to cause a stop
11742 if the program thrown that exception. */
11743
7ebaa5f7 11744static bool
28010a5d
PA
11745should_stop_exception (const struct bp_location *bl)
11746{
11747 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
11748 const struct ada_catchpoint_location *ada_loc
11749 = (const struct ada_catchpoint_location *) bl;
7ebaa5f7 11750 bool stop;
28010a5d 11751
37f6a7f4
TT
11752 struct internalvar *var = lookup_internalvar ("_ada_exception");
11753 if (c->m_kind == ada_catch_assert)
11754 clear_internalvar (var);
11755 else
11756 {
11757 try
11758 {
11759 const char *expr;
11760
11761 if (c->m_kind == ada_catch_handlers)
11762 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
11763 ".all.occurrence.id");
11764 else
11765 expr = "e";
11766
11767 struct value *exc = parse_and_eval (expr);
11768 set_internalvar (var, exc);
11769 }
11770 catch (const gdb_exception_error &ex)
11771 {
11772 clear_internalvar (var);
11773 }
11774 }
11775
28010a5d 11776 /* With no specific exception, should always stop. */
bc18fbb5 11777 if (c->excep_string.empty ())
7ebaa5f7 11778 return true;
28010a5d
PA
11779
11780 if (ada_loc->excep_cond_expr == NULL)
11781 {
11782 /* We will have a NULL expression if back when we were creating
11783 the expressions, this location's had failed to parse. */
7ebaa5f7 11784 return true;
28010a5d
PA
11785 }
11786
7ebaa5f7 11787 stop = true;
a70b8144 11788 try
28010a5d
PA
11789 {
11790 struct value *mark;
11791
11792 mark = value_mark ();
4d01a485 11793 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
28010a5d
PA
11794 value_free_to_mark (mark);
11795 }
230d2906 11796 catch (const gdb_exception &ex)
492d29ea
PA
11797 {
11798 exception_fprintf (gdb_stderr, ex,
11799 _("Error in testing exception condition:\n"));
11800 }
492d29ea 11801
28010a5d
PA
11802 return stop;
11803}
11804
11805/* Implement the CHECK_STATUS method in the breakpoint_ops structure
11806 for all exception catchpoint kinds. */
11807
11808static void
37f6a7f4 11809check_status_exception (bpstat bs)
28010a5d 11810{
b6433ede 11811 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
11812}
11813
f7f9143b
JB
11814/* Implement the PRINT_IT method in the breakpoint_ops structure
11815 for all exception catchpoint kinds. */
11816
11817static enum print_stop_action
37f6a7f4 11818print_it_exception (bpstat bs)
f7f9143b 11819{
79a45e25 11820 struct ui_out *uiout = current_uiout;
348d480f
PA
11821 struct breakpoint *b = bs->breakpoint_at;
11822
956a9fb9 11823 annotate_catchpoint (b->number);
f7f9143b 11824
112e8700 11825 if (uiout->is_mi_like_p ())
f7f9143b 11826 {
112e8700 11827 uiout->field_string ("reason",
956a9fb9 11828 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 11829 uiout->field_string ("disp", bpdisp_text (b->disposition));
f7f9143b
JB
11830 }
11831
112e8700
SM
11832 uiout->text (b->disposition == disp_del
11833 ? "\nTemporary catchpoint " : "\nCatchpoint ");
381befee 11834 uiout->field_signed ("bkptno", b->number);
112e8700 11835 uiout->text (", ");
f7f9143b 11836
45db7c09
PA
11837 /* ada_exception_name_addr relies on the selected frame being the
11838 current frame. Need to do this here because this function may be
11839 called more than once when printing a stop, and below, we'll
11840 select the first frame past the Ada run-time (see
11841 ada_find_printable_frame). */
11842 select_frame (get_current_frame ());
11843
37f6a7f4
TT
11844 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
11845 switch (c->m_kind)
f7f9143b 11846 {
761269c8
JB
11847 case ada_catch_exception:
11848 case ada_catch_exception_unhandled:
9f757bf7 11849 case ada_catch_handlers:
956a9fb9 11850 {
37f6a7f4 11851 const CORE_ADDR addr = ada_exception_name_addr (c->m_kind, b);
956a9fb9
JB
11852 char exception_name[256];
11853
11854 if (addr != 0)
11855 {
c714b426
PA
11856 read_memory (addr, (gdb_byte *) exception_name,
11857 sizeof (exception_name) - 1);
956a9fb9
JB
11858 exception_name [sizeof (exception_name) - 1] = '\0';
11859 }
11860 else
11861 {
11862 /* For some reason, we were unable to read the exception
11863 name. This could happen if the Runtime was compiled
11864 without debugging info, for instance. In that case,
11865 just replace the exception name by the generic string
11866 "exception" - it will read as "an exception" in the
11867 notification we are about to print. */
967cff16 11868 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
11869 }
11870 /* In the case of unhandled exception breakpoints, we print
11871 the exception name as "unhandled EXCEPTION_NAME", to make
11872 it clearer to the user which kind of catchpoint just got
11873 hit. We used ui_out_text to make sure that this extra
11874 info does not pollute the exception name in the MI case. */
37f6a7f4 11875 if (c->m_kind == ada_catch_exception_unhandled)
112e8700
SM
11876 uiout->text ("unhandled ");
11877 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
11878 }
11879 break;
761269c8 11880 case ada_catch_assert:
956a9fb9
JB
11881 /* In this case, the name of the exception is not really
11882 important. Just print "failed assertion" to make it clearer
11883 that his program just hit an assertion-failure catchpoint.
11884 We used ui_out_text because this info does not belong in
11885 the MI output. */
112e8700 11886 uiout->text ("failed assertion");
956a9fb9 11887 break;
f7f9143b 11888 }
e547c119 11889
6f46ac85 11890 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
11891 if (exception_message != NULL)
11892 {
e547c119 11893 uiout->text (" (");
6f46ac85 11894 uiout->field_string ("exception-message", exception_message.get ());
e547c119 11895 uiout->text (")");
e547c119
JB
11896 }
11897
112e8700 11898 uiout->text (" at ");
956a9fb9 11899 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
11900
11901 return PRINT_SRC_AND_LOC;
11902}
11903
11904/* Implement the PRINT_ONE method in the breakpoint_ops structure
11905 for all exception catchpoint kinds. */
11906
11907static void
37f6a7f4 11908print_one_exception (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 11909{
79a45e25 11910 struct ui_out *uiout = current_uiout;
28010a5d 11911 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45b7d
TT
11912 struct value_print_options opts;
11913
11914 get_user_print_options (&opts);
f06f1252 11915
79a45b7d 11916 if (opts.addressprint)
f06f1252 11917 uiout->field_skip ("addr");
f7f9143b
JB
11918
11919 annotate_field (5);
37f6a7f4 11920 switch (c->m_kind)
f7f9143b 11921 {
761269c8 11922 case ada_catch_exception:
dda83cd7
SM
11923 if (!c->excep_string.empty ())
11924 {
bc18fbb5
TT
11925 std::string msg = string_printf (_("`%s' Ada exception"),
11926 c->excep_string.c_str ());
28010a5d 11927
dda83cd7
SM
11928 uiout->field_string ("what", msg);
11929 }
11930 else
11931 uiout->field_string ("what", "all Ada exceptions");
11932
11933 break;
f7f9143b 11934
761269c8 11935 case ada_catch_exception_unhandled:
dda83cd7
SM
11936 uiout->field_string ("what", "unhandled Ada exceptions");
11937 break;
f7f9143b 11938
9f757bf7 11939 case ada_catch_handlers:
dda83cd7
SM
11940 if (!c->excep_string.empty ())
11941 {
9f757bf7
XR
11942 uiout->field_fmt ("what",
11943 _("`%s' Ada exception handlers"),
bc18fbb5 11944 c->excep_string.c_str ());
dda83cd7
SM
11945 }
11946 else
9f757bf7 11947 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 11948 break;
9f757bf7 11949
761269c8 11950 case ada_catch_assert:
dda83cd7
SM
11951 uiout->field_string ("what", "failed Ada assertions");
11952 break;
f7f9143b
JB
11953
11954 default:
dda83cd7
SM
11955 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
11956 break;
f7f9143b
JB
11957 }
11958}
11959
11960/* Implement the PRINT_MENTION method in the breakpoint_ops structure
11961 for all exception catchpoint kinds. */
11962
11963static void
37f6a7f4 11964print_mention_exception (struct breakpoint *b)
f7f9143b 11965{
28010a5d 11966 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45e25 11967 struct ui_out *uiout = current_uiout;
28010a5d 11968
112e8700 11969 uiout->text (b->disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 11970 : _("Catchpoint "));
381befee 11971 uiout->field_signed ("bkptno", b->number);
112e8700 11972 uiout->text (": ");
00eb2c4a 11973
37f6a7f4 11974 switch (c->m_kind)
f7f9143b 11975 {
761269c8 11976 case ada_catch_exception:
dda83cd7 11977 if (!c->excep_string.empty ())
00eb2c4a 11978 {
862d101a 11979 std::string info = string_printf (_("`%s' Ada exception"),
bc18fbb5 11980 c->excep_string.c_str ());
4915bfdc 11981 uiout->text (info);
00eb2c4a 11982 }
dda83cd7
SM
11983 else
11984 uiout->text (_("all Ada exceptions"));
11985 break;
f7f9143b 11986
761269c8 11987 case ada_catch_exception_unhandled:
dda83cd7
SM
11988 uiout->text (_("unhandled Ada exceptions"));
11989 break;
9f757bf7
XR
11990
11991 case ada_catch_handlers:
dda83cd7 11992 if (!c->excep_string.empty ())
9f757bf7
XR
11993 {
11994 std::string info
11995 = string_printf (_("`%s' Ada exception handlers"),
bc18fbb5 11996 c->excep_string.c_str ());
4915bfdc 11997 uiout->text (info);
9f757bf7 11998 }
dda83cd7
SM
11999 else
12000 uiout->text (_("all Ada exceptions handlers"));
12001 break;
9f757bf7 12002
761269c8 12003 case ada_catch_assert:
dda83cd7
SM
12004 uiout->text (_("failed Ada assertions"));
12005 break;
f7f9143b
JB
12006
12007 default:
dda83cd7
SM
12008 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12009 break;
f7f9143b
JB
12010 }
12011}
12012
6149aea9
PA
12013/* Implement the PRINT_RECREATE method in the breakpoint_ops structure
12014 for all exception catchpoint kinds. */
12015
12016static void
37f6a7f4 12017print_recreate_exception (struct breakpoint *b, struct ui_file *fp)
6149aea9 12018{
28010a5d
PA
12019 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12020
37f6a7f4 12021 switch (c->m_kind)
6149aea9 12022 {
761269c8 12023 case ada_catch_exception:
6149aea9 12024 fprintf_filtered (fp, "catch exception");
bc18fbb5
TT
12025 if (!c->excep_string.empty ())
12026 fprintf_filtered (fp, " %s", c->excep_string.c_str ());
6149aea9
PA
12027 break;
12028
761269c8 12029 case ada_catch_exception_unhandled:
78076abc 12030 fprintf_filtered (fp, "catch exception unhandled");
6149aea9
PA
12031 break;
12032
9f757bf7
XR
12033 case ada_catch_handlers:
12034 fprintf_filtered (fp, "catch handlers");
12035 break;
12036
761269c8 12037 case ada_catch_assert:
6149aea9
PA
12038 fprintf_filtered (fp, "catch assert");
12039 break;
12040
12041 default:
12042 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12043 }
d9b3f62e 12044 print_recreate_thread (b, fp);
6149aea9
PA
12045}
12046
37f6a7f4 12047/* Virtual tables for various breakpoint types. */
2060206e 12048static struct breakpoint_ops catch_exception_breakpoint_ops;
2060206e 12049static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops;
2060206e 12050static struct breakpoint_ops catch_assert_breakpoint_ops;
9f757bf7
XR
12051static struct breakpoint_ops catch_handlers_breakpoint_ops;
12052
f06f1252
TT
12053/* See ada-lang.h. */
12054
12055bool
12056is_ada_exception_catchpoint (breakpoint *bp)
12057{
12058 return (bp->ops == &catch_exception_breakpoint_ops
12059 || bp->ops == &catch_exception_unhandled_breakpoint_ops
12060 || bp->ops == &catch_assert_breakpoint_ops
12061 || bp->ops == &catch_handlers_breakpoint_ops);
12062}
12063
f7f9143b
JB
12064/* Split the arguments specified in a "catch exception" command.
12065 Set EX to the appropriate catchpoint type.
28010a5d 12066 Set EXCEP_STRING to the name of the specific exception if
5845583d 12067 specified by the user.
9f757bf7
XR
12068 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12069 "catch handlers" command. False otherwise.
5845583d
JB
12070 If a condition is found at the end of the arguments, the condition
12071 expression is stored in COND_STRING (memory must be deallocated
12072 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12073
12074static void
a121b7c1 12075catch_ada_exception_command_split (const char *args,
9f757bf7 12076 bool is_catch_handlers_cmd,
dda83cd7 12077 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12078 std::string *excep_string,
12079 std::string *cond_string)
f7f9143b 12080{
bc18fbb5 12081 std::string exception_name;
f7f9143b 12082
bc18fbb5
TT
12083 exception_name = extract_arg (&args);
12084 if (exception_name == "if")
5845583d
JB
12085 {
12086 /* This is not an exception name; this is the start of a condition
12087 expression for a catchpoint on all exceptions. So, "un-get"
12088 this token, and set exception_name to NULL. */
bc18fbb5 12089 exception_name.clear ();
5845583d
JB
12090 args -= 2;
12091 }
f7f9143b 12092
5845583d 12093 /* Check to see if we have a condition. */
f7f9143b 12094
f1735a53 12095 args = skip_spaces (args);
61012eef 12096 if (startswith (args, "if")
5845583d
JB
12097 && (isspace (args[2]) || args[2] == '\0'))
12098 {
12099 args += 2;
f1735a53 12100 args = skip_spaces (args);
5845583d
JB
12101
12102 if (args[0] == '\0')
dda83cd7 12103 error (_("Condition missing after `if' keyword"));
bc18fbb5 12104 *cond_string = args;
5845583d
JB
12105
12106 args += strlen (args);
12107 }
12108
12109 /* Check that we do not have any more arguments. Anything else
12110 is unexpected. */
f7f9143b
JB
12111
12112 if (args[0] != '\0')
12113 error (_("Junk at end of expression"));
12114
9f757bf7
XR
12115 if (is_catch_handlers_cmd)
12116 {
12117 /* Catch handling of exceptions. */
12118 *ex = ada_catch_handlers;
12119 *excep_string = exception_name;
12120 }
bc18fbb5 12121 else if (exception_name.empty ())
f7f9143b
JB
12122 {
12123 /* Catch all exceptions. */
761269c8 12124 *ex = ada_catch_exception;
bc18fbb5 12125 excep_string->clear ();
f7f9143b 12126 }
bc18fbb5 12127 else if (exception_name == "unhandled")
f7f9143b
JB
12128 {
12129 /* Catch unhandled exceptions. */
761269c8 12130 *ex = ada_catch_exception_unhandled;
bc18fbb5 12131 excep_string->clear ();
f7f9143b
JB
12132 }
12133 else
12134 {
12135 /* Catch a specific exception. */
761269c8 12136 *ex = ada_catch_exception;
28010a5d 12137 *excep_string = exception_name;
f7f9143b
JB
12138 }
12139}
12140
12141/* Return the name of the symbol on which we should break in order to
12142 implement a catchpoint of the EX kind. */
12143
12144static const char *
761269c8 12145ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12146{
3eecfa55
JB
12147 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12148
12149 gdb_assert (data->exception_info != NULL);
0259addd 12150
f7f9143b
JB
12151 switch (ex)
12152 {
761269c8 12153 case ada_catch_exception:
dda83cd7
SM
12154 return (data->exception_info->catch_exception_sym);
12155 break;
761269c8 12156 case ada_catch_exception_unhandled:
dda83cd7
SM
12157 return (data->exception_info->catch_exception_unhandled_sym);
12158 break;
761269c8 12159 case ada_catch_assert:
dda83cd7
SM
12160 return (data->exception_info->catch_assert_sym);
12161 break;
9f757bf7 12162 case ada_catch_handlers:
dda83cd7
SM
12163 return (data->exception_info->catch_handlers_sym);
12164 break;
f7f9143b 12165 default:
dda83cd7
SM
12166 internal_error (__FILE__, __LINE__,
12167 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12168 }
12169}
12170
12171/* Return the breakpoint ops "virtual table" used for catchpoints
12172 of the EX kind. */
12173
c0a91b2b 12174static const struct breakpoint_ops *
761269c8 12175ada_exception_breakpoint_ops (enum ada_exception_catchpoint_kind ex)
f7f9143b
JB
12176{
12177 switch (ex)
12178 {
761269c8 12179 case ada_catch_exception:
dda83cd7
SM
12180 return (&catch_exception_breakpoint_ops);
12181 break;
761269c8 12182 case ada_catch_exception_unhandled:
dda83cd7
SM
12183 return (&catch_exception_unhandled_breakpoint_ops);
12184 break;
761269c8 12185 case ada_catch_assert:
dda83cd7
SM
12186 return (&catch_assert_breakpoint_ops);
12187 break;
9f757bf7 12188 case ada_catch_handlers:
dda83cd7
SM
12189 return (&catch_handlers_breakpoint_ops);
12190 break;
f7f9143b 12191 default:
dda83cd7
SM
12192 internal_error (__FILE__, __LINE__,
12193 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12194 }
12195}
12196
12197/* Return the condition that will be used to match the current exception
12198 being raised with the exception that the user wants to catch. This
12199 assumes that this condition is used when the inferior just triggered
12200 an exception catchpoint.
cb7de75e 12201 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12202
cb7de75e 12203static std::string
9f757bf7 12204ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12205 enum ada_exception_catchpoint_kind ex)
f7f9143b 12206{
3d0b0fa3 12207 int i;
fccf9de1 12208 bool is_standard_exc = false;
cb7de75e 12209 std::string result;
9f757bf7
XR
12210
12211 if (ex == ada_catch_handlers)
12212 {
12213 /* For exception handlers catchpoints, the condition string does
dda83cd7 12214 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12215 result = ("long_integer (GNAT_GCC_exception_Access"
12216 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12217 }
12218 else
fccf9de1 12219 result = "long_integer (e)";
3d0b0fa3 12220
0963b4bd 12221 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12222 runtime units that have been compiled without debugging info; if
28010a5d 12223 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12224 exception (e.g. "constraint_error") then, during the evaluation
12225 of the condition expression, the symbol lookup on this name would
0963b4bd 12226 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12227 may then be set only on user-defined exceptions which have the
12228 same not-fully-qualified name (e.g. my_package.constraint_error).
12229
12230 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12231 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12232 exception constraint_error" is rewritten into "catch exception
12233 standard.constraint_error".
12234
85102364 12235 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12236 the inferior program, then the only way to specify this exception as a
12237 breakpoint condition is to use its fully-qualified named:
fccf9de1 12238 e.g. my_package.constraint_error. */
3d0b0fa3
JB
12239
12240 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
12241 {
28010a5d 12242 if (strcmp (standard_exc [i], excep_string) == 0)
3d0b0fa3 12243 {
fccf9de1 12244 is_standard_exc = true;
9f757bf7 12245 break;
3d0b0fa3
JB
12246 }
12247 }
9f757bf7 12248
fccf9de1
TT
12249 result += " = ";
12250
12251 if (is_standard_exc)
12252 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12253 else
12254 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12255
9f757bf7 12256 return result;
f7f9143b
JB
12257}
12258
12259/* Return the symtab_and_line that should be used to insert an exception
12260 catchpoint of the TYPE kind.
12261
28010a5d
PA
12262 ADDR_STRING returns the name of the function where the real
12263 breakpoint that implements the catchpoints is set, depending on the
12264 type of catchpoint we need to create. */
f7f9143b
JB
12265
12266static struct symtab_and_line
bc18fbb5 12267ada_exception_sal (enum ada_exception_catchpoint_kind ex,
cc12f4a8 12268 std::string *addr_string, const struct breakpoint_ops **ops)
f7f9143b
JB
12269{
12270 const char *sym_name;
12271 struct symbol *sym;
f7f9143b 12272
0259addd
JB
12273 /* First, find out which exception support info to use. */
12274 ada_exception_support_info_sniffer ();
12275
12276 /* Then lookup the function on which we will break in order to catch
f7f9143b 12277 the Ada exceptions requested by the user. */
f7f9143b
JB
12278 sym_name = ada_exception_sym_name (ex);
12279 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12280
57aff202
JB
12281 if (sym == NULL)
12282 error (_("Catchpoint symbol not found: %s"), sym_name);
12283
12284 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
12285 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12286
12287 /* Set ADDR_STRING. */
cc12f4a8 12288 *addr_string = sym_name;
f7f9143b 12289
f7f9143b 12290 /* Set OPS. */
4b9eee8c 12291 *ops = ada_exception_breakpoint_ops (ex);
f7f9143b 12292
f17011e0 12293 return find_function_start_sal (sym, 1);
f7f9143b
JB
12294}
12295
b4a5b78b 12296/* Create an Ada exception catchpoint.
f7f9143b 12297
b4a5b78b 12298 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12299
bc18fbb5 12300 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12301 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12302 of the exception to which this catchpoint applies.
2df4d1d5 12303
bc18fbb5 12304 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12305
b4a5b78b
JB
12306 TEMPFLAG, if nonzero, means that the underlying breakpoint
12307 should be temporary.
28010a5d 12308
b4a5b78b 12309 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12310
349774ef 12311void
28010a5d 12312create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12313 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12314 const std::string &excep_string,
56ecd069 12315 const std::string &cond_string,
28010a5d 12316 int tempflag,
349774ef 12317 int disabled,
28010a5d
PA
12318 int from_tty)
12319{
cc12f4a8 12320 std::string addr_string;
b4a5b78b 12321 const struct breakpoint_ops *ops = NULL;
bc18fbb5 12322 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string, &ops);
28010a5d 12323
37f6a7f4 12324 std::unique_ptr<ada_catchpoint> c (new ada_catchpoint (ex_kind));
cc12f4a8 12325 init_ada_exception_breakpoint (c.get (), gdbarch, sal, addr_string.c_str (),
349774ef 12326 ops, tempflag, disabled, from_tty);
28010a5d 12327 c->excep_string = excep_string;
9f757bf7 12328 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12329 if (!cond_string.empty ())
733d554a 12330 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12331 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12332}
12333
9ac4176b
PA
12334/* Implement the "catch exception" command. */
12335
12336static void
eb4c3f4a 12337catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12338 struct cmd_list_element *command)
12339{
a121b7c1 12340 const char *arg = arg_entry;
9ac4176b
PA
12341 struct gdbarch *gdbarch = get_current_arch ();
12342 int tempflag;
761269c8 12343 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12344 std::string excep_string;
56ecd069 12345 std::string cond_string;
9ac4176b 12346
0f8e2034 12347 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12348
12349 if (!arg)
12350 arg = "";
9f757bf7 12351 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12352 &cond_string);
9f757bf7
XR
12353 create_ada_exception_catchpoint (gdbarch, ex_kind,
12354 excep_string, cond_string,
12355 tempflag, 1 /* enabled */,
12356 from_tty);
12357}
12358
12359/* Implement the "catch handlers" command. */
12360
12361static void
12362catch_ada_handlers_command (const char *arg_entry, int from_tty,
12363 struct cmd_list_element *command)
12364{
12365 const char *arg = arg_entry;
12366 struct gdbarch *gdbarch = get_current_arch ();
12367 int tempflag;
12368 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12369 std::string excep_string;
56ecd069 12370 std::string cond_string;
9f757bf7 12371
0f8e2034 12372 tempflag = command->context () == CATCH_TEMPORARY;
9f757bf7
XR
12373
12374 if (!arg)
12375 arg = "";
12376 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12377 &cond_string);
b4a5b78b
JB
12378 create_ada_exception_catchpoint (gdbarch, ex_kind,
12379 excep_string, cond_string,
349774ef
JB
12380 tempflag, 1 /* enabled */,
12381 from_tty);
9ac4176b
PA
12382}
12383
71bed2db
TT
12384/* Completion function for the Ada "catch" commands. */
12385
12386static void
12387catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12388 const char *text, const char *word)
12389{
12390 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12391
12392 for (const ada_exc_info &info : exceptions)
12393 {
12394 if (startswith (info.name, word))
b02f78f9 12395 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12396 }
12397}
12398
b4a5b78b 12399/* Split the arguments specified in a "catch assert" command.
5845583d 12400
b4a5b78b
JB
12401 ARGS contains the command's arguments (or the empty string if
12402 no arguments were passed).
5845583d
JB
12403
12404 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12405 (the memory needs to be deallocated after use). */
5845583d 12406
b4a5b78b 12407static void
56ecd069 12408catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12409{
f1735a53 12410 args = skip_spaces (args);
f7f9143b 12411
5845583d 12412 /* Check whether a condition was provided. */
61012eef 12413 if (startswith (args, "if")
5845583d 12414 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 12415 {
5845583d 12416 args += 2;
f1735a53 12417 args = skip_spaces (args);
5845583d 12418 if (args[0] == '\0')
dda83cd7 12419 error (_("condition missing after `if' keyword"));
56ecd069 12420 cond_string.assign (args);
f7f9143b
JB
12421 }
12422
5845583d
JB
12423 /* Otherwise, there should be no other argument at the end of
12424 the command. */
12425 else if (args[0] != '\0')
12426 error (_("Junk at end of arguments."));
f7f9143b
JB
12427}
12428
9ac4176b
PA
12429/* Implement the "catch assert" command. */
12430
12431static void
eb4c3f4a 12432catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12433 struct cmd_list_element *command)
12434{
a121b7c1 12435 const char *arg = arg_entry;
9ac4176b
PA
12436 struct gdbarch *gdbarch = get_current_arch ();
12437 int tempflag;
56ecd069 12438 std::string cond_string;
9ac4176b 12439
0f8e2034 12440 tempflag = command->context () == CATCH_TEMPORARY;
9ac4176b
PA
12441
12442 if (!arg)
12443 arg = "";
56ecd069 12444 catch_ada_assert_command_split (arg, cond_string);
761269c8 12445 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 12446 "", cond_string,
349774ef
JB
12447 tempflag, 1 /* enabled */,
12448 from_tty);
9ac4176b 12449}
778865d3
JB
12450
12451/* Return non-zero if the symbol SYM is an Ada exception object. */
12452
12453static int
12454ada_is_exception_sym (struct symbol *sym)
12455{
7d93a1e0 12456 const char *type_name = SYMBOL_TYPE (sym)->name ();
778865d3
JB
12457
12458 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
dda83cd7
SM
12459 && SYMBOL_CLASS (sym) != LOC_BLOCK
12460 && SYMBOL_CLASS (sym) != LOC_CONST
12461 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
12462 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
12463}
12464
12465/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12466 Ada exception object. This matches all exceptions except the ones
12467 defined by the Ada language. */
12468
12469static int
12470ada_is_non_standard_exception_sym (struct symbol *sym)
12471{
12472 int i;
12473
12474 if (!ada_is_exception_sym (sym))
12475 return 0;
12476
12477 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
987012b8 12478 if (strcmp (sym->linkage_name (), standard_exc[i]) == 0)
778865d3
JB
12479 return 0; /* A standard exception. */
12480
12481 /* Numeric_Error is also a standard exception, so exclude it.
12482 See the STANDARD_EXC description for more details as to why
12483 this exception is not listed in that array. */
987012b8 12484 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
12485 return 0;
12486
12487 return 1;
12488}
12489
ab816a27 12490/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
12491 objects.
12492
12493 The comparison is determined first by exception name, and then
12494 by exception address. */
12495
ab816a27 12496bool
cc536b21 12497ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 12498{
778865d3
JB
12499 int result;
12500
ab816a27
TT
12501 result = strcmp (name, other.name);
12502 if (result < 0)
12503 return true;
12504 if (result == 0 && addr < other.addr)
12505 return true;
12506 return false;
12507}
778865d3 12508
ab816a27 12509bool
cc536b21 12510ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
12511{
12512 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
12513}
12514
12515/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12516 routine, but keeping the first SKIP elements untouched.
12517
12518 All duplicates are also removed. */
12519
12520static void
ab816a27 12521sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
12522 int skip)
12523{
ab816a27
TT
12524 std::sort (exceptions->begin () + skip, exceptions->end ());
12525 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
12526 exceptions->end ());
778865d3
JB
12527}
12528
778865d3
JB
12529/* Add all exceptions defined by the Ada standard whose name match
12530 a regular expression.
12531
12532 If PREG is not NULL, then this regexp_t object is used to
12533 perform the symbol name matching. Otherwise, no name-based
12534 filtering is performed.
12535
12536 EXCEPTIONS is a vector of exceptions to which matching exceptions
12537 gets pushed. */
12538
12539static void
2d7cc5c7 12540ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 12541 std::vector<ada_exc_info> *exceptions)
778865d3
JB
12542{
12543 int i;
12544
12545 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
12546 {
12547 if (preg == NULL
2d7cc5c7 12548 || preg->exec (standard_exc[i], 0, NULL, 0) == 0)
778865d3
JB
12549 {
12550 struct bound_minimal_symbol msymbol
12551 = ada_lookup_simple_minsym (standard_exc[i]);
12552
12553 if (msymbol.minsym != NULL)
12554 {
12555 struct ada_exc_info info
77e371c0 12556 = {standard_exc[i], BMSYMBOL_VALUE_ADDRESS (msymbol)};
778865d3 12557
ab816a27 12558 exceptions->push_back (info);
778865d3
JB
12559 }
12560 }
12561 }
12562}
12563
12564/* Add all Ada exceptions defined locally and accessible from the given
12565 FRAME.
12566
12567 If PREG is not NULL, then this regexp_t object is used to
12568 perform the symbol name matching. Otherwise, no name-based
12569 filtering is performed.
12570
12571 EXCEPTIONS is a vector of exceptions to which matching exceptions
12572 gets pushed. */
12573
12574static void
2d7cc5c7
PA
12575ada_add_exceptions_from_frame (compiled_regex *preg,
12576 struct frame_info *frame,
ab816a27 12577 std::vector<ada_exc_info> *exceptions)
778865d3 12578{
3977b71f 12579 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
12580
12581 while (block != 0)
12582 {
12583 struct block_iterator iter;
12584 struct symbol *sym;
12585
12586 ALL_BLOCK_SYMBOLS (block, iter, sym)
12587 {
12588 switch (SYMBOL_CLASS (sym))
12589 {
12590 case LOC_TYPEDEF:
12591 case LOC_BLOCK:
12592 case LOC_CONST:
12593 break;
12594 default:
12595 if (ada_is_exception_sym (sym))
12596 {
987012b8 12597 struct ada_exc_info info = {sym->print_name (),
778865d3
JB
12598 SYMBOL_VALUE_ADDRESS (sym)};
12599
ab816a27 12600 exceptions->push_back (info);
778865d3
JB
12601 }
12602 }
12603 }
12604 if (BLOCK_FUNCTION (block) != NULL)
12605 break;
12606 block = BLOCK_SUPERBLOCK (block);
12607 }
12608}
12609
14bc53a8
PA
12610/* Return true if NAME matches PREG or if PREG is NULL. */
12611
12612static bool
2d7cc5c7 12613name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
12614{
12615 return (preg == NULL
f945dedf 12616 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
12617}
12618
778865d3
JB
12619/* Add all exceptions defined globally whose name name match
12620 a regular expression, excluding standard exceptions.
12621
12622 The reason we exclude standard exceptions is that they need
12623 to be handled separately: Standard exceptions are defined inside
12624 a runtime unit which is normally not compiled with debugging info,
12625 and thus usually do not show up in our symbol search. However,
12626 if the unit was in fact built with debugging info, we need to
12627 exclude them because they would duplicate the entry we found
12628 during the special loop that specifically searches for those
12629 standard exceptions.
12630
12631 If PREG is not NULL, then this regexp_t object is used to
12632 perform the symbol name matching. Otherwise, no name-based
12633 filtering is performed.
12634
12635 EXCEPTIONS is a vector of exceptions to which matching exceptions
12636 gets pushed. */
12637
12638static void
2d7cc5c7 12639ada_add_global_exceptions (compiled_regex *preg,
ab816a27 12640 std::vector<ada_exc_info> *exceptions)
778865d3 12641{
14bc53a8
PA
12642 /* In Ada, the symbol "search name" is a linkage name, whereas the
12643 regular expression used to do the matching refers to the natural
12644 name. So match against the decoded name. */
12645 expand_symtabs_matching (NULL,
b5ec771e 12646 lookup_name_info::match_any (),
14bc53a8
PA
12647 [&] (const char *search_name)
12648 {
f945dedf
CB
12649 std::string decoded = ada_decode (search_name);
12650 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
12651 },
12652 NULL,
03a8ea51 12653 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
14bc53a8 12654 VARIABLES_DOMAIN);
778865d3 12655
2030c079 12656 for (objfile *objfile : current_program_space->objfiles ())
778865d3 12657 {
b669c953 12658 for (compunit_symtab *s : objfile->compunits ())
778865d3 12659 {
d8aeb77f
TT
12660 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (s);
12661 int i;
778865d3 12662
d8aeb77f
TT
12663 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
12664 {
582942f4 12665 const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
d8aeb77f
TT
12666 struct block_iterator iter;
12667 struct symbol *sym;
778865d3 12668
d8aeb77f
TT
12669 ALL_BLOCK_SYMBOLS (b, iter, sym)
12670 if (ada_is_non_standard_exception_sym (sym)
987012b8 12671 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
12672 {
12673 struct ada_exc_info info
987012b8 12674 = {sym->print_name (), SYMBOL_VALUE_ADDRESS (sym)};
d8aeb77f
TT
12675
12676 exceptions->push_back (info);
12677 }
12678 }
778865d3
JB
12679 }
12680 }
12681}
12682
12683/* Implements ada_exceptions_list with the regular expression passed
12684 as a regex_t, rather than a string.
12685
12686 If not NULL, PREG is used to filter out exceptions whose names
12687 do not match. Otherwise, all exceptions are listed. */
12688
ab816a27 12689static std::vector<ada_exc_info>
2d7cc5c7 12690ada_exceptions_list_1 (compiled_regex *preg)
778865d3 12691{
ab816a27 12692 std::vector<ada_exc_info> result;
778865d3
JB
12693 int prev_len;
12694
12695 /* First, list the known standard exceptions. These exceptions
12696 need to be handled separately, as they are usually defined in
12697 runtime units that have been compiled without debugging info. */
12698
12699 ada_add_standard_exceptions (preg, &result);
12700
12701 /* Next, find all exceptions whose scope is local and accessible
12702 from the currently selected frame. */
12703
12704 if (has_stack_frames ())
12705 {
ab816a27 12706 prev_len = result.size ();
778865d3
JB
12707 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
12708 &result);
ab816a27 12709 if (result.size () > prev_len)
778865d3
JB
12710 sort_remove_dups_ada_exceptions_list (&result, prev_len);
12711 }
12712
12713 /* Add all exceptions whose scope is global. */
12714
ab816a27 12715 prev_len = result.size ();
778865d3 12716 ada_add_global_exceptions (preg, &result);
ab816a27 12717 if (result.size () > prev_len)
778865d3
JB
12718 sort_remove_dups_ada_exceptions_list (&result, prev_len);
12719
778865d3
JB
12720 return result;
12721}
12722
12723/* Return a vector of ada_exc_info.
12724
12725 If REGEXP is NULL, all exceptions are included in the result.
12726 Otherwise, it should contain a valid regular expression,
12727 and only the exceptions whose names match that regular expression
12728 are included in the result.
12729
12730 The exceptions are sorted in the following order:
12731 - Standard exceptions (defined by the Ada language), in
12732 alphabetical order;
12733 - Exceptions only visible from the current frame, in
12734 alphabetical order;
12735 - Exceptions whose scope is global, in alphabetical order. */
12736
ab816a27 12737std::vector<ada_exc_info>
778865d3
JB
12738ada_exceptions_list (const char *regexp)
12739{
2d7cc5c7
PA
12740 if (regexp == NULL)
12741 return ada_exceptions_list_1 (NULL);
778865d3 12742
2d7cc5c7
PA
12743 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
12744 return ada_exceptions_list_1 (&reg);
778865d3
JB
12745}
12746
12747/* Implement the "info exceptions" command. */
12748
12749static void
1d12d88f 12750info_exceptions_command (const char *regexp, int from_tty)
778865d3 12751{
778865d3 12752 struct gdbarch *gdbarch = get_current_arch ();
778865d3 12753
ab816a27 12754 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
12755
12756 if (regexp != NULL)
12757 printf_filtered
12758 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
12759 else
12760 printf_filtered (_("All defined Ada exceptions:\n"));
12761
ab816a27
TT
12762 for (const ada_exc_info &info : exceptions)
12763 printf_filtered ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
12764}
12765
6c038f32
PH
12766\f
12767 /* Language vector */
12768
b5ec771e
PA
12769/* symbol_name_matcher_ftype adapter for wild_match. */
12770
12771static bool
12772do_wild_match (const char *symbol_search_name,
12773 const lookup_name_info &lookup_name,
a207cff2 12774 completion_match_result *comp_match_res)
b5ec771e
PA
12775{
12776 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
12777}
12778
12779/* symbol_name_matcher_ftype adapter for full_match. */
12780
12781static bool
12782do_full_match (const char *symbol_search_name,
12783 const lookup_name_info &lookup_name,
a207cff2 12784 completion_match_result *comp_match_res)
b5ec771e 12785{
959d6a67
TT
12786 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
12787
12788 /* If both symbols start with "_ada_", just let the loop below
12789 handle the comparison. However, if only the symbol name starts
12790 with "_ada_", skip the prefix and let the match proceed as
12791 usual. */
12792 if (startswith (symbol_search_name, "_ada_")
12793 && !startswith (lname, "_ada"))
86b44259
TT
12794 symbol_search_name += 5;
12795
86b44259
TT
12796 int uscore_count = 0;
12797 while (*lname != '\0')
12798 {
12799 if (*symbol_search_name != *lname)
12800 {
12801 if (*symbol_search_name == 'B' && uscore_count == 2
12802 && symbol_search_name[1] == '_')
12803 {
12804 symbol_search_name += 2;
12805 while (isdigit (*symbol_search_name))
12806 ++symbol_search_name;
12807 if (symbol_search_name[0] == '_'
12808 && symbol_search_name[1] == '_')
12809 {
12810 symbol_search_name += 2;
12811 continue;
12812 }
12813 }
12814 return false;
12815 }
12816
12817 if (*symbol_search_name == '_')
12818 ++uscore_count;
12819 else
12820 uscore_count = 0;
12821
12822 ++symbol_search_name;
12823 ++lname;
12824 }
12825
12826 return is_name_suffix (symbol_search_name);
b5ec771e
PA
12827}
12828
a2cd4f14
JB
12829/* symbol_name_matcher_ftype for exact (verbatim) matches. */
12830
12831static bool
12832do_exact_match (const char *symbol_search_name,
12833 const lookup_name_info &lookup_name,
12834 completion_match_result *comp_match_res)
12835{
12836 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
12837}
12838
b5ec771e
PA
12839/* Build the Ada lookup name for LOOKUP_NAME. */
12840
12841ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
12842{
e0802d59 12843 gdb::string_view user_name = lookup_name.name ();
b5ec771e 12844
6a780b67 12845 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
12846 {
12847 if (user_name.back () == '>')
e0802d59 12848 m_encoded_name
5ac58899 12849 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 12850 else
e0802d59 12851 m_encoded_name
5ac58899 12852 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
12853 m_encoded_p = true;
12854 m_verbatim_p = true;
12855 m_wild_match_p = false;
12856 m_standard_p = false;
12857 }
12858 else
12859 {
12860 m_verbatim_p = false;
12861
e0802d59 12862 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
12863
12864 if (!m_encoded_p)
12865 {
e0802d59 12866 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
12867 m_encoded_name = ada_encode_1 (folded, false);
12868 if (m_encoded_name.empty ())
5ac58899 12869 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
12870 }
12871 else
5ac58899 12872 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
12873
12874 /* Handle the 'package Standard' special case. See description
12875 of m_standard_p. */
12876 if (startswith (m_encoded_name.c_str (), "standard__"))
12877 {
12878 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
12879 m_standard_p = true;
12880 }
12881 else
12882 m_standard_p = false;
74ccd7f5 12883
b5ec771e
PA
12884 /* If the name contains a ".", then the user is entering a fully
12885 qualified entity name, and the match must not be done in wild
12886 mode. Similarly, if the user wants to complete what looks
12887 like an encoded name, the match must not be done in wild
12888 mode. Also, in the standard__ special case always do
12889 non-wild matching. */
12890 m_wild_match_p
12891 = (lookup_name.match_type () != symbol_name_match_type::FULL
12892 && !m_encoded_p
12893 && !m_standard_p
12894 && user_name.find ('.') == std::string::npos);
12895 }
12896}
12897
12898/* symbol_name_matcher_ftype method for Ada. This only handles
12899 completion mode. */
12900
12901static bool
12902ada_symbol_name_matches (const char *symbol_search_name,
12903 const lookup_name_info &lookup_name,
a207cff2 12904 completion_match_result *comp_match_res)
74ccd7f5 12905{
b5ec771e
PA
12906 return lookup_name.ada ().matches (symbol_search_name,
12907 lookup_name.match_type (),
a207cff2 12908 comp_match_res);
b5ec771e
PA
12909}
12910
de63c46b
PA
12911/* A name matcher that matches the symbol name exactly, with
12912 strcmp. */
12913
12914static bool
12915literal_symbol_name_matcher (const char *symbol_search_name,
12916 const lookup_name_info &lookup_name,
12917 completion_match_result *comp_match_res)
12918{
e0802d59 12919 gdb::string_view name_view = lookup_name.name ();
de63c46b 12920
e0802d59
TT
12921 if (lookup_name.completion_mode ()
12922 ? (strncmp (symbol_search_name, name_view.data (),
12923 name_view.size ()) == 0)
12924 : symbol_search_name == name_view)
de63c46b
PA
12925 {
12926 if (comp_match_res != NULL)
12927 comp_match_res->set_match (symbol_search_name);
12928 return true;
12929 }
12930 else
12931 return false;
12932}
12933
c9debfb9 12934/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
12935 Ada. */
12936
12937static symbol_name_matcher_ftype *
12938ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
12939{
de63c46b
PA
12940 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
12941 return literal_symbol_name_matcher;
12942
b5ec771e
PA
12943 if (lookup_name.completion_mode ())
12944 return ada_symbol_name_matches;
74ccd7f5 12945 else
b5ec771e
PA
12946 {
12947 if (lookup_name.ada ().wild_match_p ())
12948 return do_wild_match;
a2cd4f14
JB
12949 else if (lookup_name.ada ().verbatim_p ())
12950 return do_exact_match;
b5ec771e
PA
12951 else
12952 return do_full_match;
12953 }
74ccd7f5
JB
12954}
12955
0874fd07
AB
12956/* Class representing the Ada language. */
12957
12958class ada_language : public language_defn
12959{
12960public:
12961 ada_language ()
0e25e767 12962 : language_defn (language_ada)
0874fd07 12963 { /* Nothing. */ }
5bd40f2a 12964
6f7664a9
AB
12965 /* See language.h. */
12966
12967 const char *name () const override
12968 { return "ada"; }
12969
12970 /* See language.h. */
12971
12972 const char *natural_name () const override
12973 { return "Ada"; }
12974
e171d6f1
AB
12975 /* See language.h. */
12976
12977 const std::vector<const char *> &filename_extensions () const override
12978 {
12979 static const std::vector<const char *> extensions
12980 = { ".adb", ".ads", ".a", ".ada", ".dg" };
12981 return extensions;
12982 }
12983
5bd40f2a
AB
12984 /* Print an array element index using the Ada syntax. */
12985
12986 void print_array_index (struct type *index_type,
12987 LONGEST index,
12988 struct ui_file *stream,
12989 const value_print_options *options) const override
12990 {
12991 struct value *index_value = val_atr (index_type, index);
12992
00c696a6 12993 value_print (index_value, stream, options);
5bd40f2a
AB
12994 fprintf_filtered (stream, " => ");
12995 }
15e5fd35
AB
12996
12997 /* Implement the "read_var_value" language_defn method for Ada. */
12998
12999 struct value *read_var_value (struct symbol *var,
13000 const struct block *var_block,
13001 struct frame_info *frame) const override
13002 {
13003 /* The only case where default_read_var_value is not sufficient
13004 is when VAR is a renaming... */
13005 if (frame != nullptr)
13006 {
13007 const struct block *frame_block = get_frame_block (frame, NULL);
13008 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13009 return ada_read_renaming_var_value (var, frame_block);
13010 }
13011
13012 /* This is a typical case where we expect the default_read_var_value
13013 function to work. */
13014 return language_defn::read_var_value (var, var_block, frame);
13015 }
1fb314aa 13016
2c71f639
TV
13017 /* See language.h. */
13018 virtual bool symbol_printing_suppressed (struct symbol *symbol) const override
13019 {
13020 return symbol->artificial;
13021 }
13022
1fb314aa
AB
13023 /* See language.h. */
13024 void language_arch_info (struct gdbarch *gdbarch,
13025 struct language_arch_info *lai) const override
13026 {
13027 const struct builtin_type *builtin = builtin_type (gdbarch);
13028
7bea47f0
AB
13029 /* Helper function to allow shorter lines below. */
13030 auto add = [&] (struct type *t)
13031 {
13032 lai->add_primitive_type (t);
13033 };
13034
13035 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13036 0, "integer"));
13037 add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13038 0, "long_integer"));
13039 add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13040 0, "short_integer"));
13041 struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
13042 0, "character");
13043 lai->set_string_char_type (char_type);
13044 add (char_type);
13045 add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
13046 "float", gdbarch_float_format (gdbarch)));
13047 add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
13048 "long_float", gdbarch_double_format (gdbarch)));
13049 add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
13050 0, "long_long_integer"));
13051 add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
13052 "long_long_float",
13053 gdbarch_long_double_format (gdbarch)));
13054 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13055 0, "natural"));
13056 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13057 0, "positive"));
13058 add (builtin->builtin_void);
13059
13060 struct type *system_addr_ptr
1fb314aa
AB
13061 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
13062 "void"));
7bea47f0
AB
13063 system_addr_ptr->set_name ("system__address");
13064 add (system_addr_ptr);
1fb314aa
AB
13065
13066 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13067 type. This is a signed integral type whose size is the same as
13068 the size of addresses. */
7bea47f0
AB
13069 unsigned int addr_length = TYPE_LENGTH (system_addr_ptr);
13070 add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
13071 "storage_offset"));
1fb314aa 13072
7bea47f0 13073 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 13074 }
4009ee92
AB
13075
13076 /* See language.h. */
13077
13078 bool iterate_over_symbols
13079 (const struct block *block, const lookup_name_info &name,
13080 domain_enum domain,
13081 gdb::function_view<symbol_found_callback_ftype> callback) const override
13082 {
d1183b06
TT
13083 std::vector<struct block_symbol> results
13084 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
13085 for (block_symbol &sym : results)
13086 {
13087 if (!callback (&sym))
13088 return false;
13089 }
13090
13091 return true;
13092 }
6f827019
AB
13093
13094 /* See language.h. */
3456e70c
TT
13095 bool sniff_from_mangled_name
13096 (const char *mangled,
13097 gdb::unique_xmalloc_ptr<char> *out) const override
6f827019
AB
13098 {
13099 std::string demangled = ada_decode (mangled);
13100
13101 *out = NULL;
13102
13103 if (demangled != mangled && demangled[0] != '<')
13104 {
13105 /* Set the gsymbol language to Ada, but still return 0.
13106 Two reasons for that:
13107
13108 1. For Ada, we prefer computing the symbol's decoded name
13109 on the fly rather than pre-compute it, in order to save
13110 memory (Ada projects are typically very large).
13111
13112 2. There are some areas in the definition of the GNAT
13113 encoding where, with a bit of bad luck, we might be able
13114 to decode a non-Ada symbol, generating an incorrect
13115 demangled name (Eg: names ending with "TB" for instance
13116 are identified as task bodies and so stripped from
13117 the decoded name returned).
13118
13119 Returning true, here, but not setting *DEMANGLED, helps us get
13120 a little bit of the best of both worlds. Because we're last,
13121 we should not affect any of the other languages that were
13122 able to demangle the symbol before us; we get to correctly
13123 tag Ada symbols as such; and even if we incorrectly tagged a
13124 non-Ada symbol, which should be rare, any routing through the
13125 Ada language should be transparent (Ada tries to behave much
13126 like C/C++ with non-Ada symbols). */
13127 return true;
13128 }
13129
13130 return false;
13131 }
fbfb0a46
AB
13132
13133 /* See language.h. */
13134
3456e70c
TT
13135 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
13136 int options) const override
0a50df5d 13137 {
3456e70c 13138 return make_unique_xstrdup (ada_decode (mangled).c_str ());
0a50df5d
AB
13139 }
13140
13141 /* See language.h. */
13142
fbfb0a46
AB
13143 void print_type (struct type *type, const char *varstring,
13144 struct ui_file *stream, int show, int level,
13145 const struct type_print_options *flags) const override
13146 {
13147 ada_print_type (type, varstring, stream, show, level, flags);
13148 }
c9debfb9 13149
53fc67f8
AB
13150 /* See language.h. */
13151
13152 const char *word_break_characters (void) const override
13153 {
13154 return ada_completer_word_break_characters;
13155 }
13156
7e56227d
AB
13157 /* See language.h. */
13158
13159 void collect_symbol_completion_matches (completion_tracker &tracker,
13160 complete_symbol_mode mode,
13161 symbol_name_match_type name_match_type,
13162 const char *text, const char *word,
13163 enum type_code code) const override
13164 {
13165 struct symbol *sym;
13166 const struct block *b, *surrounding_static_block = 0;
13167 struct block_iterator iter;
13168
13169 gdb_assert (code == TYPE_CODE_UNDEF);
13170
13171 lookup_name_info lookup_name (text, name_match_type, true);
13172
13173 /* First, look at the partial symtab symbols. */
13174 expand_symtabs_matching (NULL,
13175 lookup_name,
13176 NULL,
13177 NULL,
03a8ea51 13178 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
7e56227d
AB
13179 ALL_DOMAIN);
13180
13181 /* At this point scan through the misc symbol vectors and add each
13182 symbol you find to the list. Eventually we want to ignore
13183 anything that isn't a text symbol (everything else will be
13184 handled by the psymtab code above). */
13185
13186 for (objfile *objfile : current_program_space->objfiles ())
13187 {
13188 for (minimal_symbol *msymbol : objfile->msymbols ())
13189 {
13190 QUIT;
13191
13192 if (completion_skip_symbol (mode, msymbol))
13193 continue;
13194
13195 language symbol_language = msymbol->language ();
13196
13197 /* Ada minimal symbols won't have their language set to Ada. If
13198 we let completion_list_add_name compare using the
13199 default/C-like matcher, then when completing e.g., symbols in a
13200 package named "pck", we'd match internal Ada symbols like
13201 "pckS", which are invalid in an Ada expression, unless you wrap
13202 them in '<' '>' to request a verbatim match.
13203
13204 Unfortunately, some Ada encoded names successfully demangle as
13205 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13206 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13207 with the wrong language set. Paper over that issue here. */
13208 if (symbol_language == language_auto
13209 || symbol_language == language_cplus)
13210 symbol_language = language_ada;
13211
13212 completion_list_add_name (tracker,
13213 symbol_language,
13214 msymbol->linkage_name (),
13215 lookup_name, text, word);
13216 }
13217 }
13218
13219 /* Search upwards from currently selected frame (so that we can
13220 complete on local vars. */
13221
13222 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
13223 {
13224 if (!BLOCK_SUPERBLOCK (b))
13225 surrounding_static_block = b; /* For elmin of dups */
13226
13227 ALL_BLOCK_SYMBOLS (b, iter, sym)
13228 {
13229 if (completion_skip_symbol (mode, sym))
13230 continue;
13231
13232 completion_list_add_name (tracker,
13233 sym->language (),
13234 sym->linkage_name (),
13235 lookup_name, text, word);
13236 }
13237 }
13238
13239 /* Go through the symtabs and check the externs and statics for
13240 symbols which match. */
13241
13242 for (objfile *objfile : current_program_space->objfiles ())
13243 {
13244 for (compunit_symtab *s : objfile->compunits ())
13245 {
13246 QUIT;
13247 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), GLOBAL_BLOCK);
13248 ALL_BLOCK_SYMBOLS (b, iter, sym)
13249 {
13250 if (completion_skip_symbol (mode, sym))
13251 continue;
13252
13253 completion_list_add_name (tracker,
13254 sym->language (),
13255 sym->linkage_name (),
13256 lookup_name, text, word);
13257 }
13258 }
13259 }
13260
13261 for (objfile *objfile : current_program_space->objfiles ())
13262 {
13263 for (compunit_symtab *s : objfile->compunits ())
13264 {
13265 QUIT;
13266 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), STATIC_BLOCK);
13267 /* Don't do this block twice. */
13268 if (b == surrounding_static_block)
13269 continue;
13270 ALL_BLOCK_SYMBOLS (b, iter, sym)
13271 {
13272 if (completion_skip_symbol (mode, sym))
13273 continue;
13274
13275 completion_list_add_name (tracker,
13276 sym->language (),
13277 sym->linkage_name (),
13278 lookup_name, text, word);
13279 }
13280 }
13281 }
13282 }
13283
f16a9f57
AB
13284 /* See language.h. */
13285
13286 gdb::unique_xmalloc_ptr<char> watch_location_expression
13287 (struct type *type, CORE_ADDR addr) const override
13288 {
13289 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
13290 std::string name = type_to_string (type);
13291 return gdb::unique_xmalloc_ptr<char>
13292 (xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr)));
13293 }
13294
a1d1fa3e
AB
13295 /* See language.h. */
13296
13297 void value_print (struct value *val, struct ui_file *stream,
13298 const struct value_print_options *options) const override
13299 {
13300 return ada_value_print (val, stream, options);
13301 }
13302
ebe2334e
AB
13303 /* See language.h. */
13304
13305 void value_print_inner
13306 (struct value *val, struct ui_file *stream, int recurse,
13307 const struct value_print_options *options) const override
13308 {
13309 return ada_value_print_inner (val, stream, recurse, options);
13310 }
13311
a78a19b1
AB
13312 /* See language.h. */
13313
13314 struct block_symbol lookup_symbol_nonlocal
13315 (const char *name, const struct block *block,
13316 const domain_enum domain) const override
13317 {
13318 struct block_symbol sym;
13319
13320 sym = ada_lookup_symbol (name, block_static_block (block), domain);
13321 if (sym.symbol != NULL)
13322 return sym;
13323
13324 /* If we haven't found a match at this point, try the primitive
13325 types. In other languages, this search is performed before
13326 searching for global symbols in order to short-circuit that
13327 global-symbol search if it happens that the name corresponds
13328 to a primitive type. But we cannot do the same in Ada, because
13329 it is perfectly legitimate for a program to declare a type which
13330 has the same name as a standard type. If looking up a type in
13331 that situation, we have traditionally ignored the primitive type
13332 in favor of user-defined types. This is why, unlike most other
13333 languages, we search the primitive types this late and only after
13334 having searched the global symbols without success. */
13335
13336 if (domain == VAR_DOMAIN)
13337 {
13338 struct gdbarch *gdbarch;
13339
13340 if (block == NULL)
13341 gdbarch = target_gdbarch ();
13342 else
13343 gdbarch = block_gdbarch (block);
13344 sym.symbol
13345 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
13346 if (sym.symbol != NULL)
13347 return sym;
13348 }
13349
13350 return {};
13351 }
13352
87afa652
AB
13353 /* See language.h. */
13354
13355 int parser (struct parser_state *ps) const override
13356 {
13357 warnings_issued = 0;
13358 return ada_parse (ps);
13359 }
13360
ec8cec5b
AB
13361 /* See language.h. */
13362
13363 void emitchar (int ch, struct type *chtype,
13364 struct ui_file *stream, int quoter) const override
13365 {
13366 ada_emit_char (ch, chtype, stream, quoter, 1);
13367 }
13368
52b50f2c
AB
13369 /* See language.h. */
13370
13371 void printchar (int ch, struct type *chtype,
13372 struct ui_file *stream) const override
13373 {
13374 ada_printchar (ch, chtype, stream);
13375 }
13376
d711ee67
AB
13377 /* See language.h. */
13378
13379 void printstr (struct ui_file *stream, struct type *elttype,
13380 const gdb_byte *string, unsigned int length,
13381 const char *encoding, int force_ellipses,
13382 const struct value_print_options *options) const override
13383 {
13384 ada_printstr (stream, elttype, string, length, encoding,
13385 force_ellipses, options);
13386 }
13387
4ffc13fb
AB
13388 /* See language.h. */
13389
13390 void print_typedef (struct type *type, struct symbol *new_symbol,
13391 struct ui_file *stream) const override
13392 {
13393 ada_print_typedef (type, new_symbol, stream);
13394 }
13395
39e7ecca
AB
13396 /* See language.h. */
13397
13398 bool is_string_type_p (struct type *type) const override
13399 {
13400 return ada_is_string_type (type);
13401 }
13402
22e3f3ed
AB
13403 /* See language.h. */
13404
13405 const char *struct_too_deep_ellipsis () const override
13406 { return "(...)"; }
39e7ecca 13407
67bd3fd5
AB
13408 /* See language.h. */
13409
13410 bool c_style_arrays_p () const override
13411 { return false; }
13412
d3355e4d
AB
13413 /* See language.h. */
13414
13415 bool store_sym_names_in_linkage_form_p () const override
13416 { return true; }
13417
b63a3f3f
AB
13418 /* See language.h. */
13419
13420 const struct lang_varobj_ops *varobj_ops () const override
13421 { return &ada_varobj_ops; }
13422
c9debfb9
AB
13423protected:
13424 /* See language.h. */
13425
13426 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
13427 (const lookup_name_info &lookup_name) const override
13428 {
13429 return ada_get_symbol_name_matcher (lookup_name);
13430 }
0874fd07
AB
13431};
13432
13433/* Single instance of the Ada language class. */
13434
13435static ada_language ada_language_defn;
13436
5bf03f13
JB
13437/* Command-list for the "set/show ada" prefix command. */
13438static struct cmd_list_element *set_ada_list;
13439static struct cmd_list_element *show_ada_list;
13440
2060206e
PA
13441static void
13442initialize_ada_catchpoint_ops (void)
13443{
13444 struct breakpoint_ops *ops;
13445
13446 initialize_breakpoint_ops ();
13447
13448 ops = &catch_exception_breakpoint_ops;
13449 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
13450 ops->allocate_location = allocate_location_exception;
13451 ops->re_set = re_set_exception;
13452 ops->check_status = check_status_exception;
13453 ops->print_it = print_it_exception;
13454 ops->print_one = print_one_exception;
13455 ops->print_mention = print_mention_exception;
13456 ops->print_recreate = print_recreate_exception;
2060206e
PA
13457
13458 ops = &catch_exception_unhandled_breakpoint_ops;
13459 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
13460 ops->allocate_location = allocate_location_exception;
13461 ops->re_set = re_set_exception;
13462 ops->check_status = check_status_exception;
13463 ops->print_it = print_it_exception;
13464 ops->print_one = print_one_exception;
13465 ops->print_mention = print_mention_exception;
13466 ops->print_recreate = print_recreate_exception;
2060206e
PA
13467
13468 ops = &catch_assert_breakpoint_ops;
13469 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
13470 ops->allocate_location = allocate_location_exception;
13471 ops->re_set = re_set_exception;
13472 ops->check_status = check_status_exception;
13473 ops->print_it = print_it_exception;
13474 ops->print_one = print_one_exception;
13475 ops->print_mention = print_mention_exception;
13476 ops->print_recreate = print_recreate_exception;
9f757bf7
XR
13477
13478 ops = &catch_handlers_breakpoint_ops;
13479 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
13480 ops->allocate_location = allocate_location_exception;
13481 ops->re_set = re_set_exception;
13482 ops->check_status = check_status_exception;
13483 ops->print_it = print_it_exception;
13484 ops->print_one = print_one_exception;
13485 ops->print_mention = print_mention_exception;
13486 ops->print_recreate = print_recreate_exception;
2060206e
PA
13487}
13488
3d9434b5
JB
13489/* This module's 'new_objfile' observer. */
13490
13491static void
13492ada_new_objfile_observer (struct objfile *objfile)
13493{
13494 ada_clear_symbol_cache ();
13495}
13496
13497/* This module's 'free_objfile' observer. */
13498
13499static void
13500ada_free_objfile_observer (struct objfile *objfile)
13501{
13502 ada_clear_symbol_cache ();
13503}
13504
6c265988 13505void _initialize_ada_language ();
d2e4a39e 13506void
6c265988 13507_initialize_ada_language ()
14f9c5c9 13508{
2060206e
PA
13509 initialize_ada_catchpoint_ops ();
13510
f54bdb6d
SM
13511 add_setshow_prefix_cmd
13512 ("ada", no_class,
13513 _("Prefix command for changing Ada-specific settings."),
13514 _("Generic command for showing Ada-specific settings."),
13515 &set_ada_list, &show_ada_list,
13516 &setlist, &showlist);
5bf03f13
JB
13517
13518 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 13519 &trust_pad_over_xvs, _("\
590042fc
PW
13520Enable or disable an optimization trusting PAD types over XVS types."), _("\
13521Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 13522 _("\
5bf03f13
JB
13523This is related to the encoding used by the GNAT compiler. The debugger\n\
13524should normally trust the contents of PAD types, but certain older versions\n\
13525of GNAT have a bug that sometimes causes the information in the PAD type\n\
13526to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13527work around this bug. It is always safe to turn this option \"off\", but\n\
13528this incurs a slight performance penalty, so it is recommended to NOT change\n\
13529this option to \"off\" unless necessary."),
dda83cd7 13530 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 13531
d72413e6
PMR
13532 add_setshow_boolean_cmd ("print-signatures", class_vars,
13533 &print_signatures, _("\
13534Enable or disable the output of formal and return types for functions in the \
590042fc 13535overloads selection menu."), _("\
d72413e6 13536Show whether the output of formal and return types for functions in the \
590042fc 13537overloads selection menu is activated."),
d72413e6
PMR
13538 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
13539
9ac4176b
PA
13540 add_catch_command ("exception", _("\
13541Catch Ada exceptions, when raised.\n\
9bf7038b 13542Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
13543Without any argument, stop when any Ada exception is raised.\n\
13544If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13545being raised does not have a handler (and will therefore lead to the task's\n\
13546termination).\n\
13547Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
13548raised is the same as ARG.\n\
13549CONDITION is a boolean expression that is evaluated to see whether the\n\
13550exception should cause a stop."),
9ac4176b 13551 catch_ada_exception_command,
71bed2db 13552 catch_ada_completer,
9ac4176b
PA
13553 CATCH_PERMANENT,
13554 CATCH_TEMPORARY);
9f757bf7
XR
13555
13556 add_catch_command ("handlers", _("\
13557Catch Ada exceptions, when handled.\n\
9bf7038b
TT
13558Usage: catch handlers [ARG] [if CONDITION]\n\
13559Without any argument, stop when any Ada exception is handled.\n\
13560With an argument, catch only exceptions with the given name.\n\
13561CONDITION is a boolean expression that is evaluated to see whether the\n\
13562exception should cause a stop."),
9f757bf7 13563 catch_ada_handlers_command,
dda83cd7 13564 catch_ada_completer,
9f757bf7
XR
13565 CATCH_PERMANENT,
13566 CATCH_TEMPORARY);
9ac4176b
PA
13567 add_catch_command ("assert", _("\
13568Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
13569Usage: catch assert [if CONDITION]\n\
13570CONDITION is a boolean expression that is evaluated to see whether the\n\
13571exception should cause a stop."),
9ac4176b 13572 catch_assert_command,
dda83cd7 13573 NULL,
9ac4176b
PA
13574 CATCH_PERMANENT,
13575 CATCH_TEMPORARY);
13576
778865d3
JB
13577 add_info ("exceptions", info_exceptions_command,
13578 _("\
13579List all Ada exception names.\n\
9bf7038b 13580Usage: info exceptions [REGEXP]\n\
778865d3
JB
13581If a regular expression is passed as an argument, only those matching\n\
13582the regular expression are listed."));
13583
f54bdb6d
SM
13584 add_setshow_prefix_cmd ("ada", class_maintenance,
13585 _("Set Ada maintenance-related variables."),
13586 _("Show Ada maintenance-related variables."),
13587 &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
13588 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
c6044dd1
JB
13589
13590 add_setshow_boolean_cmd
13591 ("ignore-descriptive-types", class_maintenance,
13592 &ada_ignore_descriptive_types_p,
13593 _("Set whether descriptive types generated by GNAT should be ignored."),
13594 _("Show whether descriptive types generated by GNAT should be ignored."),
13595 _("\
13596When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
13597DWARF attribute."),
13598 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
13599
2698f5ea
TT
13600 decoded_names_store = htab_create_alloc (256, htab_hash_string,
13601 htab_eq_string,
459a2e4c 13602 NULL, xcalloc, xfree);
6b69afc4 13603
3d9434b5 13604 /* The ada-lang observers. */
c90e7d63
SM
13605 gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
13606 gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
13607 gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
14f9c5c9 13608}