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