gdb-2.5.2
[binutils-gdb.git] / gdb / valops.c
1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20 #include "stdio.h"
21 #include "defs.h"
22 #include "initialize.h"
23 #include "param.h"
24 #include "symtab.h"
25 #include "value.h"
26
27 START_FILE
28 \f
29 /* Cast value ARG2 to type TYPE and return as a value.
30 More general than a C cast: accepts any two types of the same length,
31 and if ARG2 is an lvalue it can be cast into anything at all. */
32
33 value
34 value_cast (type, arg2)
35 struct type *type;
36 register value arg2;
37 {
38 register enum type_code code1;
39 register enum type_code code2;
40 register int scalar;
41
42 /* Coerce arrays but not enums. Enums will work as-is
43 and coercing them would cause an infinite recursion. */
44 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
45 COERCE_ARRAY (arg2);
46
47 code1 = TYPE_CODE (type);
48 code2 = TYPE_CODE (VALUE_TYPE (arg2));
49 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
50 || code2 == TYPE_CODE_ENUM);
51
52 if (code1 == TYPE_CODE_FLT && scalar)
53 return value_from_double (type, value_as_double (arg2));
54 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
55 && (scalar || code2 == TYPE_CODE_PTR))
56 return value_from_long (type, value_as_long (arg2));
57 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
58 {
59 VALUE_TYPE (arg2) = type;
60 return arg2;
61 }
62 else if (VALUE_LVAL (arg2) == lval_memory)
63 {
64 return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
65 }
66 else
67 error ("Invalid cast.");
68 }
69
70 /* Return the value with a specified type located at specified address. */
71
72 value
73 value_at (type, addr)
74 struct type *type;
75 CORE_ADDR addr;
76 {
77 register value val = allocate_value (type);
78
79 read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
80 VALUE_LVAL (val) = lval_memory;
81 VALUE_ADDRESS (val) = addr;
82
83 return val;
84 }
85
86 /* Store the contents of FROMVAL into the location of TOVAL.
87 Return a new value with the location of TOVAL and contents of FROMVAL. */
88
89 value
90 value_assign (toval, fromval)
91 register value toval, fromval;
92 {
93 register struct type *type = VALUE_TYPE (toval);
94 register value val;
95 char raw_buffer[MAX_REGISTER_RAW_SIZE];
96 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
97 int use_buffer = 0;
98
99 COERCE_ARRAY (fromval);
100
101 if (VALUE_LVAL (toval) != lval_internalvar)
102 fromval = value_cast (type, fromval);
103
104 /* If TOVAL is a special machine register requiring conversion
105 of program values to a special raw format,
106 convert FROMVAL's contents now, with result in `raw_buffer',
107 and set USE_BUFFER to the number of bytes to write. */
108
109 if (VALUE_REGNO (toval) >= 0
110 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
111 {
112 int regno = VALUE_REGNO (toval);
113 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
114 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
115 bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
116 REGISTER_VIRTUAL_SIZE (regno));
117 REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
118 use_buffer = REGISTER_RAW_SIZE (regno);
119 }
120
121 switch (VALUE_LVAL (toval))
122 {
123 case lval_internalvar:
124 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
125 break;
126
127 case lval_internalvar_component:
128 set_internalvar_component (VALUE_INTERNALVAR (toval),
129 VALUE_OFFSET (toval),
130 VALUE_BITPOS (toval),
131 VALUE_BITSIZE (toval),
132 fromval);
133 break;
134
135 case lval_memory:
136 if (VALUE_BITSIZE (toval))
137 {
138 int val;
139 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
140 &val, sizeof val);
141 modify_field (&val, value_as_long (fromval),
142 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
143 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
144 &val, sizeof val);
145 }
146 else if (use_buffer)
147 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
148 raw_buffer, use_buffer);
149 else
150 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
151 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
152 break;
153
154 case lval_register:
155 if (VALUE_BITSIZE (toval))
156 {
157 int val;
158
159 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
160 &val, sizeof val);
161 modify_field (&val, value_as_long (fromval),
162 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
163 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
164 &val, sizeof val);
165 }
166 else if (use_buffer)
167 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
168 raw_buffer, use_buffer);
169 else
170 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
171 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
172 break;
173
174 default:
175 error ("Left side of = operation is not an lvalue.");
176 }
177
178 /* Return a value just like TOVAL except with the contents of FROMVAL. */
179
180 val = allocate_value (type);
181 bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
182 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
183
184 return val;
185 }
186
187 /* Extend a value VAL to COUNT repetitions of its type. */
188
189 value
190 value_repeat (arg1, count)
191 value arg1;
192 int count;
193 {
194 register value val;
195
196 if (VALUE_LVAL (arg1) != lval_memory)
197 error ("Only values in memory can be extended with '@'.");
198 if (count < 1)
199 error ("Invalid number %d of repetitions.", count);
200
201 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
202
203 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
204 VALUE_CONTENTS (val),
205 TYPE_LENGTH (VALUE_TYPE (val)) * count);
206 VALUE_LVAL (val) = lval_memory;
207 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
208
209 return val;
210 }
211
212 value
213 value_of_variable (var)
214 struct symbol *var;
215 {
216 return read_var_value (var, (CORE_ADDR) 0);
217 }
218
219 /* Given a value which is an array, return a value which is
220 a pointer to its first element. */
221
222 value
223 value_coerce_array (arg1)
224 value arg1;
225 {
226 register struct type *type;
227 register value val;
228
229 if (VALUE_LVAL (arg1) != lval_memory)
230 error ("Attempt to take address of value not located in memory.");
231
232 /* Get type of elements. */
233 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
234 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
235 else
236 /* A phony array made by value_repeat.
237 Its type is the type of the elements, not an array type. */
238 type = VALUE_TYPE (arg1);
239
240 /* Get the type of the result. */
241 type = lookup_pointer_type (type);
242 val = value_from_long (builtin_type_long,
243 VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
244 VALUE_TYPE (val) = type;
245 return val;
246 }
247
248 /* Return a pointer value for the object for which ARG1 is the contents. */
249
250 value
251 value_addr (arg1)
252 value arg1;
253 {
254 register struct type *type;
255 register value val, arg1_coerced;
256
257 /* Taking the address of an array is really a no-op
258 once the array is coerced to a pointer to its first element. */
259 arg1_coerced = arg1;
260 COERCE_ARRAY (arg1_coerced);
261 if (arg1 != arg1_coerced)
262 return arg1_coerced;
263
264 if (VALUE_LVAL (arg1) != lval_memory)
265 error ("Attempt to take address of value not located in memory.");
266
267 /* Get the type of the result. */
268 type = lookup_pointer_type (VALUE_TYPE (arg1));
269 val = value_from_long (builtin_type_long,
270 VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
271 VALUE_TYPE (val) = type;
272 return val;
273 }
274
275 /* Given a value of a pointer type, apply the C unary * operator to it. */
276
277 value
278 value_ind (arg1)
279 value arg1;
280 {
281 COERCE_ARRAY (arg1);
282
283 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
284 error ("not implemented: member types in value_ind");
285
286 /* Allow * on an integer so we can cast it to whatever we want. */
287 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
288 return value_at (builtin_type_long,
289 (CORE_ADDR) value_as_long (arg1));
290 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
291 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
292 (CORE_ADDR) value_as_long (arg1));
293 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
294 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
295 (CORE_ADDR) value_as_long (arg1));
296 error ("Attempt to take contents of a non-pointer value.");
297 }
298 \f
299 /* Pushing small parts of stack frames. */
300
301 /* Push one word (the size of object that a register holds). */
302
303 CORE_ADDR
304 push_word (sp, buffer)
305 CORE_ADDR sp;
306 REGISTER_TYPE buffer;
307 {
308 register int len = sizeof (REGISTER_TYPE);
309
310 #if 1 INNER_THAN 2
311 sp -= len;
312 write_memory (sp, &buffer, len);
313 #else /* stack grows upward */
314 write_memory (sp, &buffer, len);
315 sp += len;
316 #endif /* stack grows upward */
317
318 return sp;
319 }
320
321 /* Push LEN bytes with data at BUFFER. */
322
323 CORE_ADDR
324 push_bytes (sp, buffer, len)
325 CORE_ADDR sp;
326 char *buffer;
327 int len;
328 {
329 #if 1 INNER_THAN 2
330 sp -= len;
331 write_memory (sp, buffer, len);
332 #else /* stack grows upward */
333 write_memory (sp, buffer, len);
334 sp += len;
335 #endif /* stack grows upward */
336
337 return sp;
338 }
339
340 /* Push onto the stack the specified value VALUE. */
341
342 CORE_ADDR
343 value_push (sp, arg)
344 register CORE_ADDR sp;
345 value arg;
346 {
347 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
348
349 #if 1 INNER_THAN 2
350 sp -= len;
351 write_memory (sp, VALUE_CONTENTS (arg), len);
352 #else /* stack grows upward */
353 write_memory (sp, VALUE_CONTENTS (arg), len);
354 sp += len;
355 #endif /* stack grows upward */
356
357 return sp;
358 }
359
360 /* Perform the standard coercions that are specified
361 for arguments to be passed to C functions. */
362
363 value
364 value_arg_coerce (arg)
365 value arg;
366 {
367 register struct type *type;
368
369 COERCE_ENUM (arg);
370
371 type = VALUE_TYPE (arg);
372
373 if (TYPE_CODE (type) == TYPE_CODE_INT
374 && TYPE_LENGTH (type) < sizeof (int))
375 return value_cast (builtin_type_int, arg);
376
377 if (type == builtin_type_float)
378 return value_cast (builtin_type_double, arg);
379
380 return arg;
381 }
382
383 /* Push the value ARG, first coercing it as an argument
384 to a C function. */
385
386 CORE_ADDR
387 value_arg_push (sp, arg)
388 register CORE_ADDR sp;
389 value arg;
390 {
391 return value_push (sp, value_arg_coerce (arg));
392 }
393
394 /* Perform a function call in the inferior.
395 ARGS is a vector of values of arguments (NARGS of them).
396 FUNCTION is a value, the function to be called.
397 Returns a value representing what the function returned.
398 May fail to return, if a breakpoint or signal is hit
399 during the execution of the function. */
400
401 value
402 call_function (function, nargs, args)
403 value function;
404 int nargs;
405 value *args;
406 {
407 register CORE_ADDR sp;
408 register int i;
409 CORE_ADDR start_sp;
410 static REGISTER_TYPE dummy[] = CALL_DUMMY;
411 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
412 CORE_ADDR old_sp;
413 struct type *value_type;
414
415 PUSH_DUMMY_FRAME;
416
417 {
418 register CORE_ADDR funaddr;
419 register struct type *ftype = VALUE_TYPE (function);
420 register enum type_code code = TYPE_CODE (ftype);
421
422 /* If it's a member function, just look at the function
423 part of it. */
424 if (code == TYPE_CODE_MEMBER)
425 {
426 ftype = TYPE_TARGET_TYPE (ftype);
427 code = TYPE_CODE (ftype);
428 }
429
430 /* Determine address to call. */
431 if (code == TYPE_CODE_FUNC)
432 {
433 funaddr = VALUE_ADDRESS (function);
434 value_type = TYPE_TARGET_TYPE (ftype);
435 }
436 else if (code == TYPE_CODE_PTR)
437 {
438 funaddr = value_as_long (function);
439 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype))
440 == TYPE_CODE_FUNC)
441 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
442 else
443 value_type = builtin_type_int;
444 }
445 else if (code == TYPE_CODE_INT)
446 {
447 /* Handle the case of functions lacking debugging info.
448 Their values are characters since their addresses are char */
449 if (TYPE_LENGTH (ftype) == 1)
450 funaddr = value_as_long (value_addr (function));
451 else
452 /* Handle integer used as address of a function. */
453 funaddr = value_as_long (function);
454
455 value_type = builtin_type_int;
456 }
457 else
458 error ("Invalid data type for function to be called.");
459
460 /* Create a call sequence customized for this function
461 and the number of arguments for it. */
462 bcopy (dummy, dummy1, sizeof dummy);
463 FIX_CALL_DUMMY (dummy1, funaddr, nargs);
464 }
465
466 old_sp = sp = read_register (SP_REGNUM);
467
468 #if 1 INNER_THAN 2 /* Stack grows down */
469 sp -= sizeof dummy;
470 write_memory (sp, dummy1, sizeof dummy);
471 start_sp = sp;
472 for (i = nargs - 1; i >= 0; i--)
473 sp = value_arg_push (sp, args[i]);
474 #else /* Stack grows up */
475 start_sp = sp;
476 write_memory (sp, dummy1, sizeof dummy);
477 sp += sizeof dummy;
478 for (i = 0; i < nargs; i++)
479 sp = value_arg_push (sp, args[i]);
480 #endif /* Stack grows up */
481
482 write_register (SP_REGNUM, sp);
483
484 /* Figure out the value returned by the function. */
485 {
486 char retbuf[REGISTER_BYTES];
487
488 /* Execute the stack dummy routine, calling FUNCTION.
489 When it is done, discard the empty frame
490 after storing the contents of all regs into retbuf. */
491 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
492
493 return value_being_returned (value_type, retbuf);
494 }
495 }
496 \f
497 /* Create a value for a string constant:
498 Call the function malloc in the inferior to get space for it,
499 then copy the data into that space
500 and then return the address with type char *.
501 PTR points to the string constant data; LEN is number of characters. */
502
503 value
504 value_string (ptr, len)
505 char *ptr;
506 int len;
507 {
508 register value val;
509 register struct symbol *sym;
510 value blocklen;
511 register char *copy = (char *) alloca (len + 1);
512 char *i = ptr;
513 register char *o = copy, *ibeg = ptr;
514 register int c;
515
516 /* Copy the string into COPY, processing escapes.
517 We could not conveniently process them in expread
518 because the string there wants to be a substring of the input. */
519
520 while (i - ibeg < len)
521 {
522 c = *i++;
523 if (c == '\\')
524 {
525 c = parse_escape (&i);
526 if (c == -1)
527 continue;
528 }
529 *o++ = c;
530 }
531 *o = 0;
532
533 /* Get the length of the string after escapes are processed. */
534
535 len = o - copy;
536
537 /* Find the address of malloc in the inferior. */
538
539 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE);
540 if (sym != 0)
541 {
542 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
543 error ("\"malloc\" exists in this program but is not a function.");
544 val = value_of_variable (sym);
545 }
546 else
547 {
548 register int i;
549 for (i = 0; i < misc_function_count; i++)
550 if (!strcmp (misc_function_vector[i].name, "malloc"))
551 break;
552 if (i < misc_function_count)
553 val = value_from_long (builtin_type_long,
554 misc_function_vector[i].address);
555 else
556 error ("String constants require the program to have a function \"malloc\".");
557 }
558
559 blocklen = value_from_long (builtin_type_int, len + 1);
560 val = call_function (val, 1, &blocklen);
561 if (value_zerop (val))
562 error ("No memory available for string constant.");
563 write_memory (value_as_long (val), copy, len + 1);
564 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
565 return val;
566 }
567 \f
568 /* Given ARG1, a value of type (pointer to a)* structure/union,
569 extract the component named NAME from the ultimate target structure/union
570 and return it as a value with its appropriate type.
571 ERR is used in the error message if ARG1's type is wrong.
572
573 C++: ARGS is a list of argument types to aid in the selection of
574 an appropriate method. Also, handle derived types.
575
576 ERR is an error message to be printed in case the field is not found. */
577
578 value
579 value_struct_elt (arg1, args, name, err)
580 register value arg1, *args;
581 char *name;
582 char *err;
583 {
584 register struct type *t;
585 register int i;
586 int found = 0;
587
588 struct type *baseclass;
589
590 COERCE_ARRAY (arg1);
591
592 t = VALUE_TYPE (arg1);
593
594 /* Follow pointers until we get to a non-pointer. */
595
596 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
597 {
598 arg1 = value_ind (arg1);
599 COERCE_ARRAY (arg1);
600 t = VALUE_TYPE (arg1);
601 }
602
603 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
604 error ("not implemented: member type in value_struct_elt");
605
606 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
607 && TYPE_CODE (t) != TYPE_CODE_UNION)
608 error ("Attempt to extract a component of a value that is not a %s.", err);
609
610 baseclass = t;
611
612 if (!args)
613 {
614 /* if there are no arguments ...do this... */
615
616 /* Try as a variable first, because if we succeed, there
617 is less work to be done. */
618 while (t)
619 {
620 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
621 {
622 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
623 {
624 found = 1;
625 break;
626 }
627 }
628
629 if (i >= 0)
630 return TYPE_FIELD_STATIC (t, i)
631 ? value_static_field (t, name, i) : value_field (arg1, i);
632
633 t = TYPE_BASECLASS (t);
634 VALUE_TYPE (arg1) = t; /* side effect! */
635 }
636
637 /* C++: If it was not found as a data field, then try to
638 return it as a pointer to a method. */
639 t = baseclass;
640 VALUE_TYPE (arg1) = t; /* side effect! */
641
642 if (destructor_name_p (name, t))
643 error ("use `info method' command to print out value of destructor");
644
645 while (t)
646 {
647 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
648 {
649 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
650 {
651 error ("use `info method' command to print value of method \"%s\"", name);
652 }
653 }
654 t = TYPE_BASECLASS (t);
655 }
656
657 if (found == 0)
658 error("there is no field named %s", name);
659 return 0;
660 }
661
662 if (destructor_name_p (name, t))
663 {
664 if (!args[1])
665 {
666 /* destructors are a special case. */
667 return (value)value_fn_field (arg1, 0, TYPE_FN_FIELDLIST_LENGTH (t, 0));
668 }
669 else
670 {
671 error ("destructor should not have any argument");
672 }
673 }
674
675 /* This following loop is for methods with arguments. */
676 while (t)
677 {
678 /* Look up as method first, because that is where we
679 expect to find it first. */
680 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
681 {
682 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
683
684 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
685 {
686 int j;
687 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
688
689 found = 1;
690 for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j)
691 {
692 if (!typecmp (TYPE_FN_FIELD_ARGS (f, j), args))
693 {
694 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
695 {
696 /* First, get the virtual function table pointer.
697 That comes with a strange type, so cast
698 it to type `pointer to long' (which
699 should serve just fine as a function type).
700 Then, index into the table, and convert
701 final value to appropriate function type. */
702 value vfn, vtbl;
703 value vi = value_from_long (builtin_type_int,
704 TYPE_FN_FIELD_VOFFSET (f, j));
705 VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (t);
706
707 if (TYPE_VPTR_FIELDNO (t) < 0)
708 TYPE_VPTR_FIELDNO (t)
709 = fill_in_vptr_fieldno (t);
710
711 vtbl = value_field (arg1, TYPE_VPTR_FIELDNO (t));
712 vfn = value_subscript (vtbl, vi);
713 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
714 return vfn;
715 }
716 else
717 return (value)value_fn_field (arg1, i, j);
718 }
719 }
720 }
721 }
722 t = TYPE_BASECLASS (t);
723 VALUE_TYPE (arg1) = t; /* side effect! */
724 }
725
726 if (found)
727 {
728 error ("Structure method %s not defined for arglist.", name);
729 return 0;
730 }
731 else
732 {
733 /* See if user tried to invoke data as function */
734 t = baseclass;
735 while (t)
736 {
737 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
738 {
739 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
740 {
741 found = 1;
742 break;
743 }
744 }
745
746 if (i >= 0)
747 return TYPE_FIELD_STATIC (t, i)
748 ? value_static_field (t, name, i) : value_field (arg1, i);
749
750 t = TYPE_BASECLASS (t);
751 VALUE_TYPE (arg1) = t; /* side effect! */
752 }
753 error ("Structure has no component named %s.", name);
754 }
755 }
756
757 /* C++: return 1 is NAME is a legitimate name for the destructor
758 of type TYPE. If TYPE does not have a destructor, or
759 if NAME is inappropriate for TYPE, an error is signaled. */
760 int
761 destructor_name_p (name, type)
762 char *name;
763 struct type *type;
764 {
765 /* destructors are a special case. */
766 char *dname = TYPE_NAME (type);
767
768 if (name[0] == '~')
769 {
770 if (! TYPE_HAS_DESTRUCTOR (type))
771 error ("type `%s' does not have destructor defined",
772 TYPE_NAME (type));
773 /* Skip past the "struct " at the front. */
774 while (*dname++ != ' ') ;
775 if (strcmp (dname, name+1))
776 error ("destructor specification error");
777 else
778 return 1;
779 }
780 return 0;
781 }
782
783 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
784 return 1 if the component named NAME from the ultimate
785 target structure/union is defined, otherwise, return 0. */
786
787 int
788 check_field (arg1, name)
789 register value arg1;
790 char *name;
791 {
792 register struct type *t;
793 register int i;
794 int found = 0;
795
796 struct type *baseclass;
797
798 COERCE_ARRAY (arg1);
799
800 t = VALUE_TYPE (arg1);
801
802 /* Follow pointers until we get to a non-pointer. */
803
804 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
805 {
806 arg1 = value_ind (arg1);
807 COERCE_ARRAY (arg1);
808 t = VALUE_TYPE (arg1);
809 }
810
811 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
812 error ("not implemented: member type in check_field");
813
814 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
815 && TYPE_CODE (t) != TYPE_CODE_UNION)
816 error ("Internal error: `this' is not an aggregate");
817
818 baseclass = t;
819
820 while (t)
821 {
822 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
823 {
824 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
825 {
826 return 1;
827 }
828 }
829 t = TYPE_BASECLASS (t);
830 VALUE_TYPE (arg1) = t; /* side effect! */
831 }
832
833 /* C++: If it was not found as a data field, then try to
834 return it as a pointer to a method. */
835 t = baseclass;
836 VALUE_TYPE (arg1) = t; /* side effect! */
837
838 /* Destructors are a special case. */
839 if (destructor_name_p (name, t))
840 return 1;
841
842 while (t)
843 {
844 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
845 {
846 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
847 return 1;
848 }
849 t = TYPE_BASECLASS (t);
850 }
851 return 0;
852 }
853
854 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
855 return the address of this member as a pointer to member
856 type. If INTYPE is non-null, then it will be the type
857 of the member we are looking for. This will help us resolve
858 pointers to member functions. */
859
860 value
861 value_struct_elt_for_address (domain, intype, name)
862 struct type *domain, *intype;
863 char *name;
864 {
865 register struct type *t = domain;
866 register int i;
867 int found = 0;
868 value v;
869
870 struct type *baseclass;
871
872 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
873 && TYPE_CODE (t) != TYPE_CODE_UNION)
874 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
875
876 baseclass = t;
877
878 while (t)
879 {
880 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
881 {
882 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
883 {
884 if (TYPE_FIELD_PACKED (t, i))
885 error ("pointers to bitfield members not allowed");
886
887 v = value_from_long (builtin_type_int, TYPE_FIELD_BITPOS (t, i) >> 3);
888 VALUE_TYPE (v) = lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass);
889 return v;
890 }
891 }
892 t = TYPE_BASECLASS (t);
893 }
894
895 /* C++: If it was not found as a data field, then try to
896 return it as a pointer to a method. */
897 t = baseclass;
898
899 /* Destructors are a special case. */
900 if (destructor_name_p (name, t))
901 {
902 error ("pointers to destructors not implemented yet");
903 }
904
905 while (t)
906 {
907 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
908 {
909 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
910 {
911 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
912 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
913
914 if (intype == 0 && j > 1)
915 error ("non-unique member `%s' requires type instantiation", name);
916 if (intype)
917 {
918 while (j--)
919 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
920 break;
921 if (j < 0)
922 error ("no member function matches that type instantiation");
923 }
924 else
925 j = 0;
926
927 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
928 {
929 v = value_from_long (builtin_type_long,
930 TYPE_FN_FIELD_VOFFSET (f, j));
931 }
932 else
933 {
934 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
935 0, VAR_NAMESPACE);
936 v = locate_var_value (s, 0);
937 }
938 VALUE_TYPE (v) = lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass);
939 return v;
940 }
941 }
942 t = TYPE_BASECLASS (t);
943 }
944 return 0;
945 }
946
947 /* Compare two argument lists and return the position in which they differ,
948 or zero if equal. Note that we ignore the first argument, which is
949 the type of the instance variable. This is because we want to handle
950 derived classes. This is not entirely correct: we should actually
951 check to make sure that a requested operation is type secure,
952 shouldn't we? */
953 int typecmp(t1, t2)
954 struct type *t1[];
955 value t2[];
956 {
957 int i;
958
959 if (t1[0]->code == TYPE_CODE_VOID) return 0;
960 if (!t1[1])return 0;
961 for (i = 1; t1[i]->code != TYPE_CODE_VOID; i++)
962 {
963 if (! t2[i]
964 || t1[i]->code != t2[i]->type->code
965 || t1[i]->target_type != t2[i]->type->target_type)
966 {
967 return i+1;
968 }
969 }
970 return t2[i] ? i+1 : 0;
971 }
972
973 #ifndef FRAME
974 #include "frame.h"
975 #endif
976
977 /* C++: return the value of the class instance variable, if one exists.
978 Flag COMPLAIN signals an error if the request is made in an
979 inappropriate context. */
980 value
981 value_of_this (complain)
982 int complain;
983 {
984 extern FRAME selected_frame;
985 struct symbol *func, *sym;
986 char *funname = 0;
987 struct block *b;
988 int i;
989
990 if (selected_frame == 0)
991 if (complain)
992 error ("no frame selected");
993 else return 0;
994
995 func = get_frame_function (selected_frame);
996 if (func)
997 funname = SYMBOL_NAME (func);
998 else
999 if (complain)
1000 error ("no `this' in nameless context");
1001 else return 0;
1002
1003 b = SYMBOL_BLOCK_VALUE (func);
1004 i = BLOCK_NSYMS (b);
1005 if (i <= 0)
1006 if (complain)
1007 error ("no args, no `this'");
1008 else return 0;
1009
1010 sym = BLOCK_SYM (b, 0);
1011 if (strncmp ("$this", SYMBOL_NAME (sym), 5))
1012 if (complain)
1013 error ("current stack frame not in method");
1014 else return 0;
1015
1016 return read_var_value (sym, selected_frame);
1017 }
1018 \f
1019 static
1020 initialize ()
1021 { }
1022
1023 END_FILE