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