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