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