]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
2004-07-09 H.J. Lu <hongjiu.lu@intel.com>
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
14f9c5c9 1/* Ada language support routines for GDB, the GNU debugger. Copyright
4c4b4cd2 2 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003, 2004.
de5ad195 3 Free Software Foundation, Inc.
14f9c5c9
AS
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
96d887e8
PH
21
22/* Sections of code marked
23
24 #ifdef GNAT_GDB
25 ...
26 #endif
27
28 indicate sections that are used in sources distributed by
29 ACT, Inc., but not yet integrated into the public tree (where
30 GNAT_GDB is not defined). They are retained here nevertheless
31 to minimize the problems of maintaining different versions
32 of the source and to make the full source available. */
33
4c4b4cd2 34#include "defs.h"
14f9c5c9 35#include <stdio.h>
0c30c098 36#include "gdb_string.h"
14f9c5c9
AS
37#include <ctype.h>
38#include <stdarg.h>
39#include "demangle.h"
4c4b4cd2
PH
40#include "gdb_regex.h"
41#include "frame.h"
14f9c5c9
AS
42#include "symtab.h"
43#include "gdbtypes.h"
44#include "gdbcmd.h"
45#include "expression.h"
46#include "parser-defs.h"
47#include "language.h"
48#include "c-lang.h"
49#include "inferior.h"
50#include "symfile.h"
51#include "objfiles.h"
52#include "breakpoint.h"
53#include "gdbcore.h"
4c4b4cd2
PH
54#include "hashtab.h"
55#include "gdb_obstack.h"
14f9c5c9 56#include "ada-lang.h"
4c4b4cd2
PH
57#include "completer.h"
58#include "gdb_stat.h"
59#ifdef UI_OUT
14f9c5c9 60#include "ui-out.h"
4c4b4cd2 61#endif
fe898f56 62#include "block.h"
04714b91 63#include "infcall.h"
de4f826b 64#include "dictionary.h"
14f9c5c9 65
4c4b4cd2
PH
66#ifndef ADA_RETAIN_DOTS
67#define ADA_RETAIN_DOTS 0
68#endif
69
70/* Define whether or not the C operator '/' truncates towards zero for
71 differently signed operands (truncation direction is undefined in C).
72 Copied from valarith.c. */
73
74#ifndef TRUNCATION_TOWARDS_ZERO
75#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
76#endif
77
96d887e8 78#ifdef GNAT_GDB
4c4b4cd2
PH
79/* A structure that contains a vector of strings.
80 The main purpose of this type is to group the vector and its
81 associated parameters in one structure. This makes it easier
82 to handle and pass around. */
14f9c5c9 83
4c4b4cd2
PH
84struct string_vector
85{
76a01679
JB
86 char **array; /* The vector itself. */
87 int index; /* Index of the next available element in the array. */
88 size_t size; /* The number of entries allocated in the array. */
4c4b4cd2
PH
89};
90
91static struct string_vector xnew_string_vector (int initial_size);
92static void string_vector_append (struct string_vector *sv, char *str);
96d887e8 93#endif /* GNAT_GDB */
4c4b4cd2
PH
94
95static const char *ada_unqualified_name (const char *decoded_name);
96static char *add_angle_brackets (const char *str);
97static void extract_string (CORE_ADDR addr, char *buf);
98static char *function_name_from_pc (CORE_ADDR pc);
14f9c5c9 99
d2e4a39e 100static struct type *ada_create_fundamental_type (struct objfile *, int);
14f9c5c9
AS
101
102static void modify_general_field (char *, LONGEST, int, int);
103
d2e4a39e 104static struct type *desc_base_type (struct type *);
14f9c5c9 105
d2e4a39e 106static struct type *desc_bounds_type (struct type *);
14f9c5c9 107
d2e4a39e 108static struct value *desc_bounds (struct value *);
14f9c5c9 109
d2e4a39e 110static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 111
d2e4a39e 112static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 113
d2e4a39e 114static struct type *desc_data_type (struct type *);
14f9c5c9 115
d2e4a39e 116static struct value *desc_data (struct value *);
14f9c5c9 117
d2e4a39e 118static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 119
d2e4a39e 120static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 121
d2e4a39e 122static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 123
d2e4a39e 124static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 125
d2e4a39e 126static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 127
d2e4a39e 128static struct type *desc_index_type (struct type *, int);
14f9c5c9 129
d2e4a39e 130static int desc_arity (struct type *);
14f9c5c9 131
d2e4a39e 132static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 133
d2e4a39e 134static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 135
4c4b4cd2 136static struct value *ensure_lval (struct value *, CORE_ADDR *);
14f9c5c9 137
d2e4a39e 138static struct value *convert_actual (struct value *, struct type *,
4c4b4cd2 139 CORE_ADDR *);
14f9c5c9 140
d2e4a39e 141static struct value *make_array_descriptor (struct type *, struct value *,
4c4b4cd2 142 CORE_ADDR *);
14f9c5c9 143
4c4b4cd2 144static void ada_add_block_symbols (struct obstack *,
76a01679 145 struct block *, const char *,
4c4b4cd2 146 domain_enum, struct objfile *,
76a01679 147 struct symtab *, int);
14f9c5c9 148
4c4b4cd2 149static int is_nonfunction (struct ada_symbol_info *, int);
14f9c5c9 150
76a01679
JB
151static void add_defn_to_vec (struct obstack *, struct symbol *,
152 struct block *, struct symtab *);
14f9c5c9 153
4c4b4cd2
PH
154static int num_defns_collected (struct obstack *);
155
156static struct ada_symbol_info *defns_collected (struct obstack *, int);
14f9c5c9 157
d2e4a39e 158static struct partial_symbol *ada_lookup_partial_symbol (struct partial_symtab
76a01679
JB
159 *, const char *, int,
160 domain_enum, int);
14f9c5c9 161
d2e4a39e 162static struct symtab *symtab_for_sym (struct symbol *);
14f9c5c9 163
4c4b4cd2 164static struct value *resolve_subexp (struct expression **, int *, int,
76a01679 165 struct type *);
14f9c5c9 166
d2e4a39e 167static void replace_operator_with_call (struct expression **, int, int, int,
4c4b4cd2 168 struct symbol *, struct block *);
14f9c5c9 169
d2e4a39e 170static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 171
4c4b4cd2
PH
172static char *ada_op_name (enum exp_opcode);
173
174static const char *ada_decoded_op_name (enum exp_opcode);
14f9c5c9 175
d2e4a39e 176static int numeric_type_p (struct type *);
14f9c5c9 177
d2e4a39e 178static int integer_type_p (struct type *);
14f9c5c9 179
d2e4a39e 180static int scalar_type_p (struct type *);
14f9c5c9 181
d2e4a39e 182static int discrete_type_p (struct type *);
14f9c5c9 183
4c4b4cd2 184static struct type *ada_lookup_struct_elt_type (struct type *, char *,
76a01679 185 int, int, int *);
4c4b4cd2 186
d2e4a39e 187static char *extended_canonical_line_spec (struct symtab_and_line,
4c4b4cd2 188 const char *);
14f9c5c9 189
d2e4a39e 190static struct value *evaluate_subexp (struct type *, struct expression *,
4c4b4cd2 191 int *, enum noside);
14f9c5c9 192
d2e4a39e 193static struct value *evaluate_subexp_type (struct expression *, int *);
14f9c5c9 194
d2e4a39e 195static struct type *ada_create_fundamental_type (struct objfile *, int);
14f9c5c9 196
d2e4a39e 197static int is_dynamic_field (struct type *, int);
14f9c5c9 198
d2e4a39e 199static struct type *to_fixed_variant_branch_type (struct type *, char *,
4c4b4cd2
PH
200 CORE_ADDR, struct value *);
201
202static struct type *to_fixed_array_type (struct type *, struct value *, int);
14f9c5c9 203
d2e4a39e 204static struct type *to_fixed_range_type (char *, struct value *,
4c4b4cd2 205 struct objfile *);
14f9c5c9 206
d2e4a39e 207static struct type *to_static_fixed_type (struct type *);
14f9c5c9 208
d2e4a39e 209static struct value *unwrap_value (struct value *);
14f9c5c9 210
d2e4a39e 211static struct type *packed_array_type (struct type *, long *);
14f9c5c9 212
d2e4a39e 213static struct type *decode_packed_array_type (struct type *);
14f9c5c9 214
d2e4a39e 215static struct value *decode_packed_array (struct value *);
14f9c5c9 216
d2e4a39e 217static struct value *value_subscript_packed (struct value *, int,
4c4b4cd2 218 struct value **);
14f9c5c9 219
4c4b4cd2
PH
220static struct value *coerce_unspec_val_to_type (struct value *,
221 struct type *);
14f9c5c9 222
d2e4a39e 223static struct value *get_var_value (char *, char *);
14f9c5c9 224
d2e4a39e 225static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 226
d2e4a39e 227static int equiv_types (struct type *, struct type *);
14f9c5c9 228
d2e4a39e 229static int is_name_suffix (const char *);
14f9c5c9 230
d2e4a39e 231static int wild_match (const char *, int, const char *);
14f9c5c9 232
76a01679 233static struct symtabs_and_lines
4c4b4cd2 234find_sal_from_funcs_and_line (const char *, int,
76a01679 235 struct ada_symbol_info *, int);
14f9c5c9 236
76a01679
JB
237static int find_line_in_linetable (struct linetable *, int,
238 struct ada_symbol_info *, int, int *);
14f9c5c9 239
d2e4a39e 240static int find_next_line_in_linetable (struct linetable *, int, int, int);
14f9c5c9 241
d2e4a39e 242static void read_all_symtabs (const char *);
14f9c5c9 243
d2e4a39e 244static int is_plausible_func_for_line (struct symbol *, int);
14f9c5c9 245
d2e4a39e 246static struct value *ada_coerce_ref (struct value *);
14f9c5c9 247
4c4b4cd2
PH
248static LONGEST pos_atr (struct value *);
249
d2e4a39e 250static struct value *value_pos_atr (struct value *);
14f9c5c9 251
d2e4a39e 252static struct value *value_val_atr (struct type *, struct value *);
14f9c5c9 253
4c4b4cd2
PH
254static struct symbol *standard_lookup (const char *, const struct block *,
255 domain_enum);
14f9c5c9 256
4c4b4cd2
PH
257static struct value *ada_search_struct_field (char *, struct value *, int,
258 struct type *);
259
260static struct value *ada_value_primitive_field (struct value *, int, int,
261 struct type *);
262
76a01679
JB
263static int find_struct_field (char *, struct type *, int,
264 struct type **, int *, int *, int *);
4c4b4cd2
PH
265
266static struct value *ada_to_fixed_value_create (struct type *, CORE_ADDR,
267 struct value *);
268
269static struct value *ada_to_fixed_value (struct value *);
14f9c5c9 270
4c4b4cd2 271static void adjust_pc_past_prologue (CORE_ADDR *);
d2e4a39e 272
4c4b4cd2
PH
273static int ada_resolve_function (struct ada_symbol_info *, int,
274 struct value **, int, const char *,
275 struct type *);
276
277static struct value *ada_coerce_to_simple_array (struct value *);
278
279static int ada_is_direct_array_type (struct type *);
280
281static void error_breakpoint_runtime_sym_not_found (const char *err_desc);
282
76a01679 283static int is_runtime_sym_defined (const char *name, int allow_tramp);
4c4b4cd2
PH
284\f
285
76a01679 286
4c4b4cd2 287/* Maximum-sized dynamic type. */
14f9c5c9
AS
288static unsigned int varsize_limit;
289
4c4b4cd2
PH
290/* FIXME: brobecker/2003-09-17: No longer a const because it is
291 returned by a function that does not return a const char *. */
292static char *ada_completer_word_break_characters =
293#ifdef VMS
294 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
295#else
14f9c5c9 296 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
4c4b4cd2 297#endif
14f9c5c9 298
4c4b4cd2 299/* The name of the symbol to use to get the name of the main subprogram. */
76a01679 300static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
4c4b4cd2 301 = "__gnat_ada_main_program_name";
14f9c5c9 302
4c4b4cd2
PH
303/* The name of the runtime function called when an exception is raised. */
304static const char raise_sym_name[] = "__gnat_raise_nodefer_with_msg";
14f9c5c9 305
4c4b4cd2
PH
306/* The name of the runtime function called when an unhandled exception
307 is raised. */
308static const char raise_unhandled_sym_name[] = "__gnat_unhandled_exception";
309
310/* The name of the runtime function called when an assert failure is
311 raised. */
312static const char raise_assert_sym_name[] =
313 "system__assertions__raise_assert_failure";
314
315/* When GDB stops on an unhandled exception, GDB will go up the stack until
316 if finds a frame corresponding to this function, in order to extract the
317 name of the exception that has been raised from one of the parameters. */
318static const char process_raise_exception_name[] =
319 "ada__exceptions__process_raise_exception";
320
321/* A string that reflects the longest exception expression rewrite,
322 aside from the exception name. */
323static const char longest_exception_template[] =
324 "'__gnat_raise_nodefer_with_msg' if long_integer(e) = long_integer(&)";
325
326/* Limit on the number of warnings to raise per expression evaluation. */
327static int warning_limit = 2;
328
329/* Number of warning messages issued; reset to 0 by cleanups after
330 expression evaluation. */
331static int warnings_issued = 0;
332
333static const char *known_runtime_file_name_patterns[] = {
334 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
335};
336
337static const char *known_auxiliary_function_name_patterns[] = {
338 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
339};
340
341/* Space for allocating results of ada_lookup_symbol_list. */
342static struct obstack symbol_list_obstack;
343
344 /* Utilities */
345
96d887e8
PH
346#ifdef GNAT_GDB
347
4c4b4cd2
PH
348/* Create a new empty string_vector struct with an initial size of
349 INITIAL_SIZE. */
350
351static struct string_vector
352xnew_string_vector (int initial_size)
353{
354 struct string_vector result;
76a01679 355
4c4b4cd2
PH
356 result.array = (char **) xmalloc ((initial_size + 1) * sizeof (char *));
357 result.index = 0;
358 result.size = initial_size;
359
360 return result;
361}
362
363/* Add STR at the end of the given string vector SV. If SV is already
364 full, its size is automatically increased (doubled). */
365
366static void
367string_vector_append (struct string_vector *sv, char *str)
368{
369 if (sv->index >= sv->size)
370 GROW_VECT (sv->array, sv->size, sv->size * 2);
371
372 sv->array[sv->index] = str;
373 sv->index++;
374}
375
376/* Given DECODED_NAME a string holding a symbol name in its
377 decoded form (ie using the Ada dotted notation), returns
378 its unqualified name. */
379
380static const char *
381ada_unqualified_name (const char *decoded_name)
382{
383 const char *result = strrchr (decoded_name, '.');
384
385 if (result != NULL)
76a01679 386 result++; /* Skip the dot... */
4c4b4cd2
PH
387 else
388 result = decoded_name;
76a01679 389
4c4b4cd2
PH
390 return result;
391}
392
393/* Return a string starting with '<', followed by STR, and '>'.
394 The result is good until the next call. */
395
396static char *
397add_angle_brackets (const char *str)
398{
399 static char *result = NULL;
400
401 xfree (result);
402 result = (char *) xmalloc ((strlen (str) + 3) * sizeof (char));
403
404 sprintf (result, "<%s>", str);
405 return result;
406}
407
96d887e8
PH
408#endif /* GNAT_GDB */
409
4c4b4cd2
PH
410static char *
411ada_get_gdb_completer_word_break_characters (void)
412{
413 return ada_completer_word_break_characters;
414}
415
416/* Read the string located at ADDR from the inferior and store the
417 result into BUF. */
418
419static void
14f9c5c9
AS
420extract_string (CORE_ADDR addr, char *buf)
421{
d2e4a39e 422 int char_index = 0;
14f9c5c9 423
4c4b4cd2
PH
424 /* Loop, reading one byte at a time, until we reach the '\000'
425 end-of-string marker. */
d2e4a39e
AS
426 do
427 {
428 target_read_memory (addr + char_index * sizeof (char),
4c4b4cd2 429 buf + char_index * sizeof (char), sizeof (char));
d2e4a39e
AS
430 char_index++;
431 }
432 while (buf[char_index - 1] != '\000');
14f9c5c9
AS
433}
434
4c4b4cd2
PH
435/* Return the name of the function owning the instruction located at PC.
436 Return NULL if no such function could be found. */
437
438static char *
439function_name_from_pc (CORE_ADDR pc)
440{
441 char *func_name;
442
443 if (!find_pc_partial_function (pc, &func_name, NULL, NULL))
444 return NULL;
445
446 return func_name;
447}
448
14f9c5c9
AS
449/* Assuming *OLD_VECT points to an array of *SIZE objects of size
450 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
4c4b4cd2 451 updating *OLD_VECT and *SIZE as necessary. */
14f9c5c9
AS
452
453void
d2e4a39e 454grow_vect (void **old_vect, size_t * size, size_t min_size, int element_size)
14f9c5c9 455{
d2e4a39e
AS
456 if (*size < min_size)
457 {
458 *size *= 2;
459 if (*size < min_size)
4c4b4cd2 460 *size = min_size;
d2e4a39e
AS
461 *old_vect = xrealloc (*old_vect, *size * element_size);
462 }
14f9c5c9
AS
463}
464
465/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
4c4b4cd2 466 suffix of FIELD_NAME beginning "___". */
14f9c5c9
AS
467
468static int
ebf56fd3 469field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
470{
471 int len = strlen (target);
d2e4a39e 472 return
4c4b4cd2
PH
473 (strncmp (field_name, target, len) == 0
474 && (field_name[len] == '\0'
475 || (strncmp (field_name + len, "___", 3) == 0
76a01679
JB
476 && strcmp (field_name + strlen (field_name) - 6,
477 "___XVN") != 0)));
14f9c5c9
AS
478}
479
480
4c4b4cd2
PH
481/* Assuming TYPE is a TYPE_CODE_STRUCT, find the field whose name matches
482 FIELD_NAME, and return its index. This function also handles fields
483 whose name have ___ suffixes because the compiler sometimes alters
484 their name by adding such a suffix to represent fields with certain
485 constraints. If the field could not be found, return a negative
486 number if MAYBE_MISSING is set. Otherwise raise an error. */
487
488int
489ada_get_field_index (const struct type *type, const char *field_name,
490 int maybe_missing)
491{
492 int fieldno;
493 for (fieldno = 0; fieldno < TYPE_NFIELDS (type); fieldno++)
494 if (field_name_match (TYPE_FIELD_NAME (type, fieldno), field_name))
495 return fieldno;
496
497 if (!maybe_missing)
498 error ("Unable to find field %s in struct %s. Aborting",
499 field_name, TYPE_NAME (type));
500
501 return -1;
502}
503
504/* The length of the prefix of NAME prior to any "___" suffix. */
14f9c5c9
AS
505
506int
d2e4a39e 507ada_name_prefix_len (const char *name)
14f9c5c9
AS
508{
509 if (name == NULL)
510 return 0;
d2e4a39e 511 else
14f9c5c9 512 {
d2e4a39e 513 const char *p = strstr (name, "___");
14f9c5c9 514 if (p == NULL)
4c4b4cd2 515 return strlen (name);
14f9c5c9 516 else
4c4b4cd2 517 return p - name;
14f9c5c9
AS
518 }
519}
520
4c4b4cd2
PH
521/* Return non-zero if SUFFIX is a suffix of STR.
522 Return zero if STR is null. */
523
14f9c5c9 524static int
d2e4a39e 525is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
526{
527 int len1, len2;
528 if (str == NULL)
529 return 0;
530 len1 = strlen (str);
531 len2 = strlen (suffix);
4c4b4cd2 532 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
14f9c5c9
AS
533}
534
535/* Create a value of type TYPE whose contents come from VALADDR, if it
4c4b4cd2
PH
536 is non-null, and whose memory address (in the inferior) is
537 ADDRESS. */
538
d2e4a39e
AS
539struct value *
540value_from_contents_and_address (struct type *type, char *valaddr,
4c4b4cd2 541 CORE_ADDR address)
14f9c5c9 542{
d2e4a39e
AS
543 struct value *v = allocate_value (type);
544 if (valaddr == NULL)
14f9c5c9
AS
545 VALUE_LAZY (v) = 1;
546 else
547 memcpy (VALUE_CONTENTS_RAW (v), valaddr, TYPE_LENGTH (type));
548 VALUE_ADDRESS (v) = address;
549 if (address != 0)
550 VALUE_LVAL (v) = lval_memory;
551 return v;
552}
553
4c4b4cd2
PH
554/* The contents of value VAL, treated as a value of type TYPE. The
555 result is an lval in memory if VAL is. */
14f9c5c9 556
d2e4a39e 557static struct value *
4c4b4cd2 558coerce_unspec_val_to_type (struct value *val, struct type *type)
14f9c5c9
AS
559{
560 CHECK_TYPEDEF (type);
4c4b4cd2
PH
561 if (VALUE_TYPE (val) == type)
562 return val;
d2e4a39e 563 else
14f9c5c9 564 {
4c4b4cd2
PH
565 struct value *result;
566
567 /* Make sure that the object size is not unreasonable before
568 trying to allocate some memory for it. */
569 if (TYPE_LENGTH (type) > varsize_limit)
570 error ("object size is larger than varsize-limit");
571
572 result = allocate_value (type);
573 VALUE_LVAL (result) = VALUE_LVAL (val);
574 VALUE_BITSIZE (result) = VALUE_BITSIZE (val);
575 VALUE_BITPOS (result) = VALUE_BITPOS (val);
576 VALUE_ADDRESS (result) = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
1265e4aa
JB
577 if (VALUE_LAZY (val)
578 || TYPE_LENGTH (type) > TYPE_LENGTH (VALUE_TYPE (val)))
4c4b4cd2 579 VALUE_LAZY (result) = 1;
d2e4a39e 580 else
4c4b4cd2
PH
581 memcpy (VALUE_CONTENTS_RAW (result), VALUE_CONTENTS (val),
582 TYPE_LENGTH (type));
14f9c5c9
AS
583 return result;
584 }
585}
586
d2e4a39e
AS
587static char *
588cond_offset_host (char *valaddr, long offset)
14f9c5c9
AS
589{
590 if (valaddr == NULL)
591 return NULL;
592 else
593 return valaddr + offset;
594}
595
596static CORE_ADDR
ebf56fd3 597cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
598{
599 if (address == 0)
600 return 0;
d2e4a39e 601 else
14f9c5c9
AS
602 return address + offset;
603}
604
4c4b4cd2
PH
605/* Issue a warning (as for the definition of warning in utils.c, but
606 with exactly one argument rather than ...), unless the limit on the
607 number of warnings has passed during the evaluation of the current
608 expression. */
14f9c5c9 609static void
4c4b4cd2 610lim_warning (const char *format, long arg)
14f9c5c9 611{
4c4b4cd2
PH
612 warnings_issued += 1;
613 if (warnings_issued <= warning_limit)
614 warning (format, arg);
615}
616
617static const char *
618ada_translate_error_message (const char *string)
619{
620 if (strcmp (string, "Invalid cast.") == 0)
621 return "Invalid type conversion.";
622 else
623 return string;
624}
625
626static LONGEST
76a01679 627MAX_OF_SIZE (int size)
4c4b4cd2 628{
76a01679
JB
629 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
630 return top_bit | (top_bit - 1);
4c4b4cd2
PH
631}
632
633static LONGEST
634MIN_OF_SIZE (int size)
635{
76a01679 636 return -MAX_OF_SIZE (size) - 1;
4c4b4cd2
PH
637}
638
639static ULONGEST
640UMAX_OF_SIZE (int size)
641{
76a01679
JB
642 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
643 return top_bit | (top_bit - 1);
4c4b4cd2
PH
644}
645
646static ULONGEST
647UMIN_OF_SIZE (int size)
648{
649 return 0;
650}
651
652/* The largest value in the domain of TYPE, a discrete type, as an integer. */
653static struct value *
654discrete_type_high_bound (struct type *type)
655{
76a01679 656 switch (TYPE_CODE (type))
4c4b4cd2
PH
657 {
658 case TYPE_CODE_RANGE:
659 return value_from_longest (TYPE_TARGET_TYPE (type),
76a01679 660 TYPE_HIGH_BOUND (type));
4c4b4cd2 661 case TYPE_CODE_ENUM:
76a01679
JB
662 return
663 value_from_longest (type,
664 TYPE_FIELD_BITPOS (type,
665 TYPE_NFIELDS (type) - 1));
666 case TYPE_CODE_INT:
4c4b4cd2
PH
667 return value_from_longest (type, MAX_OF_TYPE (type));
668 default:
669 error ("Unexpected type in discrete_type_high_bound.");
670 }
671}
672
673/* The largest value in the domain of TYPE, a discrete type, as an integer. */
674static struct value *
675discrete_type_low_bound (struct type *type)
676{
76a01679 677 switch (TYPE_CODE (type))
4c4b4cd2
PH
678 {
679 case TYPE_CODE_RANGE:
680 return value_from_longest (TYPE_TARGET_TYPE (type),
76a01679 681 TYPE_LOW_BOUND (type));
4c4b4cd2 682 case TYPE_CODE_ENUM:
76a01679
JB
683 return value_from_longest (type, TYPE_FIELD_BITPOS (type, 0));
684 case TYPE_CODE_INT:
4c4b4cd2
PH
685 return value_from_longest (type, MIN_OF_TYPE (type));
686 default:
687 error ("Unexpected type in discrete_type_low_bound.");
688 }
689}
690
691/* The identity on non-range types. For range types, the underlying
76a01679 692 non-range scalar type. */
4c4b4cd2
PH
693
694static struct type *
695base_type (struct type *type)
696{
697 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
698 {
76a01679
JB
699 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
700 return type;
4c4b4cd2
PH
701 type = TYPE_TARGET_TYPE (type);
702 }
703 return type;
14f9c5c9 704}
4c4b4cd2 705\f
76a01679 706
4c4b4cd2 707 /* Language Selection */
14f9c5c9
AS
708
709/* If the main program is in Ada, return language_ada, otherwise return LANG
710 (the main program is in Ada iif the adainit symbol is found).
711
4c4b4cd2 712 MAIN_PST is not used. */
d2e4a39e 713
14f9c5c9 714enum language
d2e4a39e 715ada_update_initial_language (enum language lang,
4c4b4cd2 716 struct partial_symtab *main_pst)
14f9c5c9 717{
d2e4a39e 718 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
4c4b4cd2
PH
719 (struct objfile *) NULL) != NULL)
720 return language_ada;
14f9c5c9
AS
721
722 return lang;
723}
96d887e8
PH
724
725/* If the main procedure is written in Ada, then return its name.
726 The result is good until the next call. Return NULL if the main
727 procedure doesn't appear to be in Ada. */
728
729char *
730ada_main_name (void)
731{
732 struct minimal_symbol *msym;
733 CORE_ADDR main_program_name_addr;
734 static char main_program_name[1024];
735 /* For Ada, the name of the main procedure is stored in a specific
736 string constant, generated by the binder. Look for that symbol,
737 extract its address, and then read that string. If we didn't find
738 that string, then most probably the main procedure is not written
739 in Ada. */
740 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
741
742 if (msym != NULL)
743 {
744 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
745 if (main_program_name_addr == 0)
746 error ("Invalid address for Ada main program name.");
747
748 extract_string (main_program_name_addr, main_program_name);
749 return main_program_name;
750 }
751
752 /* The main procedure doesn't seem to be in Ada. */
753 return NULL;
754}
14f9c5c9 755\f
4c4b4cd2 756 /* Symbols */
d2e4a39e 757
4c4b4cd2
PH
758/* Table of Ada operators and their GNAT-encoded names. Last entry is pair
759 of NULLs. */
14f9c5c9 760
d2e4a39e
AS
761const struct ada_opname_map ada_opname_table[] = {
762 {"Oadd", "\"+\"", BINOP_ADD},
763 {"Osubtract", "\"-\"", BINOP_SUB},
764 {"Omultiply", "\"*\"", BINOP_MUL},
765 {"Odivide", "\"/\"", BINOP_DIV},
766 {"Omod", "\"mod\"", BINOP_MOD},
767 {"Orem", "\"rem\"", BINOP_REM},
768 {"Oexpon", "\"**\"", BINOP_EXP},
769 {"Olt", "\"<\"", BINOP_LESS},
770 {"Ole", "\"<=\"", BINOP_LEQ},
771 {"Ogt", "\">\"", BINOP_GTR},
772 {"Oge", "\">=\"", BINOP_GEQ},
773 {"Oeq", "\"=\"", BINOP_EQUAL},
774 {"One", "\"/=\"", BINOP_NOTEQUAL},
775 {"Oand", "\"and\"", BINOP_BITWISE_AND},
776 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
777 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
778 {"Oconcat", "\"&\"", BINOP_CONCAT},
779 {"Oabs", "\"abs\"", UNOP_ABS},
780 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
781 {"Oadd", "\"+\"", UNOP_PLUS},
782 {"Osubtract", "\"-\"", UNOP_NEG},
783 {NULL, NULL}
14f9c5c9
AS
784};
785
4c4b4cd2
PH
786/* Return non-zero if STR should be suppressed in info listings. */
787
14f9c5c9 788static int
d2e4a39e 789is_suppressed_name (const char *str)
14f9c5c9 790{
4c4b4cd2 791 if (strncmp (str, "_ada_", 5) == 0)
14f9c5c9
AS
792 str += 5;
793 if (str[0] == '_' || str[0] == '\000')
794 return 1;
795 else
796 {
d2e4a39e
AS
797 const char *p;
798 const char *suffix = strstr (str, "___");
14f9c5c9 799 if (suffix != NULL && suffix[3] != 'X')
4c4b4cd2 800 return 1;
14f9c5c9 801 if (suffix == NULL)
4c4b4cd2 802 suffix = str + strlen (str);
d2e4a39e 803 for (p = suffix - 1; p != str; p -= 1)
4c4b4cd2
PH
804 if (isupper (*p))
805 {
806 int i;
807 if (p[0] == 'X' && p[-1] != '_')
808 goto OK;
809 if (*p != 'O')
810 return 1;
811 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
812 if (strncmp (ada_opname_table[i].encoded, p,
813 strlen (ada_opname_table[i].encoded)) == 0)
814 goto OK;
815 return 1;
816 OK:;
817 }
14f9c5c9
AS
818 return 0;
819 }
820}
821
4c4b4cd2
PH
822/* The "encoded" form of DECODED, according to GNAT conventions.
823 The result is valid until the next call to ada_encode. */
824
14f9c5c9 825char *
4c4b4cd2 826ada_encode (const char *decoded)
14f9c5c9 827{
4c4b4cd2
PH
828 static char *encoding_buffer = NULL;
829 static size_t encoding_buffer_size = 0;
d2e4a39e 830 const char *p;
14f9c5c9 831 int k;
d2e4a39e 832
4c4b4cd2 833 if (decoded == NULL)
14f9c5c9
AS
834 return NULL;
835
4c4b4cd2
PH
836 GROW_VECT (encoding_buffer, encoding_buffer_size,
837 2 * strlen (decoded) + 10);
14f9c5c9
AS
838
839 k = 0;
4c4b4cd2 840 for (p = decoded; *p != '\0'; p += 1)
14f9c5c9 841 {
4c4b4cd2
PH
842 if (!ADA_RETAIN_DOTS && *p == '.')
843 {
844 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
845 k += 2;
846 }
14f9c5c9 847 else if (*p == '"')
4c4b4cd2
PH
848 {
849 const struct ada_opname_map *mapping;
850
851 for (mapping = ada_opname_table;
1265e4aa
JB
852 mapping->encoded != NULL
853 && strncmp (mapping->decoded, p,
854 strlen (mapping->decoded)) != 0; mapping += 1)
4c4b4cd2
PH
855 ;
856 if (mapping->encoded == NULL)
857 error ("invalid Ada operator name: %s", p);
858 strcpy (encoding_buffer + k, mapping->encoded);
859 k += strlen (mapping->encoded);
860 break;
861 }
d2e4a39e 862 else
4c4b4cd2
PH
863 {
864 encoding_buffer[k] = *p;
865 k += 1;
866 }
14f9c5c9
AS
867 }
868
4c4b4cd2
PH
869 encoding_buffer[k] = '\0';
870 return encoding_buffer;
14f9c5c9
AS
871}
872
873/* Return NAME folded to lower case, or, if surrounded by single
4c4b4cd2
PH
874 quotes, unfolded, but with the quotes stripped away. Result good
875 to next call. */
876
d2e4a39e
AS
877char *
878ada_fold_name (const char *name)
14f9c5c9 879{
d2e4a39e 880 static char *fold_buffer = NULL;
14f9c5c9
AS
881 static size_t fold_buffer_size = 0;
882
883 int len = strlen (name);
d2e4a39e 884 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
14f9c5c9
AS
885
886 if (name[0] == '\'')
887 {
d2e4a39e
AS
888 strncpy (fold_buffer, name + 1, len - 2);
889 fold_buffer[len - 2] = '\000';
14f9c5c9
AS
890 }
891 else
892 {
893 int i;
894 for (i = 0; i <= len; i += 1)
4c4b4cd2 895 fold_buffer[i] = tolower (name[i]);
14f9c5c9
AS
896 }
897
898 return fold_buffer;
899}
900
4c4b4cd2
PH
901/* decode:
902 0. Discard trailing .{DIGIT}+ or trailing ___{DIGIT}+
903 These are suffixes introduced by GNAT5 to nested subprogram
904 names, and do not serve any purpose for the debugger.
905 1. Discard final __{DIGIT}+ or $({DIGIT}+(__{DIGIT}+)*)
14f9c5c9
AS
906 2. Convert other instances of embedded "__" to `.'.
907 3. Discard leading _ada_.
908 4. Convert operator names to the appropriate quoted symbols.
4c4b4cd2 909 5. Remove everything after first ___ if it is followed by
14f9c5c9
AS
910 'X'.
911 6. Replace TK__ with __, and a trailing B or TKB with nothing.
912 7. Put symbols that should be suppressed in <...> brackets.
913 8. Remove trailing X[bn]* suffix (indicating names in package bodies).
14f9c5c9 914
4c4b4cd2
PH
915 The resulting string is valid until the next call of ada_decode.
916 If the string is unchanged by demangling, the original string pointer
917 is returned. */
918
919const char *
920ada_decode (const char *encoded)
14f9c5c9
AS
921{
922 int i, j;
923 int len0;
d2e4a39e 924 const char *p;
4c4b4cd2 925 char *decoded;
14f9c5c9 926 int at_start_name;
4c4b4cd2
PH
927 static char *decoding_buffer = NULL;
928 static size_t decoding_buffer_size = 0;
d2e4a39e 929
4c4b4cd2
PH
930 if (strncmp (encoded, "_ada_", 5) == 0)
931 encoded += 5;
14f9c5c9 932
4c4b4cd2 933 if (encoded[0] == '_' || encoded[0] == '<')
14f9c5c9
AS
934 goto Suppress;
935
4c4b4cd2
PH
936 /* Remove trailing .{DIGIT}+ or ___{DIGIT}+. */
937 len0 = strlen (encoded);
938 if (len0 > 1 && isdigit (encoded[len0 - 1]))
939 {
940 i = len0 - 2;
941 while (i > 0 && isdigit (encoded[i]))
942 i--;
943 if (i >= 0 && encoded[i] == '.')
944 len0 = i;
945 else if (i >= 2 && strncmp (encoded + i - 2, "___", 3) == 0)
946 len0 = i - 2;
947 }
948
949 /* Remove the ___X.* suffix if present. Do not forget to verify that
950 the suffix is located before the current "end" of ENCODED. We want
951 to avoid re-matching parts of ENCODED that have previously been
952 marked as discarded (by decrementing LEN0). */
953 p = strstr (encoded, "___");
954 if (p != NULL && p - encoded < len0 - 3)
14f9c5c9
AS
955 {
956 if (p[3] == 'X')
4c4b4cd2 957 len0 = p - encoded;
14f9c5c9 958 else
4c4b4cd2 959 goto Suppress;
14f9c5c9 960 }
4c4b4cd2
PH
961
962 if (len0 > 3 && strncmp (encoded + len0 - 3, "TKB", 3) == 0)
14f9c5c9 963 len0 -= 3;
76a01679 964
4c4b4cd2 965 if (len0 > 1 && strncmp (encoded + len0 - 1, "B", 1) == 0)
14f9c5c9
AS
966 len0 -= 1;
967
4c4b4cd2
PH
968 /* Make decoded big enough for possible expansion by operator name. */
969 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
970 decoded = decoding_buffer;
14f9c5c9 971
4c4b4cd2 972 if (len0 > 1 && isdigit (encoded[len0 - 1]))
d2e4a39e 973 {
4c4b4cd2
PH
974 i = len0 - 2;
975 while ((i >= 0 && isdigit (encoded[i]))
976 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
977 i -= 1;
978 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
979 len0 = i - 1;
980 else if (encoded[i] == '$')
981 len0 = i;
d2e4a39e 982 }
14f9c5c9 983
4c4b4cd2
PH
984 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
985 decoded[j] = encoded[i];
14f9c5c9
AS
986
987 at_start_name = 1;
988 while (i < len0)
989 {
4c4b4cd2
PH
990 if (at_start_name && encoded[i] == 'O')
991 {
992 int k;
993 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
994 {
995 int op_len = strlen (ada_opname_table[k].encoded);
06d5cf63
JB
996 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
997 op_len - 1) == 0)
998 && !isalnum (encoded[i + op_len]))
4c4b4cd2
PH
999 {
1000 strcpy (decoded + j, ada_opname_table[k].decoded);
1001 at_start_name = 0;
1002 i += op_len;
1003 j += strlen (ada_opname_table[k].decoded);
1004 break;
1005 }
1006 }
1007 if (ada_opname_table[k].encoded != NULL)
1008 continue;
1009 }
14f9c5c9
AS
1010 at_start_name = 0;
1011
4c4b4cd2
PH
1012 if (i < len0 - 4 && strncmp (encoded + i, "TK__", 4) == 0)
1013 i += 2;
1014 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
1015 {
1016 do
1017 i += 1;
1018 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1019 if (i < len0)
1020 goto Suppress;
1021 }
1022 else if (!ADA_RETAIN_DOTS
1023 && i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
1024 {
1025 decoded[j] = '.';
1026 at_start_name = 1;
1027 i += 2;
1028 j += 1;
1029 }
14f9c5c9 1030 else
4c4b4cd2
PH
1031 {
1032 decoded[j] = encoded[i];
1033 i += 1;
1034 j += 1;
1035 }
14f9c5c9 1036 }
4c4b4cd2 1037 decoded[j] = '\000';
14f9c5c9 1038
4c4b4cd2
PH
1039 for (i = 0; decoded[i] != '\0'; i += 1)
1040 if (isupper (decoded[i]) || decoded[i] == ' ')
14f9c5c9
AS
1041 goto Suppress;
1042
4c4b4cd2
PH
1043 if (strcmp (decoded, encoded) == 0)
1044 return encoded;
1045 else
1046 return decoded;
14f9c5c9
AS
1047
1048Suppress:
4c4b4cd2
PH
1049 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
1050 decoded = decoding_buffer;
1051 if (encoded[0] == '<')
1052 strcpy (decoded, encoded);
14f9c5c9 1053 else
4c4b4cd2
PH
1054 sprintf (decoded, "<%s>", encoded);
1055 return decoded;
1056
1057}
1058
1059/* Table for keeping permanent unique copies of decoded names. Once
1060 allocated, names in this table are never released. While this is a
1061 storage leak, it should not be significant unless there are massive
1062 changes in the set of decoded names in successive versions of a
1063 symbol table loaded during a single session. */
1064static struct htab *decoded_names_store;
1065
1066/* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1067 in the language-specific part of GSYMBOL, if it has not been
1068 previously computed. Tries to save the decoded name in the same
1069 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1070 in any case, the decoded symbol has a lifetime at least that of
1071 GSYMBOL).
1072 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1073 const, but nevertheless modified to a semantically equivalent form
1074 when a decoded name is cached in it.
76a01679 1075*/
4c4b4cd2 1076
76a01679
JB
1077char *
1078ada_decode_symbol (const struct general_symbol_info *gsymbol)
4c4b4cd2 1079{
76a01679 1080 char **resultp =
4c4b4cd2
PH
1081 (char **) &gsymbol->language_specific.cplus_specific.demangled_name;
1082 if (*resultp == NULL)
1083 {
1084 const char *decoded = ada_decode (gsymbol->name);
1085 if (gsymbol->bfd_section != NULL)
76a01679
JB
1086 {
1087 bfd *obfd = gsymbol->bfd_section->owner;
1088 if (obfd != NULL)
1089 {
1090 struct objfile *objf;
1091 ALL_OBJFILES (objf)
1092 {
1093 if (obfd == objf->obfd)
1094 {
1095 *resultp = obsavestring (decoded, strlen (decoded),
1096 &objf->objfile_obstack);
1097 break;
1098 }
1099 }
1100 }
1101 }
4c4b4cd2 1102 /* Sometimes, we can't find a corresponding objfile, in which
76a01679
JB
1103 case, we put the result on the heap. Since we only decode
1104 when needed, we hope this usually does not cause a
1105 significant memory leak (FIXME). */
4c4b4cd2 1106 if (*resultp == NULL)
76a01679
JB
1107 {
1108 char **slot = (char **) htab_find_slot (decoded_names_store,
1109 decoded, INSERT);
1110 if (*slot == NULL)
1111 *slot = xstrdup (decoded);
1112 *resultp = *slot;
1113 }
4c4b4cd2 1114 }
14f9c5c9 1115
4c4b4cd2
PH
1116 return *resultp;
1117}
76a01679
JB
1118
1119char *
1120ada_la_decode (const char *encoded, int options)
4c4b4cd2
PH
1121{
1122 return xstrdup (ada_decode (encoded));
14f9c5c9
AS
1123}
1124
1125/* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
4c4b4cd2
PH
1126 suffixes that encode debugging information or leading _ada_ on
1127 SYM_NAME (see is_name_suffix commentary for the debugging
1128 information that is ignored). If WILD, then NAME need only match a
1129 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1130 either argument is NULL. */
14f9c5c9
AS
1131
1132int
d2e4a39e 1133ada_match_name (const char *sym_name, const char *name, int wild)
14f9c5c9
AS
1134{
1135 if (sym_name == NULL || name == NULL)
1136 return 0;
1137 else if (wild)
1138 return wild_match (name, strlen (name), sym_name);
d2e4a39e
AS
1139 else
1140 {
1141 int len_name = strlen (name);
4c4b4cd2
PH
1142 return (strncmp (sym_name, name, len_name) == 0
1143 && is_name_suffix (sym_name + len_name))
1144 || (strncmp (sym_name, "_ada_", 5) == 0
1145 && strncmp (sym_name + 5, name, len_name) == 0
1146 && is_name_suffix (sym_name + len_name + 5));
d2e4a39e 1147 }
14f9c5c9
AS
1148}
1149
4c4b4cd2
PH
1150/* True (non-zero) iff, in Ada mode, the symbol SYM should be
1151 suppressed in info listings. */
14f9c5c9
AS
1152
1153int
ebf56fd3 1154ada_suppress_symbol_printing (struct symbol *sym)
14f9c5c9 1155{
176620f1 1156 if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN)
14f9c5c9 1157 return 1;
d2e4a39e 1158 else
4c4b4cd2 1159 return is_suppressed_name (SYMBOL_LINKAGE_NAME (sym));
14f9c5c9 1160}
14f9c5c9 1161\f
d2e4a39e 1162
4c4b4cd2 1163 /* Arrays */
14f9c5c9 1164
4c4b4cd2 1165/* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
14f9c5c9 1166
d2e4a39e
AS
1167static char *bound_name[] = {
1168 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
14f9c5c9
AS
1169 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1170};
1171
1172/* Maximum number of array dimensions we are prepared to handle. */
1173
4c4b4cd2 1174#define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
14f9c5c9 1175
4c4b4cd2 1176/* Like modify_field, but allows bitpos > wordlength. */
14f9c5c9
AS
1177
1178static void
ebf56fd3 1179modify_general_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
14f9c5c9 1180{
4c4b4cd2 1181 modify_field (addr + bitpos / 8, fieldval, bitpos % 8, bitsize);
14f9c5c9
AS
1182}
1183
1184
4c4b4cd2
PH
1185/* The desc_* routines return primitive portions of array descriptors
1186 (fat pointers). */
14f9c5c9
AS
1187
1188/* The descriptor or array type, if any, indicated by TYPE; removes
4c4b4cd2
PH
1189 level of indirection, if needed. */
1190
d2e4a39e
AS
1191static struct type *
1192desc_base_type (struct type *type)
14f9c5c9
AS
1193{
1194 if (type == NULL)
1195 return NULL;
1196 CHECK_TYPEDEF (type);
1265e4aa
JB
1197 if (type != NULL
1198 && (TYPE_CODE (type) == TYPE_CODE_PTR
1199 || TYPE_CODE (type) == TYPE_CODE_REF))
14f9c5c9
AS
1200 return check_typedef (TYPE_TARGET_TYPE (type));
1201 else
1202 return type;
1203}
1204
4c4b4cd2
PH
1205/* True iff TYPE indicates a "thin" array pointer type. */
1206
14f9c5c9 1207static int
d2e4a39e 1208is_thin_pntr (struct type *type)
14f9c5c9 1209{
d2e4a39e 1210 return
14f9c5c9
AS
1211 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1212 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1213}
1214
4c4b4cd2
PH
1215/* The descriptor type for thin pointer type TYPE. */
1216
d2e4a39e
AS
1217static struct type *
1218thin_descriptor_type (struct type *type)
14f9c5c9 1219{
d2e4a39e 1220 struct type *base_type = desc_base_type (type);
14f9c5c9
AS
1221 if (base_type == NULL)
1222 return NULL;
1223 if (is_suffix (ada_type_name (base_type), "___XVE"))
1224 return base_type;
d2e4a39e 1225 else
14f9c5c9 1226 {
d2e4a39e 1227 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
14f9c5c9 1228 if (alt_type == NULL)
4c4b4cd2 1229 return base_type;
14f9c5c9 1230 else
4c4b4cd2 1231 return alt_type;
14f9c5c9
AS
1232 }
1233}
1234
4c4b4cd2
PH
1235/* A pointer to the array data for thin-pointer value VAL. */
1236
d2e4a39e
AS
1237static struct value *
1238thin_data_pntr (struct value *val)
14f9c5c9 1239{
d2e4a39e 1240 struct type *type = VALUE_TYPE (val);
14f9c5c9 1241 if (TYPE_CODE (type) == TYPE_CODE_PTR)
d2e4a39e 1242 return value_cast (desc_data_type (thin_descriptor_type (type)),
4c4b4cd2 1243 value_copy (val));
d2e4a39e 1244 else
14f9c5c9 1245 return value_from_longest (desc_data_type (thin_descriptor_type (type)),
4c4b4cd2 1246 VALUE_ADDRESS (val) + VALUE_OFFSET (val));
14f9c5c9
AS
1247}
1248
4c4b4cd2
PH
1249/* True iff TYPE indicates a "thick" array pointer type. */
1250
14f9c5c9 1251static int
d2e4a39e 1252is_thick_pntr (struct type *type)
14f9c5c9
AS
1253{
1254 type = desc_base_type (type);
1255 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2 1256 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
14f9c5c9
AS
1257}
1258
4c4b4cd2
PH
1259/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1260 pointer to one, the type of its bounds data; otherwise, NULL. */
76a01679 1261
d2e4a39e
AS
1262static struct type *
1263desc_bounds_type (struct type *type)
14f9c5c9 1264{
d2e4a39e 1265 struct type *r;
14f9c5c9
AS
1266
1267 type = desc_base_type (type);
1268
1269 if (type == NULL)
1270 return NULL;
1271 else if (is_thin_pntr (type))
1272 {
1273 type = thin_descriptor_type (type);
1274 if (type == NULL)
4c4b4cd2 1275 return NULL;
14f9c5c9
AS
1276 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1277 if (r != NULL)
4c4b4cd2 1278 return check_typedef (r);
14f9c5c9
AS
1279 }
1280 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1281 {
1282 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1283 if (r != NULL)
4c4b4cd2 1284 return check_typedef (TYPE_TARGET_TYPE (check_typedef (r)));
14f9c5c9
AS
1285 }
1286 return NULL;
1287}
1288
1289/* If ARR is an array descriptor (fat or thin pointer), or pointer to
4c4b4cd2
PH
1290 one, a pointer to its bounds data. Otherwise NULL. */
1291
d2e4a39e
AS
1292static struct value *
1293desc_bounds (struct value *arr)
14f9c5c9 1294{
d2e4a39e
AS
1295 struct type *type = check_typedef (VALUE_TYPE (arr));
1296 if (is_thin_pntr (type))
14f9c5c9 1297 {
d2e4a39e 1298 struct type *bounds_type =
4c4b4cd2 1299 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
1300 LONGEST addr;
1301
1302 if (desc_bounds_type == NULL)
4c4b4cd2 1303 error ("Bad GNAT array descriptor");
14f9c5c9
AS
1304
1305 /* NOTE: The following calculation is not really kosher, but
d2e4a39e 1306 since desc_type is an XVE-encoded type (and shouldn't be),
4c4b4cd2 1307 the correct calculation is a real pain. FIXME (and fix GCC). */
14f9c5c9 1308 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4c4b4cd2 1309 addr = value_as_long (arr);
d2e4a39e 1310 else
4c4b4cd2 1311 addr = VALUE_ADDRESS (arr) + VALUE_OFFSET (arr);
14f9c5c9 1312
d2e4a39e 1313 return
4c4b4cd2
PH
1314 value_from_longest (lookup_pointer_type (bounds_type),
1315 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
1316 }
1317
1318 else if (is_thick_pntr (type))
d2e4a39e 1319 return value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
4c4b4cd2 1320 "Bad GNAT array descriptor");
14f9c5c9
AS
1321 else
1322 return NULL;
1323}
1324
4c4b4cd2
PH
1325/* If TYPE is the type of an array-descriptor (fat pointer), the bit
1326 position of the field containing the address of the bounds data. */
1327
14f9c5c9 1328static int
d2e4a39e 1329fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
1330{
1331 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1332}
1333
1334/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1335 size of the field containing the address of the bounds data. */
1336
14f9c5c9 1337static int
d2e4a39e 1338fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
1339{
1340 type = desc_base_type (type);
1341
d2e4a39e 1342 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
1343 return TYPE_FIELD_BITSIZE (type, 1);
1344 else
1345 return 8 * TYPE_LENGTH (check_typedef (TYPE_FIELD_TYPE (type, 1)));
1346}
1347
4c4b4cd2 1348/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
14f9c5c9 1349 pointer to one, the type of its array data (a
4c4b4cd2
PH
1350 pointer-to-array-with-no-bounds type); otherwise, NULL. Use
1351 ada_type_of_array to get an array type with bounds data. */
1352
d2e4a39e
AS
1353static struct type *
1354desc_data_type (struct type *type)
14f9c5c9
AS
1355{
1356 type = desc_base_type (type);
1357
4c4b4cd2 1358 /* NOTE: The following is bogus; see comment in desc_bounds. */
14f9c5c9 1359 if (is_thin_pntr (type))
d2e4a39e
AS
1360 return lookup_pointer_type
1361 (desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1)));
14f9c5c9
AS
1362 else if (is_thick_pntr (type))
1363 return lookup_struct_elt_type (type, "P_ARRAY", 1);
1364 else
1365 return NULL;
1366}
1367
1368/* If ARR is an array descriptor (fat or thin pointer), a pointer to
1369 its array data. */
4c4b4cd2 1370
d2e4a39e
AS
1371static struct value *
1372desc_data (struct value *arr)
14f9c5c9 1373{
d2e4a39e 1374 struct type *type = VALUE_TYPE (arr);
14f9c5c9
AS
1375 if (is_thin_pntr (type))
1376 return thin_data_pntr (arr);
1377 else if (is_thick_pntr (type))
d2e4a39e 1378 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
4c4b4cd2 1379 "Bad GNAT array descriptor");
14f9c5c9
AS
1380 else
1381 return NULL;
1382}
1383
1384
1385/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1386 position of the field containing the address of the data. */
1387
14f9c5c9 1388static int
d2e4a39e 1389fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
1390{
1391 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1392}
1393
1394/* If TYPE is the type of an array-descriptor (fat pointer), the bit
4c4b4cd2
PH
1395 size of the field containing the address of the data. */
1396
14f9c5c9 1397static int
d2e4a39e 1398fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
1399{
1400 type = desc_base_type (type);
1401
1402 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1403 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 1404 else
14f9c5c9
AS
1405 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1406}
1407
4c4b4cd2 1408/* If BOUNDS is an array-bounds structure (or pointer to one), return
14f9c5c9 1409 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1410 bound, if WHICH is 1. The first bound is I=1. */
1411
d2e4a39e
AS
1412static struct value *
1413desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 1414{
d2e4a39e 1415 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
4c4b4cd2 1416 "Bad GNAT array descriptor bounds");
14f9c5c9
AS
1417}
1418
1419/* If BOUNDS is an array-bounds structure type, return the bit position
1420 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1421 bound, if WHICH is 1. The first bound is I=1. */
1422
14f9c5c9 1423static int
d2e4a39e 1424desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 1425{
d2e4a39e 1426 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
1427}
1428
1429/* If BOUNDS is an array-bounds structure type, return the bit field size
1430 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
4c4b4cd2
PH
1431 bound, if WHICH is 1. The first bound is I=1. */
1432
76a01679 1433static int
d2e4a39e 1434desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
1435{
1436 type = desc_base_type (type);
1437
d2e4a39e
AS
1438 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1439 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1440 else
1441 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
14f9c5c9
AS
1442}
1443
1444/* If TYPE is the type of an array-bounds structure, the type of its
4c4b4cd2
PH
1445 Ith bound (numbering from 1). Otherwise, NULL. */
1446
d2e4a39e
AS
1447static struct type *
1448desc_index_type (struct type *type, int i)
14f9c5c9
AS
1449{
1450 type = desc_base_type (type);
1451
1452 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
d2e4a39e
AS
1453 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1454 else
14f9c5c9
AS
1455 return NULL;
1456}
1457
4c4b4cd2
PH
1458/* The number of index positions in the array-bounds type TYPE.
1459 Return 0 if TYPE is NULL. */
1460
14f9c5c9 1461static int
d2e4a39e 1462desc_arity (struct type *type)
14f9c5c9
AS
1463{
1464 type = desc_base_type (type);
1465
1466 if (type != NULL)
1467 return TYPE_NFIELDS (type) / 2;
1468 return 0;
1469}
1470
4c4b4cd2
PH
1471/* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1472 an array descriptor type (representing an unconstrained array
1473 type). */
1474
76a01679
JB
1475static int
1476ada_is_direct_array_type (struct type *type)
4c4b4cd2
PH
1477{
1478 if (type == NULL)
1479 return 0;
1480 CHECK_TYPEDEF (type);
1481 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
76a01679 1482 || ada_is_array_descriptor_type (type));
4c4b4cd2
PH
1483}
1484
1485/* Non-zero iff TYPE is a simple array type or pointer to one. */
14f9c5c9 1486
14f9c5c9 1487int
4c4b4cd2 1488ada_is_simple_array_type (struct type *type)
14f9c5c9
AS
1489{
1490 if (type == NULL)
1491 return 0;
1492 CHECK_TYPEDEF (type);
1493 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
4c4b4cd2
PH
1494 || (TYPE_CODE (type) == TYPE_CODE_PTR
1495 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
14f9c5c9
AS
1496}
1497
4c4b4cd2
PH
1498/* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1499
14f9c5c9 1500int
4c4b4cd2 1501ada_is_array_descriptor_type (struct type *type)
14f9c5c9 1502{
d2e4a39e 1503 struct type *data_type = desc_data_type (type);
14f9c5c9
AS
1504
1505 if (type == NULL)
1506 return 0;
1507 CHECK_TYPEDEF (type);
d2e4a39e 1508 return
14f9c5c9
AS
1509 data_type != NULL
1510 && ((TYPE_CODE (data_type) == TYPE_CODE_PTR
4c4b4cd2
PH
1511 && TYPE_TARGET_TYPE (data_type) != NULL
1512 && TYPE_CODE (TYPE_TARGET_TYPE (data_type)) == TYPE_CODE_ARRAY)
1265e4aa 1513 || TYPE_CODE (data_type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
1514 && desc_arity (desc_bounds_type (type)) > 0;
1515}
1516
1517/* Non-zero iff type is a partially mal-formed GNAT array
4c4b4cd2 1518 descriptor. FIXME: This is to compensate for some problems with
14f9c5c9 1519 debugging output from GNAT. Re-examine periodically to see if it
4c4b4cd2
PH
1520 is still needed. */
1521
14f9c5c9 1522int
ebf56fd3 1523ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1524{
d2e4a39e 1525 return
14f9c5c9
AS
1526 type != NULL
1527 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1528 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
4c4b4cd2
PH
1529 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1530 && !ada_is_array_descriptor_type (type);
14f9c5c9
AS
1531}
1532
1533
4c4b4cd2 1534/* If ARR has a record type in the form of a standard GNAT array descriptor,
14f9c5c9 1535 (fat pointer) returns the type of the array data described---specifically,
4c4b4cd2 1536 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
14f9c5c9 1537 in from the descriptor; otherwise, they are left unspecified. If
4c4b4cd2
PH
1538 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1539 returns NULL. The result is simply the type of ARR if ARR is not
14f9c5c9 1540 a descriptor. */
d2e4a39e
AS
1541struct type *
1542ada_type_of_array (struct value *arr, int bounds)
14f9c5c9
AS
1543{
1544 if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1545 return decode_packed_array_type (VALUE_TYPE (arr));
1546
4c4b4cd2 1547 if (!ada_is_array_descriptor_type (VALUE_TYPE (arr)))
14f9c5c9 1548 return VALUE_TYPE (arr);
d2e4a39e
AS
1549
1550 if (!bounds)
1551 return
1552 check_typedef (TYPE_TARGET_TYPE (desc_data_type (VALUE_TYPE (arr))));
14f9c5c9
AS
1553 else
1554 {
d2e4a39e 1555 struct type *elt_type;
14f9c5c9 1556 int arity;
d2e4a39e 1557 struct value *descriptor;
14f9c5c9
AS
1558 struct objfile *objf = TYPE_OBJFILE (VALUE_TYPE (arr));
1559
1560 elt_type = ada_array_element_type (VALUE_TYPE (arr), -1);
1561 arity = ada_array_arity (VALUE_TYPE (arr));
1562
d2e4a39e 1563 if (elt_type == NULL || arity == 0)
4c4b4cd2 1564 return check_typedef (VALUE_TYPE (arr));
14f9c5c9
AS
1565
1566 descriptor = desc_bounds (arr);
d2e4a39e 1567 if (value_as_long (descriptor) == 0)
4c4b4cd2 1568 return NULL;
d2e4a39e 1569 while (arity > 0)
4c4b4cd2
PH
1570 {
1571 struct type *range_type = alloc_type (objf);
1572 struct type *array_type = alloc_type (objf);
1573 struct value *low = desc_one_bound (descriptor, arity, 0);
1574 struct value *high = desc_one_bound (descriptor, arity, 1);
1575 arity -= 1;
1576
1577 create_range_type (range_type, VALUE_TYPE (low),
1578 (int) value_as_long (low),
1579 (int) value_as_long (high));
1580 elt_type = create_array_type (array_type, elt_type, range_type);
1581 }
14f9c5c9
AS
1582
1583 return lookup_pointer_type (elt_type);
1584 }
1585}
1586
1587/* If ARR does not represent an array, returns ARR unchanged.
4c4b4cd2
PH
1588 Otherwise, returns either a standard GDB array with bounds set
1589 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1590 GDB array. Returns NULL if ARR is a null fat pointer. */
1591
d2e4a39e
AS
1592struct value *
1593ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9 1594{
4c4b4cd2 1595 if (ada_is_array_descriptor_type (VALUE_TYPE (arr)))
14f9c5c9 1596 {
d2e4a39e 1597 struct type *arrType = ada_type_of_array (arr, 1);
14f9c5c9 1598 if (arrType == NULL)
4c4b4cd2 1599 return NULL;
14f9c5c9
AS
1600 return value_cast (arrType, value_copy (desc_data (arr)));
1601 }
1602 else if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1603 return decode_packed_array (arr);
1604 else
1605 return arr;
1606}
1607
1608/* If ARR does not represent an array, returns ARR unchanged.
1609 Otherwise, returns a standard GDB array describing ARR (which may
4c4b4cd2
PH
1610 be ARR itself if it already is in the proper form). */
1611
1612static struct value *
d2e4a39e 1613ada_coerce_to_simple_array (struct value *arr)
14f9c5c9 1614{
4c4b4cd2 1615 if (ada_is_array_descriptor_type (VALUE_TYPE (arr)))
14f9c5c9 1616 {
d2e4a39e 1617 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
14f9c5c9 1618 if (arrVal == NULL)
4c4b4cd2 1619 error ("Bounds unavailable for null array pointer.");
14f9c5c9
AS
1620 return value_ind (arrVal);
1621 }
1622 else if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1623 return decode_packed_array (arr);
d2e4a39e 1624 else
14f9c5c9
AS
1625 return arr;
1626}
1627
1628/* If TYPE represents a GNAT array type, return it translated to an
1629 ordinary GDB array type (possibly with BITSIZE fields indicating
4c4b4cd2
PH
1630 packing). For other types, is the identity. */
1631
d2e4a39e
AS
1632struct type *
1633ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1634{
d2e4a39e
AS
1635 struct value *mark = value_mark ();
1636 struct value *dummy = value_from_longest (builtin_type_long, 0);
1637 struct type *result;
14f9c5c9
AS
1638 VALUE_TYPE (dummy) = type;
1639 result = ada_type_of_array (dummy, 0);
4c4b4cd2 1640 value_free_to_mark (mark);
14f9c5c9
AS
1641 return result;
1642}
1643
4c4b4cd2
PH
1644/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1645
14f9c5c9 1646int
d2e4a39e 1647ada_is_packed_array_type (struct type *type)
14f9c5c9
AS
1648{
1649 if (type == NULL)
1650 return 0;
4c4b4cd2 1651 type = desc_base_type (type);
14f9c5c9 1652 CHECK_TYPEDEF (type);
d2e4a39e 1653 return
14f9c5c9
AS
1654 ada_type_name (type) != NULL
1655 && strstr (ada_type_name (type), "___XP") != NULL;
1656}
1657
1658/* Given that TYPE is a standard GDB array type with all bounds filled
1659 in, and that the element size of its ultimate scalar constituents
1660 (that is, either its elements, or, if it is an array of arrays, its
1661 elements' elements, etc.) is *ELT_BITS, return an identical type,
1662 but with the bit sizes of its elements (and those of any
1663 constituent arrays) recorded in the BITSIZE components of its
4c4b4cd2
PH
1664 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1665 in bits. */
1666
d2e4a39e
AS
1667static struct type *
1668packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 1669{
d2e4a39e
AS
1670 struct type *new_elt_type;
1671 struct type *new_type;
14f9c5c9
AS
1672 LONGEST low_bound, high_bound;
1673
1674 CHECK_TYPEDEF (type);
1675 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1676 return type;
1677
1678 new_type = alloc_type (TYPE_OBJFILE (type));
1679 new_elt_type = packed_array_type (check_typedef (TYPE_TARGET_TYPE (type)),
4c4b4cd2 1680 elt_bits);
14f9c5c9
AS
1681 create_array_type (new_type, new_elt_type, TYPE_FIELD_TYPE (type, 0));
1682 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1683 TYPE_NAME (new_type) = ada_type_name (type);
1684
d2e4a39e 1685 if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2 1686 &low_bound, &high_bound) < 0)
14f9c5c9
AS
1687 low_bound = high_bound = 0;
1688 if (high_bound < low_bound)
1689 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 1690 else
14f9c5c9
AS
1691 {
1692 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 1693 TYPE_LENGTH (new_type) =
4c4b4cd2 1694 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
14f9c5c9
AS
1695 }
1696
4c4b4cd2 1697 TYPE_FLAGS (new_type) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
1698 return new_type;
1699}
1700
4c4b4cd2
PH
1701/* The array type encoded by TYPE, where ada_is_packed_array_type (TYPE). */
1702
d2e4a39e
AS
1703static struct type *
1704decode_packed_array_type (struct type *type)
1705{
4c4b4cd2 1706 struct symbol *sym;
d2e4a39e
AS
1707 struct block **blocks;
1708 const char *raw_name = ada_type_name (check_typedef (type));
1709 char *name = (char *) alloca (strlen (raw_name) + 1);
1710 char *tail = strstr (raw_name, "___XP");
1711 struct type *shadow_type;
14f9c5c9
AS
1712 long bits;
1713 int i, n;
1714
4c4b4cd2
PH
1715 type = desc_base_type (type);
1716
14f9c5c9
AS
1717 memcpy (name, raw_name, tail - raw_name);
1718 name[tail - raw_name] = '\000';
1719
4c4b4cd2
PH
1720 sym = standard_lookup (name, get_selected_block (0), VAR_DOMAIN);
1721 if (sym == NULL || SYMBOL_TYPE (sym) == NULL)
14f9c5c9 1722 {
4c4b4cd2 1723 lim_warning ("could not find bounds information on packed array", 0);
14f9c5c9
AS
1724 return NULL;
1725 }
4c4b4cd2 1726 shadow_type = SYMBOL_TYPE (sym);
14f9c5c9
AS
1727
1728 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
1729 {
4c4b4cd2
PH
1730 lim_warning ("could not understand bounds information on packed array",
1731 0);
14f9c5c9
AS
1732 return NULL;
1733 }
d2e4a39e 1734
14f9c5c9
AS
1735 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1736 {
4c4b4cd2
PH
1737 lim_warning
1738 ("could not understand bit size information on packed array", 0);
14f9c5c9
AS
1739 return NULL;
1740 }
d2e4a39e 1741
14f9c5c9
AS
1742 return packed_array_type (shadow_type, &bits);
1743}
1744
4c4b4cd2 1745/* Given that ARR is a struct value *indicating a GNAT packed array,
14f9c5c9
AS
1746 returns a simple array that denotes that array. Its type is a
1747 standard GDB array type except that the BITSIZEs of the array
1748 target types are set to the number of bits in each element, and the
4c4b4cd2 1749 type length is set appropriately. */
14f9c5c9 1750
d2e4a39e
AS
1751static struct value *
1752decode_packed_array (struct value *arr)
14f9c5c9 1753{
4c4b4cd2 1754 struct type *type;
14f9c5c9 1755
4c4b4cd2
PH
1756 arr = ada_coerce_ref (arr);
1757 if (TYPE_CODE (VALUE_TYPE (arr)) == TYPE_CODE_PTR)
1758 arr = ada_value_ind (arr);
1759
1760 type = decode_packed_array_type (VALUE_TYPE (arr));
14f9c5c9
AS
1761 if (type == NULL)
1762 {
1763 error ("can't unpack array");
1764 return NULL;
1765 }
4c4b4cd2 1766 return coerce_unspec_val_to_type (arr, type);
14f9c5c9
AS
1767}
1768
1769
1770/* The value of the element of packed array ARR at the ARITY indices
4c4b4cd2 1771 given in IND. ARR must be a simple array. */
14f9c5c9 1772
d2e4a39e
AS
1773static struct value *
1774value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
1775{
1776 int i;
1777 int bits, elt_off, bit_off;
1778 long elt_total_bit_offset;
d2e4a39e
AS
1779 struct type *elt_type;
1780 struct value *v;
14f9c5c9
AS
1781
1782 bits = 0;
1783 elt_total_bit_offset = 0;
1784 elt_type = check_typedef (VALUE_TYPE (arr));
d2e4a39e 1785 for (i = 0; i < arity; i += 1)
14f9c5c9 1786 {
d2e4a39e 1787 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
4c4b4cd2
PH
1788 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
1789 error
1790 ("attempt to do packed indexing of something other than a packed array");
14f9c5c9 1791 else
4c4b4cd2
PH
1792 {
1793 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
1794 LONGEST lowerbound, upperbound;
1795 LONGEST idx;
1796
1797 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
1798 {
1799 lim_warning ("don't know bounds of array", 0);
1800 lowerbound = upperbound = 0;
1801 }
1802
1803 idx = value_as_long (value_pos_atr (ind[i]));
1804 if (idx < lowerbound || idx > upperbound)
1805 lim_warning ("packed array index %ld out of bounds", (long) idx);
1806 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
1807 elt_total_bit_offset += (idx - lowerbound) * bits;
1808 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
1809 }
14f9c5c9
AS
1810 }
1811 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
1812 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
1813
1814 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
4c4b4cd2 1815 bits, elt_type);
14f9c5c9
AS
1816 if (VALUE_LVAL (arr) == lval_internalvar)
1817 VALUE_LVAL (v) = lval_internalvar_component;
1818 else
1819 VALUE_LVAL (v) = VALUE_LVAL (arr);
1820 return v;
1821}
1822
4c4b4cd2 1823/* Non-zero iff TYPE includes negative integer values. */
14f9c5c9
AS
1824
1825static int
d2e4a39e 1826has_negatives (struct type *type)
14f9c5c9 1827{
d2e4a39e
AS
1828 switch (TYPE_CODE (type))
1829 {
1830 default:
1831 return 0;
1832 case TYPE_CODE_INT:
1833 return !TYPE_UNSIGNED (type);
1834 case TYPE_CODE_RANGE:
1835 return TYPE_LOW_BOUND (type) < 0;
1836 }
14f9c5c9 1837}
d2e4a39e 1838
14f9c5c9
AS
1839
1840/* Create a new value of type TYPE from the contents of OBJ starting
1841 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
1842 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
4c4b4cd2
PH
1843 assigning through the result will set the field fetched from.
1844 VALADDR is ignored unless OBJ is NULL, in which case,
1845 VALADDR+OFFSET must address the start of storage containing the
1846 packed value. The value returned in this case is never an lval.
1847 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
14f9c5c9 1848
d2e4a39e
AS
1849struct value *
1850ada_value_primitive_packed_val (struct value *obj, char *valaddr, long offset,
4c4b4cd2
PH
1851 int bit_offset, int bit_size,
1852 struct type *type)
14f9c5c9 1853{
d2e4a39e 1854 struct value *v;
4c4b4cd2
PH
1855 int src, /* Index into the source area */
1856 targ, /* Index into the target area */
1857 srcBitsLeft, /* Number of source bits left to move */
1858 nsrc, ntarg, /* Number of source and target bytes */
1859 unusedLS, /* Number of bits in next significant
1860 byte of source that are unused */
1861 accumSize; /* Number of meaningful bits in accum */
1862 unsigned char *bytes; /* First byte containing data to unpack */
d2e4a39e 1863 unsigned char *unpacked;
4c4b4cd2 1864 unsigned long accum; /* Staging area for bits being transferred */
14f9c5c9
AS
1865 unsigned char sign;
1866 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
4c4b4cd2
PH
1867 /* Transmit bytes from least to most significant; delta is the direction
1868 the indices move. */
14f9c5c9
AS
1869 int delta = BITS_BIG_ENDIAN ? -1 : 1;
1870
1871 CHECK_TYPEDEF (type);
1872
1873 if (obj == NULL)
1874 {
1875 v = allocate_value (type);
d2e4a39e 1876 bytes = (unsigned char *) (valaddr + offset);
14f9c5c9
AS
1877 }
1878 else if (VALUE_LAZY (obj))
1879 {
1880 v = value_at (type,
4c4b4cd2 1881 VALUE_ADDRESS (obj) + VALUE_OFFSET (obj) + offset, NULL);
d2e4a39e 1882 bytes = (unsigned char *) alloca (len);
14f9c5c9
AS
1883 read_memory (VALUE_ADDRESS (v), bytes, len);
1884 }
d2e4a39e 1885 else
14f9c5c9
AS
1886 {
1887 v = allocate_value (type);
d2e4a39e 1888 bytes = (unsigned char *) VALUE_CONTENTS (obj) + offset;
14f9c5c9 1889 }
d2e4a39e
AS
1890
1891 if (obj != NULL)
14f9c5c9
AS
1892 {
1893 VALUE_LVAL (v) = VALUE_LVAL (obj);
1894 if (VALUE_LVAL (obj) == lval_internalvar)
4c4b4cd2 1895 VALUE_LVAL (v) = lval_internalvar_component;
14f9c5c9
AS
1896 VALUE_ADDRESS (v) = VALUE_ADDRESS (obj) + VALUE_OFFSET (obj) + offset;
1897 VALUE_BITPOS (v) = bit_offset + VALUE_BITPOS (obj);
1898 VALUE_BITSIZE (v) = bit_size;
1899 if (VALUE_BITPOS (v) >= HOST_CHAR_BIT)
4c4b4cd2
PH
1900 {
1901 VALUE_ADDRESS (v) += 1;
1902 VALUE_BITPOS (v) -= HOST_CHAR_BIT;
1903 }
14f9c5c9
AS
1904 }
1905 else
1906 VALUE_BITSIZE (v) = bit_size;
d2e4a39e 1907 unpacked = (unsigned char *) VALUE_CONTENTS (v);
14f9c5c9
AS
1908
1909 srcBitsLeft = bit_size;
1910 nsrc = len;
1911 ntarg = TYPE_LENGTH (type);
1912 sign = 0;
1913 if (bit_size == 0)
1914 {
1915 memset (unpacked, 0, TYPE_LENGTH (type));
1916 return v;
1917 }
1918 else if (BITS_BIG_ENDIAN)
1919 {
d2e4a39e 1920 src = len - 1;
1265e4aa
JB
1921 if (has_negatives (type)
1922 && ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
4c4b4cd2 1923 sign = ~0;
d2e4a39e
AS
1924
1925 unusedLS =
4c4b4cd2
PH
1926 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
1927 % HOST_CHAR_BIT;
14f9c5c9
AS
1928
1929 switch (TYPE_CODE (type))
4c4b4cd2
PH
1930 {
1931 case TYPE_CODE_ARRAY:
1932 case TYPE_CODE_UNION:
1933 case TYPE_CODE_STRUCT:
1934 /* Non-scalar values must be aligned at a byte boundary... */
1935 accumSize =
1936 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
1937 /* ... And are placed at the beginning (most-significant) bytes
1938 of the target. */
1939 targ = src;
1940 break;
1941 default:
1942 accumSize = 0;
1943 targ = TYPE_LENGTH (type) - 1;
1944 break;
1945 }
14f9c5c9 1946 }
d2e4a39e 1947 else
14f9c5c9
AS
1948 {
1949 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
1950
1951 src = targ = 0;
1952 unusedLS = bit_offset;
1953 accumSize = 0;
1954
d2e4a39e 1955 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
4c4b4cd2 1956 sign = ~0;
14f9c5c9 1957 }
d2e4a39e 1958
14f9c5c9
AS
1959 accum = 0;
1960 while (nsrc > 0)
1961 {
1962 /* Mask for removing bits of the next source byte that are not
4c4b4cd2 1963 part of the value. */
d2e4a39e 1964 unsigned int unusedMSMask =
4c4b4cd2
PH
1965 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
1966 1;
1967 /* Sign-extend bits for this byte. */
14f9c5c9 1968 unsigned int signMask = sign & ~unusedMSMask;
d2e4a39e 1969 accum |=
4c4b4cd2 1970 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
14f9c5c9 1971 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 1972 if (accumSize >= HOST_CHAR_BIT)
4c4b4cd2
PH
1973 {
1974 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1975 accumSize -= HOST_CHAR_BIT;
1976 accum >>= HOST_CHAR_BIT;
1977 ntarg -= 1;
1978 targ += delta;
1979 }
14f9c5c9
AS
1980 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
1981 unusedLS = 0;
1982 nsrc -= 1;
1983 src += delta;
1984 }
1985 while (ntarg > 0)
1986 {
1987 accum |= sign << accumSize;
1988 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1989 accumSize -= HOST_CHAR_BIT;
1990 accum >>= HOST_CHAR_BIT;
1991 ntarg -= 1;
1992 targ += delta;
1993 }
1994
1995 return v;
1996}
d2e4a39e 1997
14f9c5c9
AS
1998/* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
1999 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
4c4b4cd2 2000 not overlap. */
14f9c5c9 2001static void
d2e4a39e 2002move_bits (char *target, int targ_offset, char *source, int src_offset, int n)
14f9c5c9
AS
2003{
2004 unsigned int accum, mask;
2005 int accum_bits, chunk_size;
2006
2007 target += targ_offset / HOST_CHAR_BIT;
2008 targ_offset %= HOST_CHAR_BIT;
2009 source += src_offset / HOST_CHAR_BIT;
2010 src_offset %= HOST_CHAR_BIT;
d2e4a39e 2011 if (BITS_BIG_ENDIAN)
14f9c5c9
AS
2012 {
2013 accum = (unsigned char) *source;
2014 source += 1;
2015 accum_bits = HOST_CHAR_BIT - src_offset;
2016
d2e4a39e 2017 while (n > 0)
4c4b4cd2
PH
2018 {
2019 int unused_right;
2020 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
2021 accum_bits += HOST_CHAR_BIT;
2022 source += 1;
2023 chunk_size = HOST_CHAR_BIT - targ_offset;
2024 if (chunk_size > n)
2025 chunk_size = n;
2026 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
2027 mask = ((1 << chunk_size) - 1) << unused_right;
2028 *target =
2029 (*target & ~mask)
2030 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
2031 n -= chunk_size;
2032 accum_bits -= chunk_size;
2033 target += 1;
2034 targ_offset = 0;
2035 }
14f9c5c9
AS
2036 }
2037 else
2038 {
2039 accum = (unsigned char) *source >> src_offset;
2040 source += 1;
2041 accum_bits = HOST_CHAR_BIT - src_offset;
2042
d2e4a39e 2043 while (n > 0)
4c4b4cd2
PH
2044 {
2045 accum = accum + ((unsigned char) *source << accum_bits);
2046 accum_bits += HOST_CHAR_BIT;
2047 source += 1;
2048 chunk_size = HOST_CHAR_BIT - targ_offset;
2049 if (chunk_size > n)
2050 chunk_size = n;
2051 mask = ((1 << chunk_size) - 1) << targ_offset;
2052 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2053 n -= chunk_size;
2054 accum_bits -= chunk_size;
2055 accum >>= chunk_size;
2056 target += 1;
2057 targ_offset = 0;
2058 }
14f9c5c9
AS
2059 }
2060}
2061
2062
2063/* Store the contents of FROMVAL into the location of TOVAL.
2064 Return a new value with the location of TOVAL and contents of
2065 FROMVAL. Handles assignment into packed fields that have
4c4b4cd2 2066 floating-point or non-scalar types. */
14f9c5c9 2067
d2e4a39e
AS
2068static struct value *
2069ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 2070{
d2e4a39e 2071 struct type *type = VALUE_TYPE (toval);
14f9c5c9
AS
2072 int bits = VALUE_BITSIZE (toval);
2073
2074 if (!toval->modifiable)
2075 error ("Left operand of assignment is not a modifiable lvalue.");
2076
2077 COERCE_REF (toval);
2078
d2e4a39e 2079 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 2080 && bits > 0
d2e4a39e 2081 && (TYPE_CODE (type) == TYPE_CODE_FLT
4c4b4cd2 2082 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
14f9c5c9 2083 {
d2e4a39e 2084 int len =
4c4b4cd2 2085 (VALUE_BITPOS (toval) + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
d2e4a39e
AS
2086 char *buffer = (char *) alloca (len);
2087 struct value *val;
14f9c5c9
AS
2088
2089 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4c4b4cd2 2090 fromval = value_cast (type, fromval);
14f9c5c9
AS
2091
2092 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), buffer, len);
2093 if (BITS_BIG_ENDIAN)
4c4b4cd2
PH
2094 move_bits (buffer, VALUE_BITPOS (toval),
2095 VALUE_CONTENTS (fromval),
2096 TYPE_LENGTH (VALUE_TYPE (fromval)) * TARGET_CHAR_BIT -
2097 bits, bits);
14f9c5c9 2098 else
4c4b4cd2
PH
2099 move_bits (buffer, VALUE_BITPOS (toval), VALUE_CONTENTS (fromval),
2100 0, bits);
d2e4a39e 2101 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), buffer,
4c4b4cd2 2102 len);
14f9c5c9
AS
2103
2104 val = value_copy (toval);
2105 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
4c4b4cd2 2106 TYPE_LENGTH (type));
14f9c5c9 2107 VALUE_TYPE (val) = type;
d2e4a39e 2108
14f9c5c9
AS
2109 return val;
2110 }
2111
2112 return value_assign (toval, fromval);
2113}
2114
2115
4c4b4cd2
PH
2116/* The value of the element of array ARR at the ARITY indices given in IND.
2117 ARR may be either a simple array, GNAT array descriptor, or pointer
14f9c5c9
AS
2118 thereto. */
2119
d2e4a39e
AS
2120struct value *
2121ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
2122{
2123 int k;
d2e4a39e
AS
2124 struct value *elt;
2125 struct type *elt_type;
14f9c5c9
AS
2126
2127 elt = ada_coerce_to_simple_array (arr);
2128
2129 elt_type = check_typedef (VALUE_TYPE (elt));
d2e4a39e 2130 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
14f9c5c9
AS
2131 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2132 return value_subscript_packed (elt, arity, ind);
2133
2134 for (k = 0; k < arity; k += 1)
2135 {
2136 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
4c4b4cd2 2137 error ("too many subscripts (%d expected)", k);
14f9c5c9
AS
2138 elt = value_subscript (elt, value_pos_atr (ind[k]));
2139 }
2140 return elt;
2141}
2142
2143/* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
2144 value of the element of *ARR at the ARITY indices given in
4c4b4cd2 2145 IND. Does not read the entire array into memory. */
14f9c5c9 2146
d2e4a39e
AS
2147struct value *
2148ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
4c4b4cd2 2149 struct value **ind)
14f9c5c9
AS
2150{
2151 int k;
2152
2153 for (k = 0; k < arity; k += 1)
2154 {
2155 LONGEST lwb, upb;
d2e4a39e 2156 struct value *idx;
14f9c5c9
AS
2157
2158 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
4c4b4cd2 2159 error ("too many subscripts (%d expected)", k);
d2e4a39e 2160 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4c4b4cd2 2161 value_copy (arr));
14f9c5c9 2162 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
4c4b4cd2
PH
2163 idx = value_pos_atr (ind[k]);
2164 if (lwb != 0)
2165 idx = value_sub (idx, value_from_longest (builtin_type_int, lwb));
14f9c5c9
AS
2166 arr = value_add (arr, idx);
2167 type = TYPE_TARGET_TYPE (type);
2168 }
2169
2170 return value_ind (arr);
2171}
2172
0b5d8877
PH
2173/* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
2174 actual type of ARRAY_PTR is ignored), returns a reference to
2175 the Ada slice of HIGH-LOW+1 elements starting at index LOW. The lower
2176 bound of this array is LOW, as per Ada rules. */
2177static struct value *
2178ada_value_slice_ptr (struct value *array_ptr, struct type *type,
2179 int low, int high)
2180{
2181 CORE_ADDR base = value_as_address (array_ptr)
2182 + ((low - TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)))
2183 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
2184 struct type *index_type =
2185 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)),
2186 low, high);
2187 struct type *slice_type =
2188 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2189 return value_from_pointer (lookup_reference_type (slice_type), base);
2190}
2191
2192
2193static struct value *
2194ada_value_slice (struct value *array, int low, int high)
2195{
2196 struct type *type = VALUE_TYPE (array);
2197 struct type *index_type =
2198 create_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
2199 struct type *slice_type =
2200 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2201 return value_cast (slice_type, value_slice (array, low, high-low+1));
2202}
2203
14f9c5c9
AS
2204/* If type is a record type in the form of a standard GNAT array
2205 descriptor, returns the number of dimensions for type. If arr is a
2206 simple array, returns the number of "array of"s that prefix its
4c4b4cd2 2207 type designation. Otherwise, returns 0. */
14f9c5c9
AS
2208
2209int
d2e4a39e 2210ada_array_arity (struct type *type)
14f9c5c9
AS
2211{
2212 int arity;
2213
2214 if (type == NULL)
2215 return 0;
2216
2217 type = desc_base_type (type);
2218
2219 arity = 0;
d2e4a39e 2220 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9 2221 return desc_arity (desc_bounds_type (type));
d2e4a39e
AS
2222 else
2223 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9 2224 {
4c4b4cd2
PH
2225 arity += 1;
2226 type = check_typedef (TYPE_TARGET_TYPE (type));
14f9c5c9 2227 }
d2e4a39e 2228
14f9c5c9
AS
2229 return arity;
2230}
2231
2232/* If TYPE is a record type in the form of a standard GNAT array
2233 descriptor or a simple array type, returns the element type for
2234 TYPE after indexing by NINDICES indices, or by all indices if
4c4b4cd2 2235 NINDICES is -1. Otherwise, returns NULL. */
14f9c5c9 2236
d2e4a39e
AS
2237struct type *
2238ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
2239{
2240 type = desc_base_type (type);
2241
d2e4a39e 2242 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9
AS
2243 {
2244 int k;
d2e4a39e 2245 struct type *p_array_type;
14f9c5c9
AS
2246
2247 p_array_type = desc_data_type (type);
2248
2249 k = ada_array_arity (type);
2250 if (k == 0)
4c4b4cd2 2251 return NULL;
d2e4a39e 2252
4c4b4cd2 2253 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
14f9c5c9 2254 if (nindices >= 0 && k > nindices)
4c4b4cd2 2255 k = nindices;
14f9c5c9 2256 p_array_type = TYPE_TARGET_TYPE (p_array_type);
d2e4a39e 2257 while (k > 0 && p_array_type != NULL)
4c4b4cd2
PH
2258 {
2259 p_array_type = check_typedef (TYPE_TARGET_TYPE (p_array_type));
2260 k -= 1;
2261 }
14f9c5c9
AS
2262 return p_array_type;
2263 }
2264 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2265 {
2266 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
4c4b4cd2
PH
2267 {
2268 type = TYPE_TARGET_TYPE (type);
2269 nindices -= 1;
2270 }
14f9c5c9
AS
2271 return type;
2272 }
2273
2274 return NULL;
2275}
2276
4c4b4cd2
PH
2277/* The type of nth index in arrays of given type (n numbering from 1).
2278 Does not examine memory. */
14f9c5c9 2279
d2e4a39e
AS
2280struct type *
2281ada_index_type (struct type *type, int n)
14f9c5c9 2282{
4c4b4cd2
PH
2283 struct type *result_type;
2284
14f9c5c9
AS
2285 type = desc_base_type (type);
2286
2287 if (n > ada_array_arity (type))
2288 return NULL;
2289
4c4b4cd2 2290 if (ada_is_simple_array_type (type))
14f9c5c9
AS
2291 {
2292 int i;
2293
2294 for (i = 1; i < n; i += 1)
4c4b4cd2
PH
2295 type = TYPE_TARGET_TYPE (type);
2296 result_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
2297 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2298 has a target type of TYPE_CODE_UNDEF. We compensate here, but
76a01679
JB
2299 perhaps stabsread.c would make more sense. */
2300 if (result_type == NULL || TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2301 result_type = builtin_type_int;
14f9c5c9 2302
4c4b4cd2 2303 return result_type;
14f9c5c9 2304 }
d2e4a39e 2305 else
14f9c5c9
AS
2306 return desc_index_type (desc_bounds_type (type), n);
2307}
2308
2309/* Given that arr is an array type, returns the lower bound of the
2310 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
4c4b4cd2
PH
2311 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
2312 array-descriptor type. If TYPEP is non-null, *TYPEP is set to the
2313 bounds type. It works for other arrays with bounds supplied by
2314 run-time quantities other than discriminants. */
14f9c5c9
AS
2315
2316LONGEST
d2e4a39e 2317ada_array_bound_from_type (struct type * arr_type, int n, int which,
4c4b4cd2 2318 struct type ** typep)
14f9c5c9 2319{
d2e4a39e
AS
2320 struct type *type;
2321 struct type *index_type_desc;
14f9c5c9
AS
2322
2323 if (ada_is_packed_array_type (arr_type))
2324 arr_type = decode_packed_array_type (arr_type);
2325
4c4b4cd2 2326 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
14f9c5c9
AS
2327 {
2328 if (typep != NULL)
4c4b4cd2 2329 *typep = builtin_type_int;
d2e4a39e 2330 return (LONGEST) - which;
14f9c5c9
AS
2331 }
2332
2333 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
2334 type = TYPE_TARGET_TYPE (arr_type);
2335 else
2336 type = arr_type;
2337
2338 index_type_desc = ada_find_parallel_type (type, "___XA");
d2e4a39e 2339 if (index_type_desc == NULL)
14f9c5c9 2340 {
d2e4a39e
AS
2341 struct type *range_type;
2342 struct type *index_type;
14f9c5c9 2343
d2e4a39e 2344 while (n > 1)
4c4b4cd2
PH
2345 {
2346 type = TYPE_TARGET_TYPE (type);
2347 n -= 1;
2348 }
14f9c5c9
AS
2349
2350 range_type = TYPE_INDEX_TYPE (type);
2351 index_type = TYPE_TARGET_TYPE (range_type);
2352 if (TYPE_CODE (index_type) == TYPE_CODE_UNDEF)
4c4b4cd2 2353 index_type = builtin_type_long;
14f9c5c9 2354 if (typep != NULL)
4c4b4cd2 2355 *typep = index_type;
d2e4a39e 2356 return
4c4b4cd2
PH
2357 (LONGEST) (which == 0
2358 ? TYPE_LOW_BOUND (range_type)
2359 : TYPE_HIGH_BOUND (range_type));
14f9c5c9 2360 }
d2e4a39e 2361 else
14f9c5c9 2362 {
d2e4a39e 2363 struct type *index_type =
4c4b4cd2
PH
2364 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, n - 1),
2365 NULL, TYPE_OBJFILE (arr_type));
14f9c5c9 2366 if (typep != NULL)
4c4b4cd2 2367 *typep = TYPE_TARGET_TYPE (index_type);
d2e4a39e 2368 return
4c4b4cd2
PH
2369 (LONGEST) (which == 0
2370 ? TYPE_LOW_BOUND (index_type)
2371 : TYPE_HIGH_BOUND (index_type));
14f9c5c9
AS
2372 }
2373}
2374
2375/* Given that arr is an array value, returns the lower bound of the
2376 nth index (numbering from 1) if which is 0, and the upper bound if
4c4b4cd2
PH
2377 which is 1. This routine will also work for arrays with bounds
2378 supplied by run-time quantities other than discriminants. */
14f9c5c9 2379
d2e4a39e 2380struct value *
4dc81987 2381ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 2382{
d2e4a39e 2383 struct type *arr_type = VALUE_TYPE (arr);
14f9c5c9
AS
2384
2385 if (ada_is_packed_array_type (arr_type))
2386 return ada_array_bound (decode_packed_array (arr), n, which);
4c4b4cd2 2387 else if (ada_is_simple_array_type (arr_type))
14f9c5c9 2388 {
d2e4a39e 2389 struct type *type;
14f9c5c9
AS
2390 LONGEST v = ada_array_bound_from_type (arr_type, n, which, &type);
2391 return value_from_longest (type, v);
2392 }
2393 else
2394 return desc_one_bound (desc_bounds (arr), n, which);
2395}
2396
2397/* Given that arr is an array value, returns the length of the
2398 nth index. This routine will also work for arrays with bounds
4c4b4cd2
PH
2399 supplied by run-time quantities other than discriminants.
2400 Does not work for arrays indexed by enumeration types with representation
2401 clauses at the moment. */
14f9c5c9 2402
d2e4a39e
AS
2403struct value *
2404ada_array_length (struct value *arr, int n)
14f9c5c9 2405{
d2e4a39e 2406 struct type *arr_type = check_typedef (VALUE_TYPE (arr));
14f9c5c9
AS
2407
2408 if (ada_is_packed_array_type (arr_type))
2409 return ada_array_length (decode_packed_array (arr), n);
2410
4c4b4cd2 2411 if (ada_is_simple_array_type (arr_type))
14f9c5c9 2412 {
d2e4a39e 2413 struct type *type;
14f9c5c9 2414 LONGEST v =
4c4b4cd2
PH
2415 ada_array_bound_from_type (arr_type, n, 1, &type) -
2416 ada_array_bound_from_type (arr_type, n, 0, NULL) + 1;
14f9c5c9
AS
2417 return value_from_longest (type, v);
2418 }
2419 else
d2e4a39e 2420 return
14f9c5c9 2421 value_from_longest (builtin_type_ada_int,
4c4b4cd2
PH
2422 value_as_long (desc_one_bound (desc_bounds (arr),
2423 n, 1))
2424 - value_as_long (desc_one_bound (desc_bounds (arr),
2425 n, 0)) + 1);
2426}
2427
2428/* An empty array whose type is that of ARR_TYPE (an array type),
2429 with bounds LOW to LOW-1. */
2430
2431static struct value *
2432empty_array (struct type *arr_type, int low)
2433{
0b5d8877
PH
2434 struct type *index_type =
2435 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type)),
2436 low, low - 1);
2437 struct type *elt_type = ada_array_element_type (arr_type, 1);
2438 return allocate_value (create_array_type (NULL, elt_type, index_type));
14f9c5c9 2439}
14f9c5c9 2440\f
d2e4a39e 2441
4c4b4cd2 2442 /* Name resolution */
14f9c5c9 2443
4c4b4cd2
PH
2444/* The "decoded" name for the user-definable Ada operator corresponding
2445 to OP. */
14f9c5c9 2446
d2e4a39e 2447static const char *
4c4b4cd2 2448ada_decoded_op_name (enum exp_opcode op)
14f9c5c9
AS
2449{
2450 int i;
2451
4c4b4cd2 2452 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9
AS
2453 {
2454 if (ada_opname_table[i].op == op)
4c4b4cd2 2455 return ada_opname_table[i].decoded;
14f9c5c9
AS
2456 }
2457 error ("Could not find operator name for opcode");
2458}
2459
2460
4c4b4cd2
PH
2461/* Same as evaluate_type (*EXP), but resolves ambiguous symbol
2462 references (marked by OP_VAR_VALUE nodes in which the symbol has an
2463 undefined namespace) and converts operators that are
2464 user-defined into appropriate function calls. If CONTEXT_TYPE is
14f9c5c9
AS
2465 non-null, it provides a preferred result type [at the moment, only
2466 type void has any effect---causing procedures to be preferred over
2467 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
4c4b4cd2 2468 return type is preferred. May change (expand) *EXP. */
14f9c5c9 2469
4c4b4cd2
PH
2470static void
2471resolve (struct expression **expp, int void_context_p)
14f9c5c9
AS
2472{
2473 int pc;
2474 pc = 0;
4c4b4cd2 2475 resolve_subexp (expp, &pc, 1, void_context_p ? builtin_type_void : NULL);
14f9c5c9
AS
2476}
2477
4c4b4cd2
PH
2478/* Resolve the operator of the subexpression beginning at
2479 position *POS of *EXPP. "Resolving" consists of replacing
2480 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
2481 with their resolutions, replacing built-in operators with
2482 function calls to user-defined operators, where appropriate, and,
2483 when DEPROCEDURE_P is non-zero, converting function-valued variables
2484 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
2485 are as in ada_resolve, above. */
14f9c5c9 2486
d2e4a39e 2487static struct value *
4c4b4cd2 2488resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
76a01679 2489 struct type *context_type)
14f9c5c9
AS
2490{
2491 int pc = *pos;
2492 int i;
4c4b4cd2 2493 struct expression *exp; /* Convenience: == *expp. */
14f9c5c9 2494 enum exp_opcode op = (*expp)->elts[pc].opcode;
4c4b4cd2
PH
2495 struct value **argvec; /* Vector of operand types (alloca'ed). */
2496 int nargs; /* Number of operands. */
14f9c5c9
AS
2497
2498 argvec = NULL;
2499 nargs = 0;
2500 exp = *expp;
2501
4c4b4cd2 2502 /* Pass one: resolve operands, saving their types and updating *pos. */
14f9c5c9
AS
2503 switch (op)
2504 {
4c4b4cd2
PH
2505 case OP_FUNCALL:
2506 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679
JB
2507 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
2508 *pos += 7;
4c4b4cd2
PH
2509 else
2510 {
2511 *pos += 3;
2512 resolve_subexp (expp, pos, 0, NULL);
2513 }
2514 nargs = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9
AS
2515 break;
2516
4c4b4cd2
PH
2517 case UNOP_QUAL:
2518 *pos += 3;
2519 resolve_subexp (expp, pos, 1, exp->elts[pc + 1].type);
14f9c5c9
AS
2520 break;
2521
14f9c5c9 2522 case UNOP_ADDR:
4c4b4cd2
PH
2523 *pos += 1;
2524 resolve_subexp (expp, pos, 0, NULL);
2525 break;
2526
2527 case OP_ATR_MODULUS:
2528 *pos += 4;
2529 break;
2530
2531 case OP_ATR_SIZE:
2532 case OP_ATR_TAG:
2533 *pos += 1;
14f9c5c9 2534 nargs = 1;
4c4b4cd2
PH
2535 break;
2536
2537 case OP_ATR_FIRST:
2538 case OP_ATR_LAST:
2539 case OP_ATR_LENGTH:
2540 case OP_ATR_POS:
2541 case OP_ATR_VAL:
14f9c5c9 2542 *pos += 1;
4c4b4cd2
PH
2543 nargs = 2;
2544 break;
2545
2546 case OP_ATR_MIN:
2547 case OP_ATR_MAX:
2548 *pos += 1;
2549 nargs = 3;
14f9c5c9
AS
2550 break;
2551
2552 case BINOP_ASSIGN:
2553 {
4c4b4cd2
PH
2554 struct value *arg1;
2555
2556 *pos += 1;
2557 arg1 = resolve_subexp (expp, pos, 0, NULL);
2558 if (arg1 == NULL)
2559 resolve_subexp (expp, pos, 1, NULL);
2560 else
2561 resolve_subexp (expp, pos, 1, VALUE_TYPE (arg1));
2562 break;
14f9c5c9
AS
2563 }
2564
4c4b4cd2
PH
2565 case UNOP_CAST:
2566 case UNOP_IN_RANGE:
2567 *pos += 3;
2568 nargs = 1;
2569 break;
14f9c5c9 2570
4c4b4cd2
PH
2571 case BINOP_ADD:
2572 case BINOP_SUB:
2573 case BINOP_MUL:
2574 case BINOP_DIV:
2575 case BINOP_REM:
2576 case BINOP_MOD:
2577 case BINOP_EXP:
2578 case BINOP_CONCAT:
2579 case BINOP_LOGICAL_AND:
2580 case BINOP_LOGICAL_OR:
2581 case BINOP_BITWISE_AND:
2582 case BINOP_BITWISE_IOR:
2583 case BINOP_BITWISE_XOR:
14f9c5c9 2584
4c4b4cd2
PH
2585 case BINOP_EQUAL:
2586 case BINOP_NOTEQUAL:
2587 case BINOP_LESS:
2588 case BINOP_GTR:
2589 case BINOP_LEQ:
2590 case BINOP_GEQ:
14f9c5c9 2591
4c4b4cd2
PH
2592 case BINOP_REPEAT:
2593 case BINOP_SUBSCRIPT:
2594 case BINOP_COMMA:
2595 *pos += 1;
2596 nargs = 2;
2597 break;
14f9c5c9 2598
4c4b4cd2
PH
2599 case UNOP_NEG:
2600 case UNOP_PLUS:
2601 case UNOP_LOGICAL_NOT:
2602 case UNOP_ABS:
2603 case UNOP_IND:
2604 *pos += 1;
2605 nargs = 1;
2606 break;
14f9c5c9 2607
4c4b4cd2
PH
2608 case OP_LONG:
2609 case OP_DOUBLE:
2610 case OP_VAR_VALUE:
2611 *pos += 4;
2612 break;
14f9c5c9 2613
4c4b4cd2
PH
2614 case OP_TYPE:
2615 case OP_BOOL:
2616 case OP_LAST:
2617 case OP_REGISTER:
2618 case OP_INTERNALVAR:
2619 *pos += 3;
2620 break;
14f9c5c9 2621
4c4b4cd2
PH
2622 case UNOP_MEMVAL:
2623 *pos += 3;
2624 nargs = 1;
2625 break;
2626
2627 case STRUCTOP_STRUCT:
2628 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2629 nargs = 1;
2630 break;
2631
2632 case OP_STRING:
19c1ef65
PH
2633 (*pos) += 3
2634 + BYTES_TO_EXP_ELEM (longest_to_int (exp->elts[pc + 1].longconst)
2635 + 1);
4c4b4cd2
PH
2636 break;
2637
2638 case TERNOP_SLICE:
2639 case TERNOP_IN_RANGE:
2640 *pos += 1;
2641 nargs = 3;
2642 break;
2643
2644 case BINOP_IN_BOUNDS:
2645 *pos += 3;
2646 nargs = 2;
14f9c5c9 2647 break;
4c4b4cd2
PH
2648
2649 default:
2650 error ("Unexpected operator during name resolution");
14f9c5c9
AS
2651 }
2652
76a01679 2653 argvec = (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
4c4b4cd2
PH
2654 for (i = 0; i < nargs; i += 1)
2655 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
2656 argvec[i] = NULL;
2657 exp = *expp;
2658
2659 /* Pass two: perform any resolution on principal operator. */
14f9c5c9
AS
2660 switch (op)
2661 {
2662 default:
2663 break;
2664
14f9c5c9 2665 case OP_VAR_VALUE:
4c4b4cd2 2666 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
2667 {
2668 struct ada_symbol_info *candidates;
2669 int n_candidates;
2670
2671 n_candidates =
2672 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2673 (exp->elts[pc + 2].symbol),
2674 exp->elts[pc + 1].block, VAR_DOMAIN,
2675 &candidates);
2676
2677 if (n_candidates > 1)
2678 {
2679 /* Types tend to get re-introduced locally, so if there
2680 are any local symbols that are not types, first filter
2681 out all types. */
2682 int j;
2683 for (j = 0; j < n_candidates; j += 1)
2684 switch (SYMBOL_CLASS (candidates[j].sym))
2685 {
2686 case LOC_REGISTER:
2687 case LOC_ARG:
2688 case LOC_REF_ARG:
2689 case LOC_REGPARM:
2690 case LOC_REGPARM_ADDR:
2691 case LOC_LOCAL:
2692 case LOC_LOCAL_ARG:
2693 case LOC_BASEREG:
2694 case LOC_BASEREG_ARG:
2695 case LOC_COMPUTED:
2696 case LOC_COMPUTED_ARG:
2697 goto FoundNonType;
2698 default:
2699 break;
2700 }
2701 FoundNonType:
2702 if (j < n_candidates)
2703 {
2704 j = 0;
2705 while (j < n_candidates)
2706 {
2707 if (SYMBOL_CLASS (candidates[j].sym) == LOC_TYPEDEF)
2708 {
2709 candidates[j] = candidates[n_candidates - 1];
2710 n_candidates -= 1;
2711 }
2712 else
2713 j += 1;
2714 }
2715 }
2716 }
2717
2718 if (n_candidates == 0)
2719 error ("No definition found for %s",
2720 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2721 else if (n_candidates == 1)
2722 i = 0;
2723 else if (deprocedure_p
2724 && !is_nonfunction (candidates, n_candidates))
2725 {
06d5cf63
JB
2726 i = ada_resolve_function
2727 (candidates, n_candidates, NULL, 0,
2728 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
2729 context_type);
76a01679
JB
2730 if (i < 0)
2731 error ("Could not find a match for %s",
2732 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2733 }
2734 else
2735 {
2736 printf_filtered ("Multiple matches for %s\n",
2737 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
2738 user_select_syms (candidates, n_candidates, 1);
2739 i = 0;
2740 }
2741
2742 exp->elts[pc + 1].block = candidates[i].block;
2743 exp->elts[pc + 2].symbol = candidates[i].sym;
1265e4aa
JB
2744 if (innermost_block == NULL
2745 || contained_in (candidates[i].block, innermost_block))
76a01679
JB
2746 innermost_block = candidates[i].block;
2747 }
2748
2749 if (deprocedure_p
2750 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
2751 == TYPE_CODE_FUNC))
2752 {
2753 replace_operator_with_call (expp, pc, 0, 0,
2754 exp->elts[pc + 2].symbol,
2755 exp->elts[pc + 1].block);
2756 exp = *expp;
2757 }
14f9c5c9
AS
2758 break;
2759
2760 case OP_FUNCALL:
2761 {
4c4b4cd2 2762 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
76a01679 2763 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
4c4b4cd2
PH
2764 {
2765 struct ada_symbol_info *candidates;
2766 int n_candidates;
2767
2768 n_candidates =
76a01679
JB
2769 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2770 (exp->elts[pc + 5].symbol),
2771 exp->elts[pc + 4].block, VAR_DOMAIN,
2772 &candidates);
4c4b4cd2
PH
2773 if (n_candidates == 1)
2774 i = 0;
2775 else
2776 {
06d5cf63
JB
2777 i = ada_resolve_function
2778 (candidates, n_candidates,
2779 argvec, nargs,
2780 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
2781 context_type);
4c4b4cd2
PH
2782 if (i < 0)
2783 error ("Could not find a match for %s",
2784 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
2785 }
2786
2787 exp->elts[pc + 4].block = candidates[i].block;
2788 exp->elts[pc + 5].symbol = candidates[i].sym;
1265e4aa
JB
2789 if (innermost_block == NULL
2790 || contained_in (candidates[i].block, innermost_block))
4c4b4cd2
PH
2791 innermost_block = candidates[i].block;
2792 }
14f9c5c9
AS
2793 }
2794 break;
2795 case BINOP_ADD:
2796 case BINOP_SUB:
2797 case BINOP_MUL:
2798 case BINOP_DIV:
2799 case BINOP_REM:
2800 case BINOP_MOD:
2801 case BINOP_CONCAT:
2802 case BINOP_BITWISE_AND:
2803 case BINOP_BITWISE_IOR:
2804 case BINOP_BITWISE_XOR:
2805 case BINOP_EQUAL:
2806 case BINOP_NOTEQUAL:
2807 case BINOP_LESS:
2808 case BINOP_GTR:
2809 case BINOP_LEQ:
2810 case BINOP_GEQ:
2811 case BINOP_EXP:
2812 case UNOP_NEG:
2813 case UNOP_PLUS:
2814 case UNOP_LOGICAL_NOT:
2815 case UNOP_ABS:
2816 if (possible_user_operator_p (op, argvec))
4c4b4cd2
PH
2817 {
2818 struct ada_symbol_info *candidates;
2819 int n_candidates;
2820
2821 n_candidates =
2822 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
2823 (struct block *) NULL, VAR_DOMAIN,
2824 &candidates);
2825 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
76a01679 2826 ada_decoded_op_name (op), NULL);
4c4b4cd2
PH
2827 if (i < 0)
2828 break;
2829
76a01679
JB
2830 replace_operator_with_call (expp, pc, nargs, 1,
2831 candidates[i].sym, candidates[i].block);
4c4b4cd2
PH
2832 exp = *expp;
2833 }
14f9c5c9 2834 break;
4c4b4cd2
PH
2835
2836 case OP_TYPE:
2837 return NULL;
14f9c5c9
AS
2838 }
2839
2840 *pos = pc;
2841 return evaluate_subexp_type (exp, pos);
2842}
2843
2844/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
4c4b4cd2
PH
2845 MAY_DEREF is non-zero, the formal may be a pointer and the actual
2846 a non-pointer. A type of 'void' (which is never a valid expression type)
2847 by convention matches anything. */
14f9c5c9 2848/* The term "match" here is rather loose. The match is heuristic and
4c4b4cd2 2849 liberal. FIXME: TOO liberal, in fact. */
14f9c5c9
AS
2850
2851static int
4dc81987 2852ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9
AS
2853{
2854 CHECK_TYPEDEF (ftype);
2855 CHECK_TYPEDEF (atype);
2856
2857 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
2858 ftype = TYPE_TARGET_TYPE (ftype);
2859 if (TYPE_CODE (atype) == TYPE_CODE_REF)
2860 atype = TYPE_TARGET_TYPE (atype);
2861
d2e4a39e 2862 if (TYPE_CODE (ftype) == TYPE_CODE_VOID
14f9c5c9
AS
2863 || TYPE_CODE (atype) == TYPE_CODE_VOID)
2864 return 1;
2865
d2e4a39e 2866 switch (TYPE_CODE (ftype))
14f9c5c9
AS
2867 {
2868 default:
2869 return 1;
2870 case TYPE_CODE_PTR:
2871 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
4c4b4cd2
PH
2872 return ada_type_match (TYPE_TARGET_TYPE (ftype),
2873 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e 2874 else
1265e4aa
JB
2875 return (may_deref
2876 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
14f9c5c9
AS
2877 case TYPE_CODE_INT:
2878 case TYPE_CODE_ENUM:
2879 case TYPE_CODE_RANGE:
2880 switch (TYPE_CODE (atype))
4c4b4cd2
PH
2881 {
2882 case TYPE_CODE_INT:
2883 case TYPE_CODE_ENUM:
2884 case TYPE_CODE_RANGE:
2885 return 1;
2886 default:
2887 return 0;
2888 }
14f9c5c9
AS
2889
2890 case TYPE_CODE_ARRAY:
d2e4a39e 2891 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
4c4b4cd2 2892 || ada_is_array_descriptor_type (atype));
14f9c5c9
AS
2893
2894 case TYPE_CODE_STRUCT:
4c4b4cd2
PH
2895 if (ada_is_array_descriptor_type (ftype))
2896 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
2897 || ada_is_array_descriptor_type (atype));
14f9c5c9 2898 else
4c4b4cd2
PH
2899 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
2900 && !ada_is_array_descriptor_type (atype));
14f9c5c9
AS
2901
2902 case TYPE_CODE_UNION:
2903 case TYPE_CODE_FLT:
2904 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
2905 }
2906}
2907
2908/* Return non-zero if the formals of FUNC "sufficiently match" the
2909 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
2910 may also be an enumeral, in which case it is treated as a 0-
4c4b4cd2 2911 argument function. */
14f9c5c9
AS
2912
2913static int
d2e4a39e 2914ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
14f9c5c9
AS
2915{
2916 int i;
d2e4a39e 2917 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 2918
1265e4aa
JB
2919 if (SYMBOL_CLASS (func) == LOC_CONST
2920 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
14f9c5c9
AS
2921 return (n_actuals == 0);
2922 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
2923 return 0;
2924
2925 if (TYPE_NFIELDS (func_type) != n_actuals)
2926 return 0;
2927
2928 for (i = 0; i < n_actuals; i += 1)
2929 {
4c4b4cd2 2930 if (actuals[i] == NULL)
76a01679
JB
2931 return 0;
2932 else
2933 {
2934 struct type *ftype = check_typedef (TYPE_FIELD_TYPE (func_type, i));
2935 struct type *atype = check_typedef (VALUE_TYPE (actuals[i]));
4c4b4cd2 2936
76a01679
JB
2937 if (!ada_type_match (ftype, atype, 1))
2938 return 0;
2939 }
14f9c5c9
AS
2940 }
2941 return 1;
2942}
2943
2944/* False iff function type FUNC_TYPE definitely does not produce a value
2945 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
2946 FUNC_TYPE is not a valid function type with a non-null return type
2947 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
2948
2949static int
d2e4a39e 2950return_match (struct type *func_type, struct type *context_type)
14f9c5c9 2951{
d2e4a39e 2952 struct type *return_type;
14f9c5c9
AS
2953
2954 if (func_type == NULL)
2955 return 1;
2956
4c4b4cd2
PH
2957 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
2958 return_type = base_type (TYPE_TARGET_TYPE (func_type));
2959 else
2960 return_type = base_type (func_type);
14f9c5c9
AS
2961 if (return_type == NULL)
2962 return 1;
2963
4c4b4cd2 2964 context_type = base_type (context_type);
14f9c5c9
AS
2965
2966 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
2967 return context_type == NULL || return_type == context_type;
2968 else if (context_type == NULL)
2969 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
2970 else
2971 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
2972}
2973
2974
4c4b4cd2 2975/* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
14f9c5c9 2976 function (if any) that matches the types of the NARGS arguments in
4c4b4cd2
PH
2977 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
2978 that returns that type, then eliminate matches that don't. If
2979 CONTEXT_TYPE is void and there is at least one match that does not
2980 return void, eliminate all matches that do.
2981
14f9c5c9
AS
2982 Asks the user if there is more than one match remaining. Returns -1
2983 if there is no such symbol or none is selected. NAME is used
4c4b4cd2
PH
2984 solely for messages. May re-arrange and modify SYMS in
2985 the process; the index returned is for the modified vector. */
14f9c5c9 2986
4c4b4cd2
PH
2987static int
2988ada_resolve_function (struct ada_symbol_info syms[],
2989 int nsyms, struct value **args, int nargs,
2990 const char *name, struct type *context_type)
14f9c5c9
AS
2991{
2992 int k;
4c4b4cd2 2993 int m; /* Number of hits */
d2e4a39e
AS
2994 struct type *fallback;
2995 struct type *return_type;
14f9c5c9
AS
2996
2997 return_type = context_type;
2998 if (context_type == NULL)
2999 fallback = builtin_type_void;
3000 else
3001 fallback = NULL;
3002
d2e4a39e 3003 m = 0;
14f9c5c9
AS
3004 while (1)
3005 {
3006 for (k = 0; k < nsyms; k += 1)
4c4b4cd2
PH
3007 {
3008 struct type *type = check_typedef (SYMBOL_TYPE (syms[k].sym));
3009
3010 if (ada_args_match (syms[k].sym, args, nargs)
3011 && return_match (type, return_type))
3012 {
3013 syms[m] = syms[k];
3014 m += 1;
3015 }
3016 }
14f9c5c9 3017 if (m > 0 || return_type == fallback)
4c4b4cd2 3018 break;
14f9c5c9 3019 else
4c4b4cd2 3020 return_type = fallback;
14f9c5c9
AS
3021 }
3022
3023 if (m == 0)
3024 return -1;
3025 else if (m > 1)
3026 {
3027 printf_filtered ("Multiple matches for %s\n", name);
4c4b4cd2 3028 user_select_syms (syms, m, 1);
14f9c5c9
AS
3029 return 0;
3030 }
3031 return 0;
3032}
3033
4c4b4cd2
PH
3034/* Returns true (non-zero) iff decoded name N0 should appear before N1
3035 in a listing of choices during disambiguation (see sort_choices, below).
3036 The idea is that overloadings of a subprogram name from the
3037 same package should sort in their source order. We settle for ordering
3038 such symbols by their trailing number (__N or $N). */
3039
14f9c5c9 3040static int
4c4b4cd2 3041encoded_ordered_before (char *N0, char *N1)
14f9c5c9
AS
3042{
3043 if (N1 == NULL)
3044 return 0;
3045 else if (N0 == NULL)
3046 return 1;
3047 else
3048 {
3049 int k0, k1;
d2e4a39e 3050 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
4c4b4cd2 3051 ;
d2e4a39e 3052 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
4c4b4cd2 3053 ;
d2e4a39e 3054 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
4c4b4cd2
PH
3055 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3056 {
3057 int n0, n1;
3058 n0 = k0;
3059 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3060 n0 -= 1;
3061 n1 = k1;
3062 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3063 n1 -= 1;
3064 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3065 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3066 }
14f9c5c9
AS
3067 return (strcmp (N0, N1) < 0);
3068 }
3069}
d2e4a39e 3070
4c4b4cd2
PH
3071/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3072 encoded names. */
3073
d2e4a39e 3074static void
4c4b4cd2 3075sort_choices (struct ada_symbol_info syms[], int nsyms)
14f9c5c9 3076{
4c4b4cd2 3077 int i;
d2e4a39e 3078 for (i = 1; i < nsyms; i += 1)
14f9c5c9 3079 {
4c4b4cd2 3080 struct ada_symbol_info sym = syms[i];
14f9c5c9
AS
3081 int j;
3082
d2e4a39e 3083 for (j = i - 1; j >= 0; j -= 1)
4c4b4cd2
PH
3084 {
3085 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].sym),
3086 SYMBOL_LINKAGE_NAME (sym.sym)))
3087 break;
3088 syms[j + 1] = syms[j];
3089 }
d2e4a39e 3090 syms[j + 1] = sym;
14f9c5c9
AS
3091 }
3092}
3093
4c4b4cd2
PH
3094/* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3095 by asking the user (if necessary), returning the number selected,
3096 and setting the first elements of SYMS items. Error if no symbols
3097 selected. */
14f9c5c9
AS
3098
3099/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
4c4b4cd2 3100 to be re-integrated one of these days. */
14f9c5c9
AS
3101
3102int
4c4b4cd2 3103user_select_syms (struct ada_symbol_info *syms, int nsyms, int max_results)
14f9c5c9
AS
3104{
3105 int i;
d2e4a39e 3106 int *chosen = (int *) alloca (sizeof (int) * nsyms);
14f9c5c9
AS
3107 int n_chosen;
3108 int first_choice = (max_results == 1) ? 1 : 2;
3109
3110 if (max_results < 1)
3111 error ("Request to select 0 symbols!");
3112 if (nsyms <= 1)
3113 return nsyms;
3114
d2e4a39e 3115 printf_unfiltered ("[0] cancel\n");
14f9c5c9 3116 if (max_results > 1)
d2e4a39e 3117 printf_unfiltered ("[1] all\n");
14f9c5c9 3118
4c4b4cd2 3119 sort_choices (syms, nsyms);
14f9c5c9
AS
3120
3121 for (i = 0; i < nsyms; i += 1)
3122 {
4c4b4cd2
PH
3123 if (syms[i].sym == NULL)
3124 continue;
3125
3126 if (SYMBOL_CLASS (syms[i].sym) == LOC_BLOCK)
3127 {
76a01679
JB
3128 struct symtab_and_line sal =
3129 find_function_start_sal (syms[i].sym, 1);
3130 printf_unfiltered ("[%d] %s at %s:%d\n", i + first_choice,
4c4b4cd2 3131 SYMBOL_PRINT_NAME (syms[i].sym),
06d5cf63
JB
3132 (sal.symtab == NULL
3133 ? "<no source file available>"
3134 : sal.symtab->filename), sal.line);
4c4b4cd2
PH
3135 continue;
3136 }
d2e4a39e 3137 else
4c4b4cd2
PH
3138 {
3139 int is_enumeral =
3140 (SYMBOL_CLASS (syms[i].sym) == LOC_CONST
3141 && SYMBOL_TYPE (syms[i].sym) != NULL
3142 && TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) == TYPE_CODE_ENUM);
3143 struct symtab *symtab = symtab_for_sym (syms[i].sym);
3144
3145 if (SYMBOL_LINE (syms[i].sym) != 0 && symtab != NULL)
3146 printf_unfiltered ("[%d] %s at %s:%d\n",
3147 i + first_choice,
3148 SYMBOL_PRINT_NAME (syms[i].sym),
3149 symtab->filename, SYMBOL_LINE (syms[i].sym));
76a01679
JB
3150 else if (is_enumeral
3151 && TYPE_NAME (SYMBOL_TYPE (syms[i].sym)) != NULL)
4c4b4cd2
PH
3152 {
3153 printf_unfiltered ("[%d] ", i + first_choice);
76a01679
JB
3154 ada_print_type (SYMBOL_TYPE (syms[i].sym), NULL,
3155 gdb_stdout, -1, 0);
4c4b4cd2
PH
3156 printf_unfiltered ("'(%s) (enumeral)\n",
3157 SYMBOL_PRINT_NAME (syms[i].sym));
3158 }
3159 else if (symtab != NULL)
3160 printf_unfiltered (is_enumeral
3161 ? "[%d] %s in %s (enumeral)\n"
3162 : "[%d] %s at %s:?\n",
3163 i + first_choice,
3164 SYMBOL_PRINT_NAME (syms[i].sym),
3165 symtab->filename);
3166 else
3167 printf_unfiltered (is_enumeral
3168 ? "[%d] %s (enumeral)\n"
3169 : "[%d] %s at ?\n",
3170 i + first_choice,
3171 SYMBOL_PRINT_NAME (syms[i].sym));
3172 }
14f9c5c9 3173 }
d2e4a39e 3174
14f9c5c9 3175 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
4c4b4cd2 3176 "overload-choice");
14f9c5c9
AS
3177
3178 for (i = 0; i < n_chosen; i += 1)
4c4b4cd2 3179 syms[i] = syms[chosen[i]];
14f9c5c9
AS
3180
3181 return n_chosen;
3182}
3183
3184/* Read and validate a set of numeric choices from the user in the
4c4b4cd2 3185 range 0 .. N_CHOICES-1. Place the results in increasing
14f9c5c9
AS
3186 order in CHOICES[0 .. N-1], and return N.
3187
3188 The user types choices as a sequence of numbers on one line
3189 separated by blanks, encoding them as follows:
3190
4c4b4cd2 3191 + A choice of 0 means to cancel the selection, throwing an error.
14f9c5c9
AS
3192 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3193 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3194
4c4b4cd2 3195 The user is not allowed to choose more than MAX_RESULTS values.
14f9c5c9
AS
3196
3197 ANNOTATION_SUFFIX, if present, is used to annotate the input
4c4b4cd2 3198 prompts (for use with the -f switch). */
14f9c5c9
AS
3199
3200int
d2e4a39e 3201get_selections (int *choices, int n_choices, int max_results,
4c4b4cd2 3202 int is_all_choice, char *annotation_suffix)
14f9c5c9 3203{
d2e4a39e
AS
3204 char *args;
3205 const char *prompt;
14f9c5c9
AS
3206 int n_chosen;
3207 int first_choice = is_all_choice ? 2 : 1;
d2e4a39e 3208
14f9c5c9
AS
3209 prompt = getenv ("PS2");
3210 if (prompt == NULL)
3211 prompt = ">";
3212
3213 printf_unfiltered ("%s ", prompt);
3214 gdb_flush (gdb_stdout);
3215
3216 args = command_line_input ((char *) NULL, 0, annotation_suffix);
d2e4a39e 3217
14f9c5c9
AS
3218 if (args == NULL)
3219 error_no_arg ("one or more choice numbers");
3220
3221 n_chosen = 0;
76a01679 3222
4c4b4cd2
PH
3223 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3224 order, as given in args. Choices are validated. */
14f9c5c9
AS
3225 while (1)
3226 {
d2e4a39e 3227 char *args2;
14f9c5c9
AS
3228 int choice, j;
3229
3230 while (isspace (*args))
4c4b4cd2 3231 args += 1;
14f9c5c9 3232 if (*args == '\0' && n_chosen == 0)
4c4b4cd2 3233 error_no_arg ("one or more choice numbers");
14f9c5c9 3234 else if (*args == '\0')
4c4b4cd2 3235 break;
14f9c5c9
AS
3236
3237 choice = strtol (args, &args2, 10);
d2e4a39e 3238 if (args == args2 || choice < 0
4c4b4cd2
PH
3239 || choice > n_choices + first_choice - 1)
3240 error ("Argument must be choice number");
14f9c5c9
AS
3241 args = args2;
3242
d2e4a39e 3243 if (choice == 0)
4c4b4cd2 3244 error ("cancelled");
14f9c5c9
AS
3245
3246 if (choice < first_choice)
4c4b4cd2
PH
3247 {
3248 n_chosen = n_choices;
3249 for (j = 0; j < n_choices; j += 1)
3250 choices[j] = j;
3251 break;
3252 }
14f9c5c9
AS
3253 choice -= first_choice;
3254
d2e4a39e 3255 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
4c4b4cd2
PH
3256 {
3257 }
14f9c5c9
AS
3258
3259 if (j < 0 || choice != choices[j])
4c4b4cd2
PH
3260 {
3261 int k;
3262 for (k = n_chosen - 1; k > j; k -= 1)
3263 choices[k + 1] = choices[k];
3264 choices[j + 1] = choice;
3265 n_chosen += 1;
3266 }
14f9c5c9
AS
3267 }
3268
3269 if (n_chosen > max_results)
3270 error ("Select no more than %d of the above", max_results);
d2e4a39e 3271
14f9c5c9
AS
3272 return n_chosen;
3273}
3274
4c4b4cd2
PH
3275/* Replace the operator of length OPLEN at position PC in *EXPP with a call
3276 on the function identified by SYM and BLOCK, and taking NARGS
3277 arguments. Update *EXPP as needed to hold more space. */
14f9c5c9
AS
3278
3279static void
d2e4a39e 3280replace_operator_with_call (struct expression **expp, int pc, int nargs,
4c4b4cd2
PH
3281 int oplen, struct symbol *sym,
3282 struct block *block)
14f9c5c9
AS
3283{
3284 /* A new expression, with 6 more elements (3 for funcall, 4 for function
4c4b4cd2 3285 symbol, -oplen for operator being replaced). */
d2e4a39e 3286 struct expression *newexp = (struct expression *)
14f9c5c9 3287 xmalloc (sizeof (struct expression)
4c4b4cd2 3288 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
d2e4a39e 3289 struct expression *exp = *expp;
14f9c5c9
AS
3290
3291 newexp->nelts = exp->nelts + 7 - oplen;
3292 newexp->language_defn = exp->language_defn;
3293 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
d2e4a39e 3294 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
4c4b4cd2 3295 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
14f9c5c9
AS
3296
3297 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
3298 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
3299
3300 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
3301 newexp->elts[pc + 4].block = block;
3302 newexp->elts[pc + 5].symbol = sym;
3303
3304 *expp = newexp;
aacb1f0a 3305 xfree (exp);
d2e4a39e 3306}
14f9c5c9
AS
3307
3308/* Type-class predicates */
3309
4c4b4cd2
PH
3310/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3311 or FLOAT). */
14f9c5c9
AS
3312
3313static int
d2e4a39e 3314numeric_type_p (struct type *type)
14f9c5c9
AS
3315{
3316 if (type == NULL)
3317 return 0;
d2e4a39e
AS
3318 else
3319 {
3320 switch (TYPE_CODE (type))
4c4b4cd2
PH
3321 {
3322 case TYPE_CODE_INT:
3323 case TYPE_CODE_FLT:
3324 return 1;
3325 case TYPE_CODE_RANGE:
3326 return (type == TYPE_TARGET_TYPE (type)
3327 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3328 default:
3329 return 0;
3330 }
d2e4a39e 3331 }
14f9c5c9
AS
3332}
3333
4c4b4cd2 3334/* True iff TYPE is integral (an INT or RANGE of INTs). */
14f9c5c9
AS
3335
3336static int
d2e4a39e 3337integer_type_p (struct type *type)
14f9c5c9
AS
3338{
3339 if (type == NULL)
3340 return 0;
d2e4a39e
AS
3341 else
3342 {
3343 switch (TYPE_CODE (type))
4c4b4cd2
PH
3344 {
3345 case TYPE_CODE_INT:
3346 return 1;
3347 case TYPE_CODE_RANGE:
3348 return (type == TYPE_TARGET_TYPE (type)
3349 || integer_type_p (TYPE_TARGET_TYPE (type)));
3350 default:
3351 return 0;
3352 }
d2e4a39e 3353 }
14f9c5c9
AS
3354}
3355
4c4b4cd2 3356/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
14f9c5c9
AS
3357
3358static int
d2e4a39e 3359scalar_type_p (struct type *type)
14f9c5c9
AS
3360{
3361 if (type == NULL)
3362 return 0;
d2e4a39e
AS
3363 else
3364 {
3365 switch (TYPE_CODE (type))
4c4b4cd2
PH
3366 {
3367 case TYPE_CODE_INT:
3368 case TYPE_CODE_RANGE:
3369 case TYPE_CODE_ENUM:
3370 case TYPE_CODE_FLT:
3371 return 1;
3372 default:
3373 return 0;
3374 }
d2e4a39e 3375 }
14f9c5c9
AS
3376}
3377
4c4b4cd2 3378/* True iff TYPE is discrete (INT, RANGE, ENUM). */
14f9c5c9
AS
3379
3380static int
d2e4a39e 3381discrete_type_p (struct type *type)
14f9c5c9
AS
3382{
3383 if (type == NULL)
3384 return 0;
d2e4a39e
AS
3385 else
3386 {
3387 switch (TYPE_CODE (type))
4c4b4cd2
PH
3388 {
3389 case TYPE_CODE_INT:
3390 case TYPE_CODE_RANGE:
3391 case TYPE_CODE_ENUM:
3392 return 1;
3393 default:
3394 return 0;
3395 }
d2e4a39e 3396 }
14f9c5c9
AS
3397}
3398
4c4b4cd2
PH
3399/* Returns non-zero if OP with operands in the vector ARGS could be
3400 a user-defined function. Errs on the side of pre-defined operators
3401 (i.e., result 0). */
14f9c5c9
AS
3402
3403static int
d2e4a39e 3404possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 3405{
76a01679 3406 struct type *type0 =
4c4b4cd2 3407 (args[0] == NULL) ? NULL : check_typedef (VALUE_TYPE (args[0]));
d2e4a39e 3408 struct type *type1 =
14f9c5c9 3409 (args[1] == NULL) ? NULL : check_typedef (VALUE_TYPE (args[1]));
d2e4a39e 3410
4c4b4cd2
PH
3411 if (type0 == NULL)
3412 return 0;
3413
14f9c5c9
AS
3414 switch (op)
3415 {
3416 default:
3417 return 0;
3418
3419 case BINOP_ADD:
3420 case BINOP_SUB:
3421 case BINOP_MUL:
3422 case BINOP_DIV:
d2e4a39e 3423 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
3424
3425 case BINOP_REM:
3426 case BINOP_MOD:
3427 case BINOP_BITWISE_AND:
3428 case BINOP_BITWISE_IOR:
3429 case BINOP_BITWISE_XOR:
d2e4a39e 3430 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3431
3432 case BINOP_EQUAL:
3433 case BINOP_NOTEQUAL:
3434 case BINOP_LESS:
3435 case BINOP_GTR:
3436 case BINOP_LEQ:
3437 case BINOP_GEQ:
d2e4a39e 3438 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
3439
3440 case BINOP_CONCAT:
1265e4aa
JB
3441 return
3442 ((TYPE_CODE (type0) != TYPE_CODE_ARRAY
3443 && (TYPE_CODE (type0) != TYPE_CODE_PTR
3444 || TYPE_CODE (TYPE_TARGET_TYPE (type0)) != TYPE_CODE_ARRAY))
3445 || (TYPE_CODE (type1) != TYPE_CODE_ARRAY
3446 && (TYPE_CODE (type1) != TYPE_CODE_PTR
3447 || (TYPE_CODE (TYPE_TARGET_TYPE (type1)) !=
3448 TYPE_CODE_ARRAY))));
14f9c5c9
AS
3449
3450 case BINOP_EXP:
d2e4a39e 3451 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
3452
3453 case UNOP_NEG:
3454 case UNOP_PLUS:
3455 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
3456 case UNOP_ABS:
3457 return (!numeric_type_p (type0));
14f9c5c9
AS
3458
3459 }
3460}
3461\f
4c4b4cd2 3462 /* Renaming */
14f9c5c9 3463
4c4b4cd2
PH
3464/* NOTE: In the following, we assume that a renaming type's name may
3465 have an ___XD suffix. It would be nice if this went away at some
3466 point. */
14f9c5c9
AS
3467
3468/* If TYPE encodes a renaming, returns the renaming suffix, which
4c4b4cd2
PH
3469 is XR for an object renaming, XRP for a procedure renaming, XRE for
3470 an exception renaming, and XRS for a subprogram renaming. Returns
3471 NULL if NAME encodes none of these. */
3472
d2e4a39e
AS
3473const char *
3474ada_renaming_type (struct type *type)
14f9c5c9
AS
3475{
3476 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_ENUM)
3477 {
d2e4a39e
AS
3478 const char *name = type_name_no_tag (type);
3479 const char *suffix = (name == NULL) ? NULL : strstr (name, "___XR");
3480 if (suffix == NULL
4c4b4cd2
PH
3481 || (suffix[5] != '\000' && strchr ("PES_", suffix[5]) == NULL))
3482 return NULL;
14f9c5c9 3483 else
4c4b4cd2 3484 return suffix + 3;
14f9c5c9
AS
3485 }
3486 else
3487 return NULL;
3488}
3489
4c4b4cd2
PH
3490/* Return non-zero iff SYM encodes an object renaming. */
3491
14f9c5c9 3492int
d2e4a39e 3493ada_is_object_renaming (struct symbol *sym)
14f9c5c9 3494{
d2e4a39e
AS
3495 const char *renaming_type = ada_renaming_type (SYMBOL_TYPE (sym));
3496 return renaming_type != NULL
14f9c5c9
AS
3497 && (renaming_type[2] == '\0' || renaming_type[2] == '_');
3498}
3499
3500/* Assuming that SYM encodes a non-object renaming, returns the original
4c4b4cd2
PH
3501 name of the renamed entity. The name is good until the end of
3502 parsing. */
3503
3504char *
d2e4a39e 3505ada_simple_renamed_entity (struct symbol *sym)
14f9c5c9 3506{
d2e4a39e
AS
3507 struct type *type;
3508 const char *raw_name;
14f9c5c9 3509 int len;
d2e4a39e 3510 char *result;
14f9c5c9
AS
3511
3512 type = SYMBOL_TYPE (sym);
3513 if (type == NULL || TYPE_NFIELDS (type) < 1)
3514 error ("Improperly encoded renaming.");
3515
3516 raw_name = TYPE_FIELD_NAME (type, 0);
3517 len = (raw_name == NULL ? 0 : strlen (raw_name)) - 5;
3518 if (len <= 0)
3519 error ("Improperly encoded renaming.");
3520
3521 result = xmalloc (len + 1);
14f9c5c9
AS
3522 strncpy (result, raw_name, len);
3523 result[len] = '\000';
3524 return result;
3525}
14f9c5c9 3526\f
d2e4a39e 3527
4c4b4cd2 3528 /* Evaluation: Function Calls */
14f9c5c9 3529
4c4b4cd2
PH
3530/* Return an lvalue containing the value VAL. This is the identity on
3531 lvalues, and otherwise has the side-effect of pushing a copy of VAL
3532 on the stack, using and updating *SP as the stack pointer, and
3533 returning an lvalue whose VALUE_ADDRESS points to the copy. */
14f9c5c9 3534
d2e4a39e 3535static struct value *
4c4b4cd2 3536ensure_lval (struct value *val, CORE_ADDR *sp)
14f9c5c9
AS
3537{
3538 CORE_ADDR old_sp = *sp;
3539
4c4b4cd2
PH
3540 if (VALUE_LVAL (val))
3541 return val;
3542
3543 if (DEPRECATED_STACK_ALIGN_P ())
3544 *sp = push_bytes (*sp, VALUE_CONTENTS_RAW (val),
76a01679
JB
3545 DEPRECATED_STACK_ALIGN
3546 (TYPE_LENGTH (check_typedef (VALUE_TYPE (val)))));
4c4b4cd2
PH
3547 else
3548 *sp = push_bytes (*sp, VALUE_CONTENTS_RAW (val),
3549 TYPE_LENGTH (check_typedef (VALUE_TYPE (val))));
14f9c5c9
AS
3550
3551 VALUE_LVAL (val) = lval_memory;
3552 if (INNER_THAN (1, 2))
3553 VALUE_ADDRESS (val) = *sp;
3554 else
3555 VALUE_ADDRESS (val) = old_sp;
3556
3557 return val;
3558}
3559
3560/* Return the value ACTUAL, converted to be an appropriate value for a
3561 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3562 allocating any necessary descriptors (fat pointers), or copies of
4c4b4cd2 3563 values not residing in memory, updating it as needed. */
14f9c5c9 3564
d2e4a39e
AS
3565static struct value *
3566convert_actual (struct value *actual, struct type *formal_type0,
4c4b4cd2 3567 CORE_ADDR *sp)
14f9c5c9 3568{
d2e4a39e
AS
3569 struct type *actual_type = check_typedef (VALUE_TYPE (actual));
3570 struct type *formal_type = check_typedef (formal_type0);
3571 struct type *formal_target =
3572 TYPE_CODE (formal_type) == TYPE_CODE_PTR
3573 ? check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
3574 struct type *actual_target =
3575 TYPE_CODE (actual_type) == TYPE_CODE_PTR
3576 ? check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9 3577
4c4b4cd2 3578 if (ada_is_array_descriptor_type (formal_target)
14f9c5c9
AS
3579 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
3580 return make_array_descriptor (formal_type, actual, sp);
3581 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR)
3582 {
3583 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4c4b4cd2
PH
3584 && ada_is_array_descriptor_type (actual_target))
3585 return desc_data (actual);
14f9c5c9 3586 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4c4b4cd2
PH
3587 {
3588 if (VALUE_LVAL (actual) != lval_memory)
3589 {
3590 struct value *val;
3591 actual_type = check_typedef (VALUE_TYPE (actual));
3592 val = allocate_value (actual_type);
3593 memcpy ((char *) VALUE_CONTENTS_RAW (val),
3594 (char *) VALUE_CONTENTS (actual),
3595 TYPE_LENGTH (actual_type));
3596 actual = ensure_lval (val, sp);
3597 }
3598 return value_addr (actual);
3599 }
14f9c5c9
AS
3600 }
3601 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
3602 return ada_value_ind (actual);
3603
3604 return actual;
3605}
3606
3607
4c4b4cd2
PH
3608/* Push a descriptor of type TYPE for array value ARR on the stack at
3609 *SP, updating *SP to reflect the new descriptor. Return either
14f9c5c9 3610 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4c4b4cd2
PH
3611 to-descriptor type rather than a descriptor type), a struct value *
3612 representing a pointer to this descriptor. */
14f9c5c9 3613
d2e4a39e
AS
3614static struct value *
3615make_array_descriptor (struct type *type, struct value *arr, CORE_ADDR *sp)
14f9c5c9 3616{
d2e4a39e
AS
3617 struct type *bounds_type = desc_bounds_type (type);
3618 struct type *desc_type = desc_base_type (type);
3619 struct value *descriptor = allocate_value (desc_type);
3620 struct value *bounds = allocate_value (bounds_type);
14f9c5c9 3621 int i;
d2e4a39e 3622
14f9c5c9
AS
3623 for (i = ada_array_arity (check_typedef (VALUE_TYPE (arr))); i > 0; i -= 1)
3624 {
3625 modify_general_field (VALUE_CONTENTS (bounds),
4c4b4cd2
PH
3626 value_as_long (ada_array_bound (arr, i, 0)),
3627 desc_bound_bitpos (bounds_type, i, 0),
3628 desc_bound_bitsize (bounds_type, i, 0));
14f9c5c9 3629 modify_general_field (VALUE_CONTENTS (bounds),
4c4b4cd2
PH
3630 value_as_long (ada_array_bound (arr, i, 1)),
3631 desc_bound_bitpos (bounds_type, i, 1),
3632 desc_bound_bitsize (bounds_type, i, 1));
14f9c5c9 3633 }
d2e4a39e 3634
4c4b4cd2 3635 bounds = ensure_lval (bounds, sp);
d2e4a39e 3636
14f9c5c9 3637 modify_general_field (VALUE_CONTENTS (descriptor),
76a01679
JB
3638 VALUE_ADDRESS (ensure_lval (arr, sp)),
3639 fat_pntr_data_bitpos (desc_type),
3640 fat_pntr_data_bitsize (desc_type));
4c4b4cd2 3641
14f9c5c9 3642 modify_general_field (VALUE_CONTENTS (descriptor),
4c4b4cd2
PH
3643 VALUE_ADDRESS (bounds),
3644 fat_pntr_bounds_bitpos (desc_type),
3645 fat_pntr_bounds_bitsize (desc_type));
14f9c5c9 3646
4c4b4cd2 3647 descriptor = ensure_lval (descriptor, sp);
14f9c5c9
AS
3648
3649 if (TYPE_CODE (type) == TYPE_CODE_PTR)
3650 return value_addr (descriptor);
3651 else
3652 return descriptor;
3653}
3654
3655
4c4b4cd2 3656/* Assuming a dummy frame has been established on the target, perform any
14f9c5c9 3657 conversions needed for calling function FUNC on the NARGS actual
4c4b4cd2 3658 parameters in ARGS, other than standard C conversions. Does
14f9c5c9 3659 nothing if FUNC does not have Ada-style prototype data, or if NARGS
4c4b4cd2 3660 does not match the number of arguments expected. Use *SP as a
14f9c5c9 3661 stack pointer for additional data that must be pushed, updating its
4c4b4cd2 3662 value as needed. */
14f9c5c9
AS
3663
3664void
d2e4a39e 3665ada_convert_actuals (struct value *func, int nargs, struct value *args[],
4c4b4cd2 3666 CORE_ADDR *sp)
14f9c5c9
AS
3667{
3668 int i;
3669
d2e4a39e 3670 if (TYPE_NFIELDS (VALUE_TYPE (func)) == 0
14f9c5c9
AS
3671 || nargs != TYPE_NFIELDS (VALUE_TYPE (func)))
3672 return;
3673
3674 for (i = 0; i < nargs; i += 1)
d2e4a39e
AS
3675 args[i] =
3676 convert_actual (args[i], TYPE_FIELD_TYPE (VALUE_TYPE (func), i), sp);
14f9c5c9 3677}
14f9c5c9 3678\f
76a01679 3679 /* Experimental Symbol Cache Module */
d2e4a39e 3680
96d887e8
PH
3681/* This module may well have been OBE, due to improvements in the
3682 symbol-table module. So until proven otherwise, it is disabled in
3683 the submitted public code, and may be removed from all sources
3684 in the future. */
3685
3686#ifdef GNAT_GDB
3687
4c4b4cd2
PH
3688/* This section implements a simple, fixed-sized hash table for those
3689 Ada-mode symbols that get looked up in the course of executing the user's
3690 commands. The size is fixed on the grounds that there are not
3691 likely to be all that many symbols looked up during any given
3692 session, regardless of the size of the symbol table. If we decide
3693 to go to a resizable table, let's just use the stuff from libiberty
3694 instead. */
14f9c5c9 3695
4c4b4cd2 3696#define HASH_SIZE 1009
14f9c5c9 3697
76a01679
JB
3698struct cache_entry
3699{
4c4b4cd2
PH
3700 const char *name;
3701 domain_enum namespace;
3702 struct symbol *sym;
3703 struct symtab *symtab;
3704 struct block *block;
3705 struct cache_entry *next;
3706};
14f9c5c9 3707
4c4b4cd2 3708static struct obstack cache_space;
14f9c5c9 3709
4c4b4cd2 3710static struct cache_entry *cache[HASH_SIZE];
14f9c5c9 3711
4c4b4cd2 3712/* Clear all entries from the symbol cache. */
14f9c5c9 3713
4c4b4cd2
PH
3714void
3715clear_ada_sym_cache (void)
3716{
3717 obstack_free (&cache_space, NULL);
3718 obstack_init (&cache_space);
3719 memset (cache, '\000', sizeof (cache));
3720}
14f9c5c9 3721
4c4b4cd2
PH
3722static struct cache_entry **
3723find_entry (const char *name, domain_enum namespace)
14f9c5c9 3724{
4c4b4cd2
PH
3725 int h = msymbol_hash (name) % HASH_SIZE;
3726 struct cache_entry **e;
3727 for (e = &cache[h]; *e != NULL; e = &(*e)->next)
3728 {
3729 if (namespace == (*e)->namespace && strcmp (name, (*e)->name) == 0)
76a01679 3730 return e;
4c4b4cd2
PH
3731 }
3732 return NULL;
14f9c5c9 3733}
d2e4a39e 3734
4c4b4cd2
PH
3735/* Return (in SYM) the last cached definition for global or static symbol NAME
3736 in namespace DOMAIN. Returns 1 if entry found, 0 otherwise.
3737 If SYMTAB is non-NULL, store the symbol
3738 table in which the symbol was found there, or NULL if not found.
3739 *BLOCK is set to the block in which NAME is found. */
14f9c5c9 3740
14f9c5c9 3741static int
4c4b4cd2 3742lookup_cached_symbol (const char *name, domain_enum namespace,
76a01679
JB
3743 struct symbol **sym, struct block **block,
3744 struct symtab **symtab)
14f9c5c9 3745{
4c4b4cd2
PH
3746 struct cache_entry **e = find_entry (name, namespace);
3747 if (e == NULL)
3748 return 0;
3749 if (sym != NULL)
3750 *sym = (*e)->sym;
3751 if (block != NULL)
3752 *block = (*e)->block;
3753 if (symtab != NULL)
3754 *symtab = (*e)->symtab;
3755 return 1;
3756}
14f9c5c9 3757
4c4b4cd2
PH
3758/* Set the cached definition of NAME in DOMAIN to SYM in block
3759 BLOCK and symbol table SYMTAB. */
3760
3761static void
3762cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
76a01679 3763 struct block *block, struct symtab *symtab)
4c4b4cd2
PH
3764{
3765 int h = msymbol_hash (name) % HASH_SIZE;
3766 char *copy;
3767 struct cache_entry *e =
76a01679 3768 (struct cache_entry *) obstack_alloc (&cache_space, sizeof (*e));
4c4b4cd2
PH
3769 e->next = cache[h];
3770 cache[h] = e;
3771 e->name = copy = obstack_alloc (&cache_space, strlen (name) + 1);
3772 strcpy (copy, name);
3773 e->sym = sym;
3774 e->namespace = namespace;
3775 e->symtab = symtab;
3776 e->block = block;
3777}
96d887e8
PH
3778
3779#else
3780static int
3781lookup_cached_symbol (const char *name, domain_enum namespace,
76a01679
JB
3782 struct symbol **sym, struct block **block,
3783 struct symtab **symtab)
96d887e8
PH
3784{
3785 return 0;
3786}
3787
3788static void
3789cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
76a01679 3790 struct block *block, struct symtab *symtab)
96d887e8
PH
3791{
3792}
76a01679 3793#endif /* GNAT_GDB */
4c4b4cd2
PH
3794\f
3795 /* Symbol Lookup */
3796
3797/* Return the result of a standard (literal, C-like) lookup of NAME in
3798 given DOMAIN, visible from lexical block BLOCK. */
3799
3800static struct symbol *
3801standard_lookup (const char *name, const struct block *block,
3802 domain_enum domain)
3803{
3804 struct symbol *sym;
3805 struct symtab *symtab;
3806
3807 if (lookup_cached_symbol (name, domain, &sym, NULL, NULL))
3808 return sym;
76a01679
JB
3809 sym =
3810 lookup_symbol_in_language (name, block, domain, language_c, 0, &symtab);
4c4b4cd2
PH
3811 cache_symbol (name, domain, sym, block_found, symtab);
3812 return sym;
3813}
3814
3815
3816/* Non-zero iff there is at least one non-function/non-enumeral symbol
3817 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
3818 since they contend in overloading in the same way. */
3819static int
3820is_nonfunction (struct ada_symbol_info syms[], int n)
3821{
3822 int i;
3823
3824 for (i = 0; i < n; i += 1)
3825 if (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_FUNC
3826 && (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_ENUM
3827 || SYMBOL_CLASS (syms[i].sym) != LOC_CONST))
14f9c5c9
AS
3828 return 1;
3829
3830 return 0;
3831}
3832
3833/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4c4b4cd2 3834 struct types. Otherwise, they may not. */
14f9c5c9
AS
3835
3836static int
d2e4a39e 3837equiv_types (struct type *type0, struct type *type1)
14f9c5c9 3838{
d2e4a39e 3839 if (type0 == type1)
14f9c5c9 3840 return 1;
d2e4a39e 3841 if (type0 == NULL || type1 == NULL
14f9c5c9
AS
3842 || TYPE_CODE (type0) != TYPE_CODE (type1))
3843 return 0;
d2e4a39e 3844 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
14f9c5c9
AS
3845 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
3846 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4c4b4cd2 3847 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
14f9c5c9 3848 return 1;
d2e4a39e 3849
14f9c5c9
AS
3850 return 0;
3851}
3852
3853/* True iff SYM0 represents the same entity as SYM1, or one that is
4c4b4cd2 3854 no more defined than that of SYM1. */
14f9c5c9
AS
3855
3856static int
d2e4a39e 3857lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
3858{
3859 if (sym0 == sym1)
3860 return 1;
176620f1 3861 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
14f9c5c9
AS
3862 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
3863 return 0;
3864
d2e4a39e 3865 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
3866 {
3867 case LOC_UNDEF:
3868 return 1;
3869 case LOC_TYPEDEF:
3870 {
4c4b4cd2
PH
3871 struct type *type0 = SYMBOL_TYPE (sym0);
3872 struct type *type1 = SYMBOL_TYPE (sym1);
3873 char *name0 = SYMBOL_LINKAGE_NAME (sym0);
3874 char *name1 = SYMBOL_LINKAGE_NAME (sym1);
3875 int len0 = strlen (name0);
3876 return
3877 TYPE_CODE (type0) == TYPE_CODE (type1)
3878 && (equiv_types (type0, type1)
3879 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
3880 && strncmp (name1 + len0, "___XV", 5) == 0));
14f9c5c9
AS
3881 }
3882 case LOC_CONST:
3883 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4c4b4cd2 3884 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
d2e4a39e
AS
3885 default:
3886 return 0;
14f9c5c9
AS
3887 }
3888}
3889
4c4b4cd2
PH
3890/* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct ada_symbol_info
3891 records in OBSTACKP. Do nothing if SYM is a duplicate. */
14f9c5c9
AS
3892
3893static void
76a01679
JB
3894add_defn_to_vec (struct obstack *obstackp,
3895 struct symbol *sym,
3896 struct block *block, struct symtab *symtab)
14f9c5c9
AS
3897{
3898 int i;
3899 size_t tmp;
4c4b4cd2 3900 struct ada_symbol_info *prevDefns = defns_collected (obstackp, 0);
14f9c5c9 3901
d2e4a39e 3902 if (SYMBOL_TYPE (sym) != NULL)
14f9c5c9 3903 CHECK_TYPEDEF (SYMBOL_TYPE (sym));
4c4b4cd2
PH
3904 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
3905 {
3906 if (lesseq_defined_than (sym, prevDefns[i].sym))
3907 return;
3908 else if (lesseq_defined_than (prevDefns[i].sym, sym))
3909 {
3910 prevDefns[i].sym = sym;
3911 prevDefns[i].block = block;
76a01679 3912 prevDefns[i].symtab = symtab;
4c4b4cd2 3913 return;
76a01679 3914 }
4c4b4cd2
PH
3915 }
3916
3917 {
3918 struct ada_symbol_info info;
3919
3920 info.sym = sym;
3921 info.block = block;
3922 info.symtab = symtab;
3923 obstack_grow (obstackp, &info, sizeof (struct ada_symbol_info));
3924 }
3925}
3926
3927/* Number of ada_symbol_info structures currently collected in
3928 current vector in *OBSTACKP. */
3929
76a01679
JB
3930static int
3931num_defns_collected (struct obstack *obstackp)
4c4b4cd2
PH
3932{
3933 return obstack_object_size (obstackp) / sizeof (struct ada_symbol_info);
3934}
3935
3936/* Vector of ada_symbol_info structures currently collected in current
3937 vector in *OBSTACKP. If FINISH, close off the vector and return
3938 its final address. */
3939
76a01679 3940static struct ada_symbol_info *
4c4b4cd2
PH
3941defns_collected (struct obstack *obstackp, int finish)
3942{
3943 if (finish)
3944 return obstack_finish (obstackp);
3945 else
3946 return (struct ada_symbol_info *) obstack_base (obstackp);
3947}
3948
96d887e8
PH
3949/* Look, in partial_symtab PST, for symbol NAME in given namespace.
3950 Check the global symbols if GLOBAL, the static symbols if not.
3951 Do wild-card match if WILD. */
4c4b4cd2 3952
96d887e8
PH
3953static struct partial_symbol *
3954ada_lookup_partial_symbol (struct partial_symtab *pst, const char *name,
3955 int global, domain_enum namespace, int wild)
4c4b4cd2 3956{
96d887e8
PH
3957 struct partial_symbol **start;
3958 int name_len = strlen (name);
3959 int length = (global ? pst->n_global_syms : pst->n_static_syms);
3960 int i;
4c4b4cd2 3961
96d887e8 3962 if (length == 0)
4c4b4cd2 3963 {
96d887e8 3964 return (NULL);
4c4b4cd2
PH
3965 }
3966
96d887e8
PH
3967 start = (global ?
3968 pst->objfile->global_psymbols.list + pst->globals_offset :
3969 pst->objfile->static_psymbols.list + pst->statics_offset);
4c4b4cd2 3970
96d887e8 3971 if (wild)
4c4b4cd2 3972 {
96d887e8
PH
3973 for (i = 0; i < length; i += 1)
3974 {
3975 struct partial_symbol *psym = start[i];
4c4b4cd2 3976
1265e4aa
JB
3977 if (SYMBOL_DOMAIN (psym) == namespace
3978 && wild_match (name, name_len, SYMBOL_LINKAGE_NAME (psym)))
96d887e8
PH
3979 return psym;
3980 }
3981 return NULL;
4c4b4cd2 3982 }
96d887e8
PH
3983 else
3984 {
3985 if (global)
3986 {
3987 int U;
3988 i = 0;
3989 U = length - 1;
3990 while (U - i > 4)
3991 {
3992 int M = (U + i) >> 1;
3993 struct partial_symbol *psym = start[M];
3994 if (SYMBOL_LINKAGE_NAME (psym)[0] < name[0])
3995 i = M + 1;
3996 else if (SYMBOL_LINKAGE_NAME (psym)[0] > name[0])
3997 U = M - 1;
3998 else if (strcmp (SYMBOL_LINKAGE_NAME (psym), name) < 0)
3999 i = M + 1;
4000 else
4001 U = M;
4002 }
4003 }
4004 else
4005 i = 0;
4c4b4cd2 4006
96d887e8
PH
4007 while (i < length)
4008 {
4009 struct partial_symbol *psym = start[i];
4c4b4cd2 4010
96d887e8
PH
4011 if (SYMBOL_DOMAIN (psym) == namespace)
4012 {
4013 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (psym), name_len);
4c4b4cd2 4014
96d887e8
PH
4015 if (cmp < 0)
4016 {
4017 if (global)
4018 break;
4019 }
4020 else if (cmp == 0
4021 && is_name_suffix (SYMBOL_LINKAGE_NAME (psym)
76a01679 4022 + name_len))
96d887e8
PH
4023 return psym;
4024 }
4025 i += 1;
4026 }
4c4b4cd2 4027
96d887e8
PH
4028 if (global)
4029 {
4030 int U;
4031 i = 0;
4032 U = length - 1;
4033 while (U - i > 4)
4034 {
4035 int M = (U + i) >> 1;
4036 struct partial_symbol *psym = start[M];
4037 if (SYMBOL_LINKAGE_NAME (psym)[0] < '_')
4038 i = M + 1;
4039 else if (SYMBOL_LINKAGE_NAME (psym)[0] > '_')
4040 U = M - 1;
4041 else if (strcmp (SYMBOL_LINKAGE_NAME (psym), "_ada_") < 0)
4042 i = M + 1;
4043 else
4044 U = M;
4045 }
4046 }
4047 else
4048 i = 0;
4c4b4cd2 4049
96d887e8
PH
4050 while (i < length)
4051 {
4052 struct partial_symbol *psym = start[i];
4c4b4cd2 4053
96d887e8
PH
4054 if (SYMBOL_DOMAIN (psym) == namespace)
4055 {
4056 int cmp;
4c4b4cd2 4057
96d887e8
PH
4058 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (psym)[0];
4059 if (cmp == 0)
4060 {
4061 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (psym), 5);
4062 if (cmp == 0)
4063 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (psym) + 5,
76a01679 4064 name_len);
96d887e8 4065 }
4c4b4cd2 4066
96d887e8
PH
4067 if (cmp < 0)
4068 {
4069 if (global)
4070 break;
4071 }
4072 else if (cmp == 0
4073 && is_name_suffix (SYMBOL_LINKAGE_NAME (psym)
76a01679 4074 + name_len + 5))
96d887e8
PH
4075 return psym;
4076 }
4077 i += 1;
4078 }
4079 }
4080 return NULL;
4c4b4cd2
PH
4081}
4082
96d887e8 4083/* Find a symbol table containing symbol SYM or NULL if none. */
4c4b4cd2 4084
96d887e8
PH
4085static struct symtab *
4086symtab_for_sym (struct symbol *sym)
4c4b4cd2 4087{
96d887e8
PH
4088 struct symtab *s;
4089 struct objfile *objfile;
4090 struct block *b;
4091 struct symbol *tmp_sym;
4092 struct dict_iterator iter;
4093 int j;
4c4b4cd2 4094
96d887e8
PH
4095 ALL_SYMTABS (objfile, s)
4096 {
4097 switch (SYMBOL_CLASS (sym))
4098 {
4099 case LOC_CONST:
4100 case LOC_STATIC:
4101 case LOC_TYPEDEF:
4102 case LOC_REGISTER:
4103 case LOC_LABEL:
4104 case LOC_BLOCK:
4105 case LOC_CONST_BYTES:
76a01679
JB
4106 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4107 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
4108 return s;
4109 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4110 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
4111 return s;
96d887e8
PH
4112 break;
4113 default:
4114 break;
4115 }
4116 switch (SYMBOL_CLASS (sym))
4117 {
4118 case LOC_REGISTER:
4119 case LOC_ARG:
4120 case LOC_REF_ARG:
4121 case LOC_REGPARM:
4122 case LOC_REGPARM_ADDR:
4123 case LOC_LOCAL:
4124 case LOC_TYPEDEF:
4125 case LOC_LOCAL_ARG:
4126 case LOC_BASEREG:
4127 case LOC_BASEREG_ARG:
4128 case LOC_COMPUTED:
4129 case LOC_COMPUTED_ARG:
76a01679
JB
4130 for (j = FIRST_LOCAL_BLOCK;
4131 j < BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)); j += 1)
4132 {
4133 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), j);
4134 ALL_BLOCK_SYMBOLS (b, iter, tmp_sym) if (sym == tmp_sym)
4135 return s;
4136 }
4137 break;
96d887e8
PH
4138 default:
4139 break;
4140 }
4141 }
4142 return NULL;
4c4b4cd2
PH
4143}
4144
96d887e8
PH
4145/* Return a minimal symbol matching NAME according to Ada decoding
4146 rules. Returns NULL if there is no such minimal symbol. Names
4147 prefixed with "standard__" are handled specially: "standard__" is
4148 first stripped off, and only static and global symbols are searched. */
4c4b4cd2 4149
96d887e8
PH
4150struct minimal_symbol *
4151ada_lookup_simple_minsym (const char *name)
4c4b4cd2 4152{
4c4b4cd2 4153 struct objfile *objfile;
96d887e8
PH
4154 struct minimal_symbol *msymbol;
4155 int wild_match;
4c4b4cd2 4156
96d887e8 4157 if (strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
4c4b4cd2 4158 {
96d887e8 4159 name += sizeof ("standard__") - 1;
4c4b4cd2 4160 wild_match = 0;
4c4b4cd2
PH
4161 }
4162 else
96d887e8 4163 wild_match = (strstr (name, "__") == NULL);
4c4b4cd2 4164
96d887e8
PH
4165 ALL_MSYMBOLS (objfile, msymbol)
4166 {
4167 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match)
4168 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4169 return msymbol;
4170 }
4c4b4cd2 4171
96d887e8
PH
4172 return NULL;
4173}
4c4b4cd2 4174
96d887e8
PH
4175/* Return up minimal symbol for NAME, folded and encoded according to
4176 Ada conventions, or NULL if none. The last two arguments are ignored. */
4c4b4cd2 4177
96d887e8
PH
4178static struct minimal_symbol *
4179ada_lookup_minimal_symbol (const char *name, const char *sfile,
76a01679 4180 struct objfile *objf)
96d887e8
PH
4181{
4182 return ada_lookup_simple_minsym (ada_encode (name));
4183}
4c4b4cd2 4184
96d887e8
PH
4185/* For all subprograms that statically enclose the subprogram of the
4186 selected frame, add symbols matching identifier NAME in DOMAIN
4187 and their blocks to the list of data in OBSTACKP, as for
4188 ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
4189 wildcard prefix. */
4c4b4cd2 4190
96d887e8
PH
4191static void
4192add_symbols_from_enclosing_procs (struct obstack *obstackp,
76a01679 4193 const char *name, domain_enum namespace,
96d887e8
PH
4194 int wild_match)
4195{
4196#ifdef HAVE_ADD_SYMBOLS_FROM_ENCLOSING_PROCS
4197 /* Use a heuristic to find the frames of enclosing subprograms: treat the
4198 pointer-sized value at location 0 from the local-variable base of a
4199 frame as a static link, and then search up the call stack for a
4200 frame with that same local-variable base. */
4201 static struct symbol static_link_sym;
4202 static struct symbol *static_link;
4203 struct value *target_link_val;
4c4b4cd2 4204
96d887e8
PH
4205 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
4206 struct frame_info *frame;
4c4b4cd2 4207
76a01679 4208 if (!target_has_stack)
96d887e8 4209 return;
4c4b4cd2 4210
96d887e8 4211 if (static_link == NULL)
4c4b4cd2 4212 {
96d887e8
PH
4213 /* Initialize the local variable symbol that stands for the
4214 static link (when there is one). */
4215 static_link = &static_link_sym;
4216 SYMBOL_LINKAGE_NAME (static_link) = "";
4217 SYMBOL_LANGUAGE (static_link) = language_unknown;
4218 SYMBOL_CLASS (static_link) = LOC_LOCAL;
4219 SYMBOL_DOMAIN (static_link) = VAR_DOMAIN;
4220 SYMBOL_TYPE (static_link) = lookup_pointer_type (builtin_type_void);
4221 SYMBOL_VALUE (static_link) =
4222 -(long) TYPE_LENGTH (SYMBOL_TYPE (static_link));
14f9c5c9
AS
4223 }
4224
96d887e8 4225 frame = get_selected_frame ();
76a01679 4226 if (frame == NULL || inside_main_func (get_frame_address_in_block (frame)))
96d887e8 4227 return;
14f9c5c9 4228
96d887e8
PH
4229 target_link_val = read_var_value (static_link, frame);
4230 while (target_link_val != NULL
76a01679
JB
4231 && num_defns_collected (obstackp) == 0
4232 && frame_relative_level (frame) <= MAX_ENCLOSING_FRAME_LEVELS)
96d887e8
PH
4233 {
4234 CORE_ADDR target_link = value_as_address (target_link_val);
4c4b4cd2 4235
96d887e8
PH
4236 frame = get_prev_frame (frame);
4237 if (frame == NULL)
76a01679 4238 break;
14f9c5c9 4239
96d887e8 4240 if (get_frame_locals_address (frame) == target_link)
76a01679
JB
4241 {
4242 struct block *block;
4243
4244 QUIT;
4245
4246 block = get_frame_block (frame, 0);
4247 while (block != NULL && block_function (block) != NULL
4248 && num_defns_collected (obstackp) == 0)
4249 {
4250 QUIT;
14f9c5c9 4251
76a01679
JB
4252 ada_add_block_symbols (obstackp, block, name, namespace,
4253 NULL, NULL, wild_match);
14f9c5c9 4254
76a01679
JB
4255 block = BLOCK_SUPERBLOCK (block);
4256 }
4257 }
14f9c5c9 4258 }
d2e4a39e 4259
96d887e8
PH
4260 do_cleanups (old_chain);
4261#endif
4262}
14f9c5c9 4263
96d887e8 4264/* FIXME: The next two routines belong in symtab.c */
14f9c5c9 4265
76a01679
JB
4266static void
4267restore_language (void *lang)
96d887e8
PH
4268{
4269 set_language ((enum language) lang);
4270}
4c4b4cd2 4271
96d887e8
PH
4272/* As for lookup_symbol, but performed as if the current language
4273 were LANG. */
4c4b4cd2 4274
96d887e8
PH
4275struct symbol *
4276lookup_symbol_in_language (const char *name, const struct block *block,
76a01679
JB
4277 domain_enum domain, enum language lang,
4278 int *is_a_field_of_this, struct symtab **symtab)
96d887e8 4279{
76a01679
JB
4280 struct cleanup *old_chain
4281 = make_cleanup (restore_language, (void *) current_language->la_language);
96d887e8
PH
4282 struct symbol *result;
4283 set_language (lang);
4284 result = lookup_symbol (name, block, domain, is_a_field_of_this, symtab);
4285 do_cleanups (old_chain);
4286 return result;
4287}
14f9c5c9 4288
96d887e8
PH
4289/* True if TYPE is definitely an artificial type supplied to a symbol
4290 for which no debugging information was given in the symbol file. */
14f9c5c9 4291
96d887e8
PH
4292static int
4293is_nondebugging_type (struct type *type)
4294{
4295 char *name = ada_type_name (type);
4296 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4297}
4c4b4cd2 4298
96d887e8
PH
4299/* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4300 duplicate other symbols in the list (The only case I know of where
4301 this happens is when object files containing stabs-in-ecoff are
4302 linked with files containing ordinary ecoff debugging symbols (or no
4303 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4304 Returns the number of items in the modified list. */
4c4b4cd2 4305
96d887e8
PH
4306static int
4307remove_extra_symbols (struct ada_symbol_info *syms, int nsyms)
4308{
4309 int i, j;
4c4b4cd2 4310
96d887e8
PH
4311 i = 0;
4312 while (i < nsyms)
4313 {
4314 if (SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL
4315 && SYMBOL_CLASS (syms[i].sym) == LOC_STATIC
4316 && is_nondebugging_type (SYMBOL_TYPE (syms[i].sym)))
4317 {
4318 for (j = 0; j < nsyms; j += 1)
4319 {
4320 if (i != j
4321 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4322 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
76a01679 4323 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0
96d887e8
PH
4324 && SYMBOL_CLASS (syms[i].sym) == SYMBOL_CLASS (syms[j].sym)
4325 && SYMBOL_VALUE_ADDRESS (syms[i].sym)
4326 == SYMBOL_VALUE_ADDRESS (syms[j].sym))
4c4b4cd2 4327 {
96d887e8
PH
4328 int k;
4329 for (k = i + 1; k < nsyms; k += 1)
76a01679 4330 syms[k - 1] = syms[k];
96d887e8
PH
4331 nsyms -= 1;
4332 goto NextSymbol;
4c4b4cd2 4333 }
4c4b4cd2 4334 }
4c4b4cd2 4335 }
96d887e8
PH
4336 i += 1;
4337 NextSymbol:
4338 ;
14f9c5c9 4339 }
96d887e8 4340 return nsyms;
14f9c5c9
AS
4341}
4342
96d887e8
PH
4343/* Given a type that corresponds to a renaming entity, use the type name
4344 to extract the scope (package name or function name, fully qualified,
4345 and following the GNAT encoding convention) where this renaming has been
4346 defined. The string returned needs to be deallocated after use. */
4c4b4cd2 4347
96d887e8
PH
4348static char *
4349xget_renaming_scope (struct type *renaming_type)
14f9c5c9 4350{
96d887e8
PH
4351 /* The renaming types adhere to the following convention:
4352 <scope>__<rename>___<XR extension>.
4353 So, to extract the scope, we search for the "___XR" extension,
4354 and then backtrack until we find the first "__". */
76a01679 4355
96d887e8
PH
4356 const char *name = type_name_no_tag (renaming_type);
4357 char *suffix = strstr (name, "___XR");
4358 char *last;
4359 int scope_len;
4360 char *scope;
14f9c5c9 4361
96d887e8
PH
4362 /* Now, backtrack a bit until we find the first "__". Start looking
4363 at suffix - 3, as the <rename> part is at least one character long. */
14f9c5c9 4364
96d887e8
PH
4365 for (last = suffix - 3; last > name; last--)
4366 if (last[0] == '_' && last[1] == '_')
4367 break;
76a01679 4368
96d887e8 4369 /* Make a copy of scope and return it. */
14f9c5c9 4370
96d887e8
PH
4371 scope_len = last - name;
4372 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
14f9c5c9 4373
96d887e8
PH
4374 strncpy (scope, name, scope_len);
4375 scope[scope_len] = '\0';
4c4b4cd2 4376
96d887e8 4377 return scope;
4c4b4cd2
PH
4378}
4379
96d887e8 4380/* Return nonzero if NAME corresponds to a package name. */
4c4b4cd2 4381
96d887e8
PH
4382static int
4383is_package_name (const char *name)
4c4b4cd2 4384{
96d887e8
PH
4385 /* Here, We take advantage of the fact that no symbols are generated
4386 for packages, while symbols are generated for each function.
4387 So the condition for NAME represent a package becomes equivalent
4388 to NAME not existing in our list of symbols. There is only one
4389 small complication with library-level functions (see below). */
4c4b4cd2 4390
96d887e8 4391 char *fun_name;
76a01679 4392
96d887e8
PH
4393 /* If it is a function that has not been defined at library level,
4394 then we should be able to look it up in the symbols. */
4395 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4396 return 0;
14f9c5c9 4397
96d887e8
PH
4398 /* Library-level function names start with "_ada_". See if function
4399 "_ada_" followed by NAME can be found. */
14f9c5c9 4400
96d887e8
PH
4401 /* Do a quick check that NAME does not contain "__", since library-level
4402 functions names can not contain "__" in them. */
4403 if (strstr (name, "__") != NULL)
4404 return 0;
4c4b4cd2 4405
b435e160 4406 fun_name = xstrprintf ("_ada_%s", name);
14f9c5c9 4407
96d887e8
PH
4408 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
4409}
14f9c5c9 4410
96d887e8
PH
4411/* Return nonzero if SYM corresponds to a renaming entity that is
4412 visible from FUNCTION_NAME. */
14f9c5c9 4413
96d887e8
PH
4414static int
4415renaming_is_visible (const struct symbol *sym, char *function_name)
4416{
4417 char *scope = xget_renaming_scope (SYMBOL_TYPE (sym));
d2e4a39e 4418
96d887e8 4419 make_cleanup (xfree, scope);
14f9c5c9 4420
96d887e8
PH
4421 /* If the rename has been defined in a package, then it is visible. */
4422 if (is_package_name (scope))
4423 return 1;
14f9c5c9 4424
96d887e8
PH
4425 /* Check that the rename is in the current function scope by checking
4426 that its name starts with SCOPE. */
76a01679 4427
96d887e8
PH
4428 /* If the function name starts with "_ada_", it means that it is
4429 a library-level function. Strip this prefix before doing the
4430 comparison, as the encoding for the renaming does not contain
4431 this prefix. */
4432 if (strncmp (function_name, "_ada_", 5) == 0)
4433 function_name += 5;
f26caa11 4434
96d887e8 4435 return (strncmp (function_name, scope, strlen (scope)) == 0);
f26caa11
PH
4436}
4437
96d887e8
PH
4438/* Iterates over the SYMS list and remove any entry that corresponds to
4439 a renaming entity that is not visible from the function associated
4440 with CURRENT_BLOCK.
4441
4442 Rationale:
4443 GNAT emits a type following a specified encoding for each renaming
4444 entity. Unfortunately, STABS currently does not support the definition
4445 of types that are local to a given lexical block, so all renamings types
4446 are emitted at library level. As a consequence, if an application
4447 contains two renaming entities using the same name, and a user tries to
4448 print the value of one of these entities, the result of the ada symbol
4449 lookup will also contain the wrong renaming type.
f26caa11 4450
96d887e8
PH
4451 This function partially covers for this limitation by attempting to
4452 remove from the SYMS list renaming symbols that should be visible
4453 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4454 method with the current information available. The implementation
4455 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4456
4457 - When the user tries to print a rename in a function while there
4458 is another rename entity defined in a package: Normally, the
4459 rename in the function has precedence over the rename in the
4460 package, so the latter should be removed from the list. This is
4461 currently not the case.
4462
4463 - This function will incorrectly remove valid renames if
4464 the CURRENT_BLOCK corresponds to a function which symbol name
4465 has been changed by an "Export" pragma. As a consequence,
4466 the user will be unable to print such rename entities. */
4c4b4cd2 4467
14f9c5c9 4468static int
96d887e8 4469remove_out_of_scope_renamings (struct ada_symbol_info *syms,
76a01679 4470 int nsyms, struct block *current_block)
4c4b4cd2
PH
4471{
4472 struct symbol *current_function;
4473 char *current_function_name;
4474 int i;
4475
4476 /* Extract the function name associated to CURRENT_BLOCK.
4477 Abort if unable to do so. */
76a01679 4478
4c4b4cd2
PH
4479 if (current_block == NULL)
4480 return nsyms;
76a01679 4481
4c4b4cd2
PH
4482 current_function = block_function (current_block);
4483 if (current_function == NULL)
4484 return nsyms;
4485
4486 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
4487 if (current_function_name == NULL)
4488 return nsyms;
4489
4490 /* Check each of the symbols, and remove it from the list if it is
4491 a type corresponding to a renaming that is out of the scope of
4492 the current block. */
4493
4494 i = 0;
4495 while (i < nsyms)
4496 {
4497 if (ada_is_object_renaming (syms[i].sym)
4498 && !renaming_is_visible (syms[i].sym, current_function_name))
4499 {
4500 int j;
4501 for (j = i + 1; j < nsyms; j++)
76a01679 4502 syms[j - 1] = syms[j];
4c4b4cd2
PH
4503 nsyms -= 1;
4504 }
4505 else
4506 i += 1;
4507 }
4508
4509 return nsyms;
4510}
4511
4512/* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing
4513 scope and in global scopes, returning the number of matches. Sets
4514 *RESULTS to point to a vector of (SYM,BLOCK,SYMTAB) triples,
4515 indicating the symbols found and the blocks and symbol tables (if
4516 any) in which they were found. This vector are transient---good only to
4517 the next call of ada_lookup_symbol_list. Any non-function/non-enumeral
4518 symbol match within the nest of blocks whose innermost member is BLOCK0,
4519 is the one match returned (no other matches in that or
4520 enclosing blocks is returned). If there are any matches in or
4521 surrounding BLOCK0, then these alone are returned. Otherwise, the
4522 search extends to global and file-scope (static) symbol tables.
4523 Names prefixed with "standard__" are handled specially: "standard__"
4524 is first stripped off, and only static and global symbols are searched. */
14f9c5c9
AS
4525
4526int
4c4b4cd2 4527ada_lookup_symbol_list (const char *name0, const struct block *block0,
76a01679
JB
4528 domain_enum namespace,
4529 struct ada_symbol_info **results)
14f9c5c9
AS
4530{
4531 struct symbol *sym;
4532 struct symtab *s;
4533 struct partial_symtab *ps;
4534 struct blockvector *bv;
4535 struct objfile *objfile;
14f9c5c9 4536 struct block *block;
4c4b4cd2 4537 const char *name;
14f9c5c9 4538 struct minimal_symbol *msymbol;
4c4b4cd2 4539 int wild_match;
14f9c5c9 4540 int cacheIfUnique;
4c4b4cd2
PH
4541 int block_depth;
4542 int ndefns;
14f9c5c9 4543
4c4b4cd2
PH
4544 obstack_free (&symbol_list_obstack, NULL);
4545 obstack_init (&symbol_list_obstack);
14f9c5c9 4546
14f9c5c9
AS
4547 cacheIfUnique = 0;
4548
4549 /* Search specified block and its superiors. */
4550
4c4b4cd2
PH
4551 wild_match = (strstr (name0, "__") == NULL);
4552 name = name0;
76a01679
JB
4553 block = (struct block *) block0; /* FIXME: No cast ought to be
4554 needed, but adding const will
4555 have a cascade effect. */
4c4b4cd2
PH
4556 if (strncmp (name0, "standard__", sizeof ("standard__") - 1) == 0)
4557 {
4558 wild_match = 0;
4559 block = NULL;
4560 name = name0 + sizeof ("standard__") - 1;
4561 }
4562
4563 block_depth = 0;
14f9c5c9
AS
4564 while (block != NULL)
4565 {
4c4b4cd2 4566 block_depth += 1;
76a01679
JB
4567 ada_add_block_symbols (&symbol_list_obstack, block, name,
4568 namespace, NULL, NULL, wild_match);
14f9c5c9 4569
4c4b4cd2
PH
4570 /* If we found a non-function match, assume that's the one. */
4571 if (is_nonfunction (defns_collected (&symbol_list_obstack, 0),
76a01679 4572 num_defns_collected (&symbol_list_obstack)))
4c4b4cd2 4573 goto done;
14f9c5c9
AS
4574
4575 block = BLOCK_SUPERBLOCK (block);
4576 }
4577
4c4b4cd2
PH
4578 /* If no luck so far, try to find NAME as a local symbol in some lexically
4579 enclosing subprogram. */
4580 if (num_defns_collected (&symbol_list_obstack) == 0 && block_depth > 2)
4581 add_symbols_from_enclosing_procs (&symbol_list_obstack,
76a01679 4582 name, namespace, wild_match);
4c4b4cd2
PH
4583
4584 /* If we found ANY matches among non-global symbols, we're done. */
14f9c5c9 4585
4c4b4cd2 4586 if (num_defns_collected (&symbol_list_obstack) > 0)
14f9c5c9 4587 goto done;
d2e4a39e 4588
14f9c5c9 4589 cacheIfUnique = 1;
4c4b4cd2
PH
4590 if (lookup_cached_symbol (name0, namespace, &sym, &block, &s))
4591 {
4592 if (sym != NULL)
4593 add_defn_to_vec (&symbol_list_obstack, sym, block, s);
4594 goto done;
4595 }
14f9c5c9
AS
4596
4597 /* Now add symbols from all global blocks: symbol tables, minimal symbol
4c4b4cd2 4598 tables, and psymtab's. */
14f9c5c9
AS
4599
4600 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
4601 {
4602 QUIT;
4603 if (!s->primary)
4604 continue;
4605 bv = BLOCKVECTOR (s);
4606 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
76a01679
JB
4607 ada_add_block_symbols (&symbol_list_obstack, block, name, namespace,
4608 objfile, s, wild_match);
d2e4a39e 4609 }
14f9c5c9 4610
4c4b4cd2 4611 if (namespace == VAR_DOMAIN)
14f9c5c9
AS
4612 {
4613 ALL_MSYMBOLS (objfile, msymbol)
d2e4a39e 4614 {
4c4b4cd2
PH
4615 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match))
4616 {
4617 switch (MSYMBOL_TYPE (msymbol))
4618 {
4619 case mst_solib_trampoline:
4620 break;
4621 default:
4622 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
4623 if (s != NULL)
4624 {
4625 int ndefns0 = num_defns_collected (&symbol_list_obstack);
4626 QUIT;
4627 bv = BLOCKVECTOR (s);
4628 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4629 ada_add_block_symbols (&symbol_list_obstack, block,
4630 SYMBOL_LINKAGE_NAME (msymbol),
4631 namespace, objfile, s, wild_match);
76a01679 4632
4c4b4cd2
PH
4633 if (num_defns_collected (&symbol_list_obstack) == ndefns0)
4634 {
4635 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
4636 ada_add_block_symbols (&symbol_list_obstack, block,
4637 SYMBOL_LINKAGE_NAME (msymbol),
4638 namespace, objfile, s,
4639 wild_match);
4640 }
4641 }
4642 }
4643 }
d2e4a39e 4644 }
14f9c5c9 4645 }
d2e4a39e 4646
14f9c5c9 4647 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
4648 {
4649 QUIT;
4650 if (!ps->readin
4c4b4cd2 4651 && ada_lookup_partial_symbol (ps, name, 1, namespace, wild_match))
d2e4a39e 4652 {
4c4b4cd2
PH
4653 s = PSYMTAB_TO_SYMTAB (ps);
4654 if (!s->primary)
4655 continue;
4656 bv = BLOCKVECTOR (s);
4657 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4658 ada_add_block_symbols (&symbol_list_obstack, block, name,
76a01679 4659 namespace, objfile, s, wild_match);
d2e4a39e
AS
4660 }
4661 }
4662
4c4b4cd2 4663 /* Now add symbols from all per-file blocks if we've gotten no hits
14f9c5c9 4664 (Not strictly correct, but perhaps better than an error).
4c4b4cd2 4665 Do the symtabs first, then check the psymtabs. */
d2e4a39e 4666
4c4b4cd2 4667 if (num_defns_collected (&symbol_list_obstack) == 0)
14f9c5c9
AS
4668 {
4669
4670 ALL_SYMTABS (objfile, s)
d2e4a39e 4671 {
4c4b4cd2
PH
4672 QUIT;
4673 if (!s->primary)
4674 continue;
4675 bv = BLOCKVECTOR (s);
4676 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
76a01679
JB
4677 ada_add_block_symbols (&symbol_list_obstack, block, name, namespace,
4678 objfile, s, wild_match);
d2e4a39e
AS
4679 }
4680
14f9c5c9 4681 ALL_PSYMTABS (objfile, ps)
d2e4a39e 4682 {
4c4b4cd2
PH
4683 QUIT;
4684 if (!ps->readin
4685 && ada_lookup_partial_symbol (ps, name, 0, namespace, wild_match))
4686 {
4687 s = PSYMTAB_TO_SYMTAB (ps);
4688 bv = BLOCKVECTOR (s);
4689 if (!s->primary)
4690 continue;
4691 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
76a01679
JB
4692 ada_add_block_symbols (&symbol_list_obstack, block, name,
4693 namespace, objfile, s, wild_match);
4c4b4cd2 4694 }
d2e4a39e
AS
4695 }
4696 }
14f9c5c9 4697
4c4b4cd2
PH
4698done:
4699 ndefns = num_defns_collected (&symbol_list_obstack);
4700 *results = defns_collected (&symbol_list_obstack, 1);
4701
4702 ndefns = remove_extra_symbols (*results, ndefns);
4703
d2e4a39e 4704 if (ndefns == 0)
4c4b4cd2 4705 cache_symbol (name0, namespace, NULL, NULL, NULL);
14f9c5c9 4706
4c4b4cd2 4707 if (ndefns == 1 && cacheIfUnique)
76a01679
JB
4708 cache_symbol (name0, namespace, (*results)[0].sym, (*results)[0].block,
4709 (*results)[0].symtab);
14f9c5c9 4710
4c4b4cd2
PH
4711 ndefns = remove_out_of_scope_renamings (*results, ndefns,
4712 (struct block *) block0);
14f9c5c9 4713
14f9c5c9
AS
4714 return ndefns;
4715}
4716
4c4b4cd2
PH
4717/* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
4718 scope and in global scopes, or NULL if none. NAME is folded and
4719 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
4720 but is disambiguated by user query if needed. *IS_A_FIELD_OF_THIS is
4721 set to 0 and *SYMTAB is set to the symbol table in which the symbol
4722 was found (in both cases, these assignments occur only if the
4723 pointers are non-null). */
4724
14f9c5c9 4725
d2e4a39e 4726struct symbol *
4c4b4cd2
PH
4727ada_lookup_symbol (const char *name, const struct block *block0,
4728 domain_enum namespace, int *is_a_field_of_this,
76a01679 4729 struct symtab **symtab)
14f9c5c9 4730{
4c4b4cd2 4731 struct ada_symbol_info *candidates;
14f9c5c9
AS
4732 int n_candidates;
4733
4c4b4cd2
PH
4734 n_candidates = ada_lookup_symbol_list (ada_encode (ada_fold_name (name)),
4735 block0, namespace, &candidates);
14f9c5c9
AS
4736
4737 if (n_candidates == 0)
4738 return NULL;
4739 else if (n_candidates != 1)
4c4b4cd2
PH
4740 user_select_syms (candidates, n_candidates, 1);
4741
4742 if (is_a_field_of_this != NULL)
4743 *is_a_field_of_this = 0;
4744
76a01679 4745 if (symtab != NULL)
4c4b4cd2
PH
4746 {
4747 *symtab = candidates[0].symtab;
76a01679
JB
4748 if (*symtab == NULL && candidates[0].block != NULL)
4749 {
4750 struct objfile *objfile;
4751 struct symtab *s;
4752 struct block *b;
4753 struct blockvector *bv;
4754
4755 /* Search the list of symtabs for one which contains the
4756 address of the start of this block. */
4757 ALL_SYMTABS (objfile, s)
4758 {
4759 bv = BLOCKVECTOR (s);
4760 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4761 if (BLOCK_START (b) <= BLOCK_START (candidates[0].block)
4762 && BLOCK_END (b) > BLOCK_START (candidates[0].block))
4763 {
4764 *symtab = s;
4765 return fixup_symbol_section (candidates[0].sym, objfile);
4766 }
4767 return fixup_symbol_section (candidates[0].sym, NULL);
4768 }
4769 }
4770 }
4c4b4cd2
PH
4771 return candidates[0].sym;
4772}
14f9c5c9 4773
4c4b4cd2
PH
4774static struct symbol *
4775ada_lookup_symbol_nonlocal (const char *name,
76a01679
JB
4776 const char *linkage_name,
4777 const struct block *block,
4778 const domain_enum domain, struct symtab **symtab)
4c4b4cd2
PH
4779{
4780 if (linkage_name == NULL)
4781 linkage_name = name;
76a01679
JB
4782 return ada_lookup_symbol (linkage_name, block_static_block (block), domain,
4783 NULL, symtab);
14f9c5c9
AS
4784}
4785
4786
4c4b4cd2
PH
4787/* True iff STR is a possible encoded suffix of a normal Ada name
4788 that is to be ignored for matching purposes. Suffixes of parallel
4789 names (e.g., XVE) are not included here. Currently, the possible suffixes
4790 are given by either of the regular expression:
4791
19c1ef65
PH
4792 (__[0-9]+)?\.[0-9]+ [nested subprogram suffix, on platforms such
4793 as GNU/Linux]
4c4b4cd2
PH
4794 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
4795 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(LJM|X([FDBUP].*|R[^T]?)))?$
14f9c5c9 4796 */
4c4b4cd2 4797
14f9c5c9 4798static int
d2e4a39e 4799is_name_suffix (const char *str)
14f9c5c9
AS
4800{
4801 int k;
4c4b4cd2
PH
4802 const char *matching;
4803 const int len = strlen (str);
4804
4805 /* (__[0-9]+)?\.[0-9]+ */
4806 matching = str;
4807 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
4808 {
4809 matching += 3;
4810 while (isdigit (matching[0]))
4811 matching += 1;
4812 if (matching[0] == '\0')
4813 return 1;
4814 }
4815
4816 if (matching[0] == '.')
4817 {
4818 matching += 1;
4819 while (isdigit (matching[0]))
4820 matching += 1;
4821 if (matching[0] == '\0')
4822 return 1;
4823 }
4824
4825 /* ___[0-9]+ */
4826 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
4827 {
4828 matching = str + 3;
4829 while (isdigit (matching[0]))
4830 matching += 1;
4831 if (matching[0] == '\0')
4832 return 1;
4833 }
4834
4835 /* ??? We should not modify STR directly, as we are doing below. This
4836 is fine in this case, but may become problematic later if we find
4837 that this alternative did not work, and want to try matching
4838 another one from the begining of STR. Since we modified it, we
4839 won't be able to find the begining of the string anymore! */
14f9c5c9
AS
4840 if (str[0] == 'X')
4841 {
4842 str += 1;
d2e4a39e 4843 while (str[0] != '_' && str[0] != '\0')
4c4b4cd2
PH
4844 {
4845 if (str[0] != 'n' && str[0] != 'b')
4846 return 0;
4847 str += 1;
4848 }
14f9c5c9
AS
4849 }
4850 if (str[0] == '\000')
4851 return 1;
d2e4a39e 4852 if (str[0] == '_')
14f9c5c9
AS
4853 {
4854 if (str[1] != '_' || str[2] == '\000')
4c4b4cd2 4855 return 0;
d2e4a39e 4856 if (str[2] == '_')
4c4b4cd2
PH
4857 {
4858 if (strcmp (str + 3, "LJM") == 0)
4859 return 1;
4860 if (str[3] != 'X')
4861 return 0;
1265e4aa
JB
4862 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
4863 || str[4] == 'U' || str[4] == 'P')
4c4b4cd2
PH
4864 return 1;
4865 if (str[4] == 'R' && str[5] != 'T')
4866 return 1;
4867 return 0;
4868 }
4869 if (!isdigit (str[2]))
4870 return 0;
4871 for (k = 3; str[k] != '\0'; k += 1)
4872 if (!isdigit (str[k]) && str[k] != '_')
4873 return 0;
14f9c5c9
AS
4874 return 1;
4875 }
4c4b4cd2 4876 if (str[0] == '$' && isdigit (str[1]))
14f9c5c9 4877 {
4c4b4cd2
PH
4878 for (k = 2; str[k] != '\0'; k += 1)
4879 if (!isdigit (str[k]) && str[k] != '_')
4880 return 0;
14f9c5c9
AS
4881 return 1;
4882 }
4883 return 0;
4884}
d2e4a39e 4885
4c4b4cd2
PH
4886/* Return nonzero if the given string starts with a dot ('.')
4887 followed by zero or more digits.
4888
4889 Note: brobecker/2003-11-10: A forward declaration has not been
4890 added at the begining of this file yet, because this function
4891 is only used to work around a problem found during wild matching
4892 when trying to match minimal symbol names against symbol names
4893 obtained from dwarf-2 data. This function is therefore currently
4894 only used in wild_match() and is likely to be deleted when the
4895 problem in dwarf-2 is fixed. */
4896
4897static int
4898is_dot_digits_suffix (const char *str)
4899{
4900 if (str[0] != '.')
4901 return 0;
4902
4903 str++;
4904 while (isdigit (str[0]))
4905 str++;
4906 return (str[0] == '\0');
4907}
4908
4909/* True if NAME represents a name of the form A1.A2....An, n>=1 and
4910 PATN[0..PATN_LEN-1] = Ak.Ak+1.....An for some k >= 1. Ignores
4911 informational suffixes of NAME (i.e., for which is_name_suffix is
4912 true). */
4913
14f9c5c9 4914static int
4c4b4cd2 4915wild_match (const char *patn0, int patn_len, const char *name0)
14f9c5c9
AS
4916{
4917 int name_len;
4c4b4cd2
PH
4918 char *name;
4919 char *patn;
4920
4921 /* FIXME: brobecker/2003-11-10: For some reason, the symbol name
4922 stored in the symbol table for nested function names is sometimes
4923 different from the name of the associated entity stored in
4924 the dwarf-2 data: This is the case for nested subprograms, where
4925 the minimal symbol name contains a trailing ".[:digit:]+" suffix,
4926 while the symbol name from the dwarf-2 data does not.
4927
4928 Although the DWARF-2 standard documents that entity names stored
4929 in the dwarf-2 data should be identical to the name as seen in
4930 the source code, GNAT takes a different approach as we already use
4931 a special encoding mechanism to convey the information so that
4932 a C debugger can still use the information generated to debug
4933 Ada programs. A corollary is that the symbol names in the dwarf-2
4934 data should match the names found in the symbol table. I therefore
4935 consider this issue as a compiler defect.
76a01679 4936
4c4b4cd2
PH
4937 Until the compiler is properly fixed, we work-around the problem
4938 by ignoring such suffixes during the match. We do so by making
4939 a copy of PATN0 and NAME0, and then by stripping such a suffix
4940 if present. We then perform the match on the resulting strings. */
4941 {
4942 char *dot;
4943 name_len = strlen (name0);
4944
4945 name = (char *) alloca ((name_len + 1) * sizeof (char));
4946 strcpy (name, name0);
4947 dot = strrchr (name, '.');
4948 if (dot != NULL && is_dot_digits_suffix (dot))
4949 *dot = '\0';
4950
4951 patn = (char *) alloca ((patn_len + 1) * sizeof (char));
4952 strncpy (patn, patn0, patn_len);
4953 patn[patn_len] = '\0';
4954 dot = strrchr (patn, '.');
4955 if (dot != NULL && is_dot_digits_suffix (dot))
4956 {
4957 *dot = '\0';
4958 patn_len = dot - patn;
4959 }
4960 }
4961
4962 /* Now perform the wild match. */
14f9c5c9
AS
4963
4964 name_len = strlen (name);
4c4b4cd2
PH
4965 if (name_len >= patn_len + 5 && strncmp (name, "_ada_", 5) == 0
4966 && strncmp (patn, name + 5, patn_len) == 0
d2e4a39e 4967 && is_name_suffix (name + patn_len + 5))
14f9c5c9
AS
4968 return 1;
4969
d2e4a39e 4970 while (name_len >= patn_len)
14f9c5c9 4971 {
4c4b4cd2
PH
4972 if (strncmp (patn, name, patn_len) == 0
4973 && is_name_suffix (name + patn_len))
4974 return 1;
4975 do
4976 {
4977 name += 1;
4978 name_len -= 1;
4979 }
d2e4a39e 4980 while (name_len > 0
4c4b4cd2 4981 && name[0] != '.' && (name[0] != '_' || name[1] != '_'));
14f9c5c9 4982 if (name_len <= 0)
4c4b4cd2 4983 return 0;
14f9c5c9 4984 if (name[0] == '_')
4c4b4cd2
PH
4985 {
4986 if (!islower (name[2]))
4987 return 0;
4988 name += 2;
4989 name_len -= 2;
4990 }
14f9c5c9 4991 else
4c4b4cd2
PH
4992 {
4993 if (!islower (name[1]))
4994 return 0;
4995 name += 1;
4996 name_len -= 1;
4997 }
96d887e8
PH
4998 }
4999
5000 return 0;
5001}
5002
5003
5004/* Add symbols from BLOCK matching identifier NAME in DOMAIN to
5005 vector *defn_symbols, updating the list of symbols in OBSTACKP
5006 (if necessary). If WILD, treat as NAME with a wildcard prefix.
5007 OBJFILE is the section containing BLOCK.
5008 SYMTAB is recorded with each symbol added. */
5009
5010static void
5011ada_add_block_symbols (struct obstack *obstackp,
76a01679 5012 struct block *block, const char *name,
96d887e8
PH
5013 domain_enum domain, struct objfile *objfile,
5014 struct symtab *symtab, int wild)
5015{
5016 struct dict_iterator iter;
5017 int name_len = strlen (name);
5018 /* A matching argument symbol, if any. */
5019 struct symbol *arg_sym;
5020 /* Set true when we find a matching non-argument symbol. */
5021 int found_sym;
5022 struct symbol *sym;
5023
5024 arg_sym = NULL;
5025 found_sym = 0;
5026 if (wild)
5027 {
5028 struct symbol *sym;
5029 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679 5030 {
1265e4aa
JB
5031 if (SYMBOL_DOMAIN (sym) == domain
5032 && wild_match (name, name_len, SYMBOL_LINKAGE_NAME (sym)))
76a01679
JB
5033 {
5034 switch (SYMBOL_CLASS (sym))
5035 {
5036 case LOC_ARG:
5037 case LOC_LOCAL_ARG:
5038 case LOC_REF_ARG:
5039 case LOC_REGPARM:
5040 case LOC_REGPARM_ADDR:
5041 case LOC_BASEREG_ARG:
5042 case LOC_COMPUTED_ARG:
5043 arg_sym = sym;
5044 break;
5045 case LOC_UNRESOLVED:
5046 continue;
5047 default:
5048 found_sym = 1;
5049 add_defn_to_vec (obstackp,
5050 fixup_symbol_section (sym, objfile),
5051 block, symtab);
5052 break;
5053 }
5054 }
5055 }
96d887e8
PH
5056 }
5057 else
5058 {
5059 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679
JB
5060 {
5061 if (SYMBOL_DOMAIN (sym) == domain)
5062 {
5063 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym), name_len);
5064 if (cmp == 0
5065 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len))
5066 {
5067 switch (SYMBOL_CLASS (sym))
5068 {
5069 case LOC_ARG:
5070 case LOC_LOCAL_ARG:
5071 case LOC_REF_ARG:
5072 case LOC_REGPARM:
5073 case LOC_REGPARM_ADDR:
5074 case LOC_BASEREG_ARG:
5075 case LOC_COMPUTED_ARG:
5076 arg_sym = sym;
5077 break;
5078 case LOC_UNRESOLVED:
5079 break;
5080 default:
5081 found_sym = 1;
5082 add_defn_to_vec (obstackp,
5083 fixup_symbol_section (sym, objfile),
5084 block, symtab);
5085 break;
5086 }
5087 }
5088 }
5089 }
96d887e8
PH
5090 }
5091
5092 if (!found_sym && arg_sym != NULL)
5093 {
76a01679
JB
5094 add_defn_to_vec (obstackp,
5095 fixup_symbol_section (arg_sym, objfile),
5096 block, symtab);
96d887e8
PH
5097 }
5098
5099 if (!wild)
5100 {
5101 arg_sym = NULL;
5102 found_sym = 0;
5103
5104 ALL_BLOCK_SYMBOLS (block, iter, sym)
76a01679
JB
5105 {
5106 if (SYMBOL_DOMAIN (sym) == domain)
5107 {
5108 int cmp;
5109
5110 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
5111 if (cmp == 0)
5112 {
5113 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (sym), 5);
5114 if (cmp == 0)
5115 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
5116 name_len);
5117 }
5118
5119 if (cmp == 0
5120 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
5121 {
5122 switch (SYMBOL_CLASS (sym))
5123 {
5124 case LOC_ARG:
5125 case LOC_LOCAL_ARG:
5126 case LOC_REF_ARG:
5127 case LOC_REGPARM:
5128 case LOC_REGPARM_ADDR:
5129 case LOC_BASEREG_ARG:
5130 case LOC_COMPUTED_ARG:
5131 arg_sym = sym;
5132 break;
5133 case LOC_UNRESOLVED:
5134 break;
5135 default:
5136 found_sym = 1;
5137 add_defn_to_vec (obstackp,
5138 fixup_symbol_section (sym, objfile),
5139 block, symtab);
5140 break;
5141 }
5142 }
5143 }
5144 end_loop2:;
5145 }
96d887e8
PH
5146
5147 /* NOTE: This really shouldn't be needed for _ada_ symbols.
5148 They aren't parameters, right? */
5149 if (!found_sym && arg_sym != NULL)
5150 {
5151 add_defn_to_vec (obstackp,
76a01679
JB
5152 fixup_symbol_section (arg_sym, objfile),
5153 block, symtab);
96d887e8
PH
5154 }
5155 }
5156}
5157\f
5158#ifdef GNAT_GDB
5159
76a01679 5160 /* Symbol Completion */
96d887e8
PH
5161
5162/* If SYM_NAME is a completion candidate for TEXT, return this symbol
5163 name in a form that's appropriate for the completion. The result
5164 does not need to be deallocated, but is only good until the next call.
5165
5166 TEXT_LEN is equal to the length of TEXT.
5167 Perform a wild match if WILD_MATCH is set.
5168 ENCODED should be set if TEXT represents the start of a symbol name
5169 in its encoded form. */
5170
5171static const char *
76a01679 5172symbol_completion_match (const char *sym_name,
96d887e8
PH
5173 const char *text, int text_len,
5174 int wild_match, int encoded)
5175{
5176 char *result;
5177 const int verbatim_match = (text[0] == '<');
5178 int match = 0;
5179
5180 if (verbatim_match)
5181 {
5182 /* Strip the leading angle bracket. */
5183 text = text + 1;
5184 text_len--;
5185 }
5186
5187 /* First, test against the fully qualified name of the symbol. */
5188
5189 if (strncmp (sym_name, text, text_len) == 0)
5190 match = 1;
5191
5192 if (match && !encoded)
5193 {
5194 /* One needed check before declaring a positive match is to verify
5195 that iff we are doing a verbatim match, the decoded version
5196 of the symbol name starts with '<'. Otherwise, this symbol name
5197 is not a suitable completion. */
5198 const char *sym_name_copy = sym_name;
5199 int has_angle_bracket;
76a01679 5200
96d887e8 5201 sym_name = ada_decode (sym_name);
76a01679 5202 has_angle_bracket = (sym_name[0] == '<');
96d887e8
PH
5203 match = (has_angle_bracket == verbatim_match);
5204 sym_name = sym_name_copy;
5205 }
5206
5207 if (match && !verbatim_match)
5208 {
5209 /* When doing non-verbatim match, another check that needs to
5210 be done is to verify that the potentially matching symbol name
5211 does not include capital letters, because the ada-mode would
5212 not be able to understand these symbol names without the
5213 angle bracket notation. */
5214 const char *tmp;
5215
5216 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
5217 if (*tmp != '\0')
5218 match = 0;
5219 }
5220
5221 /* Second: Try wild matching... */
5222
5223 if (!match && wild_match)
5224 {
5225 /* Since we are doing wild matching, this means that TEXT
5226 may represent an unqualified symbol name. We therefore must
5227 also compare TEXT against the unqualified name of the symbol. */
5228 sym_name = ada_unqualified_name (ada_decode (sym_name));
5229
5230 if (strncmp (sym_name, text, text_len) == 0)
5231 match = 1;
5232 }
5233
5234 /* Finally: If we found a mach, prepare the result to return. */
5235
5236 if (!match)
5237 return NULL;
5238
5239 if (verbatim_match)
5240 sym_name = add_angle_brackets (sym_name);
5241
5242 if (!encoded)
5243 sym_name = ada_decode (sym_name);
5244
5245 return sym_name;
5246}
5247
5248/* A companion function to ada_make_symbol_completion_list().
5249 Check if SYM_NAME represents a symbol which name would be suitable
5250 to complete TEXT (TEXT_LEN is the length of TEXT), in which case
5251 it is appended at the end of the given string vector SV.
5252
5253 ORIG_TEXT is the string original string from the user command
5254 that needs to be completed. WORD is the entire command on which
5255 completion should be performed. These two parameters are used to
5256 determine which part of the symbol name should be added to the
5257 completion vector.
5258 if WILD_MATCH is set, then wild matching is performed.
5259 ENCODED should be set if TEXT represents a symbol name in its
5260 encoded formed (in which case the completion should also be
5261 encoded). */
76a01679 5262
96d887e8
PH
5263static void
5264symbol_completion_add (struct string_vector *sv,
5265 const char *sym_name,
5266 const char *text, int text_len,
5267 const char *orig_text, const char *word,
5268 int wild_match, int encoded)
5269{
5270 const char *match = symbol_completion_match (sym_name, text, text_len,
5271 wild_match, encoded);
5272 char *completion;
5273
5274 if (match == NULL)
5275 return;
5276
5277 /* We found a match, so add the appropriate completion to the given
5278 string vector. */
5279
5280 if (word == orig_text)
5281 {
5282 completion = xmalloc (strlen (match) + 5);
5283 strcpy (completion, match);
5284 }
5285 else if (word > orig_text)
5286 {
5287 /* Return some portion of sym_name. */
5288 completion = xmalloc (strlen (match) + 5);
5289 strcpy (completion, match + (word - orig_text));
5290 }
5291 else
5292 {
5293 /* Return some of ORIG_TEXT plus sym_name. */
5294 completion = xmalloc (strlen (match) + (orig_text - word) + 5);
5295 strncpy (completion, word, orig_text - word);
5296 completion[orig_text - word] = '\0';
5297 strcat (completion, match);
5298 }
5299
5300 string_vector_append (sv, completion);
5301}
5302
5303/* Return a list of possible symbol names completing TEXT0. The list
5304 is NULL terminated. WORD is the entire command on which completion
5305 is made. */
5306
5307char **
5308ada_make_symbol_completion_list (const char *text0, const char *word)
5309{
5310 /* Note: This function is almost a copy of make_symbol_completion_list(),
5311 except it has been adapted for Ada. It is somewhat of a shame to
5312 duplicate so much code, but we don't really have the infrastructure
5313 yet to develop a language-aware version of he symbol completer... */
5314 char *text;
5315 int text_len;
5316 int wild_match;
5317 int encoded;
5318 struct string_vector result = xnew_string_vector (128);
5319 struct symbol *sym;
5320 struct symtab *s;
5321 struct partial_symtab *ps;
5322 struct minimal_symbol *msymbol;
5323 struct objfile *objfile;
5324 struct block *b, *surrounding_static_block = 0;
5325 int i;
5326 struct dict_iterator iter;
5327
5328 if (text0[0] == '<')
5329 {
5330 text = xstrdup (text0);
5331 make_cleanup (xfree, text);
5332 text_len = strlen (text);
5333 wild_match = 0;
5334 encoded = 1;
5335 }
5336 else
5337 {
5338 text = xstrdup (ada_encode (text0));
5339 make_cleanup (xfree, text);
5340 text_len = strlen (text);
5341 for (i = 0; i < text_len; i++)
5342 text[i] = tolower (text[i]);
5343
5344 /* FIXME: brobecker/2003-09-17: When we get rid of ADA_RETAIN_DOTS,
5345 we can restrict the wild_match check to searching "__" only. */
5346 wild_match = (strstr (text0, "__") == NULL
5347 && strchr (text0, '.') == NULL);
5348 encoded = (strstr (text0, "__") != NULL);
5349 }
5350
5351 /* First, look at the partial symtab symbols. */
5352 ALL_PSYMTABS (objfile, ps)
76a01679
JB
5353 {
5354 struct partial_symbol **psym;
96d887e8 5355
76a01679
JB
5356 /* If the psymtab's been read in we'll get it when we search
5357 through the blockvector. */
5358 if (ps->readin)
5359 continue;
96d887e8 5360
76a01679
JB
5361 for (psym = objfile->global_psymbols.list + ps->globals_offset;
5362 psym < (objfile->global_psymbols.list + ps->globals_offset
5363 + ps->n_global_syms); psym++)
5364 {
5365 QUIT;
5366 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (*psym),
5367 text, text_len, text0, word,
5368 wild_match, encoded);
5369 }
96d887e8 5370
76a01679
JB
5371 for (psym = objfile->static_psymbols.list + ps->statics_offset;
5372 psym < (objfile->static_psymbols.list + ps->statics_offset
5373 + ps->n_static_syms); psym++)
5374 {
5375 QUIT;
5376 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (*psym),
5377 text, text_len, text0, word,
5378 wild_match, encoded);
5379 }
96d887e8 5380 }
14f9c5c9 5381
96d887e8
PH
5382 /* At this point scan through the misc symbol vectors and add each
5383 symbol you find to the list. Eventually we want to ignore
5384 anything that isn't a text symbol (everything else will be
5385 handled by the psymtab code above). */
14f9c5c9 5386
96d887e8
PH
5387 ALL_MSYMBOLS (objfile, msymbol)
5388 {
5389 QUIT;
5390 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (msymbol),
76a01679 5391 text, text_len, text0, word, wild_match, encoded);
96d887e8 5392 }
14f9c5c9 5393
96d887e8
PH
5394 /* Search upwards from currently selected frame (so that we can
5395 complete on local vars. */
14f9c5c9 5396
96d887e8 5397 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
14f9c5c9 5398 {
96d887e8 5399 if (!BLOCK_SUPERBLOCK (b))
76a01679 5400 surrounding_static_block = b; /* For elmin of dups */
96d887e8
PH
5401
5402 ALL_BLOCK_SYMBOLS (b, iter, sym)
76a01679
JB
5403 {
5404 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (sym),
5405 text, text_len, text0, word,
5406 wild_match, encoded);
5407 }
14f9c5c9
AS
5408 }
5409
96d887e8
PH
5410 /* Go through the symtabs and check the externs and statics for
5411 symbols which match. */
14f9c5c9 5412
96d887e8
PH
5413 ALL_SYMTABS (objfile, s)
5414 {
5415 QUIT;
5416 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
5417 ALL_BLOCK_SYMBOLS (b, iter, sym)
76a01679
JB
5418 {
5419 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (sym),
5420 text, text_len, text0, word,
5421 wild_match, encoded);
5422 }
96d887e8 5423 }
14f9c5c9 5424
96d887e8
PH
5425 ALL_SYMTABS (objfile, s)
5426 {
5427 QUIT;
5428 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
5429 /* Don't do this block twice. */
5430 if (b == surrounding_static_block)
5431 continue;
5432 ALL_BLOCK_SYMBOLS (b, iter, sym)
76a01679
JB
5433 {
5434 symbol_completion_add (&result, SYMBOL_LINKAGE_NAME (sym),
5435 text, text_len, text0, word,
5436 wild_match, encoded);
5437 }
96d887e8 5438 }
261397f8 5439
96d887e8
PH
5440 /* Append the closing NULL entry. */
5441 string_vector_append (&result, NULL);
d2e4a39e 5442
96d887e8 5443 return (result.array);
14f9c5c9 5444}
96d887e8 5445
76a01679 5446#endif /* GNAT_GDB */
14f9c5c9 5447\f
96d887e8 5448#ifdef GNAT_GDB
4c4b4cd2 5449 /* Breakpoint-related */
d2e4a39e 5450
14f9c5c9
AS
5451/* Assuming that LINE is pointing at the beginning of an argument to
5452 'break', return a pointer to the delimiter for the initial segment
4c4b4cd2
PH
5453 of that name. This is the first ':', ' ', or end of LINE. */
5454
d2e4a39e
AS
5455char *
5456ada_start_decode_line_1 (char *line)
14f9c5c9 5457{
4c4b4cd2
PH
5458 /* NOTE: strpbrk would be more elegant, but I am reluctant to be
5459 the first to use such a library function in GDB code. */
d2e4a39e 5460 char *p;
14f9c5c9
AS
5461 for (p = line; *p != '\000' && *p != ' ' && *p != ':'; p += 1)
5462 ;
5463 return p;
5464}
5465
5466/* *SPEC points to a function and line number spec (as in a break
5467 command), following any initial file name specification.
5468
5469 Return all symbol table/line specfications (sals) consistent with the
4c4b4cd2 5470 information in *SPEC and FILE_TABLE in the following sense:
14f9c5c9
AS
5471 + FILE_TABLE is null, or the sal refers to a line in the file
5472 named by FILE_TABLE.
5473 + If *SPEC points to an argument with a trailing ':LINENUM',
4c4b4cd2 5474 then the sal refers to that line (or one following it as closely as
14f9c5c9 5475 possible).
4c4b4cd2 5476 + If *SPEC does not start with '*', the sal is in a function with
14f9c5c9
AS
5477 that name.
5478
5479 Returns with 0 elements if no matching non-minimal symbols found.
5480
5481 If *SPEC begins with a function name of the form <NAME>, then NAME
5482 is taken as a literal name; otherwise the function name is subject
4c4b4cd2 5483 to the usual encoding.
14f9c5c9
AS
5484
5485 *SPEC is updated to point after the function/line number specification.
5486
5487 FUNFIRSTLINE is non-zero if we desire the first line of real code
4c4b4cd2 5488 in each function.
14f9c5c9
AS
5489
5490 If CANONICAL is non-NULL, and if any of the sals require a
5491 'canonical line spec', then *CANONICAL is set to point to an array
5492 of strings, corresponding to and equal in length to the returned
4c4b4cd2
PH
5493 list of sals, such that (*CANONICAL)[i] is non-null and contains a
5494 canonical line spec for the ith returned sal, if needed. If no
5495 canonical line specs are required and CANONICAL is non-null,
14f9c5c9
AS
5496 *CANONICAL is set to NULL.
5497
5498 A 'canonical line spec' is simply a name (in the format of the
5499 breakpoint command) that uniquely identifies a breakpoint position,
5500 with no further contextual information or user selection. It is
5501 needed whenever the file name, function name, and line number
5502 information supplied is insufficient for this unique
4c4b4cd2 5503 identification. Currently overloaded functions, the name '*',
14f9c5c9
AS
5504 or static functions without a filename yield a canonical line spec.
5505 The array and the line spec strings are allocated on the heap; it
4c4b4cd2 5506 is the caller's responsibility to free them. */
14f9c5c9
AS
5507
5508struct symtabs_and_lines
d2e4a39e 5509ada_finish_decode_line_1 (char **spec, struct symtab *file_table,
4c4b4cd2 5510 int funfirstline, char ***canonical)
14f9c5c9 5511{
4c4b4cd2
PH
5512 struct ada_symbol_info *symbols;
5513 const struct block *block;
14f9c5c9
AS
5514 int n_matches, i, line_num;
5515 struct symtabs_and_lines selected;
d2e4a39e
AS
5516 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
5517 char *name;
4c4b4cd2 5518 int is_quoted;
14f9c5c9
AS
5519
5520 int len;
d2e4a39e
AS
5521 char *lower_name;
5522 char *unquoted_name;
14f9c5c9 5523
76a01679 5524 if (file_table == NULL)
4c4b4cd2 5525 block = block_static_block (get_selected_block (0));
14f9c5c9
AS
5526 else
5527 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_table), STATIC_BLOCK);
5528
5529 if (canonical != NULL)
d2e4a39e 5530 *canonical = (char **) NULL;
14f9c5c9 5531
4c4b4cd2
PH
5532 is_quoted = (**spec && strchr (get_gdb_completer_quote_characters (),
5533 **spec) != NULL);
5534
14f9c5c9 5535 name = *spec;
d2e4a39e 5536 if (**spec == '*')
14f9c5c9
AS
5537 *spec += 1;
5538 else
5539 {
4c4b4cd2
PH
5540 if (is_quoted)
5541 *spec = skip_quoted (*spec);
1265e4aa
JB
5542 while (**spec != '\000'
5543 && !strchr (ada_completer_word_break_characters, **spec))
4c4b4cd2 5544 *spec += 1;
14f9c5c9
AS
5545 }
5546 len = *spec - name;
5547
5548 line_num = -1;
5549 if (file_table != NULL && (*spec)[0] == ':' && isdigit ((*spec)[1]))
5550 {
5551 line_num = strtol (*spec + 1, spec, 10);
d2e4a39e 5552 while (**spec == ' ' || **spec == '\t')
4c4b4cd2 5553 *spec += 1;
14f9c5c9
AS
5554 }
5555
d2e4a39e 5556 if (name[0] == '*')
14f9c5c9
AS
5557 {
5558 if (line_num == -1)
4c4b4cd2 5559 error ("Wild-card function with no line number or file name.");
14f9c5c9 5560
4c4b4cd2
PH
5561 return ada_sals_for_line (file_table->filename, line_num,
5562 funfirstline, canonical, 0);
14f9c5c9
AS
5563 }
5564
5565 if (name[0] == '\'')
5566 {
5567 name += 1;
5568 len -= 2;
5569 }
5570
5571 if (name[0] == '<')
5572 {
d2e4a39e
AS
5573 unquoted_name = (char *) alloca (len - 1);
5574 memcpy (unquoted_name, name + 1, len - 2);
5575 unquoted_name[len - 2] = '\000';
14f9c5c9
AS
5576 lower_name = NULL;
5577 }
5578 else
5579 {
d2e4a39e 5580 unquoted_name = (char *) alloca (len + 1);
14f9c5c9
AS
5581 memcpy (unquoted_name, name, len);
5582 unquoted_name[len] = '\000';
d2e4a39e 5583 lower_name = (char *) alloca (len + 1);
14f9c5c9 5584 for (i = 0; i < len; i += 1)
4c4b4cd2 5585 lower_name[i] = tolower (name[i]);
14f9c5c9
AS
5586 lower_name[len] = '\000';
5587 }
5588
5589 n_matches = 0;
d2e4a39e 5590 if (lower_name != NULL)
4c4b4cd2
PH
5591 n_matches = ada_lookup_symbol_list (ada_encode (lower_name), block,
5592 VAR_DOMAIN, &symbols);
14f9c5c9 5593 if (n_matches == 0)
d2e4a39e 5594 n_matches = ada_lookup_symbol_list (unquoted_name, block,
4c4b4cd2 5595 VAR_DOMAIN, &symbols);
14f9c5c9
AS
5596 if (n_matches == 0 && line_num >= 0)
5597 error ("No line number information found for %s.", unquoted_name);
5598 else if (n_matches == 0)
5599 {
5600#ifdef HPPA_COMPILER_BUG
5601 /* FIXME: See comment in symtab.c::decode_line_1 */
5602#undef volatile
5603 volatile struct symtab_and_line val;
4c4b4cd2 5604#define volatile /*nothing */
14f9c5c9
AS
5605#else
5606 struct symtab_and_line val;
5607#endif
d2e4a39e 5608 struct minimal_symbol *msymbol;
14f9c5c9 5609
fe39c653 5610 init_sal (&val);
14f9c5c9
AS
5611
5612 msymbol = NULL;
d2e4a39e 5613 if (lower_name != NULL)
4c4b4cd2 5614 msymbol = ada_lookup_simple_minsym (ada_encode (lower_name));
14f9c5c9 5615 if (msymbol == NULL)
4c4b4cd2 5616 msymbol = ada_lookup_simple_minsym (unquoted_name);
14f9c5c9 5617 if (msymbol != NULL)
4c4b4cd2
PH
5618 {
5619 val.pc = SYMBOL_VALUE_ADDRESS (msymbol);
5620 val.section = SYMBOL_BFD_SECTION (msymbol);
5621 if (funfirstline)
5622 {
782263ab 5623 val.pc += DEPRECATED_FUNCTION_START_OFFSET;
4c4b4cd2
PH
5624 SKIP_PROLOGUE (val.pc);
5625 }
5626 selected.sals = (struct symtab_and_line *)
5627 xmalloc (sizeof (struct symtab_and_line));
5628 selected.sals[0] = val;
5629 selected.nelts = 1;
5630 return selected;
5631 }
d2e4a39e 5632
1265e4aa
JB
5633 if (!have_full_symbols ()
5634 && !have_partial_symbols () && !have_minimal_symbols ())
4c4b4cd2 5635 error ("No symbol table is loaded. Use the \"file\" command.");
14f9c5c9
AS
5636
5637 error ("Function \"%s\" not defined.", unquoted_name);
4c4b4cd2 5638 return selected; /* for lint */
14f9c5c9
AS
5639 }
5640
5641 if (line_num >= 0)
5642 {
4c4b4cd2
PH
5643 struct symtabs_and_lines best_sal =
5644 find_sal_from_funcs_and_line (file_table->filename, line_num,
5645 symbols, n_matches);
5646 if (funfirstline)
5647 adjust_pc_past_prologue (&best_sal.sals[0].pc);
5648 return best_sal;
14f9c5c9
AS
5649 }
5650 else
5651 {
76a01679 5652 selected.nelts = user_select_syms (symbols, n_matches, n_matches);
14f9c5c9
AS
5653 }
5654
d2e4a39e 5655 selected.sals = (struct symtab_and_line *)
14f9c5c9
AS
5656 xmalloc (sizeof (struct symtab_and_line) * selected.nelts);
5657 memset (selected.sals, 0, selected.nelts * sizeof (selected.sals[i]));
aacb1f0a 5658 make_cleanup (xfree, selected.sals);
14f9c5c9
AS
5659
5660 i = 0;
5661 while (i < selected.nelts)
5662 {
4c4b4cd2 5663 if (SYMBOL_CLASS (symbols[i].sym) == LOC_BLOCK)
76a01679
JB
5664 selected.sals[i]
5665 = find_function_start_sal (symbols[i].sym, funfirstline);
4c4b4cd2
PH
5666 else if (SYMBOL_LINE (symbols[i].sym) != 0)
5667 {
76a01679
JB
5668 selected.sals[i].symtab =
5669 symbols[i].symtab
5670 ? symbols[i].symtab : symtab_for_sym (symbols[i].sym);
4c4b4cd2
PH
5671 selected.sals[i].line = SYMBOL_LINE (symbols[i].sym);
5672 }
14f9c5c9 5673 else if (line_num >= 0)
4c4b4cd2
PH
5674 {
5675 /* Ignore this choice */
5676 symbols[i] = symbols[selected.nelts - 1];
5677 selected.nelts -= 1;
5678 continue;
5679 }
d2e4a39e 5680 else
4c4b4cd2 5681 error ("Line number not known for symbol \"%s\"", unquoted_name);
14f9c5c9
AS
5682 i += 1;
5683 }
5684
5685 if (canonical != NULL && (line_num >= 0 || n_matches > 1))
5686 {
d2e4a39e 5687 *canonical = (char **) xmalloc (sizeof (char *) * selected.nelts);
14f9c5c9 5688 for (i = 0; i < selected.nelts; i += 1)
4c4b4cd2
PH
5689 (*canonical)[i] =
5690 extended_canonical_line_spec (selected.sals[i],
5691 SYMBOL_PRINT_NAME (symbols[i].sym));
14f9c5c9 5692 }
d2e4a39e 5693
14f9c5c9
AS
5694 discard_cleanups (old_chain);
5695 return selected;
d2e4a39e
AS
5696}
5697
14f9c5c9 5698/* The (single) sal corresponding to line LINE_NUM in a symbol table
4c4b4cd2
PH
5699 with file name FILENAME that occurs in one of the functions listed
5700 in the symbol fields of SYMBOLS[0 .. NSYMS-1]. */
5701
14f9c5c9 5702static struct symtabs_and_lines
d2e4a39e 5703find_sal_from_funcs_and_line (const char *filename, int line_num,
4c4b4cd2 5704 struct ada_symbol_info *symbols, int nsyms)
14f9c5c9
AS
5705{
5706 struct symtabs_and_lines sals;
5707 int best_index, best;
d2e4a39e
AS
5708 struct linetable *best_linetable;
5709 struct objfile *objfile;
5710 struct symtab *s;
5711 struct symtab *best_symtab;
14f9c5c9
AS
5712
5713 read_all_symtabs (filename);
5714
d2e4a39e
AS
5715 best_index = 0;
5716 best_linetable = NULL;
5717 best_symtab = NULL;
14f9c5c9
AS
5718 best = 0;
5719 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
5720 {
5721 struct linetable *l;
5722 int ind, exact;
14f9c5c9 5723
d2e4a39e 5724 QUIT;
14f9c5c9 5725
4c4b4cd2 5726 if (strcmp (filename, s->filename) != 0)
d2e4a39e
AS
5727 continue;
5728 l = LINETABLE (s);
5729 ind = find_line_in_linetable (l, line_num, symbols, nsyms, &exact);
5730 if (ind >= 0)
5731 {
4c4b4cd2
PH
5732 if (exact)
5733 {
5734 best_index = ind;
5735 best_linetable = l;
5736 best_symtab = s;
5737 goto done;
5738 }
5739 if (best == 0 || l->item[ind].line < best)
5740 {
5741 best = l->item[ind].line;
5742 best_index = ind;
5743 best_linetable = l;
5744 best_symtab = s;
5745 }
d2e4a39e
AS
5746 }
5747 }
14f9c5c9
AS
5748
5749 if (best == 0)
5750 error ("Line number not found in designated function.");
5751
d2e4a39e
AS
5752done:
5753
14f9c5c9 5754 sals.nelts = 1;
d2e4a39e 5755 sals.sals = (struct symtab_and_line *) xmalloc (sizeof (sals.sals[0]));
14f9c5c9 5756
fe39c653 5757 init_sal (&sals.sals[0]);
d2e4a39e 5758
14f9c5c9
AS
5759 sals.sals[0].line = best_linetable->item[best_index].line;
5760 sals.sals[0].pc = best_linetable->item[best_index].pc;
5761 sals.sals[0].symtab = best_symtab;
5762
5763 return sals;
5764}
5765
5766/* Return the index in LINETABLE of the best match for LINE_NUM whose
4c4b4cd2
PH
5767 pc falls within one of the functions denoted by the symbol fields
5768 of SYMBOLS[0..NSYMS-1]. Set *EXACTP to 1 if the match is exact,
5769 and 0 otherwise. */
5770
14f9c5c9 5771static int
d2e4a39e 5772find_line_in_linetable (struct linetable *linetable, int line_num,
76a01679
JB
5773 struct ada_symbol_info *symbols, int nsyms,
5774 int *exactp)
14f9c5c9
AS
5775{
5776 int i, len, best_index, best;
5777
5778 if (line_num <= 0 || linetable == NULL)
5779 return -1;
5780
5781 len = linetable->nitems;
5782 for (i = 0, best_index = -1, best = 0; i < len; i += 1)
5783 {
5784 int k;
d2e4a39e 5785 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
5786
5787 for (k = 0; k < nsyms; k += 1)
4c4b4cd2 5788 {
76a01679
JB
5789 if (symbols[k].sym != NULL
5790 && SYMBOL_CLASS (symbols[k].sym) == LOC_BLOCK
4c4b4cd2
PH
5791 && item->pc >= BLOCK_START (SYMBOL_BLOCK_VALUE (symbols[k].sym))
5792 && item->pc < BLOCK_END (SYMBOL_BLOCK_VALUE (symbols[k].sym)))
5793 goto candidate;
5794 }
14f9c5c9
AS
5795 continue;
5796
5797 candidate:
5798
5799 if (item->line == line_num)
4c4b4cd2
PH
5800 {
5801 *exactp = 1;
5802 return i;
5803 }
14f9c5c9
AS
5804
5805 if (item->line > line_num && (best == 0 || item->line < best))
4c4b4cd2
PH
5806 {
5807 best = item->line;
5808 best_index = i;
5809 }
14f9c5c9
AS
5810 }
5811
5812 *exactp = 0;
5813 return best_index;
5814}
5815
5816/* Find the smallest k >= LINE_NUM such that k is a line number in
5817 LINETABLE, and k falls strictly within a named function that begins at
4c4b4cd2
PH
5818 or before LINE_NUM. Return -1 if there is no such k. */
5819
14f9c5c9 5820static int
d2e4a39e 5821nearest_line_number_in_linetable (struct linetable *linetable, int line_num)
14f9c5c9
AS
5822{
5823 int i, len, best;
5824
5825 if (line_num <= 0 || linetable == NULL || linetable->nitems == 0)
5826 return -1;
5827 len = linetable->nitems;
5828
d2e4a39e
AS
5829 i = 0;
5830 best = INT_MAX;
14f9c5c9
AS
5831 while (i < len)
5832 {
d2e4a39e 5833 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
5834
5835 if (item->line >= line_num && item->line < best)
4c4b4cd2
PH
5836 {
5837 char *func_name;
5838 CORE_ADDR start, end;
5839
5840 func_name = NULL;
5841 find_pc_partial_function (item->pc, &func_name, &start, &end);
5842
5843 if (func_name != NULL && item->pc < end)
5844 {
5845 if (item->line == line_num)
5846 return line_num;
5847 else
5848 {
5849 struct symbol *sym =
5850 standard_lookup (func_name, NULL, VAR_DOMAIN);
5851 if (is_plausible_func_for_line (sym, line_num))
5852 best = item->line;
5853 else
5854 {
5855 do
5856 i += 1;
5857 while (i < len && linetable->item[i].pc < end);
5858 continue;
5859 }
5860 }
5861 }
5862 }
14f9c5c9
AS
5863
5864 i += 1;
5865 }
5866
5867 return (best == INT_MAX) ? -1 : best;
5868}
5869
5870
4c4b4cd2 5871/* Return the next higher index, k, into LINETABLE such that k > IND,
14f9c5c9 5872 entry k in LINETABLE has a line number equal to LINE_NUM, k
4c4b4cd2 5873 corresponds to a PC that is in a function different from that
14f9c5c9 5874 corresponding to IND, and falls strictly within a named function
4c4b4cd2
PH
5875 that begins at a line at or preceding STARTING_LINE.
5876 Return -1 if there is no such k.
5877 IND == -1 corresponds to no function. */
14f9c5c9
AS
5878
5879static int
d2e4a39e 5880find_next_line_in_linetable (struct linetable *linetable, int line_num,
4c4b4cd2 5881 int starting_line, int ind)
14f9c5c9
AS
5882{
5883 int i, len;
5884
5885 if (line_num <= 0 || linetable == NULL || ind >= linetable->nitems)
5886 return -1;
5887 len = linetable->nitems;
5888
d2e4a39e 5889 if (ind >= 0)
14f9c5c9
AS
5890 {
5891 CORE_ADDR start, end;
5892
5893 if (find_pc_partial_function (linetable->item[ind].pc,
4c4b4cd2
PH
5894 (char **) NULL, &start, &end))
5895 {
5896 while (ind < len && linetable->item[ind].pc < end)
5897 ind += 1;
5898 }
14f9c5c9 5899 else
4c4b4cd2 5900 ind += 1;
14f9c5c9
AS
5901 }
5902 else
5903 ind = 0;
5904
5905 i = ind;
5906 while (i < len)
5907 {
d2e4a39e 5908 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
5909
5910 if (item->line >= line_num)
4c4b4cd2
PH
5911 {
5912 char *func_name;
5913 CORE_ADDR start, end;
5914
5915 func_name = NULL;
5916 find_pc_partial_function (item->pc, &func_name, &start, &end);
5917
5918 if (func_name != NULL && item->pc < end)
5919 {
5920 if (item->line == line_num)
5921 {
5922 struct symbol *sym =
5923 standard_lookup (func_name, NULL, VAR_DOMAIN);
5924 if (is_plausible_func_for_line (sym, starting_line))
5925 return i;
5926 else
5927 {
5928 while ((i + 1) < len && linetable->item[i + 1].pc < end)
5929 i += 1;
5930 }
5931 }
5932 }
5933 }
14f9c5c9
AS
5934 i += 1;
5935 }
5936
5937 return -1;
5938}
5939
5940/* True iff function symbol SYM starts somewhere at or before line #
4c4b4cd2
PH
5941 LINE_NUM. */
5942
14f9c5c9 5943static int
d2e4a39e 5944is_plausible_func_for_line (struct symbol *sym, int line_num)
14f9c5c9
AS
5945{
5946 struct symtab_and_line start_sal;
5947
5948 if (sym == NULL)
5949 return 0;
5950
5951 start_sal = find_function_start_sal (sym, 0);
5952
5953 return (start_sal.line != 0 && line_num >= start_sal.line);
5954}
5955
14f9c5c9 5956/* Read in all symbol tables corresponding to partial symbol tables
4c4b4cd2
PH
5957 with file name FILENAME. */
5958
14f9c5c9 5959static void
d2e4a39e 5960read_all_symtabs (const char *filename)
14f9c5c9 5961{
d2e4a39e
AS
5962 struct partial_symtab *ps;
5963 struct objfile *objfile;
14f9c5c9
AS
5964
5965 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
5966 {
5967 QUIT;
14f9c5c9 5968
4c4b4cd2 5969 if (strcmp (filename, ps->filename) == 0)
d2e4a39e
AS
5970 PSYMTAB_TO_SYMTAB (ps);
5971 }
14f9c5c9
AS
5972}
5973
5974/* All sals corresponding to line LINE_NUM in a symbol table from file
4c4b4cd2
PH
5975 FILENAME, as filtered by the user. Filter out any lines that
5976 reside in functions with "suppressed" names (not corresponding to
5977 explicit Ada functions), if there is at least one in a function
5978 with a non-suppressed name. If CANONICAL is not null, set
5979 it to a corresponding array of canonical line specs.
5980 If ONE_LOCATION_ONLY is set and several matches are found for
5981 the given location, then automatically select the first match found
5982 instead of asking the user which instance should be returned. */
5983
5984struct symtabs_and_lines
5985ada_sals_for_line (const char *filename, int line_num,
76a01679 5986 int funfirstline, char ***canonical, int one_location_only)
14f9c5c9
AS
5987{
5988 struct symtabs_and_lines result;
d2e4a39e
AS
5989 struct objfile *objfile;
5990 struct symtab *s;
5991 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
14f9c5c9
AS
5992 size_t len;
5993
5994 read_all_symtabs (filename);
5995
d2e4a39e
AS
5996 result.sals =
5997 (struct symtab_and_line *) xmalloc (4 * sizeof (result.sals[0]));
14f9c5c9
AS
5998 result.nelts = 0;
5999 len = 4;
6000 make_cleanup (free_current_contents, &result.sals);
6001
d2e4a39e
AS
6002 ALL_SYMTABS (objfile, s)
6003 {
6004 int ind, target_line_num;
14f9c5c9 6005
d2e4a39e 6006 QUIT;
14f9c5c9 6007
4c4b4cd2 6008 if (strcmp (s->filename, filename) != 0)
d2e4a39e 6009 continue;
14f9c5c9 6010
d2e4a39e
AS
6011 target_line_num =
6012 nearest_line_number_in_linetable (LINETABLE (s), line_num);
6013 if (target_line_num == -1)
6014 continue;
14f9c5c9 6015
d2e4a39e
AS
6016 ind = -1;
6017 while (1)
6018 {
4c4b4cd2
PH
6019 ind =
6020 find_next_line_in_linetable (LINETABLE (s),
6021 target_line_num, line_num, ind);
14f9c5c9 6022
4c4b4cd2
PH
6023 if (ind < 0)
6024 break;
6025
6026 GROW_VECT (result.sals, len, result.nelts + 1);
6027 init_sal (&result.sals[result.nelts]);
6028 result.sals[result.nelts].line = line_num;
6029 result.sals[result.nelts].pc = LINETABLE (s)->item[ind].pc;
6030 result.sals[result.nelts].symtab = s;
d2e4a39e 6031
4c4b4cd2
PH
6032 if (funfirstline)
6033 adjust_pc_past_prologue (&result.sals[result.nelts].pc);
6034
6035 result.nelts += 1;
d2e4a39e
AS
6036 }
6037 }
14f9c5c9
AS
6038
6039 if (canonical != NULL || result.nelts > 1)
6040 {
4c4b4cd2 6041 int k, j, n;
d2e4a39e 6042 char **func_names = (char **) alloca (result.nelts * sizeof (char *));
14f9c5c9 6043 int first_choice = (result.nelts > 1) ? 2 : 1;
d2e4a39e
AS
6044 int *choices = (int *) alloca (result.nelts * sizeof (int));
6045
6046 for (k = 0; k < result.nelts; k += 1)
4c4b4cd2
PH
6047 {
6048 find_pc_partial_function (result.sals[k].pc, &func_names[k],
6049 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
6050 if (func_names[k] == NULL)
6051 error ("Could not find function for one or more breakpoints.");
6052 }
6053
6054 /* Remove suppressed names, unless all are suppressed. */
6055 for (j = 0; j < result.nelts; j += 1)
6056 if (!is_suppressed_name (func_names[j]))
6057 {
6058 /* At least one name is unsuppressed, so remove all
6059 suppressed names. */
6060 for (k = n = 0; k < result.nelts; k += 1)
6061 if (!is_suppressed_name (func_names[k]))
6062 {
6063 func_names[n] = func_names[k];
6064 result.sals[n] = result.sals[k];
6065 n += 1;
6066 }
6067 result.nelts = n;
6068 break;
6069 }
d2e4a39e
AS
6070
6071 if (result.nelts > 1)
4c4b4cd2
PH
6072 {
6073 if (one_location_only)
6074 {
6075 /* Automatically select the first of all possible choices. */
6076 n = 1;
6077 choices[0] = 0;
6078 }
6079 else
6080 {
6081 printf_unfiltered ("[0] cancel\n");
6082 if (result.nelts > 1)
6083 printf_unfiltered ("[1] all\n");
6084 for (k = 0; k < result.nelts; k += 1)
6085 printf_unfiltered ("[%d] %s\n", k + first_choice,
6086 ada_decode (func_names[k]));
6087
6088 n = get_selections (choices, result.nelts, result.nelts,
6089 result.nelts > 1, "instance-choice");
6090 }
6091
6092 for (k = 0; k < n; k += 1)
6093 {
6094 result.sals[k] = result.sals[choices[k]];
6095 func_names[k] = func_names[choices[k]];
6096 }
6097 result.nelts = n;
6098 }
6099
6100 if (canonical != NULL && result.nelts == 0)
6101 *canonical = NULL;
6102 else if (canonical != NULL)
6103 {
6104 *canonical = (char **) xmalloc (result.nelts * sizeof (char **));
6105 make_cleanup (xfree, *canonical);
6106 for (k = 0; k < result.nelts; k += 1)
6107 {
6108 (*canonical)[k] =
6109 extended_canonical_line_spec (result.sals[k], func_names[k]);
6110 if ((*canonical)[k] == NULL)
6111 error ("Could not locate one or more breakpoints.");
6112 make_cleanup (xfree, (*canonical)[k]);
6113 }
6114 }
6115 }
6116
6117 if (result.nelts == 0)
6118 {
6119 do_cleanups (old_chain);
6120 result.sals = NULL;
14f9c5c9 6121 }
4c4b4cd2
PH
6122 else
6123 discard_cleanups (old_chain);
14f9c5c9
AS
6124 return result;
6125}
6126
6127
6128/* A canonical line specification of the form FILE:NAME:LINENUM for
6129 symbol table and line data SAL. NULL if insufficient
4c4b4cd2
PH
6130 information. The caller is responsible for releasing any space
6131 allocated. */
14f9c5c9 6132
d2e4a39e
AS
6133static char *
6134extended_canonical_line_spec (struct symtab_and_line sal, const char *name)
14f9c5c9 6135{
d2e4a39e 6136 char *r;
14f9c5c9 6137
d2e4a39e 6138 if (sal.symtab == NULL || sal.symtab->filename == NULL || sal.line <= 0)
14f9c5c9
AS
6139 return NULL;
6140
d2e4a39e 6141 r = (char *) xmalloc (strlen (name) + strlen (sal.symtab->filename)
4c4b4cd2 6142 + sizeof (sal.line) * 3 + 3);
14f9c5c9
AS
6143 sprintf (r, "%s:'%s':%d", sal.symtab->filename, name, sal.line);
6144 return r;
6145}
6146
4c4b4cd2
PH
6147/* Return type of Ada breakpoint associated with bp_stat:
6148 0 if not an Ada-specific breakpoint, 1 for break on specific exception,
6149 2 for break on unhandled exception, 3 for assert. */
6150
6151static int
6152ada_exception_breakpoint_type (bpstat bs)
6153{
76a01679
JB
6154 return ((!bs || !bs->breakpoint_at) ? 0
6155 : bs->breakpoint_at->break_on_exception);
4c4b4cd2
PH
6156}
6157
6158/* True iff FRAME is very likely to be that of a function that is
6159 part of the runtime system. This is all very heuristic, but is
6160 intended to be used as advice as to what frames are uninteresting
6161 to most users. */
6162
6163static int
6164is_known_support_routine (struct frame_info *frame)
6165{
6166 struct frame_info *next_frame = get_next_frame (frame);
6167 /* If frame is not innermost, that normally means that frame->pc
6168 points to *after* the call instruction, and we want to get the line
6169 containing the call, never the next line. But if the next frame is
6170 a signal_handler_caller or a dummy frame, then the next frame was
6171 not entered as the result of a call, and we want to get the line
6172 containing frame->pc. */
76a01679 6173 const int pc_is_after_call =
4c4b4cd2
PH
6174 next_frame != NULL
6175 && get_frame_type (next_frame) != SIGTRAMP_FRAME
6176 && get_frame_type (next_frame) != DUMMY_FRAME;
76a01679 6177 struct symtab_and_line sal
4c4b4cd2
PH
6178 = find_pc_line (get_frame_pc (frame), pc_is_after_call);
6179 char *func_name;
6180 int i;
6181 struct stat st;
6182
6183 /* The heuristic:
76a01679
JB
6184 1. The symtab is null (indicating no debugging symbols)
6185 2. The symtab's filename does not exist.
6186 3. The object file's name is one of the standard libraries.
6187 4. The symtab's file name has the form of an Ada library source file.
6188 5. The function at frame's PC has a GNAT-compiler-generated name. */
4c4b4cd2
PH
6189
6190 if (sal.symtab == NULL)
6191 return 1;
6192
6193 /* On some systems (e.g. VxWorks), the kernel contains debugging
6194 symbols; in this case, the filename referenced by these symbols
6195 does not exists. */
6196
6197 if (stat (sal.symtab->filename, &st))
6198 return 1;
6199
6200 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
6201 {
6202 re_comp (known_runtime_file_name_patterns[i]);
6203 if (re_exec (sal.symtab->filename))
6204 return 1;
6205 }
6206 if (sal.symtab->objfile != NULL)
6207 {
6208 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
6209 {
6210 re_comp (known_runtime_file_name_patterns[i]);
6211 if (re_exec (sal.symtab->objfile->name))
6212 return 1;
6213 }
6214 }
6215
6216 /* If the frame PC points after the call instruction, then we need to
6217 decrement it in order to search for the function associated to this
6218 PC. Otherwise, if the associated call was the last instruction of
6219 the function, we might either find the wrong function or even fail
6220 during the function name lookup. */
6221 if (pc_is_after_call)
6222 func_name = function_name_from_pc (get_frame_pc (frame) - 1);
6223 else
6224 func_name = function_name_from_pc (get_frame_pc (frame));
6225
6226 if (func_name == NULL)
6227 return 1;
6228
6229 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
6230 {
6231 re_comp (known_auxiliary_function_name_patterns[i]);
6232 if (re_exec (func_name))
6233 return 1;
6234 }
6235
6236 return 0;
6237}
6238
6239/* Find the first frame that contains debugging information and that is not
6240 part of the Ada run-time, starting from FI and moving upward. */
6241
6242void
6243ada_find_printable_frame (struct frame_info *fi)
14f9c5c9 6244{
4c4b4cd2
PH
6245 for (; fi != NULL; fi = get_prev_frame (fi))
6246 {
6247 if (!is_known_support_routine (fi))
6248 {
6249 select_frame (fi);
6250 break;
6251 }
6252 }
14f9c5c9 6253
4c4b4cd2 6254}
d2e4a39e 6255
4c4b4cd2
PH
6256/* Name found for exception associated with last bpstat sent to
6257 ada_adjust_exception_stop. Set to the null string if that bpstat
6258 did not correspond to an Ada exception or no name could be found. */
14f9c5c9 6259
4c4b4cd2 6260static char last_exception_name[256];
14f9c5c9 6261
4c4b4cd2
PH
6262/* If BS indicates a stop in an Ada exception, try to go up to a frame
6263 that will be meaningful to the user, and save the name of the last
6264 exception (truncated, if necessary) in last_exception_name. */
14f9c5c9 6265
4c4b4cd2
PH
6266void
6267ada_adjust_exception_stop (bpstat bs)
6268{
6269 CORE_ADDR addr;
6270 struct frame_info *fi;
6271 int frame_level;
6272 char *selected_frame_func;
14f9c5c9 6273
4c4b4cd2
PH
6274 addr = 0;
6275 last_exception_name[0] = '\0';
6276 fi = get_selected_frame ();
6277 selected_frame_func = function_name_from_pc (get_frame_pc (fi));
6278
6279 switch (ada_exception_breakpoint_type (bs))
d2e4a39e 6280 {
4c4b4cd2
PH
6281 default:
6282 return;
6283 case 1:
6284 break;
6285 case 2:
6286 /* Unhandled exceptions. Select the frame corresponding to
6287 ada.exceptions.process_raise_exception. This frame is at
6288 least 2 levels up, so we simply skip the first 2 frames
6289 without checking the name of their associated function. */
6290 for (frame_level = 0; frame_level < 2; frame_level += 1)
6291 if (fi != NULL)
76a01679 6292 fi = get_prev_frame (fi);
4c4b4cd2
PH
6293 while (fi != NULL)
6294 {
6295 const char *func_name = function_name_from_pc (get_frame_pc (fi));
6296 if (func_name != NULL
6297 && strcmp (func_name, process_raise_exception_name) == 0)
76a01679 6298 break; /* We found the frame we were looking for... */
4c4b4cd2
PH
6299 fi = get_prev_frame (fi);
6300 }
6301 if (fi == NULL)
76a01679 6302 break;
4c4b4cd2
PH
6303 select_frame (fi);
6304 break;
d2e4a39e 6305 }
14f9c5c9 6306
76a01679 6307 addr = parse_and_eval_address ("e.full_name");
4c4b4cd2
PH
6308
6309 if (addr != 0)
76a01679 6310 read_memory (addr, last_exception_name, sizeof (last_exception_name) - 1);
4c4b4cd2
PH
6311 last_exception_name[sizeof (last_exception_name) - 1] = '\0';
6312 ada_find_printable_frame (get_selected_frame ());
14f9c5c9
AS
6313}
6314
4c4b4cd2
PH
6315/* Output Ada exception name (if any) associated with last call to
6316 ada_adjust_exception_stop. */
6317
6318void
6319ada_print_exception_stop (bpstat bs)
14f9c5c9 6320{
4c4b4cd2
PH
6321 if (last_exception_name[0] != '\000')
6322 {
6323 ui_out_text (uiout, last_exception_name);
6324 ui_out_text (uiout, " at ");
6325 }
14f9c5c9
AS
6326}
6327
4c4b4cd2
PH
6328/* Parses the CONDITION string associated with a breakpoint exception
6329 to get the name of the exception on which the breakpoint has been
6330 set. The returned string needs to be deallocated after use. */
14f9c5c9 6331
4c4b4cd2
PH
6332static char *
6333exception_name_from_cond (const char *condition)
14f9c5c9 6334{
4c4b4cd2
PH
6335 char *start, *end, *exception_name;
6336 int exception_name_len;
d2e4a39e 6337
4c4b4cd2
PH
6338 start = strrchr (condition, '&') + 1;
6339 end = strchr (start, ')') - 1;
6340 exception_name_len = end - start + 1;
14f9c5c9 6341
4c4b4cd2
PH
6342 exception_name =
6343 (char *) xmalloc ((exception_name_len + 1) * sizeof (char));
6344 sprintf (exception_name, "%.*s", exception_name_len, start);
6345
6346 return exception_name;
6347}
6348
6349/* Print Ada-specific exception information about B, other than task
6350 clause. Return non-zero iff B was an Ada exception breakpoint. */
14f9c5c9 6351
4c4b4cd2
PH
6352int
6353ada_print_exception_breakpoint_nontask (struct breakpoint *b)
6354{
4c4b4cd2
PH
6355 if (b->break_on_exception == 1)
6356 {
76a01679 6357 if (b->cond_string) /* the breakpoint is on a specific exception. */
4c4b4cd2
PH
6358 {
6359 char *exception_name = exception_name_from_cond (b->cond_string);
6360
6361 make_cleanup (xfree, exception_name);
6362
6363 ui_out_text (uiout, "on ");
6364 if (ui_out_is_mi_like_p (uiout))
6365 ui_out_field_string (uiout, "exception", exception_name);
6366 else
6367 {
6368 ui_out_text (uiout, "exception ");
6369 ui_out_text (uiout, exception_name);
6370 ui_out_text (uiout, " ");
6371 }
6372 }
6373 else
6374 ui_out_text (uiout, "on all exceptions");
6375 }
6376 else if (b->break_on_exception == 2)
6377 ui_out_text (uiout, "on unhandled exception");
6378 else if (b->break_on_exception == 3)
6379 ui_out_text (uiout, "on assert failure");
6380 else
6381 return 0;
6382 return 1;
14f9c5c9
AS
6383}
6384
4c4b4cd2
PH
6385/* Print task identifier for breakpoint B, if it is an Ada-specific
6386 breakpoint with non-zero tasking information. */
6387
14f9c5c9 6388void
4c4b4cd2
PH
6389ada_print_exception_breakpoint_task (struct breakpoint *b)
6390{
4c4b4cd2
PH
6391 if (b->task != 0)
6392 {
6393 ui_out_text (uiout, " task ");
6394 ui_out_field_int (uiout, "task", b->task);
6395 }
14f9c5c9
AS
6396}
6397
6398int
d2e4a39e 6399ada_is_exception_sym (struct symbol *sym)
14f9c5c9
AS
6400{
6401 char *type_name = type_name_no_tag (SYMBOL_TYPE (sym));
d2e4a39e 6402
14f9c5c9 6403 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
4c4b4cd2
PH
6404 && SYMBOL_CLASS (sym) != LOC_BLOCK
6405 && SYMBOL_CLASS (sym) != LOC_CONST
6406 && type_name != NULL && strcmp (type_name, "exception") == 0);
14f9c5c9
AS
6407}
6408
6409int
d2e4a39e 6410ada_maybe_exception_partial_symbol (struct partial_symbol *sym)
14f9c5c9
AS
6411{
6412 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
4c4b4cd2
PH
6413 && SYMBOL_CLASS (sym) != LOC_BLOCK
6414 && SYMBOL_CLASS (sym) != LOC_CONST);
6415}
6416
6417/* Cause the appropriate error if no appropriate runtime symbol is
6418 found to set a breakpoint, using ERR_DESC to describe the
6419 breakpoint. */
6420
6421static void
6422error_breakpoint_runtime_sym_not_found (const char *err_desc)
6423{
6424 /* If we are not debugging an Ada program, we can not put exception
6425 breakpoints! */
6426
6427 if (ada_update_initial_language (language_unknown, NULL) != language_ada)
6428 error ("Unable to break on %s. Is this an Ada main program?", err_desc);
6429
6430 /* If the symbol does not exist, then check that the program is
6431 already started, to make sure that shared libraries have been
6432 loaded. If it is not started, this may mean that the symbol is
6433 in a shared library. */
6434
6435 if (ptid_get_pid (inferior_ptid) == 0)
76a01679
JB
6436 error ("Unable to break on %s. Try to start the program first.",
6437 err_desc);
4c4b4cd2
PH
6438
6439 /* At this point, we know that we are debugging an Ada program and
6440 that the inferior has been started, but we still are not able to
6441 find the run-time symbols. That can mean that we are in
6442 configurable run time mode, or that a-except as been optimized
6443 out by the linker... In any case, at this point it is not worth
6444 supporting this feature. */
6445
6446 error ("Cannot break on %s in this configuration.", err_desc);
6447}
6448
6449/* Test if NAME is currently defined, and that either ALLOW_TRAMP or
6450 the symbol is not a shared-library trampoline. Return the result of
6451 the test. */
6452
6453static int
76a01679 6454is_runtime_sym_defined (const char *name, int allow_tramp)
4c4b4cd2
PH
6455{
6456 struct minimal_symbol *msym;
6457
6458 msym = lookup_minimal_symbol (name, NULL, NULL);
6459 return (msym != NULL && msym->type != mst_unknown
76a01679 6460 && (allow_tramp || msym->type != mst_solib_trampoline));
14f9c5c9
AS
6461}
6462
6463/* If ARG points to an Ada exception or assert breakpoint, rewrite
4c4b4cd2 6464 into equivalent form. Return resulting argument string. Set
14f9c5c9 6465 *BREAK_ON_EXCEPTIONP to 1 for ordinary break on exception, 2 for
4c4b4cd2
PH
6466 break on unhandled, 3 for assert, 0 otherwise. */
6467
d2e4a39e
AS
6468char *
6469ada_breakpoint_rewrite (char *arg, int *break_on_exceptionp)
14f9c5c9
AS
6470{
6471 if (arg == NULL)
6472 return arg;
6473 *break_on_exceptionp = 0;
4c4b4cd2
PH
6474 if (current_language->la_language == language_ada
6475 && strncmp (arg, "exception", 9) == 0
6476 && (arg[9] == ' ' || arg[9] == '\t' || arg[9] == '\0'))
6477 {
6478 char *tok, *end_tok;
6479 int toklen;
6480 int has_exception_propagation =
76a01679 6481 is_runtime_sym_defined (raise_sym_name, 1);
4c4b4cd2
PH
6482
6483 *break_on_exceptionp = 1;
6484
6485 tok = arg + 9;
6486 while (*tok == ' ' || *tok == '\t')
6487 tok += 1;
6488
6489 end_tok = tok;
6490
6491 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6492 end_tok += 1;
6493
6494 toklen = end_tok - tok;
6495
6496 arg = (char *) xmalloc (sizeof (longest_exception_template) + toklen);
6497 make_cleanup (xfree, arg);
6498 if (toklen == 0)
6499 {
76a01679
JB
6500 if (has_exception_propagation)
6501 sprintf (arg, "'%s'", raise_sym_name);
6502 else
6503 error_breakpoint_runtime_sym_not_found ("exception");
4c4b4cd2
PH
6504 }
6505 else if (strncmp (tok, "unhandled", toklen) == 0)
6506 {
76a01679
JB
6507 if (is_runtime_sym_defined (raise_unhandled_sym_name, 1))
6508 sprintf (arg, "'%s'", raise_unhandled_sym_name);
6509 else
6510 error_breakpoint_runtime_sym_not_found ("exception");
4c4b4cd2 6511
76a01679 6512 *break_on_exceptionp = 2;
4c4b4cd2
PH
6513 }
6514 else
6515 {
76a01679
JB
6516 if (is_runtime_sym_defined (raise_sym_name, 0))
6517 sprintf (arg, "'%s' if long_integer(e) = long_integer(&%.*s)",
6518 raise_sym_name, toklen, tok);
6519 else
6520 error_breakpoint_runtime_sym_not_found ("specific exception");
4c4b4cd2
PH
6521 }
6522 }
6523 else if (current_language->la_language == language_ada
6524 && strncmp (arg, "assert", 6) == 0
6525 && (arg[6] == ' ' || arg[6] == '\t' || arg[6] == '\0'))
6526 {
6527 char *tok = arg + 6;
6528
6529 if (!is_runtime_sym_defined (raise_assert_sym_name, 1))
76a01679 6530 error_breakpoint_runtime_sym_not_found ("failed assertion");
4c4b4cd2
PH
6531
6532 *break_on_exceptionp = 3;
6533
6534 arg =
6535 (char *) xmalloc (sizeof (raise_assert_sym_name) + strlen (tok) + 2);
6536 make_cleanup (xfree, arg);
6537 sprintf (arg, "'%s'%s", raise_assert_sym_name, tok);
6538 }
14f9c5c9
AS
6539 return arg;
6540}
96d887e8 6541#endif
14f9c5c9 6542\f
4c4b4cd2 6543 /* Field Access */
14f9c5c9
AS
6544
6545/* True if field number FIELD_NUM in struct or union type TYPE is supposed
4c4b4cd2 6546 to be invisible to users. */
14f9c5c9
AS
6547
6548int
ebf56fd3 6549ada_is_ignored_field (struct type *type, int field_num)
14f9c5c9
AS
6550{
6551 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
6552 return 1;
d2e4a39e 6553 else
14f9c5c9 6554 {
d2e4a39e 6555 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9 6556 return (name == NULL
4c4b4cd2 6557 || (name[0] == '_' && strncmp (name, "_parent", 7) != 0));
14f9c5c9
AS
6558 }
6559}
6560
4c4b4cd2
PH
6561/* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
6562 pointer or reference type whose ultimate target has a tag field. */
14f9c5c9
AS
6563
6564int
4c4b4cd2 6565ada_is_tagged_type (struct type *type, int refok)
14f9c5c9 6566{
4c4b4cd2
PH
6567 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
6568}
14f9c5c9 6569
4c4b4cd2
PH
6570/* True iff TYPE represents the type of X'Tag */
6571
6572int
6573ada_is_tag_type (struct type *type)
6574{
76a01679 6575 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
4c4b4cd2 6576 return 0;
76a01679
JB
6577 else
6578 {
6579 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
6580 return (name != NULL
6581 && strcmp (name, "ada__tags__dispatch_table") == 0);
6582 }
14f9c5c9
AS
6583}
6584
4c4b4cd2 6585/* The type of the tag on VAL. */
14f9c5c9 6586
d2e4a39e
AS
6587struct type *
6588ada_tag_type (struct value *val)
14f9c5c9 6589{
4c4b4cd2 6590 return ada_lookup_struct_elt_type (VALUE_TYPE (val), "_tag", 1, 0, NULL);
14f9c5c9
AS
6591}
6592
4c4b4cd2 6593/* The value of the tag on VAL. */
14f9c5c9 6594
d2e4a39e
AS
6595struct value *
6596ada_value_tag (struct value *val)
14f9c5c9
AS
6597{
6598 return ada_value_struct_elt (val, "_tag", "record");
6599}
6600
4c4b4cd2
PH
6601/* The value of the tag on the object of type TYPE whose contents are
6602 saved at VALADDR, if it is non-null, or is at memory address
6603 ADDRESS. */
6604
6605static struct value *
6606value_tag_from_contents_and_address (struct type *type, char *valaddr,
76a01679 6607 CORE_ADDR address)
4c4b4cd2
PH
6608{
6609 int tag_byte_offset, dummy1, dummy2;
6610 struct type *tag_type;
6611 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
76a01679 6612 &dummy1, &dummy2))
4c4b4cd2
PH
6613 {
6614 char *valaddr1 = (valaddr == NULL) ? NULL : valaddr + tag_byte_offset;
6615 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
6616
6617 return value_from_contents_and_address (tag_type, valaddr1, address1);
6618 }
6619 return NULL;
6620}
6621
6622static struct type *
6623type_from_tag (struct value *tag)
6624{
6625 const char *type_name = ada_tag_name (tag);
6626 if (type_name != NULL)
6627 return ada_find_any_type (ada_encode (type_name));
6628 return NULL;
6629}
6630
76a01679
JB
6631struct tag_args
6632{
4c4b4cd2
PH
6633 struct value *tag;
6634 char *name;
6635};
6636
6637/* Wrapper function used by ada_tag_name. Given a struct tag_args*
6638 value ARGS, sets ARGS->name to the tag name of ARGS->tag.
6639 The value stored in ARGS->name is valid until the next call to
6640 ada_tag_name_1. */
6641
6642static int
6643ada_tag_name_1 (void *args0)
6644{
6645 struct tag_args *args = (struct tag_args *) args0;
6646 static char name[1024];
76a01679 6647 char *p;
4c4b4cd2
PH
6648 struct value *val;
6649 args->name = NULL;
6650 val = ada_value_struct_elt (args->tag, "tsd", NULL);
6651 if (val == NULL)
6652 return 0;
6653 val = ada_value_struct_elt (val, "expanded_name", NULL);
6654 if (val == NULL)
6655 return 0;
6656 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
6657 for (p = name; *p != '\0'; p += 1)
6658 if (isalpha (*p))
6659 *p = tolower (*p);
6660 args->name = name;
6661 return 0;
6662}
6663
6664/* The type name of the dynamic type denoted by the 'tag value TAG, as
6665 * a C string. */
6666
6667const char *
6668ada_tag_name (struct value *tag)
6669{
6670 struct tag_args args;
76a01679 6671 if (!ada_is_tag_type (VALUE_TYPE (tag)))
4c4b4cd2 6672 return NULL;
76a01679 6673 args.tag = tag;
4c4b4cd2
PH
6674 args.name = NULL;
6675 catch_errors (ada_tag_name_1, &args, NULL, RETURN_MASK_ALL);
6676 return args.name;
6677}
6678
6679/* The parent type of TYPE, or NULL if none. */
14f9c5c9 6680
d2e4a39e 6681struct type *
ebf56fd3 6682ada_parent_type (struct type *type)
14f9c5c9
AS
6683{
6684 int i;
6685
6686 CHECK_TYPEDEF (type);
6687
6688 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
6689 return NULL;
6690
6691 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6692 if (ada_is_parent_field (type, i))
6693 return check_typedef (TYPE_FIELD_TYPE (type, i));
6694
6695 return NULL;
6696}
6697
4c4b4cd2
PH
6698/* True iff field number FIELD_NUM of structure type TYPE contains the
6699 parent-type (inherited) fields of a derived type. Assumes TYPE is
6700 a structure type with at least FIELD_NUM+1 fields. */
14f9c5c9
AS
6701
6702int
ebf56fd3 6703ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 6704{
d2e4a39e 6705 const char *name = TYPE_FIELD_NAME (check_typedef (type), field_num);
4c4b4cd2
PH
6706 return (name != NULL
6707 && (strncmp (name, "PARENT", 6) == 0
6708 || strncmp (name, "_parent", 7) == 0));
14f9c5c9
AS
6709}
6710
4c4b4cd2 6711/* True iff field number FIELD_NUM of structure type TYPE is a
14f9c5c9 6712 transparent wrapper field (which should be silently traversed when doing
4c4b4cd2 6713 field selection and flattened when printing). Assumes TYPE is a
14f9c5c9 6714 structure type with at least FIELD_NUM+1 fields. Such fields are always
4c4b4cd2 6715 structures. */
14f9c5c9
AS
6716
6717int
ebf56fd3 6718ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 6719{
d2e4a39e
AS
6720 const char *name = TYPE_FIELD_NAME (type, field_num);
6721 return (name != NULL
4c4b4cd2
PH
6722 && (strncmp (name, "PARENT", 6) == 0
6723 || strcmp (name, "REP") == 0
6724 || strncmp (name, "_parent", 7) == 0
6725 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
14f9c5c9
AS
6726}
6727
4c4b4cd2
PH
6728/* True iff field number FIELD_NUM of structure or union type TYPE
6729 is a variant wrapper. Assumes TYPE is a structure type with at least
6730 FIELD_NUM+1 fields. */
14f9c5c9
AS
6731
6732int
ebf56fd3 6733ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 6734{
d2e4a39e 6735 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
14f9c5c9 6736 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
4c4b4cd2
PH
6737 || (is_dynamic_field (type, field_num)
6738 && TYPE_CODE (TYPE_TARGET_TYPE (field_type)) ==
6739 TYPE_CODE_UNION));
14f9c5c9
AS
6740}
6741
6742/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
4c4b4cd2 6743 whose discriminants are contained in the record type OUTER_TYPE,
14f9c5c9
AS
6744 returns the type of the controlling discriminant for the variant. */
6745
d2e4a39e 6746struct type *
ebf56fd3 6747ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 6748{
d2e4a39e 6749 char *name = ada_variant_discrim_name (var_type);
76a01679 6750 struct type *type =
4c4b4cd2 6751 ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
14f9c5c9
AS
6752 if (type == NULL)
6753 return builtin_type_int;
6754 else
6755 return type;
6756}
6757
4c4b4cd2 6758/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
14f9c5c9 6759 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
4c4b4cd2 6760 represents a 'when others' clause; otherwise 0. */
14f9c5c9
AS
6761
6762int
ebf56fd3 6763ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 6764{
d2e4a39e 6765 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
6766 return (name != NULL && name[0] == 'O');
6767}
6768
6769/* Assuming that TYPE0 is the type of the variant part of a record,
4c4b4cd2
PH
6770 returns the name of the discriminant controlling the variant.
6771 The value is valid until the next call to ada_variant_discrim_name. */
14f9c5c9 6772
d2e4a39e 6773char *
ebf56fd3 6774ada_variant_discrim_name (struct type *type0)
14f9c5c9 6775{
d2e4a39e 6776 static char *result = NULL;
14f9c5c9 6777 static size_t result_len = 0;
d2e4a39e
AS
6778 struct type *type;
6779 const char *name;
6780 const char *discrim_end;
6781 const char *discrim_start;
14f9c5c9
AS
6782
6783 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
6784 type = TYPE_TARGET_TYPE (type0);
6785 else
6786 type = type0;
6787
6788 name = ada_type_name (type);
6789
6790 if (name == NULL || name[0] == '\000')
6791 return "";
6792
6793 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
6794 discrim_end -= 1)
6795 {
4c4b4cd2
PH
6796 if (strncmp (discrim_end, "___XVN", 6) == 0)
6797 break;
14f9c5c9
AS
6798 }
6799 if (discrim_end == name)
6800 return "";
6801
d2e4a39e 6802 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
6803 discrim_start -= 1)
6804 {
d2e4a39e 6805 if (discrim_start == name + 1)
4c4b4cd2 6806 return "";
76a01679 6807 if ((discrim_start > name + 3
4c4b4cd2
PH
6808 && strncmp (discrim_start - 3, "___", 3) == 0)
6809 || discrim_start[-1] == '.')
6810 break;
14f9c5c9
AS
6811 }
6812
6813 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
6814 strncpy (result, discrim_start, discrim_end - discrim_start);
d2e4a39e 6815 result[discrim_end - discrim_start] = '\0';
14f9c5c9
AS
6816 return result;
6817}
6818
4c4b4cd2
PH
6819/* Scan STR for a subtype-encoded number, beginning at position K.
6820 Put the position of the character just past the number scanned in
6821 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6822 Return 1 if there was a valid number at the given position, and 0
6823 otherwise. A "subtype-encoded" number consists of the absolute value
6824 in decimal, followed by the letter 'm' to indicate a negative number.
6825 Assumes 0m does not occur. */
14f9c5c9
AS
6826
6827int
d2e4a39e 6828ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
6829{
6830 ULONGEST RU;
6831
d2e4a39e 6832 if (!isdigit (str[k]))
14f9c5c9
AS
6833 return 0;
6834
4c4b4cd2 6835 /* Do it the hard way so as not to make any assumption about
14f9c5c9 6836 the relationship of unsigned long (%lu scan format code) and
4c4b4cd2 6837 LONGEST. */
14f9c5c9
AS
6838 RU = 0;
6839 while (isdigit (str[k]))
6840 {
d2e4a39e 6841 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
6842 k += 1;
6843 }
6844
d2e4a39e 6845 if (str[k] == 'm')
14f9c5c9
AS
6846 {
6847 if (R != NULL)
4c4b4cd2 6848 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
6849 k += 1;
6850 }
6851 else if (R != NULL)
6852 *R = (LONGEST) RU;
6853
4c4b4cd2 6854 /* NOTE on the above: Technically, C does not say what the results of
14f9c5c9
AS
6855 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6856 number representable as a LONGEST (although either would probably work
6857 in most implementations). When RU>0, the locution in the then branch
4c4b4cd2 6858 above is always equivalent to the negative of RU. */
14f9c5c9
AS
6859
6860 if (new_k != NULL)
6861 *new_k = k;
6862 return 1;
6863}
6864
4c4b4cd2
PH
6865/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6866 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6867 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
14f9c5c9 6868
d2e4a39e 6869int
ebf56fd3 6870ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 6871{
d2e4a39e 6872 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
6873 int p;
6874
6875 p = 0;
6876 while (1)
6877 {
d2e4a39e 6878 switch (name[p])
4c4b4cd2
PH
6879 {
6880 case '\0':
6881 return 0;
6882 case 'S':
6883 {
6884 LONGEST W;
6885 if (!ada_scan_number (name, p + 1, &W, &p))
6886 return 0;
6887 if (val == W)
6888 return 1;
6889 break;
6890 }
6891 case 'R':
6892 {
6893 LONGEST L, U;
6894 if (!ada_scan_number (name, p + 1, &L, &p)
6895 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6896 return 0;
6897 if (val >= L && val <= U)
6898 return 1;
6899 break;
6900 }
6901 case 'O':
6902 return 1;
6903 default:
6904 return 0;
6905 }
6906 }
6907}
6908
6909/* FIXME: Lots of redundancy below. Try to consolidate. */
6910
6911/* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6912 ARG_TYPE, extract and return the value of one of its (non-static)
6913 fields. FIELDNO says which field. Differs from value_primitive_field
6914 only in that it can handle packed values of arbitrary type. */
14f9c5c9 6915
4c4b4cd2 6916static struct value *
d2e4a39e 6917ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
4c4b4cd2 6918 struct type *arg_type)
14f9c5c9 6919{
14f9c5c9
AS
6920 struct type *type;
6921
6922 CHECK_TYPEDEF (arg_type);
6923 type = TYPE_FIELD_TYPE (arg_type, fieldno);
6924
4c4b4cd2 6925 /* Handle packed fields. */
14f9c5c9
AS
6926
6927 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
6928 {
6929 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6930 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 6931
14f9c5c9 6932 return ada_value_primitive_packed_val (arg1, VALUE_CONTENTS (arg1),
4c4b4cd2
PH
6933 offset + bit_pos / 8,
6934 bit_pos % 8, bit_size, type);
14f9c5c9
AS
6935 }
6936 else
6937 return value_primitive_field (arg1, offset, fieldno, arg_type);
6938}
6939
4c4b4cd2
PH
6940/* Find field with name NAME in object of type TYPE. If found, return 1
6941 after setting *FIELD_TYPE_P to the field's type, *BYTE_OFFSET_P to
6942 OFFSET + the byte offset of the field within an object of that type,
6943 *BIT_OFFSET_P to the bit offset modulo byte size of the field, and
6944 *BIT_SIZE_P to its size in bits if the field is packed, and 0 otherwise.
6945 Looks inside wrappers for the field. Returns 0 if field not
6946 found. */
6947static int
76a01679
JB
6948find_struct_field (char *name, struct type *type, int offset,
6949 struct type **field_type_p,
6950 int *byte_offset_p, int *bit_offset_p, int *bit_size_p)
4c4b4cd2
PH
6951{
6952 int i;
6953
6954 CHECK_TYPEDEF (type);
6955 *field_type_p = NULL;
6956 *byte_offset_p = *bit_offset_p = *bit_size_p = 0;
76a01679 6957
4c4b4cd2
PH
6958 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i -= 1)
6959 {
6960 int bit_pos = TYPE_FIELD_BITPOS (type, i);
6961 int fld_offset = offset + bit_pos / 8;
6962 char *t_field_name = TYPE_FIELD_NAME (type, i);
76a01679 6963
4c4b4cd2
PH
6964 if (t_field_name == NULL)
6965 continue;
6966
6967 else if (field_name_match (t_field_name, name))
76a01679
JB
6968 {
6969 int bit_size = TYPE_FIELD_BITSIZE (type, i);
6970 *field_type_p = TYPE_FIELD_TYPE (type, i);
6971 *byte_offset_p = fld_offset;
6972 *bit_offset_p = bit_pos % 8;
6973 *bit_size_p = bit_size;
6974 return 1;
6975 }
4c4b4cd2
PH
6976 else if (ada_is_wrapper_field (type, i))
6977 {
76a01679
JB
6978 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
6979 field_type_p, byte_offset_p, bit_offset_p,
6980 bit_size_p))
6981 return 1;
6982 }
4c4b4cd2
PH
6983 else if (ada_is_variant_part (type, i))
6984 {
6985 int j;
6986 struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
6987
6988 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
6989 {
76a01679
JB
6990 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
6991 fld_offset
6992 + TYPE_FIELD_BITPOS (field_type, j) / 8,
6993 field_type_p, byte_offset_p,
6994 bit_offset_p, bit_size_p))
6995 return 1;
4c4b4cd2
PH
6996 }
6997 }
6998 }
6999 return 0;
7000}
7001
7002
14f9c5c9 7003
4c4b4cd2 7004/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
14f9c5c9
AS
7005 and search in it assuming it has (class) type TYPE.
7006 If found, return value, else return NULL.
7007
4c4b4cd2 7008 Searches recursively through wrapper fields (e.g., '_parent'). */
14f9c5c9 7009
4c4b4cd2 7010static struct value *
d2e4a39e 7011ada_search_struct_field (char *name, struct value *arg, int offset,
4c4b4cd2 7012 struct type *type)
14f9c5c9
AS
7013{
7014 int i;
7015 CHECK_TYPEDEF (type);
7016
d2e4a39e 7017 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i -= 1)
14f9c5c9
AS
7018 {
7019 char *t_field_name = TYPE_FIELD_NAME (type, i);
7020
7021 if (t_field_name == NULL)
4c4b4cd2 7022 continue;
14f9c5c9
AS
7023
7024 else if (field_name_match (t_field_name, name))
4c4b4cd2 7025 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
7026
7027 else if (ada_is_wrapper_field (type, i))
4c4b4cd2 7028 {
06d5cf63
JB
7029 struct value *v = /* Do not let indent join lines here. */
7030 ada_search_struct_field (name, arg,
7031 offset + TYPE_FIELD_BITPOS (type, i) / 8,
7032 TYPE_FIELD_TYPE (type, i));
4c4b4cd2
PH
7033 if (v != NULL)
7034 return v;
7035 }
14f9c5c9
AS
7036
7037 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
7038 {
7039 int j;
7040 struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
7041 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
7042
7043 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
7044 {
06d5cf63
JB
7045 struct value *v = ada_search_struct_field /* Force line break. */
7046 (name, arg,
7047 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
7048 TYPE_FIELD_TYPE (field_type, j));
4c4b4cd2
PH
7049 if (v != NULL)
7050 return v;
7051 }
7052 }
14f9c5c9
AS
7053 }
7054 return NULL;
7055}
d2e4a39e 7056
4c4b4cd2
PH
7057/* Given ARG, a value of type (pointer or reference to a)*
7058 structure/union, extract the component named NAME from the ultimate
7059 target structure/union and return it as a value with its
7060 appropriate type. If ARG is a pointer or reference and the field
7061 is not packed, returns a reference to the field, otherwise the
7062 value of the field (an lvalue if ARG is an lvalue).
14f9c5c9 7063
4c4b4cd2
PH
7064 The routine searches for NAME among all members of the structure itself
7065 and (recursively) among all members of any wrapper members
14f9c5c9
AS
7066 (e.g., '_parent').
7067
4c4b4cd2
PH
7068 ERR is a name (for use in error messages) that identifies the class
7069 of entity that ARG is supposed to be. ERR may be null, indicating
7070 that on error, the function simply returns NULL, and does not
7071 throw an error. (FIXME: True only if ARG is a pointer or reference
7072 at the moment). */
14f9c5c9 7073
d2e4a39e 7074struct value *
ebf56fd3 7075ada_value_struct_elt (struct value *arg, char *name, char *err)
14f9c5c9 7076{
4c4b4cd2 7077 struct type *t, *t1;
d2e4a39e 7078 struct value *v;
14f9c5c9 7079
4c4b4cd2
PH
7080 v = NULL;
7081 t1 = t = check_typedef (VALUE_TYPE (arg));
7082 if (TYPE_CODE (t) == TYPE_CODE_REF)
7083 {
7084 t1 = TYPE_TARGET_TYPE (t);
7085 if (t1 == NULL)
76a01679
JB
7086 {
7087 if (err == NULL)
7088 return NULL;
7089 else
7090 error ("Bad value type in a %s.", err);
7091 }
4c4b4cd2
PH
7092 CHECK_TYPEDEF (t1);
7093 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679
JB
7094 {
7095 COERCE_REF (arg);
7096 t = t1;
7097 }
4c4b4cd2 7098 }
14f9c5c9 7099
4c4b4cd2
PH
7100 while (TYPE_CODE (t) == TYPE_CODE_PTR)
7101 {
7102 t1 = TYPE_TARGET_TYPE (t);
7103 if (t1 == NULL)
76a01679
JB
7104 {
7105 if (err == NULL)
7106 return NULL;
7107 else
7108 error ("Bad value type in a %s.", err);
7109 }
4c4b4cd2
PH
7110 CHECK_TYPEDEF (t1);
7111 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
76a01679
JB
7112 {
7113 arg = value_ind (arg);
7114 t = t1;
7115 }
4c4b4cd2 7116 else
76a01679 7117 break;
4c4b4cd2 7118 }
14f9c5c9 7119
4c4b4cd2 7120 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
14f9c5c9 7121 {
4c4b4cd2 7122 if (err == NULL)
76a01679 7123 return NULL;
4c4b4cd2 7124 else
76a01679
JB
7125 error ("Attempt to extract a component of a value that is not a %s.",
7126 err);
14f9c5c9
AS
7127 }
7128
4c4b4cd2
PH
7129 if (t1 == t)
7130 v = ada_search_struct_field (name, arg, 0, t);
7131 else
7132 {
7133 int bit_offset, bit_size, byte_offset;
7134 struct type *field_type;
7135 CORE_ADDR address;
7136
76a01679
JB
7137 if (TYPE_CODE (t) == TYPE_CODE_PTR)
7138 address = value_as_address (arg);
4c4b4cd2 7139 else
76a01679 7140 address = unpack_pointer (t, VALUE_CONTENTS (arg));
14f9c5c9 7141
4c4b4cd2 7142 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL);
76a01679
JB
7143 if (find_struct_field (name, t1, 0,
7144 &field_type, &byte_offset, &bit_offset,
7145 &bit_size))
7146 {
7147 if (bit_size != 0)
7148 {
7149 arg = ada_value_ind (arg);
7150 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
7151 bit_offset, bit_size,
7152 field_type);
7153 }
7154 else
7155 v = value_from_pointer (lookup_reference_type (field_type),
7156 address + byte_offset);
7157 }
7158 }
7159
4c4b4cd2 7160 if (v == NULL && err != NULL)
14f9c5c9
AS
7161 error ("There is no member named %s.", name);
7162
7163 return v;
7164}
7165
7166/* Given a type TYPE, look up the type of the component of type named NAME.
4c4b4cd2
PH
7167 If DISPP is non-null, add its byte displacement from the beginning of a
7168 structure (pointed to by a value) of type TYPE to *DISPP (does not
14f9c5c9
AS
7169 work for packed fields).
7170
7171 Matches any field whose name has NAME as a prefix, possibly
4c4b4cd2 7172 followed by "___".
14f9c5c9 7173
4c4b4cd2
PH
7174 TYPE can be either a struct or union. If REFOK, TYPE may also
7175 be a (pointer or reference)+ to a struct or union, and the
7176 ultimate target type will be searched.
14f9c5c9
AS
7177
7178 Looks recursively into variant clauses and parent types.
7179
4c4b4cd2
PH
7180 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7181 TYPE is not a type of the right kind. */
14f9c5c9 7182
4c4b4cd2 7183static struct type *
76a01679
JB
7184ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
7185 int noerr, int *dispp)
14f9c5c9
AS
7186{
7187 int i;
7188
7189 if (name == NULL)
7190 goto BadName;
7191
76a01679 7192 if (refok && type != NULL)
4c4b4cd2
PH
7193 while (1)
7194 {
76a01679
JB
7195 CHECK_TYPEDEF (type);
7196 if (TYPE_CODE (type) != TYPE_CODE_PTR
7197 && TYPE_CODE (type) != TYPE_CODE_REF)
7198 break;
7199 type = TYPE_TARGET_TYPE (type);
4c4b4cd2 7200 }
14f9c5c9 7201
76a01679 7202 if (type == NULL
1265e4aa
JB
7203 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
7204 && TYPE_CODE (type) != TYPE_CODE_UNION))
14f9c5c9 7205 {
4c4b4cd2 7206 if (noerr)
76a01679 7207 return NULL;
4c4b4cd2 7208 else
76a01679
JB
7209 {
7210 target_terminal_ours ();
7211 gdb_flush (gdb_stdout);
7212 fprintf_unfiltered (gdb_stderr, "Type ");
7213 if (type == NULL)
7214 fprintf_unfiltered (gdb_stderr, "(null)");
7215 else
7216 type_print (type, "", gdb_stderr, -1);
7217 error (" is not a structure or union type");
7218 }
14f9c5c9
AS
7219 }
7220
7221 type = to_static_fixed_type (type);
7222
7223 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
7224 {
7225 char *t_field_name = TYPE_FIELD_NAME (type, i);
7226 struct type *t;
7227 int disp;
d2e4a39e 7228
14f9c5c9 7229 if (t_field_name == NULL)
4c4b4cd2 7230 continue;
14f9c5c9
AS
7231
7232 else if (field_name_match (t_field_name, name))
4c4b4cd2
PH
7233 {
7234 if (dispp != NULL)
7235 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
7236 return check_typedef (TYPE_FIELD_TYPE (type, i));
7237 }
14f9c5c9
AS
7238
7239 else if (ada_is_wrapper_field (type, i))
4c4b4cd2
PH
7240 {
7241 disp = 0;
7242 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
7243 0, 1, &disp);
7244 if (t != NULL)
7245 {
7246 if (dispp != NULL)
7247 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
7248 return t;
7249 }
7250 }
14f9c5c9
AS
7251
7252 else if (ada_is_variant_part (type, i))
4c4b4cd2
PH
7253 {
7254 int j;
7255 struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
7256
7257 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
7258 {
7259 disp = 0;
7260 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type, j),
7261 name, 0, 1, &disp);
7262 if (t != NULL)
7263 {
7264 if (dispp != NULL)
7265 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
7266 return t;
7267 }
7268 }
7269 }
14f9c5c9
AS
7270
7271 }
7272
7273BadName:
d2e4a39e 7274 if (!noerr)
14f9c5c9
AS
7275 {
7276 target_terminal_ours ();
7277 gdb_flush (gdb_stdout);
7278 fprintf_unfiltered (gdb_stderr, "Type ");
7279 type_print (type, "", gdb_stderr, -1);
7280 fprintf_unfiltered (gdb_stderr, " has no component named ");
7281 error ("%s", name == NULL ? "<null>" : name);
7282 }
7283
7284 return NULL;
7285}
7286
7287/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7288 within a value of type OUTER_TYPE that is stored in GDB at
4c4b4cd2
PH
7289 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
7290 numbering from 0) is applicable. Returns -1 if none are. */
14f9c5c9 7291
d2e4a39e 7292int
ebf56fd3 7293ada_which_variant_applies (struct type *var_type, struct type *outer_type,
4c4b4cd2 7294 char *outer_valaddr)
14f9c5c9
AS
7295{
7296 int others_clause;
7297 int i;
7298 int disp;
d2e4a39e
AS
7299 struct type *discrim_type;
7300 char *discrim_name = ada_variant_discrim_name (var_type);
14f9c5c9
AS
7301 LONGEST discrim_val;
7302
7303 disp = 0;
d2e4a39e 7304 discrim_type =
4c4b4cd2 7305 ada_lookup_struct_elt_type (outer_type, discrim_name, 1, 1, &disp);
14f9c5c9
AS
7306 if (discrim_type == NULL)
7307 return -1;
7308 discrim_val = unpack_long (discrim_type, outer_valaddr + disp);
7309
7310 others_clause = -1;
7311 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
7312 {
7313 if (ada_is_others_clause (var_type, i))
4c4b4cd2 7314 others_clause = i;
14f9c5c9 7315 else if (ada_in_variant (discrim_val, var_type, i))
4c4b4cd2 7316 return i;
14f9c5c9
AS
7317 }
7318
7319 return others_clause;
7320}
d2e4a39e 7321\f
14f9c5c9
AS
7322
7323
4c4b4cd2 7324 /* Dynamic-Sized Records */
14f9c5c9
AS
7325
7326/* Strategy: The type ostensibly attached to a value with dynamic size
7327 (i.e., a size that is not statically recorded in the debugging
7328 data) does not accurately reflect the size or layout of the value.
7329 Our strategy is to convert these values to values with accurate,
4c4b4cd2 7330 conventional types that are constructed on the fly. */
14f9c5c9
AS
7331
7332/* There is a subtle and tricky problem here. In general, we cannot
7333 determine the size of dynamic records without its data. However,
7334 the 'struct value' data structure, which GDB uses to represent
7335 quantities in the inferior process (the target), requires the size
7336 of the type at the time of its allocation in order to reserve space
7337 for GDB's internal copy of the data. That's why the
7338 'to_fixed_xxx_type' routines take (target) addresses as parameters,
4c4b4cd2 7339 rather than struct value*s.
14f9c5c9
AS
7340
7341 However, GDB's internal history variables ($1, $2, etc.) are
7342 struct value*s containing internal copies of the data that are not, in
7343 general, the same as the data at their corresponding addresses in
7344 the target. Fortunately, the types we give to these values are all
7345 conventional, fixed-size types (as per the strategy described
7346 above), so that we don't usually have to perform the
7347 'to_fixed_xxx_type' conversions to look at their values.
7348 Unfortunately, there is one exception: if one of the internal
7349 history variables is an array whose elements are unconstrained
7350 records, then we will need to create distinct fixed types for each
7351 element selected. */
7352
7353/* The upshot of all of this is that many routines take a (type, host
7354 address, target address) triple as arguments to represent a value.
7355 The host address, if non-null, is supposed to contain an internal
7356 copy of the relevant data; otherwise, the program is to consult the
4c4b4cd2 7357 target at the target address. */
14f9c5c9
AS
7358
7359/* Assuming that VAL0 represents a pointer value, the result of
7360 dereferencing it. Differs from value_ind in its treatment of
4c4b4cd2 7361 dynamic-sized types. */
14f9c5c9 7362
d2e4a39e
AS
7363struct value *
7364ada_value_ind (struct value *val0)
14f9c5c9 7365{
d2e4a39e 7366 struct value *val = unwrap_value (value_ind (val0));
4c4b4cd2 7367 return ada_to_fixed_value (val);
14f9c5c9
AS
7368}
7369
7370/* The value resulting from dereferencing any "reference to"
4c4b4cd2
PH
7371 qualifiers on VAL0. */
7372
d2e4a39e
AS
7373static struct value *
7374ada_coerce_ref (struct value *val0)
7375{
7376 if (TYPE_CODE (VALUE_TYPE (val0)) == TYPE_CODE_REF)
7377 {
7378 struct value *val = val0;
7379 COERCE_REF (val);
7380 val = unwrap_value (val);
4c4b4cd2 7381 return ada_to_fixed_value (val);
d2e4a39e
AS
7382 }
7383 else
14f9c5c9
AS
7384 return val0;
7385}
7386
7387/* Return OFF rounded upward if necessary to a multiple of
4c4b4cd2 7388 ALIGNMENT (a power of 2). */
14f9c5c9
AS
7389
7390static unsigned int
ebf56fd3 7391align_value (unsigned int off, unsigned int alignment)
14f9c5c9
AS
7392{
7393 return (off + alignment - 1) & ~(alignment - 1);
7394}
7395
4c4b4cd2 7396/* Return the bit alignment required for field #F of template type TYPE. */
14f9c5c9
AS
7397
7398static unsigned int
ebf56fd3 7399field_alignment (struct type *type, int f)
14f9c5c9 7400{
d2e4a39e 7401 const char *name = TYPE_FIELD_NAME (type, f);
14f9c5c9
AS
7402 int len = (name == NULL) ? 0 : strlen (name);
7403 int align_offset;
7404
4c4b4cd2
PH
7405 if (!isdigit (name[len - 1]))
7406 return 1;
14f9c5c9 7407
d2e4a39e 7408 if (isdigit (name[len - 2]))
14f9c5c9
AS
7409 align_offset = len - 2;
7410 else
7411 align_offset = len - 1;
7412
4c4b4cd2 7413 if (align_offset < 7 || strncmp ("___XV", name + align_offset - 6, 5) != 0)
14f9c5c9
AS
7414 return TARGET_CHAR_BIT;
7415
4c4b4cd2
PH
7416 return atoi (name + align_offset) * TARGET_CHAR_BIT;
7417}
7418
7419/* Find a symbol named NAME. Ignores ambiguity. */
7420
7421struct symbol *
7422ada_find_any_symbol (const char *name)
7423{
7424 struct symbol *sym;
7425
7426 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
7427 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
7428 return sym;
7429
7430 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
7431 return sym;
14f9c5c9
AS
7432}
7433
7434/* Find a type named NAME. Ignores ambiguity. */
4c4b4cd2 7435
d2e4a39e 7436struct type *
ebf56fd3 7437ada_find_any_type (const char *name)
14f9c5c9 7438{
4c4b4cd2 7439 struct symbol *sym = ada_find_any_symbol (name);
14f9c5c9 7440
14f9c5c9
AS
7441 if (sym != NULL)
7442 return SYMBOL_TYPE (sym);
7443
7444 return NULL;
7445}
7446
4c4b4cd2
PH
7447/* Given a symbol NAME and its associated BLOCK, search all symbols
7448 for its ___XR counterpart, which is the ``renaming'' symbol
7449 associated to NAME. Return this symbol if found, return
7450 NULL otherwise. */
7451
7452struct symbol *
7453ada_find_renaming_symbol (const char *name, struct block *block)
7454{
7455 const struct symbol *function_sym = block_function (block);
7456 char *rename;
7457
7458 if (function_sym != NULL)
7459 {
7460 /* If the symbol is defined inside a function, NAME is not fully
7461 qualified. This means we need to prepend the function name
7462 as well as adding the ``___XR'' suffix to build the name of
7463 the associated renaming symbol. */
7464 char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
7465 const int function_name_len = strlen (function_name);
76a01679
JB
7466 const int rename_len = function_name_len + 2 /* "__" */
7467 + strlen (name) + 6 /* "___XR\0" */ ;
4c4b4cd2
PH
7468
7469 /* Library-level functions are a special case, as GNAT adds
7470 a ``_ada_'' prefix to the function name to avoid namespace
7471 pollution. However, the renaming symbol themselves do not
7472 have this prefix, so we need to skip this prefix if present. */
7473 if (function_name_len > 5 /* "_ada_" */
7474 && strstr (function_name, "_ada_") == function_name)
7475 function_name = function_name + 5;
7476
7477 rename = (char *) alloca (rename_len * sizeof (char));
7478 sprintf (rename, "%s__%s___XR", function_name, name);
7479 }
7480 else
7481 {
7482 const int rename_len = strlen (name) + 6;
7483 rename = (char *) alloca (rename_len * sizeof (char));
7484 sprintf (rename, "%s___XR", name);
7485 }
7486
7487 return ada_find_any_symbol (rename);
7488}
7489
14f9c5c9 7490/* Because of GNAT encoding conventions, several GDB symbols may match a
4c4b4cd2 7491 given type name. If the type denoted by TYPE0 is to be preferred to
14f9c5c9 7492 that of TYPE1 for purposes of type printing, return non-zero;
4c4b4cd2
PH
7493 otherwise return 0. */
7494
14f9c5c9 7495int
d2e4a39e 7496ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
7497{
7498 if (type1 == NULL)
7499 return 1;
7500 else if (type0 == NULL)
7501 return 0;
7502 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
7503 return 1;
7504 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
7505 return 0;
4c4b4cd2
PH
7506 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
7507 return 1;
14f9c5c9
AS
7508 else if (ada_is_packed_array_type (type0))
7509 return 1;
4c4b4cd2
PH
7510 else if (ada_is_array_descriptor_type (type0)
7511 && !ada_is_array_descriptor_type (type1))
14f9c5c9 7512 return 1;
d2e4a39e 7513 else if (ada_renaming_type (type0) != NULL
4c4b4cd2 7514 && ada_renaming_type (type1) == NULL)
14f9c5c9
AS
7515 return 1;
7516 return 0;
7517}
7518
7519/* The name of TYPE, which is either its TYPE_NAME, or, if that is
4c4b4cd2
PH
7520 null, its TYPE_TAG_NAME. Null if TYPE is null. */
7521
d2e4a39e
AS
7522char *
7523ada_type_name (struct type *type)
14f9c5c9 7524{
d2e4a39e 7525 if (type == NULL)
14f9c5c9
AS
7526 return NULL;
7527 else if (TYPE_NAME (type) != NULL)
7528 return TYPE_NAME (type);
7529 else
7530 return TYPE_TAG_NAME (type);
7531}
7532
7533/* Find a parallel type to TYPE whose name is formed by appending
4c4b4cd2 7534 SUFFIX to the name of TYPE. */
14f9c5c9 7535
d2e4a39e 7536struct type *
ebf56fd3 7537ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 7538{
d2e4a39e 7539 static char *name;
14f9c5c9 7540 static size_t name_len = 0;
14f9c5c9 7541 int len;
d2e4a39e
AS
7542 char *typename = ada_type_name (type);
7543
14f9c5c9
AS
7544 if (typename == NULL)
7545 return NULL;
7546
7547 len = strlen (typename);
7548
d2e4a39e 7549 GROW_VECT (name, name_len, len + strlen (suffix) + 1);
14f9c5c9
AS
7550
7551 strcpy (name, typename);
7552 strcpy (name + len, suffix);
7553
7554 return ada_find_any_type (name);
7555}
7556
7557
7558/* If TYPE is a variable-size record type, return the corresponding template
4c4b4cd2 7559 type describing its fields. Otherwise, return NULL. */
14f9c5c9 7560
d2e4a39e
AS
7561static struct type *
7562dynamic_template_type (struct type *type)
14f9c5c9
AS
7563{
7564 CHECK_TYPEDEF (type);
7565
7566 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
d2e4a39e 7567 || ada_type_name (type) == NULL)
14f9c5c9 7568 return NULL;
d2e4a39e 7569 else
14f9c5c9
AS
7570 {
7571 int len = strlen (ada_type_name (type));
4c4b4cd2
PH
7572 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
7573 return type;
14f9c5c9 7574 else
4c4b4cd2 7575 return ada_find_parallel_type (type, "___XVE");
14f9c5c9
AS
7576 }
7577}
7578
7579/* Assuming that TEMPL_TYPE is a union or struct type, returns
4c4b4cd2 7580 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
14f9c5c9 7581
d2e4a39e
AS
7582static int
7583is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
7584{
7585 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
d2e4a39e 7586 return name != NULL
14f9c5c9
AS
7587 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
7588 && strstr (name, "___XVL") != NULL;
7589}
7590
4c4b4cd2
PH
7591/* The index of the variant field of TYPE, or -1 if TYPE does not
7592 represent a variant record type. */
14f9c5c9 7593
d2e4a39e 7594static int
4c4b4cd2 7595variant_field_index (struct type *type)
14f9c5c9
AS
7596{
7597 int f;
7598
4c4b4cd2
PH
7599 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
7600 return -1;
7601
7602 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
7603 {
7604 if (ada_is_variant_part (type, f))
7605 return f;
7606 }
7607 return -1;
14f9c5c9
AS
7608}
7609
4c4b4cd2
PH
7610/* A record type with no fields. */
7611
d2e4a39e
AS
7612static struct type *
7613empty_record (struct objfile *objfile)
14f9c5c9 7614{
d2e4a39e 7615 struct type *type = alloc_type (objfile);
14f9c5c9
AS
7616 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7617 TYPE_NFIELDS (type) = 0;
7618 TYPE_FIELDS (type) = NULL;
7619 TYPE_NAME (type) = "<empty>";
7620 TYPE_TAG_NAME (type) = NULL;
7621 TYPE_FLAGS (type) = 0;
7622 TYPE_LENGTH (type) = 0;
7623 return type;
7624}
7625
7626/* An ordinary record type (with fixed-length fields) that describes
4c4b4cd2
PH
7627 the value of type TYPE at VALADDR or ADDRESS (see comments at
7628 the beginning of this section) VAL according to GNAT conventions.
7629 DVAL0 should describe the (portion of a) record that contains any
14f9c5c9
AS
7630 necessary discriminants. It should be NULL if VALUE_TYPE (VAL) is
7631 an outer-level type (i.e., as opposed to a branch of a variant.) A
7632 variant field (unless unchecked) is replaced by a particular branch
4c4b4cd2 7633 of the variant.
14f9c5c9 7634
4c4b4cd2
PH
7635 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7636 length are not statically known are discarded. As a consequence,
7637 VALADDR, ADDRESS and DVAL0 are ignored.
7638
7639 NOTE: Limitations: For now, we assume that dynamic fields and
7640 variants occupy whole numbers of bytes. However, they need not be
7641 byte-aligned. */
7642
7643struct type *
7644ada_template_to_fixed_record_type_1 (struct type *type, char *valaddr,
7645 CORE_ADDR address, struct value *dval0,
7646 int keep_dynamic_fields)
14f9c5c9 7647{
d2e4a39e
AS
7648 struct value *mark = value_mark ();
7649 struct value *dval;
7650 struct type *rtype;
14f9c5c9 7651 int nfields, bit_len;
4c4b4cd2 7652 int variant_field;
14f9c5c9 7653 long off;
4c4b4cd2 7654 int fld_bit_len, bit_incr;
14f9c5c9
AS
7655 int f;
7656
4c4b4cd2
PH
7657 /* Compute the number of fields in this record type that are going
7658 to be processed: unless keep_dynamic_fields, this includes only
7659 fields whose position and length are static will be processed. */
7660 if (keep_dynamic_fields)
7661 nfields = TYPE_NFIELDS (type);
7662 else
7663 {
7664 nfields = 0;
76a01679 7665 while (nfields < TYPE_NFIELDS (type)
4c4b4cd2
PH
7666 && !ada_is_variant_part (type, nfields)
7667 && !is_dynamic_field (type, nfields))
7668 nfields++;
7669 }
7670
14f9c5c9
AS
7671 rtype = alloc_type (TYPE_OBJFILE (type));
7672 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7673 INIT_CPLUS_SPECIFIC (rtype);
7674 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e 7675 TYPE_FIELDS (rtype) = (struct field *)
14f9c5c9
AS
7676 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7677 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
7678 TYPE_NAME (rtype) = ada_type_name (type);
7679 TYPE_TAG_NAME (rtype) = NULL;
4c4b4cd2 7680 TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9 7681
d2e4a39e
AS
7682 off = 0;
7683 bit_len = 0;
4c4b4cd2
PH
7684 variant_field = -1;
7685
14f9c5c9
AS
7686 for (f = 0; f < nfields; f += 1)
7687 {
d2e4a39e 7688 off =
4c4b4cd2
PH
7689 align_value (off,
7690 field_alignment (type, f)) + TYPE_FIELD_BITPOS (type, f);
14f9c5c9 7691 TYPE_FIELD_BITPOS (rtype, f) = off;
d2e4a39e 7692 TYPE_FIELD_BITSIZE (rtype, f) = 0;
14f9c5c9 7693
d2e4a39e 7694 if (ada_is_variant_part (type, f))
4c4b4cd2
PH
7695 {
7696 variant_field = f;
7697 fld_bit_len = bit_incr = 0;
7698 }
14f9c5c9 7699 else if (is_dynamic_field (type, f))
4c4b4cd2
PH
7700 {
7701 if (dval0 == NULL)
7702 dval = value_from_contents_and_address (rtype, valaddr, address);
7703 else
7704 dval = dval0;
7705
7706 TYPE_FIELD_TYPE (rtype, f) =
7707 ada_to_fixed_type
7708 (ada_get_base_type
7709 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f))),
7710 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7711 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
7712 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7713 bit_incr = fld_bit_len =
7714 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
7715 }
14f9c5c9 7716 else
4c4b4cd2
PH
7717 {
7718 TYPE_FIELD_TYPE (rtype, f) = TYPE_FIELD_TYPE (type, f);
7719 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7720 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7721 bit_incr = fld_bit_len =
7722 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7723 else
7724 bit_incr = fld_bit_len =
7725 TYPE_LENGTH (TYPE_FIELD_TYPE (type, f)) * TARGET_CHAR_BIT;
7726 }
14f9c5c9 7727 if (off + fld_bit_len > bit_len)
4c4b4cd2 7728 bit_len = off + fld_bit_len;
14f9c5c9 7729 off += bit_incr;
4c4b4cd2
PH
7730 TYPE_LENGTH (rtype) =
7731 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
14f9c5c9 7732 }
4c4b4cd2
PH
7733
7734 /* We handle the variant part, if any, at the end because of certain
7735 odd cases in which it is re-ordered so as NOT the last field of
7736 the record. This can happen in the presence of representation
7737 clauses. */
7738 if (variant_field >= 0)
7739 {
7740 struct type *branch_type;
7741
7742 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7743
7744 if (dval0 == NULL)
7745 dval = value_from_contents_and_address (rtype, valaddr, address);
7746 else
7747 dval = dval0;
7748
7749 branch_type =
7750 to_fixed_variant_branch_type
7751 (TYPE_FIELD_TYPE (type, variant_field),
7752 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7753 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
7754 if (branch_type == NULL)
7755 {
7756 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
7757 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7758 TYPE_NFIELDS (rtype) -= 1;
7759 }
7760 else
7761 {
7762 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7763 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7764 fld_bit_len =
7765 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
7766 TARGET_CHAR_BIT;
7767 if (off + fld_bit_len > bit_len)
7768 bit_len = off + fld_bit_len;
7769 TYPE_LENGTH (rtype) =
7770 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7771 }
7772 }
7773
14f9c5c9
AS
7774 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype), TYPE_LENGTH (type));
7775
7776 value_free_to_mark (mark);
d2e4a39e 7777 if (TYPE_LENGTH (rtype) > varsize_limit)
14f9c5c9
AS
7778 error ("record type with dynamic size is larger than varsize-limit");
7779 return rtype;
7780}
7781
4c4b4cd2
PH
7782/* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7783 of 1. */
14f9c5c9 7784
d2e4a39e 7785static struct type *
4c4b4cd2
PH
7786template_to_fixed_record_type (struct type *type, char *valaddr,
7787 CORE_ADDR address, struct value *dval0)
7788{
7789 return ada_template_to_fixed_record_type_1 (type, valaddr,
7790 address, dval0, 1);
7791}
7792
7793/* An ordinary record type in which ___XVL-convention fields and
7794 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7795 static approximations, containing all possible fields. Uses
7796 no runtime values. Useless for use in values, but that's OK,
7797 since the results are used only for type determinations. Works on both
7798 structs and unions. Representation note: to save space, we memorize
7799 the result of this function in the TYPE_TARGET_TYPE of the
7800 template type. */
7801
7802static struct type *
7803template_to_static_fixed_type (struct type *type0)
14f9c5c9
AS
7804{
7805 struct type *type;
7806 int nfields;
7807 int f;
7808
4c4b4cd2
PH
7809 if (TYPE_TARGET_TYPE (type0) != NULL)
7810 return TYPE_TARGET_TYPE (type0);
7811
7812 nfields = TYPE_NFIELDS (type0);
7813 type = type0;
14f9c5c9
AS
7814
7815 for (f = 0; f < nfields; f += 1)
7816 {
4c4b4cd2
PH
7817 struct type *field_type = CHECK_TYPEDEF (TYPE_FIELD_TYPE (type0, f));
7818 struct type *new_type;
14f9c5c9 7819
4c4b4cd2
PH
7820 if (is_dynamic_field (type0, f))
7821 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
14f9c5c9 7822 else
4c4b4cd2
PH
7823 new_type = to_static_fixed_type (field_type);
7824 if (type == type0 && new_type != field_type)
7825 {
7826 TYPE_TARGET_TYPE (type0) = type = alloc_type (TYPE_OBJFILE (type0));
7827 TYPE_CODE (type) = TYPE_CODE (type0);
7828 INIT_CPLUS_SPECIFIC (type);
7829 TYPE_NFIELDS (type) = nfields;
7830 TYPE_FIELDS (type) = (struct field *)
7831 TYPE_ALLOC (type, nfields * sizeof (struct field));
7832 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
7833 sizeof (struct field) * nfields);
7834 TYPE_NAME (type) = ada_type_name (type0);
7835 TYPE_TAG_NAME (type) = NULL;
7836 TYPE_FLAGS (type) |= TYPE_FLAG_FIXED_INSTANCE;
7837 TYPE_LENGTH (type) = 0;
7838 }
7839 TYPE_FIELD_TYPE (type, f) = new_type;
7840 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
14f9c5c9 7841 }
14f9c5c9
AS
7842 return type;
7843}
7844
4c4b4cd2
PH
7845/* Given an object of type TYPE whose contents are at VALADDR and
7846 whose address in memory is ADDRESS, returns a revision of TYPE --
7847 a non-dynamic-sized record with a variant part -- in which
7848 the variant part is replaced with the appropriate branch. Looks
7849 for discriminant values in DVAL0, which can be NULL if the record
7850 contains the necessary discriminant values. */
7851
d2e4a39e
AS
7852static struct type *
7853to_record_with_fixed_variant_part (struct type *type, char *valaddr,
4c4b4cd2 7854 CORE_ADDR address, struct value *dval0)
14f9c5c9 7855{
d2e4a39e 7856 struct value *mark = value_mark ();
4c4b4cd2 7857 struct value *dval;
d2e4a39e 7858 struct type *rtype;
14f9c5c9
AS
7859 struct type *branch_type;
7860 int nfields = TYPE_NFIELDS (type);
4c4b4cd2 7861 int variant_field = variant_field_index (type);
14f9c5c9 7862
4c4b4cd2 7863 if (variant_field == -1)
14f9c5c9
AS
7864 return type;
7865
4c4b4cd2
PH
7866 if (dval0 == NULL)
7867 dval = value_from_contents_and_address (type, valaddr, address);
7868 else
7869 dval = dval0;
7870
14f9c5c9
AS
7871 rtype = alloc_type (TYPE_OBJFILE (type));
7872 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
4c4b4cd2
PH
7873 INIT_CPLUS_SPECIFIC (rtype);
7874 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e
AS
7875 TYPE_FIELDS (rtype) =
7876 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7877 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
4c4b4cd2 7878 sizeof (struct field) * nfields);
14f9c5c9
AS
7879 TYPE_NAME (rtype) = ada_type_name (type);
7880 TYPE_TAG_NAME (rtype) = NULL;
4c4b4cd2 7881 TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
7882 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
7883
4c4b4cd2
PH
7884 branch_type = to_fixed_variant_branch_type
7885 (TYPE_FIELD_TYPE (type, variant_field),
d2e4a39e 7886 cond_offset_host (valaddr,
4c4b4cd2
PH
7887 TYPE_FIELD_BITPOS (type, variant_field)
7888 / TARGET_CHAR_BIT),
d2e4a39e 7889 cond_offset_target (address,
4c4b4cd2
PH
7890 TYPE_FIELD_BITPOS (type, variant_field)
7891 / TARGET_CHAR_BIT), dval);
d2e4a39e 7892 if (branch_type == NULL)
14f9c5c9 7893 {
4c4b4cd2
PH
7894 int f;
7895 for (f = variant_field + 1; f < nfields; f += 1)
7896 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
14f9c5c9 7897 TYPE_NFIELDS (rtype) -= 1;
14f9c5c9
AS
7898 }
7899 else
7900 {
4c4b4cd2
PH
7901 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7902 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7903 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
14f9c5c9 7904 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
14f9c5c9 7905 }
4c4b4cd2 7906 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
d2e4a39e 7907
4c4b4cd2 7908 value_free_to_mark (mark);
14f9c5c9
AS
7909 return rtype;
7910}
7911
7912/* An ordinary record type (with fixed-length fields) that describes
7913 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
7914 beginning of this section]. Any necessary discriminants' values
4c4b4cd2
PH
7915 should be in DVAL, a record value; it may be NULL if the object
7916 at ADDR itself contains any necessary discriminant values.
7917 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
7918 values from the record are needed. Except in the case that DVAL,
7919 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
7920 unchecked) is replaced by a particular branch of the variant.
7921
7922 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
7923 is questionable and may be removed. It can arise during the
7924 processing of an unconstrained-array-of-record type where all the
7925 variant branches have exactly the same size. This is because in
7926 such cases, the compiler does not bother to use the XVS convention
7927 when encoding the record. I am currently dubious of this
7928 shortcut and suspect the compiler should be altered. FIXME. */
14f9c5c9 7929
d2e4a39e 7930static struct type *
4c4b4cd2
PH
7931to_fixed_record_type (struct type *type0, char *valaddr,
7932 CORE_ADDR address, struct value *dval)
14f9c5c9 7933{
d2e4a39e 7934 struct type *templ_type;
14f9c5c9 7935
4c4b4cd2
PH
7936 if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
7937 return type0;
7938
d2e4a39e 7939 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
7940
7941 if (templ_type != NULL)
7942 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
4c4b4cd2
PH
7943 else if (variant_field_index (type0) >= 0)
7944 {
7945 if (dval == NULL && valaddr == NULL && address == 0)
7946 return type0;
7947 return to_record_with_fixed_variant_part (type0, valaddr, address,
7948 dval);
7949 }
14f9c5c9
AS
7950 else
7951 {
4c4b4cd2 7952 TYPE_FLAGS (type0) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9
AS
7953 return type0;
7954 }
7955
7956}
7957
7958/* An ordinary record type (with fixed-length fields) that describes
7959 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
7960 union type. Any necessary discriminants' values should be in DVAL,
7961 a record value. That is, this routine selects the appropriate
7962 branch of the union at ADDR according to the discriminant value
4c4b4cd2 7963 indicated in the union's type name. */
14f9c5c9 7964
d2e4a39e
AS
7965static struct type *
7966to_fixed_variant_branch_type (struct type *var_type0, char *valaddr,
4c4b4cd2 7967 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
7968{
7969 int which;
d2e4a39e
AS
7970 struct type *templ_type;
7971 struct type *var_type;
14f9c5c9
AS
7972
7973 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
7974 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 7975 else
14f9c5c9
AS
7976 var_type = var_type0;
7977
7978 templ_type = ada_find_parallel_type (var_type, "___XVU");
7979
7980 if (templ_type != NULL)
7981 var_type = templ_type;
7982
d2e4a39e
AS
7983 which =
7984 ada_which_variant_applies (var_type,
4c4b4cd2 7985 VALUE_TYPE (dval), VALUE_CONTENTS (dval));
14f9c5c9
AS
7986
7987 if (which < 0)
7988 return empty_record (TYPE_OBJFILE (var_type));
7989 else if (is_dynamic_field (var_type, which))
4c4b4cd2 7990 return to_fixed_record_type
d2e4a39e
AS
7991 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
7992 valaddr, address, dval);
4c4b4cd2 7993 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
d2e4a39e
AS
7994 return
7995 to_fixed_record_type
7996 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
14f9c5c9
AS
7997 else
7998 return TYPE_FIELD_TYPE (var_type, which);
7999}
8000
8001/* Assuming that TYPE0 is an array type describing the type of a value
8002 at ADDR, and that DVAL describes a record containing any
8003 discriminants used in TYPE0, returns a type for the value that
8004 contains no dynamic components (that is, no components whose sizes
8005 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8006 true, gives an error message if the resulting type's size is over
4c4b4cd2 8007 varsize_limit. */
14f9c5c9 8008
d2e4a39e
AS
8009static struct type *
8010to_fixed_array_type (struct type *type0, struct value *dval,
4c4b4cd2 8011 int ignore_too_big)
14f9c5c9 8012{
d2e4a39e
AS
8013 struct type *index_type_desc;
8014 struct type *result;
14f9c5c9 8015
4c4b4cd2
PH
8016 if (ada_is_packed_array_type (type0) /* revisit? */
8017 || (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE))
8018 return type0;
14f9c5c9
AS
8019
8020 index_type_desc = ada_find_parallel_type (type0, "___XA");
8021 if (index_type_desc == NULL)
8022 {
8023 struct type *elt_type0 = check_typedef (TYPE_TARGET_TYPE (type0));
8024 /* NOTE: elt_type---the fixed version of elt_type0---should never
4c4b4cd2
PH
8025 depend on the contents of the array in properly constructed
8026 debugging data. */
d2e4a39e 8027 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval);
14f9c5c9
AS
8028
8029 if (elt_type0 == elt_type)
4c4b4cd2 8030 result = type0;
14f9c5c9 8031 else
4c4b4cd2
PH
8032 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
8033 elt_type, TYPE_INDEX_TYPE (type0));
14f9c5c9
AS
8034 }
8035 else
8036 {
8037 int i;
8038 struct type *elt_type0;
8039
8040 elt_type0 = type0;
8041 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
4c4b4cd2 8042 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
14f9c5c9
AS
8043
8044 /* NOTE: result---the fixed version of elt_type0---should never
4c4b4cd2
PH
8045 depend on the contents of the array in properly constructed
8046 debugging data. */
d2e4a39e 8047 result = ada_to_fixed_type (check_typedef (elt_type0), 0, 0, dval);
14f9c5c9 8048 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
4c4b4cd2
PH
8049 {
8050 struct type *range_type =
8051 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, i),
8052 dval, TYPE_OBJFILE (type0));
8053 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
8054 result, range_type);
8055 }
d2e4a39e 8056 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
4c4b4cd2 8057 error ("array type with dynamic size is larger than varsize-limit");
14f9c5c9
AS
8058 }
8059
4c4b4cd2 8060 TYPE_FLAGS (result) |= TYPE_FLAG_FIXED_INSTANCE;
14f9c5c9 8061 return result;
d2e4a39e 8062}
14f9c5c9
AS
8063
8064
8065/* A standard type (containing no dynamically sized components)
8066 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8067 DVAL describes a record containing any discriminants used in TYPE0,
4c4b4cd2
PH
8068 and may be NULL if there are none, or if the object of type TYPE at
8069 ADDRESS or in VALADDR contains these discriminants. */
14f9c5c9 8070
d2e4a39e 8071struct type *
4c4b4cd2
PH
8072ada_to_fixed_type (struct type *type, char *valaddr,
8073 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
8074{
8075 CHECK_TYPEDEF (type);
d2e4a39e
AS
8076 switch (TYPE_CODE (type))
8077 {
8078 default:
14f9c5c9 8079 return type;
d2e4a39e 8080 case TYPE_CODE_STRUCT:
4c4b4cd2 8081 {
76a01679
JB
8082 struct type *static_type = to_static_fixed_type (type);
8083 if (ada_is_tagged_type (static_type, 0))
8084 {
8085 struct type *real_type =
8086 type_from_tag (value_tag_from_contents_and_address (static_type,
8087 valaddr,
8088 address));
8089 if (real_type != NULL)
8090 type = real_type;
8091 }
8092 return to_fixed_record_type (type, valaddr, address, NULL);
4c4b4cd2 8093 }
d2e4a39e 8094 case TYPE_CODE_ARRAY:
4c4b4cd2 8095 return to_fixed_array_type (type, dval, 1);
d2e4a39e
AS
8096 case TYPE_CODE_UNION:
8097 if (dval == NULL)
4c4b4cd2 8098 return type;
d2e4a39e 8099 else
4c4b4cd2 8100 return to_fixed_variant_branch_type (type, valaddr, address, dval);
d2e4a39e 8101 }
14f9c5c9
AS
8102}
8103
8104/* A standard (static-sized) type corresponding as well as possible to
4c4b4cd2 8105 TYPE0, but based on no runtime data. */
14f9c5c9 8106
d2e4a39e
AS
8107static struct type *
8108to_static_fixed_type (struct type *type0)
14f9c5c9 8109{
d2e4a39e 8110 struct type *type;
14f9c5c9
AS
8111
8112 if (type0 == NULL)
8113 return NULL;
8114
4c4b4cd2
PH
8115 if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
8116 return type0;
8117
14f9c5c9 8118 CHECK_TYPEDEF (type0);
d2e4a39e 8119
14f9c5c9
AS
8120 switch (TYPE_CODE (type0))
8121 {
8122 default:
8123 return type0;
8124 case TYPE_CODE_STRUCT:
8125 type = dynamic_template_type (type0);
d2e4a39e 8126 if (type != NULL)
4c4b4cd2
PH
8127 return template_to_static_fixed_type (type);
8128 else
8129 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8130 case TYPE_CODE_UNION:
8131 type = ada_find_parallel_type (type0, "___XVU");
8132 if (type != NULL)
4c4b4cd2
PH
8133 return template_to_static_fixed_type (type);
8134 else
8135 return template_to_static_fixed_type (type0);
14f9c5c9
AS
8136 }
8137}
8138
4c4b4cd2
PH
8139/* A static approximation of TYPE with all type wrappers removed. */
8140
d2e4a39e
AS
8141static struct type *
8142static_unwrap_type (struct type *type)
14f9c5c9
AS
8143{
8144 if (ada_is_aligner_type (type))
8145 {
d2e4a39e 8146 struct type *type1 = TYPE_FIELD_TYPE (check_typedef (type), 0);
14f9c5c9 8147 if (ada_type_name (type1) == NULL)
4c4b4cd2 8148 TYPE_NAME (type1) = ada_type_name (type);
14f9c5c9
AS
8149
8150 return static_unwrap_type (type1);
8151 }
d2e4a39e 8152 else
14f9c5c9 8153 {
d2e4a39e
AS
8154 struct type *raw_real_type = ada_get_base_type (type);
8155 if (raw_real_type == type)
4c4b4cd2 8156 return type;
14f9c5c9 8157 else
4c4b4cd2 8158 return to_static_fixed_type (raw_real_type);
14f9c5c9
AS
8159 }
8160}
8161
8162/* In some cases, incomplete and private types require
4c4b4cd2 8163 cross-references that are not resolved as records (for example,
14f9c5c9
AS
8164 type Foo;
8165 type FooP is access Foo;
8166 V: FooP;
8167 type Foo is array ...;
4c4b4cd2 8168 ). In these cases, since there is no mechanism for producing
14f9c5c9
AS
8169 cross-references to such types, we instead substitute for FooP a
8170 stub enumeration type that is nowhere resolved, and whose tag is
4c4b4cd2 8171 the name of the actual type. Call these types "non-record stubs". */
14f9c5c9
AS
8172
8173/* A type equivalent to TYPE that is not a non-record stub, if one
4c4b4cd2
PH
8174 exists, otherwise TYPE. */
8175
d2e4a39e
AS
8176struct type *
8177ada_completed_type (struct type *type)
14f9c5c9
AS
8178{
8179 CHECK_TYPEDEF (type);
8180 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
8181 || (TYPE_FLAGS (type) & TYPE_FLAG_STUB) == 0
8182 || TYPE_TAG_NAME (type) == NULL)
8183 return type;
d2e4a39e 8184 else
14f9c5c9 8185 {
d2e4a39e
AS
8186 char *name = TYPE_TAG_NAME (type);
8187 struct type *type1 = ada_find_any_type (name);
14f9c5c9
AS
8188 return (type1 == NULL) ? type : type1;
8189 }
8190}
8191
8192/* A value representing the data at VALADDR/ADDRESS as described by
8193 type TYPE0, but with a standard (static-sized) type that correctly
8194 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8195 type, then return VAL0 [this feature is simply to avoid redundant
4c4b4cd2 8196 creation of struct values]. */
14f9c5c9 8197
4c4b4cd2
PH
8198static struct value *
8199ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
8200 struct value *val0)
14f9c5c9 8201{
4c4b4cd2 8202 struct type *type = ada_to_fixed_type (type0, 0, address, NULL);
14f9c5c9
AS
8203 if (type == type0 && val0 != NULL)
8204 return val0;
d2e4a39e 8205 else
4c4b4cd2
PH
8206 return value_from_contents_and_address (type, 0, address);
8207}
8208
8209/* A value representing VAL, but with a standard (static-sized) type
8210 that correctly describes it. Does not necessarily create a new
8211 value. */
8212
8213static struct value *
8214ada_to_fixed_value (struct value *val)
8215{
8216 return ada_to_fixed_value_create (VALUE_TYPE (val),
8217 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
8218 val);
14f9c5c9
AS
8219}
8220
4c4b4cd2
PH
8221/* If the PC is pointing inside a function prologue, then re-adjust it
8222 past this prologue. */
8223
8224static void
8225adjust_pc_past_prologue (CORE_ADDR *pc)
8226{
8227 struct symbol *func_sym = find_pc_function (*pc);
8228
8229 if (func_sym)
8230 {
76a01679
JB
8231 const struct symtab_and_line sal =
8232 find_function_start_sal (func_sym, 1);
4c4b4cd2
PH
8233
8234 if (*pc <= sal.pc)
8235 *pc = sal.pc;
8236 }
8237}
8238
8239/* A value representing VAL, but with a standard (static-sized) type
14f9c5c9
AS
8240 chosen to approximate the real type of VAL as well as possible, but
8241 without consulting any runtime values. For Ada dynamic-sized
4c4b4cd2 8242 types, therefore, the type of the result is likely to be inaccurate. */
14f9c5c9 8243
d2e4a39e
AS
8244struct value *
8245ada_to_static_fixed_value (struct value *val)
14f9c5c9 8246{
d2e4a39e 8247 struct type *type =
14f9c5c9
AS
8248 to_static_fixed_type (static_unwrap_type (VALUE_TYPE (val)));
8249 if (type == VALUE_TYPE (val))
8250 return val;
8251 else
4c4b4cd2 8252 return coerce_unspec_val_to_type (val, type);
14f9c5c9 8253}
d2e4a39e 8254\f
14f9c5c9 8255
14f9c5c9
AS
8256/* Attributes */
8257
4c4b4cd2
PH
8258/* Table mapping attribute numbers to names.
8259 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
14f9c5c9 8260
d2e4a39e 8261static const char *attribute_names[] = {
14f9c5c9
AS
8262 "<?>",
8263
d2e4a39e 8264 "first",
14f9c5c9
AS
8265 "last",
8266 "length",
8267 "image",
14f9c5c9
AS
8268 "max",
8269 "min",
4c4b4cd2
PH
8270 "modulus",
8271 "pos",
8272 "size",
8273 "tag",
14f9c5c9 8274 "val",
14f9c5c9
AS
8275 0
8276};
8277
d2e4a39e 8278const char *
4c4b4cd2 8279ada_attribute_name (enum exp_opcode n)
14f9c5c9 8280{
4c4b4cd2
PH
8281 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
8282 return attribute_names[n - OP_ATR_FIRST + 1];
14f9c5c9
AS
8283 else
8284 return attribute_names[0];
8285}
8286
4c4b4cd2 8287/* Evaluate the 'POS attribute applied to ARG. */
14f9c5c9 8288
4c4b4cd2
PH
8289static LONGEST
8290pos_atr (struct value *arg)
14f9c5c9
AS
8291{
8292 struct type *type = VALUE_TYPE (arg);
8293
d2e4a39e 8294 if (!discrete_type_p (type))
14f9c5c9
AS
8295 error ("'POS only defined on discrete types");
8296
8297 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
8298 {
8299 int i;
8300 LONGEST v = value_as_long (arg);
8301
d2e4a39e 8302 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
4c4b4cd2
PH
8303 {
8304 if (v == TYPE_FIELD_BITPOS (type, i))
8305 return i;
8306 }
14f9c5c9
AS
8307 error ("enumeration value is invalid: can't find 'POS");
8308 }
8309 else
4c4b4cd2
PH
8310 return value_as_long (arg);
8311}
8312
8313static struct value *
8314value_pos_atr (struct value *arg)
8315{
8316 return value_from_longest (builtin_type_ada_int, pos_atr (arg));
14f9c5c9
AS
8317}
8318
4c4b4cd2 8319/* Evaluate the TYPE'VAL attribute applied to ARG. */
14f9c5c9 8320
d2e4a39e
AS
8321static struct value *
8322value_val_atr (struct type *type, struct value *arg)
14f9c5c9 8323{
d2e4a39e 8324 if (!discrete_type_p (type))
14f9c5c9 8325 error ("'VAL only defined on discrete types");
d2e4a39e 8326 if (!integer_type_p (VALUE_TYPE (arg)))
14f9c5c9
AS
8327 error ("'VAL requires integral argument");
8328
8329 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
8330 {
8331 long pos = value_as_long (arg);
8332 if (pos < 0 || pos >= TYPE_NFIELDS (type))
4c4b4cd2 8333 error ("argument to 'VAL out of range");
d2e4a39e 8334 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
14f9c5c9
AS
8335 }
8336 else
8337 return value_from_longest (type, value_as_long (arg));
8338}
14f9c5c9 8339\f
d2e4a39e 8340
4c4b4cd2 8341 /* Evaluation */
14f9c5c9 8342
4c4b4cd2
PH
8343/* True if TYPE appears to be an Ada character type.
8344 [At the moment, this is true only for Character and Wide_Character;
8345 It is a heuristic test that could stand improvement]. */
14f9c5c9 8346
d2e4a39e
AS
8347int
8348ada_is_character_type (struct type *type)
14f9c5c9 8349{
d2e4a39e
AS
8350 const char *name = ada_type_name (type);
8351 return
14f9c5c9 8352 name != NULL
d2e4a39e 8353 && (TYPE_CODE (type) == TYPE_CODE_CHAR
4c4b4cd2
PH
8354 || TYPE_CODE (type) == TYPE_CODE_INT
8355 || TYPE_CODE (type) == TYPE_CODE_RANGE)
8356 && (strcmp (name, "character") == 0
8357 || strcmp (name, "wide_character") == 0
8358 || strcmp (name, "unsigned char") == 0);
14f9c5c9
AS
8359}
8360
4c4b4cd2 8361/* True if TYPE appears to be an Ada string type. */
14f9c5c9
AS
8362
8363int
ebf56fd3 8364ada_is_string_type (struct type *type)
14f9c5c9
AS
8365{
8366 CHECK_TYPEDEF (type);
d2e4a39e 8367 if (type != NULL
14f9c5c9 8368 && TYPE_CODE (type) != TYPE_CODE_PTR
76a01679
JB
8369 && (ada_is_simple_array_type (type)
8370 || ada_is_array_descriptor_type (type))
14f9c5c9
AS
8371 && ada_array_arity (type) == 1)
8372 {
8373 struct type *elttype = ada_array_element_type (type, 1);
8374
8375 return ada_is_character_type (elttype);
8376 }
d2e4a39e 8377 else
14f9c5c9
AS
8378 return 0;
8379}
8380
8381
8382/* True if TYPE is a struct type introduced by the compiler to force the
8383 alignment of a value. Such types have a single field with a
4c4b4cd2 8384 distinctive name. */
14f9c5c9
AS
8385
8386int
ebf56fd3 8387ada_is_aligner_type (struct type *type)
14f9c5c9
AS
8388{
8389 CHECK_TYPEDEF (type);
8390 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
4c4b4cd2
PH
8391 && TYPE_NFIELDS (type) == 1
8392 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
14f9c5c9
AS
8393}
8394
8395/* If there is an ___XVS-convention type parallel to SUBTYPE, return
4c4b4cd2 8396 the parallel type. */
14f9c5c9 8397
d2e4a39e
AS
8398struct type *
8399ada_get_base_type (struct type *raw_type)
14f9c5c9 8400{
d2e4a39e
AS
8401 struct type *real_type_namer;
8402 struct type *raw_real_type;
14f9c5c9
AS
8403
8404 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
8405 return raw_type;
8406
8407 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 8408 if (real_type_namer == NULL
14f9c5c9
AS
8409 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
8410 || TYPE_NFIELDS (real_type_namer) != 1)
8411 return raw_type;
8412
8413 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
d2e4a39e 8414 if (raw_real_type == NULL)
14f9c5c9
AS
8415 return raw_type;
8416 else
8417 return raw_real_type;
d2e4a39e 8418}
14f9c5c9 8419
4c4b4cd2 8420/* The type of value designated by TYPE, with all aligners removed. */
14f9c5c9 8421
d2e4a39e
AS
8422struct type *
8423ada_aligned_type (struct type *type)
14f9c5c9
AS
8424{
8425 if (ada_is_aligner_type (type))
8426 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
8427 else
8428 return ada_get_base_type (type);
8429}
8430
8431
8432/* The address of the aligned value in an object at address VALADDR
4c4b4cd2 8433 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
14f9c5c9 8434
d2e4a39e 8435char *
ebf56fd3 8436ada_aligned_value_addr (struct type *type, char *valaddr)
14f9c5c9 8437{
d2e4a39e 8438 if (ada_is_aligner_type (type))
14f9c5c9 8439 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
4c4b4cd2
PH
8440 valaddr +
8441 TYPE_FIELD_BITPOS (type,
8442 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
8443 else
8444 return valaddr;
8445}
8446
4c4b4cd2
PH
8447
8448
14f9c5c9 8449/* The printed representation of an enumeration literal with encoded
4c4b4cd2 8450 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
8451const char *
8452ada_enum_name (const char *name)
14f9c5c9 8453{
4c4b4cd2
PH
8454 static char *result;
8455 static size_t result_len = 0;
d2e4a39e 8456 char *tmp;
14f9c5c9 8457
4c4b4cd2
PH
8458 /* First, unqualify the enumeration name:
8459 1. Search for the last '.' character. If we find one, then skip
76a01679
JB
8460 all the preceeding characters, the unqualified name starts
8461 right after that dot.
4c4b4cd2 8462 2. Otherwise, we may be debugging on a target where the compiler
76a01679
JB
8463 translates dots into "__". Search forward for double underscores,
8464 but stop searching when we hit an overloading suffix, which is
8465 of the form "__" followed by digits. */
4c4b4cd2
PH
8466
8467 if ((tmp = strrchr (name, '.')) != NULL)
8468 name = tmp + 1;
8469 else
14f9c5c9 8470 {
4c4b4cd2
PH
8471 while ((tmp = strstr (name, "__")) != NULL)
8472 {
8473 if (isdigit (tmp[2]))
8474 break;
8475 else
8476 name = tmp + 2;
8477 }
14f9c5c9
AS
8478 }
8479
8480 if (name[0] == 'Q')
8481 {
14f9c5c9
AS
8482 int v;
8483 if (name[1] == 'U' || name[1] == 'W')
4c4b4cd2
PH
8484 {
8485 if (sscanf (name + 2, "%x", &v) != 1)
8486 return name;
8487 }
14f9c5c9 8488 else
4c4b4cd2 8489 return name;
14f9c5c9 8490
4c4b4cd2 8491 GROW_VECT (result, result_len, 16);
14f9c5c9 8492 if (isascii (v) && isprint (v))
4c4b4cd2 8493 sprintf (result, "'%c'", v);
14f9c5c9 8494 else if (name[1] == 'U')
4c4b4cd2 8495 sprintf (result, "[\"%02x\"]", v);
14f9c5c9 8496 else
4c4b4cd2 8497 sprintf (result, "[\"%04x\"]", v);
14f9c5c9
AS
8498
8499 return result;
8500 }
d2e4a39e 8501 else
4c4b4cd2
PH
8502 {
8503 if ((tmp = strstr (name, "__")) != NULL
8504 || (tmp = strstr (name, "$")) != NULL)
8505 {
8506 GROW_VECT (result, result_len, tmp - name + 1);
8507 strncpy (result, name, tmp - name);
8508 result[tmp - name] = '\0';
8509 return result;
8510 }
8511
8512 return name;
8513 }
14f9c5c9
AS
8514}
8515
d2e4a39e 8516static struct value *
ebf56fd3 8517evaluate_subexp (struct type *expect_type, struct expression *exp, int *pos,
4c4b4cd2 8518 enum noside noside)
14f9c5c9 8519{
76a01679 8520 return (*exp->language_defn->la_exp_desc->evaluate_exp)
4c4b4cd2 8521 (expect_type, exp, pos, noside);
14f9c5c9
AS
8522}
8523
8524/* Evaluate the subexpression of EXP starting at *POS as for
8525 evaluate_type, updating *POS to point just past the evaluated
4c4b4cd2 8526 expression. */
14f9c5c9 8527
d2e4a39e
AS
8528static struct value *
8529evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 8530{
4c4b4cd2 8531 return (*exp->language_defn->la_exp_desc->evaluate_exp)
14f9c5c9
AS
8532 (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
8533}
8534
8535/* If VAL is wrapped in an aligner or subtype wrapper, return the
4c4b4cd2 8536 value it wraps. */
14f9c5c9 8537
d2e4a39e
AS
8538static struct value *
8539unwrap_value (struct value *val)
14f9c5c9 8540{
d2e4a39e 8541 struct type *type = check_typedef (VALUE_TYPE (val));
14f9c5c9
AS
8542 if (ada_is_aligner_type (type))
8543 {
d2e4a39e 8544 struct value *v = value_struct_elt (&val, NULL, "F",
4c4b4cd2 8545 NULL, "internal structure");
d2e4a39e 8546 struct type *val_type = check_typedef (VALUE_TYPE (v));
14f9c5c9 8547 if (ada_type_name (val_type) == NULL)
4c4b4cd2 8548 TYPE_NAME (val_type) = ada_type_name (type);
14f9c5c9
AS
8549
8550 return unwrap_value (v);
8551 }
d2e4a39e 8552 else
14f9c5c9 8553 {
d2e4a39e 8554 struct type *raw_real_type =
4c4b4cd2 8555 ada_completed_type (ada_get_base_type (type));
d2e4a39e 8556
14f9c5c9 8557 if (type == raw_real_type)
4c4b4cd2 8558 return val;
14f9c5c9 8559
d2e4a39e 8560 return
4c4b4cd2
PH
8561 coerce_unspec_val_to_type
8562 (val, ada_to_fixed_type (raw_real_type, 0,
8563 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
8564 NULL));
14f9c5c9
AS
8565 }
8566}
d2e4a39e
AS
8567
8568static struct value *
8569cast_to_fixed (struct type *type, struct value *arg)
14f9c5c9
AS
8570{
8571 LONGEST val;
8572
8573 if (type == VALUE_TYPE (arg))
8574 return arg;
8575 else if (ada_is_fixed_point_type (VALUE_TYPE (arg)))
d2e4a39e 8576 val = ada_float_to_fixed (type,
4c4b4cd2
PH
8577 ada_fixed_to_float (VALUE_TYPE (arg),
8578 value_as_long (arg)));
d2e4a39e 8579 else
14f9c5c9 8580 {
d2e4a39e 8581 DOUBLEST argd =
4c4b4cd2 8582 value_as_double (value_cast (builtin_type_double, value_copy (arg)));
14f9c5c9
AS
8583 val = ada_float_to_fixed (type, argd);
8584 }
8585
8586 return value_from_longest (type, val);
8587}
8588
d2e4a39e
AS
8589static struct value *
8590cast_from_fixed_to_double (struct value *arg)
14f9c5c9
AS
8591{
8592 DOUBLEST val = ada_fixed_to_float (VALUE_TYPE (arg),
4c4b4cd2 8593 value_as_long (arg));
14f9c5c9
AS
8594 return value_from_double (builtin_type_double, val);
8595}
8596
4c4b4cd2
PH
8597/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
8598 return the converted value. */
8599
d2e4a39e
AS
8600static struct value *
8601coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 8602{
d2e4a39e 8603 struct type *type2 = VALUE_TYPE (val);
14f9c5c9
AS
8604 if (type == type2)
8605 return val;
8606
8607 CHECK_TYPEDEF (type2);
8608 CHECK_TYPEDEF (type);
8609
d2e4a39e
AS
8610 if (TYPE_CODE (type2) == TYPE_CODE_PTR
8611 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
8612 {
8613 val = ada_value_ind (val);
8614 type2 = VALUE_TYPE (val);
8615 }
8616
d2e4a39e 8617 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
14f9c5c9
AS
8618 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8619 {
8620 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
4c4b4cd2
PH
8621 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
8622 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
8623 error ("Incompatible types in assignment");
14f9c5c9
AS
8624 VALUE_TYPE (val) = type;
8625 }
d2e4a39e 8626 return val;
14f9c5c9
AS
8627}
8628
4c4b4cd2
PH
8629static struct value *
8630ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8631{
8632 struct value *val;
8633 struct type *type1, *type2;
8634 LONGEST v, v1, v2;
8635
8636 COERCE_REF (arg1);
8637 COERCE_REF (arg2);
8638 type1 = base_type (check_typedef (VALUE_TYPE (arg1)));
8639 type2 = base_type (check_typedef (VALUE_TYPE (arg2)));
8640
76a01679
JB
8641 if (TYPE_CODE (type1) != TYPE_CODE_INT
8642 || TYPE_CODE (type2) != TYPE_CODE_INT)
4c4b4cd2
PH
8643 return value_binop (arg1, arg2, op);
8644
76a01679 8645 switch (op)
4c4b4cd2
PH
8646 {
8647 case BINOP_MOD:
8648 case BINOP_DIV:
8649 case BINOP_REM:
8650 break;
8651 default:
8652 return value_binop (arg1, arg2, op);
8653 }
8654
8655 v2 = value_as_long (arg2);
8656 if (v2 == 0)
8657 error ("second operand of %s must not be zero.", op_string (op));
8658
8659 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
8660 return value_binop (arg1, arg2, op);
8661
8662 v1 = value_as_long (arg1);
8663 switch (op)
8664 {
8665 case BINOP_DIV:
8666 v = v1 / v2;
76a01679
JB
8667 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
8668 v += v > 0 ? -1 : 1;
4c4b4cd2
PH
8669 break;
8670 case BINOP_REM:
8671 v = v1 % v2;
76a01679
JB
8672 if (v * v1 < 0)
8673 v -= v2;
4c4b4cd2
PH
8674 break;
8675 default:
8676 /* Should not reach this point. */
8677 v = 0;
8678 }
8679
8680 val = allocate_value (type1);
8681 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
76a01679 8682 TYPE_LENGTH (VALUE_TYPE (val)), v);
4c4b4cd2
PH
8683 return val;
8684}
8685
8686static int
8687ada_value_equal (struct value *arg1, struct value *arg2)
8688{
76a01679 8689 if (ada_is_direct_array_type (VALUE_TYPE (arg1))
4c4b4cd2
PH
8690 || ada_is_direct_array_type (VALUE_TYPE (arg2)))
8691 {
8692 arg1 = ada_coerce_to_simple_array (arg1);
8693 arg2 = ada_coerce_to_simple_array (arg2);
8694 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_ARRAY
76a01679
JB
8695 || TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ARRAY)
8696 error ("Attempt to compare array with non-array");
4c4b4cd2 8697 /* FIXME: The following works only for types whose
76a01679
JB
8698 representations use all bits (no padding or undefined bits)
8699 and do not have user-defined equality. */
8700 return
8701 TYPE_LENGTH (VALUE_TYPE (arg1)) == TYPE_LENGTH (VALUE_TYPE (arg2))
8702 && memcmp (VALUE_CONTENTS (arg1), VALUE_CONTENTS (arg2),
8703 TYPE_LENGTH (VALUE_TYPE (arg1))) == 0;
4c4b4cd2
PH
8704 }
8705 return value_equal (arg1, arg2);
8706}
8707
d2e4a39e 8708struct value *
ebf56fd3 8709ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
4c4b4cd2 8710 int *pos, enum noside noside)
14f9c5c9
AS
8711{
8712 enum exp_opcode op;
14f9c5c9
AS
8713 int tem, tem2, tem3;
8714 int pc;
8715 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
8716 struct type *type;
8717 int nargs;
d2e4a39e 8718 struct value **argvec;
14f9c5c9 8719
d2e4a39e
AS
8720 pc = *pos;
8721 *pos += 1;
14f9c5c9
AS
8722 op = exp->elts[pc].opcode;
8723
d2e4a39e 8724 switch (op)
14f9c5c9
AS
8725 {
8726 default:
8727 *pos -= 1;
d2e4a39e 8728 return
4c4b4cd2
PH
8729 unwrap_value (evaluate_subexp_standard
8730 (expect_type, exp, pos, noside));
8731
8732 case OP_STRING:
8733 {
76a01679
JB
8734 struct value *result;
8735 *pos -= 1;
8736 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
8737 /* The result type will have code OP_STRING, bashed there from
8738 OP_ARRAY. Bash it back. */
8739 if (TYPE_CODE (VALUE_TYPE (result)) == TYPE_CODE_STRING)
8740 TYPE_CODE (VALUE_TYPE (result)) = TYPE_CODE_ARRAY;
8741 return result;
4c4b4cd2 8742 }
14f9c5c9
AS
8743
8744 case UNOP_CAST:
8745 (*pos) += 2;
8746 type = exp->elts[pc + 1].type;
8747 arg1 = evaluate_subexp (type, exp, pos, noside);
8748 if (noside == EVAL_SKIP)
4c4b4cd2 8749 goto nosideret;
14f9c5c9 8750 if (type != check_typedef (VALUE_TYPE (arg1)))
4c4b4cd2
PH
8751 {
8752 if (ada_is_fixed_point_type (type))
8753 arg1 = cast_to_fixed (type, arg1);
8754 else if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
8755 arg1 = value_cast (type, cast_from_fixed_to_double (arg1));
8756 else if (VALUE_LVAL (arg1) == lval_memory)
8757 {
8758 /* This is in case of the really obscure (and undocumented,
8759 but apparently expected) case of (Foo) Bar.all, where Bar
8760 is an integer constant and Foo is a dynamic-sized type.
8761 If we don't do this, ARG1 will simply be relabeled with
8762 TYPE. */
8763 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8764 return value_zero (to_static_fixed_type (type), not_lval);
8765 arg1 =
8766 ada_to_fixed_value_create
8767 (type, VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), 0);
8768 }
8769 else
8770 arg1 = value_cast (type, arg1);
8771 }
14f9c5c9
AS
8772 return arg1;
8773
4c4b4cd2
PH
8774 case UNOP_QUAL:
8775 (*pos) += 2;
8776 type = exp->elts[pc + 1].type;
8777 return ada_evaluate_subexp (type, exp, pos, noside);
8778
14f9c5c9
AS
8779 case BINOP_ASSIGN:
8780 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8781 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
8782 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
8783 return arg1;
8784 if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
76a01679 8785 arg2 = cast_to_fixed (VALUE_TYPE (arg1), arg2);
4c4b4cd2 8786 else if (ada_is_fixed_point_type (VALUE_TYPE (arg2)))
76a01679
JB
8787 error
8788 ("Fixed-point values must be assigned to fixed-point variables");
d2e4a39e 8789 else
76a01679 8790 arg2 = coerce_for_assign (VALUE_TYPE (arg1), arg2);
4c4b4cd2 8791 return ada_value_assign (arg1, arg2);
14f9c5c9
AS
8792
8793 case BINOP_ADD:
8794 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8795 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8796 if (noside == EVAL_SKIP)
4c4b4cd2
PH
8797 goto nosideret;
8798 if ((ada_is_fixed_point_type (VALUE_TYPE (arg1))
76a01679
JB
8799 || ada_is_fixed_point_type (VALUE_TYPE (arg2)))
8800 && VALUE_TYPE (arg1) != VALUE_TYPE (arg2))
8801 error ("Operands of fixed-point addition must have the same type");
4c4b4cd2 8802 return value_cast (VALUE_TYPE (arg1), value_add (arg1, arg2));
14f9c5c9
AS
8803
8804 case BINOP_SUB:
8805 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8806 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8807 if (noside == EVAL_SKIP)
4c4b4cd2
PH
8808 goto nosideret;
8809 if ((ada_is_fixed_point_type (VALUE_TYPE (arg1))
76a01679
JB
8810 || ada_is_fixed_point_type (VALUE_TYPE (arg2)))
8811 && VALUE_TYPE (arg1) != VALUE_TYPE (arg2))
8812 error ("Operands of fixed-point subtraction must have the same type");
4c4b4cd2 8813 return value_cast (VALUE_TYPE (arg1), value_sub (arg1, arg2));
14f9c5c9
AS
8814
8815 case BINOP_MUL:
8816 case BINOP_DIV:
8817 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8818 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8819 if (noside == EVAL_SKIP)
4c4b4cd2
PH
8820 goto nosideret;
8821 else if (noside == EVAL_AVOID_SIDE_EFFECTS
76a01679 8822 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
4c4b4cd2 8823 return value_zero (VALUE_TYPE (arg1), not_lval);
14f9c5c9 8824 else
4c4b4cd2
PH
8825 {
8826 if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
8827 arg1 = cast_from_fixed_to_double (arg1);
8828 if (ada_is_fixed_point_type (VALUE_TYPE (arg2)))
8829 arg2 = cast_from_fixed_to_double (arg2);
8830 return ada_value_binop (arg1, arg2, op);
8831 }
8832
8833 case BINOP_REM:
8834 case BINOP_MOD:
8835 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8836 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8837 if (noside == EVAL_SKIP)
76a01679 8838 goto nosideret;
4c4b4cd2 8839 else if (noside == EVAL_AVOID_SIDE_EFFECTS
76a01679
JB
8840 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
8841 return value_zero (VALUE_TYPE (arg1), not_lval);
14f9c5c9 8842 else
76a01679 8843 return ada_value_binop (arg1, arg2, op);
14f9c5c9 8844
4c4b4cd2
PH
8845 case BINOP_EQUAL:
8846 case BINOP_NOTEQUAL:
14f9c5c9 8847 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4c4b4cd2 8848 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
14f9c5c9 8849 if (noside == EVAL_SKIP)
76a01679 8850 goto nosideret;
4c4b4cd2 8851 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 8852 tem = 0;
4c4b4cd2 8853 else
76a01679 8854 tem = ada_value_equal (arg1, arg2);
4c4b4cd2 8855 if (op == BINOP_NOTEQUAL)
76a01679 8856 tem = !tem;
4c4b4cd2
PH
8857 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
8858
8859 case UNOP_NEG:
8860 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8861 if (noside == EVAL_SKIP)
8862 goto nosideret;
14f9c5c9 8863 else if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
4c4b4cd2 8864 return value_cast (VALUE_TYPE (arg1), value_neg (arg1));
14f9c5c9 8865 else
4c4b4cd2
PH
8866 return value_neg (arg1);
8867
14f9c5c9
AS
8868 case OP_VAR_VALUE:
8869 *pos -= 1;
8870 if (noside == EVAL_SKIP)
4c4b4cd2
PH
8871 {
8872 *pos += 4;
8873 goto nosideret;
8874 }
8875 else if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
76a01679
JB
8876 /* Only encountered when an unresolved symbol occurs in a
8877 context other than a function call, in which case, it is
8878 illegal. */
4c4b4cd2
PH
8879 error ("Unexpected unresolved symbol, %s, during evaluation",
8880 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
14f9c5c9 8881 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
8882 {
8883 *pos += 4;
8884 return value_zero
8885 (to_static_fixed_type
8886 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
8887 not_lval);
8888 }
d2e4a39e 8889 else
4c4b4cd2
PH
8890 {
8891 arg1 =
8892 unwrap_value (evaluate_subexp_standard
8893 (expect_type, exp, pos, noside));
8894 return ada_to_fixed_value (arg1);
8895 }
8896
8897 case OP_FUNCALL:
8898 (*pos) += 2;
8899
8900 /* Allocate arg vector, including space for the function to be
8901 called in argvec[0] and a terminating NULL. */
8902 nargs = longest_to_int (exp->elts[pc + 1].longconst);
8903 argvec =
8904 (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
8905
8906 if (exp->elts[*pos].opcode == OP_VAR_VALUE
76a01679 8907 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
4c4b4cd2
PH
8908 error ("Unexpected unresolved symbol, %s, during evaluation",
8909 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
8910 else
8911 {
8912 for (tem = 0; tem <= nargs; tem += 1)
8913 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8914 argvec[tem] = 0;
8915
8916 if (noside == EVAL_SKIP)
8917 goto nosideret;
8918 }
8919
8920 if (ada_is_packed_array_type (desc_base_type (VALUE_TYPE (argvec[0]))))
8921 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
8922 else if (TYPE_CODE (VALUE_TYPE (argvec[0])) == TYPE_CODE_REF
76a01679
JB
8923 || (TYPE_CODE (VALUE_TYPE (argvec[0])) == TYPE_CODE_ARRAY
8924 && VALUE_LVAL (argvec[0]) == lval_memory))
4c4b4cd2
PH
8925 argvec[0] = value_addr (argvec[0]);
8926
8927 type = check_typedef (VALUE_TYPE (argvec[0]));
8928 if (TYPE_CODE (type) == TYPE_CODE_PTR)
8929 {
8930 switch (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (type))))
8931 {
8932 case TYPE_CODE_FUNC:
8933 type = check_typedef (TYPE_TARGET_TYPE (type));
8934 break;
8935 case TYPE_CODE_ARRAY:
8936 break;
8937 case TYPE_CODE_STRUCT:
8938 if (noside != EVAL_AVOID_SIDE_EFFECTS)
8939 argvec[0] = ada_value_ind (argvec[0]);
8940 type = check_typedef (TYPE_TARGET_TYPE (type));
8941 break;
8942 default:
8943 error ("cannot subscript or call something of type `%s'",
8944 ada_type_name (VALUE_TYPE (argvec[0])));
8945 break;
8946 }
8947 }
8948
8949 switch (TYPE_CODE (type))
8950 {
8951 case TYPE_CODE_FUNC:
8952 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8953 return allocate_value (TYPE_TARGET_TYPE (type));
8954 return call_function_by_hand (argvec[0], nargs, argvec + 1);
8955 case TYPE_CODE_STRUCT:
8956 {
8957 int arity;
8958
4c4b4cd2
PH
8959 arity = ada_array_arity (type);
8960 type = ada_array_element_type (type, nargs);
8961 if (type == NULL)
8962 error ("cannot subscript or call a record");
8963 if (arity != nargs)
8964 error ("wrong number of subscripts; expecting %d", arity);
8965 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8966 return allocate_value (ada_aligned_type (type));
8967 return
8968 unwrap_value (ada_value_subscript
8969 (argvec[0], nargs, argvec + 1));
8970 }
8971 case TYPE_CODE_ARRAY:
8972 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8973 {
8974 type = ada_array_element_type (type, nargs);
8975 if (type == NULL)
8976 error ("element type of array unknown");
8977 else
8978 return allocate_value (ada_aligned_type (type));
8979 }
8980 return
8981 unwrap_value (ada_value_subscript
8982 (ada_coerce_to_simple_array (argvec[0]),
8983 nargs, argvec + 1));
8984 case TYPE_CODE_PTR: /* Pointer to array */
8985 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
8986 if (noside == EVAL_AVOID_SIDE_EFFECTS)
8987 {
8988 type = ada_array_element_type (type, nargs);
8989 if (type == NULL)
8990 error ("element type of array unknown");
8991 else
8992 return allocate_value (ada_aligned_type (type));
8993 }
8994 return
8995 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
8996 nargs, argvec + 1));
8997
8998 default:
8999 error ("Internal error in evaluate_subexp");
9000 }
9001
9002 case TERNOP_SLICE:
9003 {
9004 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9005 struct value *low_bound_val =
9006 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9007 LONGEST low_bound = pos_atr (low_bound_val);
9008 LONGEST high_bound
9009 = pos_atr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
9010 if (noside == EVAL_SKIP)
9011 goto nosideret;
9012
4c4b4cd2
PH
9013 /* If this is a reference to an aligner type, then remove all
9014 the aligners. */
9015 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_REF
9016 && ada_is_aligner_type (TYPE_TARGET_TYPE (VALUE_TYPE (array))))
9017 TYPE_TARGET_TYPE (VALUE_TYPE (array)) =
9018 ada_aligned_type (TYPE_TARGET_TYPE (VALUE_TYPE (array)));
9019
76a01679
JB
9020 if (ada_is_packed_array_type (VALUE_TYPE (array)))
9021 error ("cannot slice a packed array");
4c4b4cd2
PH
9022
9023 /* If this is a reference to an array or an array lvalue,
9024 convert to a pointer. */
9025 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_REF
9026 || (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
9027 && VALUE_LVAL (array) == lval_memory))
9028 array = value_addr (array);
9029
1265e4aa 9030 if (noside == EVAL_AVOID_SIDE_EFFECTS
0b5d8877
PH
9031 && ada_is_array_descriptor_type (check_typedef
9032 (VALUE_TYPE (array))))
9033 return empty_array (ada_type_of_array (array, 0), low_bound);
4c4b4cd2
PH
9034
9035 array = ada_coerce_to_simple_array_ptr (array);
9036
4c4b4cd2
PH
9037 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_PTR)
9038 {
0b5d8877 9039 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
9040 return empty_array (TYPE_TARGET_TYPE (VALUE_TYPE (array)),
9041 low_bound);
9042 else
9043 {
9044 struct type *arr_type0 =
9045 to_fixed_array_type (TYPE_TARGET_TYPE (VALUE_TYPE (array)),
9046 NULL, 1);
0b5d8877
PH
9047 return ada_value_slice_ptr (array, arr_type0,
9048 (int) low_bound, (int) high_bound);
4c4b4cd2
PH
9049 }
9050 }
9051 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9052 return array;
9053 else if (high_bound < low_bound)
9054 return empty_array (VALUE_TYPE (array), low_bound);
9055 else
0b5d8877 9056 return ada_value_slice (array, (int) low_bound, (int) high_bound);
4c4b4cd2 9057 }
14f9c5c9 9058
4c4b4cd2
PH
9059 case UNOP_IN_RANGE:
9060 (*pos) += 2;
9061 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9062 type = exp->elts[pc + 1].type;
14f9c5c9 9063
14f9c5c9 9064 if (noside == EVAL_SKIP)
4c4b4cd2 9065 goto nosideret;
14f9c5c9 9066
4c4b4cd2
PH
9067 switch (TYPE_CODE (type))
9068 {
9069 default:
9070 lim_warning ("Membership test incompletely implemented; "
9071 "always returns true", 0);
9072 return value_from_longest (builtin_type_int, (LONGEST) 1);
9073
9074 case TYPE_CODE_RANGE:
76a01679 9075 arg2 = value_from_longest (builtin_type_int, TYPE_LOW_BOUND (type));
4c4b4cd2
PH
9076 arg3 = value_from_longest (builtin_type_int,
9077 TYPE_HIGH_BOUND (type));
9078 return
9079 value_from_longest (builtin_type_int,
9080 (value_less (arg1, arg3)
9081 || value_equal (arg1, arg3))
9082 && (value_less (arg2, arg1)
9083 || value_equal (arg2, arg1)));
9084 }
9085
9086 case BINOP_IN_BOUNDS:
14f9c5c9 9087 (*pos) += 2;
4c4b4cd2
PH
9088 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9089 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 9090
4c4b4cd2
PH
9091 if (noside == EVAL_SKIP)
9092 goto nosideret;
14f9c5c9 9093
4c4b4cd2
PH
9094 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9095 return value_zero (builtin_type_int, not_lval);
14f9c5c9 9096
4c4b4cd2 9097 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 9098
4c4b4cd2
PH
9099 if (tem < 1 || tem > ada_array_arity (VALUE_TYPE (arg2)))
9100 error ("invalid dimension number to '%s", "range");
14f9c5c9 9101
4c4b4cd2
PH
9102 arg3 = ada_array_bound (arg2, tem, 1);
9103 arg2 = ada_array_bound (arg2, tem, 0);
d2e4a39e 9104
4c4b4cd2
PH
9105 return
9106 value_from_longest (builtin_type_int,
9107 (value_less (arg1, arg3)
9108 || value_equal (arg1, arg3))
9109 && (value_less (arg2, arg1)
9110 || value_equal (arg2, arg1)));
9111
9112 case TERNOP_IN_RANGE:
9113 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9114 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9115 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9116
9117 if (noside == EVAL_SKIP)
9118 goto nosideret;
9119
9120 return
9121 value_from_longest (builtin_type_int,
9122 (value_less (arg1, arg3)
9123 || value_equal (arg1, arg3))
9124 && (value_less (arg2, arg1)
9125 || value_equal (arg2, arg1)));
9126
9127 case OP_ATR_FIRST:
9128 case OP_ATR_LAST:
9129 case OP_ATR_LENGTH:
9130 {
76a01679
JB
9131 struct type *type_arg;
9132 if (exp->elts[*pos].opcode == OP_TYPE)
9133 {
9134 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9135 arg1 = NULL;
9136 type_arg = exp->elts[pc + 2].type;
9137 }
9138 else
9139 {
9140 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9141 type_arg = NULL;
9142 }
9143
9144 if (exp->elts[*pos].opcode != OP_LONG)
9145 error ("illegal operand to '%s", ada_attribute_name (op));
9146 tem = longest_to_int (exp->elts[*pos + 2].longconst);
9147 *pos += 4;
9148
9149 if (noside == EVAL_SKIP)
9150 goto nosideret;
9151
9152 if (type_arg == NULL)
9153 {
9154 arg1 = ada_coerce_ref (arg1);
9155
9156 if (ada_is_packed_array_type (VALUE_TYPE (arg1)))
9157 arg1 = ada_coerce_to_simple_array (arg1);
9158
9159 if (tem < 1 || tem > ada_array_arity (VALUE_TYPE (arg1)))
9160 error ("invalid dimension number to '%s",
9161 ada_attribute_name (op));
9162
9163 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9164 {
9165 type = ada_index_type (VALUE_TYPE (arg1), tem);
9166 if (type == NULL)
9167 error
9168 ("attempt to take bound of something that is not an array");
9169 return allocate_value (type);
9170 }
9171
9172 switch (op)
9173 {
9174 default: /* Should never happen. */
9175 error ("unexpected attribute encountered");
9176 case OP_ATR_FIRST:
9177 return ada_array_bound (arg1, tem, 0);
9178 case OP_ATR_LAST:
9179 return ada_array_bound (arg1, tem, 1);
9180 case OP_ATR_LENGTH:
9181 return ada_array_length (arg1, tem);
9182 }
9183 }
9184 else if (discrete_type_p (type_arg))
9185 {
9186 struct type *range_type;
9187 char *name = ada_type_name (type_arg);
9188 range_type = NULL;
9189 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
9190 range_type =
9191 to_fixed_range_type (name, NULL, TYPE_OBJFILE (type_arg));
9192 if (range_type == NULL)
9193 range_type = type_arg;
9194 switch (op)
9195 {
9196 default:
9197 error ("unexpected attribute encountered");
9198 case OP_ATR_FIRST:
9199 return discrete_type_low_bound (range_type);
9200 case OP_ATR_LAST:
9201 return discrete_type_high_bound (range_type);
9202 case OP_ATR_LENGTH:
9203 error ("the 'length attribute applies only to array types");
9204 }
9205 }
9206 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
9207 error ("unimplemented type attribute");
9208 else
9209 {
9210 LONGEST low, high;
9211
9212 if (ada_is_packed_array_type (type_arg))
9213 type_arg = decode_packed_array_type (type_arg);
9214
9215 if (tem < 1 || tem > ada_array_arity (type_arg))
9216 error ("invalid dimension number to '%s",
9217 ada_attribute_name (op));
9218
9219 type = ada_index_type (type_arg, tem);
9220 if (type == NULL)
9221 error
9222 ("attempt to take bound of something that is not an array");
9223 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9224 return allocate_value (type);
9225
9226 switch (op)
9227 {
9228 default:
9229 error ("unexpected attribute encountered");
9230 case OP_ATR_FIRST:
9231 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
9232 return value_from_longest (type, low);
9233 case OP_ATR_LAST:
9234 high = ada_array_bound_from_type (type_arg, tem, 1, &type);
9235 return value_from_longest (type, high);
9236 case OP_ATR_LENGTH:
9237 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
9238 high = ada_array_bound_from_type (type_arg, tem, 1, NULL);
9239 return value_from_longest (type, high - low + 1);
9240 }
9241 }
14f9c5c9
AS
9242 }
9243
4c4b4cd2
PH
9244 case OP_ATR_TAG:
9245 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9246 if (noside == EVAL_SKIP)
76a01679 9247 goto nosideret;
4c4b4cd2
PH
9248
9249 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9250 return value_zero (ada_tag_type (arg1), not_lval);
4c4b4cd2
PH
9251
9252 return ada_value_tag (arg1);
9253
9254 case OP_ATR_MIN:
9255 case OP_ATR_MAX:
9256 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
9257 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9258 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9259 if (noside == EVAL_SKIP)
76a01679 9260 goto nosideret;
d2e4a39e 9261 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9262 return value_zero (VALUE_TYPE (arg1), not_lval);
14f9c5c9 9263 else
76a01679
JB
9264 return value_binop (arg1, arg2,
9265 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
14f9c5c9 9266
4c4b4cd2
PH
9267 case OP_ATR_MODULUS:
9268 {
76a01679
JB
9269 struct type *type_arg = exp->elts[pc + 2].type;
9270 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
4c4b4cd2 9271
76a01679
JB
9272 if (noside == EVAL_SKIP)
9273 goto nosideret;
4c4b4cd2 9274
76a01679
JB
9275 if (!ada_is_modular_type (type_arg))
9276 error ("'modulus must be applied to modular type");
4c4b4cd2 9277
76a01679
JB
9278 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
9279 ada_modulus (type_arg));
4c4b4cd2
PH
9280 }
9281
9282
9283 case OP_ATR_POS:
9284 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9
AS
9285 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9286 if (noside == EVAL_SKIP)
76a01679 9287 goto nosideret;
4c4b4cd2 9288 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9289 return value_zero (builtin_type_ada_int, not_lval);
14f9c5c9 9290 else
76a01679 9291 return value_pos_atr (arg1);
14f9c5c9 9292
4c4b4cd2
PH
9293 case OP_ATR_SIZE:
9294 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9295 if (noside == EVAL_SKIP)
76a01679 9296 goto nosideret;
4c4b4cd2 9297 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9298 return value_zero (builtin_type_ada_int, not_lval);
4c4b4cd2 9299 else
76a01679
JB
9300 return value_from_longest (builtin_type_ada_int,
9301 TARGET_CHAR_BIT
9302 * TYPE_LENGTH (VALUE_TYPE (arg1)));
4c4b4cd2
PH
9303
9304 case OP_ATR_VAL:
9305 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
14f9c5c9 9306 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4c4b4cd2 9307 type = exp->elts[pc + 2].type;
14f9c5c9 9308 if (noside == EVAL_SKIP)
76a01679 9309 goto nosideret;
4c4b4cd2 9310 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679 9311 return value_zero (type, not_lval);
4c4b4cd2 9312 else
76a01679 9313 return value_val_atr (type, arg1);
4c4b4cd2
PH
9314
9315 case BINOP_EXP:
9316 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9317 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9318 if (noside == EVAL_SKIP)
9319 goto nosideret;
9320 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9321 return value_zero (VALUE_TYPE (arg1), not_lval);
9322 else
9323 return value_binop (arg1, arg2, op);
9324
9325 case UNOP_PLUS:
9326 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9327 if (noside == EVAL_SKIP)
9328 goto nosideret;
9329 else
9330 return arg1;
9331
9332 case UNOP_ABS:
9333 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9334 if (noside == EVAL_SKIP)
9335 goto nosideret;
14f9c5c9 9336 if (value_less (arg1, value_zero (VALUE_TYPE (arg1), not_lval)))
4c4b4cd2 9337 return value_neg (arg1);
14f9c5c9 9338 else
4c4b4cd2 9339 return arg1;
14f9c5c9
AS
9340
9341 case UNOP_IND:
9342 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
4c4b4cd2 9343 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
14f9c5c9
AS
9344 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
9345 if (noside == EVAL_SKIP)
4c4b4cd2 9346 goto nosideret;
14f9c5c9
AS
9347 type = check_typedef (VALUE_TYPE (arg1));
9348 if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2
PH
9349 {
9350 if (ada_is_array_descriptor_type (type))
9351 /* GDB allows dereferencing GNAT array descriptors. */
9352 {
9353 struct type *arrType = ada_type_of_array (arg1, 0);
9354 if (arrType == NULL)
9355 error ("Attempt to dereference null array pointer.");
9356 return value_at_lazy (arrType, 0, NULL);
9357 }
9358 else if (TYPE_CODE (type) == TYPE_CODE_PTR
9359 || TYPE_CODE (type) == TYPE_CODE_REF
9360 /* In C you can dereference an array to get the 1st elt. */
9361 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
9362 return
9363 value_zero
9364 (to_static_fixed_type
9365 (ada_aligned_type (check_typedef (TYPE_TARGET_TYPE (type)))),
9366 lval_memory);
9367 else if (TYPE_CODE (type) == TYPE_CODE_INT)
9368 /* GDB allows dereferencing an int. */
9369 return value_zero (builtin_type_int, lval_memory);
9370 else
9371 error ("Attempt to take contents of a non-pointer value.");
9372 }
76a01679 9373 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
14f9c5c9 9374 type = check_typedef (VALUE_TYPE (arg1));
d2e4a39e 9375
4c4b4cd2
PH
9376 if (ada_is_array_descriptor_type (type))
9377 /* GDB allows dereferencing GNAT array descriptors. */
9378 return ada_coerce_to_simple_array (arg1);
14f9c5c9 9379 else
4c4b4cd2 9380 return ada_value_ind (arg1);
14f9c5c9
AS
9381
9382 case STRUCTOP_STRUCT:
9383 tem = longest_to_int (exp->elts[pc + 1].longconst);
9384 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
9385 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9386 if (noside == EVAL_SKIP)
4c4b4cd2 9387 goto nosideret;
14f9c5c9 9388 if (noside == EVAL_AVOID_SIDE_EFFECTS)
76a01679
JB
9389 {
9390 struct type *type1 = VALUE_TYPE (arg1);
9391 if (ada_is_tagged_type (type1, 1))
9392 {
9393 type = ada_lookup_struct_elt_type (type1,
9394 &exp->elts[pc + 2].string,
9395 1, 1, NULL);
9396 if (type == NULL)
9397 /* In this case, we assume that the field COULD exist
9398 in some extension of the type. Return an object of
9399 "type" void, which will match any formal
9400 (see ada_type_match). */
9401 return value_zero (builtin_type_void, lval_memory);
9402 }
9403 else
9404 type =
9405 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
9406 0, NULL);
9407
9408 return value_zero (ada_aligned_type (type), lval_memory);
9409 }
14f9c5c9 9410 else
76a01679
JB
9411 return
9412 ada_to_fixed_value (unwrap_value
9413 (ada_value_struct_elt
9414 (arg1, &exp->elts[pc + 2].string, "record")));
14f9c5c9 9415 case OP_TYPE:
4c4b4cd2
PH
9416 /* The value is not supposed to be used. This is here to make it
9417 easier to accommodate expressions that contain types. */
14f9c5c9
AS
9418 (*pos) += 2;
9419 if (noside == EVAL_SKIP)
4c4b4cd2 9420 goto nosideret;
14f9c5c9 9421 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4c4b4cd2 9422 return allocate_value (builtin_type_void);
14f9c5c9 9423 else
4c4b4cd2 9424 error ("Attempt to use a type name as an expression");
14f9c5c9
AS
9425 }
9426
9427nosideret:
9428 return value_from_longest (builtin_type_long, (LONGEST) 1);
9429}
14f9c5c9 9430\f
d2e4a39e 9431
4c4b4cd2 9432 /* Fixed point */
14f9c5c9
AS
9433
9434/* If TYPE encodes an Ada fixed-point type, return the suffix of the
9435 type name that encodes the 'small and 'delta information.
4c4b4cd2 9436 Otherwise, return NULL. */
14f9c5c9 9437
d2e4a39e 9438static const char *
ebf56fd3 9439fixed_type_info (struct type *type)
14f9c5c9 9440{
d2e4a39e 9441 const char *name = ada_type_name (type);
14f9c5c9
AS
9442 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
9443
d2e4a39e
AS
9444 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
9445 {
14f9c5c9
AS
9446 const char *tail = strstr (name, "___XF_");
9447 if (tail == NULL)
4c4b4cd2 9448 return NULL;
d2e4a39e 9449 else
4c4b4cd2 9450 return tail + 5;
14f9c5c9
AS
9451 }
9452 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
9453 return fixed_type_info (TYPE_TARGET_TYPE (type));
9454 else
9455 return NULL;
9456}
9457
4c4b4cd2 9458/* Returns non-zero iff TYPE represents an Ada fixed-point type. */
14f9c5c9
AS
9459
9460int
ebf56fd3 9461ada_is_fixed_point_type (struct type *type)
14f9c5c9
AS
9462{
9463 return fixed_type_info (type) != NULL;
9464}
9465
4c4b4cd2
PH
9466/* Return non-zero iff TYPE represents a System.Address type. */
9467
9468int
9469ada_is_system_address_type (struct type *type)
9470{
9471 return (TYPE_NAME (type)
9472 && strcmp (TYPE_NAME (type), "system__address") == 0);
9473}
9474
14f9c5c9
AS
9475/* Assuming that TYPE is the representation of an Ada fixed-point
9476 type, return its delta, or -1 if the type is malformed and the
4c4b4cd2 9477 delta cannot be determined. */
14f9c5c9
AS
9478
9479DOUBLEST
ebf56fd3 9480ada_delta (struct type *type)
14f9c5c9
AS
9481{
9482 const char *encoding = fixed_type_info (type);
9483 long num, den;
9484
9485 if (sscanf (encoding, "_%ld_%ld", &num, &den) < 2)
9486 return -1.0;
d2e4a39e 9487 else
14f9c5c9
AS
9488 return (DOUBLEST) num / (DOUBLEST) den;
9489}
9490
9491/* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
4c4b4cd2 9492 factor ('SMALL value) associated with the type. */
14f9c5c9
AS
9493
9494static DOUBLEST
ebf56fd3 9495scaling_factor (struct type *type)
14f9c5c9
AS
9496{
9497 const char *encoding = fixed_type_info (type);
9498 unsigned long num0, den0, num1, den1;
9499 int n;
d2e4a39e 9500
14f9c5c9
AS
9501 n = sscanf (encoding, "_%lu_%lu_%lu_%lu", &num0, &den0, &num1, &den1);
9502
9503 if (n < 2)
9504 return 1.0;
9505 else if (n == 4)
9506 return (DOUBLEST) num1 / (DOUBLEST) den1;
d2e4a39e 9507 else
14f9c5c9
AS
9508 return (DOUBLEST) num0 / (DOUBLEST) den0;
9509}
9510
9511
9512/* Assuming that X is the representation of a value of fixed-point
4c4b4cd2 9513 type TYPE, return its floating-point equivalent. */
14f9c5c9
AS
9514
9515DOUBLEST
ebf56fd3 9516ada_fixed_to_float (struct type *type, LONGEST x)
14f9c5c9 9517{
d2e4a39e 9518 return (DOUBLEST) x *scaling_factor (type);
14f9c5c9
AS
9519}
9520
4c4b4cd2
PH
9521/* The representation of a fixed-point value of type TYPE
9522 corresponding to the value X. */
14f9c5c9
AS
9523
9524LONGEST
ebf56fd3 9525ada_float_to_fixed (struct type *type, DOUBLEST x)
14f9c5c9
AS
9526{
9527 return (LONGEST) (x / scaling_factor (type) + 0.5);
9528}
9529
9530
4c4b4cd2 9531 /* VAX floating formats */
14f9c5c9
AS
9532
9533/* Non-zero iff TYPE represents one of the special VAX floating-point
4c4b4cd2
PH
9534 types. */
9535
14f9c5c9 9536int
d2e4a39e 9537ada_is_vax_floating_type (struct type *type)
14f9c5c9 9538{
d2e4a39e 9539 int name_len =
14f9c5c9 9540 (ada_type_name (type) == NULL) ? 0 : strlen (ada_type_name (type));
d2e4a39e 9541 return
14f9c5c9 9542 name_len > 6
d2e4a39e 9543 && (TYPE_CODE (type) == TYPE_CODE_INT
4c4b4cd2
PH
9544 || TYPE_CODE (type) == TYPE_CODE_RANGE)
9545 && strncmp (ada_type_name (type) + name_len - 6, "___XF", 5) == 0;
14f9c5c9
AS
9546}
9547
9548/* The type of special VAX floating-point type this is, assuming
4c4b4cd2
PH
9549 ada_is_vax_floating_point. */
9550
14f9c5c9 9551int
d2e4a39e 9552ada_vax_float_type_suffix (struct type *type)
14f9c5c9 9553{
d2e4a39e 9554 return ada_type_name (type)[strlen (ada_type_name (type)) - 1];
14f9c5c9
AS
9555}
9556
4c4b4cd2 9557/* A value representing the special debugging function that outputs
14f9c5c9 9558 VAX floating-point values of the type represented by TYPE. Assumes
4c4b4cd2
PH
9559 ada_is_vax_floating_type (TYPE). */
9560
d2e4a39e
AS
9561struct value *
9562ada_vax_float_print_function (struct type *type)
9563{
9564 switch (ada_vax_float_type_suffix (type))
9565 {
9566 case 'F':
9567 return get_var_value ("DEBUG_STRING_F", 0);
9568 case 'D':
9569 return get_var_value ("DEBUG_STRING_D", 0);
9570 case 'G':
9571 return get_var_value ("DEBUG_STRING_G", 0);
9572 default:
9573 error ("invalid VAX floating-point type");
9574 }
14f9c5c9 9575}
14f9c5c9 9576\f
d2e4a39e 9577
4c4b4cd2 9578 /* Range types */
14f9c5c9
AS
9579
9580/* Scan STR beginning at position K for a discriminant name, and
9581 return the value of that discriminant field of DVAL in *PX. If
9582 PNEW_K is not null, put the position of the character beyond the
9583 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
4c4b4cd2 9584 not alter *PX and *PNEW_K if unsuccessful. */
14f9c5c9
AS
9585
9586static int
07d8f827 9587scan_discrim_bound (char *str, int k, struct value *dval, LONGEST * px,
76a01679 9588 int *pnew_k)
14f9c5c9
AS
9589{
9590 static char *bound_buffer = NULL;
9591 static size_t bound_buffer_len = 0;
9592 char *bound;
9593 char *pend;
d2e4a39e 9594 struct value *bound_val;
14f9c5c9
AS
9595
9596 if (dval == NULL || str == NULL || str[k] == '\0')
9597 return 0;
9598
d2e4a39e 9599 pend = strstr (str + k, "__");
14f9c5c9
AS
9600 if (pend == NULL)
9601 {
d2e4a39e 9602 bound = str + k;
14f9c5c9
AS
9603 k += strlen (bound);
9604 }
d2e4a39e 9605 else
14f9c5c9 9606 {
d2e4a39e 9607 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
14f9c5c9 9608 bound = bound_buffer;
d2e4a39e
AS
9609 strncpy (bound_buffer, str + k, pend - (str + k));
9610 bound[pend - (str + k)] = '\0';
9611 k = pend - str;
14f9c5c9 9612 }
d2e4a39e
AS
9613
9614 bound_val = ada_search_struct_field (bound, dval, 0, VALUE_TYPE (dval));
14f9c5c9
AS
9615 if (bound_val == NULL)
9616 return 0;
9617
9618 *px = value_as_long (bound_val);
9619 if (pnew_k != NULL)
9620 *pnew_k = k;
9621 return 1;
9622}
9623
9624/* Value of variable named NAME in the current environment. If
9625 no such variable found, then if ERR_MSG is null, returns 0, and
4c4b4cd2
PH
9626 otherwise causes an error with message ERR_MSG. */
9627
d2e4a39e
AS
9628static struct value *
9629get_var_value (char *name, char *err_msg)
14f9c5c9 9630{
4c4b4cd2 9631 struct ada_symbol_info *syms;
14f9c5c9
AS
9632 int nsyms;
9633
4c4b4cd2
PH
9634 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
9635 &syms);
14f9c5c9
AS
9636
9637 if (nsyms != 1)
9638 {
9639 if (err_msg == NULL)
4c4b4cd2 9640 return 0;
14f9c5c9 9641 else
4c4b4cd2 9642 error ("%s", err_msg);
14f9c5c9
AS
9643 }
9644
4c4b4cd2 9645 return value_of_variable (syms[0].sym, syms[0].block);
14f9c5c9 9646}
d2e4a39e 9647
14f9c5c9 9648/* Value of integer variable named NAME in the current environment. If
4c4b4cd2
PH
9649 no such variable found, returns 0, and sets *FLAG to 0. If
9650 successful, sets *FLAG to 1. */
9651
14f9c5c9 9652LONGEST
4c4b4cd2 9653get_int_var_value (char *name, int *flag)
14f9c5c9 9654{
4c4b4cd2 9655 struct value *var_val = get_var_value (name, 0);
d2e4a39e 9656
14f9c5c9
AS
9657 if (var_val == 0)
9658 {
9659 if (flag != NULL)
4c4b4cd2 9660 *flag = 0;
14f9c5c9
AS
9661 return 0;
9662 }
9663 else
9664 {
9665 if (flag != NULL)
4c4b4cd2 9666 *flag = 1;
14f9c5c9
AS
9667 return value_as_long (var_val);
9668 }
9669}
d2e4a39e 9670
14f9c5c9
AS
9671
9672/* Return a range type whose base type is that of the range type named
9673 NAME in the current environment, and whose bounds are calculated
4c4b4cd2 9674 from NAME according to the GNAT range encoding conventions.
14f9c5c9
AS
9675 Extract discriminant values, if needed, from DVAL. If a new type
9676 must be created, allocate in OBJFILE's space. The bounds
9677 information, in general, is encoded in NAME, the base type given in
4c4b4cd2 9678 the named range type. */
14f9c5c9 9679
d2e4a39e 9680static struct type *
ebf56fd3 9681to_fixed_range_type (char *name, struct value *dval, struct objfile *objfile)
14f9c5c9
AS
9682{
9683 struct type *raw_type = ada_find_any_type (name);
9684 struct type *base_type;
d2e4a39e 9685 char *subtype_info;
14f9c5c9
AS
9686
9687 if (raw_type == NULL)
9688 base_type = builtin_type_int;
9689 else if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
9690 base_type = TYPE_TARGET_TYPE (raw_type);
9691 else
9692 base_type = raw_type;
9693
9694 subtype_info = strstr (name, "___XD");
9695 if (subtype_info == NULL)
9696 return raw_type;
9697 else
9698 {
9699 static char *name_buf = NULL;
9700 static size_t name_len = 0;
9701 int prefix_len = subtype_info - name;
9702 LONGEST L, U;
9703 struct type *type;
9704 char *bounds_str;
9705 int n;
9706
9707 GROW_VECT (name_buf, name_len, prefix_len + 5);
9708 strncpy (name_buf, name, prefix_len);
9709 name_buf[prefix_len] = '\0';
9710
9711 subtype_info += 5;
9712 bounds_str = strchr (subtype_info, '_');
9713 n = 1;
9714
d2e4a39e 9715 if (*subtype_info == 'L')
4c4b4cd2
PH
9716 {
9717 if (!ada_scan_number (bounds_str, n, &L, &n)
9718 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
9719 return raw_type;
9720 if (bounds_str[n] == '_')
9721 n += 2;
9722 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
9723 n += 1;
9724 subtype_info += 1;
9725 }
d2e4a39e 9726 else
4c4b4cd2
PH
9727 {
9728 int ok;
9729 strcpy (name_buf + prefix_len, "___L");
9730 L = get_int_var_value (name_buf, &ok);
9731 if (!ok)
9732 {
9733 lim_warning ("Unknown lower bound, using 1.", 1);
9734 L = 1;
9735 }
9736 }
14f9c5c9 9737
d2e4a39e 9738 if (*subtype_info == 'U')
4c4b4cd2
PH
9739 {
9740 if (!ada_scan_number (bounds_str, n, &U, &n)
9741 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
9742 return raw_type;
9743 }
d2e4a39e 9744 else
4c4b4cd2
PH
9745 {
9746 int ok;
9747 strcpy (name_buf + prefix_len, "___U");
9748 U = get_int_var_value (name_buf, &ok);
9749 if (!ok)
9750 {
9751 lim_warning ("Unknown upper bound, using %ld.", (long) L);
9752 U = L;
9753 }
9754 }
14f9c5c9 9755
d2e4a39e 9756 if (objfile == NULL)
4c4b4cd2 9757 objfile = TYPE_OBJFILE (base_type);
14f9c5c9 9758 type = create_range_type (alloc_type (objfile), base_type, L, U);
d2e4a39e 9759 TYPE_NAME (type) = name;
14f9c5c9
AS
9760 return type;
9761 }
9762}
9763
4c4b4cd2
PH
9764/* True iff NAME is the name of a range type. */
9765
14f9c5c9 9766int
d2e4a39e 9767ada_is_range_type_name (const char *name)
14f9c5c9
AS
9768{
9769 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 9770}
14f9c5c9 9771\f
d2e4a39e 9772
4c4b4cd2
PH
9773 /* Modular types */
9774
9775/* True iff TYPE is an Ada modular type. */
14f9c5c9 9776
14f9c5c9 9777int
d2e4a39e 9778ada_is_modular_type (struct type *type)
14f9c5c9 9779{
4c4b4cd2 9780 struct type *subranged_type = base_type (type);
14f9c5c9
AS
9781
9782 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
4c4b4cd2
PH
9783 && TYPE_CODE (subranged_type) != TYPE_CODE_ENUM
9784 && TYPE_UNSIGNED (subranged_type));
14f9c5c9
AS
9785}
9786
4c4b4cd2
PH
9787/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
9788
14f9c5c9 9789LONGEST
d2e4a39e 9790ada_modulus (struct type * type)
14f9c5c9 9791{
d2e4a39e 9792 return TYPE_HIGH_BOUND (type) + 1;
14f9c5c9 9793}
d2e4a39e 9794\f
4c4b4cd2
PH
9795 /* Operators */
9796/* Information about operators given special treatment in functions
9797 below. */
9798/* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
9799
9800#define ADA_OPERATORS \
9801 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
9802 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
9803 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
9804 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
9805 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
9806 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
9807 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
9808 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
9809 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
9810 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
9811 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
9812 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
9813 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
9814 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
9815 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
9816 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0)
9817
9818static void
9819ada_operator_length (struct expression *exp, int pc, int *oplenp, int *argsp)
9820{
9821 switch (exp->elts[pc - 1].opcode)
9822 {
76a01679 9823 default:
4c4b4cd2
PH
9824 operator_length_standard (exp, pc, oplenp, argsp);
9825 break;
9826
9827#define OP_DEFN(op, len, args, binop) \
9828 case op: *oplenp = len; *argsp = args; break;
9829 ADA_OPERATORS;
9830#undef OP_DEFN
9831 }
9832}
9833
9834static char *
9835ada_op_name (enum exp_opcode opcode)
9836{
9837 switch (opcode)
9838 {
76a01679 9839 default:
4c4b4cd2
PH
9840 return op_name_standard (opcode);
9841#define OP_DEFN(op, len, args, binop) case op: return #op;
9842 ADA_OPERATORS;
9843#undef OP_DEFN
9844 }
9845}
9846
9847/* As for operator_length, but assumes PC is pointing at the first
9848 element of the operator, and gives meaningful results only for the
9849 Ada-specific operators. */
9850
9851static void
76a01679
JB
9852ada_forward_operator_length (struct expression *exp, int pc,
9853 int *oplenp, int *argsp)
4c4b4cd2 9854{
76a01679 9855 switch (exp->elts[pc].opcode)
4c4b4cd2
PH
9856 {
9857 default:
9858 *oplenp = *argsp = 0;
9859 break;
9860#define OP_DEFN(op, len, args, binop) \
9861 case op: *oplenp = len; *argsp = args; break;
9862 ADA_OPERATORS;
9863#undef OP_DEFN
9864 }
9865}
9866
9867static int
9868ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
9869{
9870 enum exp_opcode op = exp->elts[elt].opcode;
9871 int oplen, nargs;
9872 int pc = elt;
9873 int i;
76a01679 9874
4c4b4cd2
PH
9875 ada_forward_operator_length (exp, elt, &oplen, &nargs);
9876
76a01679 9877 switch (op)
4c4b4cd2 9878 {
76a01679 9879 /* Ada attributes ('Foo). */
4c4b4cd2
PH
9880 case OP_ATR_FIRST:
9881 case OP_ATR_LAST:
9882 case OP_ATR_LENGTH:
9883 case OP_ATR_IMAGE:
9884 case OP_ATR_MAX:
9885 case OP_ATR_MIN:
9886 case OP_ATR_MODULUS:
9887 case OP_ATR_POS:
9888 case OP_ATR_SIZE:
9889 case OP_ATR_TAG:
9890 case OP_ATR_VAL:
9891 break;
9892
9893 case UNOP_IN_RANGE:
9894 case UNOP_QUAL:
9895 fprintf_filtered (stream, "Type @");
9896 gdb_print_host_address (exp->elts[pc + 1].type, stream);
9897 fprintf_filtered (stream, " (");
9898 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
9899 fprintf_filtered (stream, ")");
9900 break;
9901 case BINOP_IN_BOUNDS:
9902 fprintf_filtered (stream, " (%d)", (int) exp->elts[pc + 2].longconst);
9903 break;
9904 case TERNOP_IN_RANGE:
9905 break;
9906
9907 default:
9908 return dump_subexp_body_standard (exp, stream, elt);
9909 }
9910
9911 elt += oplen;
9912 for (i = 0; i < nargs; i += 1)
9913 elt = dump_subexp (exp, stream, elt);
9914
9915 return elt;
9916}
9917
9918/* The Ada extension of print_subexp (q.v.). */
9919
76a01679
JB
9920static void
9921ada_print_subexp (struct expression *exp, int *pos,
9922 struct ui_file *stream, enum precedence prec)
4c4b4cd2
PH
9923{
9924 int oplen, nargs;
9925 int pc = *pos;
9926 enum exp_opcode op = exp->elts[pc].opcode;
9927
9928 ada_forward_operator_length (exp, pc, &oplen, &nargs);
9929
9930 switch (op)
9931 {
9932 default:
9933 print_subexp_standard (exp, pos, stream, prec);
9934 return;
9935
9936 case OP_VAR_VALUE:
9937 *pos += oplen;
9938 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
9939 return;
9940
9941 case BINOP_IN_BOUNDS:
9942 *pos += oplen;
9943 print_subexp (exp, pos, stream, PREC_SUFFIX);
9944 fputs_filtered (" in ", stream);
9945 print_subexp (exp, pos, stream, PREC_SUFFIX);
9946 fputs_filtered ("'range", stream);
9947 if (exp->elts[pc + 1].longconst > 1)
76a01679
JB
9948 fprintf_filtered (stream, "(%ld)",
9949 (long) exp->elts[pc + 1].longconst);
4c4b4cd2
PH
9950 return;
9951
9952 case TERNOP_IN_RANGE:
9953 *pos += oplen;
9954 if (prec >= PREC_EQUAL)
76a01679 9955 fputs_filtered ("(", stream);
4c4b4cd2
PH
9956 print_subexp (exp, pos, stream, PREC_SUFFIX);
9957 fputs_filtered (" in ", stream);
9958 print_subexp (exp, pos, stream, PREC_EQUAL);
9959 fputs_filtered (" .. ", stream);
9960 print_subexp (exp, pos, stream, PREC_EQUAL);
9961 if (prec >= PREC_EQUAL)
76a01679
JB
9962 fputs_filtered (")", stream);
9963 return;
4c4b4cd2
PH
9964
9965 case OP_ATR_FIRST:
9966 case OP_ATR_LAST:
9967 case OP_ATR_LENGTH:
9968 case OP_ATR_IMAGE:
9969 case OP_ATR_MAX:
9970 case OP_ATR_MIN:
9971 case OP_ATR_MODULUS:
9972 case OP_ATR_POS:
9973 case OP_ATR_SIZE:
9974 case OP_ATR_TAG:
9975 case OP_ATR_VAL:
9976 *pos += oplen;
9977 if (exp->elts[*pos].opcode == OP_TYPE)
76a01679
JB
9978 {
9979 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
9980 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0);
9981 *pos += 3;
9982 }
4c4b4cd2 9983 else
76a01679 9984 print_subexp (exp, pos, stream, PREC_SUFFIX);
4c4b4cd2
PH
9985 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
9986 if (nargs > 1)
76a01679
JB
9987 {
9988 int tem;
9989 for (tem = 1; tem < nargs; tem += 1)
9990 {
9991 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
9992 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
9993 }
9994 fputs_filtered (")", stream);
9995 }
4c4b4cd2 9996 return;
14f9c5c9 9997
4c4b4cd2
PH
9998 case UNOP_QUAL:
9999 *pos += oplen;
10000 type_print (exp->elts[pc + 1].type, "", stream, 0);
10001 fputs_filtered ("'(", stream);
10002 print_subexp (exp, pos, stream, PREC_PREFIX);
10003 fputs_filtered (")", stream);
10004 return;
14f9c5c9 10005
4c4b4cd2
PH
10006 case UNOP_IN_RANGE:
10007 *pos += oplen;
10008 print_subexp (exp, pos, stream, PREC_SUFFIX);
10009 fputs_filtered (" in ", stream);
10010 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0);
10011 return;
10012 }
10013}
14f9c5c9
AS
10014
10015/* Table mapping opcodes into strings for printing operators
10016 and precedences of the operators. */
10017
d2e4a39e
AS
10018static const struct op_print ada_op_print_tab[] = {
10019 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
10020 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
10021 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
10022 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
10023 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
10024 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
10025 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
10026 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
10027 {"<=", BINOP_LEQ, PREC_ORDER, 0},
10028 {">=", BINOP_GEQ, PREC_ORDER, 0},
10029 {">", BINOP_GTR, PREC_ORDER, 0},
10030 {"<", BINOP_LESS, PREC_ORDER, 0},
10031 {">>", BINOP_RSH, PREC_SHIFT, 0},
10032 {"<<", BINOP_LSH, PREC_SHIFT, 0},
10033 {"+", BINOP_ADD, PREC_ADD, 0},
10034 {"-", BINOP_SUB, PREC_ADD, 0},
10035 {"&", BINOP_CONCAT, PREC_ADD, 0},
10036 {"*", BINOP_MUL, PREC_MUL, 0},
10037 {"/", BINOP_DIV, PREC_MUL, 0},
10038 {"rem", BINOP_REM, PREC_MUL, 0},
10039 {"mod", BINOP_MOD, PREC_MUL, 0},
10040 {"**", BINOP_EXP, PREC_REPEAT, 0},
10041 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
10042 {"-", UNOP_NEG, PREC_PREFIX, 0},
10043 {"+", UNOP_PLUS, PREC_PREFIX, 0},
10044 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
10045 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
10046 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
4c4b4cd2
PH
10047 {".all", UNOP_IND, PREC_SUFFIX, 1},
10048 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
10049 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
d2e4a39e 10050 {NULL, 0, 0, 0}
14f9c5c9
AS
10051};
10052\f
4c4b4cd2 10053 /* Assorted Types and Interfaces */
14f9c5c9 10054
d2e4a39e
AS
10055struct type *builtin_type_ada_int;
10056struct type *builtin_type_ada_short;
10057struct type *builtin_type_ada_long;
10058struct type *builtin_type_ada_long_long;
10059struct type *builtin_type_ada_char;
10060struct type *builtin_type_ada_float;
10061struct type *builtin_type_ada_double;
10062struct type *builtin_type_ada_long_double;
10063struct type *builtin_type_ada_natural;
10064struct type *builtin_type_ada_positive;
10065struct type *builtin_type_ada_system_address;
10066
10067struct type **const (ada_builtin_types[]) =
10068{
14f9c5c9 10069 &builtin_type_ada_int,
76a01679
JB
10070 &builtin_type_ada_long,
10071 &builtin_type_ada_short,
10072 &builtin_type_ada_char,
10073 &builtin_type_ada_float,
10074 &builtin_type_ada_double,
10075 &builtin_type_ada_long_long,
10076 &builtin_type_ada_long_double,
10077 &builtin_type_ada_natural, &builtin_type_ada_positive,
10078 /* The following types are carried over from C for convenience. */
10079&builtin_type_int,
10080 &builtin_type_long,
10081 &builtin_type_short,
10082 &builtin_type_char,
10083 &builtin_type_float,
10084 &builtin_type_double,
10085 &builtin_type_long_long,
10086 &builtin_type_void,
10087 &builtin_type_signed_char,
10088 &builtin_type_unsigned_char,
10089 &builtin_type_unsigned_short,
10090 &builtin_type_unsigned_int,
10091 &builtin_type_unsigned_long,
10092 &builtin_type_unsigned_long_long,
10093 &builtin_type_long_double,
10094 &builtin_type_complex, &builtin_type_double_complex, 0};
4c4b4cd2
PH
10095
10096/* Not really used, but needed in the ada_language_defn. */
10097
d2e4a39e
AS
10098static void
10099emit_char (int c, struct ui_file *stream, int quoter)
14f9c5c9
AS
10100{
10101 ada_emit_char (c, stream, quoter, 1);
10102}
10103
4c4b4cd2 10104static int
19c1ef65 10105parse (void)
4c4b4cd2
PH
10106{
10107 warnings_issued = 0;
10108 return ada_parse ();
10109}
10110
76a01679 10111static const struct exp_descriptor ada_exp_descriptor = {
4c4b4cd2
PH
10112 ada_print_subexp,
10113 ada_operator_length,
10114 ada_op_name,
10115 ada_dump_subexp_body,
10116 ada_evaluate_subexp
10117};
10118
14f9c5c9 10119const struct language_defn ada_language_defn = {
4c4b4cd2
PH
10120 "ada", /* Language name */
10121 language_ada,
14f9c5c9
AS
10122 ada_builtin_types,
10123 range_check_off,
10124 type_check_off,
4c4b4cd2
PH
10125 case_sensitive_on, /* Yes, Ada is case-insensitive, but
10126 that's not quite what this means. */
10127#ifdef GNAT_GDB
10128 ada_lookup_symbol,
10129 ada_lookup_minimal_symbol,
76a01679 10130#endif /* GNAT_GDB */
4c4b4cd2
PH
10131 &ada_exp_descriptor,
10132 parse,
14f9c5c9 10133 ada_error,
4c4b4cd2 10134 resolve,
76a01679
JB
10135 ada_printchar, /* Print a character constant */
10136 ada_printstr, /* Function to print string constant */
10137 emit_char, /* Function to print single char (not used) */
10138 ada_create_fundamental_type, /* Create fundamental type in this language */
10139 ada_print_type, /* Print a type using appropriate syntax */
10140 ada_val_print, /* Print a value using appropriate syntax */
10141 ada_value_print, /* Print a top-level value */
10142 NULL, /* Language specific skip_trampoline */
10143 NULL, /* value_of_this */
4c4b4cd2 10144 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
76a01679
JB
10145 basic_lookup_transparent_type, /* lookup_transparent_type */
10146 ada_la_decode, /* Language specific symbol demangler */
31c27f77 10147 NULL, /* Language specific class_name_from_physname */
76a01679 10148 {"", "", "", ""}, /* Binary format info */
14f9c5c9 10149#if 0
4c4b4cd2
PH
10150 {"8#%lo#", "8#", "o", "#"}, /* Octal format info */
10151 {"%ld", "", "d", ""}, /* Decimal format info */
10152 {"16#%lx#", "16#", "x", "#"}, /* Hex format info */
14f9c5c9 10153#else
4c4b4cd2
PH
10154 /* Copied from c-lang.c. */
10155 {"0%lo", "0", "o", ""}, /* Octal format info */
10156 {"%ld", "", "d", ""}, /* Decimal format info */
10157 {"0x%lx", "0x", "x", ""}, /* Hex format info */
14f9c5c9 10158#endif
4c4b4cd2
PH
10159 ada_op_print_tab, /* expression operators for printing */
10160 0, /* c-style arrays */
10161 1, /* String lower bound */
14f9c5c9 10162 &builtin_type_ada_char,
4c4b4cd2
PH
10163 ada_get_gdb_completer_word_break_characters,
10164#ifdef GNAT_GDB
10165 ada_translate_error_message, /* Substitute Ada-specific terminology
76a01679
JB
10166 in errors and warnings. */
10167#endif /* GNAT_GDB */
14f9c5c9
AS
10168 LANG_MAGIC
10169};
10170
4c4b4cd2 10171static void
76a01679
JB
10172build_ada_types (void)
10173{
14f9c5c9
AS
10174 builtin_type_ada_int =
10175 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10176 0, "integer", (struct objfile *) NULL);
14f9c5c9
AS
10177 builtin_type_ada_long =
10178 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10179 0, "long_integer", (struct objfile *) NULL);
14f9c5c9
AS
10180 builtin_type_ada_short =
10181 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10182 0, "short_integer", (struct objfile *) NULL);
14f9c5c9
AS
10183 builtin_type_ada_char =
10184 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10185 0, "character", (struct objfile *) NULL);
14f9c5c9
AS
10186 builtin_type_ada_float =
10187 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10188 0, "float", (struct objfile *) NULL);
14f9c5c9
AS
10189 builtin_type_ada_double =
10190 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10191 0, "long_float", (struct objfile *) NULL);
14f9c5c9
AS
10192 builtin_type_ada_long_long =
10193 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10194 0, "long_long_integer", (struct objfile *) NULL);
14f9c5c9
AS
10195 builtin_type_ada_long_double =
10196 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10197 0, "long_long_float", (struct objfile *) NULL);
14f9c5c9
AS
10198 builtin_type_ada_natural =
10199 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10200 0, "natural", (struct objfile *) NULL);
14f9c5c9
AS
10201 builtin_type_ada_positive =
10202 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4c4b4cd2 10203 0, "positive", (struct objfile *) NULL);
14f9c5c9
AS
10204
10205
d2e4a39e
AS
10206 builtin_type_ada_system_address =
10207 lookup_pointer_type (init_type (TYPE_CODE_VOID, 1, 0, "void",
4c4b4cd2 10208 (struct objfile *) NULL));
14f9c5c9 10209 TYPE_NAME (builtin_type_ada_system_address) = "system__address";
4c4b4cd2
PH
10210}
10211
10212void
10213_initialize_ada_language (void)
10214{
14f9c5c9 10215
4c4b4cd2
PH
10216 build_ada_types ();
10217 deprecated_register_gdbarch_swap (NULL, 0, build_ada_types);
14f9c5c9
AS
10218 add_language (&ada_language_defn);
10219
96d887e8 10220 varsize_limit = 65536;
4c4b4cd2 10221#ifdef GNAT_GDB
d2e4a39e 10222 add_show_from_set
14f9c5c9 10223 (add_set_cmd ("varsize-limit", class_support, var_uinteger,
4c4b4cd2
PH
10224 (char *) &varsize_limit,
10225 "Set maximum bytes in dynamic-sized object.",
10226 &setlist), &showlist);
96d887e8 10227 obstack_init (&cache_space);
76a01679 10228#endif /* GNAT_GDB */
14f9c5c9 10229
4c4b4cd2 10230 obstack_init (&symbol_list_obstack);
14f9c5c9 10231
76a01679
JB
10232 decoded_names_store = htab_create_alloc_ex
10233 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
4c4b4cd2
PH
10234 NULL, NULL, xmcalloc, xmfree);
10235}
14f9c5c9
AS
10236
10237/* Create a fundamental Ada type using default reasonable for the current
10238 target machine.
10239
10240 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
10241 define fundamental types such as "int" or "double". Others (stabs or
10242 DWARF version 2, etc) do define fundamental types. For the formats which
10243 don't provide fundamental types, gdb can create such types using this
10244 function.
10245
10246 FIXME: Some compilers distinguish explicitly signed integral types
10247 (signed short, signed int, signed long) from "regular" integral types
10248 (short, int, long) in the debugging information. There is some dis-
10249 agreement as to how useful this feature is. In particular, gcc does
10250 not support this. Also, only some debugging formats allow the
10251 distinction to be passed on to a debugger. For now, we always just
10252 use "short", "int", or "long" as the type name, for both the implicit
10253 and explicitly signed types. This also makes life easier for the
10254 gdb test suite since we don't have to account for the differences
10255 in output depending upon what the compiler and debugging format
10256 support. We will probably have to re-examine the issue when gdb
10257 starts taking it's fundamental type information directly from the
10258 debugging information supplied by the compiler. fnf@cygnus.com */
10259
10260static struct type *
ebf56fd3 10261ada_create_fundamental_type (struct objfile *objfile, int typeid)
14f9c5c9
AS
10262{
10263 struct type *type = NULL;
10264
10265 switch (typeid)
10266 {
d2e4a39e
AS
10267 default:
10268 /* FIXME: For now, if we are asked to produce a type not in this
10269 language, create the equivalent of a C integer type with the
10270 name "<?type?>". When all the dust settles from the type
4c4b4cd2 10271 reconstruction work, this should probably become an error. */
d2e4a39e 10272 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10273 TARGET_INT_BIT / TARGET_CHAR_BIT,
10274 0, "<?type?>", objfile);
d2e4a39e
AS
10275 warning ("internal error: no Ada fundamental type %d", typeid);
10276 break;
10277 case FT_VOID:
10278 type = init_type (TYPE_CODE_VOID,
4c4b4cd2
PH
10279 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
10280 0, "void", objfile);
d2e4a39e
AS
10281 break;
10282 case FT_CHAR:
10283 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10284 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
10285 0, "character", objfile);
d2e4a39e
AS
10286 break;
10287 case FT_SIGNED_CHAR:
10288 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10289 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
10290 0, "signed char", objfile);
d2e4a39e
AS
10291 break;
10292 case FT_UNSIGNED_CHAR:
10293 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10294 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
10295 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
d2e4a39e
AS
10296 break;
10297 case FT_SHORT:
10298 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10299 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
10300 0, "short_integer", objfile);
d2e4a39e
AS
10301 break;
10302 case FT_SIGNED_SHORT:
10303 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10304 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
10305 0, "short_integer", objfile);
d2e4a39e
AS
10306 break;
10307 case FT_UNSIGNED_SHORT:
10308 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10309 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
10310 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
d2e4a39e
AS
10311 break;
10312 case FT_INTEGER:
10313 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10314 TARGET_INT_BIT / TARGET_CHAR_BIT,
10315 0, "integer", objfile);
d2e4a39e
AS
10316 break;
10317 case FT_SIGNED_INTEGER:
4c4b4cd2 10318 type = init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 0, "integer", objfile); /* FIXME -fnf */
d2e4a39e
AS
10319 break;
10320 case FT_UNSIGNED_INTEGER:
10321 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10322 TARGET_INT_BIT / TARGET_CHAR_BIT,
10323 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
d2e4a39e
AS
10324 break;
10325 case FT_LONG:
10326 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10327 TARGET_LONG_BIT / TARGET_CHAR_BIT,
10328 0, "long_integer", objfile);
d2e4a39e
AS
10329 break;
10330 case FT_SIGNED_LONG:
10331 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10332 TARGET_LONG_BIT / TARGET_CHAR_BIT,
10333 0, "long_integer", objfile);
d2e4a39e
AS
10334 break;
10335 case FT_UNSIGNED_LONG:
10336 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10337 TARGET_LONG_BIT / TARGET_CHAR_BIT,
10338 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
d2e4a39e
AS
10339 break;
10340 case FT_LONG_LONG:
10341 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10342 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
10343 0, "long_long_integer", objfile);
d2e4a39e
AS
10344 break;
10345 case FT_SIGNED_LONG_LONG:
10346 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10347 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
10348 0, "long_long_integer", objfile);
d2e4a39e
AS
10349 break;
10350 case FT_UNSIGNED_LONG_LONG:
10351 type = init_type (TYPE_CODE_INT,
4c4b4cd2
PH
10352 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
10353 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
d2e4a39e
AS
10354 break;
10355 case FT_FLOAT:
10356 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
10357 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
10358 0, "float", objfile);
d2e4a39e
AS
10359 break;
10360 case FT_DBL_PREC_FLOAT:
10361 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
10362 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
10363 0, "long_float", objfile);
d2e4a39e
AS
10364 break;
10365 case FT_EXT_PREC_FLOAT:
10366 type = init_type (TYPE_CODE_FLT,
4c4b4cd2
PH
10367 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
10368 0, "long_long_float", objfile);
d2e4a39e
AS
10369 break;
10370 }
14f9c5c9
AS
10371 return (type);
10372}
10373
d2e4a39e
AS
10374void
10375ada_dump_symtab (struct symtab *s)
14f9c5c9
AS
10376{
10377 int i;
10378 fprintf (stderr, "New symtab: [\n");
d2e4a39e 10379 fprintf (stderr, " Name: %s/%s;\n",
4c4b4cd2 10380 s->dirname ? s->dirname : "?", s->filename ? s->filename : "?");
14f9c5c9
AS
10381 fprintf (stderr, " Format: %s;\n", s->debugformat);
10382 if (s->linetable != NULL)
10383 {
10384 fprintf (stderr, " Line table (section %d):\n", s->block_line_section);
10385 for (i = 0; i < s->linetable->nitems; i += 1)
4c4b4cd2
PH
10386 {
10387 struct linetable_entry *e = s->linetable->item + i;
10388 fprintf (stderr, " %4ld: %8lx\n", (long) e->line, (long) e->pc);
10389 }
14f9c5c9
AS
10390 }
10391 fprintf (stderr, "]\n");
10392}