]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
Refactor value_pos_atr
[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"
4de283e4 52
40bc484c 53#include "value.h"
4de283e4
TT
54#include "mi/mi-common.h"
55#include "arch-utils.h"
56#include "cli/cli-utils.h"
268a13a5
TT
57#include "gdbsupport/function-view.h"
58#include "gdbsupport/byte-vector.h"
4de283e4 59#include <algorithm>
03070ee9 60#include "ada-exp.h"
ccefe4c4 61
4c4b4cd2 62/* Define whether or not the C operator '/' truncates towards zero for
0963b4bd 63 differently signed operands (truncation direction is undefined in C).
4c4b4cd2
PH
64 Copied from valarith.c. */
65
66#ifndef TRUNCATION_TOWARDS_ZERO
67#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
68#endif
69
d2e4a39e 70static struct type *desc_base_type (struct type *);
14f9c5c9 71
d2e4a39e 72static struct type *desc_bounds_type (struct type *);
14f9c5c9 73
d2e4a39e 74static struct value *desc_bounds (struct value *);
14f9c5c9 75
d2e4a39e 76static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 77
d2e4a39e 78static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 79
556bdfd4 80static struct type *desc_data_target_type (struct type *);
14f9c5c9 81
d2e4a39e 82static struct value *desc_data (struct value *);
14f9c5c9 83
d2e4a39e 84static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 85
d2e4a39e 86static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 87
d2e4a39e 88static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 89
d2e4a39e 90static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 91
d2e4a39e 92static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 93
d2e4a39e 94static struct type *desc_index_type (struct type *, int);
14f9c5c9 95
d2e4a39e 96static int desc_arity (struct type *);
14f9c5c9 97
d2e4a39e 98static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 99
d2e4a39e 100static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 101
40bc484c 102static struct value *make_array_descriptor (struct type *, struct value *);
14f9c5c9 103
d1183b06 104static void ada_add_block_symbols (std::vector<struct block_symbol> &,
b5ec771e
PA
105 const struct block *,
106 const lookup_name_info &lookup_name,
107 domain_enum, struct objfile *);
14f9c5c9 108
d1183b06
TT
109static void ada_add_all_symbols (std::vector<struct block_symbol> &,
110 const struct block *,
b5ec771e
PA
111 const lookup_name_info &lookup_name,
112 domain_enum, int, int *);
22cee43f 113
d1183b06 114static int is_nonfunction (const std::vector<struct block_symbol> &);
14f9c5c9 115
d1183b06
TT
116static void add_defn_to_vec (std::vector<struct block_symbol> &,
117 struct symbol *,
dda83cd7 118 const struct block *);
14f9c5c9 119
e9d9f57e 120static struct value *resolve_subexp (expression_up *, int *, int,
dda83cd7 121 struct type *, int,
699bd4cf 122 innermost_block_tracker *);
14f9c5c9 123
e9d9f57e 124static void replace_operator_with_call (expression_up *, int, int, int,
dda83cd7 125 struct symbol *, const struct block *);
14f9c5c9 126
d2e4a39e 127static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 128
4c4b4cd2 129static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 130
d2e4a39e 131static int numeric_type_p (struct type *);
14f9c5c9 132
d2e4a39e 133static int integer_type_p (struct type *);
14f9c5c9 134
d2e4a39e 135static int scalar_type_p (struct type *);
14f9c5c9 136
d2e4a39e 137static int discrete_type_p (struct type *);
14f9c5c9 138
a121b7c1 139static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
dda83cd7 140 int, int);
4c4b4cd2 141
d2e4a39e 142static struct value *evaluate_subexp_type (struct expression *, int *);
14f9c5c9 143
b4ba55a1 144static struct type *ada_find_parallel_type_with_name (struct type *,
dda83cd7 145 const char *);
b4ba55a1 146
d2e4a39e 147static int is_dynamic_field (struct type *, int);
14f9c5c9 148
10a2c479 149static struct type *to_fixed_variant_branch_type (struct type *,
fc1a4b47 150 const gdb_byte *,
dda83cd7 151 CORE_ADDR, struct value *);
4c4b4cd2
PH
152
153static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 154
28c85d6c 155static struct type *to_fixed_range_type (struct type *, struct value *);
14f9c5c9 156
d2e4a39e 157static struct type *to_static_fixed_type (struct type *);
f192137b 158static struct type *static_unwrap_type (struct type *type);
14f9c5c9 159
d2e4a39e 160static struct value *unwrap_value (struct value *);
14f9c5c9 161
ad82864c 162static struct type *constrained_packed_array_type (struct type *, long *);
14f9c5c9 163
ad82864c 164static struct type *decode_constrained_packed_array_type (struct type *);
14f9c5c9 165
ad82864c
JB
166static long decode_packed_array_bitsize (struct type *);
167
168static struct value *decode_constrained_packed_array (struct value *);
169
ad82864c 170static int ada_is_unconstrained_packed_array_type (struct type *);
14f9c5c9 171
d2e4a39e 172static struct value *value_subscript_packed (struct value *, int,
dda83cd7 173 struct value **);
14f9c5c9 174
4c4b4cd2 175static struct value *coerce_unspec_val_to_type (struct value *,
dda83cd7 176 struct type *);
14f9c5c9 177
d2e4a39e 178static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 179
d2e4a39e 180static int equiv_types (struct type *, struct type *);
14f9c5c9 181
d2e4a39e 182static int is_name_suffix (const char *);
14f9c5c9 183
59c8a30b 184static int advance_wild_match (const char **, const char *, char);
73589123 185
b5ec771e 186static bool wild_match (const char *name, const char *patn);
14f9c5c9 187
d2e4a39e 188static struct value *ada_coerce_ref (struct value *);
14f9c5c9 189
4c4b4cd2
PH
190static LONGEST pos_atr (struct value *);
191
53a47a3e
TT
192static struct value *val_atr (struct type *, LONGEST);
193
4c4b4cd2 194static struct symbol *standard_lookup (const char *, const struct block *,
dda83cd7 195 domain_enum);
14f9c5c9 196
108d56a4 197static struct value *ada_search_struct_field (const char *, struct value *, int,
dda83cd7 198 struct type *);
4c4b4cd2 199
0d5cff50 200static int find_struct_field (const char *, struct type *, int,
dda83cd7 201 struct type **, int *, int *, int *, int *);
4c4b4cd2 202
d1183b06 203static int ada_resolve_function (std::vector<struct block_symbol> &,
dda83cd7
SM
204 struct value **, int, const char *,
205 struct type *, int);
4c4b4cd2 206
4c4b4cd2
PH
207static int ada_is_direct_array_type (struct type *);
208
52ce6436
PH
209static struct value *ada_index_struct_field (int, struct value *, int,
210 struct type *);
211
212static struct value *assign_aggregate (struct value *, struct value *,
0963b4bd
MS
213 struct expression *,
214 int *, enum noside);
52ce6436 215
cf608cc4 216static void aggregate_assign_from_choices (struct value *, struct value *,
52ce6436 217 struct expression *,
cf608cc4
TT
218 int *, std::vector<LONGEST> &,
219 LONGEST, LONGEST);
52ce6436
PH
220
221static void aggregate_assign_positional (struct value *, struct value *,
222 struct expression *,
cf608cc4 223 int *, std::vector<LONGEST> &,
52ce6436
PH
224 LONGEST, LONGEST);
225
226
227static void aggregate_assign_others (struct value *, struct value *,
228 struct expression *,
cf608cc4
TT
229 int *, std::vector<LONGEST> &,
230 LONGEST, LONGEST);
52ce6436
PH
231
232
cf608cc4 233static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
52ce6436
PH
234
235
236static struct value *ada_evaluate_subexp (struct type *, struct expression *,
237 int *, enum noside);
238
239static void ada_forward_operator_length (struct expression *, int, int *,
240 int *);
852dff6c
JB
241
242static struct type *ada_find_any_type (const char *name);
b5ec771e
PA
243
244static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
245 (const lookup_name_info &lookup_name);
246
4c4b4cd2
PH
247\f
248
ee01b665
JB
249/* The result of a symbol lookup to be stored in our symbol cache. */
250
251struct cache_entry
252{
253 /* The name used to perform the lookup. */
254 const char *name;
255 /* The namespace used during the lookup. */
fe978cb0 256 domain_enum domain;
ee01b665
JB
257 /* The symbol returned by the lookup, or NULL if no matching symbol
258 was found. */
259 struct symbol *sym;
260 /* The block where the symbol was found, or NULL if no matching
261 symbol was found. */
262 const struct block *block;
263 /* A pointer to the next entry with the same hash. */
264 struct cache_entry *next;
265};
266
267/* The Ada symbol cache, used to store the result of Ada-mode symbol
268 lookups in the course of executing the user's commands.
269
270 The cache is implemented using a simple, fixed-sized hash.
271 The size is fixed on the grounds that there are not likely to be
272 all that many symbols looked up during any given session, regardless
273 of the size of the symbol table. If we decide to go to a resizable
274 table, let's just use the stuff from libiberty instead. */
275
276#define HASH_SIZE 1009
277
278struct ada_symbol_cache
279{
280 /* An obstack used to store the entries in our cache. */
bdcccc56 281 struct auto_obstack cache_space;
ee01b665
JB
282
283 /* The root of the hash table used to implement our symbol cache. */
bdcccc56 284 struct cache_entry *root[HASH_SIZE] {};
ee01b665
JB
285};
286
4c4b4cd2 287/* Maximum-sized dynamic type. */
14f9c5c9
AS
288static unsigned int varsize_limit;
289
67cb5b2d 290static const char ada_completer_word_break_characters[] =
4c4b4cd2
PH
291#ifdef VMS
292 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
293#else
14f9c5c9 294 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 295#endif
14f9c5c9 296
4c4b4cd2 297/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 298static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 299 = "__gnat_ada_main_program_name";
14f9c5c9 300
4c4b4cd2
PH
301/* Limit on the number of warnings to raise per expression evaluation. */
302static int warning_limit = 2;
303
304/* Number of warning messages issued; reset to 0 by cleanups after
305 expression evaluation. */
306static int warnings_issued = 0;
307
27087b7f 308static const char * const known_runtime_file_name_patterns[] = {
4c4b4cd2
PH
309 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
310};
311
27087b7f 312static const char * const known_auxiliary_function_name_patterns[] = {
4c4b4cd2
PH
313 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
314};
315
c6044dd1
JB
316/* Maintenance-related settings for this module. */
317
318static struct cmd_list_element *maint_set_ada_cmdlist;
319static struct cmd_list_element *maint_show_ada_cmdlist;
320
c6044dd1
JB
321/* The "maintenance ada set/show ignore-descriptive-type" value. */
322
491144b5 323static bool ada_ignore_descriptive_types_p = false;
c6044dd1 324
e802dbe0
JB
325 /* Inferior-specific data. */
326
327/* Per-inferior data for this module. */
328
329struct ada_inferior_data
330{
331 /* The ada__tags__type_specific_data type, which is used when decoding
332 tagged types. With older versions of GNAT, this type was directly
333 accessible through a component ("tsd") in the object tag. But this
334 is no longer the case, so we cache it for each inferior. */
f37b313d 335 struct type *tsd_type = nullptr;
3eecfa55
JB
336
337 /* The exception_support_info data. This data is used to determine
338 how to implement support for Ada exception catchpoints in a given
339 inferior. */
f37b313d 340 const struct exception_support_info *exception_info = nullptr;
e802dbe0
JB
341};
342
343/* Our key to this module's inferior data. */
f37b313d 344static const struct inferior_key<ada_inferior_data> ada_inferior_data;
e802dbe0
JB
345
346/* Return our inferior data for the given inferior (INF).
347
348 This function always returns a valid pointer to an allocated
349 ada_inferior_data structure. If INF's inferior data has not
350 been previously set, this functions creates a new one with all
351 fields set to zero, sets INF's inferior to it, and then returns
352 a pointer to that newly allocated ada_inferior_data. */
353
354static struct ada_inferior_data *
355get_ada_inferior_data (struct inferior *inf)
356{
357 struct ada_inferior_data *data;
358
f37b313d 359 data = ada_inferior_data.get (inf);
e802dbe0 360 if (data == NULL)
f37b313d 361 data = ada_inferior_data.emplace (inf);
e802dbe0
JB
362
363 return data;
364}
365
366/* Perform all necessary cleanups regarding our module's inferior data
367 that is required after the inferior INF just exited. */
368
369static void
370ada_inferior_exit (struct inferior *inf)
371{
f37b313d 372 ada_inferior_data.clear (inf);
e802dbe0
JB
373}
374
ee01b665
JB
375
376 /* program-space-specific data. */
377
378/* This module's per-program-space data. */
379struct ada_pspace_data
380{
381 /* The Ada symbol cache. */
bdcccc56 382 std::unique_ptr<ada_symbol_cache> sym_cache;
ee01b665
JB
383};
384
385/* Key to our per-program-space data. */
f37b313d 386static const struct program_space_key<ada_pspace_data> ada_pspace_data_handle;
ee01b665
JB
387
388/* Return this module's data for the given program space (PSPACE).
389 If not is found, add a zero'ed one now.
390
391 This function always returns a valid object. */
392
393static struct ada_pspace_data *
394get_ada_pspace_data (struct program_space *pspace)
395{
396 struct ada_pspace_data *data;
397
f37b313d 398 data = ada_pspace_data_handle.get (pspace);
ee01b665 399 if (data == NULL)
f37b313d 400 data = ada_pspace_data_handle.emplace (pspace);
ee01b665
JB
401
402 return data;
403}
404
dda83cd7 405 /* Utilities */
4c4b4cd2 406
720d1a40 407/* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
eed9788b 408 all typedef layers have been peeled. Otherwise, return TYPE.
720d1a40
JB
409
410 Normally, we really expect a typedef type to only have 1 typedef layer.
411 In other words, we really expect the target type of a typedef type to be
412 a non-typedef type. This is particularly true for Ada units, because
413 the language does not have a typedef vs not-typedef distinction.
414 In that respect, the Ada compiler has been trying to eliminate as many
415 typedef definitions in the debugging information, since they generally
416 do not bring any extra information (we still use typedef under certain
417 circumstances related mostly to the GNAT encoding).
418
419 Unfortunately, we have seen situations where the debugging information
420 generated by the compiler leads to such multiple typedef layers. For
421 instance, consider the following example with stabs:
422
423 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
424 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
425
426 This is an error in the debugging information which causes type
427 pck__float_array___XUP to be defined twice, and the second time,
428 it is defined as a typedef of a typedef.
429
430 This is on the fringe of legality as far as debugging information is
431 concerned, and certainly unexpected. But it is easy to handle these
432 situations correctly, so we can afford to be lenient in this case. */
433
434static struct type *
435ada_typedef_target_type (struct type *type)
436{
78134374 437 while (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
438 type = TYPE_TARGET_TYPE (type);
439 return type;
440}
441
41d27058
JB
442/* Given DECODED_NAME a string holding a symbol name in its
443 decoded form (ie using the Ada dotted notation), returns
444 its unqualified name. */
445
446static const char *
447ada_unqualified_name (const char *decoded_name)
448{
2b0f535a
JB
449 const char *result;
450
451 /* If the decoded name starts with '<', it means that the encoded
452 name does not follow standard naming conventions, and thus that
453 it is not your typical Ada symbol name. Trying to unqualify it
454 is therefore pointless and possibly erroneous. */
455 if (decoded_name[0] == '<')
456 return decoded_name;
457
458 result = strrchr (decoded_name, '.');
41d27058
JB
459 if (result != NULL)
460 result++; /* Skip the dot... */
461 else
462 result = decoded_name;
463
464 return result;
465}
466
39e7af3e 467/* Return a string starting with '<', followed by STR, and '>'. */
41d27058 468
39e7af3e 469static std::string
41d27058
JB
470add_angle_brackets (const char *str)
471{
39e7af3e 472 return string_printf ("<%s>", str);
41d27058 473}
96d887e8 474
14f9c5c9 475/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 476 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
477
478static int
ebf56fd3 479field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
480{
481 int len = strlen (target);
5b4ee69b 482
d2e4a39e 483 return
4c4b4cd2
PH
484 (strncmp (field_name, target, len) == 0
485 && (field_name[len] == '\0'
dda83cd7
SM
486 || (startswith (field_name + len, "___")
487 && strcmp (field_name + strlen (field_name) - 6,
488 "___XVN") != 0)));
14f9c5c9
AS
489}
490
491
872c8b51
JB
492/* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
493 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
494 and return its index. This function also handles fields whose name
495 have ___ suffixes because the compiler sometimes alters their name
496 by adding such a suffix to represent fields with certain constraints.
497 If the field could not be found, return a negative number if
498 MAYBE_MISSING is set. Otherwise raise an error. */
4c4b4cd2
PH
499
500int
501ada_get_field_index (const struct type *type, const char *field_name,
dda83cd7 502 int maybe_missing)
4c4b4cd2
PH
503{
504 int fieldno;
872c8b51
JB
505 struct type *struct_type = check_typedef ((struct type *) type);
506
1f704f76 507 for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
872c8b51 508 if (field_name_match (TYPE_FIELD_NAME (struct_type, fieldno), field_name))
4c4b4cd2
PH
509 return fieldno;
510
511 if (!maybe_missing)
323e0a4a 512 error (_("Unable to find field %s in struct %s. Aborting"),
dda83cd7 513 field_name, struct_type->name ());
4c4b4cd2
PH
514
515 return -1;
516}
517
518/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
519
520int
d2e4a39e 521ada_name_prefix_len (const char *name)
14f9c5c9
AS
522{
523 if (name == NULL)
524 return 0;
d2e4a39e 525 else
14f9c5c9 526 {
d2e4a39e 527 const char *p = strstr (name, "___");
5b4ee69b 528
14f9c5c9 529 if (p == NULL)
dda83cd7 530 return strlen (name);
14f9c5c9 531 else
dda83cd7 532 return p - name;
14f9c5c9
AS
533 }
534}
535
4c4b4cd2
PH
536/* Return non-zero if SUFFIX is a suffix of STR.
537 Return zero if STR is null. */
538
14f9c5c9 539static int
d2e4a39e 540is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
541{
542 int len1, len2;
5b4ee69b 543
14f9c5c9
AS
544 if (str == NULL)
545 return 0;
546 len1 = strlen (str);
547 len2 = strlen (suffix);
4c4b4cd2 548 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
549}
550
4c4b4cd2
PH
551/* The contents of value VAL, treated as a value of type TYPE. The
552 result is an lval in memory if VAL is. */
14f9c5c9 553
d2e4a39e 554static struct value *
4c4b4cd2 555coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9 556{
61ee279c 557 type = ada_check_typedef (type);
df407dfe 558 if (value_type (val) == type)
4c4b4cd2 559 return val;
d2e4a39e 560 else
14f9c5c9 561 {
4c4b4cd2
PH
562 struct value *result;
563
564 /* Make sure that the object size is not unreasonable before
dda83cd7 565 trying to allocate some memory for it. */
c1b5a1a6 566 ada_ensure_varsize_limit (type);
4c4b4cd2 567
f73e424f
TT
568 if (value_optimized_out (val))
569 result = allocate_optimized_out_value (type);
570 else if (value_lazy (val)
571 /* Be careful not to make a lazy not_lval value. */
572 || (VALUE_LVAL (val) != not_lval
573 && TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val))))
41e8491f
JK
574 result = allocate_value_lazy (type);
575 else
576 {
577 result = allocate_value (type);
f73e424f 578 value_contents_copy (result, 0, val, 0, TYPE_LENGTH (type));
41e8491f 579 }
74bcbdf3 580 set_value_component_location (result, val);
9bbda503
AC
581 set_value_bitsize (result, value_bitsize (val));
582 set_value_bitpos (result, value_bitpos (val));
c408a94f
TT
583 if (VALUE_LVAL (result) == lval_memory)
584 set_value_address (result, value_address (val));
14f9c5c9
AS
585 return result;
586 }
587}
588
fc1a4b47
AC
589static const gdb_byte *
590cond_offset_host (const gdb_byte *valaddr, long offset)
14f9c5c9
AS
591{
592 if (valaddr == NULL)
593 return NULL;
594 else
595 return valaddr + offset;
596}
597
598static CORE_ADDR
ebf56fd3 599cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
600{
601 if (address == 0)
602 return 0;
d2e4a39e 603 else
14f9c5c9
AS
604 return address + offset;
605}
606
4c4b4cd2
PH
607/* Issue a warning (as for the definition of warning in utils.c, but
608 with exactly one argument rather than ...), unless the limit on the
609 number of warnings has passed during the evaluation of the current
610 expression. */
a2249542 611
77109804
AC
612/* FIXME: cagney/2004-10-10: This function is mimicking the behavior
613 provided by "complaint". */
a0b31db1 614static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
77109804 615
14f9c5c9 616static void
a2249542 617lim_warning (const char *format, ...)
14f9c5c9 618{
a2249542 619 va_list args;
a2249542 620
5b4ee69b 621 va_start (args, format);
4c4b4cd2
PH
622 warnings_issued += 1;
623 if (warnings_issued <= warning_limit)
a2249542
MK
624 vwarning (format, args);
625
626 va_end (args);
4c4b4cd2
PH
627}
628
714e53ab
PH
629/* Issue an error if the size of an object of type T is unreasonable,
630 i.e. if it would be a bad idea to allocate a value of this type in
631 GDB. */
632
c1b5a1a6
JB
633void
634ada_ensure_varsize_limit (const struct type *type)
714e53ab
PH
635{
636 if (TYPE_LENGTH (type) > varsize_limit)
323e0a4a 637 error (_("object size is larger than varsize-limit"));
714e53ab
PH
638}
639
0963b4bd 640/* Maximum value of a SIZE-byte signed integer type. */
4c4b4cd2 641static LONGEST
c3e5cd34 642max_of_size (int size)
4c4b4cd2 643{
76a01679 644 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
5b4ee69b 645
76a01679 646 return top_bit | (top_bit - 1);
4c4b4cd2
PH
647}
648
0963b4bd 649/* Minimum value of a SIZE-byte signed integer type. */
4c4b4cd2 650static LONGEST
c3e5cd34 651min_of_size (int size)
4c4b4cd2 652{
c3e5cd34 653 return -max_of_size (size) - 1;
4c4b4cd2
PH
654}
655
0963b4bd 656/* Maximum value of a SIZE-byte unsigned integer type. */
4c4b4cd2 657static ULONGEST
c3e5cd34 658umax_of_size (int size)
4c4b4cd2 659{
76a01679 660 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
5b4ee69b 661
76a01679 662 return top_bit | (top_bit - 1);
4c4b4cd2
PH
663}
664
0963b4bd 665/* Maximum value of integral type T, as a signed quantity. */
c3e5cd34
PH
666static LONGEST
667max_of_type (struct type *t)
4c4b4cd2 668{
c6d940a9 669 if (t->is_unsigned ())
c3e5cd34
PH
670 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
671 else
672 return max_of_size (TYPE_LENGTH (t));
673}
674
0963b4bd 675/* Minimum value of integral type T, as a signed quantity. */
c3e5cd34
PH
676static LONGEST
677min_of_type (struct type *t)
678{
c6d940a9 679 if (t->is_unsigned ())
c3e5cd34
PH
680 return 0;
681 else
682 return min_of_size (TYPE_LENGTH (t));
4c4b4cd2
PH
683}
684
685/* The largest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
686LONGEST
687ada_discrete_type_high_bound (struct type *type)
4c4b4cd2 688{
b249d2c2 689 type = resolve_dynamic_type (type, {}, 0);
78134374 690 switch (type->code ())
4c4b4cd2
PH
691 {
692 case TYPE_CODE_RANGE:
d1fd641e
SM
693 {
694 const dynamic_prop &high = type->bounds ()->high;
695
696 if (high.kind () == PROP_CONST)
697 return high.const_val ();
698 else
699 {
700 gdb_assert (high.kind () == PROP_UNDEFINED);
701
702 /* This happens when trying to evaluate a type's dynamic bound
703 without a live target. There is nothing relevant for us to
704 return here, so return 0. */
705 return 0;
706 }
707 }
4c4b4cd2 708 case TYPE_CODE_ENUM:
1f704f76 709 return TYPE_FIELD_ENUMVAL (type, type->num_fields () - 1);
690cc4eb
PH
710 case TYPE_CODE_BOOL:
711 return 1;
712 case TYPE_CODE_CHAR:
76a01679 713 case TYPE_CODE_INT:
690cc4eb 714 return max_of_type (type);
4c4b4cd2 715 default:
43bbcdc2 716 error (_("Unexpected type in ada_discrete_type_high_bound."));
4c4b4cd2
PH
717 }
718}
719
14e75d8e 720/* The smallest value in the domain of TYPE, a discrete type, as an integer. */
43bbcdc2
PH
721LONGEST
722ada_discrete_type_low_bound (struct type *type)
4c4b4cd2 723{
b249d2c2 724 type = resolve_dynamic_type (type, {}, 0);
78134374 725 switch (type->code ())
4c4b4cd2
PH
726 {
727 case TYPE_CODE_RANGE:
d1fd641e
SM
728 {
729 const dynamic_prop &low = type->bounds ()->low;
730
731 if (low.kind () == PROP_CONST)
732 return low.const_val ();
733 else
734 {
735 gdb_assert (low.kind () == PROP_UNDEFINED);
736
737 /* This happens when trying to evaluate a type's dynamic bound
738 without a live target. There is nothing relevant for us to
739 return here, so return 0. */
740 return 0;
741 }
742 }
4c4b4cd2 743 case TYPE_CODE_ENUM:
14e75d8e 744 return TYPE_FIELD_ENUMVAL (type, 0);
690cc4eb
PH
745 case TYPE_CODE_BOOL:
746 return 0;
747 case TYPE_CODE_CHAR:
76a01679 748 case TYPE_CODE_INT:
690cc4eb 749 return min_of_type (type);
4c4b4cd2 750 default:
43bbcdc2 751 error (_("Unexpected type in ada_discrete_type_low_bound."));
4c4b4cd2
PH
752 }
753}
754
755/* The identity on non-range types. For range types, the underlying
76a01679 756 non-range scalar type. */
4c4b4cd2
PH
757
758static struct type *
18af8284 759get_base_type (struct type *type)
4c4b4cd2 760{
78134374 761 while (type != NULL && type->code () == TYPE_CODE_RANGE)
4c4b4cd2 762 {
76a01679 763 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
dda83cd7 764 return type;
4c4b4cd2
PH
765 type = TYPE_TARGET_TYPE (type);
766 }
767 return type;
14f9c5c9 768}
41246937
JB
769
770/* Return a decoded version of the given VALUE. This means returning
771 a value whose type is obtained by applying all the GNAT-specific
85102364 772 encodings, making the resulting type a static but standard description
41246937
JB
773 of the initial type. */
774
775struct value *
776ada_get_decoded_value (struct value *value)
777{
778 struct type *type = ada_check_typedef (value_type (value));
779
780 if (ada_is_array_descriptor_type (type)
781 || (ada_is_constrained_packed_array_type (type)
dda83cd7 782 && type->code () != TYPE_CODE_PTR))
41246937 783 {
78134374 784 if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
dda83cd7 785 value = ada_coerce_to_simple_array_ptr (value);
41246937 786 else
dda83cd7 787 value = ada_coerce_to_simple_array (value);
41246937
JB
788 }
789 else
790 value = ada_to_fixed_value (value);
791
792 return value;
793}
794
795/* Same as ada_get_decoded_value, but with the given TYPE.
796 Because there is no associated actual value for this type,
797 the resulting type might be a best-effort approximation in
798 the case of dynamic types. */
799
800struct type *
801ada_get_decoded_type (struct type *type)
802{
803 type = to_static_fixed_type (type);
804 if (ada_is_constrained_packed_array_type (type))
805 type = ada_coerce_to_simple_array_type (type);
806 return type;
807}
808
4c4b4cd2 809\f
76a01679 810
dda83cd7 811 /* Language Selection */
14f9c5c9
AS
812
813/* If the main program is in Ada, return language_ada, otherwise return LANG
ccefe4c4 814 (the main program is in Ada iif the adainit symbol is found). */
d2e4a39e 815
de93309a 816static enum language
ccefe4c4 817ada_update_initial_language (enum language lang)
14f9c5c9 818{
cafb3438 819 if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
4c4b4cd2 820 return language_ada;
14f9c5c9
AS
821
822 return lang;
823}
96d887e8
PH
824
825/* If the main procedure is written in Ada, then return its name.
826 The result is good until the next call. Return NULL if the main
827 procedure doesn't appear to be in Ada. */
828
829char *
830ada_main_name (void)
831{
3b7344d5 832 struct bound_minimal_symbol msym;
e83e4e24 833 static gdb::unique_xmalloc_ptr<char> main_program_name;
6c038f32 834
96d887e8
PH
835 /* For Ada, the name of the main procedure is stored in a specific
836 string constant, generated by the binder. Look for that symbol,
837 extract its address, and then read that string. If we didn't find
838 that string, then most probably the main procedure is not written
839 in Ada. */
840 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
841
3b7344d5 842 if (msym.minsym != NULL)
96d887e8 843 {
66920317 844 CORE_ADDR main_program_name_addr = BMSYMBOL_VALUE_ADDRESS (msym);
96d887e8 845 if (main_program_name_addr == 0)
dda83cd7 846 error (_("Invalid address for Ada main program name."));
96d887e8 847
66920317 848 main_program_name = target_read_string (main_program_name_addr, 1024);
e83e4e24 849 return main_program_name.get ();
96d887e8
PH
850 }
851
852 /* The main procedure doesn't seem to be in Ada. */
853 return NULL;
854}
14f9c5c9 855\f
dda83cd7 856 /* Symbols */
d2e4a39e 857
4c4b4cd2
PH
858/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
859 of NULLs. */
14f9c5c9 860
d2e4a39e
AS
861const struct ada_opname_map ada_opname_table[] = {
862 {"Oadd", "\"+\"", BINOP_ADD},
863 {"Osubtract", "\"-\"", BINOP_SUB},
864 {"Omultiply", "\"*\"", BINOP_MUL},
865 {"Odivide", "\"/\"", BINOP_DIV},
866 {"Omod", "\"mod\"", BINOP_MOD},
867 {"Orem", "\"rem\"", BINOP_REM},
868 {"Oexpon", "\"**\"", BINOP_EXP},
869 {"Olt", "\"<\"", BINOP_LESS},
870 {"Ole", "\"<=\"", BINOP_LEQ},
871 {"Ogt", "\">\"", BINOP_GTR},
872 {"Oge", "\">=\"", BINOP_GEQ},
873 {"Oeq", "\"=\"", BINOP_EQUAL},
874 {"One", "\"/=\"", BINOP_NOTEQUAL},
875 {"Oand", "\"and\"", BINOP_BITWISE_AND},
876 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
877 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
878 {"Oconcat", "\"&\"", BINOP_CONCAT},
879 {"Oabs", "\"abs\"", UNOP_ABS},
880 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
881 {"Oadd", "\"+\"", UNOP_PLUS},
882 {"Osubtract", "\"-\"", UNOP_NEG},
883 {NULL, NULL}
14f9c5c9
AS
884};
885
5c4258f4 886/* The "encoded" form of DECODED, according to GNAT conventions. If
b5ec771e 887 THROW_ERRORS, throw an error if invalid operator name is found.
5c4258f4 888 Otherwise, return the empty string in that case. */
4c4b4cd2 889
5c4258f4 890static std::string
b5ec771e 891ada_encode_1 (const char *decoded, bool throw_errors)
14f9c5c9 892{
4c4b4cd2 893 if (decoded == NULL)
5c4258f4 894 return {};
14f9c5c9 895
5c4258f4
TT
896 std::string encoding_buffer;
897 for (const char *p = decoded; *p != '\0'; p += 1)
14f9c5c9 898 {
cdc7bb92 899 if (*p == '.')
5c4258f4 900 encoding_buffer.append ("__");
14f9c5c9 901 else if (*p == '"')
dda83cd7
SM
902 {
903 const struct ada_opname_map *mapping;
904
905 for (mapping = ada_opname_table;
906 mapping->encoded != NULL
907 && !startswith (p, mapping->decoded); mapping += 1)
908 ;
909 if (mapping->encoded == NULL)
b5ec771e
PA
910 {
911 if (throw_errors)
912 error (_("invalid Ada operator name: %s"), p);
913 else
5c4258f4 914 return {};
b5ec771e 915 }
5c4258f4 916 encoding_buffer.append (mapping->encoded);
dda83cd7
SM
917 break;
918 }
d2e4a39e 919 else
5c4258f4 920 encoding_buffer.push_back (*p);
14f9c5c9
AS
921 }
922
4c4b4cd2 923 return encoding_buffer;
14f9c5c9
AS
924}
925
5c4258f4 926/* The "encoded" form of DECODED, according to GNAT conventions. */
b5ec771e 927
5c4258f4 928std::string
b5ec771e
PA
929ada_encode (const char *decoded)
930{
931 return ada_encode_1 (decoded, true);
932}
933
14f9c5c9 934/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
935 quotes, unfolded, but with the quotes stripped away. Result good
936 to next call. */
937
5f9febe0 938static const char *
e0802d59 939ada_fold_name (gdb::string_view name)
14f9c5c9 940{
5f9febe0 941 static std::string fold_storage;
14f9c5c9 942
6a780b67 943 if (!name.empty () && name[0] == '\'')
01573d73 944 fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
14f9c5c9
AS
945 else
946 {
01573d73 947 fold_storage = gdb::to_string (name);
5f9febe0
TT
948 for (int i = 0; i < name.size (); i += 1)
949 fold_storage[i] = tolower (fold_storage[i]);
14f9c5c9
AS
950 }
951
5f9febe0 952 return fold_storage.c_str ();
14f9c5c9
AS
953}
954
529cad9c
PH
955/* Return nonzero if C is either a digit or a lowercase alphabet character. */
956
957static int
958is_lower_alphanum (const char c)
959{
960 return (isdigit (c) || (isalpha (c) && islower (c)));
961}
962
c90092fe
JB
963/* ENCODED is the linkage name of a symbol and LEN contains its length.
964 This function saves in LEN the length of that same symbol name but
965 without either of these suffixes:
29480c32
JB
966 . .{DIGIT}+
967 . ${DIGIT}+
968 . ___{DIGIT}+
969 . __{DIGIT}+.
c90092fe 970
29480c32
JB
971 These are suffixes introduced by the compiler for entities such as
972 nested subprogram for instance, in order to avoid name clashes.
973 They do not serve any purpose for the debugger. */
974
975static void
976ada_remove_trailing_digits (const char *encoded, int *len)
977{
978 if (*len > 1 && isdigit (encoded[*len - 1]))
979 {
980 int i = *len - 2;
5b4ee69b 981
29480c32 982 while (i > 0 && isdigit (encoded[i]))
dda83cd7 983 i--;
29480c32 984 if (i >= 0 && encoded[i] == '.')
dda83cd7 985 *len = i;
29480c32 986 else if (i >= 0 && encoded[i] == '$')
dda83cd7 987 *len = i;
61012eef 988 else if (i >= 2 && startswith (encoded + i - 2, "___"))
dda83cd7 989 *len = i - 2;
61012eef 990 else if (i >= 1 && startswith (encoded + i - 1, "__"))
dda83cd7 991 *len = i - 1;
29480c32
JB
992 }
993}
994
995/* Remove the suffix introduced by the compiler for protected object
996 subprograms. */
997
998static void
999ada_remove_po_subprogram_suffix (const char *encoded, int *len)
1000{
1001 /* Remove trailing N. */
1002
1003 /* Protected entry subprograms are broken into two
1004 separate subprograms: The first one is unprotected, and has
1005 a 'N' suffix; the second is the protected version, and has
0963b4bd 1006 the 'P' suffix. The second calls the first one after handling
29480c32
JB
1007 the protection. Since the P subprograms are internally generated,
1008 we leave these names undecoded, giving the user a clue that this
1009 entity is internal. */
1010
1011 if (*len > 1
1012 && encoded[*len - 1] == 'N'
1013 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
1014 *len = *len - 1;
1015}
1016
1017/* If ENCODED follows the GNAT entity encoding conventions, then return
1018 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
f945dedf 1019 replaced by ENCODED. */
14f9c5c9 1020
f945dedf 1021std::string
4c4b4cd2 1022ada_decode (const char *encoded)
14f9c5c9
AS
1023{
1024 int i, j;
1025 int len0;
d2e4a39e 1026 const char *p;
14f9c5c9 1027 int at_start_name;
f945dedf 1028 std::string decoded;
d2e4a39e 1029
0d81f350
JG
1030 /* With function descriptors on PPC64, the value of a symbol named
1031 ".FN", if it exists, is the entry point of the function "FN". */
1032 if (encoded[0] == '.')
1033 encoded += 1;
1034
29480c32
JB
1035 /* The name of the Ada main procedure starts with "_ada_".
1036 This prefix is not part of the decoded name, so skip this part
1037 if we see this prefix. */
61012eef 1038 if (startswith (encoded, "_ada_"))
4c4b4cd2 1039 encoded += 5;
14f9c5c9 1040
29480c32
JB
1041 /* If the name starts with '_', then it is not a properly encoded
1042 name, so do not attempt to decode it. Similarly, if the name
1043 starts with '<', the name should not be decoded. */
4c4b4cd2 1044 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
1045 goto Suppress;
1046
4c4b4cd2 1047 len0 = strlen (encoded);
4c4b4cd2 1048
29480c32
JB
1049 ada_remove_trailing_digits (encoded, &len0);
1050 ada_remove_po_subprogram_suffix (encoded, &len0);
529cad9c 1051
4c4b4cd2
PH
1052 /* Remove the ___X.* suffix if present. Do not forget to verify that
1053 the suffix is located before the current "end" of ENCODED. We want
1054 to avoid re-matching parts of ENCODED that have previously been
1055 marked as discarded (by decrementing LEN0). */
1056 p = strstr (encoded, "___");
1057 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
1058 {
1059 if (p[3] == 'X')
dda83cd7 1060 len0 = p - encoded;
14f9c5c9 1061 else
dda83cd7 1062 goto Suppress;
14f9c5c9 1063 }
4c4b4cd2 1064
29480c32
JB
1065 /* Remove any trailing TKB suffix. It tells us that this symbol
1066 is for the body of a task, but that information does not actually
1067 appear in the decoded name. */
1068
61012eef 1069 if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
14f9c5c9 1070 len0 -= 3;
76a01679 1071
a10967fa
JB
1072 /* Remove any trailing TB suffix. The TB suffix is slightly different
1073 from the TKB suffix because it is used for non-anonymous task
1074 bodies. */
1075
61012eef 1076 if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
a10967fa
JB
1077 len0 -= 2;
1078
29480c32
JB
1079 /* Remove trailing "B" suffixes. */
1080 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1081
61012eef 1082 if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
14f9c5c9
AS
1083 len0 -= 1;
1084
4c4b4cd2 1085 /* Make decoded big enough for possible expansion by operator name. */
29480c32 1086
f945dedf 1087 decoded.resize (2 * len0 + 1, 'X');
14f9c5c9 1088
29480c32
JB
1089 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1090
4c4b4cd2 1091 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 1092 {
4c4b4cd2
PH
1093 i = len0 - 2;
1094 while ((i >= 0 && isdigit (encoded[i]))
dda83cd7
SM
1095 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1096 i -= 1;
4c4b4cd2 1097 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
dda83cd7 1098 len0 = i - 1;
4c4b4cd2 1099 else if (encoded[i] == '$')
dda83cd7 1100 len0 = i;
d2e4a39e 1101 }
14f9c5c9 1102
29480c32
JB
1103 /* The first few characters that are not alphabetic are not part
1104 of any encoding we use, so we can copy them over verbatim. */
1105
4c4b4cd2
PH
1106 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1107 decoded[j] = encoded[i];
14f9c5c9
AS
1108
1109 at_start_name = 1;
1110 while (i < len0)
1111 {
29480c32 1112 /* Is this a symbol function? */
4c4b4cd2 1113 if (at_start_name && encoded[i] == 'O')
dda83cd7
SM
1114 {
1115 int k;
1116
1117 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1118 {
1119 int op_len = strlen (ada_opname_table[k].encoded);
1120 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1121 op_len - 1) == 0)
1122 && !isalnum (encoded[i + op_len]))
1123 {
1124 strcpy (&decoded.front() + j, ada_opname_table[k].decoded);
1125 at_start_name = 0;
1126 i += op_len;
1127 j += strlen (ada_opname_table[k].decoded);
1128 break;
1129 }
1130 }
1131 if (ada_opname_table[k].encoded != NULL)
1132 continue;
1133 }
14f9c5c9
AS
1134 at_start_name = 0;
1135
529cad9c 1136 /* Replace "TK__" with "__", which will eventually be translated
dda83cd7 1137 into "." (just below). */
529cad9c 1138
61012eef 1139 if (i < len0 - 4 && startswith (encoded + i, "TK__"))
dda83cd7 1140 i += 2;
529cad9c 1141
29480c32 1142 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
dda83cd7
SM
1143 be translated into "." (just below). These are internal names
1144 generated for anonymous blocks inside which our symbol is nested. */
29480c32
JB
1145
1146 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
dda83cd7
SM
1147 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1148 && isdigit (encoded [i+4]))
1149 {
1150 int k = i + 5;
1151
1152 while (k < len0 && isdigit (encoded[k]))
1153 k++; /* Skip any extra digit. */
1154
1155 /* Double-check that the "__B_{DIGITS}+" sequence we found
1156 is indeed followed by "__". */
1157 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1158 i = k;
1159 }
29480c32 1160
529cad9c
PH
1161 /* Remove _E{DIGITS}+[sb] */
1162
1163 /* Just as for protected object subprograms, there are 2 categories
dda83cd7
SM
1164 of subprograms created by the compiler for each entry. The first
1165 one implements the actual entry code, and has a suffix following
1166 the convention above; the second one implements the barrier and
1167 uses the same convention as above, except that the 'E' is replaced
1168 by a 'B'.
529cad9c 1169
dda83cd7
SM
1170 Just as above, we do not decode the name of barrier functions
1171 to give the user a clue that the code he is debugging has been
1172 internally generated. */
529cad9c
PH
1173
1174 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
dda83cd7
SM
1175 && isdigit (encoded[i+2]))
1176 {
1177 int k = i + 3;
1178
1179 while (k < len0 && isdigit (encoded[k]))
1180 k++;
1181
1182 if (k < len0
1183 && (encoded[k] == 'b' || encoded[k] == 's'))
1184 {
1185 k++;
1186 /* Just as an extra precaution, make sure that if this
1187 suffix is followed by anything else, it is a '_'.
1188 Otherwise, we matched this sequence by accident. */
1189 if (k == len0
1190 || (k < len0 && encoded[k] == '_'))
1191 i = k;
1192 }
1193 }
529cad9c
PH
1194
1195 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
dda83cd7 1196 the GNAT front-end in protected object subprograms. */
529cad9c
PH
1197
1198 if (i < len0 + 3
dda83cd7
SM
1199 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1200 {
1201 /* Backtrack a bit up until we reach either the begining of
1202 the encoded name, or "__". Make sure that we only find
1203 digits or lowercase characters. */
1204 const char *ptr = encoded + i - 1;
1205
1206 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1207 ptr--;
1208 if (ptr < encoded
1209 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1210 i++;
1211 }
529cad9c 1212
4c4b4cd2 1213 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
dda83cd7
SM
1214 {
1215 /* This is a X[bn]* sequence not separated from the previous
1216 part of the name with a non-alpha-numeric character (in other
1217 words, immediately following an alpha-numeric character), then
1218 verify that it is placed at the end of the encoded name. If
1219 not, then the encoding is not valid and we should abort the
1220 decoding. Otherwise, just skip it, it is used in body-nested
1221 package names. */
1222 do
1223 i += 1;
1224 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1225 if (i < len0)
1226 goto Suppress;
1227 }
cdc7bb92 1228 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
dda83cd7
SM
1229 {
1230 /* Replace '__' by '.'. */
1231 decoded[j] = '.';
1232 at_start_name = 1;
1233 i += 2;
1234 j += 1;
1235 }
14f9c5c9 1236 else
dda83cd7
SM
1237 {
1238 /* It's a character part of the decoded name, so just copy it
1239 over. */
1240 decoded[j] = encoded[i];
1241 i += 1;
1242 j += 1;
1243 }
14f9c5c9 1244 }
f945dedf 1245 decoded.resize (j);
14f9c5c9 1246
29480c32
JB
1247 /* Decoded names should never contain any uppercase character.
1248 Double-check this, and abort the decoding if we find one. */
1249
f945dedf 1250 for (i = 0; i < decoded.length(); ++i)
4c4b4cd2 1251 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1252 goto Suppress;
1253
f945dedf 1254 return decoded;
14f9c5c9
AS
1255
1256Suppress:
4c4b4cd2 1257 if (encoded[0] == '<')
f945dedf 1258 decoded = encoded;
14f9c5c9 1259 else
f945dedf 1260 decoded = '<' + std::string(encoded) + '>';
4c4b4cd2
PH
1261 return decoded;
1262
1263}
1264
1265/* Table for keeping permanent unique copies of decoded names. Once
1266 allocated, names in this table are never released. While this is a
1267 storage leak, it should not be significant unless there are massive
1268 changes in the set of decoded names in successive versions of a
1269 symbol table loaded during a single session. */
1270static struct htab *decoded_names_store;
1271
1272/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1273 in the language-specific part of GSYMBOL, if it has not been
1274 previously computed. Tries to save the decoded name in the same
1275 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1276 in any case, the decoded symbol has a lifetime at least that of
0963b4bd 1277 GSYMBOL).
4c4b4cd2
PH
1278 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1279 const, but nevertheless modified to a semantically equivalent form
0963b4bd 1280 when a decoded name is cached in it. */
4c4b4cd2 1281
45e6c716 1282const char *
f85f34ed 1283ada_decode_symbol (const struct general_symbol_info *arg)
4c4b4cd2 1284{
f85f34ed
TT
1285 struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
1286 const char **resultp =
615b3f62 1287 &gsymbol->language_specific.demangled_name;
5b4ee69b 1288
f85f34ed 1289 if (!gsymbol->ada_mangled)
4c4b4cd2 1290 {
4d4eaa30 1291 std::string decoded = ada_decode (gsymbol->linkage_name ());
f85f34ed 1292 struct obstack *obstack = gsymbol->language_specific.obstack;
5b4ee69b 1293
f85f34ed 1294 gsymbol->ada_mangled = 1;
5b4ee69b 1295
f85f34ed 1296 if (obstack != NULL)
f945dedf 1297 *resultp = obstack_strdup (obstack, decoded.c_str ());
f85f34ed 1298 else
dda83cd7 1299 {
f85f34ed
TT
1300 /* Sometimes, we can't find a corresponding objfile, in
1301 which case, we put the result on the heap. Since we only
1302 decode when needed, we hope this usually does not cause a
1303 significant memory leak (FIXME). */
1304
dda83cd7
SM
1305 char **slot = (char **) htab_find_slot (decoded_names_store,
1306 decoded.c_str (), INSERT);
5b4ee69b 1307
dda83cd7
SM
1308 if (*slot == NULL)
1309 *slot = xstrdup (decoded.c_str ());
1310 *resultp = *slot;
1311 }
4c4b4cd2 1312 }
14f9c5c9 1313
4c4b4cd2
PH
1314 return *resultp;
1315}
76a01679 1316
2c0b251b 1317static char *
76a01679 1318ada_la_decode (const char *encoded, int options)
4c4b4cd2 1319{
f945dedf 1320 return xstrdup (ada_decode (encoded).c_str ());
14f9c5c9
AS
1321}
1322
14f9c5c9 1323\f
d2e4a39e 1324
dda83cd7 1325 /* Arrays */
14f9c5c9 1326
28c85d6c
JB
1327/* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1328 generated by the GNAT compiler to describe the index type used
1329 for each dimension of an array, check whether it follows the latest
1330 known encoding. If not, fix it up to conform to the latest encoding.
1331 Otherwise, do nothing. This function also does nothing if
1332 INDEX_DESC_TYPE is NULL.
1333
85102364 1334 The GNAT encoding used to describe the array index type evolved a bit.
28c85d6c
JB
1335 Initially, the information would be provided through the name of each
1336 field of the structure type only, while the type of these fields was
1337 described as unspecified and irrelevant. The debugger was then expected
1338 to perform a global type lookup using the name of that field in order
1339 to get access to the full index type description. Because these global
1340 lookups can be very expensive, the encoding was later enhanced to make
1341 the global lookup unnecessary by defining the field type as being
1342 the full index type description.
1343
1344 The purpose of this routine is to allow us to support older versions
1345 of the compiler by detecting the use of the older encoding, and by
1346 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1347 we essentially replace each field's meaningless type by the associated
1348 index subtype). */
1349
1350void
1351ada_fixup_array_indexes_type (struct type *index_desc_type)
1352{
1353 int i;
1354
1355 if (index_desc_type == NULL)
1356 return;
1f704f76 1357 gdb_assert (index_desc_type->num_fields () > 0);
28c85d6c
JB
1358
1359 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1360 to check one field only, no need to check them all). If not, return
1361 now.
1362
1363 If our INDEX_DESC_TYPE was generated using the older encoding,
1364 the field type should be a meaningless integer type whose name
1365 is not equal to the field name. */
940da03e
SM
1366 if (index_desc_type->field (0).type ()->name () != NULL
1367 && strcmp (index_desc_type->field (0).type ()->name (),
dda83cd7 1368 TYPE_FIELD_NAME (index_desc_type, 0)) == 0)
28c85d6c
JB
1369 return;
1370
1371 /* Fixup each field of INDEX_DESC_TYPE. */
1f704f76 1372 for (i = 0; i < index_desc_type->num_fields (); i++)
28c85d6c 1373 {
0d5cff50 1374 const char *name = TYPE_FIELD_NAME (index_desc_type, i);
28c85d6c
JB
1375 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1376
1377 if (raw_type)
5d14b6e5 1378 index_desc_type->field (i).set_type (raw_type);
28c85d6c
JB
1379 }
1380}
1381
4c4b4cd2
PH
1382/* The desc_* routines return primitive portions of array descriptors
1383 (fat pointers). */
14f9c5c9
AS
1384
1385/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1386 level of indirection, if needed. */
1387
d2e4a39e
AS
1388static struct type *
1389desc_base_type (struct type *type)
14f9c5c9
AS
1390{
1391 if (type == NULL)
1392 return NULL;
61ee279c 1393 type = ada_check_typedef (type);
78134374 1394 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1395 type = ada_typedef_target_type (type);
1396
1265e4aa 1397 if (type != NULL
78134374 1398 && (type->code () == TYPE_CODE_PTR
dda83cd7 1399 || type->code () == TYPE_CODE_REF))
61ee279c 1400 return ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9
AS
1401 else
1402 return type;
1403}
1404
4c4b4cd2
PH
1405/* True iff TYPE indicates a "thin" array pointer type. */
1406
14f9c5c9 1407static int
d2e4a39e 1408is_thin_pntr (struct type *type)
14f9c5c9 1409{
d2e4a39e 1410 return
14f9c5c9
AS
1411 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1412 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1413}
1414
4c4b4cd2
PH
1415/* The descriptor type for thin pointer type TYPE. */
1416
d2e4a39e
AS
1417static struct type *
1418thin_descriptor_type (struct type *type)
14f9c5c9 1419{
d2e4a39e 1420 struct type *base_type = desc_base_type (type);
5b4ee69b 1421
14f9c5c9
AS
1422 if (base_type == NULL)
1423 return NULL;
1424 if (is_suffix (ada_type_name (base_type), "___XVE"))
1425 return base_type;
d2e4a39e 1426 else
14f9c5c9 1427 {
d2e4a39e 1428 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
5b4ee69b 1429
14f9c5c9 1430 if (alt_type == NULL)
dda83cd7 1431 return base_type;
14f9c5c9 1432 else
dda83cd7 1433 return alt_type;
14f9c5c9
AS
1434 }
1435}
1436
4c4b4cd2
PH
1437/* A pointer to the array data for thin-pointer value VAL. */
1438
d2e4a39e
AS
1439static struct value *
1440thin_data_pntr (struct value *val)
14f9c5c9 1441{
828292f2 1442 struct type *type = ada_check_typedef (value_type (val));
556bdfd4 1443 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
5b4ee69b 1444
556bdfd4
UW
1445 data_type = lookup_pointer_type (data_type);
1446
78134374 1447 if (type->code () == TYPE_CODE_PTR)
556bdfd4 1448 return value_cast (data_type, value_copy (val));
d2e4a39e 1449 else
42ae5230 1450 return value_from_longest (data_type, value_address (val));
14f9c5c9
AS
1451}
1452
4c4b4cd2
PH
1453/* True iff TYPE indicates a "thick" array pointer type. */
1454
14f9c5c9 1455static int
d2e4a39e 1456is_thick_pntr (struct type *type)
14f9c5c9
AS
1457{
1458 type = desc_base_type (type);
78134374 1459 return (type != NULL && type->code () == TYPE_CODE_STRUCT
dda83cd7 1460 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1461}
1462
4c4b4cd2
PH
1463/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1464 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1465
d2e4a39e
AS
1466static struct type *
1467desc_bounds_type (struct type *type)
14f9c5c9 1468{
d2e4a39e 1469 struct type *r;
14f9c5c9
AS
1470
1471 type = desc_base_type (type);
1472
1473 if (type == NULL)
1474 return NULL;
1475 else if (is_thin_pntr (type))
1476 {
1477 type = thin_descriptor_type (type);
1478 if (type == NULL)
dda83cd7 1479 return NULL;
14f9c5c9
AS
1480 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1481 if (r != NULL)
dda83cd7 1482 return ada_check_typedef (r);
14f9c5c9 1483 }
78134374 1484 else if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
1485 {
1486 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1487 if (r != NULL)
dda83cd7 1488 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
14f9c5c9
AS
1489 }
1490 return NULL;
1491}
1492
1493/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1494 one, a pointer to its bounds data. Otherwise NULL. */
1495
d2e4a39e
AS
1496static struct value *
1497desc_bounds (struct value *arr)
14f9c5c9 1498{
df407dfe 1499 struct type *type = ada_check_typedef (value_type (arr));
5b4ee69b 1500
d2e4a39e 1501 if (is_thin_pntr (type))
14f9c5c9 1502 {
d2e4a39e 1503 struct type *bounds_type =
dda83cd7 1504 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1505 LONGEST addr;
1506
4cdfadb1 1507 if (bounds_type == NULL)
dda83cd7 1508 error (_("Bad GNAT array descriptor"));
14f9c5c9
AS
1509
1510 /* NOTE: The following calculation is not really kosher, but
dda83cd7
SM
1511 since desc_type is an XVE-encoded type (and shouldn't be),
1512 the correct calculation is a real pain. FIXME (and fix GCC). */
78134374 1513 if (type->code () == TYPE_CODE_PTR)
dda83cd7 1514 addr = value_as_long (arr);
d2e4a39e 1515 else
dda83cd7 1516 addr = value_address (arr);
14f9c5c9 1517
d2e4a39e 1518 return
dda83cd7
SM
1519 value_from_longest (lookup_pointer_type (bounds_type),
1520 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1521 }
1522
1523 else if (is_thick_pntr (type))
05e522ef
JB
1524 {
1525 struct value *p_bounds = value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
1526 _("Bad GNAT array descriptor"));
1527 struct type *p_bounds_type = value_type (p_bounds);
1528
1529 if (p_bounds_type
78134374 1530 && p_bounds_type->code () == TYPE_CODE_PTR)
05e522ef
JB
1531 {
1532 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1533
e46d3488 1534 if (target_type->is_stub ())
05e522ef
JB
1535 p_bounds = value_cast (lookup_pointer_type
1536 (ada_check_typedef (target_type)),
1537 p_bounds);
1538 }
1539 else
1540 error (_("Bad GNAT array descriptor"));
1541
1542 return p_bounds;
1543 }
14f9c5c9
AS
1544 else
1545 return NULL;
1546}
1547
4c4b4cd2
PH
1548/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1549 position of the field containing the address of the bounds data. */
1550
14f9c5c9 1551static int
d2e4a39e 1552fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1553{
1554 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1555}
1556
1557/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1558 size of the field containing the address of the bounds data. */
1559
14f9c5c9 1560static int
d2e4a39e 1561fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1562{
1563 type = desc_base_type (type);
1564
d2e4a39e 1565 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1566 return TYPE_FIELD_BITSIZE (type, 1);
1567 else
940da03e 1568 return 8 * TYPE_LENGTH (ada_check_typedef (type->field (1).type ()));
14f9c5c9
AS
1569}
1570
4c4b4cd2 1571/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
556bdfd4
UW
1572 pointer to one, the type of its array data (a array-with-no-bounds type);
1573 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1574 data. */
4c4b4cd2 1575
d2e4a39e 1576static struct type *
556bdfd4 1577desc_data_target_type (struct type *type)
14f9c5c9
AS
1578{
1579 type = desc_base_type (type);
1580
4c4b4cd2 1581 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1582 if (is_thin_pntr (type))
940da03e 1583 return desc_base_type (thin_descriptor_type (type)->field (1).type ());
14f9c5c9 1584 else if (is_thick_pntr (type))
556bdfd4
UW
1585 {
1586 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1587
1588 if (data_type
78134374 1589 && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
05e522ef 1590 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
556bdfd4
UW
1591 }
1592
1593 return NULL;
14f9c5c9
AS
1594}
1595
1596/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1597 its array data. */
4c4b4cd2 1598
d2e4a39e
AS
1599static struct value *
1600desc_data (struct value *arr)
14f9c5c9 1601{
df407dfe 1602 struct type *type = value_type (arr);
5b4ee69b 1603
14f9c5c9
AS
1604 if (is_thin_pntr (type))
1605 return thin_data_pntr (arr);
1606 else if (is_thick_pntr (type))
d2e4a39e 1607 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
dda83cd7 1608 _("Bad GNAT array descriptor"));
14f9c5c9
AS
1609 else
1610 return NULL;
1611}
1612
1613
1614/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1615 position of the field containing the address of the data. */
1616
14f9c5c9 1617static int
d2e4a39e 1618fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1619{
1620 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1621}
1622
1623/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1624 size of the field containing the address of the data. */
1625
14f9c5c9 1626static int
d2e4a39e 1627fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1628{
1629 type = desc_base_type (type);
1630
1631 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1632 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1633 else
940da03e 1634 return TARGET_CHAR_BIT * TYPE_LENGTH (type->field (0).type ());
14f9c5c9
AS
1635}
1636
4c4b4cd2 1637/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1638 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
d2e4a39e
AS
1641static struct value *
1642desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1643{
250106a7
TT
1644 char bound_name[20];
1645 xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
1646 which ? 'U' : 'L', i - 1);
1647 return value_struct_elt (&bounds, NULL, bound_name, NULL,
dda83cd7 1648 _("Bad GNAT array descriptor bounds"));
14f9c5c9
AS
1649}
1650
1651/* If BOUNDS is an array-bounds structure type, return the bit position
1652 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1653 bound, if WHICH is 1. The first bound is I=1. */
1654
14f9c5c9 1655static int
d2e4a39e 1656desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1657{
d2e4a39e 1658 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1659}
1660
1661/* If BOUNDS is an array-bounds structure type, return the bit field size
1662 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1663 bound, if WHICH is 1. The first bound is I=1. */
1664
76a01679 1665static int
d2e4a39e 1666desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1667{
1668 type = desc_base_type (type);
1669
d2e4a39e
AS
1670 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1671 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1672 else
940da03e 1673 return 8 * TYPE_LENGTH (type->field (2 * i + which - 2).type ());
14f9c5c9
AS
1674}
1675
1676/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1677 Ith bound (numbering from 1). Otherwise, NULL. */
1678
d2e4a39e
AS
1679static struct type *
1680desc_index_type (struct type *type, int i)
14f9c5c9
AS
1681{
1682 type = desc_base_type (type);
1683
78134374 1684 if (type->code () == TYPE_CODE_STRUCT)
250106a7
TT
1685 {
1686 char bound_name[20];
1687 xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
1688 return lookup_struct_elt_type (type, bound_name, 1);
1689 }
d2e4a39e 1690 else
14f9c5c9
AS
1691 return NULL;
1692}
1693
4c4b4cd2
PH
1694/* The number of index positions in the array-bounds type TYPE.
1695 Return 0 if TYPE is NULL. */
1696
14f9c5c9 1697static int
d2e4a39e 1698desc_arity (struct type *type)
14f9c5c9
AS
1699{
1700 type = desc_base_type (type);
1701
1702 if (type != NULL)
1f704f76 1703 return type->num_fields () / 2;
14f9c5c9
AS
1704 return 0;
1705}
1706
4c4b4cd2
PH
1707/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1708 an array descriptor type (representing an unconstrained array
1709 type). */
1710
76a01679
JB
1711static int
1712ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1713{
1714 if (type == NULL)
1715 return 0;
61ee279c 1716 type = ada_check_typedef (type);
78134374 1717 return (type->code () == TYPE_CODE_ARRAY
dda83cd7 1718 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1719}
1720
52ce6436 1721/* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
0963b4bd 1722 * to one. */
52ce6436 1723
2c0b251b 1724static int
52ce6436
PH
1725ada_is_array_type (struct type *type)
1726{
78134374
SM
1727 while (type != NULL
1728 && (type->code () == TYPE_CODE_PTR
1729 || type->code () == TYPE_CODE_REF))
52ce6436
PH
1730 type = TYPE_TARGET_TYPE (type);
1731 return ada_is_direct_array_type (type);
1732}
1733
4c4b4cd2 1734/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1735
14f9c5c9 1736int
4c4b4cd2 1737ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1738{
1739 if (type == NULL)
1740 return 0;
61ee279c 1741 type = ada_check_typedef (type);
78134374
SM
1742 return (type->code () == TYPE_CODE_ARRAY
1743 || (type->code () == TYPE_CODE_PTR
1744 && (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ()
1745 == TYPE_CODE_ARRAY)));
14f9c5c9
AS
1746}
1747
4c4b4cd2
PH
1748/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1749
14f9c5c9 1750int
4c4b4cd2 1751ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1752{
556bdfd4 1753 struct type *data_type = desc_data_target_type (type);
14f9c5c9
AS
1754
1755 if (type == NULL)
1756 return 0;
61ee279c 1757 type = ada_check_typedef (type);
556bdfd4 1758 return (data_type != NULL
78134374 1759 && data_type->code () == TYPE_CODE_ARRAY
556bdfd4 1760 && desc_arity (desc_bounds_type (type)) > 0);
14f9c5c9
AS
1761}
1762
1763/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1764 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1765 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1766 is still needed. */
1767
14f9c5c9 1768int
ebf56fd3 1769ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1770{
d2e4a39e 1771 return
14f9c5c9 1772 type != NULL
78134374 1773 && type->code () == TYPE_CODE_STRUCT
14f9c5c9 1774 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
dda83cd7 1775 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
4c4b4cd2 1776 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1777}
1778
1779
4c4b4cd2 1780/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1781 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1782 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1783 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1784 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1785 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1786 a descriptor. */
de93309a
SM
1787
1788static struct type *
d2e4a39e 1789ada_type_of_array (struct value *arr, int bounds)
14f9c5c9 1790{
ad82864c
JB
1791 if (ada_is_constrained_packed_array_type (value_type (arr)))
1792 return decode_constrained_packed_array_type (value_type (arr));
14f9c5c9 1793
df407dfe
AC
1794 if (!ada_is_array_descriptor_type (value_type (arr)))
1795 return value_type (arr);
d2e4a39e
AS
1796
1797 if (!bounds)
ad82864c
JB
1798 {
1799 struct type *array_type =
1800 ada_check_typedef (desc_data_target_type (value_type (arr)));
1801
1802 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1803 TYPE_FIELD_BITSIZE (array_type, 0) =
1804 decode_packed_array_bitsize (value_type (arr));
1805
1806 return array_type;
1807 }
14f9c5c9
AS
1808 else
1809 {
d2e4a39e 1810 struct type *elt_type;
14f9c5c9 1811 int arity;
d2e4a39e 1812 struct value *descriptor;
14f9c5c9 1813
df407dfe
AC
1814 elt_type = ada_array_element_type (value_type (arr), -1);
1815 arity = ada_array_arity (value_type (arr));
14f9c5c9 1816
d2e4a39e 1817 if (elt_type == NULL || arity == 0)
dda83cd7 1818 return ada_check_typedef (value_type (arr));
14f9c5c9
AS
1819
1820 descriptor = desc_bounds (arr);
d2e4a39e 1821 if (value_as_long (descriptor) == 0)
dda83cd7 1822 return NULL;
d2e4a39e 1823 while (arity > 0)
dda83cd7
SM
1824 {
1825 struct type *range_type = alloc_type_copy (value_type (arr));
1826 struct type *array_type = alloc_type_copy (value_type (arr));
1827 struct value *low = desc_one_bound (descriptor, arity, 0);
1828 struct value *high = desc_one_bound (descriptor, arity, 1);
1829
1830 arity -= 1;
1831 create_static_range_type (range_type, value_type (low),
0c9c3474
SA
1832 longest_to_int (value_as_long (low)),
1833 longest_to_int (value_as_long (high)));
dda83cd7 1834 elt_type = create_array_type (array_type, elt_type, range_type);
ad82864c
JB
1835
1836 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
e67ad678
JB
1837 {
1838 /* We need to store the element packed bitsize, as well as
dda83cd7 1839 recompute the array size, because it was previously
e67ad678
JB
1840 computed based on the unpacked element size. */
1841 LONGEST lo = value_as_long (low);
1842 LONGEST hi = value_as_long (high);
1843
1844 TYPE_FIELD_BITSIZE (elt_type, 0) =
1845 decode_packed_array_bitsize (value_type (arr));
1846 /* If the array has no element, then the size is already
dda83cd7 1847 zero, and does not need to be recomputed. */
e67ad678
JB
1848 if (lo < hi)
1849 {
1850 int array_bitsize =
dda83cd7 1851 (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
e67ad678
JB
1852
1853 TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
1854 }
1855 }
dda83cd7 1856 }
14f9c5c9
AS
1857
1858 return lookup_pointer_type (elt_type);
1859 }
1860}
1861
1862/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
1863 Otherwise, returns either a standard GDB array with bounds set
1864 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1865 GDB array. Returns NULL if ARR is a null fat pointer. */
1866
d2e4a39e
AS
1867struct value *
1868ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 1869{
df407dfe 1870 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1871 {
d2e4a39e 1872 struct type *arrType = ada_type_of_array (arr, 1);
5b4ee69b 1873
14f9c5c9 1874 if (arrType == NULL)
dda83cd7 1875 return NULL;
14f9c5c9
AS
1876 return value_cast (arrType, value_copy (desc_data (arr)));
1877 }
ad82864c
JB
1878 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1879 return decode_constrained_packed_array (arr);
14f9c5c9
AS
1880 else
1881 return arr;
1882}
1883
1884/* If ARR does not represent an array, returns ARR unchanged.
1885 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
1886 be ARR itself if it already is in the proper form). */
1887
720d1a40 1888struct value *
d2e4a39e 1889ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 1890{
df407dfe 1891 if (ada_is_array_descriptor_type (value_type (arr)))
14f9c5c9 1892 {
d2e4a39e 1893 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
5b4ee69b 1894
14f9c5c9 1895 if (arrVal == NULL)
dda83cd7 1896 error (_("Bounds unavailable for null array pointer."));
c1b5a1a6 1897 ada_ensure_varsize_limit (TYPE_TARGET_TYPE (value_type (arrVal)));
14f9c5c9
AS
1898 return value_ind (arrVal);
1899 }
ad82864c
JB
1900 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1901 return decode_constrained_packed_array (arr);
d2e4a39e 1902 else
14f9c5c9
AS
1903 return arr;
1904}
1905
1906/* If TYPE represents a GNAT array type, return it translated to an
1907 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
1908 packing). For other types, is the identity. */
1909
d2e4a39e
AS
1910struct type *
1911ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1912{
ad82864c
JB
1913 if (ada_is_constrained_packed_array_type (type))
1914 return decode_constrained_packed_array_type (type);
17280b9f
UW
1915
1916 if (ada_is_array_descriptor_type (type))
556bdfd4 1917 return ada_check_typedef (desc_data_target_type (type));
17280b9f
UW
1918
1919 return type;
14f9c5c9
AS
1920}
1921
4c4b4cd2
PH
1922/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1923
ad82864c 1924static int
57567375 1925ada_is_gnat_encoded_packed_array_type (struct type *type)
14f9c5c9
AS
1926{
1927 if (type == NULL)
1928 return 0;
4c4b4cd2 1929 type = desc_base_type (type);
61ee279c 1930 type = ada_check_typedef (type);
d2e4a39e 1931 return
14f9c5c9
AS
1932 ada_type_name (type) != NULL
1933 && strstr (ada_type_name (type), "___XP") != NULL;
1934}
1935
ad82864c
JB
1936/* Non-zero iff TYPE represents a standard GNAT constrained
1937 packed-array type. */
1938
1939int
1940ada_is_constrained_packed_array_type (struct type *type)
1941{
57567375 1942 return ada_is_gnat_encoded_packed_array_type (type)
ad82864c
JB
1943 && !ada_is_array_descriptor_type (type);
1944}
1945
1946/* Non-zero iff TYPE represents an array descriptor for a
1947 unconstrained packed-array type. */
1948
1949static int
1950ada_is_unconstrained_packed_array_type (struct type *type)
1951{
57567375
TT
1952 if (!ada_is_array_descriptor_type (type))
1953 return 0;
1954
1955 if (ada_is_gnat_encoded_packed_array_type (type))
1956 return 1;
1957
1958 /* If we saw GNAT encodings, then the above code is sufficient.
1959 However, with minimal encodings, we will just have a thick
1960 pointer instead. */
1961 if (is_thick_pntr (type))
1962 {
1963 type = desc_base_type (type);
1964 /* The structure's first field is a pointer to an array, so this
1965 fetches the array type. */
1966 type = TYPE_TARGET_TYPE (type->field (0).type ());
1967 /* Now we can see if the array elements are packed. */
1968 return TYPE_FIELD_BITSIZE (type, 0) > 0;
1969 }
1970
1971 return 0;
ad82864c
JB
1972}
1973
c9a28cbe
TT
1974/* Return true if TYPE is a (Gnat-encoded) constrained packed array
1975 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
1976
1977static bool
1978ada_is_any_packed_array_type (struct type *type)
1979{
1980 return (ada_is_constrained_packed_array_type (type)
1981 || (type->code () == TYPE_CODE_ARRAY
1982 && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
1983}
1984
ad82864c
JB
1985/* Given that TYPE encodes a packed array type (constrained or unconstrained),
1986 return the size of its elements in bits. */
1987
1988static long
1989decode_packed_array_bitsize (struct type *type)
1990{
0d5cff50
DE
1991 const char *raw_name;
1992 const char *tail;
ad82864c
JB
1993 long bits;
1994
720d1a40
JB
1995 /* Access to arrays implemented as fat pointers are encoded as a typedef
1996 of the fat pointer type. We need the name of the fat pointer type
1997 to do the decoding, so strip the typedef layer. */
78134374 1998 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
1999 type = ada_typedef_target_type (type);
2000
2001 raw_name = ada_type_name (ada_check_typedef (type));
ad82864c
JB
2002 if (!raw_name)
2003 raw_name = ada_type_name (desc_base_type (type));
2004
2005 if (!raw_name)
2006 return 0;
2007
2008 tail = strstr (raw_name, "___XP");
57567375
TT
2009 if (tail == nullptr)
2010 {
2011 gdb_assert (is_thick_pntr (type));
2012 /* The structure's first field is a pointer to an array, so this
2013 fetches the array type. */
2014 type = TYPE_TARGET_TYPE (type->field (0).type ());
2015 /* Now we can see if the array elements are packed. */
2016 return TYPE_FIELD_BITSIZE (type, 0);
2017 }
ad82864c
JB
2018
2019 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
2020 {
2021 lim_warning
2022 (_("could not understand bit size information on packed array"));
2023 return 0;
2024 }
2025
2026 return bits;
2027}
2028
14f9c5c9
AS
2029/* Given that TYPE is a standard GDB array type with all bounds filled
2030 in, and that the element size of its ultimate scalar constituents
2031 (that is, either its elements, or, if it is an array of arrays, its
2032 elements' elements, etc.) is *ELT_BITS, return an identical type,
2033 but with the bit sizes of its elements (and those of any
2034 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2 2035 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
4a46959e
JB
2036 in bits.
2037
2038 Note that, for arrays whose index type has an XA encoding where
2039 a bound references a record discriminant, getting that discriminant,
2040 and therefore the actual value of that bound, is not possible
2041 because none of the given parameters gives us access to the record.
2042 This function assumes that it is OK in the context where it is being
2043 used to return an array whose bounds are still dynamic and where
2044 the length is arbitrary. */
4c4b4cd2 2045
d2e4a39e 2046static struct type *
ad82864c 2047constrained_packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 2048{
d2e4a39e
AS
2049 struct type *new_elt_type;
2050 struct type *new_type;
99b1c762
JB
2051 struct type *index_type_desc;
2052 struct type *index_type;
14f9c5c9
AS
2053 LONGEST low_bound, high_bound;
2054
61ee279c 2055 type = ada_check_typedef (type);
78134374 2056 if (type->code () != TYPE_CODE_ARRAY)
14f9c5c9
AS
2057 return type;
2058
99b1c762
JB
2059 index_type_desc = ada_find_parallel_type (type, "___XA");
2060 if (index_type_desc)
940da03e 2061 index_type = to_fixed_range_type (index_type_desc->field (0).type (),
99b1c762
JB
2062 NULL);
2063 else
3d967001 2064 index_type = type->index_type ();
99b1c762 2065
e9bb382b 2066 new_type = alloc_type_copy (type);
ad82864c
JB
2067 new_elt_type =
2068 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
2069 elt_bits);
99b1c762 2070 create_array_type (new_type, new_elt_type, index_type);
14f9c5c9 2071 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
d0e39ea2 2072 new_type->set_name (ada_type_name (type));
14f9c5c9 2073
78134374 2074 if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
4a46959e 2075 && is_dynamic_type (check_typedef (index_type)))
1f8d2881 2076 || !get_discrete_bounds (index_type, &low_bound, &high_bound))
14f9c5c9
AS
2077 low_bound = high_bound = 0;
2078 if (high_bound < low_bound)
2079 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 2080 else
14f9c5c9
AS
2081 {
2082 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 2083 TYPE_LENGTH (new_type) =
dda83cd7 2084 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
2085 }
2086
9cdd0d12 2087 new_type->set_is_fixed_instance (true);
14f9c5c9
AS
2088 return new_type;
2089}
2090
ad82864c
JB
2091/* The array type encoded by TYPE, where
2092 ada_is_constrained_packed_array_type (TYPE). */
4c4b4cd2 2093
d2e4a39e 2094static struct type *
ad82864c 2095decode_constrained_packed_array_type (struct type *type)
d2e4a39e 2096{
0d5cff50 2097 const char *raw_name = ada_type_name (ada_check_typedef (type));
727e3d2e 2098 char *name;
0d5cff50 2099 const char *tail;
d2e4a39e 2100 struct type *shadow_type;
14f9c5c9 2101 long bits;
14f9c5c9 2102
727e3d2e
JB
2103 if (!raw_name)
2104 raw_name = ada_type_name (desc_base_type (type));
2105
2106 if (!raw_name)
2107 return NULL;
2108
2109 name = (char *) alloca (strlen (raw_name) + 1);
2110 tail = strstr (raw_name, "___XP");
4c4b4cd2
PH
2111 type = desc_base_type (type);
2112
14f9c5c9
AS
2113 memcpy (name, raw_name, tail - raw_name);
2114 name[tail - raw_name] = '\000';
2115
b4ba55a1
JB
2116 shadow_type = ada_find_parallel_type_with_name (type, name);
2117
2118 if (shadow_type == NULL)
14f9c5c9 2119 {
323e0a4a 2120 lim_warning (_("could not find bounds information on packed array"));
14f9c5c9
AS
2121 return NULL;
2122 }
f168693b 2123 shadow_type = check_typedef (shadow_type);
14f9c5c9 2124
78134374 2125 if (shadow_type->code () != TYPE_CODE_ARRAY)
14f9c5c9 2126 {
0963b4bd
MS
2127 lim_warning (_("could not understand bounds "
2128 "information on packed array"));
14f9c5c9
AS
2129 return NULL;
2130 }
d2e4a39e 2131
ad82864c
JB
2132 bits = decode_packed_array_bitsize (type);
2133 return constrained_packed_array_type (shadow_type, &bits);
14f9c5c9
AS
2134}
2135
a7400e44
TT
2136/* Helper function for decode_constrained_packed_array. Set the field
2137 bitsize on a series of packed arrays. Returns the number of
2138 elements in TYPE. */
2139
2140static LONGEST
2141recursively_update_array_bitsize (struct type *type)
2142{
2143 gdb_assert (type->code () == TYPE_CODE_ARRAY);
2144
2145 LONGEST low, high;
1f8d2881 2146 if (!get_discrete_bounds (type->index_type (), &low, &high)
a7400e44
TT
2147 || low > high)
2148 return 0;
2149 LONGEST our_len = high - low + 1;
2150
2151 struct type *elt_type = TYPE_TARGET_TYPE (type);
2152 if (elt_type->code () == TYPE_CODE_ARRAY)
2153 {
2154 LONGEST elt_len = recursively_update_array_bitsize (elt_type);
2155 LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
2156 TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
2157
2158 TYPE_LENGTH (type) = ((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
2159 / HOST_CHAR_BIT);
2160 }
2161
2162 return our_len;
2163}
2164
ad82864c
JB
2165/* Given that ARR is a struct value *indicating a GNAT constrained packed
2166 array, returns a simple array that denotes that array. Its type is a
14f9c5c9
AS
2167 standard GDB array type except that the BITSIZEs of the array
2168 target types are set to the number of bits in each element, and the
4c4b4cd2 2169 type length is set appropriately. */
14f9c5c9 2170
d2e4a39e 2171static struct value *
ad82864c 2172decode_constrained_packed_array (struct value *arr)
14f9c5c9 2173{
4c4b4cd2 2174 struct type *type;
14f9c5c9 2175
11aa919a
PMR
2176 /* If our value is a pointer, then dereference it. Likewise if
2177 the value is a reference. Make sure that this operation does not
2178 cause the target type to be fixed, as this would indirectly cause
2179 this array to be decoded. The rest of the routine assumes that
2180 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2181 and "value_ind" routines to perform the dereferencing, as opposed
2182 to using "ada_coerce_ref" or "ada_value_ind". */
2183 arr = coerce_ref (arr);
78134374 2184 if (ada_check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
284614f0 2185 arr = value_ind (arr);
4c4b4cd2 2186
ad82864c 2187 type = decode_constrained_packed_array_type (value_type (arr));
14f9c5c9
AS
2188 if (type == NULL)
2189 {
323e0a4a 2190 error (_("can't unpack array"));
14f9c5c9
AS
2191 return NULL;
2192 }
61ee279c 2193
a7400e44
TT
2194 /* Decoding the packed array type could not correctly set the field
2195 bitsizes for any dimension except the innermost, because the
2196 bounds may be variable and were not passed to that function. So,
2197 we further resolve the array bounds here and then update the
2198 sizes. */
2199 const gdb_byte *valaddr = value_contents_for_printing (arr);
2200 CORE_ADDR address = value_address (arr);
2201 gdb::array_view<const gdb_byte> view
2202 = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
2203 type = resolve_dynamic_type (type, view, address);
2204 recursively_update_array_bitsize (type);
2205
d5a22e77 2206 if (type_byte_order (value_type (arr)) == BFD_ENDIAN_BIG
32c9a795 2207 && ada_is_modular_type (value_type (arr)))
61ee279c
PH
2208 {
2209 /* This is a (right-justified) modular type representing a packed
2210 array with no wrapper. In order to interpret the value through
2211 the (left-justified) packed array type we just built, we must
2212 first left-justify it. */
2213 int bit_size, bit_pos;
2214 ULONGEST mod;
2215
df407dfe 2216 mod = ada_modulus (value_type (arr)) - 1;
61ee279c
PH
2217 bit_size = 0;
2218 while (mod > 0)
2219 {
2220 bit_size += 1;
2221 mod >>= 1;
2222 }
df407dfe 2223 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
61ee279c
PH
2224 arr = ada_value_primitive_packed_val (arr, NULL,
2225 bit_pos / HOST_CHAR_BIT,
2226 bit_pos % HOST_CHAR_BIT,
2227 bit_size,
2228 type);
2229 }
2230
4c4b4cd2 2231 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
2232}
2233
2234
2235/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 2236 given in IND. ARR must be a simple array. */
14f9c5c9 2237
d2e4a39e
AS
2238static struct value *
2239value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2240{
2241 int i;
2242 int bits, elt_off, bit_off;
2243 long elt_total_bit_offset;
d2e4a39e
AS
2244 struct type *elt_type;
2245 struct value *v;
14f9c5c9
AS
2246
2247 bits = 0;
2248 elt_total_bit_offset = 0;
df407dfe 2249 elt_type = ada_check_typedef (value_type (arr));
d2e4a39e 2250 for (i = 0; i < arity; i += 1)
14f9c5c9 2251 {
78134374 2252 if (elt_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
2253 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2254 error
2255 (_("attempt to do packed indexing of "
0963b4bd 2256 "something other than a packed array"));
14f9c5c9 2257 else
dda83cd7
SM
2258 {
2259 struct type *range_type = elt_type->index_type ();
2260 LONGEST lowerbound, upperbound;
2261 LONGEST idx;
2262
1f8d2881 2263 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
dda83cd7
SM
2264 {
2265 lim_warning (_("don't know bounds of array"));
2266 lowerbound = upperbound = 0;
2267 }
2268
2269 idx = pos_atr (ind[i]);
2270 if (idx < lowerbound || idx > upperbound)
2271 lim_warning (_("packed array index %ld out of bounds"),
0963b4bd 2272 (long) idx);
dda83cd7
SM
2273 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2274 elt_total_bit_offset += (idx - lowerbound) * bits;
2275 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2276 }
14f9c5c9
AS
2277 }
2278 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2279 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
2280
2281 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
dda83cd7 2282 bits, elt_type);
14f9c5c9
AS
2283 return v;
2284}
2285
4c4b4cd2 2286/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
2287
2288static int
d2e4a39e 2289has_negatives (struct type *type)
14f9c5c9 2290{
78134374 2291 switch (type->code ())
d2e4a39e
AS
2292 {
2293 default:
2294 return 0;
2295 case TYPE_CODE_INT:
c6d940a9 2296 return !type->is_unsigned ();
d2e4a39e 2297 case TYPE_CODE_RANGE:
5537ddd0 2298 return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
d2e4a39e 2299 }
14f9c5c9 2300}
d2e4a39e 2301
f93fca70 2302/* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
5b639dea 2303 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
f93fca70 2304 the unpacked buffer.
14f9c5c9 2305
5b639dea
JB
2306 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2307 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2308
f93fca70
JB
2309 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2310 zero otherwise.
14f9c5c9 2311
f93fca70 2312 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
a1c95e6b 2313
f93fca70
JB
2314 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2315
2316static void
2317ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
2318 gdb_byte *unpacked, int unpacked_len,
2319 int is_big_endian, int is_signed_type,
2320 int is_scalar)
2321{
a1c95e6b
JB
2322 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2323 int src_idx; /* Index into the source area */
2324 int src_bytes_left; /* Number of source bytes left to process. */
2325 int srcBitsLeft; /* Number of source bits left to move */
2326 int unusedLS; /* Number of bits in next significant
dda83cd7 2327 byte of source that are unused */
a1c95e6b 2328
a1c95e6b
JB
2329 int unpacked_idx; /* Index into the unpacked buffer */
2330 int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
2331
4c4b4cd2 2332 unsigned long accum; /* Staging area for bits being transferred */
a1c95e6b 2333 int accumSize; /* Number of meaningful bits in accum */
14f9c5c9 2334 unsigned char sign;
a1c95e6b 2335
4c4b4cd2
PH
2336 /* Transmit bytes from least to most significant; delta is the direction
2337 the indices move. */
f93fca70 2338 int delta = is_big_endian ? -1 : 1;
14f9c5c9 2339
5b639dea
JB
2340 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2341 bits from SRC. .*/
2342 if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
2343 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2344 bit_size, unpacked_len);
2345
14f9c5c9 2346 srcBitsLeft = bit_size;
086ca51f 2347 src_bytes_left = src_len;
f93fca70 2348 unpacked_bytes_left = unpacked_len;
14f9c5c9 2349 sign = 0;
f93fca70
JB
2350
2351 if (is_big_endian)
14f9c5c9 2352 {
086ca51f 2353 src_idx = src_len - 1;
f93fca70
JB
2354 if (is_signed_type
2355 && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
dda83cd7 2356 sign = ~0;
d2e4a39e
AS
2357
2358 unusedLS =
dda83cd7
SM
2359 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2360 % HOST_CHAR_BIT;
14f9c5c9 2361
f93fca70
JB
2362 if (is_scalar)
2363 {
dda83cd7
SM
2364 accumSize = 0;
2365 unpacked_idx = unpacked_len - 1;
f93fca70
JB
2366 }
2367 else
2368 {
dda83cd7
SM
2369 /* Non-scalar values must be aligned at a byte boundary... */
2370 accumSize =
2371 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2372 /* ... And are placed at the beginning (most-significant) bytes
2373 of the target. */
2374 unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2375 unpacked_bytes_left = unpacked_idx + 1;
f93fca70 2376 }
14f9c5c9 2377 }
d2e4a39e 2378 else
14f9c5c9
AS
2379 {
2380 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2381
086ca51f 2382 src_idx = unpacked_idx = 0;
14f9c5c9
AS
2383 unusedLS = bit_offset;
2384 accumSize = 0;
2385
f93fca70 2386 if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
dda83cd7 2387 sign = ~0;
14f9c5c9 2388 }
d2e4a39e 2389
14f9c5c9 2390 accum = 0;
086ca51f 2391 while (src_bytes_left > 0)
14f9c5c9
AS
2392 {
2393 /* Mask for removing bits of the next source byte that are not
dda83cd7 2394 part of the value. */
d2e4a39e 2395 unsigned int unusedMSMask =
dda83cd7
SM
2396 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2397 1;
4c4b4cd2 2398 /* Sign-extend bits for this byte. */
14f9c5c9 2399 unsigned int signMask = sign & ~unusedMSMask;
5b4ee69b 2400
d2e4a39e 2401 accum |=
dda83cd7 2402 (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 2403 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 2404 if (accumSize >= HOST_CHAR_BIT)
dda83cd7
SM
2405 {
2406 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
2407 accumSize -= HOST_CHAR_BIT;
2408 accum >>= HOST_CHAR_BIT;
2409 unpacked_bytes_left -= 1;
2410 unpacked_idx += delta;
2411 }
14f9c5c9
AS
2412 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2413 unusedLS = 0;
086ca51f
JB
2414 src_bytes_left -= 1;
2415 src_idx += delta;
14f9c5c9 2416 }
086ca51f 2417 while (unpacked_bytes_left > 0)
14f9c5c9
AS
2418 {
2419 accum |= sign << accumSize;
db297a65 2420 unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
14f9c5c9 2421 accumSize -= HOST_CHAR_BIT;
9cd4d857
JB
2422 if (accumSize < 0)
2423 accumSize = 0;
14f9c5c9 2424 accum >>= HOST_CHAR_BIT;
086ca51f
JB
2425 unpacked_bytes_left -= 1;
2426 unpacked_idx += delta;
14f9c5c9 2427 }
f93fca70
JB
2428}
2429
2430/* Create a new value of type TYPE from the contents of OBJ starting
2431 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2432 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2433 assigning through the result will set the field fetched from.
2434 VALADDR is ignored unless OBJ is NULL, in which case,
2435 VALADDR+OFFSET must address the start of storage containing the
2436 packed value. The value returned in this case is never an lval.
2437 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2438
2439struct value *
2440ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2441 long offset, int bit_offset, int bit_size,
dda83cd7 2442 struct type *type)
f93fca70
JB
2443{
2444 struct value *v;
bfb1c796 2445 const gdb_byte *src; /* First byte containing data to unpack */
f93fca70 2446 gdb_byte *unpacked;
220475ed 2447 const int is_scalar = is_scalar_type (type);
d5a22e77 2448 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d5722aa2 2449 gdb::byte_vector staging;
f93fca70
JB
2450
2451 type = ada_check_typedef (type);
2452
d0a9e810 2453 if (obj == NULL)
bfb1c796 2454 src = valaddr + offset;
d0a9e810 2455 else
bfb1c796 2456 src = value_contents (obj) + offset;
d0a9e810
JB
2457
2458 if (is_dynamic_type (type))
2459 {
2460 /* The length of TYPE might by dynamic, so we need to resolve
2461 TYPE in order to know its actual size, which we then use
2462 to create the contents buffer of the value we return.
2463 The difficulty is that the data containing our object is
2464 packed, and therefore maybe not at a byte boundary. So, what
2465 we do, is unpack the data into a byte-aligned buffer, and then
2466 use that buffer as our object's value for resolving the type. */
d5722aa2
PA
2467 int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2468 staging.resize (staging_len);
d0a9e810
JB
2469
2470 ada_unpack_from_contents (src, bit_offset, bit_size,
dda83cd7 2471 staging.data (), staging.size (),
d0a9e810
JB
2472 is_big_endian, has_negatives (type),
2473 is_scalar);
b249d2c2 2474 type = resolve_dynamic_type (type, staging, 0);
0cafa88c
JB
2475 if (TYPE_LENGTH (type) < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
2476 {
2477 /* This happens when the length of the object is dynamic,
2478 and is actually smaller than the space reserved for it.
2479 For instance, in an array of variant records, the bit_size
2480 we're given is the array stride, which is constant and
2481 normally equal to the maximum size of its element.
2482 But, in reality, each element only actually spans a portion
2483 of that stride. */
2484 bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
2485 }
d0a9e810
JB
2486 }
2487
f93fca70
JB
2488 if (obj == NULL)
2489 {
2490 v = allocate_value (type);
bfb1c796 2491 src = valaddr + offset;
f93fca70
JB
2492 }
2493 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2494 {
0cafa88c 2495 int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
bfb1c796 2496 gdb_byte *buf;
0cafa88c 2497
f93fca70 2498 v = value_at (type, value_address (obj) + offset);
bfb1c796
PA
2499 buf = (gdb_byte *) alloca (src_len);
2500 read_memory (value_address (v), buf, src_len);
2501 src = buf;
f93fca70
JB
2502 }
2503 else
2504 {
2505 v = allocate_value (type);
bfb1c796 2506 src = value_contents (obj) + offset;
f93fca70
JB
2507 }
2508
2509 if (obj != NULL)
2510 {
2511 long new_offset = offset;
2512
2513 set_value_component_location (v, obj);
2514 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2515 set_value_bitsize (v, bit_size);
2516 if (value_bitpos (v) >= HOST_CHAR_BIT)
dda83cd7 2517 {
f93fca70 2518 ++new_offset;
dda83cd7
SM
2519 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2520 }
f93fca70
JB
2521 set_value_offset (v, new_offset);
2522
2523 /* Also set the parent value. This is needed when trying to
2524 assign a new value (in inferior memory). */
2525 set_value_parent (v, obj);
2526 }
2527 else
2528 set_value_bitsize (v, bit_size);
bfb1c796 2529 unpacked = value_contents_writeable (v);
f93fca70
JB
2530
2531 if (bit_size == 0)
2532 {
2533 memset (unpacked, 0, TYPE_LENGTH (type));
2534 return v;
2535 }
2536
d5722aa2 2537 if (staging.size () == TYPE_LENGTH (type))
f93fca70 2538 {
d0a9e810
JB
2539 /* Small short-cut: If we've unpacked the data into a buffer
2540 of the same size as TYPE's length, then we can reuse that,
2541 instead of doing the unpacking again. */
d5722aa2 2542 memcpy (unpacked, staging.data (), staging.size ());
f93fca70 2543 }
d0a9e810
JB
2544 else
2545 ada_unpack_from_contents (src, bit_offset, bit_size,
2546 unpacked, TYPE_LENGTH (type),
2547 is_big_endian, has_negatives (type), is_scalar);
f93fca70 2548
14f9c5c9
AS
2549 return v;
2550}
d2e4a39e 2551
14f9c5c9
AS
2552/* Store the contents of FROMVAL into the location of TOVAL.
2553 Return a new value with the location of TOVAL and contents of
2554 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2555 floating-point or non-scalar types. */
14f9c5c9 2556
d2e4a39e
AS
2557static struct value *
2558ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2559{
df407dfe
AC
2560 struct type *type = value_type (toval);
2561 int bits = value_bitsize (toval);
14f9c5c9 2562
52ce6436
PH
2563 toval = ada_coerce_ref (toval);
2564 fromval = ada_coerce_ref (fromval);
2565
2566 if (ada_is_direct_array_type (value_type (toval)))
2567 toval = ada_coerce_to_simple_array (toval);
2568 if (ada_is_direct_array_type (value_type (fromval)))
2569 fromval = ada_coerce_to_simple_array (fromval);
2570
88e3b34b 2571 if (!deprecated_value_modifiable (toval))
323e0a4a 2572 error (_("Left operand of assignment is not a modifiable lvalue."));
14f9c5c9 2573
d2e4a39e 2574 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2575 && bits > 0
78134374 2576 && (type->code () == TYPE_CODE_FLT
dda83cd7 2577 || type->code () == TYPE_CODE_STRUCT))
14f9c5c9 2578 {
df407dfe
AC
2579 int len = (value_bitpos (toval)
2580 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
aced2898 2581 int from_size;
224c3ddb 2582 gdb_byte *buffer = (gdb_byte *) alloca (len);
d2e4a39e 2583 struct value *val;
42ae5230 2584 CORE_ADDR to_addr = value_address (toval);
14f9c5c9 2585
78134374 2586 if (type->code () == TYPE_CODE_FLT)
dda83cd7 2587 fromval = value_cast (type, fromval);
14f9c5c9 2588
52ce6436 2589 read_memory (to_addr, buffer, len);
aced2898
PH
2590 from_size = value_bitsize (fromval);
2591 if (from_size == 0)
2592 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
d48e62f4 2593
d5a22e77 2594 const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
d48e62f4
TT
2595 ULONGEST from_offset = 0;
2596 if (is_big_endian && is_scalar_type (value_type (fromval)))
2597 from_offset = from_size - bits;
2598 copy_bitwise (buffer, value_bitpos (toval),
2599 value_contents (fromval), from_offset,
2600 bits, is_big_endian);
972daa01 2601 write_memory_with_notification (to_addr, buffer, len);
8cebebb9 2602
14f9c5c9 2603 val = value_copy (toval);
0fd88904 2604 memcpy (value_contents_raw (val), value_contents (fromval),
dda83cd7 2605 TYPE_LENGTH (type));
04624583 2606 deprecated_set_value_type (val, type);
d2e4a39e 2607
14f9c5c9
AS
2608 return val;
2609 }
2610
2611 return value_assign (toval, fromval);
2612}
2613
2614
7c512744
JB
2615/* Given that COMPONENT is a memory lvalue that is part of the lvalue
2616 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2617 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2618 COMPONENT, and not the inferior's memory. The current contents
2619 of COMPONENT are ignored.
2620
2621 Although not part of the initial design, this function also works
2622 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2623 had a null address, and COMPONENT had an address which is equal to
2624 its offset inside CONTAINER. */
2625
52ce6436
PH
2626static void
2627value_assign_to_component (struct value *container, struct value *component,
2628 struct value *val)
2629{
2630 LONGEST offset_in_container =
42ae5230 2631 (LONGEST) (value_address (component) - value_address (container));
7c512744 2632 int bit_offset_in_container =
52ce6436
PH
2633 value_bitpos (component) - value_bitpos (container);
2634 int bits;
7c512744 2635
52ce6436
PH
2636 val = value_cast (value_type (component), val);
2637
2638 if (value_bitsize (component) == 0)
2639 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2640 else
2641 bits = value_bitsize (component);
2642
d5a22e77 2643 if (type_byte_order (value_type (container)) == BFD_ENDIAN_BIG)
2a62dfa9
JB
2644 {
2645 int src_offset;
2646
2647 if (is_scalar_type (check_typedef (value_type (component))))
dda83cd7 2648 src_offset
2a62dfa9
JB
2649 = TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits;
2650 else
2651 src_offset = 0;
a99bc3d2
JB
2652 copy_bitwise (value_contents_writeable (container) + offset_in_container,
2653 value_bitpos (container) + bit_offset_in_container,
2654 value_contents (val), src_offset, bits, 1);
2a62dfa9 2655 }
52ce6436 2656 else
a99bc3d2
JB
2657 copy_bitwise (value_contents_writeable (container) + offset_in_container,
2658 value_bitpos (container) + bit_offset_in_container,
2659 value_contents (val), 0, bits, 0);
7c512744
JB
2660}
2661
736ade86
XR
2662/* Determine if TYPE is an access to an unconstrained array. */
2663
d91e9ea8 2664bool
736ade86
XR
2665ada_is_access_to_unconstrained_array (struct type *type)
2666{
78134374 2667 return (type->code () == TYPE_CODE_TYPEDEF
736ade86
XR
2668 && is_thick_pntr (ada_typedef_target_type (type)));
2669}
2670
4c4b4cd2
PH
2671/* The value of the element of array ARR at the ARITY indices given in IND.
2672 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2673 thereto. */
2674
d2e4a39e
AS
2675struct value *
2676ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2677{
2678 int k;
d2e4a39e
AS
2679 struct value *elt;
2680 struct type *elt_type;
14f9c5c9
AS
2681
2682 elt = ada_coerce_to_simple_array (arr);
2683
df407dfe 2684 elt_type = ada_check_typedef (value_type (elt));
78134374 2685 if (elt_type->code () == TYPE_CODE_ARRAY
14f9c5c9
AS
2686 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2687 return value_subscript_packed (elt, arity, ind);
2688
2689 for (k = 0; k < arity; k += 1)
2690 {
b9c50e9a
XR
2691 struct type *saved_elt_type = TYPE_TARGET_TYPE (elt_type);
2692
78134374 2693 if (elt_type->code () != TYPE_CODE_ARRAY)
dda83cd7 2694 error (_("too many subscripts (%d expected)"), k);
b9c50e9a 2695
2497b498 2696 elt = value_subscript (elt, pos_atr (ind[k]));
b9c50e9a
XR
2697
2698 if (ada_is_access_to_unconstrained_array (saved_elt_type)
78134374 2699 && value_type (elt)->code () != TYPE_CODE_TYPEDEF)
b9c50e9a
XR
2700 {
2701 /* The element is a typedef to an unconstrained array,
2702 except that the value_subscript call stripped the
2703 typedef layer. The typedef layer is GNAT's way to
2704 specify that the element is, at the source level, an
2705 access to the unconstrained array, rather than the
2706 unconstrained array. So, we need to restore that
2707 typedef layer, which we can do by forcing the element's
2708 type back to its original type. Otherwise, the returned
2709 value is going to be printed as the array, rather
2710 than as an access. Another symptom of the same issue
2711 would be that an expression trying to dereference the
2712 element would also be improperly rejected. */
2713 deprecated_set_value_type (elt, saved_elt_type);
2714 }
2715
2716 elt_type = ada_check_typedef (value_type (elt));
14f9c5c9 2717 }
b9c50e9a 2718
14f9c5c9
AS
2719 return elt;
2720}
2721
deede10c
JB
2722/* Assuming ARR is a pointer to a GDB array, the value of the element
2723 of *ARR at the ARITY indices given in IND.
919e6dbe
PMR
2724 Does not read the entire array into memory.
2725
2726 Note: Unlike what one would expect, this function is used instead of
2727 ada_value_subscript for basically all non-packed array types. The reason
2728 for this is that a side effect of doing our own pointer arithmetics instead
2729 of relying on value_subscript is that there is no implicit typedef peeling.
2730 This is important for arrays of array accesses, where it allows us to
2731 preserve the fact that the array's element is an array access, where the
2732 access part os encoded in a typedef layer. */
14f9c5c9 2733
2c0b251b 2734static struct value *
deede10c 2735ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2736{
2737 int k;
919e6dbe 2738 struct value *array_ind = ada_value_ind (arr);
deede10c 2739 struct type *type
919e6dbe
PMR
2740 = check_typedef (value_enclosing_type (array_ind));
2741
78134374 2742 if (type->code () == TYPE_CODE_ARRAY
919e6dbe
PMR
2743 && TYPE_FIELD_BITSIZE (type, 0) > 0)
2744 return value_subscript_packed (array_ind, arity, ind);
14f9c5c9
AS
2745
2746 for (k = 0; k < arity; k += 1)
2747 {
2748 LONGEST lwb, upb;
14f9c5c9 2749
78134374 2750 if (type->code () != TYPE_CODE_ARRAY)
dda83cd7 2751 error (_("too many subscripts (%d expected)"), k);
d2e4a39e 2752 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
dda83cd7 2753 value_copy (arr));
3d967001 2754 get_discrete_bounds (type->index_type (), &lwb, &upb);
53a47a3e 2755 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
14f9c5c9
AS
2756 type = TYPE_TARGET_TYPE (type);
2757 }
2758
2759 return value_ind (arr);
2760}
2761
0b5d8877 2762/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
aa715135
JG
2763 actual type of ARRAY_PTR is ignored), returns the Ada slice of
2764 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
2765 this array is LOW, as per Ada rules. */
0b5d8877 2766static struct value *
f5938064 2767ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
dda83cd7 2768 int low, int high)
0b5d8877 2769{
b0dd7688 2770 struct type *type0 = ada_check_typedef (type);
3d967001 2771 struct type *base_index_type = TYPE_TARGET_TYPE (type0->index_type ());
0c9c3474 2772 struct type *index_type
aa715135 2773 = create_static_range_type (NULL, base_index_type, low, high);
9fe561ab
JB
2774 struct type *slice_type = create_array_type_with_stride
2775 (NULL, TYPE_TARGET_TYPE (type0), index_type,
24e99c6c 2776 type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 2777 TYPE_FIELD_BITSIZE (type0, 0));
3d967001 2778 int base_low = ada_discrete_type_low_bound (type0->index_type ());
6244c119 2779 gdb::optional<LONGEST> base_low_pos, low_pos;
aa715135
JG
2780 CORE_ADDR base;
2781
6244c119
SM
2782 low_pos = discrete_position (base_index_type, low);
2783 base_low_pos = discrete_position (base_index_type, base_low);
2784
2785 if (!low_pos.has_value () || !base_low_pos.has_value ())
aa715135
JG
2786 {
2787 warning (_("unable to get positions in slice, use bounds instead"));
2788 low_pos = low;
2789 base_low_pos = base_low;
2790 }
5b4ee69b 2791
7ff5b937
TT
2792 ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
2793 if (stride == 0)
2794 stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
2795
6244c119 2796 base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
f5938064 2797 return value_at_lazy (slice_type, base);
0b5d8877
PH
2798}
2799
2800
2801static struct value *
2802ada_value_slice (struct value *array, int low, int high)
2803{
b0dd7688 2804 struct type *type = ada_check_typedef (value_type (array));
3d967001 2805 struct type *base_index_type = TYPE_TARGET_TYPE (type->index_type ());
0c9c3474 2806 struct type *index_type
3d967001 2807 = create_static_range_type (NULL, type->index_type (), low, high);
9fe561ab
JB
2808 struct type *slice_type = create_array_type_with_stride
2809 (NULL, TYPE_TARGET_TYPE (type), index_type,
24e99c6c 2810 type->dyn_prop (DYN_PROP_BYTE_STRIDE),
9fe561ab 2811 TYPE_FIELD_BITSIZE (type, 0));
6244c119
SM
2812 gdb::optional<LONGEST> low_pos, high_pos;
2813
5b4ee69b 2814
6244c119
SM
2815 low_pos = discrete_position (base_index_type, low);
2816 high_pos = discrete_position (base_index_type, high);
2817
2818 if (!low_pos.has_value () || !high_pos.has_value ())
aa715135
JG
2819 {
2820 warning (_("unable to get positions in slice, use bounds instead"));
2821 low_pos = low;
2822 high_pos = high;
2823 }
2824
2825 return value_cast (slice_type,
6244c119 2826 value_slice (array, low, *high_pos - *low_pos + 1));
0b5d8877
PH
2827}
2828
14f9c5c9
AS
2829/* If type is a record type in the form of a standard GNAT array
2830 descriptor, returns the number of dimensions for type. If arr is a
2831 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2832 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2833
2834int
d2e4a39e 2835ada_array_arity (struct type *type)
14f9c5c9
AS
2836{
2837 int arity;
2838
2839 if (type == NULL)
2840 return 0;
2841
2842 type = desc_base_type (type);
2843
2844 arity = 0;
78134374 2845 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9 2846 return desc_arity (desc_bounds_type (type));
d2e4a39e 2847 else
78134374 2848 while (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 2849 {
dda83cd7
SM
2850 arity += 1;
2851 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2852 }
d2e4a39e 2853
14f9c5c9
AS
2854 return arity;
2855}
2856
2857/* If TYPE is a record type in the form of a standard GNAT array
2858 descriptor or a simple array type, returns the element type for
2859 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2860 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2861
d2e4a39e
AS
2862struct type *
2863ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2864{
2865 type = desc_base_type (type);
2866
78134374 2867 if (type->code () == TYPE_CODE_STRUCT)
14f9c5c9
AS
2868 {
2869 int k;
d2e4a39e 2870 struct type *p_array_type;
14f9c5c9 2871
556bdfd4 2872 p_array_type = desc_data_target_type (type);
14f9c5c9
AS
2873
2874 k = ada_array_arity (type);
2875 if (k == 0)
dda83cd7 2876 return NULL;
d2e4a39e 2877
4c4b4cd2 2878 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2879 if (nindices >= 0 && k > nindices)
dda83cd7 2880 k = nindices;
d2e4a39e 2881 while (k > 0 && p_array_type != NULL)
dda83cd7
SM
2882 {
2883 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
2884 k -= 1;
2885 }
14f9c5c9
AS
2886 return p_array_type;
2887 }
78134374 2888 else if (type->code () == TYPE_CODE_ARRAY)
14f9c5c9 2889 {
78134374 2890 while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
dda83cd7
SM
2891 {
2892 type = TYPE_TARGET_TYPE (type);
2893 nindices -= 1;
2894 }
14f9c5c9
AS
2895 return type;
2896 }
2897
2898 return NULL;
2899}
2900
4c4b4cd2 2901/* The type of nth index in arrays of given type (n numbering from 1).
dd19d49e
UW
2902 Does not examine memory. Throws an error if N is invalid or TYPE
2903 is not an array type. NAME is the name of the Ada attribute being
2904 evaluated ('range, 'first, 'last, or 'length); it is used in building
2905 the error message. */
14f9c5c9 2906
1eea4ebd
UW
2907static struct type *
2908ada_index_type (struct type *type, int n, const char *name)
14f9c5c9 2909{
4c4b4cd2
PH
2910 struct type *result_type;
2911
14f9c5c9
AS
2912 type = desc_base_type (type);
2913
1eea4ebd
UW
2914 if (n < 0 || n > ada_array_arity (type))
2915 error (_("invalid dimension number to '%s"), name);
14f9c5c9 2916
4c4b4cd2 2917 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2918 {
2919 int i;
2920
2921 for (i = 1; i < n; i += 1)
dda83cd7 2922 type = TYPE_TARGET_TYPE (type);
3d967001 2923 result_type = TYPE_TARGET_TYPE (type->index_type ());
4c4b4cd2 2924 /* FIXME: The stabs type r(0,0);bound;bound in an array type
dda83cd7
SM
2925 has a target type of TYPE_CODE_UNDEF. We compensate here, but
2926 perhaps stabsread.c would make more sense. */
78134374 2927 if (result_type && result_type->code () == TYPE_CODE_UNDEF)
dda83cd7 2928 result_type = NULL;
14f9c5c9 2929 }
d2e4a39e 2930 else
1eea4ebd
UW
2931 {
2932 result_type = desc_index_type (desc_bounds_type (type), n);
2933 if (result_type == NULL)
2934 error (_("attempt to take bound of something that is not an array"));
2935 }
2936
2937 return result_type;
14f9c5c9
AS
2938}
2939
2940/* Given that arr is an array type, returns the lower bound of the
2941 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2 2942 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1eea4ebd
UW
2943 array-descriptor type. It works for other arrays with bounds supplied
2944 by run-time quantities other than discriminants. */
14f9c5c9 2945
abb68b3e 2946static LONGEST
fb5e3d5c 2947ada_array_bound_from_type (struct type *arr_type, int n, int which)
14f9c5c9 2948{
8a48ac95 2949 struct type *type, *index_type_desc, *index_type;
1ce677a4 2950 int i;
262452ec
JK
2951
2952 gdb_assert (which == 0 || which == 1);
14f9c5c9 2953
ad82864c
JB
2954 if (ada_is_constrained_packed_array_type (arr_type))
2955 arr_type = decode_constrained_packed_array_type (arr_type);
14f9c5c9 2956
4c4b4cd2 2957 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
1eea4ebd 2958 return (LONGEST) - which;
14f9c5c9 2959
78134374 2960 if (arr_type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
2961 type = TYPE_TARGET_TYPE (arr_type);
2962 else
2963 type = arr_type;
2964
22c4c60c 2965 if (type->is_fixed_instance ())
bafffb51
JB
2966 {
2967 /* The array has already been fixed, so we do not need to
2968 check the parallel ___XA type again. That encoding has
2969 already been applied, so ignore it now. */
2970 index_type_desc = NULL;
2971 }
2972 else
2973 {
2974 index_type_desc = ada_find_parallel_type (type, "___XA");
2975 ada_fixup_array_indexes_type (index_type_desc);
2976 }
2977
262452ec 2978 if (index_type_desc != NULL)
940da03e 2979 index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
28c85d6c 2980 NULL);
262452ec 2981 else
8a48ac95
JB
2982 {
2983 struct type *elt_type = check_typedef (type);
2984
2985 for (i = 1; i < n; i++)
2986 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
2987
3d967001 2988 index_type = elt_type->index_type ();
8a48ac95 2989 }
262452ec 2990
43bbcdc2
PH
2991 return
2992 (LONGEST) (which == 0
dda83cd7
SM
2993 ? ada_discrete_type_low_bound (index_type)
2994 : ada_discrete_type_high_bound (index_type));
14f9c5c9
AS
2995}
2996
2997/* Given that arr is an array value, returns the lower bound of the
abb68b3e
JB
2998 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2999 WHICH is 1. This routine will also work for arrays with bounds
4c4b4cd2 3000 supplied by run-time quantities other than discriminants. */
14f9c5c9 3001
1eea4ebd 3002static LONGEST
4dc81987 3003ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 3004{
eb479039
JB
3005 struct type *arr_type;
3006
78134374 3007 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3008 arr = value_ind (arr);
3009 arr_type = value_enclosing_type (arr);
14f9c5c9 3010
ad82864c
JB
3011 if (ada_is_constrained_packed_array_type (arr_type))
3012 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
4c4b4cd2 3013 else if (ada_is_simple_array_type (arr_type))
1eea4ebd 3014 return ada_array_bound_from_type (arr_type, n, which);
14f9c5c9 3015 else
1eea4ebd 3016 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
14f9c5c9
AS
3017}
3018
3019/* Given that arr is an array value, returns the length of the
3020 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
3021 supplied by run-time quantities other than discriminants.
3022 Does not work for arrays indexed by enumeration types with representation
3023 clauses at the moment. */
14f9c5c9 3024
1eea4ebd 3025static LONGEST
d2e4a39e 3026ada_array_length (struct value *arr, int n)
14f9c5c9 3027{
aa715135
JG
3028 struct type *arr_type, *index_type;
3029 int low, high;
eb479039 3030
78134374 3031 if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
eb479039
JB
3032 arr = value_ind (arr);
3033 arr_type = value_enclosing_type (arr);
14f9c5c9 3034
ad82864c
JB
3035 if (ada_is_constrained_packed_array_type (arr_type))
3036 return ada_array_length (decode_constrained_packed_array (arr), n);
14f9c5c9 3037
4c4b4cd2 3038 if (ada_is_simple_array_type (arr_type))
aa715135
JG
3039 {
3040 low = ada_array_bound_from_type (arr_type, n, 0);
3041 high = ada_array_bound_from_type (arr_type, n, 1);
3042 }
14f9c5c9 3043 else
aa715135
JG
3044 {
3045 low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
3046 high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
3047 }
3048
f168693b 3049 arr_type = check_typedef (arr_type);
7150d33c 3050 index_type = ada_index_type (arr_type, n, "length");
aa715135
JG
3051 if (index_type != NULL)
3052 {
3053 struct type *base_type;
78134374 3054 if (index_type->code () == TYPE_CODE_RANGE)
aa715135
JG
3055 base_type = TYPE_TARGET_TYPE (index_type);
3056 else
3057 base_type = index_type;
3058
3059 low = pos_atr (value_from_longest (base_type, low));
3060 high = pos_atr (value_from_longest (base_type, high));
3061 }
3062 return high - low + 1;
4c4b4cd2
PH
3063}
3064
bff8c71f
TT
3065/* An array whose type is that of ARR_TYPE (an array type), with
3066 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3067 less than LOW, then LOW-1 is used. */
4c4b4cd2
PH
3068
3069static struct value *
bff8c71f 3070empty_array (struct type *arr_type, int low, int high)
4c4b4cd2 3071{
b0dd7688 3072 struct type *arr_type0 = ada_check_typedef (arr_type);
0c9c3474
SA
3073 struct type *index_type
3074 = create_static_range_type
dda83cd7 3075 (NULL, TYPE_TARGET_TYPE (arr_type0->index_type ()), low,
bff8c71f 3076 high < low ? low - 1 : high);
b0dd7688 3077 struct type *elt_type = ada_array_element_type (arr_type0, 1);
5b4ee69b 3078
0b5d8877 3079 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 3080}
14f9c5c9 3081\f
d2e4a39e 3082
dda83cd7 3083 /* Name resolution */
14f9c5c9 3084
4c4b4cd2
PH
3085/* The "decoded" name for the user-definable Ada operator corresponding
3086 to OP. */
14f9c5c9 3087
d2e4a39e 3088static const char *
4c4b4cd2 3089ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
3090{
3091 int i;
3092
4c4b4cd2 3093 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
3094 {
3095 if (ada_opname_table[i].op == op)
dda83cd7 3096 return ada_opname_table[i].decoded;
14f9c5c9 3097 }
323e0a4a 3098 error (_("Could not find operator name for opcode"));
14f9c5c9
AS
3099}
3100
de93309a
SM
3101/* Returns true (non-zero) iff decoded name N0 should appear before N1
3102 in a listing of choices during disambiguation (see sort_choices, below).
3103 The idea is that overloadings of a subprogram name from the
3104 same package should sort in their source order. We settle for ordering
3105 such symbols by their trailing number (__N or $N). */
14f9c5c9 3106
de93309a
SM
3107static int
3108encoded_ordered_before (const char *N0, const char *N1)
14f9c5c9 3109{
de93309a
SM
3110 if (N1 == NULL)
3111 return 0;
3112 else if (N0 == NULL)
3113 return 1;
3114 else
3115 {
3116 int k0, k1;
30b15541 3117
de93309a 3118 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
dda83cd7 3119 ;
de93309a 3120 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
dda83cd7 3121 ;
de93309a 3122 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
dda83cd7
SM
3123 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3124 {
3125 int n0, n1;
3126
3127 n0 = k0;
3128 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3129 n0 -= 1;
3130 n1 = k1;
3131 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3132 n1 -= 1;
3133 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3134 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3135 }
de93309a
SM
3136 return (strcmp (N0, N1) < 0);
3137 }
14f9c5c9
AS
3138}
3139
de93309a
SM
3140/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3141 encoded names. */
14f9c5c9 3142
de93309a
SM
3143static void
3144sort_choices (struct block_symbol syms[], int nsyms)
14f9c5c9 3145{
14f9c5c9 3146 int i;
14f9c5c9 3147
de93309a 3148 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3149 {
de93309a
SM
3150 struct block_symbol sym = syms[i];
3151 int j;
3152
3153 for (j = i - 1; j >= 0; j -= 1)
dda83cd7
SM
3154 {
3155 if (encoded_ordered_before (syms[j].symbol->linkage_name (),
3156 sym.symbol->linkage_name ()))
3157 break;
3158 syms[j + 1] = syms[j];
3159 }
de93309a
SM
3160 syms[j + 1] = sym;
3161 }
3162}
14f9c5c9 3163
de93309a
SM
3164/* Whether GDB should display formals and return types for functions in the
3165 overloads selection menu. */
3166static bool print_signatures = true;
4c4b4cd2 3167
de93309a
SM
3168/* Print the signature for SYM on STREAM according to the FLAGS options. For
3169 all but functions, the signature is just the name of the symbol. For
3170 functions, this is the name of the function, the list of types for formals
3171 and the return type (if any). */
4c4b4cd2 3172
de93309a
SM
3173static void
3174ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
3175 const struct type_print_options *flags)
3176{
3177 struct type *type = SYMBOL_TYPE (sym);
14f9c5c9 3178
987012b8 3179 fprintf_filtered (stream, "%s", sym->print_name ());
de93309a
SM
3180 if (!print_signatures
3181 || type == NULL
78134374 3182 || type->code () != TYPE_CODE_FUNC)
de93309a 3183 return;
4c4b4cd2 3184
1f704f76 3185 if (type->num_fields () > 0)
de93309a
SM
3186 {
3187 int i;
14f9c5c9 3188
de93309a 3189 fprintf_filtered (stream, " (");
1f704f76 3190 for (i = 0; i < type->num_fields (); ++i)
de93309a
SM
3191 {
3192 if (i > 0)
3193 fprintf_filtered (stream, "; ");
940da03e 3194 ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
de93309a
SM
3195 flags);
3196 }
3197 fprintf_filtered (stream, ")");
3198 }
3199 if (TYPE_TARGET_TYPE (type) != NULL
78134374 3200 && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
de93309a
SM
3201 {
3202 fprintf_filtered (stream, " return ");
3203 ada_print_type (TYPE_TARGET_TYPE (type), NULL, stream, -1, 0, flags);
3204 }
3205}
14f9c5c9 3206
de93309a
SM
3207/* Read and validate a set of numeric choices from the user in the
3208 range 0 .. N_CHOICES-1. Place the results in increasing
3209 order in CHOICES[0 .. N-1], and return N.
14f9c5c9 3210
de93309a
SM
3211 The user types choices as a sequence of numbers on one line
3212 separated by blanks, encoding them as follows:
14f9c5c9 3213
de93309a
SM
3214 + A choice of 0 means to cancel the selection, throwing an error.
3215 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3216 + The user chooses k by typing k+IS_ALL_CHOICE+1.
14f9c5c9 3217
de93309a 3218 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9 3219
de93309a
SM
3220 ANNOTATION_SUFFIX, if present, is used to annotate the input
3221 prompts (for use with the -f switch). */
14f9c5c9 3222
de93309a
SM
3223static int
3224get_selections (int *choices, int n_choices, int max_results,
dda83cd7 3225 int is_all_choice, const char *annotation_suffix)
de93309a 3226{
992a7040 3227 const char *args;
de93309a
SM
3228 const char *prompt;
3229 int n_chosen;
3230 int first_choice = is_all_choice ? 2 : 1;
14f9c5c9 3231
de93309a
SM
3232 prompt = getenv ("PS2");
3233 if (prompt == NULL)
3234 prompt = "> ";
4c4b4cd2 3235
de93309a 3236 args = command_line_input (prompt, annotation_suffix);
4c4b4cd2 3237
de93309a
SM
3238 if (args == NULL)
3239 error_no_arg (_("one or more choice numbers"));
14f9c5c9 3240
de93309a 3241 n_chosen = 0;
4c4b4cd2 3242
de93309a
SM
3243 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3244 order, as given in args. Choices are validated. */
3245 while (1)
14f9c5c9 3246 {
de93309a
SM
3247 char *args2;
3248 int choice, j;
76a01679 3249
de93309a
SM
3250 args = skip_spaces (args);
3251 if (*args == '\0' && n_chosen == 0)
dda83cd7 3252 error_no_arg (_("one or more choice numbers"));
de93309a 3253 else if (*args == '\0')
dda83cd7 3254 break;
76a01679 3255
de93309a
SM
3256 choice = strtol (args, &args2, 10);
3257 if (args == args2 || choice < 0
dda83cd7
SM
3258 || choice > n_choices + first_choice - 1)
3259 error (_("Argument must be choice number"));
de93309a 3260 args = args2;
76a01679 3261
de93309a 3262 if (choice == 0)
dda83cd7 3263 error (_("cancelled"));
76a01679 3264
de93309a 3265 if (choice < first_choice)
dda83cd7
SM
3266 {
3267 n_chosen = n_choices;
3268 for (j = 0; j < n_choices; j += 1)
3269 choices[j] = j;
3270 break;
3271 }
de93309a 3272 choice -= first_choice;
76a01679 3273
de93309a 3274 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
dda83cd7
SM
3275 {
3276 }
4c4b4cd2 3277
de93309a 3278 if (j < 0 || choice != choices[j])
dda83cd7
SM
3279 {
3280 int k;
4c4b4cd2 3281
dda83cd7
SM
3282 for (k = n_chosen - 1; k > j; k -= 1)
3283 choices[k + 1] = choices[k];
3284 choices[j + 1] = choice;
3285 n_chosen += 1;
3286 }
14f9c5c9
AS
3287 }
3288
de93309a
SM
3289 if (n_chosen > max_results)
3290 error (_("Select no more than %d of the above"), max_results);
3291
3292 return n_chosen;
14f9c5c9
AS
3293}
3294
de93309a
SM
3295/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3296 by asking the user (if necessary), returning the number selected,
3297 and setting the first elements of SYMS items. Error if no symbols
3298 selected. */
3299
3300/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3301 to be re-integrated one of these days. */
14f9c5c9
AS
3302
3303static int
de93309a 3304user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
14f9c5c9 3305{
de93309a
SM
3306 int i;
3307 int *chosen = XALLOCAVEC (int , nsyms);
3308 int n_chosen;
3309 int first_choice = (max_results == 1) ? 1 : 2;
3310 const char *select_mode = multiple_symbols_select_mode ();
14f9c5c9 3311
de93309a
SM
3312 if (max_results < 1)
3313 error (_("Request to select 0 symbols!"));
3314 if (nsyms <= 1)
3315 return nsyms;
14f9c5c9 3316
de93309a
SM
3317 if (select_mode == multiple_symbols_cancel)
3318 error (_("\
3319canceled because the command is ambiguous\n\
3320See set/show multiple-symbol."));
14f9c5c9 3321
de93309a
SM
3322 /* If select_mode is "all", then return all possible symbols.
3323 Only do that if more than one symbol can be selected, of course.
3324 Otherwise, display the menu as usual. */
3325 if (select_mode == multiple_symbols_all && max_results > 1)
3326 return nsyms;
14f9c5c9 3327
de93309a
SM
3328 printf_filtered (_("[0] cancel\n"));
3329 if (max_results > 1)
3330 printf_filtered (_("[1] all\n"));
14f9c5c9 3331
de93309a 3332 sort_choices (syms, nsyms);
14f9c5c9 3333
de93309a
SM
3334 for (i = 0; i < nsyms; i += 1)
3335 {
3336 if (syms[i].symbol == NULL)
dda83cd7 3337 continue;
14f9c5c9 3338
de93309a 3339 if (SYMBOL_CLASS (syms[i].symbol) == LOC_BLOCK)
dda83cd7
SM
3340 {
3341 struct symtab_and_line sal =
3342 find_function_start_sal (syms[i].symbol, 1);
14f9c5c9 3343
de93309a
SM
3344 printf_filtered ("[%d] ", i + first_choice);
3345 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3346 &type_print_raw_options);
3347 if (sal.symtab == NULL)
3348 printf_filtered (_(" at %p[<no source file available>%p]:%d\n"),
3349 metadata_style.style ().ptr (), nullptr, sal.line);
3350 else
3351 printf_filtered
3352 (_(" at %ps:%d\n"),
3353 styled_string (file_name_style.style (),
3354 symtab_to_filename_for_display (sal.symtab)),
3355 sal.line);
dda83cd7
SM
3356 continue;
3357 }
76a01679 3358 else
dda83cd7
SM
3359 {
3360 int is_enumeral =
3361 (SYMBOL_CLASS (syms[i].symbol) == LOC_CONST
3362 && SYMBOL_TYPE (syms[i].symbol) != NULL
3363 && SYMBOL_TYPE (syms[i].symbol)->code () == TYPE_CODE_ENUM);
de93309a 3364 struct symtab *symtab = NULL;
4c4b4cd2 3365
de93309a
SM
3366 if (SYMBOL_OBJFILE_OWNED (syms[i].symbol))
3367 symtab = symbol_symtab (syms[i].symbol);
3368
dda83cd7 3369 if (SYMBOL_LINE (syms[i].symbol) != 0 && symtab != NULL)
de93309a
SM
3370 {
3371 printf_filtered ("[%d] ", i + first_choice);
3372 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3373 &type_print_raw_options);
3374 printf_filtered (_(" at %s:%d\n"),
3375 symtab_to_filename_for_display (symtab),
3376 SYMBOL_LINE (syms[i].symbol));
3377 }
dda83cd7
SM
3378 else if (is_enumeral
3379 && SYMBOL_TYPE (syms[i].symbol)->name () != NULL)
3380 {
3381 printf_filtered (("[%d] "), i + first_choice);
3382 ada_print_type (SYMBOL_TYPE (syms[i].symbol), NULL,
3383 gdb_stdout, -1, 0, &type_print_raw_options);
3384 printf_filtered (_("'(%s) (enumeral)\n"),
987012b8 3385 syms[i].symbol->print_name ());
dda83cd7 3386 }
de93309a
SM
3387 else
3388 {
3389 printf_filtered ("[%d] ", i + first_choice);
3390 ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
3391 &type_print_raw_options);
3392
3393 if (symtab != NULL)
3394 printf_filtered (is_enumeral
3395 ? _(" in %s (enumeral)\n")
3396 : _(" at %s:?\n"),
3397 symtab_to_filename_for_display (symtab));
3398 else
3399 printf_filtered (is_enumeral
3400 ? _(" (enumeral)\n")
3401 : _(" at ?\n"));
3402 }
dda83cd7 3403 }
14f9c5c9 3404 }
14f9c5c9 3405
de93309a 3406 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
dda83cd7 3407 "overload-choice");
14f9c5c9 3408
de93309a
SM
3409 for (i = 0; i < n_chosen; i += 1)
3410 syms[i] = syms[chosen[i]];
14f9c5c9 3411
de93309a
SM
3412 return n_chosen;
3413}
14f9c5c9 3414
cd9a3148
TT
3415/* See ada-lang.h. */
3416
3417block_symbol
3418ada_find_operator_symbol (enum exp_opcode op, int parse_completion,
3419 int nargs, value *argvec[])
3420{
3421 if (possible_user_operator_p (op, argvec))
3422 {
3423 std::vector<struct block_symbol> candidates
3424 = ada_lookup_symbol_list (ada_decoded_op_name (op),
3425 NULL, VAR_DOMAIN);
3426
3427 int i = ada_resolve_function (candidates, argvec,
3428 nargs, ada_decoded_op_name (op), NULL,
3429 parse_completion);
3430 if (i >= 0)
3431 return candidates[i];
3432 }
3433 return {};
3434}
3435
3436/* See ada-lang.h. */
3437
3438block_symbol
3439ada_resolve_funcall (struct symbol *sym, const struct block *block,
3440 struct type *context_type,
3441 int parse_completion,
3442 int nargs, value *argvec[],
3443 innermost_block_tracker *tracker)
3444{
3445 std::vector<struct block_symbol> candidates
3446 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3447
3448 int i;
3449 if (candidates.size () == 1)
3450 i = 0;
3451 else
3452 {
3453 i = ada_resolve_function
3454 (candidates,
3455 argvec, nargs,
3456 sym->linkage_name (),
3457 context_type, parse_completion);
3458 if (i < 0)
3459 error (_("Could not find a match for %s"), sym->print_name ());
3460 }
3461
3462 tracker->update (candidates[i]);
3463 return candidates[i];
3464}
3465
3466/* See ada-lang.h. */
3467
3468block_symbol
3469ada_resolve_variable (struct symbol *sym, const struct block *block,
3470 struct type *context_type,
3471 int parse_completion,
3472 int deprocedure_p,
3473 innermost_block_tracker *tracker)
3474{
3475 std::vector<struct block_symbol> candidates
3476 = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
3477
3478 if (std::any_of (candidates.begin (),
3479 candidates.end (),
3480 [] (block_symbol &bsym)
3481 {
3482 switch (SYMBOL_CLASS (bsym.symbol))
3483 {
3484 case LOC_REGISTER:
3485 case LOC_ARG:
3486 case LOC_REF_ARG:
3487 case LOC_REGPARM_ADDR:
3488 case LOC_LOCAL:
3489 case LOC_COMPUTED:
3490 return true;
3491 default:
3492 return false;
3493 }
3494 }))
3495 {
3496 /* Types tend to get re-introduced locally, so if there
3497 are any local symbols that are not types, first filter
3498 out all types. */
3499 candidates.erase
3500 (std::remove_if
3501 (candidates.begin (),
3502 candidates.end (),
3503 [] (block_symbol &bsym)
3504 {
3505 return SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF;
3506 }),
3507 candidates.end ());
3508 }
3509
3510 int i;
3511 if (candidates.empty ())
3512 error (_("No definition found for %s"), sym->print_name ());
3513 else if (candidates.size () == 1)
3514 i = 0;
3515 else if (deprocedure_p && !is_nonfunction (candidates))
3516 {
3517 i = ada_resolve_function
3518 (candidates, NULL, 0,
3519 sym->linkage_name (),
3520 context_type, parse_completion);
3521 if (i < 0)
3522 error (_("Could not find a match for %s"), sym->print_name ());
3523 }
3524 else
3525 {
3526 printf_filtered (_("Multiple matches for %s\n"), sym->print_name ());
3527 user_select_syms (candidates.data (), candidates.size (), 1);
3528 i = 0;
3529 }
3530
3531 tracker->update (candidates[i]);
3532 return candidates[i];
3533}
3534
de93309a
SM
3535/* Resolve the operator of the subexpression beginning at
3536 position *POS of *EXPP. "Resolving" consists of replacing
3537 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
3538 with their resolutions, replacing built-in operators with
3539 function calls to user-defined operators, where appropriate, and,
3540 when DEPROCEDURE_P is non-zero, converting function-valued variables
3541 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
3542 are as in ada_resolve, above. */
14f9c5c9 3543
de93309a
SM
3544static struct value *
3545resolve_subexp (expression_up *expp, int *pos, int deprocedure_p,
dda83cd7 3546 struct type *context_type, int parse_completion,
de93309a 3547 innermost_block_tracker *tracker)
14f9c5c9 3548{
de93309a
SM
3549 int pc = *pos;
3550 int i;
3551 struct expression *exp; /* Convenience: == *expp. */
3552 enum exp_opcode op = (*expp)->elts[pc].opcode;
3553 struct value **argvec; /* Vector of operand types (alloca'ed). */
3554 int nargs; /* Number of operands. */
3555 int oplen;
19184910
TT
3556 /* If we're resolving an expression like ARRAY(ARG...), then we set
3557 this to the type of the array, so we can use the index types as
3558 the expected types for resolution. */
3559 struct type *array_type = nullptr;
3560 /* The arity of ARRAY_TYPE. */
3561 int array_arity = 0;
14f9c5c9 3562
de93309a
SM
3563 argvec = NULL;
3564 nargs = 0;
3565 exp = expp->get ();
4c4b4cd2 3566
de93309a
SM
3567 /* Pass one: resolve operands, saving their types and updating *pos,
3568 if needed. */
3569 switch (op)
3570 {
3571 case OP_FUNCALL:
3572 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
dda83cd7
SM
3573 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
3574 *pos += 7;
de93309a 3575 else
dda83cd7
SM
3576 {
3577 *pos += 3;
19184910
TT
3578 struct value *lhs = resolve_subexp (expp, pos, 0, NULL,
3579 parse_completion, tracker);
3580 struct type *lhstype = ada_check_typedef (value_type (lhs));
3581 array_arity = ada_array_arity (lhstype);
3582 if (array_arity > 0)
3583 array_type = lhstype;
dda83cd7 3584 }
de93309a
SM
3585 nargs = longest_to_int (exp->elts[pc + 1].longconst);
3586 break;
14f9c5c9 3587
de93309a
SM
3588 case UNOP_ADDR:
3589 *pos += 1;
3590 resolve_subexp (expp, pos, 0, NULL, parse_completion, tracker);
3591 break;
3592
3593 case UNOP_QUAL:
3594 *pos += 3;
3595 resolve_subexp (expp, pos, 1, check_typedef (exp->elts[pc + 1].type),
3596 parse_completion, tracker);
3597 break;
3598
3599 case OP_ATR_MODULUS:
3600 case OP_ATR_SIZE:
3601 case OP_ATR_TAG:
3602 case OP_ATR_FIRST:
3603 case OP_ATR_LAST:
3604 case OP_ATR_LENGTH:
3605 case OP_ATR_POS:
3606 case OP_ATR_VAL:
3607 case OP_ATR_MIN:
3608 case OP_ATR_MAX:
3609 case TERNOP_IN_RANGE:
3610 case BINOP_IN_BOUNDS:
3611 case UNOP_IN_RANGE:
3612 case OP_AGGREGATE:
3613 case OP_OTHERS:
3614 case OP_CHOICES:
3615 case OP_POSITIONAL:
3616 case OP_DISCRETE_RANGE:
3617 case OP_NAME:
3618 ada_forward_operator_length (exp, pc, &oplen, &nargs);
3619 *pos += oplen;
3620 break;
3621
3622 case BINOP_ASSIGN:
3623 {
dda83cd7
SM
3624 struct value *arg1;
3625
3626 *pos += 1;
3627 arg1 = resolve_subexp (expp, pos, 0, NULL, parse_completion, tracker);
3628 if (arg1 == NULL)
3629 resolve_subexp (expp, pos, 1, NULL, parse_completion, tracker);
3630 else
3631 resolve_subexp (expp, pos, 1, value_type (arg1), parse_completion,
de93309a 3632 tracker);
dda83cd7 3633 break;
de93309a
SM
3634 }
3635
3636 case UNOP_CAST:
3637 *pos += 3;
3638 nargs = 1;
3639 break;
3640
3641 case BINOP_ADD:
3642 case BINOP_SUB:
3643 case BINOP_MUL:
3644 case BINOP_DIV:
3645 case BINOP_REM:
3646 case BINOP_MOD:
3647 case BINOP_EXP:
3648 case BINOP_CONCAT:
3649 case BINOP_LOGICAL_AND:
3650 case BINOP_LOGICAL_OR:
3651 case BINOP_BITWISE_AND:
3652 case BINOP_BITWISE_IOR:
3653 case BINOP_BITWISE_XOR:
3654
3655 case BINOP_EQUAL:
3656 case BINOP_NOTEQUAL:
3657 case BINOP_LESS:
3658 case BINOP_GTR:
3659 case BINOP_LEQ:
3660 case BINOP_GEQ:
3661
3662 case BINOP_REPEAT:
3663 case BINOP_SUBSCRIPT:
3664 case BINOP_COMMA:
3665 *pos += 1;
3666 nargs = 2;
3667 break;
3668
3669 case UNOP_NEG:
3670 case UNOP_PLUS:
3671 case UNOP_LOGICAL_NOT:
3672 case UNOP_ABS:
3673 case UNOP_IND:
3674 *pos += 1;
3675 nargs = 1;
3676 break;
3677
3678 case OP_LONG:
3679 case OP_FLOAT:
3680 case OP_VAR_VALUE:
3681 case OP_VAR_MSYM_VALUE:
3682 *pos += 4;
3683 break;
3684
3685 case OP_TYPE:
3686 case OP_BOOL:
3687 case OP_LAST:
3688 case OP_INTERNALVAR:
3689 *pos += 3;
3690 break;
3691
3692 case UNOP_MEMVAL:
3693 *pos += 3;
3694 nargs = 1;
3695 break;
3696
3697 case OP_REGISTER:
3698 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
3699 break;
3700
3701 case STRUCTOP_STRUCT:
3702 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
3703 nargs = 1;
3704 break;
3705
3706 case TERNOP_SLICE:
3707 *pos += 1;
3708 nargs = 3;
3709 break;
3710
3711 case OP_STRING:
3712 break;
3713
3714 default:
3715 error (_("Unexpected operator during name resolution"));
14f9c5c9 3716 }
14f9c5c9 3717
de93309a
SM
3718 argvec = XALLOCAVEC (struct value *, nargs + 1);
3719 for (i = 0; i < nargs; i += 1)
19184910
TT
3720 {
3721 struct type *subtype = nullptr;
3722 if (i < array_arity)
3723 subtype = ada_index_type (array_type, i + 1, "array type");
3724 argvec[i] = resolve_subexp (expp, pos, 1, subtype, parse_completion,
3725 tracker);
3726 }
de93309a
SM
3727 argvec[i] = NULL;
3728 exp = expp->get ();
4c4b4cd2 3729
de93309a
SM
3730 /* Pass two: perform any resolution on principal operator. */
3731 switch (op)
14f9c5c9 3732 {
de93309a
SM
3733 default:
3734 break;
5b4ee69b 3735
de93309a
SM
3736 case OP_VAR_VALUE:
3737 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
dda83cd7 3738 {
cd9a3148
TT
3739 block_symbol resolved
3740 = ada_resolve_variable (exp->elts[pc + 2].symbol,
3741 exp->elts[pc + 1].block,
3742 context_type, parse_completion,
3743 deprocedure_p, tracker);
3744 exp->elts[pc + 1].block = resolved.block;
3745 exp->elts[pc + 2].symbol = resolved.symbol;
dda83cd7 3746 }
14f9c5c9 3747
de93309a 3748 if (deprocedure_p
dda83cd7
SM
3749 && (SYMBOL_TYPE (exp->elts[pc + 2].symbol)->code ()
3750 == TYPE_CODE_FUNC))
3751 {
3752 replace_operator_with_call (expp, pc, 0, 4,
3753 exp->elts[pc + 2].symbol,
3754 exp->elts[pc + 1].block);
3755 exp = expp->get ();
3756 }
de93309a
SM
3757 break;
3758
3759 case OP_FUNCALL:
3760 {
dda83cd7
SM
3761 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
3762 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
3763 {
cd9a3148
TT
3764 block_symbol resolved
3765 = ada_resolve_funcall (exp->elts[pc + 5].symbol,
3766 exp->elts[pc + 4].block,
3767 context_type, parse_completion,
3768 nargs, argvec,
3769 tracker);
3770 exp->elts[pc + 4].block = resolved.block;
3771 exp->elts[pc + 5].symbol = resolved.symbol;
dda83cd7 3772 }
de93309a
SM
3773 }
3774 break;
3775 case BINOP_ADD:
3776 case BINOP_SUB:
3777 case BINOP_MUL:
3778 case BINOP_DIV:
3779 case BINOP_REM:
3780 case BINOP_MOD:
3781 case BINOP_CONCAT:
3782 case BINOP_BITWISE_AND:
3783 case BINOP_BITWISE_IOR:
3784 case BINOP_BITWISE_XOR:
3785 case BINOP_EQUAL:
3786 case BINOP_NOTEQUAL:
3787 case BINOP_LESS:
3788 case BINOP_GTR:
3789 case BINOP_LEQ:
3790 case BINOP_GEQ:
3791 case BINOP_EXP:
3792 case UNOP_NEG:
3793 case UNOP_PLUS:
3794 case UNOP_LOGICAL_NOT:
3795 case UNOP_ABS:
cd9a3148
TT
3796 {
3797 block_symbol found = ada_find_operator_symbol (op, parse_completion,
3798 nargs, argvec);
3799 if (found.symbol == nullptr)
3800 break;
d72413e6 3801
cd9a3148
TT
3802 replace_operator_with_call (expp, pc, nargs, 1,
3803 found.symbol, found.block);
3804 exp = expp->get ();
3805 }
de93309a 3806 break;
d72413e6 3807
de93309a
SM
3808 case OP_TYPE:
3809 case OP_REGISTER:
3810 return NULL;
d72413e6 3811 }
d72413e6 3812
de93309a
SM
3813 *pos = pc;
3814 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
3815 return evaluate_var_msym_value (EVAL_AVOID_SIDE_EFFECTS,
3816 exp->elts[pc + 1].objfile,
3817 exp->elts[pc + 2].msymbol);
3818 else
3819 return evaluate_subexp_type (exp, pos);
3820}
14f9c5c9 3821
de93309a
SM
3822/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
3823 MAY_DEREF is non-zero, the formal may be a pointer and the actual
3824 a non-pointer. */
3825/* The term "match" here is rather loose. The match is heuristic and
3826 liberal. */
14f9c5c9 3827
de93309a
SM
3828static int
3829ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9 3830{
de93309a
SM
3831 ftype = ada_check_typedef (ftype);
3832 atype = ada_check_typedef (atype);
14f9c5c9 3833
78134374 3834 if (ftype->code () == TYPE_CODE_REF)
de93309a 3835 ftype = TYPE_TARGET_TYPE (ftype);
78134374 3836 if (atype->code () == TYPE_CODE_REF)
de93309a 3837 atype = TYPE_TARGET_TYPE (atype);
14f9c5c9 3838
78134374 3839 switch (ftype->code ())
14f9c5c9 3840 {
de93309a 3841 default:
78134374 3842 return ftype->code () == atype->code ();
de93309a 3843 case TYPE_CODE_PTR:
78134374 3844 if (atype->code () == TYPE_CODE_PTR)
dda83cd7
SM
3845 return ada_type_match (TYPE_TARGET_TYPE (ftype),
3846 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e 3847 else
dda83cd7
SM
3848 return (may_deref
3849 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
de93309a
SM
3850 case TYPE_CODE_INT:
3851 case TYPE_CODE_ENUM:
3852 case TYPE_CODE_RANGE:
78134374 3853 switch (atype->code ())
dda83cd7
SM
3854 {
3855 case TYPE_CODE_INT:
3856 case TYPE_CODE_ENUM:
3857 case TYPE_CODE_RANGE:
3858 return 1;
3859 default:
3860 return 0;
3861 }
d2e4a39e 3862
de93309a 3863 case TYPE_CODE_ARRAY:
78134374 3864 return (atype->code () == TYPE_CODE_ARRAY
dda83cd7 3865 || ada_is_array_descriptor_type (atype));
14f9c5c9 3866
de93309a
SM
3867 case TYPE_CODE_STRUCT:
3868 if (ada_is_array_descriptor_type (ftype))
dda83cd7
SM
3869 return (atype->code () == TYPE_CODE_ARRAY
3870 || ada_is_array_descriptor_type (atype));
de93309a 3871 else
dda83cd7
SM
3872 return (atype->code () == TYPE_CODE_STRUCT
3873 && !ada_is_array_descriptor_type (atype));
14f9c5c9 3874
de93309a
SM
3875 case TYPE_CODE_UNION:
3876 case TYPE_CODE_FLT:
78134374 3877 return (atype->code () == ftype->code ());
de93309a 3878 }
14f9c5c9
AS
3879}
3880
de93309a
SM
3881/* Return non-zero if the formals of FUNC "sufficiently match" the
3882 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3883 may also be an enumeral, in which case it is treated as a 0-
3884 argument function. */
14f9c5c9 3885
de93309a
SM
3886static int
3887ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3888{
3889 int i;
3890 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 3891
de93309a 3892 if (SYMBOL_CLASS (func) == LOC_CONST
78134374 3893 && func_type->code () == TYPE_CODE_ENUM)
de93309a 3894 return (n_actuals == 0);
78134374 3895 else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
de93309a 3896 return 0;
14f9c5c9 3897
1f704f76 3898 if (func_type->num_fields () != n_actuals)
de93309a 3899 return 0;
14f9c5c9 3900
de93309a
SM
3901 for (i = 0; i < n_actuals; i += 1)
3902 {
3903 if (actuals[i] == NULL)
dda83cd7 3904 return 0;
de93309a 3905 else
dda83cd7
SM
3906 {
3907 struct type *ftype = ada_check_typedef (func_type->field (i).type ());
3908 struct type *atype = ada_check_typedef (value_type (actuals[i]));
14f9c5c9 3909
dda83cd7
SM
3910 if (!ada_type_match (ftype, atype, 1))
3911 return 0;
3912 }
de93309a
SM
3913 }
3914 return 1;
3915}
d2e4a39e 3916
de93309a
SM
3917/* False iff function type FUNC_TYPE definitely does not produce a value
3918 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3919 FUNC_TYPE is not a valid function type with a non-null return type
3920 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
14f9c5c9 3921
de93309a
SM
3922static int
3923return_match (struct type *func_type, struct type *context_type)
3924{
3925 struct type *return_type;
d2e4a39e 3926
de93309a
SM
3927 if (func_type == NULL)
3928 return 1;
14f9c5c9 3929
78134374 3930 if (func_type->code () == TYPE_CODE_FUNC)
de93309a
SM
3931 return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
3932 else
3933 return_type = get_base_type (func_type);
3934 if (return_type == NULL)
3935 return 1;
76a01679 3936
de93309a 3937 context_type = get_base_type (context_type);
14f9c5c9 3938
78134374 3939 if (return_type->code () == TYPE_CODE_ENUM)
de93309a
SM
3940 return context_type == NULL || return_type == context_type;
3941 else if (context_type == NULL)
78134374 3942 return return_type->code () != TYPE_CODE_VOID;
de93309a 3943 else
78134374 3944 return return_type->code () == context_type->code ();
de93309a 3945}
14f9c5c9 3946
14f9c5c9 3947
1bfa81ac 3948/* Returns the index in SYMS that contains the symbol for the
de93309a
SM
3949 function (if any) that matches the types of the NARGS arguments in
3950 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3951 that returns that type, then eliminate matches that don't. If
3952 CONTEXT_TYPE is void and there is at least one match that does not
3953 return void, eliminate all matches that do.
14f9c5c9 3954
de93309a
SM
3955 Asks the user if there is more than one match remaining. Returns -1
3956 if there is no such symbol or none is selected. NAME is used
3957 solely for messages. May re-arrange and modify SYMS in
3958 the process; the index returned is for the modified vector. */
14f9c5c9 3959
de93309a 3960static int
d1183b06
TT
3961ada_resolve_function (std::vector<struct block_symbol> &syms,
3962 struct value **args, int nargs,
dda83cd7 3963 const char *name, struct type *context_type,
de93309a
SM
3964 int parse_completion)
3965{
3966 int fallback;
3967 int k;
3968 int m; /* Number of hits */
14f9c5c9 3969
de93309a
SM
3970 m = 0;
3971 /* In the first pass of the loop, we only accept functions matching
3972 context_type. If none are found, we add a second pass of the loop
3973 where every function is accepted. */
3974 for (fallback = 0; m == 0 && fallback < 2; fallback++)
3975 {
d1183b06 3976 for (k = 0; k < syms.size (); k += 1)
dda83cd7
SM
3977 {
3978 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].symbol));
5b4ee69b 3979
dda83cd7
SM
3980 if (ada_args_match (syms[k].symbol, args, nargs)
3981 && (fallback || return_match (type, context_type)))
3982 {
3983 syms[m] = syms[k];
3984 m += 1;
3985 }
3986 }
14f9c5c9
AS
3987 }
3988
de93309a
SM
3989 /* If we got multiple matches, ask the user which one to use. Don't do this
3990 interactive thing during completion, though, as the purpose of the
3991 completion is providing a list of all possible matches. Prompting the
3992 user to filter it down would be completely unexpected in this case. */
3993 if (m == 0)
3994 return -1;
3995 else if (m > 1 && !parse_completion)
3996 {
3997 printf_filtered (_("Multiple matches for %s\n"), name);
d1183b06 3998 user_select_syms (syms.data (), m, 1);
de93309a
SM
3999 return 0;
4000 }
4001 return 0;
14f9c5c9
AS
4002}
4003
4c4b4cd2
PH
4004/* Replace the operator of length OPLEN at position PC in *EXPP with a call
4005 on the function identified by SYM and BLOCK, and taking NARGS
4006 arguments. Update *EXPP as needed to hold more space. */
14f9c5c9
AS
4007
4008static void
e9d9f57e 4009replace_operator_with_call (expression_up *expp, int pc, int nargs,
dda83cd7
SM
4010 int oplen, struct symbol *sym,
4011 const struct block *block)
14f9c5c9 4012{
00158a68
TT
4013 /* We want to add 6 more elements (3 for funcall, 4 for function
4014 symbol, -OPLEN for operator being replaced) to the
4015 expression. */
e9d9f57e 4016 struct expression *exp = expp->get ();
00158a68 4017 int save_nelts = exp->nelts;
f51f9f1d
TV
4018 int extra_elts = 7 - oplen;
4019 exp->nelts += extra_elts;
14f9c5c9 4020
f51f9f1d
TV
4021 if (extra_elts > 0)
4022 exp->resize (exp->nelts);
00158a68
TT
4023 memmove (exp->elts + pc + 7, exp->elts + pc + oplen,
4024 EXP_ELEM_TO_BYTES (save_nelts - pc - oplen));
f51f9f1d
TV
4025 if (extra_elts < 0)
4026 exp->resize (exp->nelts);
14f9c5c9 4027
00158a68
TT
4028 exp->elts[pc].opcode = exp->elts[pc + 2].opcode = OP_FUNCALL;
4029 exp->elts[pc + 1].longconst = (LONGEST) nargs;
14f9c5c9 4030
00158a68
TT
4031 exp->elts[pc + 3].opcode = exp->elts[pc + 6].opcode = OP_VAR_VALUE;
4032 exp->elts[pc + 4].block = block;
4033 exp->elts[pc + 5].symbol = sym;
d2e4a39e 4034}
14f9c5c9
AS
4035
4036/* Type-class predicates */
4037
4c4b4cd2
PH
4038/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4039 or FLOAT). */
14f9c5c9
AS
4040
4041static int
d2e4a39e 4042numeric_type_p (struct type *type)
14f9c5c9
AS
4043{
4044 if (type == NULL)
4045 return 0;
d2e4a39e
AS
4046 else
4047 {
78134374 4048 switch (type->code ())
dda83cd7
SM
4049 {
4050 case TYPE_CODE_INT:
4051 case TYPE_CODE_FLT:
4052 return 1;
4053 case TYPE_CODE_RANGE:
4054 return (type == TYPE_TARGET_TYPE (type)
4055 || numeric_type_p (TYPE_TARGET_TYPE (type)));
4056 default:
4057 return 0;
4058 }
d2e4a39e 4059 }
14f9c5c9
AS
4060}
4061
4c4b4cd2 4062/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
4063
4064static int
d2e4a39e 4065integer_type_p (struct type *type)
14f9c5c9
AS
4066{
4067 if (type == NULL)
4068 return 0;
d2e4a39e
AS
4069 else
4070 {
78134374 4071 switch (type->code ())
dda83cd7
SM
4072 {
4073 case TYPE_CODE_INT:
4074 return 1;
4075 case TYPE_CODE_RANGE:
4076 return (type == TYPE_TARGET_TYPE (type)
4077 || integer_type_p (TYPE_TARGET_TYPE (type)));
4078 default:
4079 return 0;
4080 }
d2e4a39e 4081 }
14f9c5c9
AS
4082}
4083
4c4b4cd2 4084/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
4085
4086static int
d2e4a39e 4087scalar_type_p (struct type *type)
14f9c5c9
AS
4088{
4089 if (type == NULL)
4090 return 0;
d2e4a39e
AS
4091 else
4092 {
78134374 4093 switch (type->code ())
dda83cd7
SM
4094 {
4095 case TYPE_CODE_INT:
4096 case TYPE_CODE_RANGE:
4097 case TYPE_CODE_ENUM:
4098 case TYPE_CODE_FLT:
4099 return 1;
4100 default:
4101 return 0;
4102 }
d2e4a39e 4103 }
14f9c5c9
AS
4104}
4105
4c4b4cd2 4106/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
4107
4108static int
d2e4a39e 4109discrete_type_p (struct type *type)
14f9c5c9
AS
4110{
4111 if (type == NULL)
4112 return 0;
d2e4a39e
AS
4113 else
4114 {
78134374 4115 switch (type->code ())
dda83cd7
SM
4116 {
4117 case TYPE_CODE_INT:
4118 case TYPE_CODE_RANGE:
4119 case TYPE_CODE_ENUM:
4120 case TYPE_CODE_BOOL:
4121 return 1;
4122 default:
4123 return 0;
4124 }
d2e4a39e 4125 }
14f9c5c9
AS
4126}
4127
4c4b4cd2
PH
4128/* Returns non-zero if OP with operands in the vector ARGS could be
4129 a user-defined function. Errs on the side of pre-defined operators
4130 (i.e., result 0). */
14f9c5c9
AS
4131
4132static int
d2e4a39e 4133possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 4134{
76a01679 4135 struct type *type0 =
df407dfe 4136 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
d2e4a39e 4137 struct type *type1 =
df407dfe 4138 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
d2e4a39e 4139
4c4b4cd2
PH
4140 if (type0 == NULL)
4141 return 0;
4142
14f9c5c9
AS
4143 switch (op)
4144 {
4145 default:
4146 return 0;
4147
4148 case BINOP_ADD:
4149 case BINOP_SUB:
4150 case BINOP_MUL:
4151 case BINOP_DIV:
d2e4a39e 4152 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
4153
4154 case BINOP_REM:
4155 case BINOP_MOD:
4156 case BINOP_BITWISE_AND:
4157 case BINOP_BITWISE_IOR:
4158 case BINOP_BITWISE_XOR:
d2e4a39e 4159 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4160
4161 case BINOP_EQUAL:
4162 case BINOP_NOTEQUAL:
4163 case BINOP_LESS:
4164 case BINOP_GTR:
4165 case BINOP_LEQ:
4166 case BINOP_GEQ:
d2e4a39e 4167 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
4168
4169 case BINOP_CONCAT:
ee90b9ab 4170 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
14f9c5c9
AS
4171
4172 case BINOP_EXP:
d2e4a39e 4173 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
4174
4175 case UNOP_NEG:
4176 case UNOP_PLUS:
4177 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
4178 case UNOP_ABS:
4179 return (!numeric_type_p (type0));
14f9c5c9
AS
4180
4181 }
4182}
4183\f
dda83cd7 4184 /* Renaming */
14f9c5c9 4185
aeb5907d
JB
4186/* NOTES:
4187
4188 1. In the following, we assume that a renaming type's name may
4189 have an ___XD suffix. It would be nice if this went away at some
4190 point.
4191 2. We handle both the (old) purely type-based representation of
4192 renamings and the (new) variable-based encoding. At some point,
4193 it is devoutly to be hoped that the former goes away
4194 (FIXME: hilfinger-2007-07-09).
4195 3. Subprogram renamings are not implemented, although the XRS
4196 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4197
4198/* If SYM encodes a renaming,
4199
4200 <renaming> renames <renamed entity>,
4201
4202 sets *LEN to the length of the renamed entity's name,
4203 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4204 the string describing the subcomponent selected from the renamed
0963b4bd 4205 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
aeb5907d
JB
4206 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4207 are undefined). Otherwise, returns a value indicating the category
4208 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4209 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4210 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4211 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4212 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4213 may be NULL, in which case they are not assigned.
4214
4215 [Currently, however, GCC does not generate subprogram renamings.] */
4216
4217enum ada_renaming_category
4218ada_parse_renaming (struct symbol *sym,
4219 const char **renamed_entity, int *len,
4220 const char **renaming_expr)
4221{
4222 enum ada_renaming_category kind;
4223 const char *info;
4224 const char *suffix;
4225
4226 if (sym == NULL)
4227 return ADA_NOT_RENAMING;
4228 switch (SYMBOL_CLASS (sym))
14f9c5c9 4229 {
aeb5907d
JB
4230 default:
4231 return ADA_NOT_RENAMING;
aeb5907d
JB
4232 case LOC_LOCAL:
4233 case LOC_STATIC:
4234 case LOC_COMPUTED:
4235 case LOC_OPTIMIZED_OUT:
987012b8 4236 info = strstr (sym->linkage_name (), "___XR");
aeb5907d
JB
4237 if (info == NULL)
4238 return ADA_NOT_RENAMING;
4239 switch (info[5])
4240 {
4241 case '_':
4242 kind = ADA_OBJECT_RENAMING;
4243 info += 6;
4244 break;
4245 case 'E':
4246 kind = ADA_EXCEPTION_RENAMING;
4247 info += 7;
4248 break;
4249 case 'P':
4250 kind = ADA_PACKAGE_RENAMING;
4251 info += 7;
4252 break;
4253 case 'S':
4254 kind = ADA_SUBPROGRAM_RENAMING;
4255 info += 7;
4256 break;
4257 default:
4258 return ADA_NOT_RENAMING;
4259 }
14f9c5c9 4260 }
4c4b4cd2 4261
de93309a
SM
4262 if (renamed_entity != NULL)
4263 *renamed_entity = info;
4264 suffix = strstr (info, "___XE");
4265 if (suffix == NULL || suffix == info)
4266 return ADA_NOT_RENAMING;
4267 if (len != NULL)
4268 *len = strlen (info) - strlen (suffix);
4269 suffix += 5;
4270 if (renaming_expr != NULL)
4271 *renaming_expr = suffix;
4272 return kind;
4273}
4274
4275/* Compute the value of the given RENAMING_SYM, which is expected to
4276 be a symbol encoding a renaming expression. BLOCK is the block
4277 used to evaluate the renaming. */
4278
4279static struct value *
4280ada_read_renaming_var_value (struct symbol *renaming_sym,
4281 const struct block *block)
4282{
4283 const char *sym_name;
4284
987012b8 4285 sym_name = renaming_sym->linkage_name ();
de93309a
SM
4286 expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
4287 return evaluate_expression (expr.get ());
4288}
4289\f
4290
dda83cd7 4291 /* Evaluation: Function Calls */
de93309a
SM
4292
4293/* Return an lvalue containing the value VAL. This is the identity on
4294 lvalues, and otherwise has the side-effect of allocating memory
4295 in the inferior where a copy of the value contents is copied. */
4296
4297static struct value *
4298ensure_lval (struct value *val)
4299{
4300 if (VALUE_LVAL (val) == not_lval
4301 || VALUE_LVAL (val) == lval_internalvar)
4302 {
4303 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
4304 const CORE_ADDR addr =
dda83cd7 4305 value_as_long (value_allocate_space_in_inferior (len));
de93309a
SM
4306
4307 VALUE_LVAL (val) = lval_memory;
4308 set_value_address (val, addr);
4309 write_memory (addr, value_contents (val), len);
4310 }
4311
4312 return val;
4313}
4314
4315/* Given ARG, a value of type (pointer or reference to a)*
4316 structure/union, extract the component named NAME from the ultimate
4317 target structure/union and return it as a value with its
4318 appropriate type.
4319
4320 The routine searches for NAME among all members of the structure itself
4321 and (recursively) among all members of any wrapper members
4322 (e.g., '_parent').
4323
4324 If NO_ERR, then simply return NULL in case of error, rather than
4325 calling error. */
4326
4327static struct value *
4328ada_value_struct_elt (struct value *arg, const char *name, int no_err)
4329{
4330 struct type *t, *t1;
4331 struct value *v;
4332 int check_tag;
4333
4334 v = NULL;
4335 t1 = t = ada_check_typedef (value_type (arg));
78134374 4336 if (t->code () == TYPE_CODE_REF)
de93309a
SM
4337 {
4338 t1 = TYPE_TARGET_TYPE (t);
4339 if (t1 == NULL)
4340 goto BadValue;
4341 t1 = ada_check_typedef (t1);
78134374 4342 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4343 {
4344 arg = coerce_ref (arg);
4345 t = t1;
4346 }
de93309a
SM
4347 }
4348
78134374 4349 while (t->code () == TYPE_CODE_PTR)
de93309a
SM
4350 {
4351 t1 = TYPE_TARGET_TYPE (t);
4352 if (t1 == NULL)
4353 goto BadValue;
4354 t1 = ada_check_typedef (t1);
78134374 4355 if (t1->code () == TYPE_CODE_PTR)
dda83cd7
SM
4356 {
4357 arg = value_ind (arg);
4358 t = t1;
4359 }
de93309a 4360 else
dda83cd7 4361 break;
de93309a 4362 }
aeb5907d 4363
78134374 4364 if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
de93309a 4365 goto BadValue;
52ce6436 4366
de93309a
SM
4367 if (t1 == t)
4368 v = ada_search_struct_field (name, arg, 0, t);
4369 else
4370 {
4371 int bit_offset, bit_size, byte_offset;
4372 struct type *field_type;
4373 CORE_ADDR address;
a5ee536b 4374
78134374 4375 if (t->code () == TYPE_CODE_PTR)
de93309a
SM
4376 address = value_address (ada_value_ind (arg));
4377 else
4378 address = value_address (ada_coerce_ref (arg));
d2e4a39e 4379
de93309a 4380 /* Check to see if this is a tagged type. We also need to handle
dda83cd7
SM
4381 the case where the type is a reference to a tagged type, but
4382 we have to be careful to exclude pointers to tagged types.
4383 The latter should be shown as usual (as a pointer), whereas
4384 a reference should mostly be transparent to the user. */
14f9c5c9 4385
de93309a 4386 if (ada_is_tagged_type (t1, 0)
dda83cd7
SM
4387 || (t1->code () == TYPE_CODE_REF
4388 && ada_is_tagged_type (TYPE_TARGET_TYPE (t1), 0)))
4389 {
4390 /* We first try to find the searched field in the current type.
de93309a 4391 If not found then let's look in the fixed type. */
14f9c5c9 4392
dda83cd7
SM
4393 if (!find_struct_field (name, t1, 0,
4394 &field_type, &byte_offset, &bit_offset,
4395 &bit_size, NULL))
de93309a
SM
4396 check_tag = 1;
4397 else
4398 check_tag = 0;
dda83cd7 4399 }
de93309a
SM
4400 else
4401 check_tag = 0;
c3e5cd34 4402
de93309a
SM
4403 /* Convert to fixed type in all cases, so that we have proper
4404 offsets to each field in unconstrained record types. */
4405 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
4406 address, NULL, check_tag);
4407
24aa1b02
TT
4408 /* Resolve the dynamic type as well. */
4409 arg = value_from_contents_and_address (t1, nullptr, address);
4410 t1 = value_type (arg);
4411
de93309a 4412 if (find_struct_field (name, t1, 0,
dda83cd7
SM
4413 &field_type, &byte_offset, &bit_offset,
4414 &bit_size, NULL))
4415 {
4416 if (bit_size != 0)
4417 {
4418 if (t->code () == TYPE_CODE_REF)
4419 arg = ada_coerce_ref (arg);
4420 else
4421 arg = ada_value_ind (arg);
4422 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
4423 bit_offset, bit_size,
4424 field_type);
4425 }
4426 else
4427 v = value_at_lazy (field_type, address + byte_offset);
4428 }
c3e5cd34 4429 }
14f9c5c9 4430
de93309a
SM
4431 if (v != NULL || no_err)
4432 return v;
4433 else
4434 error (_("There is no member named %s."), name);
4435
4436 BadValue:
4437 if (no_err)
4438 return NULL;
4439 else
4440 error (_("Attempt to extract a component of "
4441 "a value that is not a record."));
14f9c5c9
AS
4442}
4443
4444/* Return the value ACTUAL, converted to be an appropriate value for a
4445 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4446 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 4447 values not residing in memory, updating it as needed. */
14f9c5c9 4448
a93c0eb6 4449struct value *
40bc484c 4450ada_convert_actual (struct value *actual, struct type *formal_type0)
14f9c5c9 4451{
df407dfe 4452 struct type *actual_type = ada_check_typedef (value_type (actual));
61ee279c 4453 struct type *formal_type = ada_check_typedef (formal_type0);
d2e4a39e 4454 struct type *formal_target =
78134374 4455 formal_type->code () == TYPE_CODE_PTR
61ee279c 4456 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
d2e4a39e 4457 struct type *actual_target =
78134374 4458 actual_type->code () == TYPE_CODE_PTR
61ee279c 4459 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 4460
4c4b4cd2 4461 if (ada_is_array_descriptor_type (formal_target)
78134374 4462 && actual_target->code () == TYPE_CODE_ARRAY)
40bc484c 4463 return make_array_descriptor (formal_type, actual);
78134374
SM
4464 else if (formal_type->code () == TYPE_CODE_PTR
4465 || formal_type->code () == TYPE_CODE_REF)
14f9c5c9 4466 {
a84a8a0d 4467 struct value *result;
5b4ee69b 4468
78134374 4469 if (formal_target->code () == TYPE_CODE_ARRAY
dda83cd7 4470 && ada_is_array_descriptor_type (actual_target))
a84a8a0d 4471 result = desc_data (actual);
78134374 4472 else if (formal_type->code () != TYPE_CODE_PTR)
dda83cd7
SM
4473 {
4474 if (VALUE_LVAL (actual) != lval_memory)
4475 {
4476 struct value *val;
4477
4478 actual_type = ada_check_typedef (value_type (actual));
4479 val = allocate_value (actual_type);
4480 memcpy ((char *) value_contents_raw (val),
4481 (char *) value_contents (actual),
4482 TYPE_LENGTH (actual_type));
4483 actual = ensure_lval (val);
4484 }
4485 result = value_addr (actual);
4486 }
a84a8a0d
JB
4487 else
4488 return actual;
b1af9e97 4489 return value_cast_pointers (formal_type, result, 0);
14f9c5c9 4490 }
78134374 4491 else if (actual_type->code () == TYPE_CODE_PTR)
14f9c5c9 4492 return ada_value_ind (actual);
8344af1e
JB
4493 else if (ada_is_aligner_type (formal_type))
4494 {
4495 /* We need to turn this parameter into an aligner type
4496 as well. */
4497 struct value *aligner = allocate_value (formal_type);
4498 struct value *component = ada_value_struct_elt (aligner, "F", 0);
4499
4500 value_assign_to_component (aligner, component, actual);
4501 return aligner;
4502 }
14f9c5c9
AS
4503
4504 return actual;
4505}
4506
438c98a1
JB
4507/* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4508 type TYPE. This is usually an inefficient no-op except on some targets
4509 (such as AVR) where the representation of a pointer and an address
4510 differs. */
4511
4512static CORE_ADDR
4513value_pointer (struct value *value, struct type *type)
4514{
438c98a1 4515 unsigned len = TYPE_LENGTH (type);
224c3ddb 4516 gdb_byte *buf = (gdb_byte *) alloca (len);
438c98a1
JB
4517 CORE_ADDR addr;
4518
4519 addr = value_address (value);
8ee511af 4520 gdbarch_address_to_pointer (type->arch (), type, buf, addr);
34877895 4521 addr = extract_unsigned_integer (buf, len, type_byte_order (type));
438c98a1
JB
4522 return addr;
4523}
4524
14f9c5c9 4525
4c4b4cd2
PH
4526/* Push a descriptor of type TYPE for array value ARR on the stack at
4527 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 4528 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
4529 to-descriptor type rather than a descriptor type), a struct value *
4530 representing a pointer to this descriptor. */
14f9c5c9 4531
d2e4a39e 4532static struct value *
40bc484c 4533make_array_descriptor (struct type *type, struct value *arr)
14f9c5c9 4534{
d2e4a39e
AS
4535 struct type *bounds_type = desc_bounds_type (type);
4536 struct type *desc_type = desc_base_type (type);
4537 struct value *descriptor = allocate_value (desc_type);
4538 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 4539 int i;
d2e4a39e 4540
0963b4bd
MS
4541 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4542 i > 0; i -= 1)
14f9c5c9 4543 {
19f220c3
JK
4544 modify_field (value_type (bounds), value_contents_writeable (bounds),
4545 ada_array_bound (arr, i, 0),
4546 desc_bound_bitpos (bounds_type, i, 0),
4547 desc_bound_bitsize (bounds_type, i, 0));
4548 modify_field (value_type (bounds), value_contents_writeable (bounds),
4549 ada_array_bound (arr, i, 1),
4550 desc_bound_bitpos (bounds_type, i, 1),
4551 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 4552 }
d2e4a39e 4553
40bc484c 4554 bounds = ensure_lval (bounds);
d2e4a39e 4555
19f220c3
JK
4556 modify_field (value_type (descriptor),
4557 value_contents_writeable (descriptor),
4558 value_pointer (ensure_lval (arr),
940da03e 4559 desc_type->field (0).type ()),
19f220c3
JK
4560 fat_pntr_data_bitpos (desc_type),
4561 fat_pntr_data_bitsize (desc_type));
4562
4563 modify_field (value_type (descriptor),
4564 value_contents_writeable (descriptor),
4565 value_pointer (bounds,
940da03e 4566 desc_type->field (1).type ()),
19f220c3
JK
4567 fat_pntr_bounds_bitpos (desc_type),
4568 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 4569
40bc484c 4570 descriptor = ensure_lval (descriptor);
14f9c5c9 4571
78134374 4572 if (type->code () == TYPE_CODE_PTR)
14f9c5c9
AS
4573 return value_addr (descriptor);
4574 else
4575 return descriptor;
4576}
14f9c5c9 4577\f
dda83cd7 4578 /* Symbol Cache Module */
3d9434b5 4579
3d9434b5 4580/* Performance measurements made as of 2010-01-15 indicate that
ee01b665 4581 this cache does bring some noticeable improvements. Depending
3d9434b5
JB
4582 on the type of entity being printed, the cache can make it as much
4583 as an order of magnitude faster than without it.
4584
4585 The descriptive type DWARF extension has significantly reduced
4586 the need for this cache, at least when DWARF is being used. However,
4587 even in this case, some expensive name-based symbol searches are still
4588 sometimes necessary - to find an XVZ variable, mostly. */
4589
ee01b665
JB
4590/* Return the symbol cache associated to the given program space PSPACE.
4591 If not allocated for this PSPACE yet, allocate and initialize one. */
3d9434b5 4592
ee01b665
JB
4593static struct ada_symbol_cache *
4594ada_get_symbol_cache (struct program_space *pspace)
4595{
4596 struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
ee01b665 4597
bdcccc56
TT
4598 if (pspace_data->sym_cache == nullptr)
4599 pspace_data->sym_cache.reset (new ada_symbol_cache);
ee01b665 4600
bdcccc56 4601 return pspace_data->sym_cache.get ();
ee01b665 4602}
3d9434b5
JB
4603
4604/* Clear all entries from the symbol cache. */
4605
4606static void
bdcccc56 4607ada_clear_symbol_cache ()
3d9434b5 4608{
bdcccc56
TT
4609 struct ada_pspace_data *pspace_data
4610 = get_ada_pspace_data (current_program_space);
ee01b665 4611
bdcccc56
TT
4612 if (pspace_data->sym_cache != nullptr)
4613 pspace_data->sym_cache.reset ();
3d9434b5
JB
4614}
4615
fe978cb0 4616/* Search our cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4617 Return it if found, or NULL otherwise. */
4618
4619static struct cache_entry **
fe978cb0 4620find_entry (const char *name, domain_enum domain)
3d9434b5 4621{
ee01b665
JB
4622 struct ada_symbol_cache *sym_cache
4623 = ada_get_symbol_cache (current_program_space);
3d9434b5
JB
4624 int h = msymbol_hash (name) % HASH_SIZE;
4625 struct cache_entry **e;
4626
ee01b665 4627 for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
3d9434b5 4628 {
fe978cb0 4629 if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
dda83cd7 4630 return e;
3d9434b5
JB
4631 }
4632 return NULL;
4633}
4634
fe978cb0 4635/* Search the symbol cache for an entry matching NAME and DOMAIN.
3d9434b5
JB
4636 Return 1 if found, 0 otherwise.
4637
4638 If an entry was found and SYM is not NULL, set *SYM to the entry's
4639 SYM. Same principle for BLOCK if not NULL. */
96d887e8 4640
96d887e8 4641static int
fe978cb0 4642lookup_cached_symbol (const char *name, domain_enum domain,
dda83cd7 4643 struct symbol **sym, const struct block **block)
96d887e8 4644{
fe978cb0 4645 struct cache_entry **e = find_entry (name, domain);
3d9434b5
JB
4646
4647 if (e == NULL)
4648 return 0;
4649 if (sym != NULL)
4650 *sym = (*e)->sym;
4651 if (block != NULL)
4652 *block = (*e)->block;
4653 return 1;
96d887e8
PH
4654}
4655
3d9434b5 4656/* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
fe978cb0 4657 in domain DOMAIN, save this result in our symbol cache. */
3d9434b5 4658
96d887e8 4659static void
fe978cb0 4660cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
dda83cd7 4661 const struct block *block)
96d887e8 4662{
ee01b665
JB
4663 struct ada_symbol_cache *sym_cache
4664 = ada_get_symbol_cache (current_program_space);
3d9434b5 4665 int h;
3d9434b5
JB
4666 struct cache_entry *e;
4667
1994afbf
DE
4668 /* Symbols for builtin types don't have a block.
4669 For now don't cache such symbols. */
4670 if (sym != NULL && !SYMBOL_OBJFILE_OWNED (sym))
4671 return;
4672
3d9434b5
JB
4673 /* If the symbol is a local symbol, then do not cache it, as a search
4674 for that symbol depends on the context. To determine whether
4675 the symbol is local or not, we check the block where we found it
4676 against the global and static blocks of its associated symtab. */
4677 if (sym
08be3fe3 4678 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4679 GLOBAL_BLOCK) != block
08be3fe3 4680 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym)),
439247b6 4681 STATIC_BLOCK) != block)
3d9434b5
JB
4682 return;
4683
4684 h = msymbol_hash (name) % HASH_SIZE;
e39db4db 4685 e = XOBNEW (&sym_cache->cache_space, cache_entry);
ee01b665
JB
4686 e->next = sym_cache->root[h];
4687 sym_cache->root[h] = e;
2ef5453b 4688 e->name = obstack_strdup (&sym_cache->cache_space, name);
3d9434b5 4689 e->sym = sym;
fe978cb0 4690 e->domain = domain;
3d9434b5 4691 e->block = block;
96d887e8 4692}
4c4b4cd2 4693\f
dda83cd7 4694 /* Symbol Lookup */
4c4b4cd2 4695
b5ec771e
PA
4696/* Return the symbol name match type that should be used used when
4697 searching for all symbols matching LOOKUP_NAME.
c0431670
JB
4698
4699 LOOKUP_NAME is expected to be a symbol name after transformation
f98b2e33 4700 for Ada lookups. */
c0431670 4701
b5ec771e
PA
4702static symbol_name_match_type
4703name_match_type_from_name (const char *lookup_name)
c0431670 4704{
b5ec771e
PA
4705 return (strstr (lookup_name, "__") == NULL
4706 ? symbol_name_match_type::WILD
4707 : symbol_name_match_type::FULL);
c0431670
JB
4708}
4709
4c4b4cd2
PH
4710/* Return the result of a standard (literal, C-like) lookup of NAME in
4711 given DOMAIN, visible from lexical block BLOCK. */
4712
4713static struct symbol *
4714standard_lookup (const char *name, const struct block *block,
dda83cd7 4715 domain_enum domain)
4c4b4cd2 4716{
acbd605d 4717 /* Initialize it just to avoid a GCC false warning. */
6640a367 4718 struct block_symbol sym = {};
4c4b4cd2 4719
d12307c1
PMR
4720 if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
4721 return sym.symbol;
a2cd4f14 4722 ada_lookup_encoded_symbol (name, block, domain, &sym);
d12307c1
PMR
4723 cache_symbol (name, domain, sym.symbol, sym.block);
4724 return sym.symbol;
4c4b4cd2
PH
4725}
4726
4727
4728/* Non-zero iff there is at least one non-function/non-enumeral symbol
1bfa81ac 4729 in the symbol fields of SYMS. We treat enumerals as functions,
4c4b4cd2
PH
4730 since they contend in overloading in the same way. */
4731static int
d1183b06 4732is_nonfunction (const std::vector<struct block_symbol> &syms)
4c4b4cd2 4733{
d1183b06
TT
4734 for (const block_symbol &sym : syms)
4735 if (SYMBOL_TYPE (sym.symbol)->code () != TYPE_CODE_FUNC
4736 && (SYMBOL_TYPE (sym.symbol)->code () != TYPE_CODE_ENUM
4737 || SYMBOL_CLASS (sym.symbol) != LOC_CONST))
14f9c5c9
AS
4738 return 1;
4739
4740 return 0;
4741}
4742
4743/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 4744 struct types. Otherwise, they may not. */
14f9c5c9
AS
4745
4746static int
d2e4a39e 4747equiv_types (struct type *type0, struct type *type1)
14f9c5c9 4748{
d2e4a39e 4749 if (type0 == type1)
14f9c5c9 4750 return 1;
d2e4a39e 4751 if (type0 == NULL || type1 == NULL
78134374 4752 || type0->code () != type1->code ())
14f9c5c9 4753 return 0;
78134374
SM
4754 if ((type0->code () == TYPE_CODE_STRUCT
4755 || type0->code () == TYPE_CODE_ENUM)
14f9c5c9 4756 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 4757 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 4758 return 1;
d2e4a39e 4759
14f9c5c9
AS
4760 return 0;
4761}
4762
4763/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 4764 no more defined than that of SYM1. */
14f9c5c9
AS
4765
4766static int
d2e4a39e 4767lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
4768{
4769 if (sym0 == sym1)
4770 return 1;
176620f1 4771 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
4772 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4773 return 0;
4774
d2e4a39e 4775 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
4776 {
4777 case LOC_UNDEF:
4778 return 1;
4779 case LOC_TYPEDEF:
4780 {
dda83cd7
SM
4781 struct type *type0 = SYMBOL_TYPE (sym0);
4782 struct type *type1 = SYMBOL_TYPE (sym1);
4783 const char *name0 = sym0->linkage_name ();
4784 const char *name1 = sym1->linkage_name ();
4785 int len0 = strlen (name0);
4786
4787 return
4788 type0->code () == type1->code ()
4789 && (equiv_types (type0, type1)
4790 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4791 && startswith (name1 + len0, "___XV")));
14f9c5c9
AS
4792 }
4793 case LOC_CONST:
4794 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
dda83cd7 4795 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
4b610737
TT
4796
4797 case LOC_STATIC:
4798 {
dda83cd7
SM
4799 const char *name0 = sym0->linkage_name ();
4800 const char *name1 = sym1->linkage_name ();
4801 return (strcmp (name0, name1) == 0
4802 && SYMBOL_VALUE_ADDRESS (sym0) == SYMBOL_VALUE_ADDRESS (sym1));
4b610737
TT
4803 }
4804
d2e4a39e
AS
4805 default:
4806 return 0;
14f9c5c9
AS
4807 }
4808}
4809
d1183b06
TT
4810/* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4811 records in RESULT. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
4812
4813static void
d1183b06 4814add_defn_to_vec (std::vector<struct block_symbol> &result,
dda83cd7
SM
4815 struct symbol *sym,
4816 const struct block *block)
14f9c5c9 4817{
529cad9c
PH
4818 /* Do not try to complete stub types, as the debugger is probably
4819 already scanning all symbols matching a certain name at the
4820 time when this function is called. Trying to replace the stub
4821 type by its associated full type will cause us to restart a scan
4822 which may lead to an infinite recursion. Instead, the client
4823 collecting the matching symbols will end up collecting several
4824 matches, with at least one of them complete. It can then filter
4825 out the stub ones if needed. */
4826
d1183b06 4827 for (int i = result.size () - 1; i >= 0; i -= 1)
4c4b4cd2 4828 {
d1183b06 4829 if (lesseq_defined_than (sym, result[i].symbol))
dda83cd7 4830 return;
d1183b06 4831 else if (lesseq_defined_than (result[i].symbol, sym))
dda83cd7 4832 {
d1183b06
TT
4833 result[i].symbol = sym;
4834 result[i].block = block;
dda83cd7
SM
4835 return;
4836 }
4c4b4cd2
PH
4837 }
4838
d1183b06
TT
4839 struct block_symbol info;
4840 info.symbol = sym;
4841 info.block = block;
4842 result.push_back (info);
4c4b4cd2
PH
4843}
4844
7c7b6655
TT
4845/* Return a bound minimal symbol matching NAME according to Ada
4846 decoding rules. Returns an invalid symbol if there is no such
4847 minimal symbol. Names prefixed with "standard__" are handled
4848 specially: "standard__" is first stripped off, and only static and
4849 global symbols are searched. */
4c4b4cd2 4850
7c7b6655 4851struct bound_minimal_symbol
96d887e8 4852ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4853{
7c7b6655 4854 struct bound_minimal_symbol result;
4c4b4cd2 4855
7c7b6655
TT
4856 memset (&result, 0, sizeof (result));
4857
b5ec771e
PA
4858 symbol_name_match_type match_type = name_match_type_from_name (name);
4859 lookup_name_info lookup_name (name, match_type);
4860
4861 symbol_name_matcher_ftype *match_name
4862 = ada_get_symbol_name_matcher (lookup_name);
4c4b4cd2 4863
2030c079 4864 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4865 {
7932255d 4866 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf 4867 {
c9d95fa3 4868 if (match_name (msymbol->linkage_name (), lookup_name, NULL)
5325b9bf
TT
4869 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4870 {
4871 result.minsym = msymbol;
4872 result.objfile = objfile;
4873 break;
4874 }
4875 }
4876 }
4c4b4cd2 4877
7c7b6655 4878 return result;
96d887e8 4879}
4c4b4cd2 4880
96d887e8
PH
4881/* For all subprograms that statically enclose the subprogram of the
4882 selected frame, add symbols matching identifier NAME in DOMAIN
1bfa81ac 4883 and their blocks to the list of data in RESULT, as for
48b78332
JB
4884 ada_add_block_symbols (q.v.). If WILD_MATCH_P, treat as NAME
4885 with a wildcard prefix. */
4c4b4cd2 4886
96d887e8 4887static void
d1183b06 4888add_symbols_from_enclosing_procs (std::vector<struct block_symbol> &result,
b5ec771e
PA
4889 const lookup_name_info &lookup_name,
4890 domain_enum domain)
96d887e8 4891{
96d887e8 4892}
14f9c5c9 4893
96d887e8
PH
4894/* True if TYPE is definitely an artificial type supplied to a symbol
4895 for which no debugging information was given in the symbol file. */
14f9c5c9 4896
96d887e8
PH
4897static int
4898is_nondebugging_type (struct type *type)
4899{
0d5cff50 4900 const char *name = ada_type_name (type);
5b4ee69b 4901
96d887e8
PH
4902 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4903}
4c4b4cd2 4904
8f17729f
JB
4905/* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4906 that are deemed "identical" for practical purposes.
4907
4908 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4909 types and that their number of enumerals is identical (in other
1f704f76 4910 words, type1->num_fields () == type2->num_fields ()). */
8f17729f
JB
4911
4912static int
4913ada_identical_enum_types_p (struct type *type1, struct type *type2)
4914{
4915 int i;
4916
4917 /* The heuristic we use here is fairly conservative. We consider
4918 that 2 enumerate types are identical if they have the same
4919 number of enumerals and that all enumerals have the same
4920 underlying value and name. */
4921
4922 /* All enums in the type should have an identical underlying value. */
1f704f76 4923 for (i = 0; i < type1->num_fields (); i++)
14e75d8e 4924 if (TYPE_FIELD_ENUMVAL (type1, i) != TYPE_FIELD_ENUMVAL (type2, i))
8f17729f
JB
4925 return 0;
4926
4927 /* All enumerals should also have the same name (modulo any numerical
4928 suffix). */
1f704f76 4929 for (i = 0; i < type1->num_fields (); i++)
8f17729f 4930 {
0d5cff50
DE
4931 const char *name_1 = TYPE_FIELD_NAME (type1, i);
4932 const char *name_2 = TYPE_FIELD_NAME (type2, i);
8f17729f
JB
4933 int len_1 = strlen (name_1);
4934 int len_2 = strlen (name_2);
4935
4936 ada_remove_trailing_digits (TYPE_FIELD_NAME (type1, i), &len_1);
4937 ada_remove_trailing_digits (TYPE_FIELD_NAME (type2, i), &len_2);
4938 if (len_1 != len_2
dda83cd7 4939 || strncmp (TYPE_FIELD_NAME (type1, i),
8f17729f
JB
4940 TYPE_FIELD_NAME (type2, i),
4941 len_1) != 0)
4942 return 0;
4943 }
4944
4945 return 1;
4946}
4947
4948/* Return nonzero if all the symbols in SYMS are all enumeral symbols
4949 that are deemed "identical" for practical purposes. Sometimes,
4950 enumerals are not strictly identical, but their types are so similar
4951 that they can be considered identical.
4952
4953 For instance, consider the following code:
4954
4955 type Color is (Black, Red, Green, Blue, White);
4956 type RGB_Color is new Color range Red .. Blue;
4957
4958 Type RGB_Color is a subrange of an implicit type which is a copy
4959 of type Color. If we call that implicit type RGB_ColorB ("B" is
4960 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4961 As a result, when an expression references any of the enumeral
4962 by name (Eg. "print green"), the expression is technically
4963 ambiguous and the user should be asked to disambiguate. But
4964 doing so would only hinder the user, since it wouldn't matter
4965 what choice he makes, the outcome would always be the same.
4966 So, for practical purposes, we consider them as the same. */
4967
4968static int
54d343a2 4969symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
8f17729f
JB
4970{
4971 int i;
4972
4973 /* Before performing a thorough comparison check of each type,
4974 we perform a series of inexpensive checks. We expect that these
4975 checks will quickly fail in the vast majority of cases, and thus
4976 help prevent the unnecessary use of a more expensive comparison.
4977 Said comparison also expects us to make some of these checks
4978 (see ada_identical_enum_types_p). */
4979
4980 /* Quick check: All symbols should have an enum type. */
54d343a2 4981 for (i = 0; i < syms.size (); i++)
78134374 4982 if (SYMBOL_TYPE (syms[i].symbol)->code () != TYPE_CODE_ENUM)
8f17729f
JB
4983 return 0;
4984
4985 /* Quick check: They should all have the same value. */
54d343a2 4986 for (i = 1; i < syms.size (); i++)
d12307c1 4987 if (SYMBOL_VALUE (syms[i].symbol) != SYMBOL_VALUE (syms[0].symbol))
8f17729f
JB
4988 return 0;
4989
4990 /* Quick check: They should all have the same number of enumerals. */
54d343a2 4991 for (i = 1; i < syms.size (); i++)
1f704f76 4992 if (SYMBOL_TYPE (syms[i].symbol)->num_fields ()
dda83cd7 4993 != SYMBOL_TYPE (syms[0].symbol)->num_fields ())
8f17729f
JB
4994 return 0;
4995
4996 /* All the sanity checks passed, so we might have a set of
4997 identical enumeration types. Perform a more complete
4998 comparison of the type of each symbol. */
54d343a2 4999 for (i = 1; i < syms.size (); i++)
d12307c1 5000 if (!ada_identical_enum_types_p (SYMBOL_TYPE (syms[i].symbol),
dda83cd7 5001 SYMBOL_TYPE (syms[0].symbol)))
8f17729f
JB
5002 return 0;
5003
5004 return 1;
5005}
5006
54d343a2 5007/* Remove any non-debugging symbols in SYMS that definitely
96d887e8
PH
5008 duplicate other symbols in the list (The only case I know of where
5009 this happens is when object files containing stabs-in-ecoff are
5010 linked with files containing ordinary ecoff debugging symbols (or no
1bfa81ac 5011 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
4c4b4cd2 5012
d1183b06 5013static void
54d343a2 5014remove_extra_symbols (std::vector<struct block_symbol> *syms)
96d887e8
PH
5015{
5016 int i, j;
4c4b4cd2 5017
8f17729f
JB
5018 /* We should never be called with less than 2 symbols, as there
5019 cannot be any extra symbol in that case. But it's easy to
5020 handle, since we have nothing to do in that case. */
54d343a2 5021 if (syms->size () < 2)
d1183b06 5022 return;
8f17729f 5023
96d887e8 5024 i = 0;
54d343a2 5025 while (i < syms->size ())
96d887e8 5026 {
a35ddb44 5027 int remove_p = 0;
339c13b6
JB
5028
5029 /* If two symbols have the same name and one of them is a stub type,
dda83cd7 5030 the get rid of the stub. */
339c13b6 5031
e46d3488 5032 if (SYMBOL_TYPE ((*syms)[i].symbol)->is_stub ()
dda83cd7
SM
5033 && (*syms)[i].symbol->linkage_name () != NULL)
5034 {
5035 for (j = 0; j < syms->size (); j++)
5036 {
5037 if (j != i
5038 && !SYMBOL_TYPE ((*syms)[j].symbol)->is_stub ()
5039 && (*syms)[j].symbol->linkage_name () != NULL
5040 && strcmp ((*syms)[i].symbol->linkage_name (),
5041 (*syms)[j].symbol->linkage_name ()) == 0)
5042 remove_p = 1;
5043 }
5044 }
339c13b6
JB
5045
5046 /* Two symbols with the same name, same class and same address
dda83cd7 5047 should be identical. */
339c13b6 5048
987012b8 5049 else if ((*syms)[i].symbol->linkage_name () != NULL
dda83cd7
SM
5050 && SYMBOL_CLASS ((*syms)[i].symbol) == LOC_STATIC
5051 && is_nondebugging_type (SYMBOL_TYPE ((*syms)[i].symbol)))
5052 {
5053 for (j = 0; j < syms->size (); j += 1)
5054 {
5055 if (i != j
5056 && (*syms)[j].symbol->linkage_name () != NULL
5057 && strcmp ((*syms)[i].symbol->linkage_name (),
5058 (*syms)[j].symbol->linkage_name ()) == 0
5059 && SYMBOL_CLASS ((*syms)[i].symbol)
54d343a2 5060 == SYMBOL_CLASS ((*syms)[j].symbol)
dda83cd7
SM
5061 && SYMBOL_VALUE_ADDRESS ((*syms)[i].symbol)
5062 == SYMBOL_VALUE_ADDRESS ((*syms)[j].symbol))
5063 remove_p = 1;
5064 }
5065 }
339c13b6 5066
a35ddb44 5067 if (remove_p)
54d343a2 5068 syms->erase (syms->begin () + i);
1b788fb6
TT
5069 else
5070 i += 1;
14f9c5c9 5071 }
8f17729f
JB
5072
5073 /* If all the remaining symbols are identical enumerals, then
5074 just keep the first one and discard the rest.
5075
5076 Unlike what we did previously, we do not discard any entry
5077 unless they are ALL identical. This is because the symbol
5078 comparison is not a strict comparison, but rather a practical
5079 comparison. If all symbols are considered identical, then
5080 we can just go ahead and use the first one and discard the rest.
5081 But if we cannot reduce the list to a single element, we have
5082 to ask the user to disambiguate anyways. And if we have to
5083 present a multiple-choice menu, it's less confusing if the list
5084 isn't missing some choices that were identical and yet distinct. */
54d343a2
TT
5085 if (symbols_are_identical_enums (*syms))
5086 syms->resize (1);
14f9c5c9
AS
5087}
5088
96d887e8
PH
5089/* Given a type that corresponds to a renaming entity, use the type name
5090 to extract the scope (package name or function name, fully qualified,
5091 and following the GNAT encoding convention) where this renaming has been
49d83361 5092 defined. */
4c4b4cd2 5093
49d83361 5094static std::string
96d887e8 5095xget_renaming_scope (struct type *renaming_type)
14f9c5c9 5096{
96d887e8 5097 /* The renaming types adhere to the following convention:
0963b4bd 5098 <scope>__<rename>___<XR extension>.
96d887e8
PH
5099 So, to extract the scope, we search for the "___XR" extension,
5100 and then backtrack until we find the first "__". */
76a01679 5101
7d93a1e0 5102 const char *name = renaming_type->name ();
108d56a4
SM
5103 const char *suffix = strstr (name, "___XR");
5104 const char *last;
14f9c5c9 5105
96d887e8
PH
5106 /* Now, backtrack a bit until we find the first "__". Start looking
5107 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 5108
96d887e8
PH
5109 for (last = suffix - 3; last > name; last--)
5110 if (last[0] == '_' && last[1] == '_')
5111 break;
76a01679 5112
96d887e8 5113 /* Make a copy of scope and return it. */
49d83361 5114 return std::string (name, last);
4c4b4cd2
PH
5115}
5116
96d887e8 5117/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 5118
96d887e8
PH
5119static int
5120is_package_name (const char *name)
4c4b4cd2 5121{
96d887e8
PH
5122 /* Here, We take advantage of the fact that no symbols are generated
5123 for packages, while symbols are generated for each function.
5124 So the condition for NAME represent a package becomes equivalent
5125 to NAME not existing in our list of symbols. There is only one
5126 small complication with library-level functions (see below). */
4c4b4cd2 5127
96d887e8
PH
5128 /* If it is a function that has not been defined at library level,
5129 then we should be able to look it up in the symbols. */
5130 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
5131 return 0;
14f9c5c9 5132
96d887e8
PH
5133 /* Library-level function names start with "_ada_". See if function
5134 "_ada_" followed by NAME can be found. */
14f9c5c9 5135
96d887e8 5136 /* Do a quick check that NAME does not contain "__", since library-level
e1d5a0d2 5137 functions names cannot contain "__" in them. */
96d887e8
PH
5138 if (strstr (name, "__") != NULL)
5139 return 0;
4c4b4cd2 5140
528e1572 5141 std::string fun_name = string_printf ("_ada_%s", name);
14f9c5c9 5142
528e1572 5143 return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
96d887e8 5144}
14f9c5c9 5145
96d887e8 5146/* Return nonzero if SYM corresponds to a renaming entity that is
aeb5907d 5147 not visible from FUNCTION_NAME. */
14f9c5c9 5148
96d887e8 5149static int
0d5cff50 5150old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
96d887e8 5151{
aeb5907d
JB
5152 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
5153 return 0;
5154
49d83361 5155 std::string scope = xget_renaming_scope (SYMBOL_TYPE (sym));
14f9c5c9 5156
96d887e8 5157 /* If the rename has been defined in a package, then it is visible. */
49d83361
TT
5158 if (is_package_name (scope.c_str ()))
5159 return 0;
14f9c5c9 5160
96d887e8
PH
5161 /* Check that the rename is in the current function scope by checking
5162 that its name starts with SCOPE. */
76a01679 5163
96d887e8
PH
5164 /* If the function name starts with "_ada_", it means that it is
5165 a library-level function. Strip this prefix before doing the
5166 comparison, as the encoding for the renaming does not contain
5167 this prefix. */
61012eef 5168 if (startswith (function_name, "_ada_"))
96d887e8 5169 function_name += 5;
f26caa11 5170
49d83361 5171 return !startswith (function_name, scope.c_str ());
f26caa11
PH
5172}
5173
aeb5907d
JB
5174/* Remove entries from SYMS that corresponds to a renaming entity that
5175 is not visible from the function associated with CURRENT_BLOCK or
5176 that is superfluous due to the presence of more specific renaming
5177 information. Places surviving symbols in the initial entries of
d1183b06
TT
5178 SYMS.
5179
96d887e8 5180 Rationale:
aeb5907d
JB
5181 First, in cases where an object renaming is implemented as a
5182 reference variable, GNAT may produce both the actual reference
5183 variable and the renaming encoding. In this case, we discard the
5184 latter.
5185
5186 Second, GNAT emits a type following a specified encoding for each renaming
96d887e8
PH
5187 entity. Unfortunately, STABS currently does not support the definition
5188 of types that are local to a given lexical block, so all renamings types
5189 are emitted at library level. As a consequence, if an application
5190 contains two renaming entities using the same name, and a user tries to
5191 print the value of one of these entities, the result of the ada symbol
5192 lookup will also contain the wrong renaming type.
f26caa11 5193
96d887e8
PH
5194 This function partially covers for this limitation by attempting to
5195 remove from the SYMS list renaming symbols that should be visible
5196 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5197 method with the current information available. The implementation
5198 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5199
5200 - When the user tries to print a rename in a function while there
dda83cd7
SM
5201 is another rename entity defined in a package: Normally, the
5202 rename in the function has precedence over the rename in the
5203 package, so the latter should be removed from the list. This is
5204 currently not the case.
5205
96d887e8 5206 - This function will incorrectly remove valid renames if
dda83cd7
SM
5207 the CURRENT_BLOCK corresponds to a function which symbol name
5208 has been changed by an "Export" pragma. As a consequence,
5209 the user will be unable to print such rename entities. */
4c4b4cd2 5210
d1183b06 5211static void
54d343a2
TT
5212remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
5213 const struct block *current_block)
4c4b4cd2
PH
5214{
5215 struct symbol *current_function;
0d5cff50 5216 const char *current_function_name;
4c4b4cd2 5217 int i;
aeb5907d
JB
5218 int is_new_style_renaming;
5219
5220 /* If there is both a renaming foo___XR... encoded as a variable and
5221 a simple variable foo in the same block, discard the latter.
0963b4bd 5222 First, zero out such symbols, then compress. */
aeb5907d 5223 is_new_style_renaming = 0;
54d343a2 5224 for (i = 0; i < syms->size (); i += 1)
aeb5907d 5225 {
54d343a2
TT
5226 struct symbol *sym = (*syms)[i].symbol;
5227 const struct block *block = (*syms)[i].block;
aeb5907d
JB
5228 const char *name;
5229 const char *suffix;
5230
5231 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
5232 continue;
987012b8 5233 name = sym->linkage_name ();
aeb5907d
JB
5234 suffix = strstr (name, "___XR");
5235
5236 if (suffix != NULL)
5237 {
5238 int name_len = suffix - name;
5239 int j;
5b4ee69b 5240
aeb5907d 5241 is_new_style_renaming = 1;
54d343a2
TT
5242 for (j = 0; j < syms->size (); j += 1)
5243 if (i != j && (*syms)[j].symbol != NULL
987012b8 5244 && strncmp (name, (*syms)[j].symbol->linkage_name (),
aeb5907d 5245 name_len) == 0
54d343a2
TT
5246 && block == (*syms)[j].block)
5247 (*syms)[j].symbol = NULL;
aeb5907d
JB
5248 }
5249 }
5250 if (is_new_style_renaming)
5251 {
5252 int j, k;
5253
54d343a2
TT
5254 for (j = k = 0; j < syms->size (); j += 1)
5255 if ((*syms)[j].symbol != NULL)
aeb5907d 5256 {
54d343a2 5257 (*syms)[k] = (*syms)[j];
aeb5907d
JB
5258 k += 1;
5259 }
d1183b06
TT
5260 syms->resize (k);
5261 return;
aeb5907d 5262 }
4c4b4cd2
PH
5263
5264 /* Extract the function name associated to CURRENT_BLOCK.
5265 Abort if unable to do so. */
76a01679 5266
4c4b4cd2 5267 if (current_block == NULL)
d1183b06 5268 return;
76a01679 5269
7f0df278 5270 current_function = block_linkage_function (current_block);
4c4b4cd2 5271 if (current_function == NULL)
d1183b06 5272 return;
4c4b4cd2 5273
987012b8 5274 current_function_name = current_function->linkage_name ();
4c4b4cd2 5275 if (current_function_name == NULL)
d1183b06 5276 return;
4c4b4cd2
PH
5277
5278 /* Check each of the symbols, and remove it from the list if it is
5279 a type corresponding to a renaming that is out of the scope of
5280 the current block. */
5281
5282 i = 0;
54d343a2 5283 while (i < syms->size ())
4c4b4cd2 5284 {
54d343a2 5285 if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
dda83cd7
SM
5286 == ADA_OBJECT_RENAMING
5287 && old_renaming_is_invisible ((*syms)[i].symbol,
54d343a2
TT
5288 current_function_name))
5289 syms->erase (syms->begin () + i);
4c4b4cd2 5290 else
dda83cd7 5291 i += 1;
4c4b4cd2 5292 }
4c4b4cd2
PH
5293}
5294
d1183b06 5295/* Add to RESULT all symbols from BLOCK (and its super-blocks)
339c13b6
JB
5296 whose name and domain match NAME and DOMAIN respectively.
5297 If no match was found, then extend the search to "enclosing"
5298 routines (in other words, if we're inside a nested function,
5299 search the symbols defined inside the enclosing functions).
d0a8ab18
JB
5300 If WILD_MATCH_P is nonzero, perform the naming matching in
5301 "wild" mode (see function "wild_match" for more info).
339c13b6 5302
d1183b06 5303 Note: This function assumes that RESULT has 0 (zero) element in it. */
339c13b6
JB
5304
5305static void
d1183b06 5306ada_add_local_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5307 const lookup_name_info &lookup_name,
5308 const struct block *block, domain_enum domain)
339c13b6
JB
5309{
5310 int block_depth = 0;
5311
5312 while (block != NULL)
5313 {
5314 block_depth += 1;
d1183b06 5315 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
339c13b6
JB
5316
5317 /* If we found a non-function match, assume that's the one. */
d1183b06 5318 if (is_nonfunction (result))
dda83cd7 5319 return;
339c13b6
JB
5320
5321 block = BLOCK_SUPERBLOCK (block);
5322 }
5323
5324 /* If no luck so far, try to find NAME as a local symbol in some lexically
5325 enclosing subprogram. */
d1183b06
TT
5326 if (result.empty () && block_depth > 2)
5327 add_symbols_from_enclosing_procs (result, lookup_name, domain);
339c13b6
JB
5328}
5329
ccefe4c4 5330/* An object of this type is used as the user_data argument when
40658b94 5331 calling the map_matching_symbols method. */
ccefe4c4 5332
40658b94 5333struct match_data
ccefe4c4 5334{
1bfa81ac
TT
5335 explicit match_data (std::vector<struct block_symbol> *rp)
5336 : resultp (rp)
5337 {
5338 }
5339 DISABLE_COPY_AND_ASSIGN (match_data);
5340
5341 struct objfile *objfile = nullptr;
d1183b06 5342 std::vector<struct block_symbol> *resultp;
1bfa81ac 5343 struct symbol *arg_sym = nullptr;
1178743e 5344 bool found_sym = false;
ccefe4c4
TT
5345};
5346
199b4314
TT
5347/* A callback for add_nonlocal_symbols that adds symbol, found in BSYM,
5348 to a list of symbols. DATA is a pointer to a struct match_data *
1bfa81ac 5349 containing the vector that collects the symbol list, the file that SYM
40658b94
PH
5350 must come from, a flag indicating whether a non-argument symbol has
5351 been found in the current block, and the last argument symbol
5352 passed in SYM within the current block (if any). When SYM is null,
5353 marking the end of a block, the argument symbol is added if no
5354 other has been found. */
ccefe4c4 5355
199b4314
TT
5356static bool
5357aux_add_nonlocal_symbols (struct block_symbol *bsym,
5358 struct match_data *data)
ccefe4c4 5359{
199b4314
TT
5360 const struct block *block = bsym->block;
5361 struct symbol *sym = bsym->symbol;
5362
40658b94
PH
5363 if (sym == NULL)
5364 {
5365 if (!data->found_sym && data->arg_sym != NULL)
d1183b06 5366 add_defn_to_vec (*data->resultp,
40658b94
PH
5367 fixup_symbol_section (data->arg_sym, data->objfile),
5368 block);
1178743e 5369 data->found_sym = false;
40658b94
PH
5370 data->arg_sym = NULL;
5371 }
5372 else
5373 {
5374 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
199b4314 5375 return true;
40658b94
PH
5376 else if (SYMBOL_IS_ARGUMENT (sym))
5377 data->arg_sym = sym;
5378 else
5379 {
1178743e 5380 data->found_sym = true;
d1183b06 5381 add_defn_to_vec (*data->resultp,
40658b94
PH
5382 fixup_symbol_section (sym, data->objfile),
5383 block);
5384 }
5385 }
199b4314 5386 return true;
40658b94
PH
5387}
5388
b5ec771e
PA
5389/* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5390 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
1bfa81ac 5391 symbols to RESULT. Return whether we found such symbols. */
22cee43f
PMR
5392
5393static int
d1183b06 5394ada_add_block_renamings (std::vector<struct block_symbol> &result,
22cee43f 5395 const struct block *block,
b5ec771e
PA
5396 const lookup_name_info &lookup_name,
5397 domain_enum domain)
22cee43f
PMR
5398{
5399 struct using_direct *renaming;
d1183b06 5400 int defns_mark = result.size ();
22cee43f 5401
b5ec771e
PA
5402 symbol_name_matcher_ftype *name_match
5403 = ada_get_symbol_name_matcher (lookup_name);
5404
22cee43f
PMR
5405 for (renaming = block_using (block);
5406 renaming != NULL;
5407 renaming = renaming->next)
5408 {
5409 const char *r_name;
22cee43f
PMR
5410
5411 /* Avoid infinite recursions: skip this renaming if we are actually
5412 already traversing it.
5413
5414 Currently, symbol lookup in Ada don't use the namespace machinery from
5415 C++/Fortran support: skip namespace imports that use them. */
5416 if (renaming->searched
5417 || (renaming->import_src != NULL
5418 && renaming->import_src[0] != '\0')
5419 || (renaming->import_dest != NULL
5420 && renaming->import_dest[0] != '\0'))
5421 continue;
5422 renaming->searched = 1;
5423
5424 /* TODO: here, we perform another name-based symbol lookup, which can
5425 pull its own multiple overloads. In theory, we should be able to do
5426 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5427 not a simple name. But in order to do this, we would need to enhance
5428 the DWARF reader to associate a symbol to this renaming, instead of a
5429 name. So, for now, we do something simpler: re-use the C++/Fortran
5430 namespace machinery. */
5431 r_name = (renaming->alias != NULL
5432 ? renaming->alias
5433 : renaming->declaration);
b5ec771e
PA
5434 if (name_match (r_name, lookup_name, NULL))
5435 {
5436 lookup_name_info decl_lookup_name (renaming->declaration,
5437 lookup_name.match_type ());
d1183b06 5438 ada_add_all_symbols (result, block, decl_lookup_name, domain,
b5ec771e
PA
5439 1, NULL);
5440 }
22cee43f
PMR
5441 renaming->searched = 0;
5442 }
d1183b06 5443 return result.size () != defns_mark;
22cee43f
PMR
5444}
5445
db230ce3
JB
5446/* Implements compare_names, but only applying the comparision using
5447 the given CASING. */
5b4ee69b 5448
40658b94 5449static int
db230ce3
JB
5450compare_names_with_case (const char *string1, const char *string2,
5451 enum case_sensitivity casing)
40658b94
PH
5452{
5453 while (*string1 != '\0' && *string2 != '\0')
5454 {
db230ce3
JB
5455 char c1, c2;
5456
40658b94
PH
5457 if (isspace (*string1) || isspace (*string2))
5458 return strcmp_iw_ordered (string1, string2);
db230ce3
JB
5459
5460 if (casing == case_sensitive_off)
5461 {
5462 c1 = tolower (*string1);
5463 c2 = tolower (*string2);
5464 }
5465 else
5466 {
5467 c1 = *string1;
5468 c2 = *string2;
5469 }
5470 if (c1 != c2)
40658b94 5471 break;
db230ce3 5472
40658b94
PH
5473 string1 += 1;
5474 string2 += 1;
5475 }
db230ce3 5476
40658b94
PH
5477 switch (*string1)
5478 {
5479 case '(':
5480 return strcmp_iw_ordered (string1, string2);
5481 case '_':
5482 if (*string2 == '\0')
5483 {
052874e8 5484 if (is_name_suffix (string1))
40658b94
PH
5485 return 0;
5486 else
1a1d5513 5487 return 1;
40658b94 5488 }
dbb8534f 5489 /* FALLTHROUGH */
40658b94
PH
5490 default:
5491 if (*string2 == '(')
5492 return strcmp_iw_ordered (string1, string2);
5493 else
db230ce3
JB
5494 {
5495 if (casing == case_sensitive_off)
5496 return tolower (*string1) - tolower (*string2);
5497 else
5498 return *string1 - *string2;
5499 }
40658b94 5500 }
ccefe4c4
TT
5501}
5502
db230ce3
JB
5503/* Compare STRING1 to STRING2, with results as for strcmp.
5504 Compatible with strcmp_iw_ordered in that...
5505
5506 strcmp_iw_ordered (STRING1, STRING2) <= 0
5507
5508 ... implies...
5509
5510 compare_names (STRING1, STRING2) <= 0
5511
5512 (they may differ as to what symbols compare equal). */
5513
5514static int
5515compare_names (const char *string1, const char *string2)
5516{
5517 int result;
5518
5519 /* Similar to what strcmp_iw_ordered does, we need to perform
5520 a case-insensitive comparison first, and only resort to
5521 a second, case-sensitive, comparison if the first one was
5522 not sufficient to differentiate the two strings. */
5523
5524 result = compare_names_with_case (string1, string2, case_sensitive_off);
5525 if (result == 0)
5526 result = compare_names_with_case (string1, string2, case_sensitive_on);
5527
5528 return result;
5529}
5530
b5ec771e
PA
5531/* Convenience function to get at the Ada encoded lookup name for
5532 LOOKUP_NAME, as a C string. */
5533
5534static const char *
5535ada_lookup_name (const lookup_name_info &lookup_name)
5536{
5537 return lookup_name.ada ().lookup_name ().c_str ();
5538}
5539
1bfa81ac 5540/* Add to RESULT all non-local symbols whose name and domain match
b5ec771e
PA
5541 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5542 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5543 symbols otherwise. */
339c13b6
JB
5544
5545static void
d1183b06 5546add_nonlocal_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
5547 const lookup_name_info &lookup_name,
5548 domain_enum domain, int global)
339c13b6 5549{
1bfa81ac 5550 struct match_data data (&result);
339c13b6 5551
b5ec771e
PA
5552 bool is_wild_match = lookup_name.ada ().wild_match_p ();
5553
199b4314
TT
5554 auto callback = [&] (struct block_symbol *bsym)
5555 {
5556 return aux_add_nonlocal_symbols (bsym, &data);
5557 };
5558
2030c079 5559 for (objfile *objfile : current_program_space->objfiles ())
40658b94
PH
5560 {
5561 data.objfile = objfile;
5562
1228719f
TT
5563 if (objfile->sf != nullptr)
5564 objfile->sf->qf->map_matching_symbols (objfile, lookup_name,
5565 domain, global, callback,
5566 (is_wild_match
5567 ? NULL : compare_names));
22cee43f 5568
b669c953 5569 for (compunit_symtab *cu : objfile->compunits ())
22cee43f
PMR
5570 {
5571 const struct block *global_block
5572 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cu), GLOBAL_BLOCK);
5573
d1183b06 5574 if (ada_add_block_renamings (result, global_block, lookup_name,
b5ec771e 5575 domain))
1178743e 5576 data.found_sym = true;
22cee43f 5577 }
40658b94
PH
5578 }
5579
d1183b06 5580 if (result.empty () && global && !is_wild_match)
40658b94 5581 {
b5ec771e 5582 const char *name = ada_lookup_name (lookup_name);
e0802d59
TT
5583 std::string bracket_name = std::string ("<_ada_") + name + '>';
5584 lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
b5ec771e 5585
2030c079 5586 for (objfile *objfile : current_program_space->objfiles ())
dda83cd7 5587 {
40658b94 5588 data.objfile = objfile;
1228719f
TT
5589 if (objfile->sf != nullptr)
5590 objfile->sf->qf->map_matching_symbols (objfile, name1,
5591 domain, global, callback,
5592 compare_names);
40658b94
PH
5593 }
5594 }
339c13b6
JB
5595}
5596
b5ec771e
PA
5597/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5598 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
1bfa81ac 5599 returning the number of matches. Add these to RESULT.
4eeaa230 5600
22cee43f
PMR
5601 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5602 symbol match within the nest of blocks whose innermost member is BLOCK,
4c4b4cd2 5603 is the one match returned (no other matches in that or
d9680e73 5604 enclosing blocks is returned). If there are any matches in or
22cee43f 5605 surrounding BLOCK, then these alone are returned.
4eeaa230 5606
b5ec771e
PA
5607 Names prefixed with "standard__" are handled specially:
5608 "standard__" is first stripped off (by the lookup_name
5609 constructor), and only static and global symbols are searched.
14f9c5c9 5610
22cee43f
PMR
5611 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5612 to lookup global symbols. */
5613
5614static void
d1183b06 5615ada_add_all_symbols (std::vector<struct block_symbol> &result,
22cee43f 5616 const struct block *block,
b5ec771e 5617 const lookup_name_info &lookup_name,
22cee43f
PMR
5618 domain_enum domain,
5619 int full_search,
5620 int *made_global_lookup_p)
14f9c5c9
AS
5621{
5622 struct symbol *sym;
14f9c5c9 5623
22cee43f
PMR
5624 if (made_global_lookup_p)
5625 *made_global_lookup_p = 0;
339c13b6
JB
5626
5627 /* Special case: If the user specifies a symbol name inside package
5628 Standard, do a non-wild matching of the symbol name without
5629 the "standard__" prefix. This was primarily introduced in order
5630 to allow the user to specifically access the standard exceptions
5631 using, for instance, Standard.Constraint_Error when Constraint_Error
5632 is ambiguous (due to the user defining its own Constraint_Error
5633 entity inside its program). */
b5ec771e
PA
5634 if (lookup_name.ada ().standard_p ())
5635 block = NULL;
4c4b4cd2 5636
339c13b6 5637 /* Check the non-global symbols. If we have ANY match, then we're done. */
14f9c5c9 5638
4eeaa230
DE
5639 if (block != NULL)
5640 {
5641 if (full_search)
d1183b06 5642 ada_add_local_symbols (result, lookup_name, block, domain);
4eeaa230
DE
5643 else
5644 {
5645 /* In the !full_search case we're are being called by
4009ee92 5646 iterate_over_symbols, and we don't want to search
4eeaa230 5647 superblocks. */
d1183b06 5648 ada_add_block_symbols (result, block, lookup_name, domain, NULL);
4eeaa230 5649 }
d1183b06 5650 if (!result.empty () || !full_search)
22cee43f 5651 return;
4eeaa230 5652 }
d2e4a39e 5653
339c13b6
JB
5654 /* No non-global symbols found. Check our cache to see if we have
5655 already performed this search before. If we have, then return
5656 the same result. */
5657
b5ec771e
PA
5658 if (lookup_cached_symbol (ada_lookup_name (lookup_name),
5659 domain, &sym, &block))
4c4b4cd2
PH
5660 {
5661 if (sym != NULL)
d1183b06 5662 add_defn_to_vec (result, sym, block);
22cee43f 5663 return;
4c4b4cd2 5664 }
14f9c5c9 5665
22cee43f
PMR
5666 if (made_global_lookup_p)
5667 *made_global_lookup_p = 1;
b1eedac9 5668
339c13b6
JB
5669 /* Search symbols from all global blocks. */
5670
d1183b06 5671 add_nonlocal_symbols (result, lookup_name, domain, 1);
d2e4a39e 5672
4c4b4cd2 5673 /* Now add symbols from all per-file blocks if we've gotten no hits
339c13b6 5674 (not strictly correct, but perhaps better than an error). */
d2e4a39e 5675
d1183b06
TT
5676 if (result.empty ())
5677 add_nonlocal_symbols (result, lookup_name, domain, 0);
22cee43f
PMR
5678}
5679
b5ec771e 5680/* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
d1183b06
TT
5681 is non-zero, enclosing scope and in global scopes.
5682
5683 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5684 blocks and symbol tables (if any) in which they were found.
22cee43f
PMR
5685
5686 When full_search is non-zero, any non-function/non-enumeral
5687 symbol match within the nest of blocks whose innermost member is BLOCK,
5688 is the one match returned (no other matches in that or
5689 enclosing blocks is returned). If there are any matches in or
5690 surrounding BLOCK, then these alone are returned.
5691
5692 Names prefixed with "standard__" are handled specially: "standard__"
5693 is first stripped off, and only static and global symbols are searched. */
5694
d1183b06 5695static std::vector<struct block_symbol>
b5ec771e
PA
5696ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
5697 const struct block *block,
22cee43f 5698 domain_enum domain,
22cee43f
PMR
5699 int full_search)
5700{
22cee43f 5701 int syms_from_global_search;
d1183b06 5702 std::vector<struct block_symbol> results;
22cee43f 5703
d1183b06 5704 ada_add_all_symbols (results, block, lookup_name,
b5ec771e 5705 domain, full_search, &syms_from_global_search);
14f9c5c9 5706
d1183b06 5707 remove_extra_symbols (&results);
4c4b4cd2 5708
d1183b06 5709 if (results.empty () && full_search && syms_from_global_search)
b5ec771e 5710 cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
14f9c5c9 5711
d1183b06 5712 if (results.size () == 1 && full_search && syms_from_global_search)
b5ec771e 5713 cache_symbol (ada_lookup_name (lookup_name), domain,
d1183b06 5714 results[0].symbol, results[0].block);
ec6a20c2 5715
d1183b06
TT
5716 remove_irrelevant_renamings (&results, block);
5717 return results;
14f9c5c9
AS
5718}
5719
b5ec771e 5720/* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
d1183b06 5721 in global scopes, returning (SYM,BLOCK) tuples.
ec6a20c2 5722
4eeaa230
DE
5723 See ada_lookup_symbol_list_worker for further details. */
5724
d1183b06 5725std::vector<struct block_symbol>
b5ec771e 5726ada_lookup_symbol_list (const char *name, const struct block *block,
d1183b06 5727 domain_enum domain)
4eeaa230 5728{
b5ec771e
PA
5729 symbol_name_match_type name_match_type = name_match_type_from_name (name);
5730 lookup_name_info lookup_name (name, name_match_type);
5731
d1183b06 5732 return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
4eeaa230
DE
5733}
5734
4e5c77fe
JB
5735/* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5736 to 1, but choosing the first symbol found if there are multiple
5737 choices.
5738
5e2336be
JB
5739 The result is stored in *INFO, which must be non-NULL.
5740 If no match is found, INFO->SYM is set to NULL. */
4e5c77fe
JB
5741
5742void
5743ada_lookup_encoded_symbol (const char *name, const struct block *block,
fe978cb0 5744 domain_enum domain,
d12307c1 5745 struct block_symbol *info)
14f9c5c9 5746{
b5ec771e
PA
5747 /* Since we already have an encoded name, wrap it in '<>' to force a
5748 verbatim match. Otherwise, if the name happens to not look like
5749 an encoded name (because it doesn't include a "__"),
5750 ada_lookup_name_info would re-encode/fold it again, and that
5751 would e.g., incorrectly lowercase object renaming names like
5752 "R28b" -> "r28b". */
12932e2c 5753 std::string verbatim = add_angle_brackets (name);
b5ec771e 5754
5e2336be 5755 gdb_assert (info != NULL);
65392b3e 5756 *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
4e5c77fe 5757}
aeb5907d
JB
5758
5759/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5760 scope and in global scopes, or NULL if none. NAME is folded and
5761 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
65392b3e 5762 choosing the first symbol if there are multiple choices. */
4e5c77fe 5763
d12307c1 5764struct block_symbol
aeb5907d 5765ada_lookup_symbol (const char *name, const struct block *block0,
dda83cd7 5766 domain_enum domain)
aeb5907d 5767{
d1183b06
TT
5768 std::vector<struct block_symbol> candidates
5769 = ada_lookup_symbol_list (name, block0, domain);
f98fc17b 5770
d1183b06 5771 if (candidates.empty ())
54d343a2 5772 return {};
f98fc17b
PA
5773
5774 block_symbol info = candidates[0];
5775 info.symbol = fixup_symbol_section (info.symbol, NULL);
d12307c1 5776 return info;
4c4b4cd2 5777}
14f9c5c9 5778
14f9c5c9 5779
4c4b4cd2
PH
5780/* True iff STR is a possible encoded suffix of a normal Ada name
5781 that is to be ignored for matching purposes. Suffixes of parallel
5782 names (e.g., XVE) are not included here. Currently, the possible suffixes
5823c3ef 5783 are given by any of the regular expressions:
4c4b4cd2 5784
babe1480
JB
5785 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5786 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
9ac7f98e 5787 TKB [subprogram suffix for task bodies]
babe1480 5788 _E[0-9]+[bs]$ [protected object entry suffixes]
61ee279c 5789 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
babe1480
JB
5790
5791 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5792 match is performed. This sequence is used to differentiate homonyms,
5793 is an optional part of a valid name suffix. */
4c4b4cd2 5794
14f9c5c9 5795static int
d2e4a39e 5796is_name_suffix (const char *str)
14f9c5c9
AS
5797{
5798 int k;
4c4b4cd2
PH
5799 const char *matching;
5800 const int len = strlen (str);
5801
babe1480
JB
5802 /* Skip optional leading __[0-9]+. */
5803
4c4b4cd2
PH
5804 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
5805 {
babe1480
JB
5806 str += 3;
5807 while (isdigit (str[0]))
dda83cd7 5808 str += 1;
4c4b4cd2 5809 }
babe1480
JB
5810
5811 /* [.$][0-9]+ */
4c4b4cd2 5812
babe1480 5813 if (str[0] == '.' || str[0] == '$')
4c4b4cd2 5814 {
babe1480 5815 matching = str + 1;
4c4b4cd2 5816 while (isdigit (matching[0]))
dda83cd7 5817 matching += 1;
4c4b4cd2 5818 if (matching[0] == '\0')
dda83cd7 5819 return 1;
4c4b4cd2
PH
5820 }
5821
5822 /* ___[0-9]+ */
babe1480 5823
4c4b4cd2
PH
5824 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
5825 {
5826 matching = str + 3;
5827 while (isdigit (matching[0]))
dda83cd7 5828 matching += 1;
4c4b4cd2 5829 if (matching[0] == '\0')
dda83cd7 5830 return 1;
4c4b4cd2
PH
5831 }
5832
9ac7f98e
JB
5833 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5834
5835 if (strcmp (str, "TKB") == 0)
5836 return 1;
5837
529cad9c
PH
5838#if 0
5839 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
0963b4bd
MS
5840 with a N at the end. Unfortunately, the compiler uses the same
5841 convention for other internal types it creates. So treating
529cad9c 5842 all entity names that end with an "N" as a name suffix causes
0963b4bd
MS
5843 some regressions. For instance, consider the case of an enumerated
5844 type. To support the 'Image attribute, it creates an array whose
529cad9c
PH
5845 name ends with N.
5846 Having a single character like this as a suffix carrying some
0963b4bd 5847 information is a bit risky. Perhaps we should change the encoding
529cad9c
PH
5848 to be something like "_N" instead. In the meantime, do not do
5849 the following check. */
5850 /* Protected Object Subprograms */
5851 if (len == 1 && str [0] == 'N')
5852 return 1;
5853#endif
5854
5855 /* _E[0-9]+[bs]$ */
5856 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5857 {
5858 matching = str + 3;
5859 while (isdigit (matching[0]))
dda83cd7 5860 matching += 1;
529cad9c 5861 if ((matching[0] == 'b' || matching[0] == 's')
dda83cd7
SM
5862 && matching [1] == '\0')
5863 return 1;
529cad9c
PH
5864 }
5865
4c4b4cd2
PH
5866 /* ??? We should not modify STR directly, as we are doing below. This
5867 is fine in this case, but may become problematic later if we find
5868 that this alternative did not work, and want to try matching
5869 another one from the begining of STR. Since we modified it, we
5870 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
5871 if (str[0] == 'X')
5872 {
5873 str += 1;
d2e4a39e 5874 while (str[0] != '_' && str[0] != '\0')
dda83cd7
SM
5875 {
5876 if (str[0] != 'n' && str[0] != 'b')
5877 return 0;
5878 str += 1;
5879 }
14f9c5c9 5880 }
babe1480 5881
14f9c5c9
AS
5882 if (str[0] == '\000')
5883 return 1;
babe1480 5884
d2e4a39e 5885 if (str[0] == '_')
14f9c5c9
AS
5886 {
5887 if (str[1] != '_' || str[2] == '\000')
dda83cd7 5888 return 0;
d2e4a39e 5889 if (str[2] == '_')
dda83cd7
SM
5890 {
5891 if (strcmp (str + 3, "JM") == 0)
5892 return 1;
5893 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5894 the LJM suffix in favor of the JM one. But we will
5895 still accept LJM as a valid suffix for a reasonable
5896 amount of time, just to allow ourselves to debug programs
5897 compiled using an older version of GNAT. */
5898 if (strcmp (str + 3, "LJM") == 0)
5899 return 1;
5900 if (str[3] != 'X')
5901 return 0;
5902 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5903 || str[4] == 'U' || str[4] == 'P')
5904 return 1;
5905 if (str[4] == 'R' && str[5] != 'T')
5906 return 1;
5907 return 0;
5908 }
4c4b4cd2 5909 if (!isdigit (str[2]))
dda83cd7 5910 return 0;
4c4b4cd2 5911 for (k = 3; str[k] != '\0'; k += 1)
dda83cd7
SM
5912 if (!isdigit (str[k]) && str[k] != '_')
5913 return 0;
14f9c5c9
AS
5914 return 1;
5915 }
4c4b4cd2 5916 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 5917 {
4c4b4cd2 5918 for (k = 2; str[k] != '\0'; k += 1)
dda83cd7
SM
5919 if (!isdigit (str[k]) && str[k] != '_')
5920 return 0;
14f9c5c9
AS
5921 return 1;
5922 }
5923 return 0;
5924}
d2e4a39e 5925
aeb5907d
JB
5926/* Return non-zero if the string starting at NAME and ending before
5927 NAME_END contains no capital letters. */
529cad9c
PH
5928
5929static int
5930is_valid_name_for_wild_match (const char *name0)
5931{
f945dedf 5932 std::string decoded_name = ada_decode (name0);
529cad9c
PH
5933 int i;
5934
5823c3ef
JB
5935 /* If the decoded name starts with an angle bracket, it means that
5936 NAME0 does not follow the GNAT encoding format. It should then
5937 not be allowed as a possible wild match. */
5938 if (decoded_name[0] == '<')
5939 return 0;
5940
529cad9c
PH
5941 for (i=0; decoded_name[i] != '\0'; i++)
5942 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5943 return 0;
5944
5945 return 1;
5946}
5947
59c8a30b
JB
5948/* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5949 character which could start a simple name. Assumes that *NAMEP points
5950 somewhere inside the string beginning at NAME0. */
4c4b4cd2 5951
14f9c5c9 5952static int
59c8a30b 5953advance_wild_match (const char **namep, const char *name0, char target0)
14f9c5c9 5954{
73589123 5955 const char *name = *namep;
5b4ee69b 5956
5823c3ef 5957 while (1)
14f9c5c9 5958 {
59c8a30b 5959 char t0, t1;
73589123
PH
5960
5961 t0 = *name;
5962 if (t0 == '_')
5963 {
5964 t1 = name[1];
5965 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5966 {
5967 name += 1;
61012eef 5968 if (name == name0 + 5 && startswith (name0, "_ada"))
73589123
PH
5969 break;
5970 else
5971 name += 1;
5972 }
aa27d0b3
JB
5973 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5974 || name[2] == target0))
73589123
PH
5975 {
5976 name += 2;
5977 break;
5978 }
86b44259
TT
5979 else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
5980 {
5981 /* Names like "pkg__B_N__name", where N is a number, are
5982 block-local. We can handle these by simply skipping
5983 the "B_" here. */
5984 name += 4;
5985 }
73589123
PH
5986 else
5987 return 0;
5988 }
5989 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5990 name += 1;
5991 else
5823c3ef 5992 return 0;
73589123
PH
5993 }
5994
5995 *namep = name;
5996 return 1;
5997}
5998
b5ec771e
PA
5999/* Return true iff NAME encodes a name of the form prefix.PATN.
6000 Ignores any informational suffixes of NAME (i.e., for which
6001 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6002 simple name. */
73589123 6003
b5ec771e 6004static bool
73589123
PH
6005wild_match (const char *name, const char *patn)
6006{
22e048c9 6007 const char *p;
73589123
PH
6008 const char *name0 = name;
6009
6010 while (1)
6011 {
6012 const char *match = name;
6013
6014 if (*name == *patn)
6015 {
6016 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
6017 if (*p != *name)
6018 break;
6019 if (*p == '\0' && is_name_suffix (name))
b5ec771e 6020 return match == name0 || is_valid_name_for_wild_match (name0);
73589123
PH
6021
6022 if (name[-1] == '_')
6023 name -= 1;
6024 }
6025 if (!advance_wild_match (&name, name0, *patn))
b5ec771e 6026 return false;
96d887e8 6027 }
96d887e8
PH
6028}
6029
d1183b06 6030/* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
b5ec771e 6031 necessary). OBJFILE is the section containing BLOCK. */
96d887e8
PH
6032
6033static void
d1183b06 6034ada_add_block_symbols (std::vector<struct block_symbol> &result,
b5ec771e
PA
6035 const struct block *block,
6036 const lookup_name_info &lookup_name,
6037 domain_enum domain, struct objfile *objfile)
96d887e8 6038{
8157b174 6039 struct block_iterator iter;
96d887e8
PH
6040 /* A matching argument symbol, if any. */
6041 struct symbol *arg_sym;
6042 /* Set true when we find a matching non-argument symbol. */
1178743e 6043 bool found_sym;
96d887e8
PH
6044 struct symbol *sym;
6045
6046 arg_sym = NULL;
1178743e 6047 found_sym = false;
b5ec771e
PA
6048 for (sym = block_iter_match_first (block, lookup_name, &iter);
6049 sym != NULL;
6050 sym = block_iter_match_next (lookup_name, &iter))
96d887e8 6051 {
c1b5c1eb 6052 if (symbol_matches_domain (sym->language (), SYMBOL_DOMAIN (sym), domain))
b5ec771e
PA
6053 {
6054 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
6055 {
6056 if (SYMBOL_IS_ARGUMENT (sym))
6057 arg_sym = sym;
6058 else
6059 {
1178743e 6060 found_sym = true;
d1183b06 6061 add_defn_to_vec (result,
b5ec771e
PA
6062 fixup_symbol_section (sym, objfile),
6063 block);
6064 }
6065 }
6066 }
96d887e8
PH
6067 }
6068
22cee43f
PMR
6069 /* Handle renamings. */
6070
d1183b06 6071 if (ada_add_block_renamings (result, block, lookup_name, domain))
1178743e 6072 found_sym = true;
22cee43f 6073
96d887e8
PH
6074 if (!found_sym && arg_sym != NULL)
6075 {
d1183b06 6076 add_defn_to_vec (result,
dda83cd7
SM
6077 fixup_symbol_section (arg_sym, objfile),
6078 block);
96d887e8
PH
6079 }
6080
b5ec771e 6081 if (!lookup_name.ada ().wild_match_p ())
96d887e8
PH
6082 {
6083 arg_sym = NULL;
1178743e 6084 found_sym = false;
b5ec771e
PA
6085 const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
6086 const char *name = ada_lookup_name.c_str ();
6087 size_t name_len = ada_lookup_name.size ();
96d887e8
PH
6088
6089 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 6090 {
dda83cd7
SM
6091 if (symbol_matches_domain (sym->language (),
6092 SYMBOL_DOMAIN (sym), domain))
6093 {
6094 int cmp;
6095
6096 cmp = (int) '_' - (int) sym->linkage_name ()[0];
6097 if (cmp == 0)
6098 {
6099 cmp = !startswith (sym->linkage_name (), "_ada_");
6100 if (cmp == 0)
6101 cmp = strncmp (name, sym->linkage_name () + 5,
6102 name_len);
6103 }
6104
6105 if (cmp == 0
6106 && is_name_suffix (sym->linkage_name () + name_len + 5))
6107 {
2a2d4dc3
AS
6108 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
6109 {
6110 if (SYMBOL_IS_ARGUMENT (sym))
6111 arg_sym = sym;
6112 else
6113 {
1178743e 6114 found_sym = true;
d1183b06 6115 add_defn_to_vec (result,
2a2d4dc3
AS
6116 fixup_symbol_section (sym, objfile),
6117 block);
6118 }
6119 }
dda83cd7
SM
6120 }
6121 }
76a01679 6122 }
96d887e8
PH
6123
6124 /* NOTE: This really shouldn't be needed for _ada_ symbols.
dda83cd7 6125 They aren't parameters, right? */
96d887e8 6126 if (!found_sym && arg_sym != NULL)
dda83cd7 6127 {
d1183b06 6128 add_defn_to_vec (result,
dda83cd7
SM
6129 fixup_symbol_section (arg_sym, objfile),
6130 block);
6131 }
96d887e8
PH
6132 }
6133}
6134\f
41d27058 6135
dda83cd7 6136 /* Symbol Completion */
41d27058 6137
b5ec771e 6138/* See symtab.h. */
41d27058 6139
b5ec771e
PA
6140bool
6141ada_lookup_name_info::matches
6142 (const char *sym_name,
6143 symbol_name_match_type match_type,
a207cff2 6144 completion_match_result *comp_match_res) const
41d27058 6145{
b5ec771e
PA
6146 bool match = false;
6147 const char *text = m_encoded_name.c_str ();
6148 size_t text_len = m_encoded_name.size ();
41d27058
JB
6149
6150 /* First, test against the fully qualified name of the symbol. */
6151
6152 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6153 match = true;
41d27058 6154
f945dedf 6155 std::string decoded_name = ada_decode (sym_name);
b5ec771e 6156 if (match && !m_encoded_p)
41d27058
JB
6157 {
6158 /* One needed check before declaring a positive match is to verify
dda83cd7
SM
6159 that iff we are doing a verbatim match, the decoded version
6160 of the symbol name starts with '<'. Otherwise, this symbol name
6161 is not a suitable completion. */
41d27058 6162
f945dedf 6163 bool has_angle_bracket = (decoded_name[0] == '<');
b5ec771e 6164 match = (has_angle_bracket == m_verbatim_p);
41d27058
JB
6165 }
6166
b5ec771e 6167 if (match && !m_verbatim_p)
41d27058
JB
6168 {
6169 /* When doing non-verbatim match, another check that needs to
dda83cd7
SM
6170 be done is to verify that the potentially matching symbol name
6171 does not include capital letters, because the ada-mode would
6172 not be able to understand these symbol names without the
6173 angle bracket notation. */
41d27058
JB
6174 const char *tmp;
6175
6176 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
6177 if (*tmp != '\0')
b5ec771e 6178 match = false;
41d27058
JB
6179 }
6180
6181 /* Second: Try wild matching... */
6182
b5ec771e 6183 if (!match && m_wild_match_p)
41d27058
JB
6184 {
6185 /* Since we are doing wild matching, this means that TEXT
dda83cd7
SM
6186 may represent an unqualified symbol name. We therefore must
6187 also compare TEXT against the unqualified name of the symbol. */
f945dedf 6188 sym_name = ada_unqualified_name (decoded_name.c_str ());
41d27058
JB
6189
6190 if (strncmp (sym_name, text, text_len) == 0)
b5ec771e 6191 match = true;
41d27058
JB
6192 }
6193
b5ec771e 6194 /* Finally: If we found a match, prepare the result to return. */
41d27058
JB
6195
6196 if (!match)
b5ec771e 6197 return false;
41d27058 6198
a207cff2 6199 if (comp_match_res != NULL)
b5ec771e 6200 {
a207cff2 6201 std::string &match_str = comp_match_res->match.storage ();
41d27058 6202
b5ec771e 6203 if (!m_encoded_p)
a207cff2 6204 match_str = ada_decode (sym_name);
b5ec771e
PA
6205 else
6206 {
6207 if (m_verbatim_p)
6208 match_str = add_angle_brackets (sym_name);
6209 else
6210 match_str = sym_name;
41d27058 6211
b5ec771e 6212 }
a207cff2
PA
6213
6214 comp_match_res->set_match (match_str.c_str ());
41d27058
JB
6215 }
6216
b5ec771e 6217 return true;
41d27058
JB
6218}
6219
dda83cd7 6220 /* Field Access */
96d887e8 6221
73fb9985
JB
6222/* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6223 for tagged types. */
6224
6225static int
6226ada_is_dispatch_table_ptr_type (struct type *type)
6227{
0d5cff50 6228 const char *name;
73fb9985 6229
78134374 6230 if (type->code () != TYPE_CODE_PTR)
73fb9985
JB
6231 return 0;
6232
7d93a1e0 6233 name = TYPE_TARGET_TYPE (type)->name ();
73fb9985
JB
6234 if (name == NULL)
6235 return 0;
6236
6237 return (strcmp (name, "ada__tags__dispatch_table") == 0);
6238}
6239
ac4a2da4
JG
6240/* Return non-zero if TYPE is an interface tag. */
6241
6242static int
6243ada_is_interface_tag (struct type *type)
6244{
7d93a1e0 6245 const char *name = type->name ();
ac4a2da4
JG
6246
6247 if (name == NULL)
6248 return 0;
6249
6250 return (strcmp (name, "ada__tags__interface_tag") == 0);
6251}
6252
963a6417
PH
6253/* True if field number FIELD_NUM in struct or union type TYPE is supposed
6254 to be invisible to users. */
96d887e8 6255
963a6417
PH
6256int
6257ada_is_ignored_field (struct type *type, int field_num)
96d887e8 6258{
1f704f76 6259 if (field_num < 0 || field_num > type->num_fields ())
963a6417 6260 return 1;
ffde82bf 6261
73fb9985
JB
6262 /* Check the name of that field. */
6263 {
6264 const char *name = TYPE_FIELD_NAME (type, field_num);
6265
6266 /* Anonymous field names should not be printed.
6267 brobecker/2007-02-20: I don't think this can actually happen
30baf67b 6268 but we don't want to print the value of anonymous fields anyway. */
73fb9985
JB
6269 if (name == NULL)
6270 return 1;
6271
ffde82bf
JB
6272 /* Normally, fields whose name start with an underscore ("_")
6273 are fields that have been internally generated by the compiler,
6274 and thus should not be printed. The "_parent" field is special,
6275 however: This is a field internally generated by the compiler
6276 for tagged types, and it contains the components inherited from
6277 the parent type. This field should not be printed as is, but
6278 should not be ignored either. */
61012eef 6279 if (name[0] == '_' && !startswith (name, "_parent"))
73fb9985
JB
6280 return 1;
6281 }
6282
ac4a2da4
JG
6283 /* If this is the dispatch table of a tagged type or an interface tag,
6284 then ignore. */
73fb9985 6285 if (ada_is_tagged_type (type, 1)
940da03e
SM
6286 && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
6287 || ada_is_interface_tag (type->field (field_num).type ())))
73fb9985
JB
6288 return 1;
6289
6290 /* Not a special field, so it should not be ignored. */
6291 return 0;
963a6417 6292}
96d887e8 6293
963a6417 6294/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
0963b4bd 6295 pointer or reference type whose ultimate target has a tag field. */
96d887e8 6296
963a6417
PH
6297int
6298ada_is_tagged_type (struct type *type, int refok)
6299{
988f6b3d 6300 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
963a6417 6301}
96d887e8 6302
963a6417 6303/* True iff TYPE represents the type of X'Tag */
96d887e8 6304
963a6417
PH
6305int
6306ada_is_tag_type (struct type *type)
6307{
460efde1
JB
6308 type = ada_check_typedef (type);
6309
78134374 6310 if (type == NULL || type->code () != TYPE_CODE_PTR)
963a6417
PH
6311 return 0;
6312 else
96d887e8 6313 {
963a6417 6314 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5b4ee69b 6315
963a6417 6316 return (name != NULL
dda83cd7 6317 && strcmp (name, "ada__tags__dispatch_table") == 0);
96d887e8 6318 }
96d887e8
PH
6319}
6320
963a6417 6321/* The type of the tag on VAL. */
76a01679 6322
de93309a 6323static struct type *
963a6417 6324ada_tag_type (struct value *val)
96d887e8 6325{
988f6b3d 6326 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0);
963a6417 6327}
96d887e8 6328
b50d69b5
JG
6329/* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6330 retired at Ada 05). */
6331
6332static int
6333is_ada95_tag (struct value *tag)
6334{
6335 return ada_value_struct_elt (tag, "tsd", 1) != NULL;
6336}
6337
963a6417 6338/* The value of the tag on VAL. */
96d887e8 6339
de93309a 6340static struct value *
963a6417
PH
6341ada_value_tag (struct value *val)
6342{
03ee6b2e 6343 return ada_value_struct_elt (val, "_tag", 0);
96d887e8
PH
6344}
6345
963a6417
PH
6346/* The value of the tag on the object of type TYPE whose contents are
6347 saved at VALADDR, if it is non-null, or is at memory address
0963b4bd 6348 ADDRESS. */
96d887e8 6349
963a6417 6350static struct value *
10a2c479 6351value_tag_from_contents_and_address (struct type *type,
fc1a4b47 6352 const gdb_byte *valaddr,
dda83cd7 6353 CORE_ADDR address)
96d887e8 6354{
b5385fc0 6355 int tag_byte_offset;
963a6417 6356 struct type *tag_type;
5b4ee69b 6357
963a6417 6358 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
dda83cd7 6359 NULL, NULL, NULL))
96d887e8 6360 {
fc1a4b47 6361 const gdb_byte *valaddr1 = ((valaddr == NULL)
10a2c479
AC
6362 ? NULL
6363 : valaddr + tag_byte_offset);
963a6417 6364 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
96d887e8 6365
963a6417 6366 return value_from_contents_and_address (tag_type, valaddr1, address1);
96d887e8 6367 }
963a6417
PH
6368 return NULL;
6369}
96d887e8 6370
963a6417
PH
6371static struct type *
6372type_from_tag (struct value *tag)
6373{
f5272a3b 6374 gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
5b4ee69b 6375
963a6417 6376 if (type_name != NULL)
5c4258f4 6377 return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
963a6417
PH
6378 return NULL;
6379}
96d887e8 6380
b50d69b5
JG
6381/* Given a value OBJ of a tagged type, return a value of this
6382 type at the base address of the object. The base address, as
6383 defined in Ada.Tags, it is the address of the primary tag of
6384 the object, and therefore where the field values of its full
6385 view can be fetched. */
6386
6387struct value *
6388ada_tag_value_at_base_address (struct value *obj)
6389{
b50d69b5
JG
6390 struct value *val;
6391 LONGEST offset_to_top = 0;
6392 struct type *ptr_type, *obj_type;
6393 struct value *tag;
6394 CORE_ADDR base_address;
6395
6396 obj_type = value_type (obj);
6397
6398 /* It is the responsability of the caller to deref pointers. */
6399
78134374 6400 if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
b50d69b5
JG
6401 return obj;
6402
6403 tag = ada_value_tag (obj);
6404 if (!tag)
6405 return obj;
6406
6407 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6408
6409 if (is_ada95_tag (tag))
6410 return obj;
6411
08f49010
XR
6412 ptr_type = language_lookup_primitive_type
6413 (language_def (language_ada), target_gdbarch(), "storage_offset");
b50d69b5
JG
6414 ptr_type = lookup_pointer_type (ptr_type);
6415 val = value_cast (ptr_type, tag);
6416 if (!val)
6417 return obj;
6418
6419 /* It is perfectly possible that an exception be raised while
6420 trying to determine the base address, just like for the tag;
6421 see ada_tag_name for more details. We do not print the error
6422 message for the same reason. */
6423
a70b8144 6424 try
b50d69b5
JG
6425 {
6426 offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
6427 }
6428
230d2906 6429 catch (const gdb_exception_error &e)
492d29ea
PA
6430 {
6431 return obj;
6432 }
b50d69b5
JG
6433
6434 /* If offset is null, nothing to do. */
6435
6436 if (offset_to_top == 0)
6437 return obj;
6438
6439 /* -1 is a special case in Ada.Tags; however, what should be done
6440 is not quite clear from the documentation. So do nothing for
6441 now. */
6442
6443 if (offset_to_top == -1)
6444 return obj;
6445
08f49010
XR
6446 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6447 from the base address. This was however incompatible with
6448 C++ dispatch table: C++ uses a *negative* value to *add*
6449 to the base address. Ada's convention has therefore been
6450 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6451 use the same convention. Here, we support both cases by
6452 checking the sign of OFFSET_TO_TOP. */
6453
6454 if (offset_to_top > 0)
6455 offset_to_top = -offset_to_top;
6456
6457 base_address = value_address (obj) + offset_to_top;
b50d69b5
JG
6458 tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
6459
6460 /* Make sure that we have a proper tag at the new address.
6461 Otherwise, offset_to_top is bogus (which can happen when
6462 the object is not initialized yet). */
6463
6464 if (!tag)
6465 return obj;
6466
6467 obj_type = type_from_tag (tag);
6468
6469 if (!obj_type)
6470 return obj;
6471
6472 return value_from_contents_and_address (obj_type, NULL, base_address);
6473}
6474
1b611343
JB
6475/* Return the "ada__tags__type_specific_data" type. */
6476
6477static struct type *
6478ada_get_tsd_type (struct inferior *inf)
963a6417 6479{
1b611343 6480 struct ada_inferior_data *data = get_ada_inferior_data (inf);
4c4b4cd2 6481
1b611343
JB
6482 if (data->tsd_type == 0)
6483 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
6484 return data->tsd_type;
6485}
529cad9c 6486
1b611343
JB
6487/* Return the TSD (type-specific data) associated to the given TAG.
6488 TAG is assumed to be the tag of a tagged-type entity.
529cad9c 6489
1b611343 6490 May return NULL if we are unable to get the TSD. */
4c4b4cd2 6491
1b611343
JB
6492static struct value *
6493ada_get_tsd_from_tag (struct value *tag)
4c4b4cd2 6494{
4c4b4cd2 6495 struct value *val;
1b611343 6496 struct type *type;
5b4ee69b 6497
1b611343
JB
6498 /* First option: The TSD is simply stored as a field of our TAG.
6499 Only older versions of GNAT would use this format, but we have
6500 to test it first, because there are no visible markers for
6501 the current approach except the absence of that field. */
529cad9c 6502
1b611343
JB
6503 val = ada_value_struct_elt (tag, "tsd", 1);
6504 if (val)
6505 return val;
e802dbe0 6506
1b611343
JB
6507 /* Try the second representation for the dispatch table (in which
6508 there is no explicit 'tsd' field in the referent of the tag pointer,
6509 and instead the tsd pointer is stored just before the dispatch
6510 table. */
e802dbe0 6511
1b611343
JB
6512 type = ada_get_tsd_type (current_inferior());
6513 if (type == NULL)
6514 return NULL;
6515 type = lookup_pointer_type (lookup_pointer_type (type));
6516 val = value_cast (type, tag);
6517 if (val == NULL)
6518 return NULL;
6519 return value_ind (value_ptradd (val, -1));
e802dbe0
JB
6520}
6521
1b611343
JB
6522/* Given the TSD of a tag (type-specific data), return a string
6523 containing the name of the associated type.
6524
f5272a3b 6525 May return NULL if we are unable to determine the tag name. */
1b611343 6526
f5272a3b 6527static gdb::unique_xmalloc_ptr<char>
1b611343 6528ada_tag_name_from_tsd (struct value *tsd)
529cad9c 6529{
529cad9c 6530 char *p;
1b611343 6531 struct value *val;
529cad9c 6532
1b611343 6533 val = ada_value_struct_elt (tsd, "expanded_name", 1);
4c4b4cd2 6534 if (val == NULL)
1b611343 6535 return NULL;
66920317
TT
6536 gdb::unique_xmalloc_ptr<char> buffer
6537 = target_read_string (value_as_address (val), INT_MAX);
6538 if (buffer == nullptr)
f5272a3b
TT
6539 return nullptr;
6540
6541 for (p = buffer.get (); *p != '\0'; ++p)
6542 {
6543 if (isalpha (*p))
6544 *p = tolower (*p);
6545 }
6546
6547 return buffer;
4c4b4cd2
PH
6548}
6549
6550/* The type name of the dynamic type denoted by the 'tag value TAG, as
1b611343
JB
6551 a C string.
6552
6553 Return NULL if the TAG is not an Ada tag, or if we were unable to
f5272a3b 6554 determine the name of that tag. */
4c4b4cd2 6555
f5272a3b 6556gdb::unique_xmalloc_ptr<char>
4c4b4cd2
PH
6557ada_tag_name (struct value *tag)
6558{
f5272a3b 6559 gdb::unique_xmalloc_ptr<char> name;
5b4ee69b 6560
df407dfe 6561 if (!ada_is_tag_type (value_type (tag)))
4c4b4cd2 6562 return NULL;
1b611343
JB
6563
6564 /* It is perfectly possible that an exception be raised while trying
6565 to determine the TAG's name, even under normal circumstances:
6566 The associated variable may be uninitialized or corrupted, for
6567 instance. We do not let any exception propagate past this point.
6568 instead we return NULL.
6569
6570 We also do not print the error message either (which often is very
6571 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6572 the caller print a more meaningful message if necessary. */
a70b8144 6573 try
1b611343
JB
6574 {
6575 struct value *tsd = ada_get_tsd_from_tag (tag);
6576
6577 if (tsd != NULL)
6578 name = ada_tag_name_from_tsd (tsd);
6579 }
230d2906 6580 catch (const gdb_exception_error &e)
492d29ea
PA
6581 {
6582 }
1b611343
JB
6583
6584 return name;
4c4b4cd2
PH
6585}
6586
6587/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6588
d2e4a39e 6589struct type *
ebf56fd3 6590ada_parent_type (struct type *type)
14f9c5c9
AS
6591{
6592 int i;
6593
61ee279c 6594 type = ada_check_typedef (type);
14f9c5c9 6595
78134374 6596 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
6597 return NULL;
6598
1f704f76 6599 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 6600 if (ada_is_parent_field (type, i))
0c1f74cf 6601 {
dda83cd7 6602 struct type *parent_type = type->field (i).type ();
0c1f74cf 6603
dda83cd7
SM
6604 /* If the _parent field is a pointer, then dereference it. */
6605 if (parent_type->code () == TYPE_CODE_PTR)
6606 parent_type = TYPE_TARGET_TYPE (parent_type);
6607 /* If there is a parallel XVS type, get the actual base type. */
6608 parent_type = ada_get_base_type (parent_type);
0c1f74cf 6609
dda83cd7 6610 return ada_check_typedef (parent_type);
0c1f74cf 6611 }
14f9c5c9
AS
6612
6613 return NULL;
6614}
6615
4c4b4cd2
PH
6616/* True iff field number FIELD_NUM of structure type TYPE contains the
6617 parent-type (inherited) fields of a derived type. Assumes TYPE is
6618 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6619
6620int
ebf56fd3 6621ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6622{
61ee279c 6623 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
5b4ee69b 6624
4c4b4cd2 6625 return (name != NULL
dda83cd7
SM
6626 && (startswith (name, "PARENT")
6627 || startswith (name, "_parent")));
14f9c5c9
AS
6628}
6629
4c4b4cd2 6630/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6631 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6632 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6633 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6634 structures. */
14f9c5c9
AS
6635
6636int
ebf56fd3 6637ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6638{
d2e4a39e 6639 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 6640
dddc0e16
JB
6641 if (name != NULL && strcmp (name, "RETVAL") == 0)
6642 {
6643 /* This happens in functions with "out" or "in out" parameters
6644 which are passed by copy. For such functions, GNAT describes
6645 the function's return type as being a struct where the return
6646 value is in a field called RETVAL, and where the other "out"
6647 or "in out" parameters are fields of that struct. This is not
6648 a wrapper. */
6649 return 0;
6650 }
6651
d2e4a39e 6652 return (name != NULL
dda83cd7
SM
6653 && (startswith (name, "PARENT")
6654 || strcmp (name, "REP") == 0
6655 || startswith (name, "_parent")
6656 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6657}
6658
4c4b4cd2
PH
6659/* True iff field number FIELD_NUM of structure or union type TYPE
6660 is a variant wrapper. Assumes TYPE is a structure type with at least
6661 FIELD_NUM+1 fields. */
14f9c5c9
AS
6662
6663int
ebf56fd3 6664ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6665{
8ecb59f8
TT
6666 /* Only Ada types are eligible. */
6667 if (!ADA_TYPE_P (type))
6668 return 0;
6669
940da03e 6670 struct type *field_type = type->field (field_num).type ();
5b4ee69b 6671
78134374
SM
6672 return (field_type->code () == TYPE_CODE_UNION
6673 || (is_dynamic_field (type, field_num)
6674 && (TYPE_TARGET_TYPE (field_type)->code ()
c3e5cd34 6675 == TYPE_CODE_UNION)));
14f9c5c9
AS
6676}
6677
6678/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6679 whose discriminants are contained in the record type OUTER_TYPE,
7c964f07
UW
6680 returns the type of the controlling discriminant for the variant.
6681 May return NULL if the type could not be found. */
14f9c5c9 6682
d2e4a39e 6683struct type *
ebf56fd3 6684ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6685{
a121b7c1 6686 const char *name = ada_variant_discrim_name (var_type);
5b4ee69b 6687
988f6b3d 6688 return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
14f9c5c9
AS
6689}
6690
4c4b4cd2 6691/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6692 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6693 represents a 'when others' clause; otherwise 0. */
14f9c5c9 6694
de93309a 6695static int
ebf56fd3 6696ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6697{
d2e4a39e 6698 const char *name = TYPE_FIELD_NAME (type, field_num);
5b4ee69b 6699
14f9c5c9
AS
6700 return (name != NULL && name[0] == 'O');
6701}
6702
6703/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6704 returns the name of the discriminant controlling the variant.
6705 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6706
a121b7c1 6707const char *
ebf56fd3 6708ada_variant_discrim_name (struct type *type0)
14f9c5c9 6709{
5f9febe0 6710 static std::string result;
d2e4a39e
AS
6711 struct type *type;
6712 const char *name;
6713 const char *discrim_end;
6714 const char *discrim_start;
14f9c5c9 6715
78134374 6716 if (type0->code () == TYPE_CODE_PTR)
14f9c5c9
AS
6717 type = TYPE_TARGET_TYPE (type0);
6718 else
6719 type = type0;
6720
6721 name = ada_type_name (type);
6722
6723 if (name == NULL || name[0] == '\000')
6724 return "";
6725
6726 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6727 discrim_end -= 1)
6728 {
61012eef 6729 if (startswith (discrim_end, "___XVN"))
dda83cd7 6730 break;
14f9c5c9
AS
6731 }
6732 if (discrim_end == name)
6733 return "";
6734
d2e4a39e 6735 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6736 discrim_start -= 1)
6737 {
d2e4a39e 6738 if (discrim_start == name + 1)
dda83cd7 6739 return "";
76a01679 6740 if ((discrim_start > name + 3
dda83cd7
SM
6741 && startswith (discrim_start - 3, "___"))
6742 || discrim_start[-1] == '.')
6743 break;
14f9c5c9
AS
6744 }
6745
5f9febe0
TT
6746 result = std::string (discrim_start, discrim_end - discrim_start);
6747 return result.c_str ();
14f9c5c9
AS
6748}
6749
4c4b4cd2
PH
6750/* Scan STR for a subtype-encoded number, beginning at position K.
6751 Put the position of the character just past the number scanned in
6752 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6753 Return 1 if there was a valid number at the given position, and 0
6754 otherwise. A "subtype-encoded" number consists of the absolute value
6755 in decimal, followed by the letter 'm' to indicate a negative number.
6756 Assumes 0m does not occur. */
14f9c5c9
AS
6757
6758int
d2e4a39e 6759ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6760{
6761 ULONGEST RU;
6762
d2e4a39e 6763 if (!isdigit (str[k]))
14f9c5c9
AS
6764 return 0;
6765
4c4b4cd2 6766 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6767 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6768 LONGEST. */
14f9c5c9
AS
6769 RU = 0;
6770 while (isdigit (str[k]))
6771 {
d2e4a39e 6772 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6773 k += 1;
6774 }
6775
d2e4a39e 6776 if (str[k] == 'm')
14f9c5c9
AS
6777 {
6778 if (R != NULL)
dda83cd7 6779 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6780 k += 1;
6781 }
6782 else if (R != NULL)
6783 *R = (LONGEST) RU;
6784
4c4b4cd2 6785 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6786 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6787 number representable as a LONGEST (although either would probably work
6788 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6789 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6790
6791 if (new_k != NULL)
6792 *new_k = k;
6793 return 1;
6794}
6795
4c4b4cd2
PH
6796/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6797 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6798 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6799
de93309a 6800static int
ebf56fd3 6801ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6802{
d2e4a39e 6803 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
6804 int p;
6805
6806 p = 0;
6807 while (1)
6808 {
d2e4a39e 6809 switch (name[p])
dda83cd7
SM
6810 {
6811 case '\0':
6812 return 0;
6813 case 'S':
6814 {
6815 LONGEST W;
6816
6817 if (!ada_scan_number (name, p + 1, &W, &p))
6818 return 0;
6819 if (val == W)
6820 return 1;
6821 break;
6822 }
6823 case 'R':
6824 {
6825 LONGEST L, U;
6826
6827 if (!ada_scan_number (name, p + 1, &L, &p)
6828 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6829 return 0;
6830 if (val >= L && val <= U)
6831 return 1;
6832 break;
6833 }
6834 case 'O':
6835 return 1;
6836 default:
6837 return 0;
6838 }
4c4b4cd2
PH
6839 }
6840}
6841
0963b4bd 6842/* FIXME: Lots of redundancy below. Try to consolidate. */
4c4b4cd2
PH
6843
6844/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6845 ARG_TYPE, extract and return the value of one of its (non-static)
6846 fields. FIELDNO says which field. Differs from value_primitive_field
6847 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6848
5eb68a39 6849struct value *
d2e4a39e 6850ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
dda83cd7 6851 struct type *arg_type)
14f9c5c9 6852{
14f9c5c9
AS
6853 struct type *type;
6854
61ee279c 6855 arg_type = ada_check_typedef (arg_type);
940da03e 6856 type = arg_type->field (fieldno).type ();
14f9c5c9 6857
4504bbde
TT
6858 /* Handle packed fields. It might be that the field is not packed
6859 relative to its containing structure, but the structure itself is
6860 packed; in this case we must take the bit-field path. */
6861 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || value_bitpos (arg1) != 0)
14f9c5c9
AS
6862 {
6863 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6864 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6865
0fd88904 6866 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
dda83cd7
SM
6867 offset + bit_pos / 8,
6868 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6869 }
6870 else
6871 return value_primitive_field (arg1, offset, fieldno, arg_type);
6872}
6873
52ce6436
PH
6874/* Find field with name NAME in object of type TYPE. If found,
6875 set the following for each argument that is non-null:
6876 - *FIELD_TYPE_P to the field's type;
6877 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6878 an object of that type;
6879 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6880 - *BIT_SIZE_P to its size in bits if the field is packed, and
6881 0 otherwise;
6882 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6883 fields up to but not including the desired field, or by the total
6884 number of fields if not found. A NULL value of NAME never
6885 matches; the function just counts visible fields in this case.
6886
828d5846
XR
6887 Notice that we need to handle when a tagged record hierarchy
6888 has some components with the same name, like in this scenario:
6889
6890 type Top_T is tagged record
dda83cd7
SM
6891 N : Integer := 1;
6892 U : Integer := 974;
6893 A : Integer := 48;
828d5846
XR
6894 end record;
6895
6896 type Middle_T is new Top.Top_T with record
dda83cd7
SM
6897 N : Character := 'a';
6898 C : Integer := 3;
828d5846
XR
6899 end record;
6900
6901 type Bottom_T is new Middle.Middle_T with record
dda83cd7
SM
6902 N : Float := 4.0;
6903 C : Character := '5';
6904 X : Integer := 6;
6905 A : Character := 'J';
828d5846
XR
6906 end record;
6907
6908 Let's say we now have a variable declared and initialized as follow:
6909
6910 TC : Top_A := new Bottom_T;
6911
6912 And then we use this variable to call this function
6913
6914 procedure Assign (Obj: in out Top_T; TV : Integer);
6915
6916 as follow:
6917
6918 Assign (Top_T (B), 12);
6919
6920 Now, we're in the debugger, and we're inside that procedure
6921 then and we want to print the value of obj.c:
6922
6923 Usually, the tagged record or one of the parent type owns the
6924 component to print and there's no issue but in this particular
6925 case, what does it mean to ask for Obj.C? Since the actual
6926 type for object is type Bottom_T, it could mean two things: type
6927 component C from the Middle_T view, but also component C from
6928 Bottom_T. So in that "undefined" case, when the component is
6929 not found in the non-resolved type (which includes all the
6930 components of the parent type), then resolve it and see if we
6931 get better luck once expanded.
6932
6933 In the case of homonyms in the derived tagged type, we don't
6934 guaranty anything, and pick the one that's easiest for us
6935 to program.
6936
0963b4bd 6937 Returns 1 if found, 0 otherwise. */
52ce6436 6938
4c4b4cd2 6939static int
0d5cff50 6940find_struct_field (const char *name, struct type *type, int offset,
dda83cd7
SM
6941 struct type **field_type_p,
6942 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
52ce6436 6943 int *index_p)
4c4b4cd2
PH
6944{
6945 int i;
828d5846 6946 int parent_offset = -1;
4c4b4cd2 6947
61ee279c 6948 type = ada_check_typedef (type);
76a01679 6949
52ce6436
PH
6950 if (field_type_p != NULL)
6951 *field_type_p = NULL;
6952 if (byte_offset_p != NULL)
d5d6fca5 6953 *byte_offset_p = 0;
52ce6436
PH
6954 if (bit_offset_p != NULL)
6955 *bit_offset_p = 0;
6956 if (bit_size_p != NULL)
6957 *bit_size_p = 0;
6958
1f704f76 6959 for (i = 0; i < type->num_fields (); i += 1)
4c4b4cd2
PH
6960 {
6961 int bit_pos = TYPE_FIELD_BITPOS (type, i);
6962 int fld_offset = offset + bit_pos / 8;
0d5cff50 6963 const char *t_field_name = TYPE_FIELD_NAME (type, i);
76a01679 6964
4c4b4cd2 6965 if (t_field_name == NULL)
dda83cd7 6966 continue;
4c4b4cd2 6967
828d5846 6968 else if (ada_is_parent_field (type, i))
dda83cd7 6969 {
828d5846
XR
6970 /* This is a field pointing us to the parent type of a tagged
6971 type. As hinted in this function's documentation, we give
6972 preference to fields in the current record first, so what
6973 we do here is just record the index of this field before
6974 we skip it. If it turns out we couldn't find our field
6975 in the current record, then we'll get back to it and search
6976 inside it whether the field might exist in the parent. */
6977
dda83cd7
SM
6978 parent_offset = i;
6979 continue;
6980 }
828d5846 6981
52ce6436 6982 else if (name != NULL && field_name_match (t_field_name, name))
dda83cd7
SM
6983 {
6984 int bit_size = TYPE_FIELD_BITSIZE (type, i);
5b4ee69b 6985
52ce6436 6986 if (field_type_p != NULL)
940da03e 6987 *field_type_p = type->field (i).type ();
52ce6436
PH
6988 if (byte_offset_p != NULL)
6989 *byte_offset_p = fld_offset;
6990 if (bit_offset_p != NULL)
6991 *bit_offset_p = bit_pos % 8;
6992 if (bit_size_p != NULL)
6993 *bit_size_p = bit_size;
dda83cd7
SM
6994 return 1;
6995 }
4c4b4cd2 6996 else if (ada_is_wrapper_field (type, i))
dda83cd7 6997 {
940da03e 6998 if (find_struct_field (name, type->field (i).type (), fld_offset,
52ce6436
PH
6999 field_type_p, byte_offset_p, bit_offset_p,
7000 bit_size_p, index_p))
dda83cd7
SM
7001 return 1;
7002 }
4c4b4cd2 7003 else if (ada_is_variant_part (type, i))
dda83cd7 7004 {
52ce6436
PH
7005 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7006 fixed type?? */
dda83cd7
SM
7007 int j;
7008 struct type *field_type
940da03e 7009 = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7010
dda83cd7
SM
7011 for (j = 0; j < field_type->num_fields (); j += 1)
7012 {
7013 if (find_struct_field (name, field_type->field (j).type (),
7014 fld_offset
7015 + TYPE_FIELD_BITPOS (field_type, j) / 8,
7016 field_type_p, byte_offset_p,
7017 bit_offset_p, bit_size_p, index_p))
7018 return 1;
7019 }
7020 }
52ce6436
PH
7021 else if (index_p != NULL)
7022 *index_p += 1;
4c4b4cd2 7023 }
828d5846
XR
7024
7025 /* Field not found so far. If this is a tagged type which
7026 has a parent, try finding that field in the parent now. */
7027
7028 if (parent_offset != -1)
7029 {
7030 int bit_pos = TYPE_FIELD_BITPOS (type, parent_offset);
7031 int fld_offset = offset + bit_pos / 8;
7032
940da03e 7033 if (find_struct_field (name, type->field (parent_offset).type (),
dda83cd7
SM
7034 fld_offset, field_type_p, byte_offset_p,
7035 bit_offset_p, bit_size_p, index_p))
7036 return 1;
828d5846
XR
7037 }
7038
4c4b4cd2
PH
7039 return 0;
7040}
7041
0963b4bd 7042/* Number of user-visible fields in record type TYPE. */
4c4b4cd2 7043
52ce6436
PH
7044static int
7045num_visible_fields (struct type *type)
7046{
7047 int n;
5b4ee69b 7048
52ce6436
PH
7049 n = 0;
7050 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
7051 return n;
7052}
14f9c5c9 7053
4c4b4cd2 7054/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7055 and search in it assuming it has (class) type TYPE.
7056 If found, return value, else return NULL.
7057
828d5846
XR
7058 Searches recursively through wrapper fields (e.g., '_parent').
7059
7060 In the case of homonyms in the tagged types, please refer to the
7061 long explanation in find_struct_field's function documentation. */
14f9c5c9 7062
4c4b4cd2 7063static struct value *
108d56a4 7064ada_search_struct_field (const char *name, struct value *arg, int offset,
dda83cd7 7065 struct type *type)
14f9c5c9
AS
7066{
7067 int i;
828d5846 7068 int parent_offset = -1;
14f9c5c9 7069
5b4ee69b 7070 type = ada_check_typedef (type);
1f704f76 7071 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7072 {
0d5cff50 7073 const char *t_field_name = TYPE_FIELD_NAME (type, i);
14f9c5c9
AS
7074
7075 if (t_field_name == NULL)
dda83cd7 7076 continue;
14f9c5c9 7077
828d5846 7078 else if (ada_is_parent_field (type, i))
dda83cd7 7079 {
828d5846
XR
7080 /* This is a field pointing us to the parent type of a tagged
7081 type. As hinted in this function's documentation, we give
7082 preference to fields in the current record first, so what
7083 we do here is just record the index of this field before
7084 we skip it. If it turns out we couldn't find our field
7085 in the current record, then we'll get back to it and search
7086 inside it whether the field might exist in the parent. */
7087
dda83cd7
SM
7088 parent_offset = i;
7089 continue;
7090 }
828d5846 7091
14f9c5c9 7092 else if (field_name_match (t_field_name, name))
dda83cd7 7093 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7094
7095 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7096 {
7097 struct value *v = /* Do not let indent join lines here. */
7098 ada_search_struct_field (name, arg,
7099 offset + TYPE_FIELD_BITPOS (type, i) / 8,
7100 type->field (i).type ());
5b4ee69b 7101
dda83cd7
SM
7102 if (v != NULL)
7103 return v;
7104 }
14f9c5c9
AS
7105
7106 else if (ada_is_variant_part (type, i))
dda83cd7 7107 {
0963b4bd 7108 /* PNH: Do we ever get here? See find_struct_field. */
dda83cd7
SM
7109 int j;
7110 struct type *field_type = ada_check_typedef (type->field (i).type ());
7111 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
4c4b4cd2 7112
dda83cd7
SM
7113 for (j = 0; j < field_type->num_fields (); j += 1)
7114 {
7115 struct value *v = ada_search_struct_field /* Force line
0963b4bd 7116 break. */
dda83cd7
SM
7117 (name, arg,
7118 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
7119 field_type->field (j).type ());
5b4ee69b 7120
dda83cd7
SM
7121 if (v != NULL)
7122 return v;
7123 }
7124 }
14f9c5c9 7125 }
828d5846
XR
7126
7127 /* Field not found so far. If this is a tagged type which
7128 has a parent, try finding that field in the parent now. */
7129
7130 if (parent_offset != -1)
7131 {
7132 struct value *v = ada_search_struct_field (
7133 name, arg, offset + TYPE_FIELD_BITPOS (type, parent_offset) / 8,
940da03e 7134 type->field (parent_offset).type ());
828d5846
XR
7135
7136 if (v != NULL)
dda83cd7 7137 return v;
828d5846
XR
7138 }
7139
14f9c5c9
AS
7140 return NULL;
7141}
d2e4a39e 7142
52ce6436
PH
7143static struct value *ada_index_struct_field_1 (int *, struct value *,
7144 int, struct type *);
7145
7146
7147/* Return field #INDEX in ARG, where the index is that returned by
7148 * find_struct_field through its INDEX_P argument. Adjust the address
7149 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
0963b4bd 7150 * If found, return value, else return NULL. */
52ce6436
PH
7151
7152static struct value *
7153ada_index_struct_field (int index, struct value *arg, int offset,
7154 struct type *type)
7155{
7156 return ada_index_struct_field_1 (&index, arg, offset, type);
7157}
7158
7159
7160/* Auxiliary function for ada_index_struct_field. Like
7161 * ada_index_struct_field, but takes index from *INDEX_P and modifies
0963b4bd 7162 * *INDEX_P. */
52ce6436
PH
7163
7164static struct value *
7165ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
7166 struct type *type)
7167{
7168 int i;
7169 type = ada_check_typedef (type);
7170
1f704f76 7171 for (i = 0; i < type->num_fields (); i += 1)
52ce6436
PH
7172 {
7173 if (TYPE_FIELD_NAME (type, i) == NULL)
dda83cd7 7174 continue;
52ce6436 7175 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7176 {
7177 struct value *v = /* Do not let indent join lines here. */
7178 ada_index_struct_field_1 (index_p, arg,
52ce6436 7179 offset + TYPE_FIELD_BITPOS (type, i) / 8,
940da03e 7180 type->field (i).type ());
5b4ee69b 7181
dda83cd7
SM
7182 if (v != NULL)
7183 return v;
7184 }
52ce6436
PH
7185
7186 else if (ada_is_variant_part (type, i))
dda83cd7 7187 {
52ce6436 7188 /* PNH: Do we ever get here? See ada_search_struct_field,
0963b4bd 7189 find_struct_field. */
52ce6436 7190 error (_("Cannot assign this kind of variant record"));
dda83cd7 7191 }
52ce6436 7192 else if (*index_p == 0)
dda83cd7 7193 return ada_value_primitive_field (arg, offset, i, type);
52ce6436
PH
7194 else
7195 *index_p -= 1;
7196 }
7197 return NULL;
7198}
7199
3b4de39c 7200/* Return a string representation of type TYPE. */
99bbb428 7201
3b4de39c 7202static std::string
99bbb428
PA
7203type_as_string (struct type *type)
7204{
d7e74731 7205 string_file tmp_stream;
99bbb428 7206
d7e74731 7207 type_print (type, "", &tmp_stream, -1);
99bbb428 7208
d7e74731 7209 return std::move (tmp_stream.string ());
99bbb428
PA
7210}
7211
14f9c5c9 7212/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7213 If DISPP is non-null, add its byte displacement from the beginning of a
7214 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7215 work for packed fields).
7216
7217 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7218 followed by "___".
14f9c5c9 7219
0963b4bd 7220 TYPE can be either a struct or union. If REFOK, TYPE may also
4c4b4cd2
PH
7221 be a (pointer or reference)+ to a struct or union, and the
7222 ultimate target type will be searched.
14f9c5c9
AS
7223
7224 Looks recursively into variant clauses and parent types.
7225
828d5846
XR
7226 In the case of homonyms in the tagged types, please refer to the
7227 long explanation in find_struct_field's function documentation.
7228
4c4b4cd2
PH
7229 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7230 TYPE is not a type of the right kind. */
14f9c5c9 7231
4c4b4cd2 7232static struct type *
a121b7c1 7233ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
dda83cd7 7234 int noerr)
14f9c5c9
AS
7235{
7236 int i;
828d5846 7237 int parent_offset = -1;
14f9c5c9
AS
7238
7239 if (name == NULL)
7240 goto BadName;
7241
76a01679 7242 if (refok && type != NULL)
4c4b4cd2
PH
7243 while (1)
7244 {
dda83cd7
SM
7245 type = ada_check_typedef (type);
7246 if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
7247 break;
7248 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 7249 }
14f9c5c9 7250
76a01679 7251 if (type == NULL
78134374
SM
7252 || (type->code () != TYPE_CODE_STRUCT
7253 && type->code () != TYPE_CODE_UNION))
14f9c5c9 7254 {
4c4b4cd2 7255 if (noerr)
dda83cd7 7256 return NULL;
99bbb428 7257
3b4de39c
PA
7258 error (_("Type %s is not a structure or union type"),
7259 type != NULL ? type_as_string (type).c_str () : _("(null)"));
14f9c5c9
AS
7260 }
7261
7262 type = to_static_fixed_type (type);
7263
1f704f76 7264 for (i = 0; i < type->num_fields (); i += 1)
14f9c5c9 7265 {
0d5cff50 7266 const char *t_field_name = TYPE_FIELD_NAME (type, i);
14f9c5c9 7267 struct type *t;
d2e4a39e 7268
14f9c5c9 7269 if (t_field_name == NULL)
dda83cd7 7270 continue;
14f9c5c9 7271
828d5846 7272 else if (ada_is_parent_field (type, i))
dda83cd7 7273 {
828d5846
XR
7274 /* This is a field pointing us to the parent type of a tagged
7275 type. As hinted in this function's documentation, we give
7276 preference to fields in the current record first, so what
7277 we do here is just record the index of this field before
7278 we skip it. If it turns out we couldn't find our field
7279 in the current record, then we'll get back to it and search
7280 inside it whether the field might exist in the parent. */
7281
dda83cd7
SM
7282 parent_offset = i;
7283 continue;
7284 }
828d5846 7285
14f9c5c9 7286 else if (field_name_match (t_field_name, name))
940da03e 7287 return type->field (i).type ();
14f9c5c9
AS
7288
7289 else if (ada_is_wrapper_field (type, i))
dda83cd7
SM
7290 {
7291 t = ada_lookup_struct_elt_type (type->field (i).type (), name,
7292 0, 1);
7293 if (t != NULL)
988f6b3d 7294 return t;
dda83cd7 7295 }
14f9c5c9
AS
7296
7297 else if (ada_is_variant_part (type, i))
dda83cd7
SM
7298 {
7299 int j;
7300 struct type *field_type = ada_check_typedef (type->field (i).type ());
4c4b4cd2 7301
dda83cd7
SM
7302 for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
7303 {
b1f33ddd 7304 /* FIXME pnh 2008/01/26: We check for a field that is
dda83cd7 7305 NOT wrapped in a struct, since the compiler sometimes
b1f33ddd 7306 generates these for unchecked variant types. Revisit
dda83cd7 7307 if the compiler changes this practice. */
0d5cff50 7308 const char *v_field_name = TYPE_FIELD_NAME (field_type, j);
988f6b3d 7309
b1f33ddd
JB
7310 if (v_field_name != NULL
7311 && field_name_match (v_field_name, name))
940da03e 7312 t = field_type->field (j).type ();
b1f33ddd 7313 else
940da03e 7314 t = ada_lookup_struct_elt_type (field_type->field (j).type (),
988f6b3d 7315 name, 0, 1);
b1f33ddd 7316
dda83cd7 7317 if (t != NULL)
988f6b3d 7318 return t;
dda83cd7
SM
7319 }
7320 }
14f9c5c9
AS
7321
7322 }
7323
828d5846
XR
7324 /* Field not found so far. If this is a tagged type which
7325 has a parent, try finding that field in the parent now. */
7326
7327 if (parent_offset != -1)
7328 {
dda83cd7 7329 struct type *t;
828d5846 7330
dda83cd7
SM
7331 t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
7332 name, 0, 1);
7333 if (t != NULL)
828d5846
XR
7334 return t;
7335 }
7336
14f9c5c9 7337BadName:
d2e4a39e 7338 if (!noerr)
14f9c5c9 7339 {
2b2798cc 7340 const char *name_str = name != NULL ? name : _("<null>");
99bbb428
PA
7341
7342 error (_("Type %s has no component named %s"),
3b4de39c 7343 type_as_string (type).c_str (), name_str);
14f9c5c9
AS
7344 }
7345
7346 return NULL;
7347}
7348
b1f33ddd
JB
7349/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7350 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7351 represents an unchecked union (that is, the variant part of a
0963b4bd 7352 record that is named in an Unchecked_Union pragma). */
b1f33ddd
JB
7353
7354static int
7355is_unchecked_variant (struct type *var_type, struct type *outer_type)
7356{
a121b7c1 7357 const char *discrim_name = ada_variant_discrim_name (var_type);
5b4ee69b 7358
988f6b3d 7359 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
b1f33ddd
JB
7360}
7361
7362
14f9c5c9 7363/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
d8af9068 7364 within OUTER, determine which variant clause (field number in VAR_TYPE,
4c4b4cd2 7365 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7366
d2e4a39e 7367int
d8af9068 7368ada_which_variant_applies (struct type *var_type, struct value *outer)
14f9c5c9
AS
7369{
7370 int others_clause;
7371 int i;
a121b7c1 7372 const char *discrim_name = ada_variant_discrim_name (var_type);
0c281816 7373 struct value *discrim;
14f9c5c9
AS
7374 LONGEST discrim_val;
7375
012370f6
TT
7376 /* Using plain value_from_contents_and_address here causes problems
7377 because we will end up trying to resolve a type that is currently
7378 being constructed. */
0c281816
JB
7379 discrim = ada_value_struct_elt (outer, discrim_name, 1);
7380 if (discrim == NULL)
14f9c5c9 7381 return -1;
0c281816 7382 discrim_val = value_as_long (discrim);
14f9c5c9
AS
7383
7384 others_clause = -1;
1f704f76 7385 for (i = 0; i < var_type->num_fields (); i += 1)
14f9c5c9
AS
7386 {
7387 if (ada_is_others_clause (var_type, i))
dda83cd7 7388 others_clause = i;
14f9c5c9 7389 else if (ada_in_variant (discrim_val, var_type, i))
dda83cd7 7390 return i;
14f9c5c9
AS
7391 }
7392
7393 return others_clause;
7394}
d2e4a39e 7395\f
14f9c5c9
AS
7396
7397
dda83cd7 7398 /* Dynamic-Sized Records */
14f9c5c9
AS
7399
7400/* Strategy: The type ostensibly attached to a value with dynamic size
7401 (i.e., a size that is not statically recorded in the debugging
7402 data) does not accurately reflect the size or layout of the value.
7403 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7404 conventional types that are constructed on the fly. */
14f9c5c9
AS
7405
7406/* There is a subtle and tricky problem here. In general, we cannot
7407 determine the size of dynamic records without its data. However,
7408 the 'struct value' data structure, which GDB uses to represent
7409 quantities in the inferior process (the target), requires the size
7410 of the type at the time of its allocation in order to reserve space
7411 for GDB's internal copy of the data. That's why the
7412 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7413 rather than struct value*s.
14f9c5c9
AS
7414
7415 However, GDB's internal history variables ($1, $2, etc.) are
7416 struct value*s containing internal copies of the data that are not, in
7417 general, the same as the data at their corresponding addresses in
7418 the target. Fortunately, the types we give to these values are all
7419 conventional, fixed-size types (as per the strategy described
7420 above), so that we don't usually have to perform the
7421 'to_fixed_xxx_type' conversions to look at their values.
7422 Unfortunately, there is one exception: if one of the internal
7423 history variables is an array whose elements are unconstrained
7424 records, then we will need to create distinct fixed types for each
7425 element selected. */
7426
7427/* The upshot of all of this is that many routines take a (type, host
7428 address, target address) triple as arguments to represent a value.
7429 The host address, if non-null, is supposed to contain an internal
7430 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7431 target at the target address. */
14f9c5c9
AS
7432
7433/* Assuming that VAL0 represents a pointer value, the result of
7434 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7435 dynamic-sized types. */
14f9c5c9 7436
d2e4a39e
AS
7437struct value *
7438ada_value_ind (struct value *val0)
14f9c5c9 7439{
c48db5ca 7440 struct value *val = value_ind (val0);
5b4ee69b 7441
b50d69b5
JG
7442 if (ada_is_tagged_type (value_type (val), 0))
7443 val = ada_tag_value_at_base_address (val);
7444
4c4b4cd2 7445 return ada_to_fixed_value (val);
14f9c5c9
AS
7446}
7447
7448/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7449 qualifiers on VAL0. */
7450
d2e4a39e
AS
7451static struct value *
7452ada_coerce_ref (struct value *val0)
7453{
78134374 7454 if (value_type (val0)->code () == TYPE_CODE_REF)
d2e4a39e
AS
7455 {
7456 struct value *val = val0;
5b4ee69b 7457
994b9211 7458 val = coerce_ref (val);
b50d69b5
JG
7459
7460 if (ada_is_tagged_type (value_type (val), 0))
7461 val = ada_tag_value_at_base_address (val);
7462
4c4b4cd2 7463 return ada_to_fixed_value (val);
d2e4a39e
AS
7464 }
7465 else
14f9c5c9
AS
7466 return val0;
7467}
7468
4c4b4cd2 7469/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7470
7471static unsigned int
ebf56fd3 7472field_alignment (struct type *type, int f)
14f9c5c9 7473{
d2e4a39e 7474 const char *name = TYPE_FIELD_NAME (type, f);
64a1bf19 7475 int len;
14f9c5c9
AS
7476 int align_offset;
7477
64a1bf19
JB
7478 /* The field name should never be null, unless the debugging information
7479 is somehow malformed. In this case, we assume the field does not
7480 require any alignment. */
7481 if (name == NULL)
7482 return 1;
7483
7484 len = strlen (name);
7485
4c4b4cd2
PH
7486 if (!isdigit (name[len - 1]))
7487 return 1;
14f9c5c9 7488
d2e4a39e 7489 if (isdigit (name[len - 2]))
14f9c5c9
AS
7490 align_offset = len - 2;
7491 else
7492 align_offset = len - 1;
7493
61012eef 7494 if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
14f9c5c9
AS
7495 return TARGET_CHAR_BIT;
7496
4c4b4cd2
PH
7497 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7498}
7499
852dff6c 7500/* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
4c4b4cd2 7501
852dff6c
JB
7502static struct symbol *
7503ada_find_any_type_symbol (const char *name)
4c4b4cd2
PH
7504{
7505 struct symbol *sym;
7506
7507 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
4186eb54 7508 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4c4b4cd2
PH
7509 return sym;
7510
4186eb54
KS
7511 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7512 return sym;
14f9c5c9
AS
7513}
7514
dddfab26
UW
7515/* Find a type named NAME. Ignores ambiguity. This routine will look
7516 solely for types defined by debug info, it will not search the GDB
7517 primitive types. */
4c4b4cd2 7518
852dff6c 7519static struct type *
ebf56fd3 7520ada_find_any_type (const char *name)
14f9c5c9 7521{
852dff6c 7522 struct symbol *sym = ada_find_any_type_symbol (name);
14f9c5c9 7523
14f9c5c9 7524 if (sym != NULL)
dddfab26 7525 return SYMBOL_TYPE (sym);
14f9c5c9 7526
dddfab26 7527 return NULL;
14f9c5c9
AS
7528}
7529
739593e0
JB
7530/* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7531 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7532 symbol, in which case it is returned. Otherwise, this looks for
7533 symbols whose name is that of NAME_SYM suffixed with "___XR".
7534 Return symbol if found, and NULL otherwise. */
4c4b4cd2 7535
c0e70c62
TT
7536static bool
7537ada_is_renaming_symbol (struct symbol *name_sym)
aeb5907d 7538{
987012b8 7539 const char *name = name_sym->linkage_name ();
c0e70c62 7540 return strstr (name, "___XR") != NULL;
4c4b4cd2
PH
7541}
7542
14f9c5c9 7543/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7544 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7545 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7546 otherwise return 0. */
7547
14f9c5c9 7548int
d2e4a39e 7549ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7550{
7551 if (type1 == NULL)
7552 return 1;
7553 else if (type0 == NULL)
7554 return 0;
78134374 7555 else if (type1->code () == TYPE_CODE_VOID)
14f9c5c9 7556 return 1;
78134374 7557 else if (type0->code () == TYPE_CODE_VOID)
14f9c5c9 7558 return 0;
7d93a1e0 7559 else if (type1->name () == NULL && type0->name () != NULL)
4c4b4cd2 7560 return 1;
ad82864c 7561 else if (ada_is_constrained_packed_array_type (type0))
14f9c5c9 7562 return 1;
4c4b4cd2 7563 else if (ada_is_array_descriptor_type (type0)
dda83cd7 7564 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7565 return 1;
aeb5907d
JB
7566 else
7567 {
7d93a1e0
SM
7568 const char *type0_name = type0->name ();
7569 const char *type1_name = type1->name ();
aeb5907d
JB
7570
7571 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
7572 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
7573 return 1;
7574 }
14f9c5c9
AS
7575 return 0;
7576}
7577
e86ca25f
TT
7578/* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7579 null. */
4c4b4cd2 7580
0d5cff50 7581const char *
d2e4a39e 7582ada_type_name (struct type *type)
14f9c5c9 7583{
d2e4a39e 7584 if (type == NULL)
14f9c5c9 7585 return NULL;
7d93a1e0 7586 return type->name ();
14f9c5c9
AS
7587}
7588
b4ba55a1
JB
7589/* Search the list of "descriptive" types associated to TYPE for a type
7590 whose name is NAME. */
7591
7592static struct type *
7593find_parallel_type_by_descriptive_type (struct type *type, const char *name)
7594{
931e5bc3 7595 struct type *result, *tmp;
b4ba55a1 7596
c6044dd1
JB
7597 if (ada_ignore_descriptive_types_p)
7598 return NULL;
7599
b4ba55a1
JB
7600 /* If there no descriptive-type info, then there is no parallel type
7601 to be found. */
7602 if (!HAVE_GNAT_AUX_INFO (type))
7603 return NULL;
7604
7605 result = TYPE_DESCRIPTIVE_TYPE (type);
7606 while (result != NULL)
7607 {
0d5cff50 7608 const char *result_name = ada_type_name (result);
b4ba55a1
JB
7609
7610 if (result_name == NULL)
dda83cd7
SM
7611 {
7612 warning (_("unexpected null name on descriptive type"));
7613 return NULL;
7614 }
b4ba55a1
JB
7615
7616 /* If the names match, stop. */
7617 if (strcmp (result_name, name) == 0)
7618 break;
7619
7620 /* Otherwise, look at the next item on the list, if any. */
7621 if (HAVE_GNAT_AUX_INFO (result))
931e5bc3
JG
7622 tmp = TYPE_DESCRIPTIVE_TYPE (result);
7623 else
7624 tmp = NULL;
7625
7626 /* If not found either, try after having resolved the typedef. */
7627 if (tmp != NULL)
7628 result = tmp;
b4ba55a1 7629 else
931e5bc3 7630 {
f168693b 7631 result = check_typedef (result);
931e5bc3
JG
7632 if (HAVE_GNAT_AUX_INFO (result))
7633 result = TYPE_DESCRIPTIVE_TYPE (result);
7634 else
7635 result = NULL;
7636 }
b4ba55a1
JB
7637 }
7638
7639 /* If we didn't find a match, see whether this is a packed array. With
7640 older compilers, the descriptive type information is either absent or
7641 irrelevant when it comes to packed arrays so the above lookup fails.
7642 Fall back to using a parallel lookup by name in this case. */
12ab9e09 7643 if (result == NULL && ada_is_constrained_packed_array_type (type))
b4ba55a1
JB
7644 return ada_find_any_type (name);
7645
7646 return result;
7647}
7648
7649/* Find a parallel type to TYPE with the specified NAME, using the
7650 descriptive type taken from the debugging information, if available,
7651 and otherwise using the (slower) name-based method. */
7652
7653static struct type *
7654ada_find_parallel_type_with_name (struct type *type, const char *name)
7655{
7656 struct type *result = NULL;
7657
7658 if (HAVE_GNAT_AUX_INFO (type))
7659 result = find_parallel_type_by_descriptive_type (type, name);
7660 else
7661 result = ada_find_any_type (name);
7662
7663 return result;
7664}
7665
7666/* Same as above, but specify the name of the parallel type by appending
4c4b4cd2 7667 SUFFIX to the name of TYPE. */
14f9c5c9 7668
d2e4a39e 7669struct type *
ebf56fd3 7670ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7671{
0d5cff50 7672 char *name;
fe978cb0 7673 const char *type_name = ada_type_name (type);
14f9c5c9 7674 int len;
d2e4a39e 7675
fe978cb0 7676 if (type_name == NULL)
14f9c5c9
AS
7677 return NULL;
7678
fe978cb0 7679 len = strlen (type_name);
14f9c5c9 7680
b4ba55a1 7681 name = (char *) alloca (len + strlen (suffix) + 1);
14f9c5c9 7682
fe978cb0 7683 strcpy (name, type_name);
14f9c5c9
AS
7684 strcpy (name + len, suffix);
7685
b4ba55a1 7686 return ada_find_parallel_type_with_name (type, name);
14f9c5c9
AS
7687}
7688
14f9c5c9 7689/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7690 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7691
d2e4a39e
AS
7692static struct type *
7693dynamic_template_type (struct type *type)
14f9c5c9 7694{
61ee279c 7695 type = ada_check_typedef (type);
14f9c5c9 7696
78134374 7697 if (type == NULL || type->code () != TYPE_CODE_STRUCT
d2e4a39e 7698 || ada_type_name (type) == NULL)
14f9c5c9 7699 return NULL;
d2e4a39e 7700 else
14f9c5c9
AS
7701 {
7702 int len = strlen (ada_type_name (type));
5b4ee69b 7703
4c4b4cd2 7704 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
dda83cd7 7705 return type;
14f9c5c9 7706 else
dda83cd7 7707 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7708 }
7709}
7710
7711/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7712 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7713
d2e4a39e
AS
7714static int
7715is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
7716{
7717 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
5b4ee69b 7718
d2e4a39e 7719 return name != NULL
940da03e 7720 && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
14f9c5c9
AS
7721 && strstr (name, "___XVL") != NULL;
7722}
7723
4c4b4cd2
PH
7724/* The index of the variant field of TYPE, or -1 if TYPE does not
7725 represent a variant record type. */
14f9c5c9 7726
d2e4a39e 7727static int
4c4b4cd2 7728variant_field_index (struct type *type)
14f9c5c9
AS
7729{
7730 int f;
7731
78134374 7732 if (type == NULL || type->code () != TYPE_CODE_STRUCT)
4c4b4cd2
PH
7733 return -1;
7734
1f704f76 7735 for (f = 0; f < type->num_fields (); f += 1)
4c4b4cd2
PH
7736 {
7737 if (ada_is_variant_part (type, f))
dda83cd7 7738 return f;
4c4b4cd2
PH
7739 }
7740 return -1;
14f9c5c9
AS
7741}
7742
4c4b4cd2
PH
7743/* A record type with no fields. */
7744
d2e4a39e 7745static struct type *
fe978cb0 7746empty_record (struct type *templ)
14f9c5c9 7747{
fe978cb0 7748 struct type *type = alloc_type_copy (templ);
5b4ee69b 7749
67607e24 7750 type->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7751 INIT_NONE_SPECIFIC (type);
d0e39ea2 7752 type->set_name ("<empty>");
14f9c5c9
AS
7753 TYPE_LENGTH (type) = 0;
7754 return type;
7755}
7756
7757/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7758 the value of type TYPE at VALADDR or ADDRESS (see comments at
7759 the beginning of this section) VAL according to GNAT conventions.
7760 DVAL0 should describe the (portion of a) record that contains any
df407dfe 7761 necessary discriminants. It should be NULL if value_type (VAL) is
14f9c5c9
AS
7762 an outer-level type (i.e., as opposed to a branch of a variant.) A
7763 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7764 of the variant.
14f9c5c9 7765
4c4b4cd2
PH
7766 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7767 length are not statically known are discarded. As a consequence,
7768 VALADDR, ADDRESS and DVAL0 are ignored.
7769
7770 NOTE: Limitations: For now, we assume that dynamic fields and
7771 variants occupy whole numbers of bytes. However, they need not be
7772 byte-aligned. */
7773
7774struct type *
10a2c479 7775ada_template_to_fixed_record_type_1 (struct type *type,
fc1a4b47 7776 const gdb_byte *valaddr,
dda83cd7
SM
7777 CORE_ADDR address, struct value *dval0,
7778 int keep_dynamic_fields)
14f9c5c9 7779{
d2e4a39e
AS
7780 struct value *mark = value_mark ();
7781 struct value *dval;
7782 struct type *rtype;
14f9c5c9 7783 int nfields, bit_len;
4c4b4cd2 7784 int variant_field;
14f9c5c9 7785 long off;
d94e4f4f 7786 int fld_bit_len;
14f9c5c9
AS
7787 int f;
7788
4c4b4cd2
PH
7789 /* Compute the number of fields in this record type that are going
7790 to be processed: unless keep_dynamic_fields, this includes only
7791 fields whose position and length are static will be processed. */
7792 if (keep_dynamic_fields)
1f704f76 7793 nfields = type->num_fields ();
4c4b4cd2
PH
7794 else
7795 {
7796 nfields = 0;
1f704f76 7797 while (nfields < type->num_fields ()
dda83cd7
SM
7798 && !ada_is_variant_part (type, nfields)
7799 && !is_dynamic_field (type, nfields))
7800 nfields++;
4c4b4cd2
PH
7801 }
7802
e9bb382b 7803 rtype = alloc_type_copy (type);
67607e24 7804 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 7805 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 7806 rtype->set_num_fields (nfields);
3cabb6b0
SM
7807 rtype->set_fields
7808 ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
d0e39ea2 7809 rtype->set_name (ada_type_name (type));
9cdd0d12 7810 rtype->set_is_fixed_instance (true);
14f9c5c9 7811
d2e4a39e
AS
7812 off = 0;
7813 bit_len = 0;
4c4b4cd2
PH
7814 variant_field = -1;
7815
14f9c5c9
AS
7816 for (f = 0; f < nfields; f += 1)
7817 {
a89febbd 7818 off = align_up (off, field_alignment (type, f))
6c038f32 7819 + TYPE_FIELD_BITPOS (type, f);
ceacbf6e 7820 SET_FIELD_BITPOS (rtype->field (f), off);
d2e4a39e 7821 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7822
d2e4a39e 7823 if (ada_is_variant_part (type, f))
dda83cd7
SM
7824 {
7825 variant_field = f;
7826 fld_bit_len = 0;
7827 }
14f9c5c9 7828 else if (is_dynamic_field (type, f))
dda83cd7 7829 {
284614f0
JB
7830 const gdb_byte *field_valaddr = valaddr;
7831 CORE_ADDR field_address = address;
7832 struct type *field_type =
940da03e 7833 TYPE_TARGET_TYPE (type->field (f).type ());
284614f0 7834
dda83cd7 7835 if (dval0 == NULL)
b5304971
JG
7836 {
7837 /* rtype's length is computed based on the run-time
7838 value of discriminants. If the discriminants are not
7839 initialized, the type size may be completely bogus and
0963b4bd 7840 GDB may fail to allocate a value for it. So check the
b5304971 7841 size first before creating the value. */
c1b5a1a6 7842 ada_ensure_varsize_limit (rtype);
012370f6
TT
7843 /* Using plain value_from_contents_and_address here
7844 causes problems because we will end up trying to
7845 resolve a type that is currently being
7846 constructed. */
7847 dval = value_from_contents_and_address_unresolved (rtype,
7848 valaddr,
7849 address);
9f1f738a 7850 rtype = value_type (dval);
b5304971 7851 }
dda83cd7
SM
7852 else
7853 dval = dval0;
4c4b4cd2 7854
284614f0
JB
7855 /* If the type referenced by this field is an aligner type, we need
7856 to unwrap that aligner type, because its size might not be set.
7857 Keeping the aligner type would cause us to compute the wrong
7858 size for this field, impacting the offset of the all the fields
7859 that follow this one. */
7860 if (ada_is_aligner_type (field_type))
7861 {
7862 long field_offset = TYPE_FIELD_BITPOS (field_type, f);
7863
7864 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7865 field_address = cond_offset_target (field_address, field_offset);
7866 field_type = ada_aligned_type (field_type);
7867 }
7868
7869 field_valaddr = cond_offset_host (field_valaddr,
7870 off / TARGET_CHAR_BIT);
7871 field_address = cond_offset_target (field_address,
7872 off / TARGET_CHAR_BIT);
7873
7874 /* Get the fixed type of the field. Note that, in this case,
7875 we do not want to get the real type out of the tag: if
7876 the current field is the parent part of a tagged record,
7877 we will get the tag of the object. Clearly wrong: the real
7878 type of the parent is not the real type of the child. We
7879 would end up in an infinite loop. */
7880 field_type = ada_get_base_type (field_type);
7881 field_type = ada_to_fixed_type (field_type, field_valaddr,
7882 field_address, dval, 0);
27f2a97b
JB
7883 /* If the field size is already larger than the maximum
7884 object size, then the record itself will necessarily
7885 be larger than the maximum object size. We need to make
7886 this check now, because the size might be so ridiculously
7887 large (due to an uninitialized variable in the inferior)
7888 that it would cause an overflow when adding it to the
7889 record size. */
c1b5a1a6 7890 ada_ensure_varsize_limit (field_type);
284614f0 7891
5d14b6e5 7892 rtype->field (f).set_type (field_type);
dda83cd7 7893 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
27f2a97b
JB
7894 /* The multiplication can potentially overflow. But because
7895 the field length has been size-checked just above, and
7896 assuming that the maximum size is a reasonable value,
7897 an overflow should not happen in practice. So rather than
7898 adding overflow recovery code to this already complex code,
7899 we just assume that it's not going to happen. */
dda83cd7
SM
7900 fld_bit_len =
7901 TYPE_LENGTH (rtype->field (f).type ()) * TARGET_CHAR_BIT;
7902 }
14f9c5c9 7903 else
dda83cd7 7904 {
5ded5331
JB
7905 /* Note: If this field's type is a typedef, it is important
7906 to preserve the typedef layer.
7907
7908 Otherwise, we might be transforming a typedef to a fat
7909 pointer (encoding a pointer to an unconstrained array),
7910 into a basic fat pointer (encoding an unconstrained
7911 array). As both types are implemented using the same
7912 structure, the typedef is the only clue which allows us
7913 to distinguish between the two options. Stripping it
7914 would prevent us from printing this field appropriately. */
dda83cd7
SM
7915 rtype->field (f).set_type (type->field (f).type ());
7916 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7917 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7918 fld_bit_len =
7919 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7920 else
5ded5331 7921 {
940da03e 7922 struct type *field_type = type->field (f).type ();
5ded5331
JB
7923
7924 /* We need to be careful of typedefs when computing
7925 the length of our field. If this is a typedef,
7926 get the length of the target type, not the length
7927 of the typedef. */
78134374 7928 if (field_type->code () == TYPE_CODE_TYPEDEF)
5ded5331
JB
7929 field_type = ada_typedef_target_type (field_type);
7930
dda83cd7
SM
7931 fld_bit_len =
7932 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
5ded5331 7933 }
dda83cd7 7934 }
14f9c5c9 7935 if (off + fld_bit_len > bit_len)
dda83cd7 7936 bit_len = off + fld_bit_len;
d94e4f4f 7937 off += fld_bit_len;
4c4b4cd2 7938 TYPE_LENGTH (rtype) =
dda83cd7 7939 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7940 }
4c4b4cd2
PH
7941
7942 /* We handle the variant part, if any, at the end because of certain
b1f33ddd 7943 odd cases in which it is re-ordered so as NOT to be the last field of
4c4b4cd2
PH
7944 the record. This can happen in the presence of representation
7945 clauses. */
7946 if (variant_field >= 0)
7947 {
7948 struct type *branch_type;
7949
7950 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7951
7952 if (dval0 == NULL)
9f1f738a 7953 {
012370f6
TT
7954 /* Using plain value_from_contents_and_address here causes
7955 problems because we will end up trying to resolve a type
7956 that is currently being constructed. */
7957 dval = value_from_contents_and_address_unresolved (rtype, valaddr,
7958 address);
9f1f738a
SA
7959 rtype = value_type (dval);
7960 }
4c4b4cd2 7961 else
dda83cd7 7962 dval = dval0;
4c4b4cd2
PH
7963
7964 branch_type =
dda83cd7
SM
7965 to_fixed_variant_branch_type
7966 (type->field (variant_field).type (),
7967 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7968 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
4c4b4cd2 7969 if (branch_type == NULL)
dda83cd7
SM
7970 {
7971 for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
7972 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 7973 rtype->set_num_fields (rtype->num_fields () - 1);
dda83cd7 7974 }
4c4b4cd2 7975 else
dda83cd7
SM
7976 {
7977 rtype->field (variant_field).set_type (branch_type);
7978 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7979 fld_bit_len =
7980 TYPE_LENGTH (rtype->field (variant_field).type ()) *
7981 TARGET_CHAR_BIT;
7982 if (off + fld_bit_len > bit_len)
7983 bit_len = off + fld_bit_len;
7984 TYPE_LENGTH (rtype) =
7985 align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7986 }
4c4b4cd2
PH
7987 }
7988
714e53ab
PH
7989 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7990 should contain the alignment of that record, which should be a strictly
7991 positive value. If null or negative, then something is wrong, most
7992 probably in the debug info. In that case, we don't round up the size
0963b4bd 7993 of the resulting type. If this record is not part of another structure,
714e53ab
PH
7994 the current RTYPE length might be good enough for our purposes. */
7995 if (TYPE_LENGTH (type) <= 0)
7996 {
7d93a1e0 7997 if (rtype->name ())
cc1defb1 7998 warning (_("Invalid type size for `%s' detected: %s."),
7d93a1e0 7999 rtype->name (), pulongest (TYPE_LENGTH (type)));
323e0a4a 8000 else
cc1defb1
KS
8001 warning (_("Invalid type size for <unnamed> detected: %s."),
8002 pulongest (TYPE_LENGTH (type)));
714e53ab
PH
8003 }
8004 else
8005 {
a89febbd
TT
8006 TYPE_LENGTH (rtype) = align_up (TYPE_LENGTH (rtype),
8007 TYPE_LENGTH (type));
714e53ab 8008 }
14f9c5c9
AS
8009
8010 value_free_to_mark (mark);
d2e4a39e 8011 if (TYPE_LENGTH (rtype) > varsize_limit)
323e0a4a 8012 error (_("record type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
8013 return rtype;
8014}
8015
4c4b4cd2
PH
8016/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8017 of 1. */
14f9c5c9 8018
d2e4a39e 8019static struct type *
fc1a4b47 8020template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8021 CORE_ADDR address, struct value *dval0)
4c4b4cd2
PH
8022{
8023 return ada_template_to_fixed_record_type_1 (type, valaddr,
dda83cd7 8024 address, dval0, 1);
4c4b4cd2
PH
8025}
8026
8027/* An ordinary record type in which ___XVL-convention fields and
8028 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8029 static approximations, containing all possible fields. Uses
8030 no runtime values. Useless for use in values, but that's OK,
8031 since the results are used only for type determinations. Works on both
8032 structs and unions. Representation note: to save space, we memorize
8033 the result of this function in the TYPE_TARGET_TYPE of the
8034 template type. */
8035
8036static struct type *
8037template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
8038{
8039 struct type *type;
8040 int nfields;
8041 int f;
8042
9e195661 8043 /* No need no do anything if the input type is already fixed. */
22c4c60c 8044 if (type0->is_fixed_instance ())
9e195661
PMR
8045 return type0;
8046
8047 /* Likewise if we already have computed the static approximation. */
4c4b4cd2
PH
8048 if (TYPE_TARGET_TYPE (type0) != NULL)
8049 return TYPE_TARGET_TYPE (type0);
8050
9e195661 8051 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
4c4b4cd2 8052 type = type0;
1f704f76 8053 nfields = type0->num_fields ();
9e195661
PMR
8054
8055 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8056 recompute all over next time. */
8057 TYPE_TARGET_TYPE (type0) = type;
14f9c5c9
AS
8058
8059 for (f = 0; f < nfields; f += 1)
8060 {
940da03e 8061 struct type *field_type = type0->field (f).type ();
4c4b4cd2 8062 struct type *new_type;
14f9c5c9 8063
4c4b4cd2 8064 if (is_dynamic_field (type0, f))
460efde1
JB
8065 {
8066 field_type = ada_check_typedef (field_type);
dda83cd7 8067 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
460efde1 8068 }
14f9c5c9 8069 else
dda83cd7 8070 new_type = static_unwrap_type (field_type);
9e195661
PMR
8071
8072 if (new_type != field_type)
8073 {
8074 /* Clone TYPE0 only the first time we get a new field type. */
8075 if (type == type0)
8076 {
8077 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
78134374 8078 type->set_code (type0->code ());
8ecb59f8 8079 INIT_NONE_SPECIFIC (type);
5e33d5f4 8080 type->set_num_fields (nfields);
3cabb6b0
SM
8081
8082 field *fields =
8083 ((struct field *)
8084 TYPE_ALLOC (type, nfields * sizeof (struct field)));
80fc5e77 8085 memcpy (fields, type0->fields (),
9e195661 8086 sizeof (struct field) * nfields);
3cabb6b0
SM
8087 type->set_fields (fields);
8088
d0e39ea2 8089 type->set_name (ada_type_name (type0));
9cdd0d12 8090 type->set_is_fixed_instance (true);
9e195661
PMR
8091 TYPE_LENGTH (type) = 0;
8092 }
5d14b6e5 8093 type->field (f).set_type (new_type);
9e195661
PMR
8094 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
8095 }
14f9c5c9 8096 }
9e195661 8097
14f9c5c9
AS
8098 return type;
8099}
8100
4c4b4cd2 8101/* Given an object of type TYPE whose contents are at VALADDR and
5823c3ef
JB
8102 whose address in memory is ADDRESS, returns a revision of TYPE,
8103 which should be a non-dynamic-sized record, in which the variant
8104 part, if any, is replaced with the appropriate branch. Looks
4c4b4cd2
PH
8105 for discriminant values in DVAL0, which can be NULL if the record
8106 contains the necessary discriminant values. */
8107
d2e4a39e 8108static struct type *
fc1a4b47 8109to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
dda83cd7 8110 CORE_ADDR address, struct value *dval0)
14f9c5c9 8111{
d2e4a39e 8112 struct value *mark = value_mark ();
4c4b4cd2 8113 struct value *dval;
d2e4a39e 8114 struct type *rtype;
14f9c5c9 8115 struct type *branch_type;
1f704f76 8116 int nfields = type->num_fields ();
4c4b4cd2 8117 int variant_field = variant_field_index (type);
14f9c5c9 8118
4c4b4cd2 8119 if (variant_field == -1)
14f9c5c9
AS
8120 return type;
8121
4c4b4cd2 8122 if (dval0 == NULL)
9f1f738a
SA
8123 {
8124 dval = value_from_contents_and_address (type, valaddr, address);
8125 type = value_type (dval);
8126 }
4c4b4cd2
PH
8127 else
8128 dval = dval0;
8129
e9bb382b 8130 rtype = alloc_type_copy (type);
67607e24 8131 rtype->set_code (TYPE_CODE_STRUCT);
8ecb59f8 8132 INIT_NONE_SPECIFIC (rtype);
5e33d5f4 8133 rtype->set_num_fields (nfields);
3cabb6b0
SM
8134
8135 field *fields =
d2e4a39e 8136 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
80fc5e77 8137 memcpy (fields, type->fields (), sizeof (struct field) * nfields);
3cabb6b0
SM
8138 rtype->set_fields (fields);
8139
d0e39ea2 8140 rtype->set_name (ada_type_name (type));
9cdd0d12 8141 rtype->set_is_fixed_instance (true);
14f9c5c9
AS
8142 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
8143
4c4b4cd2 8144 branch_type = to_fixed_variant_branch_type
940da03e 8145 (type->field (variant_field).type (),
d2e4a39e 8146 cond_offset_host (valaddr,
dda83cd7
SM
8147 TYPE_FIELD_BITPOS (type, variant_field)
8148 / TARGET_CHAR_BIT),
d2e4a39e 8149 cond_offset_target (address,
dda83cd7
SM
8150 TYPE_FIELD_BITPOS (type, variant_field)
8151 / TARGET_CHAR_BIT), dval);
d2e4a39e 8152 if (branch_type == NULL)
14f9c5c9 8153 {
4c4b4cd2 8154 int f;
5b4ee69b 8155
4c4b4cd2 8156 for (f = variant_field + 1; f < nfields; f += 1)
dda83cd7 8157 rtype->field (f - 1) = rtype->field (f);
5e33d5f4 8158 rtype->set_num_fields (rtype->num_fields () - 1);
14f9c5c9
AS
8159 }
8160 else
8161 {
5d14b6e5 8162 rtype->field (variant_field).set_type (branch_type);
4c4b4cd2
PH
8163 TYPE_FIELD_NAME (rtype, variant_field) = "S";
8164 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 8165 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 8166 }
940da03e 8167 TYPE_LENGTH (rtype) -= TYPE_LENGTH (type->field (variant_field).type ());
d2e4a39e 8168
4c4b4cd2 8169 value_free_to_mark (mark);
14f9c5c9
AS
8170 return rtype;
8171}
8172
8173/* An ordinary record type (with fixed-length fields) that describes
8174 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8175 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
8176 should be in DVAL, a record value; it may be NULL if the object
8177 at ADDR itself contains any necessary discriminant values.
8178 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8179 values from the record are needed. Except in the case that DVAL,
8180 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8181 unchecked) is replaced by a particular branch of the variant.
8182
8183 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8184 is questionable and may be removed. It can arise during the
8185 processing of an unconstrained-array-of-record type where all the
8186 variant branches have exactly the same size. This is because in
8187 such cases, the compiler does not bother to use the XVS convention
8188 when encoding the record. I am currently dubious of this
8189 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 8190
d2e4a39e 8191static struct type *
fc1a4b47 8192to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
dda83cd7 8193 CORE_ADDR address, struct value *dval)
14f9c5c9 8194{
d2e4a39e 8195 struct type *templ_type;
14f9c5c9 8196
22c4c60c 8197 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8198 return type0;
8199
d2e4a39e 8200 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
8201
8202 if (templ_type != NULL)
8203 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
8204 else if (variant_field_index (type0) >= 0)
8205 {
8206 if (dval == NULL && valaddr == NULL && address == 0)
dda83cd7 8207 return type0;
4c4b4cd2 8208 return to_record_with_fixed_variant_part (type0, valaddr, address,
dda83cd7 8209 dval);
4c4b4cd2 8210 }
14f9c5c9
AS
8211 else
8212 {
9cdd0d12 8213 type0->set_is_fixed_instance (true);
14f9c5c9
AS
8214 return type0;
8215 }
8216
8217}
8218
8219/* An ordinary record type (with fixed-length fields) that describes
8220 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8221 union type. Any necessary discriminants' values should be in DVAL,
8222 a record value. That is, this routine selects the appropriate
8223 branch of the union at ADDR according to the discriminant value
b1f33ddd 8224 indicated in the union's type name. Returns VAR_TYPE0 itself if
0963b4bd 8225 it represents a variant subject to a pragma Unchecked_Union. */
14f9c5c9 8226
d2e4a39e 8227static struct type *
fc1a4b47 8228to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
dda83cd7 8229 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8230{
8231 int which;
d2e4a39e
AS
8232 struct type *templ_type;
8233 struct type *var_type;
14f9c5c9 8234
78134374 8235 if (var_type0->code () == TYPE_CODE_PTR)
14f9c5c9 8236 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 8237 else
14f9c5c9
AS
8238 var_type = var_type0;
8239
8240 templ_type = ada_find_parallel_type (var_type, "___XVU");
8241
8242 if (templ_type != NULL)
8243 var_type = templ_type;
8244
b1f33ddd
JB
8245 if (is_unchecked_variant (var_type, value_type (dval)))
8246 return var_type0;
d8af9068 8247 which = ada_which_variant_applies (var_type, dval);
14f9c5c9
AS
8248
8249 if (which < 0)
e9bb382b 8250 return empty_record (var_type);
14f9c5c9 8251 else if (is_dynamic_field (var_type, which))
4c4b4cd2 8252 return to_fixed_record_type
940da03e 8253 (TYPE_TARGET_TYPE (var_type->field (which).type ()),
d2e4a39e 8254 valaddr, address, dval);
940da03e 8255 else if (variant_field_index (var_type->field (which).type ()) >= 0)
d2e4a39e
AS
8256 return
8257 to_fixed_record_type
940da03e 8258 (var_type->field (which).type (), valaddr, address, dval);
14f9c5c9 8259 else
940da03e 8260 return var_type->field (which).type ();
14f9c5c9
AS
8261}
8262
8908fca5
JB
8263/* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8264 ENCODING_TYPE, a type following the GNAT conventions for discrete
8265 type encodings, only carries redundant information. */
8266
8267static int
8268ada_is_redundant_range_encoding (struct type *range_type,
8269 struct type *encoding_type)
8270{
108d56a4 8271 const char *bounds_str;
8908fca5
JB
8272 int n;
8273 LONGEST lo, hi;
8274
78134374 8275 gdb_assert (range_type->code () == TYPE_CODE_RANGE);
8908fca5 8276
78134374
SM
8277 if (get_base_type (range_type)->code ()
8278 != get_base_type (encoding_type)->code ())
005e2509
JB
8279 {
8280 /* The compiler probably used a simple base type to describe
8281 the range type instead of the range's actual base type,
8282 expecting us to get the real base type from the encoding
8283 anyway. In this situation, the encoding cannot be ignored
8284 as redundant. */
8285 return 0;
8286 }
8287
8908fca5
JB
8288 if (is_dynamic_type (range_type))
8289 return 0;
8290
7d93a1e0 8291 if (encoding_type->name () == NULL)
8908fca5
JB
8292 return 0;
8293
7d93a1e0 8294 bounds_str = strstr (encoding_type->name (), "___XDLU_");
8908fca5
JB
8295 if (bounds_str == NULL)
8296 return 0;
8297
8298 n = 8; /* Skip "___XDLU_". */
8299 if (!ada_scan_number (bounds_str, n, &lo, &n))
8300 return 0;
5537ddd0 8301 if (range_type->bounds ()->low.const_val () != lo)
8908fca5
JB
8302 return 0;
8303
8304 n += 2; /* Skip the "__" separator between the two bounds. */
8305 if (!ada_scan_number (bounds_str, n, &hi, &n))
8306 return 0;
5537ddd0 8307 if (range_type->bounds ()->high.const_val () != hi)
8908fca5
JB
8308 return 0;
8309
8310 return 1;
8311}
8312
8313/* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8314 a type following the GNAT encoding for describing array type
8315 indices, only carries redundant information. */
8316
8317static int
8318ada_is_redundant_index_type_desc (struct type *array_type,
8319 struct type *desc_type)
8320{
8321 struct type *this_layer = check_typedef (array_type);
8322 int i;
8323
1f704f76 8324 for (i = 0; i < desc_type->num_fields (); i++)
8908fca5 8325 {
3d967001 8326 if (!ada_is_redundant_range_encoding (this_layer->index_type (),
940da03e 8327 desc_type->field (i).type ()))
8908fca5
JB
8328 return 0;
8329 this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
8330 }
8331
8332 return 1;
8333}
8334
14f9c5c9
AS
8335/* Assuming that TYPE0 is an array type describing the type of a value
8336 at ADDR, and that DVAL describes a record containing any
8337 discriminants used in TYPE0, returns a type for the value that
8338 contains no dynamic components (that is, no components whose sizes
8339 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8340 true, gives an error message if the resulting type's size is over
4c4b4cd2 8341 varsize_limit. */
14f9c5c9 8342
d2e4a39e
AS
8343static struct type *
8344to_fixed_array_type (struct type *type0, struct value *dval,
dda83cd7 8345 int ignore_too_big)
14f9c5c9 8346{
d2e4a39e
AS
8347 struct type *index_type_desc;
8348 struct type *result;
ad82864c 8349 int constrained_packed_array_p;
931e5bc3 8350 static const char *xa_suffix = "___XA";
14f9c5c9 8351
b0dd7688 8352 type0 = ada_check_typedef (type0);
22c4c60c 8353 if (type0->is_fixed_instance ())
4c4b4cd2 8354 return type0;
14f9c5c9 8355
ad82864c
JB
8356 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
8357 if (constrained_packed_array_p)
75fd6a26
TT
8358 {
8359 type0 = decode_constrained_packed_array_type (type0);
8360 if (type0 == nullptr)
8361 error (_("could not decode constrained packed array type"));
8362 }
284614f0 8363
931e5bc3
JG
8364 index_type_desc = ada_find_parallel_type (type0, xa_suffix);
8365
8366 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8367 encoding suffixed with 'P' may still be generated. If so,
8368 it should be used to find the XA type. */
8369
8370 if (index_type_desc == NULL)
8371 {
1da0522e 8372 const char *type_name = ada_type_name (type0);
931e5bc3 8373
1da0522e 8374 if (type_name != NULL)
931e5bc3 8375 {
1da0522e 8376 const int len = strlen (type_name);
931e5bc3
JG
8377 char *name = (char *) alloca (len + strlen (xa_suffix));
8378
1da0522e 8379 if (type_name[len - 1] == 'P')
931e5bc3 8380 {
1da0522e 8381 strcpy (name, type_name);
931e5bc3
JG
8382 strcpy (name + len - 1, xa_suffix);
8383 index_type_desc = ada_find_parallel_type_with_name (type0, name);
8384 }
8385 }
8386 }
8387
28c85d6c 8388 ada_fixup_array_indexes_type (index_type_desc);
8908fca5
JB
8389 if (index_type_desc != NULL
8390 && ada_is_redundant_index_type_desc (type0, index_type_desc))
8391 {
8392 /* Ignore this ___XA parallel type, as it does not bring any
8393 useful information. This allows us to avoid creating fixed
8394 versions of the array's index types, which would be identical
8395 to the original ones. This, in turn, can also help avoid
8396 the creation of fixed versions of the array itself. */
8397 index_type_desc = NULL;
8398 }
8399
14f9c5c9
AS
8400 if (index_type_desc == NULL)
8401 {
61ee279c 8402 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
5b4ee69b 8403
14f9c5c9 8404 /* NOTE: elt_type---the fixed version of elt_type0---should never
dda83cd7
SM
8405 depend on the contents of the array in properly constructed
8406 debugging data. */
529cad9c 8407 /* Create a fixed version of the array element type.
dda83cd7
SM
8408 We're not providing the address of an element here,
8409 and thus the actual object value cannot be inspected to do
8410 the conversion. This should not be a problem, since arrays of
8411 unconstrained objects are not allowed. In particular, all
8412 the elements of an array of a tagged type should all be of
8413 the same type specified in the debugging info. No need to
8414 consult the object tag. */
1ed6ede0 8415 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
14f9c5c9 8416
284614f0
JB
8417 /* Make sure we always create a new array type when dealing with
8418 packed array types, since we're going to fix-up the array
8419 type length and element bitsize a little further down. */
ad82864c 8420 if (elt_type0 == elt_type && !constrained_packed_array_p)
dda83cd7 8421 result = type0;
14f9c5c9 8422 else
dda83cd7
SM
8423 result = create_array_type (alloc_type_copy (type0),
8424 elt_type, type0->index_type ());
14f9c5c9
AS
8425 }
8426 else
8427 {
8428 int i;
8429 struct type *elt_type0;
8430
8431 elt_type0 = type0;
1f704f76 8432 for (i = index_type_desc->num_fields (); i > 0; i -= 1)
dda83cd7 8433 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8434
8435 /* NOTE: result---the fixed version of elt_type0---should never
dda83cd7
SM
8436 depend on the contents of the array in properly constructed
8437 debugging data. */
529cad9c 8438 /* Create a fixed version of the array element type.
dda83cd7
SM
8439 We're not providing the address of an element here,
8440 and thus the actual object value cannot be inspected to do
8441 the conversion. This should not be a problem, since arrays of
8442 unconstrained objects are not allowed. In particular, all
8443 the elements of an array of a tagged type should all be of
8444 the same type specified in the debugging info. No need to
8445 consult the object tag. */
1ed6ede0 8446 result =
dda83cd7 8447 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
1ce677a4
UW
8448
8449 elt_type0 = type0;
1f704f76 8450 for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
dda83cd7
SM
8451 {
8452 struct type *range_type =
8453 to_fixed_range_type (index_type_desc->field (i).type (), dval);
5b4ee69b 8454
dda83cd7
SM
8455 result = create_array_type (alloc_type_copy (elt_type0),
8456 result, range_type);
1ce677a4 8457 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
dda83cd7 8458 }
d2e4a39e 8459 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
dda83cd7 8460 error (_("array type with dynamic size is larger than varsize-limit"));
14f9c5c9
AS
8461 }
8462
2e6fda7d
JB
8463 /* We want to preserve the type name. This can be useful when
8464 trying to get the type name of a value that has already been
8465 printed (for instance, if the user did "print VAR; whatis $". */
7d93a1e0 8466 result->set_name (type0->name ());
2e6fda7d 8467
ad82864c 8468 if (constrained_packed_array_p)
284614f0
JB
8469 {
8470 /* So far, the resulting type has been created as if the original
8471 type was a regular (non-packed) array type. As a result, the
8472 bitsize of the array elements needs to be set again, and the array
8473 length needs to be recomputed based on that bitsize. */
8474 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
8475 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
8476
8477 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
8478 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
8479 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
dda83cd7 8480 TYPE_LENGTH (result)++;
284614f0
JB
8481 }
8482
9cdd0d12 8483 result->set_is_fixed_instance (true);
14f9c5c9 8484 return result;
d2e4a39e 8485}
14f9c5c9
AS
8486
8487
8488/* A standard type (containing no dynamically sized components)
8489 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8490 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2 8491 and may be NULL if there are none, or if the object of type TYPE at
529cad9c
PH
8492 ADDRESS or in VALADDR contains these discriminants.
8493
1ed6ede0
JB
8494 If CHECK_TAG is not null, in the case of tagged types, this function
8495 attempts to locate the object's tag and use it to compute the actual
8496 type. However, when ADDRESS is null, we cannot use it to determine the
8497 location of the tag, and therefore compute the tagged type's actual type.
8498 So we return the tagged type without consulting the tag. */
529cad9c 8499
f192137b
JB
8500static struct type *
8501ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
dda83cd7 8502 CORE_ADDR address, struct value *dval, int check_tag)
14f9c5c9 8503{
61ee279c 8504 type = ada_check_typedef (type);
8ecb59f8
TT
8505
8506 /* Only un-fixed types need to be handled here. */
8507 if (!HAVE_GNAT_AUX_INFO (type))
8508 return type;
8509
78134374 8510 switch (type->code ())
d2e4a39e
AS
8511 {
8512 default:
14f9c5c9 8513 return type;
d2e4a39e 8514 case TYPE_CODE_STRUCT:
4c4b4cd2 8515 {
dda83cd7
SM
8516 struct type *static_type = to_static_fixed_type (type);
8517 struct type *fixed_record_type =
8518 to_fixed_record_type (type, valaddr, address, NULL);
8519
8520 /* If STATIC_TYPE is a tagged type and we know the object's address,
8521 then we can determine its tag, and compute the object's actual
8522 type from there. Note that we have to use the fixed record
8523 type (the parent part of the record may have dynamic fields
8524 and the way the location of _tag is expressed may depend on
8525 them). */
8526
8527 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
8528 {
b50d69b5
JG
8529 struct value *tag =
8530 value_tag_from_contents_and_address
8531 (fixed_record_type,
8532 valaddr,
8533 address);
8534 struct type *real_type = type_from_tag (tag);
8535 struct value *obj =
8536 value_from_contents_and_address (fixed_record_type,
8537 valaddr,
8538 address);
dda83cd7
SM
8539 fixed_record_type = value_type (obj);
8540 if (real_type != NULL)
8541 return to_fixed_record_type
b50d69b5
JG
8542 (real_type, NULL,
8543 value_address (ada_tag_value_at_base_address (obj)), NULL);
dda83cd7
SM
8544 }
8545
8546 /* Check to see if there is a parallel ___XVZ variable.
8547 If there is, then it provides the actual size of our type. */
8548 else if (ada_type_name (fixed_record_type) != NULL)
8549 {
8550 const char *name = ada_type_name (fixed_record_type);
8551 char *xvz_name
224c3ddb 8552 = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
eccab96d 8553 bool xvz_found = false;
dda83cd7 8554 LONGEST size;
4af88198 8555
dda83cd7 8556 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
a70b8144 8557 try
eccab96d
JB
8558 {
8559 xvz_found = get_int_var_value (xvz_name, size);
8560 }
230d2906 8561 catch (const gdb_exception_error &except)
eccab96d
JB
8562 {
8563 /* We found the variable, but somehow failed to read
8564 its value. Rethrow the same error, but with a little
8565 bit more information, to help the user understand
8566 what went wrong (Eg: the variable might have been
8567 optimized out). */
8568 throw_error (except.error,
8569 _("unable to read value of %s (%s)"),
3d6e9d23 8570 xvz_name, except.what ());
eccab96d 8571 }
eccab96d 8572
dda83cd7
SM
8573 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
8574 {
8575 fixed_record_type = copy_type (fixed_record_type);
8576 TYPE_LENGTH (fixed_record_type) = size;
8577
8578 /* The FIXED_RECORD_TYPE may have be a stub. We have
8579 observed this when the debugging info is STABS, and
8580 apparently it is something that is hard to fix.
8581
8582 In practice, we don't need the actual type definition
8583 at all, because the presence of the XVZ variable allows us
8584 to assume that there must be a XVS type as well, which we
8585 should be able to use later, when we need the actual type
8586 definition.
8587
8588 In the meantime, pretend that the "fixed" type we are
8589 returning is NOT a stub, because this can cause trouble
8590 when using this type to create new types targeting it.
8591 Indeed, the associated creation routines often check
8592 whether the target type is a stub and will try to replace
8593 it, thus using a type with the wrong size. This, in turn,
8594 might cause the new type to have the wrong size too.
8595 Consider the case of an array, for instance, where the size
8596 of the array is computed from the number of elements in
8597 our array multiplied by the size of its element. */
b4b73759 8598 fixed_record_type->set_is_stub (false);
dda83cd7
SM
8599 }
8600 }
8601 return fixed_record_type;
4c4b4cd2 8602 }
d2e4a39e 8603 case TYPE_CODE_ARRAY:
4c4b4cd2 8604 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8605 case TYPE_CODE_UNION:
8606 if (dval == NULL)
dda83cd7 8607 return type;
d2e4a39e 8608 else
dda83cd7 8609 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8610 }
14f9c5c9
AS
8611}
8612
f192137b
JB
8613/* The same as ada_to_fixed_type_1, except that it preserves the type
8614 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
96dbd2c1
JB
8615
8616 The typedef layer needs be preserved in order to differentiate between
8617 arrays and array pointers when both types are implemented using the same
8618 fat pointer. In the array pointer case, the pointer is encoded as
8619 a typedef of the pointer type. For instance, considering:
8620
8621 type String_Access is access String;
8622 S1 : String_Access := null;
8623
8624 To the debugger, S1 is defined as a typedef of type String. But
8625 to the user, it is a pointer. So if the user tries to print S1,
8626 we should not dereference the array, but print the array address
8627 instead.
8628
8629 If we didn't preserve the typedef layer, we would lose the fact that
8630 the type is to be presented as a pointer (needs de-reference before
8631 being printed). And we would also use the source-level type name. */
f192137b
JB
8632
8633struct type *
8634ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
dda83cd7 8635 CORE_ADDR address, struct value *dval, int check_tag)
f192137b
JB
8636
8637{
8638 struct type *fixed_type =
8639 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
8640
96dbd2c1
JB
8641 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8642 then preserve the typedef layer.
8643
8644 Implementation note: We can only check the main-type portion of
8645 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8646 from TYPE now returns a type that has the same instance flags
8647 as TYPE. For instance, if TYPE is a "typedef const", and its
8648 target type is a "struct", then the typedef elimination will return
8649 a "const" version of the target type. See check_typedef for more
8650 details about how the typedef layer elimination is done.
8651
8652 brobecker/2010-11-19: It seems to me that the only case where it is
8653 useful to preserve the typedef layer is when dealing with fat pointers.
8654 Perhaps, we could add a check for that and preserve the typedef layer
85102364 8655 only in that situation. But this seems unnecessary so far, probably
96dbd2c1
JB
8656 because we call check_typedef/ada_check_typedef pretty much everywhere.
8657 */
78134374 8658 if (type->code () == TYPE_CODE_TYPEDEF
720d1a40 8659 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
96dbd2c1 8660 == TYPE_MAIN_TYPE (fixed_type)))
f192137b
JB
8661 return type;
8662
8663 return fixed_type;
8664}
8665
14f9c5c9 8666/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8667 TYPE0, but based on no runtime data. */
14f9c5c9 8668
d2e4a39e
AS
8669static struct type *
8670to_static_fixed_type (struct type *type0)
14f9c5c9 8671{
d2e4a39e 8672 struct type *type;
14f9c5c9
AS
8673
8674 if (type0 == NULL)
8675 return NULL;
8676
22c4c60c 8677 if (type0->is_fixed_instance ())
4c4b4cd2
PH
8678 return type0;
8679
61ee279c 8680 type0 = ada_check_typedef (type0);
d2e4a39e 8681
78134374 8682 switch (type0->code ())
14f9c5c9
AS
8683 {
8684 default:
8685 return type0;
8686 case TYPE_CODE_STRUCT:
8687 type = dynamic_template_type (type0);
d2e4a39e 8688 if (type != NULL)
dda83cd7 8689 return template_to_static_fixed_type (type);
4c4b4cd2 8690 else
dda83cd7 8691 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8692 case TYPE_CODE_UNION:
8693 type = ada_find_parallel_type (type0, "___XVU");
8694 if (type != NULL)
dda83cd7 8695 return template_to_static_fixed_type (type);
4c4b4cd2 8696 else
dda83cd7 8697 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8698 }
8699}
8700
4c4b4cd2
PH
8701/* A static approximation of TYPE with all type wrappers removed. */
8702
d2e4a39e
AS
8703static struct type *
8704static_unwrap_type (struct type *type)
14f9c5c9
AS
8705{
8706 if (ada_is_aligner_type (type))
8707 {
940da03e 8708 struct type *type1 = ada_check_typedef (type)->field (0).type ();
14f9c5c9 8709 if (ada_type_name (type1) == NULL)
d0e39ea2 8710 type1->set_name (ada_type_name (type));
14f9c5c9
AS
8711
8712 return static_unwrap_type (type1);
8713 }
d2e4a39e 8714 else
14f9c5c9 8715 {
d2e4a39e 8716 struct type *raw_real_type = ada_get_base_type (type);
5b4ee69b 8717
d2e4a39e 8718 if (raw_real_type == type)
dda83cd7 8719 return type;
14f9c5c9 8720 else
dda83cd7 8721 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8722 }
8723}
8724
8725/* In some cases, incomplete and private types require
4c4b4cd2 8726 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8727 type Foo;
8728 type FooP is access Foo;
8729 V: FooP;
8730 type Foo is array ...;
4c4b4cd2 8731 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8732 cross-references to such types, we instead substitute for FooP a
8733 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8734 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8735
8736/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8737 exists, otherwise TYPE. */
8738
d2e4a39e 8739struct type *
61ee279c 8740ada_check_typedef (struct type *type)
14f9c5c9 8741{
727e3d2e
JB
8742 if (type == NULL)
8743 return NULL;
8744
736ade86
XR
8745 /* If our type is an access to an unconstrained array, which is encoded
8746 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
720d1a40
JB
8747 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8748 what allows us to distinguish between fat pointers that represent
8749 array types, and fat pointers that represent array access types
8750 (in both cases, the compiler implements them as fat pointers). */
736ade86 8751 if (ada_is_access_to_unconstrained_array (type))
720d1a40
JB
8752 return type;
8753
f168693b 8754 type = check_typedef (type);
78134374 8755 if (type == NULL || type->code () != TYPE_CODE_ENUM
e46d3488 8756 || !type->is_stub ()
7d93a1e0 8757 || type->name () == NULL)
14f9c5c9 8758 return type;
d2e4a39e 8759 else
14f9c5c9 8760 {
7d93a1e0 8761 const char *name = type->name ();
d2e4a39e 8762 struct type *type1 = ada_find_any_type (name);
5b4ee69b 8763
05e522ef 8764 if (type1 == NULL)
dda83cd7 8765 return type;
05e522ef
JB
8766
8767 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8768 stubs pointing to arrays, as we don't create symbols for array
3a867c22
JB
8769 types, only for the typedef-to-array types). If that's the case,
8770 strip the typedef layer. */
78134374 8771 if (type1->code () == TYPE_CODE_TYPEDEF)
3a867c22
JB
8772 type1 = ada_check_typedef (type1);
8773
8774 return type1;
14f9c5c9
AS
8775 }
8776}
8777
8778/* A value representing the data at VALADDR/ADDRESS as described by
8779 type TYPE0, but with a standard (static-sized) type that correctly
8780 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8781 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8782 creation of struct values]. */
14f9c5c9 8783
4c4b4cd2
PH
8784static struct value *
8785ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
dda83cd7 8786 struct value *val0)
14f9c5c9 8787{
1ed6ede0 8788 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
5b4ee69b 8789
14f9c5c9
AS
8790 if (type == type0 && val0 != NULL)
8791 return val0;
cc0e770c
JB
8792
8793 if (VALUE_LVAL (val0) != lval_memory)
8794 {
8795 /* Our value does not live in memory; it could be a convenience
8796 variable, for instance. Create a not_lval value using val0's
8797 contents. */
8798 return value_from_contents (type, value_contents (val0));
8799 }
8800
8801 return value_from_contents_and_address (type, 0, address);
4c4b4cd2
PH
8802}
8803
8804/* A value representing VAL, but with a standard (static-sized) type
8805 that correctly describes it. Does not necessarily create a new
8806 value. */
8807
0c3acc09 8808struct value *
4c4b4cd2
PH
8809ada_to_fixed_value (struct value *val)
8810{
c48db5ca 8811 val = unwrap_value (val);
d8ce9127 8812 val = ada_to_fixed_value_create (value_type (val), value_address (val), val);
c48db5ca 8813 return val;
14f9c5c9 8814}
d2e4a39e 8815\f
14f9c5c9 8816
14f9c5c9
AS
8817/* Attributes */
8818
4c4b4cd2
PH
8819/* Table mapping attribute numbers to names.
8820 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8821
27087b7f 8822static const char * const attribute_names[] = {
14f9c5c9
AS
8823 "<?>",
8824
d2e4a39e 8825 "first",
14f9c5c9
AS
8826 "last",
8827 "length",
8828 "image",
14f9c5c9
AS
8829 "max",
8830 "min",
4c4b4cd2
PH
8831 "modulus",
8832 "pos",
8833 "size",
8834 "tag",
14f9c5c9 8835 "val",
14f9c5c9
AS
8836 0
8837};
8838
de93309a 8839static const char *
4c4b4cd2 8840ada_attribute_name (enum exp_opcode n)
14f9c5c9 8841{
4c4b4cd2
PH
8842 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8843 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8844 else
8845 return attribute_names[0];
8846}
8847
4c4b4cd2 8848/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8849
4c4b4cd2
PH
8850static LONGEST
8851pos_atr (struct value *arg)
14f9c5c9 8852{
24209737
PH
8853 struct value *val = coerce_ref (arg);
8854 struct type *type = value_type (val);
14f9c5c9 8855
d2e4a39e 8856 if (!discrete_type_p (type))
323e0a4a 8857 error (_("'POS only defined on discrete types"));
14f9c5c9 8858
6244c119
SM
8859 gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
8860 if (!result.has_value ())
aa715135 8861 error (_("enumeration value is invalid: can't find 'POS"));
14f9c5c9 8862
6244c119 8863 return *result;
4c4b4cd2
PH
8864}
8865
8866static struct value *
7992accc
TT
8867ada_pos_atr (struct type *expect_type,
8868 struct expression *exp,
8869 enum noside noside, enum exp_opcode op,
8870 struct value *arg)
4c4b4cd2 8871{
7992accc
TT
8872 struct type *type = builtin_type (exp->gdbarch)->builtin_int;
8873 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8874 return value_zero (type, not_lval);
3cb382c9 8875 return value_from_longest (type, pos_atr (arg));
14f9c5c9
AS
8876}
8877
4c4b4cd2 8878/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8879
d2e4a39e 8880static struct value *
53a47a3e 8881val_atr (struct type *type, LONGEST val)
14f9c5c9 8882{
53a47a3e 8883 gdb_assert (discrete_type_p (type));
0bc2354b
TT
8884 if (type->code () == TYPE_CODE_RANGE)
8885 type = TYPE_TARGET_TYPE (type);
78134374 8886 if (type->code () == TYPE_CODE_ENUM)
14f9c5c9 8887 {
53a47a3e 8888 if (val < 0 || val >= type->num_fields ())
dda83cd7 8889 error (_("argument to 'VAL out of range"));
53a47a3e 8890 val = TYPE_FIELD_ENUMVAL (type, val);
14f9c5c9 8891 }
53a47a3e
TT
8892 return value_from_longest (type, val);
8893}
8894
8895static struct value *
3848abd6 8896ada_val_atr (enum noside noside, struct type *type, struct value *arg)
53a47a3e 8897{
3848abd6
TT
8898 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8899 return value_zero (type, not_lval);
8900
53a47a3e
TT
8901 if (!discrete_type_p (type))
8902 error (_("'VAL only defined on discrete types"));
8903 if (!integer_type_p (value_type (arg)))
8904 error (_("'VAL requires integral argument"));
8905
8906 return val_atr (type, value_as_long (arg));
14f9c5c9 8907}
14f9c5c9 8908\f
d2e4a39e 8909
dda83cd7 8910 /* Evaluation */
14f9c5c9 8911
4c4b4cd2
PH
8912/* True if TYPE appears to be an Ada character type.
8913 [At the moment, this is true only for Character and Wide_Character;
8914 It is a heuristic test that could stand improvement]. */
14f9c5c9 8915
fc913e53 8916bool
d2e4a39e 8917ada_is_character_type (struct type *type)
14f9c5c9 8918{
7b9f71f2
JB
8919 const char *name;
8920
8921 /* If the type code says it's a character, then assume it really is,
8922 and don't check any further. */
78134374 8923 if (type->code () == TYPE_CODE_CHAR)
fc913e53 8924 return true;
7b9f71f2
JB
8925
8926 /* Otherwise, assume it's a character type iff it is a discrete type
8927 with a known character type name. */
8928 name = ada_type_name (type);
8929 return (name != NULL
dda83cd7
SM
8930 && (type->code () == TYPE_CODE_INT
8931 || type->code () == TYPE_CODE_RANGE)
8932 && (strcmp (name, "character") == 0
8933 || strcmp (name, "wide_character") == 0
8934 || strcmp (name, "wide_wide_character") == 0
8935 || strcmp (name, "unsigned char") == 0));
14f9c5c9
AS
8936}
8937
4c4b4cd2 8938/* True if TYPE appears to be an Ada string type. */
14f9c5c9 8939
fc913e53 8940bool
ebf56fd3 8941ada_is_string_type (struct type *type)
14f9c5c9 8942{
61ee279c 8943 type = ada_check_typedef (type);
d2e4a39e 8944 if (type != NULL
78134374 8945 && type->code () != TYPE_CODE_PTR
76a01679 8946 && (ada_is_simple_array_type (type)
dda83cd7 8947 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8948 && ada_array_arity (type) == 1)
8949 {
8950 struct type *elttype = ada_array_element_type (type, 1);
8951
8952 return ada_is_character_type (elttype);
8953 }
d2e4a39e 8954 else
fc913e53 8955 return false;
14f9c5c9
AS
8956}
8957
5bf03f13
JB
8958/* The compiler sometimes provides a parallel XVS type for a given
8959 PAD type. Normally, it is safe to follow the PAD type directly,
8960 but older versions of the compiler have a bug that causes the offset
8961 of its "F" field to be wrong. Following that field in that case
8962 would lead to incorrect results, but this can be worked around
8963 by ignoring the PAD type and using the associated XVS type instead.
8964
8965 Set to True if the debugger should trust the contents of PAD types.
8966 Otherwise, ignore the PAD type if there is a parallel XVS type. */
491144b5 8967static bool trust_pad_over_xvs = true;
14f9c5c9
AS
8968
8969/* True if TYPE is a struct type introduced by the compiler to force the
8970 alignment of a value. Such types have a single field with a
4c4b4cd2 8971 distinctive name. */
14f9c5c9
AS
8972
8973int
ebf56fd3 8974ada_is_aligner_type (struct type *type)
14f9c5c9 8975{
61ee279c 8976 type = ada_check_typedef (type);
714e53ab 8977
5bf03f13 8978 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
714e53ab
PH
8979 return 0;
8980
78134374 8981 return (type->code () == TYPE_CODE_STRUCT
dda83cd7
SM
8982 && type->num_fields () == 1
8983 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
14f9c5c9
AS
8984}
8985
8986/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 8987 the parallel type. */
14f9c5c9 8988
d2e4a39e
AS
8989struct type *
8990ada_get_base_type (struct type *raw_type)
14f9c5c9 8991{
d2e4a39e
AS
8992 struct type *real_type_namer;
8993 struct type *raw_real_type;
14f9c5c9 8994
78134374 8995 if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
14f9c5c9
AS
8996 return raw_type;
8997
284614f0
JB
8998 if (ada_is_aligner_type (raw_type))
8999 /* The encoding specifies that we should always use the aligner type.
9000 So, even if this aligner type has an associated XVS type, we should
9001 simply ignore it.
9002
9003 According to the compiler gurus, an XVS type parallel to an aligner
9004 type may exist because of a stabs limitation. In stabs, aligner
9005 types are empty because the field has a variable-sized type, and
9006 thus cannot actually be used as an aligner type. As a result,
9007 we need the associated parallel XVS type to decode the type.
9008 Since the policy in the compiler is to not change the internal
9009 representation based on the debugging info format, we sometimes
9010 end up having a redundant XVS type parallel to the aligner type. */
9011 return raw_type;
9012
14f9c5c9 9013 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 9014 if (real_type_namer == NULL
78134374 9015 || real_type_namer->code () != TYPE_CODE_STRUCT
1f704f76 9016 || real_type_namer->num_fields () != 1)
14f9c5c9
AS
9017 return raw_type;
9018
940da03e 9019 if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
f80d3ff2
JB
9020 {
9021 /* This is an older encoding form where the base type needs to be
85102364 9022 looked up by name. We prefer the newer encoding because it is
f80d3ff2
JB
9023 more efficient. */
9024 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
9025 if (raw_real_type == NULL)
9026 return raw_type;
9027 else
9028 return raw_real_type;
9029 }
9030
9031 /* The field in our XVS type is a reference to the base type. */
940da03e 9032 return TYPE_TARGET_TYPE (real_type_namer->field (0).type ());
d2e4a39e 9033}
14f9c5c9 9034
4c4b4cd2 9035/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 9036
d2e4a39e
AS
9037struct type *
9038ada_aligned_type (struct type *type)
14f9c5c9
AS
9039{
9040 if (ada_is_aligner_type (type))
940da03e 9041 return ada_aligned_type (type->field (0).type ());
14f9c5c9
AS
9042 else
9043 return ada_get_base_type (type);
9044}
9045
9046
9047/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 9048 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 9049
fc1a4b47
AC
9050const gdb_byte *
9051ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
14f9c5c9 9052{
d2e4a39e 9053 if (ada_is_aligner_type (type))
940da03e 9054 return ada_aligned_value_addr (type->field (0).type (),
dda83cd7
SM
9055 valaddr +
9056 TYPE_FIELD_BITPOS (type,
9057 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
9058 else
9059 return valaddr;
9060}
9061
4c4b4cd2
PH
9062
9063
14f9c5c9 9064/* The printed representation of an enumeration literal with encoded
4c4b4cd2 9065 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
9066const char *
9067ada_enum_name (const char *name)
14f9c5c9 9068{
5f9febe0 9069 static std::string storage;
e6a959d6 9070 const char *tmp;
14f9c5c9 9071
4c4b4cd2
PH
9072 /* First, unqualify the enumeration name:
9073 1. Search for the last '.' character. If we find one, then skip
177b42fe 9074 all the preceding characters, the unqualified name starts
76a01679 9075 right after that dot.
4c4b4cd2 9076 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
9077 translates dots into "__". Search forward for double underscores,
9078 but stop searching when we hit an overloading suffix, which is
9079 of the form "__" followed by digits. */
4c4b4cd2 9080
c3e5cd34
PH
9081 tmp = strrchr (name, '.');
9082 if (tmp != NULL)
4c4b4cd2
PH
9083 name = tmp + 1;
9084 else
14f9c5c9 9085 {
4c4b4cd2 9086 while ((tmp = strstr (name, "__")) != NULL)
dda83cd7
SM
9087 {
9088 if (isdigit (tmp[2]))
9089 break;
9090 else
9091 name = tmp + 2;
9092 }
14f9c5c9
AS
9093 }
9094
9095 if (name[0] == 'Q')
9096 {
14f9c5c9 9097 int v;
5b4ee69b 9098
14f9c5c9 9099 if (name[1] == 'U' || name[1] == 'W')
dda83cd7
SM
9100 {
9101 if (sscanf (name + 2, "%x", &v) != 1)
9102 return name;
9103 }
272560b5
TT
9104 else if (((name[1] >= '0' && name[1] <= '9')
9105 || (name[1] >= 'a' && name[1] <= 'z'))
9106 && name[2] == '\0')
9107 {
5f9febe0
TT
9108 storage = string_printf ("'%c'", name[1]);
9109 return storage.c_str ();
272560b5 9110 }
14f9c5c9 9111 else
dda83cd7 9112 return name;
14f9c5c9
AS
9113
9114 if (isascii (v) && isprint (v))
5f9febe0 9115 storage = string_printf ("'%c'", v);
14f9c5c9 9116 else if (name[1] == 'U')
5f9febe0 9117 storage = string_printf ("[\"%02x\"]", v);
14f9c5c9 9118 else
5f9febe0 9119 storage = string_printf ("[\"%04x\"]", v);
14f9c5c9 9120
5f9febe0 9121 return storage.c_str ();
14f9c5c9 9122 }
d2e4a39e 9123 else
4c4b4cd2 9124 {
c3e5cd34
PH
9125 tmp = strstr (name, "__");
9126 if (tmp == NULL)
9127 tmp = strstr (name, "$");
9128 if (tmp != NULL)
dda83cd7 9129 {
5f9febe0
TT
9130 storage = std::string (name, tmp - name);
9131 return storage.c_str ();
dda83cd7 9132 }
4c4b4cd2
PH
9133
9134 return name;
9135 }
14f9c5c9
AS
9136}
9137
14f9c5c9
AS
9138/* Evaluate the subexpression of EXP starting at *POS as for
9139 evaluate_type, updating *POS to point just past the evaluated
4c4b4cd2 9140 expression. */
14f9c5c9 9141
d2e4a39e
AS
9142static struct value *
9143evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 9144{
fe1fe7ea 9145 return evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
14f9c5c9
AS
9146}
9147
9148/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 9149 value it wraps. */
14f9c5c9 9150
d2e4a39e
AS
9151static struct value *
9152unwrap_value (struct value *val)
14f9c5c9 9153{
df407dfe 9154 struct type *type = ada_check_typedef (value_type (val));
5b4ee69b 9155
14f9c5c9
AS
9156 if (ada_is_aligner_type (type))
9157 {
de4d072f 9158 struct value *v = ada_value_struct_elt (val, "F", 0);
df407dfe 9159 struct type *val_type = ada_check_typedef (value_type (v));
5b4ee69b 9160
14f9c5c9 9161 if (ada_type_name (val_type) == NULL)
d0e39ea2 9162 val_type->set_name (ada_type_name (type));
14f9c5c9
AS
9163
9164 return unwrap_value (v);
9165 }
d2e4a39e 9166 else
14f9c5c9 9167 {
d2e4a39e 9168 struct type *raw_real_type =
dda83cd7 9169 ada_check_typedef (ada_get_base_type (type));
d2e4a39e 9170
5bf03f13
JB
9171 /* If there is no parallel XVS or XVE type, then the value is
9172 already unwrapped. Return it without further modification. */
9173 if ((type == raw_real_type)
9174 && ada_find_parallel_type (type, "___XVE") == NULL)
9175 return val;
14f9c5c9 9176
d2e4a39e 9177 return
dda83cd7
SM
9178 coerce_unspec_val_to_type
9179 (val, ada_to_fixed_type (raw_real_type, 0,
9180 value_address (val),
9181 NULL, 1));
14f9c5c9
AS
9182 }
9183}
d2e4a39e 9184
d99dcf51
JB
9185/* Given two array types T1 and T2, return nonzero iff both arrays
9186 contain the same number of elements. */
9187
9188static int
9189ada_same_array_size_p (struct type *t1, struct type *t2)
9190{
9191 LONGEST lo1, hi1, lo2, hi2;
9192
9193 /* Get the array bounds in order to verify that the size of
9194 the two arrays match. */
9195 if (!get_array_bounds (t1, &lo1, &hi1)
9196 || !get_array_bounds (t2, &lo2, &hi2))
9197 error (_("unable to determine array bounds"));
9198
9199 /* To make things easier for size comparison, normalize a bit
9200 the case of empty arrays by making sure that the difference
9201 between upper bound and lower bound is always -1. */
9202 if (lo1 > hi1)
9203 hi1 = lo1 - 1;
9204 if (lo2 > hi2)
9205 hi2 = lo2 - 1;
9206
9207 return (hi1 - lo1 == hi2 - lo2);
9208}
9209
9210/* Assuming that VAL is an array of integrals, and TYPE represents
9211 an array with the same number of elements, but with wider integral
9212 elements, return an array "casted" to TYPE. In practice, this
9213 means that the returned array is built by casting each element
9214 of the original array into TYPE's (wider) element type. */
9215
9216static struct value *
9217ada_promote_array_of_integrals (struct type *type, struct value *val)
9218{
9219 struct type *elt_type = TYPE_TARGET_TYPE (type);
9220 LONGEST lo, hi;
9221 struct value *res;
9222 LONGEST i;
9223
9224 /* Verify that both val and type are arrays of scalars, and
9225 that the size of val's elements is smaller than the size
9226 of type's element. */
78134374 9227 gdb_assert (type->code () == TYPE_CODE_ARRAY);
d99dcf51 9228 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
78134374 9229 gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY);
d99dcf51
JB
9230 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
9231 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
9232 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
9233
9234 if (!get_array_bounds (type, &lo, &hi))
9235 error (_("unable to determine array bounds"));
9236
9237 res = allocate_value (type);
9238
9239 /* Promote each array element. */
9240 for (i = 0; i < hi - lo + 1; i++)
9241 {
9242 struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
9243
9244 memcpy (value_contents_writeable (res) + (i * TYPE_LENGTH (elt_type)),
9245 value_contents_all (elt), TYPE_LENGTH (elt_type));
9246 }
9247
9248 return res;
9249}
9250
4c4b4cd2
PH
9251/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9252 return the converted value. */
9253
d2e4a39e
AS
9254static struct value *
9255coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 9256{
df407dfe 9257 struct type *type2 = value_type (val);
5b4ee69b 9258
14f9c5c9
AS
9259 if (type == type2)
9260 return val;
9261
61ee279c
PH
9262 type2 = ada_check_typedef (type2);
9263 type = ada_check_typedef (type);
14f9c5c9 9264
78134374
SM
9265 if (type2->code () == TYPE_CODE_PTR
9266 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9
AS
9267 {
9268 val = ada_value_ind (val);
df407dfe 9269 type2 = value_type (val);
14f9c5c9
AS
9270 }
9271
78134374
SM
9272 if (type2->code () == TYPE_CODE_ARRAY
9273 && type->code () == TYPE_CODE_ARRAY)
14f9c5c9 9274 {
d99dcf51
JB
9275 if (!ada_same_array_size_p (type, type2))
9276 error (_("cannot assign arrays of different length"));
9277
9278 if (is_integral_type (TYPE_TARGET_TYPE (type))
9279 && is_integral_type (TYPE_TARGET_TYPE (type2))
9280 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
9281 < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9282 {
9283 /* Allow implicit promotion of the array elements to
9284 a wider type. */
9285 return ada_promote_array_of_integrals (type, val);
9286 }
9287
9288 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
dda83cd7
SM
9289 != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
9290 error (_("Incompatible types in assignment"));
04624583 9291 deprecated_set_value_type (val, type);
14f9c5c9 9292 }
d2e4a39e 9293 return val;
14f9c5c9
AS
9294}
9295
4c4b4cd2
PH
9296static struct value *
9297ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9298{
9299 struct value *val;
9300 struct type *type1, *type2;
9301 LONGEST v, v1, v2;
9302
994b9211
AC
9303 arg1 = coerce_ref (arg1);
9304 arg2 = coerce_ref (arg2);
18af8284
JB
9305 type1 = get_base_type (ada_check_typedef (value_type (arg1)));
9306 type2 = get_base_type (ada_check_typedef (value_type (arg2)));
4c4b4cd2 9307
78134374
SM
9308 if (type1->code () != TYPE_CODE_INT
9309 || type2->code () != TYPE_CODE_INT)
4c4b4cd2
PH
9310 return value_binop (arg1, arg2, op);
9311
76a01679 9312 switch (op)
4c4b4cd2
PH
9313 {
9314 case BINOP_MOD:
9315 case BINOP_DIV:
9316 case BINOP_REM:
9317 break;
9318 default:
9319 return value_binop (arg1, arg2, op);
9320 }
9321
9322 v2 = value_as_long (arg2);
9323 if (v2 == 0)
323e0a4a 9324 error (_("second operand of %s must not be zero."), op_string (op));
4c4b4cd2 9325
c6d940a9 9326 if (type1->is_unsigned () || op == BINOP_MOD)
4c4b4cd2
PH
9327 return value_binop (arg1, arg2, op);
9328
9329 v1 = value_as_long (arg1);
9330 switch (op)
9331 {
9332 case BINOP_DIV:
9333 v = v1 / v2;
76a01679 9334 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
dda83cd7 9335 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
9336 break;
9337 case BINOP_REM:
9338 v = v1 % v2;
76a01679 9339 if (v * v1 < 0)
dda83cd7 9340 v -= v2;
4c4b4cd2
PH
9341 break;
9342 default:
9343 /* Should not reach this point. */
9344 v = 0;
9345 }
9346
9347 val = allocate_value (type1);
990a07ab 9348 store_unsigned_integer (value_contents_raw (val),
dda83cd7 9349 TYPE_LENGTH (value_type (val)),
34877895 9350 type_byte_order (type1), v);
4c4b4cd2
PH
9351 return val;
9352}
9353
9354static int
9355ada_value_equal (struct value *arg1, struct value *arg2)
9356{
df407dfe
AC
9357 if (ada_is_direct_array_type (value_type (arg1))
9358 || ada_is_direct_array_type (value_type (arg2)))
4c4b4cd2 9359 {
79e8fcaa
JB
9360 struct type *arg1_type, *arg2_type;
9361
f58b38bf 9362 /* Automatically dereference any array reference before
dda83cd7 9363 we attempt to perform the comparison. */
f58b38bf
JB
9364 arg1 = ada_coerce_ref (arg1);
9365 arg2 = ada_coerce_ref (arg2);
79e8fcaa 9366
4c4b4cd2
PH
9367 arg1 = ada_coerce_to_simple_array (arg1);
9368 arg2 = ada_coerce_to_simple_array (arg2);
79e8fcaa
JB
9369
9370 arg1_type = ada_check_typedef (value_type (arg1));
9371 arg2_type = ada_check_typedef (value_type (arg2));
9372
78134374 9373 if (arg1_type->code () != TYPE_CODE_ARRAY
dda83cd7
SM
9374 || arg2_type->code () != TYPE_CODE_ARRAY)
9375 error (_("Attempt to compare array with non-array"));
4c4b4cd2 9376 /* FIXME: The following works only for types whose
dda83cd7
SM
9377 representations use all bits (no padding or undefined bits)
9378 and do not have user-defined equality. */
79e8fcaa
JB
9379 return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
9380 && memcmp (value_contents (arg1), value_contents (arg2),
9381 TYPE_LENGTH (arg1_type)) == 0);
4c4b4cd2
PH
9382 }
9383 return value_equal (arg1, arg2);
9384}
9385
52ce6436
PH
9386/* Assign the result of evaluating EXP starting at *POS to the INDEXth
9387 component of LHS (a simple array or a record), updating *POS past
9388 the expression, assuming that LHS is contained in CONTAINER. Does
9389 not modify the inferior's memory, nor does it modify LHS (unless
9390 LHS == CONTAINER). */
9391
9392static void
9393assign_component (struct value *container, struct value *lhs, LONGEST index,
9394 struct expression *exp, int *pos)
9395{
9396 struct value *mark = value_mark ();
9397 struct value *elt;
0e2da9f0 9398 struct type *lhs_type = check_typedef (value_type (lhs));
5b4ee69b 9399
78134374 9400 if (lhs_type->code () == TYPE_CODE_ARRAY)
52ce6436 9401 {
22601c15
UW
9402 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
9403 struct value *index_val = value_from_longest (index_type, index);
5b4ee69b 9404
52ce6436
PH
9405 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
9406 }
9407 else
9408 {
9409 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
c48db5ca 9410 elt = ada_to_fixed_value (elt);
52ce6436
PH
9411 }
9412
9413 if (exp->elts[*pos].opcode == OP_AGGREGATE)
9414 assign_aggregate (container, elt, exp, pos, EVAL_NORMAL);
9415 else
9416 value_assign_to_component (container, elt,
9417 ada_evaluate_subexp (NULL, exp, pos,
9418 EVAL_NORMAL));
9419
9420 value_free_to_mark (mark);
9421}
9422
9423/* Assuming that LHS represents an lvalue having a record or array
9424 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
9425 of that aggregate's value to LHS, advancing *POS past the
9426 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
9427 lvalue containing LHS (possibly LHS itself). Does not modify
9428 the inferior's memory, nor does it modify the contents of
0963b4bd 9429 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
52ce6436
PH
9430
9431static struct value *
9432assign_aggregate (struct value *container,
9433 struct value *lhs, struct expression *exp,
9434 int *pos, enum noside noside)
9435{
9436 struct type *lhs_type;
9437 int n = exp->elts[*pos+1].longconst;
9438 LONGEST low_index, high_index;
52ce6436 9439 int i;
52ce6436
PH
9440
9441 *pos += 3;
9442 if (noside != EVAL_NORMAL)
9443 {
52ce6436
PH
9444 for (i = 0; i < n; i += 1)
9445 ada_evaluate_subexp (NULL, exp, pos, noside);
9446 return container;
9447 }
9448
9449 container = ada_coerce_ref (container);
9450 if (ada_is_direct_array_type (value_type (container)))
9451 container = ada_coerce_to_simple_array (container);
9452 lhs = ada_coerce_ref (lhs);
9453 if (!deprecated_value_modifiable (lhs))
9454 error (_("Left operand of assignment is not a modifiable lvalue."));
9455
0e2da9f0 9456 lhs_type = check_typedef (value_type (lhs));
52ce6436
PH
9457 if (ada_is_direct_array_type (lhs_type))
9458 {
9459 lhs = ada_coerce_to_simple_array (lhs);
0e2da9f0 9460 lhs_type = check_typedef (value_type (lhs));
cf88be68
SM
9461 low_index = lhs_type->bounds ()->low.const_val ();
9462 high_index = lhs_type->bounds ()->high.const_val ();
52ce6436 9463 }
78134374 9464 else if (lhs_type->code () == TYPE_CODE_STRUCT)
52ce6436
PH
9465 {
9466 low_index = 0;
9467 high_index = num_visible_fields (lhs_type) - 1;
52ce6436
PH
9468 }
9469 else
9470 error (_("Left-hand side must be array or record."));
9471
cf608cc4 9472 std::vector<LONGEST> indices (4);
52ce6436
PH
9473 indices[0] = indices[1] = low_index - 1;
9474 indices[2] = indices[3] = high_index + 1;
52ce6436
PH
9475
9476 for (i = 0; i < n; i += 1)
9477 {
9478 switch (exp->elts[*pos].opcode)
9479 {
1fbf5ada 9480 case OP_CHOICES:
cf608cc4 9481 aggregate_assign_from_choices (container, lhs, exp, pos, indices,
1fbf5ada
JB
9482 low_index, high_index);
9483 break;
9484 case OP_POSITIONAL:
9485 aggregate_assign_positional (container, lhs, exp, pos, indices,
52ce6436 9486 low_index, high_index);
1fbf5ada
JB
9487 break;
9488 case OP_OTHERS:
9489 if (i != n-1)
9490 error (_("Misplaced 'others' clause"));
cf608cc4
TT
9491 aggregate_assign_others (container, lhs, exp, pos, indices,
9492 low_index, high_index);
1fbf5ada
JB
9493 break;
9494 default:
9495 error (_("Internal error: bad aggregate clause"));
52ce6436
PH
9496 }
9497 }
9498
9499 return container;
9500}
9501
9502/* Assign into the component of LHS indexed by the OP_POSITIONAL
9503 construct at *POS, updating *POS past the construct, given that
cf608cc4
TT
9504 the positions are relative to lower bound LOW, where HIGH is the
9505 upper bound. Record the position in INDICES. CONTAINER is as for
0963b4bd 9506 assign_aggregate. */
52ce6436
PH
9507static void
9508aggregate_assign_positional (struct value *container,
9509 struct value *lhs, struct expression *exp,
cf608cc4
TT
9510 int *pos, std::vector<LONGEST> &indices,
9511 LONGEST low, LONGEST high)
52ce6436
PH
9512{
9513 LONGEST ind = longest_to_int (exp->elts[*pos + 1].longconst) + low;
9514
9515 if (ind - 1 == high)
e1d5a0d2 9516 warning (_("Extra components in aggregate ignored."));
52ce6436
PH
9517 if (ind <= high)
9518 {
cf608cc4 9519 add_component_interval (ind, ind, indices);
52ce6436
PH
9520 *pos += 3;
9521 assign_component (container, lhs, ind, exp, pos);
9522 }
9523 else
9524 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9525}
9526
9527/* Assign into the components of LHS indexed by the OP_CHOICES
9528 construct at *POS, updating *POS past the construct, given that
9529 the allowable indices are LOW..HIGH. Record the indices assigned
cf608cc4 9530 to in INDICES. CONTAINER is as for assign_aggregate. */
52ce6436
PH
9531static void
9532aggregate_assign_from_choices (struct value *container,
9533 struct value *lhs, struct expression *exp,
cf608cc4
TT
9534 int *pos, std::vector<LONGEST> &indices,
9535 LONGEST low, LONGEST high)
52ce6436
PH
9536{
9537 int j;
9538 int n_choices = longest_to_int (exp->elts[*pos+1].longconst);
9539 int choice_pos, expr_pc;
9540 int is_array = ada_is_direct_array_type (value_type (lhs));
9541
9542 choice_pos = *pos += 3;
9543
9544 for (j = 0; j < n_choices; j += 1)
9545 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9546 expr_pc = *pos;
9547 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9548
9549 for (j = 0; j < n_choices; j += 1)
9550 {
9551 LONGEST lower, upper;
9552 enum exp_opcode op = exp->elts[choice_pos].opcode;
5b4ee69b 9553
52ce6436
PH
9554 if (op == OP_DISCRETE_RANGE)
9555 {
9556 choice_pos += 1;
9557 lower = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
9558 EVAL_NORMAL));
9559 upper = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
9560 EVAL_NORMAL));
9561 }
9562 else if (is_array)
9563 {
9564 lower = value_as_long (ada_evaluate_subexp (NULL, exp, &choice_pos,
9565 EVAL_NORMAL));
9566 upper = lower;
9567 }
9568 else
9569 {
9570 int ind;
0d5cff50 9571 const char *name;
5b4ee69b 9572
52ce6436
PH
9573 switch (op)
9574 {
9575 case OP_NAME:
9576 name = &exp->elts[choice_pos + 2].string;
9577 break;
9578 case OP_VAR_VALUE:
987012b8 9579 name = exp->elts[choice_pos + 2].symbol->natural_name ();
52ce6436
PH
9580 break;
9581 default:
9582 error (_("Invalid record component association."));
9583 }
9584 ada_evaluate_subexp (NULL, exp, &choice_pos, EVAL_SKIP);
9585 ind = 0;
9586 if (! find_struct_field (name, value_type (lhs), 0,
9587 NULL, NULL, NULL, NULL, &ind))
9588 error (_("Unknown component name: %s."), name);
9589 lower = upper = ind;
9590 }
9591
9592 if (lower <= upper && (lower < low || upper > high))
9593 error (_("Index in component association out of bounds."));
9594
cf608cc4 9595 add_component_interval (lower, upper, indices);
52ce6436
PH
9596 while (lower <= upper)
9597 {
9598 int pos1;
5b4ee69b 9599
52ce6436
PH
9600 pos1 = expr_pc;
9601 assign_component (container, lhs, lower, exp, &pos1);
9602 lower += 1;
9603 }
9604 }
9605}
9606
9607/* Assign the value of the expression in the OP_OTHERS construct in
9608 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9609 have not been previously assigned. The index intervals already assigned
cf608cc4
TT
9610 are in INDICES. Updates *POS to after the OP_OTHERS clause.
9611 CONTAINER is as for assign_aggregate. */
52ce6436
PH
9612static void
9613aggregate_assign_others (struct value *container,
9614 struct value *lhs, struct expression *exp,
cf608cc4 9615 int *pos, std::vector<LONGEST> &indices,
52ce6436
PH
9616 LONGEST low, LONGEST high)
9617{
9618 int i;
5ce64950 9619 int expr_pc = *pos + 1;
52ce6436 9620
cf608cc4 9621 int num_indices = indices.size ();
52ce6436
PH
9622 for (i = 0; i < num_indices - 2; i += 2)
9623 {
9624 LONGEST ind;
5b4ee69b 9625
52ce6436
PH
9626 for (ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
9627 {
5ce64950 9628 int localpos;
5b4ee69b 9629
5ce64950
MS
9630 localpos = expr_pc;
9631 assign_component (container, lhs, ind, exp, &localpos);
52ce6436
PH
9632 }
9633 }
9634 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
9635}
9636
cf608cc4
TT
9637/* Add the interval [LOW .. HIGH] to the sorted set of intervals
9638 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9639 overlap. */
52ce6436
PH
9640static void
9641add_component_interval (LONGEST low, LONGEST high,
cf608cc4 9642 std::vector<LONGEST> &indices)
52ce6436
PH
9643{
9644 int i, j;
5b4ee69b 9645
cf608cc4
TT
9646 int size = indices.size ();
9647 for (i = 0; i < size; i += 2) {
52ce6436
PH
9648 if (high >= indices[i] && low <= indices[i + 1])
9649 {
9650 int kh;
5b4ee69b 9651
cf608cc4 9652 for (kh = i + 2; kh < size; kh += 2)
52ce6436
PH
9653 if (high < indices[kh])
9654 break;
9655 if (low < indices[i])
9656 indices[i] = low;
9657 indices[i + 1] = indices[kh - 1];
9658 if (high > indices[i + 1])
9659 indices[i + 1] = high;
cf608cc4
TT
9660 memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
9661 indices.resize (kh - i - 2);
52ce6436
PH
9662 return;
9663 }
9664 else if (high < indices[i])
9665 break;
9666 }
9667
cf608cc4 9668 indices.resize (indices.size () + 2);
d4813f10 9669 for (j = indices.size () - 1; j >= i + 2; j -= 1)
52ce6436
PH
9670 indices[j] = indices[j - 2];
9671 indices[i] = low;
9672 indices[i + 1] = high;
9673}
9674
6e48bd2c
JB
9675/* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9676 is different. */
9677
9678static struct value *
b7e22850 9679ada_value_cast (struct type *type, struct value *arg2)
6e48bd2c
JB
9680{
9681 if (type == ada_check_typedef (value_type (arg2)))
9682 return arg2;
9683
6e48bd2c
JB
9684 return value_cast (type, arg2);
9685}
9686
284614f0
JB
9687/* Evaluating Ada expressions, and printing their result.
9688 ------------------------------------------------------
9689
21649b50
JB
9690 1. Introduction:
9691 ----------------
9692
284614f0
JB
9693 We usually evaluate an Ada expression in order to print its value.
9694 We also evaluate an expression in order to print its type, which
9695 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9696 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9697 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9698 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9699 similar.
9700
9701 Evaluating expressions is a little more complicated for Ada entities
9702 than it is for entities in languages such as C. The main reason for
9703 this is that Ada provides types whose definition might be dynamic.
9704 One example of such types is variant records. Or another example
9705 would be an array whose bounds can only be known at run time.
9706
9707 The following description is a general guide as to what should be
9708 done (and what should NOT be done) in order to evaluate an expression
9709 involving such types, and when. This does not cover how the semantic
9710 information is encoded by GNAT as this is covered separatly. For the
9711 document used as the reference for the GNAT encoding, see exp_dbug.ads
9712 in the GNAT sources.
9713
9714 Ideally, we should embed each part of this description next to its
9715 associated code. Unfortunately, the amount of code is so vast right
9716 now that it's hard to see whether the code handling a particular
9717 situation might be duplicated or not. One day, when the code is
9718 cleaned up, this guide might become redundant with the comments
9719 inserted in the code, and we might want to remove it.
9720
21649b50
JB
9721 2. ``Fixing'' an Entity, the Simple Case:
9722 -----------------------------------------
9723
284614f0
JB
9724 When evaluating Ada expressions, the tricky issue is that they may
9725 reference entities whose type contents and size are not statically
9726 known. Consider for instance a variant record:
9727
9728 type Rec (Empty : Boolean := True) is record
dda83cd7
SM
9729 case Empty is
9730 when True => null;
9731 when False => Value : Integer;
9732 end case;
284614f0
JB
9733 end record;
9734 Yes : Rec := (Empty => False, Value => 1);
9735 No : Rec := (empty => True);
9736
9737 The size and contents of that record depends on the value of the
9738 descriminant (Rec.Empty). At this point, neither the debugging
9739 information nor the associated type structure in GDB are able to
9740 express such dynamic types. So what the debugger does is to create
9741 "fixed" versions of the type that applies to the specific object.
30baf67b 9742 We also informally refer to this operation as "fixing" an object,
284614f0
JB
9743 which means creating its associated fixed type.
9744
9745 Example: when printing the value of variable "Yes" above, its fixed
9746 type would look like this:
9747
9748 type Rec is record
dda83cd7
SM
9749 Empty : Boolean;
9750 Value : Integer;
284614f0
JB
9751 end record;
9752
9753 On the other hand, if we printed the value of "No", its fixed type
9754 would become:
9755
9756 type Rec is record
dda83cd7 9757 Empty : Boolean;
284614f0
JB
9758 end record;
9759
9760 Things become a little more complicated when trying to fix an entity
9761 with a dynamic type that directly contains another dynamic type,
9762 such as an array of variant records, for instance. There are
9763 two possible cases: Arrays, and records.
9764
21649b50
JB
9765 3. ``Fixing'' Arrays:
9766 ---------------------
9767
9768 The type structure in GDB describes an array in terms of its bounds,
9769 and the type of its elements. By design, all elements in the array
9770 have the same type and we cannot represent an array of variant elements
9771 using the current type structure in GDB. When fixing an array,
9772 we cannot fix the array element, as we would potentially need one
9773 fixed type per element of the array. As a result, the best we can do
9774 when fixing an array is to produce an array whose bounds and size
9775 are correct (allowing us to read it from memory), but without having
9776 touched its element type. Fixing each element will be done later,
9777 when (if) necessary.
9778
9779 Arrays are a little simpler to handle than records, because the same
9780 amount of memory is allocated for each element of the array, even if
1b536f04 9781 the amount of space actually used by each element differs from element
21649b50 9782 to element. Consider for instance the following array of type Rec:
284614f0
JB
9783
9784 type Rec_Array is array (1 .. 2) of Rec;
9785
1b536f04
JB
9786 The actual amount of memory occupied by each element might be different
9787 from element to element, depending on the value of their discriminant.
21649b50 9788 But the amount of space reserved for each element in the array remains
1b536f04 9789 fixed regardless. So we simply need to compute that size using
21649b50
JB
9790 the debugging information available, from which we can then determine
9791 the array size (we multiply the number of elements of the array by
9792 the size of each element).
9793
9794 The simplest case is when we have an array of a constrained element
9795 type. For instance, consider the following type declarations:
9796
dda83cd7
SM
9797 type Bounded_String (Max_Size : Integer) is
9798 Length : Integer;
9799 Buffer : String (1 .. Max_Size);
9800 end record;
9801 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
21649b50
JB
9802
9803 In this case, the compiler describes the array as an array of
9804 variable-size elements (identified by its XVS suffix) for which
9805 the size can be read in the parallel XVZ variable.
9806
9807 In the case of an array of an unconstrained element type, the compiler
9808 wraps the array element inside a private PAD type. This type should not
9809 be shown to the user, and must be "unwrap"'ed before printing. Note
284614f0
JB
9810 that we also use the adjective "aligner" in our code to designate
9811 these wrapper types.
9812
1b536f04 9813 In some cases, the size allocated for each element is statically
21649b50
JB
9814 known. In that case, the PAD type already has the correct size,
9815 and the array element should remain unfixed.
9816
9817 But there are cases when this size is not statically known.
9818 For instance, assuming that "Five" is an integer variable:
284614f0 9819
dda83cd7
SM
9820 type Dynamic is array (1 .. Five) of Integer;
9821 type Wrapper (Has_Length : Boolean := False) is record
9822 Data : Dynamic;
9823 case Has_Length is
9824 when True => Length : Integer;
9825 when False => null;
9826 end case;
9827 end record;
9828 type Wrapper_Array is array (1 .. 2) of Wrapper;
284614f0 9829
dda83cd7
SM
9830 Hello : Wrapper_Array := (others => (Has_Length => True,
9831 Data => (others => 17),
9832 Length => 1));
284614f0
JB
9833
9834
9835 The debugging info would describe variable Hello as being an
9836 array of a PAD type. The size of that PAD type is not statically
9837 known, but can be determined using a parallel XVZ variable.
9838 In that case, a copy of the PAD type with the correct size should
9839 be used for the fixed array.
9840
21649b50
JB
9841 3. ``Fixing'' record type objects:
9842 ----------------------------------
9843
9844 Things are slightly different from arrays in the case of dynamic
284614f0
JB
9845 record types. In this case, in order to compute the associated
9846 fixed type, we need to determine the size and offset of each of
9847 its components. This, in turn, requires us to compute the fixed
9848 type of each of these components.
9849
9850 Consider for instance the example:
9851
dda83cd7
SM
9852 type Bounded_String (Max_Size : Natural) is record
9853 Str : String (1 .. Max_Size);
9854 Length : Natural;
9855 end record;
9856 My_String : Bounded_String (Max_Size => 10);
284614f0
JB
9857
9858 In that case, the position of field "Length" depends on the size
9859 of field Str, which itself depends on the value of the Max_Size
21649b50 9860 discriminant. In order to fix the type of variable My_String,
284614f0
JB
9861 we need to fix the type of field Str. Therefore, fixing a variant
9862 record requires us to fix each of its components.
9863
9864 However, if a component does not have a dynamic size, the component
9865 should not be fixed. In particular, fields that use a PAD type
9866 should not fixed. Here is an example where this might happen
9867 (assuming type Rec above):
9868
9869 type Container (Big : Boolean) is record
dda83cd7
SM
9870 First : Rec;
9871 After : Integer;
9872 case Big is
9873 when True => Another : Integer;
9874 when False => null;
9875 end case;
284614f0
JB
9876 end record;
9877 My_Container : Container := (Big => False,
dda83cd7
SM
9878 First => (Empty => True),
9879 After => 42);
284614f0
JB
9880
9881 In that example, the compiler creates a PAD type for component First,
9882 whose size is constant, and then positions the component After just
9883 right after it. The offset of component After is therefore constant
9884 in this case.
9885
9886 The debugger computes the position of each field based on an algorithm
9887 that uses, among other things, the actual position and size of the field
21649b50
JB
9888 preceding it. Let's now imagine that the user is trying to print
9889 the value of My_Container. If the type fixing was recursive, we would
284614f0
JB
9890 end up computing the offset of field After based on the size of the
9891 fixed version of field First. And since in our example First has
9892 only one actual field, the size of the fixed type is actually smaller
9893 than the amount of space allocated to that field, and thus we would
9894 compute the wrong offset of field After.
9895
21649b50
JB
9896 To make things more complicated, we need to watch out for dynamic
9897 components of variant records (identified by the ___XVL suffix in
9898 the component name). Even if the target type is a PAD type, the size
9899 of that type might not be statically known. So the PAD type needs
9900 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9901 we might end up with the wrong size for our component. This can be
9902 observed with the following type declarations:
284614f0 9903
dda83cd7
SM
9904 type Octal is new Integer range 0 .. 7;
9905 type Octal_Array is array (Positive range <>) of Octal;
9906 pragma Pack (Octal_Array);
284614f0 9907
dda83cd7
SM
9908 type Octal_Buffer (Size : Positive) is record
9909 Buffer : Octal_Array (1 .. Size);
9910 Length : Integer;
9911 end record;
284614f0
JB
9912
9913 In that case, Buffer is a PAD type whose size is unset and needs
9914 to be computed by fixing the unwrapped type.
9915
21649b50
JB
9916 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9917 ----------------------------------------------------------
9918
9919 Lastly, when should the sub-elements of an entity that remained unfixed
284614f0
JB
9920 thus far, be actually fixed?
9921
9922 The answer is: Only when referencing that element. For instance
9923 when selecting one component of a record, this specific component
9924 should be fixed at that point in time. Or when printing the value
9925 of a record, each component should be fixed before its value gets
9926 printed. Similarly for arrays, the element of the array should be
9927 fixed when printing each element of the array, or when extracting
9928 one element out of that array. On the other hand, fixing should
9929 not be performed on the elements when taking a slice of an array!
9930
31432a67 9931 Note that one of the side effects of miscomputing the offset and
284614f0
JB
9932 size of each field is that we end up also miscomputing the size
9933 of the containing type. This can have adverse results when computing
9934 the value of an entity. GDB fetches the value of an entity based
9935 on the size of its type, and thus a wrong size causes GDB to fetch
9936 the wrong amount of memory. In the case where the computed size is
9937 too small, GDB fetches too little data to print the value of our
31432a67 9938 entity. Results in this case are unpredictable, as we usually read
284614f0
JB
9939 past the buffer containing the data =:-o. */
9940
ced9779b
JB
9941/* Evaluate a subexpression of EXP, at index *POS, and return a value
9942 for that subexpression cast to TO_TYPE. Advance *POS over the
9943 subexpression. */
9944
9945static value *
9946ada_evaluate_subexp_for_cast (expression *exp, int *pos,
9947 enum noside noside, struct type *to_type)
9948{
9949 int pc = *pos;
9950
9951 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE
9952 || exp->elts[pc].opcode == OP_VAR_VALUE)
9953 {
9954 (*pos) += 4;
9955
9956 value *val;
9957 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
dda83cd7
SM
9958 {
9959 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9960 return value_zero (to_type, not_lval);
9961
9962 val = evaluate_var_msym_value (noside,
9963 exp->elts[pc + 1].objfile,
9964 exp->elts[pc + 2].msymbol);
9965 }
ced9779b 9966 else
dda83cd7
SM
9967 val = evaluate_var_value (noside,
9968 exp->elts[pc + 1].block,
9969 exp->elts[pc + 2].symbol);
ced9779b
JB
9970
9971 if (noside == EVAL_SKIP)
dda83cd7 9972 return eval_skip_value (exp);
ced9779b
JB
9973
9974 val = ada_value_cast (to_type, val);
9975
9976 /* Follow the Ada language semantics that do not allow taking
9977 an address of the result of a cast (view conversion in Ada). */
9978 if (VALUE_LVAL (val) == lval_memory)
dda83cd7
SM
9979 {
9980 if (value_lazy (val))
9981 value_fetch_lazy (val);
9982 VALUE_LVAL (val) = not_lval;
9983 }
ced9779b
JB
9984 return val;
9985 }
9986
9987 value *val = evaluate_subexp (to_type, exp, pos, noside);
9988 if (noside == EVAL_SKIP)
9989 return eval_skip_value (exp);
9990 return ada_value_cast (to_type, val);
9991}
9992
62d4bd94
TT
9993/* A helper function for TERNOP_IN_RANGE. */
9994
9995static value *
9996eval_ternop_in_range (struct type *expect_type, struct expression *exp,
9997 enum noside noside,
9998 value *arg1, value *arg2, value *arg3)
9999{
10000 if (noside == EVAL_SKIP)
10001 return eval_skip_value (exp);
10002
10003 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10004 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10005 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10006 return
10007 value_from_longest (type,
10008 (value_less (arg1, arg3)
10009 || value_equal (arg1, arg3))
10010 && (value_less (arg2, arg1)
10011 || value_equal (arg2, arg1)));
10012}
10013
82390ab8
TT
10014/* A helper function for UNOP_NEG. */
10015
7c15d377 10016value *
82390ab8
TT
10017ada_unop_neg (struct type *expect_type,
10018 struct expression *exp,
10019 enum noside noside, enum exp_opcode op,
10020 struct value *arg1)
10021{
10022 if (noside == EVAL_SKIP)
10023 return eval_skip_value (exp);
10024 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10025 return value_neg (arg1);
10026}
10027
7efc87ff
TT
10028/* A helper function for UNOP_IN_RANGE. */
10029
95d49dfb 10030value *
7efc87ff
TT
10031ada_unop_in_range (struct type *expect_type,
10032 struct expression *exp,
10033 enum noside noside, enum exp_opcode op,
10034 struct value *arg1, struct type *type)
10035{
10036 if (noside == EVAL_SKIP)
10037 return eval_skip_value (exp);
10038
10039 struct value *arg2, *arg3;
10040 switch (type->code ())
10041 {
10042 default:
10043 lim_warning (_("Membership test incompletely implemented; "
10044 "always returns true"));
10045 type = language_bool_type (exp->language_defn, exp->gdbarch);
10046 return value_from_longest (type, (LONGEST) 1);
10047
10048 case TYPE_CODE_RANGE:
10049 arg2 = value_from_longest (type,
10050 type->bounds ()->low.const_val ());
10051 arg3 = value_from_longest (type,
10052 type->bounds ()->high.const_val ());
10053 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10054 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10055 type = language_bool_type (exp->language_defn, exp->gdbarch);
10056 return
10057 value_from_longest (type,
10058 (value_less (arg1, arg3)
10059 || value_equal (arg1, arg3))
10060 && (value_less (arg2, arg1)
10061 || value_equal (arg2, arg1)));
10062 }
10063}
10064
020dbabe
TT
10065/* A helper function for OP_ATR_TAG. */
10066
7c15d377 10067value *
020dbabe
TT
10068ada_atr_tag (struct type *expect_type,
10069 struct expression *exp,
10070 enum noside noside, enum exp_opcode op,
10071 struct value *arg1)
10072{
10073 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10074 return value_zero (ada_tag_type (arg1), not_lval);
10075
10076 return ada_value_tag (arg1);
10077}
10078
68c75735
TT
10079/* A helper function for OP_ATR_SIZE. */
10080
7c15d377 10081value *
68c75735
TT
10082ada_atr_size (struct type *expect_type,
10083 struct expression *exp,
10084 enum noside noside, enum exp_opcode op,
10085 struct value *arg1)
10086{
10087 struct type *type = value_type (arg1);
10088
10089 /* If the argument is a reference, then dereference its type, since
10090 the user is really asking for the size of the actual object,
10091 not the size of the pointer. */
10092 if (type->code () == TYPE_CODE_REF)
10093 type = TYPE_TARGET_TYPE (type);
10094
10095 if (noside == EVAL_SKIP)
10096 return eval_skip_value (exp);
10097 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10098 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
10099 else
10100 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
10101 TARGET_CHAR_BIT * TYPE_LENGTH (type));
10102}
10103
d05e24e6
TT
10104/* A helper function for UNOP_ABS. */
10105
7c15d377 10106value *
d05e24e6
TT
10107ada_abs (struct type *expect_type,
10108 struct expression *exp,
10109 enum noside noside, enum exp_opcode op,
10110 struct value *arg1)
10111{
10112 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10113 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
10114 return value_neg (arg1);
10115 else
10116 return arg1;
10117}
10118
faa1dfd7
TT
10119/* A helper function for BINOP_MUL. */
10120
d9e7db06 10121value *
faa1dfd7
TT
10122ada_mult_binop (struct type *expect_type,
10123 struct expression *exp,
10124 enum noside noside, enum exp_opcode op,
10125 struct value *arg1, struct value *arg2)
10126{
10127 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10128 {
10129 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10130 return value_zero (value_type (arg1), not_lval);
10131 }
10132 else
10133 {
10134 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10135 return ada_value_binop (arg1, arg2, op);
10136 }
10137}
10138
214b13ac
TT
10139/* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10140
6e8fb7b7 10141value *
214b13ac
TT
10142ada_equal_binop (struct type *expect_type,
10143 struct expression *exp,
10144 enum noside noside, enum exp_opcode op,
10145 struct value *arg1, struct value *arg2)
10146{
10147 int tem;
10148 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10149 tem = 0;
10150 else
10151 {
10152 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10153 tem = ada_value_equal (arg1, arg2);
10154 }
10155 if (op == BINOP_NOTEQUAL)
10156 tem = !tem;
10157 struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
10158 return value_from_longest (type, (LONGEST) tem);
10159}
10160
5ce19db8
TT
10161/* A helper function for TERNOP_SLICE. */
10162
1b1ebfab 10163value *
5ce19db8
TT
10164ada_ternop_slice (struct expression *exp,
10165 enum noside noside,
10166 struct value *array, struct value *low_bound_val,
10167 struct value *high_bound_val)
10168{
10169 LONGEST low_bound;
10170 LONGEST high_bound;
10171
10172 low_bound_val = coerce_ref (low_bound_val);
10173 high_bound_val = coerce_ref (high_bound_val);
10174 low_bound = value_as_long (low_bound_val);
10175 high_bound = value_as_long (high_bound_val);
10176
10177 /* If this is a reference to an aligner type, then remove all
10178 the aligners. */
10179 if (value_type (array)->code () == TYPE_CODE_REF
10180 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
10181 TYPE_TARGET_TYPE (value_type (array)) =
10182 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
10183
10184 if (ada_is_any_packed_array_type (value_type (array)))
10185 error (_("cannot slice a packed array"));
10186
10187 /* If this is a reference to an array or an array lvalue,
10188 convert to a pointer. */
10189 if (value_type (array)->code () == TYPE_CODE_REF
10190 || (value_type (array)->code () == TYPE_CODE_ARRAY
10191 && VALUE_LVAL (array) == lval_memory))
10192 array = value_addr (array);
10193
10194 if (noside == EVAL_AVOID_SIDE_EFFECTS
10195 && ada_is_array_descriptor_type (ada_check_typedef
10196 (value_type (array))))
10197 return empty_array (ada_type_of_array (array, 0), low_bound,
10198 high_bound);
10199
10200 array = ada_coerce_to_simple_array_ptr (array);
10201
10202 /* If we have more than one level of pointer indirection,
10203 dereference the value until we get only one level. */
10204 while (value_type (array)->code () == TYPE_CODE_PTR
10205 && (TYPE_TARGET_TYPE (value_type (array))->code ()
10206 == TYPE_CODE_PTR))
10207 array = value_ind (array);
10208
10209 /* Make sure we really do have an array type before going further,
10210 to avoid a SEGV when trying to get the index type or the target
10211 type later down the road if the debug info generated by
10212 the compiler is incorrect or incomplete. */
10213 if (!ada_is_simple_array_type (value_type (array)))
10214 error (_("cannot take slice of non-array"));
10215
10216 if (ada_check_typedef (value_type (array))->code ()
10217 == TYPE_CODE_PTR)
10218 {
10219 struct type *type0 = ada_check_typedef (value_type (array));
10220
10221 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
10222 return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
10223 else
10224 {
10225 struct type *arr_type0 =
10226 to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
10227
10228 return ada_value_slice_from_ptr (array, arr_type0,
10229 longest_to_int (low_bound),
10230 longest_to_int (high_bound));
10231 }
10232 }
10233 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
10234 return array;
10235 else if (high_bound < low_bound)
10236 return empty_array (value_type (array), low_bound, high_bound);
10237 else
10238 return ada_value_slice (array, longest_to_int (low_bound),
10239 longest_to_int (high_bound));
10240}
10241
b467efaa
TT
10242/* A helper function for BINOP_IN_BOUNDS. */
10243
82c3886e 10244value *
b467efaa
TT
10245ada_binop_in_bounds (struct expression *exp, enum noside noside,
10246 struct value *arg1, struct value *arg2, int n)
10247{
10248 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10249 {
10250 struct type *type = language_bool_type (exp->language_defn,
10251 exp->gdbarch);
10252 return value_zero (type, not_lval);
10253 }
10254
10255 struct type *type = ada_index_type (value_type (arg2), n, "range");
10256 if (!type)
10257 type = value_type (arg1);
10258
10259 value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
10260 arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
10261
10262 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10263 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
10264 type = language_bool_type (exp->language_defn, exp->gdbarch);
10265 return value_from_longest (type,
10266 (value_less (arg1, arg3)
10267 || value_equal (arg1, arg3))
10268 && (value_less (arg2, arg1)
10269 || value_equal (arg2, arg1)));
10270}
10271
b84564fc
TT
10272/* A helper function for some attribute operations. */
10273
10274static value *
10275ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
10276 struct value *arg1, struct type *type_arg, int tem)
10277{
10278 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10279 {
10280 if (type_arg == NULL)
10281 type_arg = value_type (arg1);
10282
10283 if (ada_is_constrained_packed_array_type (type_arg))
10284 type_arg = decode_constrained_packed_array_type (type_arg);
10285
10286 if (!discrete_type_p (type_arg))
10287 {
10288 switch (op)
10289 {
10290 default: /* Should never happen. */
10291 error (_("unexpected attribute encountered"));
10292 case OP_ATR_FIRST:
10293 case OP_ATR_LAST:
10294 type_arg = ada_index_type (type_arg, tem,
10295 ada_attribute_name (op));
10296 break;
10297 case OP_ATR_LENGTH:
10298 type_arg = builtin_type (exp->gdbarch)->builtin_int;
10299 break;
10300 }
10301 }
10302
10303 return value_zero (type_arg, not_lval);
10304 }
10305 else if (type_arg == NULL)
10306 {
10307 arg1 = ada_coerce_ref (arg1);
10308
10309 if (ada_is_constrained_packed_array_type (value_type (arg1)))
10310 arg1 = ada_coerce_to_simple_array (arg1);
10311
10312 struct type *type;
10313 if (op == OP_ATR_LENGTH)
10314 type = builtin_type (exp->gdbarch)->builtin_int;
10315 else
10316 {
10317 type = ada_index_type (value_type (arg1), tem,
10318 ada_attribute_name (op));
10319 if (type == NULL)
10320 type = builtin_type (exp->gdbarch)->builtin_int;
10321 }
10322
10323 switch (op)
10324 {
10325 default: /* Should never happen. */
10326 error (_("unexpected attribute encountered"));
10327 case OP_ATR_FIRST:
10328 return value_from_longest
10329 (type, ada_array_bound (arg1, tem, 0));
10330 case OP_ATR_LAST:
10331 return value_from_longest
10332 (type, ada_array_bound (arg1, tem, 1));
10333 case OP_ATR_LENGTH:
10334 return value_from_longest
10335 (type, ada_array_length (arg1, tem));
10336 }
10337 }
10338 else if (discrete_type_p (type_arg))
10339 {
10340 struct type *range_type;
10341 const char *name = ada_type_name (type_arg);
10342
10343 range_type = NULL;
10344 if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
10345 range_type = to_fixed_range_type (type_arg, NULL);
10346 if (range_type == NULL)
10347 range_type = type_arg;
10348 switch (op)
10349 {
10350 default:
10351 error (_("unexpected attribute encountered"));
10352 case OP_ATR_FIRST:
10353 return value_from_longest
10354 (range_type, ada_discrete_type_low_bound (range_type));
10355 case OP_ATR_LAST:
10356 return value_from_longest
10357 (range_type, ada_discrete_type_high_bound (range_type));
10358 case OP_ATR_LENGTH:
10359 error (_("the 'length attribute applies only to array types"));
10360 }
10361 }
10362 else if (type_arg->code () == TYPE_CODE_FLT)
10363 error (_("unimplemented type attribute"));
10364 else
10365 {
10366 LONGEST low, high;
10367
10368 if (ada_is_constrained_packed_array_type (type_arg))
10369 type_arg = decode_constrained_packed_array_type (type_arg);
10370
10371 struct type *type;
10372 if (op == OP_ATR_LENGTH)
10373 type = builtin_type (exp->gdbarch)->builtin_int;
10374 else
10375 {
10376 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
10377 if (type == NULL)
10378 type = builtin_type (exp->gdbarch)->builtin_int;
10379 }
10380
10381 switch (op)
10382 {
10383 default:
10384 error (_("unexpected attribute encountered"));
10385 case OP_ATR_FIRST:
10386 low = ada_array_bound_from_type (type_arg, tem, 0);
10387 return value_from_longest (type, low);
10388 case OP_ATR_LAST:
10389 high = ada_array_bound_from_type (type_arg, tem, 1);
10390 return value_from_longest (type, high);
10391 case OP_ATR_LENGTH:
10392 low = ada_array_bound_from_type (type_arg, tem, 0);
10393 high = ada_array_bound_from_type (type_arg, tem, 1);
10394 return value_from_longest (type, high - low + 1);
10395 }
10396 }
10397}
10398
38dc70cf
TT
10399/* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10400
6ad3b8bf 10401struct value *
38dc70cf
TT
10402ada_binop_minmax (struct type *expect_type,
10403 struct expression *exp,
10404 enum noside noside, enum exp_opcode op,
10405 struct value *arg1, struct value *arg2)
10406{
10407 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10408 return value_zero (value_type (arg1), not_lval);
10409 else
10410 {
10411 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10412 return value_binop (arg1, arg2,
10413 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
10414 }
10415}
10416
dd5fd283
TT
10417/* A helper function for BINOP_EXP. */
10418
10419static struct value *
10420ada_binop_exp (struct type *expect_type,
10421 struct expression *exp,
10422 enum noside noside, enum exp_opcode op,
10423 struct value *arg1, struct value *arg2)
10424{
10425 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10426 return value_zero (value_type (arg1), not_lval);
10427 else
10428 {
10429 /* For integer exponentiation operations,
10430 only promote the first argument. */
10431 if (is_integral_type (value_type (arg2)))
10432 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
10433 else
10434 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10435
10436 return value_binop (arg1, arg2, op);
10437 }
10438}
10439
03070ee9
TT
10440namespace expr
10441{
10442
10443value *
10444ada_wrapped_operation::evaluate (struct type *expect_type,
10445 struct expression *exp,
10446 enum noside noside)
10447{
10448 value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
10449 if (noside == EVAL_NORMAL)
10450 result = unwrap_value (result);
10451
10452 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10453 then we need to perform the conversion manually, because
10454 evaluate_subexp_standard doesn't do it. This conversion is
10455 necessary in Ada because the different kinds of float/fixed
10456 types in Ada have different representations.
10457
10458 Similarly, we need to perform the conversion from OP_LONG
10459 ourselves. */
10460 if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
10461 result = ada_value_cast (expect_type, result);
10462
10463 return result;
10464}
10465
42fecb61
TT
10466value *
10467ada_string_operation::evaluate (struct type *expect_type,
10468 struct expression *exp,
10469 enum noside noside)
10470{
10471 value *result = string_operation::evaluate (expect_type, exp, noside);
10472 /* The result type will have code OP_STRING, bashed there from
10473 OP_ARRAY. Bash it back. */
10474 if (value_type (result)->code () == TYPE_CODE_STRING)
10475 value_type (result)->set_code (TYPE_CODE_ARRAY);
10476 return result;
10477}
10478
cc6bd32e
TT
10479value *
10480ada_qual_operation::evaluate (struct type *expect_type,
10481 struct expression *exp,
10482 enum noside noside)
10483{
10484 struct type *type = std::get<1> (m_storage);
10485 return std::get<0> (m_storage)->evaluate (type, exp, noside);
10486}
10487
fc715eb2
TT
10488value *
10489ada_ternop_range_operation::evaluate (struct type *expect_type,
10490 struct expression *exp,
10491 enum noside noside)
10492{
10493 value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10494 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
10495 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
10496 return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
10497}
10498
73796c73
TT
10499value *
10500ada_binop_addsub_operation::evaluate (struct type *expect_type,
10501 struct expression *exp,
10502 enum noside noside)
10503{
10504 value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
10505 value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
10506
10507 auto do_op = [=] (LONGEST x, LONGEST y)
10508 {
10509 if (std::get<0> (m_storage) == BINOP_ADD)
10510 return x + y;
10511 return x - y;
10512 };
10513
10514 if (value_type (arg1)->code () == TYPE_CODE_PTR)
10515 return (value_from_longest
10516 (value_type (arg1),
10517 do_op (value_as_long (arg1), value_as_long (arg2))));
10518 if (value_type (arg2)->code () == TYPE_CODE_PTR)
10519 return (value_from_longest
10520 (value_type (arg2),
10521 do_op (value_as_long (arg1), value_as_long (arg2))));
10522 /* Preserve the original type for use by the range case below.
10523 We cannot cast the result to a reference type, so if ARG1 is
10524 a reference type, find its underlying type. */
10525 struct type *type = value_type (arg1);
10526 while (type->code () == TYPE_CODE_REF)
10527 type = TYPE_TARGET_TYPE (type);
10528 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
10529 arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
10530 /* We need to special-case the result with a range.
10531 This is done for the benefit of "ptype". gdb's Ada support
10532 historically used the LHS to set the result type here, so
10533 preserve this behavior. */
10534 if (type->code () == TYPE_CODE_RANGE)
10535 arg1 = value_cast (type, arg1);
10536 return arg1;
10537}
10538
60fa02ca
TT
10539value *
10540ada_unop_atr_operation::evaluate (struct type *expect_type,
10541 struct expression *exp,
10542 enum noside noside)
10543{
10544 struct type *type_arg = nullptr;
10545 value *val = nullptr;
10546
10547 if (std::get<0> (m_storage)->opcode () == OP_TYPE)
10548 {
10549 value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
10550 EVAL_AVOID_SIDE_EFFECTS);
10551 type_arg = value_type (tem);
10552 }
10553 else
10554 val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
10555
10556 return ada_unop_atr (exp, noside, std::get<1> (m_storage),
10557 val, type_arg, std::get<2> (m_storage));
10558}
10559
3f4a0053
TT
10560value *
10561ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
10562 struct expression *exp,
10563 enum noside noside)
10564{
10565 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10566 return value_zero (expect_type, not_lval);
10567
10568 value *val = evaluate_var_msym_value (noside,
10569 std::get<1> (m_storage),
10570 std::get<0> (m_storage));
10571
10572 val = ada_value_cast (expect_type, val);
10573
10574 /* Follow the Ada language semantics that do not allow taking
10575 an address of the result of a cast (view conversion in Ada). */
10576 if (VALUE_LVAL (val) == lval_memory)
10577 {
10578 if (value_lazy (val))
10579 value_fetch_lazy (val);
10580 VALUE_LVAL (val) = not_lval;
10581 }
10582 return val;
10583}
10584
99a3b1e7
TT
10585value *
10586ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
10587 struct expression *exp,
10588 enum noside noside)
10589{
10590 value *val = evaluate_var_value (noside,
10591 std::get<1> (m_storage),
10592 std::get<0> (m_storage));
10593
10594 val = ada_value_cast (expect_type, val);
10595
10596 /* Follow the Ada language semantics that do not allow taking
10597 an address of the result of a cast (view conversion in Ada). */
10598 if (VALUE_LVAL (val) == lval_memory)
10599 {
10600 if (value_lazy (val))
10601 value_fetch_lazy (val);
10602 VALUE_LVAL (val) = not_lval;
10603 }
10604 return val;
10605}
10606
10607value *
10608ada_var_value_operation::evaluate (struct type *expect_type,
10609 struct expression *exp,
10610 enum noside noside)
10611{
10612 symbol *sym = std::get<0> (m_storage);
10613
10614 if (SYMBOL_DOMAIN (sym) == UNDEF_DOMAIN)
10615 /* Only encountered when an unresolved symbol occurs in a
10616 context other than a function call, in which case, it is
10617 invalid. */
10618 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10619 sym->print_name ());
10620
10621 if (noside == EVAL_AVOID_SIDE_EFFECTS)
10622 {
10623 struct type *type = static_unwrap_type (SYMBOL_TYPE (sym));
10624 /* Check to see if this is a tagged type. We also need to handle
10625 the case where the type is a reference to a tagged type, but
10626 we have to be careful to exclude pointers to tagged types.
10627 The latter should be shown as usual (as a pointer), whereas
10628 a reference should mostly be transparent to the user. */
10629 if (ada_is_tagged_type (type, 0)
10630 || (type->code () == TYPE_CODE_REF
10631 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
10632 {
10633 /* Tagged types are a little special in the fact that the real
10634 type is dynamic and can only be determined by inspecting the
10635 object's tag. This means that we need to get the object's
10636 value first (EVAL_NORMAL) and then extract the actual object
10637 type from its tag.
10638
10639 Note that we cannot skip the final step where we extract
10640 the object type from its tag, because the EVAL_NORMAL phase
10641 results in dynamic components being resolved into fixed ones.
10642 This can cause problems when trying to print the type
10643 description of tagged types whose parent has a dynamic size:
10644 We use the type name of the "_parent" component in order
10645 to print the name of the ancestor type in the type description.
10646 If that component had a dynamic size, the resolution into
10647 a fixed type would result in the loss of that type name,
10648 thus preventing us from printing the name of the ancestor
10649 type in the type description. */
10650 value *arg1 = var_value_operation::evaluate (nullptr, exp,
10651 EVAL_NORMAL);
10652
10653 if (type->code () != TYPE_CODE_REF)
10654 {
10655 struct type *actual_type;
10656
10657 actual_type = type_from_tag (ada_value_tag (arg1));
10658 if (actual_type == NULL)
10659 /* If, for some reason, we were unable to determine
10660 the actual type from the tag, then use the static
10661 approximation that we just computed as a fallback.
10662 This can happen if the debugging information is
10663 incomplete, for instance. */
10664 actual_type = type;
10665 return value_zero (actual_type, not_lval);
10666 }
10667 else
10668 {
10669 /* In the case of a ref, ada_coerce_ref takes care
10670 of determining the actual type. But the evaluation
10671 should return a ref as it should be valid to ask
10672 for its address; so rebuild a ref after coerce. */
10673 arg1 = ada_coerce_ref (arg1);
10674 return value_ref (arg1, TYPE_CODE_REF);
10675 }
10676 }
10677
10678 /* Records and unions for which GNAT encodings have been
10679 generated need to be statically fixed as well.
10680 Otherwise, non-static fixing produces a type where
10681 all dynamic properties are removed, which prevents "ptype"
10682 from being able to completely describe the type.
10683 For instance, a case statement in a variant record would be
10684 replaced by the relevant components based on the actual
10685 value of the discriminants. */
10686 if ((type->code () == TYPE_CODE_STRUCT
10687 && dynamic_template_type (type) != NULL)
10688 || (type->code () == TYPE_CODE_UNION
10689 && ada_find_parallel_type (type, "___XVU") != NULL))
10690 return value_zero (to_static_fixed_type (type), not_lval);
10691 }
10692
10693 value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
10694 return ada_to_fixed_value (arg1);
10695}
10696
03070ee9
TT
10697}
10698
284614f0
JB
10699/* Implement the evaluate_exp routine in the exp_descriptor structure
10700 for the Ada language. */
10701
52ce6436 10702static struct value *
ebf56fd3 10703ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
dda83cd7 10704 int *pos, enum noside noside)
14f9c5c9
AS
10705{
10706 enum exp_opcode op;
b5385fc0 10707 int tem;
14f9c5c9 10708 int pc;
5ec18f2b 10709 int preeval_pos;
14f9c5c9
AS
10710 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
10711 struct type *type;
52ce6436 10712 int nargs, oplen;
d2e4a39e 10713 struct value **argvec;
14f9c5c9 10714
d2e4a39e
AS
10715 pc = *pos;
10716 *pos += 1;
14f9c5c9
AS
10717 op = exp->elts[pc].opcode;
10718
d2e4a39e 10719 switch (op)
14f9c5c9
AS
10720 {
10721 default:
10722 *pos -= 1;
6e48bd2c 10723 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
ca1f964d
JG
10724
10725 if (noside == EVAL_NORMAL)
10726 arg1 = unwrap_value (arg1);
6e48bd2c 10727
edd079d9 10728 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
dda83cd7
SM
10729 then we need to perform the conversion manually, because
10730 evaluate_subexp_standard doesn't do it. This conversion is
10731 necessary in Ada because the different kinds of float/fixed
10732 types in Ada have different representations.
6e48bd2c 10733
dda83cd7
SM
10734 Similarly, we need to perform the conversion from OP_LONG
10735 ourselves. */
edd079d9 10736 if ((op == OP_FLOAT || op == OP_LONG) && expect_type != NULL)
dda83cd7 10737 arg1 = ada_value_cast (expect_type, arg1);
6e48bd2c
JB
10738
10739 return arg1;
4c4b4cd2
PH
10740
10741 case OP_STRING:
10742 {
dda83cd7
SM
10743 struct value *result;
10744
10745 *pos -= 1;
10746 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
10747 /* The result type will have code OP_STRING, bashed there from
10748 OP_ARRAY. Bash it back. */
10749 if (value_type (result)->code () == TYPE_CODE_STRING)
10750 value_type (result)->set_code (TYPE_CODE_ARRAY);
10751 return result;
4c4b4cd2 10752 }
14f9c5c9
AS
10753
10754 case UNOP_CAST:
10755 (*pos) += 2;
10756 type = exp->elts[pc + 1].type;
ced9779b 10757 return ada_evaluate_subexp_for_cast (exp, pos, noside, type);
14f9c5c9 10758
4c4b4cd2
PH
10759 case UNOP_QUAL:
10760 (*pos) += 2;
10761 type = exp->elts[pc + 1].type;
10762 return ada_evaluate_subexp (type, exp, pos, noside);
10763
14f9c5c9 10764 case BINOP_ASSIGN:
fe1fe7ea 10765 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
52ce6436
PH
10766 if (exp->elts[*pos].opcode == OP_AGGREGATE)
10767 {
10768 arg1 = assign_aggregate (arg1, arg1, exp, pos, noside);
10769 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
10770 return arg1;
10771 return ada_value_assign (arg1, arg1);
10772 }
003f3813 10773 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
dda83cd7
SM
10774 except if the lhs of our assignment is a convenience variable.
10775 In the case of assigning to a convenience variable, the lhs
10776 should be exactly the result of the evaluation of the rhs. */
003f3813
JB
10777 type = value_type (arg1);
10778 if (VALUE_LVAL (arg1) == lval_internalvar)
dda83cd7 10779 type = NULL;
003f3813 10780 arg2 = evaluate_subexp (type, exp, pos, noside);
14f9c5c9 10781 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 10782 return arg1;
f411722c
TT
10783 if (VALUE_LVAL (arg1) == lval_internalvar)
10784 {
10785 /* Nothing. */
10786 }
d2e4a39e 10787 else
dda83cd7 10788 arg2 = coerce_for_assign (value_type (arg1), arg2);
4c4b4cd2 10789 return ada_value_assign (arg1, arg2);
14f9c5c9
AS
10790
10791 case BINOP_ADD:
10792 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
10793 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
10794 if (noside == EVAL_SKIP)
dda83cd7 10795 goto nosideret;
78134374 10796 if (value_type (arg1)->code () == TYPE_CODE_PTR)
dda83cd7
SM
10797 return (value_from_longest
10798 (value_type (arg1),
10799 value_as_long (arg1) + value_as_long (arg2)));
78134374 10800 if (value_type (arg2)->code () == TYPE_CODE_PTR)
dda83cd7
SM
10801 return (value_from_longest
10802 (value_type (arg2),
10803 value_as_long (arg1) + value_as_long (arg2)));
b49180ac
TT
10804 /* Preserve the original type for use by the range case below.
10805 We cannot cast the result to a reference type, so if ARG1 is
10806 a reference type, find its underlying type. */
b7789565 10807 type = value_type (arg1);
78134374 10808 while (type->code () == TYPE_CODE_REF)
dda83cd7 10809 type = TYPE_TARGET_TYPE (type);
bbcdf9ab 10810 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
b49180ac
TT
10811 arg1 = value_binop (arg1, arg2, BINOP_ADD);
10812 /* We need to special-case the result of adding to a range.
10813 This is done for the benefit of "ptype". gdb's Ada support
10814 historically used the LHS to set the result type here, so
10815 preserve this behavior. */
10816 if (type->code () == TYPE_CODE_RANGE)
10817 arg1 = value_cast (type, arg1);
10818 return arg1;
14f9c5c9
AS
10819
10820 case BINOP_SUB:
10821 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
10822 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
10823 if (noside == EVAL_SKIP)
dda83cd7 10824 goto nosideret;
78134374 10825 if (value_type (arg1)->code () == TYPE_CODE_PTR)
dda83cd7
SM
10826 return (value_from_longest
10827 (value_type (arg1),
10828 value_as_long (arg1) - value_as_long (arg2)));
78134374 10829 if (value_type (arg2)->code () == TYPE_CODE_PTR)
dda83cd7
SM
10830 return (value_from_longest
10831 (value_type (arg2),
10832 value_as_long (arg1) - value_as_long (arg2)));
b49180ac
TT
10833 /* Preserve the original type for use by the range case below.
10834 We cannot cast the result to a reference type, so if ARG1 is
10835 a reference type, find its underlying type. */
b7789565 10836 type = value_type (arg1);
78134374 10837 while (type->code () == TYPE_CODE_REF)
dda83cd7 10838 type = TYPE_TARGET_TYPE (type);
bbcdf9ab 10839 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
b49180ac
TT
10840 arg1 = value_binop (arg1, arg2, BINOP_SUB);
10841 /* We need to special-case the result of adding to a range.
10842 This is done for the benefit of "ptype". gdb's Ada support
10843 historically used the LHS to set the result type here, so
10844 preserve this behavior. */
10845 if (type->code () == TYPE_CODE_RANGE)
10846 arg1 = value_cast (type, arg1);
10847 return arg1;
14f9c5c9
AS
10848
10849 case BINOP_MUL:
10850 case BINOP_DIV:
e1578042
JB
10851 case BINOP_REM:
10852 case BINOP_MOD:
fe1fe7ea
SM
10853 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
10854 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 10855 if (noside == EVAL_SKIP)
dda83cd7 10856 goto nosideret;
faa1dfd7
TT
10857 return ada_mult_binop (expect_type, exp, noside, op,
10858 arg1, arg2);
4c4b4cd2 10859
4c4b4cd2
PH
10860 case BINOP_EQUAL:
10861 case BINOP_NOTEQUAL:
fe1fe7ea 10862 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
df407dfe 10863 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
14f9c5c9 10864 if (noside == EVAL_SKIP)
dda83cd7 10865 goto nosideret;
214b13ac 10866 return ada_equal_binop (expect_type, exp, noside, op, arg1, arg2);
4c4b4cd2
PH
10867
10868 case UNOP_NEG:
fe1fe7ea 10869 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
82390ab8 10870 return ada_unop_neg (expect_type, exp, noside, op, arg1);
4c4b4cd2 10871
2330c6c6
JB
10872 case BINOP_LOGICAL_AND:
10873 case BINOP_LOGICAL_OR:
10874 case UNOP_LOGICAL_NOT:
000d5124 10875 {
dda83cd7 10876 struct value *val;
000d5124 10877
dda83cd7
SM
10878 *pos -= 1;
10879 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
fbb06eb1 10880 type = language_bool_type (exp->language_defn, exp->gdbarch);
dda83cd7 10881 return value_cast (type, val);
000d5124 10882 }
2330c6c6
JB
10883
10884 case BINOP_BITWISE_AND:
10885 case BINOP_BITWISE_IOR:
10886 case BINOP_BITWISE_XOR:
000d5124 10887 {
dda83cd7 10888 struct value *val;
000d5124 10889
fe1fe7ea
SM
10890 arg1 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
10891 *pos = pc;
dda83cd7 10892 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
000d5124 10893
dda83cd7 10894 return value_cast (value_type (arg1), val);
000d5124 10895 }
2330c6c6 10896
14f9c5c9
AS
10897 case OP_VAR_VALUE:
10898 *pos -= 1;
6799def4 10899
14f9c5c9 10900 if (noside == EVAL_SKIP)
dda83cd7
SM
10901 {
10902 *pos += 4;
10903 goto nosideret;
10904 }
da5c522f
JB
10905
10906 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
dda83cd7
SM
10907 /* Only encountered when an unresolved symbol occurs in a
10908 context other than a function call, in which case, it is
10909 invalid. */
10910 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10911 exp->elts[pc + 2].symbol->print_name ());
da5c522f
JB
10912
10913 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7
SM
10914 {
10915 type = static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
10916 /* Check to see if this is a tagged type. We also need to handle
10917 the case where the type is a reference to a tagged type, but
10918 we have to be careful to exclude pointers to tagged types.
10919 The latter should be shown as usual (as a pointer), whereas
10920 a reference should mostly be transparent to the user. */
10921 if (ada_is_tagged_type (type, 0)
10922 || (type->code () == TYPE_CODE_REF
10923 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
0d72a7c3
JB
10924 {
10925 /* Tagged types are a little special in the fact that the real
10926 type is dynamic and can only be determined by inspecting the
10927 object's tag. This means that we need to get the object's
10928 value first (EVAL_NORMAL) and then extract the actual object
10929 type from its tag.
10930
10931 Note that we cannot skip the final step where we extract
10932 the object type from its tag, because the EVAL_NORMAL phase
10933 results in dynamic components being resolved into fixed ones.
10934 This can cause problems when trying to print the type
10935 description of tagged types whose parent has a dynamic size:
10936 We use the type name of the "_parent" component in order
10937 to print the name of the ancestor type in the type description.
10938 If that component had a dynamic size, the resolution into
10939 a fixed type would result in the loss of that type name,
10940 thus preventing us from printing the name of the ancestor
10941 type in the type description. */
fe1fe7ea 10942 arg1 = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
0d72a7c3 10943
78134374 10944 if (type->code () != TYPE_CODE_REF)
0d72a7c3
JB
10945 {
10946 struct type *actual_type;
10947
10948 actual_type = type_from_tag (ada_value_tag (arg1));
10949 if (actual_type == NULL)
10950 /* If, for some reason, we were unable to determine
10951 the actual type from the tag, then use the static
10952 approximation that we just computed as a fallback.
10953 This can happen if the debugging information is
10954 incomplete, for instance. */
10955 actual_type = type;
10956 return value_zero (actual_type, not_lval);
10957 }
10958 else
10959 {
10960 /* In the case of a ref, ada_coerce_ref takes care
10961 of determining the actual type. But the evaluation
10962 should return a ref as it should be valid to ask
10963 for its address; so rebuild a ref after coerce. */
10964 arg1 = ada_coerce_ref (arg1);
a65cfae5 10965 return value_ref (arg1, TYPE_CODE_REF);
0d72a7c3
JB
10966 }
10967 }
0c1f74cf 10968
84754697
JB
10969 /* Records and unions for which GNAT encodings have been
10970 generated need to be statically fixed as well.
10971 Otherwise, non-static fixing produces a type where
10972 all dynamic properties are removed, which prevents "ptype"
10973 from being able to completely describe the type.
10974 For instance, a case statement in a variant record would be
10975 replaced by the relevant components based on the actual
10976 value of the discriminants. */
78134374 10977 if ((type->code () == TYPE_CODE_STRUCT
84754697 10978 && dynamic_template_type (type) != NULL)
78134374 10979 || (type->code () == TYPE_CODE_UNION
84754697
JB
10980 && ada_find_parallel_type (type, "___XVU") != NULL))
10981 {
10982 *pos += 4;
10983 return value_zero (to_static_fixed_type (type), not_lval);
10984 }
dda83cd7 10985 }
da5c522f
JB
10986
10987 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
10988 return ada_to_fixed_value (arg1);
4c4b4cd2
PH
10989
10990 case OP_FUNCALL:
10991 (*pos) += 2;
10992
10993 /* Allocate arg vector, including space for the function to be
dda83cd7 10994 called in argvec[0] and a terminating NULL. */
4c4b4cd2 10995 nargs = longest_to_int (exp->elts[pc + 1].longconst);
8d749320 10996 argvec = XALLOCAVEC (struct value *, nargs + 2);
4c4b4cd2
PH
10997
10998 if (exp->elts[*pos].opcode == OP_VAR_VALUE
dda83cd7
SM
10999 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
11000 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11001 exp->elts[pc + 5].symbol->print_name ());
4c4b4cd2 11002 else
dda83cd7
SM
11003 {
11004 for (tem = 0; tem <= nargs; tem += 1)
fe1fe7ea
SM
11005 argvec[tem] = evaluate_subexp (nullptr, exp, pos, noside);
11006 argvec[tem] = 0;
4c4b4cd2 11007
dda83cd7
SM
11008 if (noside == EVAL_SKIP)
11009 goto nosideret;
11010 }
4c4b4cd2 11011
ad82864c
JB
11012 if (ada_is_constrained_packed_array_type
11013 (desc_base_type (value_type (argvec[0]))))
dda83cd7 11014 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
78134374 11015 else if (value_type (argvec[0])->code () == TYPE_CODE_ARRAY
dda83cd7
SM
11016 && TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
11017 /* This is a packed array that has already been fixed, and
284614f0
JB
11018 therefore already coerced to a simple array. Nothing further
11019 to do. */
dda83cd7 11020 ;
78134374 11021 else if (value_type (argvec[0])->code () == TYPE_CODE_REF)
e6c2c623
PMR
11022 {
11023 /* Make sure we dereference references so that all the code below
11024 feels like it's really handling the referenced value. Wrapping
11025 types (for alignment) may be there, so make sure we strip them as
11026 well. */
11027 argvec[0] = ada_to_fixed_value (coerce_ref (argvec[0]));
11028 }
78134374 11029 else if (value_type (argvec[0])->code () == TYPE_CODE_ARRAY
e6c2c623
PMR
11030 && VALUE_LVAL (argvec[0]) == lval_memory)
11031 argvec[0] = value_addr (argvec[0]);
4c4b4cd2 11032
df407dfe 11033 type = ada_check_typedef (value_type (argvec[0]));
720d1a40
JB
11034
11035 /* Ada allows us to implicitly dereference arrays when subscripting
8f465ea7
JB
11036 them. So, if this is an array typedef (encoding use for array
11037 access types encoded as fat pointers), strip it now. */
78134374 11038 if (type->code () == TYPE_CODE_TYPEDEF)
720d1a40
JB
11039 type = ada_typedef_target_type (type);
11040
78134374 11041 if (type->code () == TYPE_CODE_PTR)
dda83cd7
SM
11042 {
11043 switch (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ())
11044 {
11045 case TYPE_CODE_FUNC:
11046 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11047 break;
11048 case TYPE_CODE_ARRAY:
11049 break;
11050 case TYPE_CODE_STRUCT:
11051 if (noside != EVAL_AVOID_SIDE_EFFECTS)
11052 argvec[0] = ada_value_ind (argvec[0]);
11053 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
11054 break;
11055 default:
11056 error (_("cannot subscript or call something of type `%s'"),
11057 ada_type_name (value_type (argvec[0])));
11058 break;
11059 }
11060 }
4c4b4cd2 11061
78134374 11062 switch (type->code ())
dda83cd7
SM
11063 {
11064 case TYPE_CODE_FUNC:
11065 if (noside == EVAL_AVOID_SIDE_EFFECTS)
c8ea1972 11066 {
7022349d
PA
11067 if (TYPE_TARGET_TYPE (type) == NULL)
11068 error_call_unknown_return_type (NULL);
11069 return allocate_value (TYPE_TARGET_TYPE (type));
c8ea1972 11070 }
e71585ff
PA
11071 return call_function_by_hand (argvec[0], NULL,
11072 gdb::make_array_view (argvec + 1,
11073 nargs));
c8ea1972
PH
11074 case TYPE_CODE_INTERNAL_FUNCTION:
11075 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11076 /* We don't know anything about what the internal
11077 function might return, but we have to return
11078 something. */
11079 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11080 not_lval);
11081 else
11082 return call_internal_function (exp->gdbarch, exp->language_defn,
11083 argvec[0], nargs, argvec + 1);
11084
dda83cd7
SM
11085 case TYPE_CODE_STRUCT:
11086 {
11087 int arity;
11088
11089 arity = ada_array_arity (type);
11090 type = ada_array_element_type (type, nargs);
11091 if (type == NULL)
11092 error (_("cannot subscript or call a record"));
11093 if (arity != nargs)
11094 error (_("wrong number of subscripts; expecting %d"), arity);
11095 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11096 return value_zero (ada_aligned_type (type), lval_memory);
11097 return
11098 unwrap_value (ada_value_subscript
11099 (argvec[0], nargs, argvec + 1));
11100 }
11101 case TYPE_CODE_ARRAY:
11102 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11103 {
11104 type = ada_array_element_type (type, nargs);
11105 if (type == NULL)
11106 error (_("element type of array unknown"));
11107 else
11108 return value_zero (ada_aligned_type (type), lval_memory);
11109 }
11110 return
11111 unwrap_value (ada_value_subscript
11112 (ada_coerce_to_simple_array (argvec[0]),
11113 nargs, argvec + 1));
11114 case TYPE_CODE_PTR: /* Pointer to array */
11115 if (noside == EVAL_AVOID_SIDE_EFFECTS)
11116 {
deede10c 11117 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
dda83cd7
SM
11118 type = ada_array_element_type (type, nargs);
11119 if (type == NULL)
11120 error (_("element type of array unknown"));
11121 else
11122 return value_zero (ada_aligned_type (type), lval_memory);
11123 }
11124 return
11125 unwrap_value (ada_value_ptr_subscript (argvec[0],
deede10c 11126 nargs, argvec + 1));
4c4b4cd2 11127
dda83cd7
SM
11128 default:
11129 error (_("Attempt to index or call something other than an "
e1d5a0d2 11130 "array or function"));
dda83cd7 11131 }
4c4b4cd2
PH
11132
11133 case TERNOP_SLICE:
11134 {
fe1fe7ea
SM
11135 struct value *array = evaluate_subexp (nullptr, exp, pos, noside);
11136 struct value *low_bound_val
11137 = evaluate_subexp (nullptr, exp, pos, noside);
11138 struct value *high_bound_val
11139 = evaluate_subexp (nullptr, exp, pos, noside);
dda83cd7
SM
11140
11141 if (noside == EVAL_SKIP)
11142 goto nosideret;
11143
5ce19db8
TT
11144 return ada_ternop_slice (exp, noside, array, low_bound_val,
11145 high_bound_val);
4c4b4cd2 11146 }
14f9c5c9 11147
4c4b4cd2
PH
11148 case UNOP_IN_RANGE:
11149 (*pos) += 2;
fe1fe7ea 11150 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
8008e265 11151 type = check_typedef (exp->elts[pc + 1].type);
7efc87ff 11152 return ada_unop_in_range (expect_type, exp, noside, op, arg1, type);
4c4b4cd2
PH
11153
11154 case BINOP_IN_BOUNDS:
14f9c5c9 11155 (*pos) += 2;
fe1fe7ea
SM
11156 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
11157 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 11158
4c4b4cd2 11159 if (noside == EVAL_SKIP)
dda83cd7 11160 goto nosideret;
14f9c5c9 11161
4c4b4cd2 11162 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 11163
b467efaa 11164 return ada_binop_in_bounds (exp, noside, arg1, arg2, tem);
4c4b4cd2
PH
11165
11166 case TERNOP_IN_RANGE:
fe1fe7ea
SM
11167 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
11168 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
11169 arg3 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11170
62d4bd94 11171 return eval_ternop_in_range (expect_type, exp, noside, arg1, arg2, arg3);
4c4b4cd2
PH
11172
11173 case OP_ATR_FIRST:
11174 case OP_ATR_LAST:
11175 case OP_ATR_LENGTH:
11176 {
dda83cd7 11177 struct type *type_arg;
5b4ee69b 11178
dda83cd7
SM
11179 if (exp->elts[*pos].opcode == OP_TYPE)
11180 {
fe1fe7ea
SM
11181 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
11182 arg1 = NULL;
dda83cd7
SM
11183 type_arg = check_typedef (exp->elts[pc + 2].type);
11184 }
11185 else
11186 {
fe1fe7ea
SM
11187 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
11188 type_arg = NULL;
dda83cd7 11189 }
76a01679 11190
dda83cd7
SM
11191 if (exp->elts[*pos].opcode != OP_LONG)
11192 error (_("Invalid operand to '%s"), ada_attribute_name (op));
11193 tem = longest_to_int (exp->elts[*pos + 2].longconst);
11194 *pos += 4;
76a01679 11195
dda83cd7
SM
11196 if (noside == EVAL_SKIP)
11197 goto nosideret;
1eea4ebd 11198
b84564fc 11199 return ada_unop_atr (exp, noside, op, arg1, type_arg, tem);
14f9c5c9
AS
11200 }
11201
4c4b4cd2 11202 case OP_ATR_TAG:
fe1fe7ea 11203 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11204 if (noside == EVAL_SKIP)
dda83cd7 11205 goto nosideret;
020dbabe 11206 return ada_atr_tag (expect_type, exp, noside, op, arg1);
4c4b4cd2
PH
11207
11208 case OP_ATR_MIN:
11209 case OP_ATR_MAX:
fe1fe7ea
SM
11210 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
11211 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
11212 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 11213 if (noside == EVAL_SKIP)
dda83cd7 11214 goto nosideret;
38dc70cf 11215 return ada_binop_minmax (expect_type, exp, noside, op, arg1, arg2);
14f9c5c9 11216
4c4b4cd2
PH
11217 case OP_ATR_MODULUS:
11218 {
dda83cd7 11219 struct type *type_arg = check_typedef (exp->elts[pc + 2].type);
4c4b4cd2 11220
fe1fe7ea
SM
11221 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
11222 if (noside == EVAL_SKIP)
dda83cd7 11223 goto nosideret;
4c4b4cd2 11224
dda83cd7
SM
11225 if (!ada_is_modular_type (type_arg))
11226 error (_("'modulus must be applied to modular type"));
4c4b4cd2 11227
dda83cd7
SM
11228 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
11229 ada_modulus (type_arg));
4c4b4cd2
PH
11230 }
11231
11232
11233 case OP_ATR_POS:
fe1fe7ea
SM
11234 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
11235 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 11236 if (noside == EVAL_SKIP)
dda83cd7 11237 goto nosideret;
7992accc 11238 return ada_pos_atr (expect_type, exp, noside, op, arg1);
14f9c5c9 11239
4c4b4cd2 11240 case OP_ATR_SIZE:
fe1fe7ea 11241 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
68c75735 11242 return ada_atr_size (expect_type, exp, noside, op, arg1);
4c4b4cd2
PH
11243
11244 case OP_ATR_VAL:
fe1fe7ea
SM
11245 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
11246 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11247 type = exp->elts[pc + 2].type;
14f9c5c9 11248 if (noside == EVAL_SKIP)
dda83cd7 11249 goto nosideret;
3848abd6 11250 return ada_val_atr (noside, type, arg1);
4c4b4cd2
PH
11251
11252 case BINOP_EXP:
fe1fe7ea
SM
11253 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
11254 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11255 if (noside == EVAL_SKIP)
dda83cd7 11256 goto nosideret;
dd5fd283 11257 return ada_binop_exp (expect_type, exp, noside, op, arg1, arg2);
4c4b4cd2
PH
11258
11259 case UNOP_PLUS:
fe1fe7ea 11260 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11261 if (noside == EVAL_SKIP)
dda83cd7 11262 goto nosideret;
4c4b4cd2 11263 else
dda83cd7 11264 return arg1;
4c4b4cd2
PH
11265
11266 case UNOP_ABS:
fe1fe7ea 11267 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
4c4b4cd2 11268 if (noside == EVAL_SKIP)
dda83cd7 11269 goto nosideret;
d05e24e6 11270 return ada_abs (expect_type, exp, noside, op, arg1);
14f9c5c9
AS
11271
11272 case UNOP_IND:
5ec18f2b 11273 preeval_pos = *pos;
fe1fe7ea 11274 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 11275 if (noside == EVAL_SKIP)
dda83cd7 11276 goto nosideret;
df407dfe 11277 type = ada_check_typedef (value_type (arg1));
14f9c5c9 11278 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7
SM
11279 {
11280 if (ada_is_array_descriptor_type (type))
11281 /* GDB allows dereferencing GNAT array descriptors. */
11282 {
11283 struct type *arrType = ada_type_of_array (arg1, 0);
11284
11285 if (arrType == NULL)
11286 error (_("Attempt to dereference null array pointer."));
11287 return value_at_lazy (arrType, 0);
11288 }
11289 else if (type->code () == TYPE_CODE_PTR
11290 || type->code () == TYPE_CODE_REF
11291 /* In C you can dereference an array to get the 1st elt. */
11292 || type->code () == TYPE_CODE_ARRAY)
11293 {
11294 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11295 only be determined by inspecting the object's tag.
11296 This means that we need to evaluate completely the
11297 expression in order to get its type. */
5ec18f2b 11298
78134374
SM
11299 if ((type->code () == TYPE_CODE_REF
11300 || type->code () == TYPE_CODE_PTR)
5ec18f2b
JG
11301 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
11302 {
fe1fe7ea
SM
11303 arg1
11304 = evaluate_subexp (nullptr, exp, &preeval_pos, EVAL_NORMAL);
5ec18f2b
JG
11305 type = value_type (ada_value_ind (arg1));
11306 }
11307 else
11308 {
11309 type = to_static_fixed_type
11310 (ada_aligned_type
11311 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
11312 }
c1b5a1a6 11313 ada_ensure_varsize_limit (type);
dda83cd7
SM
11314 return value_zero (type, lval_memory);
11315 }
11316 else if (type->code () == TYPE_CODE_INT)
6b0d7253
JB
11317 {
11318 /* GDB allows dereferencing an int. */
11319 if (expect_type == NULL)
11320 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
11321 lval_memory);
11322 else
11323 {
11324 expect_type =
11325 to_static_fixed_type (ada_aligned_type (expect_type));
11326 return value_zero (expect_type, lval_memory);
11327 }
11328 }
dda83cd7
SM
11329 else
11330 error (_("Attempt to take contents of a non-pointer value."));
11331 }
0963b4bd 11332 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
df407dfe 11333 type = ada_check_typedef (value_type (arg1));
d2e4a39e 11334
78134374 11335 if (type->code () == TYPE_CODE_INT)
dda83cd7
SM
11336 /* GDB allows dereferencing an int. If we were given
11337 the expect_type, then use that as the target type.
11338 Otherwise, assume that the target type is an int. */
11339 {
11340 if (expect_type != NULL)
96967637
JB
11341 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
11342 arg1));
11343 else
11344 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
11345 (CORE_ADDR) value_as_address (arg1));
dda83cd7 11346 }
6b0d7253 11347
4c4b4cd2 11348 if (ada_is_array_descriptor_type (type))
dda83cd7
SM
11349 /* GDB allows dereferencing GNAT array descriptors. */
11350 return ada_coerce_to_simple_array (arg1);
14f9c5c9 11351 else
dda83cd7 11352 return ada_value_ind (arg1);
14f9c5c9
AS
11353
11354 case STRUCTOP_STRUCT:
11355 tem = longest_to_int (exp->elts[pc + 1].longconst);
11356 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
5ec18f2b 11357 preeval_pos = *pos;
fe1fe7ea 11358 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
14f9c5c9 11359 if (noside == EVAL_SKIP)
dda83cd7 11360 goto nosideret;
14f9c5c9 11361 if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7
SM
11362 {
11363 struct type *type1 = value_type (arg1);
5b4ee69b 11364
dda83cd7
SM
11365 if (ada_is_tagged_type (type1, 1))
11366 {
11367 type = ada_lookup_struct_elt_type (type1,
11368 &exp->elts[pc + 2].string,
11369 1, 1);
5ec18f2b
JG
11370
11371 /* If the field is not found, check if it exists in the
11372 extension of this object's type. This means that we
11373 need to evaluate completely the expression. */
11374
dda83cd7 11375 if (type == NULL)
5ec18f2b 11376 {
fe1fe7ea
SM
11377 arg1
11378 = evaluate_subexp (nullptr, exp, &preeval_pos, EVAL_NORMAL);
5ec18f2b
JG
11379 arg1 = ada_value_struct_elt (arg1,
11380 &exp->elts[pc + 2].string,
11381 0);
11382 arg1 = unwrap_value (arg1);
11383 type = value_type (ada_to_fixed_value (arg1));
11384 }
dda83cd7
SM
11385 }
11386 else
11387 type =
11388 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
11389 0);
11390
11391 return value_zero (ada_aligned_type (type), lval_memory);
11392 }
14f9c5c9 11393 else
a579cd9a
MW
11394 {
11395 arg1 = ada_value_struct_elt (arg1, &exp->elts[pc + 2].string, 0);
11396 arg1 = unwrap_value (arg1);
11397 return ada_to_fixed_value (arg1);
11398 }
284614f0 11399
14f9c5c9 11400 case OP_TYPE:
4c4b4cd2 11401 /* The value is not supposed to be used. This is here to make it
dda83cd7 11402 easier to accommodate expressions that contain types. */
14f9c5c9
AS
11403 (*pos) += 2;
11404 if (noside == EVAL_SKIP)
dda83cd7 11405 goto nosideret;
14f9c5c9 11406 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
dda83cd7 11407 return allocate_value (exp->elts[pc + 1].type);
14f9c5c9 11408 else
dda83cd7 11409 error (_("Attempt to use a type name as an expression"));
52ce6436
PH
11410
11411 case OP_AGGREGATE:
11412 case OP_CHOICES:
11413 case OP_OTHERS:
11414 case OP_DISCRETE_RANGE:
11415 case OP_POSITIONAL:
11416 case OP_NAME:
11417 if (noside == EVAL_NORMAL)
11418 switch (op)
11419 {
11420 case OP_NAME:
11421 error (_("Undefined name, ambiguous name, or renaming used in "
e1d5a0d2 11422 "component association: %s."), &exp->elts[pc+2].string);
52ce6436
PH
11423 case OP_AGGREGATE:
11424 error (_("Aggregates only allowed on the right of an assignment"));
11425 default:
0963b4bd
MS
11426 internal_error (__FILE__, __LINE__,
11427 _("aggregate apparently mangled"));
52ce6436
PH
11428 }
11429
11430 ada_forward_operator_length (exp, pc, &oplen, &nargs);
11431 *pos += oplen - 1;
11432 for (tem = 0; tem < nargs; tem += 1)
11433 ada_evaluate_subexp (NULL, exp, pos, noside);
11434 goto nosideret;
14f9c5c9
AS
11435 }
11436
11437nosideret:
ced9779b 11438 return eval_skip_value (exp);
14f9c5c9 11439}
14f9c5c9 11440\f
d2e4a39e 11441
4c4b4cd2
PH
11442/* Return non-zero iff TYPE represents a System.Address type. */
11443
11444int
11445ada_is_system_address_type (struct type *type)
11446{
7d93a1e0 11447 return (type->name () && strcmp (type->name (), "system__address") == 0);
4c4b4cd2
PH
11448}
11449
14f9c5c9 11450\f
d2e4a39e 11451
dda83cd7 11452 /* Range types */
14f9c5c9
AS
11453
11454/* Scan STR beginning at position K for a discriminant name, and
11455 return the value of that discriminant field of DVAL in *PX. If
11456 PNEW_K is not null, put the position of the character beyond the
11457 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 11458 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
11459
11460static int
108d56a4 11461scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
dda83cd7 11462 int *pnew_k)
14f9c5c9 11463{
5f9febe0 11464 static std::string storage;
5da1a4d3 11465 const char *pstart, *pend, *bound;
d2e4a39e 11466 struct value *bound_val;
14f9c5c9
AS
11467
11468 if (dval == NULL || str == NULL || str[k] == '\0')
11469 return 0;
11470
5da1a4d3
SM
11471 pstart = str + k;
11472 pend = strstr (pstart, "__");
14f9c5c9
AS
11473 if (pend == NULL)
11474 {
5da1a4d3 11475 bound = pstart;
14f9c5c9
AS
11476 k += strlen (bound);
11477 }
d2e4a39e 11478 else
14f9c5c9 11479 {
5da1a4d3
SM
11480 int len = pend - pstart;
11481
11482 /* Strip __ and beyond. */
5f9febe0
TT
11483 storage = std::string (pstart, len);
11484 bound = storage.c_str ();
d2e4a39e 11485 k = pend - str;
14f9c5c9 11486 }
d2e4a39e 11487
df407dfe 11488 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
14f9c5c9
AS
11489 if (bound_val == NULL)
11490 return 0;
11491
11492 *px = value_as_long (bound_val);
11493 if (pnew_k != NULL)
11494 *pnew_k = k;
11495 return 1;
11496}
11497
25a1127b
TT
11498/* Value of variable named NAME. Only exact matches are considered.
11499 If no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
11500 otherwise causes an error with message ERR_MSG. */
11501
d2e4a39e 11502static struct value *
edb0c9cb 11503get_var_value (const char *name, const char *err_msg)
14f9c5c9 11504{
25a1127b
TT
11505 std::string quoted_name = add_angle_brackets (name);
11506
11507 lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
14f9c5c9 11508
d1183b06
TT
11509 std::vector<struct block_symbol> syms
11510 = ada_lookup_symbol_list_worker (lookup_name,
11511 get_selected_block (0),
11512 VAR_DOMAIN, 1);
14f9c5c9 11513
d1183b06 11514 if (syms.size () != 1)
14f9c5c9
AS
11515 {
11516 if (err_msg == NULL)
dda83cd7 11517 return 0;
14f9c5c9 11518 else
dda83cd7 11519 error (("%s"), err_msg);
14f9c5c9
AS
11520 }
11521
54d343a2 11522 return value_of_variable (syms[0].symbol, syms[0].block);
14f9c5c9 11523}
d2e4a39e 11524
edb0c9cb
PA
11525/* Value of integer variable named NAME in the current environment.
11526 If no such variable is found, returns false. Otherwise, sets VALUE
11527 to the variable's value and returns true. */
4c4b4cd2 11528
edb0c9cb
PA
11529bool
11530get_int_var_value (const char *name, LONGEST &value)
14f9c5c9 11531{
4c4b4cd2 11532 struct value *var_val = get_var_value (name, 0);
d2e4a39e 11533
14f9c5c9 11534 if (var_val == 0)
edb0c9cb
PA
11535 return false;
11536
11537 value = value_as_long (var_val);
11538 return true;
14f9c5c9 11539}
d2e4a39e 11540
14f9c5c9
AS
11541
11542/* Return a range type whose base type is that of the range type named
11543 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 11544 from NAME according to the GNAT range encoding conventions.
1ce677a4
UW
11545 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11546 corresponding range type from debug information; fall back to using it
11547 if symbol lookup fails. If a new type must be created, allocate it
11548 like ORIG_TYPE was. The bounds information, in general, is encoded
11549 in NAME, the base type given in the named range type. */
14f9c5c9 11550
d2e4a39e 11551static struct type *
28c85d6c 11552to_fixed_range_type (struct type *raw_type, struct value *dval)
14f9c5c9 11553{
0d5cff50 11554 const char *name;
14f9c5c9 11555 struct type *base_type;
108d56a4 11556 const char *subtype_info;
14f9c5c9 11557
28c85d6c 11558 gdb_assert (raw_type != NULL);
7d93a1e0 11559 gdb_assert (raw_type->name () != NULL);
dddfab26 11560
78134374 11561 if (raw_type->code () == TYPE_CODE_RANGE)
14f9c5c9
AS
11562 base_type = TYPE_TARGET_TYPE (raw_type);
11563 else
11564 base_type = raw_type;
11565
7d93a1e0 11566 name = raw_type->name ();
14f9c5c9
AS
11567 subtype_info = strstr (name, "___XD");
11568 if (subtype_info == NULL)
690cc4eb 11569 {
43bbcdc2
PH
11570 LONGEST L = ada_discrete_type_low_bound (raw_type);
11571 LONGEST U = ada_discrete_type_high_bound (raw_type);
5b4ee69b 11572
690cc4eb
PH
11573 if (L < INT_MIN || U > INT_MAX)
11574 return raw_type;
11575 else
0c9c3474
SA
11576 return create_static_range_type (alloc_type_copy (raw_type), raw_type,
11577 L, U);
690cc4eb 11578 }
14f9c5c9
AS
11579 else
11580 {
14f9c5c9
AS
11581 int prefix_len = subtype_info - name;
11582 LONGEST L, U;
11583 struct type *type;
108d56a4 11584 const char *bounds_str;
14f9c5c9
AS
11585 int n;
11586
14f9c5c9
AS
11587 subtype_info += 5;
11588 bounds_str = strchr (subtype_info, '_');
11589 n = 1;
11590
d2e4a39e 11591 if (*subtype_info == 'L')
dda83cd7
SM
11592 {
11593 if (!ada_scan_number (bounds_str, n, &L, &n)
11594 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
11595 return raw_type;
11596 if (bounds_str[n] == '_')
11597 n += 2;
11598 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
11599 n += 1;
11600 subtype_info += 1;
11601 }
d2e4a39e 11602 else
dda83cd7 11603 {
5f9febe0
TT
11604 std::string name_buf = std::string (name, prefix_len) + "___L";
11605 if (!get_int_var_value (name_buf.c_str (), L))
dda83cd7
SM
11606 {
11607 lim_warning (_("Unknown lower bound, using 1."));
11608 L = 1;
11609 }
11610 }
14f9c5c9 11611
d2e4a39e 11612 if (*subtype_info == 'U')
dda83cd7
SM
11613 {
11614 if (!ada_scan_number (bounds_str, n, &U, &n)
11615 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
11616 return raw_type;
11617 }
d2e4a39e 11618 else
dda83cd7 11619 {
5f9febe0
TT
11620 std::string name_buf = std::string (name, prefix_len) + "___U";
11621 if (!get_int_var_value (name_buf.c_str (), U))
dda83cd7
SM
11622 {
11623 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
11624 U = L;
11625 }
11626 }
14f9c5c9 11627
0c9c3474
SA
11628 type = create_static_range_type (alloc_type_copy (raw_type),
11629 base_type, L, U);
f5a91472 11630 /* create_static_range_type alters the resulting type's length
dda83cd7
SM
11631 to match the size of the base_type, which is not what we want.
11632 Set it back to the original range type's length. */
f5a91472 11633 TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
d0e39ea2 11634 type->set_name (name);
14f9c5c9
AS
11635 return type;
11636 }
11637}
11638
4c4b4cd2
PH
11639/* True iff NAME is the name of a range type. */
11640
14f9c5c9 11641int
d2e4a39e 11642ada_is_range_type_name (const char *name)
14f9c5c9
AS
11643{
11644 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 11645}
14f9c5c9 11646\f
d2e4a39e 11647
dda83cd7 11648 /* Modular types */
4c4b4cd2
PH
11649
11650/* True iff TYPE is an Ada modular type. */
14f9c5c9 11651
14f9c5c9 11652int
d2e4a39e 11653ada_is_modular_type (struct type *type)
14f9c5c9 11654{
18af8284 11655 struct type *subranged_type = get_base_type (type);
14f9c5c9 11656
78134374 11657 return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
dda83cd7
SM
11658 && subranged_type->code () == TYPE_CODE_INT
11659 && subranged_type->is_unsigned ());
14f9c5c9
AS
11660}
11661
4c4b4cd2
PH
11662/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11663
61ee279c 11664ULONGEST
0056e4d5 11665ada_modulus (struct type *type)
14f9c5c9 11666{
5e500d33
SM
11667 const dynamic_prop &high = type->bounds ()->high;
11668
11669 if (high.kind () == PROP_CONST)
11670 return (ULONGEST) high.const_val () + 1;
11671
11672 /* If TYPE is unresolved, the high bound might be a location list. Return
11673 0, for lack of a better value to return. */
11674 return 0;
14f9c5c9 11675}
d2e4a39e 11676\f
f7f9143b
JB
11677
11678/* Ada exception catchpoint support:
11679 ---------------------------------
11680
11681 We support 3 kinds of exception catchpoints:
11682 . catchpoints on Ada exceptions
11683 . catchpoints on unhandled Ada exceptions
11684 . catchpoints on failed assertions
11685
11686 Exceptions raised during failed assertions, or unhandled exceptions
11687 could perfectly be caught with the general catchpoint on Ada exceptions.
11688 However, we can easily differentiate these two special cases, and having
11689 the option to distinguish these two cases from the rest can be useful
11690 to zero-in on certain situations.
11691
11692 Exception catchpoints are a specialized form of breakpoint,
11693 since they rely on inserting breakpoints inside known routines
11694 of the GNAT runtime. The implementation therefore uses a standard
11695 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11696 of breakpoint_ops.
11697
0259addd
JB
11698 Support in the runtime for exception catchpoints have been changed
11699 a few times already, and these changes affect the implementation
11700 of these catchpoints. In order to be able to support several
11701 variants of the runtime, we use a sniffer that will determine
28010a5d 11702 the runtime variant used by the program being debugged. */
f7f9143b 11703
82eacd52
JB
11704/* Ada's standard exceptions.
11705
11706 The Ada 83 standard also defined Numeric_Error. But there so many
11707 situations where it was unclear from the Ada 83 Reference Manual
11708 (RM) whether Constraint_Error or Numeric_Error should be raised,
11709 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11710 Interpretation saying that anytime the RM says that Numeric_Error
11711 should be raised, the implementation may raise Constraint_Error.
11712 Ada 95 went one step further and pretty much removed Numeric_Error
11713 from the list of standard exceptions (it made it a renaming of
11714 Constraint_Error, to help preserve compatibility when compiling
11715 an Ada83 compiler). As such, we do not include Numeric_Error from
11716 this list of standard exceptions. */
3d0b0fa3 11717
27087b7f 11718static const char * const standard_exc[] = {
3d0b0fa3
JB
11719 "constraint_error",
11720 "program_error",
11721 "storage_error",
11722 "tasking_error"
11723};
11724
0259addd
JB
11725typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
11726
11727/* A structure that describes how to support exception catchpoints
11728 for a given executable. */
11729
11730struct exception_support_info
11731{
11732 /* The name of the symbol to break on in order to insert
11733 a catchpoint on exceptions. */
11734 const char *catch_exception_sym;
11735
11736 /* The name of the symbol to break on in order to insert
11737 a catchpoint on unhandled exceptions. */
11738 const char *catch_exception_unhandled_sym;
11739
11740 /* The name of the symbol to break on in order to insert
11741 a catchpoint on failed assertions. */
11742 const char *catch_assert_sym;
11743
9f757bf7
XR
11744 /* The name of the symbol to break on in order to insert
11745 a catchpoint on exception handling. */
11746 const char *catch_handlers_sym;
11747
0259addd
JB
11748 /* Assuming that the inferior just triggered an unhandled exception
11749 catchpoint, this function is responsible for returning the address
11750 in inferior memory where the name of that exception is stored.
11751 Return zero if the address could not be computed. */
11752 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
11753};
11754
11755static CORE_ADDR ada_unhandled_exception_name_addr (void);
11756static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
11757
11758/* The following exception support info structure describes how to
11759 implement exception catchpoints with the latest version of the
ca683e3a 11760 Ada runtime (as of 2019-08-??). */
0259addd
JB
11761
11762static const struct exception_support_info default_exception_support_info =
ca683e3a
AO
11763{
11764 "__gnat_debug_raise_exception", /* catch_exception_sym */
11765 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11766 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11767 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11768 ada_unhandled_exception_name_addr
11769};
11770
11771/* The following exception support info structure describes how to
11772 implement exception catchpoints with an earlier version of the
11773 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11774
11775static const struct exception_support_info exception_support_info_v0 =
0259addd
JB
11776{
11777 "__gnat_debug_raise_exception", /* catch_exception_sym */
11778 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11779 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
9f757bf7 11780 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11781 ada_unhandled_exception_name_addr
11782};
11783
11784/* The following exception support info structure describes how to
11785 implement exception catchpoints with a slightly older version
11786 of the Ada runtime. */
11787
11788static const struct exception_support_info exception_support_info_fallback =
11789{
11790 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11791 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11792 "system__assertions__raise_assert_failure", /* catch_assert_sym */
9f757bf7 11793 "__gnat_begin_handler", /* catch_handlers_sym */
0259addd
JB
11794 ada_unhandled_exception_name_addr_from_raise
11795};
11796
f17011e0
JB
11797/* Return nonzero if we can detect the exception support routines
11798 described in EINFO.
11799
11800 This function errors out if an abnormal situation is detected
11801 (for instance, if we find the exception support routines, but
11802 that support is found to be incomplete). */
11803
11804static int
11805ada_has_this_exception_support (const struct exception_support_info *einfo)
11806{
11807 struct symbol *sym;
11808
11809 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11810 that should be compiled with debugging information. As a result, we
11811 expect to find that symbol in the symtabs. */
11812
11813 sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
11814 if (sym == NULL)
a6af7abe
JB
11815 {
11816 /* Perhaps we did not find our symbol because the Ada runtime was
11817 compiled without debugging info, or simply stripped of it.
11818 It happens on some GNU/Linux distributions for instance, where
11819 users have to install a separate debug package in order to get
11820 the runtime's debugging info. In that situation, let the user
11821 know why we cannot insert an Ada exception catchpoint.
11822
11823 Note: Just for the purpose of inserting our Ada exception
11824 catchpoint, we could rely purely on the associated minimal symbol.
11825 But we would be operating in degraded mode anyway, since we are
11826 still lacking the debugging info needed later on to extract
11827 the name of the exception being raised (this name is printed in
11828 the catchpoint message, and is also used when trying to catch
11829 a specific exception). We do not handle this case for now. */
3b7344d5 11830 struct bound_minimal_symbol msym
1c8e84b0
JB
11831 = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
11832
3b7344d5 11833 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
a6af7abe
JB
11834 error (_("Your Ada runtime appears to be missing some debugging "
11835 "information.\nCannot insert Ada exception catchpoint "
11836 "in this configuration."));
11837
11838 return 0;
11839 }
f17011e0
JB
11840
11841 /* Make sure that the symbol we found corresponds to a function. */
11842
11843 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
ca683e3a
AO
11844 {
11845 error (_("Symbol \"%s\" is not a function (class = %d)"),
987012b8 11846 sym->linkage_name (), SYMBOL_CLASS (sym));
ca683e3a
AO
11847 return 0;
11848 }
11849
11850 sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
11851 if (sym == NULL)
11852 {
11853 struct bound_minimal_symbol msym
11854 = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
11855
11856 if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
11857 error (_("Your Ada runtime appears to be missing some debugging "
11858 "information.\nCannot insert Ada exception catchpoint "
11859 "in this configuration."));
11860
11861 return 0;
11862 }
11863
11864 /* Make sure that the symbol we found corresponds to a function. */
11865
11866 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11867 {
11868 error (_("Symbol \"%s\" is not a function (class = %d)"),
987012b8 11869 sym->linkage_name (), SYMBOL_CLASS (sym));
ca683e3a
AO
11870 return 0;
11871 }
f17011e0
JB
11872
11873 return 1;
11874}
11875
0259addd
JB
11876/* Inspect the Ada runtime and determine which exception info structure
11877 should be used to provide support for exception catchpoints.
11878
3eecfa55
JB
11879 This function will always set the per-inferior exception_info,
11880 or raise an error. */
0259addd
JB
11881
11882static void
11883ada_exception_support_info_sniffer (void)
11884{
3eecfa55 11885 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
0259addd
JB
11886
11887 /* If the exception info is already known, then no need to recompute it. */
3eecfa55 11888 if (data->exception_info != NULL)
0259addd
JB
11889 return;
11890
11891 /* Check the latest (default) exception support info. */
f17011e0 11892 if (ada_has_this_exception_support (&default_exception_support_info))
0259addd 11893 {
3eecfa55 11894 data->exception_info = &default_exception_support_info;
0259addd
JB
11895 return;
11896 }
11897
ca683e3a
AO
11898 /* Try the v0 exception suport info. */
11899 if (ada_has_this_exception_support (&exception_support_info_v0))
11900 {
11901 data->exception_info = &exception_support_info_v0;
11902 return;
11903 }
11904
0259addd 11905 /* Try our fallback exception suport info. */
f17011e0 11906 if (ada_has_this_exception_support (&exception_support_info_fallback))
0259addd 11907 {
3eecfa55 11908 data->exception_info = &exception_support_info_fallback;
0259addd
JB
11909 return;
11910 }
11911
11912 /* Sometimes, it is normal for us to not be able to find the routine
11913 we are looking for. This happens when the program is linked with
11914 the shared version of the GNAT runtime, and the program has not been
11915 started yet. Inform the user of these two possible causes if
11916 applicable. */
11917
ccefe4c4 11918 if (ada_update_initial_language (language_unknown) != language_ada)
0259addd
JB
11919 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11920
11921 /* If the symbol does not exist, then check that the program is
11922 already started, to make sure that shared libraries have been
11923 loaded. If it is not started, this may mean that the symbol is
11924 in a shared library. */
11925
e99b03dc 11926 if (inferior_ptid.pid () == 0)
0259addd
JB
11927 error (_("Unable to insert catchpoint. Try to start the program first."));
11928
11929 /* At this point, we know that we are debugging an Ada program and
11930 that the inferior has been started, but we still are not able to
0963b4bd 11931 find the run-time symbols. That can mean that we are in
0259addd
JB
11932 configurable run time mode, or that a-except as been optimized
11933 out by the linker... In any case, at this point it is not worth
11934 supporting this feature. */
11935
7dda8cff 11936 error (_("Cannot insert Ada exception catchpoints in this configuration."));
0259addd
JB
11937}
11938
f7f9143b
JB
11939/* True iff FRAME is very likely to be that of a function that is
11940 part of the runtime system. This is all very heuristic, but is
11941 intended to be used as advice as to what frames are uninteresting
11942 to most users. */
11943
11944static int
11945is_known_support_routine (struct frame_info *frame)
11946{
692465f1 11947 enum language func_lang;
f7f9143b 11948 int i;
f35a17b5 11949 const char *fullname;
f7f9143b 11950
4ed6b5be
JB
11951 /* If this code does not have any debugging information (no symtab),
11952 This cannot be any user code. */
f7f9143b 11953
51abb421 11954 symtab_and_line sal = find_frame_sal (frame);
f7f9143b
JB
11955 if (sal.symtab == NULL)
11956 return 1;
11957
4ed6b5be
JB
11958 /* If there is a symtab, but the associated source file cannot be
11959 located, then assume this is not user code: Selecting a frame
11960 for which we cannot display the code would not be very helpful
11961 for the user. This should also take care of case such as VxWorks
11962 where the kernel has some debugging info provided for a few units. */
f7f9143b 11963
f35a17b5
JK
11964 fullname = symtab_to_fullname (sal.symtab);
11965 if (access (fullname, R_OK) != 0)
f7f9143b
JB
11966 return 1;
11967
85102364 11968 /* Check the unit filename against the Ada runtime file naming.
4ed6b5be
JB
11969 We also check the name of the objfile against the name of some
11970 known system libraries that sometimes come with debugging info
11971 too. */
11972
f7f9143b
JB
11973 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
11974 {
11975 re_comp (known_runtime_file_name_patterns[i]);
f69c91ad 11976 if (re_exec (lbasename (sal.symtab->filename)))
dda83cd7 11977 return 1;
eb822aa6 11978 if (SYMTAB_OBJFILE (sal.symtab) != NULL
dda83cd7
SM
11979 && re_exec (objfile_name (SYMTAB_OBJFILE (sal.symtab))))
11980 return 1;
f7f9143b
JB
11981 }
11982
4ed6b5be 11983 /* Check whether the function is a GNAT-generated entity. */
f7f9143b 11984
c6dc63a1
TT
11985 gdb::unique_xmalloc_ptr<char> func_name
11986 = find_frame_funname (frame, &func_lang, NULL);
f7f9143b
JB
11987 if (func_name == NULL)
11988 return 1;
11989
11990 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
11991 {
11992 re_comp (known_auxiliary_function_name_patterns[i]);
c6dc63a1
TT
11993 if (re_exec (func_name.get ()))
11994 return 1;
f7f9143b
JB
11995 }
11996
11997 return 0;
11998}
11999
12000/* Find the first frame that contains debugging information and that is not
12001 part of the Ada run-time, starting from FI and moving upward. */
12002
0ef643c8 12003void
f7f9143b
JB
12004ada_find_printable_frame (struct frame_info *fi)
12005{
12006 for (; fi != NULL; fi = get_prev_frame (fi))
12007 {
12008 if (!is_known_support_routine (fi))
dda83cd7
SM
12009 {
12010 select_frame (fi);
12011 break;
12012 }
f7f9143b
JB
12013 }
12014
12015}
12016
12017/* Assuming that the inferior just triggered an unhandled exception
12018 catchpoint, return the address in inferior memory where the name
12019 of the exception is stored.
12020
12021 Return zero if the address could not be computed. */
12022
12023static CORE_ADDR
12024ada_unhandled_exception_name_addr (void)
0259addd
JB
12025{
12026 return parse_and_eval_address ("e.full_name");
12027}
12028
12029/* Same as ada_unhandled_exception_name_addr, except that this function
12030 should be used when the inferior uses an older version of the runtime,
12031 where the exception name needs to be extracted from a specific frame
12032 several frames up in the callstack. */
12033
12034static CORE_ADDR
12035ada_unhandled_exception_name_addr_from_raise (void)
f7f9143b
JB
12036{
12037 int frame_level;
12038 struct frame_info *fi;
3eecfa55 12039 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
f7f9143b
JB
12040
12041 /* To determine the name of this exception, we need to select
12042 the frame corresponding to RAISE_SYM_NAME. This frame is
12043 at least 3 levels up, so we simply skip the first 3 frames
12044 without checking the name of their associated function. */
12045 fi = get_current_frame ();
12046 for (frame_level = 0; frame_level < 3; frame_level += 1)
12047 if (fi != NULL)
12048 fi = get_prev_frame (fi);
12049
12050 while (fi != NULL)
12051 {
692465f1
JB
12052 enum language func_lang;
12053
c6dc63a1
TT
12054 gdb::unique_xmalloc_ptr<char> func_name
12055 = find_frame_funname (fi, &func_lang, NULL);
55b87a52
KS
12056 if (func_name != NULL)
12057 {
dda83cd7 12058 if (strcmp (func_name.get (),
55b87a52
KS
12059 data->exception_info->catch_exception_sym) == 0)
12060 break; /* We found the frame we were looking for... */
55b87a52 12061 }
fb44b1a7 12062 fi = get_prev_frame (fi);
f7f9143b
JB
12063 }
12064
12065 if (fi == NULL)
12066 return 0;
12067
12068 select_frame (fi);
12069 return parse_and_eval_address ("id.full_name");
12070}
12071
12072/* Assuming the inferior just triggered an Ada exception catchpoint
12073 (of any type), return the address in inferior memory where the name
12074 of the exception is stored, if applicable.
12075
45db7c09
PA
12076 Assumes the selected frame is the current frame.
12077
f7f9143b
JB
12078 Return zero if the address could not be computed, or if not relevant. */
12079
12080static CORE_ADDR
761269c8 12081ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex,
dda83cd7 12082 struct breakpoint *b)
f7f9143b 12083{
3eecfa55
JB
12084 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12085
f7f9143b
JB
12086 switch (ex)
12087 {
761269c8 12088 case ada_catch_exception:
dda83cd7
SM
12089 return (parse_and_eval_address ("e.full_name"));
12090 break;
f7f9143b 12091
761269c8 12092 case ada_catch_exception_unhandled:
dda83cd7
SM
12093 return data->exception_info->unhandled_exception_name_addr ();
12094 break;
9f757bf7
XR
12095
12096 case ada_catch_handlers:
dda83cd7 12097 return 0; /* The runtimes does not provide access to the exception
9f757bf7 12098 name. */
dda83cd7 12099 break;
9f757bf7 12100
761269c8 12101 case ada_catch_assert:
dda83cd7
SM
12102 return 0; /* Exception name is not relevant in this case. */
12103 break;
f7f9143b
JB
12104
12105 default:
dda83cd7
SM
12106 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12107 break;
f7f9143b
JB
12108 }
12109
12110 return 0; /* Should never be reached. */
12111}
12112
e547c119
JB
12113/* Assuming the inferior is stopped at an exception catchpoint,
12114 return the message which was associated to the exception, if
12115 available. Return NULL if the message could not be retrieved.
12116
e547c119
JB
12117 Note: The exception message can be associated to an exception
12118 either through the use of the Raise_Exception function, or
12119 more simply (Ada 2005 and later), via:
12120
12121 raise Exception_Name with "exception message";
12122
12123 */
12124
6f46ac85 12125static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12126ada_exception_message_1 (void)
12127{
12128 struct value *e_msg_val;
e547c119 12129 int e_msg_len;
e547c119
JB
12130
12131 /* For runtimes that support this feature, the exception message
12132 is passed as an unbounded string argument called "message". */
12133 e_msg_val = parse_and_eval ("message");
12134 if (e_msg_val == NULL)
12135 return NULL; /* Exception message not supported. */
12136
12137 e_msg_val = ada_coerce_to_simple_array (e_msg_val);
12138 gdb_assert (e_msg_val != NULL);
12139 e_msg_len = TYPE_LENGTH (value_type (e_msg_val));
12140
12141 /* If the message string is empty, then treat it as if there was
12142 no exception message. */
12143 if (e_msg_len <= 0)
12144 return NULL;
12145
15f3b077
TT
12146 gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
12147 read_memory (value_address (e_msg_val), (gdb_byte *) e_msg.get (),
12148 e_msg_len);
12149 e_msg.get ()[e_msg_len] = '\0';
12150
12151 return e_msg;
e547c119
JB
12152}
12153
12154/* Same as ada_exception_message_1, except that all exceptions are
12155 contained here (returning NULL instead). */
12156
6f46ac85 12157static gdb::unique_xmalloc_ptr<char>
e547c119
JB
12158ada_exception_message (void)
12159{
6f46ac85 12160 gdb::unique_xmalloc_ptr<char> e_msg;
e547c119 12161
a70b8144 12162 try
e547c119
JB
12163 {
12164 e_msg = ada_exception_message_1 ();
12165 }
230d2906 12166 catch (const gdb_exception_error &e)
e547c119 12167 {
6f46ac85 12168 e_msg.reset (nullptr);
e547c119 12169 }
e547c119
JB
12170
12171 return e_msg;
12172}
12173
f7f9143b
JB
12174/* Same as ada_exception_name_addr_1, except that it intercepts and contains
12175 any error that ada_exception_name_addr_1 might cause to be thrown.
12176 When an error is intercepted, a warning with the error message is printed,
12177 and zero is returned. */
12178
12179static CORE_ADDR
761269c8 12180ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
dda83cd7 12181 struct breakpoint *b)
f7f9143b 12182{
f7f9143b
JB
12183 CORE_ADDR result = 0;
12184
a70b8144 12185 try
f7f9143b
JB
12186 {
12187 result = ada_exception_name_addr_1 (ex, b);
12188 }
12189
230d2906 12190 catch (const gdb_exception_error &e)
f7f9143b 12191 {
3d6e9d23 12192 warning (_("failed to get exception name: %s"), e.what ());
f7f9143b
JB
12193 return 0;
12194 }
12195
12196 return result;
12197}
12198
cb7de75e 12199static std::string ada_exception_catchpoint_cond_string
9f757bf7
XR
12200 (const char *excep_string,
12201 enum ada_exception_catchpoint_kind ex);
28010a5d
PA
12202
12203/* Ada catchpoints.
12204
12205 In the case of catchpoints on Ada exceptions, the catchpoint will
12206 stop the target on every exception the program throws. When a user
12207 specifies the name of a specific exception, we translate this
12208 request into a condition expression (in text form), and then parse
12209 it into an expression stored in each of the catchpoint's locations.
12210 We then use this condition to check whether the exception that was
12211 raised is the one the user is interested in. If not, then the
12212 target is resumed again. We store the name of the requested
12213 exception, in order to be able to re-set the condition expression
12214 when symbols change. */
12215
12216/* An instance of this type is used to represent an Ada catchpoint
5625a286 12217 breakpoint location. */
28010a5d 12218
5625a286 12219class ada_catchpoint_location : public bp_location
28010a5d 12220{
5625a286 12221public:
5f486660 12222 ada_catchpoint_location (breakpoint *owner)
f06f1252 12223 : bp_location (owner, bp_loc_software_breakpoint)
5625a286 12224 {}
28010a5d
PA
12225
12226 /* The condition that checks whether the exception that was raised
12227 is the specific exception the user specified on catchpoint
12228 creation. */
4d01a485 12229 expression_up excep_cond_expr;
28010a5d
PA
12230};
12231
c1fc2657 12232/* An instance of this type is used to represent an Ada catchpoint. */
28010a5d 12233
c1fc2657 12234struct ada_catchpoint : public breakpoint
28010a5d 12235{
37f6a7f4
TT
12236 explicit ada_catchpoint (enum ada_exception_catchpoint_kind kind)
12237 : m_kind (kind)
12238 {
12239 }
12240
28010a5d 12241 /* The name of the specific exception the user specified. */
bc18fbb5 12242 std::string excep_string;
37f6a7f4
TT
12243
12244 /* What kind of catchpoint this is. */
12245 enum ada_exception_catchpoint_kind m_kind;
28010a5d
PA
12246};
12247
12248/* Parse the exception condition string in the context of each of the
12249 catchpoint's locations, and store them for later evaluation. */
12250
12251static void
9f757bf7 12252create_excep_cond_exprs (struct ada_catchpoint *c,
dda83cd7 12253 enum ada_exception_catchpoint_kind ex)
28010a5d 12254{
fccf9de1
TT
12255 struct bp_location *bl;
12256
28010a5d 12257 /* Nothing to do if there's no specific exception to catch. */
bc18fbb5 12258 if (c->excep_string.empty ())
28010a5d
PA
12259 return;
12260
12261 /* Same if there are no locations... */
c1fc2657 12262 if (c->loc == NULL)
28010a5d
PA
12263 return;
12264
fccf9de1
TT
12265 /* Compute the condition expression in text form, from the specific
12266 expection we want to catch. */
12267 std::string cond_string
12268 = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
28010a5d 12269
fccf9de1
TT
12270 /* Iterate over all the catchpoint's locations, and parse an
12271 expression for each. */
12272 for (bl = c->loc; bl != NULL; bl = bl->next)
28010a5d
PA
12273 {
12274 struct ada_catchpoint_location *ada_loc
fccf9de1 12275 = (struct ada_catchpoint_location *) bl;
4d01a485 12276 expression_up exp;
28010a5d 12277
fccf9de1 12278 if (!bl->shlib_disabled)
28010a5d 12279 {
bbc13ae3 12280 const char *s;
28010a5d 12281
cb7de75e 12282 s = cond_string.c_str ();
a70b8144 12283 try
28010a5d 12284 {
fccf9de1
TT
12285 exp = parse_exp_1 (&s, bl->address,
12286 block_for_pc (bl->address),
036e657b 12287 0);
28010a5d 12288 }
230d2906 12289 catch (const gdb_exception_error &e)
849f2b52
JB
12290 {
12291 warning (_("failed to reevaluate internal exception condition "
12292 "for catchpoint %d: %s"),
3d6e9d23 12293 c->number, e.what ());
849f2b52 12294 }
28010a5d
PA
12295 }
12296
b22e99fd 12297 ada_loc->excep_cond_expr = std::move (exp);
28010a5d 12298 }
28010a5d
PA
12299}
12300
28010a5d
PA
12301/* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
12302 structure for all exception catchpoint kinds. */
12303
12304static struct bp_location *
37f6a7f4 12305allocate_location_exception (struct breakpoint *self)
28010a5d 12306{
5f486660 12307 return new ada_catchpoint_location (self);
28010a5d
PA
12308}
12309
12310/* Implement the RE_SET method in the breakpoint_ops structure for all
12311 exception catchpoint kinds. */
12312
12313static void
37f6a7f4 12314re_set_exception (struct breakpoint *b)
28010a5d
PA
12315{
12316 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12317
12318 /* Call the base class's method. This updates the catchpoint's
12319 locations. */
2060206e 12320 bkpt_breakpoint_ops.re_set (b);
28010a5d
PA
12321
12322 /* Reparse the exception conditional expressions. One for each
12323 location. */
37f6a7f4 12324 create_excep_cond_exprs (c, c->m_kind);
28010a5d
PA
12325}
12326
12327/* Returns true if we should stop for this breakpoint hit. If the
12328 user specified a specific exception, we only want to cause a stop
12329 if the program thrown that exception. */
12330
12331static int
12332should_stop_exception (const struct bp_location *bl)
12333{
12334 struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
12335 const struct ada_catchpoint_location *ada_loc
12336 = (const struct ada_catchpoint_location *) bl;
28010a5d
PA
12337 int stop;
12338
37f6a7f4
TT
12339 struct internalvar *var = lookup_internalvar ("_ada_exception");
12340 if (c->m_kind == ada_catch_assert)
12341 clear_internalvar (var);
12342 else
12343 {
12344 try
12345 {
12346 const char *expr;
12347
12348 if (c->m_kind == ada_catch_handlers)
12349 expr = ("GNAT_GCC_exception_Access(gcc_exception)"
12350 ".all.occurrence.id");
12351 else
12352 expr = "e";
12353
12354 struct value *exc = parse_and_eval (expr);
12355 set_internalvar (var, exc);
12356 }
12357 catch (const gdb_exception_error &ex)
12358 {
12359 clear_internalvar (var);
12360 }
12361 }
12362
28010a5d 12363 /* With no specific exception, should always stop. */
bc18fbb5 12364 if (c->excep_string.empty ())
28010a5d
PA
12365 return 1;
12366
12367 if (ada_loc->excep_cond_expr == NULL)
12368 {
12369 /* We will have a NULL expression if back when we were creating
12370 the expressions, this location's had failed to parse. */
12371 return 1;
12372 }
12373
12374 stop = 1;
a70b8144 12375 try
28010a5d
PA
12376 {
12377 struct value *mark;
12378
12379 mark = value_mark ();
4d01a485 12380 stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
28010a5d
PA
12381 value_free_to_mark (mark);
12382 }
230d2906 12383 catch (const gdb_exception &ex)
492d29ea
PA
12384 {
12385 exception_fprintf (gdb_stderr, ex,
12386 _("Error in testing exception condition:\n"));
12387 }
492d29ea 12388
28010a5d
PA
12389 return stop;
12390}
12391
12392/* Implement the CHECK_STATUS method in the breakpoint_ops structure
12393 for all exception catchpoint kinds. */
12394
12395static void
37f6a7f4 12396check_status_exception (bpstat bs)
28010a5d 12397{
b6433ede 12398 bs->stop = should_stop_exception (bs->bp_location_at.get ());
28010a5d
PA
12399}
12400
f7f9143b
JB
12401/* Implement the PRINT_IT method in the breakpoint_ops structure
12402 for all exception catchpoint kinds. */
12403
12404static enum print_stop_action
37f6a7f4 12405print_it_exception (bpstat bs)
f7f9143b 12406{
79a45e25 12407 struct ui_out *uiout = current_uiout;
348d480f
PA
12408 struct breakpoint *b = bs->breakpoint_at;
12409
956a9fb9 12410 annotate_catchpoint (b->number);
f7f9143b 12411
112e8700 12412 if (uiout->is_mi_like_p ())
f7f9143b 12413 {
112e8700 12414 uiout->field_string ("reason",
956a9fb9 12415 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12416 uiout->field_string ("disp", bpdisp_text (b->disposition));
f7f9143b
JB
12417 }
12418
112e8700
SM
12419 uiout->text (b->disposition == disp_del
12420 ? "\nTemporary catchpoint " : "\nCatchpoint ");
381befee 12421 uiout->field_signed ("bkptno", b->number);
112e8700 12422 uiout->text (", ");
f7f9143b 12423
45db7c09
PA
12424 /* ada_exception_name_addr relies on the selected frame being the
12425 current frame. Need to do this here because this function may be
12426 called more than once when printing a stop, and below, we'll
12427 select the first frame past the Ada run-time (see
12428 ada_find_printable_frame). */
12429 select_frame (get_current_frame ());
12430
37f6a7f4
TT
12431 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12432 switch (c->m_kind)
f7f9143b 12433 {
761269c8
JB
12434 case ada_catch_exception:
12435 case ada_catch_exception_unhandled:
9f757bf7 12436 case ada_catch_handlers:
956a9fb9 12437 {
37f6a7f4 12438 const CORE_ADDR addr = ada_exception_name_addr (c->m_kind, b);
956a9fb9
JB
12439 char exception_name[256];
12440
12441 if (addr != 0)
12442 {
c714b426
PA
12443 read_memory (addr, (gdb_byte *) exception_name,
12444 sizeof (exception_name) - 1);
956a9fb9
JB
12445 exception_name [sizeof (exception_name) - 1] = '\0';
12446 }
12447 else
12448 {
12449 /* For some reason, we were unable to read the exception
12450 name. This could happen if the Runtime was compiled
12451 without debugging info, for instance. In that case,
12452 just replace the exception name by the generic string
12453 "exception" - it will read as "an exception" in the
12454 notification we are about to print. */
967cff16 12455 memcpy (exception_name, "exception", sizeof ("exception"));
956a9fb9
JB
12456 }
12457 /* In the case of unhandled exception breakpoints, we print
12458 the exception name as "unhandled EXCEPTION_NAME", to make
12459 it clearer to the user which kind of catchpoint just got
12460 hit. We used ui_out_text to make sure that this extra
12461 info does not pollute the exception name in the MI case. */
37f6a7f4 12462 if (c->m_kind == ada_catch_exception_unhandled)
112e8700
SM
12463 uiout->text ("unhandled ");
12464 uiout->field_string ("exception-name", exception_name);
956a9fb9
JB
12465 }
12466 break;
761269c8 12467 case ada_catch_assert:
956a9fb9
JB
12468 /* In this case, the name of the exception is not really
12469 important. Just print "failed assertion" to make it clearer
12470 that his program just hit an assertion-failure catchpoint.
12471 We used ui_out_text because this info does not belong in
12472 the MI output. */
112e8700 12473 uiout->text ("failed assertion");
956a9fb9 12474 break;
f7f9143b 12475 }
e547c119 12476
6f46ac85 12477 gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
e547c119
JB
12478 if (exception_message != NULL)
12479 {
e547c119 12480 uiout->text (" (");
6f46ac85 12481 uiout->field_string ("exception-message", exception_message.get ());
e547c119 12482 uiout->text (")");
e547c119
JB
12483 }
12484
112e8700 12485 uiout->text (" at ");
956a9fb9 12486 ada_find_printable_frame (get_current_frame ());
f7f9143b
JB
12487
12488 return PRINT_SRC_AND_LOC;
12489}
12490
12491/* Implement the PRINT_ONE method in the breakpoint_ops structure
12492 for all exception catchpoint kinds. */
12493
12494static void
37f6a7f4 12495print_one_exception (struct breakpoint *b, struct bp_location **last_loc)
f7f9143b 12496{
79a45e25 12497 struct ui_out *uiout = current_uiout;
28010a5d 12498 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45b7d
TT
12499 struct value_print_options opts;
12500
12501 get_user_print_options (&opts);
f06f1252 12502
79a45b7d 12503 if (opts.addressprint)
f06f1252 12504 uiout->field_skip ("addr");
f7f9143b
JB
12505
12506 annotate_field (5);
37f6a7f4 12507 switch (c->m_kind)
f7f9143b 12508 {
761269c8 12509 case ada_catch_exception:
dda83cd7
SM
12510 if (!c->excep_string.empty ())
12511 {
bc18fbb5
TT
12512 std::string msg = string_printf (_("`%s' Ada exception"),
12513 c->excep_string.c_str ());
28010a5d 12514
dda83cd7
SM
12515 uiout->field_string ("what", msg);
12516 }
12517 else
12518 uiout->field_string ("what", "all Ada exceptions");
12519
12520 break;
f7f9143b 12521
761269c8 12522 case ada_catch_exception_unhandled:
dda83cd7
SM
12523 uiout->field_string ("what", "unhandled Ada exceptions");
12524 break;
f7f9143b 12525
9f757bf7 12526 case ada_catch_handlers:
dda83cd7
SM
12527 if (!c->excep_string.empty ())
12528 {
9f757bf7
XR
12529 uiout->field_fmt ("what",
12530 _("`%s' Ada exception handlers"),
bc18fbb5 12531 c->excep_string.c_str ());
dda83cd7
SM
12532 }
12533 else
9f757bf7 12534 uiout->field_string ("what", "all Ada exceptions handlers");
dda83cd7 12535 break;
9f757bf7 12536
761269c8 12537 case ada_catch_assert:
dda83cd7
SM
12538 uiout->field_string ("what", "failed Ada assertions");
12539 break;
f7f9143b
JB
12540
12541 default:
dda83cd7
SM
12542 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12543 break;
f7f9143b
JB
12544 }
12545}
12546
12547/* Implement the PRINT_MENTION method in the breakpoint_ops structure
12548 for all exception catchpoint kinds. */
12549
12550static void
37f6a7f4 12551print_mention_exception (struct breakpoint *b)
f7f9143b 12552{
28010a5d 12553 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
79a45e25 12554 struct ui_out *uiout = current_uiout;
28010a5d 12555
112e8700 12556 uiout->text (b->disposition == disp_del ? _("Temporary catchpoint ")
dda83cd7 12557 : _("Catchpoint "));
381befee 12558 uiout->field_signed ("bkptno", b->number);
112e8700 12559 uiout->text (": ");
00eb2c4a 12560
37f6a7f4 12561 switch (c->m_kind)
f7f9143b 12562 {
761269c8 12563 case ada_catch_exception:
dda83cd7 12564 if (!c->excep_string.empty ())
00eb2c4a 12565 {
862d101a 12566 std::string info = string_printf (_("`%s' Ada exception"),
bc18fbb5 12567 c->excep_string.c_str ());
862d101a 12568 uiout->text (info.c_str ());
00eb2c4a 12569 }
dda83cd7
SM
12570 else
12571 uiout->text (_("all Ada exceptions"));
12572 break;
f7f9143b 12573
761269c8 12574 case ada_catch_exception_unhandled:
dda83cd7
SM
12575 uiout->text (_("unhandled Ada exceptions"));
12576 break;
9f757bf7
XR
12577
12578 case ada_catch_handlers:
dda83cd7 12579 if (!c->excep_string.empty ())
9f757bf7
XR
12580 {
12581 std::string info
12582 = string_printf (_("`%s' Ada exception handlers"),
bc18fbb5 12583 c->excep_string.c_str ());
9f757bf7
XR
12584 uiout->text (info.c_str ());
12585 }
dda83cd7
SM
12586 else
12587 uiout->text (_("all Ada exceptions handlers"));
12588 break;
9f757bf7 12589
761269c8 12590 case ada_catch_assert:
dda83cd7
SM
12591 uiout->text (_("failed Ada assertions"));
12592 break;
f7f9143b
JB
12593
12594 default:
dda83cd7
SM
12595 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12596 break;
f7f9143b
JB
12597 }
12598}
12599
6149aea9
PA
12600/* Implement the PRINT_RECREATE method in the breakpoint_ops structure
12601 for all exception catchpoint kinds. */
12602
12603static void
37f6a7f4 12604print_recreate_exception (struct breakpoint *b, struct ui_file *fp)
6149aea9 12605{
28010a5d
PA
12606 struct ada_catchpoint *c = (struct ada_catchpoint *) b;
12607
37f6a7f4 12608 switch (c->m_kind)
6149aea9 12609 {
761269c8 12610 case ada_catch_exception:
6149aea9 12611 fprintf_filtered (fp, "catch exception");
bc18fbb5
TT
12612 if (!c->excep_string.empty ())
12613 fprintf_filtered (fp, " %s", c->excep_string.c_str ());
6149aea9
PA
12614 break;
12615
761269c8 12616 case ada_catch_exception_unhandled:
78076abc 12617 fprintf_filtered (fp, "catch exception unhandled");
6149aea9
PA
12618 break;
12619
9f757bf7
XR
12620 case ada_catch_handlers:
12621 fprintf_filtered (fp, "catch handlers");
12622 break;
12623
761269c8 12624 case ada_catch_assert:
6149aea9
PA
12625 fprintf_filtered (fp, "catch assert");
12626 break;
12627
12628 default:
12629 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
12630 }
d9b3f62e 12631 print_recreate_thread (b, fp);
6149aea9
PA
12632}
12633
37f6a7f4 12634/* Virtual tables for various breakpoint types. */
2060206e 12635static struct breakpoint_ops catch_exception_breakpoint_ops;
2060206e 12636static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops;
2060206e 12637static struct breakpoint_ops catch_assert_breakpoint_ops;
9f757bf7
XR
12638static struct breakpoint_ops catch_handlers_breakpoint_ops;
12639
f06f1252
TT
12640/* See ada-lang.h. */
12641
12642bool
12643is_ada_exception_catchpoint (breakpoint *bp)
12644{
12645 return (bp->ops == &catch_exception_breakpoint_ops
12646 || bp->ops == &catch_exception_unhandled_breakpoint_ops
12647 || bp->ops == &catch_assert_breakpoint_ops
12648 || bp->ops == &catch_handlers_breakpoint_ops);
12649}
12650
f7f9143b
JB
12651/* Split the arguments specified in a "catch exception" command.
12652 Set EX to the appropriate catchpoint type.
28010a5d 12653 Set EXCEP_STRING to the name of the specific exception if
5845583d 12654 specified by the user.
9f757bf7
XR
12655 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12656 "catch handlers" command. False otherwise.
5845583d
JB
12657 If a condition is found at the end of the arguments, the condition
12658 expression is stored in COND_STRING (memory must be deallocated
12659 after use). Otherwise COND_STRING is set to NULL. */
f7f9143b
JB
12660
12661static void
a121b7c1 12662catch_ada_exception_command_split (const char *args,
9f757bf7 12663 bool is_catch_handlers_cmd,
dda83cd7 12664 enum ada_exception_catchpoint_kind *ex,
bc18fbb5
TT
12665 std::string *excep_string,
12666 std::string *cond_string)
f7f9143b 12667{
bc18fbb5 12668 std::string exception_name;
f7f9143b 12669
bc18fbb5
TT
12670 exception_name = extract_arg (&args);
12671 if (exception_name == "if")
5845583d
JB
12672 {
12673 /* This is not an exception name; this is the start of a condition
12674 expression for a catchpoint on all exceptions. So, "un-get"
12675 this token, and set exception_name to NULL. */
bc18fbb5 12676 exception_name.clear ();
5845583d
JB
12677 args -= 2;
12678 }
f7f9143b 12679
5845583d 12680 /* Check to see if we have a condition. */
f7f9143b 12681
f1735a53 12682 args = skip_spaces (args);
61012eef 12683 if (startswith (args, "if")
5845583d
JB
12684 && (isspace (args[2]) || args[2] == '\0'))
12685 {
12686 args += 2;
f1735a53 12687 args = skip_spaces (args);
5845583d
JB
12688
12689 if (args[0] == '\0')
dda83cd7 12690 error (_("Condition missing after `if' keyword"));
bc18fbb5 12691 *cond_string = args;
5845583d
JB
12692
12693 args += strlen (args);
12694 }
12695
12696 /* Check that we do not have any more arguments. Anything else
12697 is unexpected. */
f7f9143b
JB
12698
12699 if (args[0] != '\0')
12700 error (_("Junk at end of expression"));
12701
9f757bf7
XR
12702 if (is_catch_handlers_cmd)
12703 {
12704 /* Catch handling of exceptions. */
12705 *ex = ada_catch_handlers;
12706 *excep_string = exception_name;
12707 }
bc18fbb5 12708 else if (exception_name.empty ())
f7f9143b
JB
12709 {
12710 /* Catch all exceptions. */
761269c8 12711 *ex = ada_catch_exception;
bc18fbb5 12712 excep_string->clear ();
f7f9143b 12713 }
bc18fbb5 12714 else if (exception_name == "unhandled")
f7f9143b
JB
12715 {
12716 /* Catch unhandled exceptions. */
761269c8 12717 *ex = ada_catch_exception_unhandled;
bc18fbb5 12718 excep_string->clear ();
f7f9143b
JB
12719 }
12720 else
12721 {
12722 /* Catch a specific exception. */
761269c8 12723 *ex = ada_catch_exception;
28010a5d 12724 *excep_string = exception_name;
f7f9143b
JB
12725 }
12726}
12727
12728/* Return the name of the symbol on which we should break in order to
12729 implement a catchpoint of the EX kind. */
12730
12731static const char *
761269c8 12732ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
f7f9143b 12733{
3eecfa55
JB
12734 struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
12735
12736 gdb_assert (data->exception_info != NULL);
0259addd 12737
f7f9143b
JB
12738 switch (ex)
12739 {
761269c8 12740 case ada_catch_exception:
dda83cd7
SM
12741 return (data->exception_info->catch_exception_sym);
12742 break;
761269c8 12743 case ada_catch_exception_unhandled:
dda83cd7
SM
12744 return (data->exception_info->catch_exception_unhandled_sym);
12745 break;
761269c8 12746 case ada_catch_assert:
dda83cd7
SM
12747 return (data->exception_info->catch_assert_sym);
12748 break;
9f757bf7 12749 case ada_catch_handlers:
dda83cd7
SM
12750 return (data->exception_info->catch_handlers_sym);
12751 break;
f7f9143b 12752 default:
dda83cd7
SM
12753 internal_error (__FILE__, __LINE__,
12754 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12755 }
12756}
12757
12758/* Return the breakpoint ops "virtual table" used for catchpoints
12759 of the EX kind. */
12760
c0a91b2b 12761static const struct breakpoint_ops *
761269c8 12762ada_exception_breakpoint_ops (enum ada_exception_catchpoint_kind ex)
f7f9143b
JB
12763{
12764 switch (ex)
12765 {
761269c8 12766 case ada_catch_exception:
dda83cd7
SM
12767 return (&catch_exception_breakpoint_ops);
12768 break;
761269c8 12769 case ada_catch_exception_unhandled:
dda83cd7
SM
12770 return (&catch_exception_unhandled_breakpoint_ops);
12771 break;
761269c8 12772 case ada_catch_assert:
dda83cd7
SM
12773 return (&catch_assert_breakpoint_ops);
12774 break;
9f757bf7 12775 case ada_catch_handlers:
dda83cd7
SM
12776 return (&catch_handlers_breakpoint_ops);
12777 break;
f7f9143b 12778 default:
dda83cd7
SM
12779 internal_error (__FILE__, __LINE__,
12780 _("unexpected catchpoint kind (%d)"), ex);
f7f9143b
JB
12781 }
12782}
12783
12784/* Return the condition that will be used to match the current exception
12785 being raised with the exception that the user wants to catch. This
12786 assumes that this condition is used when the inferior just triggered
12787 an exception catchpoint.
cb7de75e 12788 EX: the type of catchpoints used for catching Ada exceptions. */
f7f9143b 12789
cb7de75e 12790static std::string
9f757bf7 12791ada_exception_catchpoint_cond_string (const char *excep_string,
dda83cd7 12792 enum ada_exception_catchpoint_kind ex)
f7f9143b 12793{
3d0b0fa3 12794 int i;
fccf9de1 12795 bool is_standard_exc = false;
cb7de75e 12796 std::string result;
9f757bf7
XR
12797
12798 if (ex == ada_catch_handlers)
12799 {
12800 /* For exception handlers catchpoints, the condition string does
dda83cd7 12801 not use the same parameter as for the other exceptions. */
fccf9de1
TT
12802 result = ("long_integer (GNAT_GCC_exception_Access"
12803 "(gcc_exception).all.occurrence.id)");
9f757bf7
XR
12804 }
12805 else
fccf9de1 12806 result = "long_integer (e)";
3d0b0fa3 12807
0963b4bd 12808 /* The standard exceptions are a special case. They are defined in
3d0b0fa3 12809 runtime units that have been compiled without debugging info; if
28010a5d 12810 EXCEP_STRING is the not-fully-qualified name of a standard
3d0b0fa3
JB
12811 exception (e.g. "constraint_error") then, during the evaluation
12812 of the condition expression, the symbol lookup on this name would
0963b4bd 12813 *not* return this standard exception. The catchpoint condition
3d0b0fa3
JB
12814 may then be set only on user-defined exceptions which have the
12815 same not-fully-qualified name (e.g. my_package.constraint_error).
12816
12817 To avoid this unexcepted behavior, these standard exceptions are
0963b4bd 12818 systematically prefixed by "standard". This means that "catch
3d0b0fa3
JB
12819 exception constraint_error" is rewritten into "catch exception
12820 standard.constraint_error".
12821
85102364 12822 If an exception named constraint_error is defined in another package of
3d0b0fa3
JB
12823 the inferior program, then the only way to specify this exception as a
12824 breakpoint condition is to use its fully-qualified named:
fccf9de1 12825 e.g. my_package.constraint_error. */
3d0b0fa3
JB
12826
12827 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
12828 {
28010a5d 12829 if (strcmp (standard_exc [i], excep_string) == 0)
3d0b0fa3 12830 {
fccf9de1 12831 is_standard_exc = true;
9f757bf7 12832 break;
3d0b0fa3
JB
12833 }
12834 }
9f757bf7 12835
fccf9de1
TT
12836 result += " = ";
12837
12838 if (is_standard_exc)
12839 string_appendf (result, "long_integer (&standard.%s)", excep_string);
12840 else
12841 string_appendf (result, "long_integer (&%s)", excep_string);
9f757bf7 12842
9f757bf7 12843 return result;
f7f9143b
JB
12844}
12845
12846/* Return the symtab_and_line that should be used to insert an exception
12847 catchpoint of the TYPE kind.
12848
28010a5d
PA
12849 ADDR_STRING returns the name of the function where the real
12850 breakpoint that implements the catchpoints is set, depending on the
12851 type of catchpoint we need to create. */
f7f9143b
JB
12852
12853static struct symtab_and_line
bc18fbb5 12854ada_exception_sal (enum ada_exception_catchpoint_kind ex,
cc12f4a8 12855 std::string *addr_string, const struct breakpoint_ops **ops)
f7f9143b
JB
12856{
12857 const char *sym_name;
12858 struct symbol *sym;
f7f9143b 12859
0259addd
JB
12860 /* First, find out which exception support info to use. */
12861 ada_exception_support_info_sniffer ();
12862
12863 /* Then lookup the function on which we will break in order to catch
f7f9143b 12864 the Ada exceptions requested by the user. */
f7f9143b
JB
12865 sym_name = ada_exception_sym_name (ex);
12866 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
12867
57aff202
JB
12868 if (sym == NULL)
12869 error (_("Catchpoint symbol not found: %s"), sym_name);
12870
12871 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
12872 error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
f7f9143b
JB
12873
12874 /* Set ADDR_STRING. */
cc12f4a8 12875 *addr_string = sym_name;
f7f9143b 12876
f7f9143b 12877 /* Set OPS. */
4b9eee8c 12878 *ops = ada_exception_breakpoint_ops (ex);
f7f9143b 12879
f17011e0 12880 return find_function_start_sal (sym, 1);
f7f9143b
JB
12881}
12882
b4a5b78b 12883/* Create an Ada exception catchpoint.
f7f9143b 12884
b4a5b78b 12885 EX_KIND is the kind of exception catchpoint to be created.
5845583d 12886
bc18fbb5 12887 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
2df4d1d5 12888 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
bc18fbb5 12889 of the exception to which this catchpoint applies.
2df4d1d5 12890
bc18fbb5 12891 COND_STRING, if not empty, is the catchpoint condition.
f7f9143b 12892
b4a5b78b
JB
12893 TEMPFLAG, if nonzero, means that the underlying breakpoint
12894 should be temporary.
28010a5d 12895
b4a5b78b 12896 FROM_TTY is the usual argument passed to all commands implementations. */
28010a5d 12897
349774ef 12898void
28010a5d 12899create_ada_exception_catchpoint (struct gdbarch *gdbarch,
761269c8 12900 enum ada_exception_catchpoint_kind ex_kind,
bc18fbb5 12901 const std::string &excep_string,
56ecd069 12902 const std::string &cond_string,
28010a5d 12903 int tempflag,
349774ef 12904 int disabled,
28010a5d
PA
12905 int from_tty)
12906{
cc12f4a8 12907 std::string addr_string;
b4a5b78b 12908 const struct breakpoint_ops *ops = NULL;
bc18fbb5 12909 struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string, &ops);
28010a5d 12910
37f6a7f4 12911 std::unique_ptr<ada_catchpoint> c (new ada_catchpoint (ex_kind));
cc12f4a8 12912 init_ada_exception_breakpoint (c.get (), gdbarch, sal, addr_string.c_str (),
349774ef 12913 ops, tempflag, disabled, from_tty);
28010a5d 12914 c->excep_string = excep_string;
9f757bf7 12915 create_excep_cond_exprs (c.get (), ex_kind);
56ecd069 12916 if (!cond_string.empty ())
733d554a 12917 set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
b270e6f9 12918 install_breakpoint (0, std::move (c), 1);
f7f9143b
JB
12919}
12920
9ac4176b
PA
12921/* Implement the "catch exception" command. */
12922
12923static void
eb4c3f4a 12924catch_ada_exception_command (const char *arg_entry, int from_tty,
9ac4176b
PA
12925 struct cmd_list_element *command)
12926{
a121b7c1 12927 const char *arg = arg_entry;
9ac4176b
PA
12928 struct gdbarch *gdbarch = get_current_arch ();
12929 int tempflag;
761269c8 12930 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12931 std::string excep_string;
56ecd069 12932 std::string cond_string;
9ac4176b
PA
12933
12934 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12935
12936 if (!arg)
12937 arg = "";
9f757bf7 12938 catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
bc18fbb5 12939 &cond_string);
9f757bf7
XR
12940 create_ada_exception_catchpoint (gdbarch, ex_kind,
12941 excep_string, cond_string,
12942 tempflag, 1 /* enabled */,
12943 from_tty);
12944}
12945
12946/* Implement the "catch handlers" command. */
12947
12948static void
12949catch_ada_handlers_command (const char *arg_entry, int from_tty,
12950 struct cmd_list_element *command)
12951{
12952 const char *arg = arg_entry;
12953 struct gdbarch *gdbarch = get_current_arch ();
12954 int tempflag;
12955 enum ada_exception_catchpoint_kind ex_kind;
bc18fbb5 12956 std::string excep_string;
56ecd069 12957 std::string cond_string;
9f757bf7
XR
12958
12959 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12960
12961 if (!arg)
12962 arg = "";
12963 catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
bc18fbb5 12964 &cond_string);
b4a5b78b
JB
12965 create_ada_exception_catchpoint (gdbarch, ex_kind,
12966 excep_string, cond_string,
349774ef
JB
12967 tempflag, 1 /* enabled */,
12968 from_tty);
9ac4176b
PA
12969}
12970
71bed2db
TT
12971/* Completion function for the Ada "catch" commands. */
12972
12973static void
12974catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
12975 const char *text, const char *word)
12976{
12977 std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
12978
12979 for (const ada_exc_info &info : exceptions)
12980 {
12981 if (startswith (info.name, word))
b02f78f9 12982 tracker.add_completion (make_unique_xstrdup (info.name));
71bed2db
TT
12983 }
12984}
12985
b4a5b78b 12986/* Split the arguments specified in a "catch assert" command.
5845583d 12987
b4a5b78b
JB
12988 ARGS contains the command's arguments (or the empty string if
12989 no arguments were passed).
5845583d
JB
12990
12991 If ARGS contains a condition, set COND_STRING to that condition
b4a5b78b 12992 (the memory needs to be deallocated after use). */
5845583d 12993
b4a5b78b 12994static void
56ecd069 12995catch_ada_assert_command_split (const char *args, std::string &cond_string)
f7f9143b 12996{
f1735a53 12997 args = skip_spaces (args);
f7f9143b 12998
5845583d 12999 /* Check whether a condition was provided. */
61012eef 13000 if (startswith (args, "if")
5845583d 13001 && (isspace (args[2]) || args[2] == '\0'))
f7f9143b 13002 {
5845583d 13003 args += 2;
f1735a53 13004 args = skip_spaces (args);
5845583d 13005 if (args[0] == '\0')
dda83cd7 13006 error (_("condition missing after `if' keyword"));
56ecd069 13007 cond_string.assign (args);
f7f9143b
JB
13008 }
13009
5845583d
JB
13010 /* Otherwise, there should be no other argument at the end of
13011 the command. */
13012 else if (args[0] != '\0')
13013 error (_("Junk at end of arguments."));
f7f9143b
JB
13014}
13015
9ac4176b
PA
13016/* Implement the "catch assert" command. */
13017
13018static void
eb4c3f4a 13019catch_assert_command (const char *arg_entry, int from_tty,
9ac4176b
PA
13020 struct cmd_list_element *command)
13021{
a121b7c1 13022 const char *arg = arg_entry;
9ac4176b
PA
13023 struct gdbarch *gdbarch = get_current_arch ();
13024 int tempflag;
56ecd069 13025 std::string cond_string;
9ac4176b
PA
13026
13027 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
13028
13029 if (!arg)
13030 arg = "";
56ecd069 13031 catch_ada_assert_command_split (arg, cond_string);
761269c8 13032 create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
241db429 13033 "", cond_string,
349774ef
JB
13034 tempflag, 1 /* enabled */,
13035 from_tty);
9ac4176b 13036}
778865d3
JB
13037
13038/* Return non-zero if the symbol SYM is an Ada exception object. */
13039
13040static int
13041ada_is_exception_sym (struct symbol *sym)
13042{
7d93a1e0 13043 const char *type_name = SYMBOL_TYPE (sym)->name ();
778865d3
JB
13044
13045 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
dda83cd7
SM
13046 && SYMBOL_CLASS (sym) != LOC_BLOCK
13047 && SYMBOL_CLASS (sym) != LOC_CONST
13048 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
13049 && type_name != NULL && strcmp (type_name, "exception") == 0);
778865d3
JB
13050}
13051
13052/* Given a global symbol SYM, return non-zero iff SYM is a non-standard
13053 Ada exception object. This matches all exceptions except the ones
13054 defined by the Ada language. */
13055
13056static int
13057ada_is_non_standard_exception_sym (struct symbol *sym)
13058{
13059 int i;
13060
13061 if (!ada_is_exception_sym (sym))
13062 return 0;
13063
13064 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
987012b8 13065 if (strcmp (sym->linkage_name (), standard_exc[i]) == 0)
778865d3
JB
13066 return 0; /* A standard exception. */
13067
13068 /* Numeric_Error is also a standard exception, so exclude it.
13069 See the STANDARD_EXC description for more details as to why
13070 this exception is not listed in that array. */
987012b8 13071 if (strcmp (sym->linkage_name (), "numeric_error") == 0)
778865d3
JB
13072 return 0;
13073
13074 return 1;
13075}
13076
ab816a27 13077/* A helper function for std::sort, comparing two struct ada_exc_info
778865d3
JB
13078 objects.
13079
13080 The comparison is determined first by exception name, and then
13081 by exception address. */
13082
ab816a27 13083bool
cc536b21 13084ada_exc_info::operator< (const ada_exc_info &other) const
778865d3 13085{
778865d3
JB
13086 int result;
13087
ab816a27
TT
13088 result = strcmp (name, other.name);
13089 if (result < 0)
13090 return true;
13091 if (result == 0 && addr < other.addr)
13092 return true;
13093 return false;
13094}
778865d3 13095
ab816a27 13096bool
cc536b21 13097ada_exc_info::operator== (const ada_exc_info &other) const
ab816a27
TT
13098{
13099 return addr == other.addr && strcmp (name, other.name) == 0;
778865d3
JB
13100}
13101
13102/* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
13103 routine, but keeping the first SKIP elements untouched.
13104
13105 All duplicates are also removed. */
13106
13107static void
ab816a27 13108sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
778865d3
JB
13109 int skip)
13110{
ab816a27
TT
13111 std::sort (exceptions->begin () + skip, exceptions->end ());
13112 exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
13113 exceptions->end ());
778865d3
JB
13114}
13115
778865d3
JB
13116/* Add all exceptions defined by the Ada standard whose name match
13117 a regular expression.
13118
13119 If PREG is not NULL, then this regexp_t object is used to
13120 perform the symbol name matching. Otherwise, no name-based
13121 filtering is performed.
13122
13123 EXCEPTIONS is a vector of exceptions to which matching exceptions
13124 gets pushed. */
13125
13126static void
2d7cc5c7 13127ada_add_standard_exceptions (compiled_regex *preg,
ab816a27 13128 std::vector<ada_exc_info> *exceptions)
778865d3
JB
13129{
13130 int i;
13131
13132 for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
13133 {
13134 if (preg == NULL
2d7cc5c7 13135 || preg->exec (standard_exc[i], 0, NULL, 0) == 0)
778865d3
JB
13136 {
13137 struct bound_minimal_symbol msymbol
13138 = ada_lookup_simple_minsym (standard_exc[i]);
13139
13140 if (msymbol.minsym != NULL)
13141 {
13142 struct ada_exc_info info
77e371c0 13143 = {standard_exc[i], BMSYMBOL_VALUE_ADDRESS (msymbol)};
778865d3 13144
ab816a27 13145 exceptions->push_back (info);
778865d3
JB
13146 }
13147 }
13148 }
13149}
13150
13151/* Add all Ada exceptions defined locally and accessible from the given
13152 FRAME.
13153
13154 If PREG is not NULL, then this regexp_t object is used to
13155 perform the symbol name matching. Otherwise, no name-based
13156 filtering is performed.
13157
13158 EXCEPTIONS is a vector of exceptions to which matching exceptions
13159 gets pushed. */
13160
13161static void
2d7cc5c7
PA
13162ada_add_exceptions_from_frame (compiled_regex *preg,
13163 struct frame_info *frame,
ab816a27 13164 std::vector<ada_exc_info> *exceptions)
778865d3 13165{
3977b71f 13166 const struct block *block = get_frame_block (frame, 0);
778865d3
JB
13167
13168 while (block != 0)
13169 {
13170 struct block_iterator iter;
13171 struct symbol *sym;
13172
13173 ALL_BLOCK_SYMBOLS (block, iter, sym)
13174 {
13175 switch (SYMBOL_CLASS (sym))
13176 {
13177 case LOC_TYPEDEF:
13178 case LOC_BLOCK:
13179 case LOC_CONST:
13180 break;
13181 default:
13182 if (ada_is_exception_sym (sym))
13183 {
987012b8 13184 struct ada_exc_info info = {sym->print_name (),
778865d3
JB
13185 SYMBOL_VALUE_ADDRESS (sym)};
13186
ab816a27 13187 exceptions->push_back (info);
778865d3
JB
13188 }
13189 }
13190 }
13191 if (BLOCK_FUNCTION (block) != NULL)
13192 break;
13193 block = BLOCK_SUPERBLOCK (block);
13194 }
13195}
13196
14bc53a8
PA
13197/* Return true if NAME matches PREG or if PREG is NULL. */
13198
13199static bool
2d7cc5c7 13200name_matches_regex (const char *name, compiled_regex *preg)
14bc53a8
PA
13201{
13202 return (preg == NULL
f945dedf 13203 || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
14bc53a8
PA
13204}
13205
778865d3
JB
13206/* Add all exceptions defined globally whose name name match
13207 a regular expression, excluding standard exceptions.
13208
13209 The reason we exclude standard exceptions is that they need
13210 to be handled separately: Standard exceptions are defined inside
13211 a runtime unit which is normally not compiled with debugging info,
13212 and thus usually do not show up in our symbol search. However,
13213 if the unit was in fact built with debugging info, we need to
13214 exclude them because they would duplicate the entry we found
13215 during the special loop that specifically searches for those
13216 standard exceptions.
13217
13218 If PREG is not NULL, then this regexp_t object is used to
13219 perform the symbol name matching. Otherwise, no name-based
13220 filtering is performed.
13221
13222 EXCEPTIONS is a vector of exceptions to which matching exceptions
13223 gets pushed. */
13224
13225static void
2d7cc5c7 13226ada_add_global_exceptions (compiled_regex *preg,
ab816a27 13227 std::vector<ada_exc_info> *exceptions)
778865d3 13228{
14bc53a8
PA
13229 /* In Ada, the symbol "search name" is a linkage name, whereas the
13230 regular expression used to do the matching refers to the natural
13231 name. So match against the decoded name. */
13232 expand_symtabs_matching (NULL,
b5ec771e 13233 lookup_name_info::match_any (),
14bc53a8
PA
13234 [&] (const char *search_name)
13235 {
f945dedf
CB
13236 std::string decoded = ada_decode (search_name);
13237 return name_matches_regex (decoded.c_str (), preg);
14bc53a8
PA
13238 },
13239 NULL,
13240 VARIABLES_DOMAIN);
778865d3 13241
2030c079 13242 for (objfile *objfile : current_program_space->objfiles ())
778865d3 13243 {
b669c953 13244 for (compunit_symtab *s : objfile->compunits ())
778865d3 13245 {
d8aeb77f
TT
13246 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (s);
13247 int i;
778865d3 13248
d8aeb77f
TT
13249 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
13250 {
582942f4 13251 const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
d8aeb77f
TT
13252 struct block_iterator iter;
13253 struct symbol *sym;
778865d3 13254
d8aeb77f
TT
13255 ALL_BLOCK_SYMBOLS (b, iter, sym)
13256 if (ada_is_non_standard_exception_sym (sym)
987012b8 13257 && name_matches_regex (sym->natural_name (), preg))
d8aeb77f
TT
13258 {
13259 struct ada_exc_info info
987012b8 13260 = {sym->print_name (), SYMBOL_VALUE_ADDRESS (sym)};
d8aeb77f
TT
13261
13262 exceptions->push_back (info);
13263 }
13264 }
778865d3
JB
13265 }
13266 }
13267}
13268
13269/* Implements ada_exceptions_list with the regular expression passed
13270 as a regex_t, rather than a string.
13271
13272 If not NULL, PREG is used to filter out exceptions whose names
13273 do not match. Otherwise, all exceptions are listed. */
13274
ab816a27 13275static std::vector<ada_exc_info>
2d7cc5c7 13276ada_exceptions_list_1 (compiled_regex *preg)
778865d3 13277{
ab816a27 13278 std::vector<ada_exc_info> result;
778865d3
JB
13279 int prev_len;
13280
13281 /* First, list the known standard exceptions. These exceptions
13282 need to be handled separately, as they are usually defined in
13283 runtime units that have been compiled without debugging info. */
13284
13285 ada_add_standard_exceptions (preg, &result);
13286
13287 /* Next, find all exceptions whose scope is local and accessible
13288 from the currently selected frame. */
13289
13290 if (has_stack_frames ())
13291 {
ab816a27 13292 prev_len = result.size ();
778865d3
JB
13293 ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
13294 &result);
ab816a27 13295 if (result.size () > prev_len)
778865d3
JB
13296 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13297 }
13298
13299 /* Add all exceptions whose scope is global. */
13300
ab816a27 13301 prev_len = result.size ();
778865d3 13302 ada_add_global_exceptions (preg, &result);
ab816a27 13303 if (result.size () > prev_len)
778865d3
JB
13304 sort_remove_dups_ada_exceptions_list (&result, prev_len);
13305
778865d3
JB
13306 return result;
13307}
13308
13309/* Return a vector of ada_exc_info.
13310
13311 If REGEXP is NULL, all exceptions are included in the result.
13312 Otherwise, it should contain a valid regular expression,
13313 and only the exceptions whose names match that regular expression
13314 are included in the result.
13315
13316 The exceptions are sorted in the following order:
13317 - Standard exceptions (defined by the Ada language), in
13318 alphabetical order;
13319 - Exceptions only visible from the current frame, in
13320 alphabetical order;
13321 - Exceptions whose scope is global, in alphabetical order. */
13322
ab816a27 13323std::vector<ada_exc_info>
778865d3
JB
13324ada_exceptions_list (const char *regexp)
13325{
2d7cc5c7
PA
13326 if (regexp == NULL)
13327 return ada_exceptions_list_1 (NULL);
778865d3 13328
2d7cc5c7
PA
13329 compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
13330 return ada_exceptions_list_1 (&reg);
778865d3
JB
13331}
13332
13333/* Implement the "info exceptions" command. */
13334
13335static void
1d12d88f 13336info_exceptions_command (const char *regexp, int from_tty)
778865d3 13337{
778865d3 13338 struct gdbarch *gdbarch = get_current_arch ();
778865d3 13339
ab816a27 13340 std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
778865d3
JB
13341
13342 if (regexp != NULL)
13343 printf_filtered
13344 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
13345 else
13346 printf_filtered (_("All defined Ada exceptions:\n"));
13347
ab816a27
TT
13348 for (const ada_exc_info &info : exceptions)
13349 printf_filtered ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
778865d3
JB
13350}
13351
dda83cd7 13352 /* Operators */
4c4b4cd2
PH
13353/* Information about operators given special treatment in functions
13354 below. */
13355/* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
13356
13357#define ADA_OPERATORS \
13358 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
13359 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
13360 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
13361 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
13362 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
13363 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
13364 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
13365 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
13366 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
13367 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
13368 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
13369 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
13370 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
13371 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
13372 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
52ce6436
PH
13373 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
13374 OP_DEFN (OP_OTHERS, 1, 1, 0) \
13375 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
13376 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
4c4b4cd2
PH
13377
13378static void
554794dc
SDJ
13379ada_operator_length (const struct expression *exp, int pc, int *oplenp,
13380 int *argsp)
4c4b4cd2
PH
13381{
13382 switch (exp->elts[pc - 1].opcode)
13383 {
76a01679 13384 default:
4c4b4cd2
PH
13385 operator_length_standard (exp, pc, oplenp, argsp);
13386 break;
13387
13388#define OP_DEFN(op, len, args, binop) \
13389 case op: *oplenp = len; *argsp = args; break;
13390 ADA_OPERATORS;
13391#undef OP_DEFN
52ce6436
PH
13392
13393 case OP_AGGREGATE:
13394 *oplenp = 3;
13395 *argsp = longest_to_int (exp->elts[pc - 2].longconst);
13396 break;
13397
13398 case OP_CHOICES:
13399 *oplenp = 3;
13400 *argsp = longest_to_int (exp->elts[pc - 2].longconst) + 1;
13401 break;
4c4b4cd2
PH
13402 }
13403}
13404
c0201579
JK
13405/* Implementation of the exp_descriptor method operator_check. */
13406
13407static int
13408ada_operator_check (struct expression *exp, int pos,
13409 int (*objfile_func) (struct objfile *objfile, void *data),
13410 void *data)
13411{
13412 const union exp_element *const elts = exp->elts;
13413 struct type *type = NULL;
13414
13415 switch (elts[pos].opcode)
13416 {
13417 case UNOP_IN_RANGE:
13418 case UNOP_QUAL:
13419 type = elts[pos + 1].type;
13420 break;
13421
13422 default:
13423 return operator_check_standard (exp, pos, objfile_func, data);
13424 }
13425
13426 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
13427
6ac37371
SM
13428 if (type != nullptr && type->objfile_owner () != nullptr
13429 && objfile_func (type->objfile_owner (), data))
c0201579
JK
13430 return 1;
13431
13432 return 0;
13433}
13434
4c4b4cd2
PH
13435/* As for operator_length, but assumes PC is pointing at the first
13436 element of the operator, and gives meaningful results only for the
52ce6436 13437 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
4c4b4cd2
PH
13438
13439static void
76a01679 13440ada_forward_operator_length (struct expression *exp, int pc,
dda83cd7 13441 int *oplenp, int *argsp)
4c4b4cd2 13442{
76a01679 13443 switch (exp->elts[pc].opcode)
4c4b4cd2
PH
13444 {
13445 default:
13446 *oplenp = *argsp = 0;
13447 break;
52ce6436 13448
4c4b4cd2
PH
13449#define OP_DEFN(op, len, args, binop) \
13450 case op: *oplenp = len; *argsp = args; break;
13451 ADA_OPERATORS;
13452#undef OP_DEFN
52ce6436
PH
13453
13454 case OP_AGGREGATE:
13455 *oplenp = 3;
13456 *argsp = longest_to_int (exp->elts[pc + 1].longconst);
13457 break;
13458
13459 case OP_CHOICES:
13460 *oplenp = 3;
13461 *argsp = longest_to_int (exp->elts[pc + 1].longconst) + 1;
13462 break;
13463
13464 case OP_STRING:
13465 case OP_NAME:
13466 {
13467 int len = longest_to_int (exp->elts[pc + 1].longconst);
5b4ee69b 13468
52ce6436
PH
13469 *oplenp = 4 + BYTES_TO_EXP_ELEM (len + 1);
13470 *argsp = 0;
13471 break;
13472 }
4c4b4cd2
PH
13473 }
13474}
13475
13476static int
13477ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
13478{
13479 enum exp_opcode op = exp->elts[elt].opcode;
13480 int oplen, nargs;
13481 int pc = elt;
13482 int i;
76a01679 13483
4c4b4cd2
PH
13484 ada_forward_operator_length (exp, elt, &oplen, &nargs);
13485
76a01679 13486 switch (op)
4c4b4cd2 13487 {
76a01679 13488 /* Ada attributes ('Foo). */
4c4b4cd2
PH
13489 case OP_ATR_FIRST:
13490 case OP_ATR_LAST:
13491 case OP_ATR_LENGTH:
13492 case OP_ATR_IMAGE:
13493 case OP_ATR_MAX:
13494 case OP_ATR_MIN:
13495 case OP_ATR_MODULUS:
13496 case OP_ATR_POS:
13497 case OP_ATR_SIZE:
13498 case OP_ATR_TAG:
13499 case OP_ATR_VAL:
13500 break;
13501
13502 case UNOP_IN_RANGE:
13503 case UNOP_QUAL:
323e0a4a
AC
13504 /* XXX: gdb_sprint_host_address, type_sprint */
13505 fprintf_filtered (stream, _("Type @"));
4c4b4cd2
PH
13506 gdb_print_host_address (exp->elts[pc + 1].type, stream);
13507 fprintf_filtered (stream, " (");
13508 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
13509 fprintf_filtered (stream, ")");
13510 break;
13511 case BINOP_IN_BOUNDS:
52ce6436
PH
13512 fprintf_filtered (stream, " (%d)",
13513 longest_to_int (exp->elts[pc + 2].longconst));
4c4b4cd2
PH
13514 break;
13515 case TERNOP_IN_RANGE:
13516 break;
13517
52ce6436
PH
13518 case OP_AGGREGATE:
13519 case OP_OTHERS:
13520 case OP_DISCRETE_RANGE:
13521 case OP_POSITIONAL:
13522 case OP_CHOICES:
13523 break;
13524
13525 case OP_NAME:
13526 case OP_STRING:
13527 {
13528 char *name = &exp->elts[elt + 2].string;
13529 int len = longest_to_int (exp->elts[elt + 1].longconst);
5b4ee69b 13530
52ce6436
PH
13531 fprintf_filtered (stream, "Text: `%.*s'", len, name);
13532 break;
13533 }
13534
4c4b4cd2
PH
13535 default:
13536 return dump_subexp_body_standard (exp, stream, elt);
13537 }
13538
13539 elt += oplen;
13540 for (i = 0; i < nargs; i += 1)
13541 elt = dump_subexp (exp, stream, elt);
13542
13543 return elt;
13544}
13545
13546/* The Ada extension of print_subexp (q.v.). */
13547
76a01679
JB
13548static void
13549ada_print_subexp (struct expression *exp, int *pos,
dda83cd7 13550 struct ui_file *stream, enum precedence prec)
4c4b4cd2 13551{
52ce6436 13552 int oplen, nargs, i;
4c4b4cd2
PH
13553 int pc = *pos;
13554 enum exp_opcode op = exp->elts[pc].opcode;
13555
13556 ada_forward_operator_length (exp, pc, &oplen, &nargs);
13557
52ce6436 13558 *pos += oplen;
4c4b4cd2
PH
13559 switch (op)
13560 {
13561 default:
52ce6436 13562 *pos -= oplen;
4c4b4cd2
PH
13563 print_subexp_standard (exp, pos, stream, prec);
13564 return;
13565
13566 case OP_VAR_VALUE:
987012b8 13567 fputs_filtered (exp->elts[pc + 2].symbol->natural_name (), stream);
4c4b4cd2
PH
13568 return;
13569
13570 case BINOP_IN_BOUNDS:
323e0a4a 13571 /* XXX: sprint_subexp */
4c4b4cd2 13572 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13573 fputs_filtered (" in ", stream);
4c4b4cd2 13574 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13575 fputs_filtered ("'range", stream);
4c4b4cd2 13576 if (exp->elts[pc + 1].longconst > 1)
dda83cd7
SM
13577 fprintf_filtered (stream, "(%ld)",
13578 (long) exp->elts[pc + 1].longconst);
4c4b4cd2
PH
13579 return;
13580
13581 case TERNOP_IN_RANGE:
4c4b4cd2 13582 if (prec >= PREC_EQUAL)
dda83cd7 13583 fputs_filtered ("(", stream);
323e0a4a 13584 /* XXX: sprint_subexp */
4c4b4cd2 13585 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13586 fputs_filtered (" in ", stream);
4c4b4cd2
PH
13587 print_subexp (exp, pos, stream, PREC_EQUAL);
13588 fputs_filtered (" .. ", stream);
13589 print_subexp (exp, pos, stream, PREC_EQUAL);
13590 if (prec >= PREC_EQUAL)
dda83cd7 13591 fputs_filtered (")", stream);
76a01679 13592 return;
4c4b4cd2
PH
13593
13594 case OP_ATR_FIRST:
13595 case OP_ATR_LAST:
13596 case OP_ATR_LENGTH:
13597 case OP_ATR_IMAGE:
13598 case OP_ATR_MAX:
13599 case OP_ATR_MIN:
13600 case OP_ATR_MODULUS:
13601 case OP_ATR_POS:
13602 case OP_ATR_SIZE:
13603 case OP_ATR_TAG:
13604 case OP_ATR_VAL:
4c4b4cd2 13605 if (exp->elts[*pos].opcode == OP_TYPE)
dda83cd7
SM
13606 {
13607 if (exp->elts[*pos + 1].type->code () != TYPE_CODE_VOID)
13608 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0,
79d43c61 13609 &type_print_raw_options);
dda83cd7
SM
13610 *pos += 3;
13611 }
4c4b4cd2 13612 else
dda83cd7 13613 print_subexp (exp, pos, stream, PREC_SUFFIX);
4c4b4cd2
PH
13614 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
13615 if (nargs > 1)
dda83cd7
SM
13616 {
13617 int tem;
13618
13619 for (tem = 1; tem < nargs; tem += 1)
13620 {
13621 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
13622 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
13623 }
13624 fputs_filtered (")", stream);
13625 }
4c4b4cd2 13626 return;
14f9c5c9 13627
4c4b4cd2 13628 case UNOP_QUAL:
4c4b4cd2
PH
13629 type_print (exp->elts[pc + 1].type, "", stream, 0);
13630 fputs_filtered ("'(", stream);
13631 print_subexp (exp, pos, stream, PREC_PREFIX);
13632 fputs_filtered (")", stream);
13633 return;
14f9c5c9 13634
4c4b4cd2 13635 case UNOP_IN_RANGE:
323e0a4a 13636 /* XXX: sprint_subexp */
4c4b4cd2 13637 print_subexp (exp, pos, stream, PREC_SUFFIX);
0b48a291 13638 fputs_filtered (" in ", stream);
79d43c61
TT
13639 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0,
13640 &type_print_raw_options);
4c4b4cd2 13641 return;
52ce6436
PH
13642
13643 case OP_DISCRETE_RANGE:
13644 print_subexp (exp, pos, stream, PREC_SUFFIX);
13645 fputs_filtered ("..", stream);
13646 print_subexp (exp, pos, stream, PREC_SUFFIX);
13647 return;
13648
13649 case OP_OTHERS:
13650 fputs_filtered ("others => ", stream);
13651 print_subexp (exp, pos, stream, PREC_SUFFIX);
13652 return;
13653
13654 case OP_CHOICES:
13655 for (i = 0; i < nargs-1; i += 1)
13656 {
13657 if (i > 0)
13658 fputs_filtered ("|", stream);
13659 print_subexp (exp, pos, stream, PREC_SUFFIX);
13660 }
13661 fputs_filtered (" => ", stream);
13662 print_subexp (exp, pos, stream, PREC_SUFFIX);
13663 return;
13664
13665 case OP_POSITIONAL:
13666 print_subexp (exp, pos, stream, PREC_SUFFIX);
13667 return;
13668
13669 case OP_AGGREGATE:
13670 fputs_filtered ("(", stream);
13671 for (i = 0; i < nargs; i += 1)
13672 {
13673 if (i > 0)
13674 fputs_filtered (", ", stream);
13675 print_subexp (exp, pos, stream, PREC_SUFFIX);
13676 }
13677 fputs_filtered (")", stream);
13678 return;
4c4b4cd2
PH
13679 }
13680}
14f9c5c9
AS
13681
13682/* Table mapping opcodes into strings for printing operators
13683 and precedences of the operators. */
13684
d2e4a39e
AS
13685static const struct op_print ada_op_print_tab[] = {
13686 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
13687 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
13688 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
13689 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
13690 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
13691 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
13692 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
13693 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
13694 {"<=", BINOP_LEQ, PREC_ORDER, 0},
13695 {">=", BINOP_GEQ, PREC_ORDER, 0},
13696 {">", BINOP_GTR, PREC_ORDER, 0},
13697 {"<", BINOP_LESS, PREC_ORDER, 0},
13698 {">>", BINOP_RSH, PREC_SHIFT, 0},
13699 {"<<", BINOP_LSH, PREC_SHIFT, 0},
13700 {"+", BINOP_ADD, PREC_ADD, 0},
13701 {"-", BINOP_SUB, PREC_ADD, 0},
13702 {"&", BINOP_CONCAT, PREC_ADD, 0},
13703 {"*", BINOP_MUL, PREC_MUL, 0},
13704 {"/", BINOP_DIV, PREC_MUL, 0},
13705 {"rem", BINOP_REM, PREC_MUL, 0},
13706 {"mod", BINOP_MOD, PREC_MUL, 0},
13707 {"**", BINOP_EXP, PREC_REPEAT, 0},
13708 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
13709 {"-", UNOP_NEG, PREC_PREFIX, 0},
13710 {"+", UNOP_PLUS, PREC_PREFIX, 0},
13711 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
13712 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
13713 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
4c4b4cd2
PH
13714 {".all", UNOP_IND, PREC_SUFFIX, 1},
13715 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
13716 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
f486487f 13717 {NULL, OP_NULL, PREC_SUFFIX, 0}
14f9c5c9 13718};
6c038f32
PH
13719\f
13720 /* Language vector */
13721
6c038f32
PH
13722static const struct exp_descriptor ada_exp_descriptor = {
13723 ada_print_subexp,
13724 ada_operator_length,
c0201579 13725 ada_operator_check,
6c038f32
PH
13726 ada_dump_subexp_body,
13727 ada_evaluate_subexp
13728};
13729
b5ec771e
PA
13730/* symbol_name_matcher_ftype adapter for wild_match. */
13731
13732static bool
13733do_wild_match (const char *symbol_search_name,
13734 const lookup_name_info &lookup_name,
a207cff2 13735 completion_match_result *comp_match_res)
b5ec771e
PA
13736{
13737 return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
13738}
13739
13740/* symbol_name_matcher_ftype adapter for full_match. */
13741
13742static bool
13743do_full_match (const char *symbol_search_name,
13744 const lookup_name_info &lookup_name,
a207cff2 13745 completion_match_result *comp_match_res)
b5ec771e 13746{
959d6a67
TT
13747 const char *lname = lookup_name.ada ().lookup_name ().c_str ();
13748
13749 /* If both symbols start with "_ada_", just let the loop below
13750 handle the comparison. However, if only the symbol name starts
13751 with "_ada_", skip the prefix and let the match proceed as
13752 usual. */
13753 if (startswith (symbol_search_name, "_ada_")
13754 && !startswith (lname, "_ada"))
86b44259
TT
13755 symbol_search_name += 5;
13756
86b44259
TT
13757 int uscore_count = 0;
13758 while (*lname != '\0')
13759 {
13760 if (*symbol_search_name != *lname)
13761 {
13762 if (*symbol_search_name == 'B' && uscore_count == 2
13763 && symbol_search_name[1] == '_')
13764 {
13765 symbol_search_name += 2;
13766 while (isdigit (*symbol_search_name))
13767 ++symbol_search_name;
13768 if (symbol_search_name[0] == '_'
13769 && symbol_search_name[1] == '_')
13770 {
13771 symbol_search_name += 2;
13772 continue;
13773 }
13774 }
13775 return false;
13776 }
13777
13778 if (*symbol_search_name == '_')
13779 ++uscore_count;
13780 else
13781 uscore_count = 0;
13782
13783 ++symbol_search_name;
13784 ++lname;
13785 }
13786
13787 return is_name_suffix (symbol_search_name);
b5ec771e
PA
13788}
13789
a2cd4f14
JB
13790/* symbol_name_matcher_ftype for exact (verbatim) matches. */
13791
13792static bool
13793do_exact_match (const char *symbol_search_name,
13794 const lookup_name_info &lookup_name,
13795 completion_match_result *comp_match_res)
13796{
13797 return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
13798}
13799
b5ec771e
PA
13800/* Build the Ada lookup name for LOOKUP_NAME. */
13801
13802ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
13803{
e0802d59 13804 gdb::string_view user_name = lookup_name.name ();
b5ec771e 13805
6a780b67 13806 if (!user_name.empty () && user_name[0] == '<')
b5ec771e
PA
13807 {
13808 if (user_name.back () == '>')
e0802d59 13809 m_encoded_name
5ac58899 13810 = gdb::to_string (user_name.substr (1, user_name.size () - 2));
b5ec771e 13811 else
e0802d59 13812 m_encoded_name
5ac58899 13813 = gdb::to_string (user_name.substr (1, user_name.size () - 1));
b5ec771e
PA
13814 m_encoded_p = true;
13815 m_verbatim_p = true;
13816 m_wild_match_p = false;
13817 m_standard_p = false;
13818 }
13819 else
13820 {
13821 m_verbatim_p = false;
13822
e0802d59 13823 m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
b5ec771e
PA
13824
13825 if (!m_encoded_p)
13826 {
e0802d59 13827 const char *folded = ada_fold_name (user_name);
5c4258f4
TT
13828 m_encoded_name = ada_encode_1 (folded, false);
13829 if (m_encoded_name.empty ())
5ac58899 13830 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13831 }
13832 else
5ac58899 13833 m_encoded_name = gdb::to_string (user_name);
b5ec771e
PA
13834
13835 /* Handle the 'package Standard' special case. See description
13836 of m_standard_p. */
13837 if (startswith (m_encoded_name.c_str (), "standard__"))
13838 {
13839 m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
13840 m_standard_p = true;
13841 }
13842 else
13843 m_standard_p = false;
74ccd7f5 13844
b5ec771e
PA
13845 /* If the name contains a ".", then the user is entering a fully
13846 qualified entity name, and the match must not be done in wild
13847 mode. Similarly, if the user wants to complete what looks
13848 like an encoded name, the match must not be done in wild
13849 mode. Also, in the standard__ special case always do
13850 non-wild matching. */
13851 m_wild_match_p
13852 = (lookup_name.match_type () != symbol_name_match_type::FULL
13853 && !m_encoded_p
13854 && !m_standard_p
13855 && user_name.find ('.') == std::string::npos);
13856 }
13857}
13858
13859/* symbol_name_matcher_ftype method for Ada. This only handles
13860 completion mode. */
13861
13862static bool
13863ada_symbol_name_matches (const char *symbol_search_name,
13864 const lookup_name_info &lookup_name,
a207cff2 13865 completion_match_result *comp_match_res)
74ccd7f5 13866{
b5ec771e
PA
13867 return lookup_name.ada ().matches (symbol_search_name,
13868 lookup_name.match_type (),
a207cff2 13869 comp_match_res);
b5ec771e
PA
13870}
13871
de63c46b
PA
13872/* A name matcher that matches the symbol name exactly, with
13873 strcmp. */
13874
13875static bool
13876literal_symbol_name_matcher (const char *symbol_search_name,
13877 const lookup_name_info &lookup_name,
13878 completion_match_result *comp_match_res)
13879{
e0802d59 13880 gdb::string_view name_view = lookup_name.name ();
de63c46b 13881
e0802d59
TT
13882 if (lookup_name.completion_mode ()
13883 ? (strncmp (symbol_search_name, name_view.data (),
13884 name_view.size ()) == 0)
13885 : symbol_search_name == name_view)
de63c46b
PA
13886 {
13887 if (comp_match_res != NULL)
13888 comp_match_res->set_match (symbol_search_name);
13889 return true;
13890 }
13891 else
13892 return false;
13893}
13894
c9debfb9 13895/* Implement the "get_symbol_name_matcher" language_defn method for
b5ec771e
PA
13896 Ada. */
13897
13898static symbol_name_matcher_ftype *
13899ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
13900{
de63c46b
PA
13901 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
13902 return literal_symbol_name_matcher;
13903
b5ec771e
PA
13904 if (lookup_name.completion_mode ())
13905 return ada_symbol_name_matches;
74ccd7f5 13906 else
b5ec771e
PA
13907 {
13908 if (lookup_name.ada ().wild_match_p ())
13909 return do_wild_match;
a2cd4f14
JB
13910 else if (lookup_name.ada ().verbatim_p ())
13911 return do_exact_match;
b5ec771e
PA
13912 else
13913 return do_full_match;
13914 }
74ccd7f5
JB
13915}
13916
0874fd07
AB
13917/* Class representing the Ada language. */
13918
13919class ada_language : public language_defn
13920{
13921public:
13922 ada_language ()
0e25e767 13923 : language_defn (language_ada)
0874fd07 13924 { /* Nothing. */ }
5bd40f2a 13925
6f7664a9
AB
13926 /* See language.h. */
13927
13928 const char *name () const override
13929 { return "ada"; }
13930
13931 /* See language.h. */
13932
13933 const char *natural_name () const override
13934 { return "Ada"; }
13935
e171d6f1
AB
13936 /* See language.h. */
13937
13938 const std::vector<const char *> &filename_extensions () const override
13939 {
13940 static const std::vector<const char *> extensions
13941 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13942 return extensions;
13943 }
13944
5bd40f2a
AB
13945 /* Print an array element index using the Ada syntax. */
13946
13947 void print_array_index (struct type *index_type,
13948 LONGEST index,
13949 struct ui_file *stream,
13950 const value_print_options *options) const override
13951 {
13952 struct value *index_value = val_atr (index_type, index);
13953
00c696a6 13954 value_print (index_value, stream, options);
5bd40f2a
AB
13955 fprintf_filtered (stream, " => ");
13956 }
15e5fd35
AB
13957
13958 /* Implement the "read_var_value" language_defn method for Ada. */
13959
13960 struct value *read_var_value (struct symbol *var,
13961 const struct block *var_block,
13962 struct frame_info *frame) const override
13963 {
13964 /* The only case where default_read_var_value is not sufficient
13965 is when VAR is a renaming... */
13966 if (frame != nullptr)
13967 {
13968 const struct block *frame_block = get_frame_block (frame, NULL);
13969 if (frame_block != nullptr && ada_is_renaming_symbol (var))
13970 return ada_read_renaming_var_value (var, frame_block);
13971 }
13972
13973 /* This is a typical case where we expect the default_read_var_value
13974 function to work. */
13975 return language_defn::read_var_value (var, var_block, frame);
13976 }
1fb314aa
AB
13977
13978 /* See language.h. */
13979 void language_arch_info (struct gdbarch *gdbarch,
13980 struct language_arch_info *lai) const override
13981 {
13982 const struct builtin_type *builtin = builtin_type (gdbarch);
13983
7bea47f0
AB
13984 /* Helper function to allow shorter lines below. */
13985 auto add = [&] (struct type *t)
13986 {
13987 lai->add_primitive_type (t);
13988 };
13989
13990 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
13991 0, "integer"));
13992 add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
13993 0, "long_integer"));
13994 add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
13995 0, "short_integer"));
13996 struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
13997 0, "character");
13998 lai->set_string_char_type (char_type);
13999 add (char_type);
14000 add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
14001 "float", gdbarch_float_format (gdbarch)));
14002 add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
14003 "long_float", gdbarch_double_format (gdbarch)));
14004 add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
14005 0, "long_long_integer"));
14006 add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
14007 "long_long_float",
14008 gdbarch_long_double_format (gdbarch)));
14009 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
14010 0, "natural"));
14011 add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
14012 0, "positive"));
14013 add (builtin->builtin_void);
14014
14015 struct type *system_addr_ptr
1fb314aa
AB
14016 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
14017 "void"));
7bea47f0
AB
14018 system_addr_ptr->set_name ("system__address");
14019 add (system_addr_ptr);
1fb314aa
AB
14020
14021 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
14022 type. This is a signed integral type whose size is the same as
14023 the size of addresses. */
7bea47f0
AB
14024 unsigned int addr_length = TYPE_LENGTH (system_addr_ptr);
14025 add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
14026 "storage_offset"));
1fb314aa 14027
7bea47f0 14028 lai->set_bool_type (builtin->builtin_bool);
1fb314aa 14029 }
4009ee92
AB
14030
14031 /* See language.h. */
14032
14033 bool iterate_over_symbols
14034 (const struct block *block, const lookup_name_info &name,
14035 domain_enum domain,
14036 gdb::function_view<symbol_found_callback_ftype> callback) const override
14037 {
d1183b06
TT
14038 std::vector<struct block_symbol> results
14039 = ada_lookup_symbol_list_worker (name, block, domain, 0);
4009ee92
AB
14040 for (block_symbol &sym : results)
14041 {
14042 if (!callback (&sym))
14043 return false;
14044 }
14045
14046 return true;
14047 }
6f827019
AB
14048
14049 /* See language.h. */
14050 bool sniff_from_mangled_name (const char *mangled,
14051 char **out) const override
14052 {
14053 std::string demangled = ada_decode (mangled);
14054
14055 *out = NULL;
14056
14057 if (demangled != mangled && demangled[0] != '<')
14058 {
14059 /* Set the gsymbol language to Ada, but still return 0.
14060 Two reasons for that:
14061
14062 1. For Ada, we prefer computing the symbol's decoded name
14063 on the fly rather than pre-compute it, in order to save
14064 memory (Ada projects are typically very large).
14065
14066 2. There are some areas in the definition of the GNAT
14067 encoding where, with a bit of bad luck, we might be able
14068 to decode a non-Ada symbol, generating an incorrect
14069 demangled name (Eg: names ending with "TB" for instance
14070 are identified as task bodies and so stripped from
14071 the decoded name returned).
14072
14073 Returning true, here, but not setting *DEMANGLED, helps us get
14074 a little bit of the best of both worlds. Because we're last,
14075 we should not affect any of the other languages that were
14076 able to demangle the symbol before us; we get to correctly
14077 tag Ada symbols as such; and even if we incorrectly tagged a
14078 non-Ada symbol, which should be rare, any routing through the
14079 Ada language should be transparent (Ada tries to behave much
14080 like C/C++ with non-Ada symbols). */
14081 return true;
14082 }
14083
14084 return false;
14085 }
fbfb0a46
AB
14086
14087 /* See language.h. */
14088
5399db93 14089 char *demangle_symbol (const char *mangled, int options) const override
0a50df5d
AB
14090 {
14091 return ada_la_decode (mangled, options);
14092 }
14093
14094 /* See language.h. */
14095
fbfb0a46
AB
14096 void print_type (struct type *type, const char *varstring,
14097 struct ui_file *stream, int show, int level,
14098 const struct type_print_options *flags) const override
14099 {
14100 ada_print_type (type, varstring, stream, show, level, flags);
14101 }
c9debfb9 14102
53fc67f8
AB
14103 /* See language.h. */
14104
14105 const char *word_break_characters (void) const override
14106 {
14107 return ada_completer_word_break_characters;
14108 }
14109
7e56227d
AB
14110 /* See language.h. */
14111
14112 void collect_symbol_completion_matches (completion_tracker &tracker,
14113 complete_symbol_mode mode,
14114 symbol_name_match_type name_match_type,
14115 const char *text, const char *word,
14116 enum type_code code) const override
14117 {
14118 struct symbol *sym;
14119 const struct block *b, *surrounding_static_block = 0;
14120 struct block_iterator iter;
14121
14122 gdb_assert (code == TYPE_CODE_UNDEF);
14123
14124 lookup_name_info lookup_name (text, name_match_type, true);
14125
14126 /* First, look at the partial symtab symbols. */
14127 expand_symtabs_matching (NULL,
14128 lookup_name,
14129 NULL,
14130 NULL,
14131 ALL_DOMAIN);
14132
14133 /* At this point scan through the misc symbol vectors and add each
14134 symbol you find to the list. Eventually we want to ignore
14135 anything that isn't a text symbol (everything else will be
14136 handled by the psymtab code above). */
14137
14138 for (objfile *objfile : current_program_space->objfiles ())
14139 {
14140 for (minimal_symbol *msymbol : objfile->msymbols ())
14141 {
14142 QUIT;
14143
14144 if (completion_skip_symbol (mode, msymbol))
14145 continue;
14146
14147 language symbol_language = msymbol->language ();
14148
14149 /* Ada minimal symbols won't have their language set to Ada. If
14150 we let completion_list_add_name compare using the
14151 default/C-like matcher, then when completing e.g., symbols in a
14152 package named "pck", we'd match internal Ada symbols like
14153 "pckS", which are invalid in an Ada expression, unless you wrap
14154 them in '<' '>' to request a verbatim match.
14155
14156 Unfortunately, some Ada encoded names successfully demangle as
14157 C++ symbols (using an old mangling scheme), such as "name__2Xn"
14158 -> "Xn::name(void)" and thus some Ada minimal symbols end up
14159 with the wrong language set. Paper over that issue here. */
14160 if (symbol_language == language_auto
14161 || symbol_language == language_cplus)
14162 symbol_language = language_ada;
14163
14164 completion_list_add_name (tracker,
14165 symbol_language,
14166 msymbol->linkage_name (),
14167 lookup_name, text, word);
14168 }
14169 }
14170
14171 /* Search upwards from currently selected frame (so that we can
14172 complete on local vars. */
14173
14174 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
14175 {
14176 if (!BLOCK_SUPERBLOCK (b))
14177 surrounding_static_block = b; /* For elmin of dups */
14178
14179 ALL_BLOCK_SYMBOLS (b, iter, sym)
14180 {
14181 if (completion_skip_symbol (mode, sym))
14182 continue;
14183
14184 completion_list_add_name (tracker,
14185 sym->language (),
14186 sym->linkage_name (),
14187 lookup_name, text, word);
14188 }
14189 }
14190
14191 /* Go through the symtabs and check the externs and statics for
14192 symbols which match. */
14193
14194 for (objfile *objfile : current_program_space->objfiles ())
14195 {
14196 for (compunit_symtab *s : objfile->compunits ())
14197 {
14198 QUIT;
14199 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), GLOBAL_BLOCK);
14200 ALL_BLOCK_SYMBOLS (b, iter, sym)
14201 {
14202 if (completion_skip_symbol (mode, sym))
14203 continue;
14204
14205 completion_list_add_name (tracker,
14206 sym->language (),
14207 sym->linkage_name (),
14208 lookup_name, text, word);
14209 }
14210 }
14211 }
14212
14213 for (objfile *objfile : current_program_space->objfiles ())
14214 {
14215 for (compunit_symtab *s : objfile->compunits ())
14216 {
14217 QUIT;
14218 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), STATIC_BLOCK);
14219 /* Don't do this block twice. */
14220 if (b == surrounding_static_block)
14221 continue;
14222 ALL_BLOCK_SYMBOLS (b, iter, sym)
14223 {
14224 if (completion_skip_symbol (mode, sym))
14225 continue;
14226
14227 completion_list_add_name (tracker,
14228 sym->language (),
14229 sym->linkage_name (),
14230 lookup_name, text, word);
14231 }
14232 }
14233 }
14234 }
14235
f16a9f57
AB
14236 /* See language.h. */
14237
14238 gdb::unique_xmalloc_ptr<char> watch_location_expression
14239 (struct type *type, CORE_ADDR addr) const override
14240 {
14241 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
14242 std::string name = type_to_string (type);
14243 return gdb::unique_xmalloc_ptr<char>
14244 (xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr)));
14245 }
14246
a1d1fa3e
AB
14247 /* See language.h. */
14248
14249 void value_print (struct value *val, struct ui_file *stream,
14250 const struct value_print_options *options) const override
14251 {
14252 return ada_value_print (val, stream, options);
14253 }
14254
ebe2334e
AB
14255 /* See language.h. */
14256
14257 void value_print_inner
14258 (struct value *val, struct ui_file *stream, int recurse,
14259 const struct value_print_options *options) const override
14260 {
14261 return ada_value_print_inner (val, stream, recurse, options);
14262 }
14263
a78a19b1
AB
14264 /* See language.h. */
14265
14266 struct block_symbol lookup_symbol_nonlocal
14267 (const char *name, const struct block *block,
14268 const domain_enum domain) const override
14269 {
14270 struct block_symbol sym;
14271
14272 sym = ada_lookup_symbol (name, block_static_block (block), domain);
14273 if (sym.symbol != NULL)
14274 return sym;
14275
14276 /* If we haven't found a match at this point, try the primitive
14277 types. In other languages, this search is performed before
14278 searching for global symbols in order to short-circuit that
14279 global-symbol search if it happens that the name corresponds
14280 to a primitive type. But we cannot do the same in Ada, because
14281 it is perfectly legitimate for a program to declare a type which
14282 has the same name as a standard type. If looking up a type in
14283 that situation, we have traditionally ignored the primitive type
14284 in favor of user-defined types. This is why, unlike most other
14285 languages, we search the primitive types this late and only after
14286 having searched the global symbols without success. */
14287
14288 if (domain == VAR_DOMAIN)
14289 {
14290 struct gdbarch *gdbarch;
14291
14292 if (block == NULL)
14293 gdbarch = target_gdbarch ();
14294 else
14295 gdbarch = block_gdbarch (block);
14296 sym.symbol
14297 = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
14298 if (sym.symbol != NULL)
14299 return sym;
14300 }
14301
14302 return {};
14303 }
14304
87afa652
AB
14305 /* See language.h. */
14306
14307 int parser (struct parser_state *ps) const override
14308 {
14309 warnings_issued = 0;
14310 return ada_parse (ps);
14311 }
14312
1bf9c363
AB
14313 /* See language.h.
14314
14315 Same as evaluate_type (*EXP), but resolves ambiguous symbol references
14316 (marked by OP_VAR_VALUE nodes in which the symbol has an undefined
14317 namespace) and converts operators that are user-defined into
14318 appropriate function calls. If CONTEXT_TYPE is non-null, it provides
14319 a preferred result type [at the moment, only type void has any
14320 effect---causing procedures to be preferred over functions in calls].
14321 A null CONTEXT_TYPE indicates that a non-void return type is
14322 preferred. May change (expand) *EXP. */
14323
c5c41205
TT
14324 void post_parser (expression_up *expp, struct parser_state *ps)
14325 const override
1bf9c363
AB
14326 {
14327 struct type *context_type = NULL;
14328 int pc = 0;
14329
c5c41205 14330 if (ps->void_context_p)
1bf9c363
AB
14331 context_type = builtin_type ((*expp)->gdbarch)->builtin_void;
14332
c5c41205
TT
14333 resolve_subexp (expp, &pc, 1, context_type, ps->parse_completion,
14334 ps->block_tracker);
1bf9c363
AB
14335 }
14336
ec8cec5b
AB
14337 /* See language.h. */
14338
14339 void emitchar (int ch, struct type *chtype,
14340 struct ui_file *stream, int quoter) const override
14341 {
14342 ada_emit_char (ch, chtype, stream, quoter, 1);
14343 }
14344
52b50f2c
AB
14345 /* See language.h. */
14346
14347 void printchar (int ch, struct type *chtype,
14348 struct ui_file *stream) const override
14349 {
14350 ada_printchar (ch, chtype, stream);
14351 }
14352
d711ee67
AB
14353 /* See language.h. */
14354
14355 void printstr (struct ui_file *stream, struct type *elttype,
14356 const gdb_byte *string, unsigned int length,
14357 const char *encoding, int force_ellipses,
14358 const struct value_print_options *options) const override
14359 {
14360 ada_printstr (stream, elttype, string, length, encoding,
14361 force_ellipses, options);
14362 }
14363
4ffc13fb
AB
14364 /* See language.h. */
14365
14366 void print_typedef (struct type *type, struct symbol *new_symbol,
14367 struct ui_file *stream) const override
14368 {
14369 ada_print_typedef (type, new_symbol, stream);
14370 }
14371
39e7ecca
AB
14372 /* See language.h. */
14373
14374 bool is_string_type_p (struct type *type) const override
14375 {
14376 return ada_is_string_type (type);
14377 }
14378
22e3f3ed
AB
14379 /* See language.h. */
14380
14381 const char *struct_too_deep_ellipsis () const override
14382 { return "(...)"; }
39e7ecca 14383
67bd3fd5
AB
14384 /* See language.h. */
14385
14386 bool c_style_arrays_p () const override
14387 { return false; }
14388
d3355e4d
AB
14389 /* See language.h. */
14390
14391 bool store_sym_names_in_linkage_form_p () const override
14392 { return true; }
14393
b63a3f3f
AB
14394 /* See language.h. */
14395
14396 const struct lang_varobj_ops *varobj_ops () const override
14397 { return &ada_varobj_ops; }
14398
5aba6ebe
AB
14399 /* See language.h. */
14400
14401 const struct exp_descriptor *expression_ops () const override
14402 { return &ada_exp_descriptor; }
14403
b7c6e27d
AB
14404 /* See language.h. */
14405
14406 const struct op_print *opcode_print_table () const override
14407 { return ada_op_print_tab; }
14408
c9debfb9
AB
14409protected:
14410 /* See language.h. */
14411
14412 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
14413 (const lookup_name_info &lookup_name) const override
14414 {
14415 return ada_get_symbol_name_matcher (lookup_name);
14416 }
0874fd07
AB
14417};
14418
14419/* Single instance of the Ada language class. */
14420
14421static ada_language ada_language_defn;
14422
5bf03f13
JB
14423/* Command-list for the "set/show ada" prefix command. */
14424static struct cmd_list_element *set_ada_list;
14425static struct cmd_list_element *show_ada_list;
14426
2060206e
PA
14427static void
14428initialize_ada_catchpoint_ops (void)
14429{
14430 struct breakpoint_ops *ops;
14431
14432 initialize_breakpoint_ops ();
14433
14434 ops = &catch_exception_breakpoint_ops;
14435 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
14436 ops->allocate_location = allocate_location_exception;
14437 ops->re_set = re_set_exception;
14438 ops->check_status = check_status_exception;
14439 ops->print_it = print_it_exception;
14440 ops->print_one = print_one_exception;
14441 ops->print_mention = print_mention_exception;
14442 ops->print_recreate = print_recreate_exception;
2060206e
PA
14443
14444 ops = &catch_exception_unhandled_breakpoint_ops;
14445 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
14446 ops->allocate_location = allocate_location_exception;
14447 ops->re_set = re_set_exception;
14448 ops->check_status = check_status_exception;
14449 ops->print_it = print_it_exception;
14450 ops->print_one = print_one_exception;
14451 ops->print_mention = print_mention_exception;
14452 ops->print_recreate = print_recreate_exception;
2060206e
PA
14453
14454 ops = &catch_assert_breakpoint_ops;
14455 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
14456 ops->allocate_location = allocate_location_exception;
14457 ops->re_set = re_set_exception;
14458 ops->check_status = check_status_exception;
14459 ops->print_it = print_it_exception;
14460 ops->print_one = print_one_exception;
14461 ops->print_mention = print_mention_exception;
14462 ops->print_recreate = print_recreate_exception;
9f757bf7
XR
14463
14464 ops = &catch_handlers_breakpoint_ops;
14465 *ops = bkpt_breakpoint_ops;
37f6a7f4
TT
14466 ops->allocate_location = allocate_location_exception;
14467 ops->re_set = re_set_exception;
14468 ops->check_status = check_status_exception;
14469 ops->print_it = print_it_exception;
14470 ops->print_one = print_one_exception;
14471 ops->print_mention = print_mention_exception;
14472 ops->print_recreate = print_recreate_exception;
2060206e
PA
14473}
14474
3d9434b5
JB
14475/* This module's 'new_objfile' observer. */
14476
14477static void
14478ada_new_objfile_observer (struct objfile *objfile)
14479{
14480 ada_clear_symbol_cache ();
14481}
14482
14483/* This module's 'free_objfile' observer. */
14484
14485static void
14486ada_free_objfile_observer (struct objfile *objfile)
14487{
14488 ada_clear_symbol_cache ();
14489}
14490
6c265988 14491void _initialize_ada_language ();
d2e4a39e 14492void
6c265988 14493_initialize_ada_language ()
14f9c5c9 14494{
2060206e
PA
14495 initialize_ada_catchpoint_ops ();
14496
0743fc83
TT
14497 add_basic_prefix_cmd ("ada", no_class,
14498 _("Prefix command for changing Ada-specific settings."),
14499 &set_ada_list, "set ada ", 0, &setlist);
5bf03f13 14500
0743fc83
TT
14501 add_show_prefix_cmd ("ada", no_class,
14502 _("Generic command for showing Ada-specific settings."),
14503 &show_ada_list, "show ada ", 0, &showlist);
5bf03f13
JB
14504
14505 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
dda83cd7 14506 &trust_pad_over_xvs, _("\
590042fc
PW
14507Enable or disable an optimization trusting PAD types over XVS types."), _("\
14508Show whether an optimization trusting PAD types over XVS types is activated."),
dda83cd7 14509 _("\
5bf03f13
JB
14510This is related to the encoding used by the GNAT compiler. The debugger\n\
14511should normally trust the contents of PAD types, but certain older versions\n\
14512of GNAT have a bug that sometimes causes the information in the PAD type\n\
14513to be incorrect. Turning this setting \"off\" allows the debugger to\n\
14514work around this bug. It is always safe to turn this option \"off\", but\n\
14515this incurs a slight performance penalty, so it is recommended to NOT change\n\
14516this option to \"off\" unless necessary."),
dda83cd7 14517 NULL, NULL, &set_ada_list, &show_ada_list);
5bf03f13 14518
d72413e6
PMR
14519 add_setshow_boolean_cmd ("print-signatures", class_vars,
14520 &print_signatures, _("\
14521Enable or disable the output of formal and return types for functions in the \
590042fc 14522overloads selection menu."), _("\
d72413e6 14523Show whether the output of formal and return types for functions in the \
590042fc 14524overloads selection menu is activated."),
d72413e6
PMR
14525 NULL, NULL, NULL, &set_ada_list, &show_ada_list);
14526
9ac4176b
PA
14527 add_catch_command ("exception", _("\
14528Catch Ada exceptions, when raised.\n\
9bf7038b 14529Usage: catch exception [ARG] [if CONDITION]\n\
60a90376
JB
14530Without any argument, stop when any Ada exception is raised.\n\
14531If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
14532being raised does not have a handler (and will therefore lead to the task's\n\
14533termination).\n\
14534Otherwise, the catchpoint only stops when the name of the exception being\n\
9bf7038b
TT
14535raised is the same as ARG.\n\
14536CONDITION is a boolean expression that is evaluated to see whether the\n\
14537exception should cause a stop."),
9ac4176b 14538 catch_ada_exception_command,
71bed2db 14539 catch_ada_completer,
9ac4176b
PA
14540 CATCH_PERMANENT,
14541 CATCH_TEMPORARY);
9f757bf7
XR
14542
14543 add_catch_command ("handlers", _("\
14544Catch Ada exceptions, when handled.\n\
9bf7038b
TT
14545Usage: catch handlers [ARG] [if CONDITION]\n\
14546Without any argument, stop when any Ada exception is handled.\n\
14547With an argument, catch only exceptions with the given name.\n\
14548CONDITION is a boolean expression that is evaluated to see whether the\n\
14549exception should cause a stop."),
9f757bf7 14550 catch_ada_handlers_command,
dda83cd7 14551 catch_ada_completer,
9f757bf7
XR
14552 CATCH_PERMANENT,
14553 CATCH_TEMPORARY);
9ac4176b
PA
14554 add_catch_command ("assert", _("\
14555Catch failed Ada assertions, when raised.\n\
9bf7038b
TT
14556Usage: catch assert [if CONDITION]\n\
14557CONDITION is a boolean expression that is evaluated to see whether the\n\
14558exception should cause a stop."),
9ac4176b 14559 catch_assert_command,
dda83cd7 14560 NULL,
9ac4176b
PA
14561 CATCH_PERMANENT,
14562 CATCH_TEMPORARY);
14563
6c038f32 14564 varsize_limit = 65536;
3fcded8f
JB
14565 add_setshow_uinteger_cmd ("varsize-limit", class_support,
14566 &varsize_limit, _("\
14567Set the maximum number of bytes allowed in a variable-size object."), _("\
14568Show the maximum number of bytes allowed in a variable-size object."), _("\
14569Attempts to access an object whose size is not a compile-time constant\n\
14570and exceeds this limit will cause an error."),
14571 NULL, NULL, &setlist, &showlist);
6c038f32 14572
778865d3
JB
14573 add_info ("exceptions", info_exceptions_command,
14574 _("\
14575List all Ada exception names.\n\
9bf7038b 14576Usage: info exceptions [REGEXP]\n\
778865d3
JB
14577If a regular expression is passed as an argument, only those matching\n\
14578the regular expression are listed."));
14579
0743fc83
TT
14580 add_basic_prefix_cmd ("ada", class_maintenance,
14581 _("Set Ada maintenance-related variables."),
14582 &maint_set_ada_cmdlist, "maintenance set ada ",
14583 0/*allow-unknown*/, &maintenance_set_cmdlist);
c6044dd1 14584
0743fc83
TT
14585 add_show_prefix_cmd ("ada", class_maintenance,
14586 _("Show Ada maintenance-related variables."),
14587 &maint_show_ada_cmdlist, "maintenance show ada ",
14588 0/*allow-unknown*/, &maintenance_show_cmdlist);
c6044dd1
JB
14589
14590 add_setshow_boolean_cmd
14591 ("ignore-descriptive-types", class_maintenance,
14592 &ada_ignore_descriptive_types_p,
14593 _("Set whether descriptive types generated by GNAT should be ignored."),
14594 _("Show whether descriptive types generated by GNAT should be ignored."),
14595 _("\
14596When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14597DWARF attribute."),
14598 NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
14599
459a2e4c
TT
14600 decoded_names_store = htab_create_alloc (256, htab_hash_string, streq_hash,
14601 NULL, xcalloc, xfree);
6b69afc4 14602
3d9434b5 14603 /* The ada-lang observers. */
76727919
TT
14604 gdb::observers::new_objfile.attach (ada_new_objfile_observer);
14605 gdb::observers::free_objfile.attach (ada_free_objfile_observer);
14606 gdb::observers::inferior_exit.attach (ada_inferior_exit);
14f9c5c9 14607}