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