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