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