* configure.in (host_makefile_frag): Don't set. Substitute for
[deliverable/binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
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., 675 Mass Ave, Cambridge, MA 02139, 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 /* Prototypes for local functions. */
34
35 static value_ptr evaluate_subexp_for_sizeof PARAMS ((struct expression *,
36 int *));
37
38 static value_ptr evaluate_subexp_for_address PARAMS ((struct expression *,
39 int *, enum noside));
40
41 #ifdef __GNUC__
42 inline
43 #endif
44 static value_ptr
45 evaluate_subexp (expect_type, exp, pos, noside)
46 struct type *expect_type;
47 register struct expression *exp;
48 register int *pos;
49 enum noside noside;
50 {
51 return (*exp->language_defn->evaluate_exp) (expect_type, exp, pos, noside);
52 }
53 \f
54 /* Parse the string EXP as a C expression, evaluate it,
55 and return the result as a number. */
56
57 CORE_ADDR
58 parse_and_eval_address (exp)
59 char *exp;
60 {
61 struct expression *expr = parse_expression (exp);
62 register CORE_ADDR addr;
63 register struct cleanup *old_chain =
64 make_cleanup (free_current_contents, &expr);
65
66 addr = value_as_pointer (evaluate_expression (expr));
67 do_cleanups (old_chain);
68 return addr;
69 }
70
71 /* Like parse_and_eval_address but takes a pointer to a char * variable
72 and advanced that variable across the characters parsed. */
73
74 CORE_ADDR
75 parse_and_eval_address_1 (expptr)
76 char **expptr;
77 {
78 struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0);
79 register CORE_ADDR addr;
80 register struct cleanup *old_chain =
81 make_cleanup (free_current_contents, &expr);
82
83 addr = value_as_pointer (evaluate_expression (expr));
84 do_cleanups (old_chain);
85 return addr;
86 }
87
88 value_ptr
89 parse_and_eval (exp)
90 char *exp;
91 {
92 struct expression *expr = parse_expression (exp);
93 register value_ptr val;
94 register struct cleanup *old_chain
95 = make_cleanup (free_current_contents, &expr);
96
97 val = evaluate_expression (expr);
98 do_cleanups (old_chain);
99 return val;
100 }
101
102 /* Parse up to a comma (or to a closeparen)
103 in the string EXPP as an expression, evaluate it, and return the value.
104 EXPP is advanced to point to the comma. */
105
106 value_ptr
107 parse_to_comma_and_eval (expp)
108 char **expp;
109 {
110 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
111 register value_ptr val;
112 register struct cleanup *old_chain
113 = make_cleanup (free_current_contents, &expr);
114
115 val = evaluate_expression (expr);
116 do_cleanups (old_chain);
117 return val;
118 }
119 \f
120 /* Evaluate an expression in internal prefix form
121 such as is constructed by parse.y.
122
123 See expression.h for info on the format of an expression. */
124
125 value_ptr
126 evaluate_expression (exp)
127 struct expression *exp;
128 {
129 int pc = 0;
130 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
131 }
132
133 /* Evaluate an expression, avoiding all memory references
134 and getting a value whose type alone is correct. */
135
136 value_ptr
137 evaluate_type (exp)
138 struct expression *exp;
139 {
140 int pc = 0;
141 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
142 }
143
144 /* Helper function called by evaluate_subexp to initialize a field
145 a structure from a tuple in Chill. This is recursive, to handle
146 more than one field name labels.
147
148 STRUCT_VAL is the structure value we are constructing.
149 (*FIELDNOP) is the field to set, if there is no label.
150 It is set to the field following this one.
151 EXP, POS, and NOSIDE are as for evaluate_subexp.
152
153 This function does not handle variant records. FIXME */
154
155 static value_ptr
156 evaluate_labeled_field_init (struct_val, fieldnop, exp, pos, noside)
157 value_ptr struct_val;
158 int *fieldnop;
159 register struct expression *exp;
160 register int *pos;
161 enum noside noside;
162 {
163 int fieldno = *fieldnop;
164 value_ptr val;
165 int bitpos, bitsize;
166 char *addr;
167 struct type *struct_type = VALUE_TYPE (struct_val);
168 if (exp->elts[*pos].opcode == OP_LABELED)
169 {
170 int pc = (*pos)++;
171 char *name = &exp->elts[pc + 2].string;
172 int tem = longest_to_int (exp->elts[pc + 1].longconst);
173 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
174 for (fieldno = 0; ; fieldno++)
175 {
176 if (fieldno >= TYPE_NFIELDS (struct_type))
177 error ("there is no field named %s", name);
178 if (STREQ (TYPE_FIELD_NAME (struct_type, fieldno), name))
179 break;
180 }
181 *fieldnop = fieldno;
182 val = evaluate_labeled_field_init (struct_val, fieldnop,
183 exp, pos, noside);
184 }
185 else
186 {
187 fieldno = (*fieldnop)++;
188 if (fieldno >= TYPE_NFIELDS (struct_type))
189 error ("too many initializers");
190 val = evaluate_subexp (TYPE_FIELD_TYPE (struct_type, fieldno),
191 exp, pos, noside);
192 }
193
194 /* Assign val to field fieldno. */
195 if (VALUE_TYPE (val) != TYPE_FIELD_TYPE (struct_type, fieldno))
196 val = value_cast (TYPE_FIELD_TYPE (struct_type, fieldno), val);
197 #if 1
198 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
199 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
200 addr = VALUE_CONTENTS (struct_val);
201 addr += bitpos / 8;
202 if (bitsize)
203 modify_field (addr, value_as_long (val),
204 bitpos % 8, bitsize);
205 else
206 memcpy (addr, VALUE_CONTENTS (val),
207 TYPE_LENGTH (VALUE_TYPE (val)));
208 #else
209 value_assign (value_primitive_field (struct_val, 0, fieldno, struct_type),
210 val);
211 #endif
212 return val;
213 }
214
215 value_ptr
216 evaluate_subexp_standard (expect_type, exp, pos, noside)
217 struct type *expect_type;
218 register struct expression *exp;
219 register int *pos;
220 enum noside noside;
221 {
222 enum exp_opcode op;
223 int tem, tem2, tem3;
224 register int pc, pc2 = 0, oldpos;
225 register value_ptr arg1 = NULL, arg2 = NULL, arg3;
226 struct type *type;
227 int nargs;
228 value_ptr *argvec;
229 int upper, lower, retcode;
230 int code;
231
232 /* This expect_type crap should not be used for C. C expressions do
233 not have any notion of expected types, never has and (goddess
234 willing) never will. The C++ code uses it for some twisted
235 purpose (I haven't investigated but I suspect it just the usual
236 combination of Stroustrup figuring out some crazy language
237 feature and Tiemann figuring out some crazier way to try to
238 implement it). CHILL has the tuple stuff; I don't know enough
239 about CHILL to know whether expected types is the way to do it.
240 FORTRAN I don't know. */
241 if (exp->language_defn->la_language != language_cplus
242 && exp->language_defn->la_language != language_chill)
243 expect_type = NULL_TYPE;
244
245 pc = (*pos)++;
246 op = exp->elts[pc].opcode;
247
248 switch (op)
249 {
250 case OP_SCOPE:
251 tem = longest_to_int (exp->elts[pc + 2].longconst);
252 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
253 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
254 0,
255 exp->elts[pc + 1].type,
256 &exp->elts[pc + 3].string,
257 expect_type);
258 if (arg1 == NULL)
259 error ("There is no field named %s", &exp->elts[pc + 3].string);
260 return arg1;
261
262 case OP_LONG:
263 (*pos) += 3;
264 return value_from_longest (exp->elts[pc + 1].type,
265 exp->elts[pc + 2].longconst);
266
267 case OP_DOUBLE:
268 (*pos) += 3;
269 return value_from_double (exp->elts[pc + 1].type,
270 exp->elts[pc + 2].doubleconst);
271
272 case OP_VAR_VALUE:
273 (*pos) += 3;
274 if (noside == EVAL_SKIP)
275 goto nosideret;
276 if (noside == EVAL_AVOID_SIDE_EFFECTS)
277 {
278 struct symbol * sym = exp->elts[pc + 2].symbol;
279 enum lval_type lv;
280
281 switch (SYMBOL_CLASS (sym))
282 {
283 case LOC_CONST:
284 case LOC_LABEL:
285 case LOC_CONST_BYTES:
286 lv = not_lval;
287 break;
288
289 case LOC_REGISTER:
290 case LOC_REGPARM:
291 lv = lval_register;
292 break;
293
294 default:
295 lv = lval_memory;
296 break;
297 }
298
299 return value_zero (SYMBOL_TYPE (sym), lv);
300 }
301 else
302 return value_of_variable (exp->elts[pc + 2].symbol,
303 exp->elts[pc + 1].block);
304
305 case OP_LAST:
306 (*pos) += 2;
307 return
308 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
309
310 case OP_REGISTER:
311 (*pos) += 2;
312 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
313
314 case OP_BOOL:
315 (*pos) += 2;
316 if (current_language->la_language == language_fortran)
317 return value_from_longest (builtin_type_f_logical_s2,
318 exp->elts[pc + 1].longconst);
319 else
320 return value_from_longest (builtin_type_chill_bool,
321 exp->elts[pc + 1].longconst);
322
323 case OP_INTERNALVAR:
324 (*pos) += 2;
325 return value_of_internalvar (exp->elts[pc + 1].internalvar);
326
327 case OP_STRING:
328 tem = longest_to_int (exp->elts[pc + 1].longconst);
329 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
330 if (noside == EVAL_SKIP)
331 goto nosideret;
332 return value_string (&exp->elts[pc + 2].string, tem);
333
334 case OP_BITSTRING:
335 tem = longest_to_int (exp->elts[pc + 1].longconst);
336 (*pos)
337 += 3 + BYTES_TO_EXP_ELEM ((tem + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
338 if (noside == EVAL_SKIP)
339 goto nosideret;
340 return value_bitstring (&exp->elts[pc + 2].string, tem);
341 break;
342
343 case OP_ARRAY:
344 (*pos) += 3;
345 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
346 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
347 nargs = tem3 - tem2 + 1;
348
349 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
350 && TYPE_CODE (expect_type) == TYPE_CODE_STRUCT)
351 {
352 value_ptr rec = allocate_value (expect_type);
353 int fieldno = 0;
354 memset (VALUE_CONTENTS_RAW (rec), '\0', TYPE_LENGTH (expect_type));
355 for (tem = 0; tem < nargs; tem++)
356 evaluate_labeled_field_init (rec, &fieldno, exp, pos, noside);
357 return rec;
358 }
359
360 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
361 && TYPE_CODE (expect_type) == TYPE_CODE_ARRAY)
362 {
363 struct type *range_type = TYPE_FIELD_TYPE (expect_type, 0);
364 struct type *element_type = TYPE_TARGET_TYPE (expect_type);
365 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
366 LONGEST high_bound = TYPE_FIELD_BITPOS (range_type, 1);
367 int element_size = TYPE_LENGTH (element_type);
368 value_ptr array = allocate_value (expect_type);
369 if (nargs != (high_bound - low_bound + 1))
370 error ("wrong number of initialiers for array type");
371 for (tem = low_bound; tem <= high_bound; tem++)
372 {
373 value_ptr element = evaluate_subexp (element_type,
374 exp, pos, noside);
375 if (VALUE_TYPE (element) != element_type)
376 element = value_cast (element_type, element);
377 memcpy (VALUE_CONTENTS_RAW (array)
378 + (tem - low_bound) * element_size,
379 VALUE_CONTENTS (element),
380 element_size);
381 }
382 return array;
383 }
384
385 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
386 && TYPE_CODE (expect_type) == TYPE_CODE_SET)
387 {
388 value_ptr set = allocate_value (expect_type);
389 struct type *element_type = TYPE_INDEX_TYPE (expect_type);
390 int low_bound = TYPE_LOW_BOUND (element_type);
391 int high_bound = TYPE_HIGH_BOUND (element_type);
392 char *valaddr = VALUE_CONTENTS_RAW (set);
393 memset (valaddr, '\0', TYPE_LENGTH (expect_type));
394 for (tem = 0; tem < nargs; tem++)
395 {
396 value_ptr element_val = evaluate_subexp (element_type,
397 exp, pos, noside);
398 LONGEST element = value_as_long (element_val);
399 int bit_index;
400 if (element < low_bound || element > high_bound)
401 error ("POWERSET tuple element out of range");
402 element -= low_bound;
403 bit_index = (unsigned) element % TARGET_CHAR_BIT;
404 if (BITS_BIG_ENDIAN)
405 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
406 valaddr [(unsigned) element / TARGET_CHAR_BIT] |= 1 << bit_index;
407 }
408 return set;
409 }
410
411 argvec = (value_ptr *) alloca (sizeof (value_ptr) * nargs);
412 for (tem = 0; tem < nargs; tem++)
413 {
414 /* Ensure that array expressions are coerced into pointer objects. */
415 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
416 }
417 if (noside == EVAL_SKIP)
418 goto nosideret;
419 return value_array (tem2, tem3, argvec);
420
421 case TERNOP_SLICE:
422 {
423 value_ptr array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
424 int lowbound
425 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
426 int upper
427 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
428 return value_slice (array, lowbound, upper - lowbound + 1);
429 }
430
431 case TERNOP_SLICE_COUNT:
432 {
433 value_ptr array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
434 int lowbound
435 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
436 int length
437 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
438 return value_slice (array, lowbound, length);
439 }
440
441 case TERNOP_COND:
442 /* Skip third and second args to evaluate the first one. */
443 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
444 if (value_logical_not (arg1))
445 {
446 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
447 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
448 }
449 else
450 {
451 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
452 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
453 return arg2;
454 }
455
456 case OP_FUNCALL:
457 (*pos) += 2;
458 op = exp->elts[*pos].opcode;
459 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
460 {
461 LONGEST fnptr;
462
463 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
464 /* First, evaluate the structure into arg2 */
465 pc2 = (*pos)++;
466
467 if (noside == EVAL_SKIP)
468 goto nosideret;
469
470 if (op == STRUCTOP_MEMBER)
471 {
472 arg2 = evaluate_subexp_for_address (exp, pos, noside);
473 }
474 else
475 {
476 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
477 }
478
479 /* If the function is a virtual function, then the
480 aggregate value (providing the structure) plays
481 its part by providing the vtable. Otherwise,
482 it is just along for the ride: call the function
483 directly. */
484
485 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
486
487 fnptr = value_as_long (arg1);
488
489 if (METHOD_PTR_IS_VIRTUAL(fnptr))
490 {
491 int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
492 struct type *basetype;
493 struct type *domain_type =
494 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
495 int i, j;
496 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
497 if (domain_type != basetype)
498 arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
499 basetype = TYPE_VPTR_BASETYPE (domain_type);
500 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
501 {
502 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
503 /* If one is virtual, then all are virtual. */
504 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
505 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
506 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
507 {
508 value_ptr temp = value_ind (arg2);
509 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
510 arg2 = value_addr (temp);
511 goto got_it;
512 }
513 }
514 if (i < 0)
515 error ("virtual function at index %d not found", fnoffset);
516 }
517 else
518 {
519 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
520 }
521 got_it:
522
523 /* Now, say which argument to start evaluating from */
524 tem = 2;
525 }
526 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
527 {
528 /* Hair for method invocations */
529 int tem2;
530
531 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
532 /* First, evaluate the structure into arg2 */
533 pc2 = (*pos)++;
534 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
535 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
536 if (noside == EVAL_SKIP)
537 goto nosideret;
538
539 if (op == STRUCTOP_STRUCT)
540 {
541 /* If v is a variable in a register, and the user types
542 v.method (), this will produce an error, because v has
543 no address.
544
545 A possible way around this would be to allocate a
546 copy of the variable on the stack, copy in the
547 contents, call the function, and copy out the
548 contents. I.e. convert this from call by reference
549 to call by copy-return (or whatever it's called).
550 However, this does not work because it is not the
551 same: the method being called could stash a copy of
552 the address, and then future uses through that address
553 (after the method returns) would be expected to
554 use the variable itself, not some copy of it. */
555 arg2 = evaluate_subexp_for_address (exp, pos, noside);
556 }
557 else
558 {
559 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
560 }
561 /* Now, say which argument to start evaluating from */
562 tem = 2;
563 }
564 else
565 {
566 nargs = longest_to_int (exp->elts[pc + 1].longconst);
567 tem = 0;
568 }
569 /* Allocate arg vector, including space for the function to be
570 called in argvec[0] and a terminating NULL */
571 argvec = (value_ptr *) alloca (sizeof (value_ptr) * (nargs + 2));
572 for (; tem <= nargs; tem++)
573 /* Ensure that array expressions are coerced into pointer objects. */
574 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
575
576 /* signal end of arglist */
577 argvec[tem] = 0;
578
579 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
580 {
581 int static_memfuncp;
582 value_ptr temp = arg2;
583 char tstr[64];
584
585 argvec[1] = arg2;
586 argvec[0] = 0;
587 strcpy(tstr, &exp->elts[pc2+2].string);
588 if (!argvec[0])
589 {
590 temp = arg2;
591 argvec[0] =
592 value_struct_elt (&temp, argvec+1, tstr,
593 &static_memfuncp,
594 op == STRUCTOP_STRUCT
595 ? "structure" : "structure pointer");
596 }
597 arg2 = value_from_longest (lookup_pointer_type(VALUE_TYPE (temp)),
598 VALUE_ADDRESS (temp)+VALUE_OFFSET (temp));
599 argvec[1] = arg2;
600
601 if (static_memfuncp)
602 {
603 argvec[1] = argvec[0];
604 nargs--;
605 argvec++;
606 }
607 }
608 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
609 {
610 argvec[1] = arg2;
611 argvec[0] = arg1;
612 }
613
614 do_call_it:
615
616 if (noside == EVAL_SKIP)
617 goto nosideret;
618 if (noside == EVAL_AVOID_SIDE_EFFECTS)
619 {
620 /* If the return type doesn't look like a function type, call an
621 error. This can happen if somebody tries to turn a variable into
622 a function call. This is here because people often want to
623 call, eg, strcmp, which gdb doesn't know is a function. If
624 gdb isn't asked for it's opinion (ie. through "whatis"),
625 it won't offer it. */
626
627 struct type *ftype =
628 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
629
630 if (ftype)
631 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
632 else
633 error ("Expression of type other than \"Function returning ...\" used as function");
634 }
635 return call_function_by_hand (argvec[0], nargs, argvec + 1);
636
637 case OP_F77_UNDETERMINED_ARGLIST:
638
639 /* Remember that in F77, functions, substring ops and
640 array subscript operations cannot be disambiguated
641 at parse time. We have made all array subscript operations,
642 substring operations as well as function calls come here
643 and we now have to discover what the heck this thing actually was.
644 If it is a function, we process just as if we got an OP_FUNCALL. */
645
646 nargs = longest_to_int (exp->elts[pc+1].longconst);
647 (*pos) += 2;
648
649 /* First determine the type code we are dealing with. */
650 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
651 code = TYPE_CODE (VALUE_TYPE (arg1));
652
653 switch (code)
654 {
655 case TYPE_CODE_ARRAY:
656 goto multi_f77_subscript;
657
658 case TYPE_CODE_STRING:
659 goto op_f77_substr;
660
661 case TYPE_CODE_PTR:
662 case TYPE_CODE_FUNC:
663 /* It's a function call. */
664 /* Allocate arg vector, including space for the function to be
665 called in argvec[0] and a terminating NULL */
666 argvec = (value_ptr *) alloca (sizeof (value_ptr) * (nargs + 2));
667 argvec[0] = arg1;
668 tem = 1;
669 for (; tem <= nargs; tem++)
670 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
671 argvec[tem] = 0; /* signal end of arglist */
672 goto do_call_it;
673
674 default:
675 error ("Cannot perform substring on this type");
676 }
677
678 op_f77_substr:
679 /* We have a substring operation on our hands here,
680 let us get the string we will be dealing with */
681
682 /* Now evaluate the 'from' and 'to' */
683
684 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
685
686 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
687 error ("Substring arguments must be of type integer");
688
689 if (nargs < 2)
690 return value_subscript (arg1, arg2);
691
692 arg3 = evaluate_subexp_with_coercion (exp, pos, noside);
693
694 if (TYPE_CODE (VALUE_TYPE (arg3)) != TYPE_CODE_INT)
695 error ("Substring arguments must be of type integer");
696
697 tem2 = *((int *) VALUE_CONTENTS_RAW (arg2));
698 tem3 = *((int *) VALUE_CONTENTS_RAW (arg3));
699
700 if ((tem2 < 1) || (tem2 > tem3))
701 error ("Bad 'from' value %d on substring operation", tem2);
702
703 if ((tem3 < tem2) || (tem3 > (TYPE_LENGTH (VALUE_TYPE (arg1)))))
704 error ("Bad 'to' value %d on substring operation", tem3);
705
706 if (noside == EVAL_SKIP)
707 goto nosideret;
708
709 return value_slice (arg1, tem2, tem3 - tem2 + 1);
710
711 case OP_COMPLEX:
712 /* We have a complex number, There should be 2 floating
713 point numbers that compose it */
714 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
715 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
716
717 return value_literal_complex (arg1, arg2, builtin_type_f_complex_s16);
718
719 case STRUCTOP_STRUCT:
720 tem = longest_to_int (exp->elts[pc + 1].longconst);
721 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
722 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
723 if (noside == EVAL_SKIP)
724 goto nosideret;
725 if (noside == EVAL_AVOID_SIDE_EFFECTS)
726 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
727 &exp->elts[pc + 2].string,
728 0),
729 lval_memory);
730 else
731 {
732 value_ptr temp = arg1;
733 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
734 NULL, "structure");
735 }
736
737 case STRUCTOP_PTR:
738 tem = longest_to_int (exp->elts[pc + 1].longconst);
739 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
740 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
741 if (noside == EVAL_SKIP)
742 goto nosideret;
743 if (noside == EVAL_AVOID_SIDE_EFFECTS)
744 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
745 &exp->elts[pc + 2].string,
746 0),
747 lval_memory);
748 else
749 {
750 value_ptr temp = arg1;
751 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
752 NULL, "structure pointer");
753 }
754
755 case STRUCTOP_MEMBER:
756 arg1 = evaluate_subexp_for_address (exp, pos, noside);
757 goto handle_pointer_to_member;
758 case STRUCTOP_MPTR:
759 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
760 handle_pointer_to_member:
761 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
762 if (noside == EVAL_SKIP)
763 goto nosideret;
764 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR)
765 goto bad_pointer_to_member;
766 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
767 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
768 error ("not implemented: pointer-to-method in pointer-to-member construct");
769 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
770 goto bad_pointer_to_member;
771 /* Now, convert these values to an address. */
772 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
773 arg1);
774 arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
775 value_as_long (arg1) + value_as_long (arg2));
776 return value_ind (arg3);
777 bad_pointer_to_member:
778 error("non-pointer-to-member value used in pointer-to-member construct");
779
780 case BINOP_CONCAT:
781 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
782 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
783 if (noside == EVAL_SKIP)
784 goto nosideret;
785 if (binop_user_defined_p (op, arg1, arg2))
786 return value_x_binop (arg1, arg2, op, OP_NULL);
787 else
788 return value_concat (arg1, arg2);
789
790 case BINOP_ASSIGN:
791 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
792 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
793 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
794 return arg1;
795 if (binop_user_defined_p (op, arg1, arg2))
796 return value_x_binop (arg1, arg2, op, OP_NULL);
797 else
798 return value_assign (arg1, arg2);
799
800 case BINOP_ASSIGN_MODIFY:
801 (*pos) += 2;
802 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
803 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
804 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
805 return arg1;
806 op = exp->elts[pc + 1].opcode;
807 if (binop_user_defined_p (op, arg1, arg2))
808 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
809 else if (op == BINOP_ADD)
810 arg2 = value_add (arg1, arg2);
811 else if (op == BINOP_SUB)
812 arg2 = value_sub (arg1, arg2);
813 else
814 arg2 = value_binop (arg1, arg2, op);
815 return value_assign (arg1, arg2);
816
817 case BINOP_ADD:
818 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
819 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
820 if (noside == EVAL_SKIP)
821 goto nosideret;
822 if (binop_user_defined_p (op, arg1, arg2))
823 return value_x_binop (arg1, arg2, op, OP_NULL);
824 else
825 return value_add (arg1, arg2);
826
827 case BINOP_SUB:
828 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
829 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
830 if (noside == EVAL_SKIP)
831 goto nosideret;
832 if (binop_user_defined_p (op, arg1, arg2))
833 return value_x_binop (arg1, arg2, op, OP_NULL);
834 else
835 return value_sub (arg1, arg2);
836
837 case BINOP_MUL:
838 case BINOP_DIV:
839 case BINOP_REM:
840 case BINOP_MOD:
841 case BINOP_LSH:
842 case BINOP_RSH:
843 case BINOP_BITWISE_AND:
844 case BINOP_BITWISE_IOR:
845 case BINOP_BITWISE_XOR:
846 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
847 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
848 if (noside == EVAL_SKIP)
849 goto nosideret;
850 if (binop_user_defined_p (op, arg1, arg2))
851 return value_x_binop (arg1, arg2, op, OP_NULL);
852 else
853 if (noside == EVAL_AVOID_SIDE_EFFECTS
854 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
855 return value_zero (VALUE_TYPE (arg1), not_lval);
856 else
857 return value_binop (arg1, arg2, op);
858
859 case BINOP_SUBSCRIPT:
860 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
861 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
862 if (noside == EVAL_SKIP)
863 goto nosideret;
864 if (noside == EVAL_AVOID_SIDE_EFFECTS)
865 {
866 /* If the user attempts to subscript something that has no target
867 type (like a plain int variable for example), then report this
868 as an error. */
869
870 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
871 if (type)
872 return value_zero (type, VALUE_LVAL (arg1));
873 else
874 error ("cannot subscript something of type `%s'",
875 TYPE_NAME (VALUE_TYPE (arg1)));
876 }
877
878 if (binop_user_defined_p (op, arg1, arg2))
879 return value_x_binop (arg1, arg2, op, OP_NULL);
880 else
881 return value_subscript (arg1, arg2);
882
883 case BINOP_IN:
884 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
885 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
886 if (noside == EVAL_SKIP)
887 goto nosideret;
888 return value_in (arg1, arg2);
889
890 case MULTI_SUBSCRIPT:
891 (*pos) += 2;
892 nargs = longest_to_int (exp->elts[pc + 1].longconst);
893 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
894 while (nargs-- > 0)
895 {
896 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
897 /* FIXME: EVAL_SKIP handling may not be correct. */
898 if (noside == EVAL_SKIP)
899 {
900 if (nargs > 0)
901 {
902 continue;
903 }
904 else
905 {
906 goto nosideret;
907 }
908 }
909 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
910 if (noside == EVAL_AVOID_SIDE_EFFECTS)
911 {
912 /* If the user attempts to subscript something that has no target
913 type (like a plain int variable for example), then report this
914 as an error. */
915
916 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
917 if (type != NULL)
918 {
919 arg1 = value_zero (type, VALUE_LVAL (arg1));
920 noside = EVAL_SKIP;
921 continue;
922 }
923 else
924 {
925 error ("cannot subscript something of type `%s'",
926 TYPE_NAME (VALUE_TYPE (arg1)));
927 }
928 }
929
930 if (binop_user_defined_p (op, arg1, arg2))
931 {
932 arg1 = value_x_binop (arg1, arg2, op, OP_NULL);
933 }
934 else
935 {
936 arg1 = value_subscript (arg1, arg2);
937 }
938 }
939 return (arg1);
940
941 multi_f77_subscript:
942 {
943 int subscript_array[MAX_FORTRAN_DIMS+1]; /* 1-based array of
944 subscripts, max == 7 */
945 int array_size_array[MAX_FORTRAN_DIMS+1];
946 int ndimensions=1,i;
947 struct type *tmp_type;
948 int offset_item; /* The array offset where the item lives */
949
950 if (nargs > MAX_FORTRAN_DIMS)
951 error ("Too many subscripts for F77 (%d Max)", MAX_FORTRAN_DIMS);
952
953 ndimensions = calc_f77_array_dims (VALUE_TYPE (arg1));
954
955 if (nargs != ndimensions)
956 error ("Wrong number of subscripts");
957
958 /* Now that we know we have a legal array subscript expression
959 let us actually find out where this element exists in the array. */
960
961 tmp_type = VALUE_TYPE (arg1);
962 offset_item = 0;
963 for (i = 1; i <= nargs; i++)
964 {
965 /* Evaluate each subscript, It must be a legal integer in F77 */
966 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
967
968 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
969 error ("Array subscripts must be of type integer");
970
971 /* Fill in the subscript and array size arrays */
972
973 subscript_array[i] = (* (unsigned int *) VALUE_CONTENTS(arg2));
974
975 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
976 if (retcode == BOUND_FETCH_ERROR)
977 error ("Cannot obtain dynamic upper bound");
978
979 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
980 if (retcode == BOUND_FETCH_ERROR)
981 error("Cannot obtain dynamic lower bound");
982
983 array_size_array[i] = upper - lower + 1;
984
985 /* Zero-normalize subscripts so that offsetting will work. */
986
987 subscript_array[i] -= lower;
988
989 /* If we are at the bottom of a multidimensional
990 array type then keep a ptr to the last ARRAY
991 type around for use when calling value_subscript()
992 below. This is done because we pretend to value_subscript
993 that we actually have a one-dimensional array
994 of base element type that we apply a simple
995 offset to. */
996
997 if (i < nargs)
998 tmp_type = TYPE_TARGET_TYPE (tmp_type);
999 }
1000
1001 /* Now let us calculate the offset for this item */
1002
1003 offset_item = subscript_array[ndimensions];
1004
1005 for (i = ndimensions - 1; i >= 1; i--)
1006 offset_item =
1007 array_size_array[i] * offset_item + subscript_array[i];
1008
1009 /* Construct a value node with the value of the offset */
1010
1011 arg2 = value_from_longest (builtin_type_f_integer, offset_item);
1012
1013 /* Let us now play a dirty trick: we will take arg1
1014 which is a value node pointing to the topmost level
1015 of the multidimensional array-set and pretend
1016 that it is actually a array of the final element
1017 type, this will ensure that value_subscript()
1018 returns the correct type value */
1019
1020 VALUE_TYPE (arg1) = tmp_type;
1021 return value_ind (value_add (value_coerce_array (arg1), arg2));
1022 }
1023
1024 case BINOP_LOGICAL_AND:
1025 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1026 if (noside == EVAL_SKIP)
1027 {
1028 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1029 goto nosideret;
1030 }
1031
1032 oldpos = *pos;
1033 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1034 *pos = oldpos;
1035
1036 if (binop_user_defined_p (op, arg1, arg2))
1037 {
1038 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1039 return value_x_binop (arg1, arg2, op, OP_NULL);
1040 }
1041 else
1042 {
1043 tem = value_logical_not (arg1);
1044 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1045 (tem ? EVAL_SKIP : noside));
1046 return value_from_longest (builtin_type_int,
1047 (LONGEST) (!tem && !value_logical_not (arg2)));
1048 }
1049
1050 case BINOP_LOGICAL_OR:
1051 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1052 if (noside == EVAL_SKIP)
1053 {
1054 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1055 goto nosideret;
1056 }
1057
1058 oldpos = *pos;
1059 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1060 *pos = oldpos;
1061
1062 if (binop_user_defined_p (op, arg1, arg2))
1063 {
1064 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1065 return value_x_binop (arg1, arg2, op, OP_NULL);
1066 }
1067 else
1068 {
1069 tem = value_logical_not (arg1);
1070 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1071 (!tem ? EVAL_SKIP : noside));
1072 return value_from_longest (builtin_type_int,
1073 (LONGEST) (!tem || !value_logical_not (arg2)));
1074 }
1075
1076 case BINOP_EQUAL:
1077 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1078 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1079 if (noside == EVAL_SKIP)
1080 goto nosideret;
1081 if (binop_user_defined_p (op, arg1, arg2))
1082 {
1083 return value_x_binop (arg1, arg2, op, OP_NULL);
1084 }
1085 else
1086 {
1087 tem = value_equal (arg1, arg2);
1088 return value_from_longest (builtin_type_int, (LONGEST) tem);
1089 }
1090
1091 case BINOP_NOTEQUAL:
1092 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1093 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1094 if (noside == EVAL_SKIP)
1095 goto nosideret;
1096 if (binop_user_defined_p (op, arg1, arg2))
1097 {
1098 return value_x_binop (arg1, arg2, op, OP_NULL);
1099 }
1100 else
1101 {
1102 tem = value_equal (arg1, arg2);
1103 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
1104 }
1105
1106 case BINOP_LESS:
1107 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1108 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1109 if (noside == EVAL_SKIP)
1110 goto nosideret;
1111 if (binop_user_defined_p (op, arg1, arg2))
1112 {
1113 return value_x_binop (arg1, arg2, op, OP_NULL);
1114 }
1115 else
1116 {
1117 tem = value_less (arg1, arg2);
1118 return value_from_longest (builtin_type_int, (LONGEST) tem);
1119 }
1120
1121 case BINOP_GTR:
1122 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1123 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1124 if (noside == EVAL_SKIP)
1125 goto nosideret;
1126 if (binop_user_defined_p (op, arg1, arg2))
1127 {
1128 return value_x_binop (arg1, arg2, op, OP_NULL);
1129 }
1130 else
1131 {
1132 tem = value_less (arg2, arg1);
1133 return value_from_longest (builtin_type_int, (LONGEST) tem);
1134 }
1135
1136 case BINOP_GEQ:
1137 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1138 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1139 if (noside == EVAL_SKIP)
1140 goto nosideret;
1141 if (binop_user_defined_p (op, arg1, arg2))
1142 {
1143 return value_x_binop (arg1, arg2, op, OP_NULL);
1144 }
1145 else
1146 {
1147 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1148 return value_from_longest (builtin_type_int, (LONGEST) tem);
1149 }
1150
1151 case BINOP_LEQ:
1152 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1153 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1154 if (noside == EVAL_SKIP)
1155 goto nosideret;
1156 if (binop_user_defined_p (op, arg1, arg2))
1157 {
1158 return value_x_binop (arg1, arg2, op, OP_NULL);
1159 }
1160 else
1161 {
1162 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1163 return value_from_longest (builtin_type_int, (LONGEST) tem);
1164 }
1165
1166 case BINOP_REPEAT:
1167 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1168 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1169 if (noside == EVAL_SKIP)
1170 goto nosideret;
1171 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
1172 error ("Non-integral right operand for \"@\" operator.");
1173 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1174 {
1175 if (VALUE_REPEATED (arg1))
1176 error ("Cannot create artificial arrays of artificial arrays.");
1177 return allocate_repeat_value (VALUE_TYPE (arg1),
1178 longest_to_int (value_as_long (arg2)));
1179 }
1180 else
1181 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1182
1183 case BINOP_COMMA:
1184 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1185 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1186
1187 case UNOP_NEG:
1188 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1189 if (noside == EVAL_SKIP)
1190 goto nosideret;
1191 if (unop_user_defined_p (op, arg1))
1192 return value_x_unop (arg1, op);
1193 else
1194 return value_neg (arg1);
1195
1196 case UNOP_COMPLEMENT:
1197 /* C++: check for and handle destructor names. */
1198 op = exp->elts[*pos].opcode;
1199
1200 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1201 if (noside == EVAL_SKIP)
1202 goto nosideret;
1203 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1204 return value_x_unop (arg1, UNOP_COMPLEMENT);
1205 else
1206 return value_complement (arg1);
1207
1208 case UNOP_LOGICAL_NOT:
1209 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1210 if (noside == EVAL_SKIP)
1211 goto nosideret;
1212 if (unop_user_defined_p (op, arg1))
1213 return value_x_unop (arg1, op);
1214 else
1215 return value_from_longest (builtin_type_int,
1216 (LONGEST) value_logical_not (arg1));
1217
1218 case UNOP_IND:
1219 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
1220 expect_type = TYPE_TARGET_TYPE (expect_type);
1221 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1222 if (noside == EVAL_SKIP)
1223 goto nosideret;
1224 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1225 {
1226 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
1227 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
1228 /* In C you can dereference an array to get the 1st elt. */
1229 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
1230 )
1231 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
1232 lval_memory);
1233 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
1234 /* GDB allows dereferencing an int. */
1235 return value_zero (builtin_type_int, lval_memory);
1236 else
1237 error ("Attempt to take contents of a non-pointer value.");
1238 }
1239 return value_ind (arg1);
1240
1241 case UNOP_ADDR:
1242 /* C++: check for and handle pointer to members. */
1243
1244 op = exp->elts[*pos].opcode;
1245
1246 if (noside == EVAL_SKIP)
1247 {
1248 if (op == OP_SCOPE)
1249 {
1250 int temm = longest_to_int (exp->elts[pc+3].longconst);
1251 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
1252 }
1253 else
1254 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
1255 goto nosideret;
1256 }
1257
1258 return evaluate_subexp_for_address (exp, pos, noside);
1259
1260 case UNOP_SIZEOF:
1261 if (noside == EVAL_SKIP)
1262 {
1263 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1264 goto nosideret;
1265 }
1266 return evaluate_subexp_for_sizeof (exp, pos);
1267
1268 case UNOP_CAST:
1269 (*pos) += 2;
1270 type = exp->elts[pc + 1].type;
1271 arg1 = evaluate_subexp (type, exp, pos, noside);
1272 if (noside == EVAL_SKIP)
1273 goto nosideret;
1274 if (type != VALUE_TYPE (arg1))
1275 arg1 = value_cast (type, arg1);
1276 return arg1;
1277
1278 case UNOP_MEMVAL:
1279 (*pos) += 2;
1280 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1281 if (noside == EVAL_SKIP)
1282 goto nosideret;
1283 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1284 return value_zero (exp->elts[pc + 1].type, lval_memory);
1285 else
1286 return value_at_lazy (exp->elts[pc + 1].type,
1287 value_as_pointer (arg1));
1288
1289 case UNOP_PREINCREMENT:
1290 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1291 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1292 return arg1;
1293 else if (unop_user_defined_p (op, arg1))
1294 {
1295 return value_x_unop (arg1, op);
1296 }
1297 else
1298 {
1299 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1300 (LONGEST) 1));
1301 return value_assign (arg1, arg2);
1302 }
1303
1304 case UNOP_PREDECREMENT:
1305 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1306 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1307 return arg1;
1308 else if (unop_user_defined_p (op, arg1))
1309 {
1310 return value_x_unop (arg1, op);
1311 }
1312 else
1313 {
1314 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1315 (LONGEST) 1));
1316 return value_assign (arg1, arg2);
1317 }
1318
1319 case UNOP_POSTINCREMENT:
1320 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1321 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1322 return arg1;
1323 else if (unop_user_defined_p (op, arg1))
1324 {
1325 return value_x_unop (arg1, op);
1326 }
1327 else
1328 {
1329 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1330 (LONGEST) 1));
1331 value_assign (arg1, arg2);
1332 return arg1;
1333 }
1334
1335 case UNOP_POSTDECREMENT:
1336 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1337 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1338 return arg1;
1339 else if (unop_user_defined_p (op, arg1))
1340 {
1341 return value_x_unop (arg1, op);
1342 }
1343 else
1344 {
1345 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1346 (LONGEST) 1));
1347 value_assign (arg1, arg2);
1348 return arg1;
1349 }
1350
1351 case OP_THIS:
1352 (*pos) += 1;
1353 return value_of_this (1);
1354
1355 case OP_TYPE:
1356 error ("Attempt to use a type name as an expression");
1357
1358 default:
1359 /* Removing this case and compiling with gcc -Wall reveals that
1360 a lot of cases are hitting this case. Some of these should
1361 probably be removed from expression.h (e.g. do we need a BINOP_SCOPE
1362 and an OP_SCOPE?); others are legitimate expressions which are
1363 (apparently) not fully implemented.
1364
1365 If there are any cases landing here which mean a user error,
1366 then they should be separate cases, with more descriptive
1367 error messages. */
1368
1369 error ("\
1370 GDB does not (yet) know how to evaluate that kind of expression");
1371 }
1372
1373 nosideret:
1374 return value_from_longest (builtin_type_long, (LONGEST) 1);
1375 }
1376 \f
1377 /* Evaluate a subexpression of EXP, at index *POS,
1378 and return the address of that subexpression.
1379 Advance *POS over the subexpression.
1380 If the subexpression isn't an lvalue, get an error.
1381 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1382 then only the type of the result need be correct. */
1383
1384 static value_ptr
1385 evaluate_subexp_for_address (exp, pos, noside)
1386 register struct expression *exp;
1387 register int *pos;
1388 enum noside noside;
1389 {
1390 enum exp_opcode op;
1391 register int pc;
1392 struct symbol *var;
1393
1394 pc = (*pos);
1395 op = exp->elts[pc].opcode;
1396
1397 switch (op)
1398 {
1399 case UNOP_IND:
1400 (*pos)++;
1401 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1402
1403 case UNOP_MEMVAL:
1404 (*pos) += 3;
1405 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1406 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1407
1408 case OP_VAR_VALUE:
1409 var = exp->elts[pc + 2].symbol;
1410
1411 /* C++: The "address" of a reference should yield the address
1412 * of the object pointed to. Let value_addr() deal with it. */
1413 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
1414 goto default_case;
1415
1416 (*pos) += 4;
1417 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1418 {
1419 struct type *type =
1420 lookup_pointer_type (SYMBOL_TYPE (var));
1421 enum address_class sym_class = SYMBOL_CLASS (var);
1422
1423 if (sym_class == LOC_CONST
1424 || sym_class == LOC_CONST_BYTES
1425 || sym_class == LOC_REGISTER
1426 || sym_class == LOC_REGPARM)
1427 error ("Attempt to take address of register or constant.");
1428
1429 return
1430 value_zero (type, not_lval);
1431 }
1432 else
1433 return
1434 locate_var_value
1435 (var,
1436 block_innermost_frame (exp->elts[pc + 1].block));
1437
1438 default:
1439 default_case:
1440 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1441 {
1442 value_ptr x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1443 if (VALUE_LVAL (x) == lval_memory)
1444 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
1445 not_lval);
1446 else
1447 error ("Attempt to take address of non-lval");
1448 }
1449 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1450 }
1451 }
1452
1453 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1454 When used in contexts where arrays will be coerced anyway, this is
1455 equivalent to `evaluate_subexp' but much faster because it avoids
1456 actually fetching array contents (perhaps obsolete now that we have
1457 VALUE_LAZY).
1458
1459 Note that we currently only do the coercion for C expressions, where
1460 arrays are zero based and the coercion is correct. For other languages,
1461 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1462 to decide if coercion is appropriate.
1463
1464 */
1465
1466 value_ptr
1467 evaluate_subexp_with_coercion (exp, pos, noside)
1468 register struct expression *exp;
1469 register int *pos;
1470 enum noside noside;
1471 {
1472 register enum exp_opcode op;
1473 register int pc;
1474 register value_ptr val;
1475 struct symbol *var;
1476
1477 pc = (*pos);
1478 op = exp->elts[pc].opcode;
1479
1480 switch (op)
1481 {
1482 case OP_VAR_VALUE:
1483 var = exp->elts[pc + 2].symbol;
1484 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY
1485 && CAST_IS_CONVERSION)
1486 {
1487 (*pos) += 4;
1488 val =
1489 locate_var_value
1490 (var, block_innermost_frame (exp->elts[pc + 1].block));
1491 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
1492 val);
1493 }
1494 /* FALLTHROUGH */
1495
1496 default:
1497 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1498 }
1499 }
1500
1501 /* Evaluate a subexpression of EXP, at index *POS,
1502 and return a value for the size of that subexpression.
1503 Advance *POS over the subexpression. */
1504
1505 static value_ptr
1506 evaluate_subexp_for_sizeof (exp, pos)
1507 register struct expression *exp;
1508 register int *pos;
1509 {
1510 enum exp_opcode op;
1511 register int pc;
1512 value_ptr val;
1513
1514 pc = (*pos);
1515 op = exp->elts[pc].opcode;
1516
1517 switch (op)
1518 {
1519 /* This case is handled specially
1520 so that we avoid creating a value for the result type.
1521 If the result type is very big, it's desirable not to
1522 create a value unnecessarily. */
1523 case UNOP_IND:
1524 (*pos)++;
1525 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1526 return value_from_longest (builtin_type_int, (LONGEST)
1527 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1528
1529 case UNOP_MEMVAL:
1530 (*pos) += 3;
1531 return value_from_longest (builtin_type_int,
1532 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1533
1534 case OP_VAR_VALUE:
1535 (*pos) += 4;
1536 return
1537 value_from_longest
1538 (builtin_type_int,
1539 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 2].symbol)));
1540
1541 default:
1542 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1543 return value_from_longest (builtin_type_int,
1544 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1545 }
1546 }
1547
1548 /* Parse a type expression in the string [P..P+LENGTH). */
1549
1550 struct type *
1551 parse_and_eval_type (p, length)
1552 char *p;
1553 int length;
1554 {
1555 char *tmp = (char *)alloca (length + 4);
1556 struct expression *expr;
1557 tmp[0] = '(';
1558 memcpy (tmp+1, p, length);
1559 tmp[length+1] = ')';
1560 tmp[length+2] = '0';
1561 tmp[length+3] = '\0';
1562 expr = parse_expression (tmp);
1563 if (expr->elts[0].opcode != UNOP_CAST)
1564 error ("Internal error in eval_type.");
1565 return expr->elts[1].type;
1566 }
1567
1568 int
1569 calc_f77_array_dims (array_type)
1570 struct type *array_type;
1571 {
1572 int ndimen = 1;
1573 struct type *tmp_type;
1574
1575 if ((TYPE_CODE(array_type) != TYPE_CODE_ARRAY))
1576 error ("Can't get dimensions for a non-array type");
1577
1578 tmp_type = array_type;
1579
1580 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
1581 {
1582 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
1583 ++ndimen;
1584 }
1585 return ndimen;
1586 }
This page took 0.06241 seconds and 4 git commands to generate.