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