]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/eval.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / gdb / eval.c
CommitLineData
c906108c 1/* Evaluate expressions for GDB.
1bac305b
AC
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
5 Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
c906108c
SS
23
24#include "defs.h"
25#include "gdb_string.h"
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "value.h"
29#include "expression.h"
30#include "target.h"
31#include "frame.h"
c5aa993b
JM
32#include "language.h" /* For CAST_IS_CONVERSION */
33#include "f-lang.h" /* for array bound stuff */
015a42b4 34#include "cp-abi.h"
04714b91 35#include "infcall.h"
c906108c 36
c5aa993b 37/* Defined in symtab.c */
c906108c
SS
38extern int hp_som_som_object_present;
39
c5aa993b 40/* This is defined in valops.c */
c906108c
SS
41extern int overload_resolution;
42
070ad9f0
DB
43/* JYG: lookup rtti type of STRUCTOP_PTR when this is set to continue
44 on with successful lookup for member/method of the rtti type. */
45extern int objectprint;
c906108c
SS
46
47/* Prototypes for local functions. */
48
61051030 49static struct value *evaluate_subexp_for_sizeof (struct expression *, int *);
c906108c 50
61051030
AC
51static struct value *evaluate_subexp_for_address (struct expression *,
52 int *, enum noside);
c906108c 53
61051030
AC
54static struct value *evaluate_subexp (struct type *, struct expression *,
55 int *, enum noside);
c906108c 56
a14ed312 57static char *get_label (struct expression *, int *);
c906108c 58
61051030
AC
59static struct value *evaluate_struct_tuple (struct value *,
60 struct expression *, int *,
61 enum noside, int);
c906108c 62
61051030
AC
63static LONGEST init_array_element (struct value *, struct value *,
64 struct expression *, int *, enum noside,
65 LONGEST, LONGEST);
c906108c 66
61051030 67static struct value *
fba45db2
KB
68evaluate_subexp (struct type *expect_type, register struct expression *exp,
69 register int *pos, enum noside noside)
c906108c
SS
70{
71 return (*exp->language_defn->evaluate_exp) (expect_type, exp, pos, noside);
72}
73\f
74/* Parse the string EXP as a C expression, evaluate it,
75 and return the result as a number. */
76
77CORE_ADDR
fba45db2 78parse_and_eval_address (char *exp)
c906108c
SS
79{
80 struct expression *expr = parse_expression (exp);
81 register CORE_ADDR addr;
c5aa993b 82 register struct cleanup *old_chain =
62995fc4 83 make_cleanup (free_current_contents, &expr);
c906108c 84
1aa20aa8 85 addr = value_as_address (evaluate_expression (expr));
c906108c
SS
86 do_cleanups (old_chain);
87 return addr;
88}
89
90/* Like parse_and_eval_address but takes a pointer to a char * variable
91 and advanced that variable across the characters parsed. */
92
93CORE_ADDR
fba45db2 94parse_and_eval_address_1 (char **expptr)
c906108c 95{
c5aa993b 96 struct expression *expr = parse_exp_1 (expptr, (struct block *) 0, 0);
c906108c
SS
97 register CORE_ADDR addr;
98 register struct cleanup *old_chain =
62995fc4 99 make_cleanup (free_current_contents, &expr);
c906108c 100
1aa20aa8 101 addr = value_as_address (evaluate_expression (expr));
c906108c
SS
102 do_cleanups (old_chain);
103 return addr;
104}
105
bb518678
DT
106/* Like parse_and_eval_address, but treats the value of the expression
107 as an integer, not an address, returns a LONGEST, not a CORE_ADDR */
108LONGEST
109parse_and_eval_long (char *exp)
110{
111 struct expression *expr = parse_expression (exp);
112 register LONGEST retval;
113 register struct cleanup *old_chain =
114 make_cleanup (free_current_contents, &expr);
115
116 retval = value_as_long (evaluate_expression (expr));
117 do_cleanups (old_chain);
118 return (retval);
119}
120
61051030 121struct value *
fba45db2 122parse_and_eval (char *exp)
c906108c
SS
123{
124 struct expression *expr = parse_expression (exp);
61051030 125 struct value *val;
62995fc4
MS
126 register struct cleanup *old_chain =
127 make_cleanup (free_current_contents, &expr);
c906108c
SS
128
129 val = evaluate_expression (expr);
130 do_cleanups (old_chain);
131 return val;
132}
133
134/* Parse up to a comma (or to a closeparen)
135 in the string EXPP as an expression, evaluate it, and return the value.
136 EXPP is advanced to point to the comma. */
137
61051030 138struct value *
fba45db2 139parse_to_comma_and_eval (char **expp)
c906108c
SS
140{
141 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
61051030 142 struct value *val;
62995fc4
MS
143 register struct cleanup *old_chain =
144 make_cleanup (free_current_contents, &expr);
c906108c
SS
145
146 val = evaluate_expression (expr);
147 do_cleanups (old_chain);
148 return val;
149}
150\f
151/* Evaluate an expression in internal prefix form
152 such as is constructed by parse.y.
153
154 See expression.h for info on the format of an expression. */
155
61051030 156struct value *
fba45db2 157evaluate_expression (struct expression *exp)
c906108c
SS
158{
159 int pc = 0;
160 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
161}
162
163/* Evaluate an expression, avoiding all memory references
164 and getting a value whose type alone is correct. */
165
61051030 166struct value *
fba45db2 167evaluate_type (struct expression *exp)
c906108c
SS
168{
169 int pc = 0;
170 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
171}
172
173/* If the next expression is an OP_LABELED, skips past it,
174 returning the label. Otherwise, does nothing and returns NULL. */
175
c5aa993b 176static char *
fba45db2 177get_label (register struct expression *exp, int *pos)
c906108c
SS
178{
179 if (exp->elts[*pos].opcode == OP_LABELED)
180 {
181 int pc = (*pos)++;
182 char *name = &exp->elts[pc + 2].string;
183 int tem = longest_to_int (exp->elts[pc + 1].longconst);
184 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
185 return name;
186 }
187 else
188 return NULL;
189}
190
1b831c93 191/* This function evaluates tuples (in (the deleted) Chill) or
db034ac5 192 brace-initializers (in C/C++) for structure types. */
c906108c 193
61051030
AC
194static struct value *
195evaluate_struct_tuple (struct value *struct_val,
196 register struct expression *exp,
fba45db2 197 register int *pos, enum noside noside, int nargs)
c906108c
SS
198{
199 struct type *struct_type = check_typedef (VALUE_TYPE (struct_val));
200 struct type *substruct_type = struct_type;
201 struct type *field_type;
202 int fieldno = -1;
203 int variantno = -1;
204 int subfieldno = -1;
c5aa993b 205 while (--nargs >= 0)
c906108c
SS
206 {
207 int pc = *pos;
61051030 208 struct value *val = NULL;
c906108c
SS
209 int nlabels = 0;
210 int bitpos, bitsize;
211 char *addr;
c5aa993b 212
c906108c
SS
213 /* Skip past the labels, and count them. */
214 while (get_label (exp, pos) != NULL)
215 nlabels++;
216
217 do
218 {
219 char *label = get_label (exp, &pc);
220 if (label)
221 {
222 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
223 fieldno++)
224 {
225 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
226 if (field_name != NULL && STREQ (field_name, label))
227 {
228 variantno = -1;
229 subfieldno = fieldno;
230 substruct_type = struct_type;
231 goto found;
232 }
233 }
234 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
235 fieldno++)
236 {
237 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
238 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
239 if ((field_name == 0 || *field_name == '\0')
240 && TYPE_CODE (field_type) == TYPE_CODE_UNION)
241 {
242 variantno = 0;
243 for (; variantno < TYPE_NFIELDS (field_type);
244 variantno++)
245 {
246 substruct_type
247 = TYPE_FIELD_TYPE (field_type, variantno);
248 if (TYPE_CODE (substruct_type) == TYPE_CODE_STRUCT)
c5aa993b 249 {
c906108c 250 for (subfieldno = 0;
c5aa993b 251 subfieldno < TYPE_NFIELDS (substruct_type);
c906108c
SS
252 subfieldno++)
253 {
254 if (STREQ (TYPE_FIELD_NAME (substruct_type,
255 subfieldno),
256 label))
257 {
258 goto found;
259 }
260 }
261 }
262 }
263 }
264 }
265 error ("there is no field named %s", label);
266 found:
267 ;
268 }
269 else
270 {
271 /* Unlabelled tuple element - go to next field. */
272 if (variantno >= 0)
273 {
274 subfieldno++;
275 if (subfieldno >= TYPE_NFIELDS (substruct_type))
276 {
277 variantno = -1;
278 substruct_type = struct_type;
279 }
280 }
281 if (variantno < 0)
282 {
283 fieldno++;
284 subfieldno = fieldno;
285 if (fieldno >= TYPE_NFIELDS (struct_type))
286 error ("too many initializers");
287 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
288 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
289 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
290 error ("don't know which variant you want to set");
291 }
292 }
293
294 /* Here, struct_type is the type of the inner struct,
295 while substruct_type is the type of the inner struct.
296 These are the same for normal structures, but a variant struct
297 contains anonymous union fields that contain substruct fields.
298 The value fieldno is the index of the top-level (normal or
299 anonymous union) field in struct_field, while the value
300 subfieldno is the index of the actual real (named inner) field
301 in substruct_type. */
302
303 field_type = TYPE_FIELD_TYPE (substruct_type, subfieldno);
304 if (val == 0)
305 val = evaluate_subexp (field_type, exp, pos, noside);
306
307 /* Now actually set the field in struct_val. */
308
309 /* Assign val to field fieldno. */
310 if (VALUE_TYPE (val) != field_type)
311 val = value_cast (field_type, val);
312
313 bitsize = TYPE_FIELD_BITSIZE (substruct_type, subfieldno);
314 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
315 if (variantno >= 0)
316 bitpos += TYPE_FIELD_BITPOS (substruct_type, subfieldno);
317 addr = VALUE_CONTENTS (struct_val) + bitpos / 8;
318 if (bitsize)
319 modify_field (addr, value_as_long (val),
320 bitpos % 8, bitsize);
321 else
322 memcpy (addr, VALUE_CONTENTS (val),
323 TYPE_LENGTH (VALUE_TYPE (val)));
c5aa993b
JM
324 }
325 while (--nlabels > 0);
c906108c
SS
326 }
327 return struct_val;
328}
329
db034ac5 330/* Recursive helper function for setting elements of array tuples for
1b831c93
AC
331 (the deleted) Chill. The target is ARRAY (which has bounds
332 LOW_BOUND to HIGH_BOUND); the element value is ELEMENT; EXP, POS
333 and NOSIDE are as usual. Evaluates index expresions and sets the
334 specified element(s) of ARRAY to ELEMENT. Returns last index
335 value. */
c906108c
SS
336
337static LONGEST
61051030 338init_array_element (struct value *array, struct value *element,
fba45db2
KB
339 register struct expression *exp, register int *pos,
340 enum noside noside, LONGEST low_bound, LONGEST high_bound)
c906108c
SS
341{
342 LONGEST index;
343 int element_size = TYPE_LENGTH (VALUE_TYPE (element));
344 if (exp->elts[*pos].opcode == BINOP_COMMA)
345 {
346 (*pos)++;
347 init_array_element (array, element, exp, pos, noside,
348 low_bound, high_bound);
349 return init_array_element (array, element,
350 exp, pos, noside, low_bound, high_bound);
351 }
352 else if (exp->elts[*pos].opcode == BINOP_RANGE)
353 {
354 LONGEST low, high;
355 (*pos)++;
356 low = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
357 high = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
358 if (low < low_bound || high > high_bound)
359 error ("tuple range index out of range");
c5aa993b 360 for (index = low; index <= high; index++)
c906108c
SS
361 {
362 memcpy (VALUE_CONTENTS_RAW (array)
363 + (index - low_bound) * element_size,
364 VALUE_CONTENTS (element), element_size);
365 }
366 }
367 else
368 {
369 index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
370 if (index < low_bound || index > high_bound)
371 error ("tuple index out of range");
372 memcpy (VALUE_CONTENTS_RAW (array) + (index - low_bound) * element_size,
373 VALUE_CONTENTS (element), element_size);
374 }
375 return index;
376}
377
61051030 378struct value *
fba45db2
KB
379evaluate_subexp_standard (struct type *expect_type,
380 register struct expression *exp, register int *pos,
381 enum noside noside)
c906108c
SS
382{
383 enum exp_opcode op;
384 int tem, tem2, tem3;
385 register int pc, pc2 = 0, oldpos;
61051030
AC
386 struct value *arg1 = NULL;
387 struct value *arg2 = NULL;
388 struct value *arg3;
c906108c
SS
389 struct type *type;
390 int nargs;
61051030 391 struct value **argvec;
c5aa993b 392 int upper, lower, retcode;
c906108c
SS
393 int code;
394 int ix;
395 long mem_offset;
c5aa993b 396 struct type **arg_types;
c906108c
SS
397 int save_pos1;
398
c906108c
SS
399 pc = (*pos)++;
400 op = exp->elts[pc].opcode;
401
402 switch (op)
403 {
404 case OP_SCOPE:
405 tem = longest_to_int (exp->elts[pc + 2].longconst);
406 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
407 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
408 0,
409 exp->elts[pc + 1].type,
410 &exp->elts[pc + 3].string,
cce74817 411 NULL_TYPE);
c906108c
SS
412 if (arg1 == NULL)
413 error ("There is no field named %s", &exp->elts[pc + 3].string);
414 return arg1;
415
416 case OP_LONG:
417 (*pos) += 3;
418 return value_from_longest (exp->elts[pc + 1].type,
419 exp->elts[pc + 2].longconst);
420
421 case OP_DOUBLE:
422 (*pos) += 3;
423 return value_from_double (exp->elts[pc + 1].type,
424 exp->elts[pc + 2].doubleconst);
425
426 case OP_VAR_VALUE:
427 (*pos) += 3;
428 if (noside == EVAL_SKIP)
429 goto nosideret;
c906108c 430
070ad9f0
DB
431 /* JYG: We used to just return value_zero of the symbol type
432 if we're asked to avoid side effects. Otherwise we return
433 value_of_variable (...). However I'm not sure if
434 value_of_variable () has any side effect.
435 We need a full value object returned here for whatis_exp ()
436 to call evaluate_type () and then pass the full value to
437 value_rtti_target_type () if we are dealing with a pointer
438 or reference to a base class and print object is on. */
c906108c 439
c906108c
SS
440 return value_of_variable (exp->elts[pc + 2].symbol,
441 exp->elts[pc + 1].block);
442
443 case OP_LAST:
444 (*pos) += 2;
445 return
446 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
447
448 case OP_REGISTER:
449 {
c5aa993b 450 int regno = longest_to_int (exp->elts[pc + 1].longconst);
0a1e1ca1 451 struct value *val = value_of_register (regno, get_selected_frame ());
c906108c
SS
452 (*pos) += 2;
453 if (val == NULL)
e36180d7
AC
454 error ("Value of register %s not available.",
455 frame_map_regnum_to_name (regno));
c906108c
SS
456 else
457 return val;
458 }
459 case OP_BOOL:
460 (*pos) += 2;
461 return value_from_longest (LA_BOOL_TYPE,
c5aa993b 462 exp->elts[pc + 1].longconst);
c906108c
SS
463
464 case OP_INTERNALVAR:
465 (*pos) += 2;
466 return value_of_internalvar (exp->elts[pc + 1].internalvar);
467
468 case OP_STRING:
469 tem = longest_to_int (exp->elts[pc + 1].longconst);
470 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
471 if (noside == EVAL_SKIP)
472 goto nosideret;
473 return value_string (&exp->elts[pc + 2].string, tem);
474
475 case OP_BITSTRING:
476 tem = longest_to_int (exp->elts[pc + 1].longconst);
477 (*pos)
478 += 3 + BYTES_TO_EXP_ELEM ((tem + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
479 if (noside == EVAL_SKIP)
480 goto nosideret;
481 return value_bitstring (&exp->elts[pc + 2].string, tem);
482 break;
483
484 case OP_ARRAY:
485 (*pos) += 3;
486 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
487 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
488 nargs = tem3 - tem2 + 1;
489 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
490
491 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
492 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
493 {
61051030 494 struct value *rec = allocate_value (expect_type);
c906108c
SS
495 memset (VALUE_CONTENTS_RAW (rec), '\0', TYPE_LENGTH (type));
496 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
497 }
498
499 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
500 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
501 {
502 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
503 struct type *element_type = TYPE_TARGET_TYPE (type);
61051030 504 struct value *array = allocate_value (expect_type);
c906108c
SS
505 int element_size = TYPE_LENGTH (check_typedef (element_type));
506 LONGEST low_bound, high_bound, index;
507 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
508 {
509 low_bound = 0;
510 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
511 }
512 index = low_bound;
513 memset (VALUE_CONTENTS_RAW (array), 0, TYPE_LENGTH (expect_type));
c5aa993b 514 for (tem = nargs; --nargs >= 0;)
c906108c 515 {
61051030 516 struct value *element;
c906108c
SS
517 int index_pc = 0;
518 if (exp->elts[*pos].opcode == BINOP_RANGE)
519 {
520 index_pc = ++(*pos);
521 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
522 }
523 element = evaluate_subexp (element_type, exp, pos, noside);
524 if (VALUE_TYPE (element) != element_type)
525 element = value_cast (element_type, element);
526 if (index_pc)
527 {
528 int continue_pc = *pos;
529 *pos = index_pc;
530 index = init_array_element (array, element, exp, pos, noside,
531 low_bound, high_bound);
532 *pos = continue_pc;
533 }
534 else
535 {
536 if (index > high_bound)
537 /* to avoid memory corruption */
538 error ("Too many array elements");
539 memcpy (VALUE_CONTENTS_RAW (array)
540 + (index - low_bound) * element_size,
541 VALUE_CONTENTS (element),
542 element_size);
543 }
544 index++;
545 }
546 return array;
547 }
548
549 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
550 && TYPE_CODE (type) == TYPE_CODE_SET)
551 {
61051030 552 struct value *set = allocate_value (expect_type);
c906108c
SS
553 char *valaddr = VALUE_CONTENTS_RAW (set);
554 struct type *element_type = TYPE_INDEX_TYPE (type);
555 struct type *check_type = element_type;
556 LONGEST low_bound, high_bound;
557
558 /* get targettype of elementtype */
559 while (TYPE_CODE (check_type) == TYPE_CODE_RANGE ||
560 TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
561 check_type = TYPE_TARGET_TYPE (check_type);
562
563 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
564 error ("(power)set type with unknown size");
565 memset (valaddr, '\0', TYPE_LENGTH (type));
566 for (tem = 0; tem < nargs; tem++)
567 {
568 LONGEST range_low, range_high;
569 struct type *range_low_type, *range_high_type;
61051030 570 struct value *elem_val;
c906108c
SS
571 if (exp->elts[*pos].opcode == BINOP_RANGE)
572 {
573 (*pos)++;
574 elem_val = evaluate_subexp (element_type, exp, pos, noside);
575 range_low_type = VALUE_TYPE (elem_val);
576 range_low = value_as_long (elem_val);
577 elem_val = evaluate_subexp (element_type, exp, pos, noside);
578 range_high_type = VALUE_TYPE (elem_val);
579 range_high = value_as_long (elem_val);
580 }
581 else
582 {
583 elem_val = evaluate_subexp (element_type, exp, pos, noside);
584 range_low_type = range_high_type = VALUE_TYPE (elem_val);
585 range_low = range_high = value_as_long (elem_val);
586 }
587 /* check types of elements to avoid mixture of elements from
c5aa993b
JM
588 different types. Also check if type of element is "compatible"
589 with element type of powerset */
c906108c
SS
590 if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
591 range_low_type = TYPE_TARGET_TYPE (range_low_type);
592 if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
593 range_high_type = TYPE_TARGET_TYPE (range_high_type);
594 if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type)) ||
595 (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM &&
596 (range_low_type != range_high_type)))
597 /* different element modes */
598 error ("POWERSET tuple elements of different mode");
599 if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type)) ||
600 (TYPE_CODE (check_type) == TYPE_CODE_ENUM &&
601 range_low_type != check_type))
602 error ("incompatible POWERSET tuple elements");
603 if (range_low > range_high)
604 {
605 warning ("empty POWERSET tuple range");
606 continue;
607 }
608 if (range_low < low_bound || range_high > high_bound)
609 error ("POWERSET tuple element out of range");
610 range_low -= low_bound;
611 range_high -= low_bound;
c5aa993b 612 for (; range_low <= range_high; range_low++)
c906108c
SS
613 {
614 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
615 if (BITS_BIG_ENDIAN)
616 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
c5aa993b 617 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
c906108c
SS
618 |= 1 << bit_index;
619 }
620 }
621 return set;
622 }
623
f976f6d4 624 argvec = (struct value **) alloca (sizeof (struct value *) * nargs);
c906108c
SS
625 for (tem = 0; tem < nargs; tem++)
626 {
627 /* Ensure that array expressions are coerced into pointer objects. */
628 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
629 }
630 if (noside == EVAL_SKIP)
631 goto nosideret;
632 return value_array (tem2, tem3, argvec);
633
634 case TERNOP_SLICE:
635 {
61051030 636 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
c906108c 637 int lowbound
c5aa993b 638 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
c906108c 639 int upper
c5aa993b 640 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
c906108c
SS
641 if (noside == EVAL_SKIP)
642 goto nosideret;
643 return value_slice (array, lowbound, upper - lowbound + 1);
644 }
645
646 case TERNOP_SLICE_COUNT:
647 {
61051030 648 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
c906108c 649 int lowbound
c5aa993b 650 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
c906108c 651 int length
c5aa993b 652 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
c906108c
SS
653 return value_slice (array, lowbound, length);
654 }
655
656 case TERNOP_COND:
657 /* Skip third and second args to evaluate the first one. */
658 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
659 if (value_logical_not (arg1))
660 {
661 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
662 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
663 }
664 else
665 {
666 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
667 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
668 return arg2;
669 }
670
671 case OP_FUNCALL:
672 (*pos) += 2;
673 op = exp->elts[*pos].opcode;
674 nargs = longest_to_int (exp->elts[pc + 1].longconst);
675 /* Allocate arg vector, including space for the function to be
c5aa993b 676 called in argvec[0] and a terminating NULL */
f976f6d4 677 argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 3));
c906108c
SS
678 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
679 {
680 LONGEST fnptr;
681
c5aa993b
JM
682 /* 1997-08-01 Currently we do not support function invocation
683 via pointers-to-methods with HP aCC. Pointer does not point
684 to the function, but possibly to some thunk. */
685 if (hp_som_som_object_present)
686 {
687 error ("Not implemented: function invocation through pointer to method with HP aCC");
688 }
c906108c
SS
689
690 nargs++;
691 /* First, evaluate the structure into arg2 */
692 pc2 = (*pos)++;
693
694 if (noside == EVAL_SKIP)
695 goto nosideret;
696
697 if (op == STRUCTOP_MEMBER)
698 {
699 arg2 = evaluate_subexp_for_address (exp, pos, noside);
700 }
701 else
702 {
703 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
704 }
705
706 /* If the function is a virtual function, then the
707 aggregate value (providing the structure) plays
708 its part by providing the vtable. Otherwise,
709 it is just along for the ride: call the function
710 directly. */
711
712 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
713
714 fnptr = value_as_long (arg1);
715
c5aa993b 716 if (METHOD_PTR_IS_VIRTUAL (fnptr))
c906108c 717 {
c5aa993b 718 int fnoffset = METHOD_PTR_TO_VOFFSET (fnptr);
c906108c
SS
719 struct type *basetype;
720 struct type *domain_type =
c5aa993b 721 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
c906108c
SS
722 int i, j;
723 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
724 if (domain_type != basetype)
c5aa993b 725 arg2 = value_cast (lookup_pointer_type (domain_type), arg2);
c906108c
SS
726 basetype = TYPE_VPTR_BASETYPE (domain_type);
727 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
728 {
729 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
730 /* If one is virtual, then all are virtual. */
731 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
732 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
733 if ((int) TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
734 {
61051030 735 struct value *temp = value_ind (arg2);
c906108c
SS
736 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
737 arg2 = value_addr (temp);
738 goto got_it;
739 }
740 }
741 if (i < 0)
742 error ("virtual function at index %d not found", fnoffset);
743 }
744 else
745 {
746 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
747 }
748 got_it:
749
750 /* Now, say which argument to start evaluating from */
751 tem = 2;
752 }
753 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
754 {
755 /* Hair for method invocations */
756 int tem2;
757
758 nargs++;
759 /* First, evaluate the structure into arg2 */
760 pc2 = (*pos)++;
761 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
762 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
763 if (noside == EVAL_SKIP)
764 goto nosideret;
765
766 if (op == STRUCTOP_STRUCT)
767 {
768 /* If v is a variable in a register, and the user types
c5aa993b
JM
769 v.method (), this will produce an error, because v has
770 no address.
771
772 A possible way around this would be to allocate a
773 copy of the variable on the stack, copy in the
774 contents, call the function, and copy out the
775 contents. I.e. convert this from call by reference
776 to call by copy-return (or whatever it's called).
777 However, this does not work because it is not the
778 same: the method being called could stash a copy of
779 the address, and then future uses through that address
780 (after the method returns) would be expected to
781 use the variable itself, not some copy of it. */
c906108c
SS
782 arg2 = evaluate_subexp_for_address (exp, pos, noside);
783 }
784 else
785 {
786 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
787 }
788 /* Now, say which argument to start evaluating from */
789 tem = 2;
790 }
791 else
792 {
793 /* Non-method function call */
794 save_pos1 = *pos;
795 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
796 tem = 1;
797 type = VALUE_TYPE (argvec[0]);
798 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
799 type = TYPE_TARGET_TYPE (type);
800 if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
801 {
802 for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
803 {
c5aa993b
JM
804 /* pai: FIXME This seems to be coercing arguments before
805 * overload resolution has been done! */
806 argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type, tem - 1),
c906108c
SS
807 exp, pos, noside);
808 }
809 }
810 }
811
812 /* Evaluate arguments */
813 for (; tem <= nargs; tem++)
814 {
815 /* Ensure that array expressions are coerced into pointer objects. */
816 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
817 }
818
819 /* signal end of arglist */
820 argvec[tem] = 0;
821
822 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
823 {
824 int static_memfuncp;
c906108c 825 char tstr[256];
c5aa993b
JM
826
827 /* Method invocation : stuff "this" as first parameter */
9b013045 828 argvec[1] = arg2;
c5aa993b
JM
829 /* Name of method from expression */
830 strcpy (tstr, &exp->elts[pc2 + 2].string);
831
832 if (overload_resolution && (exp->language_defn->la_language == language_cplus))
833 {
834 /* Language is C++, do some overload resolution before evaluation */
61051030 835 struct value *valp = NULL;
c5aa993b
JM
836
837 /* Prepare list of argument types for overload resolution */
c2636352 838 arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
c5aa993b
JM
839 for (ix = 1; ix <= nargs; ix++)
840 arg_types[ix - 1] = VALUE_TYPE (argvec[ix]);
841
842 (void) find_overload_match (arg_types, nargs, tstr,
843 1 /* method */ , 0 /* strict match */ ,
7f8c9282 844 &arg2 /* the object */ , NULL,
c5aa993b
JM
845 &valp, NULL, &static_memfuncp);
846
847
848 argvec[1] = arg2; /* the ``this'' pointer */
849 argvec[0] = valp; /* use the method found after overload resolution */
850 }
851 else
852 /* Non-C++ case -- or no overload resolution */
853 {
9b013045 854 struct value *temp = arg2;
c5aa993b
JM
855 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
856 &static_memfuncp,
857 op == STRUCTOP_STRUCT
858 ? "structure" : "structure pointer");
9b013045
PS
859 /* value_struct_elt updates temp with the correct value
860 of the ``this'' pointer if necessary, so modify argvec[1] to
861 reflect any ``this'' changes. */
862 arg2 = value_from_longest (lookup_pointer_type(VALUE_TYPE (temp)),
863 VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
864 + VALUE_EMBEDDED_OFFSET (temp));
c5aa993b
JM
865 argvec[1] = arg2; /* the ``this'' pointer */
866 }
c906108c
SS
867
868 if (static_memfuncp)
869 {
870 argvec[1] = argvec[0];
871 nargs--;
872 argvec++;
873 }
874 }
875 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
876 {
877 argvec[1] = arg2;
878 argvec[0] = arg1;
879 }
917317f4 880 else if (op == OP_VAR_VALUE)
c5aa993b 881 {
c906108c 882 /* Non-member function being called */
917317f4
JM
883 /* fn: This can only be done for C++ functions. A C-style function
884 in a C++ program, for instance, does not have the fields that
885 are expected here */
c906108c 886
c5aa993b
JM
887 if (overload_resolution && (exp->language_defn->la_language == language_cplus))
888 {
889 /* Language is C++, do some overload resolution before evaluation */
890 struct symbol *symp;
891
892 /* Prepare list of argument types for overload resolution */
c2636352 893 arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
c5aa993b
JM
894 for (ix = 1; ix <= nargs; ix++)
895 arg_types[ix - 1] = VALUE_TYPE (argvec[ix]);
896
897 (void) find_overload_match (arg_types, nargs, NULL /* no need for name */ ,
898 0 /* not method */ , 0 /* strict match */ ,
917317f4 899 NULL, exp->elts[save_pos1+2].symbol /* the function */ ,
c5aa993b
JM
900 NULL, &symp, NULL);
901
902 /* Now fix the expression being evaluated */
917317f4 903 exp->elts[save_pos1+2].symbol = symp;
c5aa993b
JM
904 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
905 }
906 else
907 {
908 /* Not C++, or no overload resolution allowed */
909 /* nothing to be done; argvec already correctly set up */
910 }
911 }
917317f4
JM
912 else
913 {
914 /* It is probably a C-style function */
915 /* nothing to be done; argvec already correctly set up */
916 }
c906108c
SS
917
918 do_call_it:
919
920 if (noside == EVAL_SKIP)
921 goto nosideret;
0478d61c
FF
922 if (argvec[0] == NULL)
923 error ("Cannot evaluate function -- may be inlined");
c906108c
SS
924 if (noside == EVAL_AVOID_SIDE_EFFECTS)
925 {
926 /* If the return type doesn't look like a function type, call an
927 error. This can happen if somebody tries to turn a variable into
928 a function call. This is here because people often want to
929 call, eg, strcmp, which gdb doesn't know is a function. If
930 gdb isn't asked for it's opinion (ie. through "whatis"),
931 it won't offer it. */
932
933 struct type *ftype =
c5aa993b 934 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
c906108c
SS
935
936 if (ftype)
937 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
938 else
939 error ("Expression of type other than \"Function returning ...\" used as function");
940 }
c906108c
SS
941 return call_function_by_hand (argvec[0], nargs, argvec + 1);
942 /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */
943
c5aa993b 944 case OP_F77_UNDETERMINED_ARGLIST:
c906108c
SS
945
946 /* Remember that in F77, functions, substring ops and
947 array subscript operations cannot be disambiguated
948 at parse time. We have made all array subscript operations,
949 substring operations as well as function calls come here
950 and we now have to discover what the heck this thing actually was.
c5aa993b 951 If it is a function, we process just as if we got an OP_FUNCALL. */
c906108c 952
c5aa993b 953 nargs = longest_to_int (exp->elts[pc + 1].longconst);
c906108c
SS
954 (*pos) += 2;
955
c5aa993b 956 /* First determine the type code we are dealing with. */
c906108c
SS
957 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
958 type = check_typedef (VALUE_TYPE (arg1));
959 code = TYPE_CODE (type);
960
c5aa993b 961 switch (code)
c906108c
SS
962 {
963 case TYPE_CODE_ARRAY:
964 goto multi_f77_subscript;
965
966 case TYPE_CODE_STRING:
967 goto op_f77_substr;
968
969 case TYPE_CODE_PTR:
970 case TYPE_CODE_FUNC:
971 /* It's a function call. */
972 /* Allocate arg vector, including space for the function to be
973 called in argvec[0] and a terminating NULL */
f976f6d4 974 argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
c906108c
SS
975 argvec[0] = arg1;
976 tem = 1;
977 for (; tem <= nargs; tem++)
978 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
c5aa993b 979 argvec[tem] = 0; /* signal end of arglist */
c906108c
SS
980 goto do_call_it;
981
982 default:
c5aa993b 983 error ("Cannot perform substring on this type");
c906108c
SS
984 }
985
986 op_f77_substr:
987 /* We have a substring operation on our hands here,
988 let us get the string we will be dealing with */
989
990 /* Now evaluate the 'from' and 'to' */
991
992 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
993
994 if (nargs < 2)
995 return value_subscript (arg1, arg2);
996
997 arg3 = evaluate_subexp_with_coercion (exp, pos, noside);
998
999 if (noside == EVAL_SKIP)
c5aa993b
JM
1000 goto nosideret;
1001
c906108c
SS
1002 tem2 = value_as_long (arg2);
1003 tem3 = value_as_long (arg3);
c5aa993b 1004
c906108c
SS
1005 return value_slice (arg1, tem2, tem3 - tem2 + 1);
1006
1007 case OP_COMPLEX:
1008 /* We have a complex number, There should be 2 floating
c5aa993b 1009 point numbers that compose it */
c906108c 1010 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
c5aa993b 1011 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
c906108c
SS
1012
1013 return value_literal_complex (arg1, arg2, builtin_type_f_complex_s16);
1014
1015 case STRUCTOP_STRUCT:
1016 tem = longest_to_int (exp->elts[pc + 1].longconst);
1017 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1018 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1019 if (noside == EVAL_SKIP)
1020 goto nosideret;
1021 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1022 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
1023 &exp->elts[pc + 2].string,
1024 0),
1025 lval_memory);
1026 else
1027 {
61051030 1028 struct value *temp = arg1;
c906108c
SS
1029 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
1030 NULL, "structure");
1031 }
1032
1033 case STRUCTOP_PTR:
1034 tem = longest_to_int (exp->elts[pc + 1].longconst);
1035 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1036 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1037 if (noside == EVAL_SKIP)
1038 goto nosideret;
070ad9f0
DB
1039
1040 /* JYG: if print object is on we need to replace the base type
1041 with rtti type in order to continue on with successful
1042 lookup of member / method only available in the rtti type. */
1043 {
1044 struct type *type = VALUE_TYPE (arg1);
1045 struct type *real_type;
1046 int full, top, using_enc;
1047
1048 if (objectprint && TYPE_TARGET_TYPE(type) &&
1049 (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
1050 {
1051 real_type = value_rtti_target_type (arg1, &full, &top, &using_enc);
1052 if (real_type)
1053 {
1054 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1055 real_type = lookup_pointer_type (real_type);
1056 else
1057 real_type = lookup_reference_type (real_type);
1058
1059 arg1 = value_cast (real_type, arg1);
1060 }
1061 }
1062 }
1063
c906108c
SS
1064 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1065 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
1066 &exp->elts[pc + 2].string,
1067 0),
1068 lval_memory);
1069 else
1070 {
61051030 1071 struct value *temp = arg1;
c906108c
SS
1072 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
1073 NULL, "structure pointer");
1074 }
1075
1076 case STRUCTOP_MEMBER:
1077 arg1 = evaluate_subexp_for_address (exp, pos, noside);
1078 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1079
c5aa993b 1080 /* With HP aCC, pointers to methods do not point to the function code */
c906108c 1081 if (hp_som_som_object_present &&
c5aa993b
JM
1082 (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) &&
1083 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_METHOD))
1084 error ("Pointers to methods not supported with HP aCC"); /* 1997-08-19 */
1085
c906108c
SS
1086 mem_offset = value_as_long (arg2);
1087 goto handle_pointer_to_member;
1088
1089 case STRUCTOP_MPTR:
1090 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1091 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1092
c5aa993b 1093 /* With HP aCC, pointers to methods do not point to the function code */
c906108c 1094 if (hp_som_som_object_present &&
c5aa993b
JM
1095 (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) &&
1096 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_METHOD))
1097 error ("Pointers to methods not supported with HP aCC"); /* 1997-08-19 */
c906108c
SS
1098
1099 mem_offset = value_as_long (arg2);
1100
c5aa993b 1101 handle_pointer_to_member:
c906108c
SS
1102 /* HP aCC generates offsets that have bit #29 set; turn it off to get
1103 a real offset to the member. */
1104 if (hp_som_som_object_present)
c5aa993b
JM
1105 {
1106 if (!mem_offset) /* no bias -> really null */
1107 error ("Attempted dereference of null pointer-to-member");
1108 mem_offset &= ~0x20000000;
1109 }
c906108c
SS
1110 if (noside == EVAL_SKIP)
1111 goto nosideret;
1112 type = check_typedef (VALUE_TYPE (arg2));
1113 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1114 goto bad_pointer_to_member;
1115 type = check_typedef (TYPE_TARGET_TYPE (type));
1116 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
1117 error ("not implemented: pointer-to-method in pointer-to-member construct");
1118 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
1119 goto bad_pointer_to_member;
1120 /* Now, convert these values to an address. */
1121 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
1122 arg1);
4478b372 1123 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
c906108c
SS
1124 value_as_long (arg1) + mem_offset);
1125 return value_ind (arg3);
c5aa993b
JM
1126 bad_pointer_to_member:
1127 error ("non-pointer-to-member value used in pointer-to-member construct");
c906108c
SS
1128
1129 case BINOP_CONCAT:
1130 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1131 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1132 if (noside == EVAL_SKIP)
1133 goto nosideret;
1134 if (binop_user_defined_p (op, arg1, arg2))
1135 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1136 else
1137 return value_concat (arg1, arg2);
1138
1139 case BINOP_ASSIGN:
1140 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1141 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1142
c5aa993b 1143 /* Do special stuff for HP aCC pointers to members */
c906108c 1144 if (hp_som_som_object_present)
c5aa993b
JM
1145 {
1146 /* 1997-08-19 Can't assign HP aCC pointers to methods. No details of
1147 the implementation yet; but the pointer appears to point to a code
1148 sequence (thunk) in memory -- in any case it is *not* the address
1149 of the function as it would be in a naive implementation. */
1150 if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) &&
1151 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_METHOD))
1152 error ("Assignment to pointers to methods not implemented with HP aCC");
1153
1154 /* HP aCC pointers to data members require a constant bias */
1155 if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) &&
1156 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_MEMBER))
1157 {
1158 unsigned int *ptr = (unsigned int *) VALUE_CONTENTS (arg2); /* forces evaluation */
1159 *ptr |= 0x20000000; /* set 29th bit */
1160 }
1161 }
1162
c906108c
SS
1163 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1164 return arg1;
1165 if (binop_user_defined_p (op, arg1, arg2))
1166 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1167 else
1168 return value_assign (arg1, arg2);
1169
1170 case BINOP_ASSIGN_MODIFY:
1171 (*pos) += 2;
1172 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1173 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1174 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1175 return arg1;
1176 op = exp->elts[pc + 1].opcode;
1177 if (binop_user_defined_p (op, arg1, arg2))
1178 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1179 else if (op == BINOP_ADD)
1180 arg2 = value_add (arg1, arg2);
1181 else if (op == BINOP_SUB)
1182 arg2 = value_sub (arg1, arg2);
1183 else
1184 arg2 = value_binop (arg1, arg2, op);
1185 return value_assign (arg1, arg2);
1186
1187 case BINOP_ADD:
1188 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1189 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1190 if (noside == EVAL_SKIP)
1191 goto nosideret;
1192 if (binop_user_defined_p (op, arg1, arg2))
1193 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1194 else
1195 return value_add (arg1, arg2);
1196
1197 case BINOP_SUB:
1198 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1199 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1200 if (noside == EVAL_SKIP)
1201 goto nosideret;
1202 if (binop_user_defined_p (op, arg1, arg2))
1203 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1204 else
1205 return value_sub (arg1, arg2);
1206
1207 case BINOP_MUL:
1208 case BINOP_DIV:
1209 case BINOP_REM:
1210 case BINOP_MOD:
1211 case BINOP_LSH:
1212 case BINOP_RSH:
1213 case BINOP_BITWISE_AND:
1214 case BINOP_BITWISE_IOR:
1215 case BINOP_BITWISE_XOR:
1216 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1217 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1218 if (noside == EVAL_SKIP)
1219 goto nosideret;
1220 if (binop_user_defined_p (op, arg1, arg2))
1221 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
c5aa993b
JM
1222 else if (noside == EVAL_AVOID_SIDE_EFFECTS
1223 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
1224 return value_zero (VALUE_TYPE (arg1), not_lval);
c906108c
SS
1225 else
1226 return value_binop (arg1, arg2, op);
1227
1228 case BINOP_RANGE:
1229 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1230 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1231 if (noside == EVAL_SKIP)
1232 goto nosideret;
1233 error ("':' operator used in invalid context");
1234
1235 case BINOP_SUBSCRIPT:
1236 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1237 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1238 if (noside == EVAL_SKIP)
1239 goto nosideret;
1240 if (binop_user_defined_p (op, arg1, arg2))
1241 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1242 else
c5aa993b 1243 {
c906108c
SS
1244 /* If the user attempts to subscript something that is not an
1245 array or pointer type (like a plain int variable for example),
1246 then report this as an error. */
1247
1248 COERCE_REF (arg1);
1249 type = check_typedef (VALUE_TYPE (arg1));
1250 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
1251 && TYPE_CODE (type) != TYPE_CODE_PTR)
1252 {
1253 if (TYPE_NAME (type))
1254 error ("cannot subscript something of type `%s'",
1255 TYPE_NAME (type));
1256 else
1257 error ("cannot subscript requested type");
1258 }
1259
1260 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1261 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
1262 else
1263 return value_subscript (arg1, arg2);
c5aa993b 1264 }
c906108c
SS
1265
1266 case BINOP_IN:
1267 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1268 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1269 if (noside == EVAL_SKIP)
1270 goto nosideret;
1271 return value_in (arg1, arg2);
c5aa993b 1272
c906108c
SS
1273 case MULTI_SUBSCRIPT:
1274 (*pos) += 2;
1275 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1276 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1277 while (nargs-- > 0)
1278 {
1279 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1280 /* FIXME: EVAL_SKIP handling may not be correct. */
1281 if (noside == EVAL_SKIP)
1282 {
1283 if (nargs > 0)
1284 {
1285 continue;
1286 }
1287 else
1288 {
1289 goto nosideret;
1290 }
1291 }
1292 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
1293 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1294 {
1295 /* If the user attempts to subscript something that has no target
c5aa993b
JM
1296 type (like a plain int variable for example), then report this
1297 as an error. */
1298
c906108c
SS
1299 type = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (arg1)));
1300 if (type != NULL)
1301 {
1302 arg1 = value_zero (type, VALUE_LVAL (arg1));
1303 noside = EVAL_SKIP;
1304 continue;
1305 }
1306 else
1307 {
1308 error ("cannot subscript something of type `%s'",
1309 TYPE_NAME (VALUE_TYPE (arg1)));
1310 }
1311 }
c5aa993b 1312
c906108c
SS
1313 if (binop_user_defined_p (op, arg1, arg2))
1314 {
1315 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
1316 }
1317 else
1318 {
1319 arg1 = value_subscript (arg1, arg2);
1320 }
1321 }
1322 return (arg1);
1323
1324 multi_f77_subscript:
c5aa993b
JM
1325 {
1326 int subscript_array[MAX_FORTRAN_DIMS + 1]; /* 1-based array of
1327 subscripts, max == 7 */
1328 int array_size_array[MAX_FORTRAN_DIMS + 1];
1329 int ndimensions = 1, i;
1330 struct type *tmp_type;
1331 int offset_item; /* The array offset where the item lives */
c906108c
SS
1332
1333 if (nargs > MAX_FORTRAN_DIMS)
1334 error ("Too many subscripts for F77 (%d Max)", MAX_FORTRAN_DIMS);
1335
1336 tmp_type = check_typedef (VALUE_TYPE (arg1));
1337 ndimensions = calc_f77_array_dims (type);
1338
1339 if (nargs != ndimensions)
1340 error ("Wrong number of subscripts");
1341
1342 /* Now that we know we have a legal array subscript expression
c5aa993b 1343 let us actually find out where this element exists in the array. */
c906108c 1344
c5aa993b 1345 offset_item = 0;
c906108c
SS
1346 for (i = 1; i <= nargs; i++)
1347 {
c5aa993b 1348 /* Evaluate each subscript, It must be a legal integer in F77 */
c906108c
SS
1349 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1350
c5aa993b 1351 /* Fill in the subscript and array size arrays */
c906108c
SS
1352
1353 subscript_array[i] = value_as_long (arg2);
c5aa993b 1354
c906108c
SS
1355 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
1356 if (retcode == BOUND_FETCH_ERROR)
c5aa993b 1357 error ("Cannot obtain dynamic upper bound");
c906108c 1358
c5aa993b 1359 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
c906108c 1360 if (retcode == BOUND_FETCH_ERROR)
c5aa993b 1361 error ("Cannot obtain dynamic lower bound");
c906108c
SS
1362
1363 array_size_array[i] = upper - lower + 1;
c5aa993b
JM
1364
1365 /* Zero-normalize subscripts so that offsetting will work. */
1366
c906108c
SS
1367 subscript_array[i] -= lower;
1368
1369 /* If we are at the bottom of a multidimensional
1370 array type then keep a ptr to the last ARRAY
1371 type around for use when calling value_subscript()
1372 below. This is done because we pretend to value_subscript
1373 that we actually have a one-dimensional array
1374 of base element type that we apply a simple
c5aa993b 1375 offset to. */
c906108c 1376
c5aa993b
JM
1377 if (i < nargs)
1378 tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
c906108c
SS
1379 }
1380
1381 /* Now let us calculate the offset for this item */
1382
c5aa993b
JM
1383 offset_item = subscript_array[ndimensions];
1384
c906108c 1385 for (i = ndimensions - 1; i >= 1; i--)
c5aa993b 1386 offset_item =
c906108c
SS
1387 array_size_array[i] * offset_item + subscript_array[i];
1388
1389 /* Construct a value node with the value of the offset */
1390
c5aa993b 1391 arg2 = value_from_longest (builtin_type_f_integer, offset_item);
c906108c
SS
1392
1393 /* Let us now play a dirty trick: we will take arg1
1394 which is a value node pointing to the topmost level
1395 of the multidimensional array-set and pretend
1396 that it is actually a array of the final element
1397 type, this will ensure that value_subscript()
1398 returns the correct type value */
1399
c5aa993b 1400 VALUE_TYPE (arg1) = tmp_type;
c906108c
SS
1401 return value_ind (value_add (value_coerce_array (arg1), arg2));
1402 }
1403
1404 case BINOP_LOGICAL_AND:
1405 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1406 if (noside == EVAL_SKIP)
1407 {
1408 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1409 goto nosideret;
1410 }
c5aa993b 1411
c906108c
SS
1412 oldpos = *pos;
1413 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1414 *pos = oldpos;
c5aa993b
JM
1415
1416 if (binop_user_defined_p (op, arg1, arg2))
c906108c
SS
1417 {
1418 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1419 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1420 }
1421 else
1422 {
1423 tem = value_logical_not (arg1);
1424 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1425 (tem ? EVAL_SKIP : noside));
1426 return value_from_longest (LA_BOOL_TYPE,
c5aa993b 1427 (LONGEST) (!tem && !value_logical_not (arg2)));
c906108c
SS
1428 }
1429
1430 case BINOP_LOGICAL_OR:
1431 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1432 if (noside == EVAL_SKIP)
1433 {
1434 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1435 goto nosideret;
1436 }
c5aa993b 1437
c906108c
SS
1438 oldpos = *pos;
1439 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1440 *pos = oldpos;
c5aa993b
JM
1441
1442 if (binop_user_defined_p (op, arg1, arg2))
c906108c
SS
1443 {
1444 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1445 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1446 }
1447 else
1448 {
1449 tem = value_logical_not (arg1);
1450 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1451 (!tem ? EVAL_SKIP : noside));
1452 return value_from_longest (LA_BOOL_TYPE,
c5aa993b 1453 (LONGEST) (!tem || !value_logical_not (arg2)));
c906108c
SS
1454 }
1455
1456 case BINOP_EQUAL:
1457 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1458 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1459 if (noside == EVAL_SKIP)
1460 goto nosideret;
1461 if (binop_user_defined_p (op, arg1, arg2))
1462 {
1463 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1464 }
1465 else
1466 {
1467 tem = value_equal (arg1, arg2);
1468 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1469 }
1470
1471 case BINOP_NOTEQUAL:
1472 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1473 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1474 if (noside == EVAL_SKIP)
1475 goto nosideret;
1476 if (binop_user_defined_p (op, arg1, arg2))
1477 {
1478 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1479 }
1480 else
1481 {
1482 tem = value_equal (arg1, arg2);
1483 return value_from_longest (LA_BOOL_TYPE, (LONGEST) ! tem);
1484 }
1485
1486 case BINOP_LESS:
1487 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1488 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1489 if (noside == EVAL_SKIP)
1490 goto nosideret;
1491 if (binop_user_defined_p (op, arg1, arg2))
1492 {
1493 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1494 }
1495 else
1496 {
1497 tem = value_less (arg1, arg2);
1498 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1499 }
1500
1501 case BINOP_GTR:
1502 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1503 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1504 if (noside == EVAL_SKIP)
1505 goto nosideret;
1506 if (binop_user_defined_p (op, arg1, arg2))
1507 {
1508 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1509 }
1510 else
1511 {
1512 tem = value_less (arg2, arg1);
1513 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1514 }
1515
1516 case BINOP_GEQ:
1517 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1518 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1519 if (noside == EVAL_SKIP)
1520 goto nosideret;
1521 if (binop_user_defined_p (op, arg1, arg2))
1522 {
1523 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1524 }
1525 else
1526 {
1527 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1528 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1529 }
1530
1531 case BINOP_LEQ:
1532 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1533 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1534 if (noside == EVAL_SKIP)
1535 goto nosideret;
1536 if (binop_user_defined_p (op, arg1, arg2))
1537 {
1538 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1539 }
c5aa993b 1540 else
c906108c
SS
1541 {
1542 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1543 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
1544 }
1545
1546 case BINOP_REPEAT:
1547 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1548 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1549 if (noside == EVAL_SKIP)
1550 goto nosideret;
1551 type = check_typedef (VALUE_TYPE (arg2));
1552 if (TYPE_CODE (type) != TYPE_CODE_INT)
1553 error ("Non-integral right operand for \"@\" operator.");
1554 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1555 {
1556 return allocate_repeat_value (VALUE_TYPE (arg1),
c5aa993b 1557 longest_to_int (value_as_long (arg2)));
c906108c
SS
1558 }
1559 else
1560 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1561
1562 case BINOP_COMMA:
1563 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1564 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1565
1566 case UNOP_NEG:
1567 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1568 if (noside == EVAL_SKIP)
1569 goto nosideret;
1570 if (unop_user_defined_p (op, arg1))
1571 return value_x_unop (arg1, op, noside);
1572 else
1573 return value_neg (arg1);
1574
1575 case UNOP_COMPLEMENT:
1576 /* C++: check for and handle destructor names. */
1577 op = exp->elts[*pos].opcode;
1578
1579 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1580 if (noside == EVAL_SKIP)
1581 goto nosideret;
1582 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1583 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1584 else
1585 return value_complement (arg1);
1586
1587 case UNOP_LOGICAL_NOT:
1588 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1589 if (noside == EVAL_SKIP)
1590 goto nosideret;
1591 if (unop_user_defined_p (op, arg1))
1592 return value_x_unop (arg1, op, noside);
1593 else
1594 return value_from_longest (LA_BOOL_TYPE,
1595 (LONGEST) value_logical_not (arg1));
1596
1597 case UNOP_IND:
1598 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
c5aa993b 1599 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
c906108c
SS
1600 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1601 if ((TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) &&
1602 ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_METHOD) ||
1603 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_MEMBER)))
c5aa993b 1604 error ("Attempt to dereference pointer to member without an object");
c906108c
SS
1605 if (noside == EVAL_SKIP)
1606 goto nosideret;
1607 if (unop_user_defined_p (op, arg1))
1608 return value_x_unop (arg1, op, noside);
1609 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1610 {
1611 type = check_typedef (VALUE_TYPE (arg1));
1612 if (TYPE_CODE (type) == TYPE_CODE_PTR
1613 || TYPE_CODE (type) == TYPE_CODE_REF
c5aa993b 1614 /* In C you can dereference an array to get the 1st elt. */
c906108c 1615 || TYPE_CODE (type) == TYPE_CODE_ARRAY
c5aa993b 1616 )
c906108c
SS
1617 return value_zero (TYPE_TARGET_TYPE (type),
1618 lval_memory);
1619 else if (TYPE_CODE (type) == TYPE_CODE_INT)
1620 /* GDB allows dereferencing an int. */
1621 return value_zero (builtin_type_int, lval_memory);
1622 else
1623 error ("Attempt to take contents of a non-pointer value.");
1624 }
1625 return value_ind (arg1);
1626
1627 case UNOP_ADDR:
1628 /* C++: check for and handle pointer to members. */
c5aa993b 1629
c906108c
SS
1630 op = exp->elts[*pos].opcode;
1631
1632 if (noside == EVAL_SKIP)
1633 {
1634 if (op == OP_SCOPE)
1635 {
c5aa993b 1636 int temm = longest_to_int (exp->elts[pc + 3].longconst);
c906108c
SS
1637 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
1638 }
1639 else
cce74817 1640 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
c906108c
SS
1641 goto nosideret;
1642 }
c5aa993b
JM
1643 else
1644 {
61051030 1645 struct value *retvalp = evaluate_subexp_for_address (exp, pos, noside);
c5aa993b
JM
1646 /* If HP aCC object, use bias for pointers to members */
1647 if (hp_som_som_object_present &&
1648 (TYPE_CODE (VALUE_TYPE (retvalp)) == TYPE_CODE_PTR) &&
1649 (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (retvalp))) == TYPE_CODE_MEMBER))
1650 {
1651 unsigned int *ptr = (unsigned int *) VALUE_CONTENTS (retvalp); /* forces evaluation */
1652 *ptr |= 0x20000000; /* set 29th bit */
1653 }
1654 return retvalp;
1655 }
1656
c906108c
SS
1657 case UNOP_SIZEOF:
1658 if (noside == EVAL_SKIP)
1659 {
1660 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1661 goto nosideret;
1662 }
1663 return evaluate_subexp_for_sizeof (exp, pos);
1664
1665 case UNOP_CAST:
1666 (*pos) += 2;
1667 type = exp->elts[pc + 1].type;
1668 arg1 = evaluate_subexp (type, exp, pos, noside);
1669 if (noside == EVAL_SKIP)
1670 goto nosideret;
1671 if (type != VALUE_TYPE (arg1))
1672 arg1 = value_cast (type, arg1);
1673 return arg1;
1674
1675 case UNOP_MEMVAL:
1676 (*pos) += 2;
1677 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1678 if (noside == EVAL_SKIP)
1679 goto nosideret;
1680 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1681 return value_zero (exp->elts[pc + 1].type, lval_memory);
1682 else
1683 return value_at_lazy (exp->elts[pc + 1].type,
1aa20aa8 1684 value_as_address (arg1),
c906108c
SS
1685 NULL);
1686
1687 case UNOP_PREINCREMENT:
1688 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1689 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1690 return arg1;
1691 else if (unop_user_defined_p (op, arg1))
1692 {
1693 return value_x_unop (arg1, op, noside);
1694 }
1695 else
1696 {
c5aa993b
JM
1697 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1698 (LONGEST) 1));
c906108c
SS
1699 return value_assign (arg1, arg2);
1700 }
1701
1702 case UNOP_PREDECREMENT:
1703 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1704 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1705 return arg1;
1706 else if (unop_user_defined_p (op, arg1))
1707 {
1708 return value_x_unop (arg1, op, noside);
1709 }
1710 else
1711 {
c5aa993b
JM
1712 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1713 (LONGEST) 1));
c906108c
SS
1714 return value_assign (arg1, arg2);
1715 }
1716
1717 case UNOP_POSTINCREMENT:
1718 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1719 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1720 return arg1;
1721 else if (unop_user_defined_p (op, arg1))
1722 {
1723 return value_x_unop (arg1, op, noside);
1724 }
1725 else
1726 {
c5aa993b
JM
1727 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1728 (LONGEST) 1));
c906108c
SS
1729 value_assign (arg1, arg2);
1730 return arg1;
1731 }
1732
1733 case UNOP_POSTDECREMENT:
1734 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1735 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1736 return arg1;
1737 else if (unop_user_defined_p (op, arg1))
1738 {
1739 return value_x_unop (arg1, op, noside);
1740 }
1741 else
1742 {
c5aa993b
JM
1743 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1744 (LONGEST) 1));
c906108c
SS
1745 value_assign (arg1, arg2);
1746 return arg1;
1747 }
c5aa993b 1748
c906108c
SS
1749 case OP_THIS:
1750 (*pos) += 1;
1751 return value_of_this (1);
1752
1753 case OP_TYPE:
1754 error ("Attempt to use a type name as an expression");
1755
1756 default:
1757 /* Removing this case and compiling with gcc -Wall reveals that
c5aa993b 1758 a lot of cases are hitting this case. Some of these should
2df3850c
JM
1759 probably be removed from expression.h; others are legitimate
1760 expressions which are (apparently) not fully implemented.
c906108c 1761
c5aa993b
JM
1762 If there are any cases landing here which mean a user error,
1763 then they should be separate cases, with more descriptive
1764 error messages. */
c906108c
SS
1765
1766 error ("\
1767GDB does not (yet) know how to evaluate that kind of expression");
1768 }
1769
c5aa993b 1770nosideret:
c906108c
SS
1771 return value_from_longest (builtin_type_long, (LONGEST) 1);
1772}
1773\f
1774/* Evaluate a subexpression of EXP, at index *POS,
1775 and return the address of that subexpression.
1776 Advance *POS over the subexpression.
1777 If the subexpression isn't an lvalue, get an error.
1778 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1779 then only the type of the result need be correct. */
1780
61051030 1781static struct value *
fba45db2
KB
1782evaluate_subexp_for_address (register struct expression *exp, register int *pos,
1783 enum noside noside)
c906108c
SS
1784{
1785 enum exp_opcode op;
1786 register int pc;
1787 struct symbol *var;
1788
1789 pc = (*pos);
1790 op = exp->elts[pc].opcode;
1791
1792 switch (op)
1793 {
1794 case UNOP_IND:
1795 (*pos)++;
1796 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1797
1798 case UNOP_MEMVAL:
1799 (*pos) += 3;
1800 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1801 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1802
1803 case OP_VAR_VALUE:
1804 var = exp->elts[pc + 2].symbol;
1805
1806 /* C++: The "address" of a reference should yield the address
1807 * of the object pointed to. Let value_addr() deal with it. */
1808 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
c5aa993b 1809 goto default_case;
c906108c
SS
1810
1811 (*pos) += 4;
1812 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1813 {
1814 struct type *type =
c5aa993b 1815 lookup_pointer_type (SYMBOL_TYPE (var));
c906108c
SS
1816 enum address_class sym_class = SYMBOL_CLASS (var);
1817
1818 if (sym_class == LOC_CONST
1819 || sym_class == LOC_CONST_BYTES
1820 || sym_class == LOC_REGISTER
1821 || sym_class == LOC_REGPARM)
1822 error ("Attempt to take address of register or constant.");
1823
c5aa993b
JM
1824 return
1825 value_zero (type, not_lval);
c906108c
SS
1826 }
1827 else
1828 return
1829 locate_var_value
c5aa993b
JM
1830 (var,
1831 block_innermost_frame (exp->elts[pc + 1].block));
c906108c
SS
1832
1833 default:
1834 default_case:
1835 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1836 {
61051030 1837 struct value *x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
c906108c
SS
1838 if (VALUE_LVAL (x) == lval_memory)
1839 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
1840 not_lval);
1841 else
1842 error ("Attempt to take address of non-lval");
1843 }
1844 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1845 }
1846}
1847
1848/* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1849 When used in contexts where arrays will be coerced anyway, this is
1850 equivalent to `evaluate_subexp' but much faster because it avoids
1851 actually fetching array contents (perhaps obsolete now that we have
1852 VALUE_LAZY).
1853
1854 Note that we currently only do the coercion for C expressions, where
1855 arrays are zero based and the coercion is correct. For other languages,
1856 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1857 to decide if coercion is appropriate.
1858
c5aa993b 1859 */
c906108c 1860
61051030 1861struct value *
fba45db2
KB
1862evaluate_subexp_with_coercion (register struct expression *exp,
1863 register int *pos, enum noside noside)
c906108c
SS
1864{
1865 register enum exp_opcode op;
1866 register int pc;
61051030 1867 struct value *val;
c906108c
SS
1868 struct symbol *var;
1869
1870 pc = (*pos);
1871 op = exp->elts[pc].opcode;
1872
1873 switch (op)
1874 {
1875 case OP_VAR_VALUE:
1876 var = exp->elts[pc + 2].symbol;
1877 if (TYPE_CODE (check_typedef (SYMBOL_TYPE (var))) == TYPE_CODE_ARRAY
1878 && CAST_IS_CONVERSION)
1879 {
1880 (*pos) += 4;
1881 val =
1882 locate_var_value
c5aa993b 1883 (var, block_innermost_frame (exp->elts[pc + 1].block));
751a959b 1884 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (check_typedef (SYMBOL_TYPE (var)))),
c906108c
SS
1885 val);
1886 }
1887 /* FALLTHROUGH */
1888
1889 default:
1890 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1891 }
1892}
1893
1894/* Evaluate a subexpression of EXP, at index *POS,
1895 and return a value for the size of that subexpression.
1896 Advance *POS over the subexpression. */
1897
61051030 1898static struct value *
fba45db2 1899evaluate_subexp_for_sizeof (register struct expression *exp, register int *pos)
c906108c
SS
1900{
1901 enum exp_opcode op;
1902 register int pc;
1903 struct type *type;
61051030 1904 struct value *val;
c906108c
SS
1905
1906 pc = (*pos);
1907 op = exp->elts[pc].opcode;
1908
1909 switch (op)
1910 {
1911 /* This case is handled specially
c5aa993b
JM
1912 so that we avoid creating a value for the result type.
1913 If the result type is very big, it's desirable not to
1914 create a value unnecessarily. */
c906108c
SS
1915 case UNOP_IND:
1916 (*pos)++;
1917 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1918 type = check_typedef (VALUE_TYPE (val));
1919 if (TYPE_CODE (type) != TYPE_CODE_PTR
1920 && TYPE_CODE (type) != TYPE_CODE_REF
1921 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
1922 error ("Attempt to take contents of a non-pointer value.");
1923 type = check_typedef (TYPE_TARGET_TYPE (type));
1924 return value_from_longest (builtin_type_int, (LONGEST)
c5aa993b 1925 TYPE_LENGTH (type));
c906108c
SS
1926
1927 case UNOP_MEMVAL:
1928 (*pos) += 3;
1929 type = check_typedef (exp->elts[pc + 1].type);
1930 return value_from_longest (builtin_type_int,
1931 (LONGEST) TYPE_LENGTH (type));
1932
1933 case OP_VAR_VALUE:
1934 (*pos) += 4;
1935 type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
1936 return
1937 value_from_longest (builtin_type_int, (LONGEST) TYPE_LENGTH (type));
1938
1939 default:
1940 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1941 return value_from_longest (builtin_type_int,
c5aa993b 1942 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
c906108c
SS
1943 }
1944}
1945
1946/* Parse a type expression in the string [P..P+LENGTH). */
1947
1948struct type *
fba45db2 1949parse_and_eval_type (char *p, int length)
c906108c 1950{
c5aa993b
JM
1951 char *tmp = (char *) alloca (length + 4);
1952 struct expression *expr;
1953 tmp[0] = '(';
1954 memcpy (tmp + 1, p, length);
1955 tmp[length + 1] = ')';
1956 tmp[length + 2] = '0';
1957 tmp[length + 3] = '\0';
1958 expr = parse_expression (tmp);
1959 if (expr->elts[0].opcode != UNOP_CAST)
1960 error ("Internal error in eval_type.");
1961 return expr->elts[1].type;
c906108c
SS
1962}
1963
1964int
fba45db2 1965calc_f77_array_dims (struct type *array_type)
c906108c
SS
1966{
1967 int ndimen = 1;
1968 struct type *tmp_type;
1969
c5aa993b 1970 if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
c906108c 1971 error ("Can't get dimensions for a non-array type");
c5aa993b
JM
1972
1973 tmp_type = array_type;
c906108c
SS
1974
1975 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
1976 {
1977 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
1978 ++ndimen;
1979 }
c5aa993b 1980 return ndimen;
c906108c 1981}