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