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