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