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