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