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