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