Update copyright dates.
[deliverable/binutils-gdb.git] / gdb / ada-lang.c
1 /* Ada language support routines for GDB, the GNU debugger. Copyright (C)
2
3 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008,
4 2009 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include "defs.h"
23 #include <stdio.h>
24 #include "gdb_string.h"
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include "demangle.h"
28 #include "gdb_regex.h"
29 #include "frame.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "gdbcmd.h"
33 #include "expression.h"
34 #include "parser-defs.h"
35 #include "language.h"
36 #include "c-lang.h"
37 #include "inferior.h"
38 #include "symfile.h"
39 #include "objfiles.h"
40 #include "breakpoint.h"
41 #include "gdbcore.h"
42 #include "hashtab.h"
43 #include "gdb_obstack.h"
44 #include "ada-lang.h"
45 #include "completer.h"
46 #include "gdb_stat.h"
47 #ifdef UI_OUT
48 #include "ui-out.h"
49 #endif
50 #include "block.h"
51 #include "infcall.h"
52 #include "dictionary.h"
53 #include "exceptions.h"
54 #include "annotate.h"
55 #include "valprint.h"
56 #include "source.h"
57 #include "observer.h"
58 #include "vec.h"
59 #include "stack.h"
60
61 #include "psymtab.h"
62 #include "value.h"
63
64 /* Define whether or not the C operator '/' truncates towards zero for
65 differently signed operands (truncation direction is undefined in C).
66 Copied from valarith.c. */
67
68 #ifndef TRUNCATION_TOWARDS_ZERO
69 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
70 #endif
71
72 static struct type *desc_base_type (struct type *);
73
74 static struct type *desc_bounds_type (struct type *);
75
76 static struct value *desc_bounds (struct value *);
77
78 static int fat_pntr_bounds_bitpos (struct type *);
79
80 static int fat_pntr_bounds_bitsize (struct type *);
81
82 static struct type *desc_data_target_type (struct type *);
83
84 static struct value *desc_data (struct value *);
85
86 static int fat_pntr_data_bitpos (struct type *);
87
88 static int fat_pntr_data_bitsize (struct type *);
89
90 static struct value *desc_one_bound (struct value *, int, int);
91
92 static int desc_bound_bitpos (struct type *, int, int);
93
94 static int desc_bound_bitsize (struct type *, int, int);
95
96 static struct type *desc_index_type (struct type *, int);
97
98 static int desc_arity (struct type *);
99
100 static int ada_type_match (struct type *, struct type *, int);
101
102 static int ada_args_match (struct symbol *, struct value **, int);
103
104 static int full_match (const char *, const char *);
105
106 static struct value *make_array_descriptor (struct type *, struct value *);
107
108 static void ada_add_block_symbols (struct obstack *,
109 struct block *, const char *,
110 domain_enum, struct objfile *, int);
111
112 static int is_nonfunction (struct ada_symbol_info *, int);
113
114 static void add_defn_to_vec (struct obstack *, struct symbol *,
115 struct block *);
116
117 static int num_defns_collected (struct obstack *);
118
119 static struct ada_symbol_info *defns_collected (struct obstack *, int);
120
121 static struct value *resolve_subexp (struct expression **, int *, int,
122 struct type *);
123
124 static void replace_operator_with_call (struct expression **, int, int, int,
125 struct symbol *, struct block *);
126
127 static int possible_user_operator_p (enum exp_opcode, struct value **);
128
129 static char *ada_op_name (enum exp_opcode);
130
131 static const char *ada_decoded_op_name (enum exp_opcode);
132
133 static int numeric_type_p (struct type *);
134
135 static int integer_type_p (struct type *);
136
137 static int scalar_type_p (struct type *);
138
139 static int discrete_type_p (struct type *);
140
141 static enum ada_renaming_category parse_old_style_renaming (struct type *,
142 const char **,
143 int *,
144 const char **);
145
146 static struct symbol *find_old_style_renaming_symbol (const char *,
147 struct block *);
148
149 static struct type *ada_lookup_struct_elt_type (struct type *, char *,
150 int, int, int *);
151
152 static struct value *evaluate_subexp_type (struct expression *, int *);
153
154 static struct type *ada_find_parallel_type_with_name (struct type *,
155 const char *);
156
157 static int is_dynamic_field (struct type *, int);
158
159 static struct type *to_fixed_variant_branch_type (struct type *,
160 const gdb_byte *,
161 CORE_ADDR, struct value *);
162
163 static struct type *to_fixed_array_type (struct type *, struct value *, int);
164
165 static struct type *to_fixed_range_type (struct type *, struct value *);
166
167 static struct type *to_static_fixed_type (struct type *);
168 static struct type *static_unwrap_type (struct type *type);
169
170 static struct value *unwrap_value (struct value *);
171
172 static struct type *constrained_packed_array_type (struct type *, long *);
173
174 static struct type *decode_constrained_packed_array_type (struct type *);
175
176 static long decode_packed_array_bitsize (struct type *);
177
178 static struct value *decode_constrained_packed_array (struct value *);
179
180 static int ada_is_packed_array_type (struct type *);
181
182 static int ada_is_unconstrained_packed_array_type (struct type *);
183
184 static struct value *value_subscript_packed (struct value *, int,
185 struct value **);
186
187 static void move_bits (gdb_byte *, int, const gdb_byte *, int, int, int);
188
189 static struct value *coerce_unspec_val_to_type (struct value *,
190 struct type *);
191
192 static struct value *get_var_value (char *, char *);
193
194 static int lesseq_defined_than (struct symbol *, struct symbol *);
195
196 static int equiv_types (struct type *, struct type *);
197
198 static int is_name_suffix (const char *);
199
200 static int advance_wild_match (const char **, const char *, int);
201
202 static int wild_match (const char *, const char *);
203
204 static struct value *ada_coerce_ref (struct value *);
205
206 static LONGEST pos_atr (struct value *);
207
208 static struct value *value_pos_atr (struct type *, struct value *);
209
210 static struct value *value_val_atr (struct type *, struct value *);
211
212 static struct symbol *standard_lookup (const char *, const struct block *,
213 domain_enum);
214
215 static struct value *ada_search_struct_field (char *, struct value *, int,
216 struct type *);
217
218 static struct value *ada_value_primitive_field (struct value *, int, int,
219 struct type *);
220
221 static int find_struct_field (char *, struct type *, int,
222 struct type **, int *, int *, int *, int *);
223
224 static struct value *ada_to_fixed_value_create (struct type *, CORE_ADDR,
225 struct value *);
226
227 static int ada_resolve_function (struct ada_symbol_info *, int,
228 struct value **, int, const char *,
229 struct type *);
230
231 static int ada_is_direct_array_type (struct type *);
232
233 static void ada_language_arch_info (struct gdbarch *,
234 struct language_arch_info *);
235
236 static void check_size (const struct type *);
237
238 static struct value *ada_index_struct_field (int, struct value *, int,
239 struct type *);
240
241 static struct value *assign_aggregate (struct value *, struct value *,
242 struct expression *,
243 int *, enum noside);
244
245 static void aggregate_assign_from_choices (struct value *, struct value *,
246 struct expression *,
247 int *, LONGEST *, int *,
248 int, LONGEST, LONGEST);
249
250 static void aggregate_assign_positional (struct value *, struct value *,
251 struct expression *,
252 int *, LONGEST *, int *, int,
253 LONGEST, LONGEST);
254
255
256 static void aggregate_assign_others (struct value *, struct value *,
257 struct expression *,
258 int *, LONGEST *, int, LONGEST, LONGEST);
259
260
261 static void add_component_interval (LONGEST, LONGEST, LONGEST *, int *, int);
262
263
264 static struct value *ada_evaluate_subexp (struct type *, struct expression *,
265 int *, enum noside);
266
267 static void ada_forward_operator_length (struct expression *, int, int *,
268 int *);
269 \f
270
271
272 /* Maximum-sized dynamic type. */
273 static unsigned int varsize_limit;
274
275 /* FIXME: brobecker/2003-09-17: No longer a const because it is
276 returned by a function that does not return a const char *. */
277 static char *ada_completer_word_break_characters =
278 #ifdef VMS
279 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
280 #else
281 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
282 #endif
283
284 /* The name of the symbol to use to get the name of the main subprogram. */
285 static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
286 = "__gnat_ada_main_program_name";
287
288 /* Limit on the number of warnings to raise per expression evaluation. */
289 static int warning_limit = 2;
290
291 /* Number of warning messages issued; reset to 0 by cleanups after
292 expression evaluation. */
293 static int warnings_issued = 0;
294
295 static const char *known_runtime_file_name_patterns[] = {
296 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
297 };
298
299 static const char *known_auxiliary_function_name_patterns[] = {
300 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
301 };
302
303 /* Space for allocating results of ada_lookup_symbol_list. */
304 static struct obstack symbol_list_obstack;
305
306 /* Inferior-specific data. */
307
308 /* Per-inferior data for this module. */
309
310 struct ada_inferior_data
311 {
312 /* The ada__tags__type_specific_data type, which is used when decoding
313 tagged types. With older versions of GNAT, this type was directly
314 accessible through a component ("tsd") in the object tag. But this
315 is no longer the case, so we cache it for each inferior. */
316 struct type *tsd_type;
317 };
318
319 /* Our key to this module's inferior data. */
320 static const struct inferior_data *ada_inferior_data;
321
322 /* A cleanup routine for our inferior data. */
323 static void
324 ada_inferior_data_cleanup (struct inferior *inf, void *arg)
325 {
326 struct ada_inferior_data *data;
327
328 data = inferior_data (inf, ada_inferior_data);
329 if (data != NULL)
330 xfree (data);
331 }
332
333 /* Return our inferior data for the given inferior (INF).
334
335 This function always returns a valid pointer to an allocated
336 ada_inferior_data structure. If INF's inferior data has not
337 been previously set, this functions creates a new one with all
338 fields set to zero, sets INF's inferior to it, and then returns
339 a pointer to that newly allocated ada_inferior_data. */
340
341 static struct ada_inferior_data *
342 get_ada_inferior_data (struct inferior *inf)
343 {
344 struct ada_inferior_data *data;
345
346 data = inferior_data (inf, ada_inferior_data);
347 if (data == NULL)
348 {
349 data = XZALLOC (struct ada_inferior_data);
350 set_inferior_data (inf, ada_inferior_data, data);
351 }
352
353 return data;
354 }
355
356 /* Perform all necessary cleanups regarding our module's inferior data
357 that is required after the inferior INF just exited. */
358
359 static void
360 ada_inferior_exit (struct inferior *inf)
361 {
362 ada_inferior_data_cleanup (inf, NULL);
363 set_inferior_data (inf, ada_inferior_data, NULL);
364 }
365
366 /* Utilities */
367
368 /* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
369 all typedef layers have been peeled. Otherwise, return TYPE.
370
371 Normally, we really expect a typedef type to only have 1 typedef layer.
372 In other words, we really expect the target type of a typedef type to be
373 a non-typedef type. This is particularly true for Ada units, because
374 the language does not have a typedef vs not-typedef distinction.
375 In that respect, the Ada compiler has been trying to eliminate as many
376 typedef definitions in the debugging information, since they generally
377 do not bring any extra information (we still use typedef under certain
378 circumstances related mostly to the GNAT encoding).
379
380 Unfortunately, we have seen situations where the debugging information
381 generated by the compiler leads to such multiple typedef layers. For
382 instance, consider the following example with stabs:
383
384 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
385 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
386
387 This is an error in the debugging information which causes type
388 pck__float_array___XUP to be defined twice, and the second time,
389 it is defined as a typedef of a typedef.
390
391 This is on the fringe of legality as far as debugging information is
392 concerned, and certainly unexpected. But it is easy to handle these
393 situations correctly, so we can afford to be lenient in this case. */
394
395 static struct type *
396 ada_typedef_target_type (struct type *type)
397 {
398 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
399 type = TYPE_TARGET_TYPE (type);
400 return type;
401 }
402
403 /* Given DECODED_NAME a string holding a symbol name in its
404 decoded form (ie using the Ada dotted notation), returns
405 its unqualified name. */
406
407 static const char *
408 ada_unqualified_name (const char *decoded_name)
409 {
410 const char *result = strrchr (decoded_name, '.');
411
412 if (result != NULL)
413 result++; /* Skip the dot... */
414 else
415 result = decoded_name;
416
417 return result;
418 }
419
420 /* Return a string starting with '<', followed by STR, and '>'.
421 The result is good until the next call. */
422
423 static char *
424 add_angle_brackets (const char *str)
425 {
426 static char *result = NULL;
427
428 xfree (result);
429 result = xstrprintf ("<%s>", str);
430 return result;
431 }
432
433 static char *
434 ada_get_gdb_completer_word_break_characters (void)
435 {
436 return ada_completer_word_break_characters;
437 }
438
439 /* Print an array element index using the Ada syntax. */
440
441 static void
442 ada_print_array_index (struct value *index_value, struct ui_file *stream,
443 const struct value_print_options *options)
444 {
445 LA_VALUE_PRINT (index_value, stream, options);
446 fprintf_filtered (stream, " => ");
447 }
448
449 /* Assuming VECT points to an array of *SIZE objects of size
450 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
451 updating *SIZE as necessary and returning the (new) array. */
452
453 void *
454 grow_vect (void *vect, size_t *size, size_t min_size, int element_size)
455 {
456 if (*size < min_size)
457 {
458 *size *= 2;
459 if (*size < min_size)
460 *size = min_size;
461 vect = xrealloc (vect, *size * element_size);
462 }
463 return vect;
464 }
465
466 /* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
467 suffix of FIELD_NAME beginning "___". */
468
469 static int
470 field_name_match (const char *field_name, const char *target)
471 {
472 int len = strlen (target);
473
474 return
475 (strncmp (field_name, target, len) == 0
476 && (field_name[len] == '\0'
477 || (strncmp (field_name + len, "___", 3) == 0
478 && strcmp (field_name + strlen (field_name) - 6,
479 "___XVN") != 0)));
480 }
481
482
483 /* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
484 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
485 and return its index. This function also handles fields whose name
486 have ___ suffixes because the compiler sometimes alters their name
487 by adding such a suffix to represent fields with certain constraints.
488 If the field could not be found, return a negative number if
489 MAYBE_MISSING is set. Otherwise raise an error. */
490
491 int
492 ada_get_field_index (const struct type *type, const char *field_name,
493 int maybe_missing)
494 {
495 int fieldno;
496 struct type *struct_type = check_typedef ((struct type *) type);
497
498 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); fieldno++)
499 if (field_name_match (TYPE_FIELD_NAME (struct_type, fieldno), field_name))
500 return fieldno;
501
502 if (!maybe_missing)
503 error (_("Unable to find field %s in struct %s. Aborting"),
504 field_name, TYPE_NAME (struct_type));
505
506 return -1;
507 }
508
509 /* The length of the prefix of NAME prior to any "___" suffix. */
510
511 int
512 ada_name_prefix_len (const char *name)
513 {
514 if (name == NULL)
515 return 0;
516 else
517 {
518 const char *p = strstr (name, "___");
519
520 if (p == NULL)
521 return strlen (name);
522 else
523 return p - name;
524 }
525 }
526
527 /* Return non-zero if SUFFIX is a suffix of STR.
528 Return zero if STR is null. */
529
530 static int
531 is_suffix (const char *str, const char *suffix)
532 {
533 int len1, len2;
534
535 if (str == NULL)
536 return 0;
537 len1 = strlen (str);
538 len2 = strlen (suffix);
539 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
540 }
541
542 /* The contents of value VAL, treated as a value of type TYPE. The
543 result is an lval in memory if VAL is. */
544
545 static struct value *
546 coerce_unspec_val_to_type (struct value *val, struct type *type)
547 {
548 type = ada_check_typedef (type);
549 if (value_type (val) == type)
550 return val;
551 else
552 {
553 struct value *result;
554
555 /* Make sure that the object size is not unreasonable before
556 trying to allocate some memory for it. */
557 check_size (type);
558
559 if (value_lazy (val)
560 || TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
561 result = allocate_value_lazy (type);
562 else
563 {
564 result = allocate_value (type);
565 memcpy (value_contents_raw (result), value_contents (val),
566 TYPE_LENGTH (type));
567 }
568 set_value_component_location (result, val);
569 set_value_bitsize (result, value_bitsize (val));
570 set_value_bitpos (result, value_bitpos (val));
571 set_value_address (result, value_address (val));
572 return result;
573 }
574 }
575
576 static const gdb_byte *
577 cond_offset_host (const gdb_byte *valaddr, long offset)
578 {
579 if (valaddr == NULL)
580 return NULL;
581 else
582 return valaddr + offset;
583 }
584
585 static CORE_ADDR
586 cond_offset_target (CORE_ADDR address, long offset)
587 {
588 if (address == 0)
589 return 0;
590 else
591 return address + offset;
592 }
593
594 /* Issue a warning (as for the definition of warning in utils.c, but
595 with exactly one argument rather than ...), unless the limit on the
596 number of warnings has passed during the evaluation of the current
597 expression. */
598
599 /* FIXME: cagney/2004-10-10: This function is mimicking the behavior
600 provided by "complaint". */
601 static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
602
603 static void
604 lim_warning (const char *format, ...)
605 {
606 va_list args;
607
608 va_start (args, format);
609 warnings_issued += 1;
610 if (warnings_issued <= warning_limit)
611 vwarning (format, args);
612
613 va_end (args);
614 }
615
616 /* Issue an error if the size of an object of type T is unreasonable,
617 i.e. if it would be a bad idea to allocate a value of this type in
618 GDB. */
619
620 static void
621 check_size (const struct type *type)
622 {
623 if (TYPE_LENGTH (type) > varsize_limit)
624 error (_("object size is larger than varsize-limit"));
625 }
626
627 /* Maximum value of a SIZE-byte signed integer type. */
628 static LONGEST
629 max_of_size (int size)
630 {
631 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
632
633 return top_bit | (top_bit - 1);
634 }
635
636 /* Minimum value of a SIZE-byte signed integer type. */
637 static LONGEST
638 min_of_size (int size)
639 {
640 return -max_of_size (size) - 1;
641 }
642
643 /* Maximum value of a SIZE-byte unsigned integer type. */
644 static ULONGEST
645 umax_of_size (int size)
646 {
647 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
648
649 return top_bit | (top_bit - 1);
650 }
651
652 /* Maximum value of integral type T, as a signed quantity. */
653 static LONGEST
654 max_of_type (struct type *t)
655 {
656 if (TYPE_UNSIGNED (t))
657 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
658 else
659 return max_of_size (TYPE_LENGTH (t));
660 }
661
662 /* Minimum value of integral type T, as a signed quantity. */
663 static LONGEST
664 min_of_type (struct type *t)
665 {
666 if (TYPE_UNSIGNED (t))
667 return 0;
668 else
669 return min_of_size (TYPE_LENGTH (t));
670 }
671
672 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
673 LONGEST
674 ada_discrete_type_high_bound (struct type *type)
675 {
676 switch (TYPE_CODE (type))
677 {
678 case TYPE_CODE_RANGE:
679 return TYPE_HIGH_BOUND (type);
680 case TYPE_CODE_ENUM:
681 return TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
682 case TYPE_CODE_BOOL:
683 return 1;
684 case TYPE_CODE_CHAR:
685 case TYPE_CODE_INT:
686 return max_of_type (type);
687 default:
688 error (_("Unexpected type in ada_discrete_type_high_bound."));
689 }
690 }
691
692 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
693 LONGEST
694 ada_discrete_type_low_bound (struct type *type)
695 {
696 switch (TYPE_CODE (type))
697 {
698 case TYPE_CODE_RANGE:
699 return TYPE_LOW_BOUND (type);
700 case TYPE_CODE_ENUM:
701 return TYPE_FIELD_BITPOS (type, 0);
702 case TYPE_CODE_BOOL:
703 return 0;
704 case TYPE_CODE_CHAR:
705 case TYPE_CODE_INT:
706 return min_of_type (type);
707 default:
708 error (_("Unexpected type in ada_discrete_type_low_bound."));
709 }
710 }
711
712 /* The identity on non-range types. For range types, the underlying
713 non-range scalar type. */
714
715 static struct type *
716 base_type (struct type *type)
717 {
718 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
719 {
720 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
721 return type;
722 type = TYPE_TARGET_TYPE (type);
723 }
724 return type;
725 }
726 \f
727
728 /* Language Selection */
729
730 /* If the main program is in Ada, return language_ada, otherwise return LANG
731 (the main program is in Ada iif the adainit symbol is found). */
732
733 enum language
734 ada_update_initial_language (enum language lang)
735 {
736 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
737 (struct objfile *) NULL) != NULL)
738 return language_ada;
739
740 return lang;
741 }
742
743 /* If the main procedure is written in Ada, then return its name.
744 The result is good until the next call. Return NULL if the main
745 procedure doesn't appear to be in Ada. */
746
747 char *
748 ada_main_name (void)
749 {
750 struct minimal_symbol *msym;
751 static char *main_program_name = NULL;
752
753 /* For Ada, the name of the main procedure is stored in a specific
754 string constant, generated by the binder. Look for that symbol,
755 extract its address, and then read that string. If we didn't find
756 that string, then most probably the main procedure is not written
757 in Ada. */
758 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
759
760 if (msym != NULL)
761 {
762 CORE_ADDR main_program_name_addr;
763 int err_code;
764
765 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
766 if (main_program_name_addr == 0)
767 error (_("Invalid address for Ada main program name."));
768
769 xfree (main_program_name);
770 target_read_string (main_program_name_addr, &main_program_name,
771 1024, &err_code);
772
773 if (err_code != 0)
774 return NULL;
775 return main_program_name;
776 }
777
778 /* The main procedure doesn't seem to be in Ada. */
779 return NULL;
780 }
781 \f
782 /* Symbols */
783
784 /* Table of Ada operators and their GNAT-encoded names. Last entry is pair
785 of NULLs. */
786
787 const struct ada_opname_map ada_opname_table[] = {
788 {"Oadd", "\"+\"", BINOP_ADD},
789 {"Osubtract", "\"-\"", BINOP_SUB},
790 {"Omultiply", "\"*\"", BINOP_MUL},
791 {"Odivide", "\"/\"", BINOP_DIV},
792 {"Omod", "\"mod\"", BINOP_MOD},
793 {"Orem", "\"rem\"", BINOP_REM},
794 {"Oexpon", "\"**\"", BINOP_EXP},
795 {"Olt", "\"<\"", BINOP_LESS},
796 {"Ole", "\"<=\"", BINOP_LEQ},
797 {"Ogt", "\">\"", BINOP_GTR},
798 {"Oge", "\">=\"", BINOP_GEQ},
799 {"Oeq", "\"=\"", BINOP_EQUAL},
800 {"One", "\"/=\"", BINOP_NOTEQUAL},
801 {"Oand", "\"and\"", BINOP_BITWISE_AND},
802 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
803 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
804 {"Oconcat", "\"&\"", BINOP_CONCAT},
805 {"Oabs", "\"abs\"", UNOP_ABS},
806 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
807 {"Oadd", "\"+\"", UNOP_PLUS},
808 {"Osubtract", "\"-\"", UNOP_NEG},
809 {NULL, NULL}
810 };
811
812 /* The "encoded" form of DECODED, according to GNAT conventions.
813 The result is valid until the next call to ada_encode. */
814
815 char *
816 ada_encode (const char *decoded)
817 {
818 static char *encoding_buffer = NULL;
819 static size_t encoding_buffer_size = 0;
820 const char *p;
821 int k;
822
823 if (decoded == NULL)
824 return NULL;
825
826 GROW_VECT (encoding_buffer, encoding_buffer_size,
827 2 * strlen (decoded) + 10);
828
829 k = 0;
830 for (p = decoded; *p != '\0'; p += 1)
831 {
832 if (*p == '.')
833 {
834 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
835 k += 2;
836 }
837 else if (*p == '"')
838 {
839 const struct ada_opname_map *mapping;
840
841 for (mapping = ada_opname_table;
842 mapping->encoded != NULL
843 && strncmp (mapping->decoded, p,
844 strlen (mapping->decoded)) != 0; mapping += 1)
845 ;
846 if (mapping->encoded == NULL)
847 error (_("invalid Ada operator name: %s"), p);
848 strcpy (encoding_buffer + k, mapping->encoded);
849 k += strlen (mapping->encoded);
850 break;
851 }
852 else
853 {
854 encoding_buffer[k] = *p;
855 k += 1;
856 }
857 }
858
859 encoding_buffer[k] = '\0';
860 return encoding_buffer;
861 }
862
863 /* Return NAME folded to lower case, or, if surrounded by single
864 quotes, unfolded, but with the quotes stripped away. Result good
865 to next call. */
866
867 char *
868 ada_fold_name (const char *name)
869 {
870 static char *fold_buffer = NULL;
871 static size_t fold_buffer_size = 0;
872
873 int len = strlen (name);
874 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
875
876 if (name[0] == '\'')
877 {
878 strncpy (fold_buffer, name + 1, len - 2);
879 fold_buffer[len - 2] = '\000';
880 }
881 else
882 {
883 int i;
884
885 for (i = 0; i <= len; i += 1)
886 fold_buffer[i] = tolower (name[i]);
887 }
888
889 return fold_buffer;
890 }
891
892 /* Return nonzero if C is either a digit or a lowercase alphabet character. */
893
894 static int
895 is_lower_alphanum (const char c)
896 {
897 return (isdigit (c) || (isalpha (c) && islower (c)));
898 }
899
900 /* Remove either of these suffixes:
901 . .{DIGIT}+
902 . ${DIGIT}+
903 . ___{DIGIT}+
904 . __{DIGIT}+.
905 These are suffixes introduced by the compiler for entities such as
906 nested subprogram for instance, in order to avoid name clashes.
907 They do not serve any purpose for the debugger. */
908
909 static void
910 ada_remove_trailing_digits (const char *encoded, int *len)
911 {
912 if (*len > 1 && isdigit (encoded[*len - 1]))
913 {
914 int i = *len - 2;
915
916 while (i > 0 && isdigit (encoded[i]))
917 i--;
918 if (i >= 0 && encoded[i] == '.')
919 *len = i;
920 else if (i >= 0 && encoded[i] == '$')
921 *len = i;
922 else if (i >= 2 && strncmp (encoded + i - 2, "___", 3) == 0)
923 *len = i - 2;
924 else if (i >= 1 && strncmp (encoded + i - 1, "__", 2) == 0)
925 *len = i - 1;
926 }
927 }
928
929 /* Remove the suffix introduced by the compiler for protected object
930 subprograms. */
931
932 static void
933 ada_remove_po_subprogram_suffix (const char *encoded, int *len)
934 {
935 /* Remove trailing N. */
936
937 /* Protected entry subprograms are broken into two
938 separate subprograms: The first one is unprotected, and has
939 a 'N' suffix; the second is the protected version, and has
940 the 'P' suffix. The second calls the first one after handling
941 the protection. Since the P subprograms are internally generated,
942 we leave these names undecoded, giving the user a clue that this
943 entity is internal. */
944
945 if (*len > 1
946 && encoded[*len - 1] == 'N'
947 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
948 *len = *len - 1;
949 }
950
951 /* Remove trailing X[bn]* suffixes (indicating names in package bodies). */
952
953 static void
954 ada_remove_Xbn_suffix (const char *encoded, int *len)
955 {
956 int i = *len - 1;
957
958 while (i > 0 && (encoded[i] == 'b' || encoded[i] == 'n'))
959 i--;
960
961 if (encoded[i] != 'X')
962 return;
963
964 if (i == 0)
965 return;
966
967 if (isalnum (encoded[i-1]))
968 *len = i;
969 }
970
971 /* If ENCODED follows the GNAT entity encoding conventions, then return
972 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
973 replaced by ENCODED.
974
975 The resulting string is valid until the next call of ada_decode.
976 If the string is unchanged by decoding, the original string pointer
977 is returned. */
978
979 const char *
980 ada_decode (const char *encoded)
981 {
982 int i, j;
983 int len0;
984 const char *p;
985 char *decoded;
986 int at_start_name;
987 static char *decoding_buffer = NULL;
988 static size_t decoding_buffer_size = 0;
989
990 /* The name of the Ada main procedure starts with "_ada_".
991 This prefix is not part of the decoded name, so skip this part
992 if we see this prefix. */
993 if (strncmp (encoded, "_ada_", 5) == 0)
994 encoded += 5;
995
996 /* If the name starts with '_', then it is not a properly encoded
997 name, so do not attempt to decode it. Similarly, if the name
998 starts with '<', the name should not be decoded. */
999 if (encoded[0] == '_' || encoded[0] == '<')
1000 goto Suppress;
1001
1002 len0 = strlen (encoded);
1003
1004 ada_remove_trailing_digits (encoded, &len0);
1005 ada_remove_po_subprogram_suffix (encoded, &len0);
1006
1007 /* Remove the ___X.* suffix if present. Do not forget to verify that
1008 the suffix is located before the current "end" of ENCODED. We want
1009 to avoid re-matching parts of ENCODED that have previously been
1010 marked as discarded (by decrementing LEN0). */
1011 p = strstr (encoded, "___");
1012 if (p != NULL && p - encoded < len0 - 3)
1013 {
1014 if (p[3] == 'X')
1015 len0 = p - encoded;
1016 else
1017 goto Suppress;
1018 }
1019
1020 /* Remove any trailing TKB suffix. It tells us that this symbol
1021 is for the body of a task, but that information does not actually
1022 appear in the decoded name. */
1023
1024 if (len0 > 3 && strncmp (encoded + len0 - 3, "TKB", 3) == 0)
1025 len0 -= 3;
1026
1027 /* Remove any trailing TB suffix. The TB suffix is slightly different
1028 from the TKB suffix because it is used for non-anonymous task
1029 bodies. */
1030
1031 if (len0 > 2 && strncmp (encoded + len0 - 2, "TB", 2) == 0)
1032 len0 -= 2;
1033
1034 /* Remove trailing "B" suffixes. */
1035 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1036
1037 if (len0 > 1 && strncmp (encoded + len0 - 1, "B", 1) == 0)
1038 len0 -= 1;
1039
1040 /* Make decoded big enough for possible expansion by operator name. */
1041
1042 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
1043 decoded = decoding_buffer;
1044
1045 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1046
1047 if (len0 > 1 && isdigit (encoded[len0 - 1]))
1048 {
1049 i = len0 - 2;
1050 while ((i >= 0 && isdigit (encoded[i]))
1051 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1052 i -= 1;
1053 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
1054 len0 = i - 1;
1055 else if (encoded[i] == '$')
1056 len0 = i;
1057 }
1058
1059 /* The first few characters that are not alphabetic are not part
1060 of any encoding we use, so we can copy them over verbatim. */
1061
1062 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1063 decoded[j] = encoded[i];
1064
1065 at_start_name = 1;
1066 while (i < len0)
1067 {
1068 /* Is this a symbol function? */
1069 if (at_start_name && encoded[i] == 'O')
1070 {
1071 int k;
1072
1073 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1074 {
1075 int op_len = strlen (ada_opname_table[k].encoded);
1076 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1077 op_len - 1) == 0)
1078 && !isalnum (encoded[i + op_len]))
1079 {
1080 strcpy (decoded + j, ada_opname_table[k].decoded);
1081 at_start_name = 0;
1082 i += op_len;
1083 j += strlen (ada_opname_table[k].decoded);
1084 break;
1085 }
1086 }
1087 if (ada_opname_table[k].encoded != NULL)
1088 continue;
1089 }
1090 at_start_name = 0;
1091
1092 /* Replace "TK__" with "__", which will eventually be translated
1093 into "." (just below). */
1094
1095 if (i < len0 - 4 && strncmp (encoded + i, "TK__", 4) == 0)
1096 i += 2;
1097
1098 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1099 be translated into "." (just below). These are internal names
1100 generated for anonymous blocks inside which our symbol is nested. */
1101
1102 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
1103 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1104 && isdigit (encoded [i+4]))
1105 {
1106 int k = i + 5;
1107
1108 while (k < len0 && isdigit (encoded[k]))
1109 k++; /* Skip any extra digit. */
1110
1111 /* Double-check that the "__B_{DIGITS}+" sequence we found
1112 is indeed followed by "__". */
1113 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1114 i = k;
1115 }
1116
1117 /* Remove _E{DIGITS}+[sb] */
1118
1119 /* Just as for protected object subprograms, there are 2 categories
1120 of subprograms created by the compiler for each entry. The first
1121 one implements the actual entry code, and has a suffix following
1122 the convention above; the second one implements the barrier and
1123 uses the same convention as above, except that the 'E' is replaced
1124 by a 'B'.
1125
1126 Just as above, we do not decode the name of barrier functions
1127 to give the user a clue that the code he is debugging has been
1128 internally generated. */
1129
1130 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
1131 && isdigit (encoded[i+2]))
1132 {
1133 int k = i + 3;
1134
1135 while (k < len0 && isdigit (encoded[k]))
1136 k++;
1137
1138 if (k < len0
1139 && (encoded[k] == 'b' || encoded[k] == 's'))
1140 {
1141 k++;
1142 /* Just as an extra precaution, make sure that if this
1143 suffix is followed by anything else, it is a '_'.
1144 Otherwise, we matched this sequence by accident. */
1145 if (k == len0
1146 || (k < len0 && encoded[k] == '_'))
1147 i = k;
1148 }
1149 }
1150
1151 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1152 the GNAT front-end in protected object subprograms. */
1153
1154 if (i < len0 + 3
1155 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1156 {
1157 /* Backtrack a bit up until we reach either the begining of
1158 the encoded name, or "__". Make sure that we only find
1159 digits or lowercase characters. */
1160 const char *ptr = encoded + i - 1;
1161
1162 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1163 ptr--;
1164 if (ptr < encoded
1165 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1166 i++;
1167 }
1168
1169 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
1170 {
1171 /* This is a X[bn]* sequence not separated from the previous
1172 part of the name with a non-alpha-numeric character (in other
1173 words, immediately following an alpha-numeric character), then
1174 verify that it is placed at the end of the encoded name. If
1175 not, then the encoding is not valid and we should abort the
1176 decoding. Otherwise, just skip it, it is used in body-nested
1177 package names. */
1178 do
1179 i += 1;
1180 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1181 if (i < len0)
1182 goto Suppress;
1183 }
1184 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
1185 {
1186 /* Replace '__' by '.'. */
1187 decoded[j] = '.';
1188 at_start_name = 1;
1189 i += 2;
1190 j += 1;
1191 }
1192 else
1193 {
1194 /* It's a character part of the decoded name, so just copy it
1195 over. */
1196 decoded[j] = encoded[i];
1197 i += 1;
1198 j += 1;
1199 }
1200 }
1201 decoded[j] = '\000';
1202
1203 /* Decoded names should never contain any uppercase character.
1204 Double-check this, and abort the decoding if we find one. */
1205
1206 for (i = 0; decoded[i] != '\0'; i += 1)
1207 if (isupper (decoded[i]) || decoded[i] == ' ')
1208 goto Suppress;
1209
1210 if (strcmp (decoded, encoded) == 0)
1211 return encoded;
1212 else
1213 return decoded;
1214
1215 Suppress:
1216 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
1217 decoded = decoding_buffer;
1218 if (encoded[0] == '<')
1219 strcpy (decoded, encoded);
1220 else
1221 xsnprintf (decoded, decoding_buffer_size, "<%s>", encoded);
1222 return decoded;
1223
1224 }
1225
1226 /* Table for keeping permanent unique copies of decoded names. Once
1227 allocated, names in this table are never released. While this is a
1228 storage leak, it should not be significant unless there are massive
1229 changes in the set of decoded names in successive versions of a
1230 symbol table loaded during a single session. */
1231 static struct htab *decoded_names_store;
1232
1233 /* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1234 in the language-specific part of GSYMBOL, if it has not been
1235 previously computed. Tries to save the decoded name in the same
1236 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1237 in any case, the decoded symbol has a lifetime at least that of
1238 GSYMBOL).
1239 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1240 const, but nevertheless modified to a semantically equivalent form
1241 when a decoded name is cached in it. */
1242
1243 char *
1244 ada_decode_symbol (const struct general_symbol_info *gsymbol)
1245 {
1246 char **resultp =
1247 (char **) &gsymbol->language_specific.mangled_lang.demangled_name;
1248
1249 if (*resultp == NULL)
1250 {
1251 const char *decoded = ada_decode (gsymbol->name);
1252
1253 if (gsymbol->obj_section != NULL)
1254 {
1255 struct objfile *objf = gsymbol->obj_section->objfile;
1256
1257 *resultp = obsavestring (decoded, strlen (decoded),
1258 &objf->objfile_obstack);
1259 }
1260 /* Sometimes, we can't find a corresponding objfile, in which
1261 case, we put the result on the heap. Since we only decode
1262 when needed, we hope this usually does not cause a
1263 significant memory leak (FIXME). */
1264 if (*resultp == NULL)
1265 {
1266 char **slot = (char **) htab_find_slot (decoded_names_store,
1267 decoded, INSERT);
1268
1269 if (*slot == NULL)
1270 *slot = xstrdup (decoded);
1271 *resultp = *slot;
1272 }
1273 }
1274
1275 return *resultp;
1276 }
1277
1278 static char *
1279 ada_la_decode (const char *encoded, int options)
1280 {
1281 return xstrdup (ada_decode (encoded));
1282 }
1283
1284 /* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
1285 suffixes that encode debugging information or leading _ada_ on
1286 SYM_NAME (see is_name_suffix commentary for the debugging
1287 information that is ignored). If WILD, then NAME need only match a
1288 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1289 either argument is NULL. */
1290
1291 static int
1292 match_name (const char *sym_name, const char *name, int wild)
1293 {
1294 if (sym_name == NULL || name == NULL)
1295 return 0;
1296 else if (wild)
1297 return wild_match (sym_name, name) == 0;
1298 else
1299 {
1300 int len_name = strlen (name);
1301
1302 return (strncmp (sym_name, name, len_name) == 0
1303 && is_name_suffix (sym_name + len_name))
1304 || (strncmp (sym_name, "_ada_", 5) == 0
1305 && strncmp (sym_name + 5, name, len_name) == 0
1306 && is_name_suffix (sym_name + len_name + 5));
1307 }
1308 }
1309 \f
1310
1311 /* Arrays */
1312
1313 /* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1314 generated by the GNAT compiler to describe the index type used
1315 for each dimension of an array, check whether it follows the latest
1316 known encoding. If not, fix it up to conform to the latest encoding.
1317 Otherwise, do nothing. This function also does nothing if
1318 INDEX_DESC_TYPE is NULL.
1319
1320 The GNAT encoding used to describle the array index type evolved a bit.
1321 Initially, the information would be provided through the name of each
1322 field of the structure type only, while the type of these fields was
1323 described as unspecified and irrelevant. The debugger was then expected
1324 to perform a global type lookup using the name of that field in order
1325 to get access to the full index type description. Because these global
1326 lookups can be very expensive, the encoding was later enhanced to make
1327 the global lookup unnecessary by defining the field type as being
1328 the full index type description.
1329
1330 The purpose of this routine is to allow us to support older versions
1331 of the compiler by detecting the use of the older encoding, and by
1332 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1333 we essentially replace each field's meaningless type by the associated
1334 index subtype). */
1335
1336 void
1337 ada_fixup_array_indexes_type (struct type *index_desc_type)
1338 {
1339 int i;
1340
1341 if (index_desc_type == NULL)
1342 return;
1343 gdb_assert (TYPE_NFIELDS (index_desc_type) > 0);
1344
1345 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1346 to check one field only, no need to check them all). If not, return
1347 now.
1348
1349 If our INDEX_DESC_TYPE was generated using the older encoding,
1350 the field type should be a meaningless integer type whose name
1351 is not equal to the field name. */
1352 if (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)) != NULL
1353 && strcmp (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)),
1354 TYPE_FIELD_NAME (index_desc_type, 0)) == 0)
1355 return;
1356
1357 /* Fixup each field of INDEX_DESC_TYPE. */
1358 for (i = 0; i < TYPE_NFIELDS (index_desc_type); i++)
1359 {
1360 char *name = TYPE_FIELD_NAME (index_desc_type, i);
1361 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1362
1363 if (raw_type)
1364 TYPE_FIELD_TYPE (index_desc_type, i) = raw_type;
1365 }
1366 }
1367
1368 /* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
1369
1370 static char *bound_name[] = {
1371 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
1372 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1373 };
1374
1375 /* Maximum number of array dimensions we are prepared to handle. */
1376
1377 #define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
1378
1379
1380 /* The desc_* routines return primitive portions of array descriptors
1381 (fat pointers). */
1382
1383 /* The descriptor or array type, if any, indicated by TYPE; removes
1384 level of indirection, if needed. */
1385
1386 static struct type *
1387 desc_base_type (struct type *type)
1388 {
1389 if (type == NULL)
1390 return NULL;
1391 type = ada_check_typedef (type);
1392 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1393 type = ada_typedef_target_type (type);
1394
1395 if (type != NULL
1396 && (TYPE_CODE (type) == TYPE_CODE_PTR
1397 || TYPE_CODE (type) == TYPE_CODE_REF))
1398 return ada_check_typedef (TYPE_TARGET_TYPE (type));
1399 else
1400 return type;
1401 }
1402
1403 /* True iff TYPE indicates a "thin" array pointer type. */
1404
1405 static int
1406 is_thin_pntr (struct type *type)
1407 {
1408 return
1409 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1410 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1411 }
1412
1413 /* The descriptor type for thin pointer type TYPE. */
1414
1415 static struct type *
1416 thin_descriptor_type (struct type *type)
1417 {
1418 struct type *base_type = desc_base_type (type);
1419
1420 if (base_type == NULL)
1421 return NULL;
1422 if (is_suffix (ada_type_name (base_type), "___XVE"))
1423 return base_type;
1424 else
1425 {
1426 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
1427
1428 if (alt_type == NULL)
1429 return base_type;
1430 else
1431 return alt_type;
1432 }
1433 }
1434
1435 /* A pointer to the array data for thin-pointer value VAL. */
1436
1437 static struct value *
1438 thin_data_pntr (struct value *val)
1439 {
1440 struct type *type = value_type (val);
1441 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
1442
1443 data_type = lookup_pointer_type (data_type);
1444
1445 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1446 return value_cast (data_type, value_copy (val));
1447 else
1448 return value_from_longest (data_type, value_address (val));
1449 }
1450
1451 /* True iff TYPE indicates a "thick" array pointer type. */
1452
1453 static int
1454 is_thick_pntr (struct type *type)
1455 {
1456 type = desc_base_type (type);
1457 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
1458 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
1459 }
1460
1461 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1462 pointer to one, the type of its bounds data; otherwise, NULL. */
1463
1464 static struct type *
1465 desc_bounds_type (struct type *type)
1466 {
1467 struct type *r;
1468
1469 type = desc_base_type (type);
1470
1471 if (type == NULL)
1472 return NULL;
1473 else if (is_thin_pntr (type))
1474 {
1475 type = thin_descriptor_type (type);
1476 if (type == NULL)
1477 return NULL;
1478 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1479 if (r != NULL)
1480 return ada_check_typedef (r);
1481 }
1482 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1483 {
1484 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1485 if (r != NULL)
1486 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
1487 }
1488 return NULL;
1489 }
1490
1491 /* If ARR is an array descriptor (fat or thin pointer), or pointer to
1492 one, a pointer to its bounds data. Otherwise NULL. */
1493
1494 static struct value *
1495 desc_bounds (struct value *arr)
1496 {
1497 struct type *type = ada_check_typedef (value_type (arr));
1498
1499 if (is_thin_pntr (type))
1500 {
1501 struct type *bounds_type =
1502 desc_bounds_type (thin_descriptor_type (type));
1503 LONGEST addr;
1504
1505 if (bounds_type == NULL)
1506 error (_("Bad GNAT array descriptor"));
1507
1508 /* NOTE: The following calculation is not really kosher, but
1509 since desc_type is an XVE-encoded type (and shouldn't be),
1510 the correct calculation is a real pain. FIXME (and fix GCC). */
1511 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1512 addr = value_as_long (arr);
1513 else
1514 addr = value_address (arr);
1515
1516 return
1517 value_from_longest (lookup_pointer_type (bounds_type),
1518 addr - TYPE_LENGTH (bounds_type));
1519 }
1520
1521 else if (is_thick_pntr (type))
1522 {
1523 struct value *p_bounds = value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
1524 _("Bad GNAT array descriptor"));
1525 struct type *p_bounds_type = value_type (p_bounds);
1526
1527 if (p_bounds_type
1528 && TYPE_CODE (p_bounds_type) == TYPE_CODE_PTR)
1529 {
1530 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1531
1532 if (TYPE_STUB (target_type))
1533 p_bounds = value_cast (lookup_pointer_type
1534 (ada_check_typedef (target_type)),
1535 p_bounds);
1536 }
1537 else
1538 error (_("Bad GNAT array descriptor"));
1539
1540 return p_bounds;
1541 }
1542 else
1543 return NULL;
1544 }
1545
1546 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1547 position of the field containing the address of the bounds data. */
1548
1549 static int
1550 fat_pntr_bounds_bitpos (struct type *type)
1551 {
1552 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1553 }
1554
1555 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1556 size of the field containing the address of the bounds data. */
1557
1558 static int
1559 fat_pntr_bounds_bitsize (struct type *type)
1560 {
1561 type = desc_base_type (type);
1562
1563 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
1564 return TYPE_FIELD_BITSIZE (type, 1);
1565 else
1566 return 8 * TYPE_LENGTH (ada_check_typedef (TYPE_FIELD_TYPE (type, 1)));
1567 }
1568
1569 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1570 pointer to one, the type of its array data (a array-with-no-bounds type);
1571 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1572 data. */
1573
1574 static struct type *
1575 desc_data_target_type (struct type *type)
1576 {
1577 type = desc_base_type (type);
1578
1579 /* NOTE: The following is bogus; see comment in desc_bounds. */
1580 if (is_thin_pntr (type))
1581 return desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1));
1582 else if (is_thick_pntr (type))
1583 {
1584 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1585
1586 if (data_type
1587 && TYPE_CODE (ada_check_typedef (data_type)) == TYPE_CODE_PTR)
1588 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
1589 }
1590
1591 return NULL;
1592 }
1593
1594 /* If ARR is an array descriptor (fat or thin pointer), a pointer to
1595 its array data. */
1596
1597 static struct value *
1598 desc_data (struct value *arr)
1599 {
1600 struct type *type = value_type (arr);
1601
1602 if (is_thin_pntr (type))
1603 return thin_data_pntr (arr);
1604 else if (is_thick_pntr (type))
1605 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
1606 _("Bad GNAT array descriptor"));
1607 else
1608 return NULL;
1609 }
1610
1611
1612 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1613 position of the field containing the address of the data. */
1614
1615 static int
1616 fat_pntr_data_bitpos (struct type *type)
1617 {
1618 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1619 }
1620
1621 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1622 size of the field containing the address of the data. */
1623
1624 static int
1625 fat_pntr_data_bitsize (struct type *type)
1626 {
1627 type = desc_base_type (type);
1628
1629 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1630 return TYPE_FIELD_BITSIZE (type, 0);
1631 else
1632 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1633 }
1634
1635 /* If BOUNDS is an array-bounds structure (or pointer to one), return
1636 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1637 bound, if WHICH is 1. The first bound is I=1. */
1638
1639 static struct value *
1640 desc_one_bound (struct value *bounds, int i, int which)
1641 {
1642 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
1643 _("Bad GNAT array descriptor bounds"));
1644 }
1645
1646 /* If BOUNDS is an array-bounds structure type, return the bit position
1647 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1648 bound, if WHICH is 1. The first bound is I=1. */
1649
1650 static int
1651 desc_bound_bitpos (struct type *type, int i, int which)
1652 {
1653 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
1654 }
1655
1656 /* If BOUNDS is an array-bounds structure type, return the bit field size
1657 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1658 bound, if WHICH is 1. The first bound is I=1. */
1659
1660 static int
1661 desc_bound_bitsize (struct type *type, int i, int which)
1662 {
1663 type = desc_base_type (type);
1664
1665 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1666 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1667 else
1668 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
1669 }
1670
1671 /* If TYPE is the type of an array-bounds structure, the type of its
1672 Ith bound (numbering from 1). Otherwise, NULL. */
1673
1674 static struct type *
1675 desc_index_type (struct type *type, int i)
1676 {
1677 type = desc_base_type (type);
1678
1679 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1680 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1681 else
1682 return NULL;
1683 }
1684
1685 /* The number of index positions in the array-bounds type TYPE.
1686 Return 0 if TYPE is NULL. */
1687
1688 static int
1689 desc_arity (struct type *type)
1690 {
1691 type = desc_base_type (type);
1692
1693 if (type != NULL)
1694 return TYPE_NFIELDS (type) / 2;
1695 return 0;
1696 }
1697
1698 /* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1699 an array descriptor type (representing an unconstrained array
1700 type). */
1701
1702 static int
1703 ada_is_direct_array_type (struct type *type)
1704 {
1705 if (type == NULL)
1706 return 0;
1707 type = ada_check_typedef (type);
1708 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
1709 || ada_is_array_descriptor_type (type));
1710 }
1711
1712 /* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
1713 * to one. */
1714
1715 static int
1716 ada_is_array_type (struct type *type)
1717 {
1718 while (type != NULL
1719 && (TYPE_CODE (type) == TYPE_CODE_PTR
1720 || TYPE_CODE (type) == TYPE_CODE_REF))
1721 type = TYPE_TARGET_TYPE (type);
1722 return ada_is_direct_array_type (type);
1723 }
1724
1725 /* Non-zero iff TYPE is a simple array type or pointer to one. */
1726
1727 int
1728 ada_is_simple_array_type (struct type *type)
1729 {
1730 if (type == NULL)
1731 return 0;
1732 type = ada_check_typedef (type);
1733 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
1734 || (TYPE_CODE (type) == TYPE_CODE_PTR
1735 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
1736 }
1737
1738 /* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1739
1740 int
1741 ada_is_array_descriptor_type (struct type *type)
1742 {
1743 struct type *data_type = desc_data_target_type (type);
1744
1745 if (type == NULL)
1746 return 0;
1747 type = ada_check_typedef (type);
1748 return (data_type != NULL
1749 && TYPE_CODE (data_type) == TYPE_CODE_ARRAY
1750 && desc_arity (desc_bounds_type (type)) > 0);
1751 }
1752
1753 /* Non-zero iff type is a partially mal-formed GNAT array
1754 descriptor. FIXME: This is to compensate for some problems with
1755 debugging output from GNAT. Re-examine periodically to see if it
1756 is still needed. */
1757
1758 int
1759 ada_is_bogus_array_descriptor (struct type *type)
1760 {
1761 return
1762 type != NULL
1763 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1764 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
1765 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1766 && !ada_is_array_descriptor_type (type);
1767 }
1768
1769
1770 /* If ARR has a record type in the form of a standard GNAT array descriptor,
1771 (fat pointer) returns the type of the array data described---specifically,
1772 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
1773 in from the descriptor; otherwise, they are left unspecified. If
1774 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1775 returns NULL. The result is simply the type of ARR if ARR is not
1776 a descriptor. */
1777 struct type *
1778 ada_type_of_array (struct value *arr, int bounds)
1779 {
1780 if (ada_is_constrained_packed_array_type (value_type (arr)))
1781 return decode_constrained_packed_array_type (value_type (arr));
1782
1783 if (!ada_is_array_descriptor_type (value_type (arr)))
1784 return value_type (arr);
1785
1786 if (!bounds)
1787 {
1788 struct type *array_type =
1789 ada_check_typedef (desc_data_target_type (value_type (arr)));
1790
1791 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1792 TYPE_FIELD_BITSIZE (array_type, 0) =
1793 decode_packed_array_bitsize (value_type (arr));
1794
1795 return array_type;
1796 }
1797 else
1798 {
1799 struct type *elt_type;
1800 int arity;
1801 struct value *descriptor;
1802
1803 elt_type = ada_array_element_type (value_type (arr), -1);
1804 arity = ada_array_arity (value_type (arr));
1805
1806 if (elt_type == NULL || arity == 0)
1807 return ada_check_typedef (value_type (arr));
1808
1809 descriptor = desc_bounds (arr);
1810 if (value_as_long (descriptor) == 0)
1811 return NULL;
1812 while (arity > 0)
1813 {
1814 struct type *range_type = alloc_type_copy (value_type (arr));
1815 struct type *array_type = alloc_type_copy (value_type (arr));
1816 struct value *low = desc_one_bound (descriptor, arity, 0);
1817 struct value *high = desc_one_bound (descriptor, arity, 1);
1818
1819 arity -= 1;
1820 create_range_type (range_type, value_type (low),
1821 longest_to_int (value_as_long (low)),
1822 longest_to_int (value_as_long (high)));
1823 elt_type = create_array_type (array_type, elt_type, range_type);
1824
1825 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1826 TYPE_FIELD_BITSIZE (elt_type, 0) =
1827 decode_packed_array_bitsize (value_type (arr));
1828 }
1829
1830 return lookup_pointer_type (elt_type);
1831 }
1832 }
1833
1834 /* If ARR does not represent an array, returns ARR unchanged.
1835 Otherwise, returns either a standard GDB array with bounds set
1836 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1837 GDB array. Returns NULL if ARR is a null fat pointer. */
1838
1839 struct value *
1840 ada_coerce_to_simple_array_ptr (struct value *arr)
1841 {
1842 if (ada_is_array_descriptor_type (value_type (arr)))
1843 {
1844 struct type *arrType = ada_type_of_array (arr, 1);
1845
1846 if (arrType == NULL)
1847 return NULL;
1848 return value_cast (arrType, value_copy (desc_data (arr)));
1849 }
1850 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1851 return decode_constrained_packed_array (arr);
1852 else
1853 return arr;
1854 }
1855
1856 /* If ARR does not represent an array, returns ARR unchanged.
1857 Otherwise, returns a standard GDB array describing ARR (which may
1858 be ARR itself if it already is in the proper form). */
1859
1860 struct value *
1861 ada_coerce_to_simple_array (struct value *arr)
1862 {
1863 if (ada_is_array_descriptor_type (value_type (arr)))
1864 {
1865 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
1866
1867 if (arrVal == NULL)
1868 error (_("Bounds unavailable for null array pointer."));
1869 check_size (TYPE_TARGET_TYPE (value_type (arrVal)));
1870 return value_ind (arrVal);
1871 }
1872 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1873 return decode_constrained_packed_array (arr);
1874 else
1875 return arr;
1876 }
1877
1878 /* If TYPE represents a GNAT array type, return it translated to an
1879 ordinary GDB array type (possibly with BITSIZE fields indicating
1880 packing). For other types, is the identity. */
1881
1882 struct type *
1883 ada_coerce_to_simple_array_type (struct type *type)
1884 {
1885 if (ada_is_constrained_packed_array_type (type))
1886 return decode_constrained_packed_array_type (type);
1887
1888 if (ada_is_array_descriptor_type (type))
1889 return ada_check_typedef (desc_data_target_type (type));
1890
1891 return type;
1892 }
1893
1894 /* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1895
1896 static int
1897 ada_is_packed_array_type (struct type *type)
1898 {
1899 if (type == NULL)
1900 return 0;
1901 type = desc_base_type (type);
1902 type = ada_check_typedef (type);
1903 return
1904 ada_type_name (type) != NULL
1905 && strstr (ada_type_name (type), "___XP") != NULL;
1906 }
1907
1908 /* Non-zero iff TYPE represents a standard GNAT constrained
1909 packed-array type. */
1910
1911 int
1912 ada_is_constrained_packed_array_type (struct type *type)
1913 {
1914 return ada_is_packed_array_type (type)
1915 && !ada_is_array_descriptor_type (type);
1916 }
1917
1918 /* Non-zero iff TYPE represents an array descriptor for a
1919 unconstrained packed-array type. */
1920
1921 static int
1922 ada_is_unconstrained_packed_array_type (struct type *type)
1923 {
1924 return ada_is_packed_array_type (type)
1925 && ada_is_array_descriptor_type (type);
1926 }
1927
1928 /* Given that TYPE encodes a packed array type (constrained or unconstrained),
1929 return the size of its elements in bits. */
1930
1931 static long
1932 decode_packed_array_bitsize (struct type *type)
1933 {
1934 char *raw_name;
1935 char *tail;
1936 long bits;
1937
1938 /* Access to arrays implemented as fat pointers are encoded as a typedef
1939 of the fat pointer type. We need the name of the fat pointer type
1940 to do the decoding, so strip the typedef layer. */
1941 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1942 type = ada_typedef_target_type (type);
1943
1944 raw_name = ada_type_name (ada_check_typedef (type));
1945 if (!raw_name)
1946 raw_name = ada_type_name (desc_base_type (type));
1947
1948 if (!raw_name)
1949 return 0;
1950
1951 tail = strstr (raw_name, "___XP");
1952 gdb_assert (tail != NULL);
1953
1954 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1955 {
1956 lim_warning
1957 (_("could not understand bit size information on packed array"));
1958 return 0;
1959 }
1960
1961 return bits;
1962 }
1963
1964 /* Given that TYPE is a standard GDB array type with all bounds filled
1965 in, and that the element size of its ultimate scalar constituents
1966 (that is, either its elements, or, if it is an array of arrays, its
1967 elements' elements, etc.) is *ELT_BITS, return an identical type,
1968 but with the bit sizes of its elements (and those of any
1969 constituent arrays) recorded in the BITSIZE components of its
1970 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1971 in bits. */
1972
1973 static struct type *
1974 constrained_packed_array_type (struct type *type, long *elt_bits)
1975 {
1976 struct type *new_elt_type;
1977 struct type *new_type;
1978 LONGEST low_bound, high_bound;
1979
1980 type = ada_check_typedef (type);
1981 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1982 return type;
1983
1984 new_type = alloc_type_copy (type);
1985 new_elt_type =
1986 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
1987 elt_bits);
1988 create_array_type (new_type, new_elt_type, TYPE_INDEX_TYPE (type));
1989 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1990 TYPE_NAME (new_type) = ada_type_name (type);
1991
1992 if (get_discrete_bounds (TYPE_INDEX_TYPE (type),
1993 &low_bound, &high_bound) < 0)
1994 low_bound = high_bound = 0;
1995 if (high_bound < low_bound)
1996 *elt_bits = TYPE_LENGTH (new_type) = 0;
1997 else
1998 {
1999 *elt_bits *= (high_bound - low_bound + 1);
2000 TYPE_LENGTH (new_type) =
2001 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2002 }
2003
2004 TYPE_FIXED_INSTANCE (new_type) = 1;
2005 return new_type;
2006 }
2007
2008 /* The array type encoded by TYPE, where
2009 ada_is_constrained_packed_array_type (TYPE). */
2010
2011 static struct type *
2012 decode_constrained_packed_array_type (struct type *type)
2013 {
2014 char *raw_name = ada_type_name (ada_check_typedef (type));
2015 char *name;
2016 char *tail;
2017 struct type *shadow_type;
2018 long bits;
2019
2020 if (!raw_name)
2021 raw_name = ada_type_name (desc_base_type (type));
2022
2023 if (!raw_name)
2024 return NULL;
2025
2026 name = (char *) alloca (strlen (raw_name) + 1);
2027 tail = strstr (raw_name, "___XP");
2028 type = desc_base_type (type);
2029
2030 memcpy (name, raw_name, tail - raw_name);
2031 name[tail - raw_name] = '\000';
2032
2033 shadow_type = ada_find_parallel_type_with_name (type, name);
2034
2035 if (shadow_type == NULL)
2036 {
2037 lim_warning (_("could not find bounds information on packed array"));
2038 return NULL;
2039 }
2040 CHECK_TYPEDEF (shadow_type);
2041
2042 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
2043 {
2044 lim_warning (_("could not understand bounds "
2045 "information on packed array"));
2046 return NULL;
2047 }
2048
2049 bits = decode_packed_array_bitsize (type);
2050 return constrained_packed_array_type (shadow_type, &bits);
2051 }
2052
2053 /* Given that ARR is a struct value *indicating a GNAT constrained packed
2054 array, returns a simple array that denotes that array. Its type is a
2055 standard GDB array type except that the BITSIZEs of the array
2056 target types are set to the number of bits in each element, and the
2057 type length is set appropriately. */
2058
2059 static struct value *
2060 decode_constrained_packed_array (struct value *arr)
2061 {
2062 struct type *type;
2063
2064 arr = ada_coerce_ref (arr);
2065
2066 /* If our value is a pointer, then dererence it. Make sure that
2067 this operation does not cause the target type to be fixed, as
2068 this would indirectly cause this array to be decoded. The rest
2069 of the routine assumes that the array hasn't been decoded yet,
2070 so we use the basic "value_ind" routine to perform the dereferencing,
2071 as opposed to using "ada_value_ind". */
2072 if (TYPE_CODE (value_type (arr)) == TYPE_CODE_PTR)
2073 arr = value_ind (arr);
2074
2075 type = decode_constrained_packed_array_type (value_type (arr));
2076 if (type == NULL)
2077 {
2078 error (_("can't unpack array"));
2079 return NULL;
2080 }
2081
2082 if (gdbarch_bits_big_endian (get_type_arch (value_type (arr)))
2083 && ada_is_modular_type (value_type (arr)))
2084 {
2085 /* This is a (right-justified) modular type representing a packed
2086 array with no wrapper. In order to interpret the value through
2087 the (left-justified) packed array type we just built, we must
2088 first left-justify it. */
2089 int bit_size, bit_pos;
2090 ULONGEST mod;
2091
2092 mod = ada_modulus (value_type (arr)) - 1;
2093 bit_size = 0;
2094 while (mod > 0)
2095 {
2096 bit_size += 1;
2097 mod >>= 1;
2098 }
2099 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
2100 arr = ada_value_primitive_packed_val (arr, NULL,
2101 bit_pos / HOST_CHAR_BIT,
2102 bit_pos % HOST_CHAR_BIT,
2103 bit_size,
2104 type);
2105 }
2106
2107 return coerce_unspec_val_to_type (arr, type);
2108 }
2109
2110
2111 /* The value of the element of packed array ARR at the ARITY indices
2112 given in IND. ARR must be a simple array. */
2113
2114 static struct value *
2115 value_subscript_packed (struct value *arr, int arity, struct value **ind)
2116 {
2117 int i;
2118 int bits, elt_off, bit_off;
2119 long elt_total_bit_offset;
2120 struct type *elt_type;
2121 struct value *v;
2122
2123 bits = 0;
2124 elt_total_bit_offset = 0;
2125 elt_type = ada_check_typedef (value_type (arr));
2126 for (i = 0; i < arity; i += 1)
2127 {
2128 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
2129 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2130 error
2131 (_("attempt to do packed indexing of "
2132 "something other than a packed array"));
2133 else
2134 {
2135 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
2136 LONGEST lowerbound, upperbound;
2137 LONGEST idx;
2138
2139 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2140 {
2141 lim_warning (_("don't know bounds of array"));
2142 lowerbound = upperbound = 0;
2143 }
2144
2145 idx = pos_atr (ind[i]);
2146 if (idx < lowerbound || idx > upperbound)
2147 lim_warning (_("packed array index %ld out of bounds"),
2148 (long) idx);
2149 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2150 elt_total_bit_offset += (idx - lowerbound) * bits;
2151 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2152 }
2153 }
2154 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2155 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
2156
2157 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
2158 bits, elt_type);
2159 return v;
2160 }
2161
2162 /* Non-zero iff TYPE includes negative integer values. */
2163
2164 static int
2165 has_negatives (struct type *type)
2166 {
2167 switch (TYPE_CODE (type))
2168 {
2169 default:
2170 return 0;
2171 case TYPE_CODE_INT:
2172 return !TYPE_UNSIGNED (type);
2173 case TYPE_CODE_RANGE:
2174 return TYPE_LOW_BOUND (type) < 0;
2175 }
2176 }
2177
2178
2179 /* Create a new value of type TYPE from the contents of OBJ starting
2180 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2181 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2182 assigning through the result will set the field fetched from.
2183 VALADDR is ignored unless OBJ is NULL, in which case,
2184 VALADDR+OFFSET must address the start of storage containing the
2185 packed value. The value returned in this case is never an lval.
2186 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2187
2188 struct value *
2189 ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2190 long offset, int bit_offset, int bit_size,
2191 struct type *type)
2192 {
2193 struct value *v;
2194 int src, /* Index into the source area */
2195 targ, /* Index into the target area */
2196 srcBitsLeft, /* Number of source bits left to move */
2197 nsrc, ntarg, /* Number of source and target bytes */
2198 unusedLS, /* Number of bits in next significant
2199 byte of source that are unused */
2200 accumSize; /* Number of meaningful bits in accum */
2201 unsigned char *bytes; /* First byte containing data to unpack */
2202 unsigned char *unpacked;
2203 unsigned long accum; /* Staging area for bits being transferred */
2204 unsigned char sign;
2205 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2206 /* Transmit bytes from least to most significant; delta is the direction
2207 the indices move. */
2208 int delta = gdbarch_bits_big_endian (get_type_arch (type)) ? -1 : 1;
2209
2210 type = ada_check_typedef (type);
2211
2212 if (obj == NULL)
2213 {
2214 v = allocate_value (type);
2215 bytes = (unsigned char *) (valaddr + offset);
2216 }
2217 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2218 {
2219 v = value_at (type,
2220 value_address (obj) + offset);
2221 bytes = (unsigned char *) alloca (len);
2222 read_memory (value_address (v), bytes, len);
2223 }
2224 else
2225 {
2226 v = allocate_value (type);
2227 bytes = (unsigned char *) value_contents (obj) + offset;
2228 }
2229
2230 if (obj != NULL)
2231 {
2232 CORE_ADDR new_addr;
2233
2234 set_value_component_location (v, obj);
2235 new_addr = value_address (obj) + offset;
2236 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2237 set_value_bitsize (v, bit_size);
2238 if (value_bitpos (v) >= HOST_CHAR_BIT)
2239 {
2240 ++new_addr;
2241 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2242 }
2243 set_value_address (v, new_addr);
2244 }
2245 else
2246 set_value_bitsize (v, bit_size);
2247 unpacked = (unsigned char *) value_contents (v);
2248
2249 srcBitsLeft = bit_size;
2250 nsrc = len;
2251 ntarg = TYPE_LENGTH (type);
2252 sign = 0;
2253 if (bit_size == 0)
2254 {
2255 memset (unpacked, 0, TYPE_LENGTH (type));
2256 return v;
2257 }
2258 else if (gdbarch_bits_big_endian (get_type_arch (type)))
2259 {
2260 src = len - 1;
2261 if (has_negatives (type)
2262 && ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
2263 sign = ~0;
2264
2265 unusedLS =
2266 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2267 % HOST_CHAR_BIT;
2268
2269 switch (TYPE_CODE (type))
2270 {
2271 case TYPE_CODE_ARRAY:
2272 case TYPE_CODE_UNION:
2273 case TYPE_CODE_STRUCT:
2274 /* Non-scalar values must be aligned at a byte boundary... */
2275 accumSize =
2276 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2277 /* ... And are placed at the beginning (most-significant) bytes
2278 of the target. */
2279 targ = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2280 ntarg = targ + 1;
2281 break;
2282 default:
2283 accumSize = 0;
2284 targ = TYPE_LENGTH (type) - 1;
2285 break;
2286 }
2287 }
2288 else
2289 {
2290 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2291
2292 src = targ = 0;
2293 unusedLS = bit_offset;
2294 accumSize = 0;
2295
2296 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
2297 sign = ~0;
2298 }
2299
2300 accum = 0;
2301 while (nsrc > 0)
2302 {
2303 /* Mask for removing bits of the next source byte that are not
2304 part of the value. */
2305 unsigned int unusedMSMask =
2306 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2307 1;
2308 /* Sign-extend bits for this byte. */
2309 unsigned int signMask = sign & ~unusedMSMask;
2310
2311 accum |=
2312 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
2313 accumSize += HOST_CHAR_BIT - unusedLS;
2314 if (accumSize >= HOST_CHAR_BIT)
2315 {
2316 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2317 accumSize -= HOST_CHAR_BIT;
2318 accum >>= HOST_CHAR_BIT;
2319 ntarg -= 1;
2320 targ += delta;
2321 }
2322 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2323 unusedLS = 0;
2324 nsrc -= 1;
2325 src += delta;
2326 }
2327 while (ntarg > 0)
2328 {
2329 accum |= sign << accumSize;
2330 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2331 accumSize -= HOST_CHAR_BIT;
2332 accum >>= HOST_CHAR_BIT;
2333 ntarg -= 1;
2334 targ += delta;
2335 }
2336
2337 return v;
2338 }
2339
2340 /* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
2341 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
2342 not overlap. */
2343 static void
2344 move_bits (gdb_byte *target, int targ_offset, const gdb_byte *source,
2345 int src_offset, int n, int bits_big_endian_p)
2346 {
2347 unsigned int accum, mask;
2348 int accum_bits, chunk_size;
2349
2350 target += targ_offset / HOST_CHAR_BIT;
2351 targ_offset %= HOST_CHAR_BIT;
2352 source += src_offset / HOST_CHAR_BIT;
2353 src_offset %= HOST_CHAR_BIT;
2354 if (bits_big_endian_p)
2355 {
2356 accum = (unsigned char) *source;
2357 source += 1;
2358 accum_bits = HOST_CHAR_BIT - src_offset;
2359
2360 while (n > 0)
2361 {
2362 int unused_right;
2363
2364 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
2365 accum_bits += HOST_CHAR_BIT;
2366 source += 1;
2367 chunk_size = HOST_CHAR_BIT - targ_offset;
2368 if (chunk_size > n)
2369 chunk_size = n;
2370 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
2371 mask = ((1 << chunk_size) - 1) << unused_right;
2372 *target =
2373 (*target & ~mask)
2374 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
2375 n -= chunk_size;
2376 accum_bits -= chunk_size;
2377 target += 1;
2378 targ_offset = 0;
2379 }
2380 }
2381 else
2382 {
2383 accum = (unsigned char) *source >> src_offset;
2384 source += 1;
2385 accum_bits = HOST_CHAR_BIT - src_offset;
2386
2387 while (n > 0)
2388 {
2389 accum = accum + ((unsigned char) *source << accum_bits);
2390 accum_bits += HOST_CHAR_BIT;
2391 source += 1;
2392 chunk_size = HOST_CHAR_BIT - targ_offset;
2393 if (chunk_size > n)
2394 chunk_size = n;
2395 mask = ((1 << chunk_size) - 1) << targ_offset;
2396 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2397 n -= chunk_size;
2398 accum_bits -= chunk_size;
2399 accum >>= chunk_size;
2400 target += 1;
2401 targ_offset = 0;
2402 }
2403 }
2404 }
2405
2406 /* Store the contents of FROMVAL into the location of TOVAL.
2407 Return a new value with the location of TOVAL and contents of
2408 FROMVAL. Handles assignment into packed fields that have
2409 floating-point or non-scalar types. */
2410
2411 static struct value *
2412 ada_value_assign (struct value *toval, struct value *fromval)
2413 {
2414 struct type *type = value_type (toval);
2415 int bits = value_bitsize (toval);
2416
2417 toval = ada_coerce_ref (toval);
2418 fromval = ada_coerce_ref (fromval);
2419
2420 if (ada_is_direct_array_type (value_type (toval)))
2421 toval = ada_coerce_to_simple_array (toval);
2422 if (ada_is_direct_array_type (value_type (fromval)))
2423 fromval = ada_coerce_to_simple_array (fromval);
2424
2425 if (!deprecated_value_modifiable (toval))
2426 error (_("Left operand of assignment is not a modifiable lvalue."));
2427
2428 if (VALUE_LVAL (toval) == lval_memory
2429 && bits > 0
2430 && (TYPE_CODE (type) == TYPE_CODE_FLT
2431 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
2432 {
2433 int len = (value_bitpos (toval)
2434 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2435 int from_size;
2436 char *buffer = (char *) alloca (len);
2437 struct value *val;
2438 CORE_ADDR to_addr = value_address (toval);
2439
2440 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2441 fromval = value_cast (type, fromval);
2442
2443 read_memory (to_addr, buffer, len);
2444 from_size = value_bitsize (fromval);
2445 if (from_size == 0)
2446 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
2447 if (gdbarch_bits_big_endian (get_type_arch (type)))
2448 move_bits (buffer, value_bitpos (toval),
2449 value_contents (fromval), from_size - bits, bits, 1);
2450 else
2451 move_bits (buffer, value_bitpos (toval),
2452 value_contents (fromval), 0, bits, 0);
2453 write_memory (to_addr, buffer, len);
2454 observer_notify_memory_changed (to_addr, len, buffer);
2455
2456 val = value_copy (toval);
2457 memcpy (value_contents_raw (val), value_contents (fromval),
2458 TYPE_LENGTH (type));
2459 deprecated_set_value_type (val, type);
2460
2461 return val;
2462 }
2463
2464 return value_assign (toval, fromval);
2465 }
2466
2467
2468 /* Given that COMPONENT is a memory lvalue that is part of the lvalue
2469 * CONTAINER, assign the contents of VAL to COMPONENTS's place in
2470 * CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2471 * COMPONENT, and not the inferior's memory. The current contents
2472 * of COMPONENT are ignored. */
2473 static void
2474 value_assign_to_component (struct value *container, struct value *component,
2475 struct value *val)
2476 {
2477 LONGEST offset_in_container =
2478 (LONGEST) (value_address (component) - value_address (container));
2479 int bit_offset_in_container =
2480 value_bitpos (component) - value_bitpos (container);
2481 int bits;
2482
2483 val = value_cast (value_type (component), val);
2484
2485 if (value_bitsize (component) == 0)
2486 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2487 else
2488 bits = value_bitsize (component);
2489
2490 if (gdbarch_bits_big_endian (get_type_arch (value_type (container))))
2491 move_bits (value_contents_writeable (container) + offset_in_container,
2492 value_bitpos (container) + bit_offset_in_container,
2493 value_contents (val),
2494 TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits,
2495 bits, 1);
2496 else
2497 move_bits (value_contents_writeable (container) + offset_in_container,
2498 value_bitpos (container) + bit_offset_in_container,
2499 value_contents (val), 0, bits, 0);
2500 }
2501
2502 /* The value of the element of array ARR at the ARITY indices given in IND.
2503 ARR may be either a simple array, GNAT array descriptor, or pointer
2504 thereto. */
2505
2506 struct value *
2507 ada_value_subscript (struct value *arr, int arity, struct value **ind)
2508 {
2509 int k;
2510 struct value *elt;
2511 struct type *elt_type;
2512
2513 elt = ada_coerce_to_simple_array (arr);
2514
2515 elt_type = ada_check_typedef (value_type (elt));
2516 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
2517 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2518 return value_subscript_packed (elt, arity, ind);
2519
2520 for (k = 0; k < arity; k += 1)
2521 {
2522 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
2523 error (_("too many subscripts (%d expected)"), k);
2524 elt = value_subscript (elt, pos_atr (ind[k]));
2525 }
2526 return elt;
2527 }
2528
2529 /* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
2530 value of the element of *ARR at the ARITY indices given in
2531 IND. Does not read the entire array into memory. */
2532
2533 static struct value *
2534 ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
2535 struct value **ind)
2536 {
2537 int k;
2538
2539 for (k = 0; k < arity; k += 1)
2540 {
2541 LONGEST lwb, upb;
2542
2543 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
2544 error (_("too many subscripts (%d expected)"), k);
2545 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2546 value_copy (arr));
2547 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
2548 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
2549 type = TYPE_TARGET_TYPE (type);
2550 }
2551
2552 return value_ind (arr);
2553 }
2554
2555 /* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
2556 actual type of ARRAY_PTR is ignored), returns the Ada slice of HIGH-LOW+1
2557 elements starting at index LOW. The lower bound of this array is LOW, as
2558 per Ada rules. */
2559 static struct value *
2560 ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
2561 int low, int high)
2562 {
2563 CORE_ADDR base = value_as_address (array_ptr)
2564 + ((low - ada_discrete_type_low_bound (TYPE_INDEX_TYPE (type)))
2565 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
2566 struct type *index_type =
2567 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)),
2568 low, high);
2569 struct type *slice_type =
2570 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2571
2572 return value_at_lazy (slice_type, base);
2573 }
2574
2575
2576 static struct value *
2577 ada_value_slice (struct value *array, int low, int high)
2578 {
2579 struct type *type = value_type (array);
2580 struct type *index_type =
2581 create_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
2582 struct type *slice_type =
2583 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2584
2585 return value_cast (slice_type, value_slice (array, low, high - low + 1));
2586 }
2587
2588 /* If type is a record type in the form of a standard GNAT array
2589 descriptor, returns the number of dimensions for type. If arr is a
2590 simple array, returns the number of "array of"s that prefix its
2591 type designation. Otherwise, returns 0. */
2592
2593 int
2594 ada_array_arity (struct type *type)
2595 {
2596 int arity;
2597
2598 if (type == NULL)
2599 return 0;
2600
2601 type = desc_base_type (type);
2602
2603 arity = 0;
2604 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
2605 return desc_arity (desc_bounds_type (type));
2606 else
2607 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2608 {
2609 arity += 1;
2610 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
2611 }
2612
2613 return arity;
2614 }
2615
2616 /* If TYPE is a record type in the form of a standard GNAT array
2617 descriptor or a simple array type, returns the element type for
2618 TYPE after indexing by NINDICES indices, or by all indices if
2619 NINDICES is -1. Otherwise, returns NULL. */
2620
2621 struct type *
2622 ada_array_element_type (struct type *type, int nindices)
2623 {
2624 type = desc_base_type (type);
2625
2626 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
2627 {
2628 int k;
2629 struct type *p_array_type;
2630
2631 p_array_type = desc_data_target_type (type);
2632
2633 k = ada_array_arity (type);
2634 if (k == 0)
2635 return NULL;
2636
2637 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
2638 if (nindices >= 0 && k > nindices)
2639 k = nindices;
2640 while (k > 0 && p_array_type != NULL)
2641 {
2642 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
2643 k -= 1;
2644 }
2645 return p_array_type;
2646 }
2647 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2648 {
2649 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
2650 {
2651 type = TYPE_TARGET_TYPE (type);
2652 nindices -= 1;
2653 }
2654 return type;
2655 }
2656
2657 return NULL;
2658 }
2659
2660 /* The type of nth index in arrays of given type (n numbering from 1).
2661 Does not examine memory. Throws an error if N is invalid or TYPE
2662 is not an array type. NAME is the name of the Ada attribute being
2663 evaluated ('range, 'first, 'last, or 'length); it is used in building
2664 the error message. */
2665
2666 static struct type *
2667 ada_index_type (struct type *type, int n, const char *name)
2668 {
2669 struct type *result_type;
2670
2671 type = desc_base_type (type);
2672
2673 if (n < 0 || n > ada_array_arity (type))
2674 error (_("invalid dimension number to '%s"), name);
2675
2676 if (ada_is_simple_array_type (type))
2677 {
2678 int i;
2679
2680 for (i = 1; i < n; i += 1)
2681 type = TYPE_TARGET_TYPE (type);
2682 result_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type));
2683 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2684 has a target type of TYPE_CODE_UNDEF. We compensate here, but
2685 perhaps stabsread.c would make more sense. */
2686 if (result_type && TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2687 result_type = NULL;
2688 }
2689 else
2690 {
2691 result_type = desc_index_type (desc_bounds_type (type), n);
2692 if (result_type == NULL)
2693 error (_("attempt to take bound of something that is not an array"));
2694 }
2695
2696 return result_type;
2697 }
2698
2699 /* Given that arr is an array type, returns the lower bound of the
2700 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
2701 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
2702 array-descriptor type. It works for other arrays with bounds supplied
2703 by run-time quantities other than discriminants. */
2704
2705 static LONGEST
2706 ada_array_bound_from_type (struct type * arr_type, int n, int which)
2707 {
2708 struct type *type, *elt_type, *index_type_desc, *index_type;
2709 int i;
2710
2711 gdb_assert (which == 0 || which == 1);
2712
2713 if (ada_is_constrained_packed_array_type (arr_type))
2714 arr_type = decode_constrained_packed_array_type (arr_type);
2715
2716 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
2717 return (LONGEST) - which;
2718
2719 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
2720 type = TYPE_TARGET_TYPE (arr_type);
2721 else
2722 type = arr_type;
2723
2724 elt_type = type;
2725 for (i = n; i > 1; i--)
2726 elt_type = TYPE_TARGET_TYPE (type);
2727
2728 index_type_desc = ada_find_parallel_type (type, "___XA");
2729 ada_fixup_array_indexes_type (index_type_desc);
2730 if (index_type_desc != NULL)
2731 index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, n - 1),
2732 NULL);
2733 else
2734 index_type = TYPE_INDEX_TYPE (elt_type);
2735
2736 return
2737 (LONGEST) (which == 0
2738 ? ada_discrete_type_low_bound (index_type)
2739 : ada_discrete_type_high_bound (index_type));
2740 }
2741
2742 /* Given that arr is an array value, returns the lower bound of the
2743 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2744 WHICH is 1. This routine will also work for arrays with bounds
2745 supplied by run-time quantities other than discriminants. */
2746
2747 static LONGEST
2748 ada_array_bound (struct value *arr, int n, int which)
2749 {
2750 struct type *arr_type = value_type (arr);
2751
2752 if (ada_is_constrained_packed_array_type (arr_type))
2753 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
2754 else if (ada_is_simple_array_type (arr_type))
2755 return ada_array_bound_from_type (arr_type, n, which);
2756 else
2757 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
2758 }
2759
2760 /* Given that arr is an array value, returns the length of the
2761 nth index. This routine will also work for arrays with bounds
2762 supplied by run-time quantities other than discriminants.
2763 Does not work for arrays indexed by enumeration types with representation
2764 clauses at the moment. */
2765
2766 static LONGEST
2767 ada_array_length (struct value *arr, int n)
2768 {
2769 struct type *arr_type = ada_check_typedef (value_type (arr));
2770
2771 if (ada_is_constrained_packed_array_type (arr_type))
2772 return ada_array_length (decode_constrained_packed_array (arr), n);
2773
2774 if (ada_is_simple_array_type (arr_type))
2775 return (ada_array_bound_from_type (arr_type, n, 1)
2776 - ada_array_bound_from_type (arr_type, n, 0) + 1);
2777 else
2778 return (value_as_long (desc_one_bound (desc_bounds (arr), n, 1))
2779 - value_as_long (desc_one_bound (desc_bounds (arr), n, 0)) + 1);
2780 }
2781
2782 /* An empty array whose type is that of ARR_TYPE (an array type),
2783 with bounds LOW to LOW-1. */
2784
2785 static struct value *
2786 empty_array (struct type *arr_type, int low)
2787 {
2788 struct type *index_type =
2789 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type)),
2790 low, low - 1);
2791 struct type *elt_type = ada_array_element_type (arr_type, 1);
2792
2793 return allocate_value (create_array_type (NULL, elt_type, index_type));
2794 }
2795 \f
2796
2797 /* Name resolution */
2798
2799 /* The "decoded" name for the user-definable Ada operator corresponding
2800 to OP. */
2801
2802 static const char *
2803 ada_decoded_op_name (enum exp_opcode op)
2804 {
2805 int i;
2806
2807 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
2808 {
2809 if (ada_opname_table[i].op == op)
2810 return ada_opname_table[i].decoded;
2811 }
2812 error (_("Could not find operator name for opcode"));
2813 }
2814
2815
2816 /* Same as evaluate_type (*EXP), but resolves ambiguous symbol
2817 references (marked by OP_VAR_VALUE nodes in which the symbol has an
2818 undefined namespace) and converts operators that are
2819 user-defined into appropriate function calls. If CONTEXT_TYPE is
2820 non-null, it provides a preferred result type [at the moment, only
2821 type void has any effect---causing procedures to be preferred over
2822 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
2823 return type is preferred. May change (expand) *EXP. */
2824
2825 static void
2826 resolve (struct expression **expp, int void_context_p)
2827 {
2828 struct type *context_type = NULL;
2829 int pc = 0;
2830
2831 if (void_context_p)
2832 context_type = builtin_type ((*expp)->gdbarch)->builtin_void;
2833
2834 resolve_subexp (expp, &pc, 1, context_type);
2835 }
2836
2837 /* Resolve the operator of the subexpression beginning at
2838 position *POS of *EXPP. "Resolving" consists of replacing
2839 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
2840 with their resolutions, replacing built-in operators with
2841 function calls to user-defined operators, where appropriate, and,
2842 when DEPROCEDURE_P is non-zero, converting function-valued variables
2843 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
2844 are as in ada_resolve, above. */
2845
2846 static struct value *
2847 resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
2848 struct type *context_type)
2849 {
2850 int pc = *pos;
2851 int i;
2852 struct expression *exp; /* Convenience: == *expp. */
2853 enum exp_opcode op = (*expp)->elts[pc].opcode;
2854 struct value **argvec; /* Vector of operand types (alloca'ed). */
2855 int nargs; /* Number of operands. */
2856 int oplen;
2857
2858 argvec = NULL;
2859 nargs = 0;
2860 exp = *expp;
2861
2862 /* Pass one: resolve operands, saving their types and updating *pos,
2863 if needed. */
2864 switch (op)
2865 {
2866 case OP_FUNCALL:
2867 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
2868 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
2869 *pos += 7;
2870 else
2871 {
2872 *pos += 3;
2873 resolve_subexp (expp, pos, 0, NULL);
2874 }
2875 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2876 break;
2877
2878 case UNOP_ADDR:
2879 *pos += 1;
2880 resolve_subexp (expp, pos, 0, NULL);
2881 break;
2882
2883 case UNOP_QUAL:
2884 *pos += 3;
2885 resolve_subexp (expp, pos, 1, check_typedef (exp->elts[pc + 1].type));
2886 break;
2887
2888 case OP_ATR_MODULUS:
2889 case OP_ATR_SIZE:
2890 case OP_ATR_TAG:
2891 case OP_ATR_FIRST:
2892 case OP_ATR_LAST:
2893 case OP_ATR_LENGTH:
2894 case OP_ATR_POS:
2895 case OP_ATR_VAL:
2896 case OP_ATR_MIN:
2897 case OP_ATR_MAX:
2898 case TERNOP_IN_RANGE:
2899 case BINOP_IN_BOUNDS:
2900 case UNOP_IN_RANGE:
2901 case OP_AGGREGATE:
2902 case OP_OTHERS:
2903 case OP_CHOICES:
2904 case OP_POSITIONAL:
2905 case OP_DISCRETE_RANGE:
2906 case OP_NAME:
2907 ada_forward_operator_length (exp, pc, &oplen, &nargs);
2908 *pos += oplen;
2909 break;
2910
2911 case BINOP_ASSIGN:
2912 {
2913 struct value *arg1;
2914
2915 *pos += 1;
2916 arg1 = resolve_subexp (expp, pos, 0, NULL);
2917 if (arg1 == NULL)
2918 resolve_subexp (expp, pos, 1, NULL);
2919 else
2920 resolve_subexp (expp, pos, 1, value_type (arg1));
2921 break;
2922 }
2923
2924 case UNOP_CAST:
2925 *pos += 3;
2926 nargs = 1;
2927 break;
2928
2929 case BINOP_ADD:
2930 case BINOP_SUB:
2931 case BINOP_MUL:
2932 case BINOP_DIV:
2933 case BINOP_REM:
2934 case BINOP_MOD:
2935 case BINOP_EXP:
2936 case BINOP_CONCAT:
2937 case BINOP_LOGICAL_AND:
2938 case BINOP_LOGICAL_OR:
2939 case BINOP_BITWISE_AND:
2940 case BINOP_BITWISE_IOR:
2941 case BINOP_BITWISE_XOR:
2942
2943 case BINOP_EQUAL:
2944 case BINOP_NOTEQUAL:
2945 case BINOP_LESS:
2946 case BINOP_GTR:
2947 case BINOP_LEQ:
2948 case BINOP_GEQ:
2949
2950 case BINOP_REPEAT:
2951 case BINOP_SUBSCRIPT:
2952 case BINOP_COMMA:
2953 *pos += 1;
2954 nargs = 2;
2955 break;
2956
2957 case UNOP_NEG:
2958 case UNOP_PLUS:
2959 case UNOP_LOGICAL_NOT:
2960 case UNOP_ABS:
2961 case UNOP_IND:
2962 *pos += 1;
2963 nargs = 1;
2964 break;
2965
2966 case OP_LONG:
2967 case OP_DOUBLE:
2968 case OP_VAR_VALUE:
2969 *pos += 4;
2970 break;
2971
2972 case OP_TYPE:
2973 case OP_BOOL:
2974 case OP_LAST:
2975 case OP_INTERNALVAR:
2976 *pos += 3;
2977 break;
2978
2979 case UNOP_MEMVAL:
2980 *pos += 3;
2981 nargs = 1;
2982 break;
2983
2984 case OP_REGISTER:
2985 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2986 break;
2987
2988 case STRUCTOP_STRUCT:
2989 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2990 nargs = 1;
2991 break;
2992
2993 case TERNOP_SLICE:
2994 *pos += 1;
2995 nargs = 3;
2996 break;
2997
2998 case OP_STRING:
2999 break;
3000
3001 default:
3002 error (_("Unexpected operator during name resolution"));
3003 }
3004
3005 argvec = (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
3006 for (i = 0; i < nargs; i += 1)
3007 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
3008 argvec[i] = NULL;
3009 exp = *expp;
3010
3011 /* Pass two: perform any resolution on principal operator. */
3012 switch (op)
3013 {
3014 default:
3015 break;
3016
3017 case OP_VAR_VALUE:
3018 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
3019 {
3020 struct ada_symbol_info *candidates;
3021 int n_candidates;
3022
3023 n_candidates =
3024 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3025 (exp->elts[pc + 2].symbol),
3026 exp->elts[pc + 1].block, VAR_DOMAIN,
3027 &candidates);
3028
3029 if (n_candidates > 1)
3030 {
3031 /* Types tend to get re-introduced locally, so if there
3032 are any local symbols that are not types, first filter
3033 out all types. */
3034 int j;
3035 for (j = 0; j < n_candidates; j += 1)
3036 switch (SYMBOL_CLASS (candidates[j].sym))
3037 {
3038 case LOC_REGISTER:
3039 case LOC_ARG:
3040 case LOC_REF_ARG:
3041 case LOC_REGPARM_ADDR:
3042 case LOC_LOCAL:
3043 case LOC_COMPUTED:
3044 goto FoundNonType;
3045 default:
3046 break;
3047 }
3048 FoundNonType:
3049 if (j < n_candidates)
3050 {
3051 j = 0;
3052 while (j < n_candidates)
3053 {
3054 if (SYMBOL_CLASS (candidates[j].sym) == LOC_TYPEDEF)
3055 {
3056 candidates[j] = candidates[n_candidates - 1];
3057 n_candidates -= 1;
3058 }
3059 else
3060 j += 1;
3061 }
3062 }
3063 }
3064
3065 if (n_candidates == 0)
3066 error (_("No definition found for %s"),
3067 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3068 else if (n_candidates == 1)
3069 i = 0;
3070 else if (deprocedure_p
3071 && !is_nonfunction (candidates, n_candidates))
3072 {
3073 i = ada_resolve_function
3074 (candidates, n_candidates, NULL, 0,
3075 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
3076 context_type);
3077 if (i < 0)
3078 error (_("Could not find a match for %s"),
3079 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3080 }
3081 else
3082 {
3083 printf_filtered (_("Multiple matches for %s\n"),
3084 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3085 user_select_syms (candidates, n_candidates, 1);
3086 i = 0;
3087 }
3088
3089 exp->elts[pc + 1].block = candidates[i].block;
3090 exp->elts[pc + 2].symbol = candidates[i].sym;
3091 if (innermost_block == NULL
3092 || contained_in (candidates[i].block, innermost_block))
3093 innermost_block = candidates[i].block;
3094 }
3095
3096 if (deprocedure_p
3097 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
3098 == TYPE_CODE_FUNC))
3099 {
3100 replace_operator_with_call (expp, pc, 0, 0,
3101 exp->elts[pc + 2].symbol,
3102 exp->elts[pc + 1].block);
3103 exp = *expp;
3104 }
3105 break;
3106
3107 case OP_FUNCALL:
3108 {
3109 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
3110 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
3111 {
3112 struct ada_symbol_info *candidates;
3113 int n_candidates;
3114
3115 n_candidates =
3116 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3117 (exp->elts[pc + 5].symbol),
3118 exp->elts[pc + 4].block, VAR_DOMAIN,
3119 &candidates);
3120 if (n_candidates == 1)
3121 i = 0;
3122 else
3123 {
3124 i = ada_resolve_function
3125 (candidates, n_candidates,
3126 argvec, nargs,
3127 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
3128 context_type);
3129 if (i < 0)
3130 error (_("Could not find a match for %s"),
3131 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
3132 }
3133
3134 exp->elts[pc + 4].block = candidates[i].block;
3135 exp->elts[pc + 5].symbol = candidates[i].sym;
3136 if (innermost_block == NULL
3137 || contained_in (candidates[i].block, innermost_block))
3138 innermost_block = candidates[i].block;
3139 }
3140 }
3141 break;
3142 case BINOP_ADD:
3143 case BINOP_SUB:
3144 case BINOP_MUL:
3145 case BINOP_DIV:
3146 case BINOP_REM:
3147 case BINOP_MOD:
3148 case BINOP_CONCAT:
3149 case BINOP_BITWISE_AND:
3150 case BINOP_BITWISE_IOR:
3151 case BINOP_BITWISE_XOR:
3152 case BINOP_EQUAL:
3153 case BINOP_NOTEQUAL:
3154 case BINOP_LESS:
3155 case BINOP_GTR:
3156 case BINOP_LEQ:
3157 case BINOP_GEQ:
3158 case BINOP_EXP:
3159 case UNOP_NEG:
3160 case UNOP_PLUS:
3161 case UNOP_LOGICAL_NOT:
3162 case UNOP_ABS:
3163 if (possible_user_operator_p (op, argvec))
3164 {
3165 struct ada_symbol_info *candidates;
3166 int n_candidates;
3167
3168 n_candidates =
3169 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
3170 (struct block *) NULL, VAR_DOMAIN,
3171 &candidates);
3172 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
3173 ada_decoded_op_name (op), NULL);
3174 if (i < 0)
3175 break;
3176
3177 replace_operator_with_call (expp, pc, nargs, 1,
3178 candidates[i].sym, candidates[i].block);
3179 exp = *expp;
3180 }
3181 break;
3182
3183 case OP_TYPE:
3184 case OP_REGISTER:
3185 return NULL;
3186 }
3187
3188 *pos = pc;
3189 return evaluate_subexp_type (exp, pos);
3190 }
3191
3192 /* Return non-zero if formal type FTYPE matches actual type ATYPE. If
3193 MAY_DEREF is non-zero, the formal may be a pointer and the actual
3194 a non-pointer. */
3195 /* The term "match" here is rather loose. The match is heuristic and
3196 liberal. */
3197
3198 static int
3199 ada_type_match (struct type *ftype, struct type *atype, int may_deref)
3200 {
3201 ftype = ada_check_typedef (ftype);
3202 atype = ada_check_typedef (atype);
3203
3204 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
3205 ftype = TYPE_TARGET_TYPE (ftype);
3206 if (TYPE_CODE (atype) == TYPE_CODE_REF)
3207 atype = TYPE_TARGET_TYPE (atype);
3208
3209 switch (TYPE_CODE (ftype))
3210 {
3211 default:
3212 return TYPE_CODE (ftype) == TYPE_CODE (atype);
3213 case TYPE_CODE_PTR:
3214 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
3215 return ada_type_match (TYPE_TARGET_TYPE (ftype),
3216 TYPE_TARGET_TYPE (atype), 0);
3217 else
3218 return (may_deref
3219 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
3220 case TYPE_CODE_INT:
3221 case TYPE_CODE_ENUM:
3222 case TYPE_CODE_RANGE:
3223 switch (TYPE_CODE (atype))
3224 {
3225 case TYPE_CODE_INT:
3226 case TYPE_CODE_ENUM:
3227 case TYPE_CODE_RANGE:
3228 return 1;
3229 default:
3230 return 0;
3231 }
3232
3233 case TYPE_CODE_ARRAY:
3234 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3235 || ada_is_array_descriptor_type (atype));
3236
3237 case TYPE_CODE_STRUCT:
3238 if (ada_is_array_descriptor_type (ftype))
3239 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3240 || ada_is_array_descriptor_type (atype));
3241 else
3242 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
3243 && !ada_is_array_descriptor_type (atype));
3244
3245 case TYPE_CODE_UNION:
3246 case TYPE_CODE_FLT:
3247 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
3248 }
3249 }
3250
3251 /* Return non-zero if the formals of FUNC "sufficiently match" the
3252 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3253 may also be an enumeral, in which case it is treated as a 0-
3254 argument function. */
3255
3256 static int
3257 ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3258 {
3259 int i;
3260 struct type *func_type = SYMBOL_TYPE (func);
3261
3262 if (SYMBOL_CLASS (func) == LOC_CONST
3263 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
3264 return (n_actuals == 0);
3265 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
3266 return 0;
3267
3268 if (TYPE_NFIELDS (func_type) != n_actuals)
3269 return 0;
3270
3271 for (i = 0; i < n_actuals; i += 1)
3272 {
3273 if (actuals[i] == NULL)
3274 return 0;
3275 else
3276 {
3277 struct type *ftype = ada_check_typedef (TYPE_FIELD_TYPE (func_type,
3278 i));
3279 struct type *atype = ada_check_typedef (value_type (actuals[i]));
3280
3281 if (!ada_type_match (ftype, atype, 1))
3282 return 0;
3283 }
3284 }
3285 return 1;
3286 }
3287
3288 /* False iff function type FUNC_TYPE definitely does not produce a value
3289 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3290 FUNC_TYPE is not a valid function type with a non-null return type
3291 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
3292
3293 static int
3294 return_match (struct type *func_type, struct type *context_type)
3295 {
3296 struct type *return_type;
3297
3298 if (func_type == NULL)
3299 return 1;
3300
3301 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
3302 return_type = base_type (TYPE_TARGET_TYPE (func_type));
3303 else
3304 return_type = base_type (func_type);
3305 if (return_type == NULL)
3306 return 1;
3307
3308 context_type = base_type (context_type);
3309
3310 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
3311 return context_type == NULL || return_type == context_type;
3312 else if (context_type == NULL)
3313 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
3314 else
3315 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
3316 }
3317
3318
3319 /* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
3320 function (if any) that matches the types of the NARGS arguments in
3321 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3322 that returns that type, then eliminate matches that don't. If
3323 CONTEXT_TYPE is void and there is at least one match that does not
3324 return void, eliminate all matches that do.
3325
3326 Asks the user if there is more than one match remaining. Returns -1
3327 if there is no such symbol or none is selected. NAME is used
3328 solely for messages. May re-arrange and modify SYMS in
3329 the process; the index returned is for the modified vector. */
3330
3331 static int
3332 ada_resolve_function (struct ada_symbol_info syms[],
3333 int nsyms, struct value **args, int nargs,
3334 const char *name, struct type *context_type)
3335 {
3336 int fallback;
3337 int k;
3338 int m; /* Number of hits */
3339
3340 m = 0;
3341 /* In the first pass of the loop, we only accept functions matching
3342 context_type. If none are found, we add a second pass of the loop
3343 where every function is accepted. */
3344 for (fallback = 0; m == 0 && fallback < 2; fallback++)
3345 {
3346 for (k = 0; k < nsyms; k += 1)
3347 {
3348 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].sym));
3349
3350 if (ada_args_match (syms[k].sym, args, nargs)
3351 && (fallback || return_match (type, context_type)))
3352 {
3353 syms[m] = syms[k];
3354 m += 1;
3355 }
3356 }
3357 }
3358
3359 if (m == 0)
3360 return -1;
3361 else if (m > 1)
3362 {
3363 printf_filtered (_("Multiple matches for %s\n"), name);
3364 user_select_syms (syms, m, 1);
3365 return 0;
3366 }
3367 return 0;
3368 }
3369
3370 /* Returns true (non-zero) iff decoded name N0 should appear before N1
3371 in a listing of choices during disambiguation (see sort_choices, below).
3372 The idea is that overloadings of a subprogram name from the
3373 same package should sort in their source order. We settle for ordering
3374 such symbols by their trailing number (__N or $N). */
3375
3376 static int
3377 encoded_ordered_before (char *N0, char *N1)
3378 {
3379 if (N1 == NULL)
3380 return 0;
3381 else if (N0 == NULL)
3382 return 1;
3383 else
3384 {
3385 int k0, k1;
3386
3387 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
3388 ;
3389 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
3390 ;
3391 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
3392 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3393 {
3394 int n0, n1;
3395
3396 n0 = k0;
3397 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3398 n0 -= 1;
3399 n1 = k1;
3400 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3401 n1 -= 1;
3402 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3403 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3404 }
3405 return (strcmp (N0, N1) < 0);
3406 }
3407 }
3408
3409 /* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3410 encoded names. */
3411
3412 static void
3413 sort_choices (struct ada_symbol_info syms[], int nsyms)
3414 {
3415 int i;
3416
3417 for (i = 1; i < nsyms; i += 1)
3418 {
3419 struct ada_symbol_info sym = syms[i];
3420 int j;
3421
3422 for (j = i - 1; j >= 0; j -= 1)
3423 {
3424 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].sym),
3425 SYMBOL_LINKAGE_NAME (sym.sym)))
3426 break;
3427 syms[j + 1] = syms[j];
3428 }
3429 syms[j + 1] = sym;
3430 }
3431 }
3432
3433 /* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3434 by asking the user (if necessary), returning the number selected,
3435 and setting the first elements of SYMS items. Error if no symbols
3436 selected. */
3437
3438 /* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3439 to be re-integrated one of these days. */
3440
3441 int
3442 user_select_syms (struct ada_symbol_info *syms, int nsyms, int max_results)
3443 {
3444 int i;
3445 int *chosen = (int *) alloca (sizeof (int) * nsyms);
3446 int n_chosen;
3447 int first_choice = (max_results == 1) ? 1 : 2;
3448 const char *select_mode = multiple_symbols_select_mode ();
3449
3450 if (max_results < 1)
3451 error (_("Request to select 0 symbols!"));
3452 if (nsyms <= 1)
3453 return nsyms;
3454
3455 if (select_mode == multiple_symbols_cancel)
3456 error (_("\
3457 canceled because the command is ambiguous\n\
3458 See set/show multiple-symbol."));
3459
3460 /* If select_mode is "all", then return all possible symbols.
3461 Only do that if more than one symbol can be selected, of course.
3462 Otherwise, display the menu as usual. */
3463 if (select_mode == multiple_symbols_all && max_results > 1)
3464 return nsyms;
3465
3466 printf_unfiltered (_("[0] cancel\n"));
3467 if (max_results > 1)
3468 printf_unfiltered (_("[1] all\n"));
3469
3470 sort_choices (syms, nsyms);
3471
3472 for (i = 0; i < nsyms; i += 1)
3473 {
3474 if (syms[i].sym == NULL)
3475 continue;
3476
3477 if (SYMBOL_CLASS (syms[i].sym) == LOC_BLOCK)
3478 {
3479 struct symtab_and_line sal =
3480 find_function_start_sal (syms[i].sym, 1);
3481
3482 if (sal.symtab == NULL)
3483 printf_unfiltered (_("[%d] %s at <no source file available>:%d\n"),
3484 i + first_choice,
3485 SYMBOL_PRINT_NAME (syms[i].sym),
3486 sal.line);
3487 else
3488 printf_unfiltered (_("[%d] %s at %s:%d\n"), i + first_choice,
3489 SYMBOL_PRINT_NAME (syms[i].sym),
3490 sal.symtab->filename, sal.line);
3491 continue;
3492 }
3493 else
3494 {
3495 int is_enumeral =
3496 (SYMBOL_CLASS (syms[i].sym) == LOC_CONST
3497 && SYMBOL_TYPE (syms[i].sym) != NULL
3498 && TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) == TYPE_CODE_ENUM);
3499 struct symtab *symtab = syms[i].sym->symtab;
3500
3501 if (SYMBOL_LINE (syms[i].sym) != 0 && symtab != NULL)
3502 printf_unfiltered (_("[%d] %s at %s:%d\n"),
3503 i + first_choice,
3504 SYMBOL_PRINT_NAME (syms[i].sym),
3505 symtab->filename, SYMBOL_LINE (syms[i].sym));
3506 else if (is_enumeral
3507 && TYPE_NAME (SYMBOL_TYPE (syms[i].sym)) != NULL)
3508 {
3509 printf_unfiltered (("[%d] "), i + first_choice);
3510 ada_print_type (SYMBOL_TYPE (syms[i].sym), NULL,
3511 gdb_stdout, -1, 0);
3512 printf_unfiltered (_("'(%s) (enumeral)\n"),
3513 SYMBOL_PRINT_NAME (syms[i].sym));
3514 }
3515 else if (symtab != NULL)
3516 printf_unfiltered (is_enumeral
3517 ? _("[%d] %s in %s (enumeral)\n")
3518 : _("[%d] %s at %s:?\n"),
3519 i + first_choice,
3520 SYMBOL_PRINT_NAME (syms[i].sym),
3521 symtab->filename);
3522 else
3523 printf_unfiltered (is_enumeral
3524 ? _("[%d] %s (enumeral)\n")
3525 : _("[%d] %s at ?\n"),
3526 i + first_choice,
3527 SYMBOL_PRINT_NAME (syms[i].sym));
3528 }
3529 }
3530
3531 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
3532 "overload-choice");
3533
3534 for (i = 0; i < n_chosen; i += 1)
3535 syms[i] = syms[chosen[i]];
3536
3537 return n_chosen;
3538 }
3539
3540 /* Read and validate a set of numeric choices from the user in the
3541 range 0 .. N_CHOICES-1. Place the results in increasing
3542 order in CHOICES[0 .. N-1], and return N.
3543
3544 The user types choices as a sequence of numbers on one line
3545 separated by blanks, encoding them as follows:
3546
3547 + A choice of 0 means to cancel the selection, throwing an error.
3548 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3549 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3550
3551 The user is not allowed to choose more than MAX_RESULTS values.
3552
3553 ANNOTATION_SUFFIX, if present, is used to annotate the input
3554 prompts (for use with the -f switch). */
3555
3556 int
3557 get_selections (int *choices, int n_choices, int max_results,
3558 int is_all_choice, char *annotation_suffix)
3559 {
3560 char *args;
3561 char *prompt;
3562 int n_chosen;
3563 int first_choice = is_all_choice ? 2 : 1;
3564
3565 prompt = getenv ("PS2");
3566 if (prompt == NULL)
3567 prompt = "> ";
3568
3569 args = command_line_input (prompt, 0, annotation_suffix);
3570
3571 if (args == NULL)
3572 error_no_arg (_("one or more choice numbers"));
3573
3574 n_chosen = 0;
3575
3576 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3577 order, as given in args. Choices are validated. */
3578 while (1)
3579 {
3580 char *args2;
3581 int choice, j;
3582
3583 while (isspace (*args))
3584 args += 1;
3585 if (*args == '\0' && n_chosen == 0)
3586 error_no_arg (_("one or more choice numbers"));
3587 else if (*args == '\0')
3588 break;
3589
3590 choice = strtol (args, &args2, 10);
3591 if (args == args2 || choice < 0
3592 || choice > n_choices + first_choice - 1)
3593 error (_("Argument must be choice number"));
3594 args = args2;
3595
3596 if (choice == 0)
3597 error (_("cancelled"));
3598
3599 if (choice < first_choice)
3600 {
3601 n_chosen = n_choices;
3602 for (j = 0; j < n_choices; j += 1)
3603 choices[j] = j;
3604 break;
3605 }
3606 choice -= first_choice;
3607
3608 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
3609 {
3610 }
3611
3612 if (j < 0 || choice != choices[j])
3613 {
3614 int k;
3615
3616 for (k = n_chosen - 1; k > j; k -= 1)
3617 choices[k + 1] = choices[k];
3618 choices[j + 1] = choice;
3619 n_chosen += 1;
3620 }
3621 }
3622
3623 if (n_chosen > max_results)
3624 error (_("Select no more than %d of the above"), max_results);
3625
3626 return n_chosen;
3627 }
3628
3629 /* Replace the operator of length OPLEN at position PC in *EXPP with a call
3630 on the function identified by SYM and BLOCK, and taking NARGS
3631 arguments. Update *EXPP as needed to hold more space. */
3632
3633 static void
3634 replace_operator_with_call (struct expression **expp, int pc, int nargs,
3635 int oplen, struct symbol *sym,
3636 struct block *block)
3637 {
3638 /* A new expression, with 6 more elements (3 for funcall, 4 for function
3639 symbol, -oplen for operator being replaced). */
3640 struct expression *newexp = (struct expression *)
3641 xmalloc (sizeof (struct expression)
3642 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
3643 struct expression *exp = *expp;
3644
3645 newexp->nelts = exp->nelts + 7 - oplen;
3646 newexp->language_defn = exp->language_defn;
3647 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
3648 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
3649 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
3650
3651 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
3652 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
3653
3654 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
3655 newexp->elts[pc + 4].block = block;
3656 newexp->elts[pc + 5].symbol = sym;
3657
3658 *expp = newexp;
3659 xfree (exp);
3660 }
3661
3662 /* Type-class predicates */
3663
3664 /* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3665 or FLOAT). */
3666
3667 static int
3668 numeric_type_p (struct type *type)
3669 {
3670 if (type == NULL)
3671 return 0;
3672 else
3673 {
3674 switch (TYPE_CODE (type))
3675 {
3676 case TYPE_CODE_INT:
3677 case TYPE_CODE_FLT:
3678 return 1;
3679 case TYPE_CODE_RANGE:
3680 return (type == TYPE_TARGET_TYPE (type)
3681 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3682 default:
3683 return 0;
3684 }
3685 }
3686 }
3687
3688 /* True iff TYPE is integral (an INT or RANGE of INTs). */
3689
3690 static int
3691 integer_type_p (struct type *type)
3692 {
3693 if (type == NULL)
3694 return 0;
3695 else
3696 {
3697 switch (TYPE_CODE (type))
3698 {
3699 case TYPE_CODE_INT:
3700 return 1;
3701 case TYPE_CODE_RANGE:
3702 return (type == TYPE_TARGET_TYPE (type)
3703 || integer_type_p (TYPE_TARGET_TYPE (type)));
3704 default:
3705 return 0;
3706 }
3707 }
3708 }
3709
3710 /* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
3711
3712 static int
3713 scalar_type_p (struct type *type)
3714 {
3715 if (type == NULL)
3716 return 0;
3717 else
3718 {
3719 switch (TYPE_CODE (type))
3720 {
3721 case TYPE_CODE_INT:
3722 case TYPE_CODE_RANGE:
3723 case TYPE_CODE_ENUM:
3724 case TYPE_CODE_FLT:
3725 return 1;
3726 default:
3727 return 0;
3728 }
3729 }
3730 }
3731
3732 /* True iff TYPE is discrete (INT, RANGE, ENUM). */
3733
3734 static int
3735 discrete_type_p (struct type *type)
3736 {
3737 if (type == NULL)
3738 return 0;
3739 else
3740 {
3741 switch (TYPE_CODE (type))
3742 {
3743 case TYPE_CODE_INT:
3744 case TYPE_CODE_RANGE:
3745 case TYPE_CODE_ENUM:
3746 case TYPE_CODE_BOOL:
3747 return 1;
3748 default:
3749 return 0;
3750 }
3751 }
3752 }
3753
3754 /* Returns non-zero if OP with operands in the vector ARGS could be
3755 a user-defined function. Errs on the side of pre-defined operators
3756 (i.e., result 0). */
3757
3758 static int
3759 possible_user_operator_p (enum exp_opcode op, struct value *args[])
3760 {
3761 struct type *type0 =
3762 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
3763 struct type *type1 =
3764 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
3765
3766 if (type0 == NULL)
3767 return 0;
3768
3769 switch (op)
3770 {
3771 default:
3772 return 0;
3773
3774 case BINOP_ADD:
3775 case BINOP_SUB:
3776 case BINOP_MUL:
3777 case BINOP_DIV:
3778 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
3779
3780 case BINOP_REM:
3781 case BINOP_MOD:
3782 case BINOP_BITWISE_AND:
3783 case BINOP_BITWISE_IOR:
3784 case BINOP_BITWISE_XOR:
3785 return (!(integer_type_p (type0) && integer_type_p (type1)));
3786
3787 case BINOP_EQUAL:
3788 case BINOP_NOTEQUAL:
3789 case BINOP_LESS:
3790 case BINOP_GTR:
3791 case BINOP_LEQ:
3792 case BINOP_GEQ:
3793 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
3794
3795 case BINOP_CONCAT:
3796 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
3797
3798 case BINOP_EXP:
3799 return (!(numeric_type_p (type0) && integer_type_p (type1)));
3800
3801 case UNOP_NEG:
3802 case UNOP_PLUS:
3803 case UNOP_LOGICAL_NOT:
3804 case UNOP_ABS:
3805 return (!numeric_type_p (type0));
3806
3807 }
3808 }
3809 \f
3810 /* Renaming */
3811
3812 /* NOTES:
3813
3814 1. In the following, we assume that a renaming type's name may
3815 have an ___XD suffix. It would be nice if this went away at some
3816 point.
3817 2. We handle both the (old) purely type-based representation of
3818 renamings and the (new) variable-based encoding. At some point,
3819 it is devoutly to be hoped that the former goes away
3820 (FIXME: hilfinger-2007-07-09).
3821 3. Subprogram renamings are not implemented, although the XRS
3822 suffix is recognized (FIXME: hilfinger-2007-07-09). */
3823
3824 /* If SYM encodes a renaming,
3825
3826 <renaming> renames <renamed entity>,
3827
3828 sets *LEN to the length of the renamed entity's name,
3829 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
3830 the string describing the subcomponent selected from the renamed
3831 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
3832 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
3833 are undefined). Otherwise, returns a value indicating the category
3834 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
3835 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
3836 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
3837 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
3838 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
3839 may be NULL, in which case they are not assigned.
3840
3841 [Currently, however, GCC does not generate subprogram renamings.] */
3842
3843 enum ada_renaming_category
3844 ada_parse_renaming (struct symbol *sym,
3845 const char **renamed_entity, int *len,
3846 const char **renaming_expr)
3847 {
3848 enum ada_renaming_category kind;
3849 const char *info;
3850 const char *suffix;
3851
3852 if (sym == NULL)
3853 return ADA_NOT_RENAMING;
3854 switch (SYMBOL_CLASS (sym))
3855 {
3856 default:
3857 return ADA_NOT_RENAMING;
3858 case LOC_TYPEDEF:
3859 return parse_old_style_renaming (SYMBOL_TYPE (sym),
3860 renamed_entity, len, renaming_expr);
3861 case LOC_LOCAL:
3862 case LOC_STATIC:
3863 case LOC_COMPUTED:
3864 case LOC_OPTIMIZED_OUT:
3865 info = strstr (SYMBOL_LINKAGE_NAME (sym), "___XR");
3866 if (info == NULL)
3867 return ADA_NOT_RENAMING;
3868 switch (info[5])
3869 {
3870 case '_':
3871 kind = ADA_OBJECT_RENAMING;
3872 info += 6;
3873 break;
3874 case 'E':
3875 kind = ADA_EXCEPTION_RENAMING;
3876 info += 7;
3877 break;
3878 case 'P':
3879 kind = ADA_PACKAGE_RENAMING;
3880 info += 7;
3881 break;
3882 case 'S':
3883 kind = ADA_SUBPROGRAM_RENAMING;
3884 info += 7;
3885 break;
3886 default:
3887 return ADA_NOT_RENAMING;
3888 }
3889 }
3890
3891 if (renamed_entity != NULL)
3892 *renamed_entity = info;
3893 suffix = strstr (info, "___XE");
3894 if (suffix == NULL || suffix == info)
3895 return ADA_NOT_RENAMING;
3896 if (len != NULL)
3897 *len = strlen (info) - strlen (suffix);
3898 suffix += 5;
3899 if (renaming_expr != NULL)
3900 *renaming_expr = suffix;
3901 return kind;
3902 }
3903
3904 /* Assuming TYPE encodes a renaming according to the old encoding in
3905 exp_dbug.ads, returns details of that renaming in *RENAMED_ENTITY,
3906 *LEN, and *RENAMING_EXPR, as for ada_parse_renaming, above. Returns
3907 ADA_NOT_RENAMING otherwise. */
3908 static enum ada_renaming_category
3909 parse_old_style_renaming (struct type *type,
3910 const char **renamed_entity, int *len,
3911 const char **renaming_expr)
3912 {
3913 enum ada_renaming_category kind;
3914 const char *name;
3915 const char *info;
3916 const char *suffix;
3917
3918 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
3919 || TYPE_NFIELDS (type) != 1)
3920 return ADA_NOT_RENAMING;
3921
3922 name = type_name_no_tag (type);
3923 if (name == NULL)
3924 return ADA_NOT_RENAMING;
3925
3926 name = strstr (name, "___XR");
3927 if (name == NULL)
3928 return ADA_NOT_RENAMING;
3929 switch (name[5])
3930 {
3931 case '\0':
3932 case '_':
3933 kind = ADA_OBJECT_RENAMING;
3934 break;
3935 case 'E':
3936 kind = ADA_EXCEPTION_RENAMING;
3937 break;
3938 case 'P':
3939 kind = ADA_PACKAGE_RENAMING;
3940 break;
3941 case 'S':
3942 kind = ADA_SUBPROGRAM_RENAMING;
3943 break;
3944 default:
3945 return ADA_NOT_RENAMING;
3946 }
3947
3948 info = TYPE_FIELD_NAME (type, 0);
3949 if (info == NULL)
3950 return ADA_NOT_RENAMING;
3951 if (renamed_entity != NULL)
3952 *renamed_entity = info;
3953 suffix = strstr (info, "___XE");
3954 if (renaming_expr != NULL)
3955 *renaming_expr = suffix + 5;
3956 if (suffix == NULL || suffix == info)
3957 return ADA_NOT_RENAMING;
3958 if (len != NULL)
3959 *len = suffix - info;
3960 return kind;
3961 }
3962
3963 \f
3964
3965 /* Evaluation: Function Calls */
3966
3967 /* Return an lvalue containing the value VAL. This is the identity on
3968 lvalues, and otherwise has the side-effect of allocating memory
3969 in the inferior where a copy of the value contents is copied. */
3970
3971 static struct value *
3972 ensure_lval (struct value *val)
3973 {
3974 if (VALUE_LVAL (val) == not_lval
3975 || VALUE_LVAL (val) == lval_internalvar)
3976 {
3977 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
3978 const CORE_ADDR addr =
3979 value_as_long (value_allocate_space_in_inferior (len));
3980
3981 set_value_address (val, addr);
3982 VALUE_LVAL (val) = lval_memory;
3983 write_memory (addr, value_contents (val), len);
3984 }
3985
3986 return val;
3987 }
3988
3989 /* Return the value ACTUAL, converted to be an appropriate value for a
3990 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3991 allocating any necessary descriptors (fat pointers), or copies of
3992 values not residing in memory, updating it as needed. */
3993
3994 struct value *
3995 ada_convert_actual (struct value *actual, struct type *formal_type0)
3996 {
3997 struct type *actual_type = ada_check_typedef (value_type (actual));
3998 struct type *formal_type = ada_check_typedef (formal_type0);
3999 struct type *formal_target =
4000 TYPE_CODE (formal_type) == TYPE_CODE_PTR
4001 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
4002 struct type *actual_target =
4003 TYPE_CODE (actual_type) == TYPE_CODE_PTR
4004 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
4005
4006 if (ada_is_array_descriptor_type (formal_target)
4007 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
4008 return make_array_descriptor (formal_type, actual);
4009 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR
4010 || TYPE_CODE (formal_type) == TYPE_CODE_REF)
4011 {
4012 struct value *result;
4013
4014 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4015 && ada_is_array_descriptor_type (actual_target))
4016 result = desc_data (actual);
4017 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4018 {
4019 if (VALUE_LVAL (actual) != lval_memory)
4020 {
4021 struct value *val;
4022
4023 actual_type = ada_check_typedef (value_type (actual));
4024 val = allocate_value (actual_type);
4025 memcpy ((char *) value_contents_raw (val),
4026 (char *) value_contents (actual),
4027 TYPE_LENGTH (actual_type));
4028 actual = ensure_lval (val);
4029 }
4030 result = value_addr (actual);
4031 }
4032 else
4033 return actual;
4034 return value_cast_pointers (formal_type, result);
4035 }
4036 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
4037 return ada_value_ind (actual);
4038
4039 return actual;
4040 }
4041
4042 /* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4043 type TYPE. This is usually an inefficient no-op except on some targets
4044 (such as AVR) where the representation of a pointer and an address
4045 differs. */
4046
4047 static CORE_ADDR
4048 value_pointer (struct value *value, struct type *type)
4049 {
4050 struct gdbarch *gdbarch = get_type_arch (type);
4051 unsigned len = TYPE_LENGTH (type);
4052 gdb_byte *buf = alloca (len);
4053 CORE_ADDR addr;
4054
4055 addr = value_address (value);
4056 gdbarch_address_to_pointer (gdbarch, type, buf, addr);
4057 addr = extract_unsigned_integer (buf, len, gdbarch_byte_order (gdbarch));
4058 return addr;
4059 }
4060
4061
4062 /* Push a descriptor of type TYPE for array value ARR on the stack at
4063 *SP, updating *SP to reflect the new descriptor. Return either
4064 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4065 to-descriptor type rather than a descriptor type), a struct value *
4066 representing a pointer to this descriptor. */
4067
4068 static struct value *
4069 make_array_descriptor (struct type *type, struct value *arr)
4070 {
4071 struct type *bounds_type = desc_bounds_type (type);
4072 struct type *desc_type = desc_base_type (type);
4073 struct value *descriptor = allocate_value (desc_type);
4074 struct value *bounds = allocate_value (bounds_type);
4075 int i;
4076
4077 for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
4078 i > 0; i -= 1)
4079 {
4080 modify_field (value_type (bounds), value_contents_writeable (bounds),
4081 ada_array_bound (arr, i, 0),
4082 desc_bound_bitpos (bounds_type, i, 0),
4083 desc_bound_bitsize (bounds_type, i, 0));
4084 modify_field (value_type (bounds), value_contents_writeable (bounds),
4085 ada_array_bound (arr, i, 1),
4086 desc_bound_bitpos (bounds_type, i, 1),
4087 desc_bound_bitsize (bounds_type, i, 1));
4088 }
4089
4090 bounds = ensure_lval (bounds);
4091
4092 modify_field (value_type (descriptor),
4093 value_contents_writeable (descriptor),
4094 value_pointer (ensure_lval (arr),
4095 TYPE_FIELD_TYPE (desc_type, 0)),
4096 fat_pntr_data_bitpos (desc_type),
4097 fat_pntr_data_bitsize (desc_type));
4098
4099 modify_field (value_type (descriptor),
4100 value_contents_writeable (descriptor),
4101 value_pointer (bounds,
4102 TYPE_FIELD_TYPE (desc_type, 1)),
4103 fat_pntr_bounds_bitpos (desc_type),
4104 fat_pntr_bounds_bitsize (desc_type));
4105
4106 descriptor = ensure_lval (descriptor);
4107
4108 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4109 return value_addr (descriptor);
4110 else
4111 return descriptor;
4112 }
4113 \f
4114 /* Dummy definitions for an experimental caching module that is not
4115 * used in the public sources. */
4116
4117 static int
4118 lookup_cached_symbol (const char *name, domain_enum namespace,
4119 struct symbol **sym, struct block **block)
4120 {
4121 return 0;
4122 }
4123
4124 static void
4125 cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
4126 struct block *block)
4127 {
4128 }
4129 \f
4130 /* Symbol Lookup */
4131
4132 /* Return the result of a standard (literal, C-like) lookup of NAME in
4133 given DOMAIN, visible from lexical block BLOCK. */
4134
4135 static struct symbol *
4136 standard_lookup (const char *name, const struct block *block,
4137 domain_enum domain)
4138 {
4139 struct symbol *sym;
4140
4141 if (lookup_cached_symbol (name, domain, &sym, NULL))
4142 return sym;
4143 sym = lookup_symbol_in_language (name, block, domain, language_c, 0);
4144 cache_symbol (name, domain, sym, block_found);
4145 return sym;
4146 }
4147
4148
4149 /* Non-zero iff there is at least one non-function/non-enumeral symbol
4150 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
4151 since they contend in overloading in the same way. */
4152 static int
4153 is_nonfunction (struct ada_symbol_info syms[], int n)
4154 {
4155 int i;
4156
4157 for (i = 0; i < n; i += 1)
4158 if (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_FUNC
4159 && (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_ENUM
4160 || SYMBOL_CLASS (syms[i].sym) != LOC_CONST))
4161 return 1;
4162
4163 return 0;
4164 }
4165
4166 /* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4167 struct types. Otherwise, they may not. */
4168
4169 static int
4170 equiv_types (struct type *type0, struct type *type1)
4171 {
4172 if (type0 == type1)
4173 return 1;
4174 if (type0 == NULL || type1 == NULL
4175 || TYPE_CODE (type0) != TYPE_CODE (type1))
4176 return 0;
4177 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
4178 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
4179 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4180 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
4181 return 1;
4182
4183 return 0;
4184 }
4185
4186 /* True iff SYM0 represents the same entity as SYM1, or one that is
4187 no more defined than that of SYM1. */
4188
4189 static int
4190 lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
4191 {
4192 if (sym0 == sym1)
4193 return 1;
4194 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
4195 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4196 return 0;
4197
4198 switch (SYMBOL_CLASS (sym0))
4199 {
4200 case LOC_UNDEF:
4201 return 1;
4202 case LOC_TYPEDEF:
4203 {
4204 struct type *type0 = SYMBOL_TYPE (sym0);
4205 struct type *type1 = SYMBOL_TYPE (sym1);
4206 char *name0 = SYMBOL_LINKAGE_NAME (sym0);
4207 char *name1 = SYMBOL_LINKAGE_NAME (sym1);
4208 int len0 = strlen (name0);
4209
4210 return
4211 TYPE_CODE (type0) == TYPE_CODE (type1)
4212 && (equiv_types (type0, type1)
4213 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4214 && strncmp (name1 + len0, "___XV", 5) == 0));
4215 }
4216 case LOC_CONST:
4217 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4218 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
4219 default:
4220 return 0;
4221 }
4222 }
4223
4224 /* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct ada_symbol_info
4225 records in OBSTACKP. Do nothing if SYM is a duplicate. */
4226
4227 static void
4228 add_defn_to_vec (struct obstack *obstackp,
4229 struct symbol *sym,
4230 struct block *block)
4231 {
4232 int i;
4233 struct ada_symbol_info *prevDefns = defns_collected (obstackp, 0);
4234
4235 /* Do not try to complete stub types, as the debugger is probably
4236 already scanning all symbols matching a certain name at the
4237 time when this function is called. Trying to replace the stub
4238 type by its associated full type will cause us to restart a scan
4239 which may lead to an infinite recursion. Instead, the client
4240 collecting the matching symbols will end up collecting several
4241 matches, with at least one of them complete. It can then filter
4242 out the stub ones if needed. */
4243
4244 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
4245 {
4246 if (lesseq_defined_than (sym, prevDefns[i].sym))
4247 return;
4248 else if (lesseq_defined_than (prevDefns[i].sym, sym))
4249 {
4250 prevDefns[i].sym = sym;
4251 prevDefns[i].block = block;
4252 return;
4253 }
4254 }
4255
4256 {
4257 struct ada_symbol_info info;
4258
4259 info.sym = sym;
4260 info.block = block;
4261 obstack_grow (obstackp, &info, sizeof (struct ada_symbol_info));
4262 }
4263 }
4264
4265 /* Number of ada_symbol_info structures currently collected in
4266 current vector in *OBSTACKP. */
4267
4268 static int
4269 num_defns_collected (struct obstack *obstackp)
4270 {
4271 return obstack_object_size (obstackp) / sizeof (struct ada_symbol_info);
4272 }
4273
4274 /* Vector of ada_symbol_info structures currently collected in current
4275 vector in *OBSTACKP. If FINISH, close off the vector and return
4276 its final address. */
4277
4278 static struct ada_symbol_info *
4279 defns_collected (struct obstack *obstackp, int finish)
4280 {
4281 if (finish)
4282 return obstack_finish (obstackp);
4283 else
4284 return (struct ada_symbol_info *) obstack_base (obstackp);
4285 }
4286
4287 /* Return a minimal symbol matching NAME according to Ada decoding
4288 rules. Returns NULL if there is no such minimal symbol. Names
4289 prefixed with "standard__" are handled specially: "standard__" is
4290 first stripped off, and only static and global symbols are searched. */
4291
4292 struct minimal_symbol *
4293 ada_lookup_simple_minsym (const char *name)
4294 {
4295 struct objfile *objfile;
4296 struct minimal_symbol *msymbol;
4297 int wild_match;
4298
4299 if (strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
4300 {
4301 name += sizeof ("standard__") - 1;
4302 wild_match = 0;
4303 }
4304 else
4305 wild_match = (strstr (name, "__") == NULL);
4306
4307 ALL_MSYMBOLS (objfile, msymbol)
4308 {
4309 if (match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match)
4310 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4311 return msymbol;
4312 }
4313
4314 return NULL;
4315 }
4316
4317 /* For all subprograms that statically enclose the subprogram of the
4318 selected frame, add symbols matching identifier NAME in DOMAIN
4319 and their blocks to the list of data in OBSTACKP, as for
4320 ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
4321 wildcard prefix. */
4322
4323 static void
4324 add_symbols_from_enclosing_procs (struct obstack *obstackp,
4325 const char *name, domain_enum namespace,
4326 int wild_match)
4327 {
4328 }
4329
4330 /* True if TYPE is definitely an artificial type supplied to a symbol
4331 for which no debugging information was given in the symbol file. */
4332
4333 static int
4334 is_nondebugging_type (struct type *type)
4335 {
4336 char *name = ada_type_name (type);
4337
4338 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4339 }
4340
4341 /* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4342 duplicate other symbols in the list (The only case I know of where
4343 this happens is when object files containing stabs-in-ecoff are
4344 linked with files containing ordinary ecoff debugging symbols (or no
4345 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4346 Returns the number of items in the modified list. */
4347
4348 static int
4349 remove_extra_symbols (struct ada_symbol_info *syms, int nsyms)
4350 {
4351 int i, j;
4352
4353 i = 0;
4354 while (i < nsyms)
4355 {
4356 int remove = 0;
4357
4358 /* If two symbols have the same name and one of them is a stub type,
4359 the get rid of the stub. */
4360
4361 if (TYPE_STUB (SYMBOL_TYPE (syms[i].sym))
4362 && SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL)
4363 {
4364 for (j = 0; j < nsyms; j++)
4365 {
4366 if (j != i
4367 && !TYPE_STUB (SYMBOL_TYPE (syms[j].sym))
4368 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4369 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
4370 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0)
4371 remove = 1;
4372 }
4373 }
4374
4375 /* Two symbols with the same name, same class and same address
4376 should be identical. */
4377
4378 else if (SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL
4379 && SYMBOL_CLASS (syms[i].sym) == LOC_STATIC
4380 && is_nondebugging_type (SYMBOL_TYPE (syms[i].sym)))
4381 {
4382 for (j = 0; j < nsyms; j += 1)
4383 {
4384 if (i != j
4385 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4386 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
4387 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0
4388 && SYMBOL_CLASS (syms[i].sym) == SYMBOL_CLASS (syms[j].sym)
4389 && SYMBOL_VALUE_ADDRESS (syms[i].sym)
4390 == SYMBOL_VALUE_ADDRESS (syms[j].sym))
4391 remove = 1;
4392 }
4393 }
4394
4395 if (remove)
4396 {
4397 for (j = i + 1; j < nsyms; j += 1)
4398 syms[j - 1] = syms[j];
4399 nsyms -= 1;
4400 }
4401
4402 i += 1;
4403 }
4404 return nsyms;
4405 }
4406
4407 /* Given a type that corresponds to a renaming entity, use the type name
4408 to extract the scope (package name or function name, fully qualified,
4409 and following the GNAT encoding convention) where this renaming has been
4410 defined. The string returned needs to be deallocated after use. */
4411
4412 static char *
4413 xget_renaming_scope (struct type *renaming_type)
4414 {
4415 /* The renaming types adhere to the following convention:
4416 <scope>__<rename>___<XR extension>.
4417 So, to extract the scope, we search for the "___XR" extension,
4418 and then backtrack until we find the first "__". */
4419
4420 const char *name = type_name_no_tag (renaming_type);
4421 char *suffix = strstr (name, "___XR");
4422 char *last;
4423 int scope_len;
4424 char *scope;
4425
4426 /* Now, backtrack a bit until we find the first "__". Start looking
4427 at suffix - 3, as the <rename> part is at least one character long. */
4428
4429 for (last = suffix - 3; last > name; last--)
4430 if (last[0] == '_' && last[1] == '_')
4431 break;
4432
4433 /* Make a copy of scope and return it. */
4434
4435 scope_len = last - name;
4436 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
4437
4438 strncpy (scope, name, scope_len);
4439 scope[scope_len] = '\0';
4440
4441 return scope;
4442 }
4443
4444 /* Return nonzero if NAME corresponds to a package name. */
4445
4446 static int
4447 is_package_name (const char *name)
4448 {
4449 /* Here, We take advantage of the fact that no symbols are generated
4450 for packages, while symbols are generated for each function.
4451 So the condition for NAME represent a package becomes equivalent
4452 to NAME not existing in our list of symbols. There is only one
4453 small complication with library-level functions (see below). */
4454
4455 char *fun_name;
4456
4457 /* If it is a function that has not been defined at library level,
4458 then we should be able to look it up in the symbols. */
4459 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4460 return 0;
4461
4462 /* Library-level function names start with "_ada_". See if function
4463 "_ada_" followed by NAME can be found. */
4464
4465 /* Do a quick check that NAME does not contain "__", since library-level
4466 functions names cannot contain "__" in them. */
4467 if (strstr (name, "__") != NULL)
4468 return 0;
4469
4470 fun_name = xstrprintf ("_ada_%s", name);
4471
4472 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
4473 }
4474
4475 /* Return nonzero if SYM corresponds to a renaming entity that is
4476 not visible from FUNCTION_NAME. */
4477
4478 static int
4479 old_renaming_is_invisible (const struct symbol *sym, char *function_name)
4480 {
4481 char *scope;
4482
4483 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
4484 return 0;
4485
4486 scope = xget_renaming_scope (SYMBOL_TYPE (sym));
4487
4488 make_cleanup (xfree, scope);
4489
4490 /* If the rename has been defined in a package, then it is visible. */
4491 if (is_package_name (scope))
4492 return 0;
4493
4494 /* Check that the rename is in the current function scope by checking
4495 that its name starts with SCOPE. */
4496
4497 /* If the function name starts with "_ada_", it means that it is
4498 a library-level function. Strip this prefix before doing the
4499 comparison, as the encoding for the renaming does not contain
4500 this prefix. */
4501 if (strncmp (function_name, "_ada_", 5) == 0)
4502 function_name += 5;
4503
4504 return (strncmp (function_name, scope, strlen (scope)) != 0);
4505 }
4506
4507 /* Remove entries from SYMS that corresponds to a renaming entity that
4508 is not visible from the function associated with CURRENT_BLOCK or
4509 that is superfluous due to the presence of more specific renaming
4510 information. Places surviving symbols in the initial entries of
4511 SYMS and returns the number of surviving symbols.
4512
4513 Rationale:
4514 First, in cases where an object renaming is implemented as a
4515 reference variable, GNAT may produce both the actual reference
4516 variable and the renaming encoding. In this case, we discard the
4517 latter.
4518
4519 Second, GNAT emits a type following a specified encoding for each renaming
4520 entity. Unfortunately, STABS currently does not support the definition
4521 of types that are local to a given lexical block, so all renamings types
4522 are emitted at library level. As a consequence, if an application
4523 contains two renaming entities using the same name, and a user tries to
4524 print the value of one of these entities, the result of the ada symbol
4525 lookup will also contain the wrong renaming type.
4526
4527 This function partially covers for this limitation by attempting to
4528 remove from the SYMS list renaming symbols that should be visible
4529 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4530 method with the current information available. The implementation
4531 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4532
4533 - When the user tries to print a rename in a function while there
4534 is another rename entity defined in a package: Normally, the
4535 rename in the function has precedence over the rename in the
4536 package, so the latter should be removed from the list. This is
4537 currently not the case.
4538
4539 - This function will incorrectly remove valid renames if
4540 the CURRENT_BLOCK corresponds to a function which symbol name
4541 has been changed by an "Export" pragma. As a consequence,
4542 the user will be unable to print such rename entities. */
4543
4544 static int
4545 remove_irrelevant_renamings (struct ada_symbol_info *syms,
4546 int nsyms, const struct block *current_block)
4547 {
4548 struct symbol *current_function;
4549 char *current_function_name;
4550 int i;
4551 int is_new_style_renaming;
4552
4553 /* If there is both a renaming foo___XR... encoded as a variable and
4554 a simple variable foo in the same block, discard the latter.
4555 First, zero out such symbols, then compress. */
4556 is_new_style_renaming = 0;
4557 for (i = 0; i < nsyms; i += 1)
4558 {
4559 struct symbol *sym = syms[i].sym;
4560 struct block *block = syms[i].block;
4561 const char *name;
4562 const char *suffix;
4563
4564 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4565 continue;
4566 name = SYMBOL_LINKAGE_NAME (sym);
4567 suffix = strstr (name, "___XR");
4568
4569 if (suffix != NULL)
4570 {
4571 int name_len = suffix - name;
4572 int j;
4573
4574 is_new_style_renaming = 1;
4575 for (j = 0; j < nsyms; j += 1)
4576 if (i != j && syms[j].sym != NULL
4577 && strncmp (name, SYMBOL_LINKAGE_NAME (syms[j].sym),
4578 name_len) == 0
4579 && block == syms[j].block)
4580 syms[j].sym = NULL;
4581 }
4582 }
4583 if (is_new_style_renaming)
4584 {
4585 int j, k;
4586
4587 for (j = k = 0; j < nsyms; j += 1)
4588 if (syms[j].sym != NULL)
4589 {
4590 syms[k] = syms[j];
4591 k += 1;
4592 }
4593 return k;
4594 }
4595
4596 /* Extract the function name associated to CURRENT_BLOCK.
4597 Abort if unable to do so. */
4598
4599 if (current_block == NULL)
4600 return nsyms;
4601
4602 current_function = block_linkage_function (current_block);
4603 if (current_function == NULL)
4604 return nsyms;
4605
4606 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
4607 if (current_function_name == NULL)
4608 return nsyms;
4609
4610 /* Check each of the symbols, and remove it from the list if it is
4611 a type corresponding to a renaming that is out of the scope of
4612 the current block. */
4613
4614 i = 0;
4615 while (i < nsyms)
4616 {
4617 if (ada_parse_renaming (syms[i].sym, NULL, NULL, NULL)
4618 == ADA_OBJECT_RENAMING
4619 && old_renaming_is_invisible (syms[i].sym, current_function_name))
4620 {
4621 int j;
4622
4623 for (j = i + 1; j < nsyms; j += 1)
4624 syms[j - 1] = syms[j];
4625 nsyms -= 1;
4626 }
4627 else
4628 i += 1;
4629 }
4630
4631 return nsyms;
4632 }
4633
4634 /* Add to OBSTACKP all symbols from BLOCK (and its super-blocks)
4635 whose name and domain match NAME and DOMAIN respectively.
4636 If no match was found, then extend the search to "enclosing"
4637 routines (in other words, if we're inside a nested function,
4638 search the symbols defined inside the enclosing functions).
4639
4640 Note: This function assumes that OBSTACKP has 0 (zero) element in it. */
4641
4642 static void
4643 ada_add_local_symbols (struct obstack *obstackp, const char *name,
4644 struct block *block, domain_enum domain,
4645 int wild_match)
4646 {
4647 int block_depth = 0;
4648
4649 while (block != NULL)
4650 {
4651 block_depth += 1;
4652 ada_add_block_symbols (obstackp, block, name, domain, NULL, wild_match);
4653
4654 /* If we found a non-function match, assume that's the one. */
4655 if (is_nonfunction (defns_collected (obstackp, 0),
4656 num_defns_collected (obstackp)))
4657 return;
4658
4659 block = BLOCK_SUPERBLOCK (block);
4660 }
4661
4662 /* If no luck so far, try to find NAME as a local symbol in some lexically
4663 enclosing subprogram. */
4664 if (num_defns_collected (obstackp) == 0 && block_depth > 2)
4665 add_symbols_from_enclosing_procs (obstackp, name, domain, wild_match);
4666 }
4667
4668 /* An object of this type is used as the user_data argument when
4669 calling the map_matching_symbols method. */
4670
4671 struct match_data
4672 {
4673 struct objfile *objfile;
4674 struct obstack *obstackp;
4675 struct symbol *arg_sym;
4676 int found_sym;
4677 };
4678
4679 /* A callback for add_matching_symbols that adds SYM, found in BLOCK,
4680 to a list of symbols. DATA0 is a pointer to a struct match_data *
4681 containing the obstack that collects the symbol list, the file that SYM
4682 must come from, a flag indicating whether a non-argument symbol has
4683 been found in the current block, and the last argument symbol
4684 passed in SYM within the current block (if any). When SYM is null,
4685 marking the end of a block, the argument symbol is added if no
4686 other has been found. */
4687
4688 static int
4689 aux_add_nonlocal_symbols (struct block *block, struct symbol *sym, void *data0)
4690 {
4691 struct match_data *data = (struct match_data *) data0;
4692
4693 if (sym == NULL)
4694 {
4695 if (!data->found_sym && data->arg_sym != NULL)
4696 add_defn_to_vec (data->obstackp,
4697 fixup_symbol_section (data->arg_sym, data->objfile),
4698 block);
4699 data->found_sym = 0;
4700 data->arg_sym = NULL;
4701 }
4702 else
4703 {
4704 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
4705 return 0;
4706 else if (SYMBOL_IS_ARGUMENT (sym))
4707 data->arg_sym = sym;
4708 else
4709 {
4710 data->found_sym = 1;
4711 add_defn_to_vec (data->obstackp,
4712 fixup_symbol_section (sym, data->objfile),
4713 block);
4714 }
4715 }
4716 return 0;
4717 }
4718
4719 /* Compare STRING1 to STRING2, with results as for strcmp.
4720 Compatible with strcmp_iw in that strcmp_iw (STRING1, STRING2) <= 0
4721 implies compare_names (STRING1, STRING2) (they may differ as to
4722 what symbols compare equal). */
4723
4724 static int
4725 compare_names (const char *string1, const char *string2)
4726 {
4727 while (*string1 != '\0' && *string2 != '\0')
4728 {
4729 if (isspace (*string1) || isspace (*string2))
4730 return strcmp_iw_ordered (string1, string2);
4731 if (*string1 != *string2)
4732 break;
4733 string1 += 1;
4734 string2 += 1;
4735 }
4736 switch (*string1)
4737 {
4738 case '(':
4739 return strcmp_iw_ordered (string1, string2);
4740 case '_':
4741 if (*string2 == '\0')
4742 {
4743 if (is_name_suffix (string2))
4744 return 0;
4745 else
4746 return -1;
4747 }
4748 default:
4749 if (*string2 == '(')
4750 return strcmp_iw_ordered (string1, string2);
4751 else
4752 return *string1 - *string2;
4753 }
4754 }
4755
4756 /* Add to OBSTACKP all non-local symbols whose name and domain match
4757 NAME and DOMAIN respectively. The search is performed on GLOBAL_BLOCK
4758 symbols if GLOBAL is non-zero, or on STATIC_BLOCK symbols otherwise. */
4759
4760 static void
4761 add_nonlocal_symbols (struct obstack *obstackp, const char *name,
4762 domain_enum domain, int global,
4763 int is_wild_match)
4764 {
4765 struct objfile *objfile;
4766 struct match_data data;
4767
4768 data.obstackp = obstackp;
4769 data.arg_sym = NULL;
4770
4771 ALL_OBJFILES (objfile)
4772 {
4773 data.objfile = objfile;
4774
4775 if (is_wild_match)
4776 objfile->sf->qf->map_matching_symbols (name, domain, objfile, global,
4777 aux_add_nonlocal_symbols, &data,
4778 wild_match, NULL);
4779 else
4780 objfile->sf->qf->map_matching_symbols (name, domain, objfile, global,
4781 aux_add_nonlocal_symbols, &data,
4782 full_match, compare_names);
4783 }
4784
4785 if (num_defns_collected (obstackp) == 0 && global && !is_wild_match)
4786 {
4787 ALL_OBJFILES (objfile)
4788 {
4789 char *name1 = alloca (strlen (name) + sizeof ("_ada_"));
4790 strcpy (name1, "_ada_");
4791 strcpy (name1 + sizeof ("_ada_") - 1, name);
4792 data.objfile = objfile;
4793 objfile->sf->qf->map_matching_symbols (name1, domain,
4794 objfile, global,
4795 aux_add_nonlocal_symbols,
4796 &data,
4797 full_match, compare_names);
4798 }
4799 }
4800 }
4801
4802 /* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing
4803 scope and in global scopes, returning the number of matches. Sets
4804 *RESULTS to point to a vector of (SYM,BLOCK) tuples,
4805 indicating the symbols found and the blocks and symbol tables (if
4806 any) in which they were found. This vector are transient---good only to
4807 the next call of ada_lookup_symbol_list. Any non-function/non-enumeral
4808 symbol match within the nest of blocks whose innermost member is BLOCK0,
4809 is the one match returned (no other matches in that or
4810 enclosing blocks is returned). If there are any matches in or
4811 surrounding BLOCK0, then these alone are returned. Otherwise, the
4812 search extends to global and file-scope (static) symbol tables.
4813 Names prefixed with "standard__" are handled specially: "standard__"
4814 is first stripped off, and only static and global symbols are searched. */
4815
4816 int
4817 ada_lookup_symbol_list (const char *name0, const struct block *block0,
4818 domain_enum namespace,
4819 struct ada_symbol_info **results)
4820 {
4821 struct symbol *sym;
4822 struct block *block;
4823 const char *name;
4824 int wild_match;
4825 int cacheIfUnique;
4826 int ndefns;
4827
4828 obstack_free (&symbol_list_obstack, NULL);
4829 obstack_init (&symbol_list_obstack);
4830
4831 cacheIfUnique = 0;
4832
4833 /* Search specified block and its superiors. */
4834
4835 wild_match = (strstr (name0, "__") == NULL);
4836 name = name0;
4837 block = (struct block *) block0; /* FIXME: No cast ought to be
4838 needed, but adding const will
4839 have a cascade effect. */
4840
4841 /* Special case: If the user specifies a symbol name inside package
4842 Standard, do a non-wild matching of the symbol name without
4843 the "standard__" prefix. This was primarily introduced in order
4844 to allow the user to specifically access the standard exceptions
4845 using, for instance, Standard.Constraint_Error when Constraint_Error
4846 is ambiguous (due to the user defining its own Constraint_Error
4847 entity inside its program). */
4848 if (strncmp (name0, "standard__", sizeof ("standard__") - 1) == 0)
4849 {
4850 wild_match = 0;
4851 block = NULL;
4852 name = name0 + sizeof ("standard__") - 1;
4853 }
4854
4855 /* Check the non-global symbols. If we have ANY match, then we're done. */
4856
4857 ada_add_local_symbols (&symbol_list_obstack, name, block, namespace,
4858 wild_match);
4859 if (num_defns_collected (&symbol_list_obstack) > 0)
4860 goto done;
4861
4862 /* No non-global symbols found. Check our cache to see if we have
4863 already performed this search before. If we have, then return
4864 the same result. */
4865
4866 cacheIfUnique = 1;
4867 if (lookup_cached_symbol (name0, namespace, &sym, &block))
4868 {
4869 if (sym != NULL)
4870 add_defn_to_vec (&symbol_list_obstack, sym, block);
4871 goto done;
4872 }
4873
4874 /* Search symbols from all global blocks. */
4875
4876 add_nonlocal_symbols (&symbol_list_obstack, name, namespace, 1,
4877 wild_match);
4878
4879 /* Now add symbols from all per-file blocks if we've gotten no hits
4880 (not strictly correct, but perhaps better than an error). */
4881
4882 if (num_defns_collected (&symbol_list_obstack) == 0)
4883 add_nonlocal_symbols (&symbol_list_obstack, name, namespace, 0,
4884 wild_match);
4885
4886 done:
4887 ndefns = num_defns_collected (&symbol_list_obstack);
4888 *results = defns_collected (&symbol_list_obstack, 1);
4889
4890 ndefns = remove_extra_symbols (*results, ndefns);
4891
4892 if (ndefns == 0)
4893 cache_symbol (name0, namespace, NULL, NULL);
4894
4895 if (ndefns == 1 && cacheIfUnique)
4896 cache_symbol (name0, namespace, (*results)[0].sym, (*results)[0].block);
4897
4898 ndefns = remove_irrelevant_renamings (*results, ndefns, block0);
4899
4900 return ndefns;
4901 }
4902
4903 struct symbol *
4904 ada_lookup_encoded_symbol (const char *name, const struct block *block0,
4905 domain_enum namespace, struct block **block_found)
4906 {
4907 struct ada_symbol_info *candidates;
4908 int n_candidates;
4909
4910 n_candidates = ada_lookup_symbol_list (name, block0, namespace, &candidates);
4911
4912 if (n_candidates == 0)
4913 return NULL;
4914
4915 if (block_found != NULL)
4916 *block_found = candidates[0].block;
4917
4918 return fixup_symbol_section (candidates[0].sym, NULL);
4919 }
4920
4921 /* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
4922 scope and in global scopes, or NULL if none. NAME is folded and
4923 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
4924 choosing the first symbol if there are multiple choices.
4925 *IS_A_FIELD_OF_THIS is set to 0 and *SYMTAB is set to the symbol
4926 table in which the symbol was found (in both cases, these
4927 assignments occur only if the pointers are non-null). */
4928 struct symbol *
4929 ada_lookup_symbol (const char *name, const struct block *block0,
4930 domain_enum namespace, int *is_a_field_of_this)
4931 {
4932 if (is_a_field_of_this != NULL)
4933 *is_a_field_of_this = 0;
4934
4935 return
4936 ada_lookup_encoded_symbol (ada_encode (ada_fold_name (name)),
4937 block0, namespace, NULL);
4938 }
4939
4940 static struct symbol *
4941 ada_lookup_symbol_nonlocal (const char *name,
4942 const struct block *block,
4943 const domain_enum domain)
4944 {
4945 return ada_lookup_symbol (name, block_static_block (block), domain, NULL);
4946 }
4947
4948
4949 /* True iff STR is a possible encoded suffix of a normal Ada name
4950 that is to be ignored for matching purposes. Suffixes of parallel
4951 names (e.g., XVE) are not included here. Currently, the possible suffixes
4952 are given by any of the regular expressions:
4953
4954 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
4955 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
4956 _E[0-9]+[bs]$ [protected object entry suffixes]
4957 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
4958
4959 Also, any leading "__[0-9]+" sequence is skipped before the suffix
4960 match is performed. This sequence is used to differentiate homonyms,
4961 is an optional part of a valid name suffix. */
4962
4963 static int
4964 is_name_suffix (const char *str)
4965 {
4966 int k;
4967 const char *matching;
4968 const int len = strlen (str);
4969
4970 /* Skip optional leading __[0-9]+. */
4971
4972 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
4973 {
4974 str += 3;
4975 while (isdigit (str[0]))
4976 str += 1;
4977 }
4978
4979 /* [.$][0-9]+ */
4980
4981 if (str[0] == '.' || str[0] == '$')
4982 {
4983 matching = str + 1;
4984 while (isdigit (matching[0]))
4985 matching += 1;
4986 if (matching[0] == '\0')
4987 return 1;
4988 }
4989
4990 /* ___[0-9]+ */
4991
4992 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
4993 {
4994 matching = str + 3;
4995 while (isdigit (matching[0]))
4996 matching += 1;
4997 if (matching[0] == '\0')
4998 return 1;
4999 }
5000
5001 #if 0
5002 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
5003 with a N at the end. Unfortunately, the compiler uses the same
5004 convention for other internal types it creates. So treating
5005 all entity names that end with an "N" as a name suffix causes
5006 some regressions. For instance, consider the case of an enumerated
5007 type. To support the 'Image attribute, it creates an array whose
5008 name ends with N.
5009 Having a single character like this as a suffix carrying some
5010 information is a bit risky. Perhaps we should change the encoding
5011 to be something like "_N" instead. In the meantime, do not do
5012 the following check. */
5013 /* Protected Object Subprograms */
5014 if (len == 1 && str [0] == 'N')
5015 return 1;
5016 #endif
5017
5018 /* _E[0-9]+[bs]$ */
5019 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
5020 {
5021 matching = str + 3;
5022 while (isdigit (matching[0]))
5023 matching += 1;
5024 if ((matching[0] == 'b' || matching[0] == 's')
5025 && matching [1] == '\0')
5026 return 1;
5027 }
5028
5029 /* ??? We should not modify STR directly, as we are doing below. This
5030 is fine in this case, but may become problematic later if we find
5031 that this alternative did not work, and want to try matching
5032 another one from the begining of STR. Since we modified it, we
5033 won't be able to find the begining of the string anymore! */
5034 if (str[0] == 'X')
5035 {
5036 str += 1;
5037 while (str[0] != '_' && str[0] != '\0')
5038 {
5039 if (str[0] != 'n' && str[0] != 'b')
5040 return 0;
5041 str += 1;
5042 }
5043 }
5044
5045 if (str[0] == '\000')
5046 return 1;
5047
5048 if (str[0] == '_')
5049 {
5050 if (str[1] != '_' || str[2] == '\000')
5051 return 0;
5052 if (str[2] == '_')
5053 {
5054 if (strcmp (str + 3, "JM") == 0)
5055 return 1;
5056 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5057 the LJM suffix in favor of the JM one. But we will
5058 still accept LJM as a valid suffix for a reasonable
5059 amount of time, just to allow ourselves to debug programs
5060 compiled using an older version of GNAT. */
5061 if (strcmp (str + 3, "LJM") == 0)
5062 return 1;
5063 if (str[3] != 'X')
5064 return 0;
5065 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
5066 || str[4] == 'U' || str[4] == 'P')
5067 return 1;
5068 if (str[4] == 'R' && str[5] != 'T')
5069 return 1;
5070 return 0;
5071 }
5072 if (!isdigit (str[2]))
5073 return 0;
5074 for (k = 3; str[k] != '\0'; k += 1)
5075 if (!isdigit (str[k]) && str[k] != '_')
5076 return 0;
5077 return 1;
5078 }
5079 if (str[0] == '$' && isdigit (str[1]))
5080 {
5081 for (k = 2; str[k] != '\0'; k += 1)
5082 if (!isdigit (str[k]) && str[k] != '_')
5083 return 0;
5084 return 1;
5085 }
5086 return 0;
5087 }
5088
5089 /* Return non-zero if the string starting at NAME and ending before
5090 NAME_END contains no capital letters. */
5091
5092 static int
5093 is_valid_name_for_wild_match (const char *name0)
5094 {
5095 const char *decoded_name = ada_decode (name0);
5096 int i;
5097
5098 /* If the decoded name starts with an angle bracket, it means that
5099 NAME0 does not follow the GNAT encoding format. It should then
5100 not be allowed as a possible wild match. */
5101 if (decoded_name[0] == '<')
5102 return 0;
5103
5104 for (i=0; decoded_name[i] != '\0'; i++)
5105 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5106 return 0;
5107
5108 return 1;
5109 }
5110
5111 /* Advance *NAMEP to next occurrence of TARGET0 in the string NAME0
5112 that could start a simple name. Assumes that *NAMEP points into
5113 the string beginning at NAME0. */
5114
5115 static int
5116 advance_wild_match (const char **namep, const char *name0, int target0)
5117 {
5118 const char *name = *namep;
5119
5120 while (1)
5121 {
5122 int t0, t1;
5123
5124 t0 = *name;
5125 if (t0 == '_')
5126 {
5127 t1 = name[1];
5128 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5129 {
5130 name += 1;
5131 if (name == name0 + 5 && strncmp (name0, "_ada", 4) == 0)
5132 break;
5133 else
5134 name += 1;
5135 }
5136 else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
5137 || name[2] == target0))
5138 {
5139 name += 2;
5140 break;
5141 }
5142 else
5143 return 0;
5144 }
5145 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5146 name += 1;
5147 else
5148 return 0;
5149 }
5150
5151 *namep = name;
5152 return 1;
5153 }
5154
5155 /* Return 0 iff NAME encodes a name of the form prefix.PATN. Ignores any
5156 informational suffixes of NAME (i.e., for which is_name_suffix is
5157 true). Assumes that PATN is a lower-cased Ada simple name. */
5158
5159 static int
5160 wild_match (const char *name, const char *patn)
5161 {
5162 const char *p, *n;
5163 const char *name0 = name;
5164
5165 while (1)
5166 {
5167 const char *match = name;
5168
5169 if (*name == *patn)
5170 {
5171 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
5172 if (*p != *name)
5173 break;
5174 if (*p == '\0' && is_name_suffix (name))
5175 return match != name0 && !is_valid_name_for_wild_match (name0);
5176
5177 if (name[-1] == '_')
5178 name -= 1;
5179 }
5180 if (!advance_wild_match (&name, name0, *patn))
5181 return 1;
5182 }
5183 }
5184
5185 /* Returns 0 iff symbol name SYM_NAME matches SEARCH_NAME, apart from
5186 informational suffix. */
5187
5188 static int
5189 full_match (const char *sym_name, const char *search_name)
5190 {
5191 return !match_name (sym_name, search_name, 0);
5192 }
5193
5194
5195 /* Add symbols from BLOCK matching identifier NAME in DOMAIN to
5196 vector *defn_symbols, updating the list of symbols in OBSTACKP
5197 (if necessary). If WILD, treat as NAME with a wildcard prefix.
5198 OBJFILE is the section containing BLOCK.
5199 SYMTAB is recorded with each symbol added. */
5200
5201 static void
5202 ada_add_block_symbols (struct obstack *obstackp,
5203 struct block *block, const char *name,
5204 domain_enum domain, struct objfile *objfile,
5205 int wild)
5206 {
5207 struct dict_iterator iter;
5208 int name_len = strlen (name);
5209 /* A matching argument symbol, if any. */
5210 struct symbol *arg_sym;
5211 /* Set true when we find a matching non-argument symbol. */
5212 int found_sym;
5213 struct symbol *sym;
5214
5215 arg_sym = NULL;
5216 found_sym = 0;
5217 if (wild)
5218 {
5219 for (sym = dict_iter_match_first (BLOCK_DICT (block), name,
5220 wild_match, &iter);
5221 sym != NULL; sym = dict_iter_match_next (name, wild_match, &iter))
5222 {
5223 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5224 SYMBOL_DOMAIN (sym), domain)
5225 && wild_match (SYMBOL_LINKAGE_NAME (sym), name) == 0)
5226 {
5227 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
5228 continue;
5229 else if (SYMBOL_IS_ARGUMENT (sym))
5230 arg_sym = sym;
5231 else
5232 {
5233 found_sym = 1;
5234 add_defn_to_vec (obstackp,
5235 fixup_symbol_section (sym, objfile),
5236 block);
5237 }
5238 }
5239 }
5240 }
5241 else
5242 {
5243 for (sym = dict_iter_match_first (BLOCK_DICT (block), name,
5244 full_match, &iter);
5245 sym != NULL; sym = dict_iter_match_next (name, full_match, &iter))
5246 {
5247 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5248 SYMBOL_DOMAIN (sym), domain))
5249 {
5250 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5251 {
5252 if (SYMBOL_IS_ARGUMENT (sym))
5253 arg_sym = sym;
5254 else
5255 {
5256 found_sym = 1;
5257 add_defn_to_vec (obstackp,
5258 fixup_symbol_section (sym, objfile),
5259 block);
5260 }
5261 }
5262 }
5263 }
5264 }
5265
5266 if (!found_sym && arg_sym != NULL)
5267 {
5268 add_defn_to_vec (obstackp,
5269 fixup_symbol_section (arg_sym, objfile),
5270 block);
5271 }
5272
5273 if (!wild)
5274 {
5275 arg_sym = NULL;
5276 found_sym = 0;
5277
5278 ALL_BLOCK_SYMBOLS (block, iter, sym)
5279 {
5280 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5281 SYMBOL_DOMAIN (sym), domain))
5282 {
5283 int cmp;
5284
5285 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
5286 if (cmp == 0)
5287 {
5288 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (sym), 5);
5289 if (cmp == 0)
5290 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
5291 name_len);
5292 }
5293
5294 if (cmp == 0
5295 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
5296 {
5297 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5298 {
5299 if (SYMBOL_IS_ARGUMENT (sym))
5300 arg_sym = sym;
5301 else
5302 {
5303 found_sym = 1;
5304 add_defn_to_vec (obstackp,
5305 fixup_symbol_section (sym, objfile),
5306 block);
5307 }
5308 }
5309 }
5310 }
5311 }
5312
5313 /* NOTE: This really shouldn't be needed for _ada_ symbols.
5314 They aren't parameters, right? */
5315 if (!found_sym && arg_sym != NULL)
5316 {
5317 add_defn_to_vec (obstackp,
5318 fixup_symbol_section (arg_sym, objfile),
5319 block);
5320 }
5321 }
5322 }
5323 \f
5324
5325 /* Symbol Completion */
5326
5327 /* If SYM_NAME is a completion candidate for TEXT, return this symbol
5328 name in a form that's appropriate for the completion. The result
5329 does not need to be deallocated, but is only good until the next call.
5330
5331 TEXT_LEN is equal to the length of TEXT.
5332 Perform a wild match if WILD_MATCH is set.
5333 ENCODED should be set if TEXT represents the start of a symbol name
5334 in its encoded form. */
5335
5336 static const char *
5337 symbol_completion_match (const char *sym_name,
5338 const char *text, int text_len,
5339 int wild_match, int encoded)
5340 {
5341 const int verbatim_match = (text[0] == '<');
5342 int match = 0;
5343
5344 if (verbatim_match)
5345 {
5346 /* Strip the leading angle bracket. */
5347 text = text + 1;
5348 text_len--;
5349 }
5350
5351 /* First, test against the fully qualified name of the symbol. */
5352
5353 if (strncmp (sym_name, text, text_len) == 0)
5354 match = 1;
5355
5356 if (match && !encoded)
5357 {
5358 /* One needed check before declaring a positive match is to verify
5359 that iff we are doing a verbatim match, the decoded version
5360 of the symbol name starts with '<'. Otherwise, this symbol name
5361 is not a suitable completion. */
5362 const char *sym_name_copy = sym_name;
5363 int has_angle_bracket;
5364
5365 sym_name = ada_decode (sym_name);
5366 has_angle_bracket = (sym_name[0] == '<');
5367 match = (has_angle_bracket == verbatim_match);
5368 sym_name = sym_name_copy;
5369 }
5370
5371 if (match && !verbatim_match)
5372 {
5373 /* When doing non-verbatim match, another check that needs to
5374 be done is to verify that the potentially matching symbol name
5375 does not include capital letters, because the ada-mode would
5376 not be able to understand these symbol names without the
5377 angle bracket notation. */
5378 const char *tmp;
5379
5380 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
5381 if (*tmp != '\0')
5382 match = 0;
5383 }
5384
5385 /* Second: Try wild matching... */
5386
5387 if (!match && wild_match)
5388 {
5389 /* Since we are doing wild matching, this means that TEXT
5390 may represent an unqualified symbol name. We therefore must
5391 also compare TEXT against the unqualified name of the symbol. */
5392 sym_name = ada_unqualified_name (ada_decode (sym_name));
5393
5394 if (strncmp (sym_name, text, text_len) == 0)
5395 match = 1;
5396 }
5397
5398 /* Finally: If we found a mach, prepare the result to return. */
5399
5400 if (!match)
5401 return NULL;
5402
5403 if (verbatim_match)
5404 sym_name = add_angle_brackets (sym_name);
5405
5406 if (!encoded)
5407 sym_name = ada_decode (sym_name);
5408
5409 return sym_name;
5410 }
5411
5412 DEF_VEC_P (char_ptr);
5413
5414 /* A companion function to ada_make_symbol_completion_list().
5415 Check if SYM_NAME represents a symbol which name would be suitable
5416 to complete TEXT (TEXT_LEN is the length of TEXT), in which case
5417 it is appended at the end of the given string vector SV.
5418
5419 ORIG_TEXT is the string original string from the user command
5420 that needs to be completed. WORD is the entire command on which
5421 completion should be performed. These two parameters are used to
5422 determine which part of the symbol name should be added to the
5423 completion vector.
5424 if WILD_MATCH is set, then wild matching is performed.
5425 ENCODED should be set if TEXT represents a symbol name in its
5426 encoded formed (in which case the completion should also be
5427 encoded). */
5428
5429 static void
5430 symbol_completion_add (VEC(char_ptr) **sv,
5431 const char *sym_name,
5432 const char *text, int text_len,
5433 const char *orig_text, const char *word,
5434 int wild_match, int encoded)
5435 {
5436 const char *match = symbol_completion_match (sym_name, text, text_len,
5437 wild_match, encoded);
5438 char *completion;
5439
5440 if (match == NULL)
5441 return;
5442
5443 /* We found a match, so add the appropriate completion to the given
5444 string vector. */
5445
5446 if (word == orig_text)
5447 {
5448 completion = xmalloc (strlen (match) + 5);
5449 strcpy (completion, match);
5450 }
5451 else if (word > orig_text)
5452 {
5453 /* Return some portion of sym_name. */
5454 completion = xmalloc (strlen (match) + 5);
5455 strcpy (completion, match + (word - orig_text));
5456 }
5457 else
5458 {
5459 /* Return some of ORIG_TEXT plus sym_name. */
5460 completion = xmalloc (strlen (match) + (orig_text - word) + 5);
5461 strncpy (completion, word, orig_text - word);
5462 completion[orig_text - word] = '\0';
5463 strcat (completion, match);
5464 }
5465
5466 VEC_safe_push (char_ptr, *sv, completion);
5467 }
5468
5469 /* An object of this type is passed as the user_data argument to the
5470 map_partial_symbol_names method. */
5471 struct add_partial_datum
5472 {
5473 VEC(char_ptr) **completions;
5474 char *text;
5475 int text_len;
5476 char *text0;
5477 char *word;
5478 int wild_match;
5479 int encoded;
5480 };
5481
5482 /* A callback for map_partial_symbol_names. */
5483 static void
5484 ada_add_partial_symbol_completions (const char *name, void *user_data)
5485 {
5486 struct add_partial_datum *data = user_data;
5487
5488 symbol_completion_add (data->completions, name,
5489 data->text, data->text_len, data->text0, data->word,
5490 data->wild_match, data->encoded);
5491 }
5492
5493 /* Return a list of possible symbol names completing TEXT0. The list
5494 is NULL terminated. WORD is the entire command on which completion
5495 is made. */
5496
5497 static char **
5498 ada_make_symbol_completion_list (char *text0, char *word)
5499 {
5500 char *text;
5501 int text_len;
5502 int wild_match;
5503 int encoded;
5504 VEC(char_ptr) *completions = VEC_alloc (char_ptr, 128);
5505 struct symbol *sym;
5506 struct symtab *s;
5507 struct minimal_symbol *msymbol;
5508 struct objfile *objfile;
5509 struct block *b, *surrounding_static_block = 0;
5510 int i;
5511 struct dict_iterator iter;
5512
5513 if (text0[0] == '<')
5514 {
5515 text = xstrdup (text0);
5516 make_cleanup (xfree, text);
5517 text_len = strlen (text);
5518 wild_match = 0;
5519 encoded = 1;
5520 }
5521 else
5522 {
5523 text = xstrdup (ada_encode (text0));
5524 make_cleanup (xfree, text);
5525 text_len = strlen (text);
5526 for (i = 0; i < text_len; i++)
5527 text[i] = tolower (text[i]);
5528
5529 encoded = (strstr (text0, "__") != NULL);
5530 /* If the name contains a ".", then the user is entering a fully
5531 qualified entity name, and the match must not be done in wild
5532 mode. Similarly, if the user wants to complete what looks like
5533 an encoded name, the match must not be done in wild mode. */
5534 wild_match = (strchr (text0, '.') == NULL && !encoded);
5535 }
5536
5537 /* First, look at the partial symtab symbols. */
5538 {
5539 struct add_partial_datum data;
5540
5541 data.completions = &completions;
5542 data.text = text;
5543 data.text_len = text_len;
5544 data.text0 = text0;
5545 data.word = word;
5546 data.wild_match = wild_match;
5547 data.encoded = encoded;
5548 map_partial_symbol_names (ada_add_partial_symbol_completions, &data);
5549 }
5550
5551 /* At this point scan through the misc symbol vectors and add each
5552 symbol you find to the list. Eventually we want to ignore
5553 anything that isn't a text symbol (everything else will be
5554 handled by the psymtab code above). */
5555
5556 ALL_MSYMBOLS (objfile, msymbol)
5557 {
5558 QUIT;
5559 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (msymbol),
5560 text, text_len, text0, word, wild_match, encoded);
5561 }
5562
5563 /* Search upwards from currently selected frame (so that we can
5564 complete on local vars. */
5565
5566 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
5567 {
5568 if (!BLOCK_SUPERBLOCK (b))
5569 surrounding_static_block = b; /* For elmin of dups */
5570
5571 ALL_BLOCK_SYMBOLS (b, iter, sym)
5572 {
5573 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5574 text, text_len, text0, word,
5575 wild_match, encoded);
5576 }
5577 }
5578
5579 /* Go through the symtabs and check the externs and statics for
5580 symbols which match. */
5581
5582 ALL_SYMTABS (objfile, s)
5583 {
5584 QUIT;
5585 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
5586 ALL_BLOCK_SYMBOLS (b, iter, sym)
5587 {
5588 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5589 text, text_len, text0, word,
5590 wild_match, encoded);
5591 }
5592 }
5593
5594 ALL_SYMTABS (objfile, s)
5595 {
5596 QUIT;
5597 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
5598 /* Don't do this block twice. */
5599 if (b == surrounding_static_block)
5600 continue;
5601 ALL_BLOCK_SYMBOLS (b, iter, sym)
5602 {
5603 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5604 text, text_len, text0, word,
5605 wild_match, encoded);
5606 }
5607 }
5608
5609 /* Append the closing NULL entry. */
5610 VEC_safe_push (char_ptr, completions, NULL);
5611
5612 /* Make a copy of the COMPLETIONS VEC before we free it, and then
5613 return the copy. It's unfortunate that we have to make a copy
5614 of an array that we're about to destroy, but there is nothing much
5615 we can do about it. Fortunately, it's typically not a very large
5616 array. */
5617 {
5618 const size_t completions_size =
5619 VEC_length (char_ptr, completions) * sizeof (char *);
5620 char **result = malloc (completions_size);
5621
5622 memcpy (result, VEC_address (char_ptr, completions), completions_size);
5623
5624 VEC_free (char_ptr, completions);
5625 return result;
5626 }
5627 }
5628
5629 /* Field Access */
5630
5631 /* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
5632 for tagged types. */
5633
5634 static int
5635 ada_is_dispatch_table_ptr_type (struct type *type)
5636 {
5637 char *name;
5638
5639 if (TYPE_CODE (type) != TYPE_CODE_PTR)
5640 return 0;
5641
5642 name = TYPE_NAME (TYPE_TARGET_TYPE (type));
5643 if (name == NULL)
5644 return 0;
5645
5646 return (strcmp (name, "ada__tags__dispatch_table") == 0);
5647 }
5648
5649 /* True if field number FIELD_NUM in struct or union type TYPE is supposed
5650 to be invisible to users. */
5651
5652 int
5653 ada_is_ignored_field (struct type *type, int field_num)
5654 {
5655 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
5656 return 1;
5657
5658 /* Check the name of that field. */
5659 {
5660 const char *name = TYPE_FIELD_NAME (type, field_num);
5661
5662 /* Anonymous field names should not be printed.
5663 brobecker/2007-02-20: I don't think this can actually happen
5664 but we don't want to print the value of annonymous fields anyway. */
5665 if (name == NULL)
5666 return 1;
5667
5668 /* A field named "_parent" is internally generated by GNAT for
5669 tagged types, and should not be printed either. */
5670 if (name[0] == '_' && strncmp (name, "_parent", 7) != 0)
5671 return 1;
5672 }
5673
5674 /* If this is the dispatch table of a tagged type, then ignore. */
5675 if (ada_is_tagged_type (type, 1)
5676 && ada_is_dispatch_table_ptr_type (TYPE_FIELD_TYPE (type, field_num)))
5677 return 1;
5678
5679 /* Not a special field, so it should not be ignored. */
5680 return 0;
5681 }
5682
5683 /* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
5684 pointer or reference type whose ultimate target has a tag field. */
5685
5686 int
5687 ada_is_tagged_type (struct type *type, int refok)
5688 {
5689 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
5690 }
5691
5692 /* True iff TYPE represents the type of X'Tag */
5693
5694 int
5695 ada_is_tag_type (struct type *type)
5696 {
5697 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
5698 return 0;
5699 else
5700 {
5701 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5702
5703 return (name != NULL
5704 && strcmp (name, "ada__tags__dispatch_table") == 0);
5705 }
5706 }
5707
5708 /* The type of the tag on VAL. */
5709
5710 struct type *
5711 ada_tag_type (struct value *val)
5712 {
5713 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0, NULL);
5714 }
5715
5716 /* The value of the tag on VAL. */
5717
5718 struct value *
5719 ada_value_tag (struct value *val)
5720 {
5721 return ada_value_struct_elt (val, "_tag", 0);
5722 }
5723
5724 /* The value of the tag on the object of type TYPE whose contents are
5725 saved at VALADDR, if it is non-null, or is at memory address
5726 ADDRESS. */
5727
5728 static struct value *
5729 value_tag_from_contents_and_address (struct type *type,
5730 const gdb_byte *valaddr,
5731 CORE_ADDR address)
5732 {
5733 int tag_byte_offset;
5734 struct type *tag_type;
5735
5736 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
5737 NULL, NULL, NULL))
5738 {
5739 const gdb_byte *valaddr1 = ((valaddr == NULL)
5740 ? NULL
5741 : valaddr + tag_byte_offset);
5742 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
5743
5744 return value_from_contents_and_address (tag_type, valaddr1, address1);
5745 }
5746 return NULL;
5747 }
5748
5749 static struct type *
5750 type_from_tag (struct value *tag)
5751 {
5752 const char *type_name = ada_tag_name (tag);
5753
5754 if (type_name != NULL)
5755 return ada_find_any_type (ada_encode (type_name));
5756 return NULL;
5757 }
5758
5759 struct tag_args
5760 {
5761 struct value *tag;
5762 char *name;
5763 };
5764
5765
5766 static int ada_tag_name_1 (void *);
5767 static int ada_tag_name_2 (struct tag_args *);
5768
5769 /* Wrapper function used by ada_tag_name. Given a struct tag_args*
5770 value ARGS, sets ARGS->name to the tag name of ARGS->tag.
5771 The value stored in ARGS->name is valid until the next call to
5772 ada_tag_name_1. */
5773
5774 static int
5775 ada_tag_name_1 (void *args0)
5776 {
5777 struct tag_args *args = (struct tag_args *) args0;
5778 static char name[1024];
5779 char *p;
5780 struct value *val;
5781
5782 args->name = NULL;
5783 val = ada_value_struct_elt (args->tag, "tsd", 1);
5784 if (val == NULL)
5785 return ada_tag_name_2 (args);
5786 val = ada_value_struct_elt (val, "expanded_name", 1);
5787 if (val == NULL)
5788 return 0;
5789 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5790 for (p = name; *p != '\0'; p += 1)
5791 if (isalpha (*p))
5792 *p = tolower (*p);
5793 args->name = name;
5794 return 0;
5795 }
5796
5797 /* Return the "ada__tags__type_specific_data" type. */
5798
5799 static struct type *
5800 ada_get_tsd_type (struct inferior *inf)
5801 {
5802 struct ada_inferior_data *data = get_ada_inferior_data (inf);
5803
5804 if (data->tsd_type == 0)
5805 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
5806 return data->tsd_type;
5807 }
5808
5809 /* Utility function for ada_tag_name_1 that tries the second
5810 representation for the dispatch table (in which there is no
5811 explicit 'tsd' field in the referent of the tag pointer, and instead
5812 the tsd pointer is stored just before the dispatch table. */
5813
5814 static int
5815 ada_tag_name_2 (struct tag_args *args)
5816 {
5817 struct type *info_type;
5818 static char name[1024];
5819 char *p;
5820 struct value *val, *valp;
5821
5822 args->name = NULL;
5823 info_type = ada_get_tsd_type (current_inferior());
5824 if (info_type == NULL)
5825 return 0;
5826 info_type = lookup_pointer_type (lookup_pointer_type (info_type));
5827 valp = value_cast (info_type, args->tag);
5828 if (valp == NULL)
5829 return 0;
5830 val = value_ind (value_ptradd (valp, -1));
5831 if (val == NULL)
5832 return 0;
5833 val = ada_value_struct_elt (val, "expanded_name", 1);
5834 if (val == NULL)
5835 return 0;
5836 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5837 for (p = name; *p != '\0'; p += 1)
5838 if (isalpha (*p))
5839 *p = tolower (*p);
5840 args->name = name;
5841 return 0;
5842 }
5843
5844 /* The type name of the dynamic type denoted by the 'tag value TAG, as
5845 a C string. */
5846
5847 const char *
5848 ada_tag_name (struct value *tag)
5849 {
5850 struct tag_args args;
5851
5852 if (!ada_is_tag_type (value_type (tag)))
5853 return NULL;
5854 args.tag = tag;
5855 args.name = NULL;
5856 catch_errors (ada_tag_name_1, &args, NULL, RETURN_MASK_ALL);
5857 return args.name;
5858 }
5859
5860 /* The parent type of TYPE, or NULL if none. */
5861
5862 struct type *
5863 ada_parent_type (struct type *type)
5864 {
5865 int i;
5866
5867 type = ada_check_typedef (type);
5868
5869 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5870 return NULL;
5871
5872 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5873 if (ada_is_parent_field (type, i))
5874 {
5875 struct type *parent_type = TYPE_FIELD_TYPE (type, i);
5876
5877 /* If the _parent field is a pointer, then dereference it. */
5878 if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
5879 parent_type = TYPE_TARGET_TYPE (parent_type);
5880 /* If there is a parallel XVS type, get the actual base type. */
5881 parent_type = ada_get_base_type (parent_type);
5882
5883 return ada_check_typedef (parent_type);
5884 }
5885
5886 return NULL;
5887 }
5888
5889 /* True iff field number FIELD_NUM of structure type TYPE contains the
5890 parent-type (inherited) fields of a derived type. Assumes TYPE is
5891 a structure type with at least FIELD_NUM+1 fields. */
5892
5893 int
5894 ada_is_parent_field (struct type *type, int field_num)
5895 {
5896 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
5897
5898 return (name != NULL
5899 && (strncmp (name, "PARENT", 6) == 0
5900 || strncmp (name, "_parent", 7) == 0));
5901 }
5902
5903 /* True iff field number FIELD_NUM of structure type TYPE is a
5904 transparent wrapper field (which should be silently traversed when doing
5905 field selection and flattened when printing). Assumes TYPE is a
5906 structure type with at least FIELD_NUM+1 fields. Such fields are always
5907 structures. */
5908
5909 int
5910 ada_is_wrapper_field (struct type *type, int field_num)
5911 {
5912 const char *name = TYPE_FIELD_NAME (type, field_num);
5913
5914 return (name != NULL
5915 && (strncmp (name, "PARENT", 6) == 0
5916 || strcmp (name, "REP") == 0
5917 || strncmp (name, "_parent", 7) == 0
5918 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
5919 }
5920
5921 /* True iff field number FIELD_NUM of structure or union type TYPE
5922 is a variant wrapper. Assumes TYPE is a structure type with at least
5923 FIELD_NUM+1 fields. */
5924
5925 int
5926 ada_is_variant_part (struct type *type, int field_num)
5927 {
5928 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
5929
5930 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
5931 || (is_dynamic_field (type, field_num)
5932 && (TYPE_CODE (TYPE_TARGET_TYPE (field_type))
5933 == TYPE_CODE_UNION)));
5934 }
5935
5936 /* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
5937 whose discriminants are contained in the record type OUTER_TYPE,
5938 returns the type of the controlling discriminant for the variant.
5939 May return NULL if the type could not be found. */
5940
5941 struct type *
5942 ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
5943 {
5944 char *name = ada_variant_discrim_name (var_type);
5945
5946 return ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
5947 }
5948
5949 /* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
5950 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
5951 represents a 'when others' clause; otherwise 0. */
5952
5953 int
5954 ada_is_others_clause (struct type *type, int field_num)
5955 {
5956 const char *name = TYPE_FIELD_NAME (type, field_num);
5957
5958 return (name != NULL && name[0] == 'O');
5959 }
5960
5961 /* Assuming that TYPE0 is the type of the variant part of a record,
5962 returns the name of the discriminant controlling the variant.
5963 The value is valid until the next call to ada_variant_discrim_name. */
5964
5965 char *
5966 ada_variant_discrim_name (struct type *type0)
5967 {
5968 static char *result = NULL;
5969 static size_t result_len = 0;
5970 struct type *type;
5971 const char *name;
5972 const char *discrim_end;
5973 const char *discrim_start;
5974
5975 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
5976 type = TYPE_TARGET_TYPE (type0);
5977 else
5978 type = type0;
5979
5980 name = ada_type_name (type);
5981
5982 if (name == NULL || name[0] == '\000')
5983 return "";
5984
5985 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
5986 discrim_end -= 1)
5987 {
5988 if (strncmp (discrim_end, "___XVN", 6) == 0)
5989 break;
5990 }
5991 if (discrim_end == name)
5992 return "";
5993
5994 for (discrim_start = discrim_end; discrim_start != name + 3;
5995 discrim_start -= 1)
5996 {
5997 if (discrim_start == name + 1)
5998 return "";
5999 if ((discrim_start > name + 3
6000 && strncmp (discrim_start - 3, "___", 3) == 0)
6001 || discrim_start[-1] == '.')
6002 break;
6003 }
6004
6005 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
6006 strncpy (result, discrim_start, discrim_end - discrim_start);
6007 result[discrim_end - discrim_start] = '\0';
6008 return result;
6009 }
6010
6011 /* Scan STR for a subtype-encoded number, beginning at position K.
6012 Put the position of the character just past the number scanned in
6013 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6014 Return 1 if there was a valid number at the given position, and 0
6015 otherwise. A "subtype-encoded" number consists of the absolute value
6016 in decimal, followed by the letter 'm' to indicate a negative number.
6017 Assumes 0m does not occur. */
6018
6019 int
6020 ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
6021 {
6022 ULONGEST RU;
6023
6024 if (!isdigit (str[k]))
6025 return 0;
6026
6027 /* Do it the hard way so as not to make any assumption about
6028 the relationship of unsigned long (%lu scan format code) and
6029 LONGEST. */
6030 RU = 0;
6031 while (isdigit (str[k]))
6032 {
6033 RU = RU * 10 + (str[k] - '0');
6034 k += 1;
6035 }
6036
6037 if (str[k] == 'm')
6038 {
6039 if (R != NULL)
6040 *R = (-(LONGEST) (RU - 1)) - 1;
6041 k += 1;
6042 }
6043 else if (R != NULL)
6044 *R = (LONGEST) RU;
6045
6046 /* NOTE on the above: Technically, C does not say what the results of
6047 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6048 number representable as a LONGEST (although either would probably work
6049 in most implementations). When RU>0, the locution in the then branch
6050 above is always equivalent to the negative of RU. */
6051
6052 if (new_k != NULL)
6053 *new_k = k;
6054 return 1;
6055 }
6056
6057 /* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6058 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6059 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
6060
6061 int
6062 ada_in_variant (LONGEST val, struct type *type, int field_num)
6063 {
6064 const char *name = TYPE_FIELD_NAME (type, field_num);
6065 int p;
6066
6067 p = 0;
6068 while (1)
6069 {
6070 switch (name[p])
6071 {
6072 case '\0':
6073 return 0;
6074 case 'S':
6075 {
6076 LONGEST W;
6077
6078 if (!ada_scan_number (name, p + 1, &W, &p))
6079 return 0;
6080 if (val == W)
6081 return 1;
6082 break;
6083 }
6084 case 'R':
6085 {
6086 LONGEST L, U;
6087
6088 if (!ada_scan_number (name, p + 1, &L, &p)
6089 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
6090 return 0;
6091 if (val >= L && val <= U)
6092 return 1;
6093 break;
6094 }
6095 case 'O':
6096 return 1;
6097 default:
6098 return 0;
6099 }
6100 }
6101 }
6102
6103 /* FIXME: Lots of redundancy below. Try to consolidate. */
6104
6105 /* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6106 ARG_TYPE, extract and return the value of one of its (non-static)
6107 fields. FIELDNO says which field. Differs from value_primitive_field
6108 only in that it can handle packed values of arbitrary type. */
6109
6110 static struct value *
6111 ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
6112 struct type *arg_type)
6113 {
6114 struct type *type;
6115
6116 arg_type = ada_check_typedef (arg_type);
6117 type = TYPE_FIELD_TYPE (arg_type, fieldno);
6118
6119 /* Handle packed fields. */
6120
6121 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
6122 {
6123 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6124 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
6125
6126 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
6127 offset + bit_pos / 8,
6128 bit_pos % 8, bit_size, type);
6129 }
6130 else
6131 return value_primitive_field (arg1, offset, fieldno, arg_type);
6132 }
6133
6134 /* Find field with name NAME in object of type TYPE. If found,
6135 set the following for each argument that is non-null:
6136 - *FIELD_TYPE_P to the field's type;
6137 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6138 an object of that type;
6139 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6140 - *BIT_SIZE_P to its size in bits if the field is packed, and
6141 0 otherwise;
6142 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6143 fields up to but not including the desired field, or by the total
6144 number of fields if not found. A NULL value of NAME never
6145 matches; the function just counts visible fields in this case.
6146
6147 Returns 1 if found, 0 otherwise. */
6148
6149 static int
6150 find_struct_field (char *name, struct type *type, int offset,
6151 struct type **field_type_p,
6152 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
6153 int *index_p)
6154 {
6155 int i;
6156
6157 type = ada_check_typedef (type);
6158
6159 if (field_type_p != NULL)
6160 *field_type_p = NULL;
6161 if (byte_offset_p != NULL)
6162 *byte_offset_p = 0;
6163 if (bit_offset_p != NULL)
6164 *bit_offset_p = 0;
6165 if (bit_size_p != NULL)
6166 *bit_size_p = 0;
6167
6168 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6169 {
6170 int bit_pos = TYPE_FIELD_BITPOS (type, i);
6171 int fld_offset = offset + bit_pos / 8;
6172 char *t_field_name = TYPE_FIELD_NAME (type, i);
6173
6174 if (t_field_name == NULL)
6175 continue;
6176
6177 else if (name != NULL && field_name_match (t_field_name, name))
6178 {
6179 int bit_size = TYPE_FIELD_BITSIZE (type, i);
6180
6181 if (field_type_p != NULL)
6182 *field_type_p = TYPE_FIELD_TYPE (type, i);
6183 if (byte_offset_p != NULL)
6184 *byte_offset_p = fld_offset;
6185 if (bit_offset_p != NULL)
6186 *bit_offset_p = bit_pos % 8;
6187 if (bit_size_p != NULL)
6188 *bit_size_p = bit_size;
6189 return 1;
6190 }
6191 else if (ada_is_wrapper_field (type, i))
6192 {
6193 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
6194 field_type_p, byte_offset_p, bit_offset_p,
6195 bit_size_p, index_p))
6196 return 1;
6197 }
6198 else if (ada_is_variant_part (type, i))
6199 {
6200 /* PNH: Wait. Do we ever execute this section, or is ARG always of
6201 fixed type?? */
6202 int j;
6203 struct type *field_type
6204 = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
6205
6206 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
6207 {
6208 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
6209 fld_offset
6210 + TYPE_FIELD_BITPOS (field_type, j) / 8,
6211 field_type_p, byte_offset_p,
6212 bit_offset_p, bit_size_p, index_p))
6213 return 1;
6214 }
6215 }
6216 else if (index_p != NULL)
6217 *index_p += 1;
6218 }
6219 return 0;
6220 }
6221
6222 /* Number of user-visible fields in record type TYPE. */
6223
6224 static int
6225 num_visible_fields (struct type *type)
6226 {
6227 int n;
6228
6229 n = 0;
6230 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
6231 return n;
6232 }
6233
6234 /* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
6235 and search in it assuming it has (class) type TYPE.
6236 If found, return value, else return NULL.
6237
6238 Searches recursively through wrapper fields (e.g., '_parent'). */
6239
6240 static struct value *
6241 ada_search_struct_field (char *name, struct value *arg, int offset,
6242 struct type *type)
6243 {
6244 int i;
6245
6246 type = ada_check_typedef (type);
6247 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6248 {
6249 char *t_field_name = TYPE_FIELD_NAME (type, i);
6250
6251 if (t_field_name == NULL)
6252 continue;
6253
6254 else if (field_name_match (t_field_name, name))
6255 return ada_value_primitive_field (arg, offset, i, type);
6256
6257 else if (ada_is_wrapper_field (type, i))
6258 {
6259 struct value *v = /* Do not let indent join lines here. */
6260 ada_search_struct_field (name, arg,
6261 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6262 TYPE_FIELD_TYPE (type, i));
6263
6264 if (v != NULL)
6265 return v;
6266 }
6267
6268 else if (ada_is_variant_part (type, i))
6269 {
6270 /* PNH: Do we ever get here? See find_struct_field. */
6271 int j;
6272 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6273 i));
6274 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
6275
6276 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
6277 {
6278 struct value *v = ada_search_struct_field /* Force line
6279 break. */
6280 (name, arg,
6281 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
6282 TYPE_FIELD_TYPE (field_type, j));
6283
6284 if (v != NULL)
6285 return v;
6286 }
6287 }
6288 }
6289 return NULL;
6290 }
6291
6292 static struct value *ada_index_struct_field_1 (int *, struct value *,
6293 int, struct type *);
6294
6295
6296 /* Return field #INDEX in ARG, where the index is that returned by
6297 * find_struct_field through its INDEX_P argument. Adjust the address
6298 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
6299 * If found, return value, else return NULL. */
6300
6301 static struct value *
6302 ada_index_struct_field (int index, struct value *arg, int offset,
6303 struct type *type)
6304 {
6305 return ada_index_struct_field_1 (&index, arg, offset, type);
6306 }
6307
6308
6309 /* Auxiliary function for ada_index_struct_field. Like
6310 * ada_index_struct_field, but takes index from *INDEX_P and modifies
6311 * *INDEX_P. */
6312
6313 static struct value *
6314 ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
6315 struct type *type)
6316 {
6317 int i;
6318 type = ada_check_typedef (type);
6319
6320 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6321 {
6322 if (TYPE_FIELD_NAME (type, i) == NULL)
6323 continue;
6324 else if (ada_is_wrapper_field (type, i))
6325 {
6326 struct value *v = /* Do not let indent join lines here. */
6327 ada_index_struct_field_1 (index_p, arg,
6328 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6329 TYPE_FIELD_TYPE (type, i));
6330
6331 if (v != NULL)
6332 return v;
6333 }
6334
6335 else if (ada_is_variant_part (type, i))
6336 {
6337 /* PNH: Do we ever get here? See ada_search_struct_field,
6338 find_struct_field. */
6339 error (_("Cannot assign this kind of variant record"));
6340 }
6341 else if (*index_p == 0)
6342 return ada_value_primitive_field (arg, offset, i, type);
6343 else
6344 *index_p -= 1;
6345 }
6346 return NULL;
6347 }
6348
6349 /* Given ARG, a value of type (pointer or reference to a)*
6350 structure/union, extract the component named NAME from the ultimate
6351 target structure/union and return it as a value with its
6352 appropriate type.
6353
6354 The routine searches for NAME among all members of the structure itself
6355 and (recursively) among all members of any wrapper members
6356 (e.g., '_parent').
6357
6358 If NO_ERR, then simply return NULL in case of error, rather than
6359 calling error. */
6360
6361 struct value *
6362 ada_value_struct_elt (struct value *arg, char *name, int no_err)
6363 {
6364 struct type *t, *t1;
6365 struct value *v;
6366
6367 v = NULL;
6368 t1 = t = ada_check_typedef (value_type (arg));
6369 if (TYPE_CODE (t) == TYPE_CODE_REF)
6370 {
6371 t1 = TYPE_TARGET_TYPE (t);
6372 if (t1 == NULL)
6373 goto BadValue;
6374 t1 = ada_check_typedef (t1);
6375 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
6376 {
6377 arg = coerce_ref (arg);
6378 t = t1;
6379 }
6380 }
6381
6382 while (TYPE_CODE (t) == TYPE_CODE_PTR)
6383 {
6384 t1 = TYPE_TARGET_TYPE (t);
6385 if (t1 == NULL)
6386 goto BadValue;
6387 t1 = ada_check_typedef (t1);
6388 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
6389 {
6390 arg = value_ind (arg);
6391 t = t1;
6392 }
6393 else
6394 break;
6395 }
6396
6397 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
6398 goto BadValue;
6399
6400 if (t1 == t)
6401 v = ada_search_struct_field (name, arg, 0, t);
6402 else
6403 {
6404 int bit_offset, bit_size, byte_offset;
6405 struct type *field_type;
6406 CORE_ADDR address;
6407
6408 if (TYPE_CODE (t) == TYPE_CODE_PTR)
6409 address = value_as_address (arg);
6410 else
6411 address = unpack_pointer (t, value_contents (arg));
6412
6413 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL, 1);
6414 if (find_struct_field (name, t1, 0,
6415 &field_type, &byte_offset, &bit_offset,
6416 &bit_size, NULL))
6417 {
6418 if (bit_size != 0)
6419 {
6420 if (TYPE_CODE (t) == TYPE_CODE_REF)
6421 arg = ada_coerce_ref (arg);
6422 else
6423 arg = ada_value_ind (arg);
6424 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
6425 bit_offset, bit_size,
6426 field_type);
6427 }
6428 else
6429 v = value_at_lazy (field_type, address + byte_offset);
6430 }
6431 }
6432
6433 if (v != NULL || no_err)
6434 return v;
6435 else
6436 error (_("There is no member named %s."), name);
6437
6438 BadValue:
6439 if (no_err)
6440 return NULL;
6441 else
6442 error (_("Attempt to extract a component of "
6443 "a value that is not a record."));
6444 }
6445
6446 /* Given a type TYPE, look up the type of the component of type named NAME.
6447 If DISPP is non-null, add its byte displacement from the beginning of a
6448 structure (pointed to by a value) of type TYPE to *DISPP (does not
6449 work for packed fields).
6450
6451 Matches any field whose name has NAME as a prefix, possibly
6452 followed by "___".
6453
6454 TYPE can be either a struct or union. If REFOK, TYPE may also
6455 be a (pointer or reference)+ to a struct or union, and the
6456 ultimate target type will be searched.
6457
6458 Looks recursively into variant clauses and parent types.
6459
6460 If NOERR is nonzero, return NULL if NAME is not suitably defined or
6461 TYPE is not a type of the right kind. */
6462
6463 static struct type *
6464 ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
6465 int noerr, int *dispp)
6466 {
6467 int i;
6468
6469 if (name == NULL)
6470 goto BadName;
6471
6472 if (refok && type != NULL)
6473 while (1)
6474 {
6475 type = ada_check_typedef (type);
6476 if (TYPE_CODE (type) != TYPE_CODE_PTR
6477 && TYPE_CODE (type) != TYPE_CODE_REF)
6478 break;
6479 type = TYPE_TARGET_TYPE (type);
6480 }
6481
6482 if (type == NULL
6483 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
6484 && TYPE_CODE (type) != TYPE_CODE_UNION))
6485 {
6486 if (noerr)
6487 return NULL;
6488 else
6489 {
6490 target_terminal_ours ();
6491 gdb_flush (gdb_stdout);
6492 if (type == NULL)
6493 error (_("Type (null) is not a structure or union type"));
6494 else
6495 {
6496 /* XXX: type_sprint */
6497 fprintf_unfiltered (gdb_stderr, _("Type "));
6498 type_print (type, "", gdb_stderr, -1);
6499 error (_(" is not a structure or union type"));
6500 }
6501 }
6502 }
6503
6504 type = to_static_fixed_type (type);
6505
6506 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6507 {
6508 char *t_field_name = TYPE_FIELD_NAME (type, i);
6509 struct type *t;
6510 int disp;
6511
6512 if (t_field_name == NULL)
6513 continue;
6514
6515 else if (field_name_match (t_field_name, name))
6516 {
6517 if (dispp != NULL)
6518 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
6519 return ada_check_typedef (TYPE_FIELD_TYPE (type, i));
6520 }
6521
6522 else if (ada_is_wrapper_field (type, i))
6523 {
6524 disp = 0;
6525 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
6526 0, 1, &disp);
6527 if (t != NULL)
6528 {
6529 if (dispp != NULL)
6530 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6531 return t;
6532 }
6533 }
6534
6535 else if (ada_is_variant_part (type, i))
6536 {
6537 int j;
6538 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6539 i));
6540
6541 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
6542 {
6543 /* FIXME pnh 2008/01/26: We check for a field that is
6544 NOT wrapped in a struct, since the compiler sometimes
6545 generates these for unchecked variant types. Revisit
6546 if the compiler changes this practice. */
6547 char *v_field_name = TYPE_FIELD_NAME (field_type, j);
6548 disp = 0;
6549 if (v_field_name != NULL
6550 && field_name_match (v_field_name, name))
6551 t = ada_check_typedef (TYPE_FIELD_TYPE (field_type, j));
6552 else
6553 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type,
6554 j),
6555 name, 0, 1, &disp);
6556
6557 if (t != NULL)
6558 {
6559 if (dispp != NULL)
6560 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6561 return t;
6562 }
6563 }
6564 }
6565
6566 }
6567
6568 BadName:
6569 if (!noerr)
6570 {
6571 target_terminal_ours ();
6572 gdb_flush (gdb_stdout);
6573 if (name == NULL)
6574 {
6575 /* XXX: type_sprint */
6576 fprintf_unfiltered (gdb_stderr, _("Type "));
6577 type_print (type, "", gdb_stderr, -1);
6578 error (_(" has no component named <null>"));
6579 }
6580 else
6581 {
6582 /* XXX: type_sprint */
6583 fprintf_unfiltered (gdb_stderr, _("Type "));
6584 type_print (type, "", gdb_stderr, -1);
6585 error (_(" has no component named %s"), name);
6586 }
6587 }
6588
6589 return NULL;
6590 }
6591
6592 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6593 within a value of type OUTER_TYPE, return true iff VAR_TYPE
6594 represents an unchecked union (that is, the variant part of a
6595 record that is named in an Unchecked_Union pragma). */
6596
6597 static int
6598 is_unchecked_variant (struct type *var_type, struct type *outer_type)
6599 {
6600 char *discrim_name = ada_variant_discrim_name (var_type);
6601
6602 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1, NULL)
6603 == NULL);
6604 }
6605
6606
6607 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6608 within a value of type OUTER_TYPE that is stored in GDB at
6609 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
6610 numbering from 0) is applicable. Returns -1 if none are. */
6611
6612 int
6613 ada_which_variant_applies (struct type *var_type, struct type *outer_type,
6614 const gdb_byte *outer_valaddr)
6615 {
6616 int others_clause;
6617 int i;
6618 char *discrim_name = ada_variant_discrim_name (var_type);
6619 struct value *outer;
6620 struct value *discrim;
6621 LONGEST discrim_val;
6622
6623 outer = value_from_contents_and_address (outer_type, outer_valaddr, 0);
6624 discrim = ada_value_struct_elt (outer, discrim_name, 1);
6625 if (discrim == NULL)
6626 return -1;
6627 discrim_val = value_as_long (discrim);
6628
6629 others_clause = -1;
6630 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
6631 {
6632 if (ada_is_others_clause (var_type, i))
6633 others_clause = i;
6634 else if (ada_in_variant (discrim_val, var_type, i))
6635 return i;
6636 }
6637
6638 return others_clause;
6639 }
6640 \f
6641
6642
6643 /* Dynamic-Sized Records */
6644
6645 /* Strategy: The type ostensibly attached to a value with dynamic size
6646 (i.e., a size that is not statically recorded in the debugging
6647 data) does not accurately reflect the size or layout of the value.
6648 Our strategy is to convert these values to values with accurate,
6649 conventional types that are constructed on the fly. */
6650
6651 /* There is a subtle and tricky problem here. In general, we cannot
6652 determine the size of dynamic records without its data. However,
6653 the 'struct value' data structure, which GDB uses to represent
6654 quantities in the inferior process (the target), requires the size
6655 of the type at the time of its allocation in order to reserve space
6656 for GDB's internal copy of the data. That's why the
6657 'to_fixed_xxx_type' routines take (target) addresses as parameters,
6658 rather than struct value*s.
6659
6660 However, GDB's internal history variables ($1, $2, etc.) are
6661 struct value*s containing internal copies of the data that are not, in
6662 general, the same as the data at their corresponding addresses in
6663 the target. Fortunately, the types we give to these values are all
6664 conventional, fixed-size types (as per the strategy described
6665 above), so that we don't usually have to perform the
6666 'to_fixed_xxx_type' conversions to look at their values.
6667 Unfortunately, there is one exception: if one of the internal
6668 history variables is an array whose elements are unconstrained
6669 records, then we will need to create distinct fixed types for each
6670 element selected. */
6671
6672 /* The upshot of all of this is that many routines take a (type, host
6673 address, target address) triple as arguments to represent a value.
6674 The host address, if non-null, is supposed to contain an internal
6675 copy of the relevant data; otherwise, the program is to consult the
6676 target at the target address. */
6677
6678 /* Assuming that VAL0 represents a pointer value, the result of
6679 dereferencing it. Differs from value_ind in its treatment of
6680 dynamic-sized types. */
6681
6682 struct value *
6683 ada_value_ind (struct value *val0)
6684 {
6685 struct value *val = unwrap_value (value_ind (val0));
6686
6687 return ada_to_fixed_value (val);
6688 }
6689
6690 /* The value resulting from dereferencing any "reference to"
6691 qualifiers on VAL0. */
6692
6693 static struct value *
6694 ada_coerce_ref (struct value *val0)
6695 {
6696 if (TYPE_CODE (value_type (val0)) == TYPE_CODE_REF)
6697 {
6698 struct value *val = val0;
6699
6700 val = coerce_ref (val);
6701 val = unwrap_value (val);
6702 return ada_to_fixed_value (val);
6703 }
6704 else
6705 return val0;
6706 }
6707
6708 /* Return OFF rounded upward if necessary to a multiple of
6709 ALIGNMENT (a power of 2). */
6710
6711 static unsigned int
6712 align_value (unsigned int off, unsigned int alignment)
6713 {
6714 return (off + alignment - 1) & ~(alignment - 1);
6715 }
6716
6717 /* Return the bit alignment required for field #F of template type TYPE. */
6718
6719 static unsigned int
6720 field_alignment (struct type *type, int f)
6721 {
6722 const char *name = TYPE_FIELD_NAME (type, f);
6723 int len;
6724 int align_offset;
6725
6726 /* The field name should never be null, unless the debugging information
6727 is somehow malformed. In this case, we assume the field does not
6728 require any alignment. */
6729 if (name == NULL)
6730 return 1;
6731
6732 len = strlen (name);
6733
6734 if (!isdigit (name[len - 1]))
6735 return 1;
6736
6737 if (isdigit (name[len - 2]))
6738 align_offset = len - 2;
6739 else
6740 align_offset = len - 1;
6741
6742 if (align_offset < 7 || strncmp ("___XV", name + align_offset - 6, 5) != 0)
6743 return TARGET_CHAR_BIT;
6744
6745 return atoi (name + align_offset) * TARGET_CHAR_BIT;
6746 }
6747
6748 /* Find a symbol named NAME. Ignores ambiguity. */
6749
6750 struct symbol *
6751 ada_find_any_symbol (const char *name)
6752 {
6753 struct symbol *sym;
6754
6755 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
6756 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
6757 return sym;
6758
6759 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
6760 return sym;
6761 }
6762
6763 /* Find a type named NAME. Ignores ambiguity. This routine will look
6764 solely for types defined by debug info, it will not search the GDB
6765 primitive types. */
6766
6767 struct type *
6768 ada_find_any_type (const char *name)
6769 {
6770 struct symbol *sym = ada_find_any_symbol (name);
6771
6772 if (sym != NULL)
6773 return SYMBOL_TYPE (sym);
6774
6775 return NULL;
6776 }
6777
6778 /* Given NAME and an associated BLOCK, search all symbols for
6779 NAME suffixed with "___XR", which is the ``renaming'' symbol
6780 associated to NAME. Return this symbol if found, return
6781 NULL otherwise. */
6782
6783 struct symbol *
6784 ada_find_renaming_symbol (const char *name, struct block *block)
6785 {
6786 struct symbol *sym;
6787
6788 sym = find_old_style_renaming_symbol (name, block);
6789
6790 if (sym != NULL)
6791 return sym;
6792
6793 /* Not right yet. FIXME pnh 7/20/2007. */
6794 sym = ada_find_any_symbol (name);
6795 if (sym != NULL && strstr (SYMBOL_LINKAGE_NAME (sym), "___XR") != NULL)
6796 return sym;
6797 else
6798 return NULL;
6799 }
6800
6801 static struct symbol *
6802 find_old_style_renaming_symbol (const char *name, struct block *block)
6803 {
6804 const struct symbol *function_sym = block_linkage_function (block);
6805 char *rename;
6806
6807 if (function_sym != NULL)
6808 {
6809 /* If the symbol is defined inside a function, NAME is not fully
6810 qualified. This means we need to prepend the function name
6811 as well as adding the ``___XR'' suffix to build the name of
6812 the associated renaming symbol. */
6813 char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
6814 /* Function names sometimes contain suffixes used
6815 for instance to qualify nested subprograms. When building
6816 the XR type name, we need to make sure that this suffix is
6817 not included. So do not include any suffix in the function
6818 name length below. */
6819 int function_name_len = ada_name_prefix_len (function_name);
6820 const int rename_len = function_name_len + 2 /* "__" */
6821 + strlen (name) + 6 /* "___XR\0" */ ;
6822
6823 /* Strip the suffix if necessary. */
6824 ada_remove_trailing_digits (function_name, &function_name_len);
6825 ada_remove_po_subprogram_suffix (function_name, &function_name_len);
6826 ada_remove_Xbn_suffix (function_name, &function_name_len);
6827
6828 /* Library-level functions are a special case, as GNAT adds
6829 a ``_ada_'' prefix to the function name to avoid namespace
6830 pollution. However, the renaming symbols themselves do not
6831 have this prefix, so we need to skip this prefix if present. */
6832 if (function_name_len > 5 /* "_ada_" */
6833 && strstr (function_name, "_ada_") == function_name)
6834 {
6835 function_name += 5;
6836 function_name_len -= 5;
6837 }
6838
6839 rename = (char *) alloca (rename_len * sizeof (char));
6840 strncpy (rename, function_name, function_name_len);
6841 xsnprintf (rename + function_name_len, rename_len - function_name_len,
6842 "__%s___XR", name);
6843 }
6844 else
6845 {
6846 const int rename_len = strlen (name) + 6;
6847
6848 rename = (char *) alloca (rename_len * sizeof (char));
6849 xsnprintf (rename, rename_len * sizeof (char), "%s___XR", name);
6850 }
6851
6852 return ada_find_any_symbol (rename);
6853 }
6854
6855 /* Because of GNAT encoding conventions, several GDB symbols may match a
6856 given type name. If the type denoted by TYPE0 is to be preferred to
6857 that of TYPE1 for purposes of type printing, return non-zero;
6858 otherwise return 0. */
6859
6860 int
6861 ada_prefer_type (struct type *type0, struct type *type1)
6862 {
6863 if (type1 == NULL)
6864 return 1;
6865 else if (type0 == NULL)
6866 return 0;
6867 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
6868 return 1;
6869 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
6870 return 0;
6871 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
6872 return 1;
6873 else if (ada_is_constrained_packed_array_type (type0))
6874 return 1;
6875 else if (ada_is_array_descriptor_type (type0)
6876 && !ada_is_array_descriptor_type (type1))
6877 return 1;
6878 else
6879 {
6880 const char *type0_name = type_name_no_tag (type0);
6881 const char *type1_name = type_name_no_tag (type1);
6882
6883 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
6884 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
6885 return 1;
6886 }
6887 return 0;
6888 }
6889
6890 /* The name of TYPE, which is either its TYPE_NAME, or, if that is
6891 null, its TYPE_TAG_NAME. Null if TYPE is null. */
6892
6893 char *
6894 ada_type_name (struct type *type)
6895 {
6896 if (type == NULL)
6897 return NULL;
6898 else if (TYPE_NAME (type) != NULL)
6899 return TYPE_NAME (type);
6900 else
6901 return TYPE_TAG_NAME (type);
6902 }
6903
6904 /* Search the list of "descriptive" types associated to TYPE for a type
6905 whose name is NAME. */
6906
6907 static struct type *
6908 find_parallel_type_by_descriptive_type (struct type *type, const char *name)
6909 {
6910 struct type *result;
6911
6912 /* If there no descriptive-type info, then there is no parallel type
6913 to be found. */
6914 if (!HAVE_GNAT_AUX_INFO (type))
6915 return NULL;
6916
6917 result = TYPE_DESCRIPTIVE_TYPE (type);
6918 while (result != NULL)
6919 {
6920 char *result_name = ada_type_name (result);
6921
6922 if (result_name == NULL)
6923 {
6924 warning (_("unexpected null name on descriptive type"));
6925 return NULL;
6926 }
6927
6928 /* If the names match, stop. */
6929 if (strcmp (result_name, name) == 0)
6930 break;
6931
6932 /* Otherwise, look at the next item on the list, if any. */
6933 if (HAVE_GNAT_AUX_INFO (result))
6934 result = TYPE_DESCRIPTIVE_TYPE (result);
6935 else
6936 result = NULL;
6937 }
6938
6939 /* If we didn't find a match, see whether this is a packed array. With
6940 older compilers, the descriptive type information is either absent or
6941 irrelevant when it comes to packed arrays so the above lookup fails.
6942 Fall back to using a parallel lookup by name in this case. */
6943 if (result == NULL && ada_is_constrained_packed_array_type (type))
6944 return ada_find_any_type (name);
6945
6946 return result;
6947 }
6948
6949 /* Find a parallel type to TYPE with the specified NAME, using the
6950 descriptive type taken from the debugging information, if available,
6951 and otherwise using the (slower) name-based method. */
6952
6953 static struct type *
6954 ada_find_parallel_type_with_name (struct type *type, const char *name)
6955 {
6956 struct type *result = NULL;
6957
6958 if (HAVE_GNAT_AUX_INFO (type))
6959 result = find_parallel_type_by_descriptive_type (type, name);
6960 else
6961 result = ada_find_any_type (name);
6962
6963 return result;
6964 }
6965
6966 /* Same as above, but specify the name of the parallel type by appending
6967 SUFFIX to the name of TYPE. */
6968
6969 struct type *
6970 ada_find_parallel_type (struct type *type, const char *suffix)
6971 {
6972 char *name, *typename = ada_type_name (type);
6973 int len;
6974
6975 if (typename == NULL)
6976 return NULL;
6977
6978 len = strlen (typename);
6979
6980 name = (char *) alloca (len + strlen (suffix) + 1);
6981
6982 strcpy (name, typename);
6983 strcpy (name + len, suffix);
6984
6985 return ada_find_parallel_type_with_name (type, name);
6986 }
6987
6988 /* If TYPE is a variable-size record type, return the corresponding template
6989 type describing its fields. Otherwise, return NULL. */
6990
6991 static struct type *
6992 dynamic_template_type (struct type *type)
6993 {
6994 type = ada_check_typedef (type);
6995
6996 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
6997 || ada_type_name (type) == NULL)
6998 return NULL;
6999 else
7000 {
7001 int len = strlen (ada_type_name (type));
7002
7003 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
7004 return type;
7005 else
7006 return ada_find_parallel_type (type, "___XVE");
7007 }
7008 }
7009
7010 /* Assuming that TEMPL_TYPE is a union or struct type, returns
7011 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
7012
7013 static int
7014 is_dynamic_field (struct type *templ_type, int field_num)
7015 {
7016 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
7017
7018 return name != NULL
7019 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
7020 && strstr (name, "___XVL") != NULL;
7021 }
7022
7023 /* The index of the variant field of TYPE, or -1 if TYPE does not
7024 represent a variant record type. */
7025
7026 static int
7027 variant_field_index (struct type *type)
7028 {
7029 int f;
7030
7031 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
7032 return -1;
7033
7034 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
7035 {
7036 if (ada_is_variant_part (type, f))
7037 return f;
7038 }
7039 return -1;
7040 }
7041
7042 /* A record type with no fields. */
7043
7044 static struct type *
7045 empty_record (struct type *template)
7046 {
7047 struct type *type = alloc_type_copy (template);
7048
7049 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7050 TYPE_NFIELDS (type) = 0;
7051 TYPE_FIELDS (type) = NULL;
7052 INIT_CPLUS_SPECIFIC (type);
7053 TYPE_NAME (type) = "<empty>";
7054 TYPE_TAG_NAME (type) = NULL;
7055 TYPE_LENGTH (type) = 0;
7056 return type;
7057 }
7058
7059 /* An ordinary record type (with fixed-length fields) that describes
7060 the value of type TYPE at VALADDR or ADDRESS (see comments at
7061 the beginning of this section) VAL according to GNAT conventions.
7062 DVAL0 should describe the (portion of a) record that contains any
7063 necessary discriminants. It should be NULL if value_type (VAL) is
7064 an outer-level type (i.e., as opposed to a branch of a variant.) A
7065 variant field (unless unchecked) is replaced by a particular branch
7066 of the variant.
7067
7068 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7069 length are not statically known are discarded. As a consequence,
7070 VALADDR, ADDRESS and DVAL0 are ignored.
7071
7072 NOTE: Limitations: For now, we assume that dynamic fields and
7073 variants occupy whole numbers of bytes. However, they need not be
7074 byte-aligned. */
7075
7076 struct type *
7077 ada_template_to_fixed_record_type_1 (struct type *type,
7078 const gdb_byte *valaddr,
7079 CORE_ADDR address, struct value *dval0,
7080 int keep_dynamic_fields)
7081 {
7082 struct value *mark = value_mark ();
7083 struct value *dval;
7084 struct type *rtype;
7085 int nfields, bit_len;
7086 int variant_field;
7087 long off;
7088 int fld_bit_len;
7089 int f;
7090
7091 /* Compute the number of fields in this record type that are going
7092 to be processed: unless keep_dynamic_fields, this includes only
7093 fields whose position and length are static will be processed. */
7094 if (keep_dynamic_fields)
7095 nfields = TYPE_NFIELDS (type);
7096 else
7097 {
7098 nfields = 0;
7099 while (nfields < TYPE_NFIELDS (type)
7100 && !ada_is_variant_part (type, nfields)
7101 && !is_dynamic_field (type, nfields))
7102 nfields++;
7103 }
7104
7105 rtype = alloc_type_copy (type);
7106 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7107 INIT_CPLUS_SPECIFIC (rtype);
7108 TYPE_NFIELDS (rtype) = nfields;
7109 TYPE_FIELDS (rtype) = (struct field *)
7110 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7111 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
7112 TYPE_NAME (rtype) = ada_type_name (type);
7113 TYPE_TAG_NAME (rtype) = NULL;
7114 TYPE_FIXED_INSTANCE (rtype) = 1;
7115
7116 off = 0;
7117 bit_len = 0;
7118 variant_field = -1;
7119
7120 for (f = 0; f < nfields; f += 1)
7121 {
7122 off = align_value (off, field_alignment (type, f))
7123 + TYPE_FIELD_BITPOS (type, f);
7124 TYPE_FIELD_BITPOS (rtype, f) = off;
7125 TYPE_FIELD_BITSIZE (rtype, f) = 0;
7126
7127 if (ada_is_variant_part (type, f))
7128 {
7129 variant_field = f;
7130 fld_bit_len = 0;
7131 }
7132 else if (is_dynamic_field (type, f))
7133 {
7134 const gdb_byte *field_valaddr = valaddr;
7135 CORE_ADDR field_address = address;
7136 struct type *field_type =
7137 TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f));
7138
7139 if (dval0 == NULL)
7140 {
7141 /* rtype's length is computed based on the run-time
7142 value of discriminants. If the discriminants are not
7143 initialized, the type size may be completely bogus and
7144 GDB may fail to allocate a value for it. So check the
7145 size first before creating the value. */
7146 check_size (rtype);
7147 dval = value_from_contents_and_address (rtype, valaddr, address);
7148 }
7149 else
7150 dval = dval0;
7151
7152 /* If the type referenced by this field is an aligner type, we need
7153 to unwrap that aligner type, because its size might not be set.
7154 Keeping the aligner type would cause us to compute the wrong
7155 size for this field, impacting the offset of the all the fields
7156 that follow this one. */
7157 if (ada_is_aligner_type (field_type))
7158 {
7159 long field_offset = TYPE_FIELD_BITPOS (field_type, f);
7160
7161 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7162 field_address = cond_offset_target (field_address, field_offset);
7163 field_type = ada_aligned_type (field_type);
7164 }
7165
7166 field_valaddr = cond_offset_host (field_valaddr,
7167 off / TARGET_CHAR_BIT);
7168 field_address = cond_offset_target (field_address,
7169 off / TARGET_CHAR_BIT);
7170
7171 /* Get the fixed type of the field. Note that, in this case,
7172 we do not want to get the real type out of the tag: if
7173 the current field is the parent part of a tagged record,
7174 we will get the tag of the object. Clearly wrong: the real
7175 type of the parent is not the real type of the child. We
7176 would end up in an infinite loop. */
7177 field_type = ada_get_base_type (field_type);
7178 field_type = ada_to_fixed_type (field_type, field_valaddr,
7179 field_address, dval, 0);
7180 /* If the field size is already larger than the maximum
7181 object size, then the record itself will necessarily
7182 be larger than the maximum object size. We need to make
7183 this check now, because the size might be so ridiculously
7184 large (due to an uninitialized variable in the inferior)
7185 that it would cause an overflow when adding it to the
7186 record size. */
7187 check_size (field_type);
7188
7189 TYPE_FIELD_TYPE (rtype, f) = field_type;
7190 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7191 /* The multiplication can potentially overflow. But because
7192 the field length has been size-checked just above, and
7193 assuming that the maximum size is a reasonable value,
7194 an overflow should not happen in practice. So rather than
7195 adding overflow recovery code to this already complex code,
7196 we just assume that it's not going to happen. */
7197 fld_bit_len =
7198 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
7199 }
7200 else
7201 {
7202 struct type *field_type = TYPE_FIELD_TYPE (type, f);
7203
7204 /* If our field is a typedef type (most likely a typedef of
7205 a fat pointer, encoding an array access), then we need to
7206 look at its target type to determine its characteristics.
7207 In particular, we would miscompute the field size if we took
7208 the size of the typedef (zero), instead of the size of
7209 the target type. */
7210 if (TYPE_CODE (field_type) == TYPE_CODE_TYPEDEF)
7211 field_type = ada_typedef_target_type (field_type);
7212
7213 TYPE_FIELD_TYPE (rtype, f) = field_type;
7214 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7215 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7216 fld_bit_len =
7217 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7218 else
7219 fld_bit_len =
7220 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
7221 }
7222 if (off + fld_bit_len > bit_len)
7223 bit_len = off + fld_bit_len;
7224 off += fld_bit_len;
7225 TYPE_LENGTH (rtype) =
7226 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7227 }
7228
7229 /* We handle the variant part, if any, at the end because of certain
7230 odd cases in which it is re-ordered so as NOT to be the last field of
7231 the record. This can happen in the presence of representation
7232 clauses. */
7233 if (variant_field >= 0)
7234 {
7235 struct type *branch_type;
7236
7237 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7238
7239 if (dval0 == NULL)
7240 dval = value_from_contents_and_address (rtype, valaddr, address);
7241 else
7242 dval = dval0;
7243
7244 branch_type =
7245 to_fixed_variant_branch_type
7246 (TYPE_FIELD_TYPE (type, variant_field),
7247 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7248 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
7249 if (branch_type == NULL)
7250 {
7251 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
7252 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7253 TYPE_NFIELDS (rtype) -= 1;
7254 }
7255 else
7256 {
7257 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7258 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7259 fld_bit_len =
7260 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
7261 TARGET_CHAR_BIT;
7262 if (off + fld_bit_len > bit_len)
7263 bit_len = off + fld_bit_len;
7264 TYPE_LENGTH (rtype) =
7265 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7266 }
7267 }
7268
7269 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7270 should contain the alignment of that record, which should be a strictly
7271 positive value. If null or negative, then something is wrong, most
7272 probably in the debug info. In that case, we don't round up the size
7273 of the resulting type. If this record is not part of another structure,
7274 the current RTYPE length might be good enough for our purposes. */
7275 if (TYPE_LENGTH (type) <= 0)
7276 {
7277 if (TYPE_NAME (rtype))
7278 warning (_("Invalid type size for `%s' detected: %d."),
7279 TYPE_NAME (rtype), TYPE_LENGTH (type));
7280 else
7281 warning (_("Invalid type size for <unnamed> detected: %d."),
7282 TYPE_LENGTH (type));
7283 }
7284 else
7285 {
7286 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype),
7287 TYPE_LENGTH (type));
7288 }
7289
7290 value_free_to_mark (mark);
7291 if (TYPE_LENGTH (rtype) > varsize_limit)
7292 error (_("record type with dynamic size is larger than varsize-limit"));
7293 return rtype;
7294 }
7295
7296 /* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7297 of 1. */
7298
7299 static struct type *
7300 template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
7301 CORE_ADDR address, struct value *dval0)
7302 {
7303 return ada_template_to_fixed_record_type_1 (type, valaddr,
7304 address, dval0, 1);
7305 }
7306
7307 /* An ordinary record type in which ___XVL-convention fields and
7308 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7309 static approximations, containing all possible fields. Uses
7310 no runtime values. Useless for use in values, but that's OK,
7311 since the results are used only for type determinations. Works on both
7312 structs and unions. Representation note: to save space, we memorize
7313 the result of this function in the TYPE_TARGET_TYPE of the
7314 template type. */
7315
7316 static struct type *
7317 template_to_static_fixed_type (struct type *type0)
7318 {
7319 struct type *type;
7320 int nfields;
7321 int f;
7322
7323 if (TYPE_TARGET_TYPE (type0) != NULL)
7324 return TYPE_TARGET_TYPE (type0);
7325
7326 nfields = TYPE_NFIELDS (type0);
7327 type = type0;
7328
7329 for (f = 0; f < nfields; f += 1)
7330 {
7331 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type0, f));
7332 struct type *new_type;
7333
7334 if (is_dynamic_field (type0, f))
7335 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
7336 else
7337 new_type = static_unwrap_type (field_type);
7338 if (type == type0 && new_type != field_type)
7339 {
7340 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
7341 TYPE_CODE (type) = TYPE_CODE (type0);
7342 INIT_CPLUS_SPECIFIC (type);
7343 TYPE_NFIELDS (type) = nfields;
7344 TYPE_FIELDS (type) = (struct field *)
7345 TYPE_ALLOC (type, nfields * sizeof (struct field));
7346 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
7347 sizeof (struct field) * nfields);
7348 TYPE_NAME (type) = ada_type_name (type0);
7349 TYPE_TAG_NAME (type) = NULL;
7350 TYPE_FIXED_INSTANCE (type) = 1;
7351 TYPE_LENGTH (type) = 0;
7352 }
7353 TYPE_FIELD_TYPE (type, f) = new_type;
7354 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
7355 }
7356 return type;
7357 }
7358
7359 /* Given an object of type TYPE whose contents are at VALADDR and
7360 whose address in memory is ADDRESS, returns a revision of TYPE,
7361 which should be a non-dynamic-sized record, in which the variant
7362 part, if any, is replaced with the appropriate branch. Looks
7363 for discriminant values in DVAL0, which can be NULL if the record
7364 contains the necessary discriminant values. */
7365
7366 static struct type *
7367 to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
7368 CORE_ADDR address, struct value *dval0)
7369 {
7370 struct value *mark = value_mark ();
7371 struct value *dval;
7372 struct type *rtype;
7373 struct type *branch_type;
7374 int nfields = TYPE_NFIELDS (type);
7375 int variant_field = variant_field_index (type);
7376
7377 if (variant_field == -1)
7378 return type;
7379
7380 if (dval0 == NULL)
7381 dval = value_from_contents_and_address (type, valaddr, address);
7382 else
7383 dval = dval0;
7384
7385 rtype = alloc_type_copy (type);
7386 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7387 INIT_CPLUS_SPECIFIC (rtype);
7388 TYPE_NFIELDS (rtype) = nfields;
7389 TYPE_FIELDS (rtype) =
7390 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7391 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
7392 sizeof (struct field) * nfields);
7393 TYPE_NAME (rtype) = ada_type_name (type);
7394 TYPE_TAG_NAME (rtype) = NULL;
7395 TYPE_FIXED_INSTANCE (rtype) = 1;
7396 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
7397
7398 branch_type = to_fixed_variant_branch_type
7399 (TYPE_FIELD_TYPE (type, variant_field),
7400 cond_offset_host (valaddr,
7401 TYPE_FIELD_BITPOS (type, variant_field)
7402 / TARGET_CHAR_BIT),
7403 cond_offset_target (address,
7404 TYPE_FIELD_BITPOS (type, variant_field)
7405 / TARGET_CHAR_BIT), dval);
7406 if (branch_type == NULL)
7407 {
7408 int f;
7409
7410 for (f = variant_field + 1; f < nfields; f += 1)
7411 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7412 TYPE_NFIELDS (rtype) -= 1;
7413 }
7414 else
7415 {
7416 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7417 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7418 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
7419 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
7420 }
7421 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
7422
7423 value_free_to_mark (mark);
7424 return rtype;
7425 }
7426
7427 /* An ordinary record type (with fixed-length fields) that describes
7428 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
7429 beginning of this section]. Any necessary discriminants' values
7430 should be in DVAL, a record value; it may be NULL if the object
7431 at ADDR itself contains any necessary discriminant values.
7432 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
7433 values from the record are needed. Except in the case that DVAL,
7434 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
7435 unchecked) is replaced by a particular branch of the variant.
7436
7437 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
7438 is questionable and may be removed. It can arise during the
7439 processing of an unconstrained-array-of-record type where all the
7440 variant branches have exactly the same size. This is because in
7441 such cases, the compiler does not bother to use the XVS convention
7442 when encoding the record. I am currently dubious of this
7443 shortcut and suspect the compiler should be altered. FIXME. */
7444
7445 static struct type *
7446 to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
7447 CORE_ADDR address, struct value *dval)
7448 {
7449 struct type *templ_type;
7450
7451 if (TYPE_FIXED_INSTANCE (type0))
7452 return type0;
7453
7454 templ_type = dynamic_template_type (type0);
7455
7456 if (templ_type != NULL)
7457 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
7458 else if (variant_field_index (type0) >= 0)
7459 {
7460 if (dval == NULL && valaddr == NULL && address == 0)
7461 return type0;
7462 return to_record_with_fixed_variant_part (type0, valaddr, address,
7463 dval);
7464 }
7465 else
7466 {
7467 TYPE_FIXED_INSTANCE (type0) = 1;
7468 return type0;
7469 }
7470
7471 }
7472
7473 /* An ordinary record type (with fixed-length fields) that describes
7474 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
7475 union type. Any necessary discriminants' values should be in DVAL,
7476 a record value. That is, this routine selects the appropriate
7477 branch of the union at ADDR according to the discriminant value
7478 indicated in the union's type name. Returns VAR_TYPE0 itself if
7479 it represents a variant subject to a pragma Unchecked_Union. */
7480
7481 static struct type *
7482 to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
7483 CORE_ADDR address, struct value *dval)
7484 {
7485 int which;
7486 struct type *templ_type;
7487 struct type *var_type;
7488
7489 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
7490 var_type = TYPE_TARGET_TYPE (var_type0);
7491 else
7492 var_type = var_type0;
7493
7494 templ_type = ada_find_parallel_type (var_type, "___XVU");
7495
7496 if (templ_type != NULL)
7497 var_type = templ_type;
7498
7499 if (is_unchecked_variant (var_type, value_type (dval)))
7500 return var_type0;
7501 which =
7502 ada_which_variant_applies (var_type,
7503 value_type (dval), value_contents (dval));
7504
7505 if (which < 0)
7506 return empty_record (var_type);
7507 else if (is_dynamic_field (var_type, which))
7508 return to_fixed_record_type
7509 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
7510 valaddr, address, dval);
7511 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
7512 return
7513 to_fixed_record_type
7514 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
7515 else
7516 return TYPE_FIELD_TYPE (var_type, which);
7517 }
7518
7519 /* Assuming that TYPE0 is an array type describing the type of a value
7520 at ADDR, and that DVAL describes a record containing any
7521 discriminants used in TYPE0, returns a type for the value that
7522 contains no dynamic components (that is, no components whose sizes
7523 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
7524 true, gives an error message if the resulting type's size is over
7525 varsize_limit. */
7526
7527 static struct type *
7528 to_fixed_array_type (struct type *type0, struct value *dval,
7529 int ignore_too_big)
7530 {
7531 struct type *index_type_desc;
7532 struct type *result;
7533 int constrained_packed_array_p;
7534
7535 if (TYPE_FIXED_INSTANCE (type0))
7536 return type0;
7537
7538 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
7539 if (constrained_packed_array_p)
7540 type0 = decode_constrained_packed_array_type (type0);
7541
7542 index_type_desc = ada_find_parallel_type (type0, "___XA");
7543 ada_fixup_array_indexes_type (index_type_desc);
7544 if (index_type_desc == NULL)
7545 {
7546 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
7547
7548 /* NOTE: elt_type---the fixed version of elt_type0---should never
7549 depend on the contents of the array in properly constructed
7550 debugging data. */
7551 /* Create a fixed version of the array element type.
7552 We're not providing the address of an element here,
7553 and thus the actual object value cannot be inspected to do
7554 the conversion. This should not be a problem, since arrays of
7555 unconstrained objects are not allowed. In particular, all
7556 the elements of an array of a tagged type should all be of
7557 the same type specified in the debugging info. No need to
7558 consult the object tag. */
7559 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
7560
7561 /* Make sure we always create a new array type when dealing with
7562 packed array types, since we're going to fix-up the array
7563 type length and element bitsize a little further down. */
7564 if (elt_type0 == elt_type && !constrained_packed_array_p)
7565 result = type0;
7566 else
7567 result = create_array_type (alloc_type_copy (type0),
7568 elt_type, TYPE_INDEX_TYPE (type0));
7569 }
7570 else
7571 {
7572 int i;
7573 struct type *elt_type0;
7574
7575 elt_type0 = type0;
7576 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
7577 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
7578
7579 /* NOTE: result---the fixed version of elt_type0---should never
7580 depend on the contents of the array in properly constructed
7581 debugging data. */
7582 /* Create a fixed version of the array element type.
7583 We're not providing the address of an element here,
7584 and thus the actual object value cannot be inspected to do
7585 the conversion. This should not be a problem, since arrays of
7586 unconstrained objects are not allowed. In particular, all
7587 the elements of an array of a tagged type should all be of
7588 the same type specified in the debugging info. No need to
7589 consult the object tag. */
7590 result =
7591 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
7592
7593 elt_type0 = type0;
7594 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
7595 {
7596 struct type *range_type =
7597 to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, i), dval);
7598
7599 result = create_array_type (alloc_type_copy (elt_type0),
7600 result, range_type);
7601 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
7602 }
7603 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
7604 error (_("array type with dynamic size is larger than varsize-limit"));
7605 }
7606
7607 if (constrained_packed_array_p)
7608 {
7609 /* So far, the resulting type has been created as if the original
7610 type was a regular (non-packed) array type. As a result, the
7611 bitsize of the array elements needs to be set again, and the array
7612 length needs to be recomputed based on that bitsize. */
7613 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
7614 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
7615
7616 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
7617 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
7618 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
7619 TYPE_LENGTH (result)++;
7620 }
7621
7622 TYPE_FIXED_INSTANCE (result) = 1;
7623 return result;
7624 }
7625
7626
7627 /* A standard type (containing no dynamically sized components)
7628 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
7629 DVAL describes a record containing any discriminants used in TYPE0,
7630 and may be NULL if there are none, or if the object of type TYPE at
7631 ADDRESS or in VALADDR contains these discriminants.
7632
7633 If CHECK_TAG is not null, in the case of tagged types, this function
7634 attempts to locate the object's tag and use it to compute the actual
7635 type. However, when ADDRESS is null, we cannot use it to determine the
7636 location of the tag, and therefore compute the tagged type's actual type.
7637 So we return the tagged type without consulting the tag. */
7638
7639 static struct type *
7640 ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
7641 CORE_ADDR address, struct value *dval, int check_tag)
7642 {
7643 type = ada_check_typedef (type);
7644 switch (TYPE_CODE (type))
7645 {
7646 default:
7647 return type;
7648 case TYPE_CODE_STRUCT:
7649 {
7650 struct type *static_type = to_static_fixed_type (type);
7651 struct type *fixed_record_type =
7652 to_fixed_record_type (type, valaddr, address, NULL);
7653
7654 /* If STATIC_TYPE is a tagged type and we know the object's address,
7655 then we can determine its tag, and compute the object's actual
7656 type from there. Note that we have to use the fixed record
7657 type (the parent part of the record may have dynamic fields
7658 and the way the location of _tag is expressed may depend on
7659 them). */
7660
7661 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
7662 {
7663 struct type *real_type =
7664 type_from_tag (value_tag_from_contents_and_address
7665 (fixed_record_type,
7666 valaddr,
7667 address));
7668
7669 if (real_type != NULL)
7670 return to_fixed_record_type (real_type, valaddr, address, NULL);
7671 }
7672
7673 /* Check to see if there is a parallel ___XVZ variable.
7674 If there is, then it provides the actual size of our type. */
7675 else if (ada_type_name (fixed_record_type) != NULL)
7676 {
7677 char *name = ada_type_name (fixed_record_type);
7678 char *xvz_name = alloca (strlen (name) + 7 /* "___XVZ\0" */);
7679 int xvz_found = 0;
7680 LONGEST size;
7681
7682 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
7683 size = get_int_var_value (xvz_name, &xvz_found);
7684 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
7685 {
7686 fixed_record_type = copy_type (fixed_record_type);
7687 TYPE_LENGTH (fixed_record_type) = size;
7688
7689 /* The FIXED_RECORD_TYPE may have be a stub. We have
7690 observed this when the debugging info is STABS, and
7691 apparently it is something that is hard to fix.
7692
7693 In practice, we don't need the actual type definition
7694 at all, because the presence of the XVZ variable allows us
7695 to assume that there must be a XVS type as well, which we
7696 should be able to use later, when we need the actual type
7697 definition.
7698
7699 In the meantime, pretend that the "fixed" type we are
7700 returning is NOT a stub, because this can cause trouble
7701 when using this type to create new types targeting it.
7702 Indeed, the associated creation routines often check
7703 whether the target type is a stub and will try to replace
7704 it, thus using a type with the wrong size. This, in turn,
7705 might cause the new type to have the wrong size too.
7706 Consider the case of an array, for instance, where the size
7707 of the array is computed from the number of elements in
7708 our array multiplied by the size of its element. */
7709 TYPE_STUB (fixed_record_type) = 0;
7710 }
7711 }
7712 return fixed_record_type;
7713 }
7714 case TYPE_CODE_ARRAY:
7715 return to_fixed_array_type (type, dval, 1);
7716 case TYPE_CODE_UNION:
7717 if (dval == NULL)
7718 return type;
7719 else
7720 return to_fixed_variant_branch_type (type, valaddr, address, dval);
7721 }
7722 }
7723
7724 /* The same as ada_to_fixed_type_1, except that it preserves the type
7725 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
7726
7727 The typedef layer needs be preserved in order to differentiate between
7728 arrays and array pointers when both types are implemented using the same
7729 fat pointer. In the array pointer case, the pointer is encoded as
7730 a typedef of the pointer type. For instance, considering:
7731
7732 type String_Access is access String;
7733 S1 : String_Access := null;
7734
7735 To the debugger, S1 is defined as a typedef of type String. But
7736 to the user, it is a pointer. So if the user tries to print S1,
7737 we should not dereference the array, but print the array address
7738 instead.
7739
7740 If we didn't preserve the typedef layer, we would lose the fact that
7741 the type is to be presented as a pointer (needs de-reference before
7742 being printed). And we would also use the source-level type name. */
7743
7744 struct type *
7745 ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
7746 CORE_ADDR address, struct value *dval, int check_tag)
7747
7748 {
7749 struct type *fixed_type =
7750 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
7751
7752 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
7753 then preserve the typedef layer.
7754
7755 Implementation note: We can only check the main-type portion of
7756 the TYPE and FIXED_TYPE, because eliminating the typedef layer
7757 from TYPE now returns a type that has the same instance flags
7758 as TYPE. For instance, if TYPE is a "typedef const", and its
7759 target type is a "struct", then the typedef elimination will return
7760 a "const" version of the target type. See check_typedef for more
7761 details about how the typedef layer elimination is done.
7762
7763 brobecker/2010-11-19: It seems to me that the only case where it is
7764 useful to preserve the typedef layer is when dealing with fat pointers.
7765 Perhaps, we could add a check for that and preserve the typedef layer
7766 only in that situation. But this seems unecessary so far, probably
7767 because we call check_typedef/ada_check_typedef pretty much everywhere.
7768 */
7769 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
7770 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
7771 == TYPE_MAIN_TYPE (fixed_type)))
7772 return type;
7773
7774 return fixed_type;
7775 }
7776
7777 /* A standard (static-sized) type corresponding as well as possible to
7778 TYPE0, but based on no runtime data. */
7779
7780 static struct type *
7781 to_static_fixed_type (struct type *type0)
7782 {
7783 struct type *type;
7784
7785 if (type0 == NULL)
7786 return NULL;
7787
7788 if (TYPE_FIXED_INSTANCE (type0))
7789 return type0;
7790
7791 type0 = ada_check_typedef (type0);
7792
7793 switch (TYPE_CODE (type0))
7794 {
7795 default:
7796 return type0;
7797 case TYPE_CODE_STRUCT:
7798 type = dynamic_template_type (type0);
7799 if (type != NULL)
7800 return template_to_static_fixed_type (type);
7801 else
7802 return template_to_static_fixed_type (type0);
7803 case TYPE_CODE_UNION:
7804 type = ada_find_parallel_type (type0, "___XVU");
7805 if (type != NULL)
7806 return template_to_static_fixed_type (type);
7807 else
7808 return template_to_static_fixed_type (type0);
7809 }
7810 }
7811
7812 /* A static approximation of TYPE with all type wrappers removed. */
7813
7814 static struct type *
7815 static_unwrap_type (struct type *type)
7816 {
7817 if (ada_is_aligner_type (type))
7818 {
7819 struct type *type1 = TYPE_FIELD_TYPE (ada_check_typedef (type), 0);
7820 if (ada_type_name (type1) == NULL)
7821 TYPE_NAME (type1) = ada_type_name (type);
7822
7823 return static_unwrap_type (type1);
7824 }
7825 else
7826 {
7827 struct type *raw_real_type = ada_get_base_type (type);
7828
7829 if (raw_real_type == type)
7830 return type;
7831 else
7832 return to_static_fixed_type (raw_real_type);
7833 }
7834 }
7835
7836 /* In some cases, incomplete and private types require
7837 cross-references that are not resolved as records (for example,
7838 type Foo;
7839 type FooP is access Foo;
7840 V: FooP;
7841 type Foo is array ...;
7842 ). In these cases, since there is no mechanism for producing
7843 cross-references to such types, we instead substitute for FooP a
7844 stub enumeration type that is nowhere resolved, and whose tag is
7845 the name of the actual type. Call these types "non-record stubs". */
7846
7847 /* A type equivalent to TYPE that is not a non-record stub, if one
7848 exists, otherwise TYPE. */
7849
7850 struct type *
7851 ada_check_typedef (struct type *type)
7852 {
7853 if (type == NULL)
7854 return NULL;
7855
7856 /* If our type is a typedef type of a fat pointer, then we're done.
7857 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
7858 what allows us to distinguish between fat pointers that represent
7859 array types, and fat pointers that represent array access types
7860 (in both cases, the compiler implements them as fat pointers). */
7861 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
7862 && is_thick_pntr (ada_typedef_target_type (type)))
7863 return type;
7864
7865 CHECK_TYPEDEF (type);
7866 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
7867 || !TYPE_STUB (type)
7868 || TYPE_TAG_NAME (type) == NULL)
7869 return type;
7870 else
7871 {
7872 char *name = TYPE_TAG_NAME (type);
7873 struct type *type1 = ada_find_any_type (name);
7874
7875 if (type1 == NULL)
7876 return type;
7877
7878 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
7879 stubs pointing to arrays, as we don't create symbols for array
7880 types, only for the typedef-to-array types). If that's the case,
7881 strip the typedef layer. */
7882 if (TYPE_CODE (type1) == TYPE_CODE_TYPEDEF)
7883 type1 = ada_check_typedef (type1);
7884
7885 return type1;
7886 }
7887 }
7888
7889 /* A value representing the data at VALADDR/ADDRESS as described by
7890 type TYPE0, but with a standard (static-sized) type that correctly
7891 describes it. If VAL0 is not NULL and TYPE0 already is a standard
7892 type, then return VAL0 [this feature is simply to avoid redundant
7893 creation of struct values]. */
7894
7895 static struct value *
7896 ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
7897 struct value *val0)
7898 {
7899 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
7900
7901 if (type == type0 && val0 != NULL)
7902 return val0;
7903 else
7904 return value_from_contents_and_address (type, 0, address);
7905 }
7906
7907 /* A value representing VAL, but with a standard (static-sized) type
7908 that correctly describes it. Does not necessarily create a new
7909 value. */
7910
7911 struct value *
7912 ada_to_fixed_value (struct value *val)
7913 {
7914 return ada_to_fixed_value_create (value_type (val),
7915 value_address (val),
7916 val);
7917 }
7918 \f
7919
7920 /* Attributes */
7921
7922 /* Table mapping attribute numbers to names.
7923 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
7924
7925 static const char *attribute_names[] = {
7926 "<?>",
7927
7928 "first",
7929 "last",
7930 "length",
7931 "image",
7932 "max",
7933 "min",
7934 "modulus",
7935 "pos",
7936 "size",
7937 "tag",
7938 "val",
7939 0
7940 };
7941
7942 const char *
7943 ada_attribute_name (enum exp_opcode n)
7944 {
7945 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
7946 return attribute_names[n - OP_ATR_FIRST + 1];
7947 else
7948 return attribute_names[0];
7949 }
7950
7951 /* Evaluate the 'POS attribute applied to ARG. */
7952
7953 static LONGEST
7954 pos_atr (struct value *arg)
7955 {
7956 struct value *val = coerce_ref (arg);
7957 struct type *type = value_type (val);
7958
7959 if (!discrete_type_p (type))
7960 error (_("'POS only defined on discrete types"));
7961
7962 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7963 {
7964 int i;
7965 LONGEST v = value_as_long (val);
7966
7967 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
7968 {
7969 if (v == TYPE_FIELD_BITPOS (type, i))
7970 return i;
7971 }
7972 error (_("enumeration value is invalid: can't find 'POS"));
7973 }
7974 else
7975 return value_as_long (val);
7976 }
7977
7978 static struct value *
7979 value_pos_atr (struct type *type, struct value *arg)
7980 {
7981 return value_from_longest (type, pos_atr (arg));
7982 }
7983
7984 /* Evaluate the TYPE'VAL attribute applied to ARG. */
7985
7986 static struct value *
7987 value_val_atr (struct type *type, struct value *arg)
7988 {
7989 if (!discrete_type_p (type))
7990 error (_("'VAL only defined on discrete types"));
7991 if (!integer_type_p (value_type (arg)))
7992 error (_("'VAL requires integral argument"));
7993
7994 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7995 {
7996 long pos = value_as_long (arg);
7997
7998 if (pos < 0 || pos >= TYPE_NFIELDS (type))
7999 error (_("argument to 'VAL out of range"));
8000 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
8001 }
8002 else
8003 return value_from_longest (type, value_as_long (arg));
8004 }
8005 \f
8006
8007 /* Evaluation */
8008
8009 /* True if TYPE appears to be an Ada character type.
8010 [At the moment, this is true only for Character and Wide_Character;
8011 It is a heuristic test that could stand improvement]. */
8012
8013 int
8014 ada_is_character_type (struct type *type)
8015 {
8016 const char *name;
8017
8018 /* If the type code says it's a character, then assume it really is,
8019 and don't check any further. */
8020 if (TYPE_CODE (type) == TYPE_CODE_CHAR)
8021 return 1;
8022
8023 /* Otherwise, assume it's a character type iff it is a discrete type
8024 with a known character type name. */
8025 name = ada_type_name (type);
8026 return (name != NULL
8027 && (TYPE_CODE (type) == TYPE_CODE_INT
8028 || TYPE_CODE (type) == TYPE_CODE_RANGE)
8029 && (strcmp (name, "character") == 0
8030 || strcmp (name, "wide_character") == 0
8031 || strcmp (name, "wide_wide_character") == 0
8032 || strcmp (name, "unsigned char") == 0));
8033 }
8034
8035 /* True if TYPE appears to be an Ada string type. */
8036
8037 int
8038 ada_is_string_type (struct type *type)
8039 {
8040 type = ada_check_typedef (type);
8041 if (type != NULL
8042 && TYPE_CODE (type) != TYPE_CODE_PTR
8043 && (ada_is_simple_array_type (type)
8044 || ada_is_array_descriptor_type (type))
8045 && ada_array_arity (type) == 1)
8046 {
8047 struct type *elttype = ada_array_element_type (type, 1);
8048
8049 return ada_is_character_type (elttype);
8050 }
8051 else
8052 return 0;
8053 }
8054
8055 /* The compiler sometimes provides a parallel XVS type for a given
8056 PAD type. Normally, it is safe to follow the PAD type directly,
8057 but older versions of the compiler have a bug that causes the offset
8058 of its "F" field to be wrong. Following that field in that case
8059 would lead to incorrect results, but this can be worked around
8060 by ignoring the PAD type and using the associated XVS type instead.
8061
8062 Set to True if the debugger should trust the contents of PAD types.
8063 Otherwise, ignore the PAD type if there is a parallel XVS type. */
8064 static int trust_pad_over_xvs = 1;
8065
8066 /* True if TYPE is a struct type introduced by the compiler to force the
8067 alignment of a value. Such types have a single field with a
8068 distinctive name. */
8069
8070 int
8071 ada_is_aligner_type (struct type *type)
8072 {
8073 type = ada_check_typedef (type);
8074
8075 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
8076 return 0;
8077
8078 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
8079 && TYPE_NFIELDS (type) == 1
8080 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
8081 }
8082
8083 /* If there is an ___XVS-convention type parallel to SUBTYPE, return
8084 the parallel type. */
8085
8086 struct type *
8087 ada_get_base_type (struct type *raw_type)
8088 {
8089 struct type *real_type_namer;
8090 struct type *raw_real_type;
8091
8092 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
8093 return raw_type;
8094
8095 if (ada_is_aligner_type (raw_type))
8096 /* The encoding specifies that we should always use the aligner type.
8097 So, even if this aligner type has an associated XVS type, we should
8098 simply ignore it.
8099
8100 According to the compiler gurus, an XVS type parallel to an aligner
8101 type may exist because of a stabs limitation. In stabs, aligner
8102 types are empty because the field has a variable-sized type, and
8103 thus cannot actually be used as an aligner type. As a result,
8104 we need the associated parallel XVS type to decode the type.
8105 Since the policy in the compiler is to not change the internal
8106 representation based on the debugging info format, we sometimes
8107 end up having a redundant XVS type parallel to the aligner type. */
8108 return raw_type;
8109
8110 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
8111 if (real_type_namer == NULL
8112 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
8113 || TYPE_NFIELDS (real_type_namer) != 1)
8114 return raw_type;
8115
8116 if (TYPE_CODE (TYPE_FIELD_TYPE (real_type_namer, 0)) != TYPE_CODE_REF)
8117 {
8118 /* This is an older encoding form where the base type needs to be
8119 looked up by name. We prefer the newer enconding because it is
8120 more efficient. */
8121 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
8122 if (raw_real_type == NULL)
8123 return raw_type;
8124 else
8125 return raw_real_type;
8126 }
8127
8128 /* The field in our XVS type is a reference to the base type. */
8129 return TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (real_type_namer, 0));
8130 }
8131
8132 /* The type of value designated by TYPE, with all aligners removed. */
8133
8134 struct type *
8135 ada_aligned_type (struct type *type)
8136 {
8137 if (ada_is_aligner_type (type))
8138 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
8139 else
8140 return ada_get_base_type (type);
8141 }
8142
8143
8144 /* The address of the aligned value in an object at address VALADDR
8145 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
8146
8147 const gdb_byte *
8148 ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
8149 {
8150 if (ada_is_aligner_type (type))
8151 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
8152 valaddr +
8153 TYPE_FIELD_BITPOS (type,
8154 0) / TARGET_CHAR_BIT);
8155 else
8156 return valaddr;
8157 }
8158
8159
8160
8161 /* The printed representation of an enumeration literal with encoded
8162 name NAME. The value is good to the next call of ada_enum_name. */
8163 const char *
8164 ada_enum_name (const char *name)
8165 {
8166 static char *result;
8167 static size_t result_len = 0;
8168 char *tmp;
8169
8170 /* First, unqualify the enumeration name:
8171 1. Search for the last '.' character. If we find one, then skip
8172 all the preceeding characters, the unqualified name starts
8173 right after that dot.
8174 2. Otherwise, we may be debugging on a target where the compiler
8175 translates dots into "__". Search forward for double underscores,
8176 but stop searching when we hit an overloading suffix, which is
8177 of the form "__" followed by digits. */
8178
8179 tmp = strrchr (name, '.');
8180 if (tmp != NULL)
8181 name = tmp + 1;
8182 else
8183 {
8184 while ((tmp = strstr (name, "__")) != NULL)
8185 {
8186 if (isdigit (tmp[2]))
8187 break;
8188 else
8189 name = tmp + 2;
8190 }
8191 }
8192
8193 if (name[0] == 'Q')
8194 {
8195 int v;
8196
8197 if (name[1] == 'U' || name[1] == 'W')
8198 {
8199 if (sscanf (name + 2, "%x", &v) != 1)
8200 return name;
8201 }
8202 else
8203 return name;
8204
8205 GROW_VECT (result, result_len, 16);
8206 if (isascii (v) && isprint (v))
8207 xsnprintf (result, result_len, "'%c'", v);
8208 else if (name[1] == 'U')
8209 xsnprintf (result, result_len, "[\"%02x\"]", v);
8210 else
8211 xsnprintf (result, result_len, "[\"%04x\"]", v);
8212
8213 return result;
8214 }
8215 else
8216 {
8217 tmp = strstr (name, "__");
8218 if (tmp == NULL)
8219 tmp = strstr (name, "$");
8220 if (tmp != NULL)
8221 {
8222 GROW_VECT (result, result_len, tmp - name + 1);
8223 strncpy (result, name, tmp - name);
8224 result[tmp - name] = '\0';
8225 return result;
8226 }
8227
8228 return name;
8229 }
8230 }
8231
8232 /* Evaluate the subexpression of EXP starting at *POS as for
8233 evaluate_type, updating *POS to point just past the evaluated
8234 expression. */
8235
8236 static struct value *
8237 evaluate_subexp_type (struct expression *exp, int *pos)
8238 {
8239 return evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
8240 }
8241
8242 /* If VAL is wrapped in an aligner or subtype wrapper, return the
8243 value it wraps. */
8244
8245 static struct value *
8246 unwrap_value (struct value *val)
8247 {
8248 struct type *type = ada_check_typedef (value_type (val));
8249
8250 if (ada_is_aligner_type (type))
8251 {
8252 struct value *v = ada_value_struct_elt (val, "F", 0);
8253 struct type *val_type = ada_check_typedef (value_type (v));
8254
8255 if (ada_type_name (val_type) == NULL)
8256 TYPE_NAME (val_type) = ada_type_name (type);
8257
8258 return unwrap_value (v);
8259 }
8260 else
8261 {
8262 struct type *raw_real_type =
8263 ada_check_typedef (ada_get_base_type (type));
8264
8265 /* If there is no parallel XVS or XVE type, then the value is
8266 already unwrapped. Return it without further modification. */
8267 if ((type == raw_real_type)
8268 && ada_find_parallel_type (type, "___XVE") == NULL)
8269 return val;
8270
8271 return
8272 coerce_unspec_val_to_type
8273 (val, ada_to_fixed_type (raw_real_type, 0,
8274 value_address (val),
8275 NULL, 1));
8276 }
8277 }
8278
8279 static struct value *
8280 cast_to_fixed (struct type *type, struct value *arg)
8281 {
8282 LONGEST val;
8283
8284 if (type == value_type (arg))
8285 return arg;
8286 else if (ada_is_fixed_point_type (value_type (arg)))
8287 val = ada_float_to_fixed (type,
8288 ada_fixed_to_float (value_type (arg),
8289 value_as_long (arg)));
8290 else
8291 {
8292 DOUBLEST argd = value_as_double (arg);
8293
8294 val = ada_float_to_fixed (type, argd);
8295 }
8296
8297 return value_from_longest (type, val);
8298 }
8299
8300 static struct value *
8301 cast_from_fixed (struct type *type, struct value *arg)
8302 {
8303 DOUBLEST val = ada_fixed_to_float (value_type (arg),
8304 value_as_long (arg));
8305
8306 return value_from_double (type, val);
8307 }
8308
8309 /* Coerce VAL as necessary for assignment to an lval of type TYPE, and
8310 return the converted value. */
8311
8312 static struct value *
8313 coerce_for_assign (struct type *type, struct value *val)
8314 {
8315 struct type *type2 = value_type (val);
8316
8317 if (type == type2)
8318 return val;
8319
8320 type2 = ada_check_typedef (type2);
8321 type = ada_check_typedef (type);
8322
8323 if (TYPE_CODE (type2) == TYPE_CODE_PTR
8324 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8325 {
8326 val = ada_value_ind (val);
8327 type2 = value_type (val);
8328 }
8329
8330 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
8331 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8332 {
8333 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
8334 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
8335 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
8336 error (_("Incompatible types in assignment"));
8337 deprecated_set_value_type (val, type);
8338 }
8339 return val;
8340 }
8341
8342 static struct value *
8343 ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8344 {
8345 struct value *val;
8346 struct type *type1, *type2;
8347 LONGEST v, v1, v2;
8348
8349 arg1 = coerce_ref (arg1);
8350 arg2 = coerce_ref (arg2);
8351 type1 = base_type (ada_check_typedef (value_type (arg1)));
8352 type2 = base_type (ada_check_typedef (value_type (arg2)));
8353
8354 if (TYPE_CODE (type1) != TYPE_CODE_INT
8355 || TYPE_CODE (type2) != TYPE_CODE_INT)
8356 return value_binop (arg1, arg2, op);
8357
8358 switch (op)
8359 {
8360 case BINOP_MOD:
8361 case BINOP_DIV:
8362 case BINOP_REM:
8363 break;
8364 default:
8365 return value_binop (arg1, arg2, op);
8366 }
8367
8368 v2 = value_as_long (arg2);
8369 if (v2 == 0)
8370 error (_("second operand of %s must not be zero."), op_string (op));
8371
8372 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
8373 return value_binop (arg1, arg2, op);
8374
8375 v1 = value_as_long (arg1);
8376 switch (op)
8377 {
8378 case BINOP_DIV:
8379 v = v1 / v2;
8380 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
8381 v += v > 0 ? -1 : 1;
8382 break;
8383 case BINOP_REM:
8384 v = v1 % v2;
8385 if (v * v1 < 0)
8386 v -= v2;
8387 break;
8388 default:
8389 /* Should not reach this point. */
8390 v = 0;
8391 }
8392
8393 val = allocate_value (type1);
8394 store_unsigned_integer (value_contents_raw (val),
8395 TYPE_LENGTH (value_type (val)),
8396 gdbarch_byte_order (get_type_arch (type1)), v);
8397 return val;
8398 }
8399
8400 static int
8401 ada_value_equal (struct value *arg1, struct value *arg2)
8402 {
8403 if (ada_is_direct_array_type (value_type (arg1))
8404 || ada_is_direct_array_type (value_type (arg2)))
8405 {
8406 /* Automatically dereference any array reference before
8407 we attempt to perform the comparison. */
8408 arg1 = ada_coerce_ref (arg1);
8409 arg2 = ada_coerce_ref (arg2);
8410
8411 arg1 = ada_coerce_to_simple_array (arg1);
8412 arg2 = ada_coerce_to_simple_array (arg2);
8413 if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
8414 || TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
8415 error (_("Attempt to compare array with non-array"));
8416 /* FIXME: The following works only for types whose
8417 representations use all bits (no padding or undefined bits)
8418 and do not have user-defined equality. */
8419 return
8420 TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
8421 && memcmp (value_contents (arg1), value_contents (arg2),
8422 TYPE_LENGTH (value_type (arg1))) == 0;
8423 }
8424 return value_equal (arg1, arg2);
8425 }
8426
8427 /* Total number of component associations in the aggregate starting at
8428 index PC in EXP. Assumes that index PC is the start of an
8429 OP_AGGREGATE. */
8430
8431 static int
8432 num_component_specs (struct expression *exp, int pc)
8433 {
8434 int n, m, i;
8435
8436 m = exp->elts[pc + 1].longconst;
8437 pc += 3;
8438 n = 0;
8439 for (i = 0; i < m; i += 1)
8440 {
8441 switch (exp->elts[pc].opcode)
8442 {
8443 default:
8444 n += 1;
8445 break;
8446 case OP_CHOICES:
8447 n += exp->elts[pc + 1].longconst;
8448 break;
8449 }
8450 ada_evaluate_subexp (NULL, exp, &pc, EVAL_SKIP);
8451 }
8452 return n;
8453 }
8454
8455 /* Assign the result of evaluating EXP starting at *POS to the INDEXth
8456 component of LHS (a simple array or a record), updating *POS past
8457 the expression, assuming that LHS is contained in CONTAINER. Does
8458 not modify the inferior's memory, nor does it modify LHS (unless
8459 LHS == CONTAINER). */
8460
8461 static void
8462 assign_component (struct value *container, struct value *lhs, LONGEST index,
8463 struct expression *exp, int *pos)
8464 {
8465 struct value *mark = value_mark ();
8466 struct value *elt;
8467
8468 if (TYPE_CODE (value_type (lhs)) == TYPE_CODE_ARRAY)
8469 {
8470 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
8471 struct value *index_val = value_from_longest (index_type, index);
8472
8473 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
8474 }
8475 else
8476 {
8477 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
8478 elt = ada_to_fixed_value (unwrap_value (elt));
8479 }
8480
8481 if (exp->elts[*pos].opcode == OP_AGGREGATE)
8482 assign_aggregate (container, elt, exp, pos, EVAL_NORMAL);
8483 else
8484 value_assign_to_component (container, elt,
8485 ada_evaluate_subexp (NULL, exp, pos,
8486 EVAL_NORMAL));
8487
8488 value_free_to_mark (mark);
8489 }
8490
8491 /* Assuming that LHS represents an lvalue having a record or array
8492 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
8493 of that aggregate's value to LHS, advancing *POS past the
8494 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
8495 lvalue containing LHS (possibly LHS itself). Does not modify
8496 the inferior's memory, nor does it modify the contents of
8497 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
8498
8499 static struct value *
8500 assign_aggregate (struct value *container,
8501 struct value *lhs, struct expression *exp,
8502 int *pos, enum noside noside)
8503 {
8504 struct type *lhs_type;
8505 int n = exp->elts[*pos+1].longconst;
8506 LONGEST low_index, high_index;
8507 int num_specs;
8508 LONGEST *indices;
8509 int max_indices, num_indices;
8510 int is_array_aggregate;
8511 int i;
8512
8513 *pos += 3;
8514 if (noside != EVAL_NORMAL)
8515 {
8516 int i;
8517
8518 for (i = 0; i < n; i += 1)
8519 ada_evaluate_subexp (NULL, exp, pos, noside);
8520 return container;
8521 }
8522
8523 container = ada_coerce_ref (container);
8524 if (ada_is_direct_array_type (value_type (container)))
8525 container = ada_coerce_to_simple_array (container);
8526 lhs = ada_coerce_ref (lhs);
8527 if (!deprecated_value_modifiable (lhs))
8528 error (_("Left operand of assignment is not a modifiable lvalue."));
8529
8530 lhs_type = value_type (lhs);
8531 if (ada_is_direct_array_type (lhs_type))
8532 {
8533 lhs = ada_coerce_to_simple_array (lhs);
8534 lhs_type = value_type (lhs);
8535 low_index = TYPE_ARRAY_LOWER_BOUND_VALUE (lhs_type);
8536 high_index = TYPE_ARRAY_UPPER_BOUND_VALUE (lhs_type);
8537 is_array_aggregate = 1;
8538 }
8539 else if (TYPE_CODE (lhs_type) == TYPE_CODE_STRUCT)
8540 {
8541 low_index = 0;
8542 high_index = num_visible_fields (lhs_type) - 1;
8543 is_array_aggregate = 0;
8544 }
8545 else
8546 error (_("Left-hand side must be array or record."));
8547
8548 num_specs = num_component_specs (exp, *pos - 3);
8549 max_indices = 4 * num_specs + 4;
8550 indices = alloca (max_indices * sizeof (indices[0]));
8551 indices[0] = indices[1] = low_index - 1;
8552 indices[2] = indices[3] = high_index + 1;
8553 num_indices = 4;
8554
8555 for (i = 0; i < n; i += 1)
8556 {
8557 switch (exp->elts[*pos].opcode)
8558 {
8559 case OP_CHOICES:
8560 aggregate_assign_from_choices (container, lhs, exp, pos, indices,
8561 &num_indices, max_indices,
8562 low_index, high_index);
8563 break;
8564 case OP_POSITIONAL:
8565 aggregate_assign_positional (container, lhs, exp, pos, indices,
8566 &num_indices, max_indices,
8567 low_index, high_index);
8568 break;
8569 case OP_OTHERS:
8570 if (i != n-1)
8571 error (_("Misplaced 'others' clause"));
8572 aggregate_assign_others (container, lhs, exp, pos, indices,
8573 num_indices, low_index, high_index);
8574 break;
8575 default:
8576 error (_("Internal error: bad aggregate clause"));
8577 }
8578 }
8579
8580 return container;
8581 }
8582
8583 /* Assign into the component of LHS indexed by the OP_POSITIONAL
8584 construct at *POS, updating *POS past the construct, given that
8585 the positions are relative to lower bound LOW, where HIGH is the
8586 upper bound. Record the position in INDICES[0 .. MAX_INDICES-1]
8587 updating *NUM_INDICES as needed. CONTAINER is as for
8588 assign_aggregate. */
8589 static void
8590 aggregate_assign_positional (struct value *container,
8591 struct value *lhs, struct expression *exp,
8592 int *pos, LONGEST *indices, int *num_indices,
8593 int max_indices, LONGEST low, LONGEST high)
8594 {
8595 LONGEST ind = longest_to_int (exp->elts[*pos + 1].longconst) + low;
8596
8597 if (ind - 1 == high)
8598 warning (_("Extra components in aggregate ignored."));
8599 if (ind <= high)
8600 {
8601 add_component_interval (ind, ind, indices, num_indices, max_indices);
8602 *pos += 3;
8603 assign_component (container, lhs, ind, exp, pos);
8604 }
8605 else
8606 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8607 }
8608
8609 /* Assign into the components of LHS indexed by the OP_CHOICES
8610 construct at *POS, updating *POS past the construct, given that
8611 the allowable indices are LOW..HIGH. Record the indices assigned
8612 to in INDICES[0 .. MAX_INDICES-1], updating *NUM_INDICES as
8613 needed. CONTAINER is as for assign_aggregate. */
8614 static void
8615 aggregate_assign_from_choices (struct value *container,
8616 struct value *lhs, struct expression *exp,
8617 int *pos, LONGEST *indices, int *num_indices,
8618 int max_indices, LONGEST low, LONGEST high)
8619 {
8620 int j;
8621 int n_choices = longest_to_int (exp->elts[*pos+1].longconst);
8622 int choice_pos, expr_pc;
8623 int is_array = ada_is_direct_array_type (value_type (lhs));
8624
8625 choice_pos = *pos += 3;
8626
8627 for (j = 0; j < n_choices; j += 1)
8628 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8629 expr_pc = *pos;
8630 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8631
8632 for (j = 0; j < n_choices; j += 1)
8633 {
8634 LONGEST lower, upper;
8635 enum exp_opcode op = exp->elts[choice_pos].opcode;
8636
8637 if (op == OP_DISCRETE_RANGE)
8638 {
8639 choice_pos += 1;
8640 lower = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8641 EVAL_NORMAL));
8642 upper = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8643 EVAL_NORMAL));
8644 }
8645 else if (is_array)
8646 {
8647 lower = value_as_long (ada_evaluate_subexp (NULL, exp, &choice_pos,
8648 EVAL_NORMAL));
8649 upper = lower;
8650 }
8651 else
8652 {
8653 int ind;
8654 char *name;
8655
8656 switch (op)
8657 {
8658 case OP_NAME:
8659 name = &exp->elts[choice_pos + 2].string;
8660 break;
8661 case OP_VAR_VALUE:
8662 name = SYMBOL_NATURAL_NAME (exp->elts[choice_pos + 2].symbol);
8663 break;
8664 default:
8665 error (_("Invalid record component association."));
8666 }
8667 ada_evaluate_subexp (NULL, exp, &choice_pos, EVAL_SKIP);
8668 ind = 0;
8669 if (! find_struct_field (name, value_type (lhs), 0,
8670 NULL, NULL, NULL, NULL, &ind))
8671 error (_("Unknown component name: %s."), name);
8672 lower = upper = ind;
8673 }
8674
8675 if (lower <= upper && (lower < low || upper > high))
8676 error (_("Index in component association out of bounds."));
8677
8678 add_component_interval (lower, upper, indices, num_indices,
8679 max_indices);
8680 while (lower <= upper)
8681 {
8682 int pos1;
8683
8684 pos1 = expr_pc;
8685 assign_component (container, lhs, lower, exp, &pos1);
8686 lower += 1;
8687 }
8688 }
8689 }
8690
8691 /* Assign the value of the expression in the OP_OTHERS construct in
8692 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
8693 have not been previously assigned. The index intervals already assigned
8694 are in INDICES[0 .. NUM_INDICES-1]. Updates *POS to after the
8695 OP_OTHERS clause. CONTAINER is as for assign_aggregate. */
8696 static void
8697 aggregate_assign_others (struct value *container,
8698 struct value *lhs, struct expression *exp,
8699 int *pos, LONGEST *indices, int num_indices,
8700 LONGEST low, LONGEST high)
8701 {
8702 int i;
8703 int expr_pc = *pos+1;
8704
8705 for (i = 0; i < num_indices - 2; i += 2)
8706 {
8707 LONGEST ind;
8708
8709 for (ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
8710 {
8711 int pos;
8712
8713 pos = expr_pc;
8714 assign_component (container, lhs, ind, exp, &pos);
8715 }
8716 }
8717 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8718 }
8719
8720 /* Add the interval [LOW .. HIGH] to the sorted set of intervals
8721 [ INDICES[0] .. INDICES[1] ],..., [ INDICES[*SIZE-2] .. INDICES[*SIZE-1] ],
8722 modifying *SIZE as needed. It is an error if *SIZE exceeds
8723 MAX_SIZE. The resulting intervals do not overlap. */
8724 static void
8725 add_component_interval (LONGEST low, LONGEST high,
8726 LONGEST* indices, int *size, int max_size)
8727 {
8728 int i, j;
8729
8730 for (i = 0; i < *size; i += 2) {
8731 if (high >= indices[i] && low <= indices[i + 1])
8732 {
8733 int kh;
8734
8735 for (kh = i + 2; kh < *size; kh += 2)
8736 if (high < indices[kh])
8737 break;
8738 if (low < indices[i])
8739 indices[i] = low;
8740 indices[i + 1] = indices[kh - 1];
8741 if (high > indices[i + 1])
8742 indices[i + 1] = high;
8743 memcpy (indices + i + 2, indices + kh, *size - kh);
8744 *size -= kh - i - 2;
8745 return;
8746 }
8747 else if (high < indices[i])
8748 break;
8749 }
8750
8751 if (*size == max_size)
8752 error (_("Internal error: miscounted aggregate components."));
8753 *size += 2;
8754 for (j = *size-1; j >= i+2; j -= 1)
8755 indices[j] = indices[j - 2];
8756 indices[i] = low;
8757 indices[i + 1] = high;
8758 }
8759
8760 /* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
8761 is different. */
8762
8763 static struct value *
8764 ada_value_cast (struct type *type, struct value *arg2, enum noside noside)
8765 {
8766 if (type == ada_check_typedef (value_type (arg2)))
8767 return arg2;
8768
8769 if (ada_is_fixed_point_type (type))
8770 return (cast_to_fixed (type, arg2));
8771
8772 if (ada_is_fixed_point_type (value_type (arg2)))
8773 return cast_from_fixed (type, arg2);
8774
8775 return value_cast (type, arg2);
8776 }
8777
8778 /* Evaluating Ada expressions, and printing their result.
8779 ------------------------------------------------------
8780
8781 1. Introduction:
8782 ----------------
8783
8784 We usually evaluate an Ada expression in order to print its value.
8785 We also evaluate an expression in order to print its type, which
8786 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
8787 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
8788 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
8789 the evaluation compared to the EVAL_NORMAL, but is otherwise very
8790 similar.
8791
8792 Evaluating expressions is a little more complicated for Ada entities
8793 than it is for entities in languages such as C. The main reason for
8794 this is that Ada provides types whose definition might be dynamic.
8795 One example of such types is variant records. Or another example
8796 would be an array whose bounds can only be known at run time.
8797
8798 The following description is a general guide as to what should be
8799 done (and what should NOT be done) in order to evaluate an expression
8800 involving such types, and when. This does not cover how the semantic
8801 information is encoded by GNAT as this is covered separatly. For the
8802 document used as the reference for the GNAT encoding, see exp_dbug.ads
8803 in the GNAT sources.
8804
8805 Ideally, we should embed each part of this description next to its
8806 associated code. Unfortunately, the amount of code is so vast right
8807 now that it's hard to see whether the code handling a particular
8808 situation might be duplicated or not. One day, when the code is
8809 cleaned up, this guide might become redundant with the comments
8810 inserted in the code, and we might want to remove it.
8811
8812 2. ``Fixing'' an Entity, the Simple Case:
8813 -----------------------------------------
8814
8815 When evaluating Ada expressions, the tricky issue is that they may
8816 reference entities whose type contents and size are not statically
8817 known. Consider for instance a variant record:
8818
8819 type Rec (Empty : Boolean := True) is record
8820 case Empty is
8821 when True => null;
8822 when False => Value : Integer;
8823 end case;
8824 end record;
8825 Yes : Rec := (Empty => False, Value => 1);
8826 No : Rec := (empty => True);
8827
8828 The size and contents of that record depends on the value of the
8829 descriminant (Rec.Empty). At this point, neither the debugging
8830 information nor the associated type structure in GDB are able to
8831 express such dynamic types. So what the debugger does is to create
8832 "fixed" versions of the type that applies to the specific object.
8833 We also informally refer to this opperation as "fixing" an object,
8834 which means creating its associated fixed type.
8835
8836 Example: when printing the value of variable "Yes" above, its fixed
8837 type would look like this:
8838
8839 type Rec is record
8840 Empty : Boolean;
8841 Value : Integer;
8842 end record;
8843
8844 On the other hand, if we printed the value of "No", its fixed type
8845 would become:
8846
8847 type Rec is record
8848 Empty : Boolean;
8849 end record;
8850
8851 Things become a little more complicated when trying to fix an entity
8852 with a dynamic type that directly contains another dynamic type,
8853 such as an array of variant records, for instance. There are
8854 two possible cases: Arrays, and records.
8855
8856 3. ``Fixing'' Arrays:
8857 ---------------------
8858
8859 The type structure in GDB describes an array in terms of its bounds,
8860 and the type of its elements. By design, all elements in the array
8861 have the same type and we cannot represent an array of variant elements
8862 using the current type structure in GDB. When fixing an array,
8863 we cannot fix the array element, as we would potentially need one
8864 fixed type per element of the array. As a result, the best we can do
8865 when fixing an array is to produce an array whose bounds and size
8866 are correct (allowing us to read it from memory), but without having
8867 touched its element type. Fixing each element will be done later,
8868 when (if) necessary.
8869
8870 Arrays are a little simpler to handle than records, because the same
8871 amount of memory is allocated for each element of the array, even if
8872 the amount of space actually used by each element differs from element
8873 to element. Consider for instance the following array of type Rec:
8874
8875 type Rec_Array is array (1 .. 2) of Rec;
8876
8877 The actual amount of memory occupied by each element might be different
8878 from element to element, depending on the value of their discriminant.
8879 But the amount of space reserved for each element in the array remains
8880 fixed regardless. So we simply need to compute that size using
8881 the debugging information available, from which we can then determine
8882 the array size (we multiply the number of elements of the array by
8883 the size of each element).
8884
8885 The simplest case is when we have an array of a constrained element
8886 type. For instance, consider the following type declarations:
8887
8888 type Bounded_String (Max_Size : Integer) is
8889 Length : Integer;
8890 Buffer : String (1 .. Max_Size);
8891 end record;
8892 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
8893
8894 In this case, the compiler describes the array as an array of
8895 variable-size elements (identified by its XVS suffix) for which
8896 the size can be read in the parallel XVZ variable.
8897
8898 In the case of an array of an unconstrained element type, the compiler
8899 wraps the array element inside a private PAD type. This type should not
8900 be shown to the user, and must be "unwrap"'ed before printing. Note
8901 that we also use the adjective "aligner" in our code to designate
8902 these wrapper types.
8903
8904 In some cases, the size allocated for each element is statically
8905 known. In that case, the PAD type already has the correct size,
8906 and the array element should remain unfixed.
8907
8908 But there are cases when this size is not statically known.
8909 For instance, assuming that "Five" is an integer variable:
8910
8911 type Dynamic is array (1 .. Five) of Integer;
8912 type Wrapper (Has_Length : Boolean := False) is record
8913 Data : Dynamic;
8914 case Has_Length is
8915 when True => Length : Integer;
8916 when False => null;
8917 end case;
8918 end record;
8919 type Wrapper_Array is array (1 .. 2) of Wrapper;
8920
8921 Hello : Wrapper_Array := (others => (Has_Length => True,
8922 Data => (others => 17),
8923 Length => 1));
8924
8925
8926 The debugging info would describe variable Hello as being an
8927 array of a PAD type. The size of that PAD type is not statically
8928 known, but can be determined using a parallel XVZ variable.
8929 In that case, a copy of the PAD type with the correct size should
8930 be used for the fixed array.
8931
8932 3. ``Fixing'' record type objects:
8933 ----------------------------------
8934
8935 Things are slightly different from arrays in the case of dynamic
8936 record types. In this case, in order to compute the associated
8937 fixed type, we need to determine the size and offset of each of
8938 its components. This, in turn, requires us to compute the fixed
8939 type of each of these components.
8940
8941 Consider for instance the example:
8942
8943 type Bounded_String (Max_Size : Natural) is record
8944 Str : String (1 .. Max_Size);
8945 Length : Natural;
8946 end record;
8947 My_String : Bounded_String (Max_Size => 10);
8948
8949 In that case, the position of field "Length" depends on the size
8950 of field Str, which itself depends on the value of the Max_Size
8951 discriminant. In order to fix the type of variable My_String,
8952 we need to fix the type of field Str. Therefore, fixing a variant
8953 record requires us to fix each of its components.
8954
8955 However, if a component does not have a dynamic size, the component
8956 should not be fixed. In particular, fields that use a PAD type
8957 should not fixed. Here is an example where this might happen
8958 (assuming type Rec above):
8959
8960 type Container (Big : Boolean) is record
8961 First : Rec;
8962 After : Integer;
8963 case Big is
8964 when True => Another : Integer;
8965 when False => null;
8966 end case;
8967 end record;
8968 My_Container : Container := (Big => False,
8969 First => (Empty => True),
8970 After => 42);
8971
8972 In that example, the compiler creates a PAD type for component First,
8973 whose size is constant, and then positions the component After just
8974 right after it. The offset of component After is therefore constant
8975 in this case.
8976
8977 The debugger computes the position of each field based on an algorithm
8978 that uses, among other things, the actual position and size of the field
8979 preceding it. Let's now imagine that the user is trying to print
8980 the value of My_Container. If the type fixing was recursive, we would
8981 end up computing the offset of field After based on the size of the
8982 fixed version of field First. And since in our example First has
8983 only one actual field, the size of the fixed type is actually smaller
8984 than the amount of space allocated to that field, and thus we would
8985 compute the wrong offset of field After.
8986
8987 To make things more complicated, we need to watch out for dynamic
8988 components of variant records (identified by the ___XVL suffix in
8989 the component name). Even if the target type is a PAD type, the size
8990 of that type might not be statically known. So the PAD type needs
8991 to be unwrapped and the resulting type needs to be fixed. Otherwise,
8992 we might end up with the wrong size for our component. This can be
8993 observed with the following type declarations:
8994
8995 type Octal is new Integer range 0 .. 7;
8996 type Octal_Array is array (Positive range <>) of Octal;
8997 pragma Pack (Octal_Array);
8998
8999 type Octal_Buffer (Size : Positive) is record
9000 Buffer : Octal_Array (1 .. Size);
9001 Length : Integer;
9002 end record;
9003
9004 In that case, Buffer is a PAD type whose size is unset and needs
9005 to be computed by fixing the unwrapped type.
9006
9007 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9008 ----------------------------------------------------------
9009
9010 Lastly, when should the sub-elements of an entity that remained unfixed
9011 thus far, be actually fixed?
9012
9013 The answer is: Only when referencing that element. For instance
9014 when selecting one component of a record, this specific component
9015 should be fixed at that point in time. Or when printing the value
9016 of a record, each component should be fixed before its value gets
9017 printed. Similarly for arrays, the element of the array should be
9018 fixed when printing each element of the array, or when extracting
9019 one element out of that array. On the other hand, fixing should
9020 not be performed on the elements when taking a slice of an array!
9021
9022 Note that one of the side-effects of miscomputing the offset and
9023 size of each field is that we end up also miscomputing the size
9024 of the containing type. This can have adverse results when computing
9025 the value of an entity. GDB fetches the value of an entity based
9026 on the size of its type, and thus a wrong size causes GDB to fetch
9027 the wrong amount of memory. In the case where the computed size is
9028 too small, GDB fetches too little data to print the value of our
9029 entiry. Results in this case as unpredicatble, as we usually read
9030 past the buffer containing the data =:-o. */
9031
9032 /* Implement the evaluate_exp routine in the exp_descriptor structure
9033 for the Ada language. */
9034
9035 static struct value *
9036 ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
9037 int *pos, enum noside noside)
9038 {
9039 enum exp_opcode op;
9040 int tem;
9041 int pc;
9042 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
9043 struct type *type;
9044 int nargs, oplen;
9045 struct value **argvec;
9046
9047 pc = *pos;
9048 *pos += 1;
9049 op = exp->elts[pc].opcode;
9050
9051 switch (op)
9052 {
9053 default:
9054 *pos -= 1;
9055 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
9056 arg1 = unwrap_value (arg1);
9057
9058 /* If evaluating an OP_DOUBLE and an EXPECT_TYPE was provided,
9059 then we need to perform the conversion manually, because
9060 evaluate_subexp_standard doesn't do it. This conversion is
9061 necessary in Ada because the different kinds of float/fixed
9062 types in Ada have different representations.
9063
9064 Similarly, we need to perform the conversion from OP_LONG
9065 ourselves. */
9066 if ((op == OP_DOUBLE || op == OP_LONG) && expect_type != NULL)
9067 arg1 = ada_value_cast (expect_type, arg1, noside);
9068
9069 return arg1;
9070
9071 case OP_STRING:
9072 {
9073 struct value *result;
9074
9075 *pos -= 1;
9076 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
9077 /* The result type will have code OP_STRING, bashed there from
9078 OP_ARRAY. Bash it back. */
9079 if (TYPE_CODE (value_type (result)) == TYPE_CODE_STRING)
9080 TYPE_CODE (value_type (result)) = TYPE_CODE_ARRAY;
9081 return result;
9082 }
9083
9084 case UNOP_CAST:
9085 (*pos) += 2;
9086 type = exp->elts[pc + 1].type;
9087 arg1 = evaluate_subexp (type, exp, pos, noside);
9088 if (noside == EVAL_SKIP)
9089 goto nosideret;
9090 arg1 = ada_value_cast (type, arg1, noside);
9091 return arg1;
9092
9093 case UNOP_QUAL:
9094 (*pos) += 2;
9095 type = exp->elts[pc + 1].type;
9096 return ada_evaluate_subexp (type, exp, pos, noside);
9097
9098 case BINOP_ASSIGN:
9099 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9100 if (exp->elts[*pos].opcode == OP_AGGREGATE)
9101 {
9102 arg1 = assign_aggregate (arg1, arg1, exp, pos, noside);
9103 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
9104 return arg1;
9105 return ada_value_assign (arg1, arg1);
9106 }
9107 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9108 except if the lhs of our assignment is a convenience variable.
9109 In the case of assigning to a convenience variable, the lhs
9110 should be exactly the result of the evaluation of the rhs. */
9111 type = value_type (arg1);
9112 if (VALUE_LVAL (arg1) == lval_internalvar)
9113 type = NULL;
9114 arg2 = evaluate_subexp (type, exp, pos, noside);
9115 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
9116 return arg1;
9117 if (ada_is_fixed_point_type (value_type (arg1)))
9118 arg2 = cast_to_fixed (value_type (arg1), arg2);
9119 else if (ada_is_fixed_point_type (value_type (arg2)))
9120 error
9121 (_("Fixed-point values must be assigned to fixed-point variables"));
9122 else
9123 arg2 = coerce_for_assign (value_type (arg1), arg2);
9124 return ada_value_assign (arg1, arg2);
9125
9126 case BINOP_ADD:
9127 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
9128 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
9129 if (noside == EVAL_SKIP)
9130 goto nosideret;
9131 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
9132 return (value_from_longest
9133 (value_type (arg1),
9134 value_as_long (arg1) + value_as_long (arg2)));
9135 if ((ada_is_fixed_point_type (value_type (arg1))
9136 || ada_is_fixed_point_type (value_type (arg2)))
9137 && value_type (arg1) != value_type (arg2))
9138 error (_("Operands of fixed-point addition must have the same type"));
9139 /* Do the addition, and cast the result to the type of the first
9140 argument. We cannot cast the result to a reference type, so if
9141 ARG1 is a reference type, find its underlying type. */
9142 type = value_type (arg1);
9143 while (TYPE_CODE (type) == TYPE_CODE_REF)
9144 type = TYPE_TARGET_TYPE (type);
9145 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9146 return value_cast (type, value_binop (arg1, arg2, BINOP_ADD));
9147
9148 case BINOP_SUB:
9149 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
9150 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
9151 if (noside == EVAL_SKIP)
9152 goto nosideret;
9153 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
9154 return (value_from_longest
9155 (value_type (arg1),
9156 value_as_long (arg1) - value_as_long (arg2)));
9157 if ((ada_is_fixed_point_type (value_type (arg1))
9158 || ada_is_fixed_point_type (value_type (arg2)))
9159 && value_type (arg1) != value_type (arg2))
9160 error (_("Operands of fixed-point subtraction "
9161 "must have the same type"));
9162 /* Do the substraction, and cast the result to the type of the first
9163 argument. We cannot cast the result to a reference type, so if
9164 ARG1 is a reference type, find its underlying type. */
9165 type = value_type (arg1);
9166 while (TYPE_CODE (type) == TYPE_CODE_REF)
9167 type = TYPE_TARGET_TYPE (type);
9168 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9169 return value_cast (type, value_binop (arg1, arg2, BINOP_SUB));
9170
9171 case BINOP_MUL:
9172 case BINOP_DIV:
9173 case BINOP_REM:
9174 case BINOP_MOD:
9175 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9176 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9177 if (noside == EVAL_SKIP)
9178 goto nosideret;
9179 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9180 {
9181 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9182 return value_zero (value_type (arg1), not_lval);
9183 }
9184 else
9185 {
9186 type = builtin_type (exp->gdbarch)->builtin_double;
9187 if (ada_is_fixed_point_type (value_type (arg1)))
9188 arg1 = cast_from_fixed (type, arg1);
9189 if (ada_is_fixed_point_type (value_type (arg2)))
9190 arg2 = cast_from_fixed (type, arg2);
9191 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9192 return ada_value_binop (arg1, arg2, op);
9193 }
9194
9195 case BINOP_EQUAL:
9196 case BINOP_NOTEQUAL:
9197 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9198 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
9199 if (noside == EVAL_SKIP)
9200 goto nosideret;
9201 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9202 tem = 0;
9203 else
9204 {
9205 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9206 tem = ada_value_equal (arg1, arg2);
9207 }
9208 if (op == BINOP_NOTEQUAL)
9209 tem = !tem;
9210 type = language_bool_type (exp->language_defn, exp->gdbarch);
9211 return value_from_longest (type, (LONGEST) tem);
9212
9213 case UNOP_NEG:
9214 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9215 if (noside == EVAL_SKIP)
9216 goto nosideret;
9217 else if (ada_is_fixed_point_type (value_type (arg1)))
9218 return value_cast (value_type (arg1), value_neg (arg1));
9219 else
9220 {
9221 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9222 return value_neg (arg1);
9223 }
9224
9225 case BINOP_LOGICAL_AND:
9226 case BINOP_LOGICAL_OR:
9227 case UNOP_LOGICAL_NOT:
9228 {
9229 struct value *val;
9230
9231 *pos -= 1;
9232 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
9233 type = language_bool_type (exp->language_defn, exp->gdbarch);
9234 return value_cast (type, val);
9235 }
9236
9237 case BINOP_BITWISE_AND:
9238 case BINOP_BITWISE_IOR:
9239 case BINOP_BITWISE_XOR:
9240 {
9241 struct value *val;
9242
9243 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
9244 *pos = pc;
9245 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
9246
9247 return value_cast (value_type (arg1), val);
9248 }
9249
9250 case OP_VAR_VALUE:
9251 *pos -= 1;
9252
9253 if (noside == EVAL_SKIP)
9254 {
9255 *pos += 4;
9256 goto nosideret;
9257 }
9258 else if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
9259 /* Only encountered when an unresolved symbol occurs in a
9260 context other than a function call, in which case, it is
9261 invalid. */
9262 error (_("Unexpected unresolved symbol, %s, during evaluation"),
9263 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
9264 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9265 {
9266 type = static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
9267 /* Check to see if this is a tagged type. We also need to handle
9268 the case where the type is a reference to a tagged type, but
9269 we have to be careful to exclude pointers to tagged types.
9270 The latter should be shown as usual (as a pointer), whereas
9271 a reference should mostly be transparent to the user. */
9272 if (ada_is_tagged_type (type, 0)
9273 || (TYPE_CODE(type) == TYPE_CODE_REF
9274 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
9275 {
9276 /* Tagged types are a little special in the fact that the real
9277 type is dynamic and can only be determined by inspecting the
9278 object's tag. This means that we need to get the object's
9279 value first (EVAL_NORMAL) and then extract the actual object
9280 type from its tag.
9281
9282 Note that we cannot skip the final step where we extract
9283 the object type from its tag, because the EVAL_NORMAL phase
9284 results in dynamic components being resolved into fixed ones.
9285 This can cause problems when trying to print the type
9286 description of tagged types whose parent has a dynamic size:
9287 We use the type name of the "_parent" component in order
9288 to print the name of the ancestor type in the type description.
9289 If that component had a dynamic size, the resolution into
9290 a fixed type would result in the loss of that type name,
9291 thus preventing us from printing the name of the ancestor
9292 type in the type description. */
9293 struct type *actual_type;
9294
9295 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
9296 actual_type = type_from_tag (ada_value_tag (arg1));
9297 if (actual_type == NULL)
9298 /* If, for some reason, we were unable to determine
9299 the actual type from the tag, then use the static
9300 approximation that we just computed as a fallback.
9301 This can happen if the debugging information is
9302 incomplete, for instance. */
9303 actual_type = type;
9304
9305 return value_zero (actual_type, not_lval);
9306 }
9307
9308 *pos += 4;
9309 return value_zero
9310 (to_static_fixed_type
9311 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
9312 not_lval);
9313 }
9314 else
9315 {
9316 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
9317 arg1 = unwrap_value (arg1);
9318 return ada_to_fixed_value (arg1);
9319 }
9320
9321 case OP_FUNCALL:
9322 (*pos) += 2;
9323
9324 /* Allocate arg vector, including space for the function to be
9325 called in argvec[0] and a terminating NULL. */
9326 nargs = longest_to_int (exp->elts[pc + 1].longconst);
9327 argvec =
9328 (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
9329
9330 if (exp->elts[*pos].opcode == OP_VAR_VALUE
9331 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
9332 error (_("Unexpected unresolved symbol, %s, during evaluation"),
9333 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
9334 else
9335 {
9336 for (tem = 0; tem <= nargs; tem += 1)
9337 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9338 argvec[tem] = 0;
9339
9340 if (noside == EVAL_SKIP)
9341 goto nosideret;
9342 }
9343
9344 if (ada_is_constrained_packed_array_type
9345 (desc_base_type (value_type (argvec[0]))))
9346 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
9347 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
9348 && TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
9349 /* This is a packed array that has already been fixed, and
9350 therefore already coerced to a simple array. Nothing further
9351 to do. */
9352 ;
9353 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_REF
9354 || (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
9355 && VALUE_LVAL (argvec[0]) == lval_memory))
9356 argvec[0] = value_addr (argvec[0]);
9357
9358 type = ada_check_typedef (value_type (argvec[0]));
9359
9360 /* Ada allows us to implicitly dereference arrays when subscripting
9361 them. So, if this is an typedef (encoding use for array access
9362 types encoded as fat pointers), strip it now. */
9363 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
9364 type = ada_typedef_target_type (type);
9365
9366 if (TYPE_CODE (type) == TYPE_CODE_PTR)
9367 {
9368 switch (TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type))))
9369 {
9370 case TYPE_CODE_FUNC:
9371 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
9372 break;
9373 case TYPE_CODE_ARRAY:
9374 break;
9375 case TYPE_CODE_STRUCT:
9376 if (noside != EVAL_AVOID_SIDE_EFFECTS)
9377 argvec[0] = ada_value_ind (argvec[0]);
9378 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
9379 break;
9380 default:
9381 error (_("cannot subscript or call something of type `%s'"),
9382 ada_type_name (value_type (argvec[0])));
9383 break;
9384 }
9385 }
9386
9387 switch (TYPE_CODE (type))
9388 {
9389 case TYPE_CODE_FUNC:
9390 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9391 return allocate_value (TYPE_TARGET_TYPE (type));
9392 return call_function_by_hand (argvec[0], nargs, argvec + 1);
9393 case TYPE_CODE_STRUCT:
9394 {
9395 int arity;
9396
9397 arity = ada_array_arity (type);
9398 type = ada_array_element_type (type, nargs);
9399 if (type == NULL)
9400 error (_("cannot subscript or call a record"));
9401 if (arity != nargs)
9402 error (_("wrong number of subscripts; expecting %d"), arity);
9403 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9404 return value_zero (ada_aligned_type (type), lval_memory);
9405 return
9406 unwrap_value (ada_value_subscript
9407 (argvec[0], nargs, argvec + 1));
9408 }
9409 case TYPE_CODE_ARRAY:
9410 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9411 {
9412 type = ada_array_element_type (type, nargs);
9413 if (type == NULL)
9414 error (_("element type of array unknown"));
9415 else
9416 return value_zero (ada_aligned_type (type), lval_memory);
9417 }
9418 return
9419 unwrap_value (ada_value_subscript
9420 (ada_coerce_to_simple_array (argvec[0]),
9421 nargs, argvec + 1));
9422 case TYPE_CODE_PTR: /* Pointer to array */
9423 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
9424 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9425 {
9426 type = ada_array_element_type (type, nargs);
9427 if (type == NULL)
9428 error (_("element type of array unknown"));
9429 else
9430 return value_zero (ada_aligned_type (type), lval_memory);
9431 }
9432 return
9433 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
9434 nargs, argvec + 1));
9435
9436 default:
9437 error (_("Attempt to index or call something other than an "
9438 "array or function"));
9439 }
9440
9441 case TERNOP_SLICE:
9442 {
9443 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9444 struct value *low_bound_val =
9445 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9446 struct value *high_bound_val =
9447 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9448 LONGEST low_bound;
9449 LONGEST high_bound;
9450
9451 low_bound_val = coerce_ref (low_bound_val);
9452 high_bound_val = coerce_ref (high_bound_val);
9453 low_bound = pos_atr (low_bound_val);
9454 high_bound = pos_atr (high_bound_val);
9455
9456 if (noside == EVAL_SKIP)
9457 goto nosideret;
9458
9459 /* If this is a reference to an aligner type, then remove all
9460 the aligners. */
9461 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9462 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
9463 TYPE_TARGET_TYPE (value_type (array)) =
9464 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
9465
9466 if (ada_is_constrained_packed_array_type (value_type (array)))
9467 error (_("cannot slice a packed array"));
9468
9469 /* If this is a reference to an array or an array lvalue,
9470 convert to a pointer. */
9471 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9472 || (TYPE_CODE (value_type (array)) == TYPE_CODE_ARRAY
9473 && VALUE_LVAL (array) == lval_memory))
9474 array = value_addr (array);
9475
9476 if (noside == EVAL_AVOID_SIDE_EFFECTS
9477 && ada_is_array_descriptor_type (ada_check_typedef
9478 (value_type (array))))
9479 return empty_array (ada_type_of_array (array, 0), low_bound);
9480
9481 array = ada_coerce_to_simple_array_ptr (array);
9482
9483 /* If we have more than one level of pointer indirection,
9484 dereference the value until we get only one level. */
9485 while (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR
9486 && (TYPE_CODE (TYPE_TARGET_TYPE (value_type (array)))
9487 == TYPE_CODE_PTR))
9488 array = value_ind (array);
9489
9490 /* Make sure we really do have an array type before going further,
9491 to avoid a SEGV when trying to get the index type or the target
9492 type later down the road if the debug info generated by
9493 the compiler is incorrect or incomplete. */
9494 if (!ada_is_simple_array_type (value_type (array)))
9495 error (_("cannot take slice of non-array"));
9496
9497 if (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR)
9498 {
9499 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
9500 return empty_array (TYPE_TARGET_TYPE (value_type (array)),
9501 low_bound);
9502 else
9503 {
9504 struct type *arr_type0 =
9505 to_fixed_array_type (TYPE_TARGET_TYPE (value_type (array)),
9506 NULL, 1);
9507
9508 return ada_value_slice_from_ptr (array, arr_type0,
9509 longest_to_int (low_bound),
9510 longest_to_int (high_bound));
9511 }
9512 }
9513 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9514 return array;
9515 else if (high_bound < low_bound)
9516 return empty_array (value_type (array), low_bound);
9517 else
9518 return ada_value_slice (array, longest_to_int (low_bound),
9519 longest_to_int (high_bound));
9520 }
9521
9522 case UNOP_IN_RANGE:
9523 (*pos) += 2;
9524 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9525 type = check_typedef (exp->elts[pc + 1].type);
9526
9527 if (noside == EVAL_SKIP)
9528 goto nosideret;
9529
9530 switch (TYPE_CODE (type))
9531 {
9532 default:
9533 lim_warning (_("Membership test incompletely implemented; "
9534 "always returns true"));
9535 type = language_bool_type (exp->language_defn, exp->gdbarch);
9536 return value_from_longest (type, (LONGEST) 1);
9537
9538 case TYPE_CODE_RANGE:
9539 arg2 = value_from_longest (type, TYPE_LOW_BOUND (type));
9540 arg3 = value_from_longest (type, TYPE_HIGH_BOUND (type));
9541 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9542 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9543 type = language_bool_type (exp->language_defn, exp->gdbarch);
9544 return
9545 value_from_longest (type,
9546 (value_less (arg1, arg3)
9547 || value_equal (arg1, arg3))
9548 && (value_less (arg2, arg1)
9549 || value_equal (arg2, arg1)));
9550 }
9551
9552 case BINOP_IN_BOUNDS:
9553 (*pos) += 2;
9554 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9555 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9556
9557 if (noside == EVAL_SKIP)
9558 goto nosideret;
9559
9560 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9561 {
9562 type = language_bool_type (exp->language_defn, exp->gdbarch);
9563 return value_zero (type, not_lval);
9564 }
9565
9566 tem = longest_to_int (exp->elts[pc + 1].longconst);
9567
9568 type = ada_index_type (value_type (arg2), tem, "range");
9569 if (!type)
9570 type = value_type (arg1);
9571
9572 arg3 = value_from_longest (type, ada_array_bound (arg2, tem, 1));
9573 arg2 = value_from_longest (type, ada_array_bound (arg2, tem, 0));
9574
9575 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9576 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9577 type = language_bool_type (exp->language_defn, exp->gdbarch);
9578 return
9579 value_from_longest (type,
9580 (value_less (arg1, arg3)
9581 || value_equal (arg1, arg3))
9582 && (value_less (arg2, arg1)
9583 || value_equal (arg2, arg1)));
9584
9585 case TERNOP_IN_RANGE:
9586 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9587 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9588 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9589
9590 if (noside == EVAL_SKIP)
9591 goto nosideret;
9592
9593 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9594 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9595 type = language_bool_type (exp->language_defn, exp->gdbarch);
9596 return
9597 value_from_longest (type,
9598 (value_less (arg1, arg3)
9599 || value_equal (arg1, arg3))
9600 && (value_less (arg2, arg1)
9601 || value_equal (arg2, arg1)));
9602
9603 case OP_ATR_FIRST:
9604 case OP_ATR_LAST:
9605 case OP_ATR_LENGTH:
9606 {
9607 struct type *type_arg;
9608
9609 if (exp->elts[*pos].opcode == OP_TYPE)
9610 {
9611 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9612 arg1 = NULL;
9613 type_arg = check_typedef (exp->elts[pc + 2].type);
9614 }
9615 else
9616 {
9617 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9618 type_arg = NULL;
9619 }
9620
9621 if (exp->elts[*pos].opcode != OP_LONG)
9622 error (_("Invalid operand to '%s"), ada_attribute_name (op));
9623 tem = longest_to_int (exp->elts[*pos + 2].longconst);
9624 *pos += 4;
9625
9626 if (noside == EVAL_SKIP)
9627 goto nosideret;
9628
9629 if (type_arg == NULL)
9630 {
9631 arg1 = ada_coerce_ref (arg1);
9632
9633 if (ada_is_constrained_packed_array_type (value_type (arg1)))
9634 arg1 = ada_coerce_to_simple_array (arg1);
9635
9636 type = ada_index_type (value_type (arg1), tem,
9637 ada_attribute_name (op));
9638 if (type == NULL)
9639 type = builtin_type (exp->gdbarch)->builtin_int;
9640
9641 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9642 return allocate_value (type);
9643
9644 switch (op)
9645 {
9646 default: /* Should never happen. */
9647 error (_("unexpected attribute encountered"));
9648 case OP_ATR_FIRST:
9649 return value_from_longest
9650 (type, ada_array_bound (arg1, tem, 0));
9651 case OP_ATR_LAST:
9652 return value_from_longest
9653 (type, ada_array_bound (arg1, tem, 1));
9654 case OP_ATR_LENGTH:
9655 return value_from_longest
9656 (type, ada_array_length (arg1, tem));
9657 }
9658 }
9659 else if (discrete_type_p (type_arg))
9660 {
9661 struct type *range_type;
9662 char *name = ada_type_name (type_arg);
9663
9664 range_type = NULL;
9665 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
9666 range_type = to_fixed_range_type (type_arg, NULL);
9667 if (range_type == NULL)
9668 range_type = type_arg;
9669 switch (op)
9670 {
9671 default:
9672 error (_("unexpected attribute encountered"));
9673 case OP_ATR_FIRST:
9674 return value_from_longest
9675 (range_type, ada_discrete_type_low_bound (range_type));
9676 case OP_ATR_LAST:
9677 return value_from_longest
9678 (range_type, ada_discrete_type_high_bound (range_type));
9679 case OP_ATR_LENGTH:
9680 error (_("the 'length attribute applies only to array types"));
9681 }
9682 }
9683 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
9684 error (_("unimplemented type attribute"));
9685 else
9686 {
9687 LONGEST low, high;
9688
9689 if (ada_is_constrained_packed_array_type (type_arg))
9690 type_arg = decode_constrained_packed_array_type (type_arg);
9691
9692 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
9693 if (type == NULL)
9694 type = builtin_type (exp->gdbarch)->builtin_int;
9695
9696 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9697 return allocate_value (type);
9698
9699 switch (op)
9700 {
9701 default:
9702 error (_("unexpected attribute encountered"));
9703 case OP_ATR_FIRST:
9704 low = ada_array_bound_from_type (type_arg, tem, 0);
9705 return value_from_longest (type, low);
9706 case OP_ATR_LAST:
9707 high = ada_array_bound_from_type (type_arg, tem, 1);
9708 return value_from_longest (type, high);
9709 case OP_ATR_LENGTH:
9710 low = ada_array_bound_from_type (type_arg, tem, 0);
9711 high = ada_array_bound_from_type (type_arg, tem, 1);
9712 return value_from_longest (type, high - low + 1);
9713 }
9714 }
9715 }
9716
9717 case OP_ATR_TAG:
9718 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9719 if (noside == EVAL_SKIP)
9720 goto nosideret;
9721
9722 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9723 return value_zero (ada_tag_type (arg1), not_lval);
9724
9725 return ada_value_tag (arg1);
9726
9727 case OP_ATR_MIN:
9728 case OP_ATR_MAX:
9729 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9730 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9731 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9732 if (noside == EVAL_SKIP)
9733 goto nosideret;
9734 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9735 return value_zero (value_type (arg1), not_lval);
9736 else
9737 {
9738 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9739 return value_binop (arg1, arg2,
9740 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
9741 }
9742
9743 case OP_ATR_MODULUS:
9744 {
9745 struct type *type_arg = check_typedef (exp->elts[pc + 2].type);
9746
9747 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9748 if (noside == EVAL_SKIP)
9749 goto nosideret;
9750
9751 if (!ada_is_modular_type (type_arg))
9752 error (_("'modulus must be applied to modular type"));
9753
9754 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
9755 ada_modulus (type_arg));
9756 }
9757
9758
9759 case OP_ATR_POS:
9760 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9761 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9762 if (noside == EVAL_SKIP)
9763 goto nosideret;
9764 type = builtin_type (exp->gdbarch)->builtin_int;
9765 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9766 return value_zero (type, not_lval);
9767 else
9768 return value_pos_atr (type, arg1);
9769
9770 case OP_ATR_SIZE:
9771 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9772 type = value_type (arg1);
9773
9774 /* If the argument is a reference, then dereference its type, since
9775 the user is really asking for the size of the actual object,
9776 not the size of the pointer. */
9777 if (TYPE_CODE (type) == TYPE_CODE_REF)
9778 type = TYPE_TARGET_TYPE (type);
9779
9780 if (noside == EVAL_SKIP)
9781 goto nosideret;
9782 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9783 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
9784 else
9785 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
9786 TARGET_CHAR_BIT * TYPE_LENGTH (type));
9787
9788 case OP_ATR_VAL:
9789 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9790 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9791 type = exp->elts[pc + 2].type;
9792 if (noside == EVAL_SKIP)
9793 goto nosideret;
9794 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9795 return value_zero (type, not_lval);
9796 else
9797 return value_val_atr (type, arg1);
9798
9799 case BINOP_EXP:
9800 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9801 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9802 if (noside == EVAL_SKIP)
9803 goto nosideret;
9804 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9805 return value_zero (value_type (arg1), not_lval);
9806 else
9807 {
9808 /* For integer exponentiation operations,
9809 only promote the first argument. */
9810 if (is_integral_type (value_type (arg2)))
9811 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9812 else
9813 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9814
9815 return value_binop (arg1, arg2, op);
9816 }
9817
9818 case UNOP_PLUS:
9819 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9820 if (noside == EVAL_SKIP)
9821 goto nosideret;
9822 else
9823 return arg1;
9824
9825 case UNOP_ABS:
9826 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9827 if (noside == EVAL_SKIP)
9828 goto nosideret;
9829 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9830 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
9831 return value_neg (arg1);
9832 else
9833 return arg1;
9834
9835 case UNOP_IND:
9836 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9837 if (noside == EVAL_SKIP)
9838 goto nosideret;
9839 type = ada_check_typedef (value_type (arg1));
9840 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9841 {
9842 if (ada_is_array_descriptor_type (type))
9843 /* GDB allows dereferencing GNAT array descriptors. */
9844 {
9845 struct type *arrType = ada_type_of_array (arg1, 0);
9846
9847 if (arrType == NULL)
9848 error (_("Attempt to dereference null array pointer."));
9849 return value_at_lazy (arrType, 0);
9850 }
9851 else if (TYPE_CODE (type) == TYPE_CODE_PTR
9852 || TYPE_CODE (type) == TYPE_CODE_REF
9853 /* In C you can dereference an array to get the 1st elt. */
9854 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
9855 {
9856 type = to_static_fixed_type
9857 (ada_aligned_type
9858 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
9859 check_size (type);
9860 return value_zero (type, lval_memory);
9861 }
9862 else if (TYPE_CODE (type) == TYPE_CODE_INT)
9863 {
9864 /* GDB allows dereferencing an int. */
9865 if (expect_type == NULL)
9866 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
9867 lval_memory);
9868 else
9869 {
9870 expect_type =
9871 to_static_fixed_type (ada_aligned_type (expect_type));
9872 return value_zero (expect_type, lval_memory);
9873 }
9874 }
9875 else
9876 error (_("Attempt to take contents of a non-pointer value."));
9877 }
9878 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
9879 type = ada_check_typedef (value_type (arg1));
9880
9881 if (TYPE_CODE (type) == TYPE_CODE_INT)
9882 /* GDB allows dereferencing an int. If we were given
9883 the expect_type, then use that as the target type.
9884 Otherwise, assume that the target type is an int. */
9885 {
9886 if (expect_type != NULL)
9887 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
9888 arg1));
9889 else
9890 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
9891 (CORE_ADDR) value_as_address (arg1));
9892 }
9893
9894 if (ada_is_array_descriptor_type (type))
9895 /* GDB allows dereferencing GNAT array descriptors. */
9896 return ada_coerce_to_simple_array (arg1);
9897 else
9898 return ada_value_ind (arg1);
9899
9900 case STRUCTOP_STRUCT:
9901 tem = longest_to_int (exp->elts[pc + 1].longconst);
9902 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
9903 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9904 if (noside == EVAL_SKIP)
9905 goto nosideret;
9906 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9907 {
9908 struct type *type1 = value_type (arg1);
9909
9910 if (ada_is_tagged_type (type1, 1))
9911 {
9912 type = ada_lookup_struct_elt_type (type1,
9913 &exp->elts[pc + 2].string,
9914 1, 1, NULL);
9915 if (type == NULL)
9916 /* In this case, we assume that the field COULD exist
9917 in some extension of the type. Return an object of
9918 "type" void, which will match any formal
9919 (see ada_type_match). */
9920 return value_zero (builtin_type (exp->gdbarch)->builtin_void,
9921 lval_memory);
9922 }
9923 else
9924 type =
9925 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
9926 0, NULL);
9927
9928 return value_zero (ada_aligned_type (type), lval_memory);
9929 }
9930 else
9931 arg1 = ada_value_struct_elt (arg1, &exp->elts[pc + 2].string, 0);
9932 arg1 = unwrap_value (arg1);
9933 return ada_to_fixed_value (arg1);
9934
9935 case OP_TYPE:
9936 /* The value is not supposed to be used. This is here to make it
9937 easier to accommodate expressions that contain types. */
9938 (*pos) += 2;
9939 if (noside == EVAL_SKIP)
9940 goto nosideret;
9941 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9942 return allocate_value (exp->elts[pc + 1].type);
9943 else
9944 error (_("Attempt to use a type name as an expression"));
9945
9946 case OP_AGGREGATE:
9947 case OP_CHOICES:
9948 case OP_OTHERS:
9949 case OP_DISCRETE_RANGE:
9950 case OP_POSITIONAL:
9951 case OP_NAME:
9952 if (noside == EVAL_NORMAL)
9953 switch (op)
9954 {
9955 case OP_NAME:
9956 error (_("Undefined name, ambiguous name, or renaming used in "
9957 "component association: %s."), &exp->elts[pc+2].string);
9958 case OP_AGGREGATE:
9959 error (_("Aggregates only allowed on the right of an assignment"));
9960 default:
9961 internal_error (__FILE__, __LINE__,
9962 _("aggregate apparently mangled"));
9963 }
9964
9965 ada_forward_operator_length (exp, pc, &oplen, &nargs);
9966 *pos += oplen - 1;
9967 for (tem = 0; tem < nargs; tem += 1)
9968 ada_evaluate_subexp (NULL, exp, pos, noside);
9969 goto nosideret;
9970 }
9971
9972 nosideret:
9973 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
9974 }
9975 \f
9976
9977 /* Fixed point */
9978
9979 /* If TYPE encodes an Ada fixed-point type, return the suffix of the
9980 type name that encodes the 'small and 'delta information.
9981 Otherwise, return NULL. */
9982
9983 static const char *
9984 fixed_type_info (struct type *type)
9985 {
9986 const char *name = ada_type_name (type);
9987 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
9988
9989 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
9990 {
9991 const char *tail = strstr (name, "___XF_");
9992
9993 if (tail == NULL)
9994 return NULL;
9995 else
9996 return tail + 5;
9997 }
9998 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
9999 return fixed_type_info (TYPE_TARGET_TYPE (type));
10000 else
10001 return NULL;
10002 }
10003
10004 /* Returns non-zero iff TYPE represents an Ada fixed-point type. */
10005
10006 int
10007 ada_is_fixed_point_type (struct type *type)
10008 {
10009 return fixed_type_info (type) != NULL;
10010 }
10011
10012 /* Return non-zero iff TYPE represents a System.Address type. */
10013
10014 int
10015 ada_is_system_address_type (struct type *type)
10016 {
10017 return (TYPE_NAME (type)
10018 && strcmp (TYPE_NAME (type), "system__address") == 0);
10019 }
10020
10021 /* Assuming that TYPE is the representation of an Ada fixed-point
10022 type, return its delta, or -1 if the type is malformed and the
10023 delta cannot be determined. */
10024
10025 DOUBLEST
10026 ada_delta (struct type *type)
10027 {
10028 const char *encoding = fixed_type_info (type);
10029 DOUBLEST num, den;
10030
10031 /* Strictly speaking, num and den are encoded as integer. However,
10032 they may not fit into a long, and they will have to be converted
10033 to DOUBLEST anyway. So scan them as DOUBLEST. */
10034 if (sscanf (encoding, "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
10035 &num, &den) < 2)
10036 return -1.0;
10037 else
10038 return num / den;
10039 }
10040
10041 /* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
10042 factor ('SMALL value) associated with the type. */
10043
10044 static DOUBLEST
10045 scaling_factor (struct type *type)
10046 {
10047 const char *encoding = fixed_type_info (type);
10048 DOUBLEST num0, den0, num1, den1;
10049 int n;
10050
10051 /* Strictly speaking, num's and den's are encoded as integer. However,
10052 they may not fit into a long, and they will have to be converted
10053 to DOUBLEST anyway. So scan them as DOUBLEST. */
10054 n = sscanf (encoding,
10055 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT
10056 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
10057 &num0, &den0, &num1, &den1);
10058
10059 if (n < 2)
10060 return 1.0;
10061 else if (n == 4)
10062 return num1 / den1;
10063 else
10064 return num0 / den0;
10065 }
10066
10067
10068 /* Assuming that X is the representation of a value of fixed-point
10069 type TYPE, return its floating-point equivalent. */
10070
10071 DOUBLEST
10072 ada_fixed_to_float (struct type *type, LONGEST x)
10073 {
10074 return (DOUBLEST) x *scaling_factor (type);
10075 }
10076
10077 /* The representation of a fixed-point value of type TYPE
10078 corresponding to the value X. */
10079
10080 LONGEST
10081 ada_float_to_fixed (struct type *type, DOUBLEST x)
10082 {
10083 return (LONGEST) (x / scaling_factor (type) + 0.5);
10084 }
10085
10086 \f
10087
10088 /* Range types */
10089
10090 /* Scan STR beginning at position K for a discriminant name, and
10091 return the value of that discriminant field of DVAL in *PX. If
10092 PNEW_K is not null, put the position of the character beyond the
10093 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
10094 not alter *PX and *PNEW_K if unsuccessful. */
10095
10096 static int
10097 scan_discrim_bound (char *str, int k, struct value *dval, LONGEST * px,
10098 int *pnew_k)
10099 {
10100 static char *bound_buffer = NULL;
10101 static size_t bound_buffer_len = 0;
10102 char *bound;
10103 char *pend;
10104 struct value *bound_val;
10105
10106 if (dval == NULL || str == NULL || str[k] == '\0')
10107 return 0;
10108
10109 pend = strstr (str + k, "__");
10110 if (pend == NULL)
10111 {
10112 bound = str + k;
10113 k += strlen (bound);
10114 }
10115 else
10116 {
10117 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
10118 bound = bound_buffer;
10119 strncpy (bound_buffer, str + k, pend - (str + k));
10120 bound[pend - (str + k)] = '\0';
10121 k = pend - str;
10122 }
10123
10124 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
10125 if (bound_val == NULL)
10126 return 0;
10127
10128 *px = value_as_long (bound_val);
10129 if (pnew_k != NULL)
10130 *pnew_k = k;
10131 return 1;
10132 }
10133
10134 /* Value of variable named NAME in the current environment. If
10135 no such variable found, then if ERR_MSG is null, returns 0, and
10136 otherwise causes an error with message ERR_MSG. */
10137
10138 static struct value *
10139 get_var_value (char *name, char *err_msg)
10140 {
10141 struct ada_symbol_info *syms;
10142 int nsyms;
10143
10144 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
10145 &syms);
10146
10147 if (nsyms != 1)
10148 {
10149 if (err_msg == NULL)
10150 return 0;
10151 else
10152 error (("%s"), err_msg);
10153 }
10154
10155 return value_of_variable (syms[0].sym, syms[0].block);
10156 }
10157
10158 /* Value of integer variable named NAME in the current environment. If
10159 no such variable found, returns 0, and sets *FLAG to 0. If
10160 successful, sets *FLAG to 1. */
10161
10162 LONGEST
10163 get_int_var_value (char *name, int *flag)
10164 {
10165 struct value *var_val = get_var_value (name, 0);
10166
10167 if (var_val == 0)
10168 {
10169 if (flag != NULL)
10170 *flag = 0;
10171 return 0;
10172 }
10173 else
10174 {
10175 if (flag != NULL)
10176 *flag = 1;
10177 return value_as_long (var_val);
10178 }
10179 }
10180
10181
10182 /* Return a range type whose base type is that of the range type named
10183 NAME in the current environment, and whose bounds are calculated
10184 from NAME according to the GNAT range encoding conventions.
10185 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
10186 corresponding range type from debug information; fall back to using it
10187 if symbol lookup fails. If a new type must be created, allocate it
10188 like ORIG_TYPE was. The bounds information, in general, is encoded
10189 in NAME, the base type given in the named range type. */
10190
10191 static struct type *
10192 to_fixed_range_type (struct type *raw_type, struct value *dval)
10193 {
10194 char *name;
10195 struct type *base_type;
10196 char *subtype_info;
10197
10198 gdb_assert (raw_type != NULL);
10199 gdb_assert (TYPE_NAME (raw_type) != NULL);
10200
10201 if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
10202 base_type = TYPE_TARGET_TYPE (raw_type);
10203 else
10204 base_type = raw_type;
10205
10206 name = TYPE_NAME (raw_type);
10207 subtype_info = strstr (name, "___XD");
10208 if (subtype_info == NULL)
10209 {
10210 LONGEST L = ada_discrete_type_low_bound (raw_type);
10211 LONGEST U = ada_discrete_type_high_bound (raw_type);
10212
10213 if (L < INT_MIN || U > INT_MAX)
10214 return raw_type;
10215 else
10216 return create_range_type (alloc_type_copy (raw_type), raw_type,
10217 ada_discrete_type_low_bound (raw_type),
10218 ada_discrete_type_high_bound (raw_type));
10219 }
10220 else
10221 {
10222 static char *name_buf = NULL;
10223 static size_t name_len = 0;
10224 int prefix_len = subtype_info - name;
10225 LONGEST L, U;
10226 struct type *type;
10227 char *bounds_str;
10228 int n;
10229
10230 GROW_VECT (name_buf, name_len, prefix_len + 5);
10231 strncpy (name_buf, name, prefix_len);
10232 name_buf[prefix_len] = '\0';
10233
10234 subtype_info += 5;
10235 bounds_str = strchr (subtype_info, '_');
10236 n = 1;
10237
10238 if (*subtype_info == 'L')
10239 {
10240 if (!ada_scan_number (bounds_str, n, &L, &n)
10241 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
10242 return raw_type;
10243 if (bounds_str[n] == '_')
10244 n += 2;
10245 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
10246 n += 1;
10247 subtype_info += 1;
10248 }
10249 else
10250 {
10251 int ok;
10252
10253 strcpy (name_buf + prefix_len, "___L");
10254 L = get_int_var_value (name_buf, &ok);
10255 if (!ok)
10256 {
10257 lim_warning (_("Unknown lower bound, using 1."));
10258 L = 1;
10259 }
10260 }
10261
10262 if (*subtype_info == 'U')
10263 {
10264 if (!ada_scan_number (bounds_str, n, &U, &n)
10265 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
10266 return raw_type;
10267 }
10268 else
10269 {
10270 int ok;
10271
10272 strcpy (name_buf + prefix_len, "___U");
10273 U = get_int_var_value (name_buf, &ok);
10274 if (!ok)
10275 {
10276 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
10277 U = L;
10278 }
10279 }
10280
10281 type = create_range_type (alloc_type_copy (raw_type), base_type, L, U);
10282 TYPE_NAME (type) = name;
10283 return type;
10284 }
10285 }
10286
10287 /* True iff NAME is the name of a range type. */
10288
10289 int
10290 ada_is_range_type_name (const char *name)
10291 {
10292 return (name != NULL && strstr (name, "___XD"));
10293 }
10294 \f
10295
10296 /* Modular types */
10297
10298 /* True iff TYPE is an Ada modular type. */
10299
10300 int
10301 ada_is_modular_type (struct type *type)
10302 {
10303 struct type *subranged_type = base_type (type);
10304
10305 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
10306 && TYPE_CODE (subranged_type) == TYPE_CODE_INT
10307 && TYPE_UNSIGNED (subranged_type));
10308 }
10309
10310 /* Try to determine the lower and upper bounds of the given modular type
10311 using the type name only. Return non-zero and set L and U as the lower
10312 and upper bounds (respectively) if successful. */
10313
10314 int
10315 ada_modulus_from_name (struct type *type, ULONGEST *modulus)
10316 {
10317 char *name = ada_type_name (type);
10318 char *suffix;
10319 int k;
10320 LONGEST U;
10321
10322 if (name == NULL)
10323 return 0;
10324
10325 /* Discrete type bounds are encoded using an __XD suffix. In our case,
10326 we are looking for static bounds, which means an __XDLU suffix.
10327 Moreover, we know that the lower bound of modular types is always
10328 zero, so the actual suffix should start with "__XDLU_0__", and
10329 then be followed by the upper bound value. */
10330 suffix = strstr (name, "__XDLU_0__");
10331 if (suffix == NULL)
10332 return 0;
10333 k = 10;
10334 if (!ada_scan_number (suffix, k, &U, NULL))
10335 return 0;
10336
10337 *modulus = (ULONGEST) U + 1;
10338 return 1;
10339 }
10340
10341 /* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
10342
10343 ULONGEST
10344 ada_modulus (struct type *type)
10345 {
10346 return (ULONGEST) TYPE_HIGH_BOUND (type) + 1;
10347 }
10348 \f
10349
10350 /* Ada exception catchpoint support:
10351 ---------------------------------
10352
10353 We support 3 kinds of exception catchpoints:
10354 . catchpoints on Ada exceptions
10355 . catchpoints on unhandled Ada exceptions
10356 . catchpoints on failed assertions
10357
10358 Exceptions raised during failed assertions, or unhandled exceptions
10359 could perfectly be caught with the general catchpoint on Ada exceptions.
10360 However, we can easily differentiate these two special cases, and having
10361 the option to distinguish these two cases from the rest can be useful
10362 to zero-in on certain situations.
10363
10364 Exception catchpoints are a specialized form of breakpoint,
10365 since they rely on inserting breakpoints inside known routines
10366 of the GNAT runtime. The implementation therefore uses a standard
10367 breakpoint structure of the BP_BREAKPOINT type, but with its own set
10368 of breakpoint_ops.
10369
10370 Support in the runtime for exception catchpoints have been changed
10371 a few times already, and these changes affect the implementation
10372 of these catchpoints. In order to be able to support several
10373 variants of the runtime, we use a sniffer that will determine
10374 the runtime variant used by the program being debugged.
10375
10376 At this time, we do not support the use of conditions on Ada exception
10377 catchpoints. The COND and COND_STRING fields are therefore set
10378 to NULL (most of the time, see below).
10379
10380 Conditions where EXP_STRING, COND, and COND_STRING are used:
10381
10382 When a user specifies the name of a specific exception in the case
10383 of catchpoints on Ada exceptions, we store the name of that exception
10384 in the EXP_STRING. We then translate this request into an actual
10385 condition stored in COND_STRING, and then parse it into an expression
10386 stored in COND. */
10387
10388 /* The different types of catchpoints that we introduced for catching
10389 Ada exceptions. */
10390
10391 enum exception_catchpoint_kind
10392 {
10393 ex_catch_exception,
10394 ex_catch_exception_unhandled,
10395 ex_catch_assert
10396 };
10397
10398 /* Ada's standard exceptions. */
10399
10400 static char *standard_exc[] = {
10401 "constraint_error",
10402 "program_error",
10403 "storage_error",
10404 "tasking_error"
10405 };
10406
10407 typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
10408
10409 /* A structure that describes how to support exception catchpoints
10410 for a given executable. */
10411
10412 struct exception_support_info
10413 {
10414 /* The name of the symbol to break on in order to insert
10415 a catchpoint on exceptions. */
10416 const char *catch_exception_sym;
10417
10418 /* The name of the symbol to break on in order to insert
10419 a catchpoint on unhandled exceptions. */
10420 const char *catch_exception_unhandled_sym;
10421
10422 /* The name of the symbol to break on in order to insert
10423 a catchpoint on failed assertions. */
10424 const char *catch_assert_sym;
10425
10426 /* Assuming that the inferior just triggered an unhandled exception
10427 catchpoint, this function is responsible for returning the address
10428 in inferior memory where the name of that exception is stored.
10429 Return zero if the address could not be computed. */
10430 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
10431 };
10432
10433 static CORE_ADDR ada_unhandled_exception_name_addr (void);
10434 static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
10435
10436 /* The following exception support info structure describes how to
10437 implement exception catchpoints with the latest version of the
10438 Ada runtime (as of 2007-03-06). */
10439
10440 static const struct exception_support_info default_exception_support_info =
10441 {
10442 "__gnat_debug_raise_exception", /* catch_exception_sym */
10443 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10444 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
10445 ada_unhandled_exception_name_addr
10446 };
10447
10448 /* The following exception support info structure describes how to
10449 implement exception catchpoints with a slightly older version
10450 of the Ada runtime. */
10451
10452 static const struct exception_support_info exception_support_info_fallback =
10453 {
10454 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
10455 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10456 "system__assertions__raise_assert_failure", /* catch_assert_sym */
10457 ada_unhandled_exception_name_addr_from_raise
10458 };
10459
10460 /* For each executable, we sniff which exception info structure to use
10461 and cache it in the following global variable. */
10462
10463 static const struct exception_support_info *exception_info = NULL;
10464
10465 /* Inspect the Ada runtime and determine which exception info structure
10466 should be used to provide support for exception catchpoints.
10467
10468 This function will always set exception_info, or raise an error. */
10469
10470 static void
10471 ada_exception_support_info_sniffer (void)
10472 {
10473 struct symbol *sym;
10474
10475 /* If the exception info is already known, then no need to recompute it. */
10476 if (exception_info != NULL)
10477 return;
10478
10479 /* Check the latest (default) exception support info. */
10480 sym = standard_lookup (default_exception_support_info.catch_exception_sym,
10481 NULL, VAR_DOMAIN);
10482 if (sym != NULL)
10483 {
10484 exception_info = &default_exception_support_info;
10485 return;
10486 }
10487
10488 /* Try our fallback exception suport info. */
10489 sym = standard_lookup (exception_support_info_fallback.catch_exception_sym,
10490 NULL, VAR_DOMAIN);
10491 if (sym != NULL)
10492 {
10493 exception_info = &exception_support_info_fallback;
10494 return;
10495 }
10496
10497 /* Sometimes, it is normal for us to not be able to find the routine
10498 we are looking for. This happens when the program is linked with
10499 the shared version of the GNAT runtime, and the program has not been
10500 started yet. Inform the user of these two possible causes if
10501 applicable. */
10502
10503 if (ada_update_initial_language (language_unknown) != language_ada)
10504 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
10505
10506 /* If the symbol does not exist, then check that the program is
10507 already started, to make sure that shared libraries have been
10508 loaded. If it is not started, this may mean that the symbol is
10509 in a shared library. */
10510
10511 if (ptid_get_pid (inferior_ptid) == 0)
10512 error (_("Unable to insert catchpoint. Try to start the program first."));
10513
10514 /* At this point, we know that we are debugging an Ada program and
10515 that the inferior has been started, but we still are not able to
10516 find the run-time symbols. That can mean that we are in
10517 configurable run time mode, or that a-except as been optimized
10518 out by the linker... In any case, at this point it is not worth
10519 supporting this feature. */
10520
10521 error (_("Cannot insert catchpoints in this configuration."));
10522 }
10523
10524 /* An observer of "executable_changed" events.
10525 Its role is to clear certain cached values that need to be recomputed
10526 each time a new executable is loaded by GDB. */
10527
10528 static void
10529 ada_executable_changed_observer (void)
10530 {
10531 /* If the executable changed, then it is possible that the Ada runtime
10532 is different. So we need to invalidate the exception support info
10533 cache. */
10534 exception_info = NULL;
10535 }
10536
10537 /* True iff FRAME is very likely to be that of a function that is
10538 part of the runtime system. This is all very heuristic, but is
10539 intended to be used as advice as to what frames are uninteresting
10540 to most users. */
10541
10542 static int
10543 is_known_support_routine (struct frame_info *frame)
10544 {
10545 struct symtab_and_line sal;
10546 char *func_name;
10547 enum language func_lang;
10548 int i;
10549
10550 /* If this code does not have any debugging information (no symtab),
10551 This cannot be any user code. */
10552
10553 find_frame_sal (frame, &sal);
10554 if (sal.symtab == NULL)
10555 return 1;
10556
10557 /* If there is a symtab, but the associated source file cannot be
10558 located, then assume this is not user code: Selecting a frame
10559 for which we cannot display the code would not be very helpful
10560 for the user. This should also take care of case such as VxWorks
10561 where the kernel has some debugging info provided for a few units. */
10562
10563 if (symtab_to_fullname (sal.symtab) == NULL)
10564 return 1;
10565
10566 /* Check the unit filename againt the Ada runtime file naming.
10567 We also check the name of the objfile against the name of some
10568 known system libraries that sometimes come with debugging info
10569 too. */
10570
10571 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
10572 {
10573 re_comp (known_runtime_file_name_patterns[i]);
10574 if (re_exec (sal.symtab->filename))
10575 return 1;
10576 if (sal.symtab->objfile != NULL
10577 && re_exec (sal.symtab->objfile->name))
10578 return 1;
10579 }
10580
10581 /* Check whether the function is a GNAT-generated entity. */
10582
10583 find_frame_funname (frame, &func_name, &func_lang, NULL);
10584 if (func_name == NULL)
10585 return 1;
10586
10587 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
10588 {
10589 re_comp (known_auxiliary_function_name_patterns[i]);
10590 if (re_exec (func_name))
10591 return 1;
10592 }
10593
10594 return 0;
10595 }
10596
10597 /* Find the first frame that contains debugging information and that is not
10598 part of the Ada run-time, starting from FI and moving upward. */
10599
10600 void
10601 ada_find_printable_frame (struct frame_info *fi)
10602 {
10603 for (; fi != NULL; fi = get_prev_frame (fi))
10604 {
10605 if (!is_known_support_routine (fi))
10606 {
10607 select_frame (fi);
10608 break;
10609 }
10610 }
10611
10612 }
10613
10614 /* Assuming that the inferior just triggered an unhandled exception
10615 catchpoint, return the address in inferior memory where the name
10616 of the exception is stored.
10617
10618 Return zero if the address could not be computed. */
10619
10620 static CORE_ADDR
10621 ada_unhandled_exception_name_addr (void)
10622 {
10623 return parse_and_eval_address ("e.full_name");
10624 }
10625
10626 /* Same as ada_unhandled_exception_name_addr, except that this function
10627 should be used when the inferior uses an older version of the runtime,
10628 where the exception name needs to be extracted from a specific frame
10629 several frames up in the callstack. */
10630
10631 static CORE_ADDR
10632 ada_unhandled_exception_name_addr_from_raise (void)
10633 {
10634 int frame_level;
10635 struct frame_info *fi;
10636
10637 /* To determine the name of this exception, we need to select
10638 the frame corresponding to RAISE_SYM_NAME. This frame is
10639 at least 3 levels up, so we simply skip the first 3 frames
10640 without checking the name of their associated function. */
10641 fi = get_current_frame ();
10642 for (frame_level = 0; frame_level < 3; frame_level += 1)
10643 if (fi != NULL)
10644 fi = get_prev_frame (fi);
10645
10646 while (fi != NULL)
10647 {
10648 char *func_name;
10649 enum language func_lang;
10650
10651 find_frame_funname (fi, &func_name, &func_lang, NULL);
10652 if (func_name != NULL
10653 && strcmp (func_name, exception_info->catch_exception_sym) == 0)
10654 break; /* We found the frame we were looking for... */
10655 fi = get_prev_frame (fi);
10656 }
10657
10658 if (fi == NULL)
10659 return 0;
10660
10661 select_frame (fi);
10662 return parse_and_eval_address ("id.full_name");
10663 }
10664
10665 /* Assuming the inferior just triggered an Ada exception catchpoint
10666 (of any type), return the address in inferior memory where the name
10667 of the exception is stored, if applicable.
10668
10669 Return zero if the address could not be computed, or if not relevant. */
10670
10671 static CORE_ADDR
10672 ada_exception_name_addr_1 (enum exception_catchpoint_kind ex,
10673 struct breakpoint *b)
10674 {
10675 switch (ex)
10676 {
10677 case ex_catch_exception:
10678 return (parse_and_eval_address ("e.full_name"));
10679 break;
10680
10681 case ex_catch_exception_unhandled:
10682 return exception_info->unhandled_exception_name_addr ();
10683 break;
10684
10685 case ex_catch_assert:
10686 return 0; /* Exception name is not relevant in this case. */
10687 break;
10688
10689 default:
10690 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10691 break;
10692 }
10693
10694 return 0; /* Should never be reached. */
10695 }
10696
10697 /* Same as ada_exception_name_addr_1, except that it intercepts and contains
10698 any error that ada_exception_name_addr_1 might cause to be thrown.
10699 When an error is intercepted, a warning with the error message is printed,
10700 and zero is returned. */
10701
10702 static CORE_ADDR
10703 ada_exception_name_addr (enum exception_catchpoint_kind ex,
10704 struct breakpoint *b)
10705 {
10706 struct gdb_exception e;
10707 CORE_ADDR result = 0;
10708
10709 TRY_CATCH (e, RETURN_MASK_ERROR)
10710 {
10711 result = ada_exception_name_addr_1 (ex, b);
10712 }
10713
10714 if (e.reason < 0)
10715 {
10716 warning (_("failed to get exception name: %s"), e.message);
10717 return 0;
10718 }
10719
10720 return result;
10721 }
10722
10723 /* Implement the PRINT_IT method in the breakpoint_ops structure
10724 for all exception catchpoint kinds. */
10725
10726 static enum print_stop_action
10727 print_it_exception (enum exception_catchpoint_kind ex, struct breakpoint *b)
10728 {
10729 const CORE_ADDR addr = ada_exception_name_addr (ex, b);
10730 char exception_name[256];
10731
10732 if (addr != 0)
10733 {
10734 read_memory (addr, exception_name, sizeof (exception_name) - 1);
10735 exception_name [sizeof (exception_name) - 1] = '\0';
10736 }
10737
10738 ada_find_printable_frame (get_current_frame ());
10739
10740 annotate_catchpoint (b->number);
10741 switch (ex)
10742 {
10743 case ex_catch_exception:
10744 if (addr != 0)
10745 printf_filtered (_("\nCatchpoint %d, %s at "),
10746 b->number, exception_name);
10747 else
10748 printf_filtered (_("\nCatchpoint %d, exception at "), b->number);
10749 break;
10750 case ex_catch_exception_unhandled:
10751 if (addr != 0)
10752 printf_filtered (_("\nCatchpoint %d, unhandled %s at "),
10753 b->number, exception_name);
10754 else
10755 printf_filtered (_("\nCatchpoint %d, unhandled exception at "),
10756 b->number);
10757 break;
10758 case ex_catch_assert:
10759 printf_filtered (_("\nCatchpoint %d, failed assertion at "),
10760 b->number);
10761 break;
10762 }
10763
10764 return PRINT_SRC_AND_LOC;
10765 }
10766
10767 /* Implement the PRINT_ONE method in the breakpoint_ops structure
10768 for all exception catchpoint kinds. */
10769
10770 static void
10771 print_one_exception (enum exception_catchpoint_kind ex,
10772 struct breakpoint *b, struct bp_location **last_loc)
10773 {
10774 struct value_print_options opts;
10775
10776 get_user_print_options (&opts);
10777 if (opts.addressprint)
10778 {
10779 annotate_field (4);
10780 ui_out_field_core_addr (uiout, "addr", b->loc->gdbarch, b->loc->address);
10781 }
10782
10783 annotate_field (5);
10784 *last_loc = b->loc;
10785 switch (ex)
10786 {
10787 case ex_catch_exception:
10788 if (b->exp_string != NULL)
10789 {
10790 char *msg = xstrprintf (_("`%s' Ada exception"), b->exp_string);
10791
10792 ui_out_field_string (uiout, "what", msg);
10793 xfree (msg);
10794 }
10795 else
10796 ui_out_field_string (uiout, "what", "all Ada exceptions");
10797
10798 break;
10799
10800 case ex_catch_exception_unhandled:
10801 ui_out_field_string (uiout, "what", "unhandled Ada exceptions");
10802 break;
10803
10804 case ex_catch_assert:
10805 ui_out_field_string (uiout, "what", "failed Ada assertions");
10806 break;
10807
10808 default:
10809 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10810 break;
10811 }
10812 }
10813
10814 /* Implement the PRINT_MENTION method in the breakpoint_ops structure
10815 for all exception catchpoint kinds. */
10816
10817 static void
10818 print_mention_exception (enum exception_catchpoint_kind ex,
10819 struct breakpoint *b)
10820 {
10821 switch (ex)
10822 {
10823 case ex_catch_exception:
10824 if (b->exp_string != NULL)
10825 printf_filtered (_("Catchpoint %d: `%s' Ada exception"),
10826 b->number, b->exp_string);
10827 else
10828 printf_filtered (_("Catchpoint %d: all Ada exceptions"), b->number);
10829
10830 break;
10831
10832 case ex_catch_exception_unhandled:
10833 printf_filtered (_("Catchpoint %d: unhandled Ada exceptions"),
10834 b->number);
10835 break;
10836
10837 case ex_catch_assert:
10838 printf_filtered (_("Catchpoint %d: failed Ada assertions"), b->number);
10839 break;
10840
10841 default:
10842 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10843 break;
10844 }
10845 }
10846
10847 /* Implement the PRINT_RECREATE method in the breakpoint_ops structure
10848 for all exception catchpoint kinds. */
10849
10850 static void
10851 print_recreate_exception (enum exception_catchpoint_kind ex,
10852 struct breakpoint *b, struct ui_file *fp)
10853 {
10854 switch (ex)
10855 {
10856 case ex_catch_exception:
10857 fprintf_filtered (fp, "catch exception");
10858 if (b->exp_string != NULL)
10859 fprintf_filtered (fp, " %s", b->exp_string);
10860 break;
10861
10862 case ex_catch_exception_unhandled:
10863 fprintf_filtered (fp, "catch exception unhandled");
10864 break;
10865
10866 case ex_catch_assert:
10867 fprintf_filtered (fp, "catch assert");
10868 break;
10869
10870 default:
10871 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10872 }
10873 }
10874
10875 /* Virtual table for "catch exception" breakpoints. */
10876
10877 static enum print_stop_action
10878 print_it_catch_exception (struct breakpoint *b)
10879 {
10880 return print_it_exception (ex_catch_exception, b);
10881 }
10882
10883 static void
10884 print_one_catch_exception (struct breakpoint *b, struct bp_location **last_loc)
10885 {
10886 print_one_exception (ex_catch_exception, b, last_loc);
10887 }
10888
10889 static void
10890 print_mention_catch_exception (struct breakpoint *b)
10891 {
10892 print_mention_exception (ex_catch_exception, b);
10893 }
10894
10895 static void
10896 print_recreate_catch_exception (struct breakpoint *b, struct ui_file *fp)
10897 {
10898 print_recreate_exception (ex_catch_exception, b, fp);
10899 }
10900
10901 static struct breakpoint_ops catch_exception_breakpoint_ops =
10902 {
10903 NULL, /* insert */
10904 NULL, /* remove */
10905 NULL, /* breakpoint_hit */
10906 NULL, /* resources_needed */
10907 print_it_catch_exception,
10908 print_one_catch_exception,
10909 print_mention_catch_exception,
10910 print_recreate_catch_exception
10911 };
10912
10913 /* Virtual table for "catch exception unhandled" breakpoints. */
10914
10915 static enum print_stop_action
10916 print_it_catch_exception_unhandled (struct breakpoint *b)
10917 {
10918 return print_it_exception (ex_catch_exception_unhandled, b);
10919 }
10920
10921 static void
10922 print_one_catch_exception_unhandled (struct breakpoint *b,
10923 struct bp_location **last_loc)
10924 {
10925 print_one_exception (ex_catch_exception_unhandled, b, last_loc);
10926 }
10927
10928 static void
10929 print_mention_catch_exception_unhandled (struct breakpoint *b)
10930 {
10931 print_mention_exception (ex_catch_exception_unhandled, b);
10932 }
10933
10934 static void
10935 print_recreate_catch_exception_unhandled (struct breakpoint *b,
10936 struct ui_file *fp)
10937 {
10938 print_recreate_exception (ex_catch_exception_unhandled, b, fp);
10939 }
10940
10941 static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops = {
10942 NULL, /* insert */
10943 NULL, /* remove */
10944 NULL, /* breakpoint_hit */
10945 NULL, /* resources_needed */
10946 print_it_catch_exception_unhandled,
10947 print_one_catch_exception_unhandled,
10948 print_mention_catch_exception_unhandled,
10949 print_recreate_catch_exception_unhandled
10950 };
10951
10952 /* Virtual table for "catch assert" breakpoints. */
10953
10954 static enum print_stop_action
10955 print_it_catch_assert (struct breakpoint *b)
10956 {
10957 return print_it_exception (ex_catch_assert, b);
10958 }
10959
10960 static void
10961 print_one_catch_assert (struct breakpoint *b, struct bp_location **last_loc)
10962 {
10963 print_one_exception (ex_catch_assert, b, last_loc);
10964 }
10965
10966 static void
10967 print_mention_catch_assert (struct breakpoint *b)
10968 {
10969 print_mention_exception (ex_catch_assert, b);
10970 }
10971
10972 static void
10973 print_recreate_catch_assert (struct breakpoint *b, struct ui_file *fp)
10974 {
10975 print_recreate_exception (ex_catch_assert, b, fp);
10976 }
10977
10978 static struct breakpoint_ops catch_assert_breakpoint_ops = {
10979 NULL, /* insert */
10980 NULL, /* remove */
10981 NULL, /* breakpoint_hit */
10982 NULL, /* resources_needed */
10983 print_it_catch_assert,
10984 print_one_catch_assert,
10985 print_mention_catch_assert,
10986 print_recreate_catch_assert
10987 };
10988
10989 /* Return non-zero if B is an Ada exception catchpoint. */
10990
10991 int
10992 ada_exception_catchpoint_p (struct breakpoint *b)
10993 {
10994 return (b->ops == &catch_exception_breakpoint_ops
10995 || b->ops == &catch_exception_unhandled_breakpoint_ops
10996 || b->ops == &catch_assert_breakpoint_ops);
10997 }
10998
10999 /* Return a newly allocated copy of the first space-separated token
11000 in ARGSP, and then adjust ARGSP to point immediately after that
11001 token.
11002
11003 Return NULL if ARGPS does not contain any more tokens. */
11004
11005 static char *
11006 ada_get_next_arg (char **argsp)
11007 {
11008 char *args = *argsp;
11009 char *end;
11010 char *result;
11011
11012 /* Skip any leading white space. */
11013
11014 while (isspace (*args))
11015 args++;
11016
11017 if (args[0] == '\0')
11018 return NULL; /* No more arguments. */
11019
11020 /* Find the end of the current argument. */
11021
11022 end = args;
11023 while (*end != '\0' && !isspace (*end))
11024 end++;
11025
11026 /* Adjust ARGSP to point to the start of the next argument. */
11027
11028 *argsp = end;
11029
11030 /* Make a copy of the current argument and return it. */
11031
11032 result = xmalloc (end - args + 1);
11033 strncpy (result, args, end - args);
11034 result[end - args] = '\0';
11035
11036 return result;
11037 }
11038
11039 /* Split the arguments specified in a "catch exception" command.
11040 Set EX to the appropriate catchpoint type.
11041 Set EXP_STRING to the name of the specific exception if
11042 specified by the user. */
11043
11044 static void
11045 catch_ada_exception_command_split (char *args,
11046 enum exception_catchpoint_kind *ex,
11047 char **exp_string)
11048 {
11049 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
11050 char *exception_name;
11051
11052 exception_name = ada_get_next_arg (&args);
11053 make_cleanup (xfree, exception_name);
11054
11055 /* Check that we do not have any more arguments. Anything else
11056 is unexpected. */
11057
11058 while (isspace (*args))
11059 args++;
11060
11061 if (args[0] != '\0')
11062 error (_("Junk at end of expression"));
11063
11064 discard_cleanups (old_chain);
11065
11066 if (exception_name == NULL)
11067 {
11068 /* Catch all exceptions. */
11069 *ex = ex_catch_exception;
11070 *exp_string = NULL;
11071 }
11072 else if (strcmp (exception_name, "unhandled") == 0)
11073 {
11074 /* Catch unhandled exceptions. */
11075 *ex = ex_catch_exception_unhandled;
11076 *exp_string = NULL;
11077 }
11078 else
11079 {
11080 /* Catch a specific exception. */
11081 *ex = ex_catch_exception;
11082 *exp_string = exception_name;
11083 }
11084 }
11085
11086 /* Return the name of the symbol on which we should break in order to
11087 implement a catchpoint of the EX kind. */
11088
11089 static const char *
11090 ada_exception_sym_name (enum exception_catchpoint_kind ex)
11091 {
11092 gdb_assert (exception_info != NULL);
11093
11094 switch (ex)
11095 {
11096 case ex_catch_exception:
11097 return (exception_info->catch_exception_sym);
11098 break;
11099 case ex_catch_exception_unhandled:
11100 return (exception_info->catch_exception_unhandled_sym);
11101 break;
11102 case ex_catch_assert:
11103 return (exception_info->catch_assert_sym);
11104 break;
11105 default:
11106 internal_error (__FILE__, __LINE__,
11107 _("unexpected catchpoint kind (%d)"), ex);
11108 }
11109 }
11110
11111 /* Return the breakpoint ops "virtual table" used for catchpoints
11112 of the EX kind. */
11113
11114 static struct breakpoint_ops *
11115 ada_exception_breakpoint_ops (enum exception_catchpoint_kind ex)
11116 {
11117 switch (ex)
11118 {
11119 case ex_catch_exception:
11120 return (&catch_exception_breakpoint_ops);
11121 break;
11122 case ex_catch_exception_unhandled:
11123 return (&catch_exception_unhandled_breakpoint_ops);
11124 break;
11125 case ex_catch_assert:
11126 return (&catch_assert_breakpoint_ops);
11127 break;
11128 default:
11129 internal_error (__FILE__, __LINE__,
11130 _("unexpected catchpoint kind (%d)"), ex);
11131 }
11132 }
11133
11134 /* Return the condition that will be used to match the current exception
11135 being raised with the exception that the user wants to catch. This
11136 assumes that this condition is used when the inferior just triggered
11137 an exception catchpoint.
11138
11139 The string returned is a newly allocated string that needs to be
11140 deallocated later. */
11141
11142 static char *
11143 ada_exception_catchpoint_cond_string (const char *exp_string)
11144 {
11145 int i;
11146
11147 /* The standard exceptions are a special case. They are defined in
11148 runtime units that have been compiled without debugging info; if
11149 EXP_STRING is the not-fully-qualified name of a standard
11150 exception (e.g. "constraint_error") then, during the evaluation
11151 of the condition expression, the symbol lookup on this name would
11152 *not* return this standard exception. The catchpoint condition
11153 may then be set only on user-defined exceptions which have the
11154 same not-fully-qualified name (e.g. my_package.constraint_error).
11155
11156 To avoid this unexcepted behavior, these standard exceptions are
11157 systematically prefixed by "standard". This means that "catch
11158 exception constraint_error" is rewritten into "catch exception
11159 standard.constraint_error".
11160
11161 If an exception named contraint_error is defined in another package of
11162 the inferior program, then the only way to specify this exception as a
11163 breakpoint condition is to use its fully-qualified named:
11164 e.g. my_package.constraint_error. */
11165
11166 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
11167 {
11168 if (strcmp (standard_exc [i], exp_string) == 0)
11169 {
11170 return xstrprintf ("long_integer (e) = long_integer (&standard.%s)",
11171 exp_string);
11172 }
11173 }
11174 return xstrprintf ("long_integer (e) = long_integer (&%s)", exp_string);
11175 }
11176
11177 /* Return the expression corresponding to COND_STRING evaluated at SAL. */
11178
11179 static struct expression *
11180 ada_parse_catchpoint_condition (char *cond_string,
11181 struct symtab_and_line sal)
11182 {
11183 return (parse_exp_1 (&cond_string, block_for_pc (sal.pc), 0));
11184 }
11185
11186 /* Return the symtab_and_line that should be used to insert an exception
11187 catchpoint of the TYPE kind.
11188
11189 EX_STRING should contain the name of a specific exception
11190 that the catchpoint should catch, or NULL otherwise.
11191
11192 The idea behind all the remaining parameters is that their names match
11193 the name of certain fields in the breakpoint structure that are used to
11194 handle exception catchpoints. This function returns the value to which
11195 these fields should be set, depending on the type of catchpoint we need
11196 to create.
11197
11198 If COND and COND_STRING are both non-NULL, any value they might
11199 hold will be free'ed, and then replaced by newly allocated ones.
11200 These parameters are left untouched otherwise. */
11201
11202 static struct symtab_and_line
11203 ada_exception_sal (enum exception_catchpoint_kind ex, char *exp_string,
11204 char **addr_string, char **cond_string,
11205 struct expression **cond, struct breakpoint_ops **ops)
11206 {
11207 const char *sym_name;
11208 struct symbol *sym;
11209 struct symtab_and_line sal;
11210
11211 /* First, find out which exception support info to use. */
11212 ada_exception_support_info_sniffer ();
11213
11214 /* Then lookup the function on which we will break in order to catch
11215 the Ada exceptions requested by the user. */
11216
11217 sym_name = ada_exception_sym_name (ex);
11218 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
11219
11220 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11221 that should be compiled with debugging information. As a result, we
11222 expect to find that symbol in the symtabs. If we don't find it, then
11223 the target most likely does not support Ada exceptions, or we cannot
11224 insert exception breakpoints yet, because the GNAT runtime hasn't been
11225 loaded yet. */
11226
11227 /* brobecker/2006-12-26: It is conceivable that the runtime was compiled
11228 in such a way that no debugging information is produced for the symbol
11229 we are looking for. In this case, we could search the minimal symbols
11230 as a fall-back mechanism. This would still be operating in degraded
11231 mode, however, as we would still be missing the debugging information
11232 that is needed in order to extract the name of the exception being
11233 raised (this name is printed in the catchpoint message, and is also
11234 used when trying to catch a specific exception). We do not handle
11235 this case for now. */
11236
11237 if (sym == NULL)
11238 error (_("Unable to break on '%s' in this configuration."), sym_name);
11239
11240 /* Make sure that the symbol we found corresponds to a function. */
11241 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11242 error (_("Symbol \"%s\" is not a function (class = %d)"),
11243 sym_name, SYMBOL_CLASS (sym));
11244
11245 sal = find_function_start_sal (sym, 1);
11246
11247 /* Set ADDR_STRING. */
11248
11249 *addr_string = xstrdup (sym_name);
11250
11251 /* Set the COND and COND_STRING (if not NULL). */
11252
11253 if (cond_string != NULL && cond != NULL)
11254 {
11255 if (*cond_string != NULL)
11256 {
11257 xfree (*cond_string);
11258 *cond_string = NULL;
11259 }
11260 if (*cond != NULL)
11261 {
11262 xfree (*cond);
11263 *cond = NULL;
11264 }
11265 if (exp_string != NULL)
11266 {
11267 *cond_string = ada_exception_catchpoint_cond_string (exp_string);
11268 *cond = ada_parse_catchpoint_condition (*cond_string, sal);
11269 }
11270 }
11271
11272 /* Set OPS. */
11273 *ops = ada_exception_breakpoint_ops (ex);
11274
11275 return sal;
11276 }
11277
11278 /* Parse the arguments (ARGS) of the "catch exception" command.
11279
11280 Set TYPE to the appropriate exception catchpoint type.
11281 If the user asked the catchpoint to catch only a specific
11282 exception, then save the exception name in ADDR_STRING.
11283
11284 See ada_exception_sal for a description of all the remaining
11285 function arguments of this function. */
11286
11287 struct symtab_and_line
11288 ada_decode_exception_location (char *args, char **addr_string,
11289 char **exp_string, char **cond_string,
11290 struct expression **cond,
11291 struct breakpoint_ops **ops)
11292 {
11293 enum exception_catchpoint_kind ex;
11294
11295 catch_ada_exception_command_split (args, &ex, exp_string);
11296 return ada_exception_sal (ex, *exp_string, addr_string, cond_string,
11297 cond, ops);
11298 }
11299
11300 struct symtab_and_line
11301 ada_decode_assert_location (char *args, char **addr_string,
11302 struct breakpoint_ops **ops)
11303 {
11304 /* Check that no argument where provided at the end of the command. */
11305
11306 if (args != NULL)
11307 {
11308 while (isspace (*args))
11309 args++;
11310 if (*args != '\0')
11311 error (_("Junk at end of arguments."));
11312 }
11313
11314 return ada_exception_sal (ex_catch_assert, NULL, addr_string, NULL, NULL,
11315 ops);
11316 }
11317
11318 /* Operators */
11319 /* Information about operators given special treatment in functions
11320 below. */
11321 /* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
11322
11323 #define ADA_OPERATORS \
11324 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
11325 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
11326 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
11327 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
11328 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
11329 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
11330 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
11331 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
11332 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
11333 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
11334 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
11335 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
11336 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
11337 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
11338 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
11339 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
11340 OP_DEFN (OP_OTHERS, 1, 1, 0) \
11341 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
11342 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
11343
11344 static void
11345 ada_operator_length (const struct expression *exp, int pc, int *oplenp,
11346 int *argsp)
11347 {
11348 switch (exp->elts[pc - 1].opcode)
11349 {
11350 default:
11351 operator_length_standard (exp, pc, oplenp, argsp);
11352 break;
11353
11354 #define OP_DEFN(op, len, args, binop) \
11355 case op: *oplenp = len; *argsp = args; break;
11356 ADA_OPERATORS;
11357 #undef OP_DEFN
11358
11359 case OP_AGGREGATE:
11360 *oplenp = 3;
11361 *argsp = longest_to_int (exp->elts[pc - 2].longconst);
11362 break;
11363
11364 case OP_CHOICES:
11365 *oplenp = 3;
11366 *argsp = longest_to_int (exp->elts[pc - 2].longconst) + 1;
11367 break;
11368 }
11369 }
11370
11371 /* Implementation of the exp_descriptor method operator_check. */
11372
11373 static int
11374 ada_operator_check (struct expression *exp, int pos,
11375 int (*objfile_func) (struct objfile *objfile, void *data),
11376 void *data)
11377 {
11378 const union exp_element *const elts = exp->elts;
11379 struct type *type = NULL;
11380
11381 switch (elts[pos].opcode)
11382 {
11383 case UNOP_IN_RANGE:
11384 case UNOP_QUAL:
11385 type = elts[pos + 1].type;
11386 break;
11387
11388 default:
11389 return operator_check_standard (exp, pos, objfile_func, data);
11390 }
11391
11392 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
11393
11394 if (type && TYPE_OBJFILE (type)
11395 && (*objfile_func) (TYPE_OBJFILE (type), data))
11396 return 1;
11397
11398 return 0;
11399 }
11400
11401 static char *
11402 ada_op_name (enum exp_opcode opcode)
11403 {
11404 switch (opcode)
11405 {
11406 default:
11407 return op_name_standard (opcode);
11408
11409 #define OP_DEFN(op, len, args, binop) case op: return #op;
11410 ADA_OPERATORS;
11411 #undef OP_DEFN
11412
11413 case OP_AGGREGATE:
11414 return "OP_AGGREGATE";
11415 case OP_CHOICES:
11416 return "OP_CHOICES";
11417 case OP_NAME:
11418 return "OP_NAME";
11419 }
11420 }
11421
11422 /* As for operator_length, but assumes PC is pointing at the first
11423 element of the operator, and gives meaningful results only for the
11424 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
11425
11426 static void
11427 ada_forward_operator_length (struct expression *exp, int pc,
11428 int *oplenp, int *argsp)
11429 {
11430 switch (exp->elts[pc].opcode)
11431 {
11432 default:
11433 *oplenp = *argsp = 0;
11434 break;
11435
11436 #define OP_DEFN(op, len, args, binop) \
11437 case op: *oplenp = len; *argsp = args; break;
11438 ADA_OPERATORS;
11439 #undef OP_DEFN
11440
11441 case OP_AGGREGATE:
11442 *oplenp = 3;
11443 *argsp = longest_to_int (exp->elts[pc + 1].longconst);
11444 break;
11445
11446 case OP_CHOICES:
11447 *oplenp = 3;
11448 *argsp = longest_to_int (exp->elts[pc + 1].longconst) + 1;
11449 break;
11450
11451 case OP_STRING:
11452 case OP_NAME:
11453 {
11454 int len = longest_to_int (exp->elts[pc + 1].longconst);
11455
11456 *oplenp = 4 + BYTES_TO_EXP_ELEM (len + 1);
11457 *argsp = 0;
11458 break;
11459 }
11460 }
11461 }
11462
11463 static int
11464 ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
11465 {
11466 enum exp_opcode op = exp->elts[elt].opcode;
11467 int oplen, nargs;
11468 int pc = elt;
11469 int i;
11470
11471 ada_forward_operator_length (exp, elt, &oplen, &nargs);
11472
11473 switch (op)
11474 {
11475 /* Ada attributes ('Foo). */
11476 case OP_ATR_FIRST:
11477 case OP_ATR_LAST:
11478 case OP_ATR_LENGTH:
11479 case OP_ATR_IMAGE:
11480 case OP_ATR_MAX:
11481 case OP_ATR_MIN:
11482 case OP_ATR_MODULUS:
11483 case OP_ATR_POS:
11484 case OP_ATR_SIZE:
11485 case OP_ATR_TAG:
11486 case OP_ATR_VAL:
11487 break;
11488
11489 case UNOP_IN_RANGE:
11490 case UNOP_QUAL:
11491 /* XXX: gdb_sprint_host_address, type_sprint */
11492 fprintf_filtered (stream, _("Type @"));
11493 gdb_print_host_address (exp->elts[pc + 1].type, stream);
11494 fprintf_filtered (stream, " (");
11495 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
11496 fprintf_filtered (stream, ")");
11497 break;
11498 case BINOP_IN_BOUNDS:
11499 fprintf_filtered (stream, " (%d)",
11500 longest_to_int (exp->elts[pc + 2].longconst));
11501 break;
11502 case TERNOP_IN_RANGE:
11503 break;
11504
11505 case OP_AGGREGATE:
11506 case OP_OTHERS:
11507 case OP_DISCRETE_RANGE:
11508 case OP_POSITIONAL:
11509 case OP_CHOICES:
11510 break;
11511
11512 case OP_NAME:
11513 case OP_STRING:
11514 {
11515 char *name = &exp->elts[elt + 2].string;
11516 int len = longest_to_int (exp->elts[elt + 1].longconst);
11517
11518 fprintf_filtered (stream, "Text: `%.*s'", len, name);
11519 break;
11520 }
11521
11522 default:
11523 return dump_subexp_body_standard (exp, stream, elt);
11524 }
11525
11526 elt += oplen;
11527 for (i = 0; i < nargs; i += 1)
11528 elt = dump_subexp (exp, stream, elt);
11529
11530 return elt;
11531 }
11532
11533 /* The Ada extension of print_subexp (q.v.). */
11534
11535 static void
11536 ada_print_subexp (struct expression *exp, int *pos,
11537 struct ui_file *stream, enum precedence prec)
11538 {
11539 int oplen, nargs, i;
11540 int pc = *pos;
11541 enum exp_opcode op = exp->elts[pc].opcode;
11542
11543 ada_forward_operator_length (exp, pc, &oplen, &nargs);
11544
11545 *pos += oplen;
11546 switch (op)
11547 {
11548 default:
11549 *pos -= oplen;
11550 print_subexp_standard (exp, pos, stream, prec);
11551 return;
11552
11553 case OP_VAR_VALUE:
11554 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
11555 return;
11556
11557 case BINOP_IN_BOUNDS:
11558 /* XXX: sprint_subexp */
11559 print_subexp (exp, pos, stream, PREC_SUFFIX);
11560 fputs_filtered (" in ", stream);
11561 print_subexp (exp, pos, stream, PREC_SUFFIX);
11562 fputs_filtered ("'range", stream);
11563 if (exp->elts[pc + 1].longconst > 1)
11564 fprintf_filtered (stream, "(%ld)",
11565 (long) exp->elts[pc + 1].longconst);
11566 return;
11567
11568 case TERNOP_IN_RANGE:
11569 if (prec >= PREC_EQUAL)
11570 fputs_filtered ("(", stream);
11571 /* XXX: sprint_subexp */
11572 print_subexp (exp, pos, stream, PREC_SUFFIX);
11573 fputs_filtered (" in ", stream);
11574 print_subexp (exp, pos, stream, PREC_EQUAL);
11575 fputs_filtered (" .. ", stream);
11576 print_subexp (exp, pos, stream, PREC_EQUAL);
11577 if (prec >= PREC_EQUAL)
11578 fputs_filtered (")", stream);
11579 return;
11580
11581 case OP_ATR_FIRST:
11582 case OP_ATR_LAST:
11583 case OP_ATR_LENGTH:
11584 case OP_ATR_IMAGE:
11585 case OP_ATR_MAX:
11586 case OP_ATR_MIN:
11587 case OP_ATR_MODULUS:
11588 case OP_ATR_POS:
11589 case OP_ATR_SIZE:
11590 case OP_ATR_TAG:
11591 case OP_ATR_VAL:
11592 if (exp->elts[*pos].opcode == OP_TYPE)
11593 {
11594 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
11595 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0);
11596 *pos += 3;
11597 }
11598 else
11599 print_subexp (exp, pos, stream, PREC_SUFFIX);
11600 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
11601 if (nargs > 1)
11602 {
11603 int tem;
11604
11605 for (tem = 1; tem < nargs; tem += 1)
11606 {
11607 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
11608 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
11609 }
11610 fputs_filtered (")", stream);
11611 }
11612 return;
11613
11614 case UNOP_QUAL:
11615 type_print (exp->elts[pc + 1].type, "", stream, 0);
11616 fputs_filtered ("'(", stream);
11617 print_subexp (exp, pos, stream, PREC_PREFIX);
11618 fputs_filtered (")", stream);
11619 return;
11620
11621 case UNOP_IN_RANGE:
11622 /* XXX: sprint_subexp */
11623 print_subexp (exp, pos, stream, PREC_SUFFIX);
11624 fputs_filtered (" in ", stream);
11625 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0);
11626 return;
11627
11628 case OP_DISCRETE_RANGE:
11629 print_subexp (exp, pos, stream, PREC_SUFFIX);
11630 fputs_filtered ("..", stream);
11631 print_subexp (exp, pos, stream, PREC_SUFFIX);
11632 return;
11633
11634 case OP_OTHERS:
11635 fputs_filtered ("others => ", stream);
11636 print_subexp (exp, pos, stream, PREC_SUFFIX);
11637 return;
11638
11639 case OP_CHOICES:
11640 for (i = 0; i < nargs-1; i += 1)
11641 {
11642 if (i > 0)
11643 fputs_filtered ("|", stream);
11644 print_subexp (exp, pos, stream, PREC_SUFFIX);
11645 }
11646 fputs_filtered (" => ", stream);
11647 print_subexp (exp, pos, stream, PREC_SUFFIX);
11648 return;
11649
11650 case OP_POSITIONAL:
11651 print_subexp (exp, pos, stream, PREC_SUFFIX);
11652 return;
11653
11654 case OP_AGGREGATE:
11655 fputs_filtered ("(", stream);
11656 for (i = 0; i < nargs; i += 1)
11657 {
11658 if (i > 0)
11659 fputs_filtered (", ", stream);
11660 print_subexp (exp, pos, stream, PREC_SUFFIX);
11661 }
11662 fputs_filtered (")", stream);
11663 return;
11664 }
11665 }
11666
11667 /* Table mapping opcodes into strings for printing operators
11668 and precedences of the operators. */
11669
11670 static const struct op_print ada_op_print_tab[] = {
11671 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
11672 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
11673 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
11674 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
11675 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
11676 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
11677 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
11678 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
11679 {"<=", BINOP_LEQ, PREC_ORDER, 0},
11680 {">=", BINOP_GEQ, PREC_ORDER, 0},
11681 {">", BINOP_GTR, PREC_ORDER, 0},
11682 {"<", BINOP_LESS, PREC_ORDER, 0},
11683 {">>", BINOP_RSH, PREC_SHIFT, 0},
11684 {"<<", BINOP_LSH, PREC_SHIFT, 0},
11685 {"+", BINOP_ADD, PREC_ADD, 0},
11686 {"-", BINOP_SUB, PREC_ADD, 0},
11687 {"&", BINOP_CONCAT, PREC_ADD, 0},
11688 {"*", BINOP_MUL, PREC_MUL, 0},
11689 {"/", BINOP_DIV, PREC_MUL, 0},
11690 {"rem", BINOP_REM, PREC_MUL, 0},
11691 {"mod", BINOP_MOD, PREC_MUL, 0},
11692 {"**", BINOP_EXP, PREC_REPEAT, 0},
11693 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
11694 {"-", UNOP_NEG, PREC_PREFIX, 0},
11695 {"+", UNOP_PLUS, PREC_PREFIX, 0},
11696 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
11697 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
11698 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
11699 {".all", UNOP_IND, PREC_SUFFIX, 1},
11700 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
11701 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
11702 {NULL, 0, 0, 0}
11703 };
11704 \f
11705 enum ada_primitive_types {
11706 ada_primitive_type_int,
11707 ada_primitive_type_long,
11708 ada_primitive_type_short,
11709 ada_primitive_type_char,
11710 ada_primitive_type_float,
11711 ada_primitive_type_double,
11712 ada_primitive_type_void,
11713 ada_primitive_type_long_long,
11714 ada_primitive_type_long_double,
11715 ada_primitive_type_natural,
11716 ada_primitive_type_positive,
11717 ada_primitive_type_system_address,
11718 nr_ada_primitive_types
11719 };
11720
11721 static void
11722 ada_language_arch_info (struct gdbarch *gdbarch,
11723 struct language_arch_info *lai)
11724 {
11725 const struct builtin_type *builtin = builtin_type (gdbarch);
11726
11727 lai->primitive_type_vector
11728 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_ada_primitive_types + 1,
11729 struct type *);
11730
11731 lai->primitive_type_vector [ada_primitive_type_int]
11732 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11733 0, "integer");
11734 lai->primitive_type_vector [ada_primitive_type_long]
11735 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
11736 0, "long_integer");
11737 lai->primitive_type_vector [ada_primitive_type_short]
11738 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
11739 0, "short_integer");
11740 lai->string_char_type
11741 = lai->primitive_type_vector [ada_primitive_type_char]
11742 = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
11743 lai->primitive_type_vector [ada_primitive_type_float]
11744 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
11745 "float", NULL);
11746 lai->primitive_type_vector [ada_primitive_type_double]
11747 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11748 "long_float", NULL);
11749 lai->primitive_type_vector [ada_primitive_type_long_long]
11750 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
11751 0, "long_long_integer");
11752 lai->primitive_type_vector [ada_primitive_type_long_double]
11753 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11754 "long_long_float", NULL);
11755 lai->primitive_type_vector [ada_primitive_type_natural]
11756 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11757 0, "natural");
11758 lai->primitive_type_vector [ada_primitive_type_positive]
11759 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11760 0, "positive");
11761 lai->primitive_type_vector [ada_primitive_type_void]
11762 = builtin->builtin_void;
11763
11764 lai->primitive_type_vector [ada_primitive_type_system_address]
11765 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"));
11766 TYPE_NAME (lai->primitive_type_vector [ada_primitive_type_system_address])
11767 = "system__address";
11768
11769 lai->bool_type_symbol = NULL;
11770 lai->bool_type_default = builtin->builtin_bool;
11771 }
11772 \f
11773 /* Language vector */
11774
11775 /* Not really used, but needed in the ada_language_defn. */
11776
11777 static void
11778 emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
11779 {
11780 ada_emit_char (c, type, stream, quoter, 1);
11781 }
11782
11783 static int
11784 parse (void)
11785 {
11786 warnings_issued = 0;
11787 return ada_parse ();
11788 }
11789
11790 static const struct exp_descriptor ada_exp_descriptor = {
11791 ada_print_subexp,
11792 ada_operator_length,
11793 ada_operator_check,
11794 ada_op_name,
11795 ada_dump_subexp_body,
11796 ada_evaluate_subexp
11797 };
11798
11799 const struct language_defn ada_language_defn = {
11800 "ada", /* Language name */
11801 language_ada,
11802 range_check_off,
11803 type_check_off,
11804 case_sensitive_on, /* Yes, Ada is case-insensitive, but
11805 that's not quite what this means. */
11806 array_row_major,
11807 macro_expansion_no,
11808 &ada_exp_descriptor,
11809 parse,
11810 ada_error,
11811 resolve,
11812 ada_printchar, /* Print a character constant */
11813 ada_printstr, /* Function to print string constant */
11814 emit_char, /* Function to print single char (not used) */
11815 ada_print_type, /* Print a type using appropriate syntax */
11816 ada_print_typedef, /* Print a typedef using appropriate syntax */
11817 ada_val_print, /* Print a value using appropriate syntax */
11818 ada_value_print, /* Print a top-level value */
11819 NULL, /* Language specific skip_trampoline */
11820 NULL, /* name_of_this */
11821 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
11822 basic_lookup_transparent_type, /* lookup_transparent_type */
11823 ada_la_decode, /* Language specific symbol demangler */
11824 NULL, /* Language specific
11825 class_name_from_physname */
11826 ada_op_print_tab, /* expression operators for printing */
11827 0, /* c-style arrays */
11828 1, /* String lower bound */
11829 ada_get_gdb_completer_word_break_characters,
11830 ada_make_symbol_completion_list,
11831 ada_language_arch_info,
11832 ada_print_array_index,
11833 default_pass_by_reference,
11834 c_get_string,
11835 LANG_MAGIC
11836 };
11837
11838 /* Provide a prototype to silence -Wmissing-prototypes. */
11839 extern initialize_file_ftype _initialize_ada_language;
11840
11841 /* Command-list for the "set/show ada" prefix command. */
11842 static struct cmd_list_element *set_ada_list;
11843 static struct cmd_list_element *show_ada_list;
11844
11845 /* Implement the "set ada" prefix command. */
11846
11847 static void
11848 set_ada_command (char *arg, int from_tty)
11849 {
11850 printf_unfiltered (_(\
11851 "\"set ada\" must be followed by the name of a setting.\n"));
11852 help_list (set_ada_list, "set ada ", -1, gdb_stdout);
11853 }
11854
11855 /* Implement the "show ada" prefix command. */
11856
11857 static void
11858 show_ada_command (char *args, int from_tty)
11859 {
11860 cmd_show_list (show_ada_list, from_tty, "");
11861 }
11862
11863 void
11864 _initialize_ada_language (void)
11865 {
11866 add_language (&ada_language_defn);
11867
11868 add_prefix_cmd ("ada", no_class, set_ada_command,
11869 _("Prefix command for changing Ada-specfic settings"),
11870 &set_ada_list, "set ada ", 0, &setlist);
11871
11872 add_prefix_cmd ("ada", no_class, show_ada_command,
11873 _("Generic command for showing Ada-specific settings."),
11874 &show_ada_list, "show ada ", 0, &showlist);
11875
11876 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
11877 &trust_pad_over_xvs, _("\
11878 Enable or disable an optimization trusting PAD types over XVS types"), _("\
11879 Show whether an optimization trusting PAD types over XVS types is activated"),
11880 _("\
11881 This is related to the encoding used by the GNAT compiler. The debugger\n\
11882 should normally trust the contents of PAD types, but certain older versions\n\
11883 of GNAT have a bug that sometimes causes the information in the PAD type\n\
11884 to be incorrect. Turning this setting \"off\" allows the debugger to\n\
11885 work around this bug. It is always safe to turn this option \"off\", but\n\
11886 this incurs a slight performance penalty, so it is recommended to NOT change\n\
11887 this option to \"off\" unless necessary."),
11888 NULL, NULL, &set_ada_list, &show_ada_list);
11889
11890 varsize_limit = 65536;
11891
11892 obstack_init (&symbol_list_obstack);
11893
11894 decoded_names_store = htab_create_alloc
11895 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
11896 NULL, xcalloc, xfree);
11897
11898 observer_attach_executable_changed (ada_executable_changed_observer);
11899
11900 /* Setup per-inferior data. */
11901 observer_attach_inferior_exit (ada_inferior_exit);
11902 ada_inferior_data
11903 = register_inferior_data_with_cleanup (ada_inferior_data_cleanup);
11904 }
This page took 0.275389 seconds and 4 git commands to generate.