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