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