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