* printcmd.c (print_scalar_formatted): If we are printing an
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
c906108c 1/* Perform non-arithmetic operations on values, for GDB.
b6ba6518
KB
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
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"
4e052eda 33#include "regcache.h"
015a42b4 34#include "cp-abi.h"
c906108c
SS
35
36#include <errno.h>
37#include "gdb_string.h"
38
c906108c
SS
39/* Flag indicating HP compilers were used; needed to correctly handle some
40 value operations with HP aCC code/runtime. */
41extern int hp_som_som_object_present;
42
070ad9f0 43extern int overload_debug;
c906108c
SS
44/* Local functions. */
45
a14ed312 46static int typecmp (int staticp, struct type *t1[], value_ptr t2[]);
c906108c 47
a14ed312
KB
48static CORE_ADDR find_function_addr (value_ptr, struct type **);
49static value_ptr value_arg_coerce (value_ptr, struct type *, int);
c906108c
SS
50
51
a14ed312 52static CORE_ADDR value_push (CORE_ADDR, value_ptr);
c906108c 53
a14ed312
KB
54static value_ptr search_struct_field (char *, value_ptr, int,
55 struct type *, int);
c906108c 56
a14ed312
KB
57static value_ptr search_struct_method (char *, value_ptr *,
58 value_ptr *,
59 int, int *, struct type *);
c906108c 60
a14ed312 61static int check_field_in (struct type *, const char *);
c906108c 62
a14ed312 63static CORE_ADDR allocate_space_in_inferior (int);
c906108c 64
a14ed312 65static value_ptr cast_into_complex (struct type *, value_ptr);
c906108c 66
a14ed312
KB
67static struct fn_field *find_method_list (value_ptr * argp, char *method,
68 int offset, int *static_memfuncp,
69 struct type *type, int *num_fns,
70 struct type **basetype,
71 int *boffset);
7a292a7a 72
a14ed312 73void _initialize_valops (void);
c906108c 74
c906108c
SS
75/* Flag for whether we want to abandon failed expression evals by default. */
76
77#if 0
78static int auto_abandon = 0;
79#endif
80
81int overload_resolution = 0;
242bfc55
FN
82
83/* This boolean tells what gdb should do if a signal is received while in
84 a function called from gdb (call dummy). If set, gdb unwinds the stack
85 and restore the context to what as it was before the call.
86 The default is to stop in the frame where the signal was received. */
87
88int unwind_on_signal_p = 0;
c5aa993b 89\f
c906108c
SS
90
91
c906108c
SS
92/* Find the address of function name NAME in the inferior. */
93
94value_ptr
fba45db2 95find_function_in_inferior (char *name)
c906108c
SS
96{
97 register struct symbol *sym;
98 sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
99 if (sym != NULL)
100 {
101 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
102 {
103 error ("\"%s\" exists in this program but is not a function.",
104 name);
105 }
106 return value_of_variable (sym, NULL);
107 }
108 else
109 {
c5aa993b 110 struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
c906108c
SS
111 if (msymbol != NULL)
112 {
113 struct type *type;
4478b372 114 CORE_ADDR maddr;
c906108c
SS
115 type = lookup_pointer_type (builtin_type_char);
116 type = lookup_function_type (type);
117 type = lookup_pointer_type (type);
4478b372
JB
118 maddr = SYMBOL_VALUE_ADDRESS (msymbol);
119 return value_from_pointer (type, maddr);
c906108c
SS
120 }
121 else
122 {
c5aa993b 123 if (!target_has_execution)
c906108c 124 error ("evaluation of this expression requires the target program to be active");
c5aa993b 125 else
c906108c
SS
126 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
127 }
128 }
129}
130
131/* Allocate NBYTES of space in the inferior using the inferior's malloc
132 and return a value that is a pointer to the allocated space. */
133
134value_ptr
fba45db2 135value_allocate_space_in_inferior (int len)
c906108c
SS
136{
137 value_ptr blocklen;
138 register value_ptr val = find_function_in_inferior ("malloc");
139
140 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
141 val = call_function_by_hand (val, 1, &blocklen);
142 if (value_logical_not (val))
143 {
144 if (!target_has_execution)
c5aa993b
JM
145 error ("No memory available to program now: you need to start the target first");
146 else
147 error ("No memory available to program: call to malloc failed");
c906108c
SS
148 }
149 return val;
150}
151
152static CORE_ADDR
fba45db2 153allocate_space_in_inferior (int len)
c906108c
SS
154{
155 return value_as_long (value_allocate_space_in_inferior (len));
156}
157
158/* Cast value ARG2 to type TYPE and return as a value.
159 More general than a C cast: accepts any two types of the same length,
160 and if ARG2 is an lvalue it can be cast into anything at all. */
161/* In C++, casts may change pointer or object representations. */
162
163value_ptr
fba45db2 164value_cast (struct type *type, register value_ptr arg2)
c906108c
SS
165{
166 register enum type_code code1;
167 register enum type_code code2;
168 register int scalar;
169 struct type *type2;
170
171 int convert_to_boolean = 0;
c5aa993b 172
c906108c
SS
173 if (VALUE_TYPE (arg2) == type)
174 return arg2;
175
176 CHECK_TYPEDEF (type);
177 code1 = TYPE_CODE (type);
c5aa993b 178 COERCE_REF (arg2);
c906108c
SS
179 type2 = check_typedef (VALUE_TYPE (arg2));
180
181 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
182 is treated like a cast to (TYPE [N])OBJECT,
183 where N is sizeof(OBJECT)/sizeof(TYPE). */
184 if (code1 == TYPE_CODE_ARRAY)
185 {
186 struct type *element_type = TYPE_TARGET_TYPE (type);
187 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
188 if (element_length > 0
c5aa993b 189 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
c906108c
SS
190 {
191 struct type *range_type = TYPE_INDEX_TYPE (type);
192 int val_length = TYPE_LENGTH (type2);
193 LONGEST low_bound, high_bound, new_length;
194 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
195 low_bound = 0, high_bound = 0;
196 new_length = val_length / element_length;
197 if (val_length % element_length != 0)
c5aa993b 198 warning ("array element type size does not divide object size in cast");
c906108c
SS
199 /* FIXME-type-allocation: need a way to free this type when we are
200 done with it. */
201 range_type = create_range_type ((struct type *) NULL,
202 TYPE_TARGET_TYPE (range_type),
203 low_bound,
204 new_length + low_bound - 1);
205 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
206 element_type, range_type);
207 return arg2;
208 }
209 }
210
211 if (current_language->c_style_arrays
212 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
213 arg2 = value_coerce_array (arg2);
214
215 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
216 arg2 = value_coerce_function (arg2);
217
218 type2 = check_typedef (VALUE_TYPE (arg2));
219 COERCE_VARYING_ARRAY (arg2, type2);
220 code2 = TYPE_CODE (type2);
221
222 if (code1 == TYPE_CODE_COMPLEX)
223 return cast_into_complex (type, arg2);
224 if (code1 == TYPE_CODE_BOOL)
225 {
226 code1 = TYPE_CODE_INT;
227 convert_to_boolean = 1;
228 }
229 if (code1 == TYPE_CODE_CHAR)
230 code1 = TYPE_CODE_INT;
231 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
232 code2 = TYPE_CODE_INT;
233
234 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
235 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
236
c5aa993b 237 if (code1 == TYPE_CODE_STRUCT
c906108c
SS
238 && code2 == TYPE_CODE_STRUCT
239 && TYPE_NAME (type) != 0)
240 {
241 /* Look in the type of the source to see if it contains the
7b83ea04
AC
242 type of the target as a superclass. If so, we'll need to
243 offset the object in addition to changing its type. */
c906108c
SS
244 value_ptr v = search_struct_field (type_name_no_tag (type),
245 arg2, 0, type2, 1);
246 if (v)
247 {
248 VALUE_TYPE (v) = type;
249 return v;
250 }
251 }
252 if (code1 == TYPE_CODE_FLT && scalar)
253 return value_from_double (type, value_as_double (arg2));
254 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
255 || code1 == TYPE_CODE_RANGE)
256 && (scalar || code2 == TYPE_CODE_PTR))
257 {
258 LONGEST longest;
c5aa993b
JM
259
260 if (hp_som_som_object_present && /* if target compiled by HP aCC */
261 (code2 == TYPE_CODE_PTR))
262 {
263 unsigned int *ptr;
264 value_ptr retvalp;
265
266 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
267 {
268 /* With HP aCC, pointers to data members have a bias */
269 case TYPE_CODE_MEMBER:
270 retvalp = value_from_longest (type, value_as_long (arg2));
716c501e 271 /* force evaluation */
802db21b 272 ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
c5aa993b
JM
273 *ptr &= ~0x20000000; /* zap 29th bit to remove bias */
274 return retvalp;
275
276 /* While pointers to methods don't really point to a function */
277 case TYPE_CODE_METHOD:
278 error ("Pointers to methods not supported with HP aCC");
279
280 default:
281 break; /* fall out and go to normal handling */
282 }
283 }
2bf1f4a1
JB
284
285 /* When we cast pointers to integers, we mustn't use
286 POINTER_TO_ADDRESS to find the address the pointer
287 represents, as value_as_long would. GDB should evaluate
288 expressions just as the compiler would --- and the compiler
289 sees a cast as a simple reinterpretation of the pointer's
290 bits. */
291 if (code2 == TYPE_CODE_PTR)
292 longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
293 TYPE_LENGTH (type2));
294 else
295 longest = value_as_long (arg2);
802db21b 296 return value_from_longest (type, convert_to_boolean ?
716c501e 297 (LONGEST) (longest ? 1 : 0) : longest);
c906108c 298 }
802db21b 299 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT ||
23e04971
MS
300 code2 == TYPE_CODE_ENUM ||
301 code2 == TYPE_CODE_RANGE))
634acd5f 302 {
4603e466
DT
303 /* TYPE_LENGTH (type) is the length of a pointer, but we really
304 want the length of an address! -- we are really dealing with
305 addresses (i.e., gdb representations) not pointers (i.e.,
306 target representations) here.
307
308 This allows things like "print *(int *)0x01000234" to work
309 without printing a misleading message -- which would
310 otherwise occur when dealing with a target having two byte
311 pointers and four byte addresses. */
312
313 int addr_bit = TARGET_ADDR_BIT;
314
634acd5f 315 LONGEST longest = value_as_long (arg2);
4603e466 316 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
634acd5f 317 {
4603e466
DT
318 if (longest >= ((LONGEST) 1 << addr_bit)
319 || longest <= -((LONGEST) 1 << addr_bit))
634acd5f
AC
320 warning ("value truncated");
321 }
322 return value_from_longest (type, longest);
323 }
c906108c
SS
324 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
325 {
326 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
327 {
328 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
329 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
c5aa993b 330 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
c906108c
SS
331 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
332 && !value_logical_not (arg2))
333 {
334 value_ptr v;
335
336 /* Look in the type of the source to see if it contains the
7b83ea04
AC
337 type of the target as a superclass. If so, we'll need to
338 offset the pointer rather than just change its type. */
c906108c
SS
339 if (TYPE_NAME (t1) != NULL)
340 {
341 v = search_struct_field (type_name_no_tag (t1),
342 value_ind (arg2), 0, t2, 1);
343 if (v)
344 {
345 v = value_addr (v);
346 VALUE_TYPE (v) = type;
347 return v;
348 }
349 }
350
351 /* Look in the type of the target to see if it contains the
7b83ea04
AC
352 type of the source as a superclass. If so, we'll need to
353 offset the pointer rather than just change its type.
354 FIXME: This fails silently with virtual inheritance. */
c906108c
SS
355 if (TYPE_NAME (t2) != NULL)
356 {
357 v = search_struct_field (type_name_no_tag (t2),
c5aa993b 358 value_zero (t1, not_lval), 0, t1, 1);
c906108c
SS
359 if (v)
360 {
361 value_ptr v2 = value_ind (arg2);
362 VALUE_ADDRESS (v2) -= VALUE_ADDRESS (v)
c5aa993b 363 + VALUE_OFFSET (v);
070ad9f0
DB
364
365 /* JYG: adjust the new pointer value and
366 embedded offset. */
367 v2->aligner.contents[0] -= VALUE_EMBEDDED_OFFSET (v);
368 VALUE_EMBEDDED_OFFSET (v2) = 0;
369
c906108c
SS
370 v2 = value_addr (v2);
371 VALUE_TYPE (v2) = type;
372 return v2;
373 }
374 }
375 }
376 /* No superclass found, just fall through to change ptr type. */
377 }
378 VALUE_TYPE (arg2) = type;
2b127877 379 arg2 = value_change_enclosing_type (arg2, type);
c5aa993b 380 VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */
c906108c
SS
381 return arg2;
382 }
383 else if (chill_varying_type (type))
384 {
385 struct type *range1, *range2, *eltype1, *eltype2;
386 value_ptr val;
387 int count1, count2;
388 LONGEST low_bound, high_bound;
389 char *valaddr, *valaddr_data;
390 /* For lint warning about eltype2 possibly uninitialized: */
391 eltype2 = NULL;
392 if (code2 == TYPE_CODE_BITSTRING)
393 error ("not implemented: converting bitstring to varying type");
394 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
395 || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))),
396 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)),
397 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
c5aa993b 398 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
c906108c
SS
399 error ("Invalid conversion to varying type");
400 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
401 range2 = TYPE_FIELD_TYPE (type2, 0);
402 if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0)
403 count1 = -1;
404 else
405 count1 = high_bound - low_bound + 1;
406 if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0)
c5aa993b 407 count1 = -1, count2 = 0; /* To force error before */
c906108c
SS
408 else
409 count2 = high_bound - low_bound + 1;
410 if (count2 > count1)
411 error ("target varying type is too small");
412 val = allocate_value (type);
413 valaddr = VALUE_CONTENTS_RAW (val);
414 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
415 /* Set val's __var_length field to count2. */
416 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
417 count2);
418 /* Set the __var_data field to count2 elements copied from arg2. */
419 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
420 count2 * TYPE_LENGTH (eltype2));
421 /* Zero the rest of the __var_data field of val. */
422 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
423 (count1 - count2) * TYPE_LENGTH (eltype2));
424 return val;
425 }
426 else if (VALUE_LVAL (arg2) == lval_memory)
427 {
428 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
429 VALUE_BFD_SECTION (arg2));
430 }
431 else if (code1 == TYPE_CODE_VOID)
432 {
433 return value_zero (builtin_type_void, not_lval);
434 }
435 else
436 {
437 error ("Invalid cast.");
438 return 0;
439 }
440}
441
442/* Create a value of type TYPE that is zero, and return it. */
443
444value_ptr
fba45db2 445value_zero (struct type *type, enum lval_type lv)
c906108c
SS
446{
447 register value_ptr val = allocate_value (type);
448
449 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
450 VALUE_LVAL (val) = lv;
451
452 return val;
453}
454
070ad9f0 455/* Return a value with type TYPE located at ADDR.
c906108c
SS
456
457 Call value_at only if the data needs to be fetched immediately;
458 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
459 value_at_lazy instead. value_at_lazy simply records the address of
070ad9f0
DB
460 the data and sets the lazy-evaluation-required flag. The lazy flag
461 is tested in the VALUE_CONTENTS macro, which is used if and when
462 the contents are actually required.
c906108c
SS
463
464 Note: value_at does *NOT* handle embedded offsets; perform such
465 adjustments before or after calling it. */
466
467value_ptr
fba45db2 468value_at (struct type *type, CORE_ADDR addr, asection *sect)
c906108c
SS
469{
470 register value_ptr val;
471
472 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
473 error ("Attempt to dereference a generic pointer.");
474
475 val = allocate_value (type);
476
7a292a7a
SS
477 if (GDB_TARGET_IS_D10V
478 && TYPE_CODE (type) == TYPE_CODE_PTR
c906108c
SS
479 && TYPE_TARGET_TYPE (type)
480 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
481 {
482 /* pointer to function */
483 unsigned long num;
484 unsigned short snum;
485 snum = read_memory_unsigned_integer (addr, 2);
7a292a7a
SS
486 num = D10V_MAKE_IADDR (snum);
487 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 488 }
7a292a7a 489 else if (GDB_TARGET_IS_D10V
c5aa993b 490 && TYPE_CODE (type) == TYPE_CODE_PTR)
c906108c
SS
491 {
492 /* pointer to data */
493 unsigned long num;
494 unsigned short snum;
495 snum = read_memory_unsigned_integer (addr, 2);
7a292a7a 496 num = D10V_MAKE_DADDR (snum);
c5aa993b 497 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c
SS
498 }
499 else
d4b2399a 500 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
c906108c
SS
501
502 VALUE_LVAL (val) = lval_memory;
503 VALUE_ADDRESS (val) = addr;
504 VALUE_BFD_SECTION (val) = sect;
505
506 return val;
507}
508
509/* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
510
511value_ptr
fba45db2 512value_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
c906108c
SS
513{
514 register value_ptr val;
515
516 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
517 error ("Attempt to dereference a generic pointer.");
518
519 val = allocate_value (type);
520
521 VALUE_LVAL (val) = lval_memory;
522 VALUE_ADDRESS (val) = addr;
523 VALUE_LAZY (val) = 1;
524 VALUE_BFD_SECTION (val) = sect;
525
526 return val;
527}
528
070ad9f0
DB
529/* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
530 if the current data for a variable needs to be loaded into
531 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
c906108c
SS
532 clears the lazy flag to indicate that the data in the buffer is valid.
533
534 If the value is zero-length, we avoid calling read_memory, which would
535 abort. We mark the value as fetched anyway -- all 0 bytes of it.
536
537 This function returns a value because it is used in the VALUE_CONTENTS
538 macro as part of an expression, where a void would not work. The
539 value is ignored. */
540
541int
fba45db2 542value_fetch_lazy (register value_ptr val)
c906108c
SS
543{
544 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
545 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
546
c5aa993b 547 struct type *type = VALUE_TYPE (val);
7a292a7a
SS
548 if (GDB_TARGET_IS_D10V
549 && TYPE_CODE (type) == TYPE_CODE_PTR
c906108c
SS
550 && TYPE_TARGET_TYPE (type)
551 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
552 {
553 /* pointer to function */
554 unsigned long num;
555 unsigned short snum;
556 snum = read_memory_unsigned_integer (addr, 2);
c5aa993b
JM
557 num = D10V_MAKE_IADDR (snum);
558 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 559 }
7a292a7a 560 else if (GDB_TARGET_IS_D10V
c5aa993b 561 && TYPE_CODE (type) == TYPE_CODE_PTR)
c906108c
SS
562 {
563 /* pointer to data */
564 unsigned long num;
565 unsigned short snum;
566 snum = read_memory_unsigned_integer (addr, 2);
c5aa993b
JM
567 num = D10V_MAKE_DADDR (snum);
568 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 569 }
7a292a7a 570 else if (length)
d4b2399a 571 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
802db21b 572
c906108c
SS
573 VALUE_LAZY (val) = 0;
574 return 0;
575}
576
577
578/* Store the contents of FROMVAL into the location of TOVAL.
579 Return a new value with the location of TOVAL and contents of FROMVAL. */
580
581value_ptr
fba45db2 582value_assign (register value_ptr toval, register value_ptr fromval)
c906108c
SS
583{
584 register struct type *type;
585 register value_ptr val;
e6cbd02a 586 char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
587 int use_buffer = 0;
588
589 if (!toval->modifiable)
590 error ("Left operand of assignment is not a modifiable lvalue.");
591
592 COERCE_REF (toval);
593
594 type = VALUE_TYPE (toval);
595 if (VALUE_LVAL (toval) != lval_internalvar)
596 fromval = value_cast (type, fromval);
597 else
598 COERCE_ARRAY (fromval);
599 CHECK_TYPEDEF (type);
600
601 /* If TOVAL is a special machine register requiring conversion
602 of program values to a special raw format,
603 convert FROMVAL's contents now, with result in `raw_buffer',
604 and set USE_BUFFER to the number of bytes to write. */
605
ac9a91a7 606 if (VALUE_REGNO (toval) >= 0)
c906108c
SS
607 {
608 int regno = VALUE_REGNO (toval);
609 if (REGISTER_CONVERTIBLE (regno))
610 {
611 struct type *fromtype = check_typedef (VALUE_TYPE (fromval));
612 REGISTER_CONVERT_TO_RAW (fromtype, regno,
613 VALUE_CONTENTS (fromval), raw_buffer);
614 use_buffer = REGISTER_RAW_SIZE (regno);
615 }
616 }
c906108c
SS
617
618 switch (VALUE_LVAL (toval))
619 {
620 case lval_internalvar:
621 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
622 val = value_copy (VALUE_INTERNALVAR (toval)->value);
2b127877 623 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
c906108c
SS
624 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
625 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
626 return val;
627
628 case lval_internalvar_component:
629 set_internalvar_component (VALUE_INTERNALVAR (toval),
630 VALUE_OFFSET (toval),
631 VALUE_BITPOS (toval),
632 VALUE_BITSIZE (toval),
633 fromval);
634 break;
635
636 case lval_memory:
637 {
638 char *dest_buffer;
c5aa993b
JM
639 CORE_ADDR changed_addr;
640 int changed_len;
c906108c 641
c5aa993b
JM
642 if (VALUE_BITSIZE (toval))
643 {
c906108c
SS
644 char buffer[sizeof (LONGEST)];
645 /* We assume that the argument to read_memory is in units of
646 host chars. FIXME: Is that correct? */
647 changed_len = (VALUE_BITPOS (toval)
c5aa993b
JM
648 + VALUE_BITSIZE (toval)
649 + HOST_CHAR_BIT - 1)
650 / HOST_CHAR_BIT;
c906108c
SS
651
652 if (changed_len > (int) sizeof (LONGEST))
653 error ("Can't handle bitfields which don't fit in a %d bit word.",
654 sizeof (LONGEST) * HOST_CHAR_BIT);
655
656 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
657 buffer, changed_len);
658 modify_field (buffer, value_as_long (fromval),
659 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
660 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
661 dest_buffer = buffer;
662 }
663 else if (use_buffer)
664 {
665 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
666 changed_len = use_buffer;
667 dest_buffer = raw_buffer;
668 }
669 else
670 {
671 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
672 changed_len = TYPE_LENGTH (type);
673 dest_buffer = VALUE_CONTENTS (fromval);
674 }
675
676 write_memory (changed_addr, dest_buffer, changed_len);
677 if (memory_changed_hook)
678 memory_changed_hook (changed_addr, changed_len);
679 }
680 break;
681
682 case lval_register:
683 if (VALUE_BITSIZE (toval))
684 {
685 char buffer[sizeof (LONGEST)];
802db21b 686 int len =
8903de4f 687 REGISTER_RAW_SIZE (VALUE_REGNO (toval)) - VALUE_OFFSET (toval);
c906108c
SS
688
689 if (len > (int) sizeof (LONGEST))
690 error ("Can't handle bitfields in registers larger than %d bits.",
691 sizeof (LONGEST) * HOST_CHAR_BIT);
692
693 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
694 > len * HOST_CHAR_BIT)
695 /* Getting this right would involve being very careful about
696 byte order. */
c2d11a7d
JM
697 error ("Can't assign to bitfields that cross register "
698 "boundaries.");
c906108c 699
c5aa993b
JM
700 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
701 buffer, len);
702 modify_field (buffer, value_as_long (fromval),
703 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
704 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
705 buffer, len);
c906108c
SS
706 }
707 else if (use_buffer)
708 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
709 raw_buffer, use_buffer);
710 else
c5aa993b 711 {
c906108c
SS
712 /* Do any conversion necessary when storing this type to more
713 than one register. */
714#ifdef REGISTER_CONVERT_FROM_TYPE
715 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
c5aa993b 716 REGISTER_CONVERT_FROM_TYPE (VALUE_REGNO (toval), type, raw_buffer);
c906108c
SS
717 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
718 raw_buffer, TYPE_LENGTH (type));
719#else
720 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
c5aa993b 721 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
c906108c
SS
722#endif
723 }
724 /* Assigning to the stack pointer, frame pointer, and other
7b83ea04
AC
725 (architecture and calling convention specific) registers may
726 cause the frame cache to be out of date. We just do this
727 on all assignments to registers for simplicity; I doubt the slowdown
728 matters. */
c906108c
SS
729 reinit_frame_cache ();
730 break;
731
732 case lval_reg_frame_relative:
733 {
734 /* value is stored in a series of registers in the frame
735 specified by the structure. Copy that value out, modify
736 it, and copy it back in. */
737 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
738 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
739 int byte_offset = VALUE_OFFSET (toval) % reg_size;
740 int reg_offset = VALUE_OFFSET (toval) / reg_size;
741 int amount_copied;
742
743 /* Make the buffer large enough in all cases. */
744 char *buffer = (char *) alloca (amount_to_copy
745 + sizeof (LONGEST)
746 + MAX_REGISTER_RAW_SIZE);
747
748 int regno;
749 struct frame_info *frame;
750
751 /* Figure out which frame this is in currently. */
752 for (frame = get_current_frame ();
753 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
754 frame = get_prev_frame (frame))
755 ;
756
757 if (!frame)
758 error ("Value being assigned to is no longer active.");
759
760 amount_to_copy += (reg_size - amount_to_copy % reg_size);
761
762 /* Copy it out. */
763 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
764 amount_copied = 0);
765 amount_copied < amount_to_copy;
766 amount_copied += reg_size, regno++)
767 {
768 get_saved_register (buffer + amount_copied,
c5aa993b
JM
769 (int *) NULL, (CORE_ADDR *) NULL,
770 frame, regno, (enum lval_type *) NULL);
c906108c
SS
771 }
772
773 /* Modify what needs to be modified. */
774 if (VALUE_BITSIZE (toval))
775 modify_field (buffer + byte_offset,
776 value_as_long (fromval),
777 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
778 else if (use_buffer)
779 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
780 else
781 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
782 TYPE_LENGTH (type));
783
784 /* Copy it back. */
785 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
786 amount_copied = 0);
787 amount_copied < amount_to_copy;
788 amount_copied += reg_size, regno++)
789 {
790 enum lval_type lval;
791 CORE_ADDR addr;
792 int optim;
793
794 /* Just find out where to put it. */
c5aa993b
JM
795 get_saved_register ((char *) NULL,
796 &optim, &addr, frame, regno, &lval);
797
c906108c
SS
798 if (optim)
799 error ("Attempt to assign to a value that was optimized out.");
800 if (lval == lval_memory)
801 write_memory (addr, buffer + amount_copied, reg_size);
802 else if (lval == lval_register)
803 write_register_bytes (addr, buffer + amount_copied, reg_size);
804 else
805 error ("Attempt to assign to an unmodifiable value.");
806 }
807
808 if (register_changed_hook)
809 register_changed_hook (-1);
810 }
811 break;
c5aa993b 812
c906108c
SS
813
814 default:
815 error ("Left operand of assignment is not an lvalue.");
816 }
817
818 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
819 If the field is signed, and is negative, then sign extend. */
820 if ((VALUE_BITSIZE (toval) > 0)
821 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
822 {
823 LONGEST fieldval = value_as_long (fromval);
824 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
825
826 fieldval &= valmask;
827 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
828 fieldval |= ~valmask;
829
830 fromval = value_from_longest (type, fieldval);
831 }
832
833 val = value_copy (toval);
834 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
835 TYPE_LENGTH (type));
836 VALUE_TYPE (val) = type;
2b127877 837 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
c906108c
SS
838 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
839 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
c5aa993b 840
c906108c
SS
841 return val;
842}
843
844/* Extend a value VAL to COUNT repetitions of its type. */
845
846value_ptr
fba45db2 847value_repeat (value_ptr arg1, int count)
c906108c
SS
848{
849 register value_ptr val;
850
851 if (VALUE_LVAL (arg1) != lval_memory)
852 error ("Only values in memory can be extended with '@'.");
853 if (count < 1)
854 error ("Invalid number %d of repetitions.", count);
855
856 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
857
858 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
859 VALUE_CONTENTS_ALL_RAW (val),
860 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
861 VALUE_LVAL (val) = lval_memory;
862 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
863
864 return val;
865}
866
867value_ptr
fba45db2 868value_of_variable (struct symbol *var, struct block *b)
c906108c
SS
869{
870 value_ptr val;
871 struct frame_info *frame = NULL;
872
873 if (!b)
874 frame = NULL; /* Use selected frame. */
875 else if (symbol_read_needs_frame (var))
876 {
877 frame = block_innermost_frame (b);
878 if (!frame)
c5aa993b 879 {
c906108c
SS
880 if (BLOCK_FUNCTION (b)
881 && SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)))
882 error ("No frame is currently executing in block %s.",
883 SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)));
884 else
885 error ("No frame is currently executing in specified block");
c5aa993b 886 }
c906108c
SS
887 }
888
889 val = read_var_value (var, frame);
890 if (!val)
891 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
892
893 return val;
894}
895
896/* Given a value which is an array, return a value which is a pointer to its
897 first element, regardless of whether or not the array has a nonzero lower
898 bound.
899
900 FIXME: A previous comment here indicated that this routine should be
901 substracting the array's lower bound. It's not clear to me that this
902 is correct. Given an array subscripting operation, it would certainly
903 work to do the adjustment here, essentially computing:
904
905 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
906
907 However I believe a more appropriate and logical place to account for
908 the lower bound is to do so in value_subscript, essentially computing:
909
910 (&array[0] + ((index - lowerbound) * sizeof array[0]))
911
912 As further evidence consider what would happen with operations other
913 than array subscripting, where the caller would get back a value that
914 had an address somewhere before the actual first element of the array,
915 and the information about the lower bound would be lost because of
916 the coercion to pointer type.
c5aa993b 917 */
c906108c
SS
918
919value_ptr
fba45db2 920value_coerce_array (value_ptr arg1)
c906108c
SS
921{
922 register struct type *type = check_typedef (VALUE_TYPE (arg1));
923
924 if (VALUE_LVAL (arg1) != lval_memory)
925 error ("Attempt to take address of value not located in memory.");
926
4478b372
JB
927 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
928 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
929}
930
931/* Given a value which is a function, return a value which is a pointer
932 to it. */
933
934value_ptr
fba45db2 935value_coerce_function (value_ptr arg1)
c906108c
SS
936{
937 value_ptr retval;
938
939 if (VALUE_LVAL (arg1) != lval_memory)
940 error ("Attempt to take address of value not located in memory.");
941
4478b372
JB
942 retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
943 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
944 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
945 return retval;
c5aa993b 946}
c906108c
SS
947
948/* Return a pointer value for the object for which ARG1 is the contents. */
949
950value_ptr
fba45db2 951value_addr (value_ptr arg1)
c906108c
SS
952{
953 value_ptr arg2;
954
955 struct type *type = check_typedef (VALUE_TYPE (arg1));
956 if (TYPE_CODE (type) == TYPE_CODE_REF)
957 {
958 /* Copy the value, but change the type from (T&) to (T*).
7b83ea04
AC
959 We keep the same location information, which is efficient,
960 and allows &(&X) to get the location containing the reference. */
c906108c
SS
961 arg2 = value_copy (arg1);
962 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
963 return arg2;
964 }
965 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
966 return value_coerce_function (arg1);
967
968 if (VALUE_LVAL (arg1) != lval_memory)
969 error ("Attempt to take address of value not located in memory.");
970
c5aa993b 971 /* Get target memory address */
4478b372
JB
972 arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
973 (VALUE_ADDRESS (arg1)
974 + VALUE_OFFSET (arg1)
975 + VALUE_EMBEDDED_OFFSET (arg1)));
c906108c
SS
976
977 /* This may be a pointer to a base subobject; so remember the
c5aa993b 978 full derived object's type ... */
2b127877 979 arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
c5aa993b
JM
980 /* ... and also the relative position of the subobject in the full object */
981 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
c906108c
SS
982 VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
983 return arg2;
984}
985
986/* Given a value of a pointer type, apply the C unary * operator to it. */
987
988value_ptr
fba45db2 989value_ind (value_ptr arg1)
c906108c
SS
990{
991 struct type *base_type;
992 value_ptr arg2;
c906108c
SS
993
994 COERCE_ARRAY (arg1);
995
996 base_type = check_typedef (VALUE_TYPE (arg1));
997
998 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
999 error ("not implemented: member types in value_ind");
1000
1001 /* Allow * on an integer so we can cast it to whatever we want.
1002 This returns an int, which seems like the most C-like thing
1003 to do. "long long" variables are rare enough that
1004 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1005 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
1006 return value_at (builtin_type_int,
1007 (CORE_ADDR) value_as_long (arg1),
1008 VALUE_BFD_SECTION (arg1));
1009 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
1010 {
1011 struct type *enc_type;
1012 /* We may be pointing to something embedded in a larger object */
c5aa993b 1013 /* Get the real type of the enclosing object */
c906108c
SS
1014 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
1015 enc_type = TYPE_TARGET_TYPE (enc_type);
c5aa993b
JM
1016 /* Retrieve the enclosing object pointed to */
1017 arg2 = value_at_lazy (enc_type,
1018 value_as_pointer (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
1019 VALUE_BFD_SECTION (arg1));
1020 /* Re-adjust type */
c906108c
SS
1021 VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
1022 /* Add embedding info */
2b127877 1023 arg2 = value_change_enclosing_type (arg2, enc_type);
c906108c
SS
1024 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
1025
1026 /* We may be pointing to an object of some derived type */
1027 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
1028 return arg2;
1029 }
1030
1031 error ("Attempt to take contents of a non-pointer value.");
c5aa993b 1032 return 0; /* For lint -- never reached */
c906108c
SS
1033}
1034\f
1035/* Pushing small parts of stack frames. */
1036
1037/* Push one word (the size of object that a register holds). */
1038
1039CORE_ADDR
fba45db2 1040push_word (CORE_ADDR sp, ULONGEST word)
c906108c
SS
1041{
1042 register int len = REGISTER_SIZE;
e6cbd02a 1043 char *buffer = alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
1044
1045 store_unsigned_integer (buffer, len, word);
1046 if (INNER_THAN (1, 2))
1047 {
1048 /* stack grows downward */
1049 sp -= len;
1050 write_memory (sp, buffer, len);
1051 }
1052 else
1053 {
1054 /* stack grows upward */
1055 write_memory (sp, buffer, len);
1056 sp += len;
1057 }
1058
1059 return sp;
1060}
1061
1062/* Push LEN bytes with data at BUFFER. */
1063
1064CORE_ADDR
fba45db2 1065push_bytes (CORE_ADDR sp, char *buffer, int len)
c906108c
SS
1066{
1067 if (INNER_THAN (1, 2))
1068 {
1069 /* stack grows downward */
1070 sp -= len;
1071 write_memory (sp, buffer, len);
1072 }
1073 else
1074 {
1075 /* stack grows upward */
1076 write_memory (sp, buffer, len);
1077 sp += len;
1078 }
1079
1080 return sp;
1081}
1082
2df3850c
JM
1083#ifndef PARM_BOUNDARY
1084#define PARM_BOUNDARY (0)
1085#endif
1086
1087/* Push onto the stack the specified value VALUE. Pad it correctly for
1088 it to be an argument to a function. */
c906108c 1089
c906108c 1090static CORE_ADDR
fba45db2 1091value_push (register CORE_ADDR sp, value_ptr arg)
c906108c
SS
1092{
1093 register int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
917317f4 1094 register int container_len = len;
2df3850c
JM
1095 register int offset;
1096
1097 /* How big is the container we're going to put this value in? */
1098 if (PARM_BOUNDARY)
1099 container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1)
1100 & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1));
1101
1102 /* Are we going to put it at the high or low end of the container? */
1103 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1104 offset = container_len - len;
1105 else
1106 offset = 0;
c906108c
SS
1107
1108 if (INNER_THAN (1, 2))
1109 {
1110 /* stack grows downward */
2df3850c
JM
1111 sp -= container_len;
1112 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
c906108c
SS
1113 }
1114 else
1115 {
1116 /* stack grows upward */
2df3850c
JM
1117 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1118 sp += container_len;
c906108c
SS
1119 }
1120
1121 return sp;
1122}
1123
392a587b
JM
1124#ifndef PUSH_ARGUMENTS
1125#define PUSH_ARGUMENTS default_push_arguments
1126#endif
1127
1128CORE_ADDR
fba45db2
KB
1129default_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
1130 int struct_return, CORE_ADDR struct_addr)
392a587b
JM
1131{
1132 /* ASSERT ( !struct_return); */
1133 int i;
1134 for (i = nargs - 1; i >= 0; i--)
1135 sp = value_push (sp, args[i]);
1136 return sp;
1137}
1138
c906108c 1139
b9a8e3bf
JB
1140/* A default function for COERCE_FLOAT_TO_DOUBLE: do the coercion only
1141 when we don't have any type for the argument at hand. This occurs
1142 when we have no debug info, or when passing varargs.
1143
1144 This is an annoying default: the rule the compiler follows is to do
1145 the standard promotions whenever there is no prototype in scope,
1146 and almost all targets want this behavior. But there are some old
1147 architectures which want this odd behavior. If you want to go
1148 through them all and fix them, please do. Modern gdbarch-style
1149 targets may find it convenient to use standard_coerce_float_to_double. */
1150int
1151default_coerce_float_to_double (struct type *formal, struct type *actual)
1152{
1153 return formal == NULL;
1154}
1155
1156
1157/* Always coerce floats to doubles when there is no prototype in scope.
1158 If your architecture follows the standard type promotion rules for
1159 calling unprototyped functions, your gdbarch init function can pass
1160 this function to set_gdbarch_coerce_float_to_double to use its logic. */
1161int
1162standard_coerce_float_to_double (struct type *formal, struct type *actual)
1163{
1164 return 1;
1165}
1166
1167
c906108c
SS
1168/* Perform the standard coercions that are specified
1169 for arguments to be passed to C functions.
1170
1171 If PARAM_TYPE is non-NULL, it is the expected parameter type.
1172 IS_PROTOTYPED is non-zero if the function declaration is prototyped. */
1173
1174static value_ptr
fba45db2 1175value_arg_coerce (value_ptr arg, struct type *param_type, int is_prototyped)
c906108c
SS
1176{
1177 register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1178 register struct type *type
c5aa993b 1179 = param_type ? check_typedef (param_type) : arg_type;
c906108c
SS
1180
1181 switch (TYPE_CODE (type))
1182 {
1183 case TYPE_CODE_REF:
1184 if (TYPE_CODE (arg_type) != TYPE_CODE_REF)
1185 {
1186 arg = value_addr (arg);
1187 VALUE_TYPE (arg) = param_type;
1188 return arg;
1189 }
1190 break;
1191 case TYPE_CODE_INT:
1192 case TYPE_CODE_CHAR:
1193 case TYPE_CODE_BOOL:
1194 case TYPE_CODE_ENUM:
1195 /* If we don't have a prototype, coerce to integer type if necessary. */
1196 if (!is_prototyped)
1197 {
1198 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1199 type = builtin_type_int;
1200 }
1201 /* Currently all target ABIs require at least the width of an integer
7b83ea04
AC
1202 type for an argument. We may have to conditionalize the following
1203 type coercion for future targets. */
c906108c
SS
1204 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1205 type = builtin_type_int;
1206 break;
1207 case TYPE_CODE_FLT:
1208 /* FIXME: We should always convert floats to doubles in the
7b83ea04
AC
1209 non-prototyped case. As many debugging formats include
1210 no information about prototyping, we have to live with
1211 COERCE_FLOAT_TO_DOUBLE for now. */
b9a8e3bf 1212 if (!is_prototyped && COERCE_FLOAT_TO_DOUBLE (param_type, arg_type))
c906108c
SS
1213 {
1214 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
1215 type = builtin_type_double;
1216 else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
1217 type = builtin_type_long_double;
1218 }
1219 break;
1220 case TYPE_CODE_FUNC:
1221 type = lookup_pointer_type (type);
1222 break;
1223 case TYPE_CODE_ARRAY:
1224 if (current_language->c_style_arrays)
1225 type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1226 break;
1227 case TYPE_CODE_UNDEF:
1228 case TYPE_CODE_PTR:
1229 case TYPE_CODE_STRUCT:
1230 case TYPE_CODE_UNION:
1231 case TYPE_CODE_VOID:
1232 case TYPE_CODE_SET:
1233 case TYPE_CODE_RANGE:
1234 case TYPE_CODE_STRING:
1235 case TYPE_CODE_BITSTRING:
1236 case TYPE_CODE_ERROR:
1237 case TYPE_CODE_MEMBER:
1238 case TYPE_CODE_METHOD:
1239 case TYPE_CODE_COMPLEX:
1240 default:
1241 break;
1242 }
1243
1244 return value_cast (type, arg);
1245}
1246
070ad9f0 1247/* Determine a function's address and its return type from its value.
c906108c
SS
1248 Calls error() if the function is not valid for calling. */
1249
1250static CORE_ADDR
fba45db2 1251find_function_addr (value_ptr function, struct type **retval_type)
c906108c
SS
1252{
1253 register struct type *ftype = check_typedef (VALUE_TYPE (function));
1254 register enum type_code code = TYPE_CODE (ftype);
1255 struct type *value_type;
1256 CORE_ADDR funaddr;
1257
1258 /* If it's a member function, just look at the function
1259 part of it. */
1260
1261 /* Determine address to call. */
1262 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1263 {
1264 funaddr = VALUE_ADDRESS (function);
1265 value_type = TYPE_TARGET_TYPE (ftype);
1266 }
1267 else if (code == TYPE_CODE_PTR)
1268 {
1269 funaddr = value_as_pointer (function);
1270 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
1271 if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
1272 || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
1273 {
c906108c 1274 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
c906108c
SS
1275 value_type = TYPE_TARGET_TYPE (ftype);
1276 }
1277 else
1278 value_type = builtin_type_int;
1279 }
1280 else if (code == TYPE_CODE_INT)
1281 {
1282 /* Handle the case of functions lacking debugging info.
7b83ea04 1283 Their values are characters since their addresses are char */
c906108c
SS
1284 if (TYPE_LENGTH (ftype) == 1)
1285 funaddr = value_as_pointer (value_addr (function));
1286 else
1287 /* Handle integer used as address of a function. */
1288 funaddr = (CORE_ADDR) value_as_long (function);
1289
1290 value_type = builtin_type_int;
1291 }
1292 else
1293 error ("Invalid data type for function to be called.");
1294
1295 *retval_type = value_type;
1296 return funaddr;
1297}
1298
1299/* All this stuff with a dummy frame may seem unnecessarily complicated
1300 (why not just save registers in GDB?). The purpose of pushing a dummy
1301 frame which looks just like a real frame is so that if you call a
1302 function and then hit a breakpoint (get a signal, etc), "backtrace"
1303 will look right. Whether the backtrace needs to actually show the
1304 stack at the time the inferior function was called is debatable, but
1305 it certainly needs to not display garbage. So if you are contemplating
1306 making dummy frames be different from normal frames, consider that. */
1307
1308/* Perform a function call in the inferior.
1309 ARGS is a vector of values of arguments (NARGS of them).
1310 FUNCTION is a value, the function to be called.
1311 Returns a value representing what the function returned.
1312 May fail to return, if a breakpoint or signal is hit
1313 during the execution of the function.
1314
1315 ARGS is modified to contain coerced values. */
1316
a14ed312
KB
1317static value_ptr hand_function_call (value_ptr function, int nargs,
1318 value_ptr * args);
7a292a7a 1319static value_ptr
fba45db2 1320hand_function_call (value_ptr function, int nargs, value_ptr *args)
c906108c
SS
1321{
1322 register CORE_ADDR sp;
1323 register int i;
da59e081 1324 int rc;
c906108c
SS
1325 CORE_ADDR start_sp;
1326 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
1327 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
1328 and remove any extra bytes which might exist because ULONGEST is
070ad9f0 1329 bigger than REGISTER_SIZE.
c906108c
SS
1330
1331 NOTE: This is pretty wierd, as the call dummy is actually a
c5aa993b
JM
1332 sequence of instructions. But CISC machines will have
1333 to pack the instructions into REGISTER_SIZE units (and
1334 so will RISC machines for which INSTRUCTION_SIZE is not
1335 REGISTER_SIZE).
7a292a7a
SS
1336
1337 NOTE: This is pretty stupid. CALL_DUMMY should be in strict
c5aa993b 1338 target byte order. */
c906108c 1339
7a292a7a
SS
1340 static ULONGEST *dummy;
1341 int sizeof_dummy1;
1342 char *dummy1;
c906108c
SS
1343 CORE_ADDR old_sp;
1344 struct type *value_type;
1345 unsigned char struct_return;
1346 CORE_ADDR struct_addr = 0;
7a292a7a 1347 struct inferior_status *inf_status;
c906108c
SS
1348 struct cleanup *old_chain;
1349 CORE_ADDR funaddr;
c5aa993b 1350 int using_gcc; /* Set to version of gcc in use, or zero if not gcc */
c906108c
SS
1351 CORE_ADDR real_pc;
1352 struct type *param_type = NULL;
1353 struct type *ftype = check_typedef (SYMBOL_TYPE (function));
1354
7a292a7a
SS
1355 dummy = alloca (SIZEOF_CALL_DUMMY_WORDS);
1356 sizeof_dummy1 = REGISTER_SIZE * SIZEOF_CALL_DUMMY_WORDS / sizeof (ULONGEST);
1357 dummy1 = alloca (sizeof_dummy1);
1358 memcpy (dummy, CALL_DUMMY_WORDS, SIZEOF_CALL_DUMMY_WORDS);
1359
c906108c 1360 if (!target_has_execution)
c5aa993b 1361 noprocess ();
c906108c 1362
7a292a7a 1363 inf_status = save_inferior_status (1);
74b7792f 1364 old_chain = make_cleanup_restore_inferior_status (inf_status);
c906108c
SS
1365
1366 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
1367 (and POP_FRAME for restoring them). (At least on most machines)
1368 they are saved on the stack in the inferior. */
1369 PUSH_DUMMY_FRAME;
1370
1371 old_sp = sp = read_sp ();
1372
1373 if (INNER_THAN (1, 2))
1374 {
1375 /* Stack grows down */
7a292a7a 1376 sp -= sizeof_dummy1;
c906108c
SS
1377 start_sp = sp;
1378 }
1379 else
1380 {
1381 /* Stack grows up */
1382 start_sp = sp;
7a292a7a 1383 sp += sizeof_dummy1;
c906108c
SS
1384 }
1385
1386 funaddr = find_function_addr (function, &value_type);
1387 CHECK_TYPEDEF (value_type);
1388
1389 {
1390 struct block *b = block_for_pc (funaddr);
1391 /* If compiled without -g, assume GCC 2. */
1392 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
1393 }
1394
1395 /* Are we returning a value using a structure return or a normal
1396 value return? */
1397
1398 struct_return = using_struct_return (function, funaddr, value_type,
1399 using_gcc);
1400
1401 /* Create a call sequence customized for this function
1402 and the number of arguments for it. */
7a292a7a 1403 for (i = 0; i < (int) (SIZEOF_CALL_DUMMY_WORDS / sizeof (dummy[0])); i++)
c906108c
SS
1404 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1405 REGISTER_SIZE,
c5aa993b 1406 (ULONGEST) dummy[i]);
c906108c
SS
1407
1408#ifdef GDB_TARGET_IS_HPPA
1409 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1410 value_type, using_gcc);
1411#else
1412 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1413 value_type, using_gcc);
1414 real_pc = start_sp;
1415#endif
1416
7a292a7a
SS
1417 if (CALL_DUMMY_LOCATION == ON_STACK)
1418 {
c5aa993b 1419 write_memory (start_sp, (char *) dummy1, sizeof_dummy1);
7a292a7a 1420 }
c906108c 1421
7a292a7a
SS
1422 if (CALL_DUMMY_LOCATION == BEFORE_TEXT_END)
1423 {
1424 /* Convex Unix prohibits executing in the stack segment. */
1425 /* Hope there is empty room at the top of the text segment. */
1426 extern CORE_ADDR text_end;
392a587b 1427 static int checked = 0;
7a292a7a
SS
1428 if (!checked)
1429 for (start_sp = text_end - sizeof_dummy1; start_sp < text_end; ++start_sp)
1430 if (read_memory_integer (start_sp, 1) != 0)
1431 error ("text segment full -- no place to put call");
1432 checked = 1;
1433 sp = old_sp;
1434 real_pc = text_end - sizeof_dummy1;
c5aa993b 1435 write_memory (real_pc, (char *) dummy1, sizeof_dummy1);
7a292a7a 1436 }
c5aa993b 1437
7a292a7a
SS
1438 if (CALL_DUMMY_LOCATION == AFTER_TEXT_END)
1439 {
1440 extern CORE_ADDR text_end;
1441 int errcode;
1442 sp = old_sp;
1443 real_pc = text_end;
c5aa993b 1444 errcode = target_write_memory (real_pc, (char *) dummy1, sizeof_dummy1);
7a292a7a
SS
1445 if (errcode != 0)
1446 error ("Cannot write text segment -- call_function failed");
1447 }
c906108c 1448
7a292a7a
SS
1449 if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
1450 {
1451 real_pc = funaddr;
1452 }
c906108c
SS
1453
1454#ifdef lint
c5aa993b 1455 sp = old_sp; /* It really is used, for some ifdef's... */
c906108c
SS
1456#endif
1457
1458 if (nargs < TYPE_NFIELDS (ftype))
1459 error ("too few arguments in function call");
1460
1461 for (i = nargs - 1; i >= 0; i--)
1462 {
1463 /* If we're off the end of the known arguments, do the standard
7b83ea04
AC
1464 promotions. FIXME: if we had a prototype, this should only
1465 be allowed if ... were present. */
c906108c
SS
1466 if (i >= TYPE_NFIELDS (ftype))
1467 args[i] = value_arg_coerce (args[i], NULL, 0);
1468
c5aa993b 1469 else
c906108c
SS
1470 {
1471 int is_prototyped = TYPE_FLAGS (ftype) & TYPE_FLAG_PROTOTYPED;
1472 param_type = TYPE_FIELD_TYPE (ftype, i);
1473
1474 args[i] = value_arg_coerce (args[i], param_type, is_prototyped);
1475 }
1476
070ad9f0
DB
1477 /*elz: this code is to handle the case in which the function to be called
1478 has a pointer to function as parameter and the corresponding actual argument
7b83ea04
AC
1479 is the address of a function and not a pointer to function variable.
1480 In aCC compiled code, the calls through pointers to functions (in the body
1481 of the function called by hand) are made via $$dyncall_external which
070ad9f0
DB
1482 requires some registers setting, this is taken care of if we call
1483 via a function pointer variable, but not via a function address.
7b83ea04 1484 In cc this is not a problem. */
c906108c
SS
1485
1486 if (using_gcc == 0)
1487 if (param_type)
c5aa993b 1488 /* if this parameter is a pointer to function */
c906108c
SS
1489 if (TYPE_CODE (param_type) == TYPE_CODE_PTR)
1490 if (TYPE_CODE (param_type->target_type) == TYPE_CODE_FUNC)
070ad9f0 1491 /* elz: FIXME here should go the test about the compiler used
7b83ea04 1492 to compile the target. We want to issue the error
070ad9f0
DB
1493 message only if the compiler used was HP's aCC.
1494 If we used HP's cc, then there is no problem and no need
7b83ea04 1495 to return at this point */
c5aa993b 1496 if (using_gcc == 0) /* && compiler == aCC */
c906108c 1497 /* go see if the actual parameter is a variable of type
c5aa993b 1498 pointer to function or just a function */
c906108c
SS
1499 if (args[i]->lval == not_lval)
1500 {
1501 char *arg_name;
c5aa993b
JM
1502 if (find_pc_partial_function ((CORE_ADDR) args[i]->aligner.contents[0], &arg_name, NULL, NULL))
1503 error ("\
c906108c
SS
1504You cannot use function <%s> as argument. \n\
1505You must use a pointer to function type variable. Command ignored.", arg_name);
c5aa993b 1506 }
c906108c
SS
1507 }
1508
d03e67c9
AC
1509 if (REG_STRUCT_HAS_ADDR_P ())
1510 {
1511 /* This is a machine like the sparc, where we may need to pass a
1512 pointer to the structure, not the structure itself. */
1513 for (i = nargs - 1; i >= 0; i--)
1514 {
1515 struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
1516 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
1517 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
1518 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1519 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
1520 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
1521 || TYPE_CODE (arg_type) == TYPE_CODE_SET
1522 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
1523 && TYPE_LENGTH (arg_type) > 8)
1524 )
1525 && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
1526 {
1527 CORE_ADDR addr;
1528 int len; /* = TYPE_LENGTH (arg_type); */
1529 int aligned_len;
1530 arg_type = check_typedef (VALUE_ENCLOSING_TYPE (args[i]));
1531 len = TYPE_LENGTH (arg_type);
1532
1533 if (STACK_ALIGN_P ())
1534 /* MVS 11/22/96: I think at least some of this
1535 stack_align code is really broken. Better to let
1536 PUSH_ARGUMENTS adjust the stack in a target-defined
1537 manner. */
1538 aligned_len = STACK_ALIGN (len);
1539 else
1540 aligned_len = len;
1541 if (INNER_THAN (1, 2))
1542 {
1543 /* stack grows downward */
1544 sp -= aligned_len;
0b3f98d3
AC
1545 /* ... so the address of the thing we push is the
1546 stack pointer after we push it. */
1547 addr = sp;
d03e67c9
AC
1548 }
1549 else
1550 {
1551 /* The stack grows up, so the address of the thing
1552 we push is the stack pointer before we push it. */
1553 addr = sp;
d03e67c9
AC
1554 sp += aligned_len;
1555 }
0b3f98d3
AC
1556 /* Push the structure. */
1557 write_memory (addr, VALUE_CONTENTS_ALL (args[i]), len);
d03e67c9
AC
1558 /* The value we're going to pass is the address of the
1559 thing we just pushed. */
1560 /*args[i] = value_from_longest (lookup_pointer_type (value_type),
1561 (LONGEST) addr); */
1562 args[i] = value_from_pointer (lookup_pointer_type (arg_type),
1563 addr);
1564 }
1565 }
1566 }
1567
c906108c
SS
1568
1569 /* Reserve space for the return structure to be written on the
1570 stack, if necessary */
1571
1572 if (struct_return)
1573 {
1574 int len = TYPE_LENGTH (value_type);
2ada493a
AC
1575 if (STACK_ALIGN_P ())
1576 /* MVS 11/22/96: I think at least some of this stack_align
1577 code is really broken. Better to let PUSH_ARGUMENTS adjust
1578 the stack in a target-defined manner. */
1579 len = STACK_ALIGN (len);
c906108c
SS
1580 if (INNER_THAN (1, 2))
1581 {
1582 /* stack grows downward */
1583 sp -= len;
1584 struct_addr = sp;
1585 }
1586 else
1587 {
1588 /* stack grows upward */
1589 struct_addr = sp;
1590 sp += len;
1591 }
1592 }
1593
0a49d05e
AC
1594 /* elz: on HPPA no need for this extra alignment, maybe it is needed
1595 on other architectures. This is because all the alignment is
1596 taken care of in the above code (ifdef REG_STRUCT_HAS_ADDR) and
1597 in hppa_push_arguments */
1598 if (EXTRA_STACK_ALIGNMENT_NEEDED)
c906108c 1599 {
0a49d05e
AC
1600 /* MVS 11/22/96: I think at least some of this stack_align code
1601 is really broken. Better to let PUSH_ARGUMENTS adjust the
1602 stack in a target-defined manner. */
1603 if (STACK_ALIGN_P () && INNER_THAN (1, 2))
1604 {
1605 /* If stack grows down, we must leave a hole at the top. */
1606 int len = 0;
1607
1608 for (i = nargs - 1; i >= 0; i--)
1609 len += TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
1610 if (CALL_DUMMY_STACK_ADJUST_P)
1611 len += CALL_DUMMY_STACK_ADJUST;
1612 sp -= STACK_ALIGN (len) - len;
1613 }
c906108c 1614 }
c906108c 1615
392a587b 1616 sp = PUSH_ARGUMENTS (nargs, args, sp, struct_return, struct_addr);
c906108c
SS
1617
1618#ifdef PUSH_RETURN_ADDRESS /* for targets that use no CALL_DUMMY */
1619 /* There are a number of targets now which actually don't write any
1620 CALL_DUMMY instructions into the target, but instead just save the
1621 machine state, push the arguments, and jump directly to the callee
1622 function. Since this doesn't actually involve executing a JSR/BSR
1623 instruction, the return address must be set up by hand, either by
1624 pushing onto the stack or copying into a return-address register
070ad9f0 1625 as appropriate. Formerly this has been done in PUSH_ARGUMENTS,
c906108c
SS
1626 but that's overloading its functionality a bit, so I'm making it
1627 explicit to do it here. */
c5aa993b
JM
1628 sp = PUSH_RETURN_ADDRESS (real_pc, sp);
1629#endif /* PUSH_RETURN_ADDRESS */
c906108c 1630
2ada493a 1631 if (STACK_ALIGN_P () && !INNER_THAN (1, 2))
c906108c
SS
1632 {
1633 /* If stack grows up, we must leave a hole at the bottom, note
7b83ea04 1634 that sp already has been advanced for the arguments! */
7a292a7a
SS
1635 if (CALL_DUMMY_STACK_ADJUST_P)
1636 sp += CALL_DUMMY_STACK_ADJUST;
c906108c
SS
1637 sp = STACK_ALIGN (sp);
1638 }
c906108c
SS
1639
1640/* XXX This seems wrong. For stacks that grow down we shouldn't do
1641 anything here! */
1642 /* MVS 11/22/96: I think at least some of this stack_align code is
1643 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1644 a target-defined manner. */
7a292a7a
SS
1645 if (CALL_DUMMY_STACK_ADJUST_P)
1646 if (INNER_THAN (1, 2))
1647 {
1648 /* stack grows downward */
1649 sp -= CALL_DUMMY_STACK_ADJUST;
1650 }
c906108c
SS
1651
1652 /* Store the address at which the structure is supposed to be
1653 written. Note that this (and the code which reserved the space
1654 above) assumes that gcc was used to compile this function. Since
1655 it doesn't cost us anything but space and if the function is pcc
1656 it will ignore this value, we will make that assumption.
1657
070ad9f0 1658 Also note that on some machines (like the sparc) pcc uses a
c906108c
SS
1659 convention like gcc's. */
1660
1661 if (struct_return)
1662 STORE_STRUCT_RETURN (struct_addr, sp);
1663
1664 /* Write the stack pointer. This is here because the statements above
1665 might fool with it. On SPARC, this write also stores the register
1666 window into the right place in the new stack frame, which otherwise
1667 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1668 write_sp (sp);
1669
d1e3cf49
AC
1670 if (SAVE_DUMMY_FRAME_TOS_P ())
1671 SAVE_DUMMY_FRAME_TOS (sp);
43ff13b4 1672
c906108c 1673 {
e6cbd02a 1674 char *retbuf = (char*) alloca (REGISTER_BYTES);
c906108c
SS
1675 char *name;
1676 struct symbol *symbol;
1677
1678 name = NULL;
1679 symbol = find_pc_function (funaddr);
1680 if (symbol)
1681 {
1682 name = SYMBOL_SOURCE_NAME (symbol);
1683 }
1684 else
1685 {
1686 /* Try the minimal symbols. */
1687 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1688
1689 if (msymbol)
1690 {
1691 name = SYMBOL_SOURCE_NAME (msymbol);
1692 }
1693 }
1694 if (name == NULL)
1695 {
1696 char format[80];
1697 sprintf (format, "at %s", local_hex_format ());
1698 name = alloca (80);
1699 /* FIXME-32x64: assumes funaddr fits in a long. */
1700 sprintf (name, format, (unsigned long) funaddr);
1701 }
1702
1703 /* Execute the stack dummy routine, calling FUNCTION.
1704 When it is done, discard the empty frame
1705 after storing the contents of all regs into retbuf. */
da59e081
JM
1706 rc = run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf);
1707
1708 if (rc == 1)
1709 {
1710 /* We stopped inside the FUNCTION because of a random signal.
1711 Further execution of the FUNCTION is not allowed. */
1712
7b83ea04 1713 if (unwind_on_signal_p)
242bfc55
FN
1714 {
1715 /* The user wants the context restored. */
da59e081 1716
7b83ea04
AC
1717 /* We must get back to the frame we were before the dummy call. */
1718 POP_FRAME;
242bfc55
FN
1719
1720 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1721 a C++ name with arguments and stuff. */
1722 error ("\
1723The program being debugged was signaled while in a function called from GDB.\n\
1724GDB has restored the context to what it was before the call.\n\
1725To change this behavior use \"set unwindonsignal off\"\n\
da59e081 1726Evaluation of the expression containing the function (%s) will be abandoned.",
242bfc55
FN
1727 name);
1728 }
1729 else
1730 {
1731 /* The user wants to stay in the frame where we stopped (default).*/
1732
1733 /* If we did the cleanups, we would print a spurious error
1734 message (Unable to restore previously selected frame),
1735 would write the registers from the inf_status (which is
1736 wrong), and would do other wrong things. */
1737 discard_cleanups (old_chain);
1738 discard_inferior_status (inf_status);
1739
1740 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1741 a C++ name with arguments and stuff. */
1742 error ("\
1743The program being debugged was signaled while in a function called from GDB.\n\
1744GDB remains in the frame where the signal was received.\n\
1745To change this behavior use \"set unwindonsignal on\"\n\
1746Evaluation of the expression containing the function (%s) will be abandoned.",
1747 name);
1748 }
da59e081
JM
1749 }
1750
1751 if (rc == 2)
c906108c 1752 {
da59e081 1753 /* We hit a breakpoint inside the FUNCTION. */
c906108c 1754
7a292a7a
SS
1755 /* If we did the cleanups, we would print a spurious error
1756 message (Unable to restore previously selected frame),
1757 would write the registers from the inf_status (which is
1758 wrong), and would do other wrong things. */
c906108c 1759 discard_cleanups (old_chain);
7a292a7a 1760 discard_inferior_status (inf_status);
c906108c
SS
1761
1762 /* The following error message used to say "The expression
1763 which contained the function call has been discarded." It
1764 is a hard concept to explain in a few words. Ideally, GDB
1765 would be able to resume evaluation of the expression when
1766 the function finally is done executing. Perhaps someday
1767 this will be implemented (it would not be easy). */
1768
1769 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1770 a C++ name with arguments and stuff. */
1771 error ("\
1772The program being debugged stopped while in a function called from GDB.\n\
1773When the function (%s) is done executing, GDB will silently\n\
1774stop (instead of continuing to evaluate the expression containing\n\
1775the function call).", name);
1776 }
1777
da59e081 1778 /* If we get here the called FUNCTION run to completion. */
c906108c
SS
1779 do_cleanups (old_chain);
1780
1781 /* Figure out the value returned by the function. */
1782/* elz: I defined this new macro for the hppa architecture only.
1783 this gives us a way to get the value returned by the function from the stack,
1784 at the same address we told the function to put it.
1785 We cannot assume on the pa that r28 still contains the address of the returned
1786 structure. Usually this will be overwritten by the callee.
1787 I don't know about other architectures, so I defined this macro
c5aa993b 1788 */
c906108c
SS
1789
1790#ifdef VALUE_RETURNED_FROM_STACK
1791 if (struct_return)
1792 return (value_ptr) VALUE_RETURNED_FROM_STACK (value_type, struct_addr);
1793#endif
1794
1795 return value_being_returned (value_type, retbuf, struct_return);
1796 }
1797}
7a292a7a 1798
c906108c 1799value_ptr
fba45db2 1800call_function_by_hand (value_ptr function, int nargs, value_ptr *args)
c906108c 1801{
7a292a7a
SS
1802 if (CALL_DUMMY_P)
1803 {
1804 return hand_function_call (function, nargs, args);
1805 }
1806 else
1807 {
1808 error ("Cannot invoke functions on this machine.");
1809 }
c906108c 1810}
c5aa993b 1811\f
7a292a7a 1812
c906108c 1813
c906108c
SS
1814/* Create a value for an array by allocating space in the inferior, copying
1815 the data into that space, and then setting up an array value.
1816
1817 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1818 populated from the values passed in ELEMVEC.
1819
1820 The element type of the array is inherited from the type of the
1821 first element, and all elements must have the same size (though we
1822 don't currently enforce any restriction on their types). */
1823
1824value_ptr
fba45db2 1825value_array (int lowbound, int highbound, value_ptr *elemvec)
c906108c
SS
1826{
1827 int nelem;
1828 int idx;
1829 unsigned int typelength;
1830 value_ptr val;
1831 struct type *rangetype;
1832 struct type *arraytype;
1833 CORE_ADDR addr;
1834
1835 /* Validate that the bounds are reasonable and that each of the elements
1836 have the same size. */
1837
1838 nelem = highbound - lowbound + 1;
1839 if (nelem <= 0)
1840 {
1841 error ("bad array bounds (%d, %d)", lowbound, highbound);
1842 }
1843 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1844 for (idx = 1; idx < nelem; idx++)
1845 {
1846 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1847 {
1848 error ("array elements must all be the same size");
1849 }
1850 }
1851
1852 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1853 lowbound, highbound);
c5aa993b
JM
1854 arraytype = create_array_type ((struct type *) NULL,
1855 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
c906108c
SS
1856
1857 if (!current_language->c_style_arrays)
1858 {
1859 val = allocate_value (arraytype);
1860 for (idx = 0; idx < nelem; idx++)
1861 {
1862 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1863 VALUE_CONTENTS_ALL (elemvec[idx]),
1864 typelength);
1865 }
1866 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1867 return val;
1868 }
1869
1870 /* Allocate space to store the array in the inferior, and then initialize
1871 it by copying in each element. FIXME: Is it worth it to create a
1872 local buffer in which to collect each value and then write all the
1873 bytes in one operation? */
1874
1875 addr = allocate_space_in_inferior (nelem * typelength);
1876 for (idx = 0; idx < nelem; idx++)
1877 {
1878 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1879 typelength);
1880 }
1881
1882 /* Create the array type and set up an array value to be evaluated lazily. */
1883
1884 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1885 return (val);
1886}
1887
1888/* Create a value for a string constant by allocating space in the inferior,
1889 copying the data into that space, and returning the address with type
1890 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1891 of characters.
1892 Note that string types are like array of char types with a lower bound of
1893 zero and an upper bound of LEN - 1. Also note that the string may contain
1894 embedded null bytes. */
1895
1896value_ptr
fba45db2 1897value_string (char *ptr, int len)
c906108c
SS
1898{
1899 value_ptr val;
1900 int lowbound = current_language->string_lower_bound;
1901 struct type *rangetype = create_range_type ((struct type *) NULL,
1902 builtin_type_int,
1903 lowbound, len + lowbound - 1);
1904 struct type *stringtype
c5aa993b 1905 = create_string_type ((struct type *) NULL, rangetype);
c906108c
SS
1906 CORE_ADDR addr;
1907
1908 if (current_language->c_style_arrays == 0)
1909 {
1910 val = allocate_value (stringtype);
1911 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1912 return val;
1913 }
1914
1915
1916 /* Allocate space to store the string in the inferior, and then
1917 copy LEN bytes from PTR in gdb to that address in the inferior. */
1918
1919 addr = allocate_space_in_inferior (len);
1920 write_memory (addr, ptr, len);
1921
1922 val = value_at_lazy (stringtype, addr, NULL);
1923 return (val);
1924}
1925
1926value_ptr
fba45db2 1927value_bitstring (char *ptr, int len)
c906108c
SS
1928{
1929 value_ptr val;
1930 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1931 0, len - 1);
c5aa993b 1932 struct type *type = create_set_type ((struct type *) NULL, domain_type);
c906108c
SS
1933 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1934 val = allocate_value (type);
1935 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1936 return val;
1937}
1938\f
1939/* See if we can pass arguments in T2 to a function which takes arguments
1940 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1941 arguments need coercion of some sort, then the coerced values are written
1942 into T2. Return value is 0 if the arguments could be matched, or the
1943 position at which they differ if not.
1944
1945 STATICP is nonzero if the T1 argument list came from a
1946 static member function.
1947
1948 For non-static member functions, we ignore the first argument,
1949 which is the type of the instance variable. This is because we want
1950 to handle calls with objects from derived classes. This is not
1951 entirely correct: we should actually check to make sure that a
1952 requested operation is type secure, shouldn't we? FIXME. */
1953
1954static int
ed37ab20 1955typecmp (int staticp, struct type *t1[], value_ptr t2[])
c906108c
SS
1956{
1957 int i;
1958
1959 if (t2 == 0)
1960 return 1;
1961 if (staticp && t1 == 0)
1962 return t2[1] != 0;
1963 if (t1 == 0)
1964 return 1;
c5aa993b
JM
1965 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID)
1966 return 0;
1967 if (t1[!staticp] == 0)
1968 return 0;
c906108c
SS
1969 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1970 {
c5aa993b
JM
1971 struct type *tt1, *tt2;
1972 if (!t2[i])
1973 return i + 1;
c906108c 1974 tt1 = check_typedef (t1[i]);
c5aa993b 1975 tt2 = check_typedef (VALUE_TYPE (t2[i]));
c906108c 1976 if (TYPE_CODE (tt1) == TYPE_CODE_REF
c5aa993b 1977 /* We should be doing hairy argument matching, as below. */
c906108c
SS
1978 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1979 {
1980 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1981 t2[i] = value_coerce_array (t2[i]);
1982 else
1983 t2[i] = value_addr (t2[i]);
1984 continue;
1985 }
1986
802db21b
DB
1987 /* djb - 20000715 - Until the new type structure is in the
1988 place, and we can attempt things like implicit conversions,
1989 we need to do this so you can take something like a map<const
1990 char *>, and properly access map["hello"], because the
1991 argument to [] will be a reference to a pointer to a char,
7168a814 1992 and the argument will be a pointer to a char. */
802db21b
DB
1993 while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1994 TYPE_CODE (tt1) == TYPE_CODE_PTR)
1995 {
1996 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1997 }
1998 while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1999 TYPE_CODE(tt2) == TYPE_CODE_PTR ||
2000 TYPE_CODE(tt2) == TYPE_CODE_REF)
c906108c 2001 {
802db21b 2002 tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
c906108c 2003 }
c5aa993b
JM
2004 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
2005 continue;
c906108c
SS
2006 /* Array to pointer is a `trivial conversion' according to the ARM. */
2007
2008 /* We should be doing much hairier argument matching (see section 13.2
7b83ea04
AC
2009 of the ARM), but as a quick kludge, just check for the same type
2010 code. */
c906108c 2011 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
c5aa993b 2012 return i + 1;
c906108c 2013 }
c5aa993b
JM
2014 if (!t1[i])
2015 return 0;
2016 return t2[i] ? i + 1 : 0;
c906108c
SS
2017}
2018
2019/* Helper function used by value_struct_elt to recurse through baseclasses.
2020 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2021 and search in it assuming it has (class) type TYPE.
2022 If found, return value, else return NULL.
2023
2024 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
2025 look for a baseclass named NAME. */
2026
2027static value_ptr
fba45db2
KB
2028search_struct_field (char *name, register value_ptr arg1, int offset,
2029 register struct type *type, int looking_for_baseclass)
c906108c
SS
2030{
2031 int i;
2032 int nbases = TYPE_N_BASECLASSES (type);
2033
2034 CHECK_TYPEDEF (type);
2035
c5aa993b 2036 if (!looking_for_baseclass)
c906108c
SS
2037 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
2038 {
2039 char *t_field_name = TYPE_FIELD_NAME (type, i);
2040
db577aea 2041 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2042 {
2043 value_ptr v;
2044 if (TYPE_FIELD_STATIC (type, i))
2045 v = value_static_field (type, i);
2046 else
2047 v = value_primitive_field (arg1, offset, i, type);
2048 if (v == 0)
c5aa993b 2049 error ("there is no field named %s", name);
c906108c
SS
2050 return v;
2051 }
2052
2053 if (t_field_name
2054 && (t_field_name[0] == '\0'
2055 || (TYPE_CODE (type) == TYPE_CODE_UNION
db577aea 2056 && (strcmp_iw (t_field_name, "else") == 0))))
c906108c
SS
2057 {
2058 struct type *field_type = TYPE_FIELD_TYPE (type, i);
2059 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
2060 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
2061 {
2062 /* Look for a match through the fields of an anonymous union,
2063 or anonymous struct. C++ provides anonymous unions.
2064
2065 In the GNU Chill implementation of variant record types,
2066 each <alternative field> has an (anonymous) union type,
2067 each member of the union represents a <variant alternative>.
2068 Each <variant alternative> is represented as a struct,
2069 with a member for each <variant field>. */
c5aa993b 2070
c906108c
SS
2071 value_ptr v;
2072 int new_offset = offset;
2073
2074 /* This is pretty gross. In G++, the offset in an anonymous
2075 union is relative to the beginning of the enclosing struct.
2076 In the GNU Chill implementation of variant records,
2077 the bitpos is zero in an anonymous union field, so we
2078 have to add the offset of the union here. */
2079 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
2080 || (TYPE_NFIELDS (field_type) > 0
2081 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
2082 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
2083
2084 v = search_struct_field (name, arg1, new_offset, field_type,
2085 looking_for_baseclass);
2086 if (v)
2087 return v;
2088 }
2089 }
2090 }
2091
c5aa993b 2092 for (i = 0; i < nbases; i++)
c906108c
SS
2093 {
2094 value_ptr v;
2095 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
2096 /* If we are looking for baseclasses, this is what we get when we
7b83ea04
AC
2097 hit them. But it could happen that the base part's member name
2098 is not yet filled in. */
c906108c
SS
2099 int found_baseclass = (looking_for_baseclass
2100 && TYPE_BASECLASS_NAME (type, i) != NULL
db577aea 2101 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
c906108c
SS
2102
2103 if (BASETYPE_VIA_VIRTUAL (type, i))
2104 {
2105 int boffset;
2106 value_ptr v2 = allocate_value (basetype);
2107
2108 boffset = baseclass_offset (type, i,
2109 VALUE_CONTENTS (arg1) + offset,
2110 VALUE_ADDRESS (arg1)
c5aa993b 2111 + VALUE_OFFSET (arg1) + offset);
c906108c
SS
2112 if (boffset == -1)
2113 error ("virtual baseclass botch");
2114
2115 /* The virtual base class pointer might have been clobbered by the
2116 user program. Make sure that it still points to a valid memory
2117 location. */
2118
2119 boffset += offset;
2120 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
2121 {
2122 CORE_ADDR base_addr;
c5aa993b 2123
c906108c
SS
2124 base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
2125 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
2126 TYPE_LENGTH (basetype)) != 0)
2127 error ("virtual baseclass botch");
2128 VALUE_LVAL (v2) = lval_memory;
2129 VALUE_ADDRESS (v2) = base_addr;
2130 }
2131 else
2132 {
2133 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
2134 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
2135 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
2136 if (VALUE_LAZY (arg1))
2137 VALUE_LAZY (v2) = 1;
2138 else
2139 memcpy (VALUE_CONTENTS_RAW (v2),
2140 VALUE_CONTENTS_RAW (arg1) + boffset,
2141 TYPE_LENGTH (basetype));
2142 }
2143
2144 if (found_baseclass)
2145 return v2;
2146 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
2147 looking_for_baseclass);
2148 }
2149 else if (found_baseclass)
2150 v = value_primitive_field (arg1, offset, i, type);
2151 else
2152 v = search_struct_field (name, arg1,
c5aa993b 2153 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
c906108c 2154 basetype, looking_for_baseclass);
c5aa993b
JM
2155 if (v)
2156 return v;
c906108c
SS
2157 }
2158 return NULL;
2159}
2160
2161
2162/* Return the offset (in bytes) of the virtual base of type BASETYPE
2163 * in an object pointed to by VALADDR (on the host), assumed to be of
2164 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
2165 * looking (in case VALADDR is the contents of an enclosing object).
2166 *
2167 * This routine recurses on the primary base of the derived class because
2168 * the virtual base entries of the primary base appear before the other
2169 * virtual base entries.
2170 *
2171 * If the virtual base is not found, a negative integer is returned.
2172 * The magnitude of the negative integer is the number of entries in
2173 * the virtual table to skip over (entries corresponding to various
2174 * ancestral classes in the chain of primary bases).
2175 *
2176 * Important: This assumes the HP / Taligent C++ runtime
2177 * conventions. Use baseclass_offset() instead to deal with g++
2178 * conventions. */
2179
2180void
fba45db2
KB
2181find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
2182 int offset, int *boffset_p, int *skip_p)
c906108c 2183{
c5aa993b
JM
2184 int boffset; /* offset of virtual base */
2185 int index; /* displacement to use in virtual table */
c906108c 2186 int skip;
c5aa993b
JM
2187
2188 value_ptr vp;
2189 CORE_ADDR vtbl; /* the virtual table pointer */
2190 struct type *pbc; /* the primary base class */
c906108c
SS
2191
2192 /* Look for the virtual base recursively in the primary base, first.
2193 * This is because the derived class object and its primary base
2194 * subobject share the primary virtual table. */
c5aa993b 2195
c906108c 2196 boffset = 0;
c5aa993b 2197 pbc = TYPE_PRIMARY_BASE (type);
c906108c
SS
2198 if (pbc)
2199 {
2200 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
2201 if (skip < 0)
c5aa993b
JM
2202 {
2203 *boffset_p = boffset;
2204 *skip_p = -1;
2205 return;
2206 }
c906108c
SS
2207 }
2208 else
2209 skip = 0;
2210
2211
2212 /* Find the index of the virtual base according to HP/Taligent
2213 runtime spec. (Depth-first, left-to-right.) */
2214 index = virtual_base_index_skip_primaries (basetype, type);
2215
c5aa993b
JM
2216 if (index < 0)
2217 {
2218 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
2219 *boffset_p = 0;
2220 return;
2221 }
c906108c 2222
c5aa993b 2223 /* pai: FIXME -- 32x64 possible problem */
c906108c 2224 /* First word (4 bytes) in object layout is the vtable pointer */
c5aa993b 2225 vtbl = *(CORE_ADDR *) (valaddr + offset);
c906108c 2226
c5aa993b 2227 /* Before the constructor is invoked, things are usually zero'd out. */
c906108c
SS
2228 if (vtbl == 0)
2229 error ("Couldn't find virtual table -- object may not be constructed yet.");
2230
2231
2232 /* Find virtual base's offset -- jump over entries for primary base
2233 * ancestors, then use the index computed above. But also adjust by
2234 * HP_ACC_VBASE_START for the vtable slots before the start of the
2235 * virtual base entries. Offset is negative -- virtual base entries
2236 * appear _before_ the address point of the virtual table. */
c5aa993b 2237
070ad9f0 2238 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
c5aa993b 2239 & use long type */
c906108c
SS
2240
2241 /* epstein : FIXME -- added param for overlay section. May not be correct */
c5aa993b 2242 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
c906108c
SS
2243 boffset = value_as_long (vp);
2244 *skip_p = -1;
2245 *boffset_p = boffset;
2246 return;
2247}
2248
2249
2250/* Helper function used by value_struct_elt to recurse through baseclasses.
2251 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2252 and search in it assuming it has (class) type TYPE.
2253 If found, return value, else if name matched and args not return (value)-1,
2254 else return NULL. */
2255
2256static value_ptr
fba45db2
KB
2257search_struct_method (char *name, register value_ptr *arg1p,
2258 register value_ptr *args, int offset,
2259 int *static_memfuncp, register struct type *type)
c906108c
SS
2260{
2261 int i;
2262 value_ptr v;
2263 int name_matched = 0;
2264 char dem_opname[64];
2265
2266 CHECK_TYPEDEF (type);
2267 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2268 {
2269 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2270 /* FIXME! May need to check for ARM demangling here */
c5aa993b
JM
2271 if (strncmp (t_field_name, "__", 2) == 0 ||
2272 strncmp (t_field_name, "op", 2) == 0 ||
2273 strncmp (t_field_name, "type", 4) == 0)
c906108c 2274 {
c5aa993b
JM
2275 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2276 t_field_name = dem_opname;
2277 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 2278 t_field_name = dem_opname;
c906108c 2279 }
db577aea 2280 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2281 {
2282 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2283 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
c5aa993b 2284 name_matched = 1;
c906108c
SS
2285
2286 if (j > 0 && args == 0)
2287 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
2288 while (j >= 0)
2289 {
2290 if (TYPE_FN_FIELD_STUB (f, j))
2291 check_stub_method (type, i, j);
2292 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
2293 TYPE_FN_FIELD_ARGS (f, j), args))
2294 {
2295 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2296 return value_virtual_fn_field (arg1p, f, j, type, offset);
2297 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
2298 *static_memfuncp = 1;
2299 v = value_fn_field (arg1p, f, j, type, offset);
c5aa993b 2300 if (v != NULL)
7168a814 2301 return v;
c906108c
SS
2302 }
2303 j--;
2304 }
2305 }
2306 }
2307
2308 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2309 {
2310 int base_offset;
2311
2312 if (BASETYPE_VIA_VIRTUAL (type, i))
2313 {
c5aa993b
JM
2314 if (TYPE_HAS_VTABLE (type))
2315 {
2316 /* HP aCC compiled type, search for virtual base offset
7b83ea04 2317 according to HP/Taligent runtime spec. */
c5aa993b
JM
2318 int skip;
2319 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
2320 VALUE_CONTENTS_ALL (*arg1p),
2321 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
2322 &base_offset, &skip);
2323 if (skip >= 0)
2324 error ("Virtual base class offset not found in vtable");
2325 }
2326 else
2327 {
2328 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2329 char *base_valaddr;
2330
2331 /* The virtual base class pointer might have been clobbered by the
7b83ea04
AC
2332 user program. Make sure that it still points to a valid memory
2333 location. */
c5aa993b
JM
2334
2335 if (offset < 0 || offset >= TYPE_LENGTH (type))
2336 {
2337 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
2338 if (target_read_memory (VALUE_ADDRESS (*arg1p)
2339 + VALUE_OFFSET (*arg1p) + offset,
2340 base_valaddr,
2341 TYPE_LENGTH (baseclass)) != 0)
2342 error ("virtual baseclass botch");
2343 }
2344 else
2345 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
2346
2347 base_offset =
2348 baseclass_offset (type, i, base_valaddr,
2349 VALUE_ADDRESS (*arg1p)
2350 + VALUE_OFFSET (*arg1p) + offset);
2351 if (base_offset == -1)
2352 error ("virtual baseclass botch");
2353 }
2354 }
c906108c
SS
2355 else
2356 {
2357 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2358 }
c906108c
SS
2359 v = search_struct_method (name, arg1p, args, base_offset + offset,
2360 static_memfuncp, TYPE_BASECLASS (type, i));
c5aa993b 2361 if (v == (value_ptr) - 1)
c906108c
SS
2362 {
2363 name_matched = 1;
2364 }
2365 else if (v)
2366 {
2367/* FIXME-bothner: Why is this commented out? Why is it here? */
c5aa993b 2368/* *arg1p = arg1_tmp; */
c906108c 2369 return v;
c5aa993b 2370 }
c906108c 2371 }
c5aa993b
JM
2372 if (name_matched)
2373 return (value_ptr) - 1;
2374 else
2375 return NULL;
c906108c
SS
2376}
2377
2378/* Given *ARGP, a value of type (pointer to a)* structure/union,
2379 extract the component named NAME from the ultimate target structure/union
2380 and return it as a value with its appropriate type.
2381 ERR is used in the error message if *ARGP's type is wrong.
2382
2383 C++: ARGS is a list of argument types to aid in the selection of
2384 an appropriate method. Also, handle derived types.
2385
2386 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2387 where the truthvalue of whether the function that was resolved was
2388 a static member function or not is stored.
2389
2390 ERR is an error message to be printed in case the field is not found. */
2391
2392value_ptr
fba45db2
KB
2393value_struct_elt (register value_ptr *argp, register value_ptr *args,
2394 char *name, int *static_memfuncp, char *err)
c906108c
SS
2395{
2396 register struct type *t;
2397 value_ptr v;
2398
2399 COERCE_ARRAY (*argp);
2400
2401 t = check_typedef (VALUE_TYPE (*argp));
2402
2403 /* Follow pointers until we get to a non-pointer. */
2404
2405 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
2406 {
2407 *argp = value_ind (*argp);
2408 /* Don't coerce fn pointer to fn and then back again! */
2409 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
2410 COERCE_ARRAY (*argp);
2411 t = check_typedef (VALUE_TYPE (*argp));
2412 }
2413
2414 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2415 error ("not implemented: member type in value_struct_elt");
2416
c5aa993b 2417 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
2418 && TYPE_CODE (t) != TYPE_CODE_UNION)
2419 error ("Attempt to extract a component of a value that is not a %s.", err);
2420
2421 /* Assume it's not, unless we see that it is. */
2422 if (static_memfuncp)
c5aa993b 2423 *static_memfuncp = 0;
c906108c
SS
2424
2425 if (!args)
2426 {
2427 /* if there are no arguments ...do this... */
2428
2429 /* Try as a field first, because if we succeed, there
7b83ea04 2430 is less work to be done. */
c906108c
SS
2431 v = search_struct_field (name, *argp, 0, t, 0);
2432 if (v)
2433 return v;
2434
2435 /* C++: If it was not found as a data field, then try to
7b83ea04 2436 return it as a pointer to a method. */
c906108c
SS
2437
2438 if (destructor_name_p (name, t))
2439 error ("Cannot get value of destructor");
2440
2441 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
2442
c5aa993b 2443 if (v == (value_ptr) - 1)
c906108c
SS
2444 error ("Cannot take address of a method");
2445 else if (v == 0)
2446 {
2447 if (TYPE_NFN_FIELDS (t))
2448 error ("There is no member or method named %s.", name);
2449 else
2450 error ("There is no member named %s.", name);
2451 }
2452 return v;
2453 }
2454
2455 if (destructor_name_p (name, t))
2456 {
2457 if (!args[1])
2458 {
2459 /* Destructors are a special case. */
2460 int m_index, f_index;
2461
2462 v = NULL;
2463 if (get_destructor_fn_field (t, &m_index, &f_index))
2464 {
2465 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
2466 f_index, NULL, 0);
2467 }
2468 if (v == NULL)
2469 error ("could not find destructor function named %s.", name);
2470 else
2471 return v;
2472 }
2473 else
2474 {
2475 error ("destructor should not have any argument");
2476 }
2477 }
2478 else
2479 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
7168a814 2480
c5aa993b 2481 if (v == (value_ptr) - 1)
c906108c 2482 {
7168a814 2483 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
c906108c
SS
2484 }
2485 else if (v == 0)
2486 {
2487 /* See if user tried to invoke data as function. If so,
7b83ea04
AC
2488 hand it back. If it's not callable (i.e., a pointer to function),
2489 gdb should give an error. */
c906108c
SS
2490 v = search_struct_field (name, *argp, 0, t, 0);
2491 }
2492
2493 if (!v)
2494 error ("Structure has no component named %s.", name);
2495 return v;
2496}
2497
2498/* Search through the methods of an object (and its bases)
2499 * to find a specified method. Return the pointer to the
2500 * fn_field list of overloaded instances.
2501 * Helper function for value_find_oload_list.
2502 * ARGP is a pointer to a pointer to a value (the object)
2503 * METHOD is a string containing the method name
2504 * OFFSET is the offset within the value
2505 * STATIC_MEMFUNCP is set if the method is static
2506 * TYPE is the assumed type of the object
2507 * NUM_FNS is the number of overloaded instances
2508 * BASETYPE is set to the actual type of the subobject where the method is found
2509 * BOFFSET is the offset of the base subobject where the method is found */
2510
7a292a7a 2511static struct fn_field *
fba45db2
KB
2512find_method_list (value_ptr *argp, char *method, int offset,
2513 int *static_memfuncp, struct type *type, int *num_fns,
2514 struct type **basetype, int *boffset)
c906108c
SS
2515{
2516 int i;
c5aa993b 2517 struct fn_field *f;
c906108c
SS
2518 CHECK_TYPEDEF (type);
2519
2520 *num_fns = 0;
2521
c5aa993b
JM
2522 /* First check in object itself */
2523 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
c906108c
SS
2524 {
2525 /* pai: FIXME What about operators and type conversions? */
c5aa993b 2526 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
db577aea 2527 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
c5aa993b
JM
2528 {
2529 *num_fns = TYPE_FN_FIELDLIST_LENGTH (type, i);
2530 *basetype = type;
2531 *boffset = offset;
2532 return TYPE_FN_FIELDLIST1 (type, i);
2533 }
2534 }
2535
c906108c
SS
2536 /* Not found in object, check in base subobjects */
2537 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2538 {
2539 int base_offset;
2540 if (BASETYPE_VIA_VIRTUAL (type, i))
2541 {
c5aa993b
JM
2542 if (TYPE_HAS_VTABLE (type))
2543 {
2544 /* HP aCC compiled type, search for virtual base offset
2545 * according to HP/Taligent runtime spec. */
2546 int skip;
2547 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
2548 VALUE_CONTENTS_ALL (*argp),
2549 offset + VALUE_EMBEDDED_OFFSET (*argp),
2550 &base_offset, &skip);
2551 if (skip >= 0)
2552 error ("Virtual base class offset not found in vtable");
2553 }
2554 else
2555 {
2556 /* probably g++ runtime model */
2557 base_offset = VALUE_OFFSET (*argp) + offset;
2558 base_offset =
2559 baseclass_offset (type, i,
2560 VALUE_CONTENTS (*argp) + base_offset,
2561 VALUE_ADDRESS (*argp) + base_offset);
2562 if (base_offset == -1)
2563 error ("virtual baseclass botch");
2564 }
2565 }
2566 else
2567 /* non-virtual base, simply use bit position from debug info */
c906108c
SS
2568 {
2569 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2570 }
c906108c 2571 f = find_method_list (argp, method, base_offset + offset,
c5aa993b 2572 static_memfuncp, TYPE_BASECLASS (type, i), num_fns, basetype, boffset);
c906108c 2573 if (f)
c5aa993b 2574 return f;
c906108c 2575 }
c5aa993b 2576 return NULL;
c906108c
SS
2577}
2578
2579/* Return the list of overloaded methods of a specified name.
2580 * ARGP is a pointer to a pointer to a value (the object)
2581 * METHOD is the method name
2582 * OFFSET is the offset within the value contents
2583 * STATIC_MEMFUNCP is set if the method is static
2584 * NUM_FNS is the number of overloaded instances
2585 * BASETYPE is set to the type of the base subobject that defines the method
2586 * BOFFSET is the offset of the base subobject which defines the method */
2587
2588struct fn_field *
fba45db2
KB
2589value_find_oload_method_list (value_ptr *argp, char *method, int offset,
2590 int *static_memfuncp, int *num_fns,
2591 struct type **basetype, int *boffset)
c906108c 2592{
c5aa993b 2593 struct type *t;
c906108c
SS
2594
2595 t = check_typedef (VALUE_TYPE (*argp));
2596
c5aa993b 2597 /* code snarfed from value_struct_elt */
c906108c
SS
2598 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
2599 {
2600 *argp = value_ind (*argp);
2601 /* Don't coerce fn pointer to fn and then back again! */
2602 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
2603 COERCE_ARRAY (*argp);
2604 t = check_typedef (VALUE_TYPE (*argp));
2605 }
c5aa993b 2606
c906108c
SS
2607 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2608 error ("Not implemented: member type in value_find_oload_lis");
c5aa993b
JM
2609
2610 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2611 && TYPE_CODE (t) != TYPE_CODE_UNION)
c906108c 2612 error ("Attempt to extract a component of a value that is not a struct or union");
c5aa993b 2613
c906108c
SS
2614 /* Assume it's not static, unless we see that it is. */
2615 if (static_memfuncp)
c5aa993b 2616 *static_memfuncp = 0;
c906108c
SS
2617
2618 return find_method_list (argp, method, 0, static_memfuncp, t, num_fns, basetype, boffset);
c5aa993b 2619
c906108c
SS
2620}
2621
2622/* Given an array of argument types (ARGTYPES) (which includes an
2623 entry for "this" in the case of C++ methods), the number of
2624 arguments NARGS, the NAME of a function whether it's a method or
2625 not (METHOD), and the degree of laxness (LAX) in conforming to
2626 overload resolution rules in ANSI C++, find the best function that
2627 matches on the argument types according to the overload resolution
2628 rules.
2629
2630 In the case of class methods, the parameter OBJ is an object value
2631 in which to search for overloaded methods.
2632
2633 In the case of non-method functions, the parameter FSYM is a symbol
2634 corresponding to one of the overloaded functions.
2635
2636 Return value is an integer: 0 -> good match, 10 -> debugger applied
2637 non-standard coercions, 100 -> incompatible.
2638
2639 If a method is being searched for, VALP will hold the value.
2640 If a non-method is being searched for, SYMP will hold the symbol for it.
2641
2642 If a method is being searched for, and it is a static method,
2643 then STATICP will point to a non-zero value.
2644
2645 Note: This function does *not* check the value of
2646 overload_resolution. Caller must check it to see whether overload
2647 resolution is permitted.
c5aa993b 2648 */
c906108c
SS
2649
2650int
fba45db2
KB
2651find_overload_match (struct type **arg_types, int nargs, char *name, int method,
2652 int lax, value_ptr obj, struct symbol *fsym,
2653 value_ptr *valp, struct symbol **symp, int *staticp)
c906108c
SS
2654{
2655 int nparms;
c5aa993b 2656 struct type **parm_types;
c906108c 2657 int champ_nparms = 0;
c5aa993b
JM
2658
2659 short oload_champ = -1; /* Index of best overloaded function */
2660 short oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
2661 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2662 short oload_ambig_champ = -1; /* 2nd contender for best match */
2663 short oload_non_standard = 0; /* did we have to use non-standard conversions? */
2664 short oload_incompatible = 0; /* are args supplied incompatible with any function? */
2665
2666 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
2667 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
2668
c906108c 2669 value_ptr temp = obj;
c5aa993b
JM
2670 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
2671 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
2672 int num_fns = 0; /* Number of overloaded instances being considered */
2673 struct type *basetype = NULL;
c906108c
SS
2674 int boffset;
2675 register int jj;
2676 register int ix;
2677
c5aa993b
JM
2678 char *obj_type_name = NULL;
2679 char *func_name = NULL;
c906108c
SS
2680
2681 /* Get the list of overloaded methods or functions */
2682 if (method)
2683 {
db577aea
AC
2684 int i;
2685 int len;
2686 struct type *domain;
c906108c
SS
2687 obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
2688 /* Hack: evaluate_subexp_standard often passes in a pointer
7b83ea04 2689 value rather than the object itself, so try again */
c906108c 2690 if ((!obj_type_name || !*obj_type_name) &&
c5aa993b
JM
2691 (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
2692 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
c906108c
SS
2693
2694 fns_ptr = value_find_oload_method_list (&temp, name, 0,
c5aa993b
JM
2695 staticp,
2696 &num_fns,
2697 &basetype, &boffset);
c906108c 2698 if (!fns_ptr || !num_fns)
c5aa993b
JM
2699 error ("Couldn't find method %s%s%s",
2700 obj_type_name,
2701 (obj_type_name && *obj_type_name) ? "::" : "",
2702 name);
db577aea
AC
2703 domain = TYPE_DOMAIN_TYPE (fns_ptr[0].type);
2704 len = TYPE_NFN_FIELDS (domain);
2705 /* NOTE: dan/2000-03-10: This stuff is for STABS, which won't
2706 give us the info we need directly in the types. We have to
2707 use the method stub conversion to get it. Be aware that this
2708 is by no means perfect, and if you use STABS, please move to
2709 DWARF-2, or something like it, because trying to improve
2710 overloading using STABS is really a waste of time. */
2711 for (i = 0; i < len; i++)
2712 {
2713 int j;
2714 struct fn_field *f = TYPE_FN_FIELDLIST1 (domain, i);
2715 int len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
2716
2717 for (j = 0; j < len2; j++)
2718 {
070ad9f0 2719 if (TYPE_FN_FIELD_STUB (f, j) && (!strcmp_iw (TYPE_FN_FIELDLIST_NAME (domain,i),name)))
db577aea
AC
2720 check_stub_method (domain, i, j);
2721 }
2722 }
c906108c
SS
2723 }
2724 else
2725 {
2726 int i = -1;
2727 func_name = cplus_demangle (SYMBOL_NAME (fsym), DMGL_NO_OPTS);
2728
917317f4 2729 /* If the name is NULL this must be a C-style function.
7b83ea04 2730 Just return the same symbol. */
917317f4 2731 if (!func_name)
7b83ea04 2732 {
917317f4 2733 *symp = fsym;
7b83ea04
AC
2734 return 0;
2735 }
917317f4 2736
c906108c
SS
2737 oload_syms = make_symbol_overload_list (fsym);
2738 while (oload_syms[++i])
c5aa993b 2739 num_fns++;
c906108c 2740 if (!num_fns)
c5aa993b 2741 error ("Couldn't find function %s", func_name);
c906108c 2742 }
c5aa993b 2743
c906108c
SS
2744 oload_champ_bv = NULL;
2745
c5aa993b 2746 /* Consider each candidate in turn */
c906108c
SS
2747 for (ix = 0; ix < num_fns; ix++)
2748 {
db577aea
AC
2749 if (method)
2750 {
2751 /* For static member functions, we won't have a this pointer, but nothing
2752 else seems to handle them right now, so we just pretend ourselves */
2753 nparms=0;
2754
2755 if (TYPE_FN_FIELD_ARGS(fns_ptr,ix))
2756 {
2757 while (TYPE_CODE(TYPE_FN_FIELD_ARGS(fns_ptr,ix)[nparms]) != TYPE_CODE_VOID)
2758 nparms++;
2759 }
2760 }
2761 else
2762 {
2763 /* If it's not a method, this is the proper place */
2764 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2765 }
c906108c 2766
c5aa993b 2767 /* Prepare array of parameter types */
c906108c
SS
2768 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2769 for (jj = 0; jj < nparms; jj++)
db577aea
AC
2770 parm_types[jj] = (method
2771 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj])
2772 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
c906108c
SS
2773
2774 /* Compare parameter types to supplied argument types */
2775 bv = rank_function (parm_types, nparms, arg_types, nargs);
c5aa993b 2776
c906108c 2777 if (!oload_champ_bv)
c5aa993b
JM
2778 {
2779 oload_champ_bv = bv;
2780 oload_champ = 0;
2781 champ_nparms = nparms;
2782 }
c906108c 2783 else
c5aa993b
JM
2784 /* See whether current candidate is better or worse than previous best */
2785 switch (compare_badness (bv, oload_champ_bv))
2786 {
2787 case 0:
2788 oload_ambiguous = 1; /* top two contenders are equally good */
2789 oload_ambig_champ = ix;
2790 break;
2791 case 1:
2792 oload_ambiguous = 2; /* incomparable top contenders */
2793 oload_ambig_champ = ix;
2794 break;
2795 case 2:
2796 oload_champ_bv = bv; /* new champion, record details */
2797 oload_ambiguous = 0;
2798 oload_champ = ix;
2799 oload_ambig_champ = -1;
2800 champ_nparms = nparms;
2801 break;
2802 case 3:
2803 default:
2804 break;
2805 }
b8c9b27d 2806 xfree (parm_types);
6b1ba9a0
ND
2807 if (overload_debug)
2808 {
2809 if (method)
2810 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2811 else
2812 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2813 for (jj = 0; jj < nargs; jj++)
2814 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2815 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2816 }
c5aa993b 2817 } /* end loop over all candidates */
db577aea
AC
2818 /* NOTE: dan/2000-03-10: Seems to be a better idea to just pick one
2819 if they have the exact same goodness. This is because there is no
2820 way to differentiate based on return type, which we need to in
2821 cases like overloads of .begin() <It's both const and non-const> */
2822#if 0
c906108c
SS
2823 if (oload_ambiguous)
2824 {
2825 if (method)
c5aa993b
JM
2826 error ("Cannot resolve overloaded method %s%s%s to unique instance; disambiguate by specifying function signature",
2827 obj_type_name,
2828 (obj_type_name && *obj_type_name) ? "::" : "",
2829 name);
c906108c 2830 else
c5aa993b
JM
2831 error ("Cannot resolve overloaded function %s to unique instance; disambiguate by specifying function signature",
2832 func_name);
c906108c 2833 }
db577aea 2834#endif
c906108c 2835
c5aa993b 2836 /* Check how bad the best match is */
c906108c
SS
2837 for (ix = 1; ix <= nargs; ix++)
2838 {
6b1ba9a0
ND
2839 if (oload_champ_bv->rank[ix] >= 100)
2840 oload_incompatible = 1; /* truly mismatched types */
2841
2842 else if (oload_champ_bv->rank[ix] >= 10)
2843 oload_non_standard = 1; /* non-standard type conversions needed */
c906108c
SS
2844 }
2845 if (oload_incompatible)
2846 {
2847 if (method)
c5aa993b
JM
2848 error ("Cannot resolve method %s%s%s to any overloaded instance",
2849 obj_type_name,
2850 (obj_type_name && *obj_type_name) ? "::" : "",
2851 name);
c906108c 2852 else
c5aa993b
JM
2853 error ("Cannot resolve function %s to any overloaded instance",
2854 func_name);
c906108c
SS
2855 }
2856 else if (oload_non_standard)
2857 {
2858 if (method)
c5aa993b
JM
2859 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2860 obj_type_name,
2861 (obj_type_name && *obj_type_name) ? "::" : "",
2862 name);
c906108c 2863 else
c5aa993b
JM
2864 warning ("Using non-standard conversion to match function %s to supplied arguments",
2865 func_name);
c906108c
SS
2866 }
2867
2868 if (method)
2869 {
2870 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
c5aa993b 2871 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c 2872 else
c5aa993b 2873 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c
SS
2874 }
2875 else
2876 {
2877 *symp = oload_syms[oload_champ];
b8c9b27d 2878 xfree (func_name);
c906108c
SS
2879 }
2880
2881 return oload_incompatible ? 100 : (oload_non_standard ? 10 : 0);
2882}
2883
2884/* C++: return 1 is NAME is a legitimate name for the destructor
2885 of type TYPE. If TYPE does not have a destructor, or
2886 if NAME is inappropriate for TYPE, an error is signaled. */
2887int
fba45db2 2888destructor_name_p (const char *name, const struct type *type)
c906108c
SS
2889{
2890 /* destructors are a special case. */
2891
2892 if (name[0] == '~')
2893 {
2894 char *dname = type_name_no_tag (type);
2895 char *cp = strchr (dname, '<');
2896 unsigned int len;
2897
2898 /* Do not compare the template part for template classes. */
2899 if (cp == NULL)
2900 len = strlen (dname);
2901 else
2902 len = cp - dname;
2903 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
2904 error ("name of destructor must equal name of class");
2905 else
2906 return 1;
2907 }
2908 return 0;
2909}
2910
2911/* Helper function for check_field: Given TYPE, a structure/union,
2912 return 1 if the component named NAME from the ultimate
2913 target structure/union is defined, otherwise, return 0. */
2914
2915static int
fba45db2 2916check_field_in (register struct type *type, const char *name)
c906108c
SS
2917{
2918 register int i;
2919
2920 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2921 {
2922 char *t_field_name = TYPE_FIELD_NAME (type, i);
db577aea 2923 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2924 return 1;
2925 }
2926
2927 /* C++: If it was not found as a data field, then try to
2928 return it as a pointer to a method. */
2929
2930 /* Destructors are a special case. */
2931 if (destructor_name_p (name, type))
2932 {
2933 int m_index, f_index;
2934
2935 return get_destructor_fn_field (type, &m_index, &f_index);
2936 }
2937
2938 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2939 {
db577aea 2940 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
c906108c
SS
2941 return 1;
2942 }
2943
2944 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2945 if (check_field_in (TYPE_BASECLASS (type, i), name))
2946 return 1;
c5aa993b 2947
c906108c
SS
2948 return 0;
2949}
2950
2951
2952/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2953 return 1 if the component named NAME from the ultimate
2954 target structure/union is defined, otherwise, return 0. */
2955
2956int
fba45db2 2957check_field (register value_ptr arg1, const char *name)
c906108c
SS
2958{
2959 register struct type *t;
2960
2961 COERCE_ARRAY (arg1);
2962
2963 t = VALUE_TYPE (arg1);
2964
2965 /* Follow pointers until we get to a non-pointer. */
2966
2967 for (;;)
2968 {
2969 CHECK_TYPEDEF (t);
2970 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2971 break;
2972 t = TYPE_TARGET_TYPE (t);
2973 }
2974
2975 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2976 error ("not implemented: member type in check_field");
2977
c5aa993b 2978 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
2979 && TYPE_CODE (t) != TYPE_CODE_UNION)
2980 error ("Internal error: `this' is not an aggregate");
2981
2982 return check_field_in (t, name);
2983}
2984
2985/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2986 return the address of this member as a "pointer to member"
2987 type. If INTYPE is non-null, then it will be the type
2988 of the member we are looking for. This will help us resolve
2989 "pointers to member functions". This function is used
2990 to resolve user expressions of the form "DOMAIN::NAME". */
2991
2992value_ptr
fba45db2
KB
2993value_struct_elt_for_reference (struct type *domain, int offset,
2994 struct type *curtype, char *name,
2995 struct type *intype)
c906108c
SS
2996{
2997 register struct type *t = curtype;
2998 register int i;
2999 value_ptr v;
3000
c5aa993b 3001 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
3002 && TYPE_CODE (t) != TYPE_CODE_UNION)
3003 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
3004
3005 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
3006 {
3007 char *t_field_name = TYPE_FIELD_NAME (t, i);
c5aa993b 3008
c906108c
SS
3009 if (t_field_name && STREQ (t_field_name, name))
3010 {
3011 if (TYPE_FIELD_STATIC (t, i))
3012 {
3013 v = value_static_field (t, i);
3014 if (v == NULL)
3015 error ("Internal error: could not find static variable %s",
3016 name);
3017 return v;
3018 }
3019 if (TYPE_FIELD_PACKED (t, i))
3020 error ("pointers to bitfield members not allowed");
c5aa993b 3021
c906108c
SS
3022 return value_from_longest
3023 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
3024 domain)),
3025 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
3026 }
3027 }
3028
3029 /* C++: If it was not found as a data field, then try to
3030 return it as a pointer to a method. */
3031
3032 /* Destructors are a special case. */
3033 if (destructor_name_p (name, t))
3034 {
3035 error ("member pointers to destructors not implemented yet");
3036 }
3037
3038 /* Perform all necessary dereferencing. */
3039 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
3040 intype = TYPE_TARGET_TYPE (intype);
3041
3042 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3043 {
3044 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
3045 char dem_opname[64];
3046
c5aa993b
JM
3047 if (strncmp (t_field_name, "__", 2) == 0 ||
3048 strncmp (t_field_name, "op", 2) == 0 ||
3049 strncmp (t_field_name, "type", 4) == 0)
c906108c 3050 {
c5aa993b
JM
3051 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
3052 t_field_name = dem_opname;
3053 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 3054 t_field_name = dem_opname;
c906108c
SS
3055 }
3056 if (t_field_name && STREQ (t_field_name, name))
3057 {
3058 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
3059 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
c5aa993b 3060
c906108c
SS
3061 if (intype == 0 && j > 1)
3062 error ("non-unique member `%s' requires type instantiation", name);
3063 if (intype)
3064 {
3065 while (j--)
3066 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
3067 break;
3068 if (j < 0)
3069 error ("no member function matches that type instantiation");
3070 }
3071 else
3072 j = 0;
c5aa993b 3073
c906108c
SS
3074 if (TYPE_FN_FIELD_STUB (f, j))
3075 check_stub_method (t, i, j);
3076 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
3077 {
3078 return value_from_longest
3079 (lookup_reference_type
3080 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
3081 domain)),
3082 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
3083 }
3084 else
3085 {
3086 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3087 0, VAR_NAMESPACE, 0, NULL);
3088 if (s == NULL)
3089 {
3090 v = 0;
3091 }
3092 else
3093 {
3094 v = read_var_value (s, 0);
3095#if 0
3096 VALUE_TYPE (v) = lookup_reference_type
3097 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
3098 domain));
3099#endif
3100 }
3101 return v;
3102 }
3103 }
3104 }
3105 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3106 {
3107 value_ptr v;
3108 int base_offset;
3109
3110 if (BASETYPE_VIA_VIRTUAL (t, i))
3111 base_offset = 0;
3112 else
3113 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
3114 v = value_struct_elt_for_reference (domain,
3115 offset + base_offset,
3116 TYPE_BASECLASS (t, i),
3117 name,
3118 intype);
3119 if (v)
3120 return v;
3121 }
3122 return 0;
3123}
3124
3125
c906108c
SS
3126/* Given a pointer value V, find the real (RTTI) type
3127 of the object it points to.
3128 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
3129 and refer to the values computed for the object pointed to. */
3130
3131struct type *
fba45db2 3132value_rtti_target_type (value_ptr v, int *full, int *top, int *using_enc)
c906108c
SS
3133{
3134 value_ptr target;
3135
3136 target = value_ind (v);
3137
3138 return value_rtti_type (target, full, top, using_enc);
3139}
3140
3141/* Given a value pointed to by ARGP, check its real run-time type, and
3142 if that is different from the enclosing type, create a new value
3143 using the real run-time type as the enclosing type (and of the same
3144 type as ARGP) and return it, with the embedded offset adjusted to
3145 be the correct offset to the enclosed object
3146 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
3147 parameters, computed by value_rtti_type(). If these are available,
3148 they can be supplied and a second call to value_rtti_type() is avoided.
3149 (Pass RTYPE == NULL if they're not available */
3150
3151value_ptr
fba45db2
KB
3152value_full_object (value_ptr argp, struct type *rtype, int xfull, int xtop,
3153 int xusing_enc)
c906108c 3154{
c5aa993b 3155 struct type *real_type;
c906108c
SS
3156 int full = 0;
3157 int top = -1;
3158 int using_enc = 0;
3159 value_ptr new_val;
3160
3161 if (rtype)
3162 {
3163 real_type = rtype;
3164 full = xfull;
3165 top = xtop;
3166 using_enc = xusing_enc;
3167 }
3168 else
3169 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3170
3171 /* If no RTTI data, or if object is already complete, do nothing */
3172 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
3173 return argp;
3174
3175 /* If we have the full object, but for some reason the enclosing
c5aa993b 3176 type is wrong, set it *//* pai: FIXME -- sounds iffy */
c906108c
SS
3177 if (full)
3178 {
2b127877 3179 argp = value_change_enclosing_type (argp, real_type);
c906108c
SS
3180 return argp;
3181 }
3182
3183 /* Check if object is in memory */
3184 if (VALUE_LVAL (argp) != lval_memory)
3185 {
3186 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
c5aa993b 3187
c906108c
SS
3188 return argp;
3189 }
c5aa993b 3190
c906108c
SS
3191 /* All other cases -- retrieve the complete object */
3192 /* Go back by the computed top_offset from the beginning of the object,
3193 adjusting for the embedded offset of argp if that's what value_rtti_type
3194 used for its computation. */
3195 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
c5aa993b
JM
3196 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
3197 VALUE_BFD_SECTION (argp));
c906108c
SS
3198 VALUE_TYPE (new_val) = VALUE_TYPE (argp);
3199 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
3200 return new_val;
3201}
3202
3203
3204
3205
3206/* C++: return the value of the class instance variable, if one exists.
3207 Flag COMPLAIN signals an error if the request is made in an
3208 inappropriate context. */
3209
3210value_ptr
fba45db2 3211value_of_this (int complain)
c906108c
SS
3212{
3213 struct symbol *func, *sym;
3214 struct block *b;
3215 int i;
3216 static const char funny_this[] = "this";
3217 value_ptr this;
3218
3219 if (selected_frame == 0)
3220 {
3221 if (complain)
c5aa993b
JM
3222 error ("no frame selected");
3223 else
3224 return 0;
c906108c
SS
3225 }
3226
3227 func = get_frame_function (selected_frame);
3228 if (!func)
3229 {
3230 if (complain)
3231 error ("no `this' in nameless context");
c5aa993b
JM
3232 else
3233 return 0;
c906108c
SS
3234 }
3235
3236 b = SYMBOL_BLOCK_VALUE (func);
3237 i = BLOCK_NSYMS (b);
3238 if (i <= 0)
3239 {
3240 if (complain)
c5aa993b
JM
3241 error ("no args, no `this'");
3242 else
3243 return 0;
c906108c
SS
3244 }
3245
3246 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
3247 symbol instead of the LOC_ARG one (if both exist). */
3248 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
3249 if (sym == NULL)
3250 {
3251 if (complain)
3252 error ("current stack frame not in method");
3253 else
3254 return NULL;
3255 }
3256
3257 this = read_var_value (sym, selected_frame);
3258 if (this == 0 && complain)
3259 error ("`this' argument at unknown address");
3260 return this;
3261}
3262
3263/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
3264 long, starting at LOWBOUND. The result has the same lower bound as
3265 the original ARRAY. */
3266
3267value_ptr
fba45db2 3268value_slice (value_ptr array, int lowbound, int length)
c906108c
SS
3269{
3270 struct type *slice_range_type, *slice_type, *range_type;
3271 LONGEST lowerbound, upperbound, offset;
3272 value_ptr slice;
3273 struct type *array_type;
3274 array_type = check_typedef (VALUE_TYPE (array));
3275 COERCE_VARYING_ARRAY (array, array_type);
3276 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
3277 && TYPE_CODE (array_type) != TYPE_CODE_STRING
3278 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
3279 error ("cannot take slice of non-array");
3280 range_type = TYPE_INDEX_TYPE (array_type);
3281 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
3282 error ("slice from bad array or bitstring");
3283 if (lowbound < lowerbound || length < 0
3284 || lowbound + length - 1 > upperbound
c5aa993b 3285 /* Chill allows zero-length strings but not arrays. */
c906108c
SS
3286 || (current_language->la_language == language_chill
3287 && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY))
3288 error ("slice out of range");
3289 /* FIXME-type-allocation: need a way to free this type when we are
3290 done with it. */
c5aa993b 3291 slice_range_type = create_range_type ((struct type *) NULL,
c906108c
SS
3292 TYPE_TARGET_TYPE (range_type),
3293 lowbound, lowbound + length - 1);
3294 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
3295 {
3296 int i;
c5aa993b 3297 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
c906108c
SS
3298 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
3299 slice = value_zero (slice_type, not_lval);
3300 for (i = 0; i < length; i++)
3301 {
3302 int element = value_bit_index (array_type,
3303 VALUE_CONTENTS (array),
3304 lowbound + i);
3305 if (element < 0)
3306 error ("internal error accessing bitstring");
3307 else if (element > 0)
3308 {
3309 int j = i % TARGET_CHAR_BIT;
3310 if (BITS_BIG_ENDIAN)
3311 j = TARGET_CHAR_BIT - 1 - j;
3312 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
3313 }
3314 }
3315 /* We should set the address, bitssize, and bitspos, so the clice
7b83ea04
AC
3316 can be used on the LHS, but that may require extensions to
3317 value_assign. For now, just leave as a non_lval. FIXME. */
c906108c
SS
3318 }
3319 else
3320 {
3321 struct type *element_type = TYPE_TARGET_TYPE (array_type);
3322 offset
3323 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
c5aa993b 3324 slice_type = create_array_type ((struct type *) NULL, element_type,
c906108c
SS
3325 slice_range_type);
3326 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
3327 slice = allocate_value (slice_type);
3328 if (VALUE_LAZY (array))
3329 VALUE_LAZY (slice) = 1;
3330 else
3331 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
3332 TYPE_LENGTH (slice_type));
3333 if (VALUE_LVAL (array) == lval_internalvar)
3334 VALUE_LVAL (slice) = lval_internalvar_component;
3335 else
3336 VALUE_LVAL (slice) = VALUE_LVAL (array);
3337 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
3338 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
3339 }
3340 return slice;
3341}
3342
3343/* Assuming chill_varying_type (VARRAY) is true, return an equivalent
3344 value as a fixed-length array. */
3345
3346value_ptr
fba45db2 3347varying_to_slice (value_ptr varray)
c906108c
SS
3348{
3349 struct type *vtype = check_typedef (VALUE_TYPE (varray));
3350 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
3351 VALUE_CONTENTS (varray)
3352 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
3353 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
3354}
3355
070ad9f0
DB
3356/* Create a value for a FORTRAN complex number. Currently most of
3357 the time values are coerced to COMPLEX*16 (i.e. a complex number
3358 composed of 2 doubles. This really should be a smarter routine
3359 that figures out precision inteligently as opposed to assuming
c5aa993b 3360 doubles. FIXME: fmb */
c906108c
SS
3361
3362value_ptr
fba45db2 3363value_literal_complex (value_ptr arg1, value_ptr arg2, struct type *type)
c906108c
SS
3364{
3365 register value_ptr val;
3366 struct type *real_type = TYPE_TARGET_TYPE (type);
3367
3368 val = allocate_value (type);
3369 arg1 = value_cast (real_type, arg1);
3370 arg2 = value_cast (real_type, arg2);
3371
3372 memcpy (VALUE_CONTENTS_RAW (val),
3373 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
3374 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
3375 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
3376 return val;
3377}
3378
3379/* Cast a value into the appropriate complex data type. */
3380
3381static value_ptr
fba45db2 3382cast_into_complex (struct type *type, register value_ptr val)
c906108c
SS
3383{
3384 struct type *real_type = TYPE_TARGET_TYPE (type);
3385 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
3386 {
3387 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
3388 value_ptr re_val = allocate_value (val_real_type);
3389 value_ptr im_val = allocate_value (val_real_type);
3390
3391 memcpy (VALUE_CONTENTS_RAW (re_val),
3392 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
3393 memcpy (VALUE_CONTENTS_RAW (im_val),
3394 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
c5aa993b 3395 TYPE_LENGTH (val_real_type));
c906108c
SS
3396
3397 return value_literal_complex (re_val, im_val, type);
3398 }
3399 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
3400 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
3401 return value_literal_complex (val, value_zero (real_type, not_lval), type);
3402 else
3403 error ("cannot cast non-number to complex");
3404}
3405
3406void
fba45db2 3407_initialize_valops (void)
c906108c
SS
3408{
3409#if 0
3410 add_show_from_set
c5aa993b 3411 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
c906108c
SS
3412 "Set automatic abandonment of expressions upon failure.",
3413 &setlist),
3414 &showlist);
3415#endif
3416
3417 add_show_from_set
c5aa993b 3418 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
c906108c
SS
3419 "Set overload resolution in evaluating C++ functions.",
3420 &setlist),
3421 &showlist);
3422 overload_resolution = 1;
3423
242bfc55
FN
3424 add_show_from_set (
3425 add_set_cmd ("unwindonsignal", no_class, var_boolean,
3426 (char *) &unwind_on_signal_p,
3427"Set unwinding of stack if a signal is received while in a call dummy.\n\
3428The unwindonsignal lets the user determine what gdb should do if a signal\n\
3429is received while in a function called from gdb (call dummy). If set, gdb\n\
3430unwinds the stack and restore the context to what as it was before the call.\n\
3431The default is to stop in the frame where the signal was received.", &setlist),
3432 &showlist);
c906108c 3433}
This page took 0.288147 seconds and 4 git commands to generate.