1 /* $MirOS: src/gnu/usr.bin/binutils/gdb/valops.c,v 1.3 2005/06/05 21:24:26 tg Exp $ */
2 
3 /* Perform non-arithmetic operations on values, for GDB.
4 
5    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
6    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
7    Free Software Foundation, Inc.
8 
9    This file is part of GDB.
10 
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25 
26 #include "defs.h"
27 #include "symtab.h"
28 #include "gdbtypes.h"
29 #include "value.h"
30 #include "frame.h"
31 #include "inferior.h"
32 #include "gdbcore.h"
33 #include "target.h"
34 #include "demangle.h"
35 #include "language.h"
36 #include "gdbcmd.h"
37 #include "regcache.h"
38 #include "cp-abi.h"
39 #include "block.h"
40 #include "infcall.h"
41 #include "dictionary.h"
42 #include "cp-support.h"
43 
44 #include <errno.h>
45 #include "gdb_string.h"
46 #include "gdb_assert.h"
47 #include "cp-support.h"
48 #include "observer.h"
49 
50 extern int overload_debug;
51 /* Local functions.  */
52 
53 static int typecmp (int staticp, int varargs, int nargs,
54 		    struct field t1[], struct value *t2[]);
55 
56 static struct value *search_struct_field (char *, struct value *, int,
57 				      struct type *, int);
58 
59 static struct value *search_struct_method (char *, struct value **,
60 				       struct value **,
61 				       int, int *, struct type *);
62 
63 static int find_oload_champ_namespace (struct type **arg_types, int nargs,
64 				       const char *func_name,
65 				       const char *qualified_name,
66 				       struct symbol ***oload_syms,
67 				       struct badness_vector **oload_champ_bv);
68 
69 static
70 int find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
71 				     const char *func_name,
72 				     const char *qualified_name,
73 				     int namespace_len,
74 				     struct symbol ***oload_syms,
75 				     struct badness_vector **oload_champ_bv,
76 				     int *oload_champ);
77 
78 static int find_oload_champ (struct type **arg_types, int nargs, int method,
79 			     int num_fns,
80 			     struct fn_field *fns_ptr,
81 			     struct symbol **oload_syms,
82 			     struct badness_vector **oload_champ_bv);
83 
84 static int oload_method_static (int method, struct fn_field *fns_ptr,
85 				int index);
86 
87 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
88 
89 static enum
90 oload_classification classify_oload_match (struct badness_vector
91 					   * oload_champ_bv,
92 					   int nargs,
93 					   int static_offset);
94 
95 static int check_field_in (struct type *, const char *);
96 
97 static struct value *value_struct_elt_for_reference (struct type *domain,
98 						     int offset,
99 						     struct type *curtype,
100 						     char *name,
101 						     struct type *intype,
102 						     enum noside noside);
103 
104 static struct value *value_namespace_elt (const struct type *curtype,
105 					  char *name,
106 					  enum noside noside);
107 
108 static struct value *value_maybe_namespace_elt (const struct type *curtype,
109 						char *name,
110 						enum noside noside);
111 
112 static CORE_ADDR allocate_space_in_inferior (int);
113 
114 static struct value *cast_into_complex (struct type *, struct value *);
115 
116 static struct fn_field *find_method_list (struct value ** argp, char *method,
117 					  int offset,
118 					  struct type *type, int *num_fns,
119 					  struct type **basetype,
120 					  int *boffset);
121 
122 void _initialize_valops (void);
123 
124 /* Flag for whether we want to abandon failed expression evals by default.  */
125 
126 #if 0
127 static int auto_abandon = 0;
128 #endif
129 
130 int overload_resolution = 0;
131 static void
show_overload_resolution(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)132 show_overload_resolution (struct ui_file *file, int from_tty,
133 			  struct cmd_list_element *c, const char *value)
134 {
135   fprintf_filtered (file, _("\
136 Overload resolution in evaluating C++ functions is %s.\n"),
137 		    value);
138 }
139 
140 /* Find the address of function name NAME in the inferior.  */
141 
142 struct value *
find_function_in_inferior(const char * name)143 find_function_in_inferior (const char *name)
144 {
145   struct symbol *sym;
146   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
147   if (sym != NULL)
148     {
149       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
150 	{
151 	  error (_("\"%s\" exists in this program but is not a function."),
152 		 name);
153 	}
154       return value_of_variable (sym, NULL);
155     }
156   else
157     {
158       struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
159       if (msymbol != NULL)
160 	{
161 	  struct type *type;
162 	  CORE_ADDR maddr;
163 	  type = lookup_pointer_type (builtin_type_char);
164 	  type = lookup_function_type (type);
165 	  type = lookup_pointer_type (type);
166 	  maddr = SYMBOL_VALUE_ADDRESS (msymbol);
167 	  return value_from_pointer (type, maddr);
168 	}
169       else
170 	{
171 	  if (!target_has_execution)
172 	    error (_("evaluation of this expression requires the target program to be active"));
173 	  else
174 	    error (_("evaluation of this expression requires the program to have a function \"%s\"."), name);
175 	}
176     }
177 }
178 
179 /* Allocate NBYTES of space in the inferior using the inferior's malloc
180    and return a value that is a pointer to the allocated space. */
181 
182 struct value *
value_allocate_space_in_inferior(int len)183 value_allocate_space_in_inferior (int len)
184 {
185   struct value *blocklen;
186   struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
187 
188   blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
189   val = call_function_by_hand (val, 1, &blocklen);
190   if (value_logical_not (val))
191     {
192       if (!target_has_execution)
193 	error (_("No memory available to program now: you need to start the target first"));
194       else
195 	error (_("No memory available to program: call to malloc failed"));
196     }
197   return val;
198 }
199 
200 static CORE_ADDR
allocate_space_in_inferior(int len)201 allocate_space_in_inferior (int len)
202 {
203   return value_as_long (value_allocate_space_in_inferior (len));
204 }
205 
206 /* Cast value ARG2 to type TYPE and return as a value.
207    More general than a C cast: accepts any two types of the same length,
208    and if ARG2 is an lvalue it can be cast into anything at all.  */
209 /* In C++, casts may change pointer or object representations.  */
210 
211 struct value *
value_cast(struct type * type,struct value * arg2)212 value_cast (struct type *type, struct value *arg2)
213 {
214   enum type_code code1;
215   enum type_code code2;
216   int scalar;
217   struct type *type2;
218 
219   int convert_to_boolean = 0;
220 
221   if (value_type (arg2) == type)
222     return arg2;
223 
224   CHECK_TYPEDEF (type);
225   code1 = TYPE_CODE (type);
226   arg2 = coerce_ref (arg2);
227   type2 = check_typedef (value_type (arg2));
228 
229   /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
230      is treated like a cast to (TYPE [N])OBJECT,
231      where N is sizeof(OBJECT)/sizeof(TYPE). */
232   if (code1 == TYPE_CODE_ARRAY)
233     {
234       struct type *element_type = TYPE_TARGET_TYPE (type);
235       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
236       if (element_length > 0
237 	&& TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
238 	{
239 	  struct type *range_type = TYPE_INDEX_TYPE (type);
240 	  int val_length = TYPE_LENGTH (type2);
241 	  LONGEST low_bound, high_bound, new_length;
242 	  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
243 	    low_bound = 0, high_bound = 0;
244 	  new_length = val_length / element_length;
245 	  if (val_length % element_length != 0)
246 	    warning (_("array element type size does not divide object size in cast"));
247 	  /* FIXME-type-allocation: need a way to free this type when we are
248 	     done with it.  */
249 	  range_type = create_range_type ((struct type *) NULL,
250 					  TYPE_TARGET_TYPE (range_type),
251 					  low_bound,
252 					  new_length + low_bound - 1);
253 	  deprecated_set_value_type (arg2, create_array_type ((struct type *) NULL,
254 							      element_type, range_type));
255 	  return arg2;
256 	}
257     }
258 
259   if (current_language->c_style_arrays
260       && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
261     arg2 = value_coerce_array (arg2);
262 
263   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
264     arg2 = value_coerce_function (arg2);
265 
266   type2 = check_typedef (value_type (arg2));
267   code2 = TYPE_CODE (type2);
268 
269   if (code1 == TYPE_CODE_COMPLEX)
270     return cast_into_complex (type, arg2);
271   if (code1 == TYPE_CODE_BOOL)
272     {
273       code1 = TYPE_CODE_INT;
274       convert_to_boolean = 1;
275     }
276   if (code1 == TYPE_CODE_CHAR)
277     code1 = TYPE_CODE_INT;
278   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
279     code2 = TYPE_CODE_INT;
280 
281   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
282 	    || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
283 
284   if (code1 == TYPE_CODE_STRUCT
285       && code2 == TYPE_CODE_STRUCT
286       && TYPE_NAME (type) != 0)
287     {
288       /* Look in the type of the source to see if it contains the
289          type of the target as a superclass.  If so, we'll need to
290          offset the object in addition to changing its type.  */
291       struct value *v = search_struct_field (type_name_no_tag (type),
292 					 arg2, 0, type2, 1);
293       if (v)
294 	{
295 	  deprecated_set_value_type (v, type);
296 	  return v;
297 	}
298     }
299   if (code1 == TYPE_CODE_FLT && scalar)
300     return value_from_double (type, value_as_double (arg2));
301   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
302 	    || code1 == TYPE_CODE_RANGE)
303 	   && (scalar || code2 == TYPE_CODE_PTR))
304     {
305       LONGEST longest;
306 
307       if (deprecated_hp_som_som_object_present	/* if target compiled by HP aCC */
308 	  && (code2 == TYPE_CODE_PTR))
309 	{
310 	  unsigned int *ptr;
311 	  struct value *retvalp;
312 
313 	  switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
314 	    {
315 	      /* With HP aCC, pointers to data members have a bias */
316 	    case TYPE_CODE_MEMBER:
317 	      retvalp = value_from_longest (type, value_as_long (arg2));
318 	      /* force evaluation */
319 	      ptr = (unsigned int *) value_contents (retvalp);
320 	      *ptr &= ~0x20000000;	/* zap 29th bit to remove bias */
321 	      return retvalp;
322 
323 	      /* While pointers to methods don't really point to a function */
324 	    case TYPE_CODE_METHOD:
325 	      error (_("Pointers to methods not supported with HP aCC"));
326 
327 	    default:
328 	      break;		/* fall out and go to normal handling */
329 	    }
330 	}
331 
332       /* When we cast pointers to integers, we mustn't use
333          POINTER_TO_ADDRESS to find the address the pointer
334          represents, as value_as_long would.  GDB should evaluate
335          expressions just as the compiler would --- and the compiler
336          sees a cast as a simple reinterpretation of the pointer's
337          bits.  */
338       if (code2 == TYPE_CODE_PTR)
339         longest = extract_unsigned_integer (value_contents (arg2),
340                                             TYPE_LENGTH (type2));
341       else
342         longest = value_as_long (arg2);
343       return value_from_longest (type, convert_to_boolean ?
344 				 (LONGEST) (longest ? 1 : 0) : longest);
345     }
346   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT  ||
347 				      code2 == TYPE_CODE_ENUM ||
348 				      code2 == TYPE_CODE_RANGE))
349     {
350       /* TYPE_LENGTH (type) is the length of a pointer, but we really
351 	 want the length of an address! -- we are really dealing with
352 	 addresses (i.e., gdb representations) not pointers (i.e.,
353 	 target representations) here.
354 
355 	 This allows things like "print *(int *)0x01000234" to work
356 	 without printing a misleading message -- which would
357 	 otherwise occur when dealing with a target having two byte
358 	 pointers and four byte addresses.  */
359 
360       int addr_bit = TARGET_ADDR_BIT;
361 
362       LONGEST longest = value_as_long (arg2);
363       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
364 	{
365 	  if (longest >= ((LONGEST) 1 << addr_bit)
366 	      || longest <= -((LONGEST) 1 << addr_bit))
367 	    warning (_("value truncated"));
368 	}
369       return value_from_longest (type, longest);
370     }
371   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
372     {
373       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
374 	{
375 	  struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
376 	  struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
377 	  if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
378 	      && TYPE_CODE (t2) == TYPE_CODE_STRUCT
379 	      && !value_logical_not (arg2))
380 	    {
381 	      struct value *v;
382 
383 	      /* Look in the type of the source to see if it contains the
384 	         type of the target as a superclass.  If so, we'll need to
385 	         offset the pointer rather than just change its type.  */
386 	      if (TYPE_NAME (t1) != NULL)
387 		{
388 		  v = search_struct_field (type_name_no_tag (t1),
389 					   value_ind (arg2), 0, t2, 1);
390 		  if (v)
391 		    {
392 		      v = value_addr (v);
393 		      deprecated_set_value_type (v, type);
394 		      return v;
395 		    }
396 		}
397 
398 	      /* Look in the type of the target to see if it contains the
399 	         type of the source as a superclass.  If so, we'll need to
400 	         offset the pointer rather than just change its type.
401 	         FIXME: This fails silently with virtual inheritance.  */
402 	      if (TYPE_NAME (t2) != NULL)
403 		{
404 		  v = search_struct_field (type_name_no_tag (t2),
405 				       value_zero (t1, not_lval), 0, t1, 1);
406 		  if (v)
407 		    {
408                       CORE_ADDR addr2 = value_as_address (arg2);
409                       addr2 -= (VALUE_ADDRESS (v)
410                                 + value_offset (v)
411                                 + value_embedded_offset (v));
412                       return value_from_pointer (type, addr2);
413 		    }
414 		}
415 	    }
416 	  /* No superclass found, just fall through to change ptr type.  */
417 	}
418       deprecated_set_value_type (arg2, type);
419       arg2 = value_change_enclosing_type (arg2, type);
420       set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
421       return arg2;
422     }
423   else if (VALUE_LVAL (arg2) == lval_memory)
424     return value_at_lazy (type, VALUE_ADDRESS (arg2) + value_offset (arg2));
425   else if (code1 == TYPE_CODE_VOID)
426     {
427       return value_zero (builtin_type_void, not_lval);
428     }
429   else
430     {
431       error (_("Invalid cast."));
432       return 0;
433     }
434 }
435 
436 /* Create a value of type TYPE that is zero, and return it.  */
437 
438 struct value *
value_zero(struct type * type,enum lval_type lv)439 value_zero (struct type *type, enum lval_type lv)
440 {
441   struct value *val = allocate_value (type);
442   VALUE_LVAL (val) = lv;
443 
444   return val;
445 }
446 
447 /* Return a value with type TYPE located at ADDR.
448 
449    Call value_at only if the data needs to be fetched immediately;
450    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
451    value_at_lazy instead.  value_at_lazy simply records the address of
452    the data and sets the lazy-evaluation-required flag.  The lazy flag
453    is tested in the value_contents macro, which is used if and when
454    the contents are actually required.
455 
456    Note: value_at does *NOT* handle embedded offsets; perform such
457    adjustments before or after calling it. */
458 
459 struct value *
value_at(struct type * type,CORE_ADDR addr)460 value_at (struct type *type, CORE_ADDR addr)
461 {
462   struct value *val;
463 
464   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
465     error (_("Attempt to dereference a generic pointer."));
466 
467   val = allocate_value (type);
468 
469   read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type));
470 
471   VALUE_LVAL (val) = lval_memory;
472   VALUE_ADDRESS (val) = addr;
473 
474   return val;
475 }
476 
477 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
478 
479 struct value *
value_at_lazy(struct type * type,CORE_ADDR addr)480 value_at_lazy (struct type *type, CORE_ADDR addr)
481 {
482   struct value *val;
483 
484   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
485     error (_("Attempt to dereference a generic pointer."));
486 
487   val = allocate_value (type);
488 
489   VALUE_LVAL (val) = lval_memory;
490   VALUE_ADDRESS (val) = addr;
491   set_value_lazy (val, 1);
492 
493   return val;
494 }
495 
496 /* Called only from the value_contents and value_contents_all()
497    macros, if the current data for a variable needs to be loaded into
498    value_contents(VAL).  Fetches the data from the user's process, and
499    clears the lazy flag to indicate that the data in the buffer is
500    valid.
501 
502    If the value is zero-length, we avoid calling read_memory, which would
503    abort.  We mark the value as fetched anyway -- all 0 bytes of it.
504 
505    This function returns a value because it is used in the value_contents
506    macro as part of an expression, where a void would not work.  The
507    value is ignored.  */
508 
509 int
value_fetch_lazy(struct value * val)510 value_fetch_lazy (struct value *val)
511 {
512   CORE_ADDR addr = VALUE_ADDRESS (val) + value_offset (val);
513   int length = TYPE_LENGTH (value_enclosing_type (val));
514 
515   struct type *type = value_type (val);
516   if (length)
517     read_memory (addr, value_contents_all_raw (val), length);
518 
519   set_value_lazy (val, 0);
520   return 0;
521 }
522 
523 
524 /* Store the contents of FROMVAL into the location of TOVAL.
525    Return a new value with the location of TOVAL and contents of FROMVAL.  */
526 
527 struct value *
value_assign(struct value * toval,struct value * fromval)528 value_assign (struct value *toval, struct value *fromval)
529 {
530   struct type *type;
531   struct value *val;
532   struct frame_id old_frame;
533 
534   if (!deprecated_value_modifiable (toval))
535     error (_("Left operand of assignment is not a modifiable lvalue."));
536 
537   toval = coerce_ref (toval);
538 
539   type = value_type (toval);
540   if (VALUE_LVAL (toval) != lval_internalvar)
541     fromval = value_cast (type, fromval);
542   else
543     fromval = coerce_array (fromval);
544   CHECK_TYPEDEF (type);
545 
546   /* Since modifying a register can trash the frame chain, and modifying memory
547      can trash the frame cache, we save the old frame and then restore the new
548      frame afterwards.  */
549   old_frame = get_frame_id (deprecated_selected_frame);
550 
551   switch (VALUE_LVAL (toval))
552     {
553     case lval_internalvar:
554       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
555       val = value_copy (VALUE_INTERNALVAR (toval)->value);
556       val = value_change_enclosing_type (val, value_enclosing_type (fromval));
557       set_value_embedded_offset (val, value_embedded_offset (fromval));
558       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
559       return val;
560 
561     case lval_internalvar_component:
562       set_internalvar_component (VALUE_INTERNALVAR (toval),
563 				 value_offset (toval),
564 				 value_bitpos (toval),
565 				 value_bitsize (toval),
566 				 fromval);
567       break;
568 
569     case lval_memory:
570       {
571 	const gdb_byte *dest_buffer;
572 	CORE_ADDR changed_addr;
573 	int changed_len;
574         gdb_byte buffer[sizeof (LONGEST)];
575 
576 	if (value_bitsize (toval))
577 	  {
578 	    /* We assume that the argument to read_memory is in units of
579 	       host chars.  FIXME:  Is that correct?  */
580 	    changed_len = (value_bitpos (toval)
581 			   + value_bitsize (toval)
582 			   + HOST_CHAR_BIT - 1)
583 	      / HOST_CHAR_BIT;
584 
585 	    if (changed_len > (int) sizeof (LONGEST))
586 	      error (_("Can't handle bitfields which don't fit in a %d bit word."),
587 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
588 
589 	    read_memory (VALUE_ADDRESS (toval) + value_offset (toval),
590 			 buffer, changed_len);
591 	    modify_field (buffer, value_as_long (fromval),
592 			  value_bitpos (toval), value_bitsize (toval));
593 	    changed_addr = VALUE_ADDRESS (toval) + value_offset (toval);
594 	    dest_buffer = buffer;
595 	  }
596 	else
597 	  {
598 	    changed_addr = VALUE_ADDRESS (toval) + value_offset (toval);
599 	    changed_len = TYPE_LENGTH (type);
600 	    dest_buffer = value_contents (fromval);
601 	  }
602 
603 	write_memory (changed_addr, dest_buffer, changed_len);
604 	if (deprecated_memory_changed_hook)
605 	  deprecated_memory_changed_hook (changed_addr, changed_len);
606       }
607       break;
608 
609     case lval_register:
610       {
611 	struct frame_info *frame;
612 	int value_reg;
613 
614 	/* Figure out which frame this is in currently.  */
615 	frame = frame_find_by_id (VALUE_FRAME_ID (toval));
616 	value_reg = VALUE_REGNUM (toval);
617 
618 	if (!frame)
619 	  error (_("Value being assigned to is no longer active."));
620 
621 	if (VALUE_LVAL (toval) == lval_register
622 	    && CONVERT_REGISTER_P (VALUE_REGNUM (toval), type))
623 	  {
624 	    /* If TOVAL is a special machine register requiring
625 	       conversion of program values to a special raw format.  */
626 	    VALUE_TO_REGISTER (frame, VALUE_REGNUM (toval),
627 			       type, value_contents (fromval));
628 	  }
629 	else
630 	  {
631 	    /* TOVAL is stored in a series of registers in the frame
632 	       specified by the structure.  Copy that value out,
633 	       modify it, and copy it back in.  */
634 	    int amount_copied;
635 	    int amount_to_copy;
636 	    gdb_byte *buffer;
637 	    int reg_offset;
638 	    int byte_offset;
639 	    int regno;
640 
641 	    /* Locate the first register that falls in the value that
642 	       needs to be transfered.  Compute the offset of the
643 	       value in that register.  */
644 	    {
645 	      int offset;
646 	      for (reg_offset = value_reg, offset = 0;
647 		   offset + register_size (current_gdbarch, reg_offset) <= value_offset (toval);
648 		   reg_offset++);
649 	      byte_offset = value_offset (toval) - offset;
650 	    }
651 
652 	    /* Compute the number of register aligned values that need
653 	       to be copied.  */
654 	    if (value_bitsize (toval))
655 	      amount_to_copy = byte_offset + 1;
656 	    else
657 	      amount_to_copy = byte_offset + TYPE_LENGTH (type);
658 
659 	    /* And a bounce buffer.  Be slightly over generous.  */
660 	    buffer = alloca (amount_to_copy + MAX_REGISTER_SIZE);
661 
662 	    /* Copy it in.  */
663 	    for (regno = reg_offset, amount_copied = 0;
664 		 amount_copied < amount_to_copy;
665 		 amount_copied += register_size (current_gdbarch, regno), regno++)
666 	      frame_register_read (frame, regno, buffer + amount_copied);
667 
668 	    /* Modify what needs to be modified.  */
669 	    if (value_bitsize (toval))
670 	      modify_field (buffer + byte_offset,
671 			    value_as_long (fromval),
672 			    value_bitpos (toval), value_bitsize (toval));
673 	    else
674 	      memcpy (buffer + byte_offset, value_contents (fromval),
675 		      TYPE_LENGTH (type));
676 
677 	    /* Copy it out.  */
678 	    for (regno = reg_offset, amount_copied = 0;
679 		 amount_copied < amount_to_copy;
680 		 amount_copied += register_size (current_gdbarch, regno), regno++)
681 	      put_frame_register (frame, regno, buffer + amount_copied);
682 
683 	  }
684 	if (deprecated_register_changed_hook)
685 	  deprecated_register_changed_hook (-1);
686 	observer_notify_target_changed (&current_target);
687 	break;
688       }
689 
690     default:
691       error (_("Left operand of assignment is not an lvalue."));
692     }
693 
694   /* Assigning to the stack pointer, frame pointer, and other
695      (architecture and calling convention specific) registers may
696      cause the frame cache to be out of date.  Assigning to memory
697      also can.  We just do this on all assignments to registers or
698      memory, for simplicity's sake; I doubt the slowdown matters.  */
699   switch (VALUE_LVAL (toval))
700     {
701     case lval_memory:
702     case lval_register:
703 
704       reinit_frame_cache ();
705 
706       /* Having destoroyed the frame cache, restore the selected frame.  */
707 
708       /* FIXME: cagney/2002-11-02: There has to be a better way of
709 	 doing this.  Instead of constantly saving/restoring the
710 	 frame.  Why not create a get_selected_frame() function that,
711 	 having saved the selected frame's ID can automatically
712 	 re-find the previously selected frame automatically.  */
713 
714       {
715 	struct frame_info *fi = frame_find_by_id (old_frame);
716 	if (fi != NULL)
717 	  select_frame (fi);
718       }
719 
720       break;
721     default:
722       break;
723     }
724 
725   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
726      If the field is signed, and is negative, then sign extend. */
727   if ((value_bitsize (toval) > 0)
728       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
729     {
730       LONGEST fieldval = value_as_long (fromval);
731       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
732 
733       fieldval &= valmask;
734       if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
735 	fieldval |= ~valmask;
736 
737       fromval = value_from_longest (type, fieldval);
738     }
739 
740   val = value_copy (toval);
741   memcpy (value_contents_raw (val), value_contents (fromval),
742 	  TYPE_LENGTH (type));
743   deprecated_set_value_type (val, type);
744   val = value_change_enclosing_type (val, value_enclosing_type (fromval));
745   set_value_embedded_offset (val, value_embedded_offset (fromval));
746   set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
747 
748   return val;
749 }
750 
751 /* Extend a value VAL to COUNT repetitions of its type.  */
752 
753 struct value *
value_repeat(struct value * arg1,int count)754 value_repeat (struct value *arg1, int count)
755 {
756   struct value *val;
757 
758   if (VALUE_LVAL (arg1) != lval_memory)
759     error (_("Only values in memory can be extended with '@'."));
760   if (count < 1)
761     error (_("Invalid number %d of repetitions."), count);
762 
763   val = allocate_repeat_value (value_enclosing_type (arg1), count);
764 
765   read_memory (VALUE_ADDRESS (arg1) + value_offset (arg1),
766 	       value_contents_all_raw (val),
767 	       TYPE_LENGTH (value_enclosing_type (val)));
768   VALUE_LVAL (val) = lval_memory;
769   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + value_offset (arg1);
770 
771   return val;
772 }
773 
774 struct value *
value_of_variable(struct symbol * var,struct block * b)775 value_of_variable (struct symbol *var, struct block *b)
776 {
777   struct value *val;
778   struct frame_info *frame = NULL;
779 
780   if (!b)
781     frame = NULL;		/* Use selected frame.  */
782   else if (symbol_read_needs_frame (var))
783     {
784       frame = block_innermost_frame (b);
785       if (!frame)
786 	{
787 	  if (BLOCK_FUNCTION (b)
788 	      && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
789 	    error (_("No frame is currently executing in block %s."),
790 		   SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
791 	  else
792 	    error (_("No frame is currently executing in specified block"));
793 	}
794     }
795 
796   val = read_var_value (var, frame);
797   if (!val)
798     error (_("Address of symbol \"%s\" is unknown."), SYMBOL_PRINT_NAME (var));
799 
800   return val;
801 }
802 
803 /* Given a value which is an array, return a value which is a pointer to its
804    first element, regardless of whether or not the array has a nonzero lower
805    bound.
806 
807    FIXME:  A previous comment here indicated that this routine should be
808    substracting the array's lower bound.  It's not clear to me that this
809    is correct.  Given an array subscripting operation, it would certainly
810    work to do the adjustment here, essentially computing:
811 
812    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
813 
814    However I believe a more appropriate and logical place to account for
815    the lower bound is to do so in value_subscript, essentially computing:
816 
817    (&array[0] + ((index - lowerbound) * sizeof array[0]))
818 
819    As further evidence consider what would happen with operations other
820    than array subscripting, where the caller would get back a value that
821    had an address somewhere before the actual first element of the array,
822    and the information about the lower bound would be lost because of
823    the coercion to pointer type.
824  */
825 
826 struct value *
value_coerce_array(struct value * arg1)827 value_coerce_array (struct value *arg1)
828 {
829   struct type *type = check_typedef (value_type (arg1));
830 
831   if (VALUE_LVAL (arg1) != lval_memory)
832     error (_("Attempt to take address of value not located in memory."));
833 
834   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
835 			     (VALUE_ADDRESS (arg1) + value_offset (arg1)));
836 }
837 
838 /* Given a value which is a function, return a value which is a pointer
839    to it.  */
840 
841 struct value *
value_coerce_function(struct value * arg1)842 value_coerce_function (struct value *arg1)
843 {
844   struct value *retval;
845 
846   if (VALUE_LVAL (arg1) != lval_memory)
847     error (_("Attempt to take address of value not located in memory."));
848 
849   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
850 			       (VALUE_ADDRESS (arg1) + value_offset (arg1)));
851   return retval;
852 }
853 
854 /* Return a pointer value for the object for which ARG1 is the contents.  */
855 
856 struct value *
value_addr(struct value * arg1)857 value_addr (struct value *arg1)
858 {
859   struct value *arg2;
860 
861   struct type *type = check_typedef (value_type (arg1));
862   if (TYPE_CODE (type) == TYPE_CODE_REF)
863     {
864       /* Copy the value, but change the type from (T&) to (T*).
865          We keep the same location information, which is efficient,
866          and allows &(&X) to get the location containing the reference. */
867       arg2 = value_copy (arg1);
868       deprecated_set_value_type (arg2, lookup_pointer_type (TYPE_TARGET_TYPE (type)));
869       return arg2;
870     }
871   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
872     return value_coerce_function (arg1);
873 
874   if (VALUE_LVAL (arg1) != lval_memory)
875     error (_("Attempt to take address of value not located in memory."));
876 
877   /* Get target memory address */
878   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
879 			     (VALUE_ADDRESS (arg1)
880 			      + value_offset (arg1)
881 			      + value_embedded_offset (arg1)));
882 
883   /* This may be a pointer to a base subobject; so remember the
884      full derived object's type ... */
885   arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (value_enclosing_type (arg1)));
886   /* ... and also the relative position of the subobject in the full object */
887   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
888   return arg2;
889 }
890 
891 /* Given a value of a pointer type, apply the C unary * operator to it.  */
892 
893 struct value *
value_ind(struct value * arg1)894 value_ind (struct value *arg1)
895 {
896   struct type *base_type;
897   struct value *arg2;
898 
899   arg1 = coerce_array (arg1);
900 
901   base_type = check_typedef (value_type (arg1));
902 
903   if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
904     error (_("not implemented: member types in value_ind"));
905 
906   /* Allow * on an integer so we can cast it to whatever we want.
907      This returns an int, which seems like the most C-like thing
908      to do.  "long long" variables are rare enough that
909      BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
910   if (TYPE_CODE (base_type) == TYPE_CODE_INT)
911     return value_at_lazy (builtin_type_int,
912 			  (CORE_ADDR) value_as_long (arg1));
913   else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
914     {
915       struct type *enc_type;
916       /* We may be pointing to something embedded in a larger object */
917       /* Get the real type of the enclosing object */
918       enc_type = check_typedef (value_enclosing_type (arg1));
919       enc_type = TYPE_TARGET_TYPE (enc_type);
920       /* Retrieve the enclosing object pointed to */
921       arg2 = value_at_lazy (enc_type, (value_as_address (arg1)
922 				       - value_pointed_to_offset (arg1)));
923       /* Re-adjust type */
924       deprecated_set_value_type (arg2, TYPE_TARGET_TYPE (base_type));
925       /* Add embedding info */
926       arg2 = value_change_enclosing_type (arg2, enc_type);
927       set_value_embedded_offset (arg2, value_pointed_to_offset (arg1));
928 
929       /* We may be pointing to an object of some derived type */
930       arg2 = value_full_object (arg2, NULL, 0, 0, 0);
931       return arg2;
932     }
933 
934   error (_("Attempt to take contents of a non-pointer value."));
935   return 0;			/* For lint -- never reached */
936 }
937 
938 /* Create a value for an array by allocating space in the inferior, copying
939    the data into that space, and then setting up an array value.
940 
941    The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
942    populated from the values passed in ELEMVEC.
943 
944    The element type of the array is inherited from the type of the
945    first element, and all elements must have the same size (though we
946    don't currently enforce any restriction on their types). */
947 
948 struct value *
value_array(int lowbound,int highbound,struct value ** elemvec)949 value_array (int lowbound, int highbound, struct value **elemvec)
950 {
951   int nelem;
952   int idx;
953   unsigned int typelength;
954   struct value *val;
955   struct type *rangetype;
956   struct type *arraytype;
957   CORE_ADDR addr;
958 
959   /* Validate that the bounds are reasonable and that each of the elements
960      have the same size. */
961 
962   nelem = highbound - lowbound + 1;
963   if (nelem <= 0)
964     {
965       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
966     }
967   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
968   for (idx = 1; idx < nelem; idx++)
969     {
970       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
971 	{
972 	  error (_("array elements must all be the same size"));
973 	}
974     }
975 
976   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
977 				 lowbound, highbound);
978   arraytype = create_array_type ((struct type *) NULL,
979 			      value_enclosing_type (elemvec[0]), rangetype);
980 
981   if (!current_language->c_style_arrays)
982     {
983       val = allocate_value (arraytype);
984       for (idx = 0; idx < nelem; idx++)
985 	{
986 	  memcpy (value_contents_all_raw (val) + (idx * typelength),
987 		  value_contents_all (elemvec[idx]),
988 		  typelength);
989 	}
990       return val;
991     }
992 
993   /* Allocate space to store the array in the inferior, and then initialize
994      it by copying in each element.  FIXME:  Is it worth it to create a
995      local buffer in which to collect each value and then write all the
996      bytes in one operation? */
997 
998   addr = allocate_space_in_inferior (nelem * typelength);
999   for (idx = 0; idx < nelem; idx++)
1000     {
1001       write_memory (addr + (idx * typelength),
1002 		    value_contents_all (elemvec[idx]),
1003 		    typelength);
1004     }
1005 
1006   /* Create the array type and set up an array value to be evaluated lazily. */
1007 
1008   val = value_at_lazy (arraytype, addr);
1009   return (val);
1010 }
1011 
1012 /* Create a value for a string constant by allocating space in the inferior,
1013    copying the data into that space, and returning the address with type
1014    TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
1015    of characters.
1016    Note that string types are like array of char types with a lower bound of
1017    zero and an upper bound of LEN - 1.  Also note that the string may contain
1018    embedded null bytes. */
1019 
1020 struct value *
value_string(char * ptr,int len)1021 value_string (char *ptr, int len)
1022 {
1023   struct value *val;
1024   int lowbound = current_language->string_lower_bound;
1025   struct type *rangetype = create_range_type ((struct type *) NULL,
1026 					      builtin_type_int,
1027 					      lowbound, len + lowbound - 1);
1028   struct type *stringtype
1029   = create_string_type ((struct type *) NULL, rangetype);
1030   CORE_ADDR addr;
1031 
1032   if (current_language->c_style_arrays == 0)
1033     {
1034       val = allocate_value (stringtype);
1035       memcpy (value_contents_raw (val), ptr, len);
1036       return val;
1037     }
1038 
1039 
1040   /* Allocate space to store the string in the inferior, and then
1041      copy LEN bytes from PTR in gdb to that address in the inferior. */
1042 
1043   addr = allocate_space_in_inferior (len);
1044   write_memory (addr, (gdb_byte *) ptr, len);
1045 
1046   val = value_at_lazy (stringtype, addr);
1047   return (val);
1048 }
1049 
1050 struct value *
value_bitstring(char * ptr,int len)1051 value_bitstring (char *ptr, int len)
1052 {
1053   struct value *val;
1054   struct type *domain_type = create_range_type (NULL, builtin_type_int,
1055 						0, len - 1);
1056   struct type *type = create_set_type ((struct type *) NULL, domain_type);
1057   TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1058   val = allocate_value (type);
1059   memcpy (value_contents_raw (val), ptr, TYPE_LENGTH (type));
1060   return val;
1061 }
1062 
1063 /* See if we can pass arguments in T2 to a function which takes arguments
1064    of types T1.  T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1065    vector.  If some arguments need coercion of some sort, then the coerced
1066    values are written into T2.  Return value is 0 if the arguments could be
1067    matched, or the position at which they differ if not.
1068 
1069    STATICP is nonzero if the T1 argument list came from a
1070    static member function.  T2 will still include the ``this'' pointer,
1071    but it will be skipped.
1072 
1073    For non-static member functions, we ignore the first argument,
1074    which is the type of the instance variable.  This is because we want
1075    to handle calls with objects from derived classes.  This is not
1076    entirely correct: we should actually check to make sure that a
1077    requested operation is type secure, shouldn't we?  FIXME.  */
1078 
1079 static int
typecmp(int staticp,int varargs,int nargs,struct field t1[],struct value * t2[])1080 typecmp (int staticp, int varargs, int nargs,
1081 	 struct field t1[], struct value *t2[])
1082 {
1083   int i;
1084 
1085   if (t2 == 0)
1086     internal_error (__FILE__, __LINE__, _("typecmp: no argument list"));
1087 
1088   /* Skip ``this'' argument if applicable.  T2 will always include THIS.  */
1089   if (staticp)
1090     t2 ++;
1091 
1092   for (i = 0;
1093        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1094        i++)
1095     {
1096       struct type *tt1, *tt2;
1097 
1098       if (!t2[i])
1099 	return i + 1;
1100 
1101       tt1 = check_typedef (t1[i].type);
1102       tt2 = check_typedef (value_type (t2[i]));
1103 
1104       if (TYPE_CODE (tt1) == TYPE_CODE_REF
1105       /* We should be doing hairy argument matching, as below.  */
1106 	  && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1107 	{
1108 	  if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1109 	    t2[i] = value_coerce_array (t2[i]);
1110 	  else
1111 	    t2[i] = value_addr (t2[i]);
1112 	  continue;
1113 	}
1114 
1115       /* djb - 20000715 - Until the new type structure is in the
1116 	 place, and we can attempt things like implicit conversions,
1117 	 we need to do this so you can take something like a map<const
1118 	 char *>, and properly access map["hello"], because the
1119 	 argument to [] will be a reference to a pointer to a char,
1120 	 and the argument will be a pointer to a char. */
1121       while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1122 	      TYPE_CODE (tt1) == TYPE_CODE_PTR)
1123 	{
1124 	  tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1125 	}
1126       while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1127 	      TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1128 	      TYPE_CODE(tt2) == TYPE_CODE_REF)
1129 	{
1130 	  tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
1131 	}
1132       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1133 	continue;
1134       /* Array to pointer is a `trivial conversion' according to the ARM.  */
1135 
1136       /* We should be doing much hairier argument matching (see section 13.2
1137          of the ARM), but as a quick kludge, just check for the same type
1138          code.  */
1139       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
1140 	return i + 1;
1141     }
1142   if (varargs || t2[i] == NULL)
1143     return 0;
1144   return i + 1;
1145 }
1146 
1147 /* Helper function used by value_struct_elt to recurse through baseclasses.
1148    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1149    and search in it assuming it has (class) type TYPE.
1150    If found, return value, else return NULL.
1151 
1152    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1153    look for a baseclass named NAME.  */
1154 
1155 static struct value *
search_struct_field(char * name,struct value * arg1,int offset,struct type * type,int looking_for_baseclass)1156 search_struct_field (char *name, struct value *arg1, int offset,
1157 		     struct type *type, int looking_for_baseclass)
1158 {
1159   int i;
1160   int nbases = TYPE_N_BASECLASSES (type);
1161 
1162   CHECK_TYPEDEF (type);
1163 
1164   if (!looking_for_baseclass)
1165     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1166       {
1167 	char *t_field_name = TYPE_FIELD_NAME (type, i);
1168 
1169 	if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1170 	  {
1171 	    struct value *v;
1172 	    if (TYPE_FIELD_STATIC (type, i))
1173 	      {
1174 		v = value_static_field (type, i);
1175 		if (v == 0)
1176 		  error (_("field %s is nonexistent or has been optimised out"),
1177 			 name);
1178 	      }
1179 	    else
1180 	      {
1181 		v = value_primitive_field (arg1, offset, i, type);
1182 		if (v == 0)
1183 		  error (_("there is no field named %s"), name);
1184 	      }
1185 	    return v;
1186 	  }
1187 
1188 	if (t_field_name
1189 	    && (t_field_name[0] == '\0'
1190 		|| (TYPE_CODE (type) == TYPE_CODE_UNION
1191 		    && (strcmp_iw (t_field_name, "else") == 0))))
1192 	  {
1193 	    struct type *field_type = TYPE_FIELD_TYPE (type, i);
1194 	    if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1195 		|| TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1196 	      {
1197 		/* Look for a match through the fields of an anonymous union,
1198 		   or anonymous struct.  C++ provides anonymous unions.
1199 
1200 		   In the GNU Chill (now deleted from GDB)
1201 		   implementation of variant record types, each
1202 		   <alternative field> has an (anonymous) union type,
1203 		   each member of the union represents a <variant
1204 		   alternative>.  Each <variant alternative> is
1205 		   represented as a struct, with a member for each
1206 		   <variant field>.  */
1207 
1208 		struct value *v;
1209 		int new_offset = offset;
1210 
1211 		/* This is pretty gross.  In G++, the offset in an
1212 		   anonymous union is relative to the beginning of the
1213 		   enclosing struct.  In the GNU Chill (now deleted
1214 		   from GDB) implementation of variant records, the
1215 		   bitpos is zero in an anonymous union field, so we
1216 		   have to add the offset of the union here. */
1217 		if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1218 		    || (TYPE_NFIELDS (field_type) > 0
1219 			&& TYPE_FIELD_BITPOS (field_type, 0) == 0))
1220 		  new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1221 
1222 		v = search_struct_field (name, arg1, new_offset, field_type,
1223 					 looking_for_baseclass);
1224 		if (v)
1225 		  return v;
1226 	      }
1227 	  }
1228       }
1229 
1230   for (i = 0; i < nbases; i++)
1231     {
1232       struct value *v;
1233       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1234       /* If we are looking for baseclasses, this is what we get when we
1235          hit them.  But it could happen that the base part's member name
1236          is not yet filled in.  */
1237       int found_baseclass = (looking_for_baseclass
1238 			     && TYPE_BASECLASS_NAME (type, i) != NULL
1239 			     && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
1240 
1241       if (BASETYPE_VIA_VIRTUAL (type, i))
1242 	{
1243 	  int boffset;
1244 	  struct value *v2 = allocate_value (basetype);
1245 
1246 	  boffset = baseclass_offset (type, i,
1247 				      value_contents (arg1) + offset,
1248 				      VALUE_ADDRESS (arg1)
1249 				      + value_offset (arg1) + offset);
1250 	  if (boffset == -1)
1251 	    error (_("virtual baseclass botch"));
1252 
1253 	  /* The virtual base class pointer might have been clobbered by the
1254 	     user program. Make sure that it still points to a valid memory
1255 	     location.  */
1256 
1257 	  boffset += offset;
1258 	  if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1259 	    {
1260 	      CORE_ADDR base_addr;
1261 
1262 	      base_addr = VALUE_ADDRESS (arg1) + value_offset (arg1) + boffset;
1263 	      if (target_read_memory (base_addr, value_contents_raw (v2),
1264 				      TYPE_LENGTH (basetype)) != 0)
1265 		error (_("virtual baseclass botch"));
1266 	      VALUE_LVAL (v2) = lval_memory;
1267 	      VALUE_ADDRESS (v2) = base_addr;
1268 	    }
1269 	  else
1270 	    {
1271 	      VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1272 	      VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1273 	      VALUE_FRAME_ID (v2) = VALUE_FRAME_ID (arg1);
1274 	      set_value_offset (v2, value_offset (arg1) + boffset);
1275 	      if (value_lazy (arg1))
1276 		set_value_lazy (v2, 1);
1277 	      else
1278 		memcpy (value_contents_raw (v2),
1279 			value_contents_raw (arg1) + boffset,
1280 			TYPE_LENGTH (basetype));
1281 	    }
1282 
1283 	  if (found_baseclass)
1284 	    return v2;
1285 	  v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1286 				   looking_for_baseclass);
1287 	}
1288       else if (found_baseclass)
1289 	v = value_primitive_field (arg1, offset, i, type);
1290       else
1291 	v = search_struct_field (name, arg1,
1292 			       offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1293 				 basetype, looking_for_baseclass);
1294       if (v)
1295 	return v;
1296     }
1297   return NULL;
1298 }
1299 
1300 
1301 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1302  * in an object pointed to by VALADDR (on the host), assumed to be of
1303  * type TYPE.  OFFSET is number of bytes beyond start of ARG to start
1304  * looking (in case VALADDR is the contents of an enclosing object).
1305  *
1306  * This routine recurses on the primary base of the derived class because
1307  * the virtual base entries of the primary base appear before the other
1308  * virtual base entries.
1309  *
1310  * If the virtual base is not found, a negative integer is returned.
1311  * The magnitude of the negative integer is the number of entries in
1312  * the virtual table to skip over (entries corresponding to various
1313  * ancestral classes in the chain of primary bases).
1314  *
1315  * Important: This assumes the HP / Taligent C++ runtime
1316  * conventions. Use baseclass_offset() instead to deal with g++
1317  * conventions.  */
1318 
1319 void
find_rt_vbase_offset(struct type * type,struct type * basetype,const gdb_byte * valaddr,int offset,int * boffset_p,int * skip_p)1320 find_rt_vbase_offset (struct type *type, struct type *basetype,
1321 		      const gdb_byte *valaddr, int offset, int *boffset_p,
1322 		      int *skip_p)
1323 {
1324   int boffset;			/* offset of virtual base */
1325   int index;			/* displacement to use in virtual table */
1326   int skip;
1327 
1328   struct value *vp;
1329   CORE_ADDR vtbl;		/* the virtual table pointer */
1330   struct type *pbc;		/* the primary base class */
1331 
1332   /* Look for the virtual base recursively in the primary base, first.
1333    * This is because the derived class object and its primary base
1334    * subobject share the primary virtual table.  */
1335 
1336   boffset = 0;
1337   pbc = TYPE_PRIMARY_BASE (type);
1338   if (pbc)
1339     {
1340       find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1341       if (skip < 0)
1342 	{
1343 	  *boffset_p = boffset;
1344 	  *skip_p = -1;
1345 	  return;
1346 	}
1347     }
1348   else
1349     skip = 0;
1350 
1351 
1352   /* Find the index of the virtual base according to HP/Taligent
1353      runtime spec. (Depth-first, left-to-right.)  */
1354   index = virtual_base_index_skip_primaries (basetype, type);
1355 
1356   if (index < 0)
1357     {
1358       *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1359       *boffset_p = 0;
1360       return;
1361     }
1362 
1363   /* pai: FIXME -- 32x64 possible problem */
1364   /* First word (4 bytes) in object layout is the vtable pointer */
1365   vtbl = *(CORE_ADDR *) (valaddr + offset);
1366 
1367   /* Before the constructor is invoked, things are usually zero'd out. */
1368   if (vtbl == 0)
1369     error (_("Couldn't find virtual table -- object may not be constructed yet."));
1370 
1371 
1372   /* Find virtual base's offset -- jump over entries for primary base
1373    * ancestors, then use the index computed above.  But also adjust by
1374    * HP_ACC_VBASE_START for the vtable slots before the start of the
1375    * virtual base entries.  Offset is negative -- virtual base entries
1376    * appear _before_ the address point of the virtual table. */
1377 
1378   /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1379      & use long type */
1380 
1381   /* epstein : FIXME -- added param for overlay section. May not be correct */
1382   vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START));
1383   boffset = value_as_long (vp);
1384   *skip_p = -1;
1385   *boffset_p = boffset;
1386   return;
1387 }
1388 
1389 
1390 /* Helper function used by value_struct_elt to recurse through baseclasses.
1391    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1392    and search in it assuming it has (class) type TYPE.
1393    If found, return value, else if name matched and args not return (value)-1,
1394    else return NULL. */
1395 
1396 static struct value *
search_struct_method(char * name,struct value ** arg1p,struct value ** args,int offset,int * static_memfuncp,struct type * type)1397 search_struct_method (char *name, struct value **arg1p,
1398 		      struct value **args, int offset,
1399 		      int *static_memfuncp, struct type *type)
1400 {
1401   int i;
1402   struct value *v;
1403   int name_matched = 0;
1404   char dem_opname[64];
1405 
1406   CHECK_TYPEDEF (type);
1407   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1408     {
1409       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1410       /* FIXME!  May need to check for ARM demangling here */
1411       if (strncmp (t_field_name, "__", 2) == 0 ||
1412 	  strncmp (t_field_name, "op", 2) == 0 ||
1413 	  strncmp (t_field_name, "type", 4) == 0)
1414 	{
1415 	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI, sizeof(dem_opname)))
1416 	    t_field_name = dem_opname;
1417 	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0, sizeof(dem_opname)))
1418 	    t_field_name = dem_opname;
1419 	}
1420       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1421 	{
1422 	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1423 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1424 	  name_matched = 1;
1425 
1426 	  check_stub_method_group (type, i);
1427 	  if (j > 0 && args == 0)
1428 	    error (_("cannot resolve overloaded method `%s': no arguments supplied"), name);
1429 	  else if (j == 0 && args == 0)
1430 	    {
1431 	      v = value_fn_field (arg1p, f, j, type, offset);
1432 	      if (v != NULL)
1433 		return v;
1434 	    }
1435 	  else
1436 	    while (j >= 0)
1437 	      {
1438 		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1439 			      TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1440 			      TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
1441 			      TYPE_FN_FIELD_ARGS (f, j), args))
1442 		  {
1443 		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1444 		      return value_virtual_fn_field (arg1p, f, j, type, offset);
1445 		    if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1446 		      *static_memfuncp = 1;
1447 		    v = value_fn_field (arg1p, f, j, type, offset);
1448 		    if (v != NULL)
1449 		      return v;
1450 		  }
1451 		j--;
1452 	      }
1453 	}
1454     }
1455 
1456   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1457     {
1458       int base_offset;
1459 
1460       if (BASETYPE_VIA_VIRTUAL (type, i))
1461 	{
1462 	  if (TYPE_HAS_VTABLE (type))
1463 	    {
1464 	      /* HP aCC compiled type, search for virtual base offset
1465 	         according to HP/Taligent runtime spec.  */
1466 	      int skip;
1467 	      find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1468 				    value_contents_all (*arg1p),
1469 				    offset + value_embedded_offset (*arg1p),
1470 				    &base_offset, &skip);
1471 	      if (skip >= 0)
1472 		error (_("Virtual base class offset not found in vtable"));
1473 	    }
1474 	  else
1475 	    {
1476 	      struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1477 	      const gdb_byte *base_valaddr;
1478 
1479 	      /* The virtual base class pointer might have been clobbered by the
1480 	         user program. Make sure that it still points to a valid memory
1481 	         location.  */
1482 
1483 	      if (offset < 0 || offset >= TYPE_LENGTH (type))
1484 		{
1485 		  gdb_byte *tmp = alloca (TYPE_LENGTH (baseclass));
1486 		  if (target_read_memory (VALUE_ADDRESS (*arg1p)
1487 					  + value_offset (*arg1p) + offset,
1488 					  tmp, TYPE_LENGTH (baseclass)) != 0)
1489 		    error (_("virtual baseclass botch"));
1490 		  base_valaddr = tmp;
1491 		}
1492 	      else
1493 		base_valaddr = value_contents (*arg1p) + offset;
1494 
1495 	      base_offset =
1496 		baseclass_offset (type, i, base_valaddr,
1497 				  VALUE_ADDRESS (*arg1p)
1498 				  + value_offset (*arg1p) + offset);
1499 	      if (base_offset == -1)
1500 		error (_("virtual baseclass botch"));
1501 	    }
1502 	}
1503       else
1504 	{
1505 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1506 	}
1507       v = search_struct_method (name, arg1p, args, base_offset + offset,
1508 				static_memfuncp, TYPE_BASECLASS (type, i));
1509       if (v == (struct value *) - 1)
1510 	{
1511 	  name_matched = 1;
1512 	}
1513       else if (v)
1514 	{
1515 /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
1516 /*        *arg1p = arg1_tmp; */
1517 	  return v;
1518 	}
1519     }
1520   if (name_matched)
1521     return (struct value *) - 1;
1522   else
1523     return NULL;
1524 }
1525 
1526 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1527    extract the component named NAME from the ultimate target structure/union
1528    and return it as a value with its appropriate type.
1529    ERR is used in the error message if *ARGP's type is wrong.
1530 
1531    C++: ARGS is a list of argument types to aid in the selection of
1532    an appropriate method. Also, handle derived types.
1533 
1534    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1535    where the truthvalue of whether the function that was resolved was
1536    a static member function or not is stored.
1537 
1538    ERR is an error message to be printed in case the field is not found.  */
1539 
1540 struct value *
value_struct_elt(struct value ** argp,struct value ** args,char * name,int * static_memfuncp,char * err)1541 value_struct_elt (struct value **argp, struct value **args,
1542 		  char *name, int *static_memfuncp, char *err)
1543 {
1544   struct type *t;
1545   struct value *v;
1546 
1547   *argp = coerce_array (*argp);
1548 
1549   t = check_typedef (value_type (*argp));
1550 
1551   /* Follow pointers until we get to a non-pointer.  */
1552 
1553   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1554     {
1555       *argp = value_ind (*argp);
1556       /* Don't coerce fn pointer to fn and then back again!  */
1557       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
1558 	*argp = coerce_array (*argp);
1559       t = check_typedef (value_type (*argp));
1560     }
1561 
1562   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1563     error (_("not implemented: member type in value_struct_elt"));
1564 
1565   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1566       && TYPE_CODE (t) != TYPE_CODE_UNION)
1567     error (_("Attempt to extract a component of a value that is not a %s."), err);
1568 
1569   /* Assume it's not, unless we see that it is.  */
1570   if (static_memfuncp)
1571     *static_memfuncp = 0;
1572 
1573   if (!args)
1574     {
1575       /* if there are no arguments ...do this...  */
1576 
1577       /* Try as a field first, because if we succeed, there
1578          is less work to be done.  */
1579       v = search_struct_field (name, *argp, 0, t, 0);
1580       if (v)
1581 	return v;
1582 
1583       /* C++: If it was not found as a data field, then try to
1584          return it as a pointer to a method.  */
1585 
1586       if (destructor_name_p (name, t))
1587 	error (_("Cannot get value of destructor"));
1588 
1589       v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1590 
1591       if (v == (struct value *) - 1)
1592 	error (_("Cannot take address of a method"));
1593       else if (v == 0)
1594 	{
1595 	  if (TYPE_NFN_FIELDS (t))
1596 	    error (_("There is no member or method named %s."), name);
1597 	  else
1598 	    error (_("There is no member named %s."), name);
1599 	}
1600       return v;
1601     }
1602 
1603   if (destructor_name_p (name, t))
1604     {
1605       if (!args[1])
1606 	{
1607 	  /* Destructors are a special case.  */
1608 	  int m_index, f_index;
1609 
1610 	  v = NULL;
1611 	  if (get_destructor_fn_field (t, &m_index, &f_index))
1612 	    {
1613 	      v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1614 				  f_index, NULL, 0);
1615 	    }
1616 	  if (v == NULL)
1617 	    error (_("could not find destructor function named %s."), name);
1618 	  else
1619 	    return v;
1620 	}
1621       else
1622 	{
1623 	  error (_("destructor should not have any argument"));
1624 	}
1625     }
1626   else
1627     v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1628 
1629   if (v == (struct value *) - 1)
1630     {
1631       error (_("One of the arguments you tried to pass to %s could not be converted to what the function wants."), name);
1632     }
1633   else if (v == 0)
1634     {
1635       /* See if user tried to invoke data as function.  If so,
1636          hand it back.  If it's not callable (i.e., a pointer to function),
1637          gdb should give an error.  */
1638       v = search_struct_field (name, *argp, 0, t, 0);
1639     }
1640 
1641   if (!v)
1642     error (_("Structure has no component named %s."), name);
1643   return v;
1644 }
1645 
1646 /* Search through the methods of an object (and its bases)
1647  * to find a specified method. Return the pointer to the
1648  * fn_field list of overloaded instances.
1649  * Helper function for value_find_oload_list.
1650  * ARGP is a pointer to a pointer to a value (the object)
1651  * METHOD is a string containing the method name
1652  * OFFSET is the offset within the value
1653  * TYPE is the assumed type of the object
1654  * NUM_FNS is the number of overloaded instances
1655  * BASETYPE is set to the actual type of the subobject where the method is found
1656  * BOFFSET is the offset of the base subobject where the method is found */
1657 
1658 static struct fn_field *
find_method_list(struct value ** argp,char * method,int offset,struct type * type,int * num_fns,struct type ** basetype,int * boffset)1659 find_method_list (struct value **argp, char *method, int offset,
1660 		  struct type *type, int *num_fns,
1661 		  struct type **basetype, int *boffset)
1662 {
1663   int i;
1664   struct fn_field *f;
1665   CHECK_TYPEDEF (type);
1666 
1667   *num_fns = 0;
1668 
1669   /* First check in object itself */
1670   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1671     {
1672       /* pai: FIXME What about operators and type conversions? */
1673       char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1674       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
1675 	{
1676 	  int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1677 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1678 
1679 	  *num_fns = len;
1680 	  *basetype = type;
1681 	  *boffset = offset;
1682 
1683 	  /* Resolve any stub methods.  */
1684 	  check_stub_method_group (type, i);
1685 
1686 	  return f;
1687 	}
1688     }
1689 
1690   /* Not found in object, check in base subobjects */
1691   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1692     {
1693       int base_offset;
1694       if (BASETYPE_VIA_VIRTUAL (type, i))
1695 	{
1696 	  if (TYPE_HAS_VTABLE (type))
1697 	    {
1698 	      /* HP aCC compiled type, search for virtual base offset
1699 	       * according to HP/Taligent runtime spec.  */
1700 	      int skip;
1701 	      find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1702 				    value_contents_all (*argp),
1703 				    offset + value_embedded_offset (*argp),
1704 				    &base_offset, &skip);
1705 	      if (skip >= 0)
1706 		error (_("Virtual base class offset not found in vtable"));
1707 	    }
1708 	  else
1709 	    {
1710 	      /* probably g++ runtime model */
1711 	      base_offset = value_offset (*argp) + offset;
1712 	      base_offset =
1713 		baseclass_offset (type, i,
1714 				  value_contents (*argp) + base_offset,
1715 				  VALUE_ADDRESS (*argp) + base_offset);
1716 	      if (base_offset == -1)
1717 		error (_("virtual baseclass botch"));
1718 	    }
1719 	}
1720       else
1721 	/* non-virtual base, simply use bit position from debug info */
1722 	{
1723 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1724 	}
1725       f = find_method_list (argp, method, base_offset + offset,
1726 			    TYPE_BASECLASS (type, i), num_fns, basetype,
1727 			    boffset);
1728       if (f)
1729 	return f;
1730     }
1731   return NULL;
1732 }
1733 
1734 /* Return the list of overloaded methods of a specified name.
1735  * ARGP is a pointer to a pointer to a value (the object)
1736  * METHOD is the method name
1737  * OFFSET is the offset within the value contents
1738  * NUM_FNS is the number of overloaded instances
1739  * BASETYPE is set to the type of the base subobject that defines the method
1740  * BOFFSET is the offset of the base subobject which defines the method */
1741 
1742 struct fn_field *
value_find_oload_method_list(struct value ** argp,char * method,int offset,int * num_fns,struct type ** basetype,int * boffset)1743 value_find_oload_method_list (struct value **argp, char *method, int offset,
1744 			      int *num_fns, struct type **basetype,
1745 			      int *boffset)
1746 {
1747   struct type *t;
1748 
1749   t = check_typedef (value_type (*argp));
1750 
1751   /* code snarfed from value_struct_elt */
1752   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1753     {
1754       *argp = value_ind (*argp);
1755       /* Don't coerce fn pointer to fn and then back again!  */
1756       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
1757 	*argp = coerce_array (*argp);
1758       t = check_typedef (value_type (*argp));
1759     }
1760 
1761   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1762     error (_("Not implemented: member type in value_find_oload_lis"));
1763 
1764   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1765       && TYPE_CODE (t) != TYPE_CODE_UNION)
1766     error (_("Attempt to extract a component of a value that is not a struct or union"));
1767 
1768   return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
1769 }
1770 
1771 /* Given an array of argument types (ARGTYPES) (which includes an
1772    entry for "this" in the case of C++ methods), the number of
1773    arguments NARGS, the NAME of a function whether it's a method or
1774    not (METHOD), and the degree of laxness (LAX) in conforming to
1775    overload resolution rules in ANSI C++, find the best function that
1776    matches on the argument types according to the overload resolution
1777    rules.
1778 
1779    In the case of class methods, the parameter OBJ is an object value
1780    in which to search for overloaded methods.
1781 
1782    In the case of non-method functions, the parameter FSYM is a symbol
1783    corresponding to one of the overloaded functions.
1784 
1785    Return value is an integer: 0 -> good match, 10 -> debugger applied
1786    non-standard coercions, 100 -> incompatible.
1787 
1788    If a method is being searched for, VALP will hold the value.
1789    If a non-method is being searched for, SYMP will hold the symbol for it.
1790 
1791    If a method is being searched for, and it is a static method,
1792    then STATICP will point to a non-zero value.
1793 
1794    Note: This function does *not* check the value of
1795    overload_resolution.  Caller must check it to see whether overload
1796    resolution is permitted.
1797  */
1798 
1799 int
find_overload_match(struct type ** arg_types,int nargs,char * name,int method,int lax,struct value ** objp,struct symbol * fsym,struct value ** valp,struct symbol ** symp,int * staticp)1800 find_overload_match (struct type **arg_types, int nargs, char *name, int method,
1801 		     int lax, struct value **objp, struct symbol *fsym,
1802 		     struct value **valp, struct symbol **symp, int *staticp)
1803 {
1804   struct value *obj = (objp ? *objp : NULL);
1805 
1806   int oload_champ;		/* Index of best overloaded function */
1807 
1808   struct badness_vector *oload_champ_bv = NULL;		/* The measure for the current best match */
1809 
1810   struct value *temp = obj;
1811   struct fn_field *fns_ptr = NULL;	/* For methods, the list of overloaded methods */
1812   struct symbol **oload_syms = NULL;	/* For non-methods, the list of overloaded function symbols */
1813   int num_fns = 0;		/* Number of overloaded instances being considered */
1814   struct type *basetype = NULL;
1815   int boffset;
1816   int ix;
1817   int static_offset;
1818   struct cleanup *old_cleanups = NULL;
1819 
1820   const char *obj_type_name = NULL;
1821   char *func_name = NULL;
1822   enum oload_classification match_quality;
1823 
1824   /* Get the list of overloaded methods or functions */
1825   if (method)
1826     {
1827       obj_type_name = TYPE_NAME (value_type (obj));
1828       /* Hack: evaluate_subexp_standard often passes in a pointer
1829          value rather than the object itself, so try again */
1830       if ((!obj_type_name || !*obj_type_name) &&
1831 	  (TYPE_CODE (value_type (obj)) == TYPE_CODE_PTR))
1832 	obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj)));
1833 
1834       fns_ptr = value_find_oload_method_list (&temp, name, 0,
1835 					      &num_fns,
1836 					      &basetype, &boffset);
1837       if (!fns_ptr || !num_fns)
1838 	error (_("Couldn't find method %s%s%s"),
1839 	       obj_type_name,
1840 	       (obj_type_name && *obj_type_name) ? "::" : "",
1841 	       name);
1842       /* If we are dealing with stub method types, they should have
1843 	 been resolved by find_method_list via value_find_oload_method_list
1844 	 above.  */
1845       gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1846       oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1847 				      fns_ptr, oload_syms, &oload_champ_bv);
1848     }
1849   else
1850     {
1851       const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1852       func_name	= cp_func_name (qualified_name);
1853 
1854       /* If the name is NULL this must be a C-style function.
1855          Just return the same symbol. */
1856       if (func_name == NULL)
1857         {
1858 	  *symp = fsym;
1859           return 0;
1860         }
1861 
1862       old_cleanups = make_cleanup (xfree, func_name);
1863       make_cleanup (xfree, oload_syms);
1864       make_cleanup (xfree, oload_champ_bv);
1865 
1866       oload_champ = find_oload_champ_namespace (arg_types, nargs,
1867 						func_name,
1868 						qualified_name,
1869 						&oload_syms,
1870 						&oload_champ_bv);
1871     }
1872 
1873   /* Check how bad the best match is.  */
1874 
1875   match_quality
1876     = classify_oload_match (oload_champ_bv, nargs,
1877 			    oload_method_static (method, fns_ptr,
1878 						 oload_champ));
1879 
1880   if (match_quality == INCOMPATIBLE)
1881     {
1882       if (method)
1883 	error (_("Cannot resolve method %s%s%s to any overloaded instance"),
1884 	       obj_type_name,
1885 	       (obj_type_name && *obj_type_name) ? "::" : "",
1886 	       name);
1887       else
1888 	error (_("Cannot resolve function %s to any overloaded instance"),
1889 	       func_name);
1890     }
1891   else if (match_quality == NON_STANDARD)
1892     {
1893       if (method)
1894 	warning (_("Using non-standard conversion to match method %s%s%s to supplied arguments"),
1895 		 obj_type_name,
1896 		 (obj_type_name && *obj_type_name) ? "::" : "",
1897 		 name);
1898       else
1899 	warning (_("Using non-standard conversion to match function %s to supplied arguments"),
1900 		 func_name);
1901     }
1902 
1903   if (method)
1904     {
1905       if (staticp != NULL)
1906 	*staticp = oload_method_static (method, fns_ptr, oload_champ);
1907       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
1908 	*valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1909       else
1910 	*valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1911     }
1912   else
1913     {
1914       *symp = oload_syms[oload_champ];
1915     }
1916 
1917   if (objp)
1918     {
1919       if (TYPE_CODE (value_type (temp)) != TYPE_CODE_PTR
1920 	  && TYPE_CODE (value_type (*objp)) == TYPE_CODE_PTR)
1921 	{
1922 	  temp = value_addr (temp);
1923 	}
1924       *objp = temp;
1925     }
1926   if (old_cleanups != NULL)
1927     do_cleanups (old_cleanups);
1928 
1929   switch (match_quality)
1930     {
1931     case INCOMPATIBLE:
1932       return 100;
1933     case NON_STANDARD:
1934       return 10;
1935     default:				/* STANDARD */
1936       return 0;
1937     }
1938 }
1939 
1940 /* Find the best overload match, searching for FUNC_NAME in namespaces
1941    contained in QUALIFIED_NAME until it either finds a good match or
1942    runs out of namespaces.  It stores the overloaded functions in
1943    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
1944    calling function is responsible for freeing *OLOAD_SYMS and
1945    *OLOAD_CHAMP_BV.  */
1946 
1947 static int
find_oload_champ_namespace(struct type ** arg_types,int nargs,const char * func_name,const char * qualified_name,struct symbol *** oload_syms,struct badness_vector ** oload_champ_bv)1948 find_oload_champ_namespace (struct type **arg_types, int nargs,
1949 			    const char *func_name,
1950 			    const char *qualified_name,
1951 			    struct symbol ***oload_syms,
1952 			    struct badness_vector **oload_champ_bv)
1953 {
1954   int oload_champ;
1955 
1956   find_oload_champ_namespace_loop (arg_types, nargs,
1957 				   func_name,
1958 				   qualified_name, 0,
1959 				   oload_syms, oload_champ_bv,
1960 				   &oload_champ);
1961 
1962   return oload_champ;
1963 }
1964 
1965 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
1966    how deep we've looked for namespaces, and the champ is stored in
1967    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
1968    if it isn't.
1969 
1970    It is the caller's responsibility to free *OLOAD_SYMS and
1971    *OLOAD_CHAMP_BV.  */
1972 
1973 static int
find_oload_champ_namespace_loop(struct type ** arg_types,int nargs,const char * func_name,const char * qualified_name,int namespace_len,struct symbol *** oload_syms,struct badness_vector ** oload_champ_bv,int * oload_champ)1974 find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
1975 				 const char *func_name,
1976 				 const char *qualified_name,
1977 				 int namespace_len,
1978 				 struct symbol ***oload_syms,
1979 				 struct badness_vector **oload_champ_bv,
1980 				 int *oload_champ)
1981 {
1982   int next_namespace_len = namespace_len;
1983   int searched_deeper = 0;
1984   int num_fns = 0;
1985   struct cleanup *old_cleanups;
1986   int new_oload_champ;
1987   struct symbol **new_oload_syms;
1988   struct badness_vector *new_oload_champ_bv;
1989   char *new_namespace;
1990 
1991   if (next_namespace_len != 0)
1992     {
1993       gdb_assert (qualified_name[next_namespace_len] == ':');
1994       next_namespace_len +=  2;
1995     }
1996   next_namespace_len
1997     += cp_find_first_component (qualified_name + next_namespace_len);
1998 
1999   /* Initialize these to values that can safely be xfree'd.  */
2000   *oload_syms = NULL;
2001   *oload_champ_bv = NULL;
2002 
2003   /* First, see if we have a deeper namespace we can search in.  If we
2004      get a good match there, use it.  */
2005 
2006   if (qualified_name[next_namespace_len] == ':')
2007     {
2008       searched_deeper = 1;
2009 
2010       if (find_oload_champ_namespace_loop (arg_types, nargs,
2011 					   func_name, qualified_name,
2012 					   next_namespace_len,
2013 					   oload_syms, oload_champ_bv,
2014 					   oload_champ))
2015 	{
2016 	  return 1;
2017 	}
2018     };
2019 
2020   /* If we reach here, either we're in the deepest namespace or we
2021      didn't find a good match in a deeper namespace.  But, in the
2022      latter case, we still have a bad match in a deeper namespace;
2023      note that we might not find any match at all in the current
2024      namespace.  (There's always a match in the deepest namespace,
2025      because this overload mechanism only gets called if there's a
2026      function symbol to start off with.)  */
2027 
2028   old_cleanups = make_cleanup (xfree, *oload_syms);
2029   old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2030   new_namespace = alloca (namespace_len + 1);
2031   strncpy (new_namespace, qualified_name, namespace_len);
2032   new_namespace[namespace_len] = '\0';
2033   new_oload_syms = make_symbol_overload_list (func_name,
2034 					      new_namespace);
2035   while (new_oload_syms[num_fns])
2036     ++num_fns;
2037 
2038   new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2039 				      NULL, new_oload_syms,
2040 				      &new_oload_champ_bv);
2041 
2042   /* Case 1: We found a good match.  Free earlier matches (if any),
2043      and return it.  Case 2: We didn't find a good match, but we're
2044      not the deepest function.  Then go with the bad match that the
2045      deeper function found.  Case 3: We found a bad match, and we're
2046      the deepest function.  Then return what we found, even though
2047      it's a bad match.  */
2048 
2049   if (new_oload_champ != -1
2050       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2051     {
2052       *oload_syms = new_oload_syms;
2053       *oload_champ = new_oload_champ;
2054       *oload_champ_bv = new_oload_champ_bv;
2055       do_cleanups (old_cleanups);
2056       return 1;
2057     }
2058   else if (searched_deeper)
2059     {
2060       xfree (new_oload_syms);
2061       xfree (new_oload_champ_bv);
2062       discard_cleanups (old_cleanups);
2063       return 0;
2064     }
2065   else
2066     {
2067       gdb_assert (new_oload_champ != -1);
2068       *oload_syms = new_oload_syms;
2069       *oload_champ = new_oload_champ;
2070       *oload_champ_bv = new_oload_champ_bv;
2071       discard_cleanups (old_cleanups);
2072       return 0;
2073     }
2074 }
2075 
2076 /* Look for a function to take NARGS args of types ARG_TYPES.  Find
2077    the best match from among the overloaded methods or functions
2078    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2079    The number of methods/functions in the list is given by NUM_FNS.
2080    Return the index of the best match; store an indication of the
2081    quality of the match in OLOAD_CHAMP_BV.
2082 
2083    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
2084 
2085 static int
find_oload_champ(struct type ** arg_types,int nargs,int method,int num_fns,struct fn_field * fns_ptr,struct symbol ** oload_syms,struct badness_vector ** oload_champ_bv)2086 find_oload_champ (struct type **arg_types, int nargs, int method,
2087 		  int num_fns, struct fn_field *fns_ptr,
2088 		  struct symbol **oload_syms,
2089 		  struct badness_vector **oload_champ_bv)
2090 {
2091   int ix;
2092   struct badness_vector *bv;	/* A measure of how good an overloaded instance is */
2093   int oload_champ = -1;		/* Index of best overloaded function */
2094   int oload_ambiguous = 0;	/* Current ambiguity state for overload resolution */
2095   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2096 
2097   *oload_champ_bv = NULL;
2098 
2099   /* Consider each candidate in turn */
2100   for (ix = 0; ix < num_fns; ix++)
2101     {
2102       int jj;
2103       int static_offset = oload_method_static (method, fns_ptr, ix);
2104       int nparms;
2105       struct type **parm_types;
2106 
2107       if (method)
2108 	{
2109 	  nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
2110 	}
2111       else
2112 	{
2113 	  /* If it's not a method, this is the proper place */
2114 	  nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2115 	}
2116 
2117       /* Prepare array of parameter types */
2118       parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2119       for (jj = 0; jj < nparms; jj++)
2120 	parm_types[jj] = (method
2121 			  ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
2122 			  : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
2123 
2124       /* Compare parameter types to supplied argument types.  Skip THIS for
2125          static methods.  */
2126       bv = rank_function (parm_types, nparms, arg_types + static_offset,
2127 			  nargs - static_offset);
2128 
2129       if (!*oload_champ_bv)
2130 	{
2131 	  *oload_champ_bv = bv;
2132 	  oload_champ = 0;
2133 	}
2134       else
2135 	/* See whether current candidate is better or worse than previous best */
2136 	switch (compare_badness (bv, *oload_champ_bv))
2137 	  {
2138 	  case 0:
2139 	    oload_ambiguous = 1;	/* top two contenders are equally good */
2140 	    break;
2141 	  case 1:
2142 	    oload_ambiguous = 2;	/* incomparable top contenders */
2143 	    break;
2144 	  case 2:
2145 	    *oload_champ_bv = bv;	/* new champion, record details */
2146 	    oload_ambiguous = 0;
2147 	    oload_champ = ix;
2148 	    break;
2149 	  case 3:
2150 	  default:
2151 	    break;
2152 	  }
2153       xfree (parm_types);
2154       if (overload_debug)
2155 	{
2156 	  if (method)
2157 	    fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2158 	  else
2159 	    fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2160 	  for (jj = 0; jj < nargs - static_offset; jj++)
2161 	    fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2162 	  fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2163 	}
2164     }
2165 
2166   return oload_champ;
2167 }
2168 
2169 /* Return 1 if we're looking at a static method, 0 if we're looking at
2170    a non-static method or a function that isn't a method.  */
2171 
2172 static int
oload_method_static(int method,struct fn_field * fns_ptr,int index)2173 oload_method_static (int method, struct fn_field *fns_ptr, int index)
2174 {
2175   if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2176     return 1;
2177   else
2178     return 0;
2179 }
2180 
2181 /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
2182 
2183 static enum oload_classification
classify_oload_match(struct badness_vector * oload_champ_bv,int nargs,int static_offset)2184 classify_oload_match (struct badness_vector *oload_champ_bv,
2185 		      int nargs,
2186 		      int static_offset)
2187 {
2188   int ix;
2189 
2190   for (ix = 1; ix <= nargs - static_offset; ix++)
2191     {
2192       if (oload_champ_bv->rank[ix] >= 100)
2193 	return INCOMPATIBLE;	/* truly mismatched types */
2194       else if (oload_champ_bv->rank[ix] >= 10)
2195 	return NON_STANDARD;	/* non-standard type conversions needed */
2196     }
2197 
2198   return STANDARD;		/* Only standard conversions needed.  */
2199 }
2200 
2201 /* C++: return 1 is NAME is a legitimate name for the destructor
2202    of type TYPE.  If TYPE does not have a destructor, or
2203    if NAME is inappropriate for TYPE, an error is signaled.  */
2204 int
destructor_name_p(const char * name,const struct type * type)2205 destructor_name_p (const char *name, const struct type *type)
2206 {
2207   /* destructors are a special case.  */
2208 
2209   if (name[0] == '~')
2210     {
2211       char *dname = type_name_no_tag (type);
2212       char *cp = strchr (dname, '<');
2213       unsigned int len;
2214 
2215       /* Do not compare the template part for template classes.  */
2216       if (cp == NULL)
2217 	len = strlen (dname);
2218       else
2219 	len = cp - dname;
2220       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
2221 	error (_("name of destructor must equal name of class"));
2222       else
2223 	return 1;
2224     }
2225   return 0;
2226 }
2227 
2228 /* Helper function for check_field: Given TYPE, a structure/union,
2229    return 1 if the component named NAME from the ultimate
2230    target structure/union is defined, otherwise, return 0. */
2231 
2232 static int
check_field_in(struct type * type,const char * name)2233 check_field_in (struct type *type, const char *name)
2234 {
2235   int i;
2236 
2237   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2238     {
2239       char *t_field_name = TYPE_FIELD_NAME (type, i);
2240       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2241 	return 1;
2242     }
2243 
2244   /* C++: If it was not found as a data field, then try to
2245      return it as a pointer to a method.  */
2246 
2247   /* Destructors are a special case.  */
2248   if (destructor_name_p (name, type))
2249     {
2250       int m_index, f_index;
2251 
2252       return get_destructor_fn_field (type, &m_index, &f_index);
2253     }
2254 
2255   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2256     {
2257       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2258 	return 1;
2259     }
2260 
2261   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2262     if (check_field_in (TYPE_BASECLASS (type, i), name))
2263       return 1;
2264 
2265   return 0;
2266 }
2267 
2268 
2269 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2270    return 1 if the component named NAME from the ultimate
2271    target structure/union is defined, otherwise, return 0.  */
2272 
2273 int
check_field(struct value * arg1,const char * name)2274 check_field (struct value *arg1, const char *name)
2275 {
2276   struct type *t;
2277 
2278   arg1 = coerce_array (arg1);
2279 
2280   t = value_type (arg1);
2281 
2282   /* Follow pointers until we get to a non-pointer.  */
2283 
2284   for (;;)
2285     {
2286       CHECK_TYPEDEF (t);
2287       if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2288 	break;
2289       t = TYPE_TARGET_TYPE (t);
2290     }
2291 
2292   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2293     error (_("not implemented: member type in check_field"));
2294 
2295   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2296       && TYPE_CODE (t) != TYPE_CODE_UNION)
2297     error (_("Internal error: `this' is not an aggregate"));
2298 
2299   return check_field_in (t, name);
2300 }
2301 
2302 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2303    return the appropriate member.  This function is used to resolve
2304    user expressions of the form "DOMAIN::NAME".  For more details on
2305    what happens, see the comment before
2306    value_struct_elt_for_reference.  */
2307 
2308 struct value *
value_aggregate_elt(struct type * curtype,char * name,enum noside noside)2309 value_aggregate_elt (struct type *curtype,
2310 		     char *name,
2311 		     enum noside noside)
2312 {
2313   switch (TYPE_CODE (curtype))
2314     {
2315     case TYPE_CODE_STRUCT:
2316     case TYPE_CODE_UNION:
2317       return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2318 					     noside);
2319     case TYPE_CODE_NAMESPACE:
2320       return value_namespace_elt (curtype, name, noside);
2321     default:
2322       internal_error (__FILE__, __LINE__,
2323 		      _("non-aggregate type in value_aggregate_elt"));
2324     }
2325 }
2326 
2327 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2328    return the address of this member as a "pointer to member"
2329    type.  If INTYPE is non-null, then it will be the type
2330    of the member we are looking for.  This will help us resolve
2331    "pointers to member functions".  This function is used
2332    to resolve user expressions of the form "DOMAIN::NAME".  */
2333 
2334 static struct value *
value_struct_elt_for_reference(struct type * domain,int offset,struct type * curtype,char * name,struct type * intype,enum noside noside)2335 value_struct_elt_for_reference (struct type *domain, int offset,
2336 				struct type *curtype, char *name,
2337 				struct type *intype,
2338 				enum noside noside)
2339 {
2340   struct type *t = curtype;
2341   int i;
2342   struct value *v;
2343 
2344   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2345       && TYPE_CODE (t) != TYPE_CODE_UNION)
2346     error (_("Internal error: non-aggregate type to value_struct_elt_for_reference"));
2347 
2348   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2349     {
2350       char *t_field_name = TYPE_FIELD_NAME (t, i);
2351 
2352       if (t_field_name && strcmp (t_field_name, name) == 0)
2353 	{
2354 	  if (TYPE_FIELD_STATIC (t, i))
2355 	    {
2356 	      v = value_static_field (t, i);
2357 	      if (v == NULL)
2358 		error (_("static field %s has been optimized out"),
2359 		       name);
2360 	      return v;
2361 	    }
2362 	  if (TYPE_FIELD_PACKED (t, i))
2363 	    error (_("pointers to bitfield members not allowed"));
2364 
2365 	  return value_from_longest
2366 	    (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2367 							domain)),
2368 	     offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2369 	}
2370     }
2371 
2372   /* C++: If it was not found as a data field, then try to
2373      return it as a pointer to a method.  */
2374 
2375   /* Destructors are a special case.  */
2376   if (destructor_name_p (name, t))
2377     {
2378       error (_("member pointers to destructors not implemented yet"));
2379     }
2380 
2381   /* Perform all necessary dereferencing.  */
2382   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2383     intype = TYPE_TARGET_TYPE (intype);
2384 
2385   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2386     {
2387       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2388       char dem_opname[64];
2389 
2390       if (strncmp (t_field_name, "__", 2) == 0 ||
2391 	  strncmp (t_field_name, "op", 2) == 0 ||
2392 	  strncmp (t_field_name, "type", 4) == 0)
2393 	{
2394 	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI, sizeof(dem_opname)))
2395 	    t_field_name = dem_opname;
2396 	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0, sizeof(dem_opname)))
2397 	    t_field_name = dem_opname;
2398 	}
2399       if (t_field_name && strcmp (t_field_name, name) == 0)
2400 	{
2401 	  int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2402 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2403 
2404 	  check_stub_method_group (t, i);
2405 
2406 	  if (intype == 0 && j > 1)
2407 	    error (_("non-unique member `%s' requires type instantiation"), name);
2408 	  if (intype)
2409 	    {
2410 	      while (j--)
2411 		if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2412 		  break;
2413 	      if (j < 0)
2414 		error (_("no member function matches that type instantiation"));
2415 	    }
2416 	  else
2417 	    j = 0;
2418 
2419 	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2420 	    {
2421 	      return value_from_longest
2422 		(lookup_reference_type
2423 		 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2424 				      domain)),
2425 		 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2426 	    }
2427 	  else
2428 	    {
2429 	      struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2430 						0, VAR_DOMAIN, 0, NULL);
2431 	      if (s == NULL)
2432 		{
2433 		  v = 0;
2434 		}
2435 	      else
2436 		{
2437 		  v = read_var_value (s, 0);
2438 #if 0
2439 		  VALUE_TYPE (v) = lookup_reference_type
2440 		    (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2441 					 domain));
2442 #endif
2443 		}
2444 	      return v;
2445 	    }
2446 	}
2447     }
2448   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2449     {
2450       struct value *v;
2451       int base_offset;
2452 
2453       if (BASETYPE_VIA_VIRTUAL (t, i))
2454 	base_offset = 0;
2455       else
2456 	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2457       v = value_struct_elt_for_reference (domain,
2458 					  offset + base_offset,
2459 					  TYPE_BASECLASS (t, i),
2460 					  name,
2461 					  intype,
2462 					  noside);
2463       if (v)
2464 	return v;
2465     }
2466 
2467   /* As a last chance, pretend that CURTYPE is a namespace, and look
2468      it up that way; this (frequently) works for types nested inside
2469      classes.  */
2470 
2471   return value_maybe_namespace_elt (curtype, name, noside);
2472 }
2473 
2474 /* C++: Return the member NAME of the namespace given by the type
2475    CURTYPE.  */
2476 
2477 static struct value *
value_namespace_elt(const struct type * curtype,char * name,enum noside noside)2478 value_namespace_elt (const struct type *curtype,
2479 		     char *name,
2480 		     enum noside noside)
2481 {
2482   struct value *retval = value_maybe_namespace_elt (curtype, name,
2483 						    noside);
2484 
2485   if (retval == NULL)
2486     error (_("No symbol \"%s\" in namespace \"%s\"."), name,
2487 	   TYPE_TAG_NAME (curtype));
2488 
2489   return retval;
2490 }
2491 
2492 /* A helper function used by value_namespace_elt and
2493    value_struct_elt_for_reference.  It looks up NAME inside the
2494    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2495    is a class and NAME refers to a type in CURTYPE itself (as opposed
2496    to, say, some base class of CURTYPE).  */
2497 
2498 static struct value *
value_maybe_namespace_elt(const struct type * curtype,char * name,enum noside noside)2499 value_maybe_namespace_elt (const struct type *curtype,
2500 			   char *name,
2501 			   enum noside noside)
2502 {
2503   const char *namespace_name = TYPE_TAG_NAME (curtype);
2504   struct symbol *sym;
2505 
2506   sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2507 				    get_selected_block (0), VAR_DOMAIN,
2508 				    NULL);
2509 
2510   if (sym == NULL)
2511     return NULL;
2512   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2513 	   && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2514     return allocate_value (SYMBOL_TYPE (sym));
2515   else
2516     return value_of_variable (sym, get_selected_block (0));
2517 }
2518 
2519 /* Given a pointer value V, find the real (RTTI) type
2520    of the object it points to.
2521    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2522    and refer to the values computed for the object pointed to. */
2523 
2524 struct type *
value_rtti_target_type(struct value * v,int * full,int * top,int * using_enc)2525 value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
2526 {
2527   struct value *target;
2528 
2529   target = value_ind (v);
2530 
2531   return value_rtti_type (target, full, top, using_enc);
2532 }
2533 
2534 /* Given a value pointed to by ARGP, check its real run-time type, and
2535    if that is different from the enclosing type, create a new value
2536    using the real run-time type as the enclosing type (and of the same
2537    type as ARGP) and return it, with the embedded offset adjusted to
2538    be the correct offset to the enclosed object
2539    RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2540    parameters, computed by value_rtti_type(). If these are available,
2541    they can be supplied and a second call to value_rtti_type() is avoided.
2542    (Pass RTYPE == NULL if they're not available */
2543 
2544 struct value *
value_full_object(struct value * argp,struct type * rtype,int xfull,int xtop,int xusing_enc)2545 value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
2546 		   int xusing_enc)
2547 {
2548   struct type *real_type;
2549   int full = 0;
2550   int top = -1;
2551   int using_enc = 0;
2552   struct value *new_val;
2553 
2554   if (rtype)
2555     {
2556       real_type = rtype;
2557       full = xfull;
2558       top = xtop;
2559       using_enc = xusing_enc;
2560     }
2561   else
2562     real_type = value_rtti_type (argp, &full, &top, &using_enc);
2563 
2564   /* If no RTTI data, or if object is already complete, do nothing */
2565   if (!real_type || real_type == value_enclosing_type (argp))
2566     return argp;
2567 
2568   /* If we have the full object, but for some reason the enclosing
2569      type is wrong, set it *//* pai: FIXME -- sounds iffy */
2570   if (full)
2571     {
2572       argp = value_change_enclosing_type (argp, real_type);
2573       return argp;
2574     }
2575 
2576   /* Check if object is in memory */
2577   if (VALUE_LVAL (argp) != lval_memory)
2578     {
2579       warning (_("Couldn't retrieve complete object of RTTI type %s; object may be in register(s)."), TYPE_NAME (real_type));
2580 
2581       return argp;
2582     }
2583 
2584   /* All other cases -- retrieve the complete object */
2585   /* Go back by the computed top_offset from the beginning of the object,
2586      adjusting for the embedded offset of argp if that's what value_rtti_type
2587      used for its computation. */
2588   new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
2589 			   (using_enc ? 0 : value_embedded_offset (argp)));
2590   deprecated_set_value_type (new_val, value_type (argp));
2591   set_value_embedded_offset (new_val, (using_enc
2592 				       ? top + value_embedded_offset (argp)
2593 				       : top));
2594   return new_val;
2595 }
2596 
2597 
2598 
2599 
2600 /* Return the value of the local variable, if one exists.
2601    Flag COMPLAIN signals an error if the request is made in an
2602    inappropriate context.  */
2603 
2604 struct value *
value_of_local(const char * name,int complain)2605 value_of_local (const char *name, int complain)
2606 {
2607   struct symbol *func, *sym;
2608   struct block *b;
2609   struct value * ret;
2610 
2611   if (deprecated_selected_frame == 0)
2612     {
2613       if (complain)
2614 	error (_("no frame selected"));
2615       else
2616 	return 0;
2617     }
2618 
2619   func = get_frame_function (deprecated_selected_frame);
2620   if (!func)
2621     {
2622       if (complain)
2623 	error (_("no `%s' in nameless context"), name);
2624       else
2625 	return 0;
2626     }
2627 
2628   b = SYMBOL_BLOCK_VALUE (func);
2629   if (dict_empty (BLOCK_DICT (b)))
2630     {
2631       if (complain)
2632 	error (_("no args, no `%s'"), name);
2633       else
2634 	return 0;
2635     }
2636 
2637   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2638      symbol instead of the LOC_ARG one (if both exist).  */
2639   sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
2640   if (sym == NULL)
2641     {
2642       if (complain)
2643 	error (_("current stack frame does not contain a variable named `%s'"), name);
2644       else
2645 	return NULL;
2646     }
2647 
2648   ret = read_var_value (sym, deprecated_selected_frame);
2649   if (ret == 0 && complain)
2650     error (_("`%s' argument unreadable"), name);
2651   return ret;
2652 }
2653 
2654 /* C++/Objective-C: return the value of the class instance variable,
2655    if one exists.  Flag COMPLAIN signals an error if the request is
2656    made in an inappropriate context.  */
2657 
2658 struct value *
value_of_this(int complain)2659 value_of_this (int complain)
2660 {
2661   if (current_language->la_language == language_objc)
2662     return value_of_local ("self", complain);
2663   else
2664     return value_of_local ("this", complain);
2665 }
2666 
2667 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2668    long, starting at LOWBOUND.  The result has the same lower bound as
2669    the original ARRAY.  */
2670 
2671 struct value *
value_slice(struct value * array,int lowbound,int length)2672 value_slice (struct value *array, int lowbound, int length)
2673 {
2674   struct type *slice_range_type, *slice_type, *range_type;
2675   LONGEST lowerbound, upperbound;
2676   struct value *slice;
2677   struct type *array_type;
2678   array_type = check_typedef (value_type (array));
2679   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2680       && TYPE_CODE (array_type) != TYPE_CODE_STRING
2681       && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2682     error (_("cannot take slice of non-array"));
2683   range_type = TYPE_INDEX_TYPE (array_type);
2684   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2685     error (_("slice from bad array or bitstring"));
2686   if (lowbound < lowerbound || length < 0
2687       || lowbound + length - 1 > upperbound)
2688     error (_("slice out of range"));
2689   /* FIXME-type-allocation: need a way to free this type when we are
2690      done with it.  */
2691   slice_range_type = create_range_type ((struct type *) NULL,
2692 					TYPE_TARGET_TYPE (range_type),
2693 					lowbound, lowbound + length - 1);
2694   if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2695     {
2696       int i;
2697       slice_type = create_set_type ((struct type *) NULL, slice_range_type);
2698       TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2699       slice = value_zero (slice_type, not_lval);
2700       for (i = 0; i < length; i++)
2701 	{
2702 	  int element = value_bit_index (array_type,
2703 					 value_contents (array),
2704 					 lowbound + i);
2705 	  if (element < 0)
2706 	    error (_("internal error accessing bitstring"));
2707 	  else if (element > 0)
2708 	    {
2709 	      int j = i % TARGET_CHAR_BIT;
2710 	      if (BITS_BIG_ENDIAN)
2711 		j = TARGET_CHAR_BIT - 1 - j;
2712 	      value_contents_raw (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2713 	    }
2714 	}
2715       /* We should set the address, bitssize, and bitspos, so the clice
2716          can be used on the LHS, but that may require extensions to
2717          value_assign.  For now, just leave as a non_lval.  FIXME.  */
2718     }
2719   else
2720     {
2721       struct type *element_type = TYPE_TARGET_TYPE (array_type);
2722       LONGEST offset
2723 	= (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2724       slice_type = create_array_type ((struct type *) NULL, element_type,
2725 				      slice_range_type);
2726       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2727       slice = allocate_value (slice_type);
2728       if (value_lazy (array))
2729 	set_value_lazy (slice, 1);
2730       else
2731 	memcpy (value_contents_writeable (slice),
2732 		value_contents (array) + offset,
2733 		TYPE_LENGTH (slice_type));
2734       if (VALUE_LVAL (array) == lval_internalvar)
2735 	VALUE_LVAL (slice) = lval_internalvar_component;
2736       else
2737 	VALUE_LVAL (slice) = VALUE_LVAL (array);
2738       VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2739       VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
2740       set_value_offset (slice, value_offset (array) + offset);
2741     }
2742   return slice;
2743 }
2744 
2745 /* Create a value for a FORTRAN complex number.  Currently most of
2746    the time values are coerced to COMPLEX*16 (i.e. a complex number
2747    composed of 2 doubles.  This really should be a smarter routine
2748    that figures out precision inteligently as opposed to assuming
2749    doubles. FIXME: fmb */
2750 
2751 struct value *
value_literal_complex(struct value * arg1,struct value * arg2,struct type * type)2752 value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
2753 {
2754   struct value *val;
2755   struct type *real_type = TYPE_TARGET_TYPE (type);
2756 
2757   val = allocate_value (type);
2758   arg1 = value_cast (real_type, arg1);
2759   arg2 = value_cast (real_type, arg2);
2760 
2761   memcpy (value_contents_raw (val),
2762 	  value_contents (arg1), TYPE_LENGTH (real_type));
2763   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
2764 	  value_contents (arg2), TYPE_LENGTH (real_type));
2765   return val;
2766 }
2767 
2768 /* Cast a value into the appropriate complex data type. */
2769 
2770 static struct value *
cast_into_complex(struct type * type,struct value * val)2771 cast_into_complex (struct type *type, struct value *val)
2772 {
2773   struct type *real_type = TYPE_TARGET_TYPE (type);
2774   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
2775     {
2776       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
2777       struct value *re_val = allocate_value (val_real_type);
2778       struct value *im_val = allocate_value (val_real_type);
2779 
2780       memcpy (value_contents_raw (re_val),
2781 	      value_contents (val), TYPE_LENGTH (val_real_type));
2782       memcpy (value_contents_raw (im_val),
2783 	      value_contents (val) + TYPE_LENGTH (val_real_type),
2784 	      TYPE_LENGTH (val_real_type));
2785 
2786       return value_literal_complex (re_val, im_val, type);
2787     }
2788   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
2789 	   || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
2790     return value_literal_complex (val, value_zero (real_type, not_lval), type);
2791   else
2792     error (_("cannot cast non-number to complex"));
2793 }
2794 
2795 void
_initialize_valops(void)2796 _initialize_valops (void)
2797 {
2798   add_setshow_boolean_cmd ("overload-resolution", class_support,
2799 			   &overload_resolution, _("\
2800 Set overload resolution in evaluating C++ functions."), _("\
2801 Show overload resolution in evaluating C++ functions."), NULL,
2802 			   NULL,
2803 			   show_overload_resolution,
2804 			   &setlist, &showlist);
2805   overload_resolution = 1;
2806 }
2807