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