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