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