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