]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ada-lang.c
* Makefile.in (install-only): Use $(SHELL) when running
[thirdparty/binutils-gdb.git] / gdb / ada-lang.c
CommitLineData
14f9c5c9 1/* Ada language support routines for GDB, the GNU debugger. Copyright
de5ad195
DC
2 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003
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
21#include <stdio.h>
0c30c098 22#include "gdb_string.h"
14f9c5c9
AS
23#include <ctype.h>
24#include <stdarg.h>
25#include "demangle.h"
26#include "defs.h"
27#include "symtab.h"
28#include "gdbtypes.h"
29#include "gdbcmd.h"
30#include "expression.h"
31#include "parser-defs.h"
32#include "language.h"
33#include "c-lang.h"
34#include "inferior.h"
35#include "symfile.h"
36#include "objfiles.h"
37#include "breakpoint.h"
38#include "gdbcore.h"
39#include "ada-lang.h"
14f9c5c9 40#include "ui-out.h"
fe898f56 41#include "block.h"
04714b91 42#include "infcall.h"
14f9c5c9 43
d2e4a39e 44struct cleanup *unresolved_names;
14f9c5c9
AS
45
46void extract_string (CORE_ADDR addr, char *buf);
47
d2e4a39e 48static struct type *ada_create_fundamental_type (struct objfile *, int);
14f9c5c9
AS
49
50static void modify_general_field (char *, LONGEST, int, int);
51
d2e4a39e 52static struct type *desc_base_type (struct type *);
14f9c5c9 53
d2e4a39e 54static struct type *desc_bounds_type (struct type *);
14f9c5c9 55
d2e4a39e 56static struct value *desc_bounds (struct value *);
14f9c5c9 57
d2e4a39e 58static int fat_pntr_bounds_bitpos (struct type *);
14f9c5c9 59
d2e4a39e 60static int fat_pntr_bounds_bitsize (struct type *);
14f9c5c9 61
d2e4a39e 62static struct type *desc_data_type (struct type *);
14f9c5c9 63
d2e4a39e 64static struct value *desc_data (struct value *);
14f9c5c9 65
d2e4a39e 66static int fat_pntr_data_bitpos (struct type *);
14f9c5c9 67
d2e4a39e 68static int fat_pntr_data_bitsize (struct type *);
14f9c5c9 69
d2e4a39e 70static struct value *desc_one_bound (struct value *, int, int);
14f9c5c9 71
d2e4a39e 72static int desc_bound_bitpos (struct type *, int, int);
14f9c5c9 73
d2e4a39e 74static int desc_bound_bitsize (struct type *, int, int);
14f9c5c9 75
d2e4a39e 76static struct type *desc_index_type (struct type *, int);
14f9c5c9 77
d2e4a39e 78static int desc_arity (struct type *);
14f9c5c9 79
d2e4a39e 80static int ada_type_match (struct type *, struct type *, int);
14f9c5c9 81
d2e4a39e 82static int ada_args_match (struct symbol *, struct value **, int);
14f9c5c9 83
d2e4a39e 84static struct value *place_on_stack (struct value *, CORE_ADDR *);
14f9c5c9 85
d2e4a39e
AS
86static struct value *convert_actual (struct value *, struct type *,
87 CORE_ADDR *);
14f9c5c9 88
d2e4a39e
AS
89static struct value *make_array_descriptor (struct type *, struct value *,
90 CORE_ADDR *);
14f9c5c9 91
d2e4a39e
AS
92static void ada_add_block_symbols (struct block *, const char *,
93 namespace_enum, struct objfile *, int);
14f9c5c9 94
d2e4a39e 95static void fill_in_ada_prototype (struct symbol *);
14f9c5c9 96
d2e4a39e 97static int is_nonfunction (struct symbol **, int);
14f9c5c9 98
d2e4a39e 99static void add_defn_to_vec (struct symbol *, struct block *);
14f9c5c9 100
d2e4a39e
AS
101static struct partial_symbol *ada_lookup_partial_symbol (struct partial_symtab
102 *, const char *, int,
103 namespace_enum, int);
14f9c5c9 104
d2e4a39e 105static struct symtab *symtab_for_sym (struct symbol *);
14f9c5c9 106
d2e4a39e
AS
107static struct value *ada_resolve_subexp (struct expression **, int *, int,
108 struct type *);
14f9c5c9 109
d2e4a39e
AS
110static void replace_operator_with_call (struct expression **, int, int, int,
111 struct symbol *, struct block *);
14f9c5c9 112
d2e4a39e 113static int possible_user_operator_p (enum exp_opcode, struct value **);
14f9c5c9 114
d2e4a39e 115static const char *ada_op_name (enum exp_opcode);
14f9c5c9 116
d2e4a39e 117static int numeric_type_p (struct type *);
14f9c5c9 118
d2e4a39e 119static int integer_type_p (struct type *);
14f9c5c9 120
d2e4a39e 121static int scalar_type_p (struct type *);
14f9c5c9 122
d2e4a39e 123static int discrete_type_p (struct type *);
14f9c5c9 124
d2e4a39e
AS
125static char *extended_canonical_line_spec (struct symtab_and_line,
126 const char *);
14f9c5c9 127
d2e4a39e
AS
128static struct value *evaluate_subexp (struct type *, struct expression *,
129 int *, enum noside);
14f9c5c9 130
d2e4a39e 131static struct value *evaluate_subexp_type (struct expression *, int *);
14f9c5c9 132
d2e4a39e 133static struct type *ada_create_fundamental_type (struct objfile *, int);
14f9c5c9 134
d2e4a39e 135static int is_dynamic_field (struct type *, int);
14f9c5c9 136
d2e4a39e
AS
137static struct type *to_fixed_variant_branch_type (struct type *, char *,
138 CORE_ADDR, struct value *);
14f9c5c9 139
d2e4a39e
AS
140static struct type *to_fixed_range_type (char *, struct value *,
141 struct objfile *);
14f9c5c9 142
d2e4a39e 143static struct type *to_static_fixed_type (struct type *);
14f9c5c9 144
d2e4a39e 145static struct value *unwrap_value (struct value *);
14f9c5c9 146
d2e4a39e 147static struct type *packed_array_type (struct type *, long *);
14f9c5c9 148
d2e4a39e 149static struct type *decode_packed_array_type (struct type *);
14f9c5c9 150
d2e4a39e 151static struct value *decode_packed_array (struct value *);
14f9c5c9 152
d2e4a39e
AS
153static struct value *value_subscript_packed (struct value *, int,
154 struct value **);
14f9c5c9 155
d2e4a39e
AS
156static struct value *coerce_unspec_val_to_type (struct value *, long,
157 struct type *);
14f9c5c9 158
d2e4a39e 159static struct value *get_var_value (char *, char *);
14f9c5c9 160
d2e4a39e 161static int lesseq_defined_than (struct symbol *, struct symbol *);
14f9c5c9 162
d2e4a39e 163static int equiv_types (struct type *, struct type *);
14f9c5c9 164
d2e4a39e 165static int is_name_suffix (const char *);
14f9c5c9 166
d2e4a39e 167static int wild_match (const char *, int, const char *);
14f9c5c9 168
d2e4a39e
AS
169static struct symtabs_and_lines find_sal_from_funcs_and_line (const char *,
170 int,
171 struct symbol
172 **, int);
14f9c5c9 173
d2e4a39e
AS
174static int find_line_in_linetable (struct linetable *, int, struct symbol **,
175 int, int *);
14f9c5c9 176
d2e4a39e 177static int find_next_line_in_linetable (struct linetable *, int, int, int);
14f9c5c9 178
d2e4a39e
AS
179static struct symtabs_and_lines all_sals_for_line (const char *, int,
180 char ***);
14f9c5c9 181
d2e4a39e 182static void read_all_symtabs (const char *);
14f9c5c9 183
d2e4a39e 184static int is_plausible_func_for_line (struct symbol *, int);
14f9c5c9 185
d2e4a39e 186static struct value *ada_coerce_ref (struct value *);
14f9c5c9 187
d2e4a39e 188static struct value *value_pos_atr (struct value *);
14f9c5c9 189
d2e4a39e 190static struct value *value_val_atr (struct type *, struct value *);
14f9c5c9 191
d2e4a39e 192static struct symbol *standard_lookup (const char *, namespace_enum);
14f9c5c9
AS
193
194extern void markTimeStart (int index);
195extern void markTimeStop (int index);
14f9c5c9
AS
196\f
197
d2e4a39e 198
14f9c5c9
AS
199/* Maximum-sized dynamic type. */
200static unsigned int varsize_limit;
201
d2e4a39e 202static const char *ada_completer_word_break_characters =
14f9c5c9
AS
203 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
204
205/* The name of the symbol to use to get the name of the main subprogram */
206#define ADA_MAIN_PROGRAM_SYMBOL_NAME "__gnat_ada_main_program_name"
207
208 /* Utilities */
209
210/* extract_string
211 *
212 * read the string located at ADDR from the inferior and store the
213 * result into BUF
214 */
215void
216extract_string (CORE_ADDR addr, char *buf)
217{
d2e4a39e 218 int char_index = 0;
14f9c5c9 219
d2e4a39e
AS
220 /* Loop, reading one byte at a time, until we reach the '\000'
221 end-of-string marker */
222 do
223 {
224 target_read_memory (addr + char_index * sizeof (char),
225 buf + char_index * sizeof (char), sizeof (char));
226 char_index++;
227 }
228 while (buf[char_index - 1] != '\000');
14f9c5c9
AS
229}
230
231/* Assuming *OLD_VECT points to an array of *SIZE objects of size
232 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
233 updating *OLD_VECT and *SIZE as necessary. */
234
235void
d2e4a39e 236grow_vect (void **old_vect, size_t * size, size_t min_size, int element_size)
14f9c5c9 237{
d2e4a39e
AS
238 if (*size < min_size)
239 {
240 *size *= 2;
241 if (*size < min_size)
242 *size = min_size;
243 *old_vect = xrealloc (*old_vect, *size * element_size);
244 }
14f9c5c9
AS
245}
246
247/* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
248 suffix of FIELD_NAME beginning "___" */
249
250static int
ebf56fd3 251field_name_match (const char *field_name, const char *target)
14f9c5c9
AS
252{
253 int len = strlen (target);
d2e4a39e
AS
254 return
255 STREQN (field_name, target, len)
256 && (field_name[len] == '\0'
14f9c5c9 257 || (STREQN (field_name + len, "___", 3)
d2e4a39e 258 && !STREQ (field_name + strlen (field_name) - 6, "___XVN")));
14f9c5c9
AS
259}
260
261
262/* The length of the prefix of NAME prior to any "___" suffix. */
263
264int
d2e4a39e 265ada_name_prefix_len (const char *name)
14f9c5c9
AS
266{
267 if (name == NULL)
268 return 0;
d2e4a39e 269 else
14f9c5c9 270 {
d2e4a39e 271 const char *p = strstr (name, "___");
14f9c5c9
AS
272 if (p == NULL)
273 return strlen (name);
274 else
275 return p - name;
276 }
277}
278
279/* SUFFIX is a suffix of STR. False if STR is null. */
280static int
d2e4a39e 281is_suffix (const char *str, const char *suffix)
14f9c5c9
AS
282{
283 int len1, len2;
284 if (str == NULL)
285 return 0;
286 len1 = strlen (str);
287 len2 = strlen (suffix);
288 return (len1 >= len2 && STREQ (str + len1 - len2, suffix));
289}
290
291/* Create a value of type TYPE whose contents come from VALADDR, if it
292 * is non-null, and whose memory address (in the inferior) is
293 * ADDRESS. */
d2e4a39e
AS
294struct value *
295value_from_contents_and_address (struct type *type, char *valaddr,
296 CORE_ADDR address)
14f9c5c9 297{
d2e4a39e
AS
298 struct value *v = allocate_value (type);
299 if (valaddr == NULL)
14f9c5c9
AS
300 VALUE_LAZY (v) = 1;
301 else
302 memcpy (VALUE_CONTENTS_RAW (v), valaddr, TYPE_LENGTH (type));
303 VALUE_ADDRESS (v) = address;
304 if (address != 0)
305 VALUE_LVAL (v) = lval_memory;
306 return v;
307}
308
309/* The contents of value VAL, beginning at offset OFFSET, treated as a
310 value of type TYPE. The result is an lval in memory if VAL is. */
311
d2e4a39e
AS
312static struct value *
313coerce_unspec_val_to_type (struct value *val, long offset, struct type *type)
14f9c5c9
AS
314{
315 CHECK_TYPEDEF (type);
316 if (VALUE_LVAL (val) == lval_memory)
317 return value_at_lazy (type,
d2e4a39e
AS
318 VALUE_ADDRESS (val) + VALUE_OFFSET (val) + offset,
319 NULL);
320 else
14f9c5c9 321 {
d2e4a39e 322 struct value *result = allocate_value (type);
14f9c5c9 323 VALUE_LVAL (result) = not_lval;
d2e4a39e 324 if (VALUE_ADDRESS (val) == 0)
14f9c5c9 325 memcpy (VALUE_CONTENTS_RAW (result), VALUE_CONTENTS (val) + offset,
d2e4a39e 326 TYPE_LENGTH (type) > TYPE_LENGTH (VALUE_TYPE (val))
14f9c5c9 327 ? TYPE_LENGTH (VALUE_TYPE (val)) : TYPE_LENGTH (type));
d2e4a39e 328 else
14f9c5c9 329 {
d2e4a39e 330 VALUE_ADDRESS (result) =
14f9c5c9
AS
331 VALUE_ADDRESS (val) + VALUE_OFFSET (val) + offset;
332 VALUE_LAZY (result) = 1;
333 }
334 return result;
335 }
336}
337
d2e4a39e
AS
338static char *
339cond_offset_host (char *valaddr, long offset)
14f9c5c9
AS
340{
341 if (valaddr == NULL)
342 return NULL;
343 else
344 return valaddr + offset;
345}
346
347static CORE_ADDR
ebf56fd3 348cond_offset_target (CORE_ADDR address, long offset)
14f9c5c9
AS
349{
350 if (address == 0)
351 return 0;
d2e4a39e 352 else
14f9c5c9
AS
353 return address + offset;
354}
355
356/* Perform execute_command on the result of concatenating all
357 arguments up to NULL. */
358static void
d2e4a39e 359do_command (const char *arg, ...)
14f9c5c9
AS
360{
361 int len;
d2e4a39e
AS
362 char *cmd;
363 const char *s;
14f9c5c9
AS
364 va_list ap;
365
366 va_start (ap, arg);
367 len = 0;
368 s = arg;
369 cmd = "";
d2e4a39e 370 for (; s != NULL; s = va_arg (ap, const char *))
14f9c5c9 371 {
d2e4a39e 372 char *cmd1;
14f9c5c9 373 len += strlen (s);
d2e4a39e 374 cmd1 = alloca (len + 1);
14f9c5c9
AS
375 strcpy (cmd1, cmd);
376 strcat (cmd1, s);
377 cmd = cmd1;
378 }
379 va_end (ap);
380 execute_command (cmd, 0);
381}
14f9c5c9 382\f
d2e4a39e 383
14f9c5c9
AS
384 /* Language Selection */
385
386/* If the main program is in Ada, return language_ada, otherwise return LANG
387 (the main program is in Ada iif the adainit symbol is found).
388
389 MAIN_PST is not used. */
d2e4a39e 390
14f9c5c9 391enum language
d2e4a39e
AS
392ada_update_initial_language (enum language lang,
393 struct partial_symtab *main_pst)
14f9c5c9 394{
d2e4a39e
AS
395 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
396 (struct objfile *) NULL) != NULL)
14f9c5c9
AS
397 /* return language_ada; */
398 /* FIXME: language_ada should be defined in defs.h */
399 return language_unknown;
400
401 return lang;
402}
14f9c5c9 403\f
d2e4a39e 404
14f9c5c9
AS
405 /* Symbols */
406
407/* Table of Ada operators and their GNAT-mangled names. Last entry is pair
408 of NULLs. */
409
d2e4a39e
AS
410const struct ada_opname_map ada_opname_table[] = {
411 {"Oadd", "\"+\"", BINOP_ADD},
412 {"Osubtract", "\"-\"", BINOP_SUB},
413 {"Omultiply", "\"*\"", BINOP_MUL},
414 {"Odivide", "\"/\"", BINOP_DIV},
415 {"Omod", "\"mod\"", BINOP_MOD},
416 {"Orem", "\"rem\"", BINOP_REM},
417 {"Oexpon", "\"**\"", BINOP_EXP},
418 {"Olt", "\"<\"", BINOP_LESS},
419 {"Ole", "\"<=\"", BINOP_LEQ},
420 {"Ogt", "\">\"", BINOP_GTR},
421 {"Oge", "\">=\"", BINOP_GEQ},
422 {"Oeq", "\"=\"", BINOP_EQUAL},
423 {"One", "\"/=\"", BINOP_NOTEQUAL},
424 {"Oand", "\"and\"", BINOP_BITWISE_AND},
425 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
426 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
427 {"Oconcat", "\"&\"", BINOP_CONCAT},
428 {"Oabs", "\"abs\"", UNOP_ABS},
429 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
430 {"Oadd", "\"+\"", UNOP_PLUS},
431 {"Osubtract", "\"-\"", UNOP_NEG},
432 {NULL, NULL}
14f9c5c9
AS
433};
434
435/* True if STR should be suppressed in info listings. */
436static int
d2e4a39e 437is_suppressed_name (const char *str)
14f9c5c9
AS
438{
439 if (STREQN (str, "_ada_", 5))
440 str += 5;
441 if (str[0] == '_' || str[0] == '\000')
442 return 1;
443 else
444 {
d2e4a39e
AS
445 const char *p;
446 const char *suffix = strstr (str, "___");
14f9c5c9
AS
447 if (suffix != NULL && suffix[3] != 'X')
448 return 1;
449 if (suffix == NULL)
450 suffix = str + strlen (str);
d2e4a39e 451 for (p = suffix - 1; p != str; p -= 1)
14f9c5c9
AS
452 if (isupper (*p))
453 {
454 int i;
455 if (p[0] == 'X' && p[-1] != '_')
456 goto OK;
457 if (*p != 'O')
458 return 1;
459 for (i = 0; ada_opname_table[i].mangled != NULL; i += 1)
d2e4a39e 460 if (STREQN (ada_opname_table[i].mangled, p,
14f9c5c9
AS
461 strlen (ada_opname_table[i].mangled)))
462 goto OK;
463 return 1;
d2e4a39e 464 OK:;
14f9c5c9
AS
465 }
466 return 0;
467 }
468}
469
470/* The "mangled" form of DEMANGLED, according to GNAT conventions.
471 * The result is valid until the next call to ada_mangle. */
472char *
d2e4a39e 473ada_mangle (const char *demangled)
14f9c5c9 474{
d2e4a39e 475 static char *mangling_buffer = NULL;
14f9c5c9 476 static size_t mangling_buffer_size = 0;
d2e4a39e 477 const char *p;
14f9c5c9 478 int k;
d2e4a39e 479
14f9c5c9
AS
480 if (demangled == NULL)
481 return NULL;
482
d2e4a39e
AS
483 GROW_VECT (mangling_buffer, mangling_buffer_size,
484 2 * strlen (demangled) + 10);
14f9c5c9
AS
485
486 k = 0;
487 for (p = demangled; *p != '\0'; p += 1)
488 {
d2e4a39e 489 if (*p == '.')
14f9c5c9 490 {
d2e4a39e 491 mangling_buffer[k] = mangling_buffer[k + 1] = '_';
14f9c5c9
AS
492 k += 2;
493 }
494 else if (*p == '"')
495 {
d2e4a39e 496 const struct ada_opname_map *mapping;
14f9c5c9
AS
497
498 for (mapping = ada_opname_table;
d2e4a39e
AS
499 mapping->mangled != NULL &&
500 !STREQN (mapping->demangled, p, strlen (mapping->demangled));
14f9c5c9
AS
501 p += 1)
502 ;
503 if (mapping->mangled == NULL)
504 error ("invalid Ada operator name: %s", p);
d2e4a39e 505 strcpy (mangling_buffer + k, mapping->mangled);
14f9c5c9
AS
506 k += strlen (mapping->mangled);
507 break;
508 }
d2e4a39e 509 else
14f9c5c9
AS
510 {
511 mangling_buffer[k] = *p;
512 k += 1;
513 }
514 }
515
516 mangling_buffer[k] = '\0';
517 return mangling_buffer;
518}
519
520/* Return NAME folded to lower case, or, if surrounded by single
521 * quotes, unfolded, but with the quotes stripped away. Result good
522 * to next call. */
d2e4a39e
AS
523char *
524ada_fold_name (const char *name)
14f9c5c9 525{
d2e4a39e 526 static char *fold_buffer = NULL;
14f9c5c9
AS
527 static size_t fold_buffer_size = 0;
528
529 int len = strlen (name);
d2e4a39e 530 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
14f9c5c9
AS
531
532 if (name[0] == '\'')
533 {
d2e4a39e
AS
534 strncpy (fold_buffer, name + 1, len - 2);
535 fold_buffer[len - 2] = '\000';
14f9c5c9
AS
536 }
537 else
538 {
539 int i;
540 for (i = 0; i <= len; i += 1)
541 fold_buffer[i] = tolower (name[i]);
542 }
543
544 return fold_buffer;
545}
546
547/* Demangle:
548 1. Discard final __{DIGIT}+ or ${DIGIT}+
549 2. Convert other instances of embedded "__" to `.'.
550 3. Discard leading _ada_.
551 4. Convert operator names to the appropriate quoted symbols.
552 5. Remove everything after first ___ if it is followed by
553 'X'.
554 6. Replace TK__ with __, and a trailing B or TKB with nothing.
555 7. Put symbols that should be suppressed in <...> brackets.
556 8. Remove trailing X[bn]* suffix (indicating names in package bodies).
557 The resulting string is valid until the next call of ada_demangle.
558 */
559
560char *
d2e4a39e 561ada_demangle (const char *mangled)
14f9c5c9
AS
562{
563 int i, j;
564 int len0;
d2e4a39e
AS
565 const char *p;
566 char *demangled;
14f9c5c9 567 int at_start_name;
d2e4a39e 568 static char *demangling_buffer = NULL;
14f9c5c9 569 static size_t demangling_buffer_size = 0;
d2e4a39e 570
14f9c5c9
AS
571 if (STREQN (mangled, "_ada_", 5))
572 mangled += 5;
573
574 if (mangled[0] == '_' || mangled[0] == '<')
575 goto Suppress;
576
577 p = strstr (mangled, "___");
578 if (p == NULL)
579 len0 = strlen (mangled);
d2e4a39e 580 else
14f9c5c9
AS
581 {
582 if (p[3] == 'X')
583 len0 = p - mangled;
584 else
585 goto Suppress;
586 }
587 if (len0 > 3 && STREQ (mangled + len0 - 3, "TKB"))
588 len0 -= 3;
589 if (len0 > 1 && STREQ (mangled + len0 - 1, "B"))
590 len0 -= 1;
591
592 /* Make demangled big enough for possible expansion by operator name. */
d2e4a39e 593 GROW_VECT (demangling_buffer, demangling_buffer_size, 2 * len0 + 1);
14f9c5c9
AS
594 demangled = demangling_buffer;
595
d2e4a39e
AS
596 if (isdigit (mangled[len0 - 1]))
597 {
598 for (i = len0 - 2; i >= 0 && isdigit (mangled[i]); i -= 1)
599 ;
600 if (i > 1 && mangled[i] == '_' && mangled[i - 1] == '_')
601 len0 = i - 1;
602 else if (mangled[i] == '$')
603 len0 = i;
604 }
14f9c5c9 605
d2e4a39e 606 for (i = 0, j = 0; i < len0 && !isalpha (mangled[i]); i += 1, j += 1)
14f9c5c9
AS
607 demangled[j] = mangled[i];
608
609 at_start_name = 1;
610 while (i < len0)
611 {
612 if (at_start_name && mangled[i] == 'O')
613 {
614 int k;
615 for (k = 0; ada_opname_table[k].mangled != NULL; k += 1)
616 {
d2e4a39e
AS
617 int op_len = strlen (ada_opname_table[k].mangled);
618 if (STREQN
619 (ada_opname_table[k].mangled + 1, mangled + i + 1,
620 op_len - 1) && !isalnum (mangled[i + op_len]))
14f9c5c9
AS
621 {
622 strcpy (demangled + j, ada_opname_table[k].demangled);
623 at_start_name = 0;
624 i += op_len;
625 j += strlen (ada_opname_table[k].demangled);
626 break;
627 }
628 }
629 if (ada_opname_table[k].mangled != NULL)
630 continue;
631 }
632 at_start_name = 0;
633
d2e4a39e 634 if (i < len0 - 4 && STREQN (mangled + i, "TK__", 4))
14f9c5c9 635 i += 2;
d2e4a39e 636 if (mangled[i] == 'X' && i != 0 && isalnum (mangled[i - 1]))
14f9c5c9
AS
637 {
638 do
639 i += 1;
640 while (i < len0 && (mangled[i] == 'b' || mangled[i] == 'n'));
641 if (i < len0)
642 goto Suppress;
643 }
d2e4a39e 644 else if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
14f9c5c9
AS
645 {
646 demangled[j] = '.';
647 at_start_name = 1;
d2e4a39e
AS
648 i += 2;
649 j += 1;
14f9c5c9
AS
650 }
651 else
652 {
653 demangled[j] = mangled[i];
d2e4a39e
AS
654 i += 1;
655 j += 1;
14f9c5c9
AS
656 }
657 }
658 demangled[j] = '\000';
659
660 for (i = 0; demangled[i] != '\0'; i += 1)
661 if (isupper (demangled[i]) || demangled[i] == ' ')
662 goto Suppress;
663
664 return demangled;
665
666Suppress:
d2e4a39e 667 GROW_VECT (demangling_buffer, demangling_buffer_size, strlen (mangled) + 3);
14f9c5c9
AS
668 demangled = demangling_buffer;
669 if (mangled[0] == '<')
670 strcpy (demangled, mangled);
671 else
672 sprintf (demangled, "<%s>", mangled);
673 return demangled;
674
675}
676
677/* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
678 * suffixes that encode debugging information or leading _ada_ on
679 * SYM_NAME (see is_name_suffix commentary for the debugging
680 * information that is ignored). If WILD, then NAME need only match a
681 * suffix of SYM_NAME minus the same suffixes. Also returns 0 if
682 * either argument is NULL. */
683
684int
d2e4a39e 685ada_match_name (const char *sym_name, const char *name, int wild)
14f9c5c9
AS
686{
687 if (sym_name == NULL || name == NULL)
688 return 0;
689 else if (wild)
690 return wild_match (name, strlen (name), sym_name);
d2e4a39e
AS
691 else
692 {
693 int len_name = strlen (name);
694 return (STREQN (sym_name, name, len_name)
695 && is_name_suffix (sym_name + len_name))
696 || (STREQN (sym_name, "_ada_", 5)
697 && STREQN (sym_name + 5, name, len_name)
698 && is_name_suffix (sym_name + len_name + 5));
699 }
14f9c5c9
AS
700}
701
702/* True (non-zero) iff in Ada mode, the symbol SYM should be
703 suppressed in info listings. */
704
705int
ebf56fd3 706ada_suppress_symbol_printing (struct symbol *sym)
14f9c5c9
AS
707{
708 if (SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE)
709 return 1;
d2e4a39e 710 else
22abf04a 711 return is_suppressed_name (DEPRECATED_SYMBOL_NAME (sym));
14f9c5c9 712}
14f9c5c9 713\f
d2e4a39e 714
14f9c5c9
AS
715 /* Arrays */
716
717/* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of
718 array descriptors. */
719
d2e4a39e
AS
720static char *bound_name[] = {
721 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
14f9c5c9
AS
722 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
723};
724
725/* Maximum number of array dimensions we are prepared to handle. */
726
727#define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char*)))
728
729/* Like modify_field, but allows bitpos > wordlength. */
730
731static void
ebf56fd3 732modify_general_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
14f9c5c9 733{
d2e4a39e
AS
734 modify_field (addr + sizeof (LONGEST) * bitpos / (8 * sizeof (LONGEST)),
735 fieldval, bitpos % (8 * sizeof (LONGEST)), bitsize);
14f9c5c9
AS
736}
737
738
739/* The desc_* routines return primitive portions of array descriptors
740 (fat pointers). */
741
742/* The descriptor or array type, if any, indicated by TYPE; removes
743 level of indirection, if needed. */
d2e4a39e
AS
744static struct type *
745desc_base_type (struct type *type)
14f9c5c9
AS
746{
747 if (type == NULL)
748 return NULL;
749 CHECK_TYPEDEF (type);
750 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_PTR)
751 return check_typedef (TYPE_TARGET_TYPE (type));
752 else
753 return type;
754}
755
756/* True iff TYPE indicates a "thin" array pointer type. */
757static int
d2e4a39e 758is_thin_pntr (struct type *type)
14f9c5c9 759{
d2e4a39e 760 return
14f9c5c9
AS
761 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
762 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
763}
764
765/* The descriptor type for thin pointer type TYPE. */
d2e4a39e
AS
766static struct type *
767thin_descriptor_type (struct type *type)
14f9c5c9 768{
d2e4a39e 769 struct type *base_type = desc_base_type (type);
14f9c5c9
AS
770 if (base_type == NULL)
771 return NULL;
772 if (is_suffix (ada_type_name (base_type), "___XVE"))
773 return base_type;
d2e4a39e 774 else
14f9c5c9 775 {
d2e4a39e 776 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
14f9c5c9
AS
777 if (alt_type == NULL)
778 return base_type;
779 else
780 return alt_type;
781 }
782}
783
784/* A pointer to the array data for thin-pointer value VAL. */
d2e4a39e
AS
785static struct value *
786thin_data_pntr (struct value *val)
14f9c5c9 787{
d2e4a39e 788 struct type *type = VALUE_TYPE (val);
14f9c5c9 789 if (TYPE_CODE (type) == TYPE_CODE_PTR)
d2e4a39e 790 return value_cast (desc_data_type (thin_descriptor_type (type)),
14f9c5c9 791 value_copy (val));
d2e4a39e 792 else
14f9c5c9
AS
793 return value_from_longest (desc_data_type (thin_descriptor_type (type)),
794 VALUE_ADDRESS (val) + VALUE_OFFSET (val));
795}
796
797/* True iff TYPE indicates a "thick" array pointer type. */
798static int
d2e4a39e 799is_thick_pntr (struct type *type)
14f9c5c9
AS
800{
801 type = desc_base_type (type);
802 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
803 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
804}
805
806/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
807 pointer to one, the type of its bounds data; otherwise, NULL. */
d2e4a39e
AS
808static struct type *
809desc_bounds_type (struct type *type)
14f9c5c9 810{
d2e4a39e 811 struct type *r;
14f9c5c9
AS
812
813 type = desc_base_type (type);
814
815 if (type == NULL)
816 return NULL;
817 else if (is_thin_pntr (type))
818 {
819 type = thin_descriptor_type (type);
820 if (type == NULL)
821 return NULL;
822 r = lookup_struct_elt_type (type, "BOUNDS", 1);
823 if (r != NULL)
824 return check_typedef (r);
825 }
826 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
827 {
828 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
829 if (r != NULL)
830 return check_typedef (TYPE_TARGET_TYPE (check_typedef (r)));
831 }
832 return NULL;
833}
834
835/* If ARR is an array descriptor (fat or thin pointer), or pointer to
836 one, a pointer to its bounds data. Otherwise NULL. */
d2e4a39e
AS
837static struct value *
838desc_bounds (struct value *arr)
14f9c5c9 839{
d2e4a39e
AS
840 struct type *type = check_typedef (VALUE_TYPE (arr));
841 if (is_thin_pntr (type))
14f9c5c9 842 {
d2e4a39e
AS
843 struct type *bounds_type =
844 desc_bounds_type (thin_descriptor_type (type));
14f9c5c9
AS
845 LONGEST addr;
846
847 if (desc_bounds_type == NULL)
848 error ("Bad GNAT array descriptor");
849
850 /* NOTE: The following calculation is not really kosher, but
d2e4a39e
AS
851 since desc_type is an XVE-encoded type (and shouldn't be),
852 the correct calculation is a real pain. FIXME (and fix GCC). */
14f9c5c9
AS
853 if (TYPE_CODE (type) == TYPE_CODE_PTR)
854 addr = value_as_long (arr);
d2e4a39e 855 else
14f9c5c9
AS
856 addr = VALUE_ADDRESS (arr) + VALUE_OFFSET (arr);
857
d2e4a39e
AS
858 return
859 value_from_longest (lookup_pointer_type (bounds_type),
860 addr - TYPE_LENGTH (bounds_type));
14f9c5c9
AS
861 }
862
863 else if (is_thick_pntr (type))
d2e4a39e 864 return value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
14f9c5c9
AS
865 "Bad GNAT array descriptor");
866 else
867 return NULL;
868}
869
870/* If TYPE is the type of an array-descriptor (fat pointer), the bit
871 position of the field containing the address of the bounds data. */
872static int
d2e4a39e 873fat_pntr_bounds_bitpos (struct type *type)
14f9c5c9
AS
874{
875 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
876}
877
878/* If TYPE is the type of an array-descriptor (fat pointer), the bit
879 size of the field containing the address of the bounds data. */
880static int
d2e4a39e 881fat_pntr_bounds_bitsize (struct type *type)
14f9c5c9
AS
882{
883 type = desc_base_type (type);
884
d2e4a39e 885 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
14f9c5c9
AS
886 return TYPE_FIELD_BITSIZE (type, 1);
887 else
888 return 8 * TYPE_LENGTH (check_typedef (TYPE_FIELD_TYPE (type, 1)));
889}
890
891/* If TYPE is the type of an array descriptor (fat or thin pointer) or a
892 pointer to one, the type of its array data (a
893 pointer-to-array-with-no-bounds type); otherwise, NULL. Use
894 ada_type_of_array to get an array type with bounds data. */
d2e4a39e
AS
895static struct type *
896desc_data_type (struct type *type)
14f9c5c9
AS
897{
898 type = desc_base_type (type);
899
900 /* NOTE: The following is bogus; see comment in desc_bounds. */
901 if (is_thin_pntr (type))
d2e4a39e
AS
902 return lookup_pointer_type
903 (desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1)));
14f9c5c9
AS
904 else if (is_thick_pntr (type))
905 return lookup_struct_elt_type (type, "P_ARRAY", 1);
906 else
907 return NULL;
908}
909
910/* If ARR is an array descriptor (fat or thin pointer), a pointer to
911 its array data. */
d2e4a39e
AS
912static struct value *
913desc_data (struct value *arr)
14f9c5c9 914{
d2e4a39e 915 struct type *type = VALUE_TYPE (arr);
14f9c5c9
AS
916 if (is_thin_pntr (type))
917 return thin_data_pntr (arr);
918 else if (is_thick_pntr (type))
d2e4a39e 919 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
14f9c5c9
AS
920 "Bad GNAT array descriptor");
921 else
922 return NULL;
923}
924
925
926/* If TYPE is the type of an array-descriptor (fat pointer), the bit
927 position of the field containing the address of the data. */
928static int
d2e4a39e 929fat_pntr_data_bitpos (struct type *type)
14f9c5c9
AS
930{
931 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
932}
933
934/* If TYPE is the type of an array-descriptor (fat pointer), the bit
935 size of the field containing the address of the data. */
936static int
d2e4a39e 937fat_pntr_data_bitsize (struct type *type)
14f9c5c9
AS
938{
939 type = desc_base_type (type);
940
941 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
942 return TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 943 else
14f9c5c9
AS
944 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
945}
946
947/* If BOUNDS is an array-bounds structure (or pointer to one), return
948 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
949 bound, if WHICH is 1. The first bound is I=1. */
d2e4a39e
AS
950static struct value *
951desc_one_bound (struct value *bounds, int i, int which)
14f9c5c9 952{
d2e4a39e 953 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
14f9c5c9
AS
954 "Bad GNAT array descriptor bounds");
955}
956
957/* If BOUNDS is an array-bounds structure type, return the bit position
958 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
959 bound, if WHICH is 1. The first bound is I=1. */
960static int
d2e4a39e 961desc_bound_bitpos (struct type *type, int i, int which)
14f9c5c9 962{
d2e4a39e 963 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
14f9c5c9
AS
964}
965
966/* If BOUNDS is an array-bounds structure type, return the bit field size
967 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
968 bound, if WHICH is 1. The first bound is I=1. */
969static int
d2e4a39e 970desc_bound_bitsize (struct type *type, int i, int which)
14f9c5c9
AS
971{
972 type = desc_base_type (type);
973
d2e4a39e
AS
974 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
975 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
976 else
977 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
14f9c5c9
AS
978}
979
980/* If TYPE is the type of an array-bounds structure, the type of its
d2e4a39e
AS
981 Ith bound (numbering from 1). Otherwise, NULL. */
982static struct type *
983desc_index_type (struct type *type, int i)
14f9c5c9
AS
984{
985 type = desc_base_type (type);
986
987 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
d2e4a39e
AS
988 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
989 else
14f9c5c9
AS
990 return NULL;
991}
992
993/* The number of index positions in the array-bounds type TYPE. 0
994 if TYPE is NULL. */
995static int
d2e4a39e 996desc_arity (struct type *type)
14f9c5c9
AS
997{
998 type = desc_base_type (type);
999
1000 if (type != NULL)
1001 return TYPE_NFIELDS (type) / 2;
1002 return 0;
1003}
1004
1005
1006/* Non-zero iff type is a simple array type (or pointer to one). */
1007int
d2e4a39e 1008ada_is_simple_array (struct type *type)
14f9c5c9
AS
1009{
1010 if (type == NULL)
1011 return 0;
1012 CHECK_TYPEDEF (type);
1013 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
1014 || (TYPE_CODE (type) == TYPE_CODE_PTR
1015 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
1016}
1017
1018/* Non-zero iff type belongs to a GNAT array descriptor. */
1019int
d2e4a39e 1020ada_is_array_descriptor (struct type *type)
14f9c5c9 1021{
d2e4a39e 1022 struct type *data_type = desc_data_type (type);
14f9c5c9
AS
1023
1024 if (type == NULL)
1025 return 0;
1026 CHECK_TYPEDEF (type);
d2e4a39e 1027 return
14f9c5c9
AS
1028 data_type != NULL
1029 && ((TYPE_CODE (data_type) == TYPE_CODE_PTR
1030 && TYPE_TARGET_TYPE (data_type) != NULL
1031 && TYPE_CODE (TYPE_TARGET_TYPE (data_type)) == TYPE_CODE_ARRAY)
d2e4a39e 1032 ||
14f9c5c9
AS
1033 TYPE_CODE (data_type) == TYPE_CODE_ARRAY)
1034 && desc_arity (desc_bounds_type (type)) > 0;
1035}
1036
1037/* Non-zero iff type is a partially mal-formed GNAT array
1038 descriptor. (FIXME: This is to compensate for some problems with
1039 debugging output from GNAT. Re-examine periodically to see if it
1040 is still needed. */
1041int
ebf56fd3 1042ada_is_bogus_array_descriptor (struct type *type)
14f9c5c9 1043{
d2e4a39e 1044 return
14f9c5c9
AS
1045 type != NULL
1046 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1047 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
1048 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
d2e4a39e 1049 && !ada_is_array_descriptor (type);
14f9c5c9
AS
1050}
1051
1052
1053/* If ARR has a record type in the form of a standard GNAT array descriptor,
1054 (fat pointer) returns the type of the array data described---specifically,
1055 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
1056 in from the descriptor; otherwise, they are left unspecified. If
1057 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1058 returns NULL. The result is simply the type of ARR if ARR is not
1059 a descriptor. */
d2e4a39e
AS
1060struct type *
1061ada_type_of_array (struct value *arr, int bounds)
14f9c5c9
AS
1062{
1063 if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1064 return decode_packed_array_type (VALUE_TYPE (arr));
1065
d2e4a39e 1066 if (!ada_is_array_descriptor (VALUE_TYPE (arr)))
14f9c5c9 1067 return VALUE_TYPE (arr);
d2e4a39e
AS
1068
1069 if (!bounds)
1070 return
1071 check_typedef (TYPE_TARGET_TYPE (desc_data_type (VALUE_TYPE (arr))));
14f9c5c9
AS
1072 else
1073 {
d2e4a39e 1074 struct type *elt_type;
14f9c5c9 1075 int arity;
d2e4a39e 1076 struct value *descriptor;
14f9c5c9
AS
1077 struct objfile *objf = TYPE_OBJFILE (VALUE_TYPE (arr));
1078
1079 elt_type = ada_array_element_type (VALUE_TYPE (arr), -1);
1080 arity = ada_array_arity (VALUE_TYPE (arr));
1081
d2e4a39e 1082 if (elt_type == NULL || arity == 0)
14f9c5c9
AS
1083 return check_typedef (VALUE_TYPE (arr));
1084
1085 descriptor = desc_bounds (arr);
d2e4a39e 1086 if (value_as_long (descriptor) == 0)
14f9c5c9 1087 return NULL;
d2e4a39e
AS
1088 while (arity > 0)
1089 {
1090 struct type *range_type = alloc_type (objf);
1091 struct type *array_type = alloc_type (objf);
1092 struct value *low = desc_one_bound (descriptor, arity, 0);
1093 struct value *high = desc_one_bound (descriptor, arity, 1);
1094 arity -= 1;
1095
1096 create_range_type (range_type, VALUE_TYPE (low),
1097 (int) value_as_long (low),
1098 (int) value_as_long (high));
1099 elt_type = create_array_type (array_type, elt_type, range_type);
1100 }
14f9c5c9
AS
1101
1102 return lookup_pointer_type (elt_type);
1103 }
1104}
1105
1106/* If ARR does not represent an array, returns ARR unchanged.
1107 Otherwise, returns either a standard GDB array with bounds set
1108 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1109 GDB array. Returns NULL if ARR is a null fat pointer. */
d2e4a39e
AS
1110struct value *
1111ada_coerce_to_simple_array_ptr (struct value *arr)
14f9c5c9
AS
1112{
1113 if (ada_is_array_descriptor (VALUE_TYPE (arr)))
1114 {
d2e4a39e 1115 struct type *arrType = ada_type_of_array (arr, 1);
14f9c5c9
AS
1116 if (arrType == NULL)
1117 return NULL;
1118 return value_cast (arrType, value_copy (desc_data (arr)));
1119 }
1120 else if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1121 return decode_packed_array (arr);
1122 else
1123 return arr;
1124}
1125
1126/* If ARR does not represent an array, returns ARR unchanged.
1127 Otherwise, returns a standard GDB array describing ARR (which may
1128 be ARR itself if it already is in the proper form). */
d2e4a39e
AS
1129struct value *
1130ada_coerce_to_simple_array (struct value *arr)
14f9c5c9
AS
1131{
1132 if (ada_is_array_descriptor (VALUE_TYPE (arr)))
1133 {
d2e4a39e 1134 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
14f9c5c9
AS
1135 if (arrVal == NULL)
1136 error ("Bounds unavailable for null array pointer.");
1137 return value_ind (arrVal);
1138 }
1139 else if (ada_is_packed_array_type (VALUE_TYPE (arr)))
1140 return decode_packed_array (arr);
d2e4a39e 1141 else
14f9c5c9
AS
1142 return arr;
1143}
1144
1145/* If TYPE represents a GNAT array type, return it translated to an
1146 ordinary GDB array type (possibly with BITSIZE fields indicating
1147 packing). For other types, is the identity. */
d2e4a39e
AS
1148struct type *
1149ada_coerce_to_simple_array_type (struct type *type)
14f9c5c9 1150{
d2e4a39e
AS
1151 struct value *mark = value_mark ();
1152 struct value *dummy = value_from_longest (builtin_type_long, 0);
1153 struct type *result;
14f9c5c9
AS
1154 VALUE_TYPE (dummy) = type;
1155 result = ada_type_of_array (dummy, 0);
1156 value_free_to_mark (dummy);
1157 return result;
1158}
1159
1160/* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1161int
d2e4a39e 1162ada_is_packed_array_type (struct type *type)
14f9c5c9
AS
1163{
1164 if (type == NULL)
1165 return 0;
1166 CHECK_TYPEDEF (type);
d2e4a39e 1167 return
14f9c5c9
AS
1168 ada_type_name (type) != NULL
1169 && strstr (ada_type_name (type), "___XP") != NULL;
1170}
1171
1172/* Given that TYPE is a standard GDB array type with all bounds filled
1173 in, and that the element size of its ultimate scalar constituents
1174 (that is, either its elements, or, if it is an array of arrays, its
1175 elements' elements, etc.) is *ELT_BITS, return an identical type,
1176 but with the bit sizes of its elements (and those of any
1177 constituent arrays) recorded in the BITSIZE components of its
1178 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1179 in bits. */
d2e4a39e
AS
1180static struct type *
1181packed_array_type (struct type *type, long *elt_bits)
14f9c5c9 1182{
d2e4a39e
AS
1183 struct type *new_elt_type;
1184 struct type *new_type;
14f9c5c9
AS
1185 LONGEST low_bound, high_bound;
1186
1187 CHECK_TYPEDEF (type);
1188 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1189 return type;
1190
1191 new_type = alloc_type (TYPE_OBJFILE (type));
1192 new_elt_type = packed_array_type (check_typedef (TYPE_TARGET_TYPE (type)),
1193 elt_bits);
1194 create_array_type (new_type, new_elt_type, TYPE_FIELD_TYPE (type, 0));
1195 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1196 TYPE_NAME (new_type) = ada_type_name (type);
1197
d2e4a39e 1198 if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
14f9c5c9
AS
1199 &low_bound, &high_bound) < 0)
1200 low_bound = high_bound = 0;
1201 if (high_bound < low_bound)
1202 *elt_bits = TYPE_LENGTH (new_type) = 0;
d2e4a39e 1203 else
14f9c5c9
AS
1204 {
1205 *elt_bits *= (high_bound - low_bound + 1);
d2e4a39e 1206 TYPE_LENGTH (new_type) =
14f9c5c9
AS
1207 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
1208 }
1209
1210 /* TYPE_FLAGS (new_type) |= TYPE_FLAG_FIXED_INSTANCE; */
1211 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
1212 return new_type;
1213}
1214
1215/* The array type encoded by TYPE, where ada_is_packed_array_type (TYPE).
1216 */
d2e4a39e
AS
1217static struct type *
1218decode_packed_array_type (struct type *type)
1219{
1220 struct symbol **syms;
1221 struct block **blocks;
1222 const char *raw_name = ada_type_name (check_typedef (type));
1223 char *name = (char *) alloca (strlen (raw_name) + 1);
1224 char *tail = strstr (raw_name, "___XP");
1225 struct type *shadow_type;
14f9c5c9
AS
1226 long bits;
1227 int i, n;
1228
1229 memcpy (name, raw_name, tail - raw_name);
1230 name[tail - raw_name] = '\000';
1231
1232 /* NOTE: Use ada_lookup_symbol_list because of bug in some versions
1233 * of gcc (Solaris, e.g.). FIXME when compiler is fixed. */
d2e4a39e 1234 n = ada_lookup_symbol_list (name, get_selected_block (NULL),
14f9c5c9
AS
1235 VAR_NAMESPACE, &syms, &blocks);
1236 for (i = 0; i < n; i += 1)
1237 if (syms[i] != NULL && SYMBOL_CLASS (syms[i]) == LOC_TYPEDEF
1238 && STREQ (name, ada_type_name (SYMBOL_TYPE (syms[i]))))
1239 break;
1240 if (i >= n)
1241 {
1242 warning ("could not find bounds information on packed array");
1243 return NULL;
1244 }
1245 shadow_type = SYMBOL_TYPE (syms[i]);
1246
1247 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
1248 {
1249 warning ("could not understand bounds information on packed array");
1250 return NULL;
1251 }
d2e4a39e 1252
14f9c5c9
AS
1253 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1254 {
1255 warning ("could not understand bit size information on packed array");
1256 return NULL;
1257 }
d2e4a39e 1258
14f9c5c9
AS
1259 return packed_array_type (shadow_type, &bits);
1260}
1261
1262/* Given that ARR is a struct value* indicating a GNAT packed array,
1263 returns a simple array that denotes that array. Its type is a
1264 standard GDB array type except that the BITSIZEs of the array
1265 target types are set to the number of bits in each element, and the
1266 type length is set appropriately. */
1267
d2e4a39e
AS
1268static struct value *
1269decode_packed_array (struct value *arr)
14f9c5c9 1270{
d2e4a39e 1271 struct type *type = decode_packed_array_type (VALUE_TYPE (arr));
14f9c5c9
AS
1272
1273 if (type == NULL)
1274 {
1275 error ("can't unpack array");
1276 return NULL;
1277 }
1278 else
1279 return coerce_unspec_val_to_type (arr, 0, type);
1280}
1281
1282
1283/* The value of the element of packed array ARR at the ARITY indices
1284 given in IND. ARR must be a simple array. */
1285
d2e4a39e
AS
1286static struct value *
1287value_subscript_packed (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
1288{
1289 int i;
1290 int bits, elt_off, bit_off;
1291 long elt_total_bit_offset;
d2e4a39e
AS
1292 struct type *elt_type;
1293 struct value *v;
14f9c5c9
AS
1294
1295 bits = 0;
1296 elt_total_bit_offset = 0;
1297 elt_type = check_typedef (VALUE_TYPE (arr));
d2e4a39e 1298 for (i = 0; i < arity; i += 1)
14f9c5c9 1299 {
d2e4a39e 1300 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
14f9c5c9 1301 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
d2e4a39e
AS
1302 error
1303 ("attempt to do packed indexing of something other than a packed array");
14f9c5c9
AS
1304 else
1305 {
1306 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
1307 LONGEST lowerbound, upperbound;
1308 LONGEST idx;
1309
d2e4a39e 1310 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
14f9c5c9
AS
1311 {
1312 warning ("don't know bounds of array");
1313 lowerbound = upperbound = 0;
1314 }
d2e4a39e 1315
14f9c5c9
AS
1316 idx = value_as_long (value_pos_atr (ind[i]));
1317 if (idx < lowerbound || idx > upperbound)
1318 warning ("packed array index %ld out of bounds", (long) idx);
1319 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
1320 elt_total_bit_offset += (idx - lowerbound) * bits;
1321 elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
1322 }
1323 }
1324 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
1325 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
d2e4a39e
AS
1326
1327 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
14f9c5c9
AS
1328 bits, elt_type);
1329 if (VALUE_LVAL (arr) == lval_internalvar)
1330 VALUE_LVAL (v) = lval_internalvar_component;
1331 else
1332 VALUE_LVAL (v) = VALUE_LVAL (arr);
1333 return v;
1334}
1335
1336/* Non-zero iff TYPE includes negative integer values. */
1337
1338static int
d2e4a39e 1339has_negatives (struct type *type)
14f9c5c9 1340{
d2e4a39e
AS
1341 switch (TYPE_CODE (type))
1342 {
1343 default:
1344 return 0;
1345 case TYPE_CODE_INT:
1346 return !TYPE_UNSIGNED (type);
1347 case TYPE_CODE_RANGE:
1348 return TYPE_LOW_BOUND (type) < 0;
1349 }
14f9c5c9 1350}
d2e4a39e 1351
14f9c5c9
AS
1352
1353/* Create a new value of type TYPE from the contents of OBJ starting
1354 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
1355 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
1356 assigning through the result will set the field fetched from. OBJ
1357 may also be NULL, in which case, VALADDR+OFFSET must address the
1358 start of storage containing the packed value. The value returned
1359 in this case is never an lval.
1360 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
1361
d2e4a39e
AS
1362struct value *
1363ada_value_primitive_packed_val (struct value *obj, char *valaddr, long offset,
1364 int bit_offset, int bit_size,
1365 struct type *type)
14f9c5c9 1366{
d2e4a39e 1367 struct value *v;
14f9c5c9
AS
1368 int src, /* Index into the source area. */
1369 targ, /* Index into the target area. */
d2e4a39e 1370 i, srcBitsLeft, /* Number of source bits left to move. */
14f9c5c9
AS
1371 nsrc, ntarg, /* Number of source and target bytes. */
1372 unusedLS, /* Number of bits in next significant
1373 * byte of source that are unused. */
1374 accumSize; /* Number of meaningful bits in accum */
d2e4a39e
AS
1375 unsigned char *bytes; /* First byte containing data to unpack. */
1376 unsigned char *unpacked;
14f9c5c9
AS
1377 unsigned long accum; /* Staging area for bits being transferred */
1378 unsigned char sign;
1379 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
1380 /* Transmit bytes from least to most significant; delta is the
d2e4a39e 1381 * direction the indices move. */
14f9c5c9
AS
1382 int delta = BITS_BIG_ENDIAN ? -1 : 1;
1383
1384 CHECK_TYPEDEF (type);
1385
1386 if (obj == NULL)
1387 {
1388 v = allocate_value (type);
d2e4a39e 1389 bytes = (unsigned char *) (valaddr + offset);
14f9c5c9
AS
1390 }
1391 else if (VALUE_LAZY (obj))
1392 {
1393 v = value_at (type,
1394 VALUE_ADDRESS (obj) + VALUE_OFFSET (obj) + offset, NULL);
d2e4a39e 1395 bytes = (unsigned char *) alloca (len);
14f9c5c9
AS
1396 read_memory (VALUE_ADDRESS (v), bytes, len);
1397 }
d2e4a39e 1398 else
14f9c5c9
AS
1399 {
1400 v = allocate_value (type);
d2e4a39e 1401 bytes = (unsigned char *) VALUE_CONTENTS (obj) + offset;
14f9c5c9 1402 }
d2e4a39e
AS
1403
1404 if (obj != NULL)
14f9c5c9
AS
1405 {
1406 VALUE_LVAL (v) = VALUE_LVAL (obj);
1407 if (VALUE_LVAL (obj) == lval_internalvar)
1408 VALUE_LVAL (v) = lval_internalvar_component;
1409 VALUE_ADDRESS (v) = VALUE_ADDRESS (obj) + VALUE_OFFSET (obj) + offset;
1410 VALUE_BITPOS (v) = bit_offset + VALUE_BITPOS (obj);
1411 VALUE_BITSIZE (v) = bit_size;
1412 if (VALUE_BITPOS (v) >= HOST_CHAR_BIT)
d2e4a39e
AS
1413 {
1414 VALUE_ADDRESS (v) += 1;
1415 VALUE_BITPOS (v) -= HOST_CHAR_BIT;
1416 }
14f9c5c9
AS
1417 }
1418 else
1419 VALUE_BITSIZE (v) = bit_size;
d2e4a39e 1420 unpacked = (unsigned char *) VALUE_CONTENTS (v);
14f9c5c9
AS
1421
1422 srcBitsLeft = bit_size;
1423 nsrc = len;
1424 ntarg = TYPE_LENGTH (type);
1425 sign = 0;
1426 if (bit_size == 0)
1427 {
1428 memset (unpacked, 0, TYPE_LENGTH (type));
1429 return v;
1430 }
1431 else if (BITS_BIG_ENDIAN)
1432 {
d2e4a39e
AS
1433 src = len - 1;
1434 if (has_negatives (type) &&
1435 ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
14f9c5c9 1436 sign = ~0;
d2e4a39e
AS
1437
1438 unusedLS =
14f9c5c9
AS
1439 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
1440 % HOST_CHAR_BIT;
1441
1442 switch (TYPE_CODE (type))
d2e4a39e
AS
1443 {
1444 case TYPE_CODE_ARRAY:
1445 case TYPE_CODE_UNION:
1446 case TYPE_CODE_STRUCT:
1447 /* Non-scalar values must be aligned at a byte boundary. */
1448 accumSize =
1449 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
14f9c5c9
AS
1450 /* And are placed at the beginning (most-significant) bytes
1451 * of the target. */
1452 targ = src;
d2e4a39e
AS
1453 break;
1454 default:
14f9c5c9
AS
1455 accumSize = 0;
1456 targ = TYPE_LENGTH (type) - 1;
d2e4a39e
AS
1457 break;
1458 }
14f9c5c9 1459 }
d2e4a39e 1460 else
14f9c5c9
AS
1461 {
1462 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
1463
1464 src = targ = 0;
1465 unusedLS = bit_offset;
1466 accumSize = 0;
1467
d2e4a39e 1468 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
14f9c5c9
AS
1469 sign = ~0;
1470 }
d2e4a39e 1471
14f9c5c9
AS
1472 accum = 0;
1473 while (nsrc > 0)
1474 {
1475 /* Mask for removing bits of the next source byte that are not
1476 * part of the value. */
d2e4a39e
AS
1477 unsigned int unusedMSMask =
1478 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
1479 1;
14f9c5c9
AS
1480 /* Sign-extend bits for this byte. */
1481 unsigned int signMask = sign & ~unusedMSMask;
d2e4a39e 1482 accum |=
14f9c5c9
AS
1483 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
1484 accumSize += HOST_CHAR_BIT - unusedLS;
d2e4a39e 1485 if (accumSize >= HOST_CHAR_BIT)
14f9c5c9
AS
1486 {
1487 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1488 accumSize -= HOST_CHAR_BIT;
1489 accum >>= HOST_CHAR_BIT;
1490 ntarg -= 1;
1491 targ += delta;
1492 }
1493 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
1494 unusedLS = 0;
1495 nsrc -= 1;
1496 src += delta;
1497 }
1498 while (ntarg > 0)
1499 {
1500 accum |= sign << accumSize;
1501 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
1502 accumSize -= HOST_CHAR_BIT;
1503 accum >>= HOST_CHAR_BIT;
1504 ntarg -= 1;
1505 targ += delta;
1506 }
1507
1508 return v;
1509}
d2e4a39e 1510
14f9c5c9
AS
1511/* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
1512 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
1513 not overlap. */
1514static void
d2e4a39e 1515move_bits (char *target, int targ_offset, char *source, int src_offset, int n)
14f9c5c9
AS
1516{
1517 unsigned int accum, mask;
1518 int accum_bits, chunk_size;
1519
1520 target += targ_offset / HOST_CHAR_BIT;
1521 targ_offset %= HOST_CHAR_BIT;
1522 source += src_offset / HOST_CHAR_BIT;
1523 src_offset %= HOST_CHAR_BIT;
d2e4a39e 1524 if (BITS_BIG_ENDIAN)
14f9c5c9
AS
1525 {
1526 accum = (unsigned char) *source;
1527 source += 1;
1528 accum_bits = HOST_CHAR_BIT - src_offset;
1529
d2e4a39e 1530 while (n > 0)
14f9c5c9
AS
1531 {
1532 int unused_right;
1533 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
1534 accum_bits += HOST_CHAR_BIT;
1535 source += 1;
1536 chunk_size = HOST_CHAR_BIT - targ_offset;
1537 if (chunk_size > n)
1538 chunk_size = n;
1539 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
1540 mask = ((1 << chunk_size) - 1) << unused_right;
d2e4a39e
AS
1541 *target =
1542 (*target & ~mask)
14f9c5c9
AS
1543 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
1544 n -= chunk_size;
1545 accum_bits -= chunk_size;
1546 target += 1;
1547 targ_offset = 0;
1548 }
1549 }
1550 else
1551 {
1552 accum = (unsigned char) *source >> src_offset;
1553 source += 1;
1554 accum_bits = HOST_CHAR_BIT - src_offset;
1555
d2e4a39e 1556 while (n > 0)
14f9c5c9
AS
1557 {
1558 accum = accum + ((unsigned char) *source << accum_bits);
1559 accum_bits += HOST_CHAR_BIT;
1560 source += 1;
1561 chunk_size = HOST_CHAR_BIT - targ_offset;
1562 if (chunk_size > n)
1563 chunk_size = n;
1564 mask = ((1 << chunk_size) - 1) << targ_offset;
d2e4a39e 1565 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
14f9c5c9
AS
1566 n -= chunk_size;
1567 accum_bits -= chunk_size;
1568 accum >>= chunk_size;
1569 target += 1;
1570 targ_offset = 0;
1571 }
1572 }
1573}
1574
1575
1576/* Store the contents of FROMVAL into the location of TOVAL.
1577 Return a new value with the location of TOVAL and contents of
1578 FROMVAL. Handles assignment into packed fields that have
1579 floating-point or non-scalar types. */
1580
d2e4a39e
AS
1581static struct value *
1582ada_value_assign (struct value *toval, struct value *fromval)
14f9c5c9 1583{
d2e4a39e 1584 struct type *type = VALUE_TYPE (toval);
14f9c5c9
AS
1585 int bits = VALUE_BITSIZE (toval);
1586
1587 if (!toval->modifiable)
1588 error ("Left operand of assignment is not a modifiable lvalue.");
1589
1590 COERCE_REF (toval);
1591
d2e4a39e 1592 if (VALUE_LVAL (toval) == lval_memory
14f9c5c9 1593 && bits > 0
d2e4a39e 1594 && (TYPE_CODE (type) == TYPE_CODE_FLT
14f9c5c9
AS
1595 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
1596 {
d2e4a39e
AS
1597 int len =
1598 (VALUE_BITPOS (toval) + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
1599 char *buffer = (char *) alloca (len);
1600 struct value *val;
14f9c5c9
AS
1601
1602 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1603 fromval = value_cast (type, fromval);
1604
1605 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), buffer, len);
1606 if (BITS_BIG_ENDIAN)
d2e4a39e
AS
1607 move_bits (buffer, VALUE_BITPOS (toval),
1608 VALUE_CONTENTS (fromval),
1609 TYPE_LENGTH (VALUE_TYPE (fromval)) * TARGET_CHAR_BIT -
1610 bits, bits);
14f9c5c9 1611 else
d2e4a39e 1612 move_bits (buffer, VALUE_BITPOS (toval), VALUE_CONTENTS (fromval),
14f9c5c9 1613 0, bits);
d2e4a39e
AS
1614 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), buffer,
1615 len);
14f9c5c9
AS
1616
1617 val = value_copy (toval);
1618 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
1619 TYPE_LENGTH (type));
1620 VALUE_TYPE (val) = type;
d2e4a39e 1621
14f9c5c9
AS
1622 return val;
1623 }
1624
1625 return value_assign (toval, fromval);
1626}
1627
1628
1629/* The value of the element of array ARR at the ARITY indices given in IND.
1630 ARR may be either a simple array, GNAT array descriptor, or pointer
1631 thereto. */
1632
d2e4a39e
AS
1633struct value *
1634ada_value_subscript (struct value *arr, int arity, struct value **ind)
14f9c5c9
AS
1635{
1636 int k;
d2e4a39e
AS
1637 struct value *elt;
1638 struct type *elt_type;
14f9c5c9
AS
1639
1640 elt = ada_coerce_to_simple_array (arr);
1641
1642 elt_type = check_typedef (VALUE_TYPE (elt));
d2e4a39e 1643 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
14f9c5c9
AS
1644 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
1645 return value_subscript_packed (elt, arity, ind);
1646
1647 for (k = 0; k < arity; k += 1)
1648 {
1649 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
d2e4a39e 1650 error ("too many subscripts (%d expected)", k);
14f9c5c9
AS
1651 elt = value_subscript (elt, value_pos_atr (ind[k]));
1652 }
1653 return elt;
1654}
1655
1656/* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
1657 value of the element of *ARR at the ARITY indices given in
1658 IND. Does not read the entire array into memory. */
1659
d2e4a39e
AS
1660struct value *
1661ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
1662 struct value **ind)
14f9c5c9
AS
1663{
1664 int k;
1665
1666 for (k = 0; k < arity; k += 1)
1667 {
1668 LONGEST lwb, upb;
d2e4a39e 1669 struct value *idx;
14f9c5c9
AS
1670
1671 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
d2e4a39e
AS
1672 error ("too many subscripts (%d expected)", k);
1673 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
14f9c5c9
AS
1674 value_copy (arr));
1675 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
d2e4a39e 1676 if (lwb == 0)
14f9c5c9
AS
1677 idx = ind[k];
1678 else
1679 idx = value_sub (ind[k], value_from_longest (builtin_type_int, lwb));
1680 arr = value_add (arr, idx);
1681 type = TYPE_TARGET_TYPE (type);
1682 }
1683
1684 return value_ind (arr);
1685}
1686
1687/* If type is a record type in the form of a standard GNAT array
1688 descriptor, returns the number of dimensions for type. If arr is a
1689 simple array, returns the number of "array of"s that prefix its
1690 type designation. Otherwise, returns 0. */
1691
1692int
d2e4a39e 1693ada_array_arity (struct type *type)
14f9c5c9
AS
1694{
1695 int arity;
1696
1697 if (type == NULL)
1698 return 0;
1699
1700 type = desc_base_type (type);
1701
1702 arity = 0;
d2e4a39e 1703 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9 1704 return desc_arity (desc_bounds_type (type));
d2e4a39e
AS
1705 else
1706 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
1707 {
1708 arity += 1;
1709 type = check_typedef (TYPE_TARGET_TYPE (type));
1710 }
d2e4a39e 1711
14f9c5c9
AS
1712 return arity;
1713}
1714
1715/* If TYPE is a record type in the form of a standard GNAT array
1716 descriptor or a simple array type, returns the element type for
1717 TYPE after indexing by NINDICES indices, or by all indices if
1718 NINDICES is -1. Otherwise, returns NULL. */
1719
d2e4a39e
AS
1720struct type *
1721ada_array_element_type (struct type *type, int nindices)
14f9c5c9
AS
1722{
1723 type = desc_base_type (type);
1724
d2e4a39e 1725 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
14f9c5c9
AS
1726 {
1727 int k;
d2e4a39e 1728 struct type *p_array_type;
14f9c5c9
AS
1729
1730 p_array_type = desc_data_type (type);
1731
1732 k = ada_array_arity (type);
1733 if (k == 0)
1734 return NULL;
d2e4a39e 1735
14f9c5c9
AS
1736 /* Initially p_array_type = elt_type(*)[]...(k times)...[] */
1737 if (nindices >= 0 && k > nindices)
1738 k = nindices;
1739 p_array_type = TYPE_TARGET_TYPE (p_array_type);
d2e4a39e 1740 while (k > 0 && p_array_type != NULL)
14f9c5c9
AS
1741 {
1742 p_array_type = check_typedef (TYPE_TARGET_TYPE (p_array_type));
1743 k -= 1;
1744 }
1745 return p_array_type;
1746 }
1747 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1748 {
1749 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
1750 {
1751 type = TYPE_TARGET_TYPE (type);
1752 nindices -= 1;
1753 }
1754 return type;
1755 }
1756
1757 return NULL;
1758}
1759
1760/* The type of nth index in arrays of given type (n numbering from 1). Does
1761 not examine memory. */
1762
d2e4a39e
AS
1763struct type *
1764ada_index_type (struct type *type, int n)
14f9c5c9
AS
1765{
1766 type = desc_base_type (type);
1767
1768 if (n > ada_array_arity (type))
1769 return NULL;
1770
1771 if (ada_is_simple_array (type))
1772 {
1773 int i;
1774
1775 for (i = 1; i < n; i += 1)
1776 type = TYPE_TARGET_TYPE (type);
1777
1778 return TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
1779 }
d2e4a39e 1780 else
14f9c5c9
AS
1781 return desc_index_type (desc_bounds_type (type), n);
1782}
1783
1784/* Given that arr is an array type, returns the lower bound of the
1785 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
1786 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
1787 array-descriptor type. If TYPEP is non-null, *TYPEP is set to the
1788 bounds type. It works for other arrays with bounds supplied by
1789 run-time quantities other than discriminants. */
1790
1791LONGEST
d2e4a39e
AS
1792ada_array_bound_from_type (struct type * arr_type, int n, int which,
1793 struct type ** typep)
14f9c5c9 1794{
d2e4a39e
AS
1795 struct type *type;
1796 struct type *index_type_desc;
14f9c5c9
AS
1797
1798 if (ada_is_packed_array_type (arr_type))
1799 arr_type = decode_packed_array_type (arr_type);
1800
d2e4a39e 1801 if (arr_type == NULL || !ada_is_simple_array (arr_type))
14f9c5c9
AS
1802 {
1803 if (typep != NULL)
1804 *typep = builtin_type_int;
d2e4a39e 1805 return (LONGEST) - which;
14f9c5c9
AS
1806 }
1807
1808 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
1809 type = TYPE_TARGET_TYPE (arr_type);
1810 else
1811 type = arr_type;
1812
1813 index_type_desc = ada_find_parallel_type (type, "___XA");
d2e4a39e 1814 if (index_type_desc == NULL)
14f9c5c9 1815 {
d2e4a39e
AS
1816 struct type *range_type;
1817 struct type *index_type;
14f9c5c9 1818
d2e4a39e 1819 while (n > 1)
14f9c5c9
AS
1820 {
1821 type = TYPE_TARGET_TYPE (type);
1822 n -= 1;
1823 }
1824
1825 range_type = TYPE_INDEX_TYPE (type);
1826 index_type = TYPE_TARGET_TYPE (range_type);
1827 if (TYPE_CODE (index_type) == TYPE_CODE_UNDEF)
d2e4a39e 1828 index_type = builtin_type_long;
14f9c5c9
AS
1829 if (typep != NULL)
1830 *typep = index_type;
d2e4a39e
AS
1831 return
1832 (LONGEST) (which == 0
14f9c5c9
AS
1833 ? TYPE_LOW_BOUND (range_type)
1834 : TYPE_HIGH_BOUND (range_type));
1835 }
d2e4a39e 1836 else
14f9c5c9 1837 {
d2e4a39e
AS
1838 struct type *index_type =
1839 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, n - 1),
14f9c5c9
AS
1840 NULL, TYPE_OBJFILE (arr_type));
1841 if (typep != NULL)
1842 *typep = TYPE_TARGET_TYPE (index_type);
d2e4a39e
AS
1843 return
1844 (LONGEST) (which == 0
14f9c5c9
AS
1845 ? TYPE_LOW_BOUND (index_type)
1846 : TYPE_HIGH_BOUND (index_type));
1847 }
1848}
1849
1850/* Given that arr is an array value, returns the lower bound of the
1851 nth index (numbering from 1) if which is 0, and the upper bound if
1852 which is 1. This routine will also work for arrays with bounds
1853 supplied by run-time quantities other than discriminants. */
1854
d2e4a39e 1855struct value *
4dc81987 1856ada_array_bound (struct value *arr, int n, int which)
14f9c5c9 1857{
d2e4a39e 1858 struct type *arr_type = VALUE_TYPE (arr);
14f9c5c9
AS
1859
1860 if (ada_is_packed_array_type (arr_type))
1861 return ada_array_bound (decode_packed_array (arr), n, which);
d2e4a39e 1862 else if (ada_is_simple_array (arr_type))
14f9c5c9 1863 {
d2e4a39e 1864 struct type *type;
14f9c5c9
AS
1865 LONGEST v = ada_array_bound_from_type (arr_type, n, which, &type);
1866 return value_from_longest (type, v);
1867 }
1868 else
1869 return desc_one_bound (desc_bounds (arr), n, which);
1870}
1871
1872/* Given that arr is an array value, returns the length of the
1873 nth index. This routine will also work for arrays with bounds
1874 supplied by run-time quantities other than discriminants. Does not
1875 work for arrays indexed by enumeration types with representation
d2e4a39e 1876 clauses at the moment. */
14f9c5c9 1877
d2e4a39e
AS
1878struct value *
1879ada_array_length (struct value *arr, int n)
14f9c5c9 1880{
d2e4a39e
AS
1881 struct type *arr_type = check_typedef (VALUE_TYPE (arr));
1882 struct type *index_type_desc;
14f9c5c9
AS
1883
1884 if (ada_is_packed_array_type (arr_type))
1885 return ada_array_length (decode_packed_array (arr), n);
1886
1887 if (ada_is_simple_array (arr_type))
1888 {
d2e4a39e 1889 struct type *type;
14f9c5c9
AS
1890 LONGEST v =
1891 ada_array_bound_from_type (arr_type, n, 1, &type) -
1892 ada_array_bound_from_type (arr_type, n, 0, NULL) + 1;
1893 return value_from_longest (type, v);
1894 }
1895 else
d2e4a39e 1896 return
14f9c5c9
AS
1897 value_from_longest (builtin_type_ada_int,
1898 value_as_long (desc_one_bound (desc_bounds (arr),
1899 n, 1))
1900 - value_as_long (desc_one_bound (desc_bounds (arr),
d2e4a39e 1901 n, 0)) + 1);
14f9c5c9 1902}
14f9c5c9 1903\f
d2e4a39e 1904
14f9c5c9
AS
1905 /* Name resolution */
1906
1907/* The "demangled" name for the user-definable Ada operator corresponding
1908 to op. */
1909
d2e4a39e 1910static const char *
ebf56fd3 1911ada_op_name (enum exp_opcode op)
14f9c5c9
AS
1912{
1913 int i;
1914
1915 for (i = 0; ada_opname_table[i].mangled != NULL; i += 1)
1916 {
1917 if (ada_opname_table[i].op == op)
1918 return ada_opname_table[i].demangled;
1919 }
1920 error ("Could not find operator name for opcode");
1921}
1922
1923
1924/* Same as evaluate_type (*EXP), but resolves ambiguous symbol
1925 references (OP_UNRESOLVED_VALUES) and converts operators that are
1926 user-defined into appropriate function calls. If CONTEXT_TYPE is
1927 non-null, it provides a preferred result type [at the moment, only
1928 type void has any effect---causing procedures to be preferred over
1929 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
1930 return type is preferred. The variable unresolved_names contains a list
1931 of character strings referenced by expout that should be freed.
1932 May change (expand) *EXP. */
1933
1934void
d2e4a39e 1935ada_resolve (struct expression **expp, struct type *context_type)
14f9c5c9
AS
1936{
1937 int pc;
1938 pc = 0;
1939 ada_resolve_subexp (expp, &pc, 1, context_type);
1940}
1941
1942/* Resolve the operator of the subexpression beginning at
1943 position *POS of *EXPP. "Resolving" consists of replacing
1944 OP_UNRESOLVED_VALUE with an appropriate OP_VAR_VALUE, replacing
1945 built-in operators with function calls to user-defined operators,
1946 where appropriate, and (when DEPROCEDURE_P is non-zero), converting
1947 function-valued variables into parameterless calls. May expand
1948 EXP. The CONTEXT_TYPE functions as in ada_resolve, above. */
1949
d2e4a39e
AS
1950static struct value *
1951ada_resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
1952 struct type *context_type)
14f9c5c9
AS
1953{
1954 int pc = *pos;
1955 int i;
d2e4a39e 1956 struct expression *exp; /* Convenience: == *expp */
14f9c5c9 1957 enum exp_opcode op = (*expp)->elts[pc].opcode;
d2e4a39e 1958 struct value **argvec; /* Vector of operand types (alloca'ed). */
14f9c5c9
AS
1959 int nargs; /* Number of operands */
1960
1961 argvec = NULL;
1962 nargs = 0;
1963 exp = *expp;
1964
1965 /* Pass one: resolve operands, saving their types and updating *pos. */
1966 switch (op)
1967 {
1968 case OP_VAR_VALUE:
d2e4a39e 1969 /* case OP_UNRESOLVED_VALUE: */
14f9c5c9
AS
1970 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
1971 *pos += 4;
1972 break;
1973
1974 case OP_FUNCALL:
1975 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
1976 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
d2e4a39e
AS
1977 /* if (exp->elts[pc+3].opcode == OP_UNRESOLVED_VALUE)
1978 {
1979 *pos += 7;
1980
1981 argvec = (struct value* *) alloca (sizeof (struct value*) * (nargs + 1));
1982 for (i = 0; i < nargs-1; i += 1)
1983 argvec[i] = ada_resolve_subexp (expp, pos, 1, NULL);
1984 argvec[i] = NULL;
1985 }
1986 else
1987 {
1988 *pos += 3;
1989 ada_resolve_subexp (expp, pos, 0, NULL);
1990 for (i = 1; i < nargs; i += 1)
1991 ada_resolve_subexp (expp, pos, 1, NULL);
1992 }
1993 */
14f9c5c9
AS
1994 exp = *expp;
1995 break;
1996
1997 /* FIXME: UNOP_QUAL should be defined in expression.h */
1998 /* case UNOP_QUAL:
d2e4a39e
AS
1999 nargs = 1;
2000 *pos += 3;
2001 ada_resolve_subexp (expp, pos, 1, exp->elts[pc + 1].type);
2002 exp = *expp;
2003 break;
2004 */
2005 /* FIXME: OP_ATTRIBUTE should be defined in expression.h */
14f9c5c9 2006 /* case OP_ATTRIBUTE:
d2e4a39e
AS
2007 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
2008 *pos += 4;
2009 for (i = 0; i < nargs; i += 1)
2010 ada_resolve_subexp (expp, pos, 1, NULL);
2011 exp = *expp;
2012 break;
2013 */
14f9c5c9
AS
2014 case UNOP_ADDR:
2015 nargs = 1;
2016 *pos += 1;
2017 ada_resolve_subexp (expp, pos, 0, NULL);
2018 exp = *expp;
2019 break;
2020
2021 case BINOP_ASSIGN:
2022 {
d2e4a39e 2023 struct value *arg1;
14f9c5c9
AS
2024 nargs = 2;
2025 *pos += 1;
2026 arg1 = ada_resolve_subexp (expp, pos, 0, NULL);
2027 if (arg1 == NULL)
2028 ada_resolve_subexp (expp, pos, 1, NULL);
2029 else
2030 ada_resolve_subexp (expp, pos, 1, VALUE_TYPE (arg1));
2031 break;
2032 }
2033
2034 default:
d2e4a39e 2035 switch (op)
14f9c5c9
AS
2036 {
2037 default:
2038 error ("Unexpected operator during name resolution");
2039 case UNOP_CAST:
d2e4a39e
AS
2040 /* case UNOP_MBR:
2041 nargs = 1;
2042 *pos += 3;
2043 break;
2044 */
14f9c5c9
AS
2045 case BINOP_ADD:
2046 case BINOP_SUB:
2047 case BINOP_MUL:
2048 case BINOP_DIV:
2049 case BINOP_REM:
2050 case BINOP_MOD:
2051 case BINOP_EXP:
2052 case BINOP_CONCAT:
2053 case BINOP_LOGICAL_AND:
2054 case BINOP_LOGICAL_OR:
2055 case BINOP_BITWISE_AND:
2056 case BINOP_BITWISE_IOR:
2057 case BINOP_BITWISE_XOR:
2058
2059 case BINOP_EQUAL:
2060 case BINOP_NOTEQUAL:
2061 case BINOP_LESS:
2062 case BINOP_GTR:
2063 case BINOP_LEQ:
2064 case BINOP_GEQ:
2065
2066 case BINOP_REPEAT:
2067 case BINOP_SUBSCRIPT:
2068 case BINOP_COMMA:
2069 nargs = 2;
2070 *pos += 1;
2071 break;
2072
2073 case UNOP_NEG:
2074 case UNOP_PLUS:
2075 case UNOP_LOGICAL_NOT:
2076 case UNOP_ABS:
2077 case UNOP_IND:
2078 nargs = 1;
2079 *pos += 1;
2080 break;
2081
2082 case OP_LONG:
2083 case OP_DOUBLE:
2084 case OP_VAR_VALUE:
2085 *pos += 4;
2086 break;
2087
2088 case OP_TYPE:
2089 case OP_BOOL:
2090 case OP_LAST:
2091 case OP_REGISTER:
2092 case OP_INTERNALVAR:
2093 *pos += 3;
2094 break;
2095
2096 case UNOP_MEMVAL:
2097 *pos += 3;
2098 nargs = 1;
2099 break;
2100
2101 case STRUCTOP_STRUCT:
2102 case STRUCTOP_PTR:
2103 nargs = 1;
2104 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2105 break;
2106
2107 case OP_ARRAY:
d2e4a39e 2108 *pos += 4;
14f9c5c9
AS
2109 nargs = longest_to_int (exp->elts[pc + 2].longconst) + 1;
2110 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
2111 /* A null array contains one dummy element to give the type. */
d2e4a39e
AS
2112 /* if (nargs == 0)
2113 nargs = 1;
2114 break; */
14f9c5c9
AS
2115
2116 case TERNOP_SLICE:
2117 /* FIXME: TERNOP_MBR should be defined in expression.h */
d2e4a39e
AS
2118 /* case TERNOP_MBR:
2119 *pos += 1;
2120 nargs = 3;
2121 break;
2122 */
14f9c5c9 2123 /* FIXME: BINOP_MBR should be defined in expression.h */
d2e4a39e
AS
2124 /* case BINOP_MBR:
2125 *pos += 3;
2126 nargs = 2;
2127 break; */
14f9c5c9
AS
2128 }
2129
d2e4a39e
AS
2130 argvec =
2131 (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
14f9c5c9
AS
2132 for (i = 0; i < nargs; i += 1)
2133 argvec[i] = ada_resolve_subexp (expp, pos, 1, NULL);
2134 argvec[i] = NULL;
2135 exp = *expp;
2136 break;
2137 }
2138
2139 /* Pass two: perform any resolution on principal operator. */
2140 switch (op)
2141 {
2142 default:
2143 break;
2144
2145 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
2146 /* case OP_UNRESOLVED_VALUE:
d2e4a39e
AS
2147 {
2148 struct symbol** candidate_syms;
2149 struct block** candidate_blocks;
2150 int n_candidates;
2151
2152 n_candidates = ada_lookup_symbol_list (exp->elts[pc + 2].name,
2153 exp->elts[pc + 1].block,
2154 VAR_NAMESPACE,
2155 &candidate_syms,
2156 &candidate_blocks);
2157
2158 if (n_candidates > 1)
2159 { */
2160 /* Types tend to get re-introduced locally, so if there
2161 are any local symbols that are not types, first filter
2162 out all types. *//*
2163 int j;
2164 for (j = 0; j < n_candidates; j += 1)
2165 switch (SYMBOL_CLASS (candidate_syms[j]))
2166 {
2167 case LOC_REGISTER:
2168 case LOC_ARG:
2169 case LOC_REF_ARG:
2170 case LOC_REGPARM:
2171 case LOC_REGPARM_ADDR:
2172 case LOC_LOCAL:
2173 case LOC_LOCAL_ARG:
2174 case LOC_BASEREG:
2175 case LOC_BASEREG_ARG:
4c2df51b
DJ
2176 case LOC_COMPUTED:
2177 case LOC_COMPUTED_ARG:
d2e4a39e
AS
2178 goto FoundNonType;
2179 default:
2180 break;
2181 }
2182 FoundNonType:
2183 if (j < n_candidates)
2184 {
2185 j = 0;
2186 while (j < n_candidates)
2187 {
2188 if (SYMBOL_CLASS (candidate_syms[j]) == LOC_TYPEDEF)
2189 {
2190 candidate_syms[j] = candidate_syms[n_candidates-1];
2191 candidate_blocks[j] = candidate_blocks[n_candidates-1];
2192 n_candidates -= 1;
2193 }
2194 else
2195 j += 1;
2196 }
2197 }
2198 }
14f9c5c9 2199
d2e4a39e
AS
2200 if (n_candidates == 0)
2201 error ("No definition found for %s",
2202 ada_demangle (exp->elts[pc + 2].name));
2203 else if (n_candidates == 1)
2204 i = 0;
2205 else if (deprocedure_p
2206 && ! is_nonfunction (candidate_syms, n_candidates))
2207 {
2208 i = ada_resolve_function (candidate_syms, candidate_blocks,
2209 n_candidates, NULL, 0,
2210 exp->elts[pc + 2].name, context_type);
2211 if (i < 0)
2212 error ("Could not find a match for %s",
2213 ada_demangle (exp->elts[pc + 2].name));
2214 }
2215 else
2216 {
2217 printf_filtered ("Multiple matches for %s\n",
2218 ada_demangle (exp->elts[pc+2].name));
2219 user_select_syms (candidate_syms, candidate_blocks,
2220 n_candidates, 1);
2221 i = 0;
2222 }
14f9c5c9 2223
d2e4a39e
AS
2224 exp->elts[pc].opcode = exp->elts[pc + 3].opcode = OP_VAR_VALUE;
2225 exp->elts[pc + 1].block = candidate_blocks[i];
2226 exp->elts[pc + 2].symbol = candidate_syms[i];
2227 if (innermost_block == NULL ||
2228 contained_in (candidate_blocks[i], innermost_block))
2229 innermost_block = candidate_blocks[i];
2230 } */
14f9c5c9
AS
2231 /* FALL THROUGH */
2232
2233 case OP_VAR_VALUE:
d2e4a39e
AS
2234 if (deprocedure_p &&
2235 TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol)) ==
2236 TYPE_CODE_FUNC)
14f9c5c9 2237 {
d2e4a39e
AS
2238 replace_operator_with_call (expp, pc, 0, 0,
2239 exp->elts[pc + 2].symbol,
2240 exp->elts[pc + 1].block);
14f9c5c9
AS
2241 exp = *expp;
2242 }
2243 break;
2244
2245 case OP_FUNCALL:
2246 {
2247 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
d2e4a39e
AS
2248 /* if (exp->elts[pc+3].opcode == OP_UNRESOLVED_VALUE)
2249 {
2250 struct symbol** candidate_syms;
2251 struct block** candidate_blocks;
2252 int n_candidates;
2253
2254 n_candidates = ada_lookup_symbol_list (exp->elts[pc + 5].name,
2255 exp->elts[pc + 4].block,
2256 VAR_NAMESPACE,
2257 &candidate_syms,
2258 &candidate_blocks);
2259 if (n_candidates == 1)
2260 i = 0;
2261 else
2262 {
2263 i = ada_resolve_function (candidate_syms, candidate_blocks,
2264 n_candidates, argvec, nargs-1,
2265 exp->elts[pc + 5].name, context_type);
2266 if (i < 0)
2267 error ("Could not find a match for %s",
2268 ada_demangle (exp->elts[pc + 5].name));
2269 }
2270
2271 exp->elts[pc + 3].opcode = exp->elts[pc + 6].opcode = OP_VAR_VALUE;
2272 exp->elts[pc + 4].block = candidate_blocks[i];
2273 exp->elts[pc + 5].symbol = candidate_syms[i];
2274 if (innermost_block == NULL ||
2275 contained_in (candidate_blocks[i], innermost_block))
2276 innermost_block = candidate_blocks[i];
2277 } */
14f9c5c9 2278
14f9c5c9
AS
2279 }
2280 break;
2281 case BINOP_ADD:
2282 case BINOP_SUB:
2283 case BINOP_MUL:
2284 case BINOP_DIV:
2285 case BINOP_REM:
2286 case BINOP_MOD:
2287 case BINOP_CONCAT:
2288 case BINOP_BITWISE_AND:
2289 case BINOP_BITWISE_IOR:
2290 case BINOP_BITWISE_XOR:
2291 case BINOP_EQUAL:
2292 case BINOP_NOTEQUAL:
2293 case BINOP_LESS:
2294 case BINOP_GTR:
2295 case BINOP_LEQ:
2296 case BINOP_GEQ:
2297 case BINOP_EXP:
2298 case UNOP_NEG:
2299 case UNOP_PLUS:
2300 case UNOP_LOGICAL_NOT:
2301 case UNOP_ABS:
2302 if (possible_user_operator_p (op, argvec))
2303 {
d2e4a39e
AS
2304 struct symbol **candidate_syms;
2305 struct block **candidate_blocks;
14f9c5c9
AS
2306 int n_candidates;
2307
d2e4a39e
AS
2308 n_candidates =
2309 ada_lookup_symbol_list (ada_mangle (ada_op_name (op)),
2310 (struct block *) NULL, VAR_NAMESPACE,
2311 &candidate_syms, &candidate_blocks);
2312 i =
2313 ada_resolve_function (candidate_syms, candidate_blocks,
2314 n_candidates, argvec, nargs,
2315 ada_op_name (op), NULL);
14f9c5c9
AS
2316 if (i < 0)
2317 break;
2318
2319 replace_operator_with_call (expp, pc, nargs, 1,
2320 candidate_syms[i], candidate_blocks[i]);
2321 exp = *expp;
2322 }
2323 break;
2324 }
2325
2326 *pos = pc;
2327 return evaluate_subexp_type (exp, pos);
2328}
2329
2330/* Return non-zero if formal type FTYPE matches actual type ATYPE. If
2331 MAY_DEREF is non-zero, the formal may be a pointer and the actual
d2e4a39e 2332 a non-pointer. */
14f9c5c9
AS
2333/* The term "match" here is rather loose. The match is heuristic and
2334 liberal. FIXME: TOO liberal, in fact. */
2335
2336static int
4dc81987 2337ada_type_match (struct type *ftype, struct type *atype, int may_deref)
14f9c5c9
AS
2338{
2339 CHECK_TYPEDEF (ftype);
2340 CHECK_TYPEDEF (atype);
2341
2342 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
2343 ftype = TYPE_TARGET_TYPE (ftype);
2344 if (TYPE_CODE (atype) == TYPE_CODE_REF)
2345 atype = TYPE_TARGET_TYPE (atype);
2346
d2e4a39e 2347 if (TYPE_CODE (ftype) == TYPE_CODE_VOID
14f9c5c9
AS
2348 || TYPE_CODE (atype) == TYPE_CODE_VOID)
2349 return 1;
2350
d2e4a39e 2351 switch (TYPE_CODE (ftype))
14f9c5c9
AS
2352 {
2353 default:
2354 return 1;
2355 case TYPE_CODE_PTR:
2356 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
2357 return ada_type_match (TYPE_TARGET_TYPE (ftype),
2358 TYPE_TARGET_TYPE (atype), 0);
d2e4a39e
AS
2359 else
2360 return (may_deref &&
2361 ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
14f9c5c9
AS
2362 case TYPE_CODE_INT:
2363 case TYPE_CODE_ENUM:
2364 case TYPE_CODE_RANGE:
2365 switch (TYPE_CODE (atype))
2366 {
2367 case TYPE_CODE_INT:
2368 case TYPE_CODE_ENUM:
2369 case TYPE_CODE_RANGE:
2370 return 1;
2371 default:
2372 return 0;
2373 }
2374
2375 case TYPE_CODE_ARRAY:
d2e4a39e 2376 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
14f9c5c9
AS
2377 || ada_is_array_descriptor (atype));
2378
2379 case TYPE_CODE_STRUCT:
2380 if (ada_is_array_descriptor (ftype))
d2e4a39e 2381 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
14f9c5c9
AS
2382 || ada_is_array_descriptor (atype));
2383 else
2384 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
d2e4a39e 2385 && !ada_is_array_descriptor (atype));
14f9c5c9
AS
2386
2387 case TYPE_CODE_UNION:
2388 case TYPE_CODE_FLT:
2389 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
2390 }
2391}
2392
2393/* Return non-zero if the formals of FUNC "sufficiently match" the
2394 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
2395 may also be an enumeral, in which case it is treated as a 0-
2396 argument function. */
2397
2398static int
d2e4a39e 2399ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
14f9c5c9
AS
2400{
2401 int i;
d2e4a39e 2402 struct type *func_type = SYMBOL_TYPE (func);
14f9c5c9 2403
d2e4a39e 2404 if (SYMBOL_CLASS (func) == LOC_CONST &&
14f9c5c9
AS
2405 TYPE_CODE (func_type) == TYPE_CODE_ENUM)
2406 return (n_actuals == 0);
2407 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
2408 return 0;
2409
2410 if (TYPE_NFIELDS (func_type) != n_actuals)
2411 return 0;
2412
2413 for (i = 0; i < n_actuals; i += 1)
2414 {
d2e4a39e
AS
2415 struct type *ftype = check_typedef (TYPE_FIELD_TYPE (func_type, i));
2416 struct type *atype = check_typedef (VALUE_TYPE (actuals[i]));
14f9c5c9 2417
d2e4a39e
AS
2418 if (!ada_type_match (TYPE_FIELD_TYPE (func_type, i),
2419 VALUE_TYPE (actuals[i]), 1))
14f9c5c9
AS
2420 return 0;
2421 }
2422 return 1;
2423}
2424
2425/* False iff function type FUNC_TYPE definitely does not produce a value
2426 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
2427 FUNC_TYPE is not a valid function type with a non-null return type
2428 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
2429
2430static int
d2e4a39e 2431return_match (struct type *func_type, struct type *context_type)
14f9c5c9 2432{
d2e4a39e 2433 struct type *return_type;
14f9c5c9
AS
2434
2435 if (func_type == NULL)
2436 return 1;
2437
2438 /* FIXME: base_type should be declared in gdbtypes.h, implemented in valarith.c */
2439 /* if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
d2e4a39e
AS
2440 return_type = base_type (TYPE_TARGET_TYPE (func_type));
2441 else
2442 return_type = base_type (func_type); */
14f9c5c9
AS
2443 if (return_type == NULL)
2444 return 1;
2445
2446 /* FIXME: base_type should be declared in gdbtypes.h, implemented in valarith.c */
d2e4a39e 2447 /* context_type = base_type (context_type); */
14f9c5c9
AS
2448
2449 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
2450 return context_type == NULL || return_type == context_type;
2451 else if (context_type == NULL)
2452 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
2453 else
2454 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
2455}
2456
2457
2458/* Return the index in SYMS[0..NSYMS-1] of symbol for the
2459 function (if any) that matches the types of the NARGS arguments in
2460 ARGS. If CONTEXT_TYPE is non-null, and there is at least one match
2461 that returns type CONTEXT_TYPE, then eliminate other matches. If
2462 CONTEXT_TYPE is null, prefer a non-void-returning function.
2463 Asks the user if there is more than one match remaining. Returns -1
2464 if there is no such symbol or none is selected. NAME is used
2465 solely for messages. May re-arrange and modify SYMS in
2466 the process; the index returned is for the modified vector. BLOCKS
2467 is modified in parallel to SYMS. */
2468
2469int
d2e4a39e
AS
2470ada_resolve_function (struct symbol *syms[], struct block *blocks[],
2471 int nsyms, struct value **args, int nargs,
2472 const char *name, struct type *context_type)
14f9c5c9
AS
2473{
2474 int k;
2475 int m; /* Number of hits */
d2e4a39e
AS
2476 struct type *fallback;
2477 struct type *return_type;
14f9c5c9
AS
2478
2479 return_type = context_type;
2480 if (context_type == NULL)
2481 fallback = builtin_type_void;
2482 else
2483 fallback = NULL;
2484
d2e4a39e 2485 m = 0;
14f9c5c9
AS
2486 while (1)
2487 {
2488 for (k = 0; k < nsyms; k += 1)
2489 {
d2e4a39e 2490 struct type *type = check_typedef (SYMBOL_TYPE (syms[k]));
14f9c5c9
AS
2491
2492 if (ada_args_match (syms[k], args, nargs)
2493 && return_match (SYMBOL_TYPE (syms[k]), return_type))
2494 {
2495 syms[m] = syms[k];
2496 if (blocks != NULL)
2497 blocks[m] = blocks[k];
2498 m += 1;
2499 }
2500 }
2501 if (m > 0 || return_type == fallback)
2502 break;
2503 else
2504 return_type = fallback;
2505 }
2506
2507 if (m == 0)
2508 return -1;
2509 else if (m > 1)
2510 {
2511 printf_filtered ("Multiple matches for %s\n", name);
2512 user_select_syms (syms, blocks, m, 1);
2513 return 0;
2514 }
2515 return 0;
2516}
2517
2518/* Returns true (non-zero) iff demangled name N0 should appear before N1 */
2519/* in a listing of choices during disambiguation (see sort_choices, below). */
2520/* The idea is that overloadings of a subprogram name from the */
2521/* same package should sort in their source order. We settle for ordering */
2522/* such symbols by their trailing number (__N or $N). */
2523static int
d2e4a39e 2524mangled_ordered_before (char *N0, char *N1)
14f9c5c9
AS
2525{
2526 if (N1 == NULL)
2527 return 0;
2528 else if (N0 == NULL)
2529 return 1;
2530 else
2531 {
2532 int k0, k1;
d2e4a39e 2533 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
14f9c5c9 2534 ;
d2e4a39e 2535 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
14f9c5c9 2536 ;
d2e4a39e
AS
2537 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
2538 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
14f9c5c9
AS
2539 {
2540 int n0, n1;
2541 n0 = k0;
d2e4a39e 2542 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
14f9c5c9
AS
2543 n0 -= 1;
2544 n1 = k1;
d2e4a39e 2545 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
14f9c5c9
AS
2546 n1 -= 1;
2547 if (n0 == n1 && STREQN (N0, N1, n0))
d2e4a39e 2548 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
14f9c5c9
AS
2549 }
2550 return (strcmp (N0, N1) < 0);
2551 }
2552}
d2e4a39e 2553
14f9c5c9
AS
2554/* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by their */
2555/* mangled names, rearranging BLOCKS[0..NSYMS-1] according to the same */
2556/* permutation. */
d2e4a39e
AS
2557static void
2558sort_choices (struct symbol *syms[], struct block *blocks[], int nsyms)
14f9c5c9
AS
2559{
2560 int i, j;
d2e4a39e 2561 for (i = 1; i < nsyms; i += 1)
14f9c5c9 2562 {
d2e4a39e
AS
2563 struct symbol *sym = syms[i];
2564 struct block *block = blocks[i];
14f9c5c9
AS
2565 int j;
2566
d2e4a39e 2567 for (j = i - 1; j >= 0; j -= 1)
14f9c5c9 2568 {
22abf04a
DC
2569 if (mangled_ordered_before (DEPRECATED_SYMBOL_NAME (syms[j]),
2570 DEPRECATED_SYMBOL_NAME (sym)))
14f9c5c9 2571 break;
d2e4a39e
AS
2572 syms[j + 1] = syms[j];
2573 blocks[j + 1] = blocks[j];
14f9c5c9 2574 }
d2e4a39e
AS
2575 syms[j + 1] = sym;
2576 blocks[j + 1] = block;
14f9c5c9
AS
2577 }
2578}
2579
2580/* Given a list of NSYMS symbols in SYMS and corresponding blocks in */
2581/* BLOCKS, select up to MAX_RESULTS>0 by asking the user (if */
2582/* necessary), returning the number selected, and setting the first */
2583/* elements of SYMS and BLOCKS to the selected symbols and */
2584/* corresponding blocks. Error if no symbols selected. BLOCKS may */
2585/* be NULL, in which case it is ignored. */
2586
2587/* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
2588 to be re-integrated one of these days. */
2589
2590int
d2e4a39e 2591user_select_syms (struct symbol *syms[], struct block *blocks[], int nsyms,
ebf56fd3 2592 int max_results)
14f9c5c9
AS
2593{
2594 int i;
d2e4a39e 2595 int *chosen = (int *) alloca (sizeof (int) * nsyms);
14f9c5c9
AS
2596 int n_chosen;
2597 int first_choice = (max_results == 1) ? 1 : 2;
2598
2599 if (max_results < 1)
2600 error ("Request to select 0 symbols!");
2601 if (nsyms <= 1)
2602 return nsyms;
2603
d2e4a39e 2604 printf_unfiltered ("[0] cancel\n");
14f9c5c9 2605 if (max_results > 1)
d2e4a39e 2606 printf_unfiltered ("[1] all\n");
14f9c5c9
AS
2607
2608 sort_choices (syms, blocks, nsyms);
2609
2610 for (i = 0; i < nsyms; i += 1)
2611 {
2612 if (syms[i] == NULL)
2613 continue;
2614
2615 if (SYMBOL_CLASS (syms[i]) == LOC_BLOCK)
2616 {
2617 struct symtab_and_line sal = find_function_start_sal (syms[i], 1);
2618 printf_unfiltered ("[%d] %s at %s:%d\n",
d2e4a39e 2619 i + first_choice,
de5ad195 2620 SYMBOL_PRINT_NAME (syms[i]),
d2e4a39e
AS
2621 sal.symtab == NULL
2622 ? "<no source file available>"
2623 : sal.symtab->filename, sal.line);
14f9c5c9
AS
2624 continue;
2625 }
d2e4a39e 2626 else
14f9c5c9 2627 {
d2e4a39e 2628 int is_enumeral =
14f9c5c9
AS
2629 (SYMBOL_CLASS (syms[i]) == LOC_CONST
2630 && SYMBOL_TYPE (syms[i]) != NULL
d2e4a39e
AS
2631 && TYPE_CODE (SYMBOL_TYPE (syms[i])) == TYPE_CODE_ENUM);
2632 struct symtab *symtab = symtab_for_sym (syms[i]);
14f9c5c9
AS
2633
2634 if (SYMBOL_LINE (syms[i]) != 0 && symtab != NULL)
2635 printf_unfiltered ("[%d] %s at %s:%d\n",
2636 i + first_choice,
de5ad195 2637 SYMBOL_PRINT_NAME (syms[i]),
14f9c5c9 2638 symtab->filename, SYMBOL_LINE (syms[i]));
d2e4a39e 2639 else if (is_enumeral && TYPE_NAME (SYMBOL_TYPE (syms[i])) != NULL)
14f9c5c9
AS
2640 {
2641 printf_unfiltered ("[%d] ", i + first_choice);
2642 ada_print_type (SYMBOL_TYPE (syms[i]), NULL, gdb_stdout, -1, 0);
2643 printf_unfiltered ("'(%s) (enumeral)\n",
de5ad195 2644 SYMBOL_PRINT_NAME (syms[i]));
14f9c5c9
AS
2645 }
2646 else if (symtab != NULL)
d2e4a39e 2647 printf_unfiltered (is_enumeral
14f9c5c9
AS
2648 ? "[%d] %s in %s (enumeral)\n"
2649 : "[%d] %s at %s:?\n",
2650 i + first_choice,
de5ad195 2651 SYMBOL_PRINT_NAME (syms[i]),
14f9c5c9
AS
2652 symtab->filename);
2653 else
2654 printf_unfiltered (is_enumeral
2655 ? "[%d] %s (enumeral)\n"
2656 : "[%d] %s at ?\n",
d2e4a39e 2657 i + first_choice,
de5ad195 2658 SYMBOL_PRINT_NAME (syms[i]));
14f9c5c9
AS
2659 }
2660 }
d2e4a39e 2661
14f9c5c9
AS
2662 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
2663 "overload-choice");
2664
2665 for (i = 0; i < n_chosen; i += 1)
2666 {
2667 syms[i] = syms[chosen[i]];
d2e4a39e 2668 if (blocks != NULL)
14f9c5c9
AS
2669 blocks[i] = blocks[chosen[i]];
2670 }
2671
2672 return n_chosen;
2673}
2674
2675/* Read and validate a set of numeric choices from the user in the
2676 range 0 .. N_CHOICES-1. Place the results in increasing
2677 order in CHOICES[0 .. N-1], and return N.
2678
2679 The user types choices as a sequence of numbers on one line
2680 separated by blanks, encoding them as follows:
2681
2682 + A choice of 0 means to cancel the selection, throwing an error.
2683 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
2684 + The user chooses k by typing k+IS_ALL_CHOICE+1.
2685
2686 The user is not allowed to choose more than MAX_RESULTS values.
2687
2688 ANNOTATION_SUFFIX, if present, is used to annotate the input
2689 prompts (for use with the -f switch). */
2690
2691int
d2e4a39e
AS
2692get_selections (int *choices, int n_choices, int max_results,
2693 int is_all_choice, char *annotation_suffix)
14f9c5c9
AS
2694{
2695 int i;
d2e4a39e
AS
2696 char *args;
2697 const char *prompt;
14f9c5c9
AS
2698 int n_chosen;
2699 int first_choice = is_all_choice ? 2 : 1;
d2e4a39e 2700
14f9c5c9
AS
2701 prompt = getenv ("PS2");
2702 if (prompt == NULL)
2703 prompt = ">";
2704
2705 printf_unfiltered ("%s ", prompt);
2706 gdb_flush (gdb_stdout);
2707
2708 args = command_line_input ((char *) NULL, 0, annotation_suffix);
d2e4a39e 2709
14f9c5c9
AS
2710 if (args == NULL)
2711 error_no_arg ("one or more choice numbers");
2712
2713 n_chosen = 0;
2714
2715 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
2716 order, as given in args. Choices are validated. */
2717 while (1)
2718 {
d2e4a39e 2719 char *args2;
14f9c5c9
AS
2720 int choice, j;
2721
2722 while (isspace (*args))
2723 args += 1;
2724 if (*args == '\0' && n_chosen == 0)
2725 error_no_arg ("one or more choice numbers");
2726 else if (*args == '\0')
2727 break;
2728
2729 choice = strtol (args, &args2, 10);
d2e4a39e
AS
2730 if (args == args2 || choice < 0
2731 || choice > n_choices + first_choice - 1)
14f9c5c9
AS
2732 error ("Argument must be choice number");
2733 args = args2;
2734
d2e4a39e 2735 if (choice == 0)
14f9c5c9
AS
2736 error ("cancelled");
2737
2738 if (choice < first_choice)
2739 {
2740 n_chosen = n_choices;
2741 for (j = 0; j < n_choices; j += 1)
2742 choices[j] = j;
2743 break;
2744 }
2745 choice -= first_choice;
2746
d2e4a39e
AS
2747 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
2748 {
2749 }
14f9c5c9
AS
2750
2751 if (j < 0 || choice != choices[j])
2752 {
2753 int k;
d2e4a39e
AS
2754 for (k = n_chosen - 1; k > j; k -= 1)
2755 choices[k + 1] = choices[k];
2756 choices[j + 1] = choice;
14f9c5c9
AS
2757 n_chosen += 1;
2758 }
2759 }
2760
2761 if (n_chosen > max_results)
2762 error ("Select no more than %d of the above", max_results);
d2e4a39e 2763
14f9c5c9
AS
2764 return n_chosen;
2765}
2766
2767/* Replace the operator of length OPLEN at position PC in *EXPP with a call */
2768/* on the function identified by SYM and BLOCK, and taking NARGS */
2769/* arguments. Update *EXPP as needed to hold more space. */
2770
2771static void
d2e4a39e
AS
2772replace_operator_with_call (struct expression **expp, int pc, int nargs,
2773 int oplen, struct symbol *sym,
2774 struct block *block)
14f9c5c9
AS
2775{
2776 /* A new expression, with 6 more elements (3 for funcall, 4 for function
2777 symbol, -oplen for operator being replaced). */
d2e4a39e 2778 struct expression *newexp = (struct expression *)
14f9c5c9
AS
2779 xmalloc (sizeof (struct expression)
2780 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
d2e4a39e 2781 struct expression *exp = *expp;
14f9c5c9
AS
2782
2783 newexp->nelts = exp->nelts + 7 - oplen;
2784 newexp->language_defn = exp->language_defn;
2785 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
d2e4a39e 2786 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
14f9c5c9
AS
2787 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
2788
2789 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
2790 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
2791
2792 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
2793 newexp->elts[pc + 4].block = block;
2794 newexp->elts[pc + 5].symbol = sym;
2795
2796 *expp = newexp;
aacb1f0a 2797 xfree (exp);
d2e4a39e 2798}
14f9c5c9
AS
2799
2800/* Type-class predicates */
2801
2802/* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type), or */
2803/* FLOAT.) */
2804
2805static int
d2e4a39e 2806numeric_type_p (struct type *type)
14f9c5c9
AS
2807{
2808 if (type == NULL)
2809 return 0;
d2e4a39e
AS
2810 else
2811 {
2812 switch (TYPE_CODE (type))
2813 {
2814 case TYPE_CODE_INT:
2815 case TYPE_CODE_FLT:
2816 return 1;
2817 case TYPE_CODE_RANGE:
2818 return (type == TYPE_TARGET_TYPE (type)
2819 || numeric_type_p (TYPE_TARGET_TYPE (type)));
2820 default:
2821 return 0;
2822 }
2823 }
14f9c5c9
AS
2824}
2825
2826/* True iff TYPE is integral (an INT or RANGE of INTs). */
2827
2828static int
d2e4a39e 2829integer_type_p (struct type *type)
14f9c5c9
AS
2830{
2831 if (type == NULL)
2832 return 0;
d2e4a39e
AS
2833 else
2834 {
2835 switch (TYPE_CODE (type))
2836 {
2837 case TYPE_CODE_INT:
2838 return 1;
2839 case TYPE_CODE_RANGE:
2840 return (type == TYPE_TARGET_TYPE (type)
2841 || integer_type_p (TYPE_TARGET_TYPE (type)));
2842 default:
2843 return 0;
2844 }
2845 }
14f9c5c9
AS
2846}
2847
2848/* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
2849
2850static int
d2e4a39e 2851scalar_type_p (struct type *type)
14f9c5c9
AS
2852{
2853 if (type == NULL)
2854 return 0;
d2e4a39e
AS
2855 else
2856 {
2857 switch (TYPE_CODE (type))
2858 {
2859 case TYPE_CODE_INT:
2860 case TYPE_CODE_RANGE:
2861 case TYPE_CODE_ENUM:
2862 case TYPE_CODE_FLT:
2863 return 1;
2864 default:
2865 return 0;
2866 }
2867 }
14f9c5c9
AS
2868}
2869
2870/* True iff TYPE is discrete (INT, RANGE, ENUM). */
2871
2872static int
d2e4a39e 2873discrete_type_p (struct type *type)
14f9c5c9
AS
2874{
2875 if (type == NULL)
2876 return 0;
d2e4a39e
AS
2877 else
2878 {
2879 switch (TYPE_CODE (type))
2880 {
2881 case TYPE_CODE_INT:
2882 case TYPE_CODE_RANGE:
2883 case TYPE_CODE_ENUM:
2884 return 1;
2885 default:
2886 return 0;
2887 }
2888 }
14f9c5c9
AS
2889}
2890
2891/* Returns non-zero if OP with operatands in the vector ARGS could be
2892 a user-defined function. Errs on the side of pre-defined operators
2893 (i.e., result 0). */
2894
2895static int
d2e4a39e 2896possible_user_operator_p (enum exp_opcode op, struct value *args[])
14f9c5c9 2897{
d2e4a39e
AS
2898 struct type *type0 = check_typedef (VALUE_TYPE (args[0]));
2899 struct type *type1 =
14f9c5c9 2900 (args[1] == NULL) ? NULL : check_typedef (VALUE_TYPE (args[1]));
d2e4a39e 2901
14f9c5c9
AS
2902 switch (op)
2903 {
2904 default:
2905 return 0;
2906
2907 case BINOP_ADD:
2908 case BINOP_SUB:
2909 case BINOP_MUL:
2910 case BINOP_DIV:
d2e4a39e 2911 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
14f9c5c9
AS
2912
2913 case BINOP_REM:
2914 case BINOP_MOD:
2915 case BINOP_BITWISE_AND:
2916 case BINOP_BITWISE_IOR:
2917 case BINOP_BITWISE_XOR:
d2e4a39e 2918 return (!(integer_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
2919
2920 case BINOP_EQUAL:
2921 case BINOP_NOTEQUAL:
2922 case BINOP_LESS:
2923 case BINOP_GTR:
2924 case BINOP_LEQ:
2925 case BINOP_GEQ:
d2e4a39e 2926 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
14f9c5c9
AS
2927
2928 case BINOP_CONCAT:
d2e4a39e
AS
2929 return ((TYPE_CODE (type0) != TYPE_CODE_ARRAY &&
2930 (TYPE_CODE (type0) != TYPE_CODE_PTR ||
2931 TYPE_CODE (TYPE_TARGET_TYPE (type0))
2932 != TYPE_CODE_ARRAY))
2933 || (TYPE_CODE (type1) != TYPE_CODE_ARRAY &&
2934 (TYPE_CODE (type1) != TYPE_CODE_PTR ||
2935 TYPE_CODE (TYPE_TARGET_TYPE (type1)) != TYPE_CODE_ARRAY)));
14f9c5c9
AS
2936
2937 case BINOP_EXP:
d2e4a39e 2938 return (!(numeric_type_p (type0) && integer_type_p (type1)));
14f9c5c9
AS
2939
2940 case UNOP_NEG:
2941 case UNOP_PLUS:
2942 case UNOP_LOGICAL_NOT:
d2e4a39e
AS
2943 case UNOP_ABS:
2944 return (!numeric_type_p (type0));
14f9c5c9
AS
2945
2946 }
2947}
2948\f
2949 /* Renaming */
2950
2951/** NOTE: In the following, we assume that a renaming type's name may
2952 * have an ___XD suffix. It would be nice if this went away at some
2953 * point. */
2954
2955/* If TYPE encodes a renaming, returns the renaming suffix, which
2956 * is XR for an object renaming, XRP for a procedure renaming, XRE for
2957 * an exception renaming, and XRS for a subprogram renaming. Returns
2958 * NULL if NAME encodes none of these. */
d2e4a39e
AS
2959const char *
2960ada_renaming_type (struct type *type)
14f9c5c9
AS
2961{
2962 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_ENUM)
2963 {
d2e4a39e
AS
2964 const char *name = type_name_no_tag (type);
2965 const char *suffix = (name == NULL) ? NULL : strstr (name, "___XR");
2966 if (suffix == NULL
2967 || (suffix[5] != '\000' && strchr ("PES_", suffix[5]) == NULL))
14f9c5c9
AS
2968 return NULL;
2969 else
2970 return suffix + 3;
2971 }
2972 else
2973 return NULL;
2974}
2975
2976/* Return non-zero iff SYM encodes an object renaming. */
2977int
d2e4a39e 2978ada_is_object_renaming (struct symbol *sym)
14f9c5c9 2979{
d2e4a39e
AS
2980 const char *renaming_type = ada_renaming_type (SYMBOL_TYPE (sym));
2981 return renaming_type != NULL
14f9c5c9
AS
2982 && (renaming_type[2] == '\0' || renaming_type[2] == '_');
2983}
2984
2985/* Assuming that SYM encodes a non-object renaming, returns the original
2986 * name of the renamed entity. The name is good until the end of
2987 * parsing. */
d2e4a39e
AS
2988const char *
2989ada_simple_renamed_entity (struct symbol *sym)
14f9c5c9 2990{
d2e4a39e
AS
2991 struct type *type;
2992 const char *raw_name;
14f9c5c9 2993 int len;
d2e4a39e 2994 char *result;
14f9c5c9
AS
2995
2996 type = SYMBOL_TYPE (sym);
2997 if (type == NULL || TYPE_NFIELDS (type) < 1)
2998 error ("Improperly encoded renaming.");
2999
3000 raw_name = TYPE_FIELD_NAME (type, 0);
3001 len = (raw_name == NULL ? 0 : strlen (raw_name)) - 5;
3002 if (len <= 0)
3003 error ("Improperly encoded renaming.");
3004
3005 result = xmalloc (len + 1);
d2e4a39e
AS
3006 /* FIXME: add_name_string_cleanup should be defined in parse.c */
3007 /* add_name_string_cleanup (result); */
14f9c5c9
AS
3008 strncpy (result, raw_name, len);
3009 result[len] = '\000';
3010 return result;
3011}
14f9c5c9 3012\f
d2e4a39e 3013
14f9c5c9
AS
3014 /* Evaluation: Function Calls */
3015
3016/* Copy VAL onto the stack, using and updating *SP as the stack
3017 pointer. Return VAL as an lvalue. */
3018
d2e4a39e
AS
3019static struct value *
3020place_on_stack (struct value *val, CORE_ADDR *sp)
14f9c5c9
AS
3021{
3022 CORE_ADDR old_sp = *sp;
3023
3024#ifdef STACK_ALIGN
d2e4a39e
AS
3025 *sp = push_bytes (*sp, VALUE_CONTENTS_RAW (val),
3026 STACK_ALIGN (TYPE_LENGTH
3027 (check_typedef (VALUE_TYPE (val)))));
14f9c5c9 3028#else
d2e4a39e 3029 *sp = push_bytes (*sp, VALUE_CONTENTS_RAW (val),
14f9c5c9
AS
3030 TYPE_LENGTH (check_typedef (VALUE_TYPE (val))));
3031#endif
3032
3033 VALUE_LVAL (val) = lval_memory;
3034 if (INNER_THAN (1, 2))
3035 VALUE_ADDRESS (val) = *sp;
3036 else
3037 VALUE_ADDRESS (val) = old_sp;
3038
3039 return val;
3040}
3041
3042/* Return the value ACTUAL, converted to be an appropriate value for a
3043 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3044 allocating any necessary descriptors (fat pointers), or copies of
d2e4a39e 3045 values not residing in memory, updating it as needed. */
14f9c5c9 3046
d2e4a39e
AS
3047static struct value *
3048convert_actual (struct value *actual, struct type *formal_type0,
3049 CORE_ADDR *sp)
14f9c5c9 3050{
d2e4a39e
AS
3051 struct type *actual_type = check_typedef (VALUE_TYPE (actual));
3052 struct type *formal_type = check_typedef (formal_type0);
3053 struct type *formal_target =
3054 TYPE_CODE (formal_type) == TYPE_CODE_PTR
3055 ? check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
3056 struct type *actual_target =
3057 TYPE_CODE (actual_type) == TYPE_CODE_PTR
3058 ? check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
14f9c5c9
AS
3059
3060 if (ada_is_array_descriptor (formal_target)
3061 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
3062 return make_array_descriptor (formal_type, actual, sp);
3063 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR)
3064 {
3065 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
d2e4a39e 3066 && ada_is_array_descriptor (actual_target))
14f9c5c9
AS
3067 return desc_data (actual);
3068 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
3069 {
3070 if (VALUE_LVAL (actual) != lval_memory)
3071 {
d2e4a39e 3072 struct value *val;
14f9c5c9
AS
3073 actual_type = check_typedef (VALUE_TYPE (actual));
3074 val = allocate_value (actual_type);
d2e4a39e
AS
3075 memcpy ((char *) VALUE_CONTENTS_RAW (val),
3076 (char *) VALUE_CONTENTS (actual),
14f9c5c9
AS
3077 TYPE_LENGTH (actual_type));
3078 actual = place_on_stack (val, sp);
3079 }
3080 return value_addr (actual);
3081 }
3082 }
3083 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
3084 return ada_value_ind (actual);
3085
3086 return actual;
3087}
3088
3089
3090/* Push a descriptor of type TYPE for array value ARR on the stack at
3091 *SP, updating *SP to reflect the new descriptor. Return either
3092 an lvalue representing the new descriptor, or (if TYPE is a pointer-
3093 to-descriptor type rather than a descriptor type), a struct value*
3094 representing a pointer to this descriptor. */
3095
d2e4a39e
AS
3096static struct value *
3097make_array_descriptor (struct type *type, struct value *arr, CORE_ADDR *sp)
14f9c5c9 3098{
d2e4a39e
AS
3099 struct type *bounds_type = desc_bounds_type (type);
3100 struct type *desc_type = desc_base_type (type);
3101 struct value *descriptor = allocate_value (desc_type);
3102 struct value *bounds = allocate_value (bounds_type);
14f9c5c9
AS
3103 CORE_ADDR bounds_addr;
3104 int i;
d2e4a39e 3105
14f9c5c9
AS
3106 for (i = ada_array_arity (check_typedef (VALUE_TYPE (arr))); i > 0; i -= 1)
3107 {
3108 modify_general_field (VALUE_CONTENTS (bounds),
d2e4a39e 3109 value_as_long (ada_array_bound (arr, i, 0)),
14f9c5c9
AS
3110 desc_bound_bitpos (bounds_type, i, 0),
3111 desc_bound_bitsize (bounds_type, i, 0));
3112 modify_general_field (VALUE_CONTENTS (bounds),
d2e4a39e 3113 value_as_long (ada_array_bound (arr, i, 1)),
14f9c5c9
AS
3114 desc_bound_bitpos (bounds_type, i, 1),
3115 desc_bound_bitsize (bounds_type, i, 1));
3116 }
d2e4a39e 3117
14f9c5c9 3118 bounds = place_on_stack (bounds, sp);
d2e4a39e 3119
14f9c5c9
AS
3120 modify_general_field (VALUE_CONTENTS (descriptor),
3121 arr,
3122 fat_pntr_data_bitpos (desc_type),
3123 fat_pntr_data_bitsize (desc_type));
3124 modify_general_field (VALUE_CONTENTS (descriptor),
3125 VALUE_ADDRESS (bounds),
3126 fat_pntr_bounds_bitpos (desc_type),
3127 fat_pntr_bounds_bitsize (desc_type));
3128
3129 descriptor = place_on_stack (descriptor, sp);
3130
3131 if (TYPE_CODE (type) == TYPE_CODE_PTR)
3132 return value_addr (descriptor);
3133 else
3134 return descriptor;
3135}
3136
3137
3138/* Assuming a dummy frame has been established on the target, perform any
3139 conversions needed for calling function FUNC on the NARGS actual
3140 parameters in ARGS, other than standard C conversions. Does
3141 nothing if FUNC does not have Ada-style prototype data, or if NARGS
3142 does not match the number of arguments expected. Use *SP as a
3143 stack pointer for additional data that must be pushed, updating its
3144 value as needed. */
3145
3146void
d2e4a39e
AS
3147ada_convert_actuals (struct value *func, int nargs, struct value *args[],
3148 CORE_ADDR *sp)
14f9c5c9
AS
3149{
3150 int i;
3151
d2e4a39e 3152 if (TYPE_NFIELDS (VALUE_TYPE (func)) == 0
14f9c5c9
AS
3153 || nargs != TYPE_NFIELDS (VALUE_TYPE (func)))
3154 return;
3155
3156 for (i = 0; i < nargs; i += 1)
d2e4a39e
AS
3157 args[i] =
3158 convert_actual (args[i], TYPE_FIELD_TYPE (VALUE_TYPE (func), i), sp);
14f9c5c9 3159}
14f9c5c9 3160\f
d2e4a39e 3161
14f9c5c9
AS
3162 /* Symbol Lookup */
3163
3164
3165/* The vectors of symbols and blocks ultimately returned from */
3166/* ada_lookup_symbol_list. */
3167
3168/* Current size of defn_symbols and defn_blocks */
d2e4a39e 3169static size_t defn_vector_size = 0;
14f9c5c9
AS
3170
3171/* Current number of symbols found. */
3172static int ndefns = 0;
3173
d2e4a39e
AS
3174static struct symbol **defn_symbols = NULL;
3175static struct block **defn_blocks = NULL;
14f9c5c9
AS
3176
3177/* Return the result of a standard (literal, C-like) lookup of NAME in
3178 * given NAMESPACE. */
3179
d2e4a39e
AS
3180static struct symbol *
3181standard_lookup (const char *name, namespace_enum namespace)
14f9c5c9 3182{
d2e4a39e
AS
3183 struct symbol *sym;
3184 struct symtab *symtab;
3185 sym = lookup_symbol (name, (struct block *) NULL, namespace, 0, &symtab);
14f9c5c9
AS
3186 return sym;
3187}
d2e4a39e 3188
14f9c5c9
AS
3189
3190/* Non-zero iff there is at least one non-function/non-enumeral symbol */
3191/* in SYMS[0..N-1]. We treat enumerals as functions, since they */
d2e4a39e 3192/* contend in overloading in the same way. */
14f9c5c9 3193static int
d2e4a39e 3194is_nonfunction (struct symbol *syms[], int n)
14f9c5c9
AS
3195{
3196 int i;
3197
3198 for (i = 0; i < n; i += 1)
3199 if (TYPE_CODE (SYMBOL_TYPE (syms[i])) != TYPE_CODE_FUNC
3200 && TYPE_CODE (SYMBOL_TYPE (syms[i])) != TYPE_CODE_ENUM)
3201 return 1;
3202
3203 return 0;
3204}
3205
3206/* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
3207 struct types. Otherwise, they may not. */
3208
3209static int
d2e4a39e 3210equiv_types (struct type *type0, struct type *type1)
14f9c5c9 3211{
d2e4a39e 3212 if (type0 == type1)
14f9c5c9 3213 return 1;
d2e4a39e 3214 if (type0 == NULL || type1 == NULL
14f9c5c9
AS
3215 || TYPE_CODE (type0) != TYPE_CODE (type1))
3216 return 0;
d2e4a39e 3217 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
14f9c5c9
AS
3218 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
3219 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
3220 && STREQ (ada_type_name (type0), ada_type_name (type1)))
3221 return 1;
d2e4a39e 3222
14f9c5c9
AS
3223 return 0;
3224}
3225
3226/* True iff SYM0 represents the same entity as SYM1, or one that is
3227 no more defined than that of SYM1. */
3228
3229static int
d2e4a39e 3230lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
14f9c5c9
AS
3231{
3232 if (sym0 == sym1)
3233 return 1;
3234 if (SYMBOL_NAMESPACE (sym0) != SYMBOL_NAMESPACE (sym1)
3235 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
3236 return 0;
3237
d2e4a39e 3238 switch (SYMBOL_CLASS (sym0))
14f9c5c9
AS
3239 {
3240 case LOC_UNDEF:
3241 return 1;
3242 case LOC_TYPEDEF:
3243 {
d2e4a39e
AS
3244 struct type *type0 = SYMBOL_TYPE (sym0);
3245 struct type *type1 = SYMBOL_TYPE (sym1);
22abf04a
DC
3246 char *name0 = DEPRECATED_SYMBOL_NAME (sym0);
3247 char *name1 = DEPRECATED_SYMBOL_NAME (sym1);
14f9c5c9 3248 int len0 = strlen (name0);
d2e4a39e 3249 return
14f9c5c9
AS
3250 TYPE_CODE (type0) == TYPE_CODE (type1)
3251 && (equiv_types (type0, type1)
3252 || (len0 < strlen (name1) && STREQN (name0, name1, len0)
3253 && STREQN (name1 + len0, "___XV", 5)));
3254 }
3255 case LOC_CONST:
3256 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
3257 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
d2e4a39e
AS
3258 default:
3259 return 0;
14f9c5c9
AS
3260 }
3261}
3262
3263/* Append SYM to the end of defn_symbols, and BLOCK to the end of
3264 defn_blocks, updating ndefns, and expanding defn_symbols and
3265 defn_blocks as needed. Do not include SYM if it is a duplicate. */
3266
3267static void
d2e4a39e 3268add_defn_to_vec (struct symbol *sym, struct block *block)
14f9c5c9
AS
3269{
3270 int i;
3271 size_t tmp;
3272
d2e4a39e 3273 if (SYMBOL_TYPE (sym) != NULL)
14f9c5c9
AS
3274 CHECK_TYPEDEF (SYMBOL_TYPE (sym));
3275 for (i = 0; i < ndefns; i += 1)
3276 {
3277 if (lesseq_defined_than (sym, defn_symbols[i]))
3278 return;
3279 else if (lesseq_defined_than (defn_symbols[i], sym))
3280 {
3281 defn_symbols[i] = sym;
3282 defn_blocks[i] = block;
3283 return;
3284 }
3285 }
3286
3287 tmp = defn_vector_size;
d2e4a39e
AS
3288 GROW_VECT (defn_symbols, tmp, ndefns + 2);
3289 GROW_VECT (defn_blocks, defn_vector_size, ndefns + 2);
14f9c5c9
AS
3290
3291 defn_symbols[ndefns] = sym;
3292 defn_blocks[ndefns] = block;
3293 ndefns += 1;
3294}
3295
3296/* Look, in partial_symtab PST, for symbol NAME in given namespace.
3297 Check the global symbols if GLOBAL, the static symbols if not. Do
3298 wild-card match if WILD. */
3299
3300static struct partial_symbol *
d2e4a39e
AS
3301ada_lookup_partial_symbol (struct partial_symtab *pst, const char *name,
3302 int global, namespace_enum namespace, int wild)
14f9c5c9
AS
3303{
3304 struct partial_symbol **start;
3305 int name_len = strlen (name);
3306 int length = (global ? pst->n_global_syms : pst->n_static_syms);
3307 int i;
3308
3309 if (length == 0)
3310 {
3311 return (NULL);
3312 }
d2e4a39e 3313
14f9c5c9
AS
3314 start = (global ?
3315 pst->objfile->global_psymbols.list + pst->globals_offset :
d2e4a39e 3316 pst->objfile->static_psymbols.list + pst->statics_offset);
14f9c5c9
AS
3317
3318 if (wild)
3319 {
3320 for (i = 0; i < length; i += 1)
3321 {
d2e4a39e 3322 struct partial_symbol *psym = start[i];
14f9c5c9
AS
3323
3324 if (SYMBOL_NAMESPACE (psym) == namespace &&
22abf04a 3325 wild_match (name, name_len, DEPRECATED_SYMBOL_NAME (psym)))
14f9c5c9
AS
3326 return psym;
3327 }
3328 return NULL;
3329 }
d2e4a39e 3330 else
14f9c5c9
AS
3331 {
3332 if (global)
3333 {
3334 int U;
d2e4a39e
AS
3335 i = 0;
3336 U = length - 1;
3337 while (U - i > 4)
14f9c5c9 3338 {
d2e4a39e
AS
3339 int M = (U + i) >> 1;
3340 struct partial_symbol *psym = start[M];
22abf04a 3341 if (DEPRECATED_SYMBOL_NAME (psym)[0] < name[0])
d2e4a39e 3342 i = M + 1;
22abf04a 3343 else if (DEPRECATED_SYMBOL_NAME (psym)[0] > name[0])
d2e4a39e 3344 U = M - 1;
22abf04a 3345 else if (strcmp (DEPRECATED_SYMBOL_NAME (psym), name) < 0)
d2e4a39e 3346 i = M + 1;
14f9c5c9
AS
3347 else
3348 U = M;
3349 }
3350 }
3351 else
3352 i = 0;
3353
3354 while (i < length)
3355 {
3356 struct partial_symbol *psym = start[i];
3357
3358 if (SYMBOL_NAMESPACE (psym) == namespace)
3359 {
22abf04a 3360 int cmp = strncmp (name, DEPRECATED_SYMBOL_NAME (psym), name_len);
d2e4a39e
AS
3361
3362 if (cmp < 0)
14f9c5c9
AS
3363 {
3364 if (global)
3365 break;
3366 }
d2e4a39e 3367 else if (cmp == 0
22abf04a 3368 && is_name_suffix (DEPRECATED_SYMBOL_NAME (psym) + name_len))
14f9c5c9
AS
3369 return psym;
3370 }
3371 i += 1;
3372 }
3373
3374 if (global)
3375 {
3376 int U;
d2e4a39e
AS
3377 i = 0;
3378 U = length - 1;
3379 while (U - i > 4)
14f9c5c9 3380 {
d2e4a39e 3381 int M = (U + i) >> 1;
14f9c5c9 3382 struct partial_symbol *psym = start[M];
22abf04a 3383 if (DEPRECATED_SYMBOL_NAME (psym)[0] < '_')
d2e4a39e 3384 i = M + 1;
22abf04a 3385 else if (DEPRECATED_SYMBOL_NAME (psym)[0] > '_')
d2e4a39e 3386 U = M - 1;
22abf04a 3387 else if (strcmp (DEPRECATED_SYMBOL_NAME (psym), "_ada_") < 0)
d2e4a39e 3388 i = M + 1;
14f9c5c9
AS
3389 else
3390 U = M;
3391 }
3392 }
3393 else
3394 i = 0;
3395
3396 while (i < length)
3397 {
d2e4a39e 3398 struct partial_symbol *psym = start[i];
14f9c5c9
AS
3399
3400 if (SYMBOL_NAMESPACE (psym) == namespace)
3401 {
3402 int cmp;
3403
22abf04a 3404 cmp = (int) '_' - (int) DEPRECATED_SYMBOL_NAME (psym)[0];
d2e4a39e 3405 if (cmp == 0)
14f9c5c9 3406 {
22abf04a 3407 cmp = strncmp ("_ada_", DEPRECATED_SYMBOL_NAME (psym), 5);
14f9c5c9 3408 if (cmp == 0)
22abf04a 3409 cmp = strncmp (name, DEPRECATED_SYMBOL_NAME (psym) + 5, name_len);
14f9c5c9 3410 }
d2e4a39e
AS
3411
3412 if (cmp < 0)
14f9c5c9
AS
3413 {
3414 if (global)
3415 break;
3416 }
d2e4a39e 3417 else if (cmp == 0
22abf04a 3418 && is_name_suffix (DEPRECATED_SYMBOL_NAME (psym) + name_len + 5))
14f9c5c9
AS
3419 return psym;
3420 }
3421 i += 1;
3422 }
d2e4a39e 3423
14f9c5c9
AS
3424 }
3425 return NULL;
3426}
3427
3428
3429/* Find a symbol table containing symbol SYM or NULL if none. */
d2e4a39e
AS
3430static struct symtab *
3431symtab_for_sym (struct symbol *sym)
14f9c5c9 3432{
d2e4a39e 3433 struct symtab *s;
14f9c5c9
AS
3434 struct objfile *objfile;
3435 struct block *b;
261397f8 3436 struct symbol *tmp_sym;
14f9c5c9
AS
3437 int i, j;
3438
3439 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
3440 {
3441 switch (SYMBOL_CLASS (sym))
3442 {
3443 case LOC_CONST:
3444 case LOC_STATIC:
3445 case LOC_TYPEDEF:
3446 case LOC_REGISTER:
3447 case LOC_LABEL:
3448 case LOC_BLOCK:
3449 case LOC_CONST_BYTES:
3450 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3451 ALL_BLOCK_SYMBOLS (b, i, tmp_sym) if (sym == tmp_sym)
3452 return s;
3453 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3454 ALL_BLOCK_SYMBOLS (b, i, tmp_sym) if (sym == tmp_sym)
3455 return s;
3456 break;
3457 default:
3458 break;
3459 }
3460 switch (SYMBOL_CLASS (sym))
3461 {
3462 case LOC_REGISTER:
3463 case LOC_ARG:
3464 case LOC_REF_ARG:
3465 case LOC_REGPARM:
3466 case LOC_REGPARM_ADDR:
3467 case LOC_LOCAL:
3468 case LOC_TYPEDEF:
3469 case LOC_LOCAL_ARG:
3470 case LOC_BASEREG:
3471 case LOC_BASEREG_ARG:
4c2df51b
DJ
3472 case LOC_COMPUTED:
3473 case LOC_COMPUTED_ARG:
d2e4a39e
AS
3474 for (j = FIRST_LOCAL_BLOCK;
3475 j < BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)); j += 1)
3476 {
3477 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), j);
3478 ALL_BLOCK_SYMBOLS (b, i, tmp_sym) if (sym == tmp_sym)
14f9c5c9 3479 return s;
d2e4a39e
AS
3480 }
3481 break;
3482 default:
3483 break;
3484 }
3485 }
14f9c5c9
AS
3486 return NULL;
3487}
3488
3489/* Return a minimal symbol matching NAME according to Ada demangling
3490 rules. Returns NULL if there is no such minimal symbol. */
3491
d2e4a39e
AS
3492struct minimal_symbol *
3493ada_lookup_minimal_symbol (const char *name)
14f9c5c9 3494{
d2e4a39e
AS
3495 struct objfile *objfile;
3496 struct minimal_symbol *msymbol;
14f9c5c9
AS
3497 int wild_match = (strstr (name, "__") == NULL);
3498
3499 ALL_MSYMBOLS (objfile, msymbol)
d2e4a39e 3500 {
22abf04a 3501 if (ada_match_name (DEPRECATED_SYMBOL_NAME (msymbol), name, wild_match)
d2e4a39e
AS
3502 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
3503 return msymbol;
3504 }
14f9c5c9
AS
3505
3506 return NULL;
3507}
3508
3509/* For all subprograms that statically enclose the subprogram of the
3510 * selected frame, add symbols matching identifier NAME in NAMESPACE
3511 * and their blocks to vectors *defn_symbols and *defn_blocks, as for
3512 * ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
3513 * wildcard prefix. At the moment, this function uses a heuristic to
3514 * find the frames of enclosing subprograms: it treats the
3515 * pointer-sized value at location 0 from the local-variable base of a
3516 * frame as a static link, and then searches up the call stack for a
3517 * frame with that same local-variable base. */
3518static void
d2e4a39e
AS
3519add_symbols_from_enclosing_procs (const char *name, namespace_enum namespace,
3520 int wild_match)
14f9c5c9
AS
3521{
3522#ifdef i386
3523 static struct symbol static_link_sym;
3524 static struct symbol *static_link;
3525
d2e4a39e
AS
3526 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
3527 struct frame_info *frame;
3528 struct frame_info *target_frame;
14f9c5c9
AS
3529
3530 if (static_link == NULL)
3531 {
3532 /* Initialize the local variable symbol that stands for the
3533 * static link (when it exists). */
3534 static_link = &static_link_sym;
22abf04a 3535 DEPRECATED_SYMBOL_NAME (static_link) = "";
14f9c5c9
AS
3536 SYMBOL_LANGUAGE (static_link) = language_unknown;
3537 SYMBOL_CLASS (static_link) = LOC_LOCAL;
3538 SYMBOL_NAMESPACE (static_link) = VAR_NAMESPACE;
3539 SYMBOL_TYPE (static_link) = lookup_pointer_type (builtin_type_void);
d2e4a39e
AS
3540 SYMBOL_VALUE (static_link) =
3541 -(long) TYPE_LENGTH (SYMBOL_TYPE (static_link));
14f9c5c9
AS
3542 }
3543
6e7f8b9c 3544 frame = deprecated_selected_frame;
14f9c5c9
AS
3545 while (frame != NULL && ndefns == 0)
3546 {
d2e4a39e
AS
3547 struct block *block;
3548 struct value *target_link_val = read_var_value (static_link, frame);
14f9c5c9
AS
3549 CORE_ADDR target_link;
3550
3551 if (target_link_val == NULL)
3552 break;
3553 QUIT;
3554
3555 target_link = target_link_val;
d2e4a39e
AS
3556 do
3557 {
14f9c5c9
AS
3558 QUIT;
3559 frame = get_prev_frame (frame);
d2e4a39e
AS
3560 }
3561 while (frame != NULL && FRAME_LOCALS_ADDRESS (frame) != target_link);
14f9c5c9
AS
3562
3563 if (frame == NULL)
3564 break;
3565
3566 block = get_frame_block (frame, 0);
3567 while (block != NULL && block_function (block) != NULL && ndefns == 0)
3568 {
3569 ada_add_block_symbols (block, name, namespace, NULL, wild_match);
d2e4a39e 3570
14f9c5c9
AS
3571 block = BLOCK_SUPERBLOCK (block);
3572 }
3573 }
3574
3575 do_cleanups (old_chain);
3576#endif
3577}
3578
3579/* True if TYPE is definitely an artificial type supplied to a symbol
3580 * for which no debugging information was given in the symbol file. */
3581static int
d2e4a39e 3582is_nondebugging_type (struct type *type)
14f9c5c9 3583{
d2e4a39e 3584 char *name = ada_type_name (type);
14f9c5c9
AS
3585 return (name != NULL && STREQ (name, "<variable, no debug info>"));
3586}
3587
3588/* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
3589 * duplicate other symbols in the list. (The only case I know of where
3590 * this happens is when object files containing stabs-in-ecoff are
3591 * linked with files containing ordinary ecoff debugging symbols (or no
3592 * debugging symbols)). Modifies SYMS to squeeze out deleted symbols,
3593 * and applies the same modification to BLOCKS to maintain the
3594 * correspondence between SYMS[i] and BLOCKS[i]. Returns the number
3595 * of symbols in the modified list. */
3596static int
d2e4a39e 3597remove_extra_symbols (struct symbol **syms, struct block **blocks, int nsyms)
14f9c5c9
AS
3598{
3599 int i, j;
3600
3601 i = 0;
3602 while (i < nsyms)
3603 {
22abf04a 3604 if (DEPRECATED_SYMBOL_NAME (syms[i]) != NULL
d2e4a39e 3605 && SYMBOL_CLASS (syms[i]) == LOC_STATIC
14f9c5c9
AS
3606 && is_nondebugging_type (SYMBOL_TYPE (syms[i])))
3607 {
3608 for (j = 0; j < nsyms; j += 1)
3609 {
d2e4a39e 3610 if (i != j
22abf04a
DC
3611 && DEPRECATED_SYMBOL_NAME (syms[j]) != NULL
3612 && STREQ (DEPRECATED_SYMBOL_NAME (syms[i]), DEPRECATED_SYMBOL_NAME (syms[j]))
14f9c5c9 3613 && SYMBOL_CLASS (syms[i]) == SYMBOL_CLASS (syms[j])
d2e4a39e 3614 && SYMBOL_VALUE_ADDRESS (syms[i])
14f9c5c9
AS
3615 == SYMBOL_VALUE_ADDRESS (syms[j]))
3616 {
3617 int k;
d2e4a39e 3618 for (k = i + 1; k < nsyms; k += 1)
14f9c5c9 3619 {
d2e4a39e
AS
3620 syms[k - 1] = syms[k];
3621 blocks[k - 1] = blocks[k];
14f9c5c9
AS
3622 }
3623 nsyms -= 1;
3624 goto NextSymbol;
3625 }
3626 }
3627 }
3628 i += 1;
3629 NextSymbol:
3630 ;
3631 }
3632 return nsyms;
3633}
3634
3635/* Find symbols in NAMESPACE matching NAME, in BLOCK0 and enclosing
3636 scope and in global scopes, returning the number of matches. Sets
3637 *SYMS to point to a vector of matching symbols, with *BLOCKS
3638 pointing to the vector of corresponding blocks in which those
3639 symbols reside. These two vectors are transient---good only to the
3640 next call of ada_lookup_symbol_list. Any non-function/non-enumeral symbol
3641 match within the nest of blocks whose innermost member is BLOCK0,
3642 is the outermost match returned (no other matches in that or
3643 enclosing blocks is returned). If there are any matches in or
3644 surrounding BLOCK0, then these alone are returned. */
3645
3646int
ebf56fd3 3647ada_lookup_symbol_list (const char *name, struct block *block0,
d2e4a39e
AS
3648 namespace_enum namespace, struct symbol ***syms,
3649 struct block ***blocks)
14f9c5c9
AS
3650{
3651 struct symbol *sym;
3652 struct symtab *s;
3653 struct partial_symtab *ps;
3654 struct blockvector *bv;
3655 struct objfile *objfile;
3656 struct block *b;
3657 struct block *block;
3658 struct minimal_symbol *msymbol;
3659 int wild_match = (strstr (name, "__") == NULL);
3660 int cacheIfUnique;
3661
3662#ifdef TIMING
3663 markTimeStart (0);
3664#endif
3665
3666 ndefns = 0;
3667 cacheIfUnique = 0;
3668
3669 /* Search specified block and its superiors. */
3670
3671 block = block0;
3672 while (block != NULL)
3673 {
3674 ada_add_block_symbols (block, name, namespace, NULL, wild_match);
3675
3676 /* If we found a non-function match, assume that's the one. */
3677 if (is_nonfunction (defn_symbols, ndefns))
3678 goto done;
3679
3680 block = BLOCK_SUPERBLOCK (block);
3681 }
3682
3683 /* If we found ANY matches in the specified BLOCK, we're done. */
3684
3685 if (ndefns > 0)
3686 goto done;
d2e4a39e 3687
14f9c5c9
AS
3688 cacheIfUnique = 1;
3689
3690 /* Now add symbols from all global blocks: symbol tables, minimal symbol
3691 tables, and psymtab's */
3692
3693 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
3694 {
3695 QUIT;
3696 if (!s->primary)
3697 continue;
3698 bv = BLOCKVECTOR (s);
3699 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3700 ada_add_block_symbols (block, name, namespace, objfile, wild_match);
3701 }
14f9c5c9
AS
3702
3703 if (namespace == VAR_NAMESPACE)
3704 {
3705 ALL_MSYMBOLS (objfile, msymbol)
d2e4a39e 3706 {
22abf04a 3707 if (ada_match_name (DEPRECATED_SYMBOL_NAME (msymbol), name, wild_match))
d2e4a39e
AS
3708 {
3709 switch (MSYMBOL_TYPE (msymbol))
3710 {
3711 case mst_solib_trampoline:
3712 break;
3713 default:
3714 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
3715 if (s != NULL)
3716 {
3717 int old_ndefns = ndefns;
3718 QUIT;
3719 bv = BLOCKVECTOR (s);
3720 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3721 ada_add_block_symbols (block,
22abf04a 3722 DEPRECATED_SYMBOL_NAME (msymbol),
d2e4a39e
AS
3723 namespace, objfile, wild_match);
3724 if (ndefns == old_ndefns)
3725 {
3726 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
3727 ada_add_block_symbols (block,
22abf04a 3728 DEPRECATED_SYMBOL_NAME (msymbol),
d2e4a39e
AS
3729 namespace, objfile,
3730 wild_match);
3731 }
3732 }
3733 }
3734 }
3735 }
14f9c5c9 3736 }
d2e4a39e 3737
14f9c5c9 3738 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
3739 {
3740 QUIT;
3741 if (!ps->readin
3742 && ada_lookup_partial_symbol (ps, name, 1, namespace, wild_match))
3743 {
3744 s = PSYMTAB_TO_SYMTAB (ps);
3745 if (!s->primary)
3746 continue;
3747 bv = BLOCKVECTOR (s);
3748 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3749 ada_add_block_symbols (block, name, namespace, objfile, wild_match);
3750 }
3751 }
3752
14f9c5c9
AS
3753 /* Now add symbols from all per-file blocks if we've gotten no hits.
3754 (Not strictly correct, but perhaps better than an error).
3755 Do the symtabs first, then check the psymtabs */
d2e4a39e 3756
14f9c5c9
AS
3757 if (ndefns == 0)
3758 {
3759
3760 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
3761 {
3762 QUIT;
3763 if (!s->primary)
3764 continue;
3765 bv = BLOCKVECTOR (s);
3766 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
3767 ada_add_block_symbols (block, name, namespace, objfile, wild_match);
3768 }
3769
14f9c5c9 3770 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
3771 {
3772 QUIT;
3773 if (!ps->readin
3774 && ada_lookup_partial_symbol (ps, name, 0, namespace, wild_match))
3775 {
3776 s = PSYMTAB_TO_SYMTAB (ps);
3777 bv = BLOCKVECTOR (s);
3778 if (!s->primary)
3779 continue;
3780 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
3781 ada_add_block_symbols (block, name, namespace,
3782 objfile, wild_match);
3783 }
3784 }
3785 }
14f9c5c9
AS
3786
3787 /* Finally, we try to find NAME as a local symbol in some lexically
3788 enclosing block. We do this last, expecting this case to be
3789 rare. */
d2e4a39e 3790 if (ndefns == 0)
14f9c5c9
AS
3791 {
3792 add_symbols_from_enclosing_procs (name, namespace, wild_match);
3793 if (ndefns > 0)
3794 goto done;
3795 }
3796
d2e4a39e 3797done:
14f9c5c9
AS
3798 ndefns = remove_extra_symbols (defn_symbols, defn_blocks, ndefns);
3799
3800
3801 *syms = defn_symbols;
3802 *blocks = defn_blocks;
3803#ifdef TIMING
3804 markTimeStop (0);
3805#endif
3806 return ndefns;
3807}
3808
3809/* Return a symbol in NAMESPACE matching NAME, in BLOCK0 and enclosing
3810 * scope and in global scopes, or NULL if none. NAME is folded to
3811 * lower case first, unless it is surrounded in single quotes.
3812 * Otherwise, the result is as for ada_lookup_symbol_list, but is
3813 * disambiguated by user query if needed. */
3814
d2e4a39e
AS
3815struct symbol *
3816ada_lookup_symbol (const char *name, struct block *block0,
3817 namespace_enum namespace)
14f9c5c9 3818{
d2e4a39e
AS
3819 struct symbol **candidate_syms;
3820 struct block **candidate_blocks;
14f9c5c9
AS
3821 int n_candidates;
3822
3823 n_candidates = ada_lookup_symbol_list (name,
3824 block0, namespace,
3825 &candidate_syms, &candidate_blocks);
3826
3827 if (n_candidates == 0)
3828 return NULL;
3829 else if (n_candidates != 1)
3830 user_select_syms (candidate_syms, candidate_blocks, n_candidates, 1);
3831
3832 return candidate_syms[0];
3833}
3834
3835
3836/* True iff STR is a possible encoded suffix of a normal Ada name
3837 * that is to be ignored for matching purposes. Suffixes of parallel
3838 * names (e.g., XVE) are not included here. Currently, the possible suffixes
3839 * are given by the regular expression:
3840 * (X[nb]*)?(__[0-9]+|\$[0-9]+|___(LJM|X([FDBUP].*|R[^T]?)))?$
3841 *
3842 */
3843static int
d2e4a39e 3844is_name_suffix (const char *str)
14f9c5c9
AS
3845{
3846 int k;
3847 if (str[0] == 'X')
3848 {
3849 str += 1;
d2e4a39e 3850 while (str[0] != '_' && str[0] != '\0')
14f9c5c9
AS
3851 {
3852 if (str[0] != 'n' && str[0] != 'b')
3853 return 0;
3854 str += 1;
d2e4a39e 3855 }
14f9c5c9
AS
3856 }
3857 if (str[0] == '\000')
3858 return 1;
d2e4a39e 3859 if (str[0] == '_')
14f9c5c9
AS
3860 {
3861 if (str[1] != '_' || str[2] == '\000')
3862 return 0;
d2e4a39e 3863 if (str[2] == '_')
14f9c5c9 3864 {
d2e4a39e 3865 if (STREQ (str + 3, "LJM"))
14f9c5c9
AS
3866 return 1;
3867 if (str[3] != 'X')
3868 return 0;
3869 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B' ||
3870 str[4] == 'U' || str[4] == 'P')
3871 return 1;
3872 if (str[4] == 'R' && str[5] != 'T')
3873 return 1;
3874 return 0;
3875 }
3876 for (k = 2; str[k] != '\0'; k += 1)
3877 if (!isdigit (str[k]))
3878 return 0;
3879 return 1;
3880 }
3881 if (str[0] == '$' && str[1] != '\000')
3882 {
3883 for (k = 1; str[k] != '\0'; k += 1)
3884 if (!isdigit (str[k]))
3885 return 0;
3886 return 1;
3887 }
3888 return 0;
3889}
d2e4a39e 3890
14f9c5c9
AS
3891/* True if NAME represents a name of the form A1.A2....An, n>=1 and
3892 * PATN[0..PATN_LEN-1] = Ak.Ak+1.....An for some k >= 1. Ignores
3893 * informational suffixes of NAME (i.e., for which is_name_suffix is
d2e4a39e 3894 * true). */
14f9c5c9 3895static int
d2e4a39e 3896wild_match (const char *patn, int patn_len, const char *name)
14f9c5c9
AS
3897{
3898 int name_len;
3899 int s, e;
3900
3901 name_len = strlen (name);
d2e4a39e
AS
3902 if (name_len >= patn_len + 5 && STREQN (name, "_ada_", 5)
3903 && STREQN (patn, name + 5, patn_len)
3904 && is_name_suffix (name + patn_len + 5))
14f9c5c9
AS
3905 return 1;
3906
d2e4a39e 3907 while (name_len >= patn_len)
14f9c5c9 3908 {
d2e4a39e 3909 if (STREQN (patn, name, patn_len) && is_name_suffix (name + patn_len))
14f9c5c9 3910 return 1;
d2e4a39e
AS
3911 do
3912 {
3913 name += 1;
3914 name_len -= 1;
3915 }
3916 while (name_len > 0
3917 && name[0] != '.' && (name[0] != '_' || name[1] != '_'));
14f9c5c9
AS
3918 if (name_len <= 0)
3919 return 0;
3920 if (name[0] == '_')
3921 {
d2e4a39e 3922 if (!islower (name[2]))
14f9c5c9 3923 return 0;
d2e4a39e
AS
3924 name += 2;
3925 name_len -= 2;
14f9c5c9
AS
3926 }
3927 else
3928 {
d2e4a39e 3929 if (!islower (name[1]))
14f9c5c9 3930 return 0;
d2e4a39e
AS
3931 name += 1;
3932 name_len -= 1;
14f9c5c9
AS
3933 }
3934 }
3935
3936 return 0;
3937}
3938
3939
3940/* Add symbols from BLOCK matching identifier NAME in NAMESPACE to
3941 vector *defn_symbols, updating *defn_symbols (if necessary), *SZ (the size of
3942 the vector *defn_symbols), and *ndefns (the number of symbols
3943 currently stored in *defn_symbols). If WILD, treat as NAME with a
3944 wildcard prefix. OBJFILE is the section containing BLOCK. */
3945
d2e4a39e
AS
3946static void
3947ada_add_block_symbols (struct block *block, const char *name,
3948 namespace_enum namespace, struct objfile *objfile,
ebf56fd3 3949 int wild)
14f9c5c9
AS
3950{
3951 int i;
3952 int name_len = strlen (name);
3953 /* A matching argument symbol, if any. */
3954 struct symbol *arg_sym;
3955 /* Set true when we find a matching non-argument symbol */
3956 int found_sym;
3957 int is_sorted = BLOCK_SHOULD_SORT (block);
261397f8 3958 struct symbol *sym;
14f9c5c9 3959
d2e4a39e
AS
3960 arg_sym = NULL;
3961 found_sym = 0;
14f9c5c9
AS
3962 if (wild)
3963 {
261397f8
DJ
3964 struct symbol *sym;
3965 ALL_BLOCK_SYMBOLS (block, i, sym)
d2e4a39e
AS
3966 {
3967 if (SYMBOL_NAMESPACE (sym) == namespace &&
22abf04a 3968 wild_match (name, name_len, DEPRECATED_SYMBOL_NAME (sym)))
d2e4a39e
AS
3969 {
3970 switch (SYMBOL_CLASS (sym))
3971 {
3972 case LOC_ARG:
3973 case LOC_LOCAL_ARG:
3974 case LOC_REF_ARG:
3975 case LOC_REGPARM:
3976 case LOC_REGPARM_ADDR:
3977 case LOC_BASEREG_ARG:
4c2df51b 3978 case LOC_COMPUTED_ARG:
d2e4a39e
AS
3979 arg_sym = sym;
3980 break;
3981 case LOC_UNRESOLVED:
3982 continue;
3983 default:
3984 found_sym = 1;
3985 fill_in_ada_prototype (sym);
3986 add_defn_to_vec (fixup_symbol_section (sym, objfile), block);
3987 break;
3988 }
3989 }
3990 }
14f9c5c9 3991 }
d2e4a39e 3992 else
14f9c5c9
AS
3993 {
3994 if (is_sorted)
3995 {
3996 int U;
d2e4a39e
AS
3997 i = 0;
3998 U = BLOCK_NSYMS (block) - 1;
3999 while (U - i > 4)
14f9c5c9 4000 {
d2e4a39e 4001 int M = (U + i) >> 1;
14f9c5c9 4002 struct symbol *sym = BLOCK_SYM (block, M);
22abf04a 4003 if (DEPRECATED_SYMBOL_NAME (sym)[0] < name[0])
d2e4a39e 4004 i = M + 1;
22abf04a 4005 else if (DEPRECATED_SYMBOL_NAME (sym)[0] > name[0])
d2e4a39e 4006 U = M - 1;
22abf04a 4007 else if (strcmp (DEPRECATED_SYMBOL_NAME (sym), name) < 0)
d2e4a39e 4008 i = M + 1;
14f9c5c9
AS
4009 else
4010 U = M;
4011 }
4012 }
4013 else
4014 i = 0;
4015
261397f8
DJ
4016 for (; i < BLOCK_BUCKETS (block); i += 1)
4017 for (sym = BLOCK_BUCKET (block, i); sym != NULL; sym = sym->hash_next)
4018 {
4019 if (SYMBOL_NAMESPACE (sym) == namespace)
4020 {
22abf04a 4021 int cmp = strncmp (name, DEPRECATED_SYMBOL_NAME (sym), name_len);
14f9c5c9 4022
d2e4a39e 4023 if (cmp < 0)
261397f8
DJ
4024 {
4025 if (is_sorted)
4026 {
4027 i = BLOCK_BUCKETS (block);
4028 break;
4029 }
4030 }
d2e4a39e 4031 else if (cmp == 0
22abf04a 4032 && is_name_suffix (DEPRECATED_SYMBOL_NAME (sym) + name_len))
261397f8
DJ
4033 {
4034 switch (SYMBOL_CLASS (sym))
4035 {
4036 case LOC_ARG:
4037 case LOC_LOCAL_ARG:
4038 case LOC_REF_ARG:
4039 case LOC_REGPARM:
4040 case LOC_REGPARM_ADDR:
4041 case LOC_BASEREG_ARG:
4c2df51b 4042 case LOC_COMPUTED_ARG:
261397f8
DJ
4043 arg_sym = sym;
4044 break;
4045 case LOC_UNRESOLVED:
4046 break;
4047 default:
4048 found_sym = 1;
4049 fill_in_ada_prototype (sym);
4050 add_defn_to_vec (fixup_symbol_section (sym, objfile),
4051 block);
4052 break;
4053 }
4054 }
4055 }
4056 }
14f9c5c9
AS
4057 }
4058
d2e4a39e 4059 if (!found_sym && arg_sym != NULL)
14f9c5c9
AS
4060 {
4061 fill_in_ada_prototype (arg_sym);
4062 add_defn_to_vec (fixup_symbol_section (arg_sym, objfile), block);
4063 }
4064
d2e4a39e 4065 if (!wild)
14f9c5c9 4066 {
d2e4a39e
AS
4067 arg_sym = NULL;
4068 found_sym = 0;
14f9c5c9
AS
4069 if (is_sorted)
4070 {
4071 int U;
d2e4a39e
AS
4072 i = 0;
4073 U = BLOCK_NSYMS (block) - 1;
4074 while (U - i > 4)
14f9c5c9 4075 {
d2e4a39e 4076 int M = (U + i) >> 1;
14f9c5c9 4077 struct symbol *sym = BLOCK_SYM (block, M);
22abf04a 4078 if (DEPRECATED_SYMBOL_NAME (sym)[0] < '_')
d2e4a39e 4079 i = M + 1;
22abf04a 4080 else if (DEPRECATED_SYMBOL_NAME (sym)[0] > '_')
d2e4a39e 4081 U = M - 1;
22abf04a 4082 else if (strcmp (DEPRECATED_SYMBOL_NAME (sym), "_ada_") < 0)
d2e4a39e 4083 i = M + 1;
14f9c5c9
AS
4084 else
4085 U = M;
4086 }
4087 }
4088 else
4089 i = 0;
4090
261397f8
DJ
4091 for (; i < BLOCK_BUCKETS (block); i += 1)
4092 for (sym = BLOCK_BUCKET (block, i); sym != NULL; sym = sym->hash_next)
4093 {
4094 struct symbol *sym = BLOCK_SYM (block, i);
14f9c5c9 4095
261397f8
DJ
4096 if (SYMBOL_NAMESPACE (sym) == namespace)
4097 {
4098 int cmp;
14f9c5c9 4099
22abf04a 4100 cmp = (int) '_' - (int) DEPRECATED_SYMBOL_NAME (sym)[0];
d2e4a39e 4101 if (cmp == 0)
261397f8 4102 {
22abf04a 4103 cmp = strncmp ("_ada_", DEPRECATED_SYMBOL_NAME (sym), 5);
261397f8 4104 if (cmp == 0)
22abf04a 4105 cmp = strncmp (name, DEPRECATED_SYMBOL_NAME (sym) + 5, name_len);
261397f8
DJ
4106 }
4107
d2e4a39e 4108 if (cmp < 0)
261397f8
DJ
4109 {
4110 if (is_sorted)
4111 {
4112 i = BLOCK_BUCKETS (block);
4113 break;
4114 }
4115 }
d2e4a39e 4116 else if (cmp == 0
22abf04a 4117 && is_name_suffix (DEPRECATED_SYMBOL_NAME (sym) + name_len + 5))
261397f8
DJ
4118 {
4119 switch (SYMBOL_CLASS (sym))
4120 {
4121 case LOC_ARG:
4122 case LOC_LOCAL_ARG:
4123 case LOC_REF_ARG:
4124 case LOC_REGPARM:
4125 case LOC_REGPARM_ADDR:
4126 case LOC_BASEREG_ARG:
4c2df51b 4127 case LOC_COMPUTED_ARG:
261397f8
DJ
4128 arg_sym = sym;
4129 break;
4130 case LOC_UNRESOLVED:
4131 break;
4132 default:
4133 found_sym = 1;
4134 fill_in_ada_prototype (sym);
4135 add_defn_to_vec (fixup_symbol_section (sym, objfile),
4136 block);
4137 break;
4138 }
4139 }
4140 }
4141 }
d2e4a39e 4142
14f9c5c9 4143 /* NOTE: This really shouldn't be needed for _ada_ symbols.
d2e4a39e
AS
4144 They aren't parameters, right? */
4145 if (!found_sym && arg_sym != NULL)
14f9c5c9
AS
4146 {
4147 fill_in_ada_prototype (arg_sym);
4148 add_defn_to_vec (fixup_symbol_section (arg_sym, objfile), block);
4149 }
4150 }
4151}
14f9c5c9 4152\f
d2e4a39e 4153
14f9c5c9
AS
4154 /* Function Types */
4155
4156/* Assuming that SYM is the symbol for a function, fill in its type
170911c7 4157 with prototype information, if it is not already there. */
14f9c5c9
AS
4158
4159static void
d2e4a39e 4160fill_in_ada_prototype (struct symbol *func)
14f9c5c9 4161{
d2e4a39e 4162 struct block *b;
14f9c5c9
AS
4163 int nargs, nsyms;
4164 int i;
d2e4a39e
AS
4165 struct type *ftype;
4166 struct type *rtype;
14f9c5c9 4167 size_t max_fields;
261397f8 4168 struct symbol *sym;
14f9c5c9
AS
4169
4170 if (func == NULL
4171 || TYPE_CODE (SYMBOL_TYPE (func)) != TYPE_CODE_FUNC
4172 || TYPE_FIELDS (SYMBOL_TYPE (func)) != NULL)
4173 return;
4174
4175 /* We make each function type unique, so that each may have its own */
4176 /* parameter types. This particular way of doing so wastes space: */
4177 /* it would be nicer to build the argument types while the original */
4178 /* function type is being built (FIXME). */
4179 rtype = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (func)));
4180 ftype = alloc_type (TYPE_OBJFILE (SYMBOL_TYPE (func)));
4181 make_function_type (rtype, &ftype);
4182 SYMBOL_TYPE (func) = ftype;
4183
4184 b = SYMBOL_BLOCK_VALUE (func);
14f9c5c9
AS
4185
4186 nargs = 0;
d2e4a39e
AS
4187 max_fields = 8;
4188 TYPE_FIELDS (ftype) =
4189 (struct field *) xmalloc (sizeof (struct field) * max_fields);
261397f8 4190 ALL_BLOCK_SYMBOLS (b, i, sym)
d2e4a39e
AS
4191 {
4192 GROW_VECT (TYPE_FIELDS (ftype), max_fields, nargs + 1);
14f9c5c9 4193
d2e4a39e
AS
4194 switch (SYMBOL_CLASS (sym))
4195 {
4196 case LOC_REF_ARG:
4197 case LOC_REGPARM_ADDR:
4198 TYPE_FIELD_BITPOS (ftype, nargs) = nargs;
4199 TYPE_FIELD_BITSIZE (ftype, nargs) = 0;
01ad7f36 4200 TYPE_FIELD_STATIC_KIND (ftype, nargs) = 0;
d2e4a39e
AS
4201 TYPE_FIELD_TYPE (ftype, nargs) =
4202 lookup_pointer_type (check_typedef (SYMBOL_TYPE (sym)));
22abf04a 4203 TYPE_FIELD_NAME (ftype, nargs) = DEPRECATED_SYMBOL_NAME (sym);
d2e4a39e 4204 nargs += 1;
14f9c5c9 4205
d2e4a39e
AS
4206 break;
4207
4208 case LOC_ARG:
4209 case LOC_REGPARM:
4210 case LOC_LOCAL_ARG:
4211 case LOC_BASEREG_ARG:
4c2df51b 4212 case LOC_COMPUTED_ARG:
d2e4a39e
AS
4213 TYPE_FIELD_BITPOS (ftype, nargs) = nargs;
4214 TYPE_FIELD_BITSIZE (ftype, nargs) = 0;
01ad7f36 4215 TYPE_FIELD_STATIC_KIND (ftype, nargs) = 0;
d2e4a39e 4216 TYPE_FIELD_TYPE (ftype, nargs) = check_typedef (SYMBOL_TYPE (sym));
22abf04a 4217 TYPE_FIELD_NAME (ftype, nargs) = DEPRECATED_SYMBOL_NAME (sym);
d2e4a39e
AS
4218 nargs += 1;
4219
4220 break;
4221
4222 default:
4223 break;
4224 }
4225 }
14f9c5c9
AS
4226
4227 /* Re-allocate fields vector; if there are no fields, make the */
4228 /* fields pointer non-null anyway, to mark that this function type */
4229 /* has been filled in. */
4230
4231 TYPE_NFIELDS (ftype) = nargs;
4232 if (nargs == 0)
4233 {
d2e4a39e 4234 static struct field dummy_field = { 0, 0, 0, 0 };
aacb1f0a 4235 xfree (TYPE_FIELDS (ftype));
14f9c5c9
AS
4236 TYPE_FIELDS (ftype) = &dummy_field;
4237 }
4238 else
4239 {
d2e4a39e
AS
4240 struct field *fields =
4241 (struct field *) TYPE_ALLOC (ftype, nargs * sizeof (struct field));
4242 memcpy ((char *) fields,
4243 (char *) TYPE_FIELDS (ftype), nargs * sizeof (struct field));
aacb1f0a 4244 xfree (TYPE_FIELDS (ftype));
14f9c5c9
AS
4245 TYPE_FIELDS (ftype) = fields;
4246 }
4247}
14f9c5c9 4248\f
d2e4a39e 4249
14f9c5c9
AS
4250 /* Breakpoint-related */
4251
d2e4a39e
AS
4252char no_symtab_msg[] =
4253 "No symbol table is loaded. Use the \"file\" command.";
14f9c5c9
AS
4254
4255/* Assuming that LINE is pointing at the beginning of an argument to
4256 'break', return a pointer to the delimiter for the initial segment
4257 of that name. This is the first ':', ' ', or end of LINE.
4258*/
d2e4a39e
AS
4259char *
4260ada_start_decode_line_1 (char *line)
14f9c5c9
AS
4261{
4262 /* [NOTE: strpbrk would be more elegant, but I am reluctant to be
4263 the first to use such a library function in GDB code.] */
d2e4a39e 4264 char *p;
14f9c5c9
AS
4265 for (p = line; *p != '\000' && *p != ' ' && *p != ':'; p += 1)
4266 ;
4267 return p;
4268}
4269
4270/* *SPEC points to a function and line number spec (as in a break
4271 command), following any initial file name specification.
4272
4273 Return all symbol table/line specfications (sals) consistent with the
4274 information in *SPEC and FILE_TABLE in the
4275 following sense:
4276 + FILE_TABLE is null, or the sal refers to a line in the file
4277 named by FILE_TABLE.
4278 + If *SPEC points to an argument with a trailing ':LINENUM',
4279 then the sal refers to that line (or one following it as closely as
4280 possible).
4281 + If *SPEC does not start with '*', the sal is in a function with
4282 that name.
4283
4284 Returns with 0 elements if no matching non-minimal symbols found.
4285
4286 If *SPEC begins with a function name of the form <NAME>, then NAME
4287 is taken as a literal name; otherwise the function name is subject
4288 to the usual mangling.
4289
4290 *SPEC is updated to point after the function/line number specification.
4291
4292 FUNFIRSTLINE is non-zero if we desire the first line of real code
4293 in each function (this is ignored in the presence of a LINENUM spec.).
4294
4295 If CANONICAL is non-NULL, and if any of the sals require a
4296 'canonical line spec', then *CANONICAL is set to point to an array
4297 of strings, corresponding to and equal in length to the returned
4298 list of sals, such that (*CANONICAL)[i] is non-null and contains a
4299 canonical line spec for the ith returned sal, if needed. If no
4300 canonical line specs are required and CANONICAL is non-null,
4301 *CANONICAL is set to NULL.
4302
4303 A 'canonical line spec' is simply a name (in the format of the
4304 breakpoint command) that uniquely identifies a breakpoint position,
4305 with no further contextual information or user selection. It is
4306 needed whenever the file name, function name, and line number
4307 information supplied is insufficient for this unique
4308 identification. Currently overloaded functions, the name '*',
4309 or static functions without a filename yield a canonical line spec.
4310 The array and the line spec strings are allocated on the heap; it
4311 is the caller's responsibility to free them. */
4312
4313struct symtabs_and_lines
d2e4a39e
AS
4314ada_finish_decode_line_1 (char **spec, struct symtab *file_table,
4315 int funfirstline, char ***canonical)
14f9c5c9 4316{
d2e4a39e
AS
4317 struct symbol **symbols;
4318 struct block **blocks;
4319 struct block *block;
14f9c5c9
AS
4320 int n_matches, i, line_num;
4321 struct symtabs_and_lines selected;
d2e4a39e
AS
4322 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
4323 char *name;
14f9c5c9
AS
4324
4325 int len;
d2e4a39e
AS
4326 char *lower_name;
4327 char *unquoted_name;
14f9c5c9
AS
4328
4329 if (file_table == NULL)
4330 block = get_selected_block (NULL);
4331 else
4332 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_table), STATIC_BLOCK);
4333
4334 if (canonical != NULL)
d2e4a39e 4335 *canonical = (char **) NULL;
14f9c5c9
AS
4336
4337 name = *spec;
d2e4a39e 4338 if (**spec == '*')
14f9c5c9
AS
4339 *spec += 1;
4340 else
4341 {
d2e4a39e
AS
4342 while (**spec != '\000' &&
4343 !strchr (ada_completer_word_break_characters, **spec))
14f9c5c9
AS
4344 *spec += 1;
4345 }
4346 len = *spec - name;
4347
4348 line_num = -1;
4349 if (file_table != NULL && (*spec)[0] == ':' && isdigit ((*spec)[1]))
4350 {
4351 line_num = strtol (*spec + 1, spec, 10);
d2e4a39e 4352 while (**spec == ' ' || **spec == '\t')
14f9c5c9
AS
4353 *spec += 1;
4354 }
4355
d2e4a39e 4356 if (name[0] == '*')
14f9c5c9
AS
4357 {
4358 if (line_num == -1)
4359 error ("Wild-card function with no line number or file name.");
4360
4361 return all_sals_for_line (file_table->filename, line_num, canonical);
4362 }
4363
4364 if (name[0] == '\'')
4365 {
4366 name += 1;
4367 len -= 2;
4368 }
4369
4370 if (name[0] == '<')
4371 {
d2e4a39e
AS
4372 unquoted_name = (char *) alloca (len - 1);
4373 memcpy (unquoted_name, name + 1, len - 2);
4374 unquoted_name[len - 2] = '\000';
14f9c5c9
AS
4375 lower_name = NULL;
4376 }
4377 else
4378 {
d2e4a39e 4379 unquoted_name = (char *) alloca (len + 1);
14f9c5c9
AS
4380 memcpy (unquoted_name, name, len);
4381 unquoted_name[len] = '\000';
d2e4a39e 4382 lower_name = (char *) alloca (len + 1);
14f9c5c9
AS
4383 for (i = 0; i < len; i += 1)
4384 lower_name[i] = tolower (name[i]);
4385 lower_name[len] = '\000';
4386 }
4387
4388 n_matches = 0;
d2e4a39e
AS
4389 if (lower_name != NULL)
4390 n_matches = ada_lookup_symbol_list (ada_mangle (lower_name), block,
14f9c5c9
AS
4391 VAR_NAMESPACE, &symbols, &blocks);
4392 if (n_matches == 0)
d2e4a39e 4393 n_matches = ada_lookup_symbol_list (unquoted_name, block,
14f9c5c9
AS
4394 VAR_NAMESPACE, &symbols, &blocks);
4395 if (n_matches == 0 && line_num >= 0)
4396 error ("No line number information found for %s.", unquoted_name);
4397 else if (n_matches == 0)
4398 {
4399#ifdef HPPA_COMPILER_BUG
4400 /* FIXME: See comment in symtab.c::decode_line_1 */
4401#undef volatile
4402 volatile struct symtab_and_line val;
d2e4a39e 4403#define volatile /*nothing */
14f9c5c9
AS
4404#else
4405 struct symtab_and_line val;
4406#endif
d2e4a39e 4407 struct minimal_symbol *msymbol;
14f9c5c9 4408
fe39c653 4409 init_sal (&val);
14f9c5c9
AS
4410
4411 msymbol = NULL;
d2e4a39e 4412 if (lower_name != NULL)
14f9c5c9
AS
4413 msymbol = ada_lookup_minimal_symbol (ada_mangle (lower_name));
4414 if (msymbol == NULL)
4415 msymbol = ada_lookup_minimal_symbol (unquoted_name);
4416 if (msymbol != NULL)
4417 {
d2e4a39e 4418 val.pc = SYMBOL_VALUE_ADDRESS (msymbol);
14f9c5c9
AS
4419 val.section = SYMBOL_BFD_SECTION (msymbol);
4420 if (funfirstline)
4421 {
4422 val.pc += FUNCTION_START_OFFSET;
4423 SKIP_PROLOGUE (val.pc);
4424 }
4425 selected.sals = (struct symtab_and_line *)
4426 xmalloc (sizeof (struct symtab_and_line));
4427 selected.sals[0] = val;
4428 selected.nelts = 1;
4429 return selected;
d2e4a39e
AS
4430 }
4431
14f9c5c9
AS
4432 if (!have_full_symbols () &&
4433 !have_partial_symbols () && !have_minimal_symbols ())
4434 error (no_symtab_msg);
4435
4436 error ("Function \"%s\" not defined.", unquoted_name);
d2e4a39e 4437 return selected; /* for lint */
14f9c5c9
AS
4438 }
4439
4440 if (line_num >= 0)
4441 {
d2e4a39e
AS
4442 return
4443 find_sal_from_funcs_and_line (file_table->filename, line_num,
14f9c5c9
AS
4444 symbols, n_matches);
4445 }
4446 else
4447 {
d2e4a39e
AS
4448 selected.nelts =
4449 user_select_syms (symbols, blocks, n_matches, n_matches);
14f9c5c9
AS
4450 }
4451
d2e4a39e 4452 selected.sals = (struct symtab_and_line *)
14f9c5c9
AS
4453 xmalloc (sizeof (struct symtab_and_line) * selected.nelts);
4454 memset (selected.sals, 0, selected.nelts * sizeof (selected.sals[i]));
aacb1f0a 4455 make_cleanup (xfree, selected.sals);
14f9c5c9
AS
4456
4457 i = 0;
4458 while (i < selected.nelts)
4459 {
d2e4a39e 4460 if (SYMBOL_CLASS (symbols[i]) == LOC_BLOCK)
14f9c5c9 4461 selected.sals[i] = find_function_start_sal (symbols[i], funfirstline);
d2e4a39e 4462 else if (SYMBOL_LINE (symbols[i]) != 0)
14f9c5c9
AS
4463 {
4464 selected.sals[i].symtab = symtab_for_sym (symbols[i]);
4465 selected.sals[i].line = SYMBOL_LINE (symbols[i]);
4466 }
4467 else if (line_num >= 0)
4468 {
4469 /* Ignore this choice */
d2e4a39e
AS
4470 symbols[i] = symbols[selected.nelts - 1];
4471 blocks[i] = blocks[selected.nelts - 1];
14f9c5c9
AS
4472 selected.nelts -= 1;
4473 continue;
4474 }
d2e4a39e 4475 else
14f9c5c9
AS
4476 error ("Line number not known for symbol \"%s\"", unquoted_name);
4477 i += 1;
4478 }
4479
4480 if (canonical != NULL && (line_num >= 0 || n_matches > 1))
4481 {
d2e4a39e 4482 *canonical = (char **) xmalloc (sizeof (char *) * selected.nelts);
14f9c5c9 4483 for (i = 0; i < selected.nelts; i += 1)
d2e4a39e
AS
4484 (*canonical)[i] =
4485 extended_canonical_line_spec (selected.sals[i],
de5ad195 4486 SYMBOL_PRINT_NAME (symbols[i]));
14f9c5c9 4487 }
d2e4a39e 4488
14f9c5c9
AS
4489 discard_cleanups (old_chain);
4490 return selected;
d2e4a39e
AS
4491}
4492
14f9c5c9
AS
4493/* The (single) sal corresponding to line LINE_NUM in a symbol table
4494 with file name FILENAME that occurs in one of the functions listed
d2e4a39e 4495 in SYMBOLS[0 .. NSYMS-1]. */
14f9c5c9 4496static struct symtabs_and_lines
d2e4a39e
AS
4497find_sal_from_funcs_and_line (const char *filename, int line_num,
4498 struct symbol **symbols, int nsyms)
14f9c5c9
AS
4499{
4500 struct symtabs_and_lines sals;
4501 int best_index, best;
d2e4a39e
AS
4502 struct linetable *best_linetable;
4503 struct objfile *objfile;
4504 struct symtab *s;
4505 struct symtab *best_symtab;
14f9c5c9
AS
4506
4507 read_all_symtabs (filename);
4508
d2e4a39e
AS
4509 best_index = 0;
4510 best_linetable = NULL;
4511 best_symtab = NULL;
14f9c5c9
AS
4512 best = 0;
4513 ALL_SYMTABS (objfile, s)
d2e4a39e
AS
4514 {
4515 struct linetable *l;
4516 int ind, exact;
14f9c5c9 4517
d2e4a39e 4518 QUIT;
14f9c5c9 4519
d2e4a39e
AS
4520 if (!STREQ (filename, s->filename))
4521 continue;
4522 l = LINETABLE (s);
4523 ind = find_line_in_linetable (l, line_num, symbols, nsyms, &exact);
4524 if (ind >= 0)
4525 {
4526 if (exact)
4527 {
4528 best_index = ind;
4529 best_linetable = l;
4530 best_symtab = s;
4531 goto done;
4532 }
4533 if (best == 0 || l->item[ind].line < best)
4534 {
4535 best = l->item[ind].line;
4536 best_index = ind;
4537 best_linetable = l;
4538 best_symtab = s;
4539 }
4540 }
4541 }
14f9c5c9
AS
4542
4543 if (best == 0)
4544 error ("Line number not found in designated function.");
4545
d2e4a39e
AS
4546done:
4547
14f9c5c9 4548 sals.nelts = 1;
d2e4a39e 4549 sals.sals = (struct symtab_and_line *) xmalloc (sizeof (sals.sals[0]));
14f9c5c9 4550
fe39c653 4551 init_sal (&sals.sals[0]);
d2e4a39e 4552
14f9c5c9
AS
4553 sals.sals[0].line = best_linetable->item[best_index].line;
4554 sals.sals[0].pc = best_linetable->item[best_index].pc;
4555 sals.sals[0].symtab = best_symtab;
4556
4557 return sals;
4558}
4559
4560/* Return the index in LINETABLE of the best match for LINE_NUM whose
4561 pc falls within one of the functions denoted by SYMBOLS[0..NSYMS-1].
4562 Set *EXACTP to the 1 if the match is exact, and 0 otherwise. */
4563static int
d2e4a39e
AS
4564find_line_in_linetable (struct linetable *linetable, int line_num,
4565 struct symbol **symbols, int nsyms, int *exactp)
14f9c5c9
AS
4566{
4567 int i, len, best_index, best;
4568
4569 if (line_num <= 0 || linetable == NULL)
4570 return -1;
4571
4572 len = linetable->nitems;
4573 for (i = 0, best_index = -1, best = 0; i < len; i += 1)
4574 {
4575 int k;
d2e4a39e 4576 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
4577
4578 for (k = 0; k < nsyms; k += 1)
4579 {
4580 if (symbols[k] != NULL && SYMBOL_CLASS (symbols[k]) == LOC_BLOCK
4581 && item->pc >= BLOCK_START (SYMBOL_BLOCK_VALUE (symbols[k]))
4582 && item->pc < BLOCK_END (SYMBOL_BLOCK_VALUE (symbols[k])))
4583 goto candidate;
4584 }
4585 continue;
4586
4587 candidate:
4588
4589 if (item->line == line_num)
4590 {
4591 *exactp = 1;
4592 return i;
4593 }
4594
4595 if (item->line > line_num && (best == 0 || item->line < best))
4596 {
4597 best = item->line;
4598 best_index = i;
4599 }
4600 }
4601
4602 *exactp = 0;
4603 return best_index;
4604}
4605
4606/* Find the smallest k >= LINE_NUM such that k is a line number in
4607 LINETABLE, and k falls strictly within a named function that begins at
4608 or before LINE_NUM. Return -1 if there is no such k. */
4609static int
d2e4a39e 4610nearest_line_number_in_linetable (struct linetable *linetable, int line_num)
14f9c5c9
AS
4611{
4612 int i, len, best;
4613
4614 if (line_num <= 0 || linetable == NULL || linetable->nitems == 0)
4615 return -1;
4616 len = linetable->nitems;
4617
d2e4a39e
AS
4618 i = 0;
4619 best = INT_MAX;
14f9c5c9
AS
4620 while (i < len)
4621 {
4622 int k;
d2e4a39e 4623 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
4624
4625 if (item->line >= line_num && item->line < best)
4626 {
d2e4a39e 4627 char *func_name;
14f9c5c9
AS
4628 CORE_ADDR start, end;
4629
4630 func_name = NULL;
4631 find_pc_partial_function (item->pc, &func_name, &start, &end);
4632
4633 if (func_name != NULL && item->pc < end)
4634 {
4635 if (item->line == line_num)
4636 return line_num;
d2e4a39e 4637 else
14f9c5c9 4638 {
d2e4a39e 4639 struct symbol *sym =
14f9c5c9
AS
4640 standard_lookup (func_name, VAR_NAMESPACE);
4641 if (is_plausible_func_for_line (sym, line_num))
4642 best = item->line;
4643 else
4644 {
4645 do
4646 i += 1;
4647 while (i < len && linetable->item[i].pc < end);
4648 continue;
4649 }
4650 }
4651 }
4652 }
4653
4654 i += 1;
4655 }
4656
4657 return (best == INT_MAX) ? -1 : best;
4658}
4659
4660
4661/* Return the next higher index, k, into LINETABLE such that k > IND,
4662 entry k in LINETABLE has a line number equal to LINE_NUM, k
4663 corresponds to a PC that is in a function different from that
4664 corresponding to IND, and falls strictly within a named function
4665 that begins at a line at or preceding STARTING_LINE.
4666 Return -1 if there is no such k.
4667 IND == -1 corresponds to no function. */
4668
4669static int
d2e4a39e 4670find_next_line_in_linetable (struct linetable *linetable, int line_num,
ebf56fd3 4671 int starting_line, int ind)
14f9c5c9
AS
4672{
4673 int i, len;
4674
4675 if (line_num <= 0 || linetable == NULL || ind >= linetable->nitems)
4676 return -1;
4677 len = linetable->nitems;
4678
d2e4a39e 4679 if (ind >= 0)
14f9c5c9
AS
4680 {
4681 CORE_ADDR start, end;
4682
4683 if (find_pc_partial_function (linetable->item[ind].pc,
d2e4a39e 4684 (char **) NULL, &start, &end))
14f9c5c9
AS
4685 {
4686 while (ind < len && linetable->item[ind].pc < end)
4687 ind += 1;
4688 }
4689 else
4690 ind += 1;
4691 }
4692 else
4693 ind = 0;
4694
4695 i = ind;
4696 while (i < len)
4697 {
4698 int k;
d2e4a39e 4699 struct linetable_entry *item = &(linetable->item[i]);
14f9c5c9
AS
4700
4701 if (item->line >= line_num)
4702 {
d2e4a39e 4703 char *func_name;
14f9c5c9
AS
4704 CORE_ADDR start, end;
4705
4706 func_name = NULL;
4707 find_pc_partial_function (item->pc, &func_name, &start, &end);
4708
4709 if (func_name != NULL && item->pc < end)
4710 {
4711 if (item->line == line_num)
4712 {
d2e4a39e 4713 struct symbol *sym =
14f9c5c9
AS
4714 standard_lookup (func_name, VAR_NAMESPACE);
4715 if (is_plausible_func_for_line (sym, starting_line))
4716 return i;
4717 else
4718 {
d2e4a39e 4719 while ((i + 1) < len && linetable->item[i + 1].pc < end)
14f9c5c9
AS
4720 i += 1;
4721 }
4722 }
4723 }
4724 }
4725 i += 1;
4726 }
4727
4728 return -1;
4729}
4730
4731/* True iff function symbol SYM starts somewhere at or before line #
4732 LINE_NUM. */
4733static int
d2e4a39e 4734is_plausible_func_for_line (struct symbol *sym, int line_num)
14f9c5c9
AS
4735{
4736 struct symtab_and_line start_sal;
4737
4738 if (sym == NULL)
4739 return 0;
4740
4741 start_sal = find_function_start_sal (sym, 0);
4742
4743 return (start_sal.line != 0 && line_num >= start_sal.line);
4744}
4745
4746static void
d2e4a39e 4747debug_print_lines (struct linetable *lt)
14f9c5c9
AS
4748{
4749 int i;
4750
d2e4a39e 4751 if (lt == NULL)
14f9c5c9
AS
4752 return;
4753
4754 fprintf (stderr, "\t");
4755 for (i = 0; i < lt->nitems; i += 1)
4756 fprintf (stderr, "(%d->%p) ", lt->item[i].line, (void *) lt->item[i].pc);
4757 fprintf (stderr, "\n");
4758}
4759
4760static void
d2e4a39e 4761debug_print_block (struct block *b)
14f9c5c9
AS
4762{
4763 int i;
261397f8
DJ
4764 struct symbol *i;
4765
d2e4a39e
AS
4766 fprintf (stderr, "Block: %p; [0x%lx, 0x%lx]",
4767 b, BLOCK_START (b), BLOCK_END (b));
4768 if (BLOCK_FUNCTION (b) != NULL)
22abf04a 4769 fprintf (stderr, " Function: %s", DEPRECATED_SYMBOL_NAME (BLOCK_FUNCTION (b)));
14f9c5c9 4770 fprintf (stderr, "\n");
d2e4a39e 4771 fprintf (stderr, "\t Superblock: %p\n", BLOCK_SUPERBLOCK (b));
14f9c5c9 4772 fprintf (stderr, "\t Symbols:");
261397f8 4773 ALL_BLOCK_SYMBOLS (b, i, sym)
d2e4a39e
AS
4774 {
4775 if (i > 0 && i % 4 == 0)
4776 fprintf (stderr, "\n\t\t ");
22abf04a 4777 fprintf (stderr, " %s", DEPRECATED_SYMBOL_NAME (sym));
d2e4a39e 4778 }
14f9c5c9
AS
4779 fprintf (stderr, "\n");
4780}
4781
4782static void
d2e4a39e 4783debug_print_blocks (struct blockvector *bv)
14f9c5c9
AS
4784{
4785 int i;
4786
4787 if (bv == NULL)
4788 return;
d2e4a39e
AS
4789 for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); i += 1)
4790 {
4791 fprintf (stderr, "%6d. ", i);
4792 debug_print_block (BLOCKVECTOR_BLOCK (bv, i));
4793 }
14f9c5c9
AS
4794}
4795
4796static void
d2e4a39e 4797debug_print_symtab (struct symtab *s)
14f9c5c9
AS
4798{
4799 fprintf (stderr, "Symtab %p\n File: %s; Dir: %s\n", s,
4800 s->filename, s->dirname);
4801 fprintf (stderr, " Blockvector: %p, Primary: %d\n",
d2e4a39e
AS
4802 BLOCKVECTOR (s), s->primary);
4803 debug_print_blocks (BLOCKVECTOR (s));
14f9c5c9 4804 fprintf (stderr, " Line table: %p\n", LINETABLE (s));
d2e4a39e 4805 debug_print_lines (LINETABLE (s));
14f9c5c9
AS
4806}
4807
4808/* Read in all symbol tables corresponding to partial symbol tables
4809 with file name FILENAME. */
4810static void
d2e4a39e 4811read_all_symtabs (const char *filename)
14f9c5c9 4812{
d2e4a39e
AS
4813 struct partial_symtab *ps;
4814 struct objfile *objfile;
14f9c5c9
AS
4815
4816 ALL_PSYMTABS (objfile, ps)
d2e4a39e
AS
4817 {
4818 QUIT;
14f9c5c9 4819
d2e4a39e
AS
4820 if (STREQ (filename, ps->filename))
4821 PSYMTAB_TO_SYMTAB (ps);
4822 }
14f9c5c9
AS
4823}
4824
4825/* All sals corresponding to line LINE_NUM in a symbol table from file
4826 FILENAME, as filtered by the user. If CANONICAL is not null, set
4827 it to a corresponding array of canonical line specs. */
4828static struct symtabs_and_lines
d2e4a39e 4829all_sals_for_line (const char *filename, int line_num, char ***canonical)
14f9c5c9
AS
4830{
4831 struct symtabs_and_lines result;
d2e4a39e
AS
4832 struct objfile *objfile;
4833 struct symtab *s;
4834 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
14f9c5c9
AS
4835 size_t len;
4836
4837 read_all_symtabs (filename);
4838
d2e4a39e
AS
4839 result.sals =
4840 (struct symtab_and_line *) xmalloc (4 * sizeof (result.sals[0]));
14f9c5c9
AS
4841 result.nelts = 0;
4842 len = 4;
4843 make_cleanup (free_current_contents, &result.sals);
4844
d2e4a39e
AS
4845 ALL_SYMTABS (objfile, s)
4846 {
4847 int ind, target_line_num;
14f9c5c9 4848
d2e4a39e 4849 QUIT;
14f9c5c9 4850
d2e4a39e
AS
4851 if (!STREQ (s->filename, filename))
4852 continue;
14f9c5c9 4853
d2e4a39e
AS
4854 target_line_num =
4855 nearest_line_number_in_linetable (LINETABLE (s), line_num);
4856 if (target_line_num == -1)
4857 continue;
14f9c5c9 4858
d2e4a39e
AS
4859 ind = -1;
4860 while (1)
4861 {
4862 ind =
4863 find_next_line_in_linetable (LINETABLE (s),
4864 target_line_num, line_num, ind);
14f9c5c9 4865
d2e4a39e
AS
4866 if (ind < 0)
4867 break;
4868
4869 GROW_VECT (result.sals, len, result.nelts + 1);
fe39c653 4870 init_sal (&result.sals[result.nelts]);
d2e4a39e
AS
4871 result.sals[result.nelts].line = LINETABLE (s)->item[ind].line;
4872 result.sals[result.nelts].pc = LINETABLE (s)->item[ind].pc;
4873 result.sals[result.nelts].symtab = s;
4874 result.nelts += 1;
4875 }
4876 }
14f9c5c9
AS
4877
4878 if (canonical != NULL || result.nelts > 1)
4879 {
4880 int k;
d2e4a39e 4881 char **func_names = (char **) alloca (result.nelts * sizeof (char *));
14f9c5c9
AS
4882 int first_choice = (result.nelts > 1) ? 2 : 1;
4883 int n;
d2e4a39e
AS
4884 int *choices = (int *) alloca (result.nelts * sizeof (int));
4885
4886 for (k = 0; k < result.nelts; k += 1)
14f9c5c9 4887 {
d2e4a39e
AS
4888 find_pc_partial_function (result.sals[k].pc, &func_names[k],
4889 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
14f9c5c9
AS
4890 if (func_names[k] == NULL)
4891 error ("Could not find function for one or more breakpoints.");
4892 }
d2e4a39e
AS
4893
4894 if (result.nelts > 1)
14f9c5c9 4895 {
d2e4a39e
AS
4896 printf_unfiltered ("[0] cancel\n");
4897 if (result.nelts > 1)
4898 printf_unfiltered ("[1] all\n");
14f9c5c9 4899 for (k = 0; k < result.nelts; k += 1)
d2e4a39e 4900 printf_unfiltered ("[%d] %s\n", k + first_choice,
14f9c5c9 4901 ada_demangle (func_names[k]));
d2e4a39e 4902
14f9c5c9
AS
4903 n = get_selections (choices, result.nelts, result.nelts,
4904 result.nelts > 1, "instance-choice");
d2e4a39e
AS
4905
4906 for (k = 0; k < n; k += 1)
14f9c5c9
AS
4907 {
4908 result.sals[k] = result.sals[choices[k]];
4909 func_names[k] = func_names[choices[k]];
4910 }
4911 result.nelts = n;
4912 }
4913
d2e4a39e 4914 if (canonical != NULL)
14f9c5c9 4915 {
d2e4a39e 4916 *canonical = (char **) xmalloc (result.nelts * sizeof (char **));
aacb1f0a 4917 make_cleanup (xfree, *canonical);
d2e4a39e 4918 for (k = 0; k < result.nelts; k += 1)
14f9c5c9 4919 {
d2e4a39e 4920 (*canonical)[k] =
14f9c5c9
AS
4921 extended_canonical_line_spec (result.sals[k], func_names[k]);
4922 if ((*canonical)[k] == NULL)
4923 error ("Could not locate one or more breakpoints.");
aacb1f0a 4924 make_cleanup (xfree, (*canonical)[k]);
14f9c5c9
AS
4925 }
4926 }
4927 }
4928
4929 discard_cleanups (old_chain);
4930 return result;
4931}
4932
4933
4934/* A canonical line specification of the form FILE:NAME:LINENUM for
4935 symbol table and line data SAL. NULL if insufficient
4936 information. The caller is responsible for releasing any space
4937 allocated. */
4938
d2e4a39e
AS
4939static char *
4940extended_canonical_line_spec (struct symtab_and_line sal, const char *name)
14f9c5c9 4941{
d2e4a39e 4942 char *r;
14f9c5c9 4943
d2e4a39e 4944 if (sal.symtab == NULL || sal.symtab->filename == NULL || sal.line <= 0)
14f9c5c9
AS
4945 return NULL;
4946
d2e4a39e
AS
4947 r = (char *) xmalloc (strlen (name) + strlen (sal.symtab->filename)
4948 + sizeof (sal.line) * 3 + 3);
14f9c5c9
AS
4949 sprintf (r, "%s:'%s':%d", sal.symtab->filename, name, sal.line);
4950 return r;
4951}
4952
4953#if 0
4954int begin_bnum = -1;
4955#endif
4956int begin_annotate_level = 0;
4957
d2e4a39e
AS
4958static void
4959begin_cleanup (void *dummy)
14f9c5c9
AS
4960{
4961 begin_annotate_level = 0;
4962}
4963
4964static void
ebf56fd3 4965begin_command (char *args, int from_tty)
14f9c5c9
AS
4966{
4967 struct minimal_symbol *msym;
4968 CORE_ADDR main_program_name_addr;
4969 char main_program_name[1024];
d2e4a39e 4970 struct cleanup *old_chain = make_cleanup (begin_cleanup, NULL);
14f9c5c9
AS
4971 begin_annotate_level = 2;
4972
4973 /* Check that there is a program to debug */
4974 if (!have_full_symbols () && !have_partial_symbols ())
4975 error ("No symbol table is loaded. Use the \"file\" command.");
d2e4a39e 4976
14f9c5c9
AS
4977 /* Check that we are debugging an Ada program */
4978 /* if (ada_update_initial_language (language_unknown, NULL) != language_ada)
d2e4a39e
AS
4979 error ("Cannot find the Ada initialization procedure. Is this an Ada main program?");
4980 */
14f9c5c9
AS
4981 /* FIXME: language_ada should be defined in defs.h */
4982
4983 /* Get the address of the name of the main procedure */
4984 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
4985
4986 if (msym != NULL)
d2e4a39e
AS
4987 {
4988 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
4989 if (main_program_name_addr == 0)
4990 error ("Invalid address for Ada main program name.");
14f9c5c9 4991
d2e4a39e
AS
4992 /* Read the name of the main procedure */
4993 extract_string (main_program_name_addr, main_program_name);
14f9c5c9 4994
d2e4a39e
AS
4995 /* Put a temporary breakpoint in the Ada main program and run */
4996 do_command ("tbreak ", main_program_name, 0);
4997 do_command ("run ", args, 0);
4998 }
14f9c5c9 4999 else
d2e4a39e
AS
5000 {
5001 /* If we could not find the symbol containing the name of the
5002 main program, that means that the compiler that was used to build
5003 was not recent enough. In that case, we fallback to the previous
5004 mechanism, which is a little bit less reliable, but has proved to work
5005 in most cases. The only cases where it will fail is when the user
5006 has set some breakpoints which will be hit before the end of the
5007 begin command processing (eg in the initialization code).
5008
5009 The begining of the main Ada subprogram is located by breaking
5010 on the adainit procedure. Since we know that the binder generates
5011 the call to this procedure exactly 2 calls before the call to the
5012 Ada main subprogram, it is then easy to put a breakpoint on this
5013 Ada main subprogram once we hit adainit.
5014 */
5015 do_command ("tbreak adainit", 0);
5016 do_command ("run ", args, 0);
5017 do_command ("up", 0);
5018 do_command ("tbreak +2", 0);
5019 do_command ("continue", 0);
5020 do_command ("step", 0);
5021 }
14f9c5c9
AS
5022
5023 do_cleanups (old_chain);
5024}
5025
5026int
ebf56fd3 5027is_ada_runtime_file (char *filename)
14f9c5c9
AS
5028{
5029 return (STREQN (filename, "s-", 2) ||
5030 STREQN (filename, "a-", 2) ||
d2e4a39e 5031 STREQN (filename, "g-", 2) || STREQN (filename, "i-", 2));
14f9c5c9
AS
5032}
5033
5034/* find the first frame that contains debugging information and that is not
5035 part of the Ada run-time, starting from fi and moving upward. */
5036
5037int
ebf56fd3 5038find_printable_frame (struct frame_info *fi, int level)
14f9c5c9
AS
5039{
5040 struct symtab_and_line sal;
d2e4a39e 5041
14f9c5c9
AS
5042 for (; fi != NULL; level += 1, fi = get_prev_frame (fi))
5043 {
1058bca7 5044 find_frame_sal (fi, &sal);
14f9c5c9
AS
5045 if (sal.symtab && !is_ada_runtime_file (sal.symtab->filename))
5046 {
5047#if defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET)
d2e4a39e
AS
5048 /* libpthread.so contains some debugging information that prevents us
5049 from finding the right frame */
14f9c5c9
AS
5050
5051 if (sal.symtab->objfile &&
5052 STREQ (sal.symtab->objfile->name, "/usr/shlib/libpthread.so"))
d2e4a39e 5053 continue;
14f9c5c9 5054#endif
6e7f8b9c 5055 deprecated_selected_frame = fi;
14f9c5c9
AS
5056 break;
5057 }
5058 }
5059
5060 return level;
5061}
5062
5063void
ebf56fd3 5064ada_report_exception_break (struct breakpoint *b)
14f9c5c9 5065{
14f9c5c9
AS
5066 /* FIXME: break_on_exception should be defined in breakpoint.h */
5067 /* if (b->break_on_exception == 1)
d2e4a39e
AS
5068 {
5069 /* Assume that cond has 16 elements, the 15th
5070 being the exception *//*
5071 if (b->cond && b->cond->nelts == 16)
5072 {
5073 ui_out_text (uiout, "on ");
5074 ui_out_field_string (uiout, "exception",
5075 SYMBOL_NAME (b->cond->elts[14].symbol));
5076 }
5077 else
5078 ui_out_text (uiout, "on all exceptions");
5079 }
5080 else if (b->break_on_exception == 2)
5081 ui_out_text (uiout, "on unhandled exception");
5082 else if (b->break_on_exception == 3)
5083 ui_out_text (uiout, "on assert failure");
5084 #else
5085 if (b->break_on_exception == 1)
5086 { */
5087 /* Assume that cond has 16 elements, the 15th
5088 being the exception *//*
5089 if (b->cond && b->cond->nelts == 16)
5090 {
5091 fputs_filtered ("on ", gdb_stdout);
5092 fputs_filtered (SYMBOL_NAME
5093 (b->cond->elts[14].symbol), gdb_stdout);
5094 }
5095 else
5096 fputs_filtered ("on all exceptions", gdb_stdout);
5097 }
5098 else if (b->break_on_exception == 2)
5099 fputs_filtered ("on unhandled exception", gdb_stdout);
5100 else if (b->break_on_exception == 3)
5101 fputs_filtered ("on assert failure", gdb_stdout);
5102 */
14f9c5c9
AS
5103}
5104
5105int
d2e4a39e 5106ada_is_exception_sym (struct symbol *sym)
14f9c5c9
AS
5107{
5108 char *type_name = type_name_no_tag (SYMBOL_TYPE (sym));
d2e4a39e 5109
14f9c5c9
AS
5110 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
5111 && SYMBOL_CLASS (sym) != LOC_BLOCK
5112 && SYMBOL_CLASS (sym) != LOC_CONST
d2e4a39e 5113 && type_name != NULL && STREQ (type_name, "exception"));
14f9c5c9
AS
5114}
5115
5116int
d2e4a39e 5117ada_maybe_exception_partial_symbol (struct partial_symbol *sym)
14f9c5c9
AS
5118{
5119 return (SYMBOL_CLASS (sym) != LOC_TYPEDEF
5120 && SYMBOL_CLASS (sym) != LOC_BLOCK
5121 && SYMBOL_CLASS (sym) != LOC_CONST);
5122}
5123
5124/* If ARG points to an Ada exception or assert breakpoint, rewrite
5125 into equivalent form. Return resulting argument string. Set
5126 *BREAK_ON_EXCEPTIONP to 1 for ordinary break on exception, 2 for
5127 break on unhandled, 3 for assert, 0 otherwise. */
d2e4a39e
AS
5128char *
5129ada_breakpoint_rewrite (char *arg, int *break_on_exceptionp)
14f9c5c9
AS
5130{
5131 if (arg == NULL)
5132 return arg;
5133 *break_on_exceptionp = 0;
d2e4a39e 5134 /* FIXME: language_ada should be defined in defs.h */
14f9c5c9 5135 /* if (current_language->la_language == language_ada
d2e4a39e
AS
5136 && STREQN (arg, "exception", 9) &&
5137 (arg[9] == ' ' || arg[9] == '\t' || arg[9] == '\0'))
5138 {
5139 char *tok, *end_tok;
5140 int toklen;
5141
5142 *break_on_exceptionp = 1;
5143
5144 tok = arg+9;
5145 while (*tok == ' ' || *tok == '\t')
5146 tok += 1;
5147
5148 end_tok = tok;
5149
5150 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5151 end_tok += 1;
5152
5153 toklen = end_tok - tok;
5154
5155 arg = (char*) xmalloc (sizeof ("__gnat_raise_nodefer_with_msg if "
5156 "long_integer(e) = long_integer(&)")
5157 + toklen + 1);
5158 make_cleanup (xfree, arg);
5159 if (toklen == 0)
5160 strcpy (arg, "__gnat_raise_nodefer_with_msg");
5161 else if (STREQN (tok, "unhandled", toklen))
5162 {
5163 *break_on_exceptionp = 2;
5164 strcpy (arg, "__gnat_unhandled_exception");
5165 }
5166 else
5167 {
5168 sprintf (arg, "__gnat_raise_nodefer_with_msg if "
5169 "long_integer(e) = long_integer(&%.*s)",
5170 toklen, tok);
5171 }
5172 }
5173 else if (current_language->la_language == language_ada
5174 && STREQN (arg, "assert", 6) &&
5175 (arg[6] == ' ' || arg[6] == '\t' || arg[6] == '\0'))
5176 {
5177 char *tok = arg + 6;
5178
5179 *break_on_exceptionp = 3;
5180
5181 arg = (char*)
5182 xmalloc (sizeof ("system__assertions__raise_assert_failure")
5183 + strlen (tok) + 1);
5184 make_cleanup (xfree, arg);
5185 sprintf (arg, "system__assertions__raise_assert_failure%s", tok);
5186 }
5187 */
14f9c5c9
AS
5188 return arg;
5189}
14f9c5c9 5190\f
d2e4a39e 5191
14f9c5c9
AS
5192 /* Field Access */
5193
5194/* True if field number FIELD_NUM in struct or union type TYPE is supposed
5195 to be invisible to users. */
5196
5197int
ebf56fd3 5198ada_is_ignored_field (struct type *type, int field_num)
14f9c5c9
AS
5199{
5200 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
5201 return 1;
d2e4a39e 5202 else
14f9c5c9 5203 {
d2e4a39e 5204 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9 5205 return (name == NULL
d2e4a39e 5206 || (name[0] == '_' && !STREQN (name, "_parent", 7)));
14f9c5c9
AS
5207 }
5208}
5209
5210/* True iff structure type TYPE has a tag field. */
5211
5212int
ebf56fd3 5213ada_is_tagged_type (struct type *type)
14f9c5c9
AS
5214{
5215 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5216 return 0;
5217
5218 return (ada_lookup_struct_elt_type (type, "_tag", 1, NULL) != NULL);
5219}
5220
5221/* The type of the tag on VAL. */
5222
d2e4a39e
AS
5223struct type *
5224ada_tag_type (struct value *val)
14f9c5c9
AS
5225{
5226 return ada_lookup_struct_elt_type (VALUE_TYPE (val), "_tag", 0, NULL);
5227}
5228
5229/* The value of the tag on VAL. */
5230
d2e4a39e
AS
5231struct value *
5232ada_value_tag (struct value *val)
14f9c5c9
AS
5233{
5234 return ada_value_struct_elt (val, "_tag", "record");
5235}
5236
5237/* The parent type of TYPE, or NULL if none. */
5238
d2e4a39e 5239struct type *
ebf56fd3 5240ada_parent_type (struct type *type)
14f9c5c9
AS
5241{
5242 int i;
5243
5244 CHECK_TYPEDEF (type);
5245
5246 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5247 return NULL;
5248
5249 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5250 if (ada_is_parent_field (type, i))
5251 return check_typedef (TYPE_FIELD_TYPE (type, i));
5252
5253 return NULL;
5254}
5255
5256/* True iff field number FIELD_NUM of structure type TYPE contains the
5257 parent-type (inherited) fields of a derived type. Assumes TYPE is
5258 a structure type with at least FIELD_NUM+1 fields. */
5259
5260int
ebf56fd3 5261ada_is_parent_field (struct type *type, int field_num)
14f9c5c9 5262{
d2e4a39e
AS
5263 const char *name = TYPE_FIELD_NAME (check_typedef (type), field_num);
5264 return (name != NULL &&
14f9c5c9
AS
5265 (STREQN (name, "PARENT", 6) || STREQN (name, "_parent", 7)));
5266}
5267
5268/* True iff field number FIELD_NUM of structure type TYPE is a
5269 transparent wrapper field (which should be silently traversed when doing
5270 field selection and flattened when printing). Assumes TYPE is a
5271 structure type with at least FIELD_NUM+1 fields. Such fields are always
5272 structures. */
5273
5274int
ebf56fd3 5275ada_is_wrapper_field (struct type *type, int field_num)
14f9c5c9 5276{
d2e4a39e
AS
5277 const char *name = TYPE_FIELD_NAME (type, field_num);
5278 return (name != NULL
5279 && (STREQN (name, "PARENT", 6) || STREQ (name, "REP")
14f9c5c9
AS
5280 || STREQN (name, "_parent", 7)
5281 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
5282}
5283
5284/* True iff field number FIELD_NUM of structure or union type TYPE
5285 is a variant wrapper. Assumes TYPE is a structure type with at least
d2e4a39e 5286 FIELD_NUM+1 fields. */
14f9c5c9
AS
5287
5288int
ebf56fd3 5289ada_is_variant_part (struct type *type, int field_num)
14f9c5c9 5290{
d2e4a39e 5291 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
14f9c5c9
AS
5292 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
5293 || (is_dynamic_field (type, field_num)
d2e4a39e
AS
5294 && TYPE_CODE (TYPE_TARGET_TYPE (field_type)) ==
5295 TYPE_CODE_UNION));
14f9c5c9
AS
5296}
5297
5298/* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
5299 whose discriminants are contained in the record type OUTER_TYPE,
5300 returns the type of the controlling discriminant for the variant. */
5301
d2e4a39e 5302struct type *
ebf56fd3 5303ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
14f9c5c9 5304{
d2e4a39e
AS
5305 char *name = ada_variant_discrim_name (var_type);
5306 struct type *type = ada_lookup_struct_elt_type (outer_type, name, 1, NULL);
14f9c5c9
AS
5307 if (type == NULL)
5308 return builtin_type_int;
5309 else
5310 return type;
5311}
5312
5313/* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
5314 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
5315 represents a 'when others' clause; otherwise 0. */
5316
5317int
ebf56fd3 5318ada_is_others_clause (struct type *type, int field_num)
14f9c5c9 5319{
d2e4a39e 5320 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
5321 return (name != NULL && name[0] == 'O');
5322}
5323
5324/* Assuming that TYPE0 is the type of the variant part of a record,
5325 returns the name of the discriminant controlling the variant. The
5326 value is valid until the next call to ada_variant_discrim_name. */
5327
d2e4a39e 5328char *
ebf56fd3 5329ada_variant_discrim_name (struct type *type0)
14f9c5c9 5330{
d2e4a39e 5331 static char *result = NULL;
14f9c5c9 5332 static size_t result_len = 0;
d2e4a39e
AS
5333 struct type *type;
5334 const char *name;
5335 const char *discrim_end;
5336 const char *discrim_start;
14f9c5c9
AS
5337
5338 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
5339 type = TYPE_TARGET_TYPE (type0);
5340 else
5341 type = type0;
5342
5343 name = ada_type_name (type);
5344
5345 if (name == NULL || name[0] == '\000')
5346 return "";
5347
5348 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
5349 discrim_end -= 1)
5350 {
5351 if (STREQN (discrim_end, "___XVN", 6))
5352 break;
5353 }
5354 if (discrim_end == name)
5355 return "";
5356
d2e4a39e 5357 for (discrim_start = discrim_end; discrim_start != name + 3;
14f9c5c9
AS
5358 discrim_start -= 1)
5359 {
d2e4a39e 5360 if (discrim_start == name + 1)
14f9c5c9 5361 return "";
d2e4a39e 5362 if ((discrim_start > name + 3 && STREQN (discrim_start - 3, "___", 3))
14f9c5c9
AS
5363 || discrim_start[-1] == '.')
5364 break;
5365 }
5366
5367 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
5368 strncpy (result, discrim_start, discrim_end - discrim_start);
d2e4a39e 5369 result[discrim_end - discrim_start] = '\0';
14f9c5c9
AS
5370 return result;
5371}
5372
5373/* Scan STR for a subtype-encoded number, beginning at position K. Put the
5374 position of the character just past the number scanned in *NEW_K,
5375 if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL. Return 1
5376 if there was a valid number at the given position, and 0 otherwise. A
5377 "subtype-encoded" number consists of the absolute value in decimal,
5378 followed by the letter 'm' to indicate a negative number. Assumes 0m
5379 does not occur. */
5380
5381int
d2e4a39e 5382ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
14f9c5c9
AS
5383{
5384 ULONGEST RU;
5385
d2e4a39e 5386 if (!isdigit (str[k]))
14f9c5c9
AS
5387 return 0;
5388
5389 /* Do it the hard way so as not to make any assumption about
5390 the relationship of unsigned long (%lu scan format code) and
5391 LONGEST. */
5392 RU = 0;
5393 while (isdigit (str[k]))
5394 {
d2e4a39e 5395 RU = RU * 10 + (str[k] - '0');
14f9c5c9
AS
5396 k += 1;
5397 }
5398
d2e4a39e 5399 if (str[k] == 'm')
14f9c5c9
AS
5400 {
5401 if (R != NULL)
d2e4a39e 5402 *R = (-(LONGEST) (RU - 1)) - 1;
14f9c5c9
AS
5403 k += 1;
5404 }
5405 else if (R != NULL)
5406 *R = (LONGEST) RU;
5407
5408 /* NOTE on the above: Technically, C does not say what the results of
5409 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
5410 number representable as a LONGEST (although either would probably work
5411 in most implementations). When RU>0, the locution in the then branch
5412 above is always equivalent to the negative of RU. */
5413
5414 if (new_k != NULL)
5415 *new_k = k;
5416 return 1;
5417}
5418
5419/* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
5420 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
5421 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
5422
d2e4a39e 5423int
ebf56fd3 5424ada_in_variant (LONGEST val, struct type *type, int field_num)
14f9c5c9 5425{
d2e4a39e 5426 const char *name = TYPE_FIELD_NAME (type, field_num);
14f9c5c9
AS
5427 int p;
5428
5429 p = 0;
5430 while (1)
5431 {
d2e4a39e 5432 switch (name[p])
14f9c5c9
AS
5433 {
5434 case '\0':
5435 return 0;
5436 case 'S':
5437 {
5438 LONGEST W;
d2e4a39e 5439 if (!ada_scan_number (name, p + 1, &W, &p))
14f9c5c9
AS
5440 return 0;
5441 if (val == W)
5442 return 1;
5443 break;
5444 }
5445 case 'R':
5446 {
5447 LONGEST L, U;
d2e4a39e
AS
5448 if (!ada_scan_number (name, p + 1, &L, &p)
5449 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
14f9c5c9
AS
5450 return 0;
5451 if (val >= L && val <= U)
5452 return 1;
5453 break;
5454 }
5455 case 'O':
5456 return 1;
5457 default:
5458 return 0;
5459 }
5460 }
5461}
5462
5463/* Given a value ARG1 (offset by OFFSET bytes)
5464 of a struct or union type ARG_TYPE,
5465 extract and return the value of one of its (non-static) fields.
5466 FIELDNO says which field. Differs from value_primitive_field only
5467 in that it can handle packed values of arbitrary type. */
5468
d2e4a39e
AS
5469struct value *
5470ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
ebf56fd3 5471 struct type *arg_type)
14f9c5c9 5472{
d2e4a39e 5473 struct value *v;
14f9c5c9
AS
5474 struct type *type;
5475
5476 CHECK_TYPEDEF (arg_type);
5477 type = TYPE_FIELD_TYPE (arg_type, fieldno);
5478
5479 /* Handle packed fields */
5480
5481 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
5482 {
5483 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
5484 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
d2e4a39e 5485
14f9c5c9 5486 return ada_value_primitive_packed_val (arg1, VALUE_CONTENTS (arg1),
d2e4a39e
AS
5487 offset + bit_pos / 8,
5488 bit_pos % 8, bit_size, type);
14f9c5c9
AS
5489 }
5490 else
5491 return value_primitive_field (arg1, offset, fieldno, arg_type);
5492}
5493
5494
5495/* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
5496 and search in it assuming it has (class) type TYPE.
5497 If found, return value, else return NULL.
5498
5499 Searches recursively through wrapper fields (e.g., '_parent'). */
5500
d2e4a39e
AS
5501struct value *
5502ada_search_struct_field (char *name, struct value *arg, int offset,
ebf56fd3 5503 struct type *type)
14f9c5c9
AS
5504{
5505 int i;
5506 CHECK_TYPEDEF (type);
5507
d2e4a39e 5508 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i -= 1)
14f9c5c9
AS
5509 {
5510 char *t_field_name = TYPE_FIELD_NAME (type, i);
5511
5512 if (t_field_name == NULL)
5513 continue;
5514
5515 else if (field_name_match (t_field_name, name))
d2e4a39e 5516 return ada_value_primitive_field (arg, offset, i, type);
14f9c5c9
AS
5517
5518 else if (ada_is_wrapper_field (type, i))
5519 {
d2e4a39e
AS
5520 struct value *v = ada_search_struct_field (name, arg,
5521 offset +
5522 TYPE_FIELD_BITPOS (type,
5523 i) /
5524 8,
5525 TYPE_FIELD_TYPE (type,
5526 i));
14f9c5c9
AS
5527 if (v != NULL)
5528 return v;
5529 }
5530
5531 else if (ada_is_variant_part (type, i))
5532 {
5533 int j;
5534 struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
5535 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
5536
5537 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
5538 {
d2e4a39e
AS
5539 struct value *v = ada_search_struct_field (name, arg,
5540 var_offset
5541 +
5542 TYPE_FIELD_BITPOS
5543 (field_type, j) / 8,
5544 TYPE_FIELD_TYPE
5545 (field_type, j));
14f9c5c9
AS
5546 if (v != NULL)
5547 return v;
5548 }
5549 }
5550 }
5551 return NULL;
5552}
d2e4a39e 5553
14f9c5c9
AS
5554/* Given ARG, a value of type (pointer to a)* structure/union,
5555 extract the component named NAME from the ultimate target structure/union
5556 and return it as a value with its appropriate type.
5557
5558 The routine searches for NAME among all members of the structure itself
5559 and (recursively) among all members of any wrapper members
5560 (e.g., '_parent').
5561
5562 ERR is a name (for use in error messages) that identifies the class
5563 of entity that ARG is supposed to be. */
5564
d2e4a39e 5565struct value *
ebf56fd3 5566ada_value_struct_elt (struct value *arg, char *name, char *err)
14f9c5c9
AS
5567{
5568 struct type *t;
d2e4a39e 5569 struct value *v;
14f9c5c9
AS
5570
5571 arg = ada_coerce_ref (arg);
5572 t = check_typedef (VALUE_TYPE (arg));
5573
5574 /* Follow pointers until we get to a non-pointer. */
5575
5576 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
5577 {
5578 arg = ada_value_ind (arg);
5579 t = check_typedef (VALUE_TYPE (arg));
5580 }
5581
d2e4a39e
AS
5582 if (TYPE_CODE (t) != TYPE_CODE_STRUCT && TYPE_CODE (t) != TYPE_CODE_UNION)
5583 error ("Attempt to extract a component of a value that is not a %s.",
5584 err);
14f9c5c9
AS
5585
5586 v = ada_search_struct_field (name, arg, 0, t);
5587 if (v == NULL)
5588 error ("There is no member named %s.", name);
5589
5590 return v;
5591}
5592
5593/* Given a type TYPE, look up the type of the component of type named NAME.
5594 If DISPP is non-null, add its byte displacement from the beginning of a
5595 structure (pointed to by a value) of type TYPE to *DISPP (does not
5596 work for packed fields).
5597
5598 Matches any field whose name has NAME as a prefix, possibly
5599 followed by "___".
5600
5601 TYPE can be either a struct or union, or a pointer or reference to
5602 a struct or union. If it is a pointer or reference, its target
5603 type is automatically used.
5604
5605 Looks recursively into variant clauses and parent types.
5606
5607 If NOERR is nonzero, return NULL if NAME is not suitably defined. */
5608
5609struct type *
d2e4a39e
AS
5610ada_lookup_struct_elt_type (struct type *type, char *name, int noerr,
5611 int *dispp)
14f9c5c9
AS
5612{
5613 int i;
5614
5615 if (name == NULL)
5616 goto BadName;
5617
5618 while (1)
5619 {
5620 CHECK_TYPEDEF (type);
5621 if (TYPE_CODE (type) != TYPE_CODE_PTR
5622 && TYPE_CODE (type) != TYPE_CODE_REF)
5623 break;
5624 type = TYPE_TARGET_TYPE (type);
5625 }
5626
5627 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
5628 TYPE_CODE (type) != TYPE_CODE_UNION)
5629 {
5630 target_terminal_ours ();
5631 gdb_flush (gdb_stdout);
5632 fprintf_unfiltered (gdb_stderr, "Type ");
5633 type_print (type, "", gdb_stderr, -1);
5634 error (" is not a structure or union type");
5635 }
5636
5637 type = to_static_fixed_type (type);
5638
5639 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5640 {
5641 char *t_field_name = TYPE_FIELD_NAME (type, i);
5642 struct type *t;
5643 int disp;
d2e4a39e 5644
14f9c5c9
AS
5645 if (t_field_name == NULL)
5646 continue;
5647
5648 else if (field_name_match (t_field_name, name))
5649 {
d2e4a39e 5650 if (dispp != NULL)
14f9c5c9
AS
5651 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
5652 return check_typedef (TYPE_FIELD_TYPE (type, i));
5653 }
5654
5655 else if (ada_is_wrapper_field (type, i))
5656 {
5657 disp = 0;
d2e4a39e 5658 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
14f9c5c9
AS
5659 1, &disp);
5660 if (t != NULL)
5661 {
5662 if (dispp != NULL)
5663 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
5664 return t;
5665 }
5666 }
5667
5668 else if (ada_is_variant_part (type, i))
5669 {
5670 int j;
5671 struct type *field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
5672
5673 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
5674 {
5675 disp = 0;
5676 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type, j),
5677 name, 1, &disp);
5678 if (t != NULL)
5679 {
d2e4a39e 5680 if (dispp != NULL)
14f9c5c9
AS
5681 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
5682 return t;
5683 }
5684 }
5685 }
5686
5687 }
5688
5689BadName:
d2e4a39e 5690 if (!noerr)
14f9c5c9
AS
5691 {
5692 target_terminal_ours ();
5693 gdb_flush (gdb_stdout);
5694 fprintf_unfiltered (gdb_stderr, "Type ");
5695 type_print (type, "", gdb_stderr, -1);
5696 fprintf_unfiltered (gdb_stderr, " has no component named ");
5697 error ("%s", name == NULL ? "<null>" : name);
5698 }
5699
5700 return NULL;
5701}
5702
5703/* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
5704 within a value of type OUTER_TYPE that is stored in GDB at
5705 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
5706 numbering from 0) is applicable. Returns -1 if none are. */
5707
d2e4a39e 5708int
ebf56fd3 5709ada_which_variant_applies (struct type *var_type, struct type *outer_type,
d2e4a39e 5710 char *outer_valaddr)
14f9c5c9
AS
5711{
5712 int others_clause;
5713 int i;
5714 int disp;
d2e4a39e
AS
5715 struct type *discrim_type;
5716 char *discrim_name = ada_variant_discrim_name (var_type);
14f9c5c9
AS
5717 LONGEST discrim_val;
5718
5719 disp = 0;
d2e4a39e 5720 discrim_type =
14f9c5c9
AS
5721 ada_lookup_struct_elt_type (outer_type, discrim_name, 1, &disp);
5722 if (discrim_type == NULL)
5723 return -1;
5724 discrim_val = unpack_long (discrim_type, outer_valaddr + disp);
5725
5726 others_clause = -1;
5727 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
5728 {
5729 if (ada_is_others_clause (var_type, i))
5730 others_clause = i;
5731 else if (ada_in_variant (discrim_val, var_type, i))
5732 return i;
5733 }
5734
5735 return others_clause;
5736}
d2e4a39e 5737\f
14f9c5c9
AS
5738
5739
14f9c5c9
AS
5740 /* Dynamic-Sized Records */
5741
5742/* Strategy: The type ostensibly attached to a value with dynamic size
5743 (i.e., a size that is not statically recorded in the debugging
5744 data) does not accurately reflect the size or layout of the value.
5745 Our strategy is to convert these values to values with accurate,
5746 conventional types that are constructed on the fly. */
5747
5748/* There is a subtle and tricky problem here. In general, we cannot
5749 determine the size of dynamic records without its data. However,
5750 the 'struct value' data structure, which GDB uses to represent
5751 quantities in the inferior process (the target), requires the size
5752 of the type at the time of its allocation in order to reserve space
5753 for GDB's internal copy of the data. That's why the
5754 'to_fixed_xxx_type' routines take (target) addresses as parameters,
5755 rather than struct value*s.
5756
5757 However, GDB's internal history variables ($1, $2, etc.) are
5758 struct value*s containing internal copies of the data that are not, in
5759 general, the same as the data at their corresponding addresses in
5760 the target. Fortunately, the types we give to these values are all
5761 conventional, fixed-size types (as per the strategy described
5762 above), so that we don't usually have to perform the
5763 'to_fixed_xxx_type' conversions to look at their values.
5764 Unfortunately, there is one exception: if one of the internal
5765 history variables is an array whose elements are unconstrained
5766 records, then we will need to create distinct fixed types for each
5767 element selected. */
5768
5769/* The upshot of all of this is that many routines take a (type, host
5770 address, target address) triple as arguments to represent a value.
5771 The host address, if non-null, is supposed to contain an internal
5772 copy of the relevant data; otherwise, the program is to consult the
5773 target at the target address. */
5774
5775/* Assuming that VAL0 represents a pointer value, the result of
5776 dereferencing it. Differs from value_ind in its treatment of
5777 dynamic-sized types. */
5778
d2e4a39e
AS
5779struct value *
5780ada_value_ind (struct value *val0)
14f9c5c9 5781{
d2e4a39e 5782 struct value *val = unwrap_value (value_ind (val0));
14f9c5c9 5783 return ada_to_fixed_value (VALUE_TYPE (val), 0,
d2e4a39e 5784 VALUE_ADDRESS (val) + VALUE_OFFSET (val), val);
14f9c5c9
AS
5785}
5786
5787/* The value resulting from dereferencing any "reference to"
5788 * qualifiers on VAL0. */
d2e4a39e
AS
5789static struct value *
5790ada_coerce_ref (struct value *val0)
5791{
5792 if (TYPE_CODE (VALUE_TYPE (val0)) == TYPE_CODE_REF)
5793 {
5794 struct value *val = val0;
5795 COERCE_REF (val);
5796 val = unwrap_value (val);
5797 return ada_to_fixed_value (VALUE_TYPE (val), 0,
5798 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
5799 val);
5800 }
5801 else
14f9c5c9
AS
5802 return val0;
5803}
5804
5805/* Return OFF rounded upward if necessary to a multiple of
5806 ALIGNMENT (a power of 2). */
5807
5808static unsigned int
ebf56fd3 5809align_value (unsigned int off, unsigned int alignment)
14f9c5c9
AS
5810{
5811 return (off + alignment - 1) & ~(alignment - 1);
5812}
5813
5814/* Return the additional bit offset required by field F of template
5815 type TYPE. */
5816
5817static unsigned int
ebf56fd3 5818field_offset (struct type *type, int f)
14f9c5c9
AS
5819{
5820 int n = TYPE_FIELD_BITPOS (type, f);
5821 /* Kludge (temporary?) to fix problem with dwarf output. */
5822 if (n < 0)
5823 return (unsigned int) n & 0xffff;
5824 else
5825 return n;
5826}
5827
5828
5829/* Return the bit alignment required for field #F of template type TYPE. */
5830
5831static unsigned int
ebf56fd3 5832field_alignment (struct type *type, int f)
14f9c5c9 5833{
d2e4a39e 5834 const char *name = TYPE_FIELD_NAME (type, f);
14f9c5c9
AS
5835 int len = (name == NULL) ? 0 : strlen (name);
5836 int align_offset;
5837
d2e4a39e 5838 if (len < 8 || !isdigit (name[len - 1]))
14f9c5c9
AS
5839 return TARGET_CHAR_BIT;
5840
d2e4a39e 5841 if (isdigit (name[len - 2]))
14f9c5c9
AS
5842 align_offset = len - 2;
5843 else
5844 align_offset = len - 1;
5845
d2e4a39e 5846 if (align_offset < 7 || !STREQN ("___XV", name + align_offset - 6, 5))
14f9c5c9
AS
5847 return TARGET_CHAR_BIT;
5848
d2e4a39e 5849 return atoi (name + align_offset) * TARGET_CHAR_BIT;
14f9c5c9
AS
5850}
5851
5852/* Find a type named NAME. Ignores ambiguity. */
d2e4a39e 5853struct type *
ebf56fd3 5854ada_find_any_type (const char *name)
14f9c5c9 5855{
d2e4a39e 5856 struct symbol *sym;
14f9c5c9
AS
5857
5858 sym = standard_lookup (name, VAR_NAMESPACE);
5859 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
5860 return SYMBOL_TYPE (sym);
5861
5862 sym = standard_lookup (name, STRUCT_NAMESPACE);
5863 if (sym != NULL)
5864 return SYMBOL_TYPE (sym);
5865
5866 return NULL;
5867}
5868
5869/* Because of GNAT encoding conventions, several GDB symbols may match a
5870 given type name. If the type denoted by TYPE0 is to be preferred to
5871 that of TYPE1 for purposes of type printing, return non-zero;
5872 otherwise return 0. */
5873int
d2e4a39e 5874ada_prefer_type (struct type *type0, struct type *type1)
14f9c5c9
AS
5875{
5876 if (type1 == NULL)
5877 return 1;
5878 else if (type0 == NULL)
5879 return 0;
5880 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
5881 return 1;
5882 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
5883 return 0;
5884 else if (ada_is_packed_array_type (type0))
5885 return 1;
d2e4a39e
AS
5886 else if (ada_is_array_descriptor (type0)
5887 && !ada_is_array_descriptor (type1))
14f9c5c9 5888 return 1;
d2e4a39e 5889 else if (ada_renaming_type (type0) != NULL
14f9c5c9
AS
5890 && ada_renaming_type (type1) == NULL)
5891 return 1;
5892 return 0;
5893}
5894
5895/* The name of TYPE, which is either its TYPE_NAME, or, if that is
5896 null, its TYPE_TAG_NAME. Null if TYPE is null. */
d2e4a39e
AS
5897char *
5898ada_type_name (struct type *type)
14f9c5c9 5899{
d2e4a39e 5900 if (type == NULL)
14f9c5c9
AS
5901 return NULL;
5902 else if (TYPE_NAME (type) != NULL)
5903 return TYPE_NAME (type);
5904 else
5905 return TYPE_TAG_NAME (type);
5906}
5907
5908/* Find a parallel type to TYPE whose name is formed by appending
5909 SUFFIX to the name of TYPE. */
5910
d2e4a39e 5911struct type *
ebf56fd3 5912ada_find_parallel_type (struct type *type, const char *suffix)
14f9c5c9 5913{
d2e4a39e 5914 static char *name;
14f9c5c9 5915 static size_t name_len = 0;
d2e4a39e
AS
5916 struct symbol **syms;
5917 struct block **blocks;
14f9c5c9
AS
5918 int nsyms;
5919 int len;
d2e4a39e
AS
5920 char *typename = ada_type_name (type);
5921
14f9c5c9
AS
5922 if (typename == NULL)
5923 return NULL;
5924
5925 len = strlen (typename);
5926
d2e4a39e 5927 GROW_VECT (name, name_len, len + strlen (suffix) + 1);
14f9c5c9
AS
5928
5929 strcpy (name, typename);
5930 strcpy (name + len, suffix);
5931
5932 return ada_find_any_type (name);
5933}
5934
5935
5936/* If TYPE is a variable-size record type, return the corresponding template
5937 type describing its fields. Otherwise, return NULL. */
5938
d2e4a39e
AS
5939static struct type *
5940dynamic_template_type (struct type *type)
14f9c5c9
AS
5941{
5942 CHECK_TYPEDEF (type);
5943
5944 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
d2e4a39e 5945 || ada_type_name (type) == NULL)
14f9c5c9 5946 return NULL;
d2e4a39e 5947 else
14f9c5c9
AS
5948 {
5949 int len = strlen (ada_type_name (type));
5950 if (len > 6 && STREQ (ada_type_name (type) + len - 6, "___XVE"))
5951 return type;
5952 else
5953 return ada_find_parallel_type (type, "___XVE");
5954 }
5955}
5956
5957/* Assuming that TEMPL_TYPE is a union or struct type, returns
5958 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
5959
d2e4a39e
AS
5960static int
5961is_dynamic_field (struct type *templ_type, int field_num)
14f9c5c9
AS
5962{
5963 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
d2e4a39e 5964 return name != NULL
14f9c5c9
AS
5965 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
5966 && strstr (name, "___XVL") != NULL;
5967}
5968
5969/* Assuming that TYPE is a struct type, returns non-zero iff TYPE
5970 contains a variant part. */
5971
d2e4a39e
AS
5972static int
5973contains_variant_part (struct type *type)
14f9c5c9
AS
5974{
5975 int f;
5976
5977 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
5978 || TYPE_NFIELDS (type) <= 0)
5979 return 0;
5980 return ada_is_variant_part (type, TYPE_NFIELDS (type) - 1);
5981}
5982
5983/* A record type with no fields, . */
d2e4a39e
AS
5984static struct type *
5985empty_record (struct objfile *objfile)
14f9c5c9 5986{
d2e4a39e 5987 struct type *type = alloc_type (objfile);
14f9c5c9
AS
5988 TYPE_CODE (type) = TYPE_CODE_STRUCT;
5989 TYPE_NFIELDS (type) = 0;
5990 TYPE_FIELDS (type) = NULL;
5991 TYPE_NAME (type) = "<empty>";
5992 TYPE_TAG_NAME (type) = NULL;
5993 TYPE_FLAGS (type) = 0;
5994 TYPE_LENGTH (type) = 0;
5995 return type;
5996}
5997
5998/* An ordinary record type (with fixed-length fields) that describes
5999 the value of type TYPE at VALADDR or ADDRESS (see comments at
6000 the beginning of this section) VAL according to GNAT conventions.
6001 DVAL0 should describe the (portion of a) record that contains any
6002 necessary discriminants. It should be NULL if VALUE_TYPE (VAL) is
6003 an outer-level type (i.e., as opposed to a branch of a variant.) A
6004 variant field (unless unchecked) is replaced by a particular branch
6005 of the variant. */
6006/* NOTE: Limitations: For now, we assume that dynamic fields and
6007 * variants occupy whole numbers of bytes. However, they need not be
6008 * byte-aligned. */
6009
d2e4a39e
AS
6010static struct type *
6011template_to_fixed_record_type (struct type *type, char *valaddr,
6012 CORE_ADDR address, struct value *dval0)
14f9c5c9 6013{
d2e4a39e
AS
6014 struct value *mark = value_mark ();
6015 struct value *dval;
6016 struct type *rtype;
14f9c5c9
AS
6017 int nfields, bit_len;
6018 long off;
6019 int f;
6020
6021 nfields = TYPE_NFIELDS (type);
6022 rtype = alloc_type (TYPE_OBJFILE (type));
6023 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
6024 INIT_CPLUS_SPECIFIC (rtype);
6025 TYPE_NFIELDS (rtype) = nfields;
d2e4a39e 6026 TYPE_FIELDS (rtype) = (struct field *)
14f9c5c9
AS
6027 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
6028 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
6029 TYPE_NAME (rtype) = ada_type_name (type);
6030 TYPE_TAG_NAME (rtype) = NULL;
6031 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in
d2e4a39e
AS
6032 gdbtypes.h */
6033 /* TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE; */
14f9c5c9 6034
d2e4a39e
AS
6035 off = 0;
6036 bit_len = 0;
14f9c5c9
AS
6037 for (f = 0; f < nfields; f += 1)
6038 {
6039 int fld_bit_len, bit_incr;
d2e4a39e
AS
6040 off =
6041 align_value (off,
6042 field_alignment (type, f)) + TYPE_FIELD_BITPOS (type, f);
14f9c5c9
AS
6043 /* NOTE: used to use field_offset above, but that causes
6044 * problems with really negative bit positions. So, let's
6045 * rediscover why we needed field_offset and fix it properly. */
6046 TYPE_FIELD_BITPOS (rtype, f) = off;
d2e4a39e 6047 TYPE_FIELD_BITSIZE (rtype, f) = 0;
01ad7f36 6048 TYPE_FIELD_STATIC_KIND (rtype, f) = 0;
14f9c5c9 6049
d2e4a39e 6050 if (ada_is_variant_part (type, f))
14f9c5c9
AS
6051 {
6052 struct type *branch_type;
6053
6054 if (dval0 == NULL)
d2e4a39e 6055 dval = value_from_contents_and_address (rtype, valaddr, address);
14f9c5c9
AS
6056 else
6057 dval = dval0;
6058
d2e4a39e
AS
6059 branch_type =
6060 to_fixed_variant_branch_type
6061 (TYPE_FIELD_TYPE (type, f),
6062 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
6063 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
6064 if (branch_type == NULL)
14f9c5c9
AS
6065 TYPE_NFIELDS (rtype) -= 1;
6066 else
6067 {
6068 TYPE_FIELD_TYPE (rtype, f) = branch_type;
6069 TYPE_FIELD_NAME (rtype, f) = "S";
6070 }
6071 bit_incr = 0;
6072 fld_bit_len =
6073 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
6074 }
6075 else if (is_dynamic_field (type, f))
6076 {
6077 if (dval0 == NULL)
d2e4a39e 6078 dval = value_from_contents_and_address (rtype, valaddr, address);
14f9c5c9
AS
6079 else
6080 dval = dval0;
6081
d2e4a39e
AS
6082 TYPE_FIELD_TYPE (rtype, f) =
6083 ada_to_fixed_type
6084 (ada_get_base_type
6085 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f))),
6086 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
6087 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
14f9c5c9
AS
6088 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
6089 bit_incr = fld_bit_len =
6090 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
6091 }
6092 else
6093 {
6094 TYPE_FIELD_TYPE (rtype, f) = TYPE_FIELD_TYPE (type, f);
6095 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
6096 if (TYPE_FIELD_BITSIZE (type, f) > 0)
d2e4a39e 6097 bit_incr = fld_bit_len =
14f9c5c9
AS
6098 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
6099 else
6100 bit_incr = fld_bit_len =
6101 TYPE_LENGTH (TYPE_FIELD_TYPE (type, f)) * TARGET_CHAR_BIT;
6102 }
6103 if (off + fld_bit_len > bit_len)
6104 bit_len = off + fld_bit_len;
6105 off += bit_incr;
6106 TYPE_LENGTH (rtype) = bit_len / TARGET_CHAR_BIT;
6107 }
6108 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype), TYPE_LENGTH (type));
6109
6110 value_free_to_mark (mark);
d2e4a39e 6111 if (TYPE_LENGTH (rtype) > varsize_limit)
14f9c5c9
AS
6112 error ("record type with dynamic size is larger than varsize-limit");
6113 return rtype;
6114}
6115
6116/* As for template_to_fixed_record_type, but uses no run-time values.
6117 As a result, this type can only be approximate, but that's OK,
6118 since it is used only for type determinations. Works on both
6119 structs and unions.
6120 Representation note: to save space, we memoize the result of this
6121 function in the TYPE_TARGET_TYPE of the template type. */
6122
d2e4a39e
AS
6123static struct type *
6124template_to_static_fixed_type (struct type *templ_type)
14f9c5c9
AS
6125{
6126 struct type *type;
6127 int nfields;
6128 int f;
6129
6130 if (TYPE_TARGET_TYPE (templ_type) != NULL)
6131 return TYPE_TARGET_TYPE (templ_type);
6132
6133 nfields = TYPE_NFIELDS (templ_type);
d2e4a39e
AS
6134 TYPE_TARGET_TYPE (templ_type) = type =
6135 alloc_type (TYPE_OBJFILE (templ_type));
14f9c5c9
AS
6136 TYPE_CODE (type) = TYPE_CODE (templ_type);
6137 INIT_CPLUS_SPECIFIC (type);
6138 TYPE_NFIELDS (type) = nfields;
d2e4a39e 6139 TYPE_FIELDS (type) = (struct field *)
14f9c5c9
AS
6140 TYPE_ALLOC (type, nfields * sizeof (struct field));
6141 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6142 TYPE_NAME (type) = ada_type_name (templ_type);
6143 TYPE_TAG_NAME (type) = NULL;
d2e4a39e 6144 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
14f9c5c9
AS
6145 /* TYPE_FLAGS (type) |= TYPE_FLAG_FIXED_INSTANCE; */
6146 TYPE_LENGTH (type) = 0;
6147
6148 for (f = 0; f < nfields; f += 1)
6149 {
6150 TYPE_FIELD_BITPOS (type, f) = 0;
d2e4a39e 6151 TYPE_FIELD_BITSIZE (type, f) = 0;
01ad7f36 6152 TYPE_FIELD_STATIC_KIND (type, f) = 0;
14f9c5c9
AS
6153
6154 if (is_dynamic_field (templ_type, f))
6155 {
d2e4a39e
AS
6156 TYPE_FIELD_TYPE (type, f) =
6157 to_static_fixed_type (TYPE_TARGET_TYPE
14f9c5c9
AS
6158 (TYPE_FIELD_TYPE (templ_type, f)));
6159 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (templ_type, f);
6160 }
6161 else
6162 {
d2e4a39e 6163 TYPE_FIELD_TYPE (type, f) =
14f9c5c9
AS
6164 check_typedef (TYPE_FIELD_TYPE (templ_type, f));
6165 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (templ_type, f);
6166 }
6167 }
6168
6169 return type;
6170}
6171
6172/* A revision of TYPE0 -- a non-dynamic-sized record with a variant
6173 part -- in which the variant part is replaced with the appropriate
6174 branch. */
d2e4a39e
AS
6175static struct type *
6176to_record_with_fixed_variant_part (struct type *type, char *valaddr,
6177 CORE_ADDR address, struct value *dval)
14f9c5c9 6178{
d2e4a39e
AS
6179 struct value *mark = value_mark ();
6180 struct type *rtype;
14f9c5c9
AS
6181 struct type *branch_type;
6182 int nfields = TYPE_NFIELDS (type);
6183
6184 if (dval == NULL)
6185 return type;
6186
6187 rtype = alloc_type (TYPE_OBJFILE (type));
6188 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
6189 INIT_CPLUS_SPECIFIC (type);
6190 TYPE_NFIELDS (rtype) = TYPE_NFIELDS (type);
d2e4a39e
AS
6191 TYPE_FIELDS (rtype) =
6192 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
6193 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
14f9c5c9
AS
6194 sizeof (struct field) * nfields);
6195 TYPE_NAME (rtype) = ada_type_name (type);
6196 TYPE_TAG_NAME (rtype) = NULL;
d2e4a39e 6197 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
14f9c5c9
AS
6198 /* TYPE_FLAGS (rtype) |= TYPE_FLAG_FIXED_INSTANCE; */
6199 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
6200
d2e4a39e
AS
6201 branch_type =
6202 to_fixed_variant_branch_type
6203 (TYPE_FIELD_TYPE (type, nfields - 1),
6204 cond_offset_host (valaddr,
6205 TYPE_FIELD_BITPOS (type,
6206 nfields - 1) / TARGET_CHAR_BIT),
6207 cond_offset_target (address,
6208 TYPE_FIELD_BITPOS (type,
6209 nfields - 1) / TARGET_CHAR_BIT),
6210 dval);
6211 if (branch_type == NULL)
14f9c5c9
AS
6212 {
6213 TYPE_NFIELDS (rtype) -= 1;
d2e4a39e
AS
6214 TYPE_LENGTH (rtype) -=
6215 TYPE_LENGTH (TYPE_FIELD_TYPE (type, nfields - 1));
14f9c5c9
AS
6216 }
6217 else
6218 {
d2e4a39e
AS
6219 TYPE_FIELD_TYPE (rtype, nfields - 1) = branch_type;
6220 TYPE_FIELD_NAME (rtype, nfields - 1) = "S";
6221 TYPE_FIELD_BITSIZE (rtype, nfields - 1) = 0;
01ad7f36 6222 TYPE_FIELD_STATIC_KIND (rtype, nfields - 1) = 0;
14f9c5c9 6223 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
d2e4a39e 6224 -TYPE_LENGTH (TYPE_FIELD_TYPE (type, nfields - 1));
14f9c5c9 6225 }
d2e4a39e 6226
14f9c5c9
AS
6227 return rtype;
6228}
6229
6230/* An ordinary record type (with fixed-length fields) that describes
6231 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
6232 beginning of this section]. Any necessary discriminants' values
6233 should be in DVAL, a record value; it should be NULL if the object
6234 at ADDR itself contains any necessary discriminant values. A
6235 variant field (unless unchecked) is replaced by a particular branch
d2e4a39e 6236 of the variant. */
14f9c5c9 6237
d2e4a39e
AS
6238static struct type *
6239to_fixed_record_type (struct type *type0, char *valaddr, CORE_ADDR address,
6240 struct value *dval)
14f9c5c9 6241{
d2e4a39e 6242 struct type *templ_type;
14f9c5c9
AS
6243
6244 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
6245 /* if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
d2e4a39e
AS
6246 return type0;
6247 */
6248 templ_type = dynamic_template_type (type0);
14f9c5c9
AS
6249
6250 if (templ_type != NULL)
6251 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
6252 else if (contains_variant_part (type0))
6253 return to_record_with_fixed_variant_part (type0, valaddr, address, dval);
6254 else
6255 {
d2e4a39e 6256 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
14f9c5c9
AS
6257 /* TYPE_FLAGS (type0) |= TYPE_FLAG_FIXED_INSTANCE; */
6258 return type0;
6259 }
6260
6261}
6262
6263/* An ordinary record type (with fixed-length fields) that describes
6264 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
6265 union type. Any necessary discriminants' values should be in DVAL,
6266 a record value. That is, this routine selects the appropriate
6267 branch of the union at ADDR according to the discriminant value
6268 indicated in the union's type name. */
6269
d2e4a39e
AS
6270static struct type *
6271to_fixed_variant_branch_type (struct type *var_type0, char *valaddr,
6272 CORE_ADDR address, struct value *dval)
14f9c5c9
AS
6273{
6274 int which;
d2e4a39e
AS
6275 struct type *templ_type;
6276 struct type *var_type;
14f9c5c9
AS
6277
6278 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
6279 var_type = TYPE_TARGET_TYPE (var_type0);
d2e4a39e 6280 else
14f9c5c9
AS
6281 var_type = var_type0;
6282
6283 templ_type = ada_find_parallel_type (var_type, "___XVU");
6284
6285 if (templ_type != NULL)
6286 var_type = templ_type;
6287
d2e4a39e
AS
6288 which =
6289 ada_which_variant_applies (var_type,
14f9c5c9
AS
6290 VALUE_TYPE (dval), VALUE_CONTENTS (dval));
6291
6292 if (which < 0)
6293 return empty_record (TYPE_OBJFILE (var_type));
6294 else if (is_dynamic_field (var_type, which))
d2e4a39e
AS
6295 return
6296 to_fixed_record_type
6297 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
6298 valaddr, address, dval);
14f9c5c9 6299 else if (contains_variant_part (TYPE_FIELD_TYPE (var_type, which)))
d2e4a39e
AS
6300 return
6301 to_fixed_record_type
6302 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
14f9c5c9
AS
6303 else
6304 return TYPE_FIELD_TYPE (var_type, which);
6305}
6306
6307/* Assuming that TYPE0 is an array type describing the type of a value
6308 at ADDR, and that DVAL describes a record containing any
6309 discriminants used in TYPE0, returns a type for the value that
6310 contains no dynamic components (that is, no components whose sizes
6311 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
6312 true, gives an error message if the resulting type's size is over
6313 varsize_limit.
6314*/
6315
d2e4a39e
AS
6316static struct type *
6317to_fixed_array_type (struct type *type0, struct value *dval,
ebf56fd3 6318 int ignore_too_big)
14f9c5c9 6319{
d2e4a39e
AS
6320 struct type *index_type_desc;
6321 struct type *result;
14f9c5c9
AS
6322
6323 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
d2e4a39e
AS
6324/* if (ada_is_packed_array_type (type0) /* revisit? *//*
6325 || (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE))
6326 return type0; */
14f9c5c9
AS
6327
6328 index_type_desc = ada_find_parallel_type (type0, "___XA");
6329 if (index_type_desc == NULL)
6330 {
6331 struct type *elt_type0 = check_typedef (TYPE_TARGET_TYPE (type0));
6332 /* NOTE: elt_type---the fixed version of elt_type0---should never
6333 * depend on the contents of the array in properly constructed
d2e4a39e
AS
6334 * debugging data. */
6335 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval);
14f9c5c9
AS
6336
6337 if (elt_type0 == elt_type)
6338 result = type0;
6339 else
d2e4a39e 6340 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
14f9c5c9
AS
6341 elt_type, TYPE_INDEX_TYPE (type0));
6342 }
6343 else
6344 {
6345 int i;
6346 struct type *elt_type0;
6347
6348 elt_type0 = type0;
6349 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
6350 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
6351
6352 /* NOTE: result---the fixed version of elt_type0---should never
6353 * depend on the contents of the array in properly constructed
d2e4a39e
AS
6354 * debugging data. */
6355 result = ada_to_fixed_type (check_typedef (elt_type0), 0, 0, dval);
14f9c5c9
AS
6356 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
6357 {
d2e4a39e 6358 struct type *range_type =
14f9c5c9
AS
6359 to_fixed_range_type (TYPE_FIELD_NAME (index_type_desc, i),
6360 dval, TYPE_OBJFILE (type0));
6361 result = create_array_type (alloc_type (TYPE_OBJFILE (type0)),
6362 result, range_type);
6363 }
d2e4a39e 6364 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
14f9c5c9
AS
6365 error ("array type with dynamic size is larger than varsize-limit");
6366 }
6367
6368/* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
6369/* TYPE_FLAGS (result) |= TYPE_FLAG_FIXED_INSTANCE; */
6370 return result;
d2e4a39e 6371}
14f9c5c9
AS
6372
6373
6374/* A standard type (containing no dynamically sized components)
6375 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
6376 DVAL describes a record containing any discriminants used in TYPE0,
6377 and may be NULL if there are none. */
6378
d2e4a39e
AS
6379struct type *
6380ada_to_fixed_type (struct type *type, char *valaddr, CORE_ADDR address,
6381 struct value *dval)
14f9c5c9
AS
6382{
6383 CHECK_TYPEDEF (type);
d2e4a39e
AS
6384 switch (TYPE_CODE (type))
6385 {
6386 default:
14f9c5c9 6387 return type;
d2e4a39e
AS
6388 case TYPE_CODE_STRUCT:
6389 return to_fixed_record_type (type, valaddr, address, NULL);
6390 case TYPE_CODE_ARRAY:
6391 return to_fixed_array_type (type, dval, 0);
6392 case TYPE_CODE_UNION:
6393 if (dval == NULL)
6394 return type;
6395 else
6396 return to_fixed_variant_branch_type (type, valaddr, address, dval);
6397 }
14f9c5c9
AS
6398}
6399
6400/* A standard (static-sized) type corresponding as well as possible to
6401 TYPE0, but based on no runtime data. */
6402
d2e4a39e
AS
6403static struct type *
6404to_static_fixed_type (struct type *type0)
14f9c5c9 6405{
d2e4a39e 6406 struct type *type;
14f9c5c9
AS
6407
6408 if (type0 == NULL)
6409 return NULL;
6410
6411 /* FIXME: TYPE_FLAG_FIXED_INSTANCE should be defined in gdbtypes.h */
6412 /* if (TYPE_FLAGS (type0) & TYPE_FLAG_FIXED_INSTANCE)
d2e4a39e
AS
6413 return type0;
6414 */
14f9c5c9 6415 CHECK_TYPEDEF (type0);
d2e4a39e 6416
14f9c5c9
AS
6417 switch (TYPE_CODE (type0))
6418 {
6419 default:
6420 return type0;
6421 case TYPE_CODE_STRUCT:
6422 type = dynamic_template_type (type0);
d2e4a39e 6423 if (type != NULL)
14f9c5c9
AS
6424 return template_to_static_fixed_type (type);
6425 return type0;
6426 case TYPE_CODE_UNION:
6427 type = ada_find_parallel_type (type0, "___XVU");
6428 if (type != NULL)
6429 return template_to_static_fixed_type (type);
6430 return type0;
6431 }
6432}
6433
6434/* A static approximation of TYPE with all type wrappers removed. */
d2e4a39e
AS
6435static struct type *
6436static_unwrap_type (struct type *type)
14f9c5c9
AS
6437{
6438 if (ada_is_aligner_type (type))
6439 {
d2e4a39e 6440 struct type *type1 = TYPE_FIELD_TYPE (check_typedef (type), 0);
14f9c5c9
AS
6441 if (ada_type_name (type1) == NULL)
6442 TYPE_NAME (type1) = ada_type_name (type);
6443
6444 return static_unwrap_type (type1);
6445 }
d2e4a39e 6446 else
14f9c5c9 6447 {
d2e4a39e
AS
6448 struct type *raw_real_type = ada_get_base_type (type);
6449 if (raw_real_type == type)
14f9c5c9
AS
6450 return type;
6451 else
6452 return to_static_fixed_type (raw_real_type);
6453 }
6454}
6455
6456/* In some cases, incomplete and private types require
6457 cross-references that are not resolved as records (for example,
6458 type Foo;
6459 type FooP is access Foo;
6460 V: FooP;
6461 type Foo is array ...;
6462 ). In these cases, since there is no mechanism for producing
6463 cross-references to such types, we instead substitute for FooP a
6464 stub enumeration type that is nowhere resolved, and whose tag is
6465 the name of the actual type. Call these types "non-record stubs". */
6466
6467/* A type equivalent to TYPE that is not a non-record stub, if one
6468 exists, otherwise TYPE. */
d2e4a39e
AS
6469struct type *
6470ada_completed_type (struct type *type)
14f9c5c9
AS
6471{
6472 CHECK_TYPEDEF (type);
6473 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
6474 || (TYPE_FLAGS (type) & TYPE_FLAG_STUB) == 0
6475 || TYPE_TAG_NAME (type) == NULL)
6476 return type;
d2e4a39e 6477 else
14f9c5c9 6478 {
d2e4a39e
AS
6479 char *name = TYPE_TAG_NAME (type);
6480 struct type *type1 = ada_find_any_type (name);
14f9c5c9
AS
6481 return (type1 == NULL) ? type : type1;
6482 }
6483}
6484
6485/* A value representing the data at VALADDR/ADDRESS as described by
6486 type TYPE0, but with a standard (static-sized) type that correctly
6487 describes it. If VAL0 is not NULL and TYPE0 already is a standard
6488 type, then return VAL0 [this feature is simply to avoid redundant
d2e4a39e 6489 creation of struct values]. */
14f9c5c9 6490
d2e4a39e
AS
6491struct value *
6492ada_to_fixed_value (struct type *type0, char *valaddr, CORE_ADDR address,
6493 struct value *val0)
14f9c5c9 6494{
d2e4a39e 6495 struct type *type = ada_to_fixed_type (type0, valaddr, address, NULL);
14f9c5c9
AS
6496 if (type == type0 && val0 != NULL)
6497 return val0;
d2e4a39e
AS
6498 else
6499 return value_from_contents_and_address (type, valaddr, address);
14f9c5c9
AS
6500}
6501
6502/* A value representing VAL, but with a standard (static-sized) type
6503 chosen to approximate the real type of VAL as well as possible, but
6504 without consulting any runtime values. For Ada dynamic-sized
6505 types, therefore, the type of the result is likely to be inaccurate. */
6506
d2e4a39e
AS
6507struct value *
6508ada_to_static_fixed_value (struct value *val)
14f9c5c9 6509{
d2e4a39e 6510 struct type *type =
14f9c5c9
AS
6511 to_static_fixed_type (static_unwrap_type (VALUE_TYPE (val)));
6512 if (type == VALUE_TYPE (val))
6513 return val;
6514 else
6515 return coerce_unspec_val_to_type (val, 0, type);
6516}
d2e4a39e 6517\f
14f9c5c9
AS
6518
6519
14f9c5c9
AS
6520
6521
6522/* Attributes */
6523
6524/* Table mapping attribute numbers to names */
6525/* NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h */
6526
d2e4a39e 6527static const char *attribute_names[] = {
14f9c5c9
AS
6528 "<?>",
6529
d2e4a39e 6530 "first",
14f9c5c9
AS
6531 "last",
6532 "length",
6533 "image",
6534 "img",
6535 "max",
6536 "min",
d2e4a39e 6537 "pos" "tag",
14f9c5c9
AS
6538 "val",
6539
6540 0
6541};
6542
d2e4a39e 6543const char *
ebf56fd3 6544ada_attribute_name (int n)
14f9c5c9
AS
6545{
6546 if (n > 0 && n < (int) ATR_END)
6547 return attribute_names[n];
6548 else
6549 return attribute_names[0];
6550}
6551
6552/* Evaluate the 'POS attribute applied to ARG. */
6553
d2e4a39e
AS
6554static struct value *
6555value_pos_atr (struct value *arg)
14f9c5c9
AS
6556{
6557 struct type *type = VALUE_TYPE (arg);
6558
d2e4a39e 6559 if (!discrete_type_p (type))
14f9c5c9
AS
6560 error ("'POS only defined on discrete types");
6561
6562 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
6563 {
6564 int i;
6565 LONGEST v = value_as_long (arg);
6566
d2e4a39e 6567 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
14f9c5c9
AS
6568 {
6569 if (v == TYPE_FIELD_BITPOS (type, i))
6570 return value_from_longest (builtin_type_ada_int, i);
6571 }
6572 error ("enumeration value is invalid: can't find 'POS");
6573 }
6574 else
6575 return value_from_longest (builtin_type_ada_int, value_as_long (arg));
6576}
6577
6578/* Evaluate the TYPE'VAL attribute applied to ARG. */
6579
d2e4a39e
AS
6580static struct value *
6581value_val_atr (struct type *type, struct value *arg)
14f9c5c9 6582{
d2e4a39e 6583 if (!discrete_type_p (type))
14f9c5c9 6584 error ("'VAL only defined on discrete types");
d2e4a39e 6585 if (!integer_type_p (VALUE_TYPE (arg)))
14f9c5c9
AS
6586 error ("'VAL requires integral argument");
6587
6588 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
6589 {
6590 long pos = value_as_long (arg);
6591 if (pos < 0 || pos >= TYPE_NFIELDS (type))
6592 error ("argument to 'VAL out of range");
d2e4a39e 6593 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
14f9c5c9
AS
6594 }
6595 else
6596 return value_from_longest (type, value_as_long (arg));
6597}
14f9c5c9 6598\f
d2e4a39e 6599
14f9c5c9
AS
6600 /* Evaluation */
6601
6602/* True if TYPE appears to be an Ada character type.
6603 * [At the moment, this is true only for Character and Wide_Character;
6604 * It is a heuristic test that could stand improvement]. */
6605
d2e4a39e
AS
6606int
6607ada_is_character_type (struct type *type)
14f9c5c9 6608{
d2e4a39e
AS
6609 const char *name = ada_type_name (type);
6610 return
14f9c5c9 6611 name != NULL
d2e4a39e 6612 && (TYPE_CODE (type) == TYPE_CODE_CHAR
14f9c5c9
AS
6613 || TYPE_CODE (type) == TYPE_CODE_INT
6614 || TYPE_CODE (type) == TYPE_CODE_RANGE)
6615 && (STREQ (name, "character") || STREQ (name, "wide_character")
6616 || STREQ (name, "unsigned char"));
6617}
6618
6619/* True if TYPE appears to be an Ada string type. */
6620
6621int
ebf56fd3 6622ada_is_string_type (struct type *type)
14f9c5c9
AS
6623{
6624 CHECK_TYPEDEF (type);
d2e4a39e 6625 if (type != NULL
14f9c5c9
AS
6626 && TYPE_CODE (type) != TYPE_CODE_PTR
6627 && (ada_is_simple_array (type) || ada_is_array_descriptor (type))
6628 && ada_array_arity (type) == 1)
6629 {
6630 struct type *elttype = ada_array_element_type (type, 1);
6631
6632 return ada_is_character_type (elttype);
6633 }
d2e4a39e 6634 else
14f9c5c9
AS
6635 return 0;
6636}
6637
6638
6639/* True if TYPE is a struct type introduced by the compiler to force the
6640 alignment of a value. Such types have a single field with a
6641 distinctive name. */
6642
6643int
ebf56fd3 6644ada_is_aligner_type (struct type *type)
14f9c5c9
AS
6645{
6646 CHECK_TYPEDEF (type);
6647 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
6648 && TYPE_NFIELDS (type) == 1
6649 && STREQ (TYPE_FIELD_NAME (type, 0), "F"));
6650}
6651
6652/* If there is an ___XVS-convention type parallel to SUBTYPE, return
6653 the parallel type. */
6654
d2e4a39e
AS
6655struct type *
6656ada_get_base_type (struct type *raw_type)
14f9c5c9 6657{
d2e4a39e
AS
6658 struct type *real_type_namer;
6659 struct type *raw_real_type;
6660 struct type *real_type;
14f9c5c9
AS
6661
6662 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
6663 return raw_type;
6664
6665 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
d2e4a39e 6666 if (real_type_namer == NULL
14f9c5c9
AS
6667 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
6668 || TYPE_NFIELDS (real_type_namer) != 1)
6669 return raw_type;
6670
6671 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
d2e4a39e 6672 if (raw_real_type == NULL)
14f9c5c9
AS
6673 return raw_type;
6674 else
6675 return raw_real_type;
d2e4a39e 6676}
14f9c5c9
AS
6677
6678/* The type of value designated by TYPE, with all aligners removed. */
6679
d2e4a39e
AS
6680struct type *
6681ada_aligned_type (struct type *type)
14f9c5c9
AS
6682{
6683 if (ada_is_aligner_type (type))
6684 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
6685 else
6686 return ada_get_base_type (type);
6687}
6688
6689
6690/* The address of the aligned value in an object at address VALADDR
6691 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
6692
d2e4a39e 6693char *
ebf56fd3 6694ada_aligned_value_addr (struct type *type, char *valaddr)
14f9c5c9 6695{
d2e4a39e 6696 if (ada_is_aligner_type (type))
14f9c5c9 6697 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
d2e4a39e
AS
6698 valaddr +
6699 TYPE_FIELD_BITPOS (type,
6700 0) / TARGET_CHAR_BIT);
14f9c5c9
AS
6701 else
6702 return valaddr;
6703}
6704
6705/* The printed representation of an enumeration literal with encoded
6706 name NAME. The value is good to the next call of ada_enum_name. */
d2e4a39e
AS
6707const char *
6708ada_enum_name (const char *name)
14f9c5c9 6709{
d2e4a39e 6710 char *tmp;
14f9c5c9 6711
d2e4a39e 6712 while (1)
14f9c5c9
AS
6713 {
6714 if ((tmp = strstr (name, "__")) != NULL)
d2e4a39e 6715 name = tmp + 2;
14f9c5c9 6716 else if ((tmp = strchr (name, '.')) != NULL)
d2e4a39e 6717 name = tmp + 1;
14f9c5c9
AS
6718 else
6719 break;
6720 }
6721
6722 if (name[0] == 'Q')
6723 {
6724 static char result[16];
6725 int v;
6726 if (name[1] == 'U' || name[1] == 'W')
6727 {
d2e4a39e 6728 if (sscanf (name + 2, "%x", &v) != 1)
14f9c5c9
AS
6729 return name;
6730 }
6731 else
6732 return name;
6733
6734 if (isascii (v) && isprint (v))
6735 sprintf (result, "'%c'", v);
6736 else if (name[1] == 'U')
6737 sprintf (result, "[\"%02x\"]", v);
6738 else
6739 sprintf (result, "[\"%04x\"]", v);
6740
6741 return result;
6742 }
d2e4a39e 6743 else
14f9c5c9
AS
6744 return name;
6745}
6746
d2e4a39e 6747static struct value *
ebf56fd3
AS
6748evaluate_subexp (struct type *expect_type, struct expression *exp, int *pos,
6749 enum noside noside)
14f9c5c9
AS
6750{
6751 return (*exp->language_defn->evaluate_exp) (expect_type, exp, pos, noside);
6752}
6753
6754/* Evaluate the subexpression of EXP starting at *POS as for
6755 evaluate_type, updating *POS to point just past the evaluated
6756 expression. */
6757
d2e4a39e
AS
6758static struct value *
6759evaluate_subexp_type (struct expression *exp, int *pos)
14f9c5c9 6760{
d2e4a39e 6761 return (*exp->language_defn->evaluate_exp)
14f9c5c9
AS
6762 (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
6763}
6764
6765/* If VAL is wrapped in an aligner or subtype wrapper, return the
d2e4a39e 6766 value it wraps. */
14f9c5c9 6767
d2e4a39e
AS
6768static struct value *
6769unwrap_value (struct value *val)
14f9c5c9 6770{
d2e4a39e 6771 struct type *type = check_typedef (VALUE_TYPE (val));
14f9c5c9
AS
6772 if (ada_is_aligner_type (type))
6773 {
d2e4a39e
AS
6774 struct value *v = value_struct_elt (&val, NULL, "F",
6775 NULL, "internal structure");
6776 struct type *val_type = check_typedef (VALUE_TYPE (v));
14f9c5c9
AS
6777 if (ada_type_name (val_type) == NULL)
6778 TYPE_NAME (val_type) = ada_type_name (type);
6779
6780 return unwrap_value (v);
6781 }
d2e4a39e 6782 else
14f9c5c9 6783 {
d2e4a39e 6784 struct type *raw_real_type =
14f9c5c9 6785 ada_completed_type (ada_get_base_type (type));
d2e4a39e 6786
14f9c5c9
AS
6787 if (type == raw_real_type)
6788 return val;
6789
d2e4a39e
AS
6790 return
6791 coerce_unspec_val_to_type
14f9c5c9
AS
6792 (val, 0, ada_to_fixed_type (raw_real_type, 0,
6793 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
6794 NULL));
6795 }
6796}
d2e4a39e
AS
6797
6798static struct value *
6799cast_to_fixed (struct type *type, struct value *arg)
14f9c5c9
AS
6800{
6801 LONGEST val;
6802
6803 if (type == VALUE_TYPE (arg))
6804 return arg;
6805 else if (ada_is_fixed_point_type (VALUE_TYPE (arg)))
d2e4a39e 6806 val = ada_float_to_fixed (type,
14f9c5c9
AS
6807 ada_fixed_to_float (VALUE_TYPE (arg),
6808 value_as_long (arg)));
d2e4a39e 6809 else
14f9c5c9 6810 {
d2e4a39e 6811 DOUBLEST argd =
14f9c5c9
AS
6812 value_as_double (value_cast (builtin_type_double, value_copy (arg)));
6813 val = ada_float_to_fixed (type, argd);
6814 }
6815
6816 return value_from_longest (type, val);
6817}
6818
d2e4a39e
AS
6819static struct value *
6820cast_from_fixed_to_double (struct value *arg)
14f9c5c9
AS
6821{
6822 DOUBLEST val = ada_fixed_to_float (VALUE_TYPE (arg),
6823 value_as_long (arg));
6824 return value_from_double (builtin_type_double, val);
6825}
6826
6827/* Coerce VAL as necessary for assignment to an lval of type TYPE, and
6828 * return the converted value. */
d2e4a39e
AS
6829static struct value *
6830coerce_for_assign (struct type *type, struct value *val)
14f9c5c9 6831{
d2e4a39e 6832 struct type *type2 = VALUE_TYPE (val);
14f9c5c9
AS
6833 if (type == type2)
6834 return val;
6835
6836 CHECK_TYPEDEF (type2);
6837 CHECK_TYPEDEF (type);
6838
d2e4a39e
AS
6839 if (TYPE_CODE (type2) == TYPE_CODE_PTR
6840 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
14f9c5c9
AS
6841 {
6842 val = ada_value_ind (val);
6843 type2 = VALUE_TYPE (val);
6844 }
6845
d2e4a39e 6846 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
14f9c5c9
AS
6847 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
6848 {
6849 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
6850 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
d2e4a39e 6851 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
14f9c5c9
AS
6852 error ("Incompatible types in assignment");
6853 VALUE_TYPE (val) = type;
6854 }
d2e4a39e 6855 return val;
14f9c5c9
AS
6856}
6857
d2e4a39e 6858struct value *
ebf56fd3
AS
6859ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
6860 int *pos, enum noside noside)
14f9c5c9
AS
6861{
6862 enum exp_opcode op;
6863 enum ada_attribute atr;
6864 int tem, tem2, tem3;
6865 int pc;
6866 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
6867 struct type *type;
6868 int nargs;
d2e4a39e 6869 struct value **argvec;
14f9c5c9 6870
d2e4a39e
AS
6871 pc = *pos;
6872 *pos += 1;
14f9c5c9
AS
6873 op = exp->elts[pc].opcode;
6874
d2e4a39e 6875 switch (op)
14f9c5c9
AS
6876 {
6877 default:
6878 *pos -= 1;
d2e4a39e
AS
6879 return
6880 unwrap_value (evaluate_subexp_standard
6881 (expect_type, exp, pos, noside));
14f9c5c9
AS
6882
6883 case UNOP_CAST:
6884 (*pos) += 2;
6885 type = exp->elts[pc + 1].type;
6886 arg1 = evaluate_subexp (type, exp, pos, noside);
6887 if (noside == EVAL_SKIP)
6888 goto nosideret;
6889 if (type != check_typedef (VALUE_TYPE (arg1)))
6890 {
6891 if (ada_is_fixed_point_type (type))
6892 arg1 = cast_to_fixed (type, arg1);
6893 else if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
6894 arg1 = value_cast (type, cast_from_fixed_to_double (arg1));
d2e4a39e 6895 else if (VALUE_LVAL (arg1) == lval_memory)
14f9c5c9
AS
6896 {
6897 /* This is in case of the really obscure (and undocumented,
d2e4a39e
AS
6898 but apparently expected) case of (Foo) Bar.all, where Bar
6899 is an integer constant and Foo is a dynamic-sized type.
6900 If we don't do this, ARG1 will simply be relabeled with
6901 TYPE. */
6902 if (noside == EVAL_AVOID_SIDE_EFFECTS)
14f9c5c9 6903 return value_zero (to_static_fixed_type (type), not_lval);
d2e4a39e
AS
6904 arg1 =
6905 ada_to_fixed_value
6906 (type, 0, VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), 0);
14f9c5c9 6907 }
d2e4a39e
AS
6908 else
6909 arg1 = value_cast (type, arg1);
14f9c5c9
AS
6910 }
6911 return arg1;
6912
6913 /* FIXME: UNOP_QUAL should be defined in expression.h */
6914 /* case UNOP_QUAL:
d2e4a39e
AS
6915 (*pos) += 2;
6916 type = exp->elts[pc + 1].type;
6917 return ada_evaluate_subexp (type, exp, pos, noside);
6918 */
14f9c5c9
AS
6919 case BINOP_ASSIGN:
6920 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
6921 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
6922 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
6923 return arg1;
6924 if (binop_user_defined_p (op, arg1, arg2))
6925 return value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
d2e4a39e 6926 else
14f9c5c9
AS
6927 {
6928 if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
6929 arg2 = cast_to_fixed (VALUE_TYPE (arg1), arg2);
6930 else if (ada_is_fixed_point_type (VALUE_TYPE (arg2)))
d2e4a39e
AS
6931 error
6932 ("Fixed-point values must be assigned to fixed-point variables");
6933 else
14f9c5c9
AS
6934 arg2 = coerce_for_assign (VALUE_TYPE (arg1), arg2);
6935 return ada_value_assign (arg1, arg2);
6936 }
6937
6938 case BINOP_ADD:
6939 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
6940 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
6941 if (noside == EVAL_SKIP)
6942 goto nosideret;
6943 if (binop_user_defined_p (op, arg1, arg2))
6944 return value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
6945 else
6946 {
6947 if ((ada_is_fixed_point_type (VALUE_TYPE (arg1))
6948 || ada_is_fixed_point_type (VALUE_TYPE (arg2)))
6949 && VALUE_TYPE (arg1) != VALUE_TYPE (arg2))
d2e4a39e
AS
6950 error
6951 ("Operands of fixed-point addition must have the same type");
14f9c5c9
AS
6952 return value_cast (VALUE_TYPE (arg1), value_add (arg1, arg2));
6953 }
6954
6955 case BINOP_SUB:
6956 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
6957 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
6958 if (noside == EVAL_SKIP)
6959 goto nosideret;
6960 if (binop_user_defined_p (op, arg1, arg2))
6961 return value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
6962 else
6963 {
6964 if ((ada_is_fixed_point_type (VALUE_TYPE (arg1))
6965 || ada_is_fixed_point_type (VALUE_TYPE (arg2)))
6966 && VALUE_TYPE (arg1) != VALUE_TYPE (arg2))
d2e4a39e
AS
6967 error
6968 ("Operands of fixed-point subtraction must have the same type");
14f9c5c9
AS
6969 return value_cast (VALUE_TYPE (arg1), value_sub (arg1, arg2));
6970 }
6971
6972 case BINOP_MUL:
6973 case BINOP_DIV:
6974 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
6975 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
6976 if (noside == EVAL_SKIP)
6977 goto nosideret;
6978 if (binop_user_defined_p (op, arg1, arg2))
6979 return value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
6980 else
6981 if (noside == EVAL_AVOID_SIDE_EFFECTS
6982 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
d2e4a39e 6983 return value_zero (VALUE_TYPE (arg1), not_lval);
14f9c5c9
AS
6984 else
6985 {
6986 if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
6987 arg1 = cast_from_fixed_to_double (arg1);
6988 if (ada_is_fixed_point_type (VALUE_TYPE (arg2)))
6989 arg2 = cast_from_fixed_to_double (arg2);
6990 return value_binop (arg1, arg2, op);
6991 }
6992
6993 case UNOP_NEG:
6994 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
6995 if (noside == EVAL_SKIP)
6996 goto nosideret;
6997 if (unop_user_defined_p (op, arg1))
6998 return value_x_unop (arg1, op, EVAL_NORMAL);
6999 else if (ada_is_fixed_point_type (VALUE_TYPE (arg1)))
7000 return value_cast (VALUE_TYPE (arg1), value_neg (arg1));
7001 else
7002 return value_neg (arg1);
7003
7004 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
7005 /* case OP_UNRESOLVED_VALUE:
d2e4a39e 7006 /* Only encountered when an unresolved symbol occurs in a
14f9c5c9 7007 context other than a function call, in which case, it is
d2e4a39e
AS
7008 illegal. *//*
7009 (*pos) += 3;
7010 if (noside == EVAL_SKIP)
7011 goto nosideret;
7012 else
7013 error ("Unexpected unresolved symbol, %s, during evaluation",
7014 ada_demangle (exp->elts[pc + 2].name));
7015 */
14f9c5c9
AS
7016 case OP_VAR_VALUE:
7017 *pos -= 1;
7018 if (noside == EVAL_SKIP)
7019 {
7020 *pos += 4;
7021 goto nosideret;
d2e4a39e 7022 }
14f9c5c9
AS
7023 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7024 {
7025 *pos += 4;
d2e4a39e
AS
7026 return value_zero
7027 (to_static_fixed_type
7028 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
14f9c5c9
AS
7029 not_lval);
7030 }
d2e4a39e 7031 else
14f9c5c9 7032 {
d2e4a39e
AS
7033 arg1 =
7034 unwrap_value (evaluate_subexp_standard
7035 (expect_type, exp, pos, noside));
14f9c5c9 7036 return ada_to_fixed_value (VALUE_TYPE (arg1), 0,
d2e4a39e
AS
7037 VALUE_ADDRESS (arg1) +
7038 VALUE_OFFSET (arg1), arg1);
14f9c5c9
AS
7039 }
7040
7041 case OP_ARRAY:
7042 (*pos) += 3;
7043 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
7044 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
7045 nargs = tem3 - tem2 + 1;
7046 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
7047
d2e4a39e
AS
7048 argvec =
7049 (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
14f9c5c9
AS
7050 for (tem = 0; tem == 0 || tem < nargs; tem += 1)
7051 /* At least one element gets inserted for the type */
7052 {
7053 /* Ensure that array expressions are coerced into pointer objects. */
7054 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
7055 }
7056 if (noside == EVAL_SKIP)
7057 goto nosideret;
7058 return value_array (tem2, tem3, argvec);
7059
7060 case OP_FUNCALL:
7061 (*pos) += 2;
7062
7063 /* Allocate arg vector, including space for the function to be
d2e4a39e 7064 called in argvec[0] and a terminating NULL */
14f9c5c9 7065 nargs = longest_to_int (exp->elts[pc + 1].longconst);
d2e4a39e
AS
7066 argvec =
7067 (struct value * *) alloca (sizeof (struct value *) * (nargs + 2));
14f9c5c9
AS
7068
7069 /* FIXME: OP_UNRESOLVED_VALUE should be defined in expression.h */
7070 /* FIXME: name should be defined in expresion.h */
7071 /* if (exp->elts[*pos].opcode == OP_UNRESOLVED_VALUE)
d2e4a39e
AS
7072 error ("Unexpected unresolved symbol, %s, during evaluation",
7073 ada_demangle (exp->elts[pc + 5].name));
7074 */
7075 if (0)
14f9c5c9
AS
7076 {
7077 error ("unexpected code path, FIXME");
7078 }
7079 else
7080 {
7081 for (tem = 0; tem <= nargs; tem += 1)
7082 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7083 argvec[tem] = 0;
7084
7085 if (noside == EVAL_SKIP)
7086 goto nosideret;
7087 }
7088
7089 if (TYPE_CODE (VALUE_TYPE (argvec[0])) == TYPE_CODE_REF)
7090 argvec[0] = value_addr (argvec[0]);
7091
7092 if (ada_is_packed_array_type (VALUE_TYPE (argvec[0])))
7093 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
7094
7095 type = check_typedef (VALUE_TYPE (argvec[0]));
7096 if (TYPE_CODE (type) == TYPE_CODE_PTR)
d2e4a39e 7097 {
14f9c5c9
AS
7098 switch (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (type))))
7099 {
7100 case TYPE_CODE_FUNC:
7101 type = check_typedef (TYPE_TARGET_TYPE (type));
7102 break;
7103 case TYPE_CODE_ARRAY:
7104 break;
7105 case TYPE_CODE_STRUCT:
7106 if (noside != EVAL_AVOID_SIDE_EFFECTS)
7107 argvec[0] = ada_value_ind (argvec[0]);
7108 type = check_typedef (TYPE_TARGET_TYPE (type));
7109 break;
7110 default:
7111 error ("cannot subscript or call something of type `%s'",
7112 ada_type_name (VALUE_TYPE (argvec[0])));
7113 break;
d2e4a39e 7114 }
14f9c5c9 7115 }
d2e4a39e 7116
14f9c5c9
AS
7117 switch (TYPE_CODE (type))
7118 {
7119 case TYPE_CODE_FUNC:
7120 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7121 return allocate_value (TYPE_TARGET_TYPE (type));
7122 return call_function_by_hand (argvec[0], nargs, argvec + 1);
d2e4a39e 7123 case TYPE_CODE_STRUCT:
14f9c5c9
AS
7124 {
7125 int arity = ada_array_arity (type);
7126 type = ada_array_element_type (type, nargs);
d2e4a39e 7127 if (type == NULL)
14f9c5c9 7128 error ("cannot subscript or call a record");
d2e4a39e 7129 if (arity != nargs)
14f9c5c9 7130 error ("wrong number of subscripts; expecting %d", arity);
d2e4a39e 7131 if (noside == EVAL_AVOID_SIDE_EFFECTS)
14f9c5c9 7132 return allocate_value (ada_aligned_type (type));
d2e4a39e
AS
7133 return
7134 unwrap_value (ada_value_subscript
7135 (argvec[0], nargs, argvec + 1));
14f9c5c9
AS
7136 }
7137 case TYPE_CODE_ARRAY:
7138 if (noside == EVAL_AVOID_SIDE_EFFECTS)
d2e4a39e 7139 {
14f9c5c9
AS
7140 type = ada_array_element_type (type, nargs);
7141 if (type == NULL)
7142 error ("element type of array unknown");
7143 else
7144 return allocate_value (ada_aligned_type (type));
7145 }
d2e4a39e 7146 return
14f9c5c9
AS
7147 unwrap_value (ada_value_subscript
7148 (ada_coerce_to_simple_array (argvec[0]),
d2e4a39e
AS
7149 nargs, argvec + 1));
7150 case TYPE_CODE_PTR: /* Pointer to array */
14f9c5c9
AS
7151 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
7152 if (noside == EVAL_AVOID_SIDE_EFFECTS)
d2e4a39e 7153 {
14f9c5c9
AS
7154 type = ada_array_element_type (type, nargs);
7155 if (type == NULL)
7156 error ("element type of array unknown");
7157 else
7158 return allocate_value (ada_aligned_type (type));
7159 }
d2e4a39e
AS
7160 return
7161 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
7162 nargs, argvec + 1));
14f9c5c9
AS
7163
7164 default:
7165 error ("Internal error in evaluate_subexp");
7166 }
7167
7168 case TERNOP_SLICE:
7169 {
d2e4a39e 7170 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9
AS
7171 int lowbound
7172 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
7173 int upper
7174 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
7175 if (noside == EVAL_SKIP)
7176 goto nosideret;
d2e4a39e
AS
7177
7178 /* If this is a reference to an array, then dereference it */
7179 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_REF
7180 && TYPE_TARGET_TYPE (VALUE_TYPE (array)) != NULL
7181 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (array))) ==
7182 TYPE_CODE_ARRAY
7183 && !ada_is_array_descriptor (check_typedef (VALUE_TYPE (array))))
7184 {
7185 array = ada_coerce_ref (array);
7186 }
14f9c5c9
AS
7187
7188 if (noside == EVAL_AVOID_SIDE_EFFECTS &&
7189 ada_is_array_descriptor (check_typedef (VALUE_TYPE (array))))
7190 {
7191 /* Try to dereference the array, in case it is an access to array */
d2e4a39e 7192 struct type *arrType = ada_type_of_array (array, 0);
14f9c5c9 7193 if (arrType != NULL)
d2e4a39e 7194 array = value_at_lazy (arrType, 0, NULL);
14f9c5c9
AS
7195 }
7196 if (ada_is_array_descriptor (VALUE_TYPE (array)))
7197 array = ada_coerce_to_simple_array (array);
7198
d2e4a39e
AS
7199 /* If at this point we have a pointer to an array, it means that
7200 it is a pointer to a simple (non-ada) array. We just then
7201 dereference it */
7202 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_PTR
7203 && TYPE_TARGET_TYPE (VALUE_TYPE (array)) != NULL
7204 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (array))) ==
7205 TYPE_CODE_ARRAY)
7206 {
7207 array = ada_value_ind (array);
7208 }
7209
14f9c5c9
AS
7210 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7211 /* The following will get the bounds wrong, but only in contexts
7212 where the value is not being requested (FIXME?). */
7213 return array;
7214 else
7215 return value_slice (array, lowbound, upper - lowbound + 1);
7216 }
7217
7218 /* FIXME: UNOP_MBR should be defined in expression.h */
7219 /* case UNOP_MBR:
d2e4a39e
AS
7220 (*pos) += 2;
7221 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7222 type = exp->elts[pc + 1].type;
7223
7224 if (noside == EVAL_SKIP)
7225 goto nosideret;
7226
7227 switch (TYPE_CODE (type))
7228 {
7229 default:
7230 warning ("Membership test incompletely implemented; always returns true");
7231 return value_from_longest (builtin_type_int, (LONGEST) 1);
7232
7233 case TYPE_CODE_RANGE:
7234 arg2 = value_from_longest (builtin_type_int,
7235 (LONGEST) TYPE_LOW_BOUND (type));
7236 arg3 = value_from_longest (builtin_type_int,
7237 (LONGEST) TYPE_HIGH_BOUND (type));
7238 return
7239 value_from_longest (builtin_type_int,
7240 (value_less (arg1,arg3)
7241 || value_equal (arg1,arg3))
7242 && (value_less (arg2,arg1)
7243 || value_equal (arg2,arg1)));
7244 }
7245 */
7246 /* FIXME: BINOP_MBR should be defined in expression.h */
14f9c5c9 7247 /* case BINOP_MBR:
d2e4a39e
AS
7248 (*pos) += 2;
7249 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7250 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
14f9c5c9 7251
d2e4a39e
AS
7252 if (noside == EVAL_SKIP)
7253 goto nosideret;
14f9c5c9 7254
d2e4a39e
AS
7255 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7256 return value_zero (builtin_type_int, not_lval);
14f9c5c9 7257
d2e4a39e 7258 tem = longest_to_int (exp->elts[pc + 1].longconst);
14f9c5c9 7259
d2e4a39e
AS
7260 if (tem < 1 || tem > ada_array_arity (VALUE_TYPE (arg2)))
7261 error ("invalid dimension number to '%s", "range");
14f9c5c9 7262
d2e4a39e
AS
7263 arg3 = ada_array_bound (arg2, tem, 1);
7264 arg2 = ada_array_bound (arg2, tem, 0);
14f9c5c9 7265
d2e4a39e
AS
7266 return
7267 value_from_longest (builtin_type_int,
7268 (value_less (arg1,arg3)
7269 || value_equal (arg1,arg3))
7270 && (value_less (arg2,arg1)
7271 || value_equal (arg2,arg1)));
7272 */
14f9c5c9
AS
7273 /* FIXME: TERNOP_MBR should be defined in expression.h */
7274 /* case TERNOP_MBR:
d2e4a39e
AS
7275 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7276 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7277 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7278
7279 if (noside == EVAL_SKIP)
7280 goto nosideret;
7281
7282 return
7283 value_from_longest (builtin_type_int,
7284 (value_less (arg1,arg3)
7285 || value_equal (arg1,arg3))
7286 && (value_less (arg2,arg1)
7287 || value_equal (arg2,arg1)));
7288 */
14f9c5c9
AS
7289 /* FIXME: OP_ATTRIBUTE should be defined in expression.h */
7290 /* case OP_ATTRIBUTE:
d2e4a39e
AS
7291 *pos += 3;
7292 atr = (enum ada_attribute) longest_to_int (exp->elts[pc + 2].longconst);
7293 switch (atr)
7294 {
7295 default:
7296 error ("unexpected attribute encountered");
7297
7298 case ATR_FIRST:
7299 case ATR_LAST:
7300 case ATR_LENGTH:
7301 {
7302 struct type* type_arg;
7303 if (exp->elts[*pos].opcode == OP_TYPE)
7304 {
7305 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7306 arg1 = NULL;
7307 type_arg = exp->elts[pc + 5].type;
7308 }
7309 else
7310 {
7311 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7312 type_arg = NULL;
7313 }
7314
7315 if (exp->elts[*pos].opcode != OP_LONG)
7316 error ("illegal operand to '%s", ada_attribute_name (atr));
7317 tem = longest_to_int (exp->elts[*pos+2].longconst);
7318 *pos += 4;
7319
7320 if (noside == EVAL_SKIP)
7321 goto nosideret;
7322
7323 if (type_arg == NULL)
7324 {
7325 arg1 = ada_coerce_ref (arg1);
7326
7327 if (ada_is_packed_array_type (VALUE_TYPE (arg1)))
7328 arg1 = ada_coerce_to_simple_array (arg1);
7329
7330 if (tem < 1 || tem > ada_array_arity (VALUE_TYPE (arg1)))
7331 error ("invalid dimension number to '%s",
7332 ada_attribute_name (atr));
7333
7334 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7335 {
7336 type = ada_index_type (VALUE_TYPE (arg1), tem);
7337 if (type == NULL)
7338 error ("attempt to take bound of something that is not an array");
7339 return allocate_value (type);
7340 }
7341
7342 switch (atr)
7343 {
7344 default:
7345 error ("unexpected attribute encountered");
7346 case ATR_FIRST:
7347 return ada_array_bound (arg1, tem, 0);
7348 case ATR_LAST:
7349 return ada_array_bound (arg1, tem, 1);
7350 case ATR_LENGTH:
7351 return ada_array_length (arg1, tem);
7352 }
7353 }
7354 else if (TYPE_CODE (type_arg) == TYPE_CODE_RANGE
7355 || TYPE_CODE (type_arg) == TYPE_CODE_INT)
7356 {
7357 struct type* range_type;
7358 char* name = ada_type_name (type_arg);
7359 if (name == NULL)
7360 {
7361 if (TYPE_CODE (type_arg) == TYPE_CODE_RANGE)
7362 range_type = type_arg;
7363 else
7364 error ("unimplemented type attribute");
7365 }
7366 else
7367 range_type =
7368 to_fixed_range_type (name, NULL, TYPE_OBJFILE (type_arg));
7369 switch (atr)
7370 {
7371 default:
7372 error ("unexpected attribute encountered");
7373 case ATR_FIRST:
7374 return value_from_longest (TYPE_TARGET_TYPE (range_type),
7375 TYPE_LOW_BOUND (range_type));
7376 case ATR_LAST:
7377 return value_from_longest (TYPE_TARGET_TYPE (range_type),
7378 TYPE_HIGH_BOUND (range_type));
7379 }
7380 }
7381 else if (TYPE_CODE (type_arg) == TYPE_CODE_ENUM)
7382 {
7383 switch (atr)
7384 {
7385 default:
7386 error ("unexpected attribute encountered");
7387 case ATR_FIRST:
7388 return value_from_longest
7389 (type_arg, TYPE_FIELD_BITPOS (type_arg, 0));
7390 case ATR_LAST:
7391 return value_from_longest
7392 (type_arg,
7393 TYPE_FIELD_BITPOS (type_arg,
7394 TYPE_NFIELDS (type_arg) - 1));
7395 }
7396 }
7397 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
7398 error ("unimplemented type attribute");
7399 else
7400 {
7401 LONGEST low, high;
7402
7403 if (ada_is_packed_array_type (type_arg))
7404 type_arg = decode_packed_array_type (type_arg);
7405
7406 if (tem < 1 || tem > ada_array_arity (type_arg))
7407 error ("invalid dimension number to '%s",
7408 ada_attribute_name (atr));
7409
7410 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7411 {
7412 type = ada_index_type (type_arg, tem);
7413 if (type == NULL)
7414 error ("attempt to take bound of something that is not an array");
7415 return allocate_value (type);
7416 }
7417
7418 switch (atr)
7419 {
7420 default:
7421 error ("unexpected attribute encountered");
7422 case ATR_FIRST:
7423 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
7424 return value_from_longest (type, low);
7425 case ATR_LAST:
7426 high = ada_array_bound_from_type (type_arg, tem, 1, &type);
7427 return value_from_longest (type, high);
7428 case ATR_LENGTH:
7429 low = ada_array_bound_from_type (type_arg, tem, 0, &type);
7430 high = ada_array_bound_from_type (type_arg, tem, 1, NULL);
7431 return value_from_longest (type, high-low+1);
7432 }
7433 }
7434 }
7435
7436 case ATR_TAG:
7437 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7438 if (noside == EVAL_SKIP)
7439 goto nosideret;
7440
7441 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7442 return
7443 value_zero (ada_tag_type (arg1), not_lval);
7444
7445 return ada_value_tag (arg1);
7446
7447 case ATR_MIN:
7448 case ATR_MAX:
7449 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7450 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7451 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7452 if (noside == EVAL_SKIP)
7453 goto nosideret;
7454 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7455 return value_zero (VALUE_TYPE (arg1), not_lval);
7456 else
7457 return value_binop (arg1, arg2,
7458 atr == ATR_MIN ? BINOP_MIN : BINOP_MAX);
7459
7460 case ATR_MODULUS:
7461 {
7462 struct type* type_arg = exp->elts[pc + 5].type;
7463 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7464 *pos += 4;
7465
7466 if (noside == EVAL_SKIP)
7467 goto nosideret;
7468
7469 if (! ada_is_modular_type (type_arg))
7470 error ("'modulus must be applied to modular type");
7471
7472 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
7473 ada_modulus (type_arg));
7474 }
7475
7476
7477 case ATR_POS:
7478 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7479 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7480 if (noside == EVAL_SKIP)
7481 goto nosideret;
7482 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7483 return value_zero (builtin_type_ada_int, not_lval);
7484 else
7485 return value_pos_atr (arg1);
7486
7487 case ATR_SIZE:
7488 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7489 if (noside == EVAL_SKIP)
7490 goto nosideret;
7491 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7492 return value_zero (builtin_type_ada_int, not_lval);
7493 else
7494 return value_from_longest (builtin_type_ada_int,
7495 TARGET_CHAR_BIT
7496 * TYPE_LENGTH (VALUE_TYPE (arg1)));
7497
7498 case ATR_VAL:
7499 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
7500 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7501 type = exp->elts[pc + 5].type;
7502 if (noside == EVAL_SKIP)
7503 goto nosideret;
7504 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7505 return value_zero (type, not_lval);
7506 else
7507 return value_val_atr (type, arg1);
7508 } */
14f9c5c9
AS
7509 case BINOP_EXP:
7510 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7511 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7512 if (noside == EVAL_SKIP)
7513 goto nosideret;
7514 if (binop_user_defined_p (op, arg1, arg2))
7515 return unwrap_value (value_x_binop (arg1, arg2, op, OP_NULL,
d2e4a39e
AS
7516 EVAL_NORMAL));
7517 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7518 return value_zero (VALUE_TYPE (arg1), not_lval);
14f9c5c9
AS
7519 else
7520 return value_binop (arg1, arg2, op);
7521
7522 case UNOP_PLUS:
7523 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7524 if (noside == EVAL_SKIP)
7525 goto nosideret;
7526 if (unop_user_defined_p (op, arg1))
7527 return unwrap_value (value_x_unop (arg1, op, EVAL_NORMAL));
7528 else
7529 return arg1;
7530
7531 case UNOP_ABS:
7532 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7533 if (noside == EVAL_SKIP)
7534 goto nosideret;
7535 if (value_less (arg1, value_zero (VALUE_TYPE (arg1), not_lval)))
7536 return value_neg (arg1);
7537 else
7538 return arg1;
7539
7540 case UNOP_IND:
7541 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
d2e4a39e 7542 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
14f9c5c9
AS
7543 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
7544 if (noside == EVAL_SKIP)
7545 goto nosideret;
7546 type = check_typedef (VALUE_TYPE (arg1));
7547 if (noside == EVAL_AVOID_SIDE_EFFECTS)
7548 {
7549 if (ada_is_array_descriptor (type))
7550 /* GDB allows dereferencing GNAT array descriptors. */
7551 {
d2e4a39e 7552 struct type *arrType = ada_type_of_array (arg1, 0);
14f9c5c9
AS
7553 if (arrType == NULL)
7554 error ("Attempt to dereference null array pointer.");
7555 return value_at_lazy (arrType, 0, NULL);
7556 }
7557 else if (TYPE_CODE (type) == TYPE_CODE_PTR
d2e4a39e
AS
7558 || TYPE_CODE (type) == TYPE_CODE_REF
7559 /* In C you can dereference an array to get the 1st elt. */
7560 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
7561 return
7562 value_zero
7563 (to_static_fixed_type
7564 (ada_aligned_type (check_typedef (TYPE_TARGET_TYPE (type)))),
7565 lval_memory);
14f9c5c9
AS
7566 else if (TYPE_CODE (type) == TYPE_CODE_INT)
7567 /* GDB allows dereferencing an int. */
7568 return value_zero (builtin_type_int, lval_memory);
7569 else
7570 error ("Attempt to take contents of a non-pointer value.");
7571 }
7572 arg1 = ada_coerce_ref (arg1);
7573 type = check_typedef (VALUE_TYPE (arg1));
d2e4a39e 7574
14f9c5c9
AS
7575 if (ada_is_array_descriptor (type))
7576 /* GDB allows dereferencing GNAT array descriptors. */
7577 return ada_coerce_to_simple_array (arg1);
7578 else
7579 return ada_value_ind (arg1);
7580
7581 case STRUCTOP_STRUCT:
7582 tem = longest_to_int (exp->elts[pc + 1].longconst);
7583 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
7584 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7585 if (noside == EVAL_SKIP)
7586 goto nosideret;
7587 if (noside == EVAL_AVOID_SIDE_EFFECTS)
d2e4a39e 7588 return value_zero (ada_aligned_type
14f9c5c9 7589 (ada_lookup_struct_elt_type (VALUE_TYPE (arg1),
d2e4a39e
AS
7590 &exp->elts[pc +
7591 2].string,
14f9c5c9
AS
7592 0, NULL)),
7593 lval_memory);
7594 else
7595 return unwrap_value (ada_value_struct_elt (arg1,
7596 &exp->elts[pc + 2].string,
7597 "record"));
7598 case OP_TYPE:
7599 /* The value is not supposed to be used. This is here to make it
7600 easier to accommodate expressions that contain types. */
7601 (*pos) += 2;
7602 if (noside == EVAL_SKIP)
7603 goto nosideret;
7604 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
7605 return allocate_value (builtin_type_void);
d2e4a39e 7606 else
14f9c5c9 7607 error ("Attempt to use a type name as an expression");
d2e4a39e 7608
14f9c5c9
AS
7609 case STRUCTOP_PTR:
7610 tem = longest_to_int (exp->elts[pc + 1].longconst);
7611 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
7612 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
7613 if (noside == EVAL_SKIP)
7614 goto nosideret;
7615 if (noside == EVAL_AVOID_SIDE_EFFECTS)
d2e4a39e 7616 return value_zero (ada_aligned_type
14f9c5c9 7617 (ada_lookup_struct_elt_type (VALUE_TYPE (arg1),
d2e4a39e
AS
7618 &exp->elts[pc +
7619 2].string,
14f9c5c9
AS
7620 0, NULL)),
7621 lval_memory);
7622 else
7623 return unwrap_value (ada_value_struct_elt (arg1,
7624 &exp->elts[pc + 2].string,
7625 "record access"));
7626 }
7627
7628nosideret:
7629 return value_from_longest (builtin_type_long, (LONGEST) 1);
7630}
14f9c5c9 7631\f
d2e4a39e 7632
14f9c5c9
AS
7633 /* Fixed point */
7634
7635/* If TYPE encodes an Ada fixed-point type, return the suffix of the
7636 type name that encodes the 'small and 'delta information.
7637 Otherwise, return NULL. */
7638
d2e4a39e 7639static const char *
ebf56fd3 7640fixed_type_info (struct type *type)
14f9c5c9 7641{
d2e4a39e 7642 const char *name = ada_type_name (type);
14f9c5c9
AS
7643 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
7644
d2e4a39e
AS
7645 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
7646 {
14f9c5c9
AS
7647 const char *tail = strstr (name, "___XF_");
7648 if (tail == NULL)
7649 return NULL;
d2e4a39e 7650 else
14f9c5c9
AS
7651 return tail + 5;
7652 }
7653 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
7654 return fixed_type_info (TYPE_TARGET_TYPE (type));
7655 else
7656 return NULL;
7657}
7658
7659/* Returns non-zero iff TYPE represents an Ada fixed-point type. */
7660
7661int
ebf56fd3 7662ada_is_fixed_point_type (struct type *type)
14f9c5c9
AS
7663{
7664 return fixed_type_info (type) != NULL;
7665}
7666
7667/* Assuming that TYPE is the representation of an Ada fixed-point
7668 type, return its delta, or -1 if the type is malformed and the
7669 delta cannot be determined. */
7670
7671DOUBLEST
ebf56fd3 7672ada_delta (struct type *type)
14f9c5c9
AS
7673{
7674 const char *encoding = fixed_type_info (type);
7675 long num, den;
7676
7677 if (sscanf (encoding, "_%ld_%ld", &num, &den) < 2)
7678 return -1.0;
d2e4a39e 7679 else
14f9c5c9
AS
7680 return (DOUBLEST) num / (DOUBLEST) den;
7681}
7682
7683/* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
7684 factor ('SMALL value) associated with the type. */
7685
7686static DOUBLEST
ebf56fd3 7687scaling_factor (struct type *type)
14f9c5c9
AS
7688{
7689 const char *encoding = fixed_type_info (type);
7690 unsigned long num0, den0, num1, den1;
7691 int n;
d2e4a39e 7692
14f9c5c9
AS
7693 n = sscanf (encoding, "_%lu_%lu_%lu_%lu", &num0, &den0, &num1, &den1);
7694
7695 if (n < 2)
7696 return 1.0;
7697 else if (n == 4)
7698 return (DOUBLEST) num1 / (DOUBLEST) den1;
d2e4a39e 7699 else
14f9c5c9
AS
7700 return (DOUBLEST) num0 / (DOUBLEST) den0;
7701}
7702
7703
7704/* Assuming that X is the representation of a value of fixed-point
7705 type TYPE, return its floating-point equivalent. */
7706
7707DOUBLEST
ebf56fd3 7708ada_fixed_to_float (struct type *type, LONGEST x)
14f9c5c9 7709{
d2e4a39e 7710 return (DOUBLEST) x *scaling_factor (type);
14f9c5c9
AS
7711}
7712
7713/* The representation of a fixed-point value of type TYPE
7714 corresponding to the value X. */
7715
7716LONGEST
ebf56fd3 7717ada_float_to_fixed (struct type *type, DOUBLEST x)
14f9c5c9
AS
7718{
7719 return (LONGEST) (x / scaling_factor (type) + 0.5);
7720}
7721
7722
7723 /* VAX floating formats */
7724
7725/* Non-zero iff TYPE represents one of the special VAX floating-point
7726 types. */
7727int
d2e4a39e 7728ada_is_vax_floating_type (struct type *type)
14f9c5c9 7729{
d2e4a39e 7730 int name_len =
14f9c5c9 7731 (ada_type_name (type) == NULL) ? 0 : strlen (ada_type_name (type));
d2e4a39e 7732 return
14f9c5c9 7733 name_len > 6
d2e4a39e 7734 && (TYPE_CODE (type) == TYPE_CODE_INT
14f9c5c9
AS
7735 || TYPE_CODE (type) == TYPE_CODE_RANGE)
7736 && STREQN (ada_type_name (type) + name_len - 6, "___XF", 5);
7737}
7738
7739/* The type of special VAX floating-point type this is, assuming
7740 ada_is_vax_floating_point */
7741int
d2e4a39e 7742ada_vax_float_type_suffix (struct type *type)
14f9c5c9 7743{
d2e4a39e 7744 return ada_type_name (type)[strlen (ada_type_name (type)) - 1];
14f9c5c9
AS
7745}
7746
7747/* A value representing the special debugging function that outputs
7748 VAX floating-point values of the type represented by TYPE. Assumes
7749 ada_is_vax_floating_type (TYPE). */
d2e4a39e
AS
7750struct value *
7751ada_vax_float_print_function (struct type *type)
7752{
7753 switch (ada_vax_float_type_suffix (type))
7754 {
7755 case 'F':
7756 return get_var_value ("DEBUG_STRING_F", 0);
7757 case 'D':
7758 return get_var_value ("DEBUG_STRING_D", 0);
7759 case 'G':
7760 return get_var_value ("DEBUG_STRING_G", 0);
7761 default:
7762 error ("invalid VAX floating-point type");
7763 }
14f9c5c9 7764}
14f9c5c9 7765\f
d2e4a39e 7766
14f9c5c9
AS
7767 /* Range types */
7768
7769/* Scan STR beginning at position K for a discriminant name, and
7770 return the value of that discriminant field of DVAL in *PX. If
7771 PNEW_K is not null, put the position of the character beyond the
7772 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
7773 not alter *PX and *PNEW_K if unsuccessful. */
7774
7775static int
d2e4a39e
AS
7776scan_discrim_bound (char *, int k, struct value *dval, LONGEST * px,
7777 int *pnew_k)
14f9c5c9
AS
7778{
7779 static char *bound_buffer = NULL;
7780 static size_t bound_buffer_len = 0;
7781 char *bound;
7782 char *pend;
d2e4a39e 7783 struct value *bound_val;
14f9c5c9
AS
7784
7785 if (dval == NULL || str == NULL || str[k] == '\0')
7786 return 0;
7787
d2e4a39e 7788 pend = strstr (str + k, "__");
14f9c5c9
AS
7789 if (pend == NULL)
7790 {
d2e4a39e 7791 bound = str + k;
14f9c5c9
AS
7792 k += strlen (bound);
7793 }
d2e4a39e 7794 else
14f9c5c9 7795 {
d2e4a39e 7796 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
14f9c5c9 7797 bound = bound_buffer;
d2e4a39e
AS
7798 strncpy (bound_buffer, str + k, pend - (str + k));
7799 bound[pend - (str + k)] = '\0';
7800 k = pend - str;
14f9c5c9 7801 }
d2e4a39e
AS
7802
7803 bound_val = ada_search_struct_field (bound, dval, 0, VALUE_TYPE (dval));
14f9c5c9
AS
7804 if (bound_val == NULL)
7805 return 0;
7806
7807 *px = value_as_long (bound_val);
7808 if (pnew_k != NULL)
7809 *pnew_k = k;
7810 return 1;
7811}
7812
7813/* Value of variable named NAME in the current environment. If
7814 no such variable found, then if ERR_MSG is null, returns 0, and
7815 otherwise causes an error with message ERR_MSG. */
d2e4a39e
AS
7816static struct value *
7817get_var_value (char *name, char *err_msg)
14f9c5c9 7818{
d2e4a39e
AS
7819 struct symbol **syms;
7820 struct block **blocks;
14f9c5c9
AS
7821 int nsyms;
7822
d2e4a39e
AS
7823 nsyms =
7824 ada_lookup_symbol_list (name, get_selected_block (NULL), VAR_NAMESPACE,
7825 &syms, &blocks);
14f9c5c9
AS
7826
7827 if (nsyms != 1)
7828 {
7829 if (err_msg == NULL)
7830 return 0;
7831 else
7832 error ("%s", err_msg);
7833 }
7834
7835 return value_of_variable (syms[0], blocks[0]);
7836}
d2e4a39e 7837
14f9c5c9
AS
7838/* Value of integer variable named NAME in the current environment. If
7839 no such variable found, then if ERR_MSG is null, returns 0, and sets
7840 *FLAG to 0. If successful, sets *FLAG to 1. */
7841LONGEST
d2e4a39e 7842get_int_var_value (char *name, char *err_msg, int *flag)
14f9c5c9 7843{
d2e4a39e
AS
7844 struct value *var_val = get_var_value (name, err_msg);
7845
14f9c5c9
AS
7846 if (var_val == 0)
7847 {
7848 if (flag != NULL)
7849 *flag = 0;
7850 return 0;
7851 }
7852 else
7853 {
7854 if (flag != NULL)
7855 *flag = 1;
7856 return value_as_long (var_val);
7857 }
7858}
d2e4a39e 7859
14f9c5c9
AS
7860
7861/* Return a range type whose base type is that of the range type named
7862 NAME in the current environment, and whose bounds are calculated
7863 from NAME according to the GNAT range encoding conventions.
7864 Extract discriminant values, if needed, from DVAL. If a new type
7865 must be created, allocate in OBJFILE's space. The bounds
7866 information, in general, is encoded in NAME, the base type given in
7867 the named range type. */
7868
d2e4a39e 7869static struct type *
ebf56fd3 7870to_fixed_range_type (char *name, struct value *dval, struct objfile *objfile)
14f9c5c9
AS
7871{
7872 struct type *raw_type = ada_find_any_type (name);
7873 struct type *base_type;
7874 LONGEST low, high;
d2e4a39e 7875 char *subtype_info;
14f9c5c9
AS
7876
7877 if (raw_type == NULL)
7878 base_type = builtin_type_int;
7879 else if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
7880 base_type = TYPE_TARGET_TYPE (raw_type);
7881 else
7882 base_type = raw_type;
7883
7884 subtype_info = strstr (name, "___XD");
7885 if (subtype_info == NULL)
7886 return raw_type;
7887 else
7888 {
7889 static char *name_buf = NULL;
7890 static size_t name_len = 0;
7891 int prefix_len = subtype_info - name;
7892 LONGEST L, U;
7893 struct type *type;
7894 char *bounds_str;
7895 int n;
7896
7897 GROW_VECT (name_buf, name_len, prefix_len + 5);
7898 strncpy (name_buf, name, prefix_len);
7899 name_buf[prefix_len] = '\0';
7900
7901 subtype_info += 5;
7902 bounds_str = strchr (subtype_info, '_');
7903 n = 1;
7904
d2e4a39e 7905 if (*subtype_info == 'L')
14f9c5c9 7906 {
d2e4a39e
AS
7907 if (!ada_scan_number (bounds_str, n, &L, &n)
7908 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
14f9c5c9
AS
7909 return raw_type;
7910 if (bounds_str[n] == '_')
7911 n += 2;
d2e4a39e 7912 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
14f9c5c9
AS
7913 n += 1;
7914 subtype_info += 1;
7915 }
d2e4a39e 7916 else
14f9c5c9 7917 {
d2e4a39e 7918 strcpy (name_buf + prefix_len, "___L");
14f9c5c9
AS
7919 L = get_int_var_value (name_buf, "Index bound unknown.", NULL);
7920 }
7921
d2e4a39e 7922 if (*subtype_info == 'U')
14f9c5c9 7923 {
d2e4a39e 7924 if (!ada_scan_number (bounds_str, n, &U, &n)
14f9c5c9
AS
7925 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
7926 return raw_type;
7927 }
d2e4a39e 7928 else
14f9c5c9 7929 {
d2e4a39e 7930 strcpy (name_buf + prefix_len, "___U");
14f9c5c9
AS
7931 U = get_int_var_value (name_buf, "Index bound unknown.", NULL);
7932 }
7933
d2e4a39e 7934 if (objfile == NULL)
14f9c5c9
AS
7935 objfile = TYPE_OBJFILE (base_type);
7936 type = create_range_type (alloc_type (objfile), base_type, L, U);
d2e4a39e 7937 TYPE_NAME (type) = name;
14f9c5c9
AS
7938 return type;
7939 }
7940}
7941
7942/* True iff NAME is the name of a range type. */
7943int
d2e4a39e 7944ada_is_range_type_name (const char *name)
14f9c5c9
AS
7945{
7946 return (name != NULL && strstr (name, "___XD"));
d2e4a39e 7947}
14f9c5c9 7948\f
d2e4a39e 7949
14f9c5c9
AS
7950 /* Modular types */
7951
7952/* True iff TYPE is an Ada modular type. */
7953int
d2e4a39e 7954ada_is_modular_type (struct type *type)
14f9c5c9
AS
7955{
7956 /* FIXME: base_type should be declared in gdbtypes.h, implemented in
d2e4a39e
AS
7957 valarith.c */
7958 struct type *subranged_type; /* = base_type (type); */
14f9c5c9
AS
7959
7960 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
7961 && TYPE_CODE (subranged_type) != TYPE_CODE_ENUM
7962 && TYPE_UNSIGNED (subranged_type));
7963}
7964
7965/* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
7966LONGEST
d2e4a39e 7967ada_modulus (struct type * type)
14f9c5c9 7968{
d2e4a39e 7969 return TYPE_HIGH_BOUND (type) + 1;
14f9c5c9 7970}
d2e4a39e 7971\f
14f9c5c9
AS
7972
7973
14f9c5c9
AS
7974 /* Operators */
7975
7976/* Table mapping opcodes into strings for printing operators
7977 and precedences of the operators. */
7978
d2e4a39e
AS
7979static const struct op_print ada_op_print_tab[] = {
7980 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
7981 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
7982 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
7983 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
7984 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
7985 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
7986 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
7987 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
7988 {"<=", BINOP_LEQ, PREC_ORDER, 0},
7989 {">=", BINOP_GEQ, PREC_ORDER, 0},
7990 {">", BINOP_GTR, PREC_ORDER, 0},
7991 {"<", BINOP_LESS, PREC_ORDER, 0},
7992 {">>", BINOP_RSH, PREC_SHIFT, 0},
7993 {"<<", BINOP_LSH, PREC_SHIFT, 0},
7994 {"+", BINOP_ADD, PREC_ADD, 0},
7995 {"-", BINOP_SUB, PREC_ADD, 0},
7996 {"&", BINOP_CONCAT, PREC_ADD, 0},
7997 {"*", BINOP_MUL, PREC_MUL, 0},
7998 {"/", BINOP_DIV, PREC_MUL, 0},
7999 {"rem", BINOP_REM, PREC_MUL, 0},
8000 {"mod", BINOP_MOD, PREC_MUL, 0},
8001 {"**", BINOP_EXP, PREC_REPEAT, 0},
8002 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
8003 {"-", UNOP_NEG, PREC_PREFIX, 0},
8004 {"+", UNOP_PLUS, PREC_PREFIX, 0},
8005 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
8006 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
8007 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
8008 {".all", UNOP_IND, PREC_SUFFIX, 1}, /* FIXME: postfix .ALL */
8009 {"'access", UNOP_ADDR, PREC_SUFFIX, 1}, /* FIXME: postfix 'ACCESS */
8010 {NULL, 0, 0, 0}
14f9c5c9
AS
8011};
8012\f
8013 /* Assorted Types and Interfaces */
8014
d2e4a39e
AS
8015struct type *builtin_type_ada_int;
8016struct type *builtin_type_ada_short;
8017struct type *builtin_type_ada_long;
8018struct type *builtin_type_ada_long_long;
8019struct type *builtin_type_ada_char;
8020struct type *builtin_type_ada_float;
8021struct type *builtin_type_ada_double;
8022struct type *builtin_type_ada_long_double;
8023struct type *builtin_type_ada_natural;
8024struct type *builtin_type_ada_positive;
8025struct type *builtin_type_ada_system_address;
8026
8027struct type **const (ada_builtin_types[]) =
8028{
8029
14f9c5c9 8030 &builtin_type_ada_int,
d2e4a39e
AS
8031 &builtin_type_ada_long,
8032 &builtin_type_ada_short,
8033 &builtin_type_ada_char,
8034 &builtin_type_ada_float,
8035 &builtin_type_ada_double,
8036 &builtin_type_ada_long_long,
8037 &builtin_type_ada_long_double,
8038 &builtin_type_ada_natural, &builtin_type_ada_positive,
8039 /* The following types are carried over from C for convenience. */
8040&builtin_type_int,
8041 &builtin_type_long,
8042 &builtin_type_short,
8043 &builtin_type_char,
8044 &builtin_type_float,
8045 &builtin_type_double,
8046 &builtin_type_long_long,
8047 &builtin_type_void,
8048 &builtin_type_signed_char,
8049 &builtin_type_unsigned_char,
8050 &builtin_type_unsigned_short,
8051 &builtin_type_unsigned_int,
8052 &builtin_type_unsigned_long,
8053 &builtin_type_unsigned_long_long,
8054 &builtin_type_long_double,
8055 &builtin_type_complex, &builtin_type_double_complex, 0};
14f9c5c9
AS
8056
8057/* Not really used, but needed in the ada_language_defn. */
d2e4a39e
AS
8058static void
8059emit_char (int c, struct ui_file *stream, int quoter)
14f9c5c9
AS
8060{
8061 ada_emit_char (c, stream, quoter, 1);
8062}
8063
8064const struct language_defn ada_language_defn = {
8065 "ada", /* Language name */
8066 /* language_ada, */
8067 language_unknown,
8068 /* FIXME: language_ada should be defined in defs.h */
8069 ada_builtin_types,
8070 range_check_off,
8071 type_check_off,
8072 case_sensitive_on, /* Yes, Ada is case-insensitive, but
8073 * that's not quite what this means. */
8074 ada_parse,
8075 ada_error,
8076 ada_evaluate_subexp,
8077 ada_printchar, /* Print a character constant */
8078 ada_printstr, /* Function to print string constant */
8079 emit_char, /* Function to print single char (not used) */
8080 ada_create_fundamental_type, /* Create fundamental type in this language */
8081 ada_print_type, /* Print a type using appropriate syntax */
8082 ada_val_print, /* Print a value using appropriate syntax */
8083 ada_value_print, /* Print a top-level value */
f636b87d 8084 NULL, /* Language specific skip_trampoline */
9a3d7dfd 8085 NULL, /* Language specific symbol demangler */
d2e4a39e 8086 {"", "", "", ""}, /* Binary format info */
14f9c5c9 8087#if 0
d2e4a39e
AS
8088 {"8#%lo#", "8#", "o", "#"}, /* Octal format info */
8089 {"%ld", "", "d", ""}, /* Decimal format info */
8090 {"16#%lx#", "16#", "x", "#"}, /* Hex format info */
14f9c5c9
AS
8091#else
8092 /* Copied from c-lang.c. */
d2e4a39e
AS
8093 {"0%lo", "0", "o", ""}, /* Octal format info */
8094 {"%ld", "", "d", ""}, /* Decimal format info */
8095 {"0x%lx", "0x", "x", ""}, /* Hex format info */
14f9c5c9
AS
8096#endif
8097 ada_op_print_tab, /* expression operators for printing */
8098 1, /* c-style arrays (FIXME?) */
8099 0, /* String lower bound (FIXME?) */
8100 &builtin_type_ada_char,
8101 LANG_MAGIC
8102};
8103
8104void
4dc81987 8105_initialize_ada_language (void)
14f9c5c9
AS
8106{
8107 builtin_type_ada_int =
8108 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
d2e4a39e 8109 0, "integer", (struct objfile *) NULL);
14f9c5c9
AS
8110 builtin_type_ada_long =
8111 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
d2e4a39e 8112 0, "long_integer", (struct objfile *) NULL);
14f9c5c9
AS
8113 builtin_type_ada_short =
8114 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
d2e4a39e 8115 0, "short_integer", (struct objfile *) NULL);
14f9c5c9
AS
8116 builtin_type_ada_char =
8117 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
d2e4a39e 8118 0, "character", (struct objfile *) NULL);
14f9c5c9
AS
8119 builtin_type_ada_float =
8120 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
d2e4a39e 8121 0, "float", (struct objfile *) NULL);
14f9c5c9
AS
8122 builtin_type_ada_double =
8123 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
d2e4a39e 8124 0, "long_float", (struct objfile *) NULL);
14f9c5c9
AS
8125 builtin_type_ada_long_long =
8126 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
d2e4a39e 8127 0, "long_long_integer", (struct objfile *) NULL);
14f9c5c9
AS
8128 builtin_type_ada_long_double =
8129 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
d2e4a39e 8130 0, "long_long_float", (struct objfile *) NULL);
14f9c5c9
AS
8131 builtin_type_ada_natural =
8132 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
d2e4a39e 8133 0, "natural", (struct objfile *) NULL);
14f9c5c9
AS
8134 builtin_type_ada_positive =
8135 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
d2e4a39e 8136 0, "positive", (struct objfile *) NULL);
14f9c5c9
AS
8137
8138
d2e4a39e
AS
8139 builtin_type_ada_system_address =
8140 lookup_pointer_type (init_type (TYPE_CODE_VOID, 1, 0, "void",
14f9c5c9
AS
8141 (struct objfile *) NULL));
8142 TYPE_NAME (builtin_type_ada_system_address) = "system__address";
8143
8144 add_language (&ada_language_defn);
8145
d2e4a39e 8146 add_show_from_set
14f9c5c9 8147 (add_set_cmd ("varsize-limit", class_support, var_uinteger,
d2e4a39e 8148 (char *) &varsize_limit,
14f9c5c9 8149 "Set maximum bytes in dynamic-sized object.",
d2e4a39e 8150 &setlist), &showlist);
14f9c5c9
AS
8151 varsize_limit = 65536;
8152
8153 add_com ("begin", class_breakpoint, begin_command,
8154 "Start the debugged program, stopping at the beginning of the\n\
8155main program. You may specify command-line arguments to give it, as for\n\
8156the \"run\" command (q.v.).");
8157}
8158
8159
8160/* Create a fundamental Ada type using default reasonable for the current
8161 target machine.
8162
8163 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
8164 define fundamental types such as "int" or "double". Others (stabs or
8165 DWARF version 2, etc) do define fundamental types. For the formats which
8166 don't provide fundamental types, gdb can create such types using this
8167 function.
8168
8169 FIXME: Some compilers distinguish explicitly signed integral types
8170 (signed short, signed int, signed long) from "regular" integral types
8171 (short, int, long) in the debugging information. There is some dis-
8172 agreement as to how useful this feature is. In particular, gcc does
8173 not support this. Also, only some debugging formats allow the
8174 distinction to be passed on to a debugger. For now, we always just
8175 use "short", "int", or "long" as the type name, for both the implicit
8176 and explicitly signed types. This also makes life easier for the
8177 gdb test suite since we don't have to account for the differences
8178 in output depending upon what the compiler and debugging format
8179 support. We will probably have to re-examine the issue when gdb
8180 starts taking it's fundamental type information directly from the
8181 debugging information supplied by the compiler. fnf@cygnus.com */
8182
8183static struct type *
ebf56fd3 8184ada_create_fundamental_type (struct objfile *objfile, int typeid)
14f9c5c9
AS
8185{
8186 struct type *type = NULL;
8187
8188 switch (typeid)
8189 {
d2e4a39e
AS
8190 default:
8191 /* FIXME: For now, if we are asked to produce a type not in this
8192 language, create the equivalent of a C integer type with the
8193 name "<?type?>". When all the dust settles from the type
8194 reconstruction work, this should probably become an error. */
8195 type = init_type (TYPE_CODE_INT,
8196 TARGET_INT_BIT / TARGET_CHAR_BIT,
8197 0, "<?type?>", objfile);
8198 warning ("internal error: no Ada fundamental type %d", typeid);
8199 break;
8200 case FT_VOID:
8201 type = init_type (TYPE_CODE_VOID,
8202 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8203 0, "void", objfile);
8204 break;
8205 case FT_CHAR:
8206 type = init_type (TYPE_CODE_INT,
8207 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8208 0, "character", objfile);
8209 break;
8210 case FT_SIGNED_CHAR:
8211 type = init_type (TYPE_CODE_INT,
8212 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8213 0, "signed char", objfile);
8214 break;
8215 case FT_UNSIGNED_CHAR:
8216 type = init_type (TYPE_CODE_INT,
8217 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
8218 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
8219 break;
8220 case FT_SHORT:
8221 type = init_type (TYPE_CODE_INT,
8222 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8223 0, "short_integer", objfile);
8224 break;
8225 case FT_SIGNED_SHORT:
8226 type = init_type (TYPE_CODE_INT,
8227 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8228 0, "short_integer", objfile);
8229 break;
8230 case FT_UNSIGNED_SHORT:
8231 type = init_type (TYPE_CODE_INT,
8232 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
8233 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
8234 break;
8235 case FT_INTEGER:
8236 type = init_type (TYPE_CODE_INT,
8237 TARGET_INT_BIT / TARGET_CHAR_BIT,
8238 0, "integer", objfile);
8239 break;
8240 case FT_SIGNED_INTEGER:
8241 type = init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 0, "integer", objfile); /* FIXME -fnf */
8242 break;
8243 case FT_UNSIGNED_INTEGER:
8244 type = init_type (TYPE_CODE_INT,
8245 TARGET_INT_BIT / TARGET_CHAR_BIT,
8246 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
8247 break;
8248 case FT_LONG:
8249 type = init_type (TYPE_CODE_INT,
8250 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8251 0, "long_integer", objfile);
8252 break;
8253 case FT_SIGNED_LONG:
8254 type = init_type (TYPE_CODE_INT,
8255 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8256 0, "long_integer", objfile);
8257 break;
8258 case FT_UNSIGNED_LONG:
8259 type = init_type (TYPE_CODE_INT,
8260 TARGET_LONG_BIT / TARGET_CHAR_BIT,
8261 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
8262 break;
8263 case FT_LONG_LONG:
8264 type = init_type (TYPE_CODE_INT,
8265 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8266 0, "long_long_integer", objfile);
8267 break;
8268 case FT_SIGNED_LONG_LONG:
8269 type = init_type (TYPE_CODE_INT,
8270 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8271 0, "long_long_integer", objfile);
8272 break;
8273 case FT_UNSIGNED_LONG_LONG:
8274 type = init_type (TYPE_CODE_INT,
8275 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
8276 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
8277 break;
8278 case FT_FLOAT:
8279 type = init_type (TYPE_CODE_FLT,
8280 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
8281 0, "float", objfile);
8282 break;
8283 case FT_DBL_PREC_FLOAT:
8284 type = init_type (TYPE_CODE_FLT,
8285 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
8286 0, "long_float", objfile);
8287 break;
8288 case FT_EXT_PREC_FLOAT:
8289 type = init_type (TYPE_CODE_FLT,
8290 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
8291 0, "long_long_float", objfile);
8292 break;
8293 }
14f9c5c9
AS
8294 return (type);
8295}
8296
d2e4a39e
AS
8297void
8298ada_dump_symtab (struct symtab *s)
14f9c5c9
AS
8299{
8300 int i;
8301 fprintf (stderr, "New symtab: [\n");
d2e4a39e
AS
8302 fprintf (stderr, " Name: %s/%s;\n",
8303 s->dirname ? s->dirname : "?", s->filename ? s->filename : "?");
14f9c5c9
AS
8304 fprintf (stderr, " Format: %s;\n", s->debugformat);
8305 if (s->linetable != NULL)
8306 {
8307 fprintf (stderr, " Line table (section %d):\n", s->block_line_section);
8308 for (i = 0; i < s->linetable->nitems; i += 1)
8309 {
d2e4a39e 8310 struct linetable_entry *e = s->linetable->item + i;
14f9c5c9
AS
8311 fprintf (stderr, " %4ld: %8lx\n", (long) e->line, (long) e->pc);
8312 }
8313 }
8314 fprintf (stderr, "]\n");
8315}