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