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