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