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