gdb: Don't leak memory with TYPE_ALLOC / TYPE_ZALLOC
[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 nargs, argvec + 1);
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 struct value *
1242 evaluate_subexp_standard (struct type *expect_type,
1243 struct expression *exp, int *pos,
1244 enum noside noside)
1245 {
1246 enum exp_opcode op;
1247 int tem, tem2, tem3;
1248 int pc, oldpos;
1249 struct value *arg1 = NULL;
1250 struct value *arg2 = NULL;
1251 struct value *arg3;
1252 struct type *type;
1253 int nargs;
1254 struct value **argvec;
1255 int code;
1256 int ix;
1257 long mem_offset;
1258 struct type **arg_types;
1259
1260 pc = (*pos)++;
1261 op = exp->elts[pc].opcode;
1262
1263 switch (op)
1264 {
1265 case OP_SCOPE:
1266 tem = longest_to_int (exp->elts[pc + 2].longconst);
1267 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
1268 if (noside == EVAL_SKIP)
1269 return eval_skip_value (exp);
1270 arg1 = value_aggregate_elt (exp->elts[pc + 1].type,
1271 &exp->elts[pc + 3].string,
1272 expect_type, 0, noside);
1273 if (arg1 == NULL)
1274 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
1275 return arg1;
1276
1277 case OP_LONG:
1278 (*pos) += 3;
1279 return value_from_longest (exp->elts[pc + 1].type,
1280 exp->elts[pc + 2].longconst);
1281
1282 case OP_FLOAT:
1283 (*pos) += 3;
1284 return value_from_contents (exp->elts[pc + 1].type,
1285 exp->elts[pc + 2].floatconst);
1286
1287 case OP_ADL_FUNC:
1288 case OP_VAR_VALUE:
1289 (*pos) += 3;
1290 if (noside == EVAL_SKIP)
1291 return eval_skip_value (exp);
1292
1293 {
1294 symbol *var = exp->elts[pc + 2].symbol;
1295 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ERROR)
1296 error_unknown_type (SYMBOL_PRINT_NAME (var));
1297
1298 return evaluate_var_value (noside, exp->elts[pc + 1].block, var);
1299 }
1300
1301 case OP_VAR_MSYM_VALUE:
1302 {
1303 (*pos) += 3;
1304
1305 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
1306 value *val = evaluate_var_msym_value (noside,
1307 exp->elts[pc + 1].objfile,
1308 msymbol);
1309
1310 type = value_type (val);
1311 if (TYPE_CODE (type) == TYPE_CODE_ERROR
1312 && (noside != EVAL_AVOID_SIDE_EFFECTS || pc != 0))
1313 error_unknown_type (MSYMBOL_PRINT_NAME (msymbol));
1314 return val;
1315 }
1316
1317 case OP_VAR_ENTRY_VALUE:
1318 (*pos) += 2;
1319 if (noside == EVAL_SKIP)
1320 return eval_skip_value (exp);
1321
1322 {
1323 struct symbol *sym = exp->elts[pc + 1].symbol;
1324 struct frame_info *frame;
1325
1326 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1327 return value_zero (SYMBOL_TYPE (sym), not_lval);
1328
1329 if (SYMBOL_COMPUTED_OPS (sym) == NULL
1330 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
1331 error (_("Symbol \"%s\" does not have any specific entry value"),
1332 SYMBOL_PRINT_NAME (sym));
1333
1334 frame = get_selected_frame (NULL);
1335 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1336 }
1337
1338 case OP_FUNC_STATIC_VAR:
1339 tem = longest_to_int (exp->elts[pc + 1].longconst);
1340 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1341 if (noside == EVAL_SKIP)
1342 return eval_skip_value (exp);
1343
1344 {
1345 value *func = evaluate_subexp_standard (NULL, exp, pos, noside);
1346 CORE_ADDR addr = value_address (func);
1347
1348 const block *blk = block_for_pc (addr);
1349 const char *var = &exp->elts[pc + 2].string;
1350
1351 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1352
1353 if (sym.symbol == NULL)
1354 error (_("No symbol \"%s\" in specified context."), var);
1355
1356 return evaluate_var_value (noside, sym.block, sym.symbol);
1357 }
1358
1359 case OP_LAST:
1360 (*pos) += 2;
1361 return
1362 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
1363
1364 case OP_REGISTER:
1365 {
1366 const char *name = &exp->elts[pc + 2].string;
1367 int regno;
1368 struct value *val;
1369
1370 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
1371 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1372 name, strlen (name));
1373 if (regno == -1)
1374 error (_("Register $%s not available."), name);
1375
1376 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1377 a value with the appropriate register type. Unfortunately,
1378 we don't have easy access to the type of user registers.
1379 So for these registers, we fetch the register value regardless
1380 of the evaluation mode. */
1381 if (noside == EVAL_AVOID_SIDE_EFFECTS
1382 && regno < gdbarch_num_regs (exp->gdbarch)
1383 + gdbarch_num_pseudo_regs (exp->gdbarch))
1384 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
1385 else
1386 val = value_of_register (regno, get_selected_frame (NULL));
1387 if (val == NULL)
1388 error (_("Value of register %s not available."), name);
1389 else
1390 return val;
1391 }
1392 case OP_BOOL:
1393 (*pos) += 2;
1394 type = language_bool_type (exp->language_defn, exp->gdbarch);
1395 return value_from_longest (type, exp->elts[pc + 1].longconst);
1396
1397 case OP_INTERNALVAR:
1398 (*pos) += 2;
1399 return value_of_internalvar (exp->gdbarch,
1400 exp->elts[pc + 1].internalvar);
1401
1402 case OP_STRING:
1403 tem = longest_to_int (exp->elts[pc + 1].longconst);
1404 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1405 if (noside == EVAL_SKIP)
1406 return eval_skip_value (exp);
1407 type = language_string_char_type (exp->language_defn, exp->gdbarch);
1408 return value_string (&exp->elts[pc + 2].string, tem, type);
1409
1410 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
1411 NSString constant. */
1412 tem = longest_to_int (exp->elts[pc + 1].longconst);
1413 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1414 if (noside == EVAL_SKIP)
1415 return eval_skip_value (exp);
1416 return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
1417
1418 case OP_ARRAY:
1419 (*pos) += 3;
1420 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
1421 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
1422 nargs = tem3 - tem2 + 1;
1423 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
1424
1425 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
1426 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
1427 {
1428 struct value *rec = allocate_value (expect_type);
1429
1430 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
1431 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
1432 }
1433
1434 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
1435 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
1436 {
1437 struct type *range_type = TYPE_INDEX_TYPE (type);
1438 struct type *element_type = TYPE_TARGET_TYPE (type);
1439 struct value *array = allocate_value (expect_type);
1440 int element_size = TYPE_LENGTH (check_typedef (element_type));
1441 LONGEST low_bound, high_bound, index;
1442
1443 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
1444 {
1445 low_bound = 0;
1446 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
1447 }
1448 index = low_bound;
1449 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
1450 for (tem = nargs; --nargs >= 0;)
1451 {
1452 struct value *element;
1453 int index_pc = 0;
1454
1455 element = evaluate_subexp (element_type, exp, pos, noside);
1456 if (value_type (element) != element_type)
1457 element = value_cast (element_type, element);
1458 if (index_pc)
1459 {
1460 int continue_pc = *pos;
1461
1462 *pos = index_pc;
1463 index = init_array_element (array, element, exp, pos, noside,
1464 low_bound, high_bound);
1465 *pos = continue_pc;
1466 }
1467 else
1468 {
1469 if (index > high_bound)
1470 /* To avoid memory corruption. */
1471 error (_("Too many array elements"));
1472 memcpy (value_contents_raw (array)
1473 + (index - low_bound) * element_size,
1474 value_contents (element),
1475 element_size);
1476 }
1477 index++;
1478 }
1479 return array;
1480 }
1481
1482 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
1483 && TYPE_CODE (type) == TYPE_CODE_SET)
1484 {
1485 struct value *set = allocate_value (expect_type);
1486 gdb_byte *valaddr = value_contents_raw (set);
1487 struct type *element_type = TYPE_INDEX_TYPE (type);
1488 struct type *check_type = element_type;
1489 LONGEST low_bound, high_bound;
1490
1491 /* Get targettype of elementtype. */
1492 while (TYPE_CODE (check_type) == TYPE_CODE_RANGE
1493 || TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
1494 check_type = TYPE_TARGET_TYPE (check_type);
1495
1496 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
1497 error (_("(power)set type with unknown size"));
1498 memset (valaddr, '\0', TYPE_LENGTH (type));
1499 for (tem = 0; tem < nargs; tem++)
1500 {
1501 LONGEST range_low, range_high;
1502 struct type *range_low_type, *range_high_type;
1503 struct value *elem_val;
1504
1505 elem_val = evaluate_subexp (element_type, exp, pos, noside);
1506 range_low_type = range_high_type = value_type (elem_val);
1507 range_low = range_high = value_as_long (elem_val);
1508
1509 /* Check types of elements to avoid mixture of elements from
1510 different types. Also check if type of element is "compatible"
1511 with element type of powerset. */
1512 if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
1513 range_low_type = TYPE_TARGET_TYPE (range_low_type);
1514 if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
1515 range_high_type = TYPE_TARGET_TYPE (range_high_type);
1516 if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type))
1517 || (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM
1518 && (range_low_type != range_high_type)))
1519 /* different element modes. */
1520 error (_("POWERSET tuple elements of different mode"));
1521 if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type))
1522 || (TYPE_CODE (check_type) == TYPE_CODE_ENUM
1523 && range_low_type != check_type))
1524 error (_("incompatible POWERSET tuple elements"));
1525 if (range_low > range_high)
1526 {
1527 warning (_("empty POWERSET tuple range"));
1528 continue;
1529 }
1530 if (range_low < low_bound || range_high > high_bound)
1531 error (_("POWERSET tuple element out of range"));
1532 range_low -= low_bound;
1533 range_high -= low_bound;
1534 for (; range_low <= range_high; range_low++)
1535 {
1536 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
1537
1538 if (gdbarch_bits_big_endian (exp->gdbarch))
1539 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
1540 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
1541 |= 1 << bit_index;
1542 }
1543 }
1544 return set;
1545 }
1546
1547 argvec = XALLOCAVEC (struct value *, nargs);
1548 for (tem = 0; tem < nargs; tem++)
1549 {
1550 /* Ensure that array expressions are coerced into pointer
1551 objects. */
1552 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1553 }
1554 if (noside == EVAL_SKIP)
1555 return eval_skip_value (exp);
1556 return value_array (tem2, tem3, argvec);
1557
1558 case TERNOP_SLICE:
1559 {
1560 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1561 int lowbound
1562 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1563 int upper
1564 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1565
1566 if (noside == EVAL_SKIP)
1567 return eval_skip_value (exp);
1568 return value_slice (array, lowbound, upper - lowbound + 1);
1569 }
1570
1571 case TERNOP_COND:
1572 /* Skip third and second args to evaluate the first one. */
1573 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1574 if (value_logical_not (arg1))
1575 {
1576 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1577 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1578 }
1579 else
1580 {
1581 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1582 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1583 return arg2;
1584 }
1585
1586 case OP_OBJC_SELECTOR:
1587 { /* Objective C @selector operator. */
1588 char *sel = &exp->elts[pc + 2].string;
1589 int len = longest_to_int (exp->elts[pc + 1].longconst);
1590 struct type *selector_type;
1591
1592 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
1593 if (noside == EVAL_SKIP)
1594 return eval_skip_value (exp);
1595
1596 if (sel[len] != 0)
1597 sel[len] = 0; /* Make sure it's terminated. */
1598
1599 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1600 return value_from_longest (selector_type,
1601 lookup_child_selector (exp->gdbarch, sel));
1602 }
1603
1604 case OP_OBJC_MSGCALL:
1605 { /* Objective C message (method) call. */
1606
1607 CORE_ADDR responds_selector = 0;
1608 CORE_ADDR method_selector = 0;
1609
1610 CORE_ADDR selector = 0;
1611
1612 int struct_return = 0;
1613 enum noside sub_no_side = EVAL_NORMAL;
1614
1615 struct value *msg_send = NULL;
1616 struct value *msg_send_stret = NULL;
1617 int gnu_runtime = 0;
1618
1619 struct value *target = NULL;
1620 struct value *method = NULL;
1621 struct value *called_method = NULL;
1622
1623 struct type *selector_type = NULL;
1624 struct type *long_type;
1625
1626 struct value *ret = NULL;
1627 CORE_ADDR addr = 0;
1628
1629 selector = exp->elts[pc + 1].longconst;
1630 nargs = exp->elts[pc + 2].longconst;
1631 argvec = XALLOCAVEC (struct value *, nargs + 5);
1632
1633 (*pos) += 3;
1634
1635 long_type = builtin_type (exp->gdbarch)->builtin_long;
1636 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1637
1638 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1639 sub_no_side = EVAL_NORMAL;
1640 else
1641 sub_no_side = noside;
1642
1643 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
1644
1645 if (value_as_long (target) == 0)
1646 return value_from_longest (long_type, 0);
1647
1648 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1649 gnu_runtime = 1;
1650
1651 /* Find the method dispatch (Apple runtime) or method lookup
1652 (GNU runtime) function for Objective-C. These will be used
1653 to lookup the symbol information for the method. If we
1654 can't find any symbol information, then we'll use these to
1655 call the method, otherwise we can call the method
1656 directly. The msg_send_stret function is used in the special
1657 case of a method that returns a structure (Apple runtime
1658 only). */
1659 if (gnu_runtime)
1660 {
1661 struct type *type = selector_type;
1662
1663 type = lookup_function_type (type);
1664 type = lookup_pointer_type (type);
1665 type = lookup_function_type (type);
1666 type = lookup_pointer_type (type);
1667
1668 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1669 msg_send_stret
1670 = find_function_in_inferior ("objc_msg_lookup", NULL);
1671
1672 msg_send = value_from_pointer (type, value_as_address (msg_send));
1673 msg_send_stret = value_from_pointer (type,
1674 value_as_address (msg_send_stret));
1675 }
1676 else
1677 {
1678 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1679 /* Special dispatcher for methods returning structs. */
1680 msg_send_stret
1681 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1682 }
1683
1684 /* Verify the target object responds to this method. The
1685 standard top-level 'Object' class uses a different name for
1686 the verification method than the non-standard, but more
1687 often used, 'NSObject' class. Make sure we check for both. */
1688
1689 responds_selector
1690 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1691 if (responds_selector == 0)
1692 responds_selector
1693 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1694
1695 if (responds_selector == 0)
1696 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1697
1698 method_selector
1699 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1700 if (method_selector == 0)
1701 method_selector
1702 = lookup_child_selector (exp->gdbarch, "methodFor:");
1703
1704 if (method_selector == 0)
1705 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1706
1707 /* Call the verification method, to make sure that the target
1708 class implements the desired method. */
1709
1710 argvec[0] = msg_send;
1711 argvec[1] = target;
1712 argvec[2] = value_from_longest (long_type, responds_selector);
1713 argvec[3] = value_from_longest (long_type, selector);
1714 argvec[4] = 0;
1715
1716 ret = call_function_by_hand (argvec[0], NULL, 3, argvec + 1);
1717 if (gnu_runtime)
1718 {
1719 /* Function objc_msg_lookup returns a pointer. */
1720 argvec[0] = ret;
1721 ret = call_function_by_hand (argvec[0], NULL, 3, argvec + 1);
1722 }
1723 if (value_as_long (ret) == 0)
1724 error (_("Target does not respond to this message selector."));
1725
1726 /* Call "methodForSelector:" method, to get the address of a
1727 function method that implements this selector for this
1728 class. If we can find a symbol at that address, then we
1729 know the return type, parameter types etc. (that's a good
1730 thing). */
1731
1732 argvec[0] = msg_send;
1733 argvec[1] = target;
1734 argvec[2] = value_from_longest (long_type, method_selector);
1735 argvec[3] = value_from_longest (long_type, selector);
1736 argvec[4] = 0;
1737
1738 ret = call_function_by_hand (argvec[0], NULL, 3, argvec + 1);
1739 if (gnu_runtime)
1740 {
1741 argvec[0] = ret;
1742 ret = call_function_by_hand (argvec[0], NULL, 3, argvec + 1);
1743 }
1744
1745 /* ret should now be the selector. */
1746
1747 addr = value_as_long (ret);
1748 if (addr)
1749 {
1750 struct symbol *sym = NULL;
1751
1752 /* The address might point to a function descriptor;
1753 resolve it to the actual code address instead. */
1754 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1755 current_top_target ());
1756
1757 /* Is it a high_level symbol? */
1758 sym = find_pc_function (addr);
1759 if (sym != NULL)
1760 method = value_of_variable (sym, 0);
1761 }
1762
1763 /* If we found a method with symbol information, check to see
1764 if it returns a struct. Otherwise assume it doesn't. */
1765
1766 if (method)
1767 {
1768 CORE_ADDR funaddr;
1769 struct type *val_type;
1770
1771 funaddr = find_function_addr (method, &val_type);
1772
1773 block_for_pc (funaddr);
1774
1775 val_type = check_typedef (val_type);
1776
1777 if ((val_type == NULL)
1778 || (TYPE_CODE(val_type) == TYPE_CODE_ERROR))
1779 {
1780 if (expect_type != NULL)
1781 val_type = expect_type;
1782 }
1783
1784 struct_return = using_struct_return (exp->gdbarch, method,
1785 val_type);
1786 }
1787 else if (expect_type != NULL)
1788 {
1789 struct_return = using_struct_return (exp->gdbarch, NULL,
1790 check_typedef (expect_type));
1791 }
1792
1793 /* Found a function symbol. Now we will substitute its
1794 value in place of the message dispatcher (obj_msgSend),
1795 so that we call the method directly instead of thru
1796 the dispatcher. The main reason for doing this is that
1797 we can now evaluate the return value and parameter values
1798 according to their known data types, in case we need to
1799 do things like promotion, dereferencing, special handling
1800 of structs and doubles, etc.
1801
1802 We want to use the type signature of 'method', but still
1803 jump to objc_msgSend() or objc_msgSend_stret() to better
1804 mimic the behavior of the runtime. */
1805
1806 if (method)
1807 {
1808 if (TYPE_CODE (value_type (method)) != TYPE_CODE_FUNC)
1809 error (_("method address has symbol information "
1810 "with non-function type; skipping"));
1811
1812 /* Create a function pointer of the appropriate type, and
1813 replace its value with the value of msg_send or
1814 msg_send_stret. We must use a pointer here, as
1815 msg_send and msg_send_stret are of pointer type, and
1816 the representation may be different on systems that use
1817 function descriptors. */
1818 if (struct_return)
1819 called_method
1820 = value_from_pointer (lookup_pointer_type (value_type (method)),
1821 value_as_address (msg_send_stret));
1822 else
1823 called_method
1824 = value_from_pointer (lookup_pointer_type (value_type (method)),
1825 value_as_address (msg_send));
1826 }
1827 else
1828 {
1829 if (struct_return)
1830 called_method = msg_send_stret;
1831 else
1832 called_method = msg_send;
1833 }
1834
1835 if (noside == EVAL_SKIP)
1836 return eval_skip_value (exp);
1837
1838 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1839 {
1840 /* If the return type doesn't look like a function type,
1841 call an error. This can happen if somebody tries to
1842 turn a variable into a function call. This is here
1843 because people often want to call, eg, strcmp, which
1844 gdb doesn't know is a function. If gdb isn't asked for
1845 it's opinion (ie. through "whatis"), it won't offer
1846 it. */
1847
1848 struct type *type = value_type (called_method);
1849
1850 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1851 type = TYPE_TARGET_TYPE (type);
1852 type = TYPE_TARGET_TYPE (type);
1853
1854 if (type)
1855 {
1856 if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type)
1857 return allocate_value (expect_type);
1858 else
1859 return allocate_value (type);
1860 }
1861 else
1862 error (_("Expression of type other than "
1863 "\"method returning ...\" used as a method"));
1864 }
1865
1866 /* Now depending on whether we found a symbol for the method,
1867 we will either call the runtime dispatcher or the method
1868 directly. */
1869
1870 argvec[0] = called_method;
1871 argvec[1] = target;
1872 argvec[2] = value_from_longest (long_type, selector);
1873 /* User-supplied arguments. */
1874 for (tem = 0; tem < nargs; tem++)
1875 argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
1876 argvec[tem + 3] = 0;
1877
1878 if (gnu_runtime && (method != NULL))
1879 {
1880 /* Function objc_msg_lookup returns a pointer. */
1881 deprecated_set_value_type (argvec[0],
1882 lookup_pointer_type (lookup_function_type (value_type (argvec[0]))));
1883 argvec[0]
1884 = call_function_by_hand (argvec[0], NULL, nargs + 2, argvec + 1);
1885 }
1886
1887 ret = call_function_by_hand (argvec[0], NULL, nargs + 2, argvec + 1);
1888 return ret;
1889 }
1890 break;
1891
1892 case OP_FUNCALL:
1893 return evaluate_funcall (expect_type, exp, pos, noside);
1894
1895 case OP_F77_UNDETERMINED_ARGLIST:
1896
1897 /* Remember that in F77, functions, substring ops and
1898 array subscript operations cannot be disambiguated
1899 at parse time. We have made all array subscript operations,
1900 substring operations as well as function calls come here
1901 and we now have to discover what the heck this thing actually was.
1902 If it is a function, we process just as if we got an OP_FUNCALL. */
1903
1904 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1905 (*pos) += 2;
1906
1907 /* First determine the type code we are dealing with. */
1908 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1909 type = check_typedef (value_type (arg1));
1910 code = TYPE_CODE (type);
1911
1912 if (code == TYPE_CODE_PTR)
1913 {
1914 /* Fortran always passes variable to subroutines as pointer.
1915 So we need to look into its target type to see if it is
1916 array, string or function. If it is, we need to switch
1917 to the target value the original one points to. */
1918 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1919
1920 if (TYPE_CODE (target_type) == TYPE_CODE_ARRAY
1921 || TYPE_CODE (target_type) == TYPE_CODE_STRING
1922 || TYPE_CODE (target_type) == TYPE_CODE_FUNC)
1923 {
1924 arg1 = value_ind (arg1);
1925 type = check_typedef (value_type (arg1));
1926 code = TYPE_CODE (type);
1927 }
1928 }
1929
1930 switch (code)
1931 {
1932 case TYPE_CODE_ARRAY:
1933 if (exp->elts[*pos].opcode == OP_RANGE)
1934 return value_f90_subarray (arg1, exp, pos, noside);
1935 else
1936 goto multi_f77_subscript;
1937
1938 case TYPE_CODE_STRING:
1939 if (exp->elts[*pos].opcode == OP_RANGE)
1940 return value_f90_subarray (arg1, exp, pos, noside);
1941 else
1942 {
1943 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1944 return value_subscript (arg1, value_as_long (arg2));
1945 }
1946
1947 case TYPE_CODE_PTR:
1948 case TYPE_CODE_FUNC:
1949 /* It's a function call. */
1950 /* Allocate arg vector, including space for the function to be
1951 called in argvec[0] and a terminating NULL. */
1952 argvec = (struct value **)
1953 alloca (sizeof (struct value *) * (nargs + 2));
1954 argvec[0] = arg1;
1955 tem = 1;
1956 for (; tem <= nargs; tem++)
1957 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1958 argvec[tem] = 0; /* signal end of arglist */
1959 if (noside == EVAL_SKIP)
1960 return eval_skip_value (exp);
1961 return eval_call (exp, noside, nargs, argvec, NULL, expect_type);
1962
1963 default:
1964 error (_("Cannot perform substring on this type"));
1965 }
1966
1967 case OP_COMPLEX:
1968 /* We have a complex number, There should be 2 floating
1969 point numbers that compose it. */
1970 (*pos) += 2;
1971 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1972 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1973
1974 return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
1975
1976 case STRUCTOP_STRUCT:
1977 tem = longest_to_int (exp->elts[pc + 1].longconst);
1978 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1979 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1980 if (noside == EVAL_SKIP)
1981 return eval_skip_value (exp);
1982 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1983 NULL, "structure");
1984 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1985 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1986 return arg3;
1987
1988 case STRUCTOP_PTR:
1989 tem = longest_to_int (exp->elts[pc + 1].longconst);
1990 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1991 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1992 if (noside == EVAL_SKIP)
1993 return eval_skip_value (exp);
1994
1995 /* Check to see if operator '->' has been overloaded. If so replace
1996 arg1 with the value returned by evaluating operator->(). */
1997 while (unop_user_defined_p (op, arg1))
1998 {
1999 struct value *value = NULL;
2000 TRY
2001 {
2002 value = value_x_unop (arg1, op, noside);
2003 }
2004
2005 CATCH (except, RETURN_MASK_ERROR)
2006 {
2007 if (except.error == NOT_FOUND_ERROR)
2008 break;
2009 else
2010 throw_exception (except);
2011 }
2012 END_CATCH
2013
2014 arg1 = value;
2015 }
2016
2017 /* JYG: if print object is on we need to replace the base type
2018 with rtti type in order to continue on with successful
2019 lookup of member / method only available in the rtti type. */
2020 {
2021 struct type *type = value_type (arg1);
2022 struct type *real_type;
2023 int full, using_enc;
2024 LONGEST top;
2025 struct value_print_options opts;
2026
2027 get_user_print_options (&opts);
2028 if (opts.objectprint && TYPE_TARGET_TYPE(type)
2029 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
2030 {
2031 real_type = value_rtti_indirect_type (arg1, &full, &top,
2032 &using_enc);
2033 if (real_type)
2034 arg1 = value_cast (real_type, arg1);
2035 }
2036 }
2037
2038 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
2039 NULL, "structure pointer");
2040 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2041 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
2042 return arg3;
2043
2044 case STRUCTOP_MEMBER:
2045 case STRUCTOP_MPTR:
2046 if (op == STRUCTOP_MEMBER)
2047 arg1 = evaluate_subexp_for_address (exp, pos, noside);
2048 else
2049 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2050
2051 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2052
2053 if (noside == EVAL_SKIP)
2054 return eval_skip_value (exp);
2055
2056 type = check_typedef (value_type (arg2));
2057 switch (TYPE_CODE (type))
2058 {
2059 case TYPE_CODE_METHODPTR:
2060 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2061 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
2062 else
2063 {
2064 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
2065 gdb_assert (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR);
2066 return value_ind (arg2);
2067 }
2068
2069 case TYPE_CODE_MEMBERPTR:
2070 /* Now, convert these values to an address. */
2071 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
2072 arg1, 1);
2073
2074 mem_offset = value_as_long (arg2);
2075
2076 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2077 value_as_long (arg1) + mem_offset);
2078 return value_ind (arg3);
2079
2080 default:
2081 error (_("non-pointer-to-member value used "
2082 "in pointer-to-member construct"));
2083 }
2084
2085 case TYPE_INSTANCE:
2086 {
2087 type_instance_flags flags
2088 = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst);
2089 nargs = longest_to_int (exp->elts[pc + 2].longconst);
2090 arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
2091 for (ix = 0; ix < nargs; ++ix)
2092 arg_types[ix] = exp->elts[pc + 2 + ix + 1].type;
2093
2094 fake_method expect_type (flags, nargs, arg_types);
2095 *(pos) += 4 + nargs;
2096 return evaluate_subexp_standard (expect_type.type (), exp, pos, noside);
2097 }
2098
2099 case BINOP_CONCAT:
2100 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2101 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2102 if (noside == EVAL_SKIP)
2103 return eval_skip_value (exp);
2104 if (binop_user_defined_p (op, arg1, arg2))
2105 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2106 else
2107 return value_concat (arg1, arg2);
2108
2109 case BINOP_ASSIGN:
2110 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2111 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2112
2113 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2114 return arg1;
2115 if (binop_user_defined_p (op, arg1, arg2))
2116 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2117 else
2118 return value_assign (arg1, arg2);
2119
2120 case BINOP_ASSIGN_MODIFY:
2121 (*pos) += 2;
2122 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2123 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2124 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2125 return arg1;
2126 op = exp->elts[pc + 1].opcode;
2127 if (binop_user_defined_p (op, arg1, arg2))
2128 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
2129 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
2130 value_type (arg1))
2131 && is_integral_type (value_type (arg2)))
2132 arg2 = value_ptradd (arg1, value_as_long (arg2));
2133 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
2134 value_type (arg1))
2135 && is_integral_type (value_type (arg2)))
2136 arg2 = value_ptradd (arg1, - value_as_long (arg2));
2137 else
2138 {
2139 struct value *tmp = arg1;
2140
2141 /* For shift and integer exponentiation operations,
2142 only promote the first argument. */
2143 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2144 && is_integral_type (value_type (arg2)))
2145 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
2146 else
2147 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2148
2149 arg2 = value_binop (tmp, arg2, op);
2150 }
2151 return value_assign (arg1, arg2);
2152
2153 case BINOP_ADD:
2154 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2155 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2156 if (noside == EVAL_SKIP)
2157 return eval_skip_value (exp);
2158 if (binop_user_defined_p (op, arg1, arg2))
2159 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2160 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2161 && is_integral_type (value_type (arg2)))
2162 return value_ptradd (arg1, value_as_long (arg2));
2163 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
2164 && is_integral_type (value_type (arg1)))
2165 return value_ptradd (arg2, value_as_long (arg1));
2166 else
2167 {
2168 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2169 return value_binop (arg1, arg2, BINOP_ADD);
2170 }
2171
2172 case BINOP_SUB:
2173 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2174 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2175 if (noside == EVAL_SKIP)
2176 return eval_skip_value (exp);
2177 if (binop_user_defined_p (op, arg1, arg2))
2178 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2179 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2180 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
2181 {
2182 /* FIXME -- should be ptrdiff_t */
2183 type = builtin_type (exp->gdbarch)->builtin_long;
2184 return value_from_longest (type, value_ptrdiff (arg1, arg2));
2185 }
2186 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2187 && is_integral_type (value_type (arg2)))
2188 return value_ptradd (arg1, - value_as_long (arg2));
2189 else
2190 {
2191 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2192 return value_binop (arg1, arg2, BINOP_SUB);
2193 }
2194
2195 case BINOP_EXP:
2196 case BINOP_MUL:
2197 case BINOP_DIV:
2198 case BINOP_INTDIV:
2199 case BINOP_REM:
2200 case BINOP_MOD:
2201 case BINOP_LSH:
2202 case BINOP_RSH:
2203 case BINOP_BITWISE_AND:
2204 case BINOP_BITWISE_IOR:
2205 case BINOP_BITWISE_XOR:
2206 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2207 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2208 if (noside == EVAL_SKIP)
2209 return eval_skip_value (exp);
2210 if (binop_user_defined_p (op, arg1, arg2))
2211 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2212 else
2213 {
2214 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2215 fudge arg2 to avoid division-by-zero, the caller is
2216 (theoretically) only looking for the type of the result. */
2217 if (noside == EVAL_AVOID_SIDE_EFFECTS
2218 /* ??? Do we really want to test for BINOP_MOD here?
2219 The implementation of value_binop gives it a well-defined
2220 value. */
2221 && (op == BINOP_DIV
2222 || op == BINOP_INTDIV
2223 || op == BINOP_REM
2224 || op == BINOP_MOD)
2225 && value_logical_not (arg2))
2226 {
2227 struct value *v_one, *retval;
2228
2229 v_one = value_one (value_type (arg2));
2230 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
2231 retval = value_binop (arg1, v_one, op);
2232 return retval;
2233 }
2234 else
2235 {
2236 /* For shift and integer exponentiation operations,
2237 only promote the first argument. */
2238 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2239 && is_integral_type (value_type (arg2)))
2240 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2241 else
2242 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2243
2244 return value_binop (arg1, arg2, op);
2245 }
2246 }
2247
2248 case BINOP_SUBSCRIPT:
2249 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2250 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2251 if (noside == EVAL_SKIP)
2252 return eval_skip_value (exp);
2253 if (binop_user_defined_p (op, arg1, arg2))
2254 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2255 else
2256 {
2257 /* If the user attempts to subscript something that is not an
2258 array or pointer type (like a plain int variable for example),
2259 then report this as an error. */
2260
2261 arg1 = coerce_ref (arg1);
2262 type = check_typedef (value_type (arg1));
2263 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2264 && TYPE_CODE (type) != TYPE_CODE_PTR)
2265 {
2266 if (TYPE_NAME (type))
2267 error (_("cannot subscript something of type `%s'"),
2268 TYPE_NAME (type));
2269 else
2270 error (_("cannot subscript requested type"));
2271 }
2272
2273 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2274 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
2275 else
2276 return value_subscript (arg1, value_as_long (arg2));
2277 }
2278 case MULTI_SUBSCRIPT:
2279 (*pos) += 2;
2280 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2281 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2282 while (nargs-- > 0)
2283 {
2284 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2285 /* FIXME: EVAL_SKIP handling may not be correct. */
2286 if (noside == EVAL_SKIP)
2287 {
2288 if (nargs > 0)
2289 continue;
2290 return eval_skip_value (exp);
2291 }
2292 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
2293 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2294 {
2295 /* If the user attempts to subscript something that has no target
2296 type (like a plain int variable for example), then report this
2297 as an error. */
2298
2299 type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
2300 if (type != NULL)
2301 {
2302 arg1 = value_zero (type, VALUE_LVAL (arg1));
2303 noside = EVAL_SKIP;
2304 continue;
2305 }
2306 else
2307 {
2308 error (_("cannot subscript something of type `%s'"),
2309 TYPE_NAME (value_type (arg1)));
2310 }
2311 }
2312
2313 if (binop_user_defined_p (op, arg1, arg2))
2314 {
2315 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2316 }
2317 else
2318 {
2319 arg1 = coerce_ref (arg1);
2320 type = check_typedef (value_type (arg1));
2321
2322 switch (TYPE_CODE (type))
2323 {
2324 case TYPE_CODE_PTR:
2325 case TYPE_CODE_ARRAY:
2326 case TYPE_CODE_STRING:
2327 arg1 = value_subscript (arg1, value_as_long (arg2));
2328 break;
2329
2330 default:
2331 if (TYPE_NAME (type))
2332 error (_("cannot subscript something of type `%s'"),
2333 TYPE_NAME (type));
2334 else
2335 error (_("cannot subscript requested type"));
2336 }
2337 }
2338 }
2339 return (arg1);
2340
2341 multi_f77_subscript:
2342 {
2343 LONGEST subscript_array[MAX_FORTRAN_DIMS];
2344 int ndimensions = 1, i;
2345 struct value *array = arg1;
2346
2347 if (nargs > MAX_FORTRAN_DIMS)
2348 error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
2349
2350 ndimensions = calc_f77_array_dims (type);
2351
2352 if (nargs != ndimensions)
2353 error (_("Wrong number of subscripts"));
2354
2355 gdb_assert (nargs > 0);
2356
2357 /* Now that we know we have a legal array subscript expression
2358 let us actually find out where this element exists in the array. */
2359
2360 /* Take array indices left to right. */
2361 for (i = 0; i < nargs; i++)
2362 {
2363 /* Evaluate each subscript; it must be a legal integer in F77. */
2364 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2365
2366 /* Fill in the subscript array. */
2367
2368 subscript_array[i] = value_as_long (arg2);
2369 }
2370
2371 /* Internal type of array is arranged right to left. */
2372 for (i = nargs; i > 0; i--)
2373 {
2374 struct type *array_type = check_typedef (value_type (array));
2375 LONGEST index = subscript_array[i - 1];
2376
2377 array = value_subscripted_rvalue (array, index,
2378 f77_get_lowerbound (array_type));
2379 }
2380
2381 return array;
2382 }
2383
2384 case BINOP_LOGICAL_AND:
2385 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2386 if (noside == EVAL_SKIP)
2387 {
2388 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2389 return eval_skip_value (exp);
2390 }
2391
2392 oldpos = *pos;
2393 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2394 *pos = oldpos;
2395
2396 if (binop_user_defined_p (op, arg1, arg2))
2397 {
2398 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2399 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2400 }
2401 else
2402 {
2403 tem = value_logical_not (arg1);
2404 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2405 (tem ? EVAL_SKIP : noside));
2406 type = language_bool_type (exp->language_defn, exp->gdbarch);
2407 return value_from_longest (type,
2408 (LONGEST) (!tem && !value_logical_not (arg2)));
2409 }
2410
2411 case BINOP_LOGICAL_OR:
2412 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2413 if (noside == EVAL_SKIP)
2414 {
2415 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2416 return eval_skip_value (exp);
2417 }
2418
2419 oldpos = *pos;
2420 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2421 *pos = oldpos;
2422
2423 if (binop_user_defined_p (op, arg1, arg2))
2424 {
2425 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2426 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2427 }
2428 else
2429 {
2430 tem = value_logical_not (arg1);
2431 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2432 (!tem ? EVAL_SKIP : noside));
2433 type = language_bool_type (exp->language_defn, exp->gdbarch);
2434 return value_from_longest (type,
2435 (LONGEST) (!tem || !value_logical_not (arg2)));
2436 }
2437
2438 case BINOP_EQUAL:
2439 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2440 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2441 if (noside == EVAL_SKIP)
2442 return eval_skip_value (exp);
2443 if (binop_user_defined_p (op, arg1, arg2))
2444 {
2445 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2446 }
2447 else
2448 {
2449 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2450 tem = value_equal (arg1, arg2);
2451 type = language_bool_type (exp->language_defn, exp->gdbarch);
2452 return value_from_longest (type, (LONGEST) tem);
2453 }
2454
2455 case BINOP_NOTEQUAL:
2456 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2457 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2458 if (noside == EVAL_SKIP)
2459 return eval_skip_value (exp);
2460 if (binop_user_defined_p (op, arg1, arg2))
2461 {
2462 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2463 }
2464 else
2465 {
2466 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2467 tem = value_equal (arg1, arg2);
2468 type = language_bool_type (exp->language_defn, exp->gdbarch);
2469 return value_from_longest (type, (LONGEST) ! tem);
2470 }
2471
2472 case BINOP_LESS:
2473 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2474 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2475 if (noside == EVAL_SKIP)
2476 return eval_skip_value (exp);
2477 if (binop_user_defined_p (op, arg1, arg2))
2478 {
2479 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2480 }
2481 else
2482 {
2483 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2484 tem = value_less (arg1, arg2);
2485 type = language_bool_type (exp->language_defn, exp->gdbarch);
2486 return value_from_longest (type, (LONGEST) tem);
2487 }
2488
2489 case BINOP_GTR:
2490 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2491 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2492 if (noside == EVAL_SKIP)
2493 return eval_skip_value (exp);
2494 if (binop_user_defined_p (op, arg1, arg2))
2495 {
2496 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2497 }
2498 else
2499 {
2500 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2501 tem = value_less (arg2, arg1);
2502 type = language_bool_type (exp->language_defn, exp->gdbarch);
2503 return value_from_longest (type, (LONGEST) tem);
2504 }
2505
2506 case BINOP_GEQ:
2507 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2508 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2509 if (noside == EVAL_SKIP)
2510 return eval_skip_value (exp);
2511 if (binop_user_defined_p (op, arg1, arg2))
2512 {
2513 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2514 }
2515 else
2516 {
2517 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2518 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2519 type = language_bool_type (exp->language_defn, exp->gdbarch);
2520 return value_from_longest (type, (LONGEST) tem);
2521 }
2522
2523 case BINOP_LEQ:
2524 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2525 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2526 if (noside == EVAL_SKIP)
2527 return eval_skip_value (exp);
2528 if (binop_user_defined_p (op, arg1, arg2))
2529 {
2530 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2531 }
2532 else
2533 {
2534 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2535 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2536 type = language_bool_type (exp->language_defn, exp->gdbarch);
2537 return value_from_longest (type, (LONGEST) tem);
2538 }
2539
2540 case BINOP_REPEAT:
2541 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2542 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2543 if (noside == EVAL_SKIP)
2544 return eval_skip_value (exp);
2545 type = check_typedef (value_type (arg2));
2546 if (TYPE_CODE (type) != TYPE_CODE_INT
2547 && TYPE_CODE (type) != TYPE_CODE_ENUM)
2548 error (_("Non-integral right operand for \"@\" operator."));
2549 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2550 {
2551 return allocate_repeat_value (value_type (arg1),
2552 longest_to_int (value_as_long (arg2)));
2553 }
2554 else
2555 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2556
2557 case BINOP_COMMA:
2558 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2559 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2560
2561 case UNOP_PLUS:
2562 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2563 if (noside == EVAL_SKIP)
2564 return eval_skip_value (exp);
2565 if (unop_user_defined_p (op, arg1))
2566 return value_x_unop (arg1, op, noside);
2567 else
2568 {
2569 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2570 return value_pos (arg1);
2571 }
2572
2573 case UNOP_NEG:
2574 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2575 if (noside == EVAL_SKIP)
2576 return eval_skip_value (exp);
2577 if (unop_user_defined_p (op, arg1))
2578 return value_x_unop (arg1, op, noside);
2579 else
2580 {
2581 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2582 return value_neg (arg1);
2583 }
2584
2585 case UNOP_COMPLEMENT:
2586 /* C++: check for and handle destructor names. */
2587
2588 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2589 if (noside == EVAL_SKIP)
2590 return eval_skip_value (exp);
2591 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2592 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2593 else
2594 {
2595 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2596 return value_complement (arg1);
2597 }
2598
2599 case UNOP_LOGICAL_NOT:
2600 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2601 if (noside == EVAL_SKIP)
2602 return eval_skip_value (exp);
2603 if (unop_user_defined_p (op, arg1))
2604 return value_x_unop (arg1, op, noside);
2605 else
2606 {
2607 type = language_bool_type (exp->language_defn, exp->gdbarch);
2608 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2609 }
2610
2611 case UNOP_IND:
2612 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
2613 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2614 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2615 type = check_typedef (value_type (arg1));
2616 if (TYPE_CODE (type) == TYPE_CODE_METHODPTR
2617 || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
2618 error (_("Attempt to dereference pointer "
2619 "to member without an object"));
2620 if (noside == EVAL_SKIP)
2621 return eval_skip_value (exp);
2622 if (unop_user_defined_p (op, arg1))
2623 return value_x_unop (arg1, op, noside);
2624 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2625 {
2626 type = check_typedef (value_type (arg1));
2627 if (TYPE_CODE (type) == TYPE_CODE_PTR
2628 || TYPE_IS_REFERENCE (type)
2629 /* In C you can dereference an array to get the 1st elt. */
2630 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2631 )
2632 return value_zero (TYPE_TARGET_TYPE (type),
2633 lval_memory);
2634 else if (TYPE_CODE (type) == TYPE_CODE_INT)
2635 /* GDB allows dereferencing an int. */
2636 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2637 lval_memory);
2638 else
2639 error (_("Attempt to take contents of a non-pointer value."));
2640 }
2641
2642 /* Allow * on an integer so we can cast it to whatever we want.
2643 This returns an int, which seems like the most C-like thing to
2644 do. "long long" variables are rare enough that
2645 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2646 if (TYPE_CODE (type) == TYPE_CODE_INT)
2647 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2648 (CORE_ADDR) value_as_address (arg1));
2649 return value_ind (arg1);
2650
2651 case UNOP_ADDR:
2652 /* C++: check for and handle pointer to members. */
2653
2654 if (noside == EVAL_SKIP)
2655 {
2656 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2657 return eval_skip_value (exp);
2658 }
2659 else
2660 {
2661 struct value *retvalp = evaluate_subexp_for_address (exp, pos,
2662 noside);
2663
2664 return retvalp;
2665 }
2666
2667 case UNOP_SIZEOF:
2668 if (noside == EVAL_SKIP)
2669 {
2670 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2671 return eval_skip_value (exp);
2672 }
2673 return evaluate_subexp_for_sizeof (exp, pos, noside);
2674
2675 case UNOP_ALIGNOF:
2676 {
2677 struct type *type
2678 = value_type (evaluate_subexp (NULL_TYPE, exp, pos,
2679 EVAL_AVOID_SIDE_EFFECTS));
2680 /* FIXME: This should be size_t. */
2681 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2682 ULONGEST align = type_align (type);
2683 if (align == 0)
2684 error (_("could not determine alignment of type"));
2685 return value_from_longest (size_type, align);
2686 }
2687
2688 case UNOP_CAST:
2689 (*pos) += 2;
2690 type = exp->elts[pc + 1].type;
2691 return evaluate_subexp_for_cast (exp, pos, noside, type);
2692
2693 case UNOP_CAST_TYPE:
2694 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2695 type = value_type (arg1);
2696 return evaluate_subexp_for_cast (exp, pos, noside, type);
2697
2698 case UNOP_DYNAMIC_CAST:
2699 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2700 type = value_type (arg1);
2701 arg1 = evaluate_subexp (type, exp, pos, noside);
2702 if (noside == EVAL_SKIP)
2703 return eval_skip_value (exp);
2704 return value_dynamic_cast (type, arg1);
2705
2706 case UNOP_REINTERPRET_CAST:
2707 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2708 type = value_type (arg1);
2709 arg1 = evaluate_subexp (type, exp, pos, noside);
2710 if (noside == EVAL_SKIP)
2711 return eval_skip_value (exp);
2712 return value_reinterpret_cast (type, arg1);
2713
2714 case UNOP_MEMVAL:
2715 (*pos) += 2;
2716 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2717 if (noside == EVAL_SKIP)
2718 return eval_skip_value (exp);
2719 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2720 return value_zero (exp->elts[pc + 1].type, lval_memory);
2721 else
2722 return value_at_lazy (exp->elts[pc + 1].type,
2723 value_as_address (arg1));
2724
2725 case UNOP_MEMVAL_TYPE:
2726 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2727 type = value_type (arg1);
2728 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2729 if (noside == EVAL_SKIP)
2730 return eval_skip_value (exp);
2731 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2732 return value_zero (type, lval_memory);
2733 else
2734 return value_at_lazy (type, value_as_address (arg1));
2735
2736 case UNOP_PREINCREMENT:
2737 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2738 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2739 return arg1;
2740 else if (unop_user_defined_p (op, arg1))
2741 {
2742 return value_x_unop (arg1, op, noside);
2743 }
2744 else
2745 {
2746 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2747 arg2 = value_ptradd (arg1, 1);
2748 else
2749 {
2750 struct value *tmp = arg1;
2751
2752 arg2 = value_one (value_type (arg1));
2753 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2754 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2755 }
2756
2757 return value_assign (arg1, arg2);
2758 }
2759
2760 case UNOP_PREDECREMENT:
2761 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2762 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2763 return arg1;
2764 else if (unop_user_defined_p (op, arg1))
2765 {
2766 return value_x_unop (arg1, op, noside);
2767 }
2768 else
2769 {
2770 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2771 arg2 = value_ptradd (arg1, -1);
2772 else
2773 {
2774 struct value *tmp = arg1;
2775
2776 arg2 = value_one (value_type (arg1));
2777 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2778 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2779 }
2780
2781 return value_assign (arg1, arg2);
2782 }
2783
2784 case UNOP_POSTINCREMENT:
2785 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2786 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2787 return arg1;
2788 else if (unop_user_defined_p (op, arg1))
2789 {
2790 return value_x_unop (arg1, op, noside);
2791 }
2792 else
2793 {
2794 arg3 = value_non_lval (arg1);
2795
2796 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2797 arg2 = value_ptradd (arg1, 1);
2798 else
2799 {
2800 struct value *tmp = arg1;
2801
2802 arg2 = value_one (value_type (arg1));
2803 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2804 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2805 }
2806
2807 value_assign (arg1, arg2);
2808 return arg3;
2809 }
2810
2811 case UNOP_POSTDECREMENT:
2812 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2813 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2814 return arg1;
2815 else if (unop_user_defined_p (op, arg1))
2816 {
2817 return value_x_unop (arg1, op, noside);
2818 }
2819 else
2820 {
2821 arg3 = value_non_lval (arg1);
2822
2823 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2824 arg2 = value_ptradd (arg1, -1);
2825 else
2826 {
2827 struct value *tmp = arg1;
2828
2829 arg2 = value_one (value_type (arg1));
2830 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2831 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2832 }
2833
2834 value_assign (arg1, arg2);
2835 return arg3;
2836 }
2837
2838 case OP_THIS:
2839 (*pos) += 1;
2840 return value_of_this (exp->language_defn);
2841
2842 case OP_TYPE:
2843 /* The value is not supposed to be used. This is here to make it
2844 easier to accommodate expressions that contain types. */
2845 (*pos) += 2;
2846 if (noside == EVAL_SKIP)
2847 return eval_skip_value (exp);
2848 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2849 return allocate_value (exp->elts[pc + 1].type);
2850 else
2851 error (_("Attempt to use a type name as an expression"));
2852
2853 case OP_TYPEOF:
2854 case OP_DECLTYPE:
2855 if (noside == EVAL_SKIP)
2856 {
2857 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2858 return eval_skip_value (exp);
2859 }
2860 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2861 {
2862 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2863 struct value *result;
2864
2865 result = evaluate_subexp (NULL_TYPE, exp, pos,
2866 EVAL_AVOID_SIDE_EFFECTS);
2867
2868 /* 'decltype' has special semantics for lvalues. */
2869 if (op == OP_DECLTYPE
2870 && (sub_op == BINOP_SUBSCRIPT
2871 || sub_op == STRUCTOP_MEMBER
2872 || sub_op == STRUCTOP_MPTR
2873 || sub_op == UNOP_IND
2874 || sub_op == STRUCTOP_STRUCT
2875 || sub_op == STRUCTOP_PTR
2876 || sub_op == OP_SCOPE))
2877 {
2878 struct type *type = value_type (result);
2879
2880 if (!TYPE_IS_REFERENCE (type))
2881 {
2882 type = lookup_lvalue_reference_type (type);
2883 result = allocate_value (type);
2884 }
2885 }
2886
2887 return result;
2888 }
2889 else
2890 error (_("Attempt to use a type as an expression"));
2891
2892 case OP_TYPEID:
2893 {
2894 struct value *result;
2895 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2896
2897 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2898 result = evaluate_subexp (NULL_TYPE, exp, pos,
2899 EVAL_AVOID_SIDE_EFFECTS);
2900 else
2901 result = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2902
2903 if (noside != EVAL_NORMAL)
2904 return allocate_value (cplus_typeid_type (exp->gdbarch));
2905
2906 return cplus_typeid (result);
2907 }
2908
2909 default:
2910 /* Removing this case and compiling with gcc -Wall reveals that
2911 a lot of cases are hitting this case. Some of these should
2912 probably be removed from expression.h; others are legitimate
2913 expressions which are (apparently) not fully implemented.
2914
2915 If there are any cases landing here which mean a user error,
2916 then they should be separate cases, with more descriptive
2917 error messages. */
2918
2919 error (_("GDB does not (yet) know how to "
2920 "evaluate that kind of expression"));
2921 }
2922
2923 gdb_assert_not_reached ("missed return?");
2924 }
2925 \f
2926 /* Evaluate a subexpression of EXP, at index *POS,
2927 and return the address of that subexpression.
2928 Advance *POS over the subexpression.
2929 If the subexpression isn't an lvalue, get an error.
2930 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2931 then only the type of the result need be correct. */
2932
2933 static struct value *
2934 evaluate_subexp_for_address (struct expression *exp, int *pos,
2935 enum noside noside)
2936 {
2937 enum exp_opcode op;
2938 int pc;
2939 struct symbol *var;
2940 struct value *x;
2941 int tem;
2942
2943 pc = (*pos);
2944 op = exp->elts[pc].opcode;
2945
2946 switch (op)
2947 {
2948 case UNOP_IND:
2949 (*pos)++;
2950 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2951
2952 /* We can't optimize out "&*" if there's a user-defined operator*. */
2953 if (unop_user_defined_p (op, x))
2954 {
2955 x = value_x_unop (x, op, noside);
2956 goto default_case_after_eval;
2957 }
2958
2959 return coerce_array (x);
2960
2961 case UNOP_MEMVAL:
2962 (*pos) += 3;
2963 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2964 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2965
2966 case UNOP_MEMVAL_TYPE:
2967 {
2968 struct type *type;
2969
2970 (*pos) += 1;
2971 x = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2972 type = value_type (x);
2973 return value_cast (lookup_pointer_type (type),
2974 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2975 }
2976
2977 case OP_VAR_VALUE:
2978 var = exp->elts[pc + 2].symbol;
2979
2980 /* C++: The "address" of a reference should yield the address
2981 * of the object pointed to. Let value_addr() deal with it. */
2982 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
2983 goto default_case;
2984
2985 (*pos) += 4;
2986 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2987 {
2988 struct type *type =
2989 lookup_pointer_type (SYMBOL_TYPE (var));
2990 enum address_class sym_class = SYMBOL_CLASS (var);
2991
2992 if (sym_class == LOC_CONST
2993 || sym_class == LOC_CONST_BYTES
2994 || sym_class == LOC_REGISTER)
2995 error (_("Attempt to take address of register or constant."));
2996
2997 return
2998 value_zero (type, not_lval);
2999 }
3000 else
3001 return address_of_variable (var, exp->elts[pc + 1].block);
3002
3003 case OP_VAR_MSYM_VALUE:
3004 {
3005 (*pos) += 4;
3006
3007 value *val = evaluate_var_msym_value (noside,
3008 exp->elts[pc + 1].objfile,
3009 exp->elts[pc + 2].msymbol);
3010 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3011 {
3012 struct type *type = lookup_pointer_type (value_type (val));
3013 return value_zero (type, not_lval);
3014 }
3015 else
3016 return value_addr (val);
3017 }
3018
3019 case OP_SCOPE:
3020 tem = longest_to_int (exp->elts[pc + 2].longconst);
3021 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
3022 x = value_aggregate_elt (exp->elts[pc + 1].type,
3023 &exp->elts[pc + 3].string,
3024 NULL, 1, noside);
3025 if (x == NULL)
3026 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
3027 return x;
3028
3029 default:
3030 default_case:
3031 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
3032 default_case_after_eval:
3033 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3034 {
3035 struct type *type = check_typedef (value_type (x));
3036
3037 if (TYPE_IS_REFERENCE (type))
3038 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
3039 not_lval);
3040 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
3041 return value_zero (lookup_pointer_type (value_type (x)),
3042 not_lval);
3043 else
3044 error (_("Attempt to take address of "
3045 "value not located in memory."));
3046 }
3047 return value_addr (x);
3048 }
3049 }
3050
3051 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
3052 When used in contexts where arrays will be coerced anyway, this is
3053 equivalent to `evaluate_subexp' but much faster because it avoids
3054 actually fetching array contents (perhaps obsolete now that we have
3055 value_lazy()).
3056
3057 Note that we currently only do the coercion for C expressions, where
3058 arrays are zero based and the coercion is correct. For other languages,
3059 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
3060 to decide if coercion is appropriate. */
3061
3062 struct value *
3063 evaluate_subexp_with_coercion (struct expression *exp,
3064 int *pos, enum noside noside)
3065 {
3066 enum exp_opcode op;
3067 int pc;
3068 struct value *val;
3069 struct symbol *var;
3070 struct type *type;
3071
3072 pc = (*pos);
3073 op = exp->elts[pc].opcode;
3074
3075 switch (op)
3076 {
3077 case OP_VAR_VALUE:
3078 var = exp->elts[pc + 2].symbol;
3079 type = check_typedef (SYMBOL_TYPE (var));
3080 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
3081 && !TYPE_VECTOR (type)
3082 && CAST_IS_CONVERSION (exp->language_defn))
3083 {
3084 (*pos) += 4;
3085 val = address_of_variable (var, exp->elts[pc + 1].block);
3086 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
3087 val);
3088 }
3089 /* FALLTHROUGH */
3090
3091 default:
3092 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
3093 }
3094 }
3095
3096 /* Evaluate a subexpression of EXP, at index *POS,
3097 and return a value for the size of that subexpression.
3098 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
3099 we allow side-effects on the operand if its type is a variable
3100 length array. */
3101
3102 static struct value *
3103 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
3104 enum noside noside)
3105 {
3106 /* FIXME: This should be size_t. */
3107 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
3108 enum exp_opcode op;
3109 int pc;
3110 struct type *type;
3111 struct value *val;
3112
3113 pc = (*pos);
3114 op = exp->elts[pc].opcode;
3115
3116 switch (op)
3117 {
3118 /* This case is handled specially
3119 so that we avoid creating a value for the result type.
3120 If the result type is very big, it's desirable not to
3121 create a value unnecessarily. */
3122 case UNOP_IND:
3123 (*pos)++;
3124 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3125 type = check_typedef (value_type (val));
3126 if (TYPE_CODE (type) != TYPE_CODE_PTR
3127 && !TYPE_IS_REFERENCE (type)
3128 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
3129 error (_("Attempt to take contents of a non-pointer value."));
3130 type = TYPE_TARGET_TYPE (type);
3131 if (is_dynamic_type (type))
3132 type = value_type (value_ind (val));
3133 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3134
3135 case UNOP_MEMVAL:
3136 (*pos) += 3;
3137 type = exp->elts[pc + 1].type;
3138 break;
3139
3140 case UNOP_MEMVAL_TYPE:
3141 (*pos) += 1;
3142 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3143 type = value_type (val);
3144 break;
3145
3146 case OP_VAR_VALUE:
3147 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
3148 if (is_dynamic_type (type))
3149 {
3150 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3151 type = value_type (val);
3152 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
3153 && is_dynamic_type (TYPE_INDEX_TYPE (type))
3154 && TYPE_HIGH_BOUND_UNDEFINED (TYPE_INDEX_TYPE (type)))
3155 return allocate_optimized_out_value (size_type);
3156 }
3157 else
3158 (*pos) += 4;
3159 break;
3160
3161 case OP_VAR_MSYM_VALUE:
3162 {
3163 (*pos) += 4;
3164
3165 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
3166 value *val = evaluate_var_msym_value (noside,
3167 exp->elts[pc + 1].objfile,
3168 msymbol);
3169
3170 type = value_type (val);
3171 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
3172 error_unknown_type (MSYMBOL_PRINT_NAME (msymbol));
3173
3174 return value_from_longest (size_type, TYPE_LENGTH (type));
3175 }
3176 break;
3177
3178 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3179 type of the subscript is a variable length array type. In this case we
3180 must re-evaluate the right hand side of the subcription to allow
3181 side-effects. */
3182 case BINOP_SUBSCRIPT:
3183 if (noside == EVAL_NORMAL)
3184 {
3185 int pc = (*pos) + 1;
3186
3187 val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
3188 type = check_typedef (value_type (val));
3189 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3190 {
3191 type = check_typedef (TYPE_TARGET_TYPE (type));
3192 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3193 {
3194 type = TYPE_INDEX_TYPE (type);
3195 /* Only re-evaluate the right hand side if the resulting type
3196 is a variable length type. */
3197 if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
3198 {
3199 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3200 return value_from_longest
3201 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
3202 }
3203 }
3204 }
3205 }
3206
3207 /* Fall through. */
3208
3209 default:
3210 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3211 type = value_type (val);
3212 break;
3213 }
3214
3215 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3216 "When applied to a reference or a reference type, the result is
3217 the size of the referenced type." */
3218 type = check_typedef (type);
3219 if (exp->language_defn->la_language == language_cplus
3220 && (TYPE_IS_REFERENCE (type)))
3221 type = check_typedef (TYPE_TARGET_TYPE (type));
3222 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3223 }
3224
3225 /* Evaluate a subexpression of EXP, at index *POS, and return a value
3226 for that subexpression cast to TO_TYPE. Advance *POS over the
3227 subexpression. */
3228
3229 static value *
3230 evaluate_subexp_for_cast (expression *exp, int *pos,
3231 enum noside noside,
3232 struct type *to_type)
3233 {
3234 int pc = *pos;
3235
3236 /* Don't let symbols be evaluated with evaluate_subexp because that
3237 throws an "unknown type" error for no-debug data symbols.
3238 Instead, we want the cast to reinterpret the symbol. */
3239 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE
3240 || exp->elts[pc].opcode == OP_VAR_VALUE)
3241 {
3242 (*pos) += 4;
3243
3244 value *val;
3245 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
3246 {
3247 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3248 return value_zero (to_type, not_lval);
3249
3250 val = evaluate_var_msym_value (noside,
3251 exp->elts[pc + 1].objfile,
3252 exp->elts[pc + 2].msymbol);
3253 }
3254 else
3255 val = evaluate_var_value (noside,
3256 exp->elts[pc + 1].block,
3257 exp->elts[pc + 2].symbol);
3258
3259 if (noside == EVAL_SKIP)
3260 return eval_skip_value (exp);
3261
3262 val = value_cast (to_type, val);
3263
3264 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
3265 if (VALUE_LVAL (val) == lval_memory)
3266 {
3267 if (value_lazy (val))
3268 value_fetch_lazy (val);
3269 VALUE_LVAL (val) = not_lval;
3270 }
3271 return val;
3272 }
3273
3274 value *val = evaluate_subexp (to_type, exp, pos, noside);
3275 if (noside == EVAL_SKIP)
3276 return eval_skip_value (exp);
3277 return value_cast (to_type, val);
3278 }
3279
3280 /* Parse a type expression in the string [P..P+LENGTH). */
3281
3282 struct type *
3283 parse_and_eval_type (char *p, int length)
3284 {
3285 char *tmp = (char *) alloca (length + 4);
3286
3287 tmp[0] = '(';
3288 memcpy (tmp + 1, p, length);
3289 tmp[length + 1] = ')';
3290 tmp[length + 2] = '0';
3291 tmp[length + 3] = '\0';
3292 expression_up expr = parse_expression (tmp);
3293 if (expr->elts[0].opcode != UNOP_CAST)
3294 error (_("Internal error in eval_type."));
3295 return expr->elts[1].type;
3296 }
3297
3298 int
3299 calc_f77_array_dims (struct type *array_type)
3300 {
3301 int ndimen = 1;
3302 struct type *tmp_type;
3303
3304 if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
3305 error (_("Can't get dimensions for a non-array type"));
3306
3307 tmp_type = array_type;
3308
3309 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
3310 {
3311 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
3312 ++ndimen;
3313 }
3314 return ndimen;
3315 }
This page took 0.09514 seconds and 5 git commands to generate.