OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [valops.c] - Blame information for rev 272

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.