* parser-defs.h (struct exp_descriptor): New definition, containing
[deliverable/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
5 Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "value.h"
29 #include "expression.h"
30 #include "target.h"
31 #include "frame.h"
32 #include "language.h" /* For CAST_IS_CONVERSION */
33 #include "f-lang.h" /* for array bound stuff */
34 #include "cp-abi.h"
35 #include "infcall.h"
36 #include "objc-lang.h"
37 #include "block.h"
38 #include "parser-defs.h"
39
40 /* Defined in symtab.c */
41 extern int hp_som_som_object_present;
42
43 /* This is defined in valops.c */
44 extern int overload_resolution;
45
46 /* JYG: lookup rtti type of STRUCTOP_PTR when this is set to continue
47 on with successful lookup for member/method of the rtti type. */
48 extern int objectprint;
49
50 /* Prototypes for local functions. */
51
52 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *);
53
54 static struct value *evaluate_subexp_for_address (struct expression *,
55 int *, enum noside);
56
57 static struct value *evaluate_subexp (struct type *, struct expression *,
58 int *, enum noside);
59
60 static char *get_label (struct expression *, int *);
61
62 static struct value *evaluate_struct_tuple (struct value *,
63 struct expression *, int *,
64 enum noside, int);
65
66 static LONGEST init_array_element (struct value *, struct value *,
67 struct expression *, int *, enum noside,
68 LONGEST, LONGEST);
69
70 static struct value *
71 evaluate_subexp (struct type *expect_type, struct expression *exp,
72 int *pos, enum noside noside)
73 {
74 return (*exp->language_defn->la_exp_desc->evaluate_exp)
75 (expect_type, exp, pos, noside);
76 }
77 \f
78 /* Parse the string EXP as a C expression, evaluate it,
79 and return the result as a number. */
80
81 CORE_ADDR
82 parse_and_eval_address (char *exp)
83 {
84 struct expression *expr = parse_expression (exp);
85 CORE_ADDR addr;
86 struct cleanup *old_chain =
87 make_cleanup (free_current_contents, &expr);
88
89 addr = value_as_address (evaluate_expression (expr));
90 do_cleanups (old_chain);
91 return addr;
92 }
93
94 /* Like parse_and_eval_address but takes a pointer to a char * variable
95 and advanced that variable across the characters parsed. */
96
97 CORE_ADDR
98 parse_and_eval_address_1 (char **expptr)
99 {
100 struct expression *expr = parse_exp_1 (expptr, (struct block *) 0, 0);
101 CORE_ADDR addr;
102 struct cleanup *old_chain =
103 make_cleanup (free_current_contents, &expr);
104
105 addr = value_as_address (evaluate_expression (expr));
106 do_cleanups (old_chain);
107 return addr;
108 }
109
110 /* Like parse_and_eval_address, but treats the value of the expression
111 as an integer, not an address, returns a LONGEST, not a CORE_ADDR */
112 LONGEST
113 parse_and_eval_long (char *exp)
114 {
115 struct expression *expr = parse_expression (exp);
116 LONGEST retval;
117 struct cleanup *old_chain =
118 make_cleanup (free_current_contents, &expr);
119
120 retval = value_as_long (evaluate_expression (expr));
121 do_cleanups (old_chain);
122 return (retval);
123 }
124
125 struct value *
126 parse_and_eval (char *exp)
127 {
128 struct expression *expr = parse_expression (exp);
129 struct value *val;
130 struct cleanup *old_chain =
131 make_cleanup (free_current_contents, &expr);
132
133 val = evaluate_expression (expr);
134 do_cleanups (old_chain);
135 return val;
136 }
137
138 /* Parse up to a comma (or to a closeparen)
139 in the string EXPP as an expression, evaluate it, and return the value.
140 EXPP is advanced to point to the comma. */
141
142 struct value *
143 parse_to_comma_and_eval (char **expp)
144 {
145 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
146 struct value *val;
147 struct cleanup *old_chain =
148 make_cleanup (free_current_contents, &expr);
149
150 val = evaluate_expression (expr);
151 do_cleanups (old_chain);
152 return val;
153 }
154 \f
155 /* Evaluate an expression in internal prefix form
156 such as is constructed by parse.y.
157
158 See expression.h for info on the format of an expression. */
159
160 struct value *
161 evaluate_expression (struct expression *exp)
162 {
163 int pc = 0;
164 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
165 }
166
167 /* Evaluate an expression, avoiding all memory references
168 and getting a value whose type alone is correct. */
169
170 struct value *
171 evaluate_type (struct expression *exp)
172 {
173 int pc = 0;
174 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
175 }
176
177 /* If the next expression is an OP_LABELED, skips past it,
178 returning the label. Otherwise, does nothing and returns NULL. */
179
180 static char *
181 get_label (struct expression *exp, int *pos)
182 {
183 if (exp->elts[*pos].opcode == OP_LABELED)
184 {
185 int pc = (*pos)++;
186 char *name = &exp->elts[pc + 2].string;
187 int tem = longest_to_int (exp->elts[pc + 1].longconst);
188 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
189 return name;
190 }
191 else
192 return NULL;
193 }
194
195 /* This function evaluates tuples (in (the deleted) Chill) or
196 brace-initializers (in C/C++) for structure types. */
197
198 static struct value *
199 evaluate_struct_tuple (struct value *struct_val,
200 struct expression *exp,
201 int *pos, enum noside noside, int nargs)
202 {
203 struct type *struct_type = check_typedef (VALUE_TYPE (struct_val));
204 struct type *substruct_type = struct_type;
205 struct type *field_type;
206 int fieldno = -1;
207 int variantno = -1;
208 int subfieldno = -1;
209 while (--nargs >= 0)
210 {
211 int pc = *pos;
212 struct value *val = NULL;
213 int nlabels = 0;
214 int bitpos, bitsize;
215 char *addr;
216
217 /* Skip past the labels, and count them. */
218 while (get_label (exp, pos) != NULL)
219 nlabels++;
220
221 do
222 {
223 char *label = get_label (exp, &pc);
224 if (label)
225 {
226 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
227 fieldno++)
228 {
229 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
230 if (field_name != NULL && STREQ (field_name, label))
231 {
232 variantno = -1;
233 subfieldno = fieldno;
234 substruct_type = struct_type;
235 goto found;
236 }
237 }
238 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
239 fieldno++)
240 {
241 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
242 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
243 if ((field_name == 0 || *field_name == '\0')
244 && TYPE_CODE (field_type) == TYPE_CODE_UNION)
245 {
246 variantno = 0;
247 for (; variantno < TYPE_NFIELDS (field_type);
248 variantno++)
249 {
250 substruct_type
251 = TYPE_FIELD_TYPE (field_type, variantno);
252 if (TYPE_CODE (substruct_type) == TYPE_CODE_STRUCT)
253 {
254 for (subfieldno = 0;
255 subfieldno < TYPE_NFIELDS (substruct_type);
256 subfieldno++)
257 {
258 if (STREQ (TYPE_FIELD_NAME (substruct_type,
259 subfieldno),
260 label))
261 {
262 goto found;
263 }
264 }
265 }
266 }
267 }
268 }
269 error ("there is no field named %s", label);
270 found:
271 ;
272 }
273 else
274 {
275 /* Unlabelled tuple element - go to next field. */
276 if (variantno >= 0)
277 {
278 subfieldno++;
279 if (subfieldno >= TYPE_NFIELDS (substruct_type))
280 {
281 variantno = -1;
282 substruct_type = struct_type;
283 }
284 }
285 if (variantno < 0)
286 {
287 fieldno++;
288 subfieldno = fieldno;
289 if (fieldno >= TYPE_NFIELDS (struct_type))
290 error ("too many initializers");
291 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
292 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
293 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
294 error ("don't know which variant you want to set");
295 }
296 }
297
298 /* Here, struct_type is the type of the inner struct,
299 while substruct_type is the type of the inner struct.
300 These are the same for normal structures, but a variant struct
301 contains anonymous union fields that contain substruct fields.
302 The value fieldno is the index of the top-level (normal or
303 anonymous union) field in struct_field, while the value
304 subfieldno is the index of the actual real (named inner) field
305 in substruct_type. */
306
307 field_type = TYPE_FIELD_TYPE (substruct_type, subfieldno);
308 if (val == 0)
309 val = evaluate_subexp (field_type, exp, pos, noside);
310
311 /* Now actually set the field in struct_val. */
312
313 /* Assign val to field fieldno. */
314 if (VALUE_TYPE (val) != field_type)
315 val = value_cast (field_type, val);
316
317 bitsize = TYPE_FIELD_BITSIZE (substruct_type, subfieldno);
318 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
319 if (variantno >= 0)
320 bitpos += TYPE_FIELD_BITPOS (substruct_type, subfieldno);
321 addr = VALUE_CONTENTS (struct_val) + bitpos / 8;
322 if (bitsize)
323 modify_field (addr, value_as_long (val),
324 bitpos % 8, bitsize);
325 else
326 memcpy (addr, VALUE_CONTENTS (val),
327 TYPE_LENGTH (VALUE_TYPE (val)));
328 }
329 while (--nlabels > 0);
330 }
331 return struct_val;
332 }
333
334 /* Recursive helper function for setting elements of array tuples for
335 (the deleted) Chill. The target is ARRAY (which has bounds
336 LOW_BOUND to HIGH_BOUND); the element value is ELEMENT; EXP, POS
337 and NOSIDE are as usual. Evaluates index expresions and sets the
338 specified element(s) of ARRAY to ELEMENT. Returns last index
339 value. */
340
341 static LONGEST
342 init_array_element (struct value *array, struct value *element,
343 struct expression *exp, int *pos,
344 enum noside noside, LONGEST low_bound, LONGEST high_bound)
345 {
346 LONGEST index;
347 int element_size = TYPE_LENGTH (VALUE_TYPE (element));
348 if (exp->elts[*pos].opcode == BINOP_COMMA)
349 {
350 (*pos)++;
351 init_array_element (array, element, exp, pos, noside,
352 low_bound, high_bound);
353 return init_array_element (array, element,
354 exp, pos, noside, low_bound, high_bound);
355 }
356 else if (exp->elts[*pos].opcode == BINOP_RANGE)
357 {
358 LONGEST low, high;
359 (*pos)++;
360 low = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
361 high = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
362 if (low < low_bound || high > high_bound)
363 error ("tuple range index out of range");
364 for (index = low; index <= high; index++)
365 {
366 memcpy (VALUE_CONTENTS_RAW (array)
367 + (index - low_bound) * element_size,
368 VALUE_CONTENTS (element), element_size);
369 }
370 }
371 else
372 {
373 index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
374 if (index < low_bound || index > high_bound)
375 error ("tuple index out of range");
376 memcpy (VALUE_CONTENTS_RAW (array) + (index - low_bound) * element_size,
377 VALUE_CONTENTS (element), element_size);
378 }
379 return index;
380 }
381
382 struct value *
383 evaluate_subexp_standard (struct type *expect_type,
384 struct expression *exp, int *pos,
385 enum noside noside)
386 {
387 enum exp_opcode op;
388 int tem, tem2, tem3;
389 int pc, pc2 = 0, oldpos;
390 struct value *arg1 = NULL;
391 struct value *arg2 = NULL;
392 struct value *arg3;
393 struct type *type;
394 int nargs;
395 struct value **argvec;
396 int upper, lower, retcode;
397 int code;
398 int ix;
399 long mem_offset;
400 struct type **arg_types;
401 int save_pos1;
402
403 pc = (*pos)++;
404 op = exp->elts[pc].opcode;
405
406 switch (op)
407 {
408 case OP_SCOPE:
409 tem = longest_to_int (exp->elts[pc + 2].longconst);
410 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
411 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
412 0,
413 exp->elts[pc + 1].type,
414 &exp->elts[pc + 3].string,
415 NULL_TYPE);
416 if (arg1 == NULL)
417 error ("There is no field named %s", &exp->elts[pc + 3].string);
418 return arg1;
419
420 case OP_LONG:
421 (*pos) += 3;
422 return value_from_longest (exp->elts[pc + 1].type,
423 exp->elts[pc + 2].longconst);
424
425 case OP_DOUBLE:
426 (*pos) += 3;
427 return value_from_double (exp->elts[pc + 1].type,
428 exp->elts[pc + 2].doubleconst);
429
430 case OP_VAR_VALUE:
431 (*pos) += 3;
432 if (noside == EVAL_SKIP)
433 goto nosideret;
434
435 /* JYG: We used to just return value_zero of the symbol type
436 if we're asked to avoid side effects. Otherwise we return
437 value_of_variable (...). However I'm not sure if
438 value_of_variable () has any side effect.
439 We need a full value object returned here for whatis_exp ()
440 to call evaluate_type () and then pass the full value to
441 value_rtti_target_type () if we are dealing with a pointer
442 or reference to a base class and print object is on. */
443
444 return value_of_variable (exp->elts[pc + 2].symbol,
445 exp->elts[pc + 1].block);
446
447 case OP_LAST:
448 (*pos) += 2;
449 return
450 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
451
452 case OP_REGISTER:
453 {
454 int regno = longest_to_int (exp->elts[pc + 1].longconst);
455 struct value *val = value_of_register (regno, get_selected_frame ());
456 (*pos) += 2;
457 if (val == NULL)
458 error ("Value of register %s not available.",
459 frame_map_regnum_to_name (get_selected_frame (), regno));
460 else
461 return val;
462 }
463 case OP_BOOL:
464 (*pos) += 2;
465 return value_from_longest (LA_BOOL_TYPE,
466 exp->elts[pc + 1].longconst);
467
468 case OP_INTERNALVAR:
469 (*pos) += 2;
470 return value_of_internalvar (exp->elts[pc + 1].internalvar);
471
472 case OP_STRING:
473 tem = longest_to_int (exp->elts[pc + 1].longconst);
474 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
475 if (noside == EVAL_SKIP)
476 goto nosideret;
477 return value_string (&exp->elts[pc + 2].string, tem);
478
479 case OP_OBJC_NSSTRING: /* Objective C Foundation Class NSString constant. */
480 tem = longest_to_int (exp->elts[pc + 1].longconst);
481 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
482 if (noside == EVAL_SKIP)
483 {
484 goto nosideret;
485 }
486 return (struct value *) value_nsstring (&exp->elts[pc + 2].string, tem + 1);
487
488 case OP_BITSTRING:
489 tem = longest_to_int (exp->elts[pc + 1].longconst);
490 (*pos)
491 += 3 + BYTES_TO_EXP_ELEM ((tem + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
492 if (noside == EVAL_SKIP)
493 goto nosideret;
494 return value_bitstring (&exp->elts[pc + 2].string, tem);
495 break;
496
497 case OP_ARRAY:
498 (*pos) += 3;
499 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
500 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
501 nargs = tem3 - tem2 + 1;
502 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
503
504 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
505 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
506 {
507 struct value *rec = allocate_value (expect_type);
508 memset (VALUE_CONTENTS_RAW (rec), '\0', TYPE_LENGTH (type));
509 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
510 }
511
512 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
513 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
514 {
515 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
516 struct type *element_type = TYPE_TARGET_TYPE (type);
517 struct value *array = allocate_value (expect_type);
518 int element_size = TYPE_LENGTH (check_typedef (element_type));
519 LONGEST low_bound, high_bound, index;
520 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
521 {
522 low_bound = 0;
523 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
524 }
525 index = low_bound;
526 memset (VALUE_CONTENTS_RAW (array), 0, TYPE_LENGTH (expect_type));
527 for (tem = nargs; --nargs >= 0;)
528 {
529 struct value *element;
530 int index_pc = 0;
531 if (exp->elts[*pos].opcode == BINOP_RANGE)
532 {
533 index_pc = ++(*pos);
534 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
535 }
536 element = evaluate_subexp (element_type, exp, pos, noside);
537 if (VALUE_TYPE (element) != element_type)
538 element = value_cast (element_type, element);
539 if (index_pc)
540 {
541 int continue_pc = *pos;
542 *pos = index_pc;
543 index = init_array_element (array, element, exp, pos, noside,
544 low_bound, high_bound);
545 *pos = continue_pc;
546 }
547 else
548 {
549 if (index > high_bound)
550 /* to avoid memory corruption */
551 error ("Too many array elements");
552 memcpy (VALUE_CONTENTS_RAW (array)
553 + (index - low_bound) * element_size,
554 VALUE_CONTENTS (element),
555 element_size);
556 }
557 index++;
558 }
559 return array;
560 }
561
562 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
563 && TYPE_CODE (type) == TYPE_CODE_SET)
564 {
565 struct value *set = allocate_value (expect_type);
566 char *valaddr = VALUE_CONTENTS_RAW (set);
567 struct type *element_type = TYPE_INDEX_TYPE (type);
568 struct type *check_type = element_type;
569 LONGEST low_bound, high_bound;
570
571 /* get targettype of elementtype */
572 while (TYPE_CODE (check_type) == TYPE_CODE_RANGE ||
573 TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
574 check_type = TYPE_TARGET_TYPE (check_type);
575
576 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
577 error ("(power)set type with unknown size");
578 memset (valaddr, '\0', TYPE_LENGTH (type));
579 for (tem = 0; tem < nargs; tem++)
580 {
581 LONGEST range_low, range_high;
582 struct type *range_low_type, *range_high_type;
583 struct value *elem_val;
584 if (exp->elts[*pos].opcode == BINOP_RANGE)
585 {
586 (*pos)++;
587 elem_val = evaluate_subexp (element_type, exp, pos, noside);
588 range_low_type = VALUE_TYPE (elem_val);
589 range_low = value_as_long (elem_val);
590 elem_val = evaluate_subexp (element_type, exp, pos, noside);
591 range_high_type = VALUE_TYPE (elem_val);
592 range_high = value_as_long (elem_val);
593 }
594 else
595 {
596 elem_val = evaluate_subexp (element_type, exp, pos, noside);
597 range_low_type = range_high_type = VALUE_TYPE (elem_val);
598 range_low = range_high = value_as_long (elem_val);
599 }
600 /* check types of elements to avoid mixture of elements from
601 different types. Also check if type of element is "compatible"
602 with element type of powerset */
603 if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
604 range_low_type = TYPE_TARGET_TYPE (range_low_type);
605 if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
606 range_high_type = TYPE_TARGET_TYPE (range_high_type);
607 if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type)) ||
608 (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM &&
609 (range_low_type != range_high_type)))
610 /* different element modes */
611 error ("POWERSET tuple elements of different mode");
612 if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type)) ||
613 (TYPE_CODE (check_type) == TYPE_CODE_ENUM &&
614 range_low_type != check_type))
615 error ("incompatible POWERSET tuple elements");
616 if (range_low > range_high)
617 {
618 warning ("empty POWERSET tuple range");
619 continue;
620 }
621 if (range_low < low_bound || range_high > high_bound)
622 error ("POWERSET tuple element out of range");
623 range_low -= low_bound;
624 range_high -= low_bound;
625 for (; range_low <= range_high; range_low++)
626 {
627 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
628 if (BITS_BIG_ENDIAN)
629 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
630 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
631 |= 1 << bit_index;
632 }
633 }
634 return set;
635 }
636
637 argvec = (struct value **) alloca (sizeof (struct value *) * nargs);
638 for (tem = 0; tem < nargs; tem++)
639 {
640 /* Ensure that array expressions are coerced into pointer objects. */
641 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
642 }
643 if (noside == EVAL_SKIP)
644 goto nosideret;
645 return value_array (tem2, tem3, argvec);
646
647 case TERNOP_SLICE:
648 {
649 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
650 int lowbound
651 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
652 int upper
653 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
654 if (noside == EVAL_SKIP)
655 goto nosideret;
656 return value_slice (array, lowbound, upper - lowbound + 1);
657 }
658
659 case TERNOP_SLICE_COUNT:
660 {
661 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
662 int lowbound
663 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
664 int length
665 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
666 return value_slice (array, lowbound, length);
667 }
668
669 case TERNOP_COND:
670 /* Skip third and second args to evaluate the first one. */
671 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
672 if (value_logical_not (arg1))
673 {
674 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
675 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
676 }
677 else
678 {
679 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
680 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
681 return arg2;
682 }
683
684 case OP_OBJC_SELECTOR:
685 { /* Objective C @selector operator. */
686 char *sel = &exp->elts[pc + 2].string;
687 int len = longest_to_int (exp->elts[pc + 1].longconst);
688
689 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
690 if (noside == EVAL_SKIP)
691 goto nosideret;
692
693 if (sel[len] != 0)
694 sel[len] = 0; /* Make sure it's terminated. */
695 return value_from_longest (lookup_pointer_type (builtin_type_void),
696 lookup_child_selector (sel));
697 }
698
699 case OP_OBJC_MSGCALL:
700 { /* Objective C message (method) call. */
701
702 static unsigned long responds_selector = 0;
703 static unsigned long method_selector = 0;
704
705 unsigned long selector = 0;
706
707 int using_gcc = 0;
708 int struct_return = 0;
709 int sub_no_side = 0;
710
711 static struct value *msg_send = NULL;
712 static struct value *msg_send_stret = NULL;
713 static int gnu_runtime = 0;
714
715 struct value *target = NULL;
716 struct value *method = NULL;
717 struct value *called_method = NULL;
718
719 struct type *selector_type = NULL;
720
721 struct value *ret = NULL;
722 CORE_ADDR addr = 0;
723
724 selector = exp->elts[pc + 1].longconst;
725 nargs = exp->elts[pc + 2].longconst;
726 argvec = (struct value **) alloca (sizeof (struct value *)
727 * (nargs + 5));
728
729 (*pos) += 3;
730
731 selector_type = lookup_pointer_type (builtin_type_void);
732 if (noside == EVAL_AVOID_SIDE_EFFECTS)
733 sub_no_side = EVAL_NORMAL;
734 else
735 sub_no_side = noside;
736
737 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
738
739 if (value_as_long (target) == 0)
740 return value_from_longest (builtin_type_long, 0);
741
742 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0))
743 gnu_runtime = 1;
744
745 /* Find the method dispatch (Apple runtime) or method lookup
746 (GNU runtime) function for Objective-C. These will be used
747 to lookup the symbol information for the method. If we
748 can't find any symbol information, then we'll use these to
749 call the method, otherwise we can call the method
750 directly. The msg_send_stret function is used in the special
751 case of a method that returns a structure (Apple runtime
752 only). */
753 if (gnu_runtime)
754 {
755 msg_send = find_function_in_inferior ("objc_msg_lookup");
756 msg_send_stret = find_function_in_inferior ("objc_msg_lookup");
757 }
758 else
759 {
760 msg_send = find_function_in_inferior ("objc_msgSend");
761 /* Special dispatcher for methods returning structs */
762 msg_send_stret = find_function_in_inferior ("objc_msgSend_stret");
763 }
764
765 /* Verify the target object responds to this method. The
766 standard top-level 'Object' class uses a different name for
767 the verification method than the non-standard, but more
768 often used, 'NSObject' class. Make sure we check for both. */
769
770 responds_selector = lookup_child_selector ("respondsToSelector:");
771 if (responds_selector == 0)
772 responds_selector = lookup_child_selector ("respondsTo:");
773
774 if (responds_selector == 0)
775 error ("no 'respondsTo:' or 'respondsToSelector:' method");
776
777 method_selector = lookup_child_selector ("methodForSelector:");
778 if (method_selector == 0)
779 method_selector = lookup_child_selector ("methodFor:");
780
781 if (method_selector == 0)
782 error ("no 'methodFor:' or 'methodForSelector:' method");
783
784 /* Call the verification method, to make sure that the target
785 class implements the desired method. */
786
787 argvec[0] = msg_send;
788 argvec[1] = target;
789 argvec[2] = value_from_longest (builtin_type_long, responds_selector);
790 argvec[3] = value_from_longest (builtin_type_long, selector);
791 argvec[4] = 0;
792
793 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
794 if (gnu_runtime)
795 {
796 /* Function objc_msg_lookup returns a pointer. */
797 argvec[0] = ret;
798 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
799 }
800 if (value_as_long (ret) == 0)
801 error ("Target does not respond to this message selector.");
802
803 /* Call "methodForSelector:" method, to get the address of a
804 function method that implements this selector for this
805 class. If we can find a symbol at that address, then we
806 know the return type, parameter types etc. (that's a good
807 thing). */
808
809 argvec[0] = msg_send;
810 argvec[1] = target;
811 argvec[2] = value_from_longest (builtin_type_long, method_selector);
812 argvec[3] = value_from_longest (builtin_type_long, selector);
813 argvec[4] = 0;
814
815 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
816 if (gnu_runtime)
817 {
818 argvec[0] = ret;
819 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
820 }
821
822 /* ret should now be the selector. */
823
824 addr = value_as_long (ret);
825 if (addr)
826 {
827 struct symbol *sym = NULL;
828 /* Is it a high_level symbol? */
829
830 sym = find_pc_function (addr);
831 if (sym != NULL)
832 method = value_of_variable (sym, 0);
833 }
834
835 /* If we found a method with symbol information, check to see
836 if it returns a struct. Otherwise assume it doesn't. */
837
838 if (method)
839 {
840 struct block *b;
841 CORE_ADDR funaddr;
842 struct type *value_type;
843
844 funaddr = find_function_addr (method, &value_type);
845
846 b = block_for_pc (funaddr);
847
848 /* If compiled without -g, assume GCC 2. */
849 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
850
851 CHECK_TYPEDEF (value_type);
852
853 if ((value_type == NULL)
854 || (TYPE_CODE(value_type) == TYPE_CODE_ERROR))
855 {
856 if (expect_type != NULL)
857 value_type = expect_type;
858 }
859
860 struct_return = using_struct_return (value_type, using_gcc);
861 }
862 else if (expect_type != NULL)
863 {
864 struct_return = using_struct_return (check_typedef (expect_type), using_gcc);
865 }
866
867 /* Found a function symbol. Now we will substitute its
868 value in place of the message dispatcher (obj_msgSend),
869 so that we call the method directly instead of thru
870 the dispatcher. The main reason for doing this is that
871 we can now evaluate the return value and parameter values
872 according to their known data types, in case we need to
873 do things like promotion, dereferencing, special handling
874 of structs and doubles, etc.
875
876 We want to use the type signature of 'method', but still
877 jump to objc_msgSend() or objc_msgSend_stret() to better
878 mimic the behavior of the runtime. */
879
880 if (method)
881 {
882 if (TYPE_CODE (VALUE_TYPE (method)) != TYPE_CODE_FUNC)
883 error ("method address has symbol information with non-function type; skipping");
884 if (struct_return)
885 VALUE_ADDRESS (method) = value_as_address (msg_send_stret);
886 else
887 VALUE_ADDRESS (method) = value_as_address (msg_send);
888 called_method = method;
889 }
890 else
891 {
892 if (struct_return)
893 called_method = msg_send_stret;
894 else
895 called_method = msg_send;
896 }
897
898 if (noside == EVAL_SKIP)
899 goto nosideret;
900
901 if (noside == EVAL_AVOID_SIDE_EFFECTS)
902 {
903 /* If the return type doesn't look like a function type,
904 call an error. This can happen if somebody tries to
905 turn a variable into a function call. This is here
906 because people often want to call, eg, strcmp, which
907 gdb doesn't know is a function. If gdb isn't asked for
908 it's opinion (ie. through "whatis"), it won't offer
909 it. */
910
911 struct type *type = VALUE_TYPE (called_method);
912 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
913 type = TYPE_TARGET_TYPE (type);
914 type = TYPE_TARGET_TYPE (type);
915
916 if (type)
917 {
918 if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type)
919 return allocate_value (expect_type);
920 else
921 return allocate_value (type);
922 }
923 else
924 error ("Expression of type other than \"method returning ...\" used as a method");
925 }
926
927 /* Now depending on whether we found a symbol for the method,
928 we will either call the runtime dispatcher or the method
929 directly. */
930
931 argvec[0] = called_method;
932 argvec[1] = target;
933 argvec[2] = value_from_longest (builtin_type_long, selector);
934 /* User-supplied arguments. */
935 for (tem = 0; tem < nargs; tem++)
936 argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
937 argvec[tem + 3] = 0;
938
939 if (gnu_runtime && (method != NULL))
940 {
941 ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
942 /* Function objc_msg_lookup returns a pointer. */
943 argvec[0] = ret;
944 ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
945 }
946 else
947 ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
948
949 return ret;
950 }
951 break;
952
953 case OP_FUNCALL:
954 (*pos) += 2;
955 op = exp->elts[*pos].opcode;
956 nargs = longest_to_int (exp->elts[pc + 1].longconst);
957 /* Allocate arg vector, including space for the function to be
958 called in argvec[0] and a terminating NULL */
959 argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 3));
960 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
961 {
962 LONGEST fnptr;
963
964 /* 1997-08-01 Currently we do not support function invocation
965 via pointers-to-methods with HP aCC. Pointer does not point
966 to the function, but possibly to some thunk. */
967 if (hp_som_som_object_present)
968 {
969 error ("Not implemented: function invocation through pointer to method with HP aCC");
970 }
971
972 nargs++;
973 /* First, evaluate the structure into arg2 */
974 pc2 = (*pos)++;
975
976 if (noside == EVAL_SKIP)
977 goto nosideret;
978
979 if (op == STRUCTOP_MEMBER)
980 {
981 arg2 = evaluate_subexp_for_address (exp, pos, noside);
982 }
983 else
984 {
985 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
986 }
987
988 /* If the function is a virtual function, then the
989 aggregate value (providing the structure) plays
990 its part by providing the vtable. Otherwise,
991 it is just along for the ride: call the function
992 directly. */
993
994 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
995
996 fnptr = value_as_long (arg1);
997
998 if (METHOD_PTR_IS_VIRTUAL (fnptr))
999 {
1000 int fnoffset = METHOD_PTR_TO_VOFFSET (fnptr);
1001 struct type *basetype;
1002 struct type *domain_type =
1003 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
1004 int i, j;
1005 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
1006 if (domain_type != basetype)
1007 arg2 = value_cast (lookup_pointer_type (domain_type), arg2);
1008 basetype = TYPE_VPTR_BASETYPE (domain_type);
1009 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
1010 {
1011 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
1012 /* If one is virtual, then all are virtual. */
1013 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
1014 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
1015 if ((int) TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
1016 {
1017 struct value *temp = value_ind (arg2);
1018 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
1019 arg2 = value_addr (temp);
1020 goto got_it;
1021 }
1022 }
1023 if (i < 0)
1024 error ("virtual function at index %d not found", fnoffset);
1025 }
1026 else
1027 {
1028 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
1029 }
1030 got_it:
1031
1032 /* Now, say which argument to start evaluating from */
1033 tem = 2;
1034 }
1035 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
1036 {
1037 /* Hair for method invocations */
1038 int tem2;
1039
1040 nargs++;
1041 /* First, evaluate the structure into arg2 */
1042 pc2 = (*pos)++;
1043 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
1044 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
1045 if (noside == EVAL_SKIP)
1046 goto nosideret;
1047
1048 if (op == STRUCTOP_STRUCT)
1049 {
1050 /* If v is a variable in a register, and the user types
1051 v.method (), this will produce an error, because v has
1052 no address.
1053
1054 A possible way around this would be to allocate a
1055 copy of the variable on the stack, copy in the
1056 contents, call the function, and copy out the
1057 contents. I.e. convert this from call by reference
1058 to call by copy-return (or whatever it's called).
1059 However, this does not work because it is not the
1060 same: the method being called could stash a copy of
1061 the address, and then future uses through that address
1062 (after the method returns) would be expected to
1063 use the variable itself, not some copy of it. */
1064 arg2 = evaluate_subexp_for_address (exp, pos, noside);
1065 }
1066 else
1067 {
1068 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1069 }
1070 /* Now, say which argument to start evaluating from */
1071 tem = 2;
1072 }
1073 else
1074 {
1075 /* Non-method function call */
1076 save_pos1 = *pos;
1077 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
1078 tem = 1;
1079 type = VALUE_TYPE (argvec[0]);
1080 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1081 type = TYPE_TARGET_TYPE (type);
1082 if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
1083 {
1084 for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
1085 {
1086 /* pai: FIXME This seems to be coercing arguments before
1087 * overload resolution has been done! */
1088 argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type, tem - 1),
1089 exp, pos, noside);
1090 }
1091 }
1092 }
1093
1094 /* Evaluate arguments */
1095 for (; tem <= nargs; tem++)
1096 {
1097 /* Ensure that array expressions are coerced into pointer objects. */
1098 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1099 }
1100
1101 /* signal end of arglist */
1102 argvec[tem] = 0;
1103
1104 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
1105 {
1106 int static_memfuncp;
1107 char tstr[256];
1108
1109 /* Method invocation : stuff "this" as first parameter */
1110 argvec[1] = arg2;
1111 /* Name of method from expression */
1112 strcpy (tstr, &exp->elts[pc2 + 2].string);
1113
1114 if (overload_resolution && (exp->language_defn->la_language == language_cplus))
1115 {
1116 /* Language is C++, do some overload resolution before evaluation */
1117 struct value *valp = NULL;
1118
1119 /* Prepare list of argument types for overload resolution */
1120 arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
1121 for (ix = 1; ix <= nargs; ix++)
1122 arg_types[ix - 1] = VALUE_TYPE (argvec[ix]);
1123
1124 (void) find_overload_match (arg_types, nargs, tstr,
1125 1 /* method */ , 0 /* strict match */ ,
1126 &arg2 /* the object */ , NULL,
1127 &valp, NULL, &static_memfuncp);
1128
1129
1130 argvec[1] = arg2; /* the ``this'' pointer */
1131 argvec[0] = valp; /* use the method found after overload resolution */
1132 }
1133 else
1134 /* Non-C++ case -- or no overload resolution */
1135 {
1136 struct value *temp = arg2;
1137 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1138 &static_memfuncp,
1139 op == STRUCTOP_STRUCT
1140 ? "structure" : "structure pointer");
1141 /* value_struct_elt updates temp with the correct value
1142 of the ``this'' pointer if necessary, so modify argvec[1] to
1143 reflect any ``this'' changes. */
1144 arg2 = value_from_longest (lookup_pointer_type(VALUE_TYPE (temp)),
1145 VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
1146 + VALUE_EMBEDDED_OFFSET (temp));
1147 argvec[1] = arg2; /* the ``this'' pointer */
1148 }
1149
1150 if (static_memfuncp)
1151 {
1152 argvec[1] = argvec[0];
1153 nargs--;
1154 argvec++;
1155 }
1156 }
1157 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1158 {
1159 argvec[1] = arg2;
1160 argvec[0] = arg1;
1161 }
1162 else if (op == OP_VAR_VALUE)
1163 {
1164 /* Non-member function being called */
1165 /* fn: This can only be done for C++ functions. A C-style function
1166 in a C++ program, for instance, does not have the fields that
1167 are expected here */
1168
1169 if (overload_resolution && (exp->language_defn->la_language == language_cplus))
1170 {
1171 /* Language is C++, do some overload resolution before evaluation */
1172 struct symbol *symp;
1173
1174 /* Prepare list of argument types for overload resolution */
1175 arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
1176 for (ix = 1; ix <= nargs; ix++)
1177 arg_types[ix - 1] = VALUE_TYPE (argvec[ix]);
1178
1179 (void) find_overload_match (arg_types, nargs, NULL /* no need for name */ ,
1180 0 /* not method */ , 0 /* strict match */ ,
1181 NULL, exp->elts[save_pos1+2].symbol /* the function */ ,
1182 NULL, &symp, NULL);
1183
1184 /* Now fix the expression being evaluated */
1185 exp->elts[save_pos1+2].symbol = symp;
1186 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1187 }
1188 else
1189 {
1190 /* Not C++, or no overload resolution allowed */
1191 /* nothing to be done; argvec already correctly set up */
1192 }
1193 }
1194 else
1195 {
1196 /* It is probably a C-style function */
1197 /* nothing to be done; argvec already correctly set up */
1198 }
1199
1200 do_call_it:
1201
1202 if (noside == EVAL_SKIP)
1203 goto nosideret;
1204 if (argvec[0] == NULL)
1205 error ("Cannot evaluate function -- may be inlined");
1206 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1207 {
1208 /* If the return type doesn't look like a function type, call an
1209 error. This can happen if somebody tries to turn a variable into
1210 a function call. This is here because people often want to
1211 call, eg, strcmp, which gdb doesn't know is a function. If
1212 gdb isn't asked for it's opinion (ie. through "whatis"),
1213 it won't offer it. */
1214
1215 struct type *ftype =
1216 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
1217
1218 if (ftype)
1219 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
1220 else
1221 error ("Expression of type other than \"Function returning ...\" used as function");
1222 }
1223 return call_function_by_hand (argvec[0], nargs, argvec + 1);
1224 /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */
1225
1226 case OP_F77_UNDETERMINED_ARGLIST:
1227
1228 /* Remember that in F77, functions, substring ops and
1229 array subscript operations cannot be disambiguated
1230 at parse time. We have made all array subscript operations,
1231 substring operations as well as function calls come here
1232 and we now have to discover what the heck this thing actually was.
1233 If it is a function, we process just as if we got an OP_FUNCALL. */
1234
1235 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1236 (*pos) += 2;
1237
1238 /* First determine the type code we are dealing with. */
1239 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1240 type = check_typedef (VALUE_TYPE (arg1));
1241 code = TYPE_CODE (type);
1242
1243 switch (code)
1244 {
1245 case TYPE_CODE_ARRAY:
1246 goto multi_f77_subscript;
1247
1248 case TYPE_CODE_STRING:
1249 goto op_f77_substr;
1250
1251 case TYPE_CODE_PTR:
1252 case TYPE_CODE_FUNC:
1253 /* It's a function call. */
1254 /* Allocate arg vector, including space for the function to be
1255 called in argvec[0] and a terminating NULL */
1256 argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
1257 argvec[0] = arg1;
1258 tem = 1;
1259 for (; tem <= nargs; tem++)
1260 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1261 argvec[tem] = 0; /* signal end of arglist */
1262 goto do_call_it;
1263
1264 default:
1265 error ("Cannot perform substring on this type");
1266 }
1267
1268 op_f77_substr:
1269 /* We have a substring operation on our hands here,
1270 let us get the string we will be dealing with */
1271
1272 /* Now evaluate the 'from' and 'to' */
1273
1274 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1275
1276 if (nargs < 2)
1277 return value_subscript (arg1, arg2);
1278
1279 arg3 = evaluate_subexp_with_coercion (exp, pos, noside);
1280
1281 if (noside == EVAL_SKIP)
1282 goto nosideret;
1283
1284 tem2 = value_as_long (arg2);
1285 tem3 = value_as_long (arg3);
1286
1287 return value_slice (arg1, tem2, tem3 - tem2 + 1);
1288
1289 case OP_COMPLEX:
1290 /* We have a complex number, There should be 2 floating
1291 point numbers that compose it */
1292 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1293 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1294
1295 return value_literal_complex (arg1, arg2, builtin_type_f_complex_s16);
1296
1297 case STRUCTOP_STRUCT:
1298 tem = longest_to_int (exp->elts[pc + 1].longconst);
1299 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1300 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1301 if (noside == EVAL_SKIP)
1302 goto nosideret;
1303 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1304 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
1305 &exp->elts[pc + 2].string,
1306 0),
1307 lval_memory);
1308 else
1309 {
1310 struct value *temp = arg1;
1311 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
1312 NULL, "structure");
1313 }
1314
1315 case STRUCTOP_PTR:
1316 tem = longest_to_int (exp->elts[pc + 1].longconst);
1317 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1318 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1319 if (noside == EVAL_SKIP)
1320 goto nosideret;
1321
1322 /* JYG: if print object is on we need to replace the base type
1323 with rtti type in order to continue on with successful
1324 lookup of member / method only available in the rtti type. */
1325 {
1326 struct type *type = VALUE_TYPE (arg1);
1327 struct type *real_type;
1328 int full, top, using_enc;
1329
1330 if (objectprint && TYPE_TARGET_TYPE(type) &&
1331 (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
1332 {
1333 real_type = value_rtti_target_type (arg1, &full, &top, &using_enc);
1334 if (real_type)
1335 {
1336 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1337 real_type = lookup_pointer_type (real_type);
1338 else
1339 real_type = lookup_reference_type (real_type);
1340
1341 arg1 = value_cast (real_type, arg1);
1342 }
1343 }
1344 }
1345
1346 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1347 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
1348 &exp->elts[pc + 2].string,
1349 0),
1350 lval_memory);
1351 else
1352 {
1353 struct value *temp = arg1;
1354 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
1355 NULL, "structure pointer");
1356 }
1357
1358 case STRUCTOP_MEMBER:
1359 arg1 = evaluate_subexp_for_address (exp, pos, noside);
1360 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1361
1362 /* With HP aCC, pointers to methods do not point to the function code */
1363 if (hp_som_som_object_present &&
1364 (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) &&
1365 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_METHOD))
1366 error ("Pointers to methods not supported with HP aCC"); /* 1997-08-19 */
1367
1368 mem_offset = value_as_long (arg2);
1369 goto handle_pointer_to_member;
1370
1371 case STRUCTOP_MPTR:
1372 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1373 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1374
1375 /* With HP aCC, pointers to methods do not point to the function code */
1376 if (hp_som_som_object_present &&
1377 (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) &&
1378 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_METHOD))
1379 error ("Pointers to methods not supported with HP aCC"); /* 1997-08-19 */
1380
1381 mem_offset = value_as_long (arg2);
1382
1383 handle_pointer_to_member:
1384 /* HP aCC generates offsets that have bit #29 set; turn it off to get
1385 a real offset to the member. */
1386 if (hp_som_som_object_present)
1387 {
1388 if (!mem_offset) /* no bias -> really null */
1389 error ("Attempted dereference of null pointer-to-member");
1390 mem_offset &= ~0x20000000;
1391 }
1392 if (noside == EVAL_SKIP)
1393 goto nosideret;
1394 type = check_typedef (VALUE_TYPE (arg2));
1395 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1396 goto bad_pointer_to_member;
1397 type = check_typedef (TYPE_TARGET_TYPE (type));
1398 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
1399 error ("not implemented: pointer-to-method in pointer-to-member construct");
1400 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
1401 goto bad_pointer_to_member;
1402 /* Now, convert these values to an address. */
1403 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
1404 arg1);
1405 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1406 value_as_long (arg1) + mem_offset);
1407 return value_ind (arg3);
1408 bad_pointer_to_member:
1409 error ("non-pointer-to-member value used in pointer-to-member construct");
1410
1411 case BINOP_CONCAT:
1412 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1413 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1414 if (noside == EVAL_SKIP)
1415 goto nosideret;
1416 if (binop_user_defined_p (op, arg1, arg2))
1417 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1418 else
1419 return value_concat (arg1, arg2);
1420
1421 case BINOP_ASSIGN:
1422 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1423 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1424
1425 /* Do special stuff for HP aCC pointers to members */
1426 if (hp_som_som_object_present)
1427 {
1428 /* 1997-08-19 Can't assign HP aCC pointers to methods. No details of
1429 the implementation yet; but the pointer appears to point to a code
1430 sequence (thunk) in memory -- in any case it is *not* the address
1431 of the function as it would be in a naive implementation. */
1432 if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) &&
1433 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_METHOD))
1434 error ("Assignment to pointers to methods not implemented with HP aCC");
1435
1436 /* HP aCC pointers to data members require a constant bias */
1437 if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) &&
1438 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_MEMBER))
1439 {
1440 unsigned int *ptr = (unsigned int *) VALUE_CONTENTS (arg2); /* forces evaluation */
1441 *ptr |= 0x20000000; /* set 29th bit */
1442 }
1443 }
1444
1445 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1446 return arg1;
1447 if (binop_user_defined_p (op, arg1, arg2))
1448 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1449 else
1450 return value_assign (arg1, arg2);
1451
1452 case BINOP_ASSIGN_MODIFY:
1453 (*pos) += 2;
1454 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1455 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1456 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1457 return arg1;
1458 op = exp->elts[pc + 1].opcode;
1459 if (binop_user_defined_p (op, arg1, arg2))
1460 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1461 else if (op == BINOP_ADD)
1462 arg2 = value_add (arg1, arg2);
1463 else if (op == BINOP_SUB)
1464 arg2 = value_sub (arg1, arg2);
1465 else
1466 arg2 = value_binop (arg1, arg2, op);
1467 return value_assign (arg1, arg2);
1468
1469 case BINOP_ADD:
1470 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1471 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1472 if (noside == EVAL_SKIP)
1473 goto nosideret;
1474 if (binop_user_defined_p (op, arg1, arg2))
1475 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1476 else
1477 return value_add (arg1, arg2);
1478
1479 case BINOP_SUB:
1480 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1481 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1482 if (noside == EVAL_SKIP)
1483 goto nosideret;
1484 if (binop_user_defined_p (op, arg1, arg2))
1485 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1486 else
1487 return value_sub (arg1, arg2);
1488
1489 case BINOP_MUL:
1490 case BINOP_DIV:
1491 case BINOP_REM:
1492 case BINOP_MOD:
1493 case BINOP_LSH:
1494 case BINOP_RSH:
1495 case BINOP_BITWISE_AND:
1496 case BINOP_BITWISE_IOR:
1497 case BINOP_BITWISE_XOR:
1498 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1499 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1500 if (noside == EVAL_SKIP)
1501 goto nosideret;
1502 if (binop_user_defined_p (op, arg1, arg2))
1503 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1504 else if (noside == EVAL_AVOID_SIDE_EFFECTS
1505 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
1506 return value_zero (VALUE_TYPE (arg1), not_lval);
1507 else
1508 return value_binop (arg1, arg2, op);
1509
1510 case BINOP_RANGE:
1511 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1512 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1513 if (noside == EVAL_SKIP)
1514 goto nosideret;
1515 error ("':' operator used in invalid context");
1516
1517 case BINOP_SUBSCRIPT:
1518 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1519 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1520 if (noside == EVAL_SKIP)
1521 goto nosideret;
1522 if (binop_user_defined_p (op, arg1, arg2))
1523 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1524 else
1525 {
1526 /* If the user attempts to subscript something that is not an
1527 array or pointer type (like a plain int variable for example),
1528 then report this as an error. */
1529
1530 COERCE_REF (arg1);
1531 type = check_typedef (VALUE_TYPE (arg1));
1532 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
1533 && TYPE_CODE (type) != TYPE_CODE_PTR)
1534 {
1535 if (TYPE_NAME (type))
1536 error ("cannot subscript something of type `%s'",
1537 TYPE_NAME (type));
1538 else
1539 error ("cannot subscript requested type");
1540 }
1541
1542 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1543 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
1544 else
1545 return value_subscript (arg1, arg2);
1546 }
1547
1548 case BINOP_IN:
1549 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1550 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1551 if (noside == EVAL_SKIP)
1552 goto nosideret;
1553 return value_in (arg1, arg2);
1554
1555 case MULTI_SUBSCRIPT:
1556 (*pos) += 2;
1557 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1558 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1559 while (nargs-- > 0)
1560 {
1561 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1562 /* FIXME: EVAL_SKIP handling may not be correct. */
1563 if (noside == EVAL_SKIP)
1564 {
1565 if (nargs > 0)
1566 {
1567 continue;
1568 }
1569 else
1570 {
1571 goto nosideret;
1572 }
1573 }
1574 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
1575 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1576 {
1577 /* If the user attempts to subscript something that has no target
1578 type (like a plain int variable for example), then report this
1579 as an error. */
1580
1581 type = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (arg1)));
1582 if (type != NULL)
1583 {
1584 arg1 = value_zero (type, VALUE_LVAL (arg1));
1585 noside = EVAL_SKIP;
1586 continue;
1587 }
1588 else
1589 {
1590 error ("cannot subscript something of type `%s'",
1591 TYPE_NAME (VALUE_TYPE (arg1)));
1592 }
1593 }
1594
1595 if (binop_user_defined_p (op, arg1, arg2))
1596 {
1597 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
1598 }
1599 else
1600 {
1601 arg1 = value_subscript (arg1, arg2);
1602 }
1603 }
1604 return (arg1);
1605
1606 multi_f77_subscript:
1607 {
1608 int subscript_array[MAX_FORTRAN_DIMS + 1]; /* 1-based array of
1609 subscripts, max == 7 */
1610 int array_size_array[MAX_FORTRAN_DIMS + 1];
1611 int ndimensions = 1, i;
1612 struct type *tmp_type;
1613 int offset_item; /* The array offset where the item lives */
1614
1615 if (nargs > MAX_FORTRAN_DIMS)
1616 error ("Too many subscripts for F77 (%d Max)", MAX_FORTRAN_DIMS);
1617
1618 tmp_type = check_typedef (VALUE_TYPE (arg1));
1619 ndimensions = calc_f77_array_dims (type);
1620
1621 if (nargs != ndimensions)
1622 error ("Wrong number of subscripts");
1623
1624 /* Now that we know we have a legal array subscript expression
1625 let us actually find out where this element exists in the array. */
1626
1627 offset_item = 0;
1628 for (i = 1; i <= nargs; i++)
1629 {
1630 /* Evaluate each subscript, It must be a legal integer in F77 */
1631 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1632
1633 /* Fill in the subscript and array size arrays */
1634
1635 subscript_array[i] = value_as_long (arg2);
1636
1637 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
1638 if (retcode == BOUND_FETCH_ERROR)
1639 error ("Cannot obtain dynamic upper bound");
1640
1641 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
1642 if (retcode == BOUND_FETCH_ERROR)
1643 error ("Cannot obtain dynamic lower bound");
1644
1645 array_size_array[i] = upper - lower + 1;
1646
1647 /* Zero-normalize subscripts so that offsetting will work. */
1648
1649 subscript_array[i] -= lower;
1650
1651 /* If we are at the bottom of a multidimensional
1652 array type then keep a ptr to the last ARRAY
1653 type around for use when calling value_subscript()
1654 below. This is done because we pretend to value_subscript
1655 that we actually have a one-dimensional array
1656 of base element type that we apply a simple
1657 offset to. */
1658
1659 if (i < nargs)
1660 tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
1661 }
1662
1663 /* Now let us calculate the offset for this item */
1664
1665 offset_item = subscript_array[ndimensions];
1666
1667 for (i = ndimensions - 1; i >= 1; i--)
1668 offset_item =
1669 array_size_array[i] * offset_item + subscript_array[i];
1670
1671 /* Construct a value node with the value of the offset */
1672
1673 arg2 = value_from_longest (builtin_type_f_integer, offset_item);
1674
1675 /* Let us now play a dirty trick: we will take arg1
1676 which is a value node pointing to the topmost level
1677 of the multidimensional array-set and pretend
1678 that it is actually a array of the final element
1679 type, this will ensure that value_subscript()
1680 returns the correct type value */
1681
1682 VALUE_TYPE (arg1) = tmp_type;
1683 return value_ind (value_add (value_coerce_array (arg1), arg2));
1684 }
1685
1686 case BINOP_LOGICAL_AND:
1687 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1688 if (noside == EVAL_SKIP)
1689 {
1690 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1691 goto nosideret;
1692 }
1693
1694 oldpos = *pos;
1695 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1696 *pos = oldpos;
1697
1698 if (binop_user_defined_p (op, arg1, arg2))
1699 {
1700 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1701 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1702 }
1703 else
1704 {
1705 tem = value_logical_not (arg1);
1706 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1707 (tem ? EVAL_SKIP : noside));
1708 return value_from_longest (LA_BOOL_TYPE,
1709 (LONGEST) (!tem && !value_logical_not (arg2)));
1710 }
1711
1712 case BINOP_LOGICAL_OR:
1713 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1714 if (noside == EVAL_SKIP)
1715 {
1716 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1717 goto nosideret;
1718 }
1719
1720 oldpos = *pos;
1721 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1722 *pos = oldpos;
1723
1724 if (binop_user_defined_p (op, arg1, arg2))
1725 {
1726 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1727 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1728 }
1729 else
1730 {
1731 tem = value_logical_not (arg1);
1732 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1733 (!tem ? EVAL_SKIP : noside));
1734 return value_from_longest (LA_BOOL_TYPE,
1735 (LONGEST) (!tem || !value_logical_not (arg2)));
1736 }
1737
1738 case BINOP_EQUAL:
1739 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1740 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1741 if (noside == EVAL_SKIP)
1742 goto nosideret;
1743 if (binop_user_defined_p (op, arg1, arg2))
1744 {
1745 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1746 }
1747 else
1748 {
1749 tem = value_equal (arg1, arg2);
1750 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1751 }
1752
1753 case BINOP_NOTEQUAL:
1754 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1755 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1756 if (noside == EVAL_SKIP)
1757 goto nosideret;
1758 if (binop_user_defined_p (op, arg1, arg2))
1759 {
1760 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1761 }
1762 else
1763 {
1764 tem = value_equal (arg1, arg2);
1765 return value_from_longest (LA_BOOL_TYPE, (LONGEST) ! tem);
1766 }
1767
1768 case BINOP_LESS:
1769 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1770 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1771 if (noside == EVAL_SKIP)
1772 goto nosideret;
1773 if (binop_user_defined_p (op, arg1, arg2))
1774 {
1775 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1776 }
1777 else
1778 {
1779 tem = value_less (arg1, arg2);
1780 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1781 }
1782
1783 case BINOP_GTR:
1784 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1785 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1786 if (noside == EVAL_SKIP)
1787 goto nosideret;
1788 if (binop_user_defined_p (op, arg1, arg2))
1789 {
1790 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1791 }
1792 else
1793 {
1794 tem = value_less (arg2, arg1);
1795 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1796 }
1797
1798 case BINOP_GEQ:
1799 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1800 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1801 if (noside == EVAL_SKIP)
1802 goto nosideret;
1803 if (binop_user_defined_p (op, arg1, arg2))
1804 {
1805 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1806 }
1807 else
1808 {
1809 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1810 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1811 }
1812
1813 case BINOP_LEQ:
1814 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1815 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1816 if (noside == EVAL_SKIP)
1817 goto nosideret;
1818 if (binop_user_defined_p (op, arg1, arg2))
1819 {
1820 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1821 }
1822 else
1823 {
1824 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1825 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1826 }
1827
1828 case BINOP_REPEAT:
1829 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1830 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1831 if (noside == EVAL_SKIP)
1832 goto nosideret;
1833 type = check_typedef (VALUE_TYPE (arg2));
1834 if (TYPE_CODE (type) != TYPE_CODE_INT)
1835 error ("Non-integral right operand for \"@\" operator.");
1836 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1837 {
1838 return allocate_repeat_value (VALUE_TYPE (arg1),
1839 longest_to_int (value_as_long (arg2)));
1840 }
1841 else
1842 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1843
1844 case BINOP_COMMA:
1845 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1846 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1847
1848 case UNOP_NEG:
1849 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1850 if (noside == EVAL_SKIP)
1851 goto nosideret;
1852 if (unop_user_defined_p (op, arg1))
1853 return value_x_unop (arg1, op, noside);
1854 else
1855 return value_neg (arg1);
1856
1857 case UNOP_COMPLEMENT:
1858 /* C++: check for and handle destructor names. */
1859 op = exp->elts[*pos].opcode;
1860
1861 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1862 if (noside == EVAL_SKIP)
1863 goto nosideret;
1864 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1865 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1866 else
1867 return value_complement (arg1);
1868
1869 case UNOP_LOGICAL_NOT:
1870 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1871 if (noside == EVAL_SKIP)
1872 goto nosideret;
1873 if (unop_user_defined_p (op, arg1))
1874 return value_x_unop (arg1, op, noside);
1875 else
1876 return value_from_longest (LA_BOOL_TYPE,
1877 (LONGEST) value_logical_not (arg1));
1878
1879 case UNOP_IND:
1880 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
1881 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
1882 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1883 if ((TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) &&
1884 ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_METHOD) ||
1885 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_MEMBER)))
1886 error ("Attempt to dereference pointer to member without an object");
1887 if (noside == EVAL_SKIP)
1888 goto nosideret;
1889 if (unop_user_defined_p (op, arg1))
1890 return value_x_unop (arg1, op, noside);
1891 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1892 {
1893 type = check_typedef (VALUE_TYPE (arg1));
1894 if (TYPE_CODE (type) == TYPE_CODE_PTR
1895 || TYPE_CODE (type) == TYPE_CODE_REF
1896 /* In C you can dereference an array to get the 1st elt. */
1897 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1898 )
1899 return value_zero (TYPE_TARGET_TYPE (type),
1900 lval_memory);
1901 else if (TYPE_CODE (type) == TYPE_CODE_INT)
1902 /* GDB allows dereferencing an int. */
1903 return value_zero (builtin_type_int, lval_memory);
1904 else
1905 error ("Attempt to take contents of a non-pointer value.");
1906 }
1907 return value_ind (arg1);
1908
1909 case UNOP_ADDR:
1910 /* C++: check for and handle pointer to members. */
1911
1912 op = exp->elts[*pos].opcode;
1913
1914 if (noside == EVAL_SKIP)
1915 {
1916 if (op == OP_SCOPE)
1917 {
1918 int temm = longest_to_int (exp->elts[pc + 3].longconst);
1919 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
1920 }
1921 else
1922 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1923 goto nosideret;
1924 }
1925 else
1926 {
1927 struct value *retvalp = evaluate_subexp_for_address (exp, pos, noside);
1928 /* If HP aCC object, use bias for pointers to members */
1929 if (hp_som_som_object_present &&
1930 (TYPE_CODE (VALUE_TYPE (retvalp)) == TYPE_CODE_PTR) &&
1931 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (retvalp))) == TYPE_CODE_MEMBER))
1932 {
1933 unsigned int *ptr = (unsigned int *) VALUE_CONTENTS (retvalp); /* forces evaluation */
1934 *ptr |= 0x20000000; /* set 29th bit */
1935 }
1936 return retvalp;
1937 }
1938
1939 case UNOP_SIZEOF:
1940 if (noside == EVAL_SKIP)
1941 {
1942 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1943 goto nosideret;
1944 }
1945 return evaluate_subexp_for_sizeof (exp, pos);
1946
1947 case UNOP_CAST:
1948 (*pos) += 2;
1949 type = exp->elts[pc + 1].type;
1950 arg1 = evaluate_subexp (type, exp, pos, noside);
1951 if (noside == EVAL_SKIP)
1952 goto nosideret;
1953 if (type != VALUE_TYPE (arg1))
1954 arg1 = value_cast (type, arg1);
1955 return arg1;
1956
1957 case UNOP_MEMVAL:
1958 (*pos) += 2;
1959 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1960 if (noside == EVAL_SKIP)
1961 goto nosideret;
1962 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1963 return value_zero (exp->elts[pc + 1].type, lval_memory);
1964 else
1965 return value_at_lazy (exp->elts[pc + 1].type,
1966 value_as_address (arg1),
1967 NULL);
1968
1969 case UNOP_PREINCREMENT:
1970 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1971 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1972 return arg1;
1973 else if (unop_user_defined_p (op, arg1))
1974 {
1975 return value_x_unop (arg1, op, noside);
1976 }
1977 else
1978 {
1979 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1980 (LONGEST) 1));
1981 return value_assign (arg1, arg2);
1982 }
1983
1984 case UNOP_PREDECREMENT:
1985 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1986 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1987 return arg1;
1988 else if (unop_user_defined_p (op, arg1))
1989 {
1990 return value_x_unop (arg1, op, noside);
1991 }
1992 else
1993 {
1994 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1995 (LONGEST) 1));
1996 return value_assign (arg1, arg2);
1997 }
1998
1999 case UNOP_POSTINCREMENT:
2000 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2001 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2002 return arg1;
2003 else if (unop_user_defined_p (op, arg1))
2004 {
2005 return value_x_unop (arg1, op, noside);
2006 }
2007 else
2008 {
2009 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
2010 (LONGEST) 1));
2011 value_assign (arg1, arg2);
2012 return arg1;
2013 }
2014
2015 case UNOP_POSTDECREMENT:
2016 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2017 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2018 return arg1;
2019 else if (unop_user_defined_p (op, arg1))
2020 {
2021 return value_x_unop (arg1, op, noside);
2022 }
2023 else
2024 {
2025 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
2026 (LONGEST) 1));
2027 value_assign (arg1, arg2);
2028 return arg1;
2029 }
2030
2031 case OP_THIS:
2032 (*pos) += 1;
2033 return value_of_this (1);
2034
2035 case OP_OBJC_SELF:
2036 (*pos) += 1;
2037 return value_of_local ("self", 1);
2038
2039 case OP_TYPE:
2040 error ("Attempt to use a type name as an expression");
2041
2042 default:
2043 /* Removing this case and compiling with gcc -Wall reveals that
2044 a lot of cases are hitting this case. Some of these should
2045 probably be removed from expression.h; others are legitimate
2046 expressions which are (apparently) not fully implemented.
2047
2048 If there are any cases landing here which mean a user error,
2049 then they should be separate cases, with more descriptive
2050 error messages. */
2051
2052 error ("\
2053 GDB does not (yet) know how to evaluate that kind of expression");
2054 }
2055
2056 nosideret:
2057 return value_from_longest (builtin_type_long, (LONGEST) 1);
2058 }
2059 \f
2060 /* Evaluate a subexpression of EXP, at index *POS,
2061 and return the address of that subexpression.
2062 Advance *POS over the subexpression.
2063 If the subexpression isn't an lvalue, get an error.
2064 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2065 then only the type of the result need be correct. */
2066
2067 static struct value *
2068 evaluate_subexp_for_address (struct expression *exp, int *pos,
2069 enum noside noside)
2070 {
2071 enum exp_opcode op;
2072 int pc;
2073 struct symbol *var;
2074
2075 pc = (*pos);
2076 op = exp->elts[pc].opcode;
2077
2078 switch (op)
2079 {
2080 case UNOP_IND:
2081 (*pos)++;
2082 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2083
2084 case UNOP_MEMVAL:
2085 (*pos) += 3;
2086 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2087 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2088
2089 case OP_VAR_VALUE:
2090 var = exp->elts[pc + 2].symbol;
2091
2092 /* C++: The "address" of a reference should yield the address
2093 * of the object pointed to. Let value_addr() deal with it. */
2094 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
2095 goto default_case;
2096
2097 (*pos) += 4;
2098 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2099 {
2100 struct type *type =
2101 lookup_pointer_type (SYMBOL_TYPE (var));
2102 enum address_class sym_class = SYMBOL_CLASS (var);
2103
2104 if (sym_class == LOC_CONST
2105 || sym_class == LOC_CONST_BYTES
2106 || sym_class == LOC_REGISTER
2107 || sym_class == LOC_REGPARM)
2108 error ("Attempt to take address of register or constant.");
2109
2110 return
2111 value_zero (type, not_lval);
2112 }
2113 else
2114 return
2115 locate_var_value
2116 (var,
2117 block_innermost_frame (exp->elts[pc + 1].block));
2118
2119 default:
2120 default_case:
2121 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2122 {
2123 struct value *x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2124 if (VALUE_LVAL (x) == lval_memory)
2125 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
2126 not_lval);
2127 else
2128 error ("Attempt to take address of non-lval");
2129 }
2130 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
2131 }
2132 }
2133
2134 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2135 When used in contexts where arrays will be coerced anyway, this is
2136 equivalent to `evaluate_subexp' but much faster because it avoids
2137 actually fetching array contents (perhaps obsolete now that we have
2138 VALUE_LAZY).
2139
2140 Note that we currently only do the coercion for C expressions, where
2141 arrays are zero based and the coercion is correct. For other languages,
2142 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2143 to decide if coercion is appropriate.
2144
2145 */
2146
2147 struct value *
2148 evaluate_subexp_with_coercion (struct expression *exp,
2149 int *pos, enum noside noside)
2150 {
2151 enum exp_opcode op;
2152 int pc;
2153 struct value *val;
2154 struct symbol *var;
2155
2156 pc = (*pos);
2157 op = exp->elts[pc].opcode;
2158
2159 switch (op)
2160 {
2161 case OP_VAR_VALUE:
2162 var = exp->elts[pc + 2].symbol;
2163 if (TYPE_CODE (check_typedef (SYMBOL_TYPE (var))) == TYPE_CODE_ARRAY
2164 && CAST_IS_CONVERSION)
2165 {
2166 (*pos) += 4;
2167 val =
2168 locate_var_value
2169 (var, block_innermost_frame (exp->elts[pc + 1].block));
2170 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (check_typedef (SYMBOL_TYPE (var)))),
2171 val);
2172 }
2173 /* FALLTHROUGH */
2174
2175 default:
2176 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2177 }
2178 }
2179
2180 /* Evaluate a subexpression of EXP, at index *POS,
2181 and return a value for the size of that subexpression.
2182 Advance *POS over the subexpression. */
2183
2184 static struct value *
2185 evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
2186 {
2187 enum exp_opcode op;
2188 int pc;
2189 struct type *type;
2190 struct value *val;
2191
2192 pc = (*pos);
2193 op = exp->elts[pc].opcode;
2194
2195 switch (op)
2196 {
2197 /* This case is handled specially
2198 so that we avoid creating a value for the result type.
2199 If the result type is very big, it's desirable not to
2200 create a value unnecessarily. */
2201 case UNOP_IND:
2202 (*pos)++;
2203 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2204 type = check_typedef (VALUE_TYPE (val));
2205 if (TYPE_CODE (type) != TYPE_CODE_PTR
2206 && TYPE_CODE (type) != TYPE_CODE_REF
2207 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
2208 error ("Attempt to take contents of a non-pointer value.");
2209 type = check_typedef (TYPE_TARGET_TYPE (type));
2210 return value_from_longest (builtin_type_int, (LONGEST)
2211 TYPE_LENGTH (type));
2212
2213 case UNOP_MEMVAL:
2214 (*pos) += 3;
2215 type = check_typedef (exp->elts[pc + 1].type);
2216 return value_from_longest (builtin_type_int,
2217 (LONGEST) TYPE_LENGTH (type));
2218
2219 case OP_VAR_VALUE:
2220 (*pos) += 4;
2221 type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
2222 return
2223 value_from_longest (builtin_type_int, (LONGEST) TYPE_LENGTH (type));
2224
2225 default:
2226 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2227 return value_from_longest (builtin_type_int,
2228 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
2229 }
2230 }
2231
2232 /* Parse a type expression in the string [P..P+LENGTH). */
2233
2234 struct type *
2235 parse_and_eval_type (char *p, int length)
2236 {
2237 char *tmp = (char *) alloca (length + 4);
2238 struct expression *expr;
2239 tmp[0] = '(';
2240 memcpy (tmp + 1, p, length);
2241 tmp[length + 1] = ')';
2242 tmp[length + 2] = '0';
2243 tmp[length + 3] = '\0';
2244 expr = parse_expression (tmp);
2245 if (expr->elts[0].opcode != UNOP_CAST)
2246 error ("Internal error in eval_type.");
2247 return expr->elts[1].type;
2248 }
2249
2250 int
2251 calc_f77_array_dims (struct type *array_type)
2252 {
2253 int ndimen = 1;
2254 struct type *tmp_type;
2255
2256 if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
2257 error ("Can't get dimensions for a non-array type");
2258
2259 tmp_type = array_type;
2260
2261 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
2262 {
2263 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
2264 ++ndimen;
2265 }
2266 return ndimen;
2267 }
This page took 0.078121 seconds and 4 git commands to generate.