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