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