Remove CHECK_TYPEDEF, use check_typedef instead
[deliverable/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "language.h" /* For CAST_IS_CONVERSION. */
29 #include "f-lang.h" /* For array bound stuff. */
30 #include "cp-abi.h"
31 #include "infcall.h"
32 #include "objc-lang.h"
33 #include "block.h"
34 #include "parser-defs.h"
35 #include "cp-support.h"
36 #include "ui-out.h"
37 #include "regcache.h"
38 #include "user-regs.h"
39 #include "valprint.h"
40 #include "gdb_obstack.h"
41 #include "objfiles.h"
42 #include <ctype.h>
43
44 /* This is defined in valops.c */
45 extern int overload_resolution;
46
47 /* Prototypes for local functions. */
48
49 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
50 enum noside);
51
52 static struct value *evaluate_subexp_for_address (struct expression *,
53 int *, enum noside);
54
55 static struct value *evaluate_struct_tuple (struct value *,
56 struct expression *, int *,
57 enum noside, int);
58
59 static LONGEST init_array_element (struct value *, struct value *,
60 struct expression *, int *, enum noside,
61 LONGEST, LONGEST);
62
63 struct value *
64 evaluate_subexp (struct type *expect_type, struct expression *exp,
65 int *pos, enum noside noside)
66 {
67 struct cleanup *cleanups;
68 struct value *retval;
69 int cleanup_temps = 0;
70
71 if (*pos == 0 && target_has_execution
72 && exp->language_defn->la_language == language_cplus
73 && !thread_stack_temporaries_enabled_p (inferior_ptid))
74 {
75 cleanups = enable_thread_stack_temporaries (inferior_ptid);
76 cleanup_temps = 1;
77 }
78
79 retval = (*exp->language_defn->la_exp_desc->evaluate_exp)
80 (expect_type, exp, pos, noside);
81
82 if (cleanup_temps)
83 {
84 if (value_in_thread_stack_temporaries (retval, inferior_ptid))
85 retval = value_non_lval (retval);
86 do_cleanups (cleanups);
87 }
88
89 return retval;
90 }
91 \f
92 /* Parse the string EXP as a C expression, evaluate it,
93 and return the result as a number. */
94
95 CORE_ADDR
96 parse_and_eval_address (const char *exp)
97 {
98 struct expression *expr = parse_expression (exp);
99 CORE_ADDR addr;
100 struct cleanup *old_chain =
101 make_cleanup (free_current_contents, &expr);
102
103 addr = value_as_address (evaluate_expression (expr));
104 do_cleanups (old_chain);
105 return addr;
106 }
107
108 /* Like parse_and_eval_address, but treats the value of the expression
109 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
110 LONGEST
111 parse_and_eval_long (const char *exp)
112 {
113 struct expression *expr = parse_expression (exp);
114 LONGEST retval;
115 struct cleanup *old_chain =
116 make_cleanup (free_current_contents, &expr);
117
118 retval = value_as_long (evaluate_expression (expr));
119 do_cleanups (old_chain);
120 return (retval);
121 }
122
123 struct value *
124 parse_and_eval (const char *exp)
125 {
126 struct expression *expr = parse_expression (exp);
127 struct value *val;
128 struct cleanup *old_chain =
129 make_cleanup (free_current_contents, &expr);
130
131 val = evaluate_expression (expr);
132 do_cleanups (old_chain);
133 return val;
134 }
135
136 /* Parse up to a comma (or to a closeparen)
137 in the string EXPP as an expression, evaluate it, and return the value.
138 EXPP is advanced to point to the comma. */
139
140 struct value *
141 parse_to_comma_and_eval (const char **expp)
142 {
143 struct expression *expr = parse_exp_1 (expp, 0, (struct block *) 0, 1);
144 struct value *val;
145 struct cleanup *old_chain =
146 make_cleanup (free_current_contents, &expr);
147
148 val = evaluate_expression (expr);
149 do_cleanups (old_chain);
150 return val;
151 }
152 \f
153 /* Evaluate an expression in internal prefix form
154 such as is constructed by parse.y.
155
156 See expression.h for info on the format of an expression. */
157
158 struct value *
159 evaluate_expression (struct expression *exp)
160 {
161 int pc = 0;
162
163 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
164 }
165
166 /* Evaluate an expression, avoiding all memory references
167 and getting a value whose type alone is correct. */
168
169 struct value *
170 evaluate_type (struct expression *exp)
171 {
172 int pc = 0;
173
174 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
175 }
176
177 /* Evaluate a subexpression, avoiding all memory references and
178 getting a value whose type alone is correct. */
179
180 struct value *
181 evaluate_subexpression_type (struct expression *exp, int subexp)
182 {
183 return evaluate_subexp (NULL_TYPE, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS);
184 }
185
186 /* Find the current value of a watchpoint on EXP. Return the value in
187 *VALP and *RESULTP and the chain of intermediate and final values
188 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
189 not need them.
190
191 If PRESERVE_ERRORS is true, then exceptions are passed through.
192 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
193 occurs while evaluating the expression, *RESULTP will be set to
194 NULL. *RESULTP may be a lazy value, if the result could not be
195 read from memory. It is used to determine whether a value is
196 user-specified (we should watch the whole value) or intermediate
197 (we should watch only the bit used to locate the final value).
198
199 If the final value, or any intermediate value, could not be read
200 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
201 set to any referenced values. *VALP will never be a lazy value.
202 This is the value which we store in struct breakpoint.
203
204 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
205 value chain. The caller must free the values individually. If
206 VAL_CHAIN is NULL, all generated values will be left on the value
207 chain. */
208
209 void
210 fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
211 struct value **resultp, struct value **val_chain,
212 int preserve_errors)
213 {
214 struct value *mark, *new_mark, *result;
215
216 *valp = NULL;
217 if (resultp)
218 *resultp = NULL;
219 if (val_chain)
220 *val_chain = NULL;
221
222 /* Evaluate the expression. */
223 mark = value_mark ();
224 result = NULL;
225
226 TRY
227 {
228 result = evaluate_subexp (NULL_TYPE, exp, pc, EVAL_NORMAL);
229 }
230 CATCH (ex, RETURN_MASK_ALL)
231 {
232 /* Ignore memory errors if we want watchpoints pointing at
233 inaccessible memory to still be created; otherwise, throw the
234 error to some higher catcher. */
235 switch (ex.error)
236 {
237 case MEMORY_ERROR:
238 if (!preserve_errors)
239 break;
240 default:
241 throw_exception (ex);
242 break;
243 }
244 }
245 END_CATCH
246
247 new_mark = value_mark ();
248 if (mark == new_mark)
249 return;
250 if (resultp)
251 *resultp = result;
252
253 /* Make sure it's not lazy, so that after the target stops again we
254 have a non-lazy previous value to compare with. */
255 if (result != NULL)
256 {
257 if (!value_lazy (result))
258 *valp = result;
259 else
260 {
261
262 TRY
263 {
264 value_fetch_lazy (result);
265 *valp = result;
266 }
267 CATCH (except, RETURN_MASK_ERROR)
268 {
269 }
270 END_CATCH
271 }
272 }
273
274 if (val_chain)
275 {
276 /* Return the chain of intermediate values. We use this to
277 decide which addresses to watch. */
278 *val_chain = new_mark;
279 value_release_to_mark (mark);
280 }
281 }
282
283 /* Extract a field operation from an expression. If the subexpression
284 of EXP starting at *SUBEXP is not a structure dereference
285 operation, return NULL. Otherwise, return the name of the
286 dereferenced field, and advance *SUBEXP to point to the
287 subexpression of the left-hand-side of the dereference. This is
288 used when completing field names. */
289
290 char *
291 extract_field_op (struct expression *exp, int *subexp)
292 {
293 int tem;
294 char *result;
295
296 if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT
297 && exp->elts[*subexp].opcode != STRUCTOP_PTR)
298 return NULL;
299 tem = longest_to_int (exp->elts[*subexp + 1].longconst);
300 result = &exp->elts[*subexp + 2].string;
301 (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1);
302 return result;
303 }
304
305 /* This function evaluates brace-initializers (in C/C++) for
306 structure types. */
307
308 static struct value *
309 evaluate_struct_tuple (struct value *struct_val,
310 struct expression *exp,
311 int *pos, enum noside noside, int nargs)
312 {
313 struct type *struct_type = check_typedef (value_type (struct_val));
314 struct type *field_type;
315 int fieldno = -1;
316
317 while (--nargs >= 0)
318 {
319 struct value *val = NULL;
320 int bitpos, bitsize;
321 bfd_byte *addr;
322
323 fieldno++;
324 /* Skip static fields. */
325 while (fieldno < TYPE_NFIELDS (struct_type)
326 && field_is_static (&TYPE_FIELD (struct_type,
327 fieldno)))
328 fieldno++;
329 if (fieldno >= TYPE_NFIELDS (struct_type))
330 error (_("too many initializers"));
331 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
332 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
333 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
334 error (_("don't know which variant you want to set"));
335
336 /* Here, struct_type is the type of the inner struct,
337 while substruct_type is the type of the inner struct.
338 These are the same for normal structures, but a variant struct
339 contains anonymous union fields that contain substruct fields.
340 The value fieldno is the index of the top-level (normal or
341 anonymous union) field in struct_field, while the value
342 subfieldno is the index of the actual real (named inner) field
343 in substruct_type. */
344
345 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
346 if (val == 0)
347 val = evaluate_subexp (field_type, exp, pos, noside);
348
349 /* Now actually set the field in struct_val. */
350
351 /* Assign val to field fieldno. */
352 if (value_type (val) != field_type)
353 val = value_cast (field_type, val);
354
355 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
356 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
357 addr = value_contents_writeable (struct_val) + bitpos / 8;
358 if (bitsize)
359 modify_field (struct_type, addr,
360 value_as_long (val), bitpos % 8, bitsize);
361 else
362 memcpy (addr, value_contents (val),
363 TYPE_LENGTH (value_type (val)));
364
365 }
366 return struct_val;
367 }
368
369 /* Recursive helper function for setting elements of array tuples.
370 The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND); the
371 element value is ELEMENT; EXP, POS and NOSIDE are as usual.
372 Evaluates index expresions and sets the specified element(s) of
373 ARRAY to ELEMENT. Returns last index value. */
374
375 static LONGEST
376 init_array_element (struct value *array, struct value *element,
377 struct expression *exp, int *pos,
378 enum noside noside, LONGEST low_bound, LONGEST high_bound)
379 {
380 LONGEST index;
381 int element_size = TYPE_LENGTH (value_type (element));
382
383 if (exp->elts[*pos].opcode == BINOP_COMMA)
384 {
385 (*pos)++;
386 init_array_element (array, element, exp, pos, noside,
387 low_bound, high_bound);
388 return init_array_element (array, element,
389 exp, pos, noside, low_bound, high_bound);
390 }
391 else
392 {
393 index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
394 if (index < low_bound || index > high_bound)
395 error (_("tuple index out of range"));
396 memcpy (value_contents_raw (array) + (index - low_bound) * element_size,
397 value_contents (element), element_size);
398 }
399 return index;
400 }
401
402 static struct value *
403 value_f90_subarray (struct value *array,
404 struct expression *exp, int *pos, enum noside noside)
405 {
406 int pc = (*pos) + 1;
407 LONGEST low_bound, high_bound;
408 struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array)));
409 enum f90_range_type range_type = longest_to_int (exp->elts[pc].longconst);
410
411 *pos += 3;
412
413 if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
414 low_bound = TYPE_LOW_BOUND (range);
415 else
416 low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
417
418 if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
419 high_bound = TYPE_HIGH_BOUND (range);
420 else
421 high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
422
423 return value_slice (array, low_bound, high_bound - low_bound + 1);
424 }
425
426
427 /* Promote value ARG1 as appropriate before performing a unary operation
428 on this argument.
429 If the result is not appropriate for any particular language then it
430 needs to patch this function. */
431
432 void
433 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
434 struct value **arg1)
435 {
436 struct type *type1;
437
438 *arg1 = coerce_ref (*arg1);
439 type1 = check_typedef (value_type (*arg1));
440
441 if (is_integral_type (type1))
442 {
443 switch (language->la_language)
444 {
445 default:
446 /* Perform integral promotion for ANSI C/C++.
447 If not appropropriate for any particular language
448 it needs to modify this function. */
449 {
450 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
451
452 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int))
453 *arg1 = value_cast (builtin_int, *arg1);
454 }
455 break;
456 }
457 }
458 }
459
460 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
461 operation on those two operands.
462 If the result is not appropriate for any particular language then it
463 needs to patch this function. */
464
465 void
466 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
467 struct value **arg1, struct value **arg2)
468 {
469 struct type *promoted_type = NULL;
470 struct type *type1;
471 struct type *type2;
472
473 *arg1 = coerce_ref (*arg1);
474 *arg2 = coerce_ref (*arg2);
475
476 type1 = check_typedef (value_type (*arg1));
477 type2 = check_typedef (value_type (*arg2));
478
479 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
480 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
481 && !is_integral_type (type1))
482 || (TYPE_CODE (type2) != TYPE_CODE_FLT
483 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
484 && !is_integral_type (type2)))
485 return;
486
487 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
488 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
489 {
490 /* No promotion required. */
491 }
492 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
493 || TYPE_CODE (type2) == TYPE_CODE_FLT)
494 {
495 switch (language->la_language)
496 {
497 case language_c:
498 case language_cplus:
499 case language_asm:
500 case language_objc:
501 case language_opencl:
502 /* No promotion required. */
503 break;
504
505 default:
506 /* For other languages the result type is unchanged from gdb
507 version 6.7 for backward compatibility.
508 If either arg was long double, make sure that value is also long
509 double. Otherwise use double. */
510 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch)
511 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch))
512 promoted_type = builtin_type (gdbarch)->builtin_long_double;
513 else
514 promoted_type = builtin_type (gdbarch)->builtin_double;
515 break;
516 }
517 }
518 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
519 && TYPE_CODE (type2) == TYPE_CODE_BOOL)
520 {
521 /* No promotion required. */
522 }
523 else
524 /* Integral operations here. */
525 /* FIXME: Also mixed integral/booleans, with result an integer. */
526 {
527 const struct builtin_type *builtin = builtin_type (gdbarch);
528 unsigned int promoted_len1 = TYPE_LENGTH (type1);
529 unsigned int promoted_len2 = TYPE_LENGTH (type2);
530 int is_unsigned1 = TYPE_UNSIGNED (type1);
531 int is_unsigned2 = TYPE_UNSIGNED (type2);
532 unsigned int result_len;
533 int unsigned_operation;
534
535 /* Determine type length and signedness after promotion for
536 both operands. */
537 if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int))
538 {
539 is_unsigned1 = 0;
540 promoted_len1 = TYPE_LENGTH (builtin->builtin_int);
541 }
542 if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int))
543 {
544 is_unsigned2 = 0;
545 promoted_len2 = TYPE_LENGTH (builtin->builtin_int);
546 }
547
548 if (promoted_len1 > promoted_len2)
549 {
550 unsigned_operation = is_unsigned1;
551 result_len = promoted_len1;
552 }
553 else if (promoted_len2 > promoted_len1)
554 {
555 unsigned_operation = is_unsigned2;
556 result_len = promoted_len2;
557 }
558 else
559 {
560 unsigned_operation = is_unsigned1 || is_unsigned2;
561 result_len = promoted_len1;
562 }
563
564 switch (language->la_language)
565 {
566 case language_c:
567 case language_cplus:
568 case language_asm:
569 case language_objc:
570 if (result_len <= TYPE_LENGTH (builtin->builtin_int))
571 {
572 promoted_type = (unsigned_operation
573 ? builtin->builtin_unsigned_int
574 : builtin->builtin_int);
575 }
576 else if (result_len <= TYPE_LENGTH (builtin->builtin_long))
577 {
578 promoted_type = (unsigned_operation
579 ? builtin->builtin_unsigned_long
580 : builtin->builtin_long);
581 }
582 else
583 {
584 promoted_type = (unsigned_operation
585 ? builtin->builtin_unsigned_long_long
586 : builtin->builtin_long_long);
587 }
588 break;
589 case language_opencl:
590 if (result_len <= TYPE_LENGTH (lookup_signed_typename
591 (language, gdbarch, "int")))
592 {
593 promoted_type =
594 (unsigned_operation
595 ? lookup_unsigned_typename (language, gdbarch, "int")
596 : lookup_signed_typename (language, gdbarch, "int"));
597 }
598 else if (result_len <= TYPE_LENGTH (lookup_signed_typename
599 (language, gdbarch, "long")))
600 {
601 promoted_type =
602 (unsigned_operation
603 ? lookup_unsigned_typename (language, gdbarch, "long")
604 : lookup_signed_typename (language, gdbarch,"long"));
605 }
606 break;
607 default:
608 /* For other languages the result type is unchanged from gdb
609 version 6.7 for backward compatibility.
610 If either arg was long long, make sure that value is also long
611 long. Otherwise use long. */
612 if (unsigned_operation)
613 {
614 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
615 promoted_type = builtin->builtin_unsigned_long_long;
616 else
617 promoted_type = builtin->builtin_unsigned_long;
618 }
619 else
620 {
621 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
622 promoted_type = builtin->builtin_long_long;
623 else
624 promoted_type = builtin->builtin_long;
625 }
626 break;
627 }
628 }
629
630 if (promoted_type)
631 {
632 /* Promote both operands to common type. */
633 *arg1 = value_cast (promoted_type, *arg1);
634 *arg2 = value_cast (promoted_type, *arg2);
635 }
636 }
637
638 static int
639 ptrmath_type_p (const struct language_defn *lang, struct type *type)
640 {
641 type = check_typedef (type);
642 if (TYPE_CODE (type) == TYPE_CODE_REF)
643 type = TYPE_TARGET_TYPE (type);
644
645 switch (TYPE_CODE (type))
646 {
647 case TYPE_CODE_PTR:
648 case TYPE_CODE_FUNC:
649 return 1;
650
651 case TYPE_CODE_ARRAY:
652 return TYPE_VECTOR (type) ? 0 : lang->c_style_arrays;
653
654 default:
655 return 0;
656 }
657 }
658
659 /* Constructs a fake method with the given parameter types.
660 This function is used by the parser to construct an "expected"
661 type for method overload resolution. */
662
663 static struct type *
664 make_params (int num_types, struct type **param_types)
665 {
666 struct type *type = XCNEW (struct type);
667 TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
668 TYPE_LENGTH (type) = 1;
669 TYPE_CODE (type) = TYPE_CODE_METHOD;
670 TYPE_CHAIN (type) = type;
671 if (num_types > 0)
672 {
673 if (param_types[num_types - 1] == NULL)
674 {
675 --num_types;
676 TYPE_VARARGS (type) = 1;
677 }
678 else if (TYPE_CODE (check_typedef (param_types[num_types - 1]))
679 == TYPE_CODE_VOID)
680 {
681 --num_types;
682 /* Caller should have ensured this. */
683 gdb_assert (num_types == 0);
684 TYPE_PROTOTYPED (type) = 1;
685 }
686 }
687
688 TYPE_NFIELDS (type) = num_types;
689 TYPE_FIELDS (type) = (struct field *)
690 TYPE_ZALLOC (type, sizeof (struct field) * num_types);
691
692 while (num_types-- > 0)
693 TYPE_FIELD_TYPE (type, num_types) = param_types[num_types];
694
695 return type;
696 }
697
698 struct value *
699 evaluate_subexp_standard (struct type *expect_type,
700 struct expression *exp, int *pos,
701 enum noside noside)
702 {
703 enum exp_opcode op;
704 int tem, tem2, tem3;
705 int pc, pc2 = 0, oldpos;
706 struct value *arg1 = NULL;
707 struct value *arg2 = NULL;
708 struct value *arg3;
709 struct type *type;
710 int nargs;
711 struct value **argvec;
712 int code;
713 int ix;
714 long mem_offset;
715 struct type **arg_types;
716 int save_pos1;
717 struct symbol *function = NULL;
718 char *function_name = NULL;
719
720 pc = (*pos)++;
721 op = exp->elts[pc].opcode;
722
723 switch (op)
724 {
725 case OP_SCOPE:
726 tem = longest_to_int (exp->elts[pc + 2].longconst);
727 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
728 if (noside == EVAL_SKIP)
729 goto nosideret;
730 arg1 = value_aggregate_elt (exp->elts[pc + 1].type,
731 &exp->elts[pc + 3].string,
732 expect_type, 0, noside);
733 if (arg1 == NULL)
734 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
735 return arg1;
736
737 case OP_LONG:
738 (*pos) += 3;
739 return value_from_longest (exp->elts[pc + 1].type,
740 exp->elts[pc + 2].longconst);
741
742 case OP_DOUBLE:
743 (*pos) += 3;
744 return value_from_double (exp->elts[pc + 1].type,
745 exp->elts[pc + 2].doubleconst);
746
747 case OP_DECFLOAT:
748 (*pos) += 3;
749 return value_from_decfloat (exp->elts[pc + 1].type,
750 exp->elts[pc + 2].decfloatconst);
751
752 case OP_ADL_FUNC:
753 case OP_VAR_VALUE:
754 (*pos) += 3;
755 if (noside == EVAL_SKIP)
756 goto nosideret;
757
758 /* JYG: We used to just return value_zero of the symbol type
759 if we're asked to avoid side effects. Otherwise we return
760 value_of_variable (...). However I'm not sure if
761 value_of_variable () has any side effect.
762 We need a full value object returned here for whatis_exp ()
763 to call evaluate_type () and then pass the full value to
764 value_rtti_target_type () if we are dealing with a pointer
765 or reference to a base class and print object is on. */
766
767 {
768 struct value *ret = NULL;
769
770 TRY
771 {
772 ret = value_of_variable (exp->elts[pc + 2].symbol,
773 exp->elts[pc + 1].block);
774 }
775
776 CATCH (except, RETURN_MASK_ERROR)
777 {
778 if (noside == EVAL_AVOID_SIDE_EFFECTS)
779 ret = value_zero (SYMBOL_TYPE (exp->elts[pc + 2].symbol),
780 not_lval);
781 else
782 throw_exception (except);
783 }
784 END_CATCH
785
786 return ret;
787 }
788
789 case OP_VAR_ENTRY_VALUE:
790 (*pos) += 2;
791 if (noside == EVAL_SKIP)
792 goto nosideret;
793
794 {
795 struct symbol *sym = exp->elts[pc + 1].symbol;
796 struct frame_info *frame;
797
798 if (noside == EVAL_AVOID_SIDE_EFFECTS)
799 return value_zero (SYMBOL_TYPE (sym), not_lval);
800
801 if (SYMBOL_COMPUTED_OPS (sym) == NULL
802 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
803 error (_("Symbol \"%s\" does not have any specific entry value"),
804 SYMBOL_PRINT_NAME (sym));
805
806 frame = get_selected_frame (NULL);
807 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
808 }
809
810 case OP_LAST:
811 (*pos) += 2;
812 return
813 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
814
815 case OP_REGISTER:
816 {
817 const char *name = &exp->elts[pc + 2].string;
818 int regno;
819 struct value *val;
820
821 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
822 regno = user_reg_map_name_to_regnum (exp->gdbarch,
823 name, strlen (name));
824 if (regno == -1)
825 error (_("Register $%s not available."), name);
826
827 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
828 a value with the appropriate register type. Unfortunately,
829 we don't have easy access to the type of user registers.
830 So for these registers, we fetch the register value regardless
831 of the evaluation mode. */
832 if (noside == EVAL_AVOID_SIDE_EFFECTS
833 && regno < gdbarch_num_regs (exp->gdbarch)
834 + gdbarch_num_pseudo_regs (exp->gdbarch))
835 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
836 else
837 val = value_of_register (regno, get_selected_frame (NULL));
838 if (val == NULL)
839 error (_("Value of register %s not available."), name);
840 else
841 return val;
842 }
843 case OP_BOOL:
844 (*pos) += 2;
845 type = language_bool_type (exp->language_defn, exp->gdbarch);
846 return value_from_longest (type, exp->elts[pc + 1].longconst);
847
848 case OP_INTERNALVAR:
849 (*pos) += 2;
850 return value_of_internalvar (exp->gdbarch,
851 exp->elts[pc + 1].internalvar);
852
853 case OP_STRING:
854 tem = longest_to_int (exp->elts[pc + 1].longconst);
855 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
856 if (noside == EVAL_SKIP)
857 goto nosideret;
858 type = language_string_char_type (exp->language_defn, exp->gdbarch);
859 return value_string (&exp->elts[pc + 2].string, tem, type);
860
861 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
862 NSString constant. */
863 tem = longest_to_int (exp->elts[pc + 1].longconst);
864 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
865 if (noside == EVAL_SKIP)
866 {
867 goto nosideret;
868 }
869 return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
870
871 case OP_ARRAY:
872 (*pos) += 3;
873 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
874 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
875 nargs = tem3 - tem2 + 1;
876 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
877
878 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
879 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
880 {
881 struct value *rec = allocate_value (expect_type);
882
883 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
884 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
885 }
886
887 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
888 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
889 {
890 struct type *range_type = TYPE_INDEX_TYPE (type);
891 struct type *element_type = TYPE_TARGET_TYPE (type);
892 struct value *array = allocate_value (expect_type);
893 int element_size = TYPE_LENGTH (check_typedef (element_type));
894 LONGEST low_bound, high_bound, index;
895
896 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
897 {
898 low_bound = 0;
899 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
900 }
901 index = low_bound;
902 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
903 for (tem = nargs; --nargs >= 0;)
904 {
905 struct value *element;
906 int index_pc = 0;
907
908 element = evaluate_subexp (element_type, exp, pos, noside);
909 if (value_type (element) != element_type)
910 element = value_cast (element_type, element);
911 if (index_pc)
912 {
913 int continue_pc = *pos;
914
915 *pos = index_pc;
916 index = init_array_element (array, element, exp, pos, noside,
917 low_bound, high_bound);
918 *pos = continue_pc;
919 }
920 else
921 {
922 if (index > high_bound)
923 /* To avoid memory corruption. */
924 error (_("Too many array elements"));
925 memcpy (value_contents_raw (array)
926 + (index - low_bound) * element_size,
927 value_contents (element),
928 element_size);
929 }
930 index++;
931 }
932 return array;
933 }
934
935 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
936 && TYPE_CODE (type) == TYPE_CODE_SET)
937 {
938 struct value *set = allocate_value (expect_type);
939 gdb_byte *valaddr = value_contents_raw (set);
940 struct type *element_type = TYPE_INDEX_TYPE (type);
941 struct type *check_type = element_type;
942 LONGEST low_bound, high_bound;
943
944 /* Get targettype of elementtype. */
945 while (TYPE_CODE (check_type) == TYPE_CODE_RANGE
946 || TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
947 check_type = TYPE_TARGET_TYPE (check_type);
948
949 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
950 error (_("(power)set type with unknown size"));
951 memset (valaddr, '\0', TYPE_LENGTH (type));
952 for (tem = 0; tem < nargs; tem++)
953 {
954 LONGEST range_low, range_high;
955 struct type *range_low_type, *range_high_type;
956 struct value *elem_val;
957
958 elem_val = evaluate_subexp (element_type, exp, pos, noside);
959 range_low_type = range_high_type = value_type (elem_val);
960 range_low = range_high = value_as_long (elem_val);
961
962 /* Check types of elements to avoid mixture of elements from
963 different types. Also check if type of element is "compatible"
964 with element type of powerset. */
965 if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
966 range_low_type = TYPE_TARGET_TYPE (range_low_type);
967 if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
968 range_high_type = TYPE_TARGET_TYPE (range_high_type);
969 if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type))
970 || (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM
971 && (range_low_type != range_high_type)))
972 /* different element modes. */
973 error (_("POWERSET tuple elements of different mode"));
974 if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type))
975 || (TYPE_CODE (check_type) == TYPE_CODE_ENUM
976 && range_low_type != check_type))
977 error (_("incompatible POWERSET tuple elements"));
978 if (range_low > range_high)
979 {
980 warning (_("empty POWERSET tuple range"));
981 continue;
982 }
983 if (range_low < low_bound || range_high > high_bound)
984 error (_("POWERSET tuple element out of range"));
985 range_low -= low_bound;
986 range_high -= low_bound;
987 for (; range_low <= range_high; range_low++)
988 {
989 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
990
991 if (gdbarch_bits_big_endian (exp->gdbarch))
992 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
993 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
994 |= 1 << bit_index;
995 }
996 }
997 return set;
998 }
999
1000 argvec = (struct value **) alloca (sizeof (struct value *) * nargs);
1001 for (tem = 0; tem < nargs; tem++)
1002 {
1003 /* Ensure that array expressions are coerced into pointer
1004 objects. */
1005 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1006 }
1007 if (noside == EVAL_SKIP)
1008 goto nosideret;
1009 return value_array (tem2, tem3, argvec);
1010
1011 case TERNOP_SLICE:
1012 {
1013 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1014 int lowbound
1015 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1016 int upper
1017 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1018
1019 if (noside == EVAL_SKIP)
1020 goto nosideret;
1021 return value_slice (array, lowbound, upper - lowbound + 1);
1022 }
1023
1024 case TERNOP_COND:
1025 /* Skip third and second args to evaluate the first one. */
1026 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1027 if (value_logical_not (arg1))
1028 {
1029 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1030 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1031 }
1032 else
1033 {
1034 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1035 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1036 return arg2;
1037 }
1038
1039 case OP_OBJC_SELECTOR:
1040 { /* Objective C @selector operator. */
1041 char *sel = &exp->elts[pc + 2].string;
1042 int len = longest_to_int (exp->elts[pc + 1].longconst);
1043 struct type *selector_type;
1044
1045 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
1046 if (noside == EVAL_SKIP)
1047 goto nosideret;
1048
1049 if (sel[len] != 0)
1050 sel[len] = 0; /* Make sure it's terminated. */
1051
1052 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1053 return value_from_longest (selector_type,
1054 lookup_child_selector (exp->gdbarch, sel));
1055 }
1056
1057 case OP_OBJC_MSGCALL:
1058 { /* Objective C message (method) call. */
1059
1060 CORE_ADDR responds_selector = 0;
1061 CORE_ADDR method_selector = 0;
1062
1063 CORE_ADDR selector = 0;
1064
1065 int struct_return = 0;
1066 int sub_no_side = 0;
1067
1068 struct value *msg_send = NULL;
1069 struct value *msg_send_stret = NULL;
1070 int gnu_runtime = 0;
1071
1072 struct value *target = NULL;
1073 struct value *method = NULL;
1074 struct value *called_method = NULL;
1075
1076 struct type *selector_type = NULL;
1077 struct type *long_type;
1078
1079 struct value *ret = NULL;
1080 CORE_ADDR addr = 0;
1081
1082 selector = exp->elts[pc + 1].longconst;
1083 nargs = exp->elts[pc + 2].longconst;
1084 argvec = (struct value **) alloca (sizeof (struct value *)
1085 * (nargs + 5));
1086
1087 (*pos) += 3;
1088
1089 long_type = builtin_type (exp->gdbarch)->builtin_long;
1090 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1091
1092 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1093 sub_no_side = EVAL_NORMAL;
1094 else
1095 sub_no_side = noside;
1096
1097 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
1098
1099 if (value_as_long (target) == 0)
1100 return value_from_longest (long_type, 0);
1101
1102 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1103 gnu_runtime = 1;
1104
1105 /* Find the method dispatch (Apple runtime) or method lookup
1106 (GNU runtime) function for Objective-C. These will be used
1107 to lookup the symbol information for the method. If we
1108 can't find any symbol information, then we'll use these to
1109 call the method, otherwise we can call the method
1110 directly. The msg_send_stret function is used in the special
1111 case of a method that returns a structure (Apple runtime
1112 only). */
1113 if (gnu_runtime)
1114 {
1115 struct type *type = selector_type;
1116
1117 type = lookup_function_type (type);
1118 type = lookup_pointer_type (type);
1119 type = lookup_function_type (type);
1120 type = lookup_pointer_type (type);
1121
1122 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1123 msg_send_stret
1124 = find_function_in_inferior ("objc_msg_lookup", NULL);
1125
1126 msg_send = value_from_pointer (type, value_as_address (msg_send));
1127 msg_send_stret = value_from_pointer (type,
1128 value_as_address (msg_send_stret));
1129 }
1130 else
1131 {
1132 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1133 /* Special dispatcher for methods returning structs. */
1134 msg_send_stret
1135 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1136 }
1137
1138 /* Verify the target object responds to this method. The
1139 standard top-level 'Object' class uses a different name for
1140 the verification method than the non-standard, but more
1141 often used, 'NSObject' class. Make sure we check for both. */
1142
1143 responds_selector
1144 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1145 if (responds_selector == 0)
1146 responds_selector
1147 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1148
1149 if (responds_selector == 0)
1150 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1151
1152 method_selector
1153 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1154 if (method_selector == 0)
1155 method_selector
1156 = lookup_child_selector (exp->gdbarch, "methodFor:");
1157
1158 if (method_selector == 0)
1159 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1160
1161 /* Call the verification method, to make sure that the target
1162 class implements the desired method. */
1163
1164 argvec[0] = msg_send;
1165 argvec[1] = target;
1166 argvec[2] = value_from_longest (long_type, responds_selector);
1167 argvec[3] = value_from_longest (long_type, selector);
1168 argvec[4] = 0;
1169
1170 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1171 if (gnu_runtime)
1172 {
1173 /* Function objc_msg_lookup returns a pointer. */
1174 argvec[0] = ret;
1175 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1176 }
1177 if (value_as_long (ret) == 0)
1178 error (_("Target does not respond to this message selector."));
1179
1180 /* Call "methodForSelector:" method, to get the address of a
1181 function method that implements this selector for this
1182 class. If we can find a symbol at that address, then we
1183 know the return type, parameter types etc. (that's a good
1184 thing). */
1185
1186 argvec[0] = msg_send;
1187 argvec[1] = target;
1188 argvec[2] = value_from_longest (long_type, method_selector);
1189 argvec[3] = value_from_longest (long_type, selector);
1190 argvec[4] = 0;
1191
1192 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1193 if (gnu_runtime)
1194 {
1195 argvec[0] = ret;
1196 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1197 }
1198
1199 /* ret should now be the selector. */
1200
1201 addr = value_as_long (ret);
1202 if (addr)
1203 {
1204 struct symbol *sym = NULL;
1205
1206 /* The address might point to a function descriptor;
1207 resolve it to the actual code address instead. */
1208 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1209 &current_target);
1210
1211 /* Is it a high_level symbol? */
1212 sym = find_pc_function (addr);
1213 if (sym != NULL)
1214 method = value_of_variable (sym, 0);
1215 }
1216
1217 /* If we found a method with symbol information, check to see
1218 if it returns a struct. Otherwise assume it doesn't. */
1219
1220 if (method)
1221 {
1222 CORE_ADDR funaddr;
1223 struct type *val_type;
1224
1225 funaddr = find_function_addr (method, &val_type);
1226
1227 block_for_pc (funaddr);
1228
1229 val_type = check_typedef (val_type);
1230
1231 if ((val_type == NULL)
1232 || (TYPE_CODE(val_type) == TYPE_CODE_ERROR))
1233 {
1234 if (expect_type != NULL)
1235 val_type = expect_type;
1236 }
1237
1238 struct_return = using_struct_return (exp->gdbarch, method,
1239 val_type);
1240 }
1241 else if (expect_type != NULL)
1242 {
1243 struct_return = using_struct_return (exp->gdbarch, NULL,
1244 check_typedef (expect_type));
1245 }
1246
1247 /* Found a function symbol. Now we will substitute its
1248 value in place of the message dispatcher (obj_msgSend),
1249 so that we call the method directly instead of thru
1250 the dispatcher. The main reason for doing this is that
1251 we can now evaluate the return value and parameter values
1252 according to their known data types, in case we need to
1253 do things like promotion, dereferencing, special handling
1254 of structs and doubles, etc.
1255
1256 We want to use the type signature of 'method', but still
1257 jump to objc_msgSend() or objc_msgSend_stret() to better
1258 mimic the behavior of the runtime. */
1259
1260 if (method)
1261 {
1262 if (TYPE_CODE (value_type (method)) != TYPE_CODE_FUNC)
1263 error (_("method address has symbol information "
1264 "with non-function type; skipping"));
1265
1266 /* Create a function pointer of the appropriate type, and
1267 replace its value with the value of msg_send or
1268 msg_send_stret. We must use a pointer here, as
1269 msg_send and msg_send_stret are of pointer type, and
1270 the representation may be different on systems that use
1271 function descriptors. */
1272 if (struct_return)
1273 called_method
1274 = value_from_pointer (lookup_pointer_type (value_type (method)),
1275 value_as_address (msg_send_stret));
1276 else
1277 called_method
1278 = value_from_pointer (lookup_pointer_type (value_type (method)),
1279 value_as_address (msg_send));
1280 }
1281 else
1282 {
1283 if (struct_return)
1284 called_method = msg_send_stret;
1285 else
1286 called_method = msg_send;
1287 }
1288
1289 if (noside == EVAL_SKIP)
1290 goto nosideret;
1291
1292 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1293 {
1294 /* If the return type doesn't look like a function type,
1295 call an error. This can happen if somebody tries to
1296 turn a variable into a function call. This is here
1297 because people often want to call, eg, strcmp, which
1298 gdb doesn't know is a function. If gdb isn't asked for
1299 it's opinion (ie. through "whatis"), it won't offer
1300 it. */
1301
1302 struct type *type = value_type (called_method);
1303
1304 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1305 type = TYPE_TARGET_TYPE (type);
1306 type = TYPE_TARGET_TYPE (type);
1307
1308 if (type)
1309 {
1310 if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type)
1311 return allocate_value (expect_type);
1312 else
1313 return allocate_value (type);
1314 }
1315 else
1316 error (_("Expression of type other than "
1317 "\"method returning ...\" used as a method"));
1318 }
1319
1320 /* Now depending on whether we found a symbol for the method,
1321 we will either call the runtime dispatcher or the method
1322 directly. */
1323
1324 argvec[0] = called_method;
1325 argvec[1] = target;
1326 argvec[2] = value_from_longest (long_type, selector);
1327 /* User-supplied arguments. */
1328 for (tem = 0; tem < nargs; tem++)
1329 argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
1330 argvec[tem + 3] = 0;
1331
1332 if (gnu_runtime && (method != NULL))
1333 {
1334 /* Function objc_msg_lookup returns a pointer. */
1335 deprecated_set_value_type (argvec[0],
1336 lookup_pointer_type (lookup_function_type (value_type (argvec[0]))));
1337 argvec[0]
1338 = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1339 }
1340
1341 ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1342 return ret;
1343 }
1344 break;
1345
1346 case OP_FUNCALL:
1347 (*pos) += 2;
1348 op = exp->elts[*pos].opcode;
1349 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1350 /* Allocate arg vector, including space for the function to be
1351 called in argvec[0], a potential `this', and a terminating NULL. */
1352 argvec = (struct value **)
1353 alloca (sizeof (struct value *) * (nargs + 3));
1354 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1355 {
1356 /* First, evaluate the structure into arg2. */
1357 pc2 = (*pos)++;
1358
1359 if (op == STRUCTOP_MEMBER)
1360 {
1361 arg2 = evaluate_subexp_for_address (exp, pos, noside);
1362 }
1363 else
1364 {
1365 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1366 }
1367
1368 /* If the function is a virtual function, then the
1369 aggregate value (providing the structure) plays
1370 its part by providing the vtable. Otherwise,
1371 it is just along for the ride: call the function
1372 directly. */
1373
1374 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1375
1376 type = check_typedef (value_type (arg1));
1377 if (noside == EVAL_SKIP)
1378 tem = 1; /* Set it to the right arg index so that all arguments
1379 can also be skipped. */
1380 else if (TYPE_CODE (type) == TYPE_CODE_METHODPTR)
1381 {
1382 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1383 arg1 = value_zero (TYPE_TARGET_TYPE (type), not_lval);
1384 else
1385 arg1 = cplus_method_ptr_to_value (&arg2, arg1);
1386
1387 /* Now, say which argument to start evaluating from. */
1388 nargs++;
1389 tem = 2;
1390 argvec[1] = arg2;
1391 }
1392 else if (TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
1393 {
1394 struct type *type_ptr
1395 = lookup_pointer_type (TYPE_SELF_TYPE (type));
1396 struct type *target_type_ptr
1397 = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1398
1399 /* Now, convert these values to an address. */
1400 arg2 = value_cast (type_ptr, arg2);
1401
1402 mem_offset = value_as_long (arg1);
1403
1404 arg1 = value_from_pointer (target_type_ptr,
1405 value_as_long (arg2) + mem_offset);
1406 arg1 = value_ind (arg1);
1407 tem = 1;
1408 }
1409 else
1410 error (_("Non-pointer-to-member value used in pointer-to-member "
1411 "construct"));
1412 }
1413 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
1414 {
1415 /* Hair for method invocations. */
1416 int tem2;
1417
1418 nargs++;
1419 /* First, evaluate the structure into arg2. */
1420 pc2 = (*pos)++;
1421 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
1422 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
1423
1424 if (op == STRUCTOP_STRUCT)
1425 {
1426 /* If v is a variable in a register, and the user types
1427 v.method (), this will produce an error, because v has
1428 no address.
1429
1430 A possible way around this would be to allocate a
1431 copy of the variable on the stack, copy in the
1432 contents, call the function, and copy out the
1433 contents. I.e. convert this from call by reference
1434 to call by copy-return (or whatever it's called).
1435 However, this does not work because it is not the
1436 same: the method being called could stash a copy of
1437 the address, and then future uses through that address
1438 (after the method returns) would be expected to
1439 use the variable itself, not some copy of it. */
1440 arg2 = evaluate_subexp_for_address (exp, pos, noside);
1441 }
1442 else
1443 {
1444 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1445
1446 /* Check to see if the operator '->' has been
1447 overloaded. If the operator has been overloaded
1448 replace arg2 with the value returned by the custom
1449 operator and continue evaluation. */
1450 while (unop_user_defined_p (op, arg2))
1451 {
1452 struct value *value = NULL;
1453 TRY
1454 {
1455 value = value_x_unop (arg2, op, noside);
1456 }
1457
1458 CATCH (except, RETURN_MASK_ERROR)
1459 {
1460 if (except.error == NOT_FOUND_ERROR)
1461 break;
1462 else
1463 throw_exception (except);
1464 }
1465 END_CATCH
1466
1467 arg2 = value;
1468 }
1469 }
1470 /* Now, say which argument to start evaluating from. */
1471 tem = 2;
1472 }
1473 else if (op == OP_SCOPE
1474 && overload_resolution
1475 && (exp->language_defn->la_language == language_cplus))
1476 {
1477 /* Unpack it locally so we can properly handle overload
1478 resolution. */
1479 char *name;
1480 int local_tem;
1481
1482 pc2 = (*pos)++;
1483 local_tem = longest_to_int (exp->elts[pc2 + 2].longconst);
1484 (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1);
1485 type = exp->elts[pc2 + 1].type;
1486 name = &exp->elts[pc2 + 3].string;
1487
1488 function = NULL;
1489 function_name = NULL;
1490 if (TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
1491 {
1492 function = cp_lookup_symbol_namespace (TYPE_TAG_NAME (type),
1493 name,
1494 get_selected_block (0),
1495 VAR_DOMAIN);
1496 if (function == NULL)
1497 error (_("No symbol \"%s\" in namespace \"%s\"."),
1498 name, TYPE_TAG_NAME (type));
1499
1500 tem = 1;
1501 /* arg2 is left as NULL on purpose. */
1502 }
1503 else
1504 {
1505 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1506 || TYPE_CODE (type) == TYPE_CODE_UNION);
1507 function_name = name;
1508
1509 /* We need a properly typed value for method lookup. For
1510 static methods arg2 is otherwise unused. */
1511 arg2 = value_zero (type, lval_memory);
1512 ++nargs;
1513 tem = 2;
1514 }
1515 }
1516 else if (op == OP_ADL_FUNC)
1517 {
1518 /* Save the function position and move pos so that the arguments
1519 can be evaluated. */
1520 int func_name_len;
1521
1522 save_pos1 = *pos;
1523 tem = 1;
1524
1525 func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst);
1526 (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1);
1527 }
1528 else
1529 {
1530 /* Non-method function call. */
1531 save_pos1 = *pos;
1532 tem = 1;
1533
1534 /* If this is a C++ function wait until overload resolution. */
1535 if (op == OP_VAR_VALUE
1536 && overload_resolution
1537 && (exp->language_defn->la_language == language_cplus))
1538 {
1539 (*pos) += 4; /* Skip the evaluation of the symbol. */
1540 argvec[0] = NULL;
1541 }
1542 else
1543 {
1544 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
1545 type = value_type (argvec[0]);
1546 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1547 type = TYPE_TARGET_TYPE (type);
1548 if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
1549 {
1550 for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
1551 {
1552 argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type,
1553 tem - 1),
1554 exp, pos, noside);
1555 }
1556 }
1557 }
1558 }
1559
1560 /* Evaluate arguments (if not already done, e.g., namespace::func()
1561 and overload-resolution is off). */
1562 for (; tem <= nargs; tem++)
1563 {
1564 /* Ensure that array expressions are coerced into pointer
1565 objects. */
1566 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1567 }
1568
1569 /* Signal end of arglist. */
1570 argvec[tem] = 0;
1571
1572 if (noside == EVAL_SKIP)
1573 goto nosideret;
1574
1575 if (op == OP_ADL_FUNC)
1576 {
1577 struct symbol *symp;
1578 char *func_name;
1579 int name_len;
1580 int string_pc = save_pos1 + 3;
1581
1582 /* Extract the function name. */
1583 name_len = longest_to_int (exp->elts[string_pc].longconst);
1584 func_name = (char *) alloca (name_len + 1);
1585 strcpy (func_name, &exp->elts[string_pc + 1].string);
1586
1587 find_overload_match (&argvec[1], nargs, func_name,
1588 NON_METHOD, /* not method */
1589 NULL, NULL, /* pass NULL symbol since
1590 symbol is unknown */
1591 NULL, &symp, NULL, 0, noside);
1592
1593 /* Now fix the expression being evaluated. */
1594 exp->elts[save_pos1 + 2].symbol = symp;
1595 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1596 }
1597
1598 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
1599 || (op == OP_SCOPE && function_name != NULL))
1600 {
1601 int static_memfuncp;
1602 char *tstr;
1603
1604 /* Method invocation: stuff "this" as first parameter.
1605 If the method turns out to be static we undo this below. */
1606 argvec[1] = arg2;
1607
1608 if (op != OP_SCOPE)
1609 {
1610 /* Name of method from expression. */
1611 tstr = &exp->elts[pc2 + 2].string;
1612 }
1613 else
1614 tstr = function_name;
1615
1616 if (overload_resolution && (exp->language_defn->la_language
1617 == language_cplus))
1618 {
1619 /* Language is C++, do some overload resolution before
1620 evaluation. */
1621 struct value *valp = NULL;
1622
1623 (void) find_overload_match (&argvec[1], nargs, tstr,
1624 METHOD, /* method */
1625 &arg2, /* the object */
1626 NULL, &valp, NULL,
1627 &static_memfuncp, 0, noside);
1628
1629 if (op == OP_SCOPE && !static_memfuncp)
1630 {
1631 /* For the time being, we don't handle this. */
1632 error (_("Call to overloaded function %s requires "
1633 "`this' pointer"),
1634 function_name);
1635 }
1636 argvec[1] = arg2; /* the ``this'' pointer */
1637 argvec[0] = valp; /* Use the method found after overload
1638 resolution. */
1639 }
1640 else
1641 /* Non-C++ case -- or no overload resolution. */
1642 {
1643 struct value *temp = arg2;
1644
1645 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1646 &static_memfuncp,
1647 op == STRUCTOP_STRUCT
1648 ? "structure" : "structure pointer");
1649 /* value_struct_elt updates temp with the correct value
1650 of the ``this'' pointer if necessary, so modify argvec[1] to
1651 reflect any ``this'' changes. */
1652 arg2
1653 = value_from_longest (lookup_pointer_type(value_type (temp)),
1654 value_address (temp)
1655 + value_embedded_offset (temp));
1656 argvec[1] = arg2; /* the ``this'' pointer */
1657 }
1658
1659 /* Take out `this' if needed. */
1660 if (static_memfuncp)
1661 {
1662 argvec[1] = argvec[0];
1663 nargs--;
1664 argvec++;
1665 }
1666 }
1667 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1668 {
1669 /* Pointer to member. argvec[1] is already set up. */
1670 argvec[0] = arg1;
1671 }
1672 else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL))
1673 {
1674 /* Non-member function being called. */
1675 /* fn: This can only be done for C++ functions. A C-style function
1676 in a C++ program, for instance, does not have the fields that
1677 are expected here. */
1678
1679 if (overload_resolution && (exp->language_defn->la_language
1680 == language_cplus))
1681 {
1682 /* Language is C++, do some overload resolution before
1683 evaluation. */
1684 struct symbol *symp;
1685 int no_adl = 0;
1686
1687 /* If a scope has been specified disable ADL. */
1688 if (op == OP_SCOPE)
1689 no_adl = 1;
1690
1691 if (op == OP_VAR_VALUE)
1692 function = exp->elts[save_pos1+2].symbol;
1693
1694 (void) find_overload_match (&argvec[1], nargs,
1695 NULL, /* no need for name */
1696 NON_METHOD, /* not method */
1697 NULL, function, /* the function */
1698 NULL, &symp, NULL, no_adl, noside);
1699
1700 if (op == OP_VAR_VALUE)
1701 {
1702 /* Now fix the expression being evaluated. */
1703 exp->elts[save_pos1+2].symbol = symp;
1704 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1,
1705 noside);
1706 }
1707 else
1708 argvec[0] = value_of_variable (symp, get_selected_block (0));
1709 }
1710 else
1711 {
1712 /* Not C++, or no overload resolution allowed. */
1713 /* Nothing to be done; argvec already correctly set up. */
1714 }
1715 }
1716 else
1717 {
1718 /* It is probably a C-style function. */
1719 /* Nothing to be done; argvec already correctly set up. */
1720 }
1721
1722 do_call_it:
1723
1724 if (argvec[0] == NULL)
1725 error (_("Cannot evaluate function -- may be inlined"));
1726 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1727 {
1728 /* If the return type doesn't look like a function type, call an
1729 error. This can happen if somebody tries to turn a variable into
1730 a function call. This is here because people often want to
1731 call, eg, strcmp, which gdb doesn't know is a function. If
1732 gdb isn't asked for it's opinion (ie. through "whatis"),
1733 it won't offer it. */
1734
1735 struct type *ftype = value_type (argvec[0]);
1736
1737 if (TYPE_CODE (ftype) == TYPE_CODE_INTERNAL_FUNCTION)
1738 {
1739 /* We don't know anything about what the internal
1740 function might return, but we have to return
1741 something. */
1742 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
1743 not_lval);
1744 }
1745 else if (TYPE_CODE (ftype) == TYPE_CODE_XMETHOD)
1746 {
1747 struct type *return_type
1748 = result_type_of_xmethod (argvec[0], nargs, argvec + 1);
1749
1750 if (return_type == NULL)
1751 error (_("Xmethod is missing return type."));
1752 return value_zero (return_type, not_lval);
1753 }
1754 else if (TYPE_GNU_IFUNC (ftype))
1755 return allocate_value (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)));
1756 else if (TYPE_TARGET_TYPE (ftype))
1757 return allocate_value (TYPE_TARGET_TYPE (ftype));
1758 else
1759 error (_("Expression of type other than "
1760 "\"Function returning ...\" used as function"));
1761 }
1762 switch (TYPE_CODE (value_type (argvec[0])))
1763 {
1764 case TYPE_CODE_INTERNAL_FUNCTION:
1765 return call_internal_function (exp->gdbarch, exp->language_defn,
1766 argvec[0], nargs, argvec + 1);
1767 case TYPE_CODE_XMETHOD:
1768 return call_xmethod (argvec[0], nargs, argvec + 1);
1769 default:
1770 return call_function_by_hand (argvec[0], nargs, argvec + 1);
1771 }
1772 /* pai: FIXME save value from call_function_by_hand, then adjust
1773 pc by adjust_fn_pc if +ve. */
1774
1775 case OP_F77_UNDETERMINED_ARGLIST:
1776
1777 /* Remember that in F77, functions, substring ops and
1778 array subscript operations cannot be disambiguated
1779 at parse time. We have made all array subscript operations,
1780 substring operations as well as function calls come here
1781 and we now have to discover what the heck this thing actually was.
1782 If it is a function, we process just as if we got an OP_FUNCALL. */
1783
1784 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1785 (*pos) += 2;
1786
1787 /* First determine the type code we are dealing with. */
1788 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1789 type = check_typedef (value_type (arg1));
1790 code = TYPE_CODE (type);
1791
1792 if (code == TYPE_CODE_PTR)
1793 {
1794 /* Fortran always passes variable to subroutines as pointer.
1795 So we need to look into its target type to see if it is
1796 array, string or function. If it is, we need to switch
1797 to the target value the original one points to. */
1798 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1799
1800 if (TYPE_CODE (target_type) == TYPE_CODE_ARRAY
1801 || TYPE_CODE (target_type) == TYPE_CODE_STRING
1802 || TYPE_CODE (target_type) == TYPE_CODE_FUNC)
1803 {
1804 arg1 = value_ind (arg1);
1805 type = check_typedef (value_type (arg1));
1806 code = TYPE_CODE (type);
1807 }
1808 }
1809
1810 switch (code)
1811 {
1812 case TYPE_CODE_ARRAY:
1813 if (exp->elts[*pos].opcode == OP_F90_RANGE)
1814 return value_f90_subarray (arg1, exp, pos, noside);
1815 else
1816 goto multi_f77_subscript;
1817
1818 case TYPE_CODE_STRING:
1819 if (exp->elts[*pos].opcode == OP_F90_RANGE)
1820 return value_f90_subarray (arg1, exp, pos, noside);
1821 else
1822 {
1823 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1824 return value_subscript (arg1, value_as_long (arg2));
1825 }
1826
1827 case TYPE_CODE_PTR:
1828 case TYPE_CODE_FUNC:
1829 /* It's a function call. */
1830 /* Allocate arg vector, including space for the function to be
1831 called in argvec[0] and a terminating NULL. */
1832 argvec = (struct value **)
1833 alloca (sizeof (struct value *) * (nargs + 2));
1834 argvec[0] = arg1;
1835 tem = 1;
1836 for (; tem <= nargs; tem++)
1837 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1838 argvec[tem] = 0; /* signal end of arglist */
1839 if (noside == EVAL_SKIP)
1840 goto nosideret;
1841 goto do_call_it;
1842
1843 default:
1844 error (_("Cannot perform substring on this type"));
1845 }
1846
1847 case OP_COMPLEX:
1848 /* We have a complex number, There should be 2 floating
1849 point numbers that compose it. */
1850 (*pos) += 2;
1851 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1852 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1853
1854 return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
1855
1856 case STRUCTOP_STRUCT:
1857 tem = longest_to_int (exp->elts[pc + 1].longconst);
1858 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1859 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1860 if (noside == EVAL_SKIP)
1861 goto nosideret;
1862 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1863 NULL, "structure");
1864 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1865 arg3 = value_zero (value_type (arg3), not_lval);
1866 return arg3;
1867
1868 case STRUCTOP_PTR:
1869 tem = longest_to_int (exp->elts[pc + 1].longconst);
1870 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1871 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1872 if (noside == EVAL_SKIP)
1873 goto nosideret;
1874
1875 /* Check to see if operator '->' has been overloaded. If so replace
1876 arg1 with the value returned by evaluating operator->(). */
1877 while (unop_user_defined_p (op, arg1))
1878 {
1879 struct value *value = NULL;
1880 TRY
1881 {
1882 value = value_x_unop (arg1, op, noside);
1883 }
1884
1885 CATCH (except, RETURN_MASK_ERROR)
1886 {
1887 if (except.error == NOT_FOUND_ERROR)
1888 break;
1889 else
1890 throw_exception (except);
1891 }
1892 END_CATCH
1893
1894 arg1 = value;
1895 }
1896
1897 /* JYG: if print object is on we need to replace the base type
1898 with rtti type in order to continue on with successful
1899 lookup of member / method only available in the rtti type. */
1900 {
1901 struct type *type = value_type (arg1);
1902 struct type *real_type;
1903 int full, top, using_enc;
1904 struct value_print_options opts;
1905
1906 get_user_print_options (&opts);
1907 if (opts.objectprint && TYPE_TARGET_TYPE(type)
1908 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
1909 {
1910 real_type = value_rtti_indirect_type (arg1, &full, &top,
1911 &using_enc);
1912 if (real_type)
1913 arg1 = value_cast (real_type, arg1);
1914 }
1915 }
1916
1917 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1918 NULL, "structure pointer");
1919 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1920 arg3 = value_zero (value_type (arg3), not_lval);
1921 return arg3;
1922
1923 case STRUCTOP_MEMBER:
1924 case STRUCTOP_MPTR:
1925 if (op == STRUCTOP_MEMBER)
1926 arg1 = evaluate_subexp_for_address (exp, pos, noside);
1927 else
1928 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1929
1930 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1931
1932 if (noside == EVAL_SKIP)
1933 goto nosideret;
1934
1935 type = check_typedef (value_type (arg2));
1936 switch (TYPE_CODE (type))
1937 {
1938 case TYPE_CODE_METHODPTR:
1939 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1940 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1941 else
1942 {
1943 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1944 gdb_assert (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR);
1945 return value_ind (arg2);
1946 }
1947
1948 case TYPE_CODE_MEMBERPTR:
1949 /* Now, convert these values to an address. */
1950 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1951 arg1, 1);
1952
1953 mem_offset = value_as_long (arg2);
1954
1955 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1956 value_as_long (arg1) + mem_offset);
1957 return value_ind (arg3);
1958
1959 default:
1960 error (_("non-pointer-to-member value used "
1961 "in pointer-to-member construct"));
1962 }
1963
1964 case TYPE_INSTANCE:
1965 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1966 arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
1967 for (ix = 0; ix < nargs; ++ix)
1968 arg_types[ix] = exp->elts[pc + 1 + ix + 1].type;
1969
1970 expect_type = make_params (nargs, arg_types);
1971 *(pos) += 3 + nargs;
1972 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
1973 xfree (TYPE_FIELDS (expect_type));
1974 xfree (TYPE_MAIN_TYPE (expect_type));
1975 xfree (expect_type);
1976 return arg1;
1977
1978 case BINOP_CONCAT:
1979 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1980 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1981 if (noside == EVAL_SKIP)
1982 goto nosideret;
1983 if (binop_user_defined_p (op, arg1, arg2))
1984 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1985 else
1986 return value_concat (arg1, arg2);
1987
1988 case BINOP_ASSIGN:
1989 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1990 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1991
1992 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1993 return arg1;
1994 if (binop_user_defined_p (op, arg1, arg2))
1995 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1996 else
1997 return value_assign (arg1, arg2);
1998
1999 case BINOP_ASSIGN_MODIFY:
2000 (*pos) += 2;
2001 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2002 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2003 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2004 return arg1;
2005 op = exp->elts[pc + 1].opcode;
2006 if (binop_user_defined_p (op, arg1, arg2))
2007 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
2008 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
2009 value_type (arg1))
2010 && is_integral_type (value_type (arg2)))
2011 arg2 = value_ptradd (arg1, value_as_long (arg2));
2012 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
2013 value_type (arg1))
2014 && is_integral_type (value_type (arg2)))
2015 arg2 = value_ptradd (arg1, - value_as_long (arg2));
2016 else
2017 {
2018 struct value *tmp = arg1;
2019
2020 /* For shift and integer exponentiation operations,
2021 only promote the first argument. */
2022 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2023 && is_integral_type (value_type (arg2)))
2024 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
2025 else
2026 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2027
2028 arg2 = value_binop (tmp, arg2, op);
2029 }
2030 return value_assign (arg1, arg2);
2031
2032 case BINOP_ADD:
2033 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2034 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2035 if (noside == EVAL_SKIP)
2036 goto nosideret;
2037 if (binop_user_defined_p (op, arg1, arg2))
2038 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2039 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2040 && is_integral_type (value_type (arg2)))
2041 return value_ptradd (arg1, value_as_long (arg2));
2042 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
2043 && is_integral_type (value_type (arg1)))
2044 return value_ptradd (arg2, value_as_long (arg1));
2045 else
2046 {
2047 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2048 return value_binop (arg1, arg2, BINOP_ADD);
2049 }
2050
2051 case BINOP_SUB:
2052 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2053 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2054 if (noside == EVAL_SKIP)
2055 goto nosideret;
2056 if (binop_user_defined_p (op, arg1, arg2))
2057 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2058 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2059 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
2060 {
2061 /* FIXME -- should be ptrdiff_t */
2062 type = builtin_type (exp->gdbarch)->builtin_long;
2063 return value_from_longest (type, value_ptrdiff (arg1, arg2));
2064 }
2065 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2066 && is_integral_type (value_type (arg2)))
2067 return value_ptradd (arg1, - value_as_long (arg2));
2068 else
2069 {
2070 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2071 return value_binop (arg1, arg2, BINOP_SUB);
2072 }
2073
2074 case BINOP_EXP:
2075 case BINOP_MUL:
2076 case BINOP_DIV:
2077 case BINOP_INTDIV:
2078 case BINOP_REM:
2079 case BINOP_MOD:
2080 case BINOP_LSH:
2081 case BINOP_RSH:
2082 case BINOP_BITWISE_AND:
2083 case BINOP_BITWISE_IOR:
2084 case BINOP_BITWISE_XOR:
2085 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2086 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2087 if (noside == EVAL_SKIP)
2088 goto nosideret;
2089 if (binop_user_defined_p (op, arg1, arg2))
2090 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2091 else
2092 {
2093 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2094 fudge arg2 to avoid division-by-zero, the caller is
2095 (theoretically) only looking for the type of the result. */
2096 if (noside == EVAL_AVOID_SIDE_EFFECTS
2097 /* ??? Do we really want to test for BINOP_MOD here?
2098 The implementation of value_binop gives it a well-defined
2099 value. */
2100 && (op == BINOP_DIV
2101 || op == BINOP_INTDIV
2102 || op == BINOP_REM
2103 || op == BINOP_MOD)
2104 && value_logical_not (arg2))
2105 {
2106 struct value *v_one, *retval;
2107
2108 v_one = value_one (value_type (arg2));
2109 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
2110 retval = value_binop (arg1, v_one, op);
2111 return retval;
2112 }
2113 else
2114 {
2115 /* For shift and integer exponentiation operations,
2116 only promote the first argument. */
2117 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2118 && is_integral_type (value_type (arg2)))
2119 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2120 else
2121 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2122
2123 return value_binop (arg1, arg2, op);
2124 }
2125 }
2126
2127 case BINOP_SUBSCRIPT:
2128 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2129 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2130 if (noside == EVAL_SKIP)
2131 goto nosideret;
2132 if (binop_user_defined_p (op, arg1, arg2))
2133 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2134 else
2135 {
2136 /* If the user attempts to subscript something that is not an
2137 array or pointer type (like a plain int variable for example),
2138 then report this as an error. */
2139
2140 arg1 = coerce_ref (arg1);
2141 type = check_typedef (value_type (arg1));
2142 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2143 && TYPE_CODE (type) != TYPE_CODE_PTR)
2144 {
2145 if (TYPE_NAME (type))
2146 error (_("cannot subscript something of type `%s'"),
2147 TYPE_NAME (type));
2148 else
2149 error (_("cannot subscript requested type"));
2150 }
2151
2152 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2153 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
2154 else
2155 return value_subscript (arg1, value_as_long (arg2));
2156 }
2157 case MULTI_SUBSCRIPT:
2158 (*pos) += 2;
2159 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2160 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2161 while (nargs-- > 0)
2162 {
2163 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2164 /* FIXME: EVAL_SKIP handling may not be correct. */
2165 if (noside == EVAL_SKIP)
2166 {
2167 if (nargs > 0)
2168 {
2169 continue;
2170 }
2171 else
2172 {
2173 goto nosideret;
2174 }
2175 }
2176 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
2177 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2178 {
2179 /* If the user attempts to subscript something that has no target
2180 type (like a plain int variable for example), then report this
2181 as an error. */
2182
2183 type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
2184 if (type != NULL)
2185 {
2186 arg1 = value_zero (type, VALUE_LVAL (arg1));
2187 noside = EVAL_SKIP;
2188 continue;
2189 }
2190 else
2191 {
2192 error (_("cannot subscript something of type `%s'"),
2193 TYPE_NAME (value_type (arg1)));
2194 }
2195 }
2196
2197 if (binop_user_defined_p (op, arg1, arg2))
2198 {
2199 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2200 }
2201 else
2202 {
2203 arg1 = coerce_ref (arg1);
2204 type = check_typedef (value_type (arg1));
2205
2206 switch (TYPE_CODE (type))
2207 {
2208 case TYPE_CODE_PTR:
2209 case TYPE_CODE_ARRAY:
2210 case TYPE_CODE_STRING:
2211 arg1 = value_subscript (arg1, value_as_long (arg2));
2212 break;
2213
2214 default:
2215 if (TYPE_NAME (type))
2216 error (_("cannot subscript something of type `%s'"),
2217 TYPE_NAME (type));
2218 else
2219 error (_("cannot subscript requested type"));
2220 }
2221 }
2222 }
2223 return (arg1);
2224
2225 multi_f77_subscript:
2226 {
2227 LONGEST subscript_array[MAX_FORTRAN_DIMS];
2228 int ndimensions = 1, i;
2229 struct value *array = arg1;
2230
2231 if (nargs > MAX_FORTRAN_DIMS)
2232 error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
2233
2234 ndimensions = calc_f77_array_dims (type);
2235
2236 if (nargs != ndimensions)
2237 error (_("Wrong number of subscripts"));
2238
2239 gdb_assert (nargs > 0);
2240
2241 /* Now that we know we have a legal array subscript expression
2242 let us actually find out where this element exists in the array. */
2243
2244 /* Take array indices left to right. */
2245 for (i = 0; i < nargs; i++)
2246 {
2247 /* Evaluate each subscript; it must be a legal integer in F77. */
2248 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2249
2250 /* Fill in the subscript array. */
2251
2252 subscript_array[i] = value_as_long (arg2);
2253 }
2254
2255 /* Internal type of array is arranged right to left. */
2256 for (i = nargs; i > 0; i--)
2257 {
2258 struct type *array_type = check_typedef (value_type (array));
2259 LONGEST index = subscript_array[i - 1];
2260
2261 array = value_subscripted_rvalue (array, index,
2262 f77_get_lowerbound (array_type));
2263 }
2264
2265 return array;
2266 }
2267
2268 case BINOP_LOGICAL_AND:
2269 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2270 if (noside == EVAL_SKIP)
2271 {
2272 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2273 goto nosideret;
2274 }
2275
2276 oldpos = *pos;
2277 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2278 *pos = oldpos;
2279
2280 if (binop_user_defined_p (op, arg1, arg2))
2281 {
2282 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2283 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2284 }
2285 else
2286 {
2287 tem = value_logical_not (arg1);
2288 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2289 (tem ? EVAL_SKIP : noside));
2290 type = language_bool_type (exp->language_defn, exp->gdbarch);
2291 return value_from_longest (type,
2292 (LONGEST) (!tem && !value_logical_not (arg2)));
2293 }
2294
2295 case BINOP_LOGICAL_OR:
2296 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2297 if (noside == EVAL_SKIP)
2298 {
2299 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2300 goto nosideret;
2301 }
2302
2303 oldpos = *pos;
2304 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2305 *pos = oldpos;
2306
2307 if (binop_user_defined_p (op, arg1, arg2))
2308 {
2309 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2310 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2311 }
2312 else
2313 {
2314 tem = value_logical_not (arg1);
2315 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2316 (!tem ? EVAL_SKIP : noside));
2317 type = language_bool_type (exp->language_defn, exp->gdbarch);
2318 return value_from_longest (type,
2319 (LONGEST) (!tem || !value_logical_not (arg2)));
2320 }
2321
2322 case BINOP_EQUAL:
2323 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2324 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2325 if (noside == EVAL_SKIP)
2326 goto nosideret;
2327 if (binop_user_defined_p (op, arg1, arg2))
2328 {
2329 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2330 }
2331 else
2332 {
2333 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2334 tem = value_equal (arg1, arg2);
2335 type = language_bool_type (exp->language_defn, exp->gdbarch);
2336 return value_from_longest (type, (LONGEST) tem);
2337 }
2338
2339 case BINOP_NOTEQUAL:
2340 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2341 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2342 if (noside == EVAL_SKIP)
2343 goto nosideret;
2344 if (binop_user_defined_p (op, arg1, arg2))
2345 {
2346 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2347 }
2348 else
2349 {
2350 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2351 tem = value_equal (arg1, arg2);
2352 type = language_bool_type (exp->language_defn, exp->gdbarch);
2353 return value_from_longest (type, (LONGEST) ! tem);
2354 }
2355
2356 case BINOP_LESS:
2357 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2358 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2359 if (noside == EVAL_SKIP)
2360 goto nosideret;
2361 if (binop_user_defined_p (op, arg1, arg2))
2362 {
2363 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2364 }
2365 else
2366 {
2367 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2368 tem = value_less (arg1, arg2);
2369 type = language_bool_type (exp->language_defn, exp->gdbarch);
2370 return value_from_longest (type, (LONGEST) tem);
2371 }
2372
2373 case BINOP_GTR:
2374 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2375 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2376 if (noside == EVAL_SKIP)
2377 goto nosideret;
2378 if (binop_user_defined_p (op, arg1, arg2))
2379 {
2380 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2381 }
2382 else
2383 {
2384 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2385 tem = value_less (arg2, arg1);
2386 type = language_bool_type (exp->language_defn, exp->gdbarch);
2387 return value_from_longest (type, (LONGEST) tem);
2388 }
2389
2390 case BINOP_GEQ:
2391 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2392 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2393 if (noside == EVAL_SKIP)
2394 goto nosideret;
2395 if (binop_user_defined_p (op, arg1, arg2))
2396 {
2397 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2398 }
2399 else
2400 {
2401 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2402 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2403 type = language_bool_type (exp->language_defn, exp->gdbarch);
2404 return value_from_longest (type, (LONGEST) tem);
2405 }
2406
2407 case BINOP_LEQ:
2408 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2409 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2410 if (noside == EVAL_SKIP)
2411 goto nosideret;
2412 if (binop_user_defined_p (op, arg1, arg2))
2413 {
2414 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2415 }
2416 else
2417 {
2418 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2419 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2420 type = language_bool_type (exp->language_defn, exp->gdbarch);
2421 return value_from_longest (type, (LONGEST) tem);
2422 }
2423
2424 case BINOP_REPEAT:
2425 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2426 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2427 if (noside == EVAL_SKIP)
2428 goto nosideret;
2429 type = check_typedef (value_type (arg2));
2430 if (TYPE_CODE (type) != TYPE_CODE_INT)
2431 error (_("Non-integral right operand for \"@\" operator."));
2432 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2433 {
2434 return allocate_repeat_value (value_type (arg1),
2435 longest_to_int (value_as_long (arg2)));
2436 }
2437 else
2438 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2439
2440 case BINOP_COMMA:
2441 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2442 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2443
2444 case UNOP_PLUS:
2445 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2446 if (noside == EVAL_SKIP)
2447 goto nosideret;
2448 if (unop_user_defined_p (op, arg1))
2449 return value_x_unop (arg1, op, noside);
2450 else
2451 {
2452 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2453 return value_pos (arg1);
2454 }
2455
2456 case UNOP_NEG:
2457 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2458 if (noside == EVAL_SKIP)
2459 goto nosideret;
2460 if (unop_user_defined_p (op, arg1))
2461 return value_x_unop (arg1, op, noside);
2462 else
2463 {
2464 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2465 return value_neg (arg1);
2466 }
2467
2468 case UNOP_COMPLEMENT:
2469 /* C++: check for and handle destructor names. */
2470 op = exp->elts[*pos].opcode;
2471
2472 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2473 if (noside == EVAL_SKIP)
2474 goto nosideret;
2475 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2476 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2477 else
2478 {
2479 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2480 return value_complement (arg1);
2481 }
2482
2483 case UNOP_LOGICAL_NOT:
2484 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2485 if (noside == EVAL_SKIP)
2486 goto nosideret;
2487 if (unop_user_defined_p (op, arg1))
2488 return value_x_unop (arg1, op, noside);
2489 else
2490 {
2491 type = language_bool_type (exp->language_defn, exp->gdbarch);
2492 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2493 }
2494
2495 case UNOP_IND:
2496 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
2497 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2498 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2499 type = check_typedef (value_type (arg1));
2500 if (TYPE_CODE (type) == TYPE_CODE_METHODPTR
2501 || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
2502 error (_("Attempt to dereference pointer "
2503 "to member without an object"));
2504 if (noside == EVAL_SKIP)
2505 goto nosideret;
2506 if (unop_user_defined_p (op, arg1))
2507 return value_x_unop (arg1, op, noside);
2508 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2509 {
2510 type = check_typedef (value_type (arg1));
2511 if (TYPE_CODE (type) == TYPE_CODE_PTR
2512 || TYPE_CODE (type) == TYPE_CODE_REF
2513 /* In C you can dereference an array to get the 1st elt. */
2514 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2515 )
2516 return value_zero (TYPE_TARGET_TYPE (type),
2517 lval_memory);
2518 else if (TYPE_CODE (type) == TYPE_CODE_INT)
2519 /* GDB allows dereferencing an int. */
2520 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2521 lval_memory);
2522 else
2523 error (_("Attempt to take contents of a non-pointer value."));
2524 }
2525
2526 /* Allow * on an integer so we can cast it to whatever we want.
2527 This returns an int, which seems like the most C-like thing to
2528 do. "long long" variables are rare enough that
2529 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2530 if (TYPE_CODE (type) == TYPE_CODE_INT)
2531 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2532 (CORE_ADDR) value_as_address (arg1));
2533 return value_ind (arg1);
2534
2535 case UNOP_ADDR:
2536 /* C++: check for and handle pointer to members. */
2537
2538 op = exp->elts[*pos].opcode;
2539
2540 if (noside == EVAL_SKIP)
2541 {
2542 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2543 goto nosideret;
2544 }
2545 else
2546 {
2547 struct value *retvalp = evaluate_subexp_for_address (exp, pos,
2548 noside);
2549
2550 return retvalp;
2551 }
2552
2553 case UNOP_SIZEOF:
2554 if (noside == EVAL_SKIP)
2555 {
2556 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2557 goto nosideret;
2558 }
2559 return evaluate_subexp_for_sizeof (exp, pos, noside);
2560
2561 case UNOP_CAST:
2562 (*pos) += 2;
2563 type = exp->elts[pc + 1].type;
2564 arg1 = evaluate_subexp (type, exp, pos, noside);
2565 if (noside == EVAL_SKIP)
2566 goto nosideret;
2567 if (type != value_type (arg1))
2568 arg1 = value_cast (type, arg1);
2569 return arg1;
2570
2571 case UNOP_CAST_TYPE:
2572 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2573 type = value_type (arg1);
2574 arg1 = evaluate_subexp (type, exp, pos, noside);
2575 if (noside == EVAL_SKIP)
2576 goto nosideret;
2577 if (type != value_type (arg1))
2578 arg1 = value_cast (type, arg1);
2579 return arg1;
2580
2581 case UNOP_DYNAMIC_CAST:
2582 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2583 type = value_type (arg1);
2584 arg1 = evaluate_subexp (type, exp, pos, noside);
2585 if (noside == EVAL_SKIP)
2586 goto nosideret;
2587 return value_dynamic_cast (type, arg1);
2588
2589 case UNOP_REINTERPRET_CAST:
2590 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2591 type = value_type (arg1);
2592 arg1 = evaluate_subexp (type, exp, pos, noside);
2593 if (noside == EVAL_SKIP)
2594 goto nosideret;
2595 return value_reinterpret_cast (type, arg1);
2596
2597 case UNOP_MEMVAL:
2598 (*pos) += 2;
2599 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2600 if (noside == EVAL_SKIP)
2601 goto nosideret;
2602 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2603 return value_zero (exp->elts[pc + 1].type, lval_memory);
2604 else
2605 return value_at_lazy (exp->elts[pc + 1].type,
2606 value_as_address (arg1));
2607
2608 case UNOP_MEMVAL_TYPE:
2609 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2610 type = value_type (arg1);
2611 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2612 if (noside == EVAL_SKIP)
2613 goto nosideret;
2614 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2615 return value_zero (type, lval_memory);
2616 else
2617 return value_at_lazy (type, value_as_address (arg1));
2618
2619 case UNOP_MEMVAL_TLS:
2620 (*pos) += 3;
2621 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2622 if (noside == EVAL_SKIP)
2623 goto nosideret;
2624 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2625 return value_zero (exp->elts[pc + 2].type, lval_memory);
2626 else
2627 {
2628 CORE_ADDR tls_addr;
2629
2630 tls_addr = target_translate_tls_address (exp->elts[pc + 1].objfile,
2631 value_as_address (arg1));
2632 return value_at_lazy (exp->elts[pc + 2].type, tls_addr);
2633 }
2634
2635 case UNOP_PREINCREMENT:
2636 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2637 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2638 return arg1;
2639 else if (unop_user_defined_p (op, arg1))
2640 {
2641 return value_x_unop (arg1, op, noside);
2642 }
2643 else
2644 {
2645 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2646 arg2 = value_ptradd (arg1, 1);
2647 else
2648 {
2649 struct value *tmp = arg1;
2650
2651 arg2 = value_one (value_type (arg1));
2652 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2653 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2654 }
2655
2656 return value_assign (arg1, arg2);
2657 }
2658
2659 case UNOP_PREDECREMENT:
2660 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2661 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2662 return arg1;
2663 else if (unop_user_defined_p (op, arg1))
2664 {
2665 return value_x_unop (arg1, op, noside);
2666 }
2667 else
2668 {
2669 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2670 arg2 = value_ptradd (arg1, -1);
2671 else
2672 {
2673 struct value *tmp = arg1;
2674
2675 arg2 = value_one (value_type (arg1));
2676 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2677 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2678 }
2679
2680 return value_assign (arg1, arg2);
2681 }
2682
2683 case UNOP_POSTINCREMENT:
2684 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2685 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2686 return arg1;
2687 else if (unop_user_defined_p (op, arg1))
2688 {
2689 return value_x_unop (arg1, op, noside);
2690 }
2691 else
2692 {
2693 arg3 = value_non_lval (arg1);
2694
2695 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2696 arg2 = value_ptradd (arg1, 1);
2697 else
2698 {
2699 struct value *tmp = arg1;
2700
2701 arg2 = value_one (value_type (arg1));
2702 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2703 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2704 }
2705
2706 value_assign (arg1, arg2);
2707 return arg3;
2708 }
2709
2710 case UNOP_POSTDECREMENT:
2711 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2712 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2713 return arg1;
2714 else if (unop_user_defined_p (op, arg1))
2715 {
2716 return value_x_unop (arg1, op, noside);
2717 }
2718 else
2719 {
2720 arg3 = value_non_lval (arg1);
2721
2722 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2723 arg2 = value_ptradd (arg1, -1);
2724 else
2725 {
2726 struct value *tmp = arg1;
2727
2728 arg2 = value_one (value_type (arg1));
2729 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2730 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2731 }
2732
2733 value_assign (arg1, arg2);
2734 return arg3;
2735 }
2736
2737 case OP_THIS:
2738 (*pos) += 1;
2739 return value_of_this (exp->language_defn);
2740
2741 case OP_TYPE:
2742 /* The value is not supposed to be used. This is here to make it
2743 easier to accommodate expressions that contain types. */
2744 (*pos) += 2;
2745 if (noside == EVAL_SKIP)
2746 goto nosideret;
2747 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2748 {
2749 struct type *type = exp->elts[pc + 1].type;
2750
2751 /* If this is a typedef, then find its immediate target. We
2752 use check_typedef to resolve stubs, but we ignore its
2753 result because we do not want to dig past all
2754 typedefs. */
2755 check_typedef (type);
2756 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2757 type = TYPE_TARGET_TYPE (type);
2758 return allocate_value (type);
2759 }
2760 else
2761 error (_("Attempt to use a type name as an expression"));
2762
2763 case OP_TYPEOF:
2764 case OP_DECLTYPE:
2765 if (noside == EVAL_SKIP)
2766 {
2767 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2768 goto nosideret;
2769 }
2770 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2771 {
2772 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2773 struct value *result;
2774
2775 result = evaluate_subexp (NULL_TYPE, exp, pos,
2776 EVAL_AVOID_SIDE_EFFECTS);
2777
2778 /* 'decltype' has special semantics for lvalues. */
2779 if (op == OP_DECLTYPE
2780 && (sub_op == BINOP_SUBSCRIPT
2781 || sub_op == STRUCTOP_MEMBER
2782 || sub_op == STRUCTOP_MPTR
2783 || sub_op == UNOP_IND
2784 || sub_op == STRUCTOP_STRUCT
2785 || sub_op == STRUCTOP_PTR
2786 || sub_op == OP_SCOPE))
2787 {
2788 struct type *type = value_type (result);
2789
2790 if (TYPE_CODE (check_typedef (type)) != TYPE_CODE_REF)
2791 {
2792 type = lookup_reference_type (type);
2793 result = allocate_value (type);
2794 }
2795 }
2796
2797 return result;
2798 }
2799 else
2800 error (_("Attempt to use a type as an expression"));
2801
2802 case OP_TYPEID:
2803 {
2804 struct value *result;
2805 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2806
2807 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2808 result = evaluate_subexp (NULL_TYPE, exp, pos,
2809 EVAL_AVOID_SIDE_EFFECTS);
2810 else
2811 result = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2812
2813 if (noside != EVAL_NORMAL)
2814 return allocate_value (cplus_typeid_type (exp->gdbarch));
2815
2816 return cplus_typeid (result);
2817 }
2818
2819 default:
2820 /* Removing this case and compiling with gcc -Wall reveals that
2821 a lot of cases are hitting this case. Some of these should
2822 probably be removed from expression.h; others are legitimate
2823 expressions which are (apparently) not fully implemented.
2824
2825 If there are any cases landing here which mean a user error,
2826 then they should be separate cases, with more descriptive
2827 error messages. */
2828
2829 error (_("GDB does not (yet) know how to "
2830 "evaluate that kind of expression"));
2831 }
2832
2833 nosideret:
2834 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
2835 }
2836 \f
2837 /* Evaluate a subexpression of EXP, at index *POS,
2838 and return the address of that subexpression.
2839 Advance *POS over the subexpression.
2840 If the subexpression isn't an lvalue, get an error.
2841 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2842 then only the type of the result need be correct. */
2843
2844 static struct value *
2845 evaluate_subexp_for_address (struct expression *exp, int *pos,
2846 enum noside noside)
2847 {
2848 enum exp_opcode op;
2849 int pc;
2850 struct symbol *var;
2851 struct value *x;
2852 int tem;
2853
2854 pc = (*pos);
2855 op = exp->elts[pc].opcode;
2856
2857 switch (op)
2858 {
2859 case UNOP_IND:
2860 (*pos)++;
2861 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2862
2863 /* We can't optimize out "&*" if there's a user-defined operator*. */
2864 if (unop_user_defined_p (op, x))
2865 {
2866 x = value_x_unop (x, op, noside);
2867 goto default_case_after_eval;
2868 }
2869
2870 return coerce_array (x);
2871
2872 case UNOP_MEMVAL:
2873 (*pos) += 3;
2874 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2875 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2876
2877 case UNOP_MEMVAL_TYPE:
2878 {
2879 struct type *type;
2880
2881 (*pos) += 1;
2882 x = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2883 type = value_type (x);
2884 return value_cast (lookup_pointer_type (type),
2885 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2886 }
2887
2888 case OP_VAR_VALUE:
2889 var = exp->elts[pc + 2].symbol;
2890
2891 /* C++: The "address" of a reference should yield the address
2892 * of the object pointed to. Let value_addr() deal with it. */
2893 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
2894 goto default_case;
2895
2896 (*pos) += 4;
2897 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2898 {
2899 struct type *type =
2900 lookup_pointer_type (SYMBOL_TYPE (var));
2901 enum address_class sym_class = SYMBOL_CLASS (var);
2902
2903 if (sym_class == LOC_CONST
2904 || sym_class == LOC_CONST_BYTES
2905 || sym_class == LOC_REGISTER)
2906 error (_("Attempt to take address of register or constant."));
2907
2908 return
2909 value_zero (type, not_lval);
2910 }
2911 else
2912 return address_of_variable (var, exp->elts[pc + 1].block);
2913
2914 case OP_SCOPE:
2915 tem = longest_to_int (exp->elts[pc + 2].longconst);
2916 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
2917 x = value_aggregate_elt (exp->elts[pc + 1].type,
2918 &exp->elts[pc + 3].string,
2919 NULL, 1, noside);
2920 if (x == NULL)
2921 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
2922 return x;
2923
2924 default:
2925 default_case:
2926 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2927 default_case_after_eval:
2928 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2929 {
2930 struct type *type = check_typedef (value_type (x));
2931
2932 if (TYPE_CODE (type) == TYPE_CODE_REF)
2933 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2934 not_lval);
2935 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2936 return value_zero (lookup_pointer_type (value_type (x)),
2937 not_lval);
2938 else
2939 error (_("Attempt to take address of "
2940 "value not located in memory."));
2941 }
2942 return value_addr (x);
2943 }
2944 }
2945
2946 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2947 When used in contexts where arrays will be coerced anyway, this is
2948 equivalent to `evaluate_subexp' but much faster because it avoids
2949 actually fetching array contents (perhaps obsolete now that we have
2950 value_lazy()).
2951
2952 Note that we currently only do the coercion for C expressions, where
2953 arrays are zero based and the coercion is correct. For other languages,
2954 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2955 to decide if coercion is appropriate. */
2956
2957 struct value *
2958 evaluate_subexp_with_coercion (struct expression *exp,
2959 int *pos, enum noside noside)
2960 {
2961 enum exp_opcode op;
2962 int pc;
2963 struct value *val;
2964 struct symbol *var;
2965 struct type *type;
2966
2967 pc = (*pos);
2968 op = exp->elts[pc].opcode;
2969
2970 switch (op)
2971 {
2972 case OP_VAR_VALUE:
2973 var = exp->elts[pc + 2].symbol;
2974 type = check_typedef (SYMBOL_TYPE (var));
2975 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
2976 && !TYPE_VECTOR (type)
2977 && CAST_IS_CONVERSION (exp->language_defn))
2978 {
2979 (*pos) += 4;
2980 val = address_of_variable (var, exp->elts[pc + 1].block);
2981 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2982 val);
2983 }
2984 /* FALLTHROUGH */
2985
2986 default:
2987 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2988 }
2989 }
2990
2991 /* Evaluate a subexpression of EXP, at index *POS,
2992 and return a value for the size of that subexpression.
2993 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
2994 we allow side-effects on the operand if its type is a variable
2995 length array. */
2996
2997 static struct value *
2998 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
2999 enum noside noside)
3000 {
3001 /* FIXME: This should be size_t. */
3002 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
3003 enum exp_opcode op;
3004 int pc;
3005 struct type *type;
3006 struct value *val;
3007
3008 pc = (*pos);
3009 op = exp->elts[pc].opcode;
3010
3011 switch (op)
3012 {
3013 /* This case is handled specially
3014 so that we avoid creating a value for the result type.
3015 If the result type is very big, it's desirable not to
3016 create a value unnecessarily. */
3017 case UNOP_IND:
3018 (*pos)++;
3019 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3020 type = check_typedef (value_type (val));
3021 if (TYPE_CODE (type) != TYPE_CODE_PTR
3022 && TYPE_CODE (type) != TYPE_CODE_REF
3023 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
3024 error (_("Attempt to take contents of a non-pointer value."));
3025 type = TYPE_TARGET_TYPE (type);
3026 if (is_dynamic_type (type))
3027 type = value_type (value_ind (val));
3028 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3029
3030 case UNOP_MEMVAL:
3031 (*pos) += 3;
3032 type = exp->elts[pc + 1].type;
3033 break;
3034
3035 case UNOP_MEMVAL_TYPE:
3036 (*pos) += 1;
3037 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3038 type = value_type (val);
3039 break;
3040
3041 case OP_VAR_VALUE:
3042 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
3043 if (is_dynamic_type (type))
3044 {
3045 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3046 type = value_type (val);
3047 }
3048 else
3049 (*pos) += 4;
3050 break;
3051
3052 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3053 type of the subscript is a variable length array type. In this case we
3054 must re-evaluate the right hand side of the subcription to allow
3055 side-effects. */
3056 case BINOP_SUBSCRIPT:
3057 if (noside == EVAL_NORMAL)
3058 {
3059 int pc = (*pos) + 1;
3060
3061 val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
3062 type = check_typedef (value_type (val));
3063 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3064 {
3065 type = check_typedef (TYPE_TARGET_TYPE (type));
3066 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3067 {
3068 type = TYPE_INDEX_TYPE (type);
3069 /* Only re-evaluate the right hand side if the resulting type
3070 is a variable length type. */
3071 if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
3072 {
3073 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3074 return value_from_longest
3075 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
3076 }
3077 }
3078 }
3079 }
3080
3081 /* Fall through. */
3082
3083 default:
3084 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3085 type = value_type (val);
3086 break;
3087 }
3088
3089 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3090 "When applied to a reference or a reference type, the result is
3091 the size of the referenced type." */
3092 type = check_typedef (type);
3093 if (exp->language_defn->la_language == language_cplus
3094 && TYPE_CODE (type) == TYPE_CODE_REF)
3095 type = check_typedef (TYPE_TARGET_TYPE (type));
3096 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3097 }
3098
3099 /* Parse a type expression in the string [P..P+LENGTH). */
3100
3101 struct type *
3102 parse_and_eval_type (char *p, int length)
3103 {
3104 char *tmp = (char *) alloca (length + 4);
3105 struct expression *expr;
3106
3107 tmp[0] = '(';
3108 memcpy (tmp + 1, p, length);
3109 tmp[length + 1] = ')';
3110 tmp[length + 2] = '0';
3111 tmp[length + 3] = '\0';
3112 expr = parse_expression (tmp);
3113 if (expr->elts[0].opcode != UNOP_CAST)
3114 error (_("Internal error in eval_type."));
3115 return expr->elts[1].type;
3116 }
3117
3118 int
3119 calc_f77_array_dims (struct type *array_type)
3120 {
3121 int ndimen = 1;
3122 struct type *tmp_type;
3123
3124 if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
3125 error (_("Can't get dimensions for a non-array type"));
3126
3127 tmp_type = array_type;
3128
3129 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
3130 {
3131 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
3132 ++ndimen;
3133 }
3134 return ndimen;
3135 }
This page took 0.132004 seconds and 5 git commands to generate.