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