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