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