119370Spst/* Perform non-arithmetic operations on values, for GDB.
298944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3130803Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
419370Spst   Free Software Foundation, Inc.
519370Spst
698944Sobrien   This file is part of GDB.
719370Spst
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1219370Spst
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1719370Spst
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2219370Spst
2319370Spst#include "defs.h"
2419370Spst#include "symtab.h"
2519370Spst#include "gdbtypes.h"
2619370Spst#include "value.h"
2719370Spst#include "frame.h"
2819370Spst#include "inferior.h"
2919370Spst#include "gdbcore.h"
3019370Spst#include "target.h"
3119370Spst#include "demangle.h"
3219370Spst#include "language.h"
3346283Sdfr#include "gdbcmd.h"
3498944Sobrien#include "regcache.h"
3598944Sobrien#include "cp-abi.h"
36130803Smarcel#include "block.h"
37130803Smarcel#include "infcall.h"
38130803Smarcel#include "dictionary.h"
39130803Smarcel#include "cp-support.h"
4019370Spst
4119370Spst#include <errno.h>
4219370Spst#include "gdb_string.h"
43130803Smarcel#include "gdb_assert.h"
44130803Smarcel#include "cp-support.h"
4519370Spst
4646283Sdfr/* Flag indicating HP compilers were used; needed to correctly handle some
4746283Sdfr   value operations with HP aCC code/runtime. */
4846283Sdfrextern int hp_som_som_object_present;
4946283Sdfr
5098944Sobrienextern int overload_debug;
5119370Spst/* Local functions.  */
5219370Spst
53130803Smarcelstatic int typecmp (int staticp, int varargs, int nargs,
54130803Smarcel		    struct field t1[], struct value *t2[]);
5519370Spst
5698944Sobrienstatic CORE_ADDR value_push (CORE_ADDR, struct value *);
5719370Spst
5898944Sobrienstatic struct value *search_struct_field (char *, struct value *, int,
5998944Sobrien				      struct type *, int);
6019370Spst
6198944Sobrienstatic struct value *search_struct_method (char *, struct value **,
6298944Sobrien				       struct value **,
6398944Sobrien				       int, int *, struct type *);
6446283Sdfr
65130803Smarcelstatic int find_oload_champ_namespace (struct type **arg_types, int nargs,
66130803Smarcel				       const char *func_name,
67130803Smarcel				       const char *qualified_name,
68130803Smarcel				       struct symbol ***oload_syms,
69130803Smarcel				       struct badness_vector **oload_champ_bv);
70130803Smarcel
71130803Smarcelstatic
72130803Smarcelint find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
73130803Smarcel				     const char *func_name,
74130803Smarcel				     const char *qualified_name,
75130803Smarcel				     int namespace_len,
76130803Smarcel				     struct symbol ***oload_syms,
77130803Smarcel				     struct badness_vector **oload_champ_bv,
78130803Smarcel				     int *oload_champ);
79130803Smarcel
80130803Smarcelstatic int find_oload_champ (struct type **arg_types, int nargs, int method,
81130803Smarcel			     int num_fns,
82130803Smarcel			     struct fn_field *fns_ptr,
83130803Smarcel			     struct symbol **oload_syms,
84130803Smarcel			     struct badness_vector **oload_champ_bv);
85130803Smarcel
86130803Smarcelstatic int oload_method_static (int method, struct fn_field *fns_ptr,
87130803Smarcel				int index);
88130803Smarcel
89130803Smarcelenum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
90130803Smarcel
91130803Smarcelstatic enum
92130803Smarceloload_classification classify_oload_match (struct badness_vector
93130803Smarcel					   * oload_champ_bv,
94130803Smarcel					   int nargs,
95130803Smarcel					   int static_offset);
96130803Smarcel
9798944Sobrienstatic int check_field_in (struct type *, const char *);
9819370Spst
99130803Smarcelstatic struct value *value_struct_elt_for_reference (struct type *domain,
100130803Smarcel						     int offset,
101130803Smarcel						     struct type *curtype,
102130803Smarcel						     char *name,
103130803Smarcel						     struct type *intype,
104130803Smarcel						     enum noside noside);
105130803Smarcel
106130803Smarcelstatic struct value *value_namespace_elt (const struct type *curtype,
107130803Smarcel					  char *name,
108130803Smarcel					  enum noside noside);
109130803Smarcel
110130803Smarcelstatic struct value *value_maybe_namespace_elt (const struct type *curtype,
111130803Smarcel						char *name,
112130803Smarcel						enum noside noside);
113130803Smarcel
11498944Sobrienstatic CORE_ADDR allocate_space_in_inferior (int);
11519370Spst
11698944Sobrienstatic struct value *cast_into_complex (struct type *, struct value *);
11719370Spst
11898944Sobrienstatic struct fn_field *find_method_list (struct value ** argp, char *method,
119130803Smarcel					  int offset,
12098944Sobrien					  struct type *type, int *num_fns,
12198944Sobrien					  struct type **basetype,
12298944Sobrien					  int *boffset);
12319370Spst
12498944Sobrienvoid _initialize_valops (void);
12546283Sdfr
12619370Spst/* Flag for whether we want to abandon failed expression evals by default.  */
12719370Spst
12819370Spst#if 0
12919370Spststatic int auto_abandon = 0;
13019370Spst#endif
13119370Spst
13246283Sdfrint overload_resolution = 0;
13346283Sdfr
13419370Spst/* Find the address of function name NAME in the inferior.  */
13519370Spst
13698944Sobrienstruct value *
137130803Smarcelfind_function_in_inferior (const char *name)
13819370Spst{
139130803Smarcel  struct symbol *sym;
140130803Smarcel  sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
14119370Spst  if (sym != NULL)
14219370Spst    {
14319370Spst      if (SYMBOL_CLASS (sym) != LOC_BLOCK)
14419370Spst	{
14519370Spst	  error ("\"%s\" exists in this program but is not a function.",
14619370Spst		 name);
14719370Spst	}
14819370Spst      return value_of_variable (sym, NULL);
14919370Spst    }
15019370Spst  else
15119370Spst    {
15298944Sobrien      struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
15319370Spst      if (msymbol != NULL)
15419370Spst	{
15519370Spst	  struct type *type;
15698944Sobrien	  CORE_ADDR maddr;
15719370Spst	  type = lookup_pointer_type (builtin_type_char);
15819370Spst	  type = lookup_function_type (type);
15919370Spst	  type = lookup_pointer_type (type);
16098944Sobrien	  maddr = SYMBOL_VALUE_ADDRESS (msymbol);
16198944Sobrien	  return value_from_pointer (type, maddr);
16219370Spst	}
16319370Spst      else
16419370Spst	{
16598944Sobrien	  if (!target_has_execution)
16646283Sdfr	    error ("evaluation of this expression requires the target program to be active");
16798944Sobrien	  else
16846283Sdfr	    error ("evaluation of this expression requires the program to have a function \"%s\".", name);
16919370Spst	}
17019370Spst    }
17119370Spst}
17219370Spst
17319370Spst/* Allocate NBYTES of space in the inferior using the inferior's malloc
17419370Spst   and return a value that is a pointer to the allocated space. */
17519370Spst
17698944Sobrienstruct value *
17798944Sobrienvalue_allocate_space_in_inferior (int len)
17819370Spst{
17998944Sobrien  struct value *blocklen;
180130803Smarcel  struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
18119370Spst
18219370Spst  blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
18319370Spst  val = call_function_by_hand (val, 1, &blocklen);
18419370Spst  if (value_logical_not (val))
18519370Spst    {
18646283Sdfr      if (!target_has_execution)
18798944Sobrien	error ("No memory available to program now: you need to start the target first");
18898944Sobrien      else
18998944Sobrien	error ("No memory available to program: call to malloc failed");
19019370Spst    }
19119370Spst  return val;
19219370Spst}
19319370Spst
19419370Spststatic CORE_ADDR
19598944Sobrienallocate_space_in_inferior (int len)
19619370Spst{
19719370Spst  return value_as_long (value_allocate_space_in_inferior (len));
19819370Spst}
19919370Spst
20019370Spst/* Cast value ARG2 to type TYPE and return as a value.
20119370Spst   More general than a C cast: accepts any two types of the same length,
20219370Spst   and if ARG2 is an lvalue it can be cast into anything at all.  */
20319370Spst/* In C++, casts may change pointer or object representations.  */
20419370Spst
20598944Sobrienstruct value *
20698944Sobrienvalue_cast (struct type *type, struct value *arg2)
20719370Spst{
208130803Smarcel  enum type_code code1;
209130803Smarcel  enum type_code code2;
210130803Smarcel  int scalar;
21119370Spst  struct type *type2;
21219370Spst
21346283Sdfr  int convert_to_boolean = 0;
21498944Sobrien
21519370Spst  if (VALUE_TYPE (arg2) == type)
21619370Spst    return arg2;
21719370Spst
21819370Spst  CHECK_TYPEDEF (type);
21919370Spst  code1 = TYPE_CODE (type);
22098944Sobrien  COERCE_REF (arg2);
22119370Spst  type2 = check_typedef (VALUE_TYPE (arg2));
22219370Spst
22319370Spst  /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
22419370Spst     is treated like a cast to (TYPE [N])OBJECT,
22519370Spst     where N is sizeof(OBJECT)/sizeof(TYPE). */
22619370Spst  if (code1 == TYPE_CODE_ARRAY)
22719370Spst    {
22819370Spst      struct type *element_type = TYPE_TARGET_TYPE (type);
22919370Spst      unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
23019370Spst      if (element_length > 0
23198944Sobrien	&& TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
23219370Spst	{
23319370Spst	  struct type *range_type = TYPE_INDEX_TYPE (type);
23419370Spst	  int val_length = TYPE_LENGTH (type2);
23519370Spst	  LONGEST low_bound, high_bound, new_length;
23619370Spst	  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
23719370Spst	    low_bound = 0, high_bound = 0;
23819370Spst	  new_length = val_length / element_length;
23919370Spst	  if (val_length % element_length != 0)
24098944Sobrien	    warning ("array element type size does not divide object size in cast");
24119370Spst	  /* FIXME-type-allocation: need a way to free this type when we are
24219370Spst	     done with it.  */
24319370Spst	  range_type = create_range_type ((struct type *) NULL,
24419370Spst					  TYPE_TARGET_TYPE (range_type),
24519370Spst					  low_bound,
24619370Spst					  new_length + low_bound - 1);
24719370Spst	  VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
24819370Spst						 element_type, range_type);
24919370Spst	  return arg2;
25019370Spst	}
25119370Spst    }
25219370Spst
25319370Spst  if (current_language->c_style_arrays
25419370Spst      && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
25519370Spst    arg2 = value_coerce_array (arg2);
25619370Spst
25719370Spst  if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
25819370Spst    arg2 = value_coerce_function (arg2);
25919370Spst
26019370Spst  type2 = check_typedef (VALUE_TYPE (arg2));
26119370Spst  COERCE_VARYING_ARRAY (arg2, type2);
26219370Spst  code2 = TYPE_CODE (type2);
26319370Spst
26419370Spst  if (code1 == TYPE_CODE_COMPLEX)
26519370Spst    return cast_into_complex (type, arg2);
26646283Sdfr  if (code1 == TYPE_CODE_BOOL)
26746283Sdfr    {
26846283Sdfr      code1 = TYPE_CODE_INT;
26946283Sdfr      convert_to_boolean = 1;
27046283Sdfr    }
27146283Sdfr  if (code1 == TYPE_CODE_CHAR)
27246283Sdfr    code1 = TYPE_CODE_INT;
27319370Spst  if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
27419370Spst    code2 = TYPE_CODE_INT;
27519370Spst
27619370Spst  scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
27719370Spst	    || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
27819370Spst
27998944Sobrien  if (code1 == TYPE_CODE_STRUCT
28019370Spst      && code2 == TYPE_CODE_STRUCT
28119370Spst      && TYPE_NAME (type) != 0)
28219370Spst    {
28319370Spst      /* Look in the type of the source to see if it contains the
28498944Sobrien         type of the target as a superclass.  If so, we'll need to
28598944Sobrien         offset the object in addition to changing its type.  */
28698944Sobrien      struct value *v = search_struct_field (type_name_no_tag (type),
28719370Spst					 arg2, 0, type2, 1);
28819370Spst      if (v)
28919370Spst	{
29019370Spst	  VALUE_TYPE (v) = type;
29119370Spst	  return v;
29219370Spst	}
29319370Spst    }
29419370Spst  if (code1 == TYPE_CODE_FLT && scalar)
29519370Spst    return value_from_double (type, value_as_double (arg2));
29619370Spst  else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
29719370Spst	    || code1 == TYPE_CODE_RANGE)
29819370Spst	   && (scalar || code2 == TYPE_CODE_PTR))
29946283Sdfr    {
30046283Sdfr      LONGEST longest;
30146283Sdfr
30298944Sobrien      if (hp_som_som_object_present &&	/* if target compiled by HP aCC */
30398944Sobrien	  (code2 == TYPE_CODE_PTR))
30498944Sobrien	{
30598944Sobrien	  unsigned int *ptr;
30698944Sobrien	  struct value *retvalp;
30746283Sdfr
30898944Sobrien	  switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
30998944Sobrien	    {
31098944Sobrien	      /* With HP aCC, pointers to data members have a bias */
31198944Sobrien	    case TYPE_CODE_MEMBER:
31298944Sobrien	      retvalp = value_from_longest (type, value_as_long (arg2));
31398944Sobrien	      /* force evaluation */
31498944Sobrien	      ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
31598944Sobrien	      *ptr &= ~0x20000000;	/* zap 29th bit to remove bias */
31698944Sobrien	      return retvalp;
31798944Sobrien
31898944Sobrien	      /* While pointers to methods don't really point to a function */
31998944Sobrien	    case TYPE_CODE_METHOD:
32098944Sobrien	      error ("Pointers to methods not supported with HP aCC");
32198944Sobrien
32298944Sobrien	    default:
32398944Sobrien	      break;		/* fall out and go to normal handling */
32498944Sobrien	    }
32598944Sobrien	}
32698944Sobrien
32798944Sobrien      /* When we cast pointers to integers, we mustn't use
32898944Sobrien         POINTER_TO_ADDRESS to find the address the pointer
32998944Sobrien         represents, as value_as_long would.  GDB should evaluate
33098944Sobrien         expressions just as the compiler would --- and the compiler
33198944Sobrien         sees a cast as a simple reinterpretation of the pointer's
33298944Sobrien         bits.  */
33398944Sobrien      if (code2 == TYPE_CODE_PTR)
33498944Sobrien        longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
33598944Sobrien                                            TYPE_LENGTH (type2));
33698944Sobrien      else
33798944Sobrien        longest = value_as_long (arg2);
33898944Sobrien      return value_from_longest (type, convert_to_boolean ?
33998944Sobrien				 (LONGEST) (longest ? 1 : 0) : longest);
34046283Sdfr    }
34198944Sobrien  else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT  ||
34298944Sobrien				      code2 == TYPE_CODE_ENUM ||
34398944Sobrien				      code2 == TYPE_CODE_RANGE))
34498944Sobrien    {
34598944Sobrien      /* TYPE_LENGTH (type) is the length of a pointer, but we really
34698944Sobrien	 want the length of an address! -- we are really dealing with
34798944Sobrien	 addresses (i.e., gdb representations) not pointers (i.e.,
34898944Sobrien	 target representations) here.
34998944Sobrien
35098944Sobrien	 This allows things like "print *(int *)0x01000234" to work
35198944Sobrien	 without printing a misleading message -- which would
35298944Sobrien	 otherwise occur when dealing with a target having two byte
35398944Sobrien	 pointers and four byte addresses.  */
35498944Sobrien
35598944Sobrien      int addr_bit = TARGET_ADDR_BIT;
35698944Sobrien
35798944Sobrien      LONGEST longest = value_as_long (arg2);
35898944Sobrien      if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
35998944Sobrien	{
36098944Sobrien	  if (longest >= ((LONGEST) 1 << addr_bit)
36198944Sobrien	      || longest <= -((LONGEST) 1 << addr_bit))
36298944Sobrien	    warning ("value truncated");
36398944Sobrien	}
36498944Sobrien      return value_from_longest (type, longest);
36598944Sobrien    }
36619370Spst  else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
36719370Spst    {
36819370Spst      if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
36919370Spst	{
37019370Spst	  struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
37119370Spst	  struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
37298944Sobrien	  if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
37319370Spst	      && TYPE_CODE (t2) == TYPE_CODE_STRUCT
37446283Sdfr	      && !value_logical_not (arg2))
37519370Spst	    {
37698944Sobrien	      struct value *v;
37746283Sdfr
37846283Sdfr	      /* Look in the type of the source to see if it contains the
37998944Sobrien	         type of the target as a superclass.  If so, we'll need to
38098944Sobrien	         offset the pointer rather than just change its type.  */
38146283Sdfr	      if (TYPE_NAME (t1) != NULL)
38219370Spst		{
38346283Sdfr		  v = search_struct_field (type_name_no_tag (t1),
38446283Sdfr					   value_ind (arg2), 0, t2, 1);
38546283Sdfr		  if (v)
38646283Sdfr		    {
38746283Sdfr		      v = value_addr (v);
38846283Sdfr		      VALUE_TYPE (v) = type;
38946283Sdfr		      return v;
39046283Sdfr		    }
39119370Spst		}
39246283Sdfr
39346283Sdfr	      /* Look in the type of the target to see if it contains the
39498944Sobrien	         type of the source as a superclass.  If so, we'll need to
39598944Sobrien	         offset the pointer rather than just change its type.
39698944Sobrien	         FIXME: This fails silently with virtual inheritance.  */
39746283Sdfr	      if (TYPE_NAME (t2) != NULL)
39846283Sdfr		{
39946283Sdfr		  v = search_struct_field (type_name_no_tag (t2),
40098944Sobrien				       value_zero (t1, not_lval), 0, t1, 1);
40146283Sdfr		  if (v)
40246283Sdfr		    {
403130803Smarcel                      CORE_ADDR addr2 = value_as_address (arg2);
404130803Smarcel                      addr2 -= (VALUE_ADDRESS (v)
405130803Smarcel                                + VALUE_OFFSET (v)
406130803Smarcel                                + VALUE_EMBEDDED_OFFSET (v));
407130803Smarcel                      return value_from_pointer (type, addr2);
40846283Sdfr		    }
40946283Sdfr		}
41019370Spst	    }
41119370Spst	  /* No superclass found, just fall through to change ptr type.  */
41219370Spst	}
41319370Spst      VALUE_TYPE (arg2) = type;
41498944Sobrien      arg2 = value_change_enclosing_type (arg2, type);
41598944Sobrien      VALUE_POINTED_TO_OFFSET (arg2) = 0;	/* pai: chk_val */
41619370Spst      return arg2;
41719370Spst    }
41819370Spst  else if (VALUE_LVAL (arg2) == lval_memory)
41919370Spst    {
42046283Sdfr      return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
42146283Sdfr			    VALUE_BFD_SECTION (arg2));
42219370Spst    }
42319370Spst  else if (code1 == TYPE_CODE_VOID)
42419370Spst    {
42519370Spst      return value_zero (builtin_type_void, not_lval);
42619370Spst    }
42719370Spst  else
42819370Spst    {
42919370Spst      error ("Invalid cast.");
43019370Spst      return 0;
43119370Spst    }
43219370Spst}
43319370Spst
43419370Spst/* Create a value of type TYPE that is zero, and return it.  */
43519370Spst
43698944Sobrienstruct value *
43798944Sobrienvalue_zero (struct type *type, enum lval_type lv)
43819370Spst{
43998944Sobrien  struct value *val = allocate_value (type);
44019370Spst
44119370Spst  memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
44219370Spst  VALUE_LVAL (val) = lv;
44319370Spst
44419370Spst  return val;
44519370Spst}
44619370Spst
44798944Sobrien/* Return a value with type TYPE located at ADDR.
44819370Spst
44919370Spst   Call value_at only if the data needs to be fetched immediately;
45019370Spst   if we can be 'lazy' and defer the fetch, perhaps indefinately, call
45119370Spst   value_at_lazy instead.  value_at_lazy simply records the address of
45298944Sobrien   the data and sets the lazy-evaluation-required flag.  The lazy flag
45398944Sobrien   is tested in the VALUE_CONTENTS macro, which is used if and when
45498944Sobrien   the contents are actually required.
45519370Spst
45646283Sdfr   Note: value_at does *NOT* handle embedded offsets; perform such
45746283Sdfr   adjustments before or after calling it. */
45846283Sdfr
45998944Sobrienstruct value *
46098944Sobrienvalue_at (struct type *type, CORE_ADDR addr, asection *sect)
46119370Spst{
46298944Sobrien  struct value *val;
46319370Spst
46419370Spst  if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
46519370Spst    error ("Attempt to dereference a generic pointer.");
46619370Spst
46719370Spst  val = allocate_value (type);
46819370Spst
46998944Sobrien  read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
47019370Spst
47119370Spst  VALUE_LVAL (val) = lval_memory;
47219370Spst  VALUE_ADDRESS (val) = addr;
47346283Sdfr  VALUE_BFD_SECTION (val) = sect;
47419370Spst
47519370Spst  return val;
47619370Spst}
47719370Spst
47819370Spst/* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
47919370Spst
48098944Sobrienstruct value *
48198944Sobrienvalue_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
48219370Spst{
48398944Sobrien  struct value *val;
48419370Spst
48519370Spst  if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
48619370Spst    error ("Attempt to dereference a generic pointer.");
48719370Spst
48819370Spst  val = allocate_value (type);
48919370Spst
49019370Spst  VALUE_LVAL (val) = lval_memory;
49119370Spst  VALUE_ADDRESS (val) = addr;
49219370Spst  VALUE_LAZY (val) = 1;
49346283Sdfr  VALUE_BFD_SECTION (val) = sect;
49419370Spst
49519370Spst  return val;
49619370Spst}
49719370Spst
49898944Sobrien/* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
49998944Sobrien   if the current data for a variable needs to be loaded into
50098944Sobrien   VALUE_CONTENTS(VAL).  Fetches the data from the user's process, and
50146283Sdfr   clears the lazy flag to indicate that the data in the buffer is valid.
50219370Spst
50319370Spst   If the value is zero-length, we avoid calling read_memory, which would
50419370Spst   abort.  We mark the value as fetched anyway -- all 0 bytes of it.
50519370Spst
50619370Spst   This function returns a value because it is used in the VALUE_CONTENTS
50719370Spst   macro as part of an expression, where a void would not work.  The
50819370Spst   value is ignored.  */
50919370Spst
51019370Spstint
51198944Sobrienvalue_fetch_lazy (struct value *val)
51219370Spst{
51319370Spst  CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
51446283Sdfr  int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
51519370Spst
51698944Sobrien  struct type *type = VALUE_TYPE (val);
51798944Sobrien  if (length)
51898944Sobrien    read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
51946283Sdfr
52019370Spst  VALUE_LAZY (val) = 0;
52119370Spst  return 0;
52219370Spst}
52319370Spst
52419370Spst
52519370Spst/* Store the contents of FROMVAL into the location of TOVAL.
52619370Spst   Return a new value with the location of TOVAL and contents of FROMVAL.  */
52719370Spst
52898944Sobrienstruct value *
52998944Sobrienvalue_assign (struct value *toval, struct value *fromval)
53019370Spst{
531130803Smarcel  struct type *type;
53298944Sobrien  struct value *val;
533130803Smarcel  char raw_buffer[MAX_REGISTER_SIZE];
53419370Spst  int use_buffer = 0;
535130803Smarcel  struct frame_id old_frame;
53619370Spst
53719370Spst  if (!toval->modifiable)
53819370Spst    error ("Left operand of assignment is not a modifiable lvalue.");
53919370Spst
54019370Spst  COERCE_REF (toval);
54119370Spst
54219370Spst  type = VALUE_TYPE (toval);
54319370Spst  if (VALUE_LVAL (toval) != lval_internalvar)
54419370Spst    fromval = value_cast (type, fromval);
54519370Spst  else
54619370Spst    COERCE_ARRAY (fromval);
54719370Spst  CHECK_TYPEDEF (type);
54819370Spst
549130803Smarcel  /* Since modifying a register can trash the frame chain, and modifying memory
550130803Smarcel     can trash the frame cache, we save the old frame and then restore the new
551130803Smarcel     frame afterwards.  */
552130803Smarcel  old_frame = get_frame_id (deprecated_selected_frame);
55319370Spst
55419370Spst  switch (VALUE_LVAL (toval))
55519370Spst    {
55619370Spst    case lval_internalvar:
55719370Spst      set_internalvar (VALUE_INTERNALVAR (toval), fromval);
55846283Sdfr      val = value_copy (VALUE_INTERNALVAR (toval)->value);
55998944Sobrien      val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
56046283Sdfr      VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
56146283Sdfr      VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
56246283Sdfr      return val;
56319370Spst
56419370Spst    case lval_internalvar_component:
56519370Spst      set_internalvar_component (VALUE_INTERNALVAR (toval),
56619370Spst				 VALUE_OFFSET (toval),
56719370Spst				 VALUE_BITPOS (toval),
56819370Spst				 VALUE_BITSIZE (toval),
56919370Spst				 fromval);
57019370Spst      break;
57119370Spst
57219370Spst    case lval_memory:
57346283Sdfr      {
57446283Sdfr	char *dest_buffer;
57598944Sobrien	CORE_ADDR changed_addr;
57698944Sobrien	int changed_len;
57746283Sdfr
57898944Sobrien	if (VALUE_BITSIZE (toval))
57998944Sobrien	  {
58046283Sdfr	    char buffer[sizeof (LONGEST)];
58146283Sdfr	    /* We assume that the argument to read_memory is in units of
58246283Sdfr	       host chars.  FIXME:  Is that correct?  */
58346283Sdfr	    changed_len = (VALUE_BITPOS (toval)
58498944Sobrien			   + VALUE_BITSIZE (toval)
58598944Sobrien			   + HOST_CHAR_BIT - 1)
58698944Sobrien	      / HOST_CHAR_BIT;
58719370Spst
58846283Sdfr	    if (changed_len > (int) sizeof (LONGEST))
58946283Sdfr	      error ("Can't handle bitfields which don't fit in a %d bit word.",
590130803Smarcel		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
59119370Spst
59246283Sdfr	    read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
59346283Sdfr			 buffer, changed_len);
59446283Sdfr	    modify_field (buffer, value_as_long (fromval),
59546283Sdfr			  VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
59646283Sdfr	    changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
59746283Sdfr	    dest_buffer = buffer;
59846283Sdfr	  }
59946283Sdfr	else if (use_buffer)
60046283Sdfr	  {
60146283Sdfr	    changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
60246283Sdfr	    changed_len = use_buffer;
60346283Sdfr	    dest_buffer = raw_buffer;
60446283Sdfr	  }
60546283Sdfr	else
60646283Sdfr	  {
60746283Sdfr	    changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
60846283Sdfr	    changed_len = TYPE_LENGTH (type);
60946283Sdfr	    dest_buffer = VALUE_CONTENTS (fromval);
61046283Sdfr	  }
61146283Sdfr
61246283Sdfr	write_memory (changed_addr, dest_buffer, changed_len);
61346283Sdfr	if (memory_changed_hook)
61446283Sdfr	  memory_changed_hook (changed_addr, changed_len);
615130803Smarcel	target_changed_event ();
61646283Sdfr      }
61719370Spst      break;
61819370Spst
619130803Smarcel    case lval_reg_frame_relative:
62019370Spst    case lval_register:
62119370Spst      {
62219370Spst	struct frame_info *frame;
623130803Smarcel	int value_reg;
62419370Spst
62519370Spst	/* Figure out which frame this is in currently.  */
626130803Smarcel	if (VALUE_LVAL (toval) == lval_register)
627130803Smarcel	  {
628130803Smarcel	    frame = get_current_frame ();
629130803Smarcel	    value_reg = VALUE_REGNO (toval);
630130803Smarcel	  }
631130803Smarcel	else
632130803Smarcel	  {
633130803Smarcel	    frame = frame_find_by_id (VALUE_FRAME_ID (toval));
634130803Smarcel	    value_reg = VALUE_FRAME_REGNUM (toval);
635130803Smarcel	  }
63619370Spst
63719370Spst	if (!frame)
63819370Spst	  error ("Value being assigned to is no longer active.");
639130803Smarcel
640130803Smarcel	if (VALUE_LVAL (toval) == lval_reg_frame_relative
641130803Smarcel	    && CONVERT_REGISTER_P (VALUE_FRAME_REGNUM (toval), type))
64219370Spst	  {
643130803Smarcel	    /* If TOVAL is a special machine register requiring
644130803Smarcel	       conversion of program values to a special raw format.  */
645130803Smarcel	    VALUE_TO_REGISTER (frame, VALUE_FRAME_REGNUM (toval),
646130803Smarcel			       type, VALUE_CONTENTS (fromval));
64719370Spst	  }
64819370Spst	else
64919370Spst	  {
650130803Smarcel	    /* TOVAL is stored in a series of registers in the frame
651130803Smarcel	       specified by the structure.  Copy that value out,
652130803Smarcel	       modify it, and copy it back in.  */
653130803Smarcel	    int amount_copied;
654130803Smarcel	    int amount_to_copy;
655130803Smarcel	    char *buffer;
656130803Smarcel	    int reg_offset;
657130803Smarcel	    int byte_offset;
658130803Smarcel	    int regno;
65919370Spst
660130803Smarcel	    /* Locate the first register that falls in the value that
661130803Smarcel	       needs to be transfered.  Compute the offset of the
662130803Smarcel	       value in that register.  */
663130803Smarcel	    {
664130803Smarcel	      int offset;
665130803Smarcel	      for (reg_offset = value_reg, offset = 0;
666130803Smarcel		   offset + DEPRECATED_REGISTER_RAW_SIZE (reg_offset) <= VALUE_OFFSET (toval);
667130803Smarcel		   reg_offset++);
668130803Smarcel	      byte_offset = VALUE_OFFSET (toval) - offset;
669130803Smarcel	    }
67098944Sobrien
671130803Smarcel	    /* Compute the number of register aligned values that need
672130803Smarcel	       to be copied.  */
673130803Smarcel	    if (VALUE_BITSIZE (toval))
674130803Smarcel	      amount_to_copy = byte_offset + 1;
67519370Spst	    else
676130803Smarcel	      amount_to_copy = byte_offset + TYPE_LENGTH (type);
677130803Smarcel
678130803Smarcel	    /* And a bounce buffer.  Be slightly over generous.  */
679130803Smarcel	    buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
680130803Smarcel
681130803Smarcel	    /* Copy it in.  */
682130803Smarcel	    for (regno = reg_offset, amount_copied = 0;
683130803Smarcel		 amount_copied < amount_to_copy;
684130803Smarcel		 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
685130803Smarcel	      frame_register_read (frame, regno, buffer + amount_copied);
686130803Smarcel
687130803Smarcel	    /* Modify what needs to be modified.  */
688130803Smarcel	    if (VALUE_BITSIZE (toval))
689130803Smarcel	      modify_field (buffer + byte_offset,
690130803Smarcel			    value_as_long (fromval),
691130803Smarcel			    VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
692130803Smarcel	    else if (use_buffer)
693130803Smarcel	      memcpy (buffer + VALUE_OFFSET (toval), raw_buffer, use_buffer);
694130803Smarcel	    else
695130803Smarcel	      memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
696130803Smarcel		      TYPE_LENGTH (type));
697130803Smarcel
698130803Smarcel	    /* Copy it out.  */
699130803Smarcel	    for (regno = reg_offset, amount_copied = 0;
700130803Smarcel		 amount_copied < amount_to_copy;
701130803Smarcel		 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
702130803Smarcel	      put_frame_register (frame, regno, buffer + amount_copied);
703130803Smarcel
70419370Spst	  }
70546283Sdfr	if (register_changed_hook)
70646283Sdfr	  register_changed_hook (-1);
707130803Smarcel	target_changed_event ();
708130803Smarcel	break;
70919370Spst      }
710130803Smarcel
711130803Smarcel    default:
712130803Smarcel      error ("Left operand of assignment is not an lvalue.");
713130803Smarcel    }
71419370Spst
715130803Smarcel  /* Assigning to the stack pointer, frame pointer, and other
716130803Smarcel     (architecture and calling convention specific) registers may
717130803Smarcel     cause the frame cache to be out of date.  Assigning to memory
718130803Smarcel     also can.  We just do this on all assignments to registers or
719130803Smarcel     memory, for simplicity's sake; I doubt the slowdown matters.  */
720130803Smarcel  switch (VALUE_LVAL (toval))
721130803Smarcel    {
722130803Smarcel    case lval_memory:
723130803Smarcel    case lval_register:
724130803Smarcel    case lval_reg_frame_relative:
72598944Sobrien
726130803Smarcel      reinit_frame_cache ();
727130803Smarcel
728130803Smarcel      /* Having destoroyed the frame cache, restore the selected frame.  */
729130803Smarcel
730130803Smarcel      /* FIXME: cagney/2002-11-02: There has to be a better way of
731130803Smarcel	 doing this.  Instead of constantly saving/restoring the
732130803Smarcel	 frame.  Why not create a get_selected_frame() function that,
733130803Smarcel	 having saved the selected frame's ID can automatically
734130803Smarcel	 re-find the previously selected frame automatically.  */
735130803Smarcel
736130803Smarcel      {
737130803Smarcel	struct frame_info *fi = frame_find_by_id (old_frame);
738130803Smarcel	if (fi != NULL)
739130803Smarcel	  select_frame (fi);
740130803Smarcel      }
741130803Smarcel
742130803Smarcel      break;
74319370Spst    default:
744130803Smarcel      break;
74519370Spst    }
746130803Smarcel
74719370Spst  /* If the field does not entirely fill a LONGEST, then zero the sign bits.
74819370Spst     If the field is signed, and is negative, then sign extend. */
74919370Spst  if ((VALUE_BITSIZE (toval) > 0)
75019370Spst      && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
75119370Spst    {
75219370Spst      LONGEST fieldval = value_as_long (fromval);
75346283Sdfr      LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
75419370Spst
75519370Spst      fieldval &= valmask;
75619370Spst      if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
75719370Spst	fieldval |= ~valmask;
75819370Spst
75919370Spst      fromval = value_from_longest (type, fieldval);
76019370Spst    }
76119370Spst
76219370Spst  val = value_copy (toval);
76319370Spst  memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
76419370Spst	  TYPE_LENGTH (type));
76519370Spst  VALUE_TYPE (val) = type;
76698944Sobrien  val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
76746283Sdfr  VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
76846283Sdfr  VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
76998944Sobrien
77019370Spst  return val;
77119370Spst}
77219370Spst
77319370Spst/* Extend a value VAL to COUNT repetitions of its type.  */
77419370Spst
77598944Sobrienstruct value *
77698944Sobrienvalue_repeat (struct value *arg1, int count)
77719370Spst{
77898944Sobrien  struct value *val;
77919370Spst
78019370Spst  if (VALUE_LVAL (arg1) != lval_memory)
78119370Spst    error ("Only values in memory can be extended with '@'.");
78219370Spst  if (count < 1)
78319370Spst    error ("Invalid number %d of repetitions.", count);
78419370Spst
78546283Sdfr  val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
78619370Spst
78719370Spst  read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
78846283Sdfr	       VALUE_CONTENTS_ALL_RAW (val),
78946283Sdfr	       TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
79019370Spst  VALUE_LVAL (val) = lval_memory;
79119370Spst  VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
79219370Spst
79319370Spst  return val;
79419370Spst}
79519370Spst
79698944Sobrienstruct value *
79798944Sobrienvalue_of_variable (struct symbol *var, struct block *b)
79819370Spst{
79998944Sobrien  struct value *val;
80046283Sdfr  struct frame_info *frame = NULL;
80119370Spst
80246283Sdfr  if (!b)
80346283Sdfr    frame = NULL;		/* Use selected frame.  */
80446283Sdfr  else if (symbol_read_needs_frame (var))
80519370Spst    {
80619370Spst      frame = block_innermost_frame (b);
80746283Sdfr      if (!frame)
80898944Sobrien	{
80946283Sdfr	  if (BLOCK_FUNCTION (b)
810130803Smarcel	      && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
81119370Spst	    error ("No frame is currently executing in block %s.",
812130803Smarcel		   SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
81319370Spst	  else
81419370Spst	    error ("No frame is currently executing in specified block");
81598944Sobrien	}
81619370Spst    }
81746283Sdfr
81819370Spst  val = read_var_value (var, frame);
81946283Sdfr  if (!val)
820130803Smarcel    error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
82146283Sdfr
82219370Spst  return val;
82319370Spst}
82419370Spst
82519370Spst/* Given a value which is an array, return a value which is a pointer to its
82619370Spst   first element, regardless of whether or not the array has a nonzero lower
82719370Spst   bound.
82819370Spst
82919370Spst   FIXME:  A previous comment here indicated that this routine should be
83019370Spst   substracting the array's lower bound.  It's not clear to me that this
83119370Spst   is correct.  Given an array subscripting operation, it would certainly
83219370Spst   work to do the adjustment here, essentially computing:
83319370Spst
83419370Spst   (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
83519370Spst
83619370Spst   However I believe a more appropriate and logical place to account for
83719370Spst   the lower bound is to do so in value_subscript, essentially computing:
83819370Spst
83919370Spst   (&array[0] + ((index - lowerbound) * sizeof array[0]))
84019370Spst
84119370Spst   As further evidence consider what would happen with operations other
84219370Spst   than array subscripting, where the caller would get back a value that
84319370Spst   had an address somewhere before the actual first element of the array,
84419370Spst   and the information about the lower bound would be lost because of
84519370Spst   the coercion to pointer type.
84698944Sobrien */
84719370Spst
84898944Sobrienstruct value *
84998944Sobrienvalue_coerce_array (struct value *arg1)
85019370Spst{
851130803Smarcel  struct type *type = check_typedef (VALUE_TYPE (arg1));
85219370Spst
85319370Spst  if (VALUE_LVAL (arg1) != lval_memory)
85419370Spst    error ("Attempt to take address of value not located in memory.");
85519370Spst
85698944Sobrien  return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
85798944Sobrien			     (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
85819370Spst}
85919370Spst
86019370Spst/* Given a value which is a function, return a value which is a pointer
86119370Spst   to it.  */
86219370Spst
86398944Sobrienstruct value *
86498944Sobrienvalue_coerce_function (struct value *arg1)
86519370Spst{
86698944Sobrien  struct value *retval;
86719370Spst
86819370Spst  if (VALUE_LVAL (arg1) != lval_memory)
86919370Spst    error ("Attempt to take address of value not located in memory.");
87019370Spst
87198944Sobrien  retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
87298944Sobrien			       (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
87346283Sdfr  VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
87446283Sdfr  return retval;
87598944Sobrien}
87619370Spst
87719370Spst/* Return a pointer value for the object for which ARG1 is the contents.  */
87819370Spst
87998944Sobrienstruct value *
88098944Sobrienvalue_addr (struct value *arg1)
88119370Spst{
88298944Sobrien  struct value *arg2;
88346283Sdfr
88419370Spst  struct type *type = check_typedef (VALUE_TYPE (arg1));
88519370Spst  if (TYPE_CODE (type) == TYPE_CODE_REF)
88619370Spst    {
88719370Spst      /* Copy the value, but change the type from (T&) to (T*).
88898944Sobrien         We keep the same location information, which is efficient,
88998944Sobrien         and allows &(&X) to get the location containing the reference. */
89046283Sdfr      arg2 = value_copy (arg1);
89119370Spst      VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
89219370Spst      return arg2;
89319370Spst    }
89419370Spst  if (TYPE_CODE (type) == TYPE_CODE_FUNC)
89519370Spst    return value_coerce_function (arg1);
89619370Spst
89719370Spst  if (VALUE_LVAL (arg1) != lval_memory)
89819370Spst    error ("Attempt to take address of value not located in memory.");
89919370Spst
90098944Sobrien  /* Get target memory address */
90198944Sobrien  arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
90298944Sobrien			     (VALUE_ADDRESS (arg1)
90398944Sobrien			      + VALUE_OFFSET (arg1)
90498944Sobrien			      + VALUE_EMBEDDED_OFFSET (arg1)));
90546283Sdfr
90646283Sdfr  /* This may be a pointer to a base subobject; so remember the
90798944Sobrien     full derived object's type ... */
90898944Sobrien  arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
90998944Sobrien  /* ... and also the relative position of the subobject in the full object */
91098944Sobrien  VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
91146283Sdfr  VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
91246283Sdfr  return arg2;
91319370Spst}
91419370Spst
91519370Spst/* Given a value of a pointer type, apply the C unary * operator to it.  */
91619370Spst
91798944Sobrienstruct value *
91898944Sobrienvalue_ind (struct value *arg1)
91919370Spst{
92046283Sdfr  struct type *base_type;
92198944Sobrien  struct value *arg2;
92246283Sdfr
92319370Spst  COERCE_ARRAY (arg1);
92419370Spst
92546283Sdfr  base_type = check_typedef (VALUE_TYPE (arg1));
92646283Sdfr
92746283Sdfr  if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
92819370Spst    error ("not implemented: member types in value_ind");
92919370Spst
93019370Spst  /* Allow * on an integer so we can cast it to whatever we want.
93119370Spst     This returns an int, which seems like the most C-like thing
93219370Spst     to do.  "long long" variables are rare enough that
93319370Spst     BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
93446283Sdfr  if (TYPE_CODE (base_type) == TYPE_CODE_INT)
935130803Smarcel    return value_at_lazy (builtin_type_int,
936130803Smarcel			  (CORE_ADDR) value_as_long (arg1),
937130803Smarcel			  VALUE_BFD_SECTION (arg1));
93846283Sdfr  else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
93946283Sdfr    {
94046283Sdfr      struct type *enc_type;
94146283Sdfr      /* We may be pointing to something embedded in a larger object */
94298944Sobrien      /* Get the real type of the enclosing object */
94346283Sdfr      enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
94446283Sdfr      enc_type = TYPE_TARGET_TYPE (enc_type);
94598944Sobrien      /* Retrieve the enclosing object pointed to */
94698944Sobrien      arg2 = value_at_lazy (enc_type,
94798944Sobrien		   value_as_address (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
94898944Sobrien			    VALUE_BFD_SECTION (arg1));
94998944Sobrien      /* Re-adjust type */
95046283Sdfr      VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
95146283Sdfr      /* Add embedding info */
95298944Sobrien      arg2 = value_change_enclosing_type (arg2, enc_type);
95346283Sdfr      VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
95446283Sdfr
95546283Sdfr      /* We may be pointing to an object of some derived type */
95646283Sdfr      arg2 = value_full_object (arg2, NULL, 0, 0, 0);
95746283Sdfr      return arg2;
95846283Sdfr    }
95946283Sdfr
96019370Spst  error ("Attempt to take contents of a non-pointer value.");
96198944Sobrien  return 0;			/* For lint -- never reached */
96219370Spst}
96319370Spst
96419370Spst/* Pushing small parts of stack frames.  */
96519370Spst
96619370Spst/* Push one word (the size of object that a register holds).  */
96719370Spst
96819370SpstCORE_ADDR
96998944Sobrienpush_word (CORE_ADDR sp, ULONGEST word)
97019370Spst{
971130803Smarcel  int len = DEPRECATED_REGISTER_SIZE;
972130803Smarcel  char buffer[MAX_REGISTER_SIZE];
97319370Spst
97419370Spst  store_unsigned_integer (buffer, len, word);
97546283Sdfr  if (INNER_THAN (1, 2))
97646283Sdfr    {
97746283Sdfr      /* stack grows downward */
97846283Sdfr      sp -= len;
97946283Sdfr      write_memory (sp, buffer, len);
98046283Sdfr    }
98146283Sdfr  else
98246283Sdfr    {
98346283Sdfr      /* stack grows upward */
98446283Sdfr      write_memory (sp, buffer, len);
98546283Sdfr      sp += len;
98646283Sdfr    }
98719370Spst
98819370Spst  return sp;
98919370Spst}
99019370Spst
99119370Spst/* Push LEN bytes with data at BUFFER.  */
99219370Spst
99319370SpstCORE_ADDR
99498944Sobrienpush_bytes (CORE_ADDR sp, char *buffer, int len)
99519370Spst{
99646283Sdfr  if (INNER_THAN (1, 2))
99746283Sdfr    {
99846283Sdfr      /* stack grows downward */
99946283Sdfr      sp -= len;
100046283Sdfr      write_memory (sp, buffer, len);
100146283Sdfr    }
100246283Sdfr  else
100346283Sdfr    {
100446283Sdfr      /* stack grows upward */
100546283Sdfr      write_memory (sp, buffer, len);
100646283Sdfr      sp += len;
100746283Sdfr    }
100819370Spst
100919370Spst  return sp;
101019370Spst}
101119370Spst
101298944Sobrien#ifndef PARM_BOUNDARY
101398944Sobrien#define PARM_BOUNDARY (0)
101498944Sobrien#endif
101519370Spst
101698944Sobrien/* Push onto the stack the specified value VALUE.  Pad it correctly for
101798944Sobrien   it to be an argument to a function.  */
101846283Sdfr
101919370Spststatic CORE_ADDR
1020130803Smarcelvalue_push (CORE_ADDR sp, struct value *arg)
102119370Spst{
1022130803Smarcel  int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
1023130803Smarcel  int container_len = len;
1024130803Smarcel  int offset;
102519370Spst
102698944Sobrien  /* How big is the container we're going to put this value in?  */
102798944Sobrien  if (PARM_BOUNDARY)
102898944Sobrien    container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1)
102998944Sobrien		     & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1));
103098944Sobrien
103198944Sobrien  /* Are we going to put it at the high or low end of the container?  */
103298944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
103398944Sobrien    offset = container_len - len;
103498944Sobrien  else
103598944Sobrien    offset = 0;
103698944Sobrien
103746283Sdfr  if (INNER_THAN (1, 2))
103846283Sdfr    {
103946283Sdfr      /* stack grows downward */
104098944Sobrien      sp -= container_len;
104198944Sobrien      write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
104246283Sdfr    }
104346283Sdfr  else
104446283Sdfr    {
104546283Sdfr      /* stack grows upward */
104698944Sobrien      write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
104798944Sobrien      sp += container_len;
104846283Sdfr    }
104919370Spst
105019370Spst  return sp;
105119370Spst}
105219370Spst
105398944SobrienCORE_ADDR
1054130803Smarcellegacy_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1055130803Smarcel		       int struct_return, CORE_ADDR struct_addr)
105698944Sobrien{
105798944Sobrien  /* ASSERT ( !struct_return); */
105898944Sobrien  int i;
105998944Sobrien  for (i = nargs - 1; i >= 0; i--)
106098944Sobrien    sp = value_push (sp, args[i]);
106198944Sobrien  return sp;
106298944Sobrien}
106346283Sdfr
106419370Spst/* Create a value for an array by allocating space in the inferior, copying
106519370Spst   the data into that space, and then setting up an array value.
106619370Spst
106719370Spst   The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
106819370Spst   populated from the values passed in ELEMVEC.
106919370Spst
107019370Spst   The element type of the array is inherited from the type of the
107119370Spst   first element, and all elements must have the same size (though we
107219370Spst   don't currently enforce any restriction on their types). */
107319370Spst
107498944Sobrienstruct value *
107598944Sobrienvalue_array (int lowbound, int highbound, struct value **elemvec)
107619370Spst{
107719370Spst  int nelem;
107819370Spst  int idx;
107919370Spst  unsigned int typelength;
108098944Sobrien  struct value *val;
108119370Spst  struct type *rangetype;
108219370Spst  struct type *arraytype;
108319370Spst  CORE_ADDR addr;
108419370Spst
108519370Spst  /* Validate that the bounds are reasonable and that each of the elements
108619370Spst     have the same size. */
108719370Spst
108819370Spst  nelem = highbound - lowbound + 1;
108919370Spst  if (nelem <= 0)
109019370Spst    {
109119370Spst      error ("bad array bounds (%d, %d)", lowbound, highbound);
109219370Spst    }
109346283Sdfr  typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
109419370Spst  for (idx = 1; idx < nelem; idx++)
109519370Spst    {
109646283Sdfr      if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
109719370Spst	{
109819370Spst	  error ("array elements must all be the same size");
109919370Spst	}
110019370Spst    }
110119370Spst
110219370Spst  rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
110319370Spst				 lowbound, highbound);
110498944Sobrien  arraytype = create_array_type ((struct type *) NULL,
110598944Sobrien			      VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
110619370Spst
110719370Spst  if (!current_language->c_style_arrays)
110819370Spst    {
110919370Spst      val = allocate_value (arraytype);
111019370Spst      for (idx = 0; idx < nelem; idx++)
111119370Spst	{
111246283Sdfr	  memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
111346283Sdfr		  VALUE_CONTENTS_ALL (elemvec[idx]),
111419370Spst		  typelength);
111519370Spst	}
111646283Sdfr      VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
111719370Spst      return val;
111819370Spst    }
111919370Spst
112019370Spst  /* Allocate space to store the array in the inferior, and then initialize
112119370Spst     it by copying in each element.  FIXME:  Is it worth it to create a
112219370Spst     local buffer in which to collect each value and then write all the
112319370Spst     bytes in one operation? */
112419370Spst
112519370Spst  addr = allocate_space_in_inferior (nelem * typelength);
112619370Spst  for (idx = 0; idx < nelem; idx++)
112719370Spst    {
112846283Sdfr      write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
112919370Spst		    typelength);
113019370Spst    }
113119370Spst
113219370Spst  /* Create the array type and set up an array value to be evaluated lazily. */
113319370Spst
113446283Sdfr  val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
113519370Spst  return (val);
113619370Spst}
113719370Spst
113819370Spst/* Create a value for a string constant by allocating space in the inferior,
113919370Spst   copying the data into that space, and returning the address with type
114019370Spst   TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
114119370Spst   of characters.
114219370Spst   Note that string types are like array of char types with a lower bound of
114319370Spst   zero and an upper bound of LEN - 1.  Also note that the string may contain
114419370Spst   embedded null bytes. */
114519370Spst
114698944Sobrienstruct value *
114798944Sobrienvalue_string (char *ptr, int len)
114819370Spst{
114998944Sobrien  struct value *val;
115019370Spst  int lowbound = current_language->string_lower_bound;
115119370Spst  struct type *rangetype = create_range_type ((struct type *) NULL,
115219370Spst					      builtin_type_int,
115319370Spst					      lowbound, len + lowbound - 1);
115419370Spst  struct type *stringtype
115598944Sobrien  = create_string_type ((struct type *) NULL, rangetype);
115619370Spst  CORE_ADDR addr;
115719370Spst
115819370Spst  if (current_language->c_style_arrays == 0)
115919370Spst    {
116019370Spst      val = allocate_value (stringtype);
116119370Spst      memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
116219370Spst      return val;
116319370Spst    }
116419370Spst
116519370Spst
116619370Spst  /* Allocate space to store the string in the inferior, and then
116719370Spst     copy LEN bytes from PTR in gdb to that address in the inferior. */
116819370Spst
116919370Spst  addr = allocate_space_in_inferior (len);
117019370Spst  write_memory (addr, ptr, len);
117119370Spst
117246283Sdfr  val = value_at_lazy (stringtype, addr, NULL);
117319370Spst  return (val);
117419370Spst}
117519370Spst
117698944Sobrienstruct value *
117798944Sobrienvalue_bitstring (char *ptr, int len)
117819370Spst{
117998944Sobrien  struct value *val;
118019370Spst  struct type *domain_type = create_range_type (NULL, builtin_type_int,
118119370Spst						0, len - 1);
118298944Sobrien  struct type *type = create_set_type ((struct type *) NULL, domain_type);
118319370Spst  TYPE_CODE (type) = TYPE_CODE_BITSTRING;
118419370Spst  val = allocate_value (type);
118519370Spst  memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
118619370Spst  return val;
118719370Spst}
118819370Spst
118919370Spst/* See if we can pass arguments in T2 to a function which takes arguments
1190130803Smarcel   of types T1.  T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1191130803Smarcel   vector.  If some arguments need coercion of some sort, then the coerced
1192130803Smarcel   values are written into T2.  Return value is 0 if the arguments could be
1193130803Smarcel   matched, or the position at which they differ if not.
119419370Spst
119519370Spst   STATICP is nonzero if the T1 argument list came from a
1196130803Smarcel   static member function.  T2 will still include the ``this'' pointer,
1197130803Smarcel   but it will be skipped.
119819370Spst
119919370Spst   For non-static member functions, we ignore the first argument,
120019370Spst   which is the type of the instance variable.  This is because we want
120119370Spst   to handle calls with objects from derived classes.  This is not
120219370Spst   entirely correct: we should actually check to make sure that a
120319370Spst   requested operation is type secure, shouldn't we?  FIXME.  */
120419370Spst
120519370Spststatic int
1206130803Smarceltypecmp (int staticp, int varargs, int nargs,
1207130803Smarcel	 struct field t1[], struct value *t2[])
120819370Spst{
120919370Spst  int i;
121019370Spst
121119370Spst  if (t2 == 0)
1212130803Smarcel    internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1213130803Smarcel
1214130803Smarcel  /* Skip ``this'' argument if applicable.  T2 will always include THIS.  */
1215130803Smarcel  if (staticp)
1216130803Smarcel    t2 ++;
1217130803Smarcel
1218130803Smarcel  for (i = 0;
1219130803Smarcel       (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1220130803Smarcel       i++)
122119370Spst    {
122298944Sobrien      struct type *tt1, *tt2;
1223130803Smarcel
122498944Sobrien      if (!t2[i])
122598944Sobrien	return i + 1;
1226130803Smarcel
1227130803Smarcel      tt1 = check_typedef (t1[i].type);
122898944Sobrien      tt2 = check_typedef (VALUE_TYPE (t2[i]));
1229130803Smarcel
123019370Spst      if (TYPE_CODE (tt1) == TYPE_CODE_REF
123198944Sobrien      /* We should be doing hairy argument matching, as below.  */
123219370Spst	  && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
123319370Spst	{
123419370Spst	  if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
123519370Spst	    t2[i] = value_coerce_array (t2[i]);
123619370Spst	  else
123719370Spst	    t2[i] = value_addr (t2[i]);
123819370Spst	  continue;
123919370Spst	}
124019370Spst
124198944Sobrien      /* djb - 20000715 - Until the new type structure is in the
124298944Sobrien	 place, and we can attempt things like implicit conversions,
124398944Sobrien	 we need to do this so you can take something like a map<const
124498944Sobrien	 char *>, and properly access map["hello"], because the
124598944Sobrien	 argument to [] will be a reference to a pointer to a char,
124698944Sobrien	 and the argument will be a pointer to a char. */
124798944Sobrien      while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
124898944Sobrien	      TYPE_CODE (tt1) == TYPE_CODE_PTR)
124919370Spst	{
125098944Sobrien	  tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
125119370Spst	}
125298944Sobrien      while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
125398944Sobrien	      TYPE_CODE(tt2) == TYPE_CODE_PTR ||
125498944Sobrien	      TYPE_CODE(tt2) == TYPE_CODE_REF)
125598944Sobrien	{
125698944Sobrien	  tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
125798944Sobrien	}
125898944Sobrien      if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
125998944Sobrien	continue;
126019370Spst      /* Array to pointer is a `trivial conversion' according to the ARM.  */
126119370Spst
126219370Spst      /* We should be doing much hairier argument matching (see section 13.2
126398944Sobrien         of the ARM), but as a quick kludge, just check for the same type
126498944Sobrien         code.  */
1265130803Smarcel      if (TYPE_CODE (t1[i].type) != TYPE_CODE (VALUE_TYPE (t2[i])))
126698944Sobrien	return i + 1;
126719370Spst    }
1268130803Smarcel  if (varargs || t2[i] == NULL)
126998944Sobrien    return 0;
1270130803Smarcel  return i + 1;
127119370Spst}
127219370Spst
127319370Spst/* Helper function used by value_struct_elt to recurse through baseclasses.
127419370Spst   Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
127519370Spst   and search in it assuming it has (class) type TYPE.
127619370Spst   If found, return value, else return NULL.
127719370Spst
127819370Spst   If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
127919370Spst   look for a baseclass named NAME.  */
128019370Spst
128198944Sobrienstatic struct value *
128298944Sobriensearch_struct_field (char *name, struct value *arg1, int offset,
1283130803Smarcel		     struct type *type, int looking_for_baseclass)
128419370Spst{
128519370Spst  int i;
128646283Sdfr  int nbases = TYPE_N_BASECLASSES (type);
128719370Spst
128819370Spst  CHECK_TYPEDEF (type);
128919370Spst
129098944Sobrien  if (!looking_for_baseclass)
129146283Sdfr    for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
129219370Spst      {
129319370Spst	char *t_field_name = TYPE_FIELD_NAME (type, i);
129419370Spst
129598944Sobrien	if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
129619370Spst	  {
129798944Sobrien	    struct value *v;
129819370Spst	    if (TYPE_FIELD_STATIC (type, i))
1299130803Smarcel	      {
1300130803Smarcel		v = value_static_field (type, i);
1301130803Smarcel		if (v == 0)
1302130803Smarcel		  error ("field %s is nonexistent or has been optimised out",
1303130803Smarcel			 name);
1304130803Smarcel	      }
130519370Spst	    else
1306130803Smarcel	      {
1307130803Smarcel		v = value_primitive_field (arg1, offset, i, type);
1308130803Smarcel		if (v == 0)
1309130803Smarcel		  error ("there is no field named %s", name);
1310130803Smarcel	      }
131119370Spst	    return v;
131219370Spst	  }
131319370Spst
131419370Spst	if (t_field_name
131519370Spst	    && (t_field_name[0] == '\0'
131619370Spst		|| (TYPE_CODE (type) == TYPE_CODE_UNION
131798944Sobrien		    && (strcmp_iw (t_field_name, "else") == 0))))
131819370Spst	  {
131919370Spst	    struct type *field_type = TYPE_FIELD_TYPE (type, i);
132019370Spst	    if (TYPE_CODE (field_type) == TYPE_CODE_UNION
132119370Spst		|| TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
132219370Spst	      {
132319370Spst		/* Look for a match through the fields of an anonymous union,
132419370Spst		   or anonymous struct.  C++ provides anonymous unions.
132519370Spst
1326130803Smarcel		   In the GNU Chill (now deleted from GDB)
1327130803Smarcel		   implementation of variant record types, each
1328130803Smarcel		   <alternative field> has an (anonymous) union type,
1329130803Smarcel		   each member of the union represents a <variant
1330130803Smarcel		   alternative>.  Each <variant alternative> is
1331130803Smarcel		   represented as a struct, with a member for each
1332130803Smarcel		   <variant field>.  */
133398944Sobrien
133498944Sobrien		struct value *v;
133519370Spst		int new_offset = offset;
133619370Spst
1337130803Smarcel		/* This is pretty gross.  In G++, the offset in an
1338130803Smarcel		   anonymous union is relative to the beginning of the
1339130803Smarcel		   enclosing struct.  In the GNU Chill (now deleted
1340130803Smarcel		   from GDB) implementation of variant records, the
1341130803Smarcel		   bitpos is zero in an anonymous union field, so we
134219370Spst		   have to add the offset of the union here. */
134319370Spst		if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
134419370Spst		    || (TYPE_NFIELDS (field_type) > 0
134519370Spst			&& TYPE_FIELD_BITPOS (field_type, 0) == 0))
134619370Spst		  new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
134719370Spst
134819370Spst		v = search_struct_field (name, arg1, new_offset, field_type,
134919370Spst					 looking_for_baseclass);
135019370Spst		if (v)
135119370Spst		  return v;
135219370Spst	      }
135319370Spst	  }
135419370Spst      }
135519370Spst
135698944Sobrien  for (i = 0; i < nbases; i++)
135719370Spst    {
135898944Sobrien      struct value *v;
135919370Spst      struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
136019370Spst      /* If we are looking for baseclasses, this is what we get when we
136198944Sobrien         hit them.  But it could happen that the base part's member name
136298944Sobrien         is not yet filled in.  */
136319370Spst      int found_baseclass = (looking_for_baseclass
136419370Spst			     && TYPE_BASECLASS_NAME (type, i) != NULL
136598944Sobrien			     && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
136619370Spst
136719370Spst      if (BASETYPE_VIA_VIRTUAL (type, i))
136819370Spst	{
136946283Sdfr	  int boffset;
137098944Sobrien	  struct value *v2 = allocate_value (basetype);
137146283Sdfr
137219370Spst	  boffset = baseclass_offset (type, i,
137346283Sdfr				      VALUE_CONTENTS (arg1) + offset,
137446283Sdfr				      VALUE_ADDRESS (arg1)
137598944Sobrien				      + VALUE_OFFSET (arg1) + offset);
137619370Spst	  if (boffset == -1)
137719370Spst	    error ("virtual baseclass botch");
137846283Sdfr
137946283Sdfr	  /* The virtual base class pointer might have been clobbered by the
138046283Sdfr	     user program. Make sure that it still points to a valid memory
138146283Sdfr	     location.  */
138246283Sdfr
138346283Sdfr	  boffset += offset;
138446283Sdfr	  if (boffset < 0 || boffset >= TYPE_LENGTH (type))
138519370Spst	    {
138646283Sdfr	      CORE_ADDR base_addr;
138798944Sobrien
138846283Sdfr	      base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
138946283Sdfr	      if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
139046283Sdfr				      TYPE_LENGTH (basetype)) != 0)
139146283Sdfr		error ("virtual baseclass botch");
139246283Sdfr	      VALUE_LVAL (v2) = lval_memory;
139346283Sdfr	      VALUE_ADDRESS (v2) = base_addr;
139446283Sdfr	    }
139546283Sdfr	  else
139646283Sdfr	    {
139719370Spst	      VALUE_LVAL (v2) = VALUE_LVAL (arg1);
139819370Spst	      VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
139946283Sdfr	      VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
140019370Spst	      if (VALUE_LAZY (arg1))
140119370Spst		VALUE_LAZY (v2) = 1;
140219370Spst	      else
140319370Spst		memcpy (VALUE_CONTENTS_RAW (v2),
140446283Sdfr			VALUE_CONTENTS_RAW (arg1) + boffset,
140519370Spst			TYPE_LENGTH (basetype));
140619370Spst	    }
140746283Sdfr
140846283Sdfr	  if (found_baseclass)
140946283Sdfr	    return v2;
141046283Sdfr	  v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
141119370Spst				   looking_for_baseclass);
141219370Spst	}
141319370Spst      else if (found_baseclass)
141419370Spst	v = value_primitive_field (arg1, offset, i, type);
141519370Spst      else
141619370Spst	v = search_struct_field (name, arg1,
141798944Sobrien			       offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
141819370Spst				 basetype, looking_for_baseclass);
141998944Sobrien      if (v)
142098944Sobrien	return v;
142119370Spst    }
142219370Spst  return NULL;
142319370Spst}
142419370Spst
142546283Sdfr
142646283Sdfr/* Return the offset (in bytes) of the virtual base of type BASETYPE
142746283Sdfr * in an object pointed to by VALADDR (on the host), assumed to be of
142846283Sdfr * type TYPE.  OFFSET is number of bytes beyond start of ARG to start
142946283Sdfr * looking (in case VALADDR is the contents of an enclosing object).
143046283Sdfr *
143146283Sdfr * This routine recurses on the primary base of the derived class because
143246283Sdfr * the virtual base entries of the primary base appear before the other
143346283Sdfr * virtual base entries.
143446283Sdfr *
143546283Sdfr * If the virtual base is not found, a negative integer is returned.
143646283Sdfr * The magnitude of the negative integer is the number of entries in
143746283Sdfr * the virtual table to skip over (entries corresponding to various
143846283Sdfr * ancestral classes in the chain of primary bases).
143946283Sdfr *
144046283Sdfr * Important: This assumes the HP / Taligent C++ runtime
144146283Sdfr * conventions. Use baseclass_offset() instead to deal with g++
144246283Sdfr * conventions.  */
144346283Sdfr
144446283Sdfrvoid
144598944Sobrienfind_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
144698944Sobrien		      int offset, int *boffset_p, int *skip_p)
144746283Sdfr{
144898944Sobrien  int boffset;			/* offset of virtual base */
144998944Sobrien  int index;			/* displacement to use in virtual table */
145046283Sdfr  int skip;
145146283Sdfr
145298944Sobrien  struct value *vp;
145398944Sobrien  CORE_ADDR vtbl;		/* the virtual table pointer */
145498944Sobrien  struct type *pbc;		/* the primary base class */
145598944Sobrien
145646283Sdfr  /* Look for the virtual base recursively in the primary base, first.
145746283Sdfr   * This is because the derived class object and its primary base
145846283Sdfr   * subobject share the primary virtual table.  */
145998944Sobrien
146046283Sdfr  boffset = 0;
146198944Sobrien  pbc = TYPE_PRIMARY_BASE (type);
146246283Sdfr  if (pbc)
146346283Sdfr    {
146446283Sdfr      find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
146546283Sdfr      if (skip < 0)
146698944Sobrien	{
146798944Sobrien	  *boffset_p = boffset;
146898944Sobrien	  *skip_p = -1;
146998944Sobrien	  return;
147098944Sobrien	}
147146283Sdfr    }
147246283Sdfr  else
147346283Sdfr    skip = 0;
147446283Sdfr
147546283Sdfr
147646283Sdfr  /* Find the index of the virtual base according to HP/Taligent
147746283Sdfr     runtime spec. (Depth-first, left-to-right.)  */
147846283Sdfr  index = virtual_base_index_skip_primaries (basetype, type);
147946283Sdfr
148098944Sobrien  if (index < 0)
148198944Sobrien    {
148298944Sobrien      *skip_p = skip + virtual_base_list_length_skip_primaries (type);
148398944Sobrien      *boffset_p = 0;
148498944Sobrien      return;
148598944Sobrien    }
148646283Sdfr
148798944Sobrien  /* pai: FIXME -- 32x64 possible problem */
148846283Sdfr  /* First word (4 bytes) in object layout is the vtable pointer */
148998944Sobrien  vtbl = *(CORE_ADDR *) (valaddr + offset);
149046283Sdfr
149198944Sobrien  /* Before the constructor is invoked, things are usually zero'd out. */
149246283Sdfr  if (vtbl == 0)
149346283Sdfr    error ("Couldn't find virtual table -- object may not be constructed yet.");
149446283Sdfr
149546283Sdfr
149646283Sdfr  /* Find virtual base's offset -- jump over entries for primary base
149746283Sdfr   * ancestors, then use the index computed above.  But also adjust by
149846283Sdfr   * HP_ACC_VBASE_START for the vtable slots before the start of the
149946283Sdfr   * virtual base entries.  Offset is negative -- virtual base entries
150046283Sdfr   * appear _before_ the address point of the virtual table. */
150146283Sdfr
150298944Sobrien  /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
150398944Sobrien     & use long type */
150498944Sobrien
150546283Sdfr  /* epstein : FIXME -- added param for overlay section. May not be correct */
150698944Sobrien  vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
150746283Sdfr  boffset = value_as_long (vp);
150846283Sdfr  *skip_p = -1;
150946283Sdfr  *boffset_p = boffset;
151046283Sdfr  return;
151146283Sdfr}
151246283Sdfr
151346283Sdfr
151419370Spst/* Helper function used by value_struct_elt to recurse through baseclasses.
151519370Spst   Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
151619370Spst   and search in it assuming it has (class) type TYPE.
151719370Spst   If found, return value, else if name matched and args not return (value)-1,
151819370Spst   else return NULL. */
151919370Spst
152098944Sobrienstatic struct value *
152198944Sobriensearch_struct_method (char *name, struct value **arg1p,
152298944Sobrien		      struct value **args, int offset,
1523130803Smarcel		      int *static_memfuncp, struct type *type)
152419370Spst{
152519370Spst  int i;
152698944Sobrien  struct value *v;
152719370Spst  int name_matched = 0;
152819370Spst  char dem_opname[64];
152919370Spst
153019370Spst  CHECK_TYPEDEF (type);
153119370Spst  for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
153219370Spst    {
153319370Spst      char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
153446283Sdfr      /* FIXME!  May need to check for ARM demangling here */
153598944Sobrien      if (strncmp (t_field_name, "__", 2) == 0 ||
153698944Sobrien	  strncmp (t_field_name, "op", 2) == 0 ||
153798944Sobrien	  strncmp (t_field_name, "type", 4) == 0)
153819370Spst	{
153998944Sobrien	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
154019370Spst	    t_field_name = dem_opname;
154198944Sobrien	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
154298944Sobrien	    t_field_name = dem_opname;
154319370Spst	}
154498944Sobrien      if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
154519370Spst	{
154619370Spst	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
154719370Spst	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
154898944Sobrien	  name_matched = 1;
154919370Spst
1550130803Smarcel	  check_stub_method_group (type, i);
155119370Spst	  if (j > 0 && args == 0)
155246283Sdfr	    error ("cannot resolve overloaded method `%s': no arguments supplied", name);
155398944Sobrien	  else if (j == 0 && args == 0)
155419370Spst	    {
155598944Sobrien	      v = value_fn_field (arg1p, f, j, type, offset);
155698944Sobrien	      if (v != NULL)
155798944Sobrien		return v;
155819370Spst	    }
155998944Sobrien	  else
156098944Sobrien	    while (j >= 0)
156198944Sobrien	      {
156298944Sobrien		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1563130803Smarcel			      TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1564130803Smarcel			      TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
156598944Sobrien			      TYPE_FN_FIELD_ARGS (f, j), args))
156698944Sobrien		  {
156798944Sobrien		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
156898944Sobrien		      return value_virtual_fn_field (arg1p, f, j, type, offset);
156998944Sobrien		    if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
157098944Sobrien		      *static_memfuncp = 1;
157198944Sobrien		    v = value_fn_field (arg1p, f, j, type, offset);
157298944Sobrien		    if (v != NULL)
157398944Sobrien		      return v;
157498944Sobrien		  }
157598944Sobrien		j--;
157698944Sobrien	      }
157719370Spst	}
157819370Spst    }
157919370Spst
158019370Spst  for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
158119370Spst    {
158219370Spst      int base_offset;
158319370Spst
158419370Spst      if (BASETYPE_VIA_VIRTUAL (type, i))
158519370Spst	{
158698944Sobrien	  if (TYPE_HAS_VTABLE (type))
158798944Sobrien	    {
158898944Sobrien	      /* HP aCC compiled type, search for virtual base offset
158998944Sobrien	         according to HP/Taligent runtime spec.  */
159098944Sobrien	      int skip;
159198944Sobrien	      find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
159298944Sobrien				    VALUE_CONTENTS_ALL (*arg1p),
159398944Sobrien				    offset + VALUE_EMBEDDED_OFFSET (*arg1p),
159498944Sobrien				    &base_offset, &skip);
159598944Sobrien	      if (skip >= 0)
159698944Sobrien		error ("Virtual base class offset not found in vtable");
159798944Sobrien	    }
159898944Sobrien	  else
159998944Sobrien	    {
160098944Sobrien	      struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
160198944Sobrien	      char *base_valaddr;
160246283Sdfr
160398944Sobrien	      /* The virtual base class pointer might have been clobbered by the
160498944Sobrien	         user program. Make sure that it still points to a valid memory
160598944Sobrien	         location.  */
160646283Sdfr
160798944Sobrien	      if (offset < 0 || offset >= TYPE_LENGTH (type))
160898944Sobrien		{
160998944Sobrien		  base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
161098944Sobrien		  if (target_read_memory (VALUE_ADDRESS (*arg1p)
161198944Sobrien					  + VALUE_OFFSET (*arg1p) + offset,
161298944Sobrien					  base_valaddr,
161398944Sobrien					  TYPE_LENGTH (baseclass)) != 0)
161498944Sobrien		    error ("virtual baseclass botch");
161598944Sobrien		}
161698944Sobrien	      else
161798944Sobrien		base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
161846283Sdfr
161998944Sobrien	      base_offset =
162098944Sobrien		baseclass_offset (type, i, base_valaddr,
162198944Sobrien				  VALUE_ADDRESS (*arg1p)
162298944Sobrien				  + VALUE_OFFSET (*arg1p) + offset);
162398944Sobrien	      if (base_offset == -1)
162498944Sobrien		error ("virtual baseclass botch");
162598944Sobrien	    }
162698944Sobrien	}
162719370Spst      else
162819370Spst	{
162919370Spst	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
163098944Sobrien	}
163119370Spst      v = search_struct_method (name, arg1p, args, base_offset + offset,
163219370Spst				static_memfuncp, TYPE_BASECLASS (type, i));
163398944Sobrien      if (v == (struct value *) - 1)
163419370Spst	{
163519370Spst	  name_matched = 1;
163619370Spst	}
163719370Spst      else if (v)
163819370Spst	{
163919370Spst/* FIXME-bothner:  Why is this commented out?  Why is it here?  */
164098944Sobrien/*        *arg1p = arg1_tmp; */
164119370Spst	  return v;
164298944Sobrien	}
164319370Spst    }
164498944Sobrien  if (name_matched)
164598944Sobrien    return (struct value *) - 1;
164698944Sobrien  else
164798944Sobrien    return NULL;
164819370Spst}
164919370Spst
165019370Spst/* Given *ARGP, a value of type (pointer to a)* structure/union,
165119370Spst   extract the component named NAME from the ultimate target structure/union
165219370Spst   and return it as a value with its appropriate type.
165319370Spst   ERR is used in the error message if *ARGP's type is wrong.
165419370Spst
165519370Spst   C++: ARGS is a list of argument types to aid in the selection of
165619370Spst   an appropriate method. Also, handle derived types.
165719370Spst
165819370Spst   STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
165919370Spst   where the truthvalue of whether the function that was resolved was
166019370Spst   a static member function or not is stored.
166119370Spst
166219370Spst   ERR is an error message to be printed in case the field is not found.  */
166319370Spst
166498944Sobrienstruct value *
166598944Sobrienvalue_struct_elt (struct value **argp, struct value **args,
166698944Sobrien		  char *name, int *static_memfuncp, char *err)
166719370Spst{
1668130803Smarcel  struct type *t;
166998944Sobrien  struct value *v;
167019370Spst
167119370Spst  COERCE_ARRAY (*argp);
167219370Spst
167319370Spst  t = check_typedef (VALUE_TYPE (*argp));
167419370Spst
167519370Spst  /* Follow pointers until we get to a non-pointer.  */
167619370Spst
167719370Spst  while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
167819370Spst    {
167919370Spst      *argp = value_ind (*argp);
168019370Spst      /* Don't coerce fn pointer to fn and then back again!  */
168119370Spst      if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
168219370Spst	COERCE_ARRAY (*argp);
168319370Spst      t = check_typedef (VALUE_TYPE (*argp));
168419370Spst    }
168519370Spst
168619370Spst  if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
168719370Spst    error ("not implemented: member type in value_struct_elt");
168819370Spst
168998944Sobrien  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
169019370Spst      && TYPE_CODE (t) != TYPE_CODE_UNION)
169119370Spst    error ("Attempt to extract a component of a value that is not a %s.", err);
169219370Spst
169319370Spst  /* Assume it's not, unless we see that it is.  */
169419370Spst  if (static_memfuncp)
169598944Sobrien    *static_memfuncp = 0;
169619370Spst
169719370Spst  if (!args)
169819370Spst    {
169919370Spst      /* if there are no arguments ...do this...  */
170019370Spst
170119370Spst      /* Try as a field first, because if we succeed, there
170298944Sobrien         is less work to be done.  */
170319370Spst      v = search_struct_field (name, *argp, 0, t, 0);
170419370Spst      if (v)
170519370Spst	return v;
170619370Spst
170719370Spst      /* C++: If it was not found as a data field, then try to
170819370Spst         return it as a pointer to a method.  */
170919370Spst
171019370Spst      if (destructor_name_p (name, t))
171119370Spst	error ("Cannot get value of destructor");
171219370Spst
171319370Spst      v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
171419370Spst
171598944Sobrien      if (v == (struct value *) - 1)
171619370Spst	error ("Cannot take address of a method");
171719370Spst      else if (v == 0)
171819370Spst	{
171919370Spst	  if (TYPE_NFN_FIELDS (t))
172019370Spst	    error ("There is no member or method named %s.", name);
172119370Spst	  else
172219370Spst	    error ("There is no member named %s.", name);
172319370Spst	}
172419370Spst      return v;
172519370Spst    }
172619370Spst
172719370Spst  if (destructor_name_p (name, t))
172819370Spst    {
172919370Spst      if (!args[1])
173019370Spst	{
173146283Sdfr	  /* Destructors are a special case.  */
173246283Sdfr	  int m_index, f_index;
173346283Sdfr
173446283Sdfr	  v = NULL;
173546283Sdfr	  if (get_destructor_fn_field (t, &m_index, &f_index))
173646283Sdfr	    {
173746283Sdfr	      v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
173846283Sdfr				  f_index, NULL, 0);
173946283Sdfr	    }
174046283Sdfr	  if (v == NULL)
174146283Sdfr	    error ("could not find destructor function named %s.", name);
174246283Sdfr	  else
174346283Sdfr	    return v;
174419370Spst	}
174519370Spst      else
174619370Spst	{
174719370Spst	  error ("destructor should not have any argument");
174819370Spst	}
174919370Spst    }
175019370Spst  else
175119370Spst    v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
175298944Sobrien
175398944Sobrien  if (v == (struct value *) - 1)
175419370Spst    {
175598944Sobrien      error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
175619370Spst    }
175719370Spst  else if (v == 0)
175819370Spst    {
175919370Spst      /* See if user tried to invoke data as function.  If so,
176098944Sobrien         hand it back.  If it's not callable (i.e., a pointer to function),
176198944Sobrien         gdb should give an error.  */
176219370Spst      v = search_struct_field (name, *argp, 0, t, 0);
176319370Spst    }
176419370Spst
176519370Spst  if (!v)
176619370Spst    error ("Structure has no component named %s.", name);
176719370Spst  return v;
176819370Spst}
176919370Spst
177046283Sdfr/* Search through the methods of an object (and its bases)
177146283Sdfr * to find a specified method. Return the pointer to the
177246283Sdfr * fn_field list of overloaded instances.
177346283Sdfr * Helper function for value_find_oload_list.
177446283Sdfr * ARGP is a pointer to a pointer to a value (the object)
177546283Sdfr * METHOD is a string containing the method name
177646283Sdfr * OFFSET is the offset within the value
177746283Sdfr * TYPE is the assumed type of the object
177846283Sdfr * NUM_FNS is the number of overloaded instances
177946283Sdfr * BASETYPE is set to the actual type of the subobject where the method is found
178046283Sdfr * BOFFSET is the offset of the base subobject where the method is found */
178146283Sdfr
178298944Sobrienstatic struct fn_field *
178398944Sobrienfind_method_list (struct value **argp, char *method, int offset,
1784130803Smarcel		  struct type *type, int *num_fns,
178598944Sobrien		  struct type **basetype, int *boffset)
178646283Sdfr{
178746283Sdfr  int i;
178898944Sobrien  struct fn_field *f;
178946283Sdfr  CHECK_TYPEDEF (type);
179046283Sdfr
179146283Sdfr  *num_fns = 0;
179246283Sdfr
179398944Sobrien  /* First check in object itself */
179498944Sobrien  for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
179546283Sdfr    {
179646283Sdfr      /* pai: FIXME What about operators and type conversions? */
179798944Sobrien      char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
179898944Sobrien      if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
179998944Sobrien	{
1800130803Smarcel	  int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1801130803Smarcel	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1802130803Smarcel
1803130803Smarcel	  *num_fns = len;
180498944Sobrien	  *basetype = type;
180598944Sobrien	  *boffset = offset;
1806130803Smarcel
1807130803Smarcel	  /* Resolve any stub methods.  */
1808130803Smarcel	  check_stub_method_group (type, i);
1809130803Smarcel
1810130803Smarcel	  return f;
181198944Sobrien	}
181246283Sdfr    }
181398944Sobrien
181446283Sdfr  /* Not found in object, check in base subobjects */
181546283Sdfr  for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
181646283Sdfr    {
181746283Sdfr      int base_offset;
181846283Sdfr      if (BASETYPE_VIA_VIRTUAL (type, i))
181946283Sdfr	{
182098944Sobrien	  if (TYPE_HAS_VTABLE (type))
182198944Sobrien	    {
182298944Sobrien	      /* HP aCC compiled type, search for virtual base offset
182398944Sobrien	       * according to HP/Taligent runtime spec.  */
182498944Sobrien	      int skip;
182598944Sobrien	      find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
182698944Sobrien				    VALUE_CONTENTS_ALL (*argp),
182798944Sobrien				    offset + VALUE_EMBEDDED_OFFSET (*argp),
182898944Sobrien				    &base_offset, &skip);
182998944Sobrien	      if (skip >= 0)
183098944Sobrien		error ("Virtual base class offset not found in vtable");
183198944Sobrien	    }
183298944Sobrien	  else
183398944Sobrien	    {
183498944Sobrien	      /* probably g++ runtime model */
183598944Sobrien	      base_offset = VALUE_OFFSET (*argp) + offset;
183698944Sobrien	      base_offset =
183798944Sobrien		baseclass_offset (type, i,
183898944Sobrien				  VALUE_CONTENTS (*argp) + base_offset,
183998944Sobrien				  VALUE_ADDRESS (*argp) + base_offset);
184098944Sobrien	      if (base_offset == -1)
184198944Sobrien		error ("virtual baseclass botch");
184298944Sobrien	    }
184398944Sobrien	}
184498944Sobrien      else
184598944Sobrien	/* non-virtual base, simply use bit position from debug info */
184646283Sdfr	{
184746283Sdfr	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
184898944Sobrien	}
184946283Sdfr      f = find_method_list (argp, method, base_offset + offset,
1850130803Smarcel			    TYPE_BASECLASS (type, i), num_fns, basetype,
1851130803Smarcel			    boffset);
185246283Sdfr      if (f)
185398944Sobrien	return f;
185446283Sdfr    }
185598944Sobrien  return NULL;
185646283Sdfr}
185746283Sdfr
185846283Sdfr/* Return the list of overloaded methods of a specified name.
185946283Sdfr * ARGP is a pointer to a pointer to a value (the object)
186046283Sdfr * METHOD is the method name
186146283Sdfr * OFFSET is the offset within the value contents
186246283Sdfr * NUM_FNS is the number of overloaded instances
186346283Sdfr * BASETYPE is set to the type of the base subobject that defines the method
186446283Sdfr * BOFFSET is the offset of the base subobject which defines the method */
186546283Sdfr
186646283Sdfrstruct fn_field *
186798944Sobrienvalue_find_oload_method_list (struct value **argp, char *method, int offset,
1868130803Smarcel			      int *num_fns, struct type **basetype,
1869130803Smarcel			      int *boffset)
187046283Sdfr{
187198944Sobrien  struct type *t;
187246283Sdfr
187346283Sdfr  t = check_typedef (VALUE_TYPE (*argp));
187446283Sdfr
187598944Sobrien  /* code snarfed from value_struct_elt */
187646283Sdfr  while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
187746283Sdfr    {
187846283Sdfr      *argp = value_ind (*argp);
187946283Sdfr      /* Don't coerce fn pointer to fn and then back again!  */
188046283Sdfr      if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
188146283Sdfr	COERCE_ARRAY (*argp);
188246283Sdfr      t = check_typedef (VALUE_TYPE (*argp));
188346283Sdfr    }
188498944Sobrien
188546283Sdfr  if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
188646283Sdfr    error ("Not implemented: member type in value_find_oload_lis");
188798944Sobrien
188898944Sobrien  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
188998944Sobrien      && TYPE_CODE (t) != TYPE_CODE_UNION)
189046283Sdfr    error ("Attempt to extract a component of a value that is not a struct or union");
189198944Sobrien
1892130803Smarcel  return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
189346283Sdfr}
189446283Sdfr
189546283Sdfr/* Given an array of argument types (ARGTYPES) (which includes an
189646283Sdfr   entry for "this" in the case of C++ methods), the number of
189746283Sdfr   arguments NARGS, the NAME of a function whether it's a method or
189846283Sdfr   not (METHOD), and the degree of laxness (LAX) in conforming to
189946283Sdfr   overload resolution rules in ANSI C++, find the best function that
190046283Sdfr   matches on the argument types according to the overload resolution
190146283Sdfr   rules.
190246283Sdfr
190346283Sdfr   In the case of class methods, the parameter OBJ is an object value
190446283Sdfr   in which to search for overloaded methods.
190546283Sdfr
190646283Sdfr   In the case of non-method functions, the parameter FSYM is a symbol
190746283Sdfr   corresponding to one of the overloaded functions.
190846283Sdfr
190946283Sdfr   Return value is an integer: 0 -> good match, 10 -> debugger applied
191046283Sdfr   non-standard coercions, 100 -> incompatible.
191146283Sdfr
191246283Sdfr   If a method is being searched for, VALP will hold the value.
191346283Sdfr   If a non-method is being searched for, SYMP will hold the symbol for it.
191446283Sdfr
191546283Sdfr   If a method is being searched for, and it is a static method,
191646283Sdfr   then STATICP will point to a non-zero value.
191746283Sdfr
191846283Sdfr   Note: This function does *not* check the value of
191946283Sdfr   overload_resolution.  Caller must check it to see whether overload
192046283Sdfr   resolution is permitted.
192198944Sobrien */
192246283Sdfr
192346283Sdfrint
192498944Sobrienfind_overload_match (struct type **arg_types, int nargs, char *name, int method,
192598944Sobrien		     int lax, struct value **objp, struct symbol *fsym,
192698944Sobrien		     struct value **valp, struct symbol **symp, int *staticp)
192746283Sdfr{
192898944Sobrien  struct value *obj = (objp ? *objp : NULL);
192998944Sobrien
1930130803Smarcel  int oload_champ;		/* Index of best overloaded function */
193198944Sobrien
193298944Sobrien  struct badness_vector *oload_champ_bv = NULL;		/* The measure for the current best match */
193398944Sobrien
193498944Sobrien  struct value *temp = obj;
193598944Sobrien  struct fn_field *fns_ptr = NULL;	/* For methods, the list of overloaded methods */
193698944Sobrien  struct symbol **oload_syms = NULL;	/* For non-methods, the list of overloaded function symbols */
193798944Sobrien  int num_fns = 0;		/* Number of overloaded instances being considered */
193898944Sobrien  struct type *basetype = NULL;
193946283Sdfr  int boffset;
1940130803Smarcel  int ix;
1941130803Smarcel  int static_offset;
1942130803Smarcel  struct cleanup *old_cleanups = NULL;
194346283Sdfr
1944130803Smarcel  const char *obj_type_name = NULL;
194598944Sobrien  char *func_name = NULL;
1946130803Smarcel  enum oload_classification match_quality;
194746283Sdfr
194846283Sdfr  /* Get the list of overloaded methods or functions */
194946283Sdfr  if (method)
195046283Sdfr    {
195146283Sdfr      obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
195246283Sdfr      /* Hack: evaluate_subexp_standard often passes in a pointer
195346283Sdfr         value rather than the object itself, so try again */
195446283Sdfr      if ((!obj_type_name || !*obj_type_name) &&
195598944Sobrien	  (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
195698944Sobrien	obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
195746283Sdfr
195846283Sdfr      fns_ptr = value_find_oload_method_list (&temp, name, 0,
195998944Sobrien					      &num_fns,
196098944Sobrien					      &basetype, &boffset);
196146283Sdfr      if (!fns_ptr || !num_fns)
196298944Sobrien	error ("Couldn't find method %s%s%s",
196398944Sobrien	       obj_type_name,
196498944Sobrien	       (obj_type_name && *obj_type_name) ? "::" : "",
196598944Sobrien	       name);
1966130803Smarcel      /* If we are dealing with stub method types, they should have
1967130803Smarcel	 been resolved by find_method_list via value_find_oload_method_list
1968130803Smarcel	 above.  */
1969130803Smarcel      gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1970130803Smarcel      oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1971130803Smarcel				      fns_ptr, oload_syms, &oload_champ_bv);
197246283Sdfr    }
197346283Sdfr  else
197446283Sdfr    {
1975130803Smarcel      const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1976130803Smarcel      func_name	= cp_func_name (qualified_name);
197746283Sdfr
197898944Sobrien      /* If the name is NULL this must be a C-style function.
197998944Sobrien         Just return the same symbol. */
1980130803Smarcel      if (func_name == NULL)
198198944Sobrien        {
198298944Sobrien	  *symp = fsym;
198398944Sobrien          return 0;
198498944Sobrien        }
198598944Sobrien
1986130803Smarcel      old_cleanups = make_cleanup (xfree, func_name);
1987130803Smarcel      make_cleanup (xfree, oload_syms);
1988130803Smarcel      make_cleanup (xfree, oload_champ_bv);
1989130803Smarcel
1990130803Smarcel      oload_champ = find_oload_champ_namespace (arg_types, nargs,
1991130803Smarcel						func_name,
1992130803Smarcel						qualified_name,
1993130803Smarcel						&oload_syms,
1994130803Smarcel						&oload_champ_bv);
199546283Sdfr    }
199698944Sobrien
1997130803Smarcel  /* Check how bad the best match is.  */
199846283Sdfr
1999130803Smarcel  match_quality
2000130803Smarcel    = classify_oload_match (oload_champ_bv, nargs,
2001130803Smarcel			    oload_method_static (method, fns_ptr,
2002130803Smarcel						 oload_champ));
2003130803Smarcel
2004130803Smarcel  if (match_quality == INCOMPATIBLE)
2005130803Smarcel    {
2006130803Smarcel      if (method)
2007130803Smarcel	error ("Cannot resolve method %s%s%s to any overloaded instance",
2008130803Smarcel	       obj_type_name,
2009130803Smarcel	       (obj_type_name && *obj_type_name) ? "::" : "",
2010130803Smarcel	       name);
2011130803Smarcel      else
2012130803Smarcel	error ("Cannot resolve function %s to any overloaded instance",
2013130803Smarcel	       func_name);
2014130803Smarcel    }
2015130803Smarcel  else if (match_quality == NON_STANDARD)
2016130803Smarcel    {
2017130803Smarcel      if (method)
2018130803Smarcel	warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2019130803Smarcel		 obj_type_name,
2020130803Smarcel		 (obj_type_name && *obj_type_name) ? "::" : "",
2021130803Smarcel		 name);
2022130803Smarcel      else
2023130803Smarcel	warning ("Using non-standard conversion to match function %s to supplied arguments",
2024130803Smarcel		 func_name);
2025130803Smarcel    }
2026130803Smarcel
2027130803Smarcel  if (method)
2028130803Smarcel    {
2029130803Smarcel      if (staticp != NULL)
2030130803Smarcel	*staticp = oload_method_static (method, fns_ptr, oload_champ);
2031130803Smarcel      if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
2032130803Smarcel	*valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2033130803Smarcel      else
2034130803Smarcel	*valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2035130803Smarcel    }
2036130803Smarcel  else
2037130803Smarcel    {
2038130803Smarcel      *symp = oload_syms[oload_champ];
2039130803Smarcel    }
2040130803Smarcel
2041130803Smarcel  if (objp)
2042130803Smarcel    {
2043130803Smarcel      if (TYPE_CODE (VALUE_TYPE (temp)) != TYPE_CODE_PTR
2044130803Smarcel	  && TYPE_CODE (VALUE_TYPE (*objp)) == TYPE_CODE_PTR)
2045130803Smarcel	{
2046130803Smarcel	  temp = value_addr (temp);
2047130803Smarcel	}
2048130803Smarcel      *objp = temp;
2049130803Smarcel    }
2050130803Smarcel  if (old_cleanups != NULL)
2051130803Smarcel    do_cleanups (old_cleanups);
2052130803Smarcel
2053130803Smarcel  switch (match_quality)
2054130803Smarcel    {
2055130803Smarcel    case INCOMPATIBLE:
2056130803Smarcel      return 100;
2057130803Smarcel    case NON_STANDARD:
2058130803Smarcel      return 10;
2059130803Smarcel    default:				/* STANDARD */
2060130803Smarcel      return 0;
2061130803Smarcel    }
2062130803Smarcel}
2063130803Smarcel
2064130803Smarcel/* Find the best overload match, searching for FUNC_NAME in namespaces
2065130803Smarcel   contained in QUALIFIED_NAME until it either finds a good match or
2066130803Smarcel   runs out of namespaces.  It stores the overloaded functions in
2067130803Smarcel   *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
2068130803Smarcel   calling function is responsible for freeing *OLOAD_SYMS and
2069130803Smarcel   *OLOAD_CHAMP_BV.  */
2070130803Smarcel
2071130803Smarcelstatic int
2072130803Smarcelfind_oload_champ_namespace (struct type **arg_types, int nargs,
2073130803Smarcel			    const char *func_name,
2074130803Smarcel			    const char *qualified_name,
2075130803Smarcel			    struct symbol ***oload_syms,
2076130803Smarcel			    struct badness_vector **oload_champ_bv)
2077130803Smarcel{
2078130803Smarcel  int oload_champ;
2079130803Smarcel
2080130803Smarcel  find_oload_champ_namespace_loop (arg_types, nargs,
2081130803Smarcel				   func_name,
2082130803Smarcel				   qualified_name, 0,
2083130803Smarcel				   oload_syms, oload_champ_bv,
2084130803Smarcel				   &oload_champ);
2085130803Smarcel
2086130803Smarcel  return oload_champ;
2087130803Smarcel}
2088130803Smarcel
2089130803Smarcel/* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2090130803Smarcel   how deep we've looked for namespaces, and the champ is stored in
2091130803Smarcel   OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
2092130803Smarcel   if it isn't.
2093130803Smarcel
2094130803Smarcel   It is the caller's responsibility to free *OLOAD_SYMS and
2095130803Smarcel   *OLOAD_CHAMP_BV.  */
2096130803Smarcel
2097130803Smarcelstatic int
2098130803Smarcelfind_oload_champ_namespace_loop (struct type **arg_types, int nargs,
2099130803Smarcel				 const char *func_name,
2100130803Smarcel				 const char *qualified_name,
2101130803Smarcel				 int namespace_len,
2102130803Smarcel				 struct symbol ***oload_syms,
2103130803Smarcel				 struct badness_vector **oload_champ_bv,
2104130803Smarcel				 int *oload_champ)
2105130803Smarcel{
2106130803Smarcel  int next_namespace_len = namespace_len;
2107130803Smarcel  int searched_deeper = 0;
2108130803Smarcel  int num_fns = 0;
2109130803Smarcel  struct cleanup *old_cleanups;
2110130803Smarcel  int new_oload_champ;
2111130803Smarcel  struct symbol **new_oload_syms;
2112130803Smarcel  struct badness_vector *new_oload_champ_bv;
2113130803Smarcel  char *new_namespace;
2114130803Smarcel
2115130803Smarcel  if (next_namespace_len != 0)
2116130803Smarcel    {
2117130803Smarcel      gdb_assert (qualified_name[next_namespace_len] == ':');
2118130803Smarcel      next_namespace_len +=  2;
2119130803Smarcel    }
2120130803Smarcel  next_namespace_len
2121130803Smarcel    += cp_find_first_component (qualified_name + next_namespace_len);
2122130803Smarcel
2123130803Smarcel  /* Initialize these to values that can safely be xfree'd.  */
2124130803Smarcel  *oload_syms = NULL;
2125130803Smarcel  *oload_champ_bv = NULL;
2126130803Smarcel
2127130803Smarcel  /* First, see if we have a deeper namespace we can search in.  If we
2128130803Smarcel     get a good match there, use it.  */
2129130803Smarcel
2130130803Smarcel  if (qualified_name[next_namespace_len] == ':')
2131130803Smarcel    {
2132130803Smarcel      searched_deeper = 1;
2133130803Smarcel
2134130803Smarcel      if (find_oload_champ_namespace_loop (arg_types, nargs,
2135130803Smarcel					   func_name, qualified_name,
2136130803Smarcel					   next_namespace_len,
2137130803Smarcel					   oload_syms, oload_champ_bv,
2138130803Smarcel					   oload_champ))
2139130803Smarcel	{
2140130803Smarcel	  return 1;
2141130803Smarcel	}
2142130803Smarcel    };
2143130803Smarcel
2144130803Smarcel  /* If we reach here, either we're in the deepest namespace or we
2145130803Smarcel     didn't find a good match in a deeper namespace.  But, in the
2146130803Smarcel     latter case, we still have a bad match in a deeper namespace;
2147130803Smarcel     note that we might not find any match at all in the current
2148130803Smarcel     namespace.  (There's always a match in the deepest namespace,
2149130803Smarcel     because this overload mechanism only gets called if there's a
2150130803Smarcel     function symbol to start off with.)  */
2151130803Smarcel
2152130803Smarcel  old_cleanups = make_cleanup (xfree, *oload_syms);
2153130803Smarcel  old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2154130803Smarcel  new_namespace = alloca (namespace_len + 1);
2155130803Smarcel  strncpy (new_namespace, qualified_name, namespace_len);
2156130803Smarcel  new_namespace[namespace_len] = '\0';
2157130803Smarcel  new_oload_syms = make_symbol_overload_list (func_name,
2158130803Smarcel					      new_namespace);
2159130803Smarcel  while (new_oload_syms[num_fns])
2160130803Smarcel    ++num_fns;
2161130803Smarcel
2162130803Smarcel  new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2163130803Smarcel				      NULL, new_oload_syms,
2164130803Smarcel				      &new_oload_champ_bv);
2165130803Smarcel
2166130803Smarcel  /* Case 1: We found a good match.  Free earlier matches (if any),
2167130803Smarcel     and return it.  Case 2: We didn't find a good match, but we're
2168130803Smarcel     not the deepest function.  Then go with the bad match that the
2169130803Smarcel     deeper function found.  Case 3: We found a bad match, and we're
2170130803Smarcel     the deepest function.  Then return what we found, even though
2171130803Smarcel     it's a bad match.  */
2172130803Smarcel
2173130803Smarcel  if (new_oload_champ != -1
2174130803Smarcel      && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2175130803Smarcel    {
2176130803Smarcel      *oload_syms = new_oload_syms;
2177130803Smarcel      *oload_champ = new_oload_champ;
2178130803Smarcel      *oload_champ_bv = new_oload_champ_bv;
2179130803Smarcel      do_cleanups (old_cleanups);
2180130803Smarcel      return 1;
2181130803Smarcel    }
2182130803Smarcel  else if (searched_deeper)
2183130803Smarcel    {
2184130803Smarcel      xfree (new_oload_syms);
2185130803Smarcel      xfree (new_oload_champ_bv);
2186130803Smarcel      discard_cleanups (old_cleanups);
2187130803Smarcel      return 0;
2188130803Smarcel    }
2189130803Smarcel  else
2190130803Smarcel    {
2191130803Smarcel      gdb_assert (new_oload_champ != -1);
2192130803Smarcel      *oload_syms = new_oload_syms;
2193130803Smarcel      *oload_champ = new_oload_champ;
2194130803Smarcel      *oload_champ_bv = new_oload_champ_bv;
2195130803Smarcel      discard_cleanups (old_cleanups);
2196130803Smarcel      return 0;
2197130803Smarcel    }
2198130803Smarcel}
2199130803Smarcel
2200130803Smarcel/* Look for a function to take NARGS args of types ARG_TYPES.  Find
2201130803Smarcel   the best match from among the overloaded methods or functions
2202130803Smarcel   (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2203130803Smarcel   The number of methods/functions in the list is given by NUM_FNS.
2204130803Smarcel   Return the index of the best match; store an indication of the
2205130803Smarcel   quality of the match in OLOAD_CHAMP_BV.
2206130803Smarcel
2207130803Smarcel   It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
2208130803Smarcel
2209130803Smarcelstatic int
2210130803Smarcelfind_oload_champ (struct type **arg_types, int nargs, int method,
2211130803Smarcel		  int num_fns, struct fn_field *fns_ptr,
2212130803Smarcel		  struct symbol **oload_syms,
2213130803Smarcel		  struct badness_vector **oload_champ_bv)
2214130803Smarcel{
2215130803Smarcel  int ix;
2216130803Smarcel  struct badness_vector *bv;	/* A measure of how good an overloaded instance is */
2217130803Smarcel  int oload_champ = -1;		/* Index of best overloaded function */
2218130803Smarcel  int oload_ambiguous = 0;	/* Current ambiguity state for overload resolution */
2219130803Smarcel  /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2220130803Smarcel
2221130803Smarcel  *oload_champ_bv = NULL;
2222130803Smarcel
222398944Sobrien  /* Consider each candidate in turn */
222446283Sdfr  for (ix = 0; ix < num_fns; ix++)
222546283Sdfr    {
2226130803Smarcel      int jj;
2227130803Smarcel      int static_offset = oload_method_static (method, fns_ptr, ix);
2228130803Smarcel      int nparms;
2229130803Smarcel      struct type **parm_types;
2230130803Smarcel
223198944Sobrien      if (method)
223298944Sobrien	{
2233130803Smarcel	  nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
223498944Sobrien	}
223598944Sobrien      else
223698944Sobrien	{
223798944Sobrien	  /* If it's not a method, this is the proper place */
223898944Sobrien	  nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
223998944Sobrien	}
224046283Sdfr
224198944Sobrien      /* Prepare array of parameter types */
224246283Sdfr      parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
224346283Sdfr      for (jj = 0; jj < nparms; jj++)
224498944Sobrien	parm_types[jj] = (method
2245130803Smarcel			  ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
224698944Sobrien			  : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
224746283Sdfr
2248130803Smarcel      /* Compare parameter types to supplied argument types.  Skip THIS for
2249130803Smarcel         static methods.  */
2250130803Smarcel      bv = rank_function (parm_types, nparms, arg_types + static_offset,
2251130803Smarcel			  nargs - static_offset);
225298944Sobrien
2253130803Smarcel      if (!*oload_champ_bv)
225498944Sobrien	{
2255130803Smarcel	  *oload_champ_bv = bv;
225698944Sobrien	  oload_champ = 0;
225798944Sobrien	}
225846283Sdfr      else
225998944Sobrien	/* See whether current candidate is better or worse than previous best */
2260130803Smarcel	switch (compare_badness (bv, *oload_champ_bv))
226198944Sobrien	  {
226298944Sobrien	  case 0:
226398944Sobrien	    oload_ambiguous = 1;	/* top two contenders are equally good */
226498944Sobrien	    break;
226598944Sobrien	  case 1:
226698944Sobrien	    oload_ambiguous = 2;	/* incomparable top contenders */
226798944Sobrien	    break;
226898944Sobrien	  case 2:
2269130803Smarcel	    *oload_champ_bv = bv;	/* new champion, record details */
227098944Sobrien	    oload_ambiguous = 0;
227198944Sobrien	    oload_champ = ix;
227298944Sobrien	    break;
227398944Sobrien	  case 3:
227498944Sobrien	  default:
227598944Sobrien	    break;
227698944Sobrien	  }
227798944Sobrien      xfree (parm_types);
227898944Sobrien      if (overload_debug)
227998944Sobrien	{
228098944Sobrien	  if (method)
228198944Sobrien	    fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
228298944Sobrien	  else
228398944Sobrien	    fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2284130803Smarcel	  for (jj = 0; jj < nargs - static_offset; jj++)
228598944Sobrien	    fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
228698944Sobrien	  fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
228798944Sobrien	}
228846283Sdfr    }
228946283Sdfr
2290130803Smarcel  return oload_champ;
2291130803Smarcel}
229298944Sobrien
2293130803Smarcel/* Return 1 if we're looking at a static method, 0 if we're looking at
2294130803Smarcel   a non-static method or a function that isn't a method.  */
229546283Sdfr
2296130803Smarcelstatic int
2297130803Smarceloload_method_static (int method, struct fn_field *fns_ptr, int index)
2298130803Smarcel{
2299130803Smarcel  if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2300130803Smarcel    return 1;
230146283Sdfr  else
2302130803Smarcel    return 0;
2303130803Smarcel}
2304130803Smarcel
2305130803Smarcel/* Check how good an overload match OLOAD_CHAMP_BV represents.  */
2306130803Smarcel
2307130803Smarcelstatic enum oload_classification
2308130803Smarcelclassify_oload_match (struct badness_vector *oload_champ_bv,
2309130803Smarcel		      int nargs,
2310130803Smarcel		      int static_offset)
2311130803Smarcel{
2312130803Smarcel  int ix;
2313130803Smarcel
2314130803Smarcel  for (ix = 1; ix <= nargs - static_offset; ix++)
231546283Sdfr    {
2316130803Smarcel      if (oload_champ_bv->rank[ix] >= 100)
2317130803Smarcel	return INCOMPATIBLE;	/* truly mismatched types */
2318130803Smarcel      else if (oload_champ_bv->rank[ix] >= 10)
2319130803Smarcel	return NON_STANDARD;	/* non-standard type conversions needed */
232046283Sdfr    }
232146283Sdfr
2322130803Smarcel  return STANDARD;		/* Only standard conversions needed.  */
232346283Sdfr}
232446283Sdfr
232519370Spst/* C++: return 1 is NAME is a legitimate name for the destructor
232619370Spst   of type TYPE.  If TYPE does not have a destructor, or
232719370Spst   if NAME is inappropriate for TYPE, an error is signaled.  */
232819370Spstint
232998944Sobriendestructor_name_p (const char *name, const struct type *type)
233019370Spst{
233119370Spst  /* destructors are a special case.  */
233219370Spst
233319370Spst  if (name[0] == '~')
233419370Spst    {
233519370Spst      char *dname = type_name_no_tag (type);
233619370Spst      char *cp = strchr (dname, '<');
233719370Spst      unsigned int len;
233819370Spst
233919370Spst      /* Do not compare the template part for template classes.  */
234019370Spst      if (cp == NULL)
234119370Spst	len = strlen (dname);
234219370Spst      else
234319370Spst	len = cp - dname;
2344130803Smarcel      if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
234519370Spst	error ("name of destructor must equal name of class");
234619370Spst      else
234719370Spst	return 1;
234819370Spst    }
234919370Spst  return 0;
235019370Spst}
235119370Spst
235219370Spst/* Helper function for check_field: Given TYPE, a structure/union,
235319370Spst   return 1 if the component named NAME from the ultimate
235419370Spst   target structure/union is defined, otherwise, return 0. */
235519370Spst
235619370Spststatic int
2357130803Smarcelcheck_field_in (struct type *type, const char *name)
235819370Spst{
2359130803Smarcel  int i;
236019370Spst
236119370Spst  for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
236219370Spst    {
236319370Spst      char *t_field_name = TYPE_FIELD_NAME (type, i);
236498944Sobrien      if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
236519370Spst	return 1;
236619370Spst    }
236719370Spst
236819370Spst  /* C++: If it was not found as a data field, then try to
236919370Spst     return it as a pointer to a method.  */
237019370Spst
237119370Spst  /* Destructors are a special case.  */
237219370Spst  if (destructor_name_p (name, type))
237346283Sdfr    {
237446283Sdfr      int m_index, f_index;
237519370Spst
237646283Sdfr      return get_destructor_fn_field (type, &m_index, &f_index);
237746283Sdfr    }
237846283Sdfr
237919370Spst  for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
238019370Spst    {
238198944Sobrien      if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
238219370Spst	return 1;
238319370Spst    }
238419370Spst
238519370Spst  for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
238619370Spst    if (check_field_in (TYPE_BASECLASS (type, i), name))
238719370Spst      return 1;
238898944Sobrien
238919370Spst  return 0;
239019370Spst}
239119370Spst
239219370Spst
239319370Spst/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
239419370Spst   return 1 if the component named NAME from the ultimate
239519370Spst   target structure/union is defined, otherwise, return 0.  */
239619370Spst
239719370Spstint
239898944Sobriencheck_field (struct value *arg1, const char *name)
239919370Spst{
2400130803Smarcel  struct type *t;
240119370Spst
240219370Spst  COERCE_ARRAY (arg1);
240319370Spst
240419370Spst  t = VALUE_TYPE (arg1);
240519370Spst
240619370Spst  /* Follow pointers until we get to a non-pointer.  */
240719370Spst
240819370Spst  for (;;)
240919370Spst    {
241019370Spst      CHECK_TYPEDEF (t);
241119370Spst      if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
241219370Spst	break;
241319370Spst      t = TYPE_TARGET_TYPE (t);
241419370Spst    }
241519370Spst
241619370Spst  if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
241719370Spst    error ("not implemented: member type in check_field");
241819370Spst
241998944Sobrien  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
242019370Spst      && TYPE_CODE (t) != TYPE_CODE_UNION)
242119370Spst    error ("Internal error: `this' is not an aggregate");
242219370Spst
242319370Spst  return check_field_in (t, name);
242419370Spst}
242519370Spst
242619370Spst/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2427130803Smarcel   return the appropriate member.  This function is used to resolve
2428130803Smarcel   user expressions of the form "DOMAIN::NAME".  For more details on
2429130803Smarcel   what happens, see the comment before
2430130803Smarcel   value_struct_elt_for_reference.  */
2431130803Smarcel
2432130803Smarcelstruct value *
2433130803Smarcelvalue_aggregate_elt (struct type *curtype,
2434130803Smarcel		     char *name,
2435130803Smarcel		     enum noside noside)
2436130803Smarcel{
2437130803Smarcel  switch (TYPE_CODE (curtype))
2438130803Smarcel    {
2439130803Smarcel    case TYPE_CODE_STRUCT:
2440130803Smarcel    case TYPE_CODE_UNION:
2441130803Smarcel      return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2442130803Smarcel					     noside);
2443130803Smarcel    case TYPE_CODE_NAMESPACE:
2444130803Smarcel      return value_namespace_elt (curtype, name, noside);
2445130803Smarcel    default:
2446130803Smarcel      internal_error (__FILE__, __LINE__,
2447130803Smarcel		      "non-aggregate type in value_aggregate_elt");
2448130803Smarcel    }
2449130803Smarcel}
2450130803Smarcel
2451130803Smarcel/* C++: Given an aggregate type CURTYPE, and a member name NAME,
245219370Spst   return the address of this member as a "pointer to member"
245319370Spst   type.  If INTYPE is non-null, then it will be the type
245419370Spst   of the member we are looking for.  This will help us resolve
245519370Spst   "pointers to member functions".  This function is used
245619370Spst   to resolve user expressions of the form "DOMAIN::NAME".  */
245719370Spst
2458130803Smarcelstatic struct value *
245998944Sobrienvalue_struct_elt_for_reference (struct type *domain, int offset,
246098944Sobrien				struct type *curtype, char *name,
2461130803Smarcel				struct type *intype,
2462130803Smarcel				enum noside noside)
246319370Spst{
2464130803Smarcel  struct type *t = curtype;
2465130803Smarcel  int i;
246698944Sobrien  struct value *v;
246719370Spst
246898944Sobrien  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
246919370Spst      && TYPE_CODE (t) != TYPE_CODE_UNION)
247019370Spst    error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
247119370Spst
247219370Spst  for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
247319370Spst    {
247419370Spst      char *t_field_name = TYPE_FIELD_NAME (t, i);
247598944Sobrien
2476130803Smarcel      if (t_field_name && strcmp (t_field_name, name) == 0)
247719370Spst	{
247819370Spst	  if (TYPE_FIELD_STATIC (t, i))
247919370Spst	    {
248046283Sdfr	      v = value_static_field (t, i);
248146283Sdfr	      if (v == NULL)
2482130803Smarcel		error ("static field %s has been optimized out",
248346283Sdfr		       name);
248446283Sdfr	      return v;
248519370Spst	    }
248619370Spst	  if (TYPE_FIELD_PACKED (t, i))
248719370Spst	    error ("pointers to bitfield members not allowed");
248898944Sobrien
248919370Spst	  return value_from_longest
249019370Spst	    (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
249119370Spst							domain)),
249219370Spst	     offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
249319370Spst	}
249419370Spst    }
249519370Spst
249619370Spst  /* C++: If it was not found as a data field, then try to
249719370Spst     return it as a pointer to a method.  */
249819370Spst
249919370Spst  /* Destructors are a special case.  */
250019370Spst  if (destructor_name_p (name, t))
250119370Spst    {
250219370Spst      error ("member pointers to destructors not implemented yet");
250319370Spst    }
250419370Spst
250519370Spst  /* Perform all necessary dereferencing.  */
250619370Spst  while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
250719370Spst    intype = TYPE_TARGET_TYPE (intype);
250819370Spst
250919370Spst  for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
251019370Spst    {
251119370Spst      char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
251219370Spst      char dem_opname[64];
251319370Spst
251498944Sobrien      if (strncmp (t_field_name, "__", 2) == 0 ||
251598944Sobrien	  strncmp (t_field_name, "op", 2) == 0 ||
251698944Sobrien	  strncmp (t_field_name, "type", 4) == 0)
251719370Spst	{
251898944Sobrien	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
251919370Spst	    t_field_name = dem_opname;
252098944Sobrien	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
252198944Sobrien	    t_field_name = dem_opname;
252219370Spst	}
2523130803Smarcel      if (t_field_name && strcmp (t_field_name, name) == 0)
252419370Spst	{
252519370Spst	  int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
252619370Spst	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
252798944Sobrien
2528130803Smarcel	  check_stub_method_group (t, i);
2529130803Smarcel
253019370Spst	  if (intype == 0 && j > 1)
253119370Spst	    error ("non-unique member `%s' requires type instantiation", name);
253219370Spst	  if (intype)
253319370Spst	    {
253419370Spst	      while (j--)
253519370Spst		if (TYPE_FN_FIELD_TYPE (f, j) == intype)
253619370Spst		  break;
253719370Spst	      if (j < 0)
253819370Spst		error ("no member function matches that type instantiation");
253919370Spst	    }
254019370Spst	  else
254119370Spst	    j = 0;
254298944Sobrien
254319370Spst	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
254419370Spst	    {
254519370Spst	      return value_from_longest
254619370Spst		(lookup_reference_type
254719370Spst		 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
254819370Spst				      domain)),
254919370Spst		 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
255019370Spst	    }
255119370Spst	  else
255219370Spst	    {
255319370Spst	      struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2554130803Smarcel						0, VAR_DOMAIN, 0, NULL);
255519370Spst	      if (s == NULL)
255619370Spst		{
255719370Spst		  v = 0;
255819370Spst		}
255919370Spst	      else
256019370Spst		{
256119370Spst		  v = read_var_value (s, 0);
256219370Spst#if 0
256319370Spst		  VALUE_TYPE (v) = lookup_reference_type
256419370Spst		    (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
256519370Spst					 domain));
256619370Spst#endif
256719370Spst		}
256819370Spst	      return v;
256919370Spst	    }
257019370Spst	}
257119370Spst    }
257219370Spst  for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
257319370Spst    {
257498944Sobrien      struct value *v;
257519370Spst      int base_offset;
257619370Spst
257719370Spst      if (BASETYPE_VIA_VIRTUAL (t, i))
257819370Spst	base_offset = 0;
257919370Spst      else
258019370Spst	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
258119370Spst      v = value_struct_elt_for_reference (domain,
258219370Spst					  offset + base_offset,
258319370Spst					  TYPE_BASECLASS (t, i),
258419370Spst					  name,
2585130803Smarcel					  intype,
2586130803Smarcel					  noside);
258719370Spst      if (v)
258819370Spst	return v;
258919370Spst    }
2590130803Smarcel
2591130803Smarcel  /* As a last chance, pretend that CURTYPE is a namespace, and look
2592130803Smarcel     it up that way; this (frequently) works for types nested inside
2593130803Smarcel     classes.  */
2594130803Smarcel
2595130803Smarcel  return value_maybe_namespace_elt (curtype, name, noside);
259619370Spst}
259719370Spst
2598130803Smarcel/* C++: Return the member NAME of the namespace given by the type
2599130803Smarcel   CURTYPE.  */
260046283Sdfr
2601130803Smarcelstatic struct value *
2602130803Smarcelvalue_namespace_elt (const struct type *curtype,
2603130803Smarcel		     char *name,
2604130803Smarcel		     enum noside noside)
2605130803Smarcel{
2606130803Smarcel  struct value *retval = value_maybe_namespace_elt (curtype, name,
2607130803Smarcel						    noside);
2608130803Smarcel
2609130803Smarcel  if (retval == NULL)
2610130803Smarcel    error ("No symbol \"%s\" in namespace \"%s\".", name,
2611130803Smarcel	   TYPE_TAG_NAME (curtype));
2612130803Smarcel
2613130803Smarcel  return retval;
2614130803Smarcel}
2615130803Smarcel
2616130803Smarcel/* A helper function used by value_namespace_elt and
2617130803Smarcel   value_struct_elt_for_reference.  It looks up NAME inside the
2618130803Smarcel   context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2619130803Smarcel   is a class and NAME refers to a type in CURTYPE itself (as opposed
2620130803Smarcel   to, say, some base class of CURTYPE).  */
2621130803Smarcel
2622130803Smarcelstatic struct value *
2623130803Smarcelvalue_maybe_namespace_elt (const struct type *curtype,
2624130803Smarcel			   char *name,
2625130803Smarcel			   enum noside noside)
2626130803Smarcel{
2627130803Smarcel  const char *namespace_name = TYPE_TAG_NAME (curtype);
2628130803Smarcel  struct symbol *sym;
2629130803Smarcel
2630130803Smarcel  sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2631130803Smarcel				    get_selected_block (0), VAR_DOMAIN,
2632130803Smarcel				    NULL);
2633130803Smarcel
2634130803Smarcel  if (sym == NULL)
2635130803Smarcel    return NULL;
2636130803Smarcel  else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2637130803Smarcel	   && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2638130803Smarcel    return allocate_value (SYMBOL_TYPE (sym));
2639130803Smarcel  else
2640130803Smarcel    return value_of_variable (sym, get_selected_block (0));
2641130803Smarcel}
2642130803Smarcel
264346283Sdfr/* Given a pointer value V, find the real (RTTI) type
264446283Sdfr   of the object it points to.
264546283Sdfr   Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
264646283Sdfr   and refer to the values computed for the object pointed to. */
264746283Sdfr
264846283Sdfrstruct type *
264998944Sobrienvalue_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
265046283Sdfr{
265198944Sobrien  struct value *target;
265246283Sdfr
265346283Sdfr  target = value_ind (v);
265446283Sdfr
265546283Sdfr  return value_rtti_type (target, full, top, using_enc);
265646283Sdfr}
265746283Sdfr
265846283Sdfr/* Given a value pointed to by ARGP, check its real run-time type, and
265946283Sdfr   if that is different from the enclosing type, create a new value
266046283Sdfr   using the real run-time type as the enclosing type (and of the same
266146283Sdfr   type as ARGP) and return it, with the embedded offset adjusted to
266246283Sdfr   be the correct offset to the enclosed object
266346283Sdfr   RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
266446283Sdfr   parameters, computed by value_rtti_type(). If these are available,
266546283Sdfr   they can be supplied and a second call to value_rtti_type() is avoided.
266646283Sdfr   (Pass RTYPE == NULL if they're not available */
266746283Sdfr
266898944Sobrienstruct value *
266998944Sobrienvalue_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
267098944Sobrien		   int xusing_enc)
267146283Sdfr{
267298944Sobrien  struct type *real_type;
267346283Sdfr  int full = 0;
267446283Sdfr  int top = -1;
267546283Sdfr  int using_enc = 0;
267698944Sobrien  struct value *new_val;
267746283Sdfr
267846283Sdfr  if (rtype)
267946283Sdfr    {
268046283Sdfr      real_type = rtype;
268146283Sdfr      full = xfull;
268246283Sdfr      top = xtop;
268346283Sdfr      using_enc = xusing_enc;
268446283Sdfr    }
268546283Sdfr  else
268646283Sdfr    real_type = value_rtti_type (argp, &full, &top, &using_enc);
268746283Sdfr
268846283Sdfr  /* If no RTTI data, or if object is already complete, do nothing */
268946283Sdfr  if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
269046283Sdfr    return argp;
269146283Sdfr
269246283Sdfr  /* If we have the full object, but for some reason the enclosing
269398944Sobrien     type is wrong, set it *//* pai: FIXME -- sounds iffy */
269446283Sdfr  if (full)
269546283Sdfr    {
269698944Sobrien      argp = value_change_enclosing_type (argp, real_type);
269746283Sdfr      return argp;
269846283Sdfr    }
269946283Sdfr
270046283Sdfr  /* Check if object is in memory */
270146283Sdfr  if (VALUE_LVAL (argp) != lval_memory)
270246283Sdfr    {
270346283Sdfr      warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
270498944Sobrien
270546283Sdfr      return argp;
270646283Sdfr    }
270798944Sobrien
270846283Sdfr  /* All other cases -- retrieve the complete object */
270946283Sdfr  /* Go back by the computed top_offset from the beginning of the object,
271046283Sdfr     adjusting for the embedded offset of argp if that's what value_rtti_type
271146283Sdfr     used for its computation. */
271246283Sdfr  new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
271398944Sobrien			   (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
271498944Sobrien			   VALUE_BFD_SECTION (argp));
271546283Sdfr  VALUE_TYPE (new_val) = VALUE_TYPE (argp);
271646283Sdfr  VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
271746283Sdfr  return new_val;
271846283Sdfr}
271946283Sdfr
272046283Sdfr
272146283Sdfr
272246283Sdfr
2723130803Smarcel/* Return the value of the local variable, if one exists.
272419370Spst   Flag COMPLAIN signals an error if the request is made in an
272519370Spst   inappropriate context.  */
272619370Spst
272798944Sobrienstruct value *
2728130803Smarcelvalue_of_local (const char *name, int complain)
272919370Spst{
273019370Spst  struct symbol *func, *sym;
273119370Spst  struct block *b;
2732130803Smarcel  struct value * ret;
273319370Spst
2734130803Smarcel  if (deprecated_selected_frame == 0)
273546283Sdfr    {
273646283Sdfr      if (complain)
273798944Sobrien	error ("no frame selected");
273898944Sobrien      else
273998944Sobrien	return 0;
274046283Sdfr    }
274119370Spst
2742130803Smarcel  func = get_frame_function (deprecated_selected_frame);
274319370Spst  if (!func)
274419370Spst    {
274519370Spst      if (complain)
2746130803Smarcel	error ("no `%s' in nameless context", name);
274798944Sobrien      else
274898944Sobrien	return 0;
274919370Spst    }
275019370Spst
275119370Spst  b = SYMBOL_BLOCK_VALUE (func);
2752130803Smarcel  if (dict_empty (BLOCK_DICT (b)))
275346283Sdfr    {
275446283Sdfr      if (complain)
2755130803Smarcel	error ("no args, no `%s'", name);
275698944Sobrien      else
275798944Sobrien	return 0;
275846283Sdfr    }
275919370Spst
276019370Spst  /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
276119370Spst     symbol instead of the LOC_ARG one (if both exist).  */
2762130803Smarcel  sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
276319370Spst  if (sym == NULL)
276419370Spst    {
276519370Spst      if (complain)
2766130803Smarcel	error ("current stack frame does not contain a variable named `%s'", name);
276719370Spst      else
276819370Spst	return NULL;
276919370Spst    }
277019370Spst
2771130803Smarcel  ret = read_var_value (sym, deprecated_selected_frame);
2772130803Smarcel  if (ret == 0 && complain)
2773130803Smarcel    error ("`%s' argument unreadable", name);
2774130803Smarcel  return ret;
277519370Spst}
277619370Spst
2777130803Smarcel/* C++/Objective-C: return the value of the class instance variable,
2778130803Smarcel   if one exists.  Flag COMPLAIN signals an error if the request is
2779130803Smarcel   made in an inappropriate context.  */
2780130803Smarcel
2781130803Smarcelstruct value *
2782130803Smarcelvalue_of_this (int complain)
2783130803Smarcel{
2784130803Smarcel  if (current_language->la_language == language_objc)
2785130803Smarcel    return value_of_local ("self", complain);
2786130803Smarcel  else
2787130803Smarcel    return value_of_local ("this", complain);
2788130803Smarcel}
2789130803Smarcel
279019370Spst/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
279119370Spst   long, starting at LOWBOUND.  The result has the same lower bound as
279219370Spst   the original ARRAY.  */
279319370Spst
279498944Sobrienstruct value *
279598944Sobrienvalue_slice (struct value *array, int lowbound, int length)
279619370Spst{
279719370Spst  struct type *slice_range_type, *slice_type, *range_type;
2798130803Smarcel  LONGEST lowerbound, upperbound;
279998944Sobrien  struct value *slice;
280019370Spst  struct type *array_type;
280119370Spst  array_type = check_typedef (VALUE_TYPE (array));
280219370Spst  COERCE_VARYING_ARRAY (array, array_type);
280319370Spst  if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
280419370Spst      && TYPE_CODE (array_type) != TYPE_CODE_STRING
280519370Spst      && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
280619370Spst    error ("cannot take slice of non-array");
280719370Spst  range_type = TYPE_INDEX_TYPE (array_type);
280819370Spst  if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
280919370Spst    error ("slice from bad array or bitstring");
281019370Spst  if (lowbound < lowerbound || length < 0
2811130803Smarcel      || lowbound + length - 1 > upperbound)
281219370Spst    error ("slice out of range");
281319370Spst  /* FIXME-type-allocation: need a way to free this type when we are
281419370Spst     done with it.  */
281598944Sobrien  slice_range_type = create_range_type ((struct type *) NULL,
281619370Spst					TYPE_TARGET_TYPE (range_type),
281746283Sdfr					lowbound, lowbound + length - 1);
281819370Spst  if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
281919370Spst    {
282019370Spst      int i;
282198944Sobrien      slice_type = create_set_type ((struct type *) NULL, slice_range_type);
282219370Spst      TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
282319370Spst      slice = value_zero (slice_type, not_lval);
282419370Spst      for (i = 0; i < length; i++)
282519370Spst	{
282619370Spst	  int element = value_bit_index (array_type,
282719370Spst					 VALUE_CONTENTS (array),
282819370Spst					 lowbound + i);
282919370Spst	  if (element < 0)
283019370Spst	    error ("internal error accessing bitstring");
283119370Spst	  else if (element > 0)
283219370Spst	    {
283319370Spst	      int j = i % TARGET_CHAR_BIT;
283419370Spst	      if (BITS_BIG_ENDIAN)
283519370Spst		j = TARGET_CHAR_BIT - 1 - j;
283619370Spst	      VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
283719370Spst	    }
283819370Spst	}
283919370Spst      /* We should set the address, bitssize, and bitspos, so the clice
284098944Sobrien         can be used on the LHS, but that may require extensions to
284198944Sobrien         value_assign.  For now, just leave as a non_lval.  FIXME.  */
284219370Spst    }
284319370Spst  else
284419370Spst    {
284519370Spst      struct type *element_type = TYPE_TARGET_TYPE (array_type);
2846130803Smarcel      LONGEST offset
284719370Spst	= (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
284898944Sobrien      slice_type = create_array_type ((struct type *) NULL, element_type,
284919370Spst				      slice_range_type);
285019370Spst      TYPE_CODE (slice_type) = TYPE_CODE (array_type);
285119370Spst      slice = allocate_value (slice_type);
285219370Spst      if (VALUE_LAZY (array))
285319370Spst	VALUE_LAZY (slice) = 1;
285419370Spst      else
285519370Spst	memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
285619370Spst		TYPE_LENGTH (slice_type));
285719370Spst      if (VALUE_LVAL (array) == lval_internalvar)
285819370Spst	VALUE_LVAL (slice) = lval_internalvar_component;
285919370Spst      else
286019370Spst	VALUE_LVAL (slice) = VALUE_LVAL (array);
286119370Spst      VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
286219370Spst      VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
286319370Spst    }
286419370Spst  return slice;
286519370Spst}
286619370Spst
286798944Sobrien/* Create a value for a FORTRAN complex number.  Currently most of
286898944Sobrien   the time values are coerced to COMPLEX*16 (i.e. a complex number
286998944Sobrien   composed of 2 doubles.  This really should be a smarter routine
287098944Sobrien   that figures out precision inteligently as opposed to assuming
287198944Sobrien   doubles. FIXME: fmb */
287219370Spst
287398944Sobrienstruct value *
287498944Sobrienvalue_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
287519370Spst{
287698944Sobrien  struct value *val;
287719370Spst  struct type *real_type = TYPE_TARGET_TYPE (type);
287819370Spst
287919370Spst  val = allocate_value (type);
288019370Spst  arg1 = value_cast (real_type, arg1);
288119370Spst  arg2 = value_cast (real_type, arg2);
288219370Spst
288319370Spst  memcpy (VALUE_CONTENTS_RAW (val),
288419370Spst	  VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
288519370Spst  memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
288619370Spst	  VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
288719370Spst  return val;
288819370Spst}
288919370Spst
289019370Spst/* Cast a value into the appropriate complex data type. */
289119370Spst
289298944Sobrienstatic struct value *
289398944Sobriencast_into_complex (struct type *type, struct value *val)
289419370Spst{
289519370Spst  struct type *real_type = TYPE_TARGET_TYPE (type);
289619370Spst  if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
289719370Spst    {
289819370Spst      struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
289998944Sobrien      struct value *re_val = allocate_value (val_real_type);
290098944Sobrien      struct value *im_val = allocate_value (val_real_type);
290119370Spst
290219370Spst      memcpy (VALUE_CONTENTS_RAW (re_val),
290319370Spst	      VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
290419370Spst      memcpy (VALUE_CONTENTS_RAW (im_val),
290519370Spst	      VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
290698944Sobrien	      TYPE_LENGTH (val_real_type));
290719370Spst
290819370Spst      return value_literal_complex (re_val, im_val, type);
290919370Spst    }
291019370Spst  else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
291119370Spst	   || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
291219370Spst    return value_literal_complex (val, value_zero (real_type, not_lval), type);
291319370Spst  else
291419370Spst    error ("cannot cast non-number to complex");
291519370Spst}
291619370Spst
291719370Spstvoid
291898944Sobrien_initialize_valops (void)
291919370Spst{
292019370Spst#if 0
292119370Spst  add_show_from_set
292298944Sobrien    (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
292319370Spst		  "Set automatic abandonment of expressions upon failure.",
292419370Spst		  &setlist),
292519370Spst     &showlist);
292619370Spst#endif
292746283Sdfr
292846283Sdfr  add_show_from_set
292998944Sobrien    (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
293046283Sdfr		  "Set overload resolution in evaluating C++ functions.",
293146283Sdfr		  &setlist),
293246283Sdfr     &showlist);
293346283Sdfr  overload_resolution = 1;
293419370Spst}
2935