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